remoteproc: Pass struct fw to load_segments and find_rsc_table.
[deliverable/linux.git] / drivers / remoteproc / remoteproc_core.c
1 /*
2 * Remote Processor Framework
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 *
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
9 * Mark Grosen <mgrosen@ti.com>
10 * Fernando Guzman Lugo <fernando.lugo@ti.com>
11 * Suman Anna <s-anna@ti.com>
12 * Robert Tivy <rtivy@ti.com>
13 * Armando Uribe De Leon <x0095078@ti.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25 #define pr_fmt(fmt) "%s: " fmt, __func__
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/firmware.h>
34 #include <linux/string.h>
35 #include <linux/debugfs.h>
36 #include <linux/remoteproc.h>
37 #include <linux/iommu.h>
38 #include <linux/idr.h>
39 #include <linux/elf.h>
40 #include <linux/virtio_ids.h>
41 #include <linux/virtio_ring.h>
42 #include <asm/byteorder.h>
43
44 #include "remoteproc_internal.h"
45
46 typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
47 struct resource_table *table, int len);
48 typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
49
50 /* Unique indices for remoteproc devices */
51 static DEFINE_IDA(rproc_dev_index);
52
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,
65 unsigned long iova, int flags, void *token)
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;
79 struct device *dev = rproc->dev.parent;
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)) {
94 dev_dbg(dev, "iommu not found\n");
95 return 0;
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
104 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
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;
124 struct device *dev = rproc->dev.parent;
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
179 * @fw: the ELF firmware image
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 */
200 static int
201 rproc_load_segments(struct rproc *rproc, const struct firmware *fw)
202 {
203 struct device *dev = &rproc->dev;
204 struct elf32_hdr *ehdr;
205 struct elf32_phdr *phdr;
206 int i, ret = 0;
207 const u8 *elf_data = fw->data;
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;
217 u32 offset = phdr->p_offset;
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
233 if (offset + filesz > fw->size) {
234 dev_err(dev, "truncated fw: need 0x%x avail 0x%x\n",
235 offset + filesz, fw->size);
236 ret = -EINVAL;
237 break;
238 }
239
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
266 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
267 {
268 struct rproc *rproc = rvdev->rproc;
269 struct device *dev = &rproc->dev;
270 struct rproc_vring *rvring = &rvdev->vring[i];
271 dma_addr_t dma;
272 void *va;
273 int ret, size, notifyid;
274
275 /* actual size of vring (in bytes) */
276 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
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
286 * TODO: let the rproc know the da of this vring
287 */
288 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
289 if (!va) {
290 dev_err(dev->parent, "dma_alloc_coherent failed\n");
291 return -EINVAL;
292 }
293
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, &notifyid);
301 if (ret) {
302 dev_err(dev, "idr_get_new failed: %d\n", ret);
303 dma_free_coherent(dev->parent, size, va, dma);
304 return ret;
305 }
306
307 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
308 dma, size, notifyid);
309
310 rvring->va = va;
311 rvring->dma = dma;
312 rvring->notifyid = notifyid;
313
314 return 0;
315 }
316
317 static int
318 rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
319 {
320 struct rproc *rproc = rvdev->rproc;
321 struct device *dev = &rproc->dev;
322 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
323 struct rproc_vring *rvring = &rvdev->vring[i];
324
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 }
333
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;
339 }
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
353 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
354 idr_remove(&rproc->notifyids, rvring->notifyid);
355 }
356
357 /**
358 * rproc_handle_vdev() - handle a vdev fw resource
359 * @rproc: the remote processor
360 * @rsc: the vring resource descriptor
361 * @avail: size of available data (for sanity checking the image)
362 *
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!).
381 *
382 * Returns 0 on success, or an appropriate error code otherwise
383 */
384 static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
385 int avail)
386 {
387 struct device *dev = &rproc->dev;
388 struct rproc_vdev *rvdev;
389 int i, ret;
390
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) {
394 dev_err(dev, "vdev rsc is truncated\n");
395 return -EINVAL;
396 }
397
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");
401 return -EINVAL;
402 }
403
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
407 /* we currently support only two vrings per rvdev */
408 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
409 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
410 return -EINVAL;
411 }
412
413 rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
414 if (!rvdev)
415 return -ENOMEM;
416
417 rvdev->rproc = rproc;
418
419 /* parse the vrings */
420 for (i = 0; i < rsc->num_of_vrings; i++) {
421 ret = rproc_parse_vring(rvdev, rsc, i);
422 if (ret)
423 goto free_rvdev;
424 }
425
426 /* remember the device features */
427 rvdev->dfeatures = rsc->dfeatures;
428
429 list_add_tail(&rvdev->node, &rproc->rvdevs);
430
431 /* it is now safe to add the virtio device */
432 ret = rproc_add_virtio_dev(rvdev, rsc->id);
433 if (ret)
434 goto free_rvdev;
435
436 return 0;
437
438 free_rvdev:
439 kfree(rvdev);
440 return ret;
441 }
442
443 /**
444 * rproc_handle_trace() - handle a shared trace buffer resource
445 * @rproc: the remote processor
446 * @rsc: the trace resource descriptor
447 * @avail: size of available data (for sanity checking the image)
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 */
459 static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
460 int avail)
461 {
462 struct rproc_mem_entry *trace;
463 struct device *dev = &rproc->dev;
464 void *ptr;
465 char name[15];
466
467 if (sizeof(*rsc) > avail) {
468 dev_err(dev, "trace rsc is truncated\n");
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
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
510 dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
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
520 * @avail: size of available data (for sanity checking the image)
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 */
541 static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
542 int avail)
543 {
544 struct rproc_mem_entry *mapping;
545 struct device *dev = &rproc->dev;
546 int ret;
547
548 /* no point in handling this resource without a valid iommu domain */
549 if (!rproc->domain)
550 return -EINVAL;
551
552 if (sizeof(*rsc) > avail) {
553 dev_err(dev, "devmem rsc is truncated\n");
554 return -EINVAL;
555 }
556
557 /* make sure reserved bytes are zeroes */
558 if (rsc->reserved) {
559 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
560 return -EINVAL;
561 }
562
563 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
564 if (!mapping) {
565 dev_err(dev, "kzalloc mapping failed\n");
566 return -ENOMEM;
567 }
568
569 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
570 if (ret) {
571 dev_err(dev, "failed to map devmem: %d\n", ret);
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
586 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
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
600 * @avail: size of available data (for image validation)
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 */
614 static int rproc_handle_carveout(struct rproc *rproc,
615 struct fw_rsc_carveout *rsc, int avail)
616 {
617 struct rproc_mem_entry *carveout, *mapping;
618 struct device *dev = &rproc->dev;
619 dma_addr_t dma;
620 void *va;
621 int ret;
622
623 if (sizeof(*rsc) > avail) {
624 dev_err(dev, "carveout rsc is truncated\n");
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
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
650 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
651 if (!va) {
652 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
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
695 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
696 }
697
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
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:
727 dma_free_coherent(dev->parent, rsc->len, va, dma);
728 free_carv:
729 kfree(carveout);
730 free_mapping:
731 kfree(mapping);
732 return ret;
733 }
734
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[] = {
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,
743 [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
744 };
745
746 /* handle firmware resource entries before booting the remote processor */
747 static int
748 rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
749 {
750 struct device *dev = &rproc->dev;
751 rproc_handle_resource_t handler;
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 }
765
766 dev_dbg(dev, "rsc: type %d\n", hdr->type);
767
768 if (hdr->type >= RSC_LAST) {
769 dev_warn(dev, "unsupported resource %d\n", hdr->type);
770 continue;
771 }
772
773 handler = rproc_handle_rsc[hdr->type];
774 if (!handler)
775 continue;
776
777 ret = handler(rproc, rsc, avail);
778 if (ret)
779 break;
780 }
781
782 return ret;
783 }
784
785 /* handle firmware resource entries while registering the remote processor */
786 static int
787 rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
788 {
789 struct device *dev = &rproc->dev;
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);
796 struct fw_rsc_vdev *vrsc;
797
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
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)
813 break;
814 }
815
816 return ret;
817 }
818
819 /**
820 * rproc_find_rsc_table() - find the resource table
821 * @rproc: the rproc handle
822 * @fw: the ELF firmware image
823 * @tablesz: place holder for providing back the table size
824 *
825 * This function finds the resource table inside the remote processor's
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.
829 *
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).
833 */
834 static struct resource_table *
835 rproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
836 int *tablesz)
837 {
838 struct elf32_hdr *ehdr;
839 struct elf32_shdr *shdr;
840 const char *name_table;
841 struct device *dev = &rproc->dev;
842 struct resource_table *table = NULL;
843 int i;
844 const u8 *elf_data = fw->data;
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++) {
852 int size = shdr->sh_size;
853 int offset = shdr->sh_offset;
854
855 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
856 continue;
857
858 table = (struct resource_table *)(elf_data + offset);
859
860 /* make sure we have the entire table */
861 if (offset + size > fw->size) {
862 dev_err(dev, "resource table truncated\n");
863 return NULL;
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");
869 return NULL;
870 }
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);
875 return NULL;
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");
881 return NULL;
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");
888 return NULL;
889 }
890
891 *tablesz = shdr->sh_size;
892 break;
893 }
894
895 return table;
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
903 * is called whenever @rproc either shuts down or fails to boot.
904 */
905 static void rproc_resource_cleanup(struct rproc *rproc)
906 {
907 struct rproc_mem_entry *entry, *tmp;
908 struct device *dev = &rproc->dev;
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
918 /* clean up carveout allocations */
919 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
920 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
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;
945 struct device *dev = &rproc->dev;
946 struct elf32_hdr *ehdr;
947 char class;
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
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
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
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
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 {
1006 struct device *dev = &rproc->dev;
1007 const char *name = rproc->firmware;
1008 struct elf32_hdr *ehdr;
1009 struct resource_table *table;
1010 int ret, tablesz;
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
1037 /* look for the resource table */
1038 table = rproc_find_rsc_table(rproc, fw, &tablesz);
1039 if (!table)
1040 goto clean_up;
1041
1042 /* handle fw resources which are required to boot rproc */
1043 ret = rproc_handle_boot_rsc(rproc, table, tablesz);
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 */
1050 ret = rproc_load_segments(rproc, fw);
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;
1086 struct resource_table *table;
1087 int ret, tablesz;
1088
1089 if (rproc_fw_sanity_check(rproc, fw) < 0)
1090 goto out;
1091
1092 /* look for the resource table */
1093 table = rproc_find_rsc_table(rproc, fw, &tablesz);
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)
1100 goto out;
1101
1102 out:
1103 release_firmware(fw);
1104 /* allow rproc_del() contexts, if any, to proceed */
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
1130 dev = &rproc->dev;
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 */
1146 if (!try_module_get(dev->parent->driver->owner)) {
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) {
1173 module_put(dev->parent->driver->owner);
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.
1200 */
1201 void rproc_shutdown(struct rproc *rproc)
1202 {
1203 struct device *dev = &rproc->dev;
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)
1236 module_put(dev->parent->driver->owner);
1237 }
1238 EXPORT_SYMBOL(rproc_shutdown);
1239
1240 /**
1241 * rproc_add() - register a remote processor
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
1257 * of registering this remote processor, additional virtio drivers might be
1258 * probed.
1259 */
1260 int rproc_add(struct rproc *rproc)
1261 {
1262 struct device *dev = &rproc->dev;
1263 int ret = 0;
1264
1265 ret = device_add(dev);
1266 if (ret < 0)
1267 return ret;
1268
1269 dev_info(dev, "%s is available\n", rproc->name);
1270
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
1274 /* create debugfs entries */
1275 rproc_create_debug_dir(rproc);
1276
1277 /* rproc_del() calls must wait until async loader completes */
1278 init_completion(&rproc->firmware_loading_complete);
1279
1280 /*
1281 * We must retrieve early virtio configuration info from
1282 * the firmware (e.g. whether to register a virtio device,
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);
1294 }
1295
1296 return ret;
1297 }
1298 EXPORT_SYMBOL(rproc_add);
1299
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
1313 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1314
1315 rproc_delete_debug_dir(rproc);
1316
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
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,
1346 * implementations should then call rproc_add() to complete
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
1352 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
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
1369 rproc->name = name;
1370 rproc->ops = ops;
1371 rproc->firmware = firmware;
1372 rproc->priv = &rproc[1];
1373
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
1388 atomic_set(&rproc->power, 0);
1389
1390 mutex_init(&rproc->lock);
1391
1392 idr_init(&rproc->notifyids);
1393
1394 INIT_LIST_HEAD(&rproc->carveouts);
1395 INIT_LIST_HEAD(&rproc->mappings);
1396 INIT_LIST_HEAD(&rproc->traces);
1397 INIT_LIST_HEAD(&rproc->rvdevs);
1398
1399 rproc->state = RPROC_OFFLINE;
1400
1401 return rproc;
1402 }
1403 EXPORT_SYMBOL(rproc_alloc);
1404
1405 /**
1406 * rproc_put() - unroll rproc_alloc()
1407 * @rproc: the remote processor handle
1408 *
1409 * This function decrements the rproc dev refcount.
1410 *
1411 * If no one holds any reference to rproc anymore, then its refcount would
1412 * now drop to zero, and it would be freed.
1413 */
1414 void rproc_put(struct rproc *rproc)
1415 {
1416 put_device(&rproc->dev);
1417 }
1418 EXPORT_SYMBOL(rproc_put);
1419
1420 /**
1421 * rproc_del() - unregister a remote processor
1422 * @rproc: rproc handle to unregister
1423 *
1424 * This function should be called when the platform specific rproc
1425 * implementation decides to remove the rproc device. it should
1426 * _only_ be called if a previous invocation of rproc_add()
1427 * has completed successfully.
1428 *
1429 * After rproc_del() returns, @rproc isn't freed yet, because
1430 * of the outstanding reference created by rproc_alloc. To decrement that
1431 * one last refcount, one still needs to call rproc_put().
1432 *
1433 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1434 */
1435 int rproc_del(struct rproc *rproc)
1436 {
1437 struct rproc_vdev *rvdev, *tmp;
1438
1439 if (!rproc)
1440 return -EINVAL;
1441
1442 /* if rproc is just being registered, wait */
1443 wait_for_completion(&rproc->firmware_loading_complete);
1444
1445 /* clean up remote vdev entries */
1446 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
1447 rproc_remove_virtio_dev(rvdev);
1448
1449 device_del(&rproc->dev);
1450
1451 return 0;
1452 }
1453 EXPORT_SYMBOL(rproc_del);
1454
1455 static int __init remoteproc_init(void)
1456 {
1457 rproc_init_debugfs();
1458
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");
This page took 0.059538 seconds and 6 git commands to generate.