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