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