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