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