linux/drivers/remoteproc/remoteproc_core.c
<<
>>
Prefs
   1/*
   2 * Remote Processor Framework
   3 *
   4 * Copyright (C) 2011 Texas Instruments, Inc.
   5 * Copyright (C) 2011 Google, Inc.
   6 *
   7 * Ohad Ben-Cohen <ohad@wizery.com>
   8 * Brian Swetland <swetland@google.com>
   9 * Mark Grosen <mgrosen@ti.com>
  10 * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11 * Suman Anna <s-anna@ti.com>
  12 * Robert Tivy <rtivy@ti.com>
  13 * Armando Uribe De Leon <x0095078@ti.com>
  14 *
  15 * This program is free software; you can redistribute it and/or
  16 * modify it under the terms of the GNU General Public License
  17 * version 2 as published by the Free Software Foundation.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 * GNU General Public License for more details.
  23 */
  24
  25#define pr_fmt(fmt)    "%s: " fmt, __func__
  26
  27#include <linux/kernel.h>
  28#include <linux/module.h>
  29#include <linux/device.h>
  30#include <linux/slab.h>
  31#include <linux/mutex.h>
  32#include <linux/dma-mapping.h>
  33#include <linux/firmware.h>
  34#include <linux/string.h>
  35#include <linux/debugfs.h>
  36#include <linux/remoteproc.h>
  37#include <linux/iommu.h>
  38#include <linux/idr.h>
  39#include <linux/elf.h>
  40#include <linux/virtio_ids.h>
  41#include <linux/virtio_ring.h>
  42#include <asm/byteorder.h>
  43
  44#include "remoteproc_internal.h"
  45
  46typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
  47                                struct resource_table *table, int len);
  48typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
  49
  50/* Unique indices for remoteproc devices */
  51static DEFINE_IDA(rproc_dev_index);
  52
  53static const char * const rproc_crash_names[] = {
  54        [RPROC_MMUFAULT]        = "mmufault",
  55};
  56
  57/* translate rproc_crash_type to string */
  58static const char *rproc_crash_to_string(enum rproc_crash_type type)
  59{
  60        if (type < ARRAY_SIZE(rproc_crash_names))
  61                return rproc_crash_names[type];
  62        return "unkown";
  63}
  64
  65/*
  66 * This is the IOMMU fault handler we register with the IOMMU API
  67 * (when relevant; not all remote processors access memory through
  68 * an IOMMU).
  69 *
  70 * IOMMU core will invoke this handler whenever the remote processor
  71 * will try to access an unmapped device address.
  72 */
  73static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
  74                unsigned long iova, int flags, void *token)
  75{
  76        struct rproc *rproc = token;
  77
  78        dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
  79
  80        rproc_report_crash(rproc, RPROC_MMUFAULT);
  81
  82        /*
  83         * Let the iommu core know we're not really handling this fault;
  84         * we just used it as a recovery trigger.
  85         */
  86        return -ENOSYS;
  87}
  88
  89static int rproc_enable_iommu(struct rproc *rproc)
  90{
  91        struct iommu_domain *domain;
  92        struct device *dev = rproc->dev.parent;
  93        int ret;
  94
  95        /*
  96         * We currently use iommu_present() to decide if an IOMMU
  97         * setup is needed.
  98         *
  99         * This works for simple cases, but will easily fail with
 100         * platforms that do have an IOMMU, but not for this specific
 101         * rproc.
 102         *
 103         * This will be easily solved by introducing hw capabilities
 104         * that will be set by the remoteproc driver.
 105         */
 106        if (!iommu_present(dev->bus)) {
 107                dev_dbg(dev, "iommu not found\n");
 108                return 0;
 109        }
 110
 111        domain = iommu_domain_alloc(dev->bus);
 112        if (!domain) {
 113                dev_err(dev, "can't alloc iommu domain\n");
 114                return -ENOMEM;
 115        }
 116
 117        iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
 118
 119        ret = iommu_attach_device(domain, dev);
 120        if (ret) {
 121                dev_err(dev, "can't attach iommu device: %d\n", ret);
 122                goto free_domain;
 123        }
 124
 125        rproc->domain = domain;
 126
 127        return 0;
 128
 129free_domain:
 130        iommu_domain_free(domain);
 131        return ret;
 132}
 133
 134static void rproc_disable_iommu(struct rproc *rproc)
 135{
 136        struct iommu_domain *domain = rproc->domain;
 137        struct device *dev = rproc->dev.parent;
 138
 139        if (!domain)
 140                return;
 141
 142        iommu_detach_device(domain, dev);
 143        iommu_domain_free(domain);
 144
 145        return;
 146}
 147
 148/*
 149 * Some remote processors will ask us to allocate them physically contiguous
 150 * memory regions (which we call "carveouts"), and map them to specific
 151 * device addresses (which are hardcoded in the firmware).
 152 *
 153 * They may then ask us to copy objects into specific device addresses (e.g.
 154 * code/data sections) or expose us certain symbols in other device address
 155 * (e.g. their trace buffer).
 156 *
 157 * This function is an internal helper with which we can go over the allocated
 158 * carveouts and translate specific device address to kernel virtual addresses
 159 * so we can access the referenced memory.
 160 *
 161 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
 162 * but only on kernel direct mapped RAM memory. Instead, we're just using
 163 * here the output of the DMA API, which should be more correct.
 164 */
 165void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
 166{
 167        struct rproc_mem_entry *carveout;
 168        void *ptr = NULL;
 169
 170        list_for_each_entry(carveout, &rproc->carveouts, node) {
 171                int offset = da - carveout->da;
 172
 173                /* try next carveout if da is too small */
 174                if (offset < 0)
 175                        continue;
 176
 177                /* try next carveout if da is too large */
 178                if (offset + len > carveout->len)
 179                        continue;
 180
 181                ptr = carveout->va + offset;
 182
 183                break;
 184        }
 185
 186        return ptr;
 187}
 188EXPORT_SYMBOL(rproc_da_to_va);
 189
 190int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
 191{
 192        struct rproc *rproc = rvdev->rproc;
 193        struct device *dev = &rproc->dev;
 194        struct rproc_vring *rvring = &rvdev->vring[i];
 195        dma_addr_t dma;
 196        void *va;
 197        int ret, size, notifyid;
 198
 199        /* actual size of vring (in bytes) */
 200        size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
 201
 202        if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
 203                dev_err(dev, "idr_pre_get failed\n");
 204                return -ENOMEM;
 205        }
 206
 207        /*
 208         * Allocate non-cacheable memory for the vring. In the future
 209         * this call will also configure the IOMMU for us
 210         * TODO: let the rproc know the da of this vring
 211         */
 212        va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
 213        if (!va) {
 214                dev_err(dev->parent, "dma_alloc_coherent failed\n");
 215                return -EINVAL;
 216        }
 217
 218        /*
 219         * Assign an rproc-wide unique index for this vring
 220         * TODO: assign a notifyid for rvdev updates as well
 221         * TODO: let the rproc know the notifyid of this vring
 222         * TODO: support predefined notifyids (via resource table)
 223         */
 224        ret = idr_get_new(&rproc->notifyids, rvring, &notifyid);
 225        if (ret) {
 226                dev_err(dev, "idr_get_new failed: %d\n", ret);
 227                dma_free_coherent(dev->parent, size, va, dma);
 228                return ret;
 229        }
 230
 231        /* Store largest notifyid */
 232        rproc->max_notifyid = max(rproc->max_notifyid, notifyid);
 233
 234        dev_dbg(dev, "vring%d: va %p dma %llx size %x idr %d\n", i, va,
 235                                (unsigned long long)dma, size, notifyid);
 236
 237        rvring->va = va;
 238        rvring->dma = dma;
 239        rvring->notifyid = notifyid;
 240
 241        return 0;
 242}
 243
 244static int
 245rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
 246{
 247        struct rproc *rproc = rvdev->rproc;
 248        struct device *dev = &rproc->dev;
 249        struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
 250        struct rproc_vring *rvring = &rvdev->vring[i];
 251
 252        dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
 253                                i, vring->da, vring->num, vring->align);
 254
 255        /* make sure reserved bytes are zeroes */
 256        if (vring->reserved) {
 257                dev_err(dev, "vring rsc has non zero reserved bytes\n");
 258                return -EINVAL;
 259        }
 260
 261        /* verify queue size and vring alignment are sane */
 262        if (!vring->num || !vring->align) {
 263                dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
 264                                                vring->num, vring->align);
 265                return -EINVAL;
 266        }
 267
 268        rvring->len = vring->num;
 269        rvring->align = vring->align;
 270        rvring->rvdev = rvdev;
 271
 272        return 0;
 273}
 274
 275static int rproc_max_notifyid(int id, void *p, void *data)
 276{
 277        int *maxid = data;
 278        *maxid = max(*maxid, id);
 279        return 0;
 280}
 281
 282void rproc_free_vring(struct rproc_vring *rvring)
 283{
 284        int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
 285        struct rproc *rproc = rvring->rvdev->rproc;
 286        int maxid = 0;
 287
 288        dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
 289        idr_remove(&rproc->notifyids, rvring->notifyid);
 290
 291        /* Find the largest remaining notifyid */
 292        idr_for_each(&rproc->notifyids, rproc_max_notifyid, &maxid);
 293        rproc->max_notifyid = maxid;
 294}
 295
 296/**
 297 * rproc_handle_vdev() - handle a vdev fw resource
 298 * @rproc: the remote processor
 299 * @rsc: the vring resource descriptor
 300 * @avail: size of available data (for sanity checking the image)
 301 *
 302 * This resource entry requests the host to statically register a virtio
 303 * device (vdev), and setup everything needed to support it. It contains
 304 * everything needed to make it possible: the virtio device id, virtio
 305 * device features, vrings information, virtio config space, etc...
 306 *
 307 * Before registering the vdev, the vrings are allocated from non-cacheable
 308 * physically contiguous memory. Currently we only support two vrings per
 309 * remote processor (temporary limitation). We might also want to consider
 310 * doing the vring allocation only later when ->find_vqs() is invoked, and
 311 * then release them upon ->del_vqs().
 312 *
 313 * Note: @da is currently not really handled correctly: we dynamically
 314 * allocate it using the DMA API, ignoring requested hard coded addresses,
 315 * and we don't take care of any required IOMMU programming. This is all
 316 * going to be taken care of when the generic iommu-based DMA API will be
 317 * merged. Meanwhile, statically-addressed iommu-based firmware images should
 318 * use RSC_DEVMEM resource entries to map their required @da to the physical
 319 * address of their base CMA region (ouch, hacky!).
 320 *
 321 * Returns 0 on success, or an appropriate error code otherwise
 322 */
 323static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
 324                                                                int avail)
 325{
 326        struct device *dev = &rproc->dev;
 327        struct rproc_vdev *rvdev;
 328        int i, ret;
 329
 330        /* make sure resource isn't truncated */
 331        if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
 332                        + rsc->config_len > avail) {
 333                dev_err(dev, "vdev rsc is truncated\n");
 334                return -EINVAL;
 335        }
 336
 337        /* make sure reserved bytes are zeroes */
 338        if (rsc->reserved[0] || rsc->reserved[1]) {
 339                dev_err(dev, "vdev rsc has non zero reserved bytes\n");
 340                return -EINVAL;
 341        }
 342
 343        dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
 344                rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
 345
 346        /* we currently support only two vrings per rvdev */
 347        if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
 348                dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
 349                return -EINVAL;
 350        }
 351
 352        rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
 353        if (!rvdev)
 354                return -ENOMEM;
 355
 356        rvdev->rproc = rproc;
 357
 358        /* parse the vrings */
 359        for (i = 0; i < rsc->num_of_vrings; i++) {
 360                ret = rproc_parse_vring(rvdev, rsc, i);
 361                if (ret)
 362                        goto free_rvdev;
 363        }
 364
 365        /* remember the device features */
 366        rvdev->dfeatures = rsc->dfeatures;
 367
 368        list_add_tail(&rvdev->node, &rproc->rvdevs);
 369
 370        /* it is now safe to add the virtio device */
 371        ret = rproc_add_virtio_dev(rvdev, rsc->id);
 372        if (ret)
 373                goto remove_rvdev;
 374
 375        return 0;
 376
 377remove_rvdev:
 378        list_del(&rvdev->node);
 379free_rvdev:
 380        kfree(rvdev);
 381        return ret;
 382}
 383
 384/**
 385 * rproc_handle_trace() - handle a shared trace buffer resource
 386 * @rproc: the remote processor
 387 * @rsc: the trace resource descriptor
 388 * @avail: size of available data (for sanity checking the image)
 389 *
 390 * In case the remote processor dumps trace logs into memory,
 391 * export it via debugfs.
 392 *
 393 * Currently, the 'da' member of @rsc should contain the device address
 394 * where the remote processor is dumping the traces. Later we could also
 395 * support dynamically allocating this address using the generic
 396 * DMA API (but currently there isn't a use case for that).
 397 *
 398 * Returns 0 on success, or an appropriate error code otherwise
 399 */
 400static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
 401                                                                int avail)
 402{
 403        struct rproc_mem_entry *trace;
 404        struct device *dev = &rproc->dev;
 405        void *ptr;
 406        char name[15];
 407
 408        if (sizeof(*rsc) > avail) {
 409                dev_err(dev, "trace rsc is truncated\n");
 410                return -EINVAL;
 411        }
 412
 413        /* make sure reserved bytes are zeroes */
 414        if (rsc->reserved) {
 415                dev_err(dev, "trace rsc has non zero reserved bytes\n");
 416                return -EINVAL;
 417        }
 418
 419        /* what's the kernel address of this resource ? */
 420        ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
 421        if (!ptr) {
 422                dev_err(dev, "erroneous trace resource entry\n");
 423                return -EINVAL;
 424        }
 425
 426        trace = kzalloc(sizeof(*trace), GFP_KERNEL);
 427        if (!trace) {
 428                dev_err(dev, "kzalloc trace failed\n");
 429                return -ENOMEM;
 430        }
 431
 432        /* set the trace buffer dma properties */
 433        trace->len = rsc->len;
 434        trace->va = ptr;
 435
 436        /* make sure snprintf always null terminates, even if truncating */
 437        snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
 438
 439        /* create the debugfs entry */
 440        trace->priv = rproc_create_trace_file(name, rproc, trace);
 441        if (!trace->priv) {
 442                trace->va = NULL;
 443                kfree(trace);
 444                return -EINVAL;
 445        }
 446
 447        list_add_tail(&trace->node, &rproc->traces);
 448
 449        rproc->num_traces++;
 450
 451        dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
 452                                                rsc->da, rsc->len);
 453
 454        return 0;
 455}
 456
 457/**
 458 * rproc_handle_devmem() - handle devmem resource entry
 459 * @rproc: remote processor handle
 460 * @rsc: the devmem resource entry
 461 * @avail: size of available data (for sanity checking the image)
 462 *
 463 * Remote processors commonly need to access certain on-chip peripherals.
 464 *
 465 * Some of these remote processors access memory via an iommu device,
 466 * and might require us to configure their iommu before they can access
 467 * the on-chip peripherals they need.
 468 *
 469 * This resource entry is a request to map such a peripheral device.
 470 *
 471 * These devmem entries will contain the physical address of the device in
 472 * the 'pa' member. If a specific device address is expected, then 'da' will
 473 * contain it (currently this is the only use case supported). 'len' will
 474 * contain the size of the physical region we need to map.
 475 *
 476 * Currently we just "trust" those devmem entries to contain valid physical
 477 * addresses, but this is going to change: we want the implementations to
 478 * tell us ranges of physical addresses the firmware is allowed to request,
 479 * and not allow firmwares to request access to physical addresses that
 480 * are outside those ranges.
 481 */
 482static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
 483                                                                int avail)
 484{
 485        struct rproc_mem_entry *mapping;
 486        struct device *dev = &rproc->dev;
 487        int ret;
 488
 489        /* no point in handling this resource without a valid iommu domain */
 490        if (!rproc->domain)
 491                return -EINVAL;
 492
 493        if (sizeof(*rsc) > avail) {
 494                dev_err(dev, "devmem rsc is truncated\n");
 495                return -EINVAL;
 496        }
 497
 498        /* make sure reserved bytes are zeroes */
 499        if (rsc->reserved) {
 500                dev_err(dev, "devmem rsc has non zero reserved bytes\n");
 501                return -EINVAL;
 502        }
 503
 504        mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
 505        if (!mapping) {
 506                dev_err(dev, "kzalloc mapping failed\n");
 507                return -ENOMEM;
 508        }
 509
 510        ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
 511        if (ret) {
 512                dev_err(dev, "failed to map devmem: %d\n", ret);
 513                goto out;
 514        }
 515
 516        /*
 517         * We'll need this info later when we'll want to unmap everything
 518         * (e.g. on shutdown).
 519         *
 520         * We can't trust the remote processor not to change the resource
 521         * table, so we must maintain this info independently.
 522         */
 523        mapping->da = rsc->da;
 524        mapping->len = rsc->len;
 525        list_add_tail(&mapping->node, &rproc->mappings);
 526
 527        dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
 528                                        rsc->pa, rsc->da, rsc->len);
 529
 530        return 0;
 531
 532out:
 533        kfree(mapping);
 534        return ret;
 535}
 536
 537/**
 538 * rproc_handle_carveout() - handle phys contig memory allocation requests
 539 * @rproc: rproc handle
 540 * @rsc: the resource entry
 541 * @avail: size of available data (for image validation)
 542 *
 543 * This function will handle firmware requests for allocation of physically
 544 * contiguous memory regions.
 545 *
 546 * These request entries should come first in the firmware's resource table,
 547 * as other firmware entries might request placing other data objects inside
 548 * these memory regions (e.g. data/code segments, trace resource entries, ...).
 549 *
 550 * Allocating memory this way helps utilizing the reserved physical memory
 551 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
 552 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
 553 * pressure is important; it may have a substantial impact on performance.
 554 */
 555static int rproc_handle_carveout(struct rproc *rproc,
 556                                struct fw_rsc_carveout *rsc, int avail)
 557{
 558        struct rproc_mem_entry *carveout, *mapping;
 559        struct device *dev = &rproc->dev;
 560        dma_addr_t dma;
 561        void *va;
 562        int ret;
 563
 564        if (sizeof(*rsc) > avail) {
 565                dev_err(dev, "carveout rsc is truncated\n");
 566                return -EINVAL;
 567        }
 568
 569        /* make sure reserved bytes are zeroes */
 570        if (rsc->reserved) {
 571                dev_err(dev, "carveout rsc has non zero reserved bytes\n");
 572                return -EINVAL;
 573        }
 574
 575        dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
 576                        rsc->da, rsc->pa, rsc->len, rsc->flags);
 577
 578        carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
 579        if (!carveout) {
 580                dev_err(dev, "kzalloc carveout failed\n");
 581                return -ENOMEM;
 582        }
 583
 584        va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
 585        if (!va) {
 586                dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
 587                ret = -ENOMEM;
 588                goto free_carv;
 589        }
 590
 591        dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va,
 592                                        (unsigned long long)dma, rsc->len);
 593
 594        /*
 595         * Ok, this is non-standard.
 596         *
 597         * Sometimes we can't rely on the generic iommu-based DMA API
 598         * to dynamically allocate the device address and then set the IOMMU
 599         * tables accordingly, because some remote processors might
 600         * _require_ us to use hard coded device addresses that their
 601         * firmware was compiled with.
 602         *
 603         * In this case, we must use the IOMMU API directly and map
 604         * the memory to the device address as expected by the remote
 605         * processor.
 606         *
 607         * Obviously such remote processor devices should not be configured
 608         * to use the iommu-based DMA API: we expect 'dma' to contain the
 609         * physical address in this case.
 610         */
 611        if (rproc->domain) {
 612                mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
 613                if (!mapping) {
 614                        dev_err(dev, "kzalloc mapping failed\n");
 615                        ret = -ENOMEM;
 616                        goto dma_free;
 617                }
 618
 619                ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
 620                                                                rsc->flags);
 621                if (ret) {
 622                        dev_err(dev, "iommu_map failed: %d\n", ret);
 623                        goto free_mapping;
 624                }
 625
 626                /*
 627                 * We'll need this info later when we'll want to unmap
 628                 * everything (e.g. on shutdown).
 629                 *
 630                 * We can't trust the remote processor not to change the
 631                 * resource table, so we must maintain this info independently.
 632                 */
 633                mapping->da = rsc->da;
 634                mapping->len = rsc->len;
 635                list_add_tail(&mapping->node, &rproc->mappings);
 636
 637                dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n",
 638                                        rsc->da, (unsigned long long)dma);
 639        }
 640
 641        /*
 642         * Some remote processors might need to know the pa
 643         * even though they are behind an IOMMU. E.g., OMAP4's
 644         * remote M3 processor needs this so it can control
 645         * on-chip hardware accelerators that are not behind
 646         * the IOMMU, and therefor must know the pa.
 647         *
 648         * Generally we don't want to expose physical addresses
 649         * if we don't have to (remote processors are generally
 650         * _not_ trusted), so we might want to do this only for
 651         * remote processor that _must_ have this (e.g. OMAP4's
 652         * dual M3 subsystem).
 653         *
 654         * Non-IOMMU processors might also want to have this info.
 655         * In this case, the device address and the physical address
 656         * are the same.
 657         */
 658        rsc->pa = dma;
 659
 660        carveout->va = va;
 661        carveout->len = rsc->len;
 662        carveout->dma = dma;
 663        carveout->da = rsc->da;
 664
 665        list_add_tail(&carveout->node, &rproc->carveouts);
 666
 667        return 0;
 668
 669free_mapping:
 670        kfree(mapping);
 671dma_free:
 672        dma_free_coherent(dev->parent, rsc->len, va, dma);
 673free_carv:
 674        kfree(carveout);
 675        return ret;
 676}
 677
 678/*
 679 * A lookup table for resource handlers. The indices are defined in
 680 * enum fw_resource_type.
 681 */
 682static rproc_handle_resource_t rproc_handle_rsc[] = {
 683        [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
 684        [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
 685        [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
 686        [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
 687};
 688
 689/* handle firmware resource entries before booting the remote processor */
 690static int
 691rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
 692{
 693        struct device *dev = &rproc->dev;
 694        rproc_handle_resource_t handler;
 695        int ret = 0, i;
 696
 697        for (i = 0; i < table->num; i++) {
 698                int offset = table->offset[i];
 699                struct fw_rsc_hdr *hdr = (void *)table + offset;
 700                int avail = len - offset - sizeof(*hdr);
 701                void *rsc = (void *)hdr + sizeof(*hdr);
 702
 703                /* make sure table isn't truncated */
 704                if (avail < 0) {
 705                        dev_err(dev, "rsc table is truncated\n");
 706                        return -EINVAL;
 707                }
 708
 709                dev_dbg(dev, "rsc: type %d\n", hdr->type);
 710
 711                if (hdr->type >= RSC_LAST) {
 712                        dev_warn(dev, "unsupported resource %d\n", hdr->type);
 713                        continue;
 714                }
 715
 716                handler = rproc_handle_rsc[hdr->type];
 717                if (!handler)
 718                        continue;
 719
 720                ret = handler(rproc, rsc, avail);
 721                if (ret)
 722                        break;
 723        }
 724
 725        return ret;
 726}
 727
 728/* handle firmware resource entries while registering the remote processor */
 729static int
 730rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
 731{
 732        struct device *dev = &rproc->dev;
 733        int ret = 0, i;
 734
 735        for (i = 0; i < table->num; i++) {
 736                int offset = table->offset[i];
 737                struct fw_rsc_hdr *hdr = (void *)table + offset;
 738                int avail = len - offset - sizeof(*hdr);
 739                struct fw_rsc_vdev *vrsc;
 740
 741                /* make sure table isn't truncated */
 742                if (avail < 0) {
 743                        dev_err(dev, "rsc table is truncated\n");
 744                        return -EINVAL;
 745                }
 746
 747                dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
 748
 749                if (hdr->type != RSC_VDEV)
 750                        continue;
 751
 752                vrsc = (struct fw_rsc_vdev *)hdr->data;
 753
 754                ret = rproc_handle_vdev(rproc, vrsc, avail);
 755                if (ret)
 756                        break;
 757        }
 758
 759        return ret;
 760}
 761
 762/**
 763 * rproc_resource_cleanup() - clean up and free all acquired resources
 764 * @rproc: rproc handle
 765 *
 766 * This function will free all resources acquired for @rproc, and it
 767 * is called whenever @rproc either shuts down or fails to boot.
 768 */
 769static void rproc_resource_cleanup(struct rproc *rproc)
 770{
 771        struct rproc_mem_entry *entry, *tmp;
 772        struct device *dev = &rproc->dev;
 773
 774        /* clean up debugfs trace entries */
 775        list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
 776                rproc_remove_trace_file(entry->priv);
 777                rproc->num_traces--;
 778                list_del(&entry->node);
 779                kfree(entry);
 780        }
 781
 782        /* clean up carveout allocations */
 783        list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
 784                dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
 785                list_del(&entry->node);
 786                kfree(entry);
 787        }
 788
 789        /* clean up iommu mapping entries */
 790        list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
 791                size_t unmapped;
 792
 793                unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
 794                if (unmapped != entry->len) {
 795                        /* nothing much to do besides complaining */
 796                        dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
 797                                                                unmapped);
 798                }
 799
 800                list_del(&entry->node);
 801                kfree(entry);
 802        }
 803}
 804
 805/*
 806 * take a firmware and boot a remote processor with it.
 807 */
 808static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
 809{
 810        struct device *dev = &rproc->dev;
 811        const char *name = rproc->firmware;
 812        struct resource_table *table;
 813        int ret, tablesz;
 814
 815        ret = rproc_fw_sanity_check(rproc, fw);
 816        if (ret)
 817                return ret;
 818
 819        dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
 820
 821        /*
 822         * if enabling an IOMMU isn't relevant for this rproc, this is
 823         * just a nop
 824         */
 825        ret = rproc_enable_iommu(rproc);
 826        if (ret) {
 827                dev_err(dev, "can't enable iommu: %d\n", ret);
 828                return ret;
 829        }
 830
 831        rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
 832
 833        /* look for the resource table */
 834        table = rproc_find_rsc_table(rproc, fw, &tablesz);
 835        if (!table) {
 836                ret = -EINVAL;
 837                goto clean_up;
 838        }
 839
 840        /* handle fw resources which are required to boot rproc */
 841        ret = rproc_handle_boot_rsc(rproc, table, tablesz);
 842        if (ret) {
 843                dev_err(dev, "Failed to process resources: %d\n", ret);
 844                goto clean_up;
 845        }
 846
 847        /* load the ELF segments to memory */
 848        ret = rproc_load_segments(rproc, fw);
 849        if (ret) {
 850                dev_err(dev, "Failed to load program segments: %d\n", ret);
 851                goto clean_up;
 852        }
 853
 854        /* power up the remote processor */
 855        ret = rproc->ops->start(rproc);
 856        if (ret) {
 857                dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
 858                goto clean_up;
 859        }
 860
 861        rproc->state = RPROC_RUNNING;
 862
 863        dev_info(dev, "remote processor %s is now up\n", rproc->name);
 864
 865        return 0;
 866
 867clean_up:
 868        rproc_resource_cleanup(rproc);
 869        rproc_disable_iommu(rproc);
 870        return ret;
 871}
 872
 873/*
 874 * take a firmware and look for virtio devices to register.
 875 *
 876 * Note: this function is called asynchronously upon registration of the
 877 * remote processor (so we must wait until it completes before we try
 878 * to unregister the device. one other option is just to use kref here,
 879 * that might be cleaner).
 880 */
 881static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
 882{
 883        struct rproc *rproc = context;
 884        struct resource_table *table;
 885        int ret, tablesz;
 886
 887        if (rproc_fw_sanity_check(rproc, fw) < 0)
 888                goto out;
 889
 890        /* look for the resource table */
 891        table = rproc_find_rsc_table(rproc, fw,  &tablesz);
 892        if (!table)
 893                goto out;
 894
 895        /* look for virtio devices and register them */
 896        ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
 897        if (ret)
 898                goto out;
 899
 900out:
 901        release_firmware(fw);
 902        /* allow rproc_del() contexts, if any, to proceed */
 903        complete_all(&rproc->firmware_loading_complete);
 904}
 905
 906static int rproc_add_virtio_devices(struct rproc *rproc)
 907{
 908        int ret;
 909
 910        /* rproc_del() calls must wait until async loader completes */
 911        init_completion(&rproc->firmware_loading_complete);
 912
 913        /*
 914         * We must retrieve early virtio configuration info from
 915         * the firmware (e.g. whether to register a virtio device,
 916         * what virtio features does it support, ...).
 917         *
 918         * We're initiating an asynchronous firmware loading, so we can
 919         * be built-in kernel code, without hanging the boot process.
 920         */
 921        ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
 922                                      rproc->firmware, &rproc->dev, GFP_KERNEL,
 923                                      rproc, rproc_fw_config_virtio);
 924        if (ret < 0) {
 925                dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
 926                complete_all(&rproc->firmware_loading_complete);
 927        }
 928
 929        return ret;
 930}
 931
 932/**
 933 * rproc_trigger_recovery() - recover a remoteproc
 934 * @rproc: the remote processor
 935 *
 936 * The recovery is done by reseting all the virtio devices, that way all the
 937 * rpmsg drivers will be reseted along with the remote processor making the
 938 * remoteproc functional again.
 939 *
 940 * This function can sleep, so it cannot be called from atomic context.
 941 */
 942int rproc_trigger_recovery(struct rproc *rproc)
 943{
 944        struct rproc_vdev *rvdev, *rvtmp;
 945
 946        dev_err(&rproc->dev, "recovering %s\n", rproc->name);
 947
 948        init_completion(&rproc->crash_comp);
 949
 950        /* clean up remote vdev entries */
 951        list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
 952                rproc_remove_virtio_dev(rvdev);
 953
 954        /* wait until there is no more rproc users */
 955        wait_for_completion(&rproc->crash_comp);
 956
 957        return rproc_add_virtio_devices(rproc);
 958}
 959
 960/**
 961 * rproc_crash_handler_work() - handle a crash
 962 *
 963 * This function needs to handle everything related to a crash, like cpu
 964 * registers and stack dump, information to help to debug the fatal error, etc.
 965 */
 966static void rproc_crash_handler_work(struct work_struct *work)
 967{
 968        struct rproc *rproc = container_of(work, struct rproc, crash_handler);
 969        struct device *dev = &rproc->dev;
 970
 971        dev_dbg(dev, "enter %s\n", __func__);
 972
 973        mutex_lock(&rproc->lock);
 974
 975        if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
 976                /* handle only the first crash detected */
 977                mutex_unlock(&rproc->lock);
 978                return;
 979        }
 980
 981        rproc->state = RPROC_CRASHED;
 982        dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
 983                rproc->name);
 984
 985        mutex_unlock(&rproc->lock);
 986
 987        if (!rproc->recovery_disabled)
 988                rproc_trigger_recovery(rproc);
 989}
 990
 991/**
 992 * rproc_boot() - boot a remote processor
 993 * @rproc: handle of a remote processor
 994 *
 995 * Boot a remote processor (i.e. load its firmware, power it on, ...).
 996 *
 997 * If the remote processor is already powered on, this function immediately
 998 * returns (successfully).
 999 *
1000 * Returns 0 on success, and an appropriate error value otherwise.
1001 */
1002int rproc_boot(struct rproc *rproc)
1003{
1004        const struct firmware *firmware_p;
1005        struct device *dev;
1006        int ret;
1007
1008        if (!rproc) {
1009                pr_err("invalid rproc handle\n");
1010                return -EINVAL;
1011        }
1012
1013        dev = &rproc->dev;
1014
1015        ret = mutex_lock_interruptible(&rproc->lock);
1016        if (ret) {
1017                dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1018                return ret;
1019        }
1020
1021        /* loading a firmware is required */
1022        if (!rproc->firmware) {
1023                dev_err(dev, "%s: no firmware to load\n", __func__);
1024                ret = -EINVAL;
1025                goto unlock_mutex;
1026        }
1027
1028        /* prevent underlying implementation from being removed */
1029        if (!try_module_get(dev->parent->driver->owner)) {
1030                dev_err(dev, "%s: can't get owner\n", __func__);
1031                ret = -EINVAL;
1032                goto unlock_mutex;
1033        }
1034
1035        /* skip the boot process if rproc is already powered up */
1036        if (atomic_inc_return(&rproc->power) > 1) {
1037                ret = 0;
1038                goto unlock_mutex;
1039        }
1040
1041        dev_info(dev, "powering up %s\n", rproc->name);
1042
1043        /* load firmware */
1044        ret = request_firmware(&firmware_p, rproc->firmware, dev);
1045        if (ret < 0) {
1046                dev_err(dev, "request_firmware failed: %d\n", ret);
1047                goto downref_rproc;
1048        }
1049
1050        ret = rproc_fw_boot(rproc, firmware_p);
1051
1052        release_firmware(firmware_p);
1053
1054downref_rproc:
1055        if (ret) {
1056                module_put(dev->parent->driver->owner);
1057                atomic_dec(&rproc->power);
1058        }
1059unlock_mutex:
1060        mutex_unlock(&rproc->lock);
1061        return ret;
1062}
1063EXPORT_SYMBOL(rproc_boot);
1064
1065/**
1066 * rproc_shutdown() - power off the remote processor
1067 * @rproc: the remote processor
1068 *
1069 * Power off a remote processor (previously booted with rproc_boot()).
1070 *
1071 * In case @rproc is still being used by an additional user(s), then
1072 * this function will just decrement the power refcount and exit,
1073 * without really powering off the device.
1074 *
1075 * Every call to rproc_boot() must (eventually) be accompanied by a call
1076 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1077 *
1078 * Notes:
1079 * - we're not decrementing the rproc's refcount, only the power refcount.
1080 *   which means that the @rproc handle stays valid even after rproc_shutdown()
1081 *   returns, and users can still use it with a subsequent rproc_boot(), if
1082 *   needed.
1083 */
1084void rproc_shutdown(struct rproc *rproc)
1085{
1086        struct device *dev = &rproc->dev;
1087        int ret;
1088
1089        ret = mutex_lock_interruptible(&rproc->lock);
1090        if (ret) {
1091                dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1092                return;
1093        }
1094
1095        /* if the remote proc is still needed, bail out */
1096        if (!atomic_dec_and_test(&rproc->power))
1097                goto out;
1098
1099        /* power off the remote processor */
1100        ret = rproc->ops->stop(rproc);
1101        if (ret) {
1102                atomic_inc(&rproc->power);
1103                dev_err(dev, "can't stop rproc: %d\n", ret);
1104                goto out;
1105        }
1106
1107        /* clean up all acquired resources */
1108        rproc_resource_cleanup(rproc);
1109
1110        rproc_disable_iommu(rproc);
1111
1112        /* if in crash state, unlock crash handler */
1113        if (rproc->state == RPROC_CRASHED)
1114                complete_all(&rproc->crash_comp);
1115
1116        rproc->state = RPROC_OFFLINE;
1117
1118        dev_info(dev, "stopped remote processor %s\n", rproc->name);
1119
1120out:
1121        mutex_unlock(&rproc->lock);
1122        if (!ret)
1123                module_put(dev->parent->driver->owner);
1124}
1125EXPORT_SYMBOL(rproc_shutdown);
1126
1127/**
1128 * rproc_add() - register a remote processor
1129 * @rproc: the remote processor handle to register
1130 *
1131 * Registers @rproc with the remoteproc framework, after it has been
1132 * allocated with rproc_alloc().
1133 *
1134 * This is called by the platform-specific rproc implementation, whenever
1135 * a new remote processor device is probed.
1136 *
1137 * Returns 0 on success and an appropriate error code otherwise.
1138 *
1139 * Note: this function initiates an asynchronous firmware loading
1140 * context, which will look for virtio devices supported by the rproc's
1141 * firmware.
1142 *
1143 * If found, those virtio devices will be created and added, so as a result
1144 * of registering this remote processor, additional virtio drivers might be
1145 * probed.
1146 */
1147int rproc_add(struct rproc *rproc)
1148{
1149        struct device *dev = &rproc->dev;
1150        int ret;
1151
1152        ret = device_add(dev);
1153        if (ret < 0)
1154                return ret;
1155
1156        dev_info(dev, "%s is available\n", rproc->name);
1157
1158        dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1159        dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1160
1161        /* create debugfs entries */
1162        rproc_create_debug_dir(rproc);
1163
1164        return rproc_add_virtio_devices(rproc);
1165}
1166EXPORT_SYMBOL(rproc_add);
1167
1168/**
1169 * rproc_type_release() - release a remote processor instance
1170 * @dev: the rproc's device
1171 *
1172 * This function should _never_ be called directly.
1173 *
1174 * It will be called by the driver core when no one holds a valid pointer
1175 * to @dev anymore.
1176 */
1177static void rproc_type_release(struct device *dev)
1178{
1179        struct rproc *rproc = container_of(dev, struct rproc, dev);
1180
1181        dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1182
1183        rproc_delete_debug_dir(rproc);
1184
1185        idr_remove_all(&rproc->notifyids);
1186        idr_destroy(&rproc->notifyids);
1187
1188        if (rproc->index >= 0)
1189                ida_simple_remove(&rproc_dev_index, rproc->index);
1190
1191        kfree(rproc);
1192}
1193
1194static struct device_type rproc_type = {
1195        .name           = "remoteproc",
1196        .release        = rproc_type_release,
1197};
1198
1199/**
1200 * rproc_alloc() - allocate a remote processor handle
1201 * @dev: the underlying device
1202 * @name: name of this remote processor
1203 * @ops: platform-specific handlers (mainly start/stop)
1204 * @firmware: name of firmware file to load
1205 * @len: length of private data needed by the rproc driver (in bytes)
1206 *
1207 * Allocates a new remote processor handle, but does not register
1208 * it yet.
1209 *
1210 * This function should be used by rproc implementations during initialization
1211 * of the remote processor.
1212 *
1213 * After creating an rproc handle using this function, and when ready,
1214 * implementations should then call rproc_add() to complete
1215 * the registration of the remote processor.
1216 *
1217 * On success the new rproc is returned, and on failure, NULL.
1218 *
1219 * Note: _never_ directly deallocate @rproc, even if it was not registered
1220 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1221 */
1222struct rproc *rproc_alloc(struct device *dev, const char *name,
1223                                const struct rproc_ops *ops,
1224                                const char *firmware, int len)
1225{
1226        struct rproc *rproc;
1227
1228        if (!dev || !name || !ops)
1229                return NULL;
1230
1231        rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1232        if (!rproc) {
1233                dev_err(dev, "%s: kzalloc failed\n", __func__);
1234                return NULL;
1235        }
1236
1237        rproc->name = name;
1238        rproc->ops = ops;
1239        rproc->firmware = firmware;
1240        rproc->priv = &rproc[1];
1241
1242        device_initialize(&rproc->dev);
1243        rproc->dev.parent = dev;
1244        rproc->dev.type = &rproc_type;
1245
1246        /* Assign a unique device index and name */
1247        rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1248        if (rproc->index < 0) {
1249                dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1250                put_device(&rproc->dev);
1251                return NULL;
1252        }
1253
1254        dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1255
1256        atomic_set(&rproc->power, 0);
1257
1258        /* Set ELF as the default fw_ops handler */
1259        rproc->fw_ops = &rproc_elf_fw_ops;
1260
1261        mutex_init(&rproc->lock);
1262
1263        idr_init(&rproc->notifyids);
1264
1265        INIT_LIST_HEAD(&rproc->carveouts);
1266        INIT_LIST_HEAD(&rproc->mappings);
1267        INIT_LIST_HEAD(&rproc->traces);
1268        INIT_LIST_HEAD(&rproc->rvdevs);
1269
1270        INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
1271        init_completion(&rproc->crash_comp);
1272
1273        rproc->state = RPROC_OFFLINE;
1274
1275        return rproc;
1276}
1277EXPORT_SYMBOL(rproc_alloc);
1278
1279/**
1280 * rproc_put() - unroll rproc_alloc()
1281 * @rproc: the remote processor handle
1282 *
1283 * This function decrements the rproc dev refcount.
1284 *
1285 * If no one holds any reference to rproc anymore, then its refcount would
1286 * now drop to zero, and it would be freed.
1287 */
1288void rproc_put(struct rproc *rproc)
1289{
1290        put_device(&rproc->dev);
1291}
1292EXPORT_SYMBOL(rproc_put);
1293
1294/**
1295 * rproc_del() - unregister a remote processor
1296 * @rproc: rproc handle to unregister
1297 *
1298 * This function should be called when the platform specific rproc
1299 * implementation decides to remove the rproc device. it should
1300 * _only_ be called if a previous invocation of rproc_add()
1301 * has completed successfully.
1302 *
1303 * After rproc_del() returns, @rproc isn't freed yet, because
1304 * of the outstanding reference created by rproc_alloc. To decrement that
1305 * one last refcount, one still needs to call rproc_put().
1306 *
1307 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1308 */
1309int rproc_del(struct rproc *rproc)
1310{
1311        struct rproc_vdev *rvdev, *tmp;
1312
1313        if (!rproc)
1314                return -EINVAL;
1315
1316        /* if rproc is just being registered, wait */
1317        wait_for_completion(&rproc->firmware_loading_complete);
1318
1319        /* clean up remote vdev entries */
1320        list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
1321                rproc_remove_virtio_dev(rvdev);
1322
1323        device_del(&rproc->dev);
1324
1325        return 0;
1326}
1327EXPORT_SYMBOL(rproc_del);
1328
1329/**
1330 * rproc_report_crash() - rproc crash reporter function
1331 * @rproc: remote processor
1332 * @type: crash type
1333 *
1334 * This function must be called every time a crash is detected by the low-level
1335 * drivers implementing a specific remoteproc. This should not be called from a
1336 * non-remoteproc driver.
1337 *
1338 * This function can be called from atomic/interrupt context.
1339 */
1340void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
1341{
1342        if (!rproc) {
1343                pr_err("NULL rproc pointer\n");
1344                return;
1345        }
1346
1347        dev_err(&rproc->dev, "crash detected in %s: type %s\n",
1348                rproc->name, rproc_crash_to_string(type));
1349
1350        /* create a new task to handle the error */
1351        schedule_work(&rproc->crash_handler);
1352}
1353EXPORT_SYMBOL(rproc_report_crash);
1354
1355static int __init remoteproc_init(void)
1356{
1357        rproc_init_debugfs();
1358
1359        return 0;
1360}
1361module_init(remoteproc_init);
1362
1363static void __exit remoteproc_exit(void)
1364{
1365        rproc_exit_debugfs();
1366}
1367module_exit(remoteproc_exit);
1368
1369MODULE_LICENSE("GPL v2");
1370MODULE_DESCRIPTION("Generic Remote Processor Framework");
1371
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.