1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Remote Processor Framework
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Brian Swetland <swetland@google.com>
10 * Mark Grosen <mgrosen@ti.com>
11 * Fernando Guzman Lugo <fernando.lugo@ti.com>
12 * Suman Anna <s-anna@ti.com>
13 * Robert Tivy <rtivy@ti.com>
14 * Armando Uribe De Leon <x0095078@ti.com>
15 */
17 #define pr_fmt(fmt) "%s: " fmt, __func__
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/firmware.h>
26 #include <linux/string.h>
27 #include <linux/debugfs.h>
28 #include <linux/devcoredump.h>
29 #include <linux/remoteproc.h>
30 #include <linux/iommu.h>
31 #include <linux/idr.h>
32 #include <linux/elf.h>
33 #include <linux/crc32.h>
34 #include <linux/of_reserved_mem.h>
35 #include <linux/virtio_ids.h>
36 #include <linux/virtio_ring.h>
37 #include <linux/of.h>
38 #include <linux/platform_device.h>
39 #include <asm/byteorder.h>
40 #include <linux/platform_device.h>
42 #include "remoteproc_internal.h"
44 #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
46 static DEFINE_MUTEX(rproc_list_mutex);
47 static LIST_HEAD(rproc_list);
49 typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
50 void *, int offset, int avail);
52 static int rproc_alloc_carveout(struct rproc *rproc,
53 struct rproc_mem_entry *mem);
54 static int rproc_release_carveout(struct rproc *rproc,
55 struct rproc_mem_entry *mem);
57 /* Unique indices for remoteproc devices */
58 static DEFINE_IDA(rproc_dev_index);
60 static const char * const rproc_crash_names[] = {
61 [RPROC_MMUFAULT] = "mmufault",
62 [RPROC_WATCHDOG] = "watchdog",
63 [RPROC_FATAL_ERROR] = "fatal error",
64 };
66 /* translate rproc_crash_type to string */
67 static const char *rproc_crash_to_string(enum rproc_crash_type type)
68 {
69 if (type < ARRAY_SIZE(rproc_crash_names))
70 return rproc_crash_names[type];
71 return "unknown";
72 }
74 /*
75 * This is the IOMMU fault handler we register with the IOMMU API
76 * (when relevant; not all remote processors access memory through
77 * an IOMMU).
78 *
79 * IOMMU core will invoke this handler whenever the remote processor
80 * will try to access an unmapped device address.
81 */
82 static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
83 unsigned long iova, int flags, void *token)
84 {
85 struct rproc *rproc = token;
87 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
89 rproc_report_crash(rproc, RPROC_MMUFAULT);
91 /*
92 * Let the iommu core know we're not really handling this fault;
93 * we just used it as a recovery trigger.
94 */
95 return -ENOSYS;
96 }
98 static int rproc_enable_iommu(struct rproc *rproc)
99 {
100 struct iommu_domain *domain;
101 struct device *dev = rproc->dev.parent;
102 int ret;
104 if (!rproc->has_iommu) {
105 dev_dbg(dev, "iommu not present\n");
106 return 0;
107 }
109 domain = iommu_domain_alloc(dev->bus);
110 if (!domain) {
111 dev_err(dev, "can't alloc iommu domain\n");
112 return -ENOMEM;
113 }
115 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
117 ret = iommu_attach_device(domain, dev);
118 if (ret) {
119 dev_err(dev, "can't attach iommu device: %d\n", ret);
120 goto free_domain;
121 }
123 rproc->domain = domain;
125 return 0;
127 free_domain:
128 iommu_domain_free(domain);
129 return ret;
130 }
132 static void rproc_disable_iommu(struct rproc *rproc)
133 {
134 struct iommu_domain *domain = rproc->domain;
135 struct device *dev = rproc->dev.parent;
137 if (!domain)
138 return;
140 iommu_detach_device(domain, dev);
141 iommu_domain_free(domain);
142 }
144 phys_addr_t rproc_va_to_pa(void *cpu_addr)
145 {
146 /*
147 * Return physical address according to virtual address location
148 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
149 * - in kernel: if region allocated in generic dma memory pool
150 */
151 if (is_vmalloc_addr(cpu_addr)) {
152 return page_to_phys(vmalloc_to_page(cpu_addr)) +
153 offset_in_page(cpu_addr);
154 }
156 WARN_ON(!virt_addr_valid(cpu_addr));
157 return virt_to_phys(cpu_addr);
158 }
159 EXPORT_SYMBOL(rproc_va_to_pa);
161 /**
162 * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
163 * @rproc: handle of a remote processor
164 * @da: remoteproc device address to translate
165 * @len: length of the memory region @da is pointing to
166 *
167 * Some remote processors will ask us to allocate them physically contiguous
168 * memory regions (which we call "carveouts"), and map them to specific
169 * device addresses (which are hardcoded in the firmware). They may also have
170 * dedicated memory regions internal to the processors, and use them either
171 * exclusively or alongside carveouts.
172 *
173 * They may then ask us to copy objects into specific device addresses (e.g.
174 * code/data sections) or expose us certain symbols in other device address
175 * (e.g. their trace buffer).
176 *
177 * This function is a helper function with which we can go over the allocated
178 * carveouts and translate specific device addresses to kernel virtual addresses
179 * so we can access the referenced memory. This function also allows to perform
180 * translations on the internal remoteproc memory regions through a platform
181 * implementation specific da_to_va ops, if present.
182 *
183 * The function returns a valid kernel address on success or NULL on failure.
184 *
185 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
186 * but only on kernel direct mapped RAM memory. Instead, we're just using
187 * here the output of the DMA API for the carveouts, which should be more
188 * correct.
189 */
190 void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
191 {
192 struct rproc_mem_entry *carveout;
193 void *ptr = NULL;
195 if (rproc->ops->da_to_va) {
196 ptr = rproc->ops->da_to_va(rproc, da, len);
197 if (ptr)
198 goto out;
199 }
201 list_for_each_entry(carveout, &rproc->carveouts, node) {
202 int offset = da - carveout->da;
204 /* Verify that carveout is allocated */
205 if (!carveout->va)
206 continue;
208 /* try next carveout if da is too small */
209 if (offset < 0)
210 continue;
212 /* try next carveout if da is too large */
213 if (offset + len > carveout->len)
214 continue;
216 ptr = carveout->va + offset;
218 break;
219 }
221 out:
222 return ptr;
223 }
224 EXPORT_SYMBOL(rproc_da_to_va);
226 /**
227 * rproc_pa_to_da() - lookup the rproc device address for a physical address
228 * @rproc: handle of a remote processor
229 * @pa: physical address of the buffer to translate
230 * @da: device address to return
231 *
232 * Communication clients of remote processors usually would need a means to
233 * convert a host buffer pointer to an equivalent device virtual address pointer
234 * that the code running on the remote processor can operate on. These buffer
235 * pointers can either be from the physically contiguous memory regions (or
236 * "carveouts") or can be some memory-mapped Device IO memory. This function
237 * provides a means to translate a given physical address to its associated
238 * device address.
239 *
240 * The function looks through both the carveouts and the device memory mappings
241 * since both of them are stored in separate lists.
242 *
243 * Returns 0 on success, or an appropriate error code otherwise. The translated
244 * device address is returned through the appropriate function argument.
245 */
246 int rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da)
247 {
248 int ret = -EINVAL;
249 struct rproc_mem_entry *maps = NULL;
251 if (!rproc || !da)
252 return -EINVAL;
254 if (mutex_lock_interruptible(&rproc->lock))
255 return -EINTR;
257 if (rproc->state == RPROC_RUNNING || rproc->state == RPROC_SUSPENDED) {
258 /* Look in the mappings first */
259 list_for_each_entry(maps, &rproc->mappings, node) {
260 if (pa >= maps->dma && pa < (maps->dma + maps->len)) {
261 *da = maps->da + (pa - maps->dma);
262 ret = 0;
263 goto exit;
264 }
265 }
266 /* If not, check in the carveouts */
267 list_for_each_entry(maps, &rproc->carveouts, node) {
268 if (pa >= maps->dma && pa < (maps->dma + maps->len)) {
269 *da = maps->da + (pa - maps->dma);
270 ret = 0;
271 break;
272 }
273 }
274 }
275 exit:
276 mutex_unlock(&rproc->lock);
277 return ret;
278 }
279 EXPORT_SYMBOL(rproc_pa_to_da);
281 /**
282 * rproc_find_carveout_by_name() - lookup the carveout region by a name
283 * @rproc: handle of a remote processor
284 * @name,..: carveout name to find (standard printf format)
285 *
286 * Platform driver has the capability to register some pre-allacoted carveout
287 * (physically contiguous memory regions) before rproc firmware loading and
288 * associated resource table analysis. These regions may be dedicated memory
289 * regions internal to the coprocessor or specified DDR region with specific
290 * attributes
291 *
292 * This function is a helper function with which we can go over the
293 * allocated carveouts and return associated region characteristics like
294 * coprocessor address, length or processor virtual address.
295 *
296 * Return: a valid pointer on carveout entry on success or NULL on failure.
297 */
298 struct rproc_mem_entry *
299 rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
300 {
301 va_list args;
302 char _name[32];
303 struct rproc_mem_entry *carveout, *mem = NULL;
305 if (!name)
306 return NULL;
308 va_start(args, name);
309 vsnprintf(_name, sizeof(_name), name, args);
310 va_end(args);
312 list_for_each_entry(carveout, &rproc->carveouts, node) {
313 /* Compare carveout and requested names */
314 if (!strcmp(carveout->name, _name)) {
315 mem = carveout;
316 break;
317 }
318 }
320 return mem;
321 }
323 /**
324 * rproc_check_carveout_da() - Check specified carveout da configuration
325 * @rproc: handle of a remote processor
326 * @mem: pointer on carveout to check
327 * @da: area device address
328 * @len: associated area size
329 *
330 * This function is a helper function to verify requested device area (couple
331 * da, len) is part of specified carveout.
332 * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
333 * checked.
334 *
335 * Return: 0 if carveout matches request else error
336 */
337 static int rproc_check_carveout_da(struct rproc *rproc,
338 struct rproc_mem_entry *mem, u32 da, u32 len)
339 {
340 struct device *dev = &rproc->dev;
341 int delta;
343 /* Check requested resource length */
344 if (len > mem->len) {
345 dev_err(dev, "Registered carveout doesn't fit len request\n");
346 return -EINVAL;
347 }
349 if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
350 /* Address doesn't match registered carveout configuration */
351 return -EINVAL;
352 } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
353 delta = da - mem->da;
355 /* Check requested resource belongs to registered carveout */
356 if (delta < 0) {
357 dev_err(dev,
358 "Registered carveout doesn't fit da request\n");
359 return -EINVAL;
360 }
362 if (delta + len > mem->len) {
363 dev_err(dev,
364 "Registered carveout doesn't fit len request\n");
365 return -EINVAL;
366 }
367 }
369 return 0;
370 }
372 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
373 {
374 struct rproc *rproc = rvdev->rproc;
375 struct device *dev = &rproc->dev;
376 struct rproc_vring *rvring = &rvdev->vring[i];
377 struct fw_rsc_vdev *rsc;
378 int ret, size, notifyid;
379 struct rproc_mem_entry *mem;
381 /* actual size of vring (in bytes) */
382 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
384 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
386 /* Search for pre-registered carveout */
387 mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
388 i);
389 if (mem) {
390 if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
391 return -ENOMEM;
392 } else {
393 /* Register carveout in in list */
394 mem = rproc_mem_entry_init(dev, NULL, 0,
395 size, rsc->vring[i].da,
396 rproc_alloc_carveout,
397 rproc_release_carveout,
398 "vdev%dvring%d",
399 rvdev->index, i);
400 if (!mem) {
401 dev_err(dev, "Can't allocate memory entry structure\n");
402 return -ENOMEM;
403 }
405 rproc_add_carveout(rproc, mem);
406 }
408 /*
409 * Assign an rproc-wide unique index for this vring
410 * TODO: assign a notifyid for rvdev updates as well
411 * TODO: support predefined notifyids (via resource table)
412 */
413 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
414 if (ret < 0) {
415 dev_err(dev, "idr_alloc failed: %d\n", ret);
416 return ret;
417 }
418 notifyid = ret;
420 /* Potentially bump max_notifyid */
421 if (notifyid > rproc->max_notifyid)
422 rproc->max_notifyid = notifyid;
424 rvring->notifyid = notifyid;
426 /* Let the rproc know the notifyid of this vring.*/
427 rsc->vring[i].notifyid = notifyid;
428 return 0;
429 }
431 static int
432 rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
433 {
434 struct rproc *rproc = rvdev->rproc;
435 struct device *dev = &rproc->dev;
436 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
437 struct rproc_vring *rvring = &rvdev->vring[i];
439 dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
440 i, vring->da, vring->num, vring->align);
442 /* verify queue size and vring alignment are sane */
443 if (!vring->num || !vring->align) {
444 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
445 vring->num, vring->align);
446 return -EINVAL;
447 }
449 rvring->len = vring->num;
450 rvring->align = vring->align;
451 rvring->rvdev = rvdev;
453 return 0;
454 }
456 void rproc_free_vring(struct rproc_vring *rvring)
457 {
458 struct rproc *rproc = rvring->rvdev->rproc;
459 int idx = rvring - rvring->rvdev->vring;
460 struct fw_rsc_vdev *rsc;
462 idr_remove(&rproc->notifyids, rvring->notifyid);
464 /* reset resource entry info */
465 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
466 rsc->vring[idx].da = 0;
467 rsc->vring[idx].notifyid = -1;
468 }
470 static int rproc_vdev_do_start(struct rproc_subdev *subdev)
471 {
472 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
474 return rproc_add_virtio_dev(rvdev, rvdev->id);
475 }
477 static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
478 {
479 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
480 int ret;
482 ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev);
483 if (ret)
484 dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret);
485 }
487 /**
488 * rproc_rvdev_release() - release the existence of a rvdev
489 *
490 * @dev: the subdevice's dev
491 */
492 static void rproc_rvdev_release(struct device *dev)
493 {
494 struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev);
496 of_reserved_mem_device_release(dev);
498 kfree(rvdev);
499 }
501 /**
502 * rproc_handle_vdev() - handle a vdev fw resource
503 * @rproc: the remote processor
504 * @rsc: the vring resource descriptor
505 * @avail: size of available data (for sanity checking the image)
506 *
507 * This resource entry requests the host to statically register a virtio
508 * device (vdev), and setup everything needed to support it. It contains
509 * everything needed to make it possible: the virtio device id, virtio
510 * device features, vrings information, virtio config space, etc...
511 *
512 * Before registering the vdev, the vrings are allocated from non-cacheable
513 * physically contiguous memory. Currently we only support two vrings per
514 * remote processor (temporary limitation). We might also want to consider
515 * doing the vring allocation only later when ->find_vqs() is invoked, and
516 * then release them upon ->del_vqs().
517 *
518 * Note: @da is currently not really handled correctly: we dynamically
519 * allocate it using the DMA API, ignoring requested hard coded addresses,
520 * and we don't take care of any required IOMMU programming. This is all
521 * going to be taken care of when the generic iommu-based DMA API will be
522 * merged. Meanwhile, statically-addressed iommu-based firmware images should
523 * use RSC_DEVMEM resource entries to map their required @da to the physical
524 * address of their base CMA region (ouch, hacky!).
525 *
526 * Returns 0 on success, or an appropriate error code otherwise
527 */
528 static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
529 int offset, int avail)
530 {
531 struct device *dev = &rproc->dev;
532 struct rproc_vdev *rvdev;
533 int i, ret;
534 char name[16];
536 /* make sure resource isn't truncated */
537 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
538 + rsc->config_len > avail) {
539 dev_err(dev, "vdev rsc is truncated\n");
540 return -EINVAL;
541 }
543 /* make sure reserved bytes are zeroes */
544 if (rsc->reserved[0] || rsc->reserved[1]) {
545 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
546 return -EINVAL;
547 }
549 dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
550 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
552 /* we currently support only two vrings per rvdev */
553 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
554 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
555 return -EINVAL;
556 }
558 rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
559 if (!rvdev)
560 return -ENOMEM;
562 kref_init(&rvdev->refcount);
564 rvdev->id = rsc->id;
565 rvdev->rproc = rproc;
566 rvdev->index = rproc->nb_vdev++;
568 /* Initialise vdev subdevice */
569 snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index);
570 rvdev->dev.parent = &rproc->dev;
571 rvdev->dev.dma_pfn_offset = rproc->dev.parent->dma_pfn_offset;
572 rvdev->dev.release = rproc_rvdev_release;
573 dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name);
574 dev_set_drvdata(&rvdev->dev, rvdev);
576 ret = device_register(&rvdev->dev);
577 if (ret) {
578 put_device(&rvdev->dev);
579 return ret;
580 }
581 /* Make device dma capable by inheriting from parent's capabilities */
582 set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent));
584 ret = dma_coerce_mask_and_coherent(&rvdev->dev,
585 dma_get_mask(rproc->dev.parent));
586 if (ret) {
587 dev_warn(dev,
588 "Failed to set DMA mask %llx. Trying to continue... %x\n",
589 dma_get_mask(rproc->dev.parent), ret);
590 }
592 /* parse the vrings */
593 for (i = 0; i < rsc->num_of_vrings; i++) {
594 ret = rproc_parse_vring(rvdev, rsc, i);
595 if (ret)
596 goto free_rvdev;
597 }
599 /* remember the resource offset*/
600 rvdev->rsc_offset = offset;
602 /* allocate the vring resources */
603 for (i = 0; i < rsc->num_of_vrings; i++) {
604 ret = rproc_alloc_vring(rvdev, i);
605 if (ret)
606 goto unwind_vring_allocations;
607 }
609 list_add_tail(&rvdev->node, &rproc->rvdevs);
611 rvdev->subdev.start = rproc_vdev_do_start;
612 rvdev->subdev.stop = rproc_vdev_do_stop;
614 rproc_add_subdev(rproc, &rvdev->subdev);
616 return 0;
618 unwind_vring_allocations:
619 for (i--; i >= 0; i--)
620 rproc_free_vring(&rvdev->vring[i]);
621 free_rvdev:
622 device_unregister(&rvdev->dev);
623 return ret;
624 }
626 void rproc_vdev_release(struct kref *ref)
627 {
628 struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
629 struct rproc_vring *rvring;
630 struct rproc *rproc = rvdev->rproc;
631 int id;
633 for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
634 rvring = &rvdev->vring[id];
635 rproc_free_vring(rvring);
636 }
638 rproc_remove_subdev(rproc, &rvdev->subdev);
639 list_del(&rvdev->node);
640 device_unregister(&rvdev->dev);
641 }
643 /**
644 * rproc_handle_trace() - handle a shared trace buffer resource
645 * @rproc: the remote processor
646 * @rsc: the trace resource descriptor
647 * @avail: size of available data (for sanity checking the image)
648 *
649 * In case the remote processor dumps trace logs into memory,
650 * export it via debugfs.
651 *
652 * Currently, the 'da' member of @rsc should contain the device address
653 * where the remote processor is dumping the traces. Later we could also
654 * support dynamically allocating this address using the generic
655 * DMA API (but currently there isn't a use case for that).
656 *
657 * Returns 0 on success, or an appropriate error code otherwise
658 */
659 static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
660 int offset, int avail)
661 {
662 struct rproc_debug_trace *trace;
663 struct device *dev = &rproc->dev;
664 char name[15];
666 if (sizeof(*rsc) > avail) {
667 dev_err(dev, "trace rsc is truncated\n");
668 return -EINVAL;
669 }
671 /* make sure reserved bytes are zeroes */
672 if (rsc->reserved) {
673 dev_err(dev, "trace rsc has non zero reserved bytes\n");
674 return -EINVAL;
675 }
677 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
678 if (!trace)
679 return -ENOMEM;
681 /* set the trace buffer dma properties */
682 trace->trace_mem.len = rsc->len;
683 trace->trace_mem.da = rsc->da;
685 /* set pointer on rproc device */
686 trace->rproc = rproc;
688 /* make sure snprintf always null terminates, even if truncating */
689 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
691 /* create the debugfs entry */
692 trace->tfile = rproc_create_trace_file(name, rproc, trace);
693 if (!trace->tfile) {
694 kfree(trace);
695 return -EINVAL;
696 }
698 list_add_tail(&trace->node, &rproc->traces);
700 rproc->num_traces++;
702 dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
703 name, rsc->da, rsc->len);
705 return 0;
706 }
708 /**
709 * rproc_handle_devmem() - handle devmem resource entry
710 * @rproc: remote processor handle
711 * @rsc: the devmem resource entry
712 * @avail: size of available data (for sanity checking the image)
713 *
714 * Remote processors commonly need to access certain on-chip peripherals.
715 *
716 * Some of these remote processors access memory via an iommu device,
717 * and might require us to configure their iommu before they can access
718 * the on-chip peripherals they need.
719 *
720 * This resource entry is a request to map such a peripheral device.
721 *
722 * These devmem entries will contain the physical address of the device in
723 * the 'pa' member. If a specific device address is expected, then 'da' will
724 * contain it (currently this is the only use case supported). 'len' will
725 * contain the size of the physical region we need to map.
726 *
727 * Currently we just "trust" those devmem entries to contain valid physical
728 * addresses, but this is going to change: we want the implementations to
729 * tell us ranges of physical addresses the firmware is allowed to request,
730 * and not allow firmwares to request access to physical addresses that
731 * are outside those ranges.
732 */
733 static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
734 int offset, int avail)
735 {
736 struct rproc_mem_entry *mapping;
737 struct device *dev = &rproc->dev;
738 int ret;
740 /* no point in handling this resource without a valid iommu domain */
741 if (!rproc->domain)
742 return -EINVAL;
744 if (sizeof(*rsc) > avail) {
745 dev_err(dev, "devmem rsc is truncated\n");
746 return -EINVAL;
747 }
749 /* make sure reserved bytes are zeroes */
750 if (rsc->reserved) {
751 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
752 return -EINVAL;
753 }
755 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
756 if (!mapping)
757 return -ENOMEM;
759 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
760 if (ret) {
761 dev_err(dev, "failed to map devmem: %d\n", ret);
762 goto out;
763 }
765 /*
766 * We'll need this info later when we'll want to unmap everything
767 * (e.g. on shutdown).
768 *
769 * We can't trust the remote processor not to change the resource
770 * table, so we must maintain this info independently.
771 */
772 mapping->dma = rsc->pa;
773 mapping->da = rsc->da;
774 mapping->len = rsc->len;
775 list_add_tail(&mapping->node, &rproc->mappings);
777 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
778 rsc->pa, rsc->da, rsc->len);
780 return 0;
782 out:
783 kfree(mapping);
784 return ret;
785 }
787 /**
788 * rproc_alloc_carveout() - allocated specified carveout
789 * @rproc: rproc handle
790 * @mem: the memory entry to allocate
791 *
792 * This function allocate specified memory entry @mem using
793 * dma_alloc_coherent() as default allocator
794 */
795 static int rproc_alloc_carveout(struct rproc *rproc,
796 struct rproc_mem_entry *mem)
797 {
798 struct rproc_mem_entry *mapping = NULL;
799 struct device *dev = &rproc->dev;
800 dma_addr_t dma;
801 void *va;
802 int ret;
804 va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
805 if (!va) {
806 dev_err(dev->parent,
807 "failed to allocate dma memory: len 0x%x\n", mem->len);
808 return -ENOMEM;
809 }
811 dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n",
812 va, &dma, mem->len);
814 if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
815 /*
816 * Check requested da is equal to dma address
817 * and print a warn message in case of missalignment.
818 * Don't stop rproc_start sequence as coprocessor may
819 * build pa to da translation on its side.
820 */
821 if (mem->da != (u32)dma)
822 dev_warn(dev->parent,
823 "Allocated carveout doesn't fit device address request\n");
824 }
826 /*
827 * Ok, this is non-standard.
828 *
829 * Sometimes we can't rely on the generic iommu-based DMA API
830 * to dynamically allocate the device address and then set the IOMMU
831 * tables accordingly, because some remote processors might
832 * _require_ us to use hard coded device addresses that their
833 * firmware was compiled with.
834 *
835 * In this case, we must use the IOMMU API directly and map
836 * the memory to the device address as expected by the remote
837 * processor.
838 *
839 * Obviously such remote processor devices should not be configured
840 * to use the iommu-based DMA API: we expect 'dma' to contain the
841 * physical address in this case.
842 */
843 if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
844 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
845 if (!mapping) {
846 ret = -ENOMEM;
847 goto dma_free;
848 }
850 ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
851 mem->flags);
852 if (ret) {
853 dev_err(dev, "iommu_map failed: %d\n", ret);
854 goto free_mapping;
855 }
857 /*
858 * We'll need this info later when we'll want to unmap
859 * everything (e.g. on shutdown).
860 *
861 * We can't trust the remote processor not to change the
862 * resource table, so we must maintain this info independently.
863 */
864 mapping->da = mem->da;
865 mapping->len = mem->len;
866 list_add_tail(&mapping->node, &rproc->mappings);
868 dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
869 mem->da, &dma);
870 }
872 if (mem->da == FW_RSC_ADDR_ANY) {
873 /* Update device address as undefined by requester */
874 if ((u64)dma & HIGH_BITS_MASK)
875 dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
877 mem->da = (u32)dma;
878 }
880 mem->dma = dma;
881 mem->va = va;
883 return 0;
885 free_mapping:
886 kfree(mapping);
887 dma_free:
888 dma_free_coherent(dev->parent, mem->len, va, dma);
889 return ret;
890 }
892 /**
893 * rproc_release_carveout() - release acquired carveout
894 * @rproc: rproc handle
895 * @mem: the memory entry to release
896 *
897 * This function releases specified memory entry @mem allocated via
898 * rproc_alloc_carveout() function by @rproc.
899 */
900 static int rproc_release_carveout(struct rproc *rproc,
901 struct rproc_mem_entry *mem)
902 {
903 struct device *dev = &rproc->dev;
905 /* clean up carveout allocations */
906 dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
907 return 0;
908 }
910 /**
911 * rproc_handle_carveout() - handle phys contig memory allocation requests
912 * @rproc: rproc handle
913 * @rsc: the resource entry
914 * @avail: size of available data (for image validation)
915 *
916 * This function will handle firmware requests for allocation of physically
917 * contiguous memory regions.
918 *
919 * These request entries should come first in the firmware's resource table,
920 * as other firmware entries might request placing other data objects inside
921 * these memory regions (e.g. data/code segments, trace resource entries, ...).
922 *
923 * Allocating memory this way helps utilizing the reserved physical memory
924 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
925 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
926 * pressure is important; it may have a substantial impact on performance.
927 */
928 static int rproc_handle_carveout(struct rproc *rproc,
929 struct fw_rsc_carveout *rsc,
930 int offset, int avail)
931 {
932 struct rproc_mem_entry *carveout;
933 struct device *dev = &rproc->dev;
935 if (sizeof(*rsc) > avail) {
936 dev_err(dev, "carveout rsc is truncated\n");
937 return -EINVAL;
938 }
940 /* make sure reserved bytes are zeroes */
941 if (rsc->reserved) {
942 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
943 return -EINVAL;
944 }
946 dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
947 rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
949 /*
950 * Check carveout rsc already part of a registered carveout,
951 * Search by name, then check the da and length
952 */
953 carveout = rproc_find_carveout_by_name(rproc, rsc->name);
955 if (carveout) {
956 if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
957 dev_err(dev,
958 "Carveout already associated to resource table\n");
959 return -ENOMEM;
960 }
962 if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
963 return -ENOMEM;
965 /* Update memory carveout with resource table info */
966 carveout->rsc_offset = offset;
967 carveout->flags = rsc->flags;
969 return 0;
970 }
972 /* Register carveout in in list */
973 carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
974 rproc_alloc_carveout,
975 rproc_release_carveout, rsc->name);
976 if (!carveout) {
977 dev_err(dev, "Can't allocate memory entry structure\n");
978 return -ENOMEM;
979 }
981 carveout->flags = rsc->flags;
982 carveout->rsc_offset = offset;
983 rproc_add_carveout(rproc, carveout);
985 return 0;
986 }
988 /**
989 * rproc_add_carveout() - register an allocated carveout region
990 * @rproc: rproc handle
991 * @mem: memory entry to register
992 *
993 * This function registers specified memory entry in @rproc carveouts list.
994 * Specified carveout should have been allocated before registering.
995 */
996 void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
997 {
998 list_add_tail(&mem->node, &rproc->carveouts);
999 }
1000 EXPORT_SYMBOL(rproc_add_carveout);
1002 /**
1003 * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
1004 * @dev: pointer on device struct
1005 * @va: virtual address
1006 * @dma: dma address
1007 * @len: memory carveout length
1008 * @da: device address
1009 * @alloc: memory carveout allocation function
1010 * @release: memory carveout release function
1011 * @name: carveout name
1012 *
1013 * This function allocates a rproc_mem_entry struct and fill it with parameters
1014 * provided by client.
1015 */
1016 struct rproc_mem_entry *
1017 rproc_mem_entry_init(struct device *dev,
1018 void *va, dma_addr_t dma, int len, u32 da,
1019 int (*alloc)(struct rproc *, struct rproc_mem_entry *),
1020 int (*release)(struct rproc *, struct rproc_mem_entry *),
1021 const char *name, ...)
1022 {
1023 struct rproc_mem_entry *mem;
1024 va_list args;
1026 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
1027 if (!mem)
1028 return mem;
1030 mem->va = va;
1031 mem->dma = dma;
1032 mem->da = da;
1033 mem->len = len;
1034 mem->alloc = alloc;
1035 mem->release = release;
1036 mem->rsc_offset = FW_RSC_ADDR_ANY;
1037 mem->of_resm_idx = -1;
1039 va_start(args, name);
1040 vsnprintf(mem->name, sizeof(mem->name), name, args);
1041 va_end(args);
1043 return mem;
1044 }
1045 EXPORT_SYMBOL(rproc_mem_entry_init);
1047 /**
1048 * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
1049 * from a reserved memory phandle
1050 * @dev: pointer on device struct
1051 * @of_resm_idx: reserved memory phandle index in "memory-region"
1052 * @len: memory carveout length
1053 * @da: device address
1054 * @name: carveout name
1055 *
1056 * This function allocates a rproc_mem_entry struct and fill it with parameters
1057 * provided by client.
1058 */
1059 struct rproc_mem_entry *
1060 rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, int len,
1061 u32 da, const char *name, ...)
1062 {
1063 struct rproc_mem_entry *mem;
1064 va_list args;
1066 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
1067 if (!mem)
1068 return mem;
1070 mem->da = da;
1071 mem->len = len;
1072 mem->rsc_offset = FW_RSC_ADDR_ANY;
1073 mem->of_resm_idx = of_resm_idx;
1075 va_start(args, name);
1076 vsnprintf(mem->name, sizeof(mem->name), name, args);
1077 va_end(args);
1079 return mem;
1080 }
1081 EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
1083 /**
1084 * A lookup table for resource handlers. The indices are defined in
1085 * enum fw_resource_type.
1086 */
1087 static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
1088 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
1089 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
1090 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
1091 [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
1092 };
1094 /* handle firmware resource entries before booting the remote processor */
1095 static int rproc_handle_resources(struct rproc *rproc,
1096 rproc_handle_resource_t handlers[RSC_LAST])
1097 {
1098 struct device *dev = &rproc->dev;
1099 rproc_handle_resource_t handler;
1100 int ret = 0, i;
1102 if (!rproc->table_ptr)
1103 return 0;
1105 for (i = 0; i < rproc->table_ptr->num; i++) {
1106 int offset = rproc->table_ptr->offset[i];
1107 struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1108 int avail = rproc->table_sz - offset - sizeof(*hdr);
1109 void *rsc = (void *)hdr + sizeof(*hdr);
1111 /* make sure table isn't truncated */
1112 if (avail < 0) {
1113 dev_err(dev, "rsc table is truncated\n");
1114 return -EINVAL;
1115 }
1117 dev_dbg(dev, "rsc: type %d\n", hdr->type);
1119 if (hdr->type >= RSC_VENDOR_START &&
1120 hdr->type <= RSC_VENDOR_END) {
1121 ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1122 offset + sizeof(*hdr), avail);
1123 if (ret == RSC_HANDLED)
1124 continue;
1125 else if (ret < 0)
1126 break;
1128 dev_warn(dev, "unsupported vendor resource %d\n",
1129 hdr->type);
1130 continue;
1131 }
1133 if (hdr->type >= RSC_LAST) {
1134 dev_warn(dev, "unsupported resource %d\n", hdr->type);
1135 continue;
1136 }
1138 handler = handlers[hdr->type];
1139 if (!handler)
1140 continue;
1142 ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
1143 if (ret)
1144 break;
1145 }
1147 return ret;
1148 }
1150 static int rproc_prepare_subdevices(struct rproc *rproc)
1151 {
1152 struct rproc_subdev *subdev;
1153 int ret;
1155 list_for_each_entry(subdev, &rproc->subdevs, node) {
1156 if (subdev->prepare) {
1157 ret = subdev->prepare(subdev);
1158 if (ret)
1159 goto unroll_preparation;
1160 }
1161 }
1163 return 0;
1165 unroll_preparation:
1166 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1167 if (subdev->unprepare)
1168 subdev->unprepare(subdev);
1169 }
1171 return ret;
1172 }
1174 static int rproc_start_subdevices(struct rproc *rproc)
1175 {
1176 struct rproc_subdev *subdev;
1177 int ret;
1179 list_for_each_entry(subdev, &rproc->subdevs, node) {
1180 if (subdev->start) {
1181 ret = subdev->start(subdev);
1182 if (ret)
1183 goto unroll_registration;
1184 }
1185 }
1187 return 0;
1189 unroll_registration:
1190 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1191 if (subdev->stop)
1192 subdev->stop(subdev, true);
1193 }
1195 return ret;
1196 }
1198 static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
1199 {
1200 struct rproc_subdev *subdev;
1202 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1203 if (subdev->stop)
1204 subdev->stop(subdev, crashed);
1205 }
1206 }
1208 static void rproc_unprepare_subdevices(struct rproc *rproc)
1209 {
1210 struct rproc_subdev *subdev;
1212 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1213 if (subdev->unprepare)
1214 subdev->unprepare(subdev);
1215 }
1216 }
1218 /**
1219 * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1220 * in the list
1221 * @rproc: the remote processor handle
1222 *
1223 * This function parses registered carveout list, performs allocation
1224 * if alloc() ops registered and updates resource table information
1225 * if rsc_offset set.
1226 *
1227 * Return: 0 on success
1228 */
1229 static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1230 {
1231 struct rproc_mem_entry *entry, *tmp;
1232 struct fw_rsc_carveout *rsc;
1233 struct device *dev = &rproc->dev;
1234 u64 pa;
1235 int ret;
1237 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1238 if (entry->alloc) {
1239 ret = entry->alloc(rproc, entry);
1240 if (ret) {
1241 dev_err(dev, "Unable to allocate carveout %s: %d\n",
1242 entry->name, ret);
1243 return -ENOMEM;
1244 }
1245 }
1247 if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1248 /* update resource table */
1249 rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1251 /*
1252 * Some remote processors might need to know the pa
1253 * even though they are behind an IOMMU. E.g., OMAP4's
1254 * remote M3 processor needs this so it can control
1255 * on-chip hardware accelerators that are not behind
1256 * the IOMMU, and therefor must know the pa.
1257 *
1258 * Generally we don't want to expose physical addresses
1259 * if we don't have to (remote processors are generally
1260 * _not_ trusted), so we might want to do this only for
1261 * remote processor that _must_ have this (e.g. OMAP4's
1262 * dual M3 subsystem).
1263 *
1264 * Non-IOMMU processors might also want to have this info.
1265 * In this case, the device address and the physical address
1266 * are the same.
1267 */
1269 /* Use va if defined else dma to generate pa */
1270 if (entry->va)
1271 pa = (u64)rproc_va_to_pa(entry->va);
1272 else
1273 pa = (u64)entry->dma;
1275 if (((u64)pa) & HIGH_BITS_MASK)
1276 dev_warn(dev,
1277 "Physical address cast in 32bit to fit resource table format\n");
1279 rsc->pa = (u32)pa;
1280 rsc->da = entry->da;
1281 rsc->len = entry->len;
1282 }
1283 }
1285 return 0;
1286 }
1288 /**
1289 * rproc_coredump_cleanup() - clean up dump_segments list
1290 * @rproc: the remote processor handle
1291 */
1292 static void rproc_coredump_cleanup(struct rproc *rproc)
1293 {
1294 struct rproc_dump_segment *entry, *tmp;
1296 list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
1297 list_del(&entry->node);
1298 kfree(entry);
1299 }
1300 }
1302 /**
1303 * rproc_resource_cleanup() - clean up and free all acquired resources
1304 * @rproc: rproc handle
1305 *
1306 * This function will free all resources acquired for @rproc, and it
1307 * is called whenever @rproc either shuts down or fails to boot.
1308 */
1309 static void rproc_resource_cleanup(struct rproc *rproc)
1310 {
1311 struct rproc_mem_entry *entry, *tmp;
1312 struct rproc_debug_trace *trace, *ttmp;
1313 struct rproc_vdev *rvdev, *rvtmp;
1314 struct device *dev = &rproc->dev;
1316 /* clean up debugfs trace entries */
1317 list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1318 rproc_remove_trace_file(trace->tfile);
1319 rproc->num_traces--;
1320 list_del(&trace->node);
1321 kfree(trace);
1322 }
1324 /* clean up iommu mapping entries */
1325 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1326 size_t unmapped;
1328 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1329 if (unmapped != entry->len) {
1330 /* nothing much to do besides complaining */
1331 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
1332 unmapped);
1333 }
1335 list_del(&entry->node);
1336 kfree(entry);
1337 }
1339 /* clean up carveout allocations */
1340 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1341 if (entry->release)
1342 entry->release(rproc, entry);
1343 list_del(&entry->node);
1344 kfree(entry);
1345 }
1347 /* clean up remote vdev entries */
1348 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
1349 kref_put(&rvdev->refcount, rproc_vdev_release);
1351 rproc_coredump_cleanup(rproc);
1352 }
1354 static int rproc_start(struct rproc *rproc, const struct firmware *fw)
1355 {
1356 struct resource_table *loaded_table;
1357 struct device *dev = &rproc->dev;
1358 int ret;
1360 if (!rproc->skip_load) {
1361 /* load the ELF segments to memory */
1362 ret = rproc_load_segments(rproc, fw);
1363 if (ret) {
1364 dev_err(dev, "Failed to load program segments: %d\n",
1365 ret);
1366 return ret;
1367 }
1368 }
1370 /*
1371 * The starting device has been given the rproc->cached_table as the
1372 * resource table. The address of the vring along with the other
1373 * allocated resources (carveouts etc) is stored in cached_table.
1374 * In order to pass this information to the remote device we must copy
1375 * this information to device memory. We also update the table_ptr so
1376 * that any subsequent changes will be applied to the loaded version.
1377 */
1378 loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
1379 if (loaded_table) {
1380 memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
1381 rproc->table_ptr = loaded_table;
1382 }
1384 ret = rproc_prepare_subdevices(rproc);
1385 if (ret) {
1386 dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1387 rproc->name, ret);
1388 goto reset_table_ptr;
1389 }
1391 /* power up the remote processor */
1392 ret = rproc->ops->start(rproc);
1393 if (ret) {
1394 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1395 goto unprepare_subdevices;
1396 }
1398 /* Start any subdevices for the remote processor */
1399 ret = rproc_start_subdevices(rproc);
1400 if (ret) {
1401 dev_err(dev, "failed to probe subdevices for %s: %d\n",
1402 rproc->name, ret);
1403 goto stop_rproc;
1404 }
1406 rproc->state = RPROC_RUNNING;
1408 dev_info(dev, "remote processor %s is now up\n", rproc->name);
1410 return 0;
1412 stop_rproc:
1413 rproc->ops->stop(rproc);
1414 unprepare_subdevices:
1415 rproc_unprepare_subdevices(rproc);
1416 reset_table_ptr:
1417 rproc->table_ptr = rproc->cached_table;
1419 return ret;
1420 }
1422 /*
1423 * take a firmware and boot a remote processor with it.
1424 */
1425 static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1426 {
1427 struct device *dev = &rproc->dev;
1428 const char *name = rproc->firmware;
1429 int ret;
1431 ret = rproc_fw_sanity_check(rproc, fw);
1432 if (ret)
1433 return ret;
1435 if (!rproc->skip_firmware_request)
1436 dev_info(dev, "Booting fw image %s, size %zd\n",
1437 name, fw->size);
1438 else
1439 dev_info(dev, "Booting unspecified pre-loaded fw image\n");
1441 /*
1442 * if enabling an IOMMU isn't relevant for this rproc, this is
1443 * just a nop
1444 */
1445 ret = rproc_enable_iommu(rproc);
1446 if (ret) {
1447 dev_err(dev, "can't enable iommu: %d\n", ret);
1448 return ret;
1449 }
1451 /* Prepare rproc for firmware loading if needed */
1452 if (rproc->ops->prepare) {
1453 ret = rproc->ops->prepare(rproc);
1454 if (ret) {
1455 dev_err(dev, "can't prepare rproc %s: %d\n",
1456 rproc->name, ret);
1457 goto disable_iommu;
1458 }
1459 }
1461 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1463 /* Load resource table, core dump segment list etc from the firmware */
1464 ret = rproc_parse_fw(rproc, fw);
1465 if (ret)
1466 goto unprepare_rproc;
1468 /* reset max_notifyid */
1469 rproc->max_notifyid = -1;
1471 /* reset handled vdev */
1472 rproc->nb_vdev = 0;
1474 /* handle fw resources which are required to boot rproc */
1475 ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1476 if (ret) {
1477 dev_err(dev, "Failed to process resources: %d\n", ret);
1478 goto clean_up_resources;
1479 }
1481 /* Allocate carveout resources associated to rproc */
1482 ret = rproc_alloc_registered_carveouts(rproc);
1483 if (ret) {
1484 dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1485 ret);
1486 goto clean_up_resources;
1487 }
1489 ret = rproc_start(rproc, fw);
1490 if (ret)
1491 goto clean_up_resources;
1493 return 0;
1495 clean_up_resources:
1496 rproc_resource_cleanup(rproc);
1497 kfree(rproc->cached_table);
1498 rproc->cached_table = NULL;
1499 rproc->table_ptr = NULL;
1500 unprepare_rproc:
1501 /* release HW resources if needed */
1502 if (rproc->ops->unprepare)
1503 rproc->ops->unprepare(rproc);
1504 disable_iommu:
1505 rproc_disable_iommu(rproc);
1506 return ret;
1507 }
1509 /*
1510 * take a firmware and boot it up.
1511 *
1512 * Note: this function is called asynchronously upon registration of the
1513 * remote processor (so we must wait until it completes before we try
1514 * to unregister the device. one other option is just to use kref here,
1515 * that might be cleaner).
1516 */
1517 static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1518 {
1519 struct rproc *rproc = context;
1521 rproc_boot(rproc);
1523 release_firmware(fw);
1524 }
1526 static int rproc_trigger_auto_boot(struct rproc *rproc)
1527 {
1528 int ret;
1530 /*
1531 * We're initiating an asynchronous firmware loading, so we can
1532 * be built-in kernel code, without hanging the boot process.
1533 */
1534 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1535 rproc->firmware, &rproc->dev, GFP_KERNEL,
1536 rproc, rproc_auto_boot_callback);
1537 if (ret < 0)
1538 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
1540 return ret;
1541 }
1543 static int rproc_stop(struct rproc *rproc, bool crashed)
1544 {
1545 struct device *dev = &rproc->dev;
1546 int ret;
1548 /* Stop any subdevices for the remote processor */
1549 rproc_stop_subdevices(rproc, crashed);
1551 /* the installed resource table is no longer accessible */
1552 rproc->table_ptr = rproc->cached_table;
1554 /* power off the remote processor */
1555 ret = rproc->ops->stop(rproc);
1556 if (ret) {
1557 dev_err(dev, "can't stop rproc: %d\n", ret);
1558 return ret;
1559 }
1561 rproc_unprepare_subdevices(rproc);
1563 rproc->state = RPROC_OFFLINE;
1565 dev_info(dev, "stopped remote processor %s\n", rproc->name);
1567 return 0;
1568 }
1570 /**
1571 * rproc_coredump_add_segment() - add segment of device memory to coredump
1572 * @rproc: handle of a remote processor
1573 * @da: device address
1574 * @size: size of segment
1575 *
1576 * Add device memory to the list of segments to be included in a coredump for
1577 * the remoteproc.
1578 *
1579 * Return: 0 on success, negative errno on error.
1580 */
1581 int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
1582 {
1583 struct rproc_dump_segment *segment;
1585 segment = kzalloc(sizeof(*segment), GFP_KERNEL);
1586 if (!segment)
1587 return -ENOMEM;
1589 segment->da = da;
1590 segment->size = size;
1592 list_add_tail(&segment->node, &rproc->dump_segments);
1594 return 0;
1595 }
1596 EXPORT_SYMBOL(rproc_coredump_add_segment);
1598 /**
1599 * rproc_coredump_add_custom_segment() - add custom coredump segment
1600 * @rproc: handle of a remote processor
1601 * @da: device address
1602 * @size: size of segment
1603 * @dumpfn: custom dump function called for each segment during coredump
1604 * @priv: private data
1605 *
1606 * Add device memory to the list of segments to be included in the coredump
1607 * and associate the segment with the given custom dump function and private
1608 * data.
1609 *
1610 * Return: 0 on success, negative errno on error.
1611 */
1612 int rproc_coredump_add_custom_segment(struct rproc *rproc,
1613 dma_addr_t da, size_t size,
1614 void (*dumpfn)(struct rproc *rproc,
1615 struct rproc_dump_segment *segment,
1616 void *dest),
1617 void *priv)
1618 {
1619 struct rproc_dump_segment *segment;
1621 segment = kzalloc(sizeof(*segment), GFP_KERNEL);
1622 if (!segment)
1623 return -ENOMEM;
1625 segment->da = da;
1626 segment->size = size;
1627 segment->priv = priv;
1628 segment->dump = dumpfn;
1630 list_add_tail(&segment->node, &rproc->dump_segments);
1632 return 0;
1633 }
1634 EXPORT_SYMBOL(rproc_coredump_add_custom_segment);
1636 /**
1637 * rproc_coredump() - perform coredump
1638 * @rproc: rproc handle
1639 *
1640 * This function will generate an ELF header for the registered segments
1641 * and create a devcoredump device associated with rproc.
1642 */
1643 static void rproc_coredump(struct rproc *rproc)
1644 {
1645 struct rproc_dump_segment *segment;
1646 struct elf32_phdr *phdr;
1647 struct elf32_hdr *ehdr;
1648 size_t data_size;
1649 size_t offset;
1650 void *data;
1651 void *ptr;
1652 int phnum = 0;
1654 if (list_empty(&rproc->dump_segments))
1655 return;
1657 data_size = sizeof(*ehdr);
1658 list_for_each_entry(segment, &rproc->dump_segments, node) {
1659 data_size += sizeof(*phdr) + segment->size;
1661 phnum++;
1662 }
1664 data = vmalloc(data_size);
1665 if (!data)
1666 return;
1668 ehdr = data;
1670 memset(ehdr, 0, sizeof(*ehdr));
1671 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1672 ehdr->e_ident[EI_CLASS] = ELFCLASS32;
1673 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1674 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1675 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1676 ehdr->e_type = ET_CORE;
1677 ehdr->e_machine = EM_NONE;
1678 ehdr->e_version = EV_CURRENT;
1679 ehdr->e_entry = rproc->bootaddr;
1680 ehdr->e_phoff = sizeof(*ehdr);
1681 ehdr->e_ehsize = sizeof(*ehdr);
1682 ehdr->e_phentsize = sizeof(*phdr);
1683 ehdr->e_phnum = phnum;
1685 phdr = data + ehdr->e_phoff;
1686 offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum;
1687 list_for_each_entry(segment, &rproc->dump_segments, node) {
1688 memset(phdr, 0, sizeof(*phdr));
1689 phdr->p_type = PT_LOAD;
1690 phdr->p_offset = offset;
1691 phdr->p_vaddr = segment->da;
1692 phdr->p_paddr = segment->da;
1693 phdr->p_filesz = segment->size;
1694 phdr->p_memsz = segment->size;
1695 phdr->p_flags = PF_R | PF_W | PF_X;
1696 phdr->p_align = 0;
1698 if (segment->dump) {
1699 segment->dump(rproc, segment, data + offset);
1700 } else {
1701 ptr = rproc_da_to_va(rproc, segment->da, segment->size);
1702 if (!ptr) {
1703 dev_err(&rproc->dev,
1704 "invalid coredump segment (%pad, %zu)\n",
1705 &segment->da, segment->size);
1706 memset(data + offset, 0xff, segment->size);
1707 } else {
1708 memcpy(data + offset, ptr, segment->size);
1709 }
1710 }
1712 offset += phdr->p_filesz;
1713 phdr++;
1714 }
1716 dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
1717 }
1719 /**
1720 * rproc_trigger_recovery() - recover a remoteproc
1721 * @rproc: the remote processor
1722 *
1723 * The recovery is done by resetting all the virtio devices, that way all the
1724 * rpmsg drivers will be reseted along with the remote processor making the
1725 * remoteproc functional again.
1726 *
1727 * This function can sleep, so it cannot be called from atomic context.
1728 */
1729 int rproc_trigger_recovery(struct rproc *rproc)
1730 {
1731 const struct firmware *firmware_p;
1732 struct device *dev = &rproc->dev;
1733 int ret;
1735 dev_err(dev, "recovering %s\n", rproc->name);
1737 ret = mutex_lock_interruptible(&rproc->lock);
1738 if (ret)
1739 return ret;
1741 ret = rproc_stop(rproc, true);
1742 if (ret)
1743 goto unlock_mutex;
1745 /* generate coredump */
1746 rproc_coredump(rproc);
1748 /* load firmware */
1749 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1750 if (ret < 0) {
1751 dev_err(dev, "request_firmware failed: %d\n", ret);
1752 goto unlock_mutex;
1753 }
1755 /* boot the remote processor up again */
1756 ret = rproc_start(rproc, firmware_p);
1758 release_firmware(firmware_p);
1760 unlock_mutex:
1761 mutex_unlock(&rproc->lock);
1762 return ret;
1763 }
1765 /**
1766 * rproc_crash_handler_work() - handle a crash
1767 *
1768 * This function needs to handle everything related to a crash, like cpu
1769 * registers and stack dump, information to help to debug the fatal error, etc.
1770 */
1771 static void rproc_crash_handler_work(struct work_struct *work)
1772 {
1773 struct rproc *rproc = container_of(work, struct rproc, crash_handler);
1774 struct device *dev = &rproc->dev;
1776 dev_dbg(dev, "enter %s\n", __func__);
1778 mutex_lock(&rproc->lock);
1780 if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
1781 /* handle only the first crash detected */
1782 mutex_unlock(&rproc->lock);
1783 return;
1784 }
1786 rproc->state = RPROC_CRASHED;
1787 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
1788 rproc->name);
1790 mutex_unlock(&rproc->lock);
1792 if (!rproc->recovery_disabled)
1793 rproc_trigger_recovery(rproc);
1794 }
1796 /**
1797 * rproc_get_id() - return the id for the rproc device
1798 * @rproc: handle of a remote processor
1799 *
1800 * Each rproc device is associated with a platform device, which is created
1801 * either from device tree (majority newer platforms) or using legacy style
1802 * platform device creation (fewer legacy platforms). This function retrieves
1803 * an unique id for each remote processor and is useful for clients needing
1804 * to distinguish each of the remoteprocs. This unique id is derived using
1805 * the platform device id for non-DT devices, or an alternate alias id for
1806 * DT devices (since they do not have a valid platform device id). It is
1807 * assumed that the platform devices were created with known ids or were
1808 * given proper alias ids using the stem "rproc".
1809 *
1810 * Return: alias id for DT devices or platform device id for non-DT devices
1811 * associated with the rproc
1812 */
1813 int rproc_get_id(struct rproc *rproc)
1814 {
1815 struct device *dev = rproc->dev.parent;
1816 struct device_node *np = dev->of_node;
1817 struct platform_device *pdev = to_platform_device(dev);
1819 if (np)
1820 return of_alias_get_id(np, "rproc");
1821 else
1822 return pdev->id;
1823 }
1824 EXPORT_SYMBOL(rproc_get_id);
1826 /**
1827 * rproc_boot() - boot a remote processor
1828 * @rproc: handle of a remote processor
1829 *
1830 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1831 *
1832 * If the remote processor is already powered on, this function immediately
1833 * returns (successfully).
1834 *
1835 * Returns 0 on success, and an appropriate error value otherwise.
1836 */
1837 int rproc_boot(struct rproc *rproc)
1838 {
1839 const struct firmware *firmware_p;
1840 struct device *dev;
1841 int ret;
1843 if (!rproc) {
1844 pr_err("invalid rproc handle\n");
1845 return -EINVAL;
1846 }
1848 dev = &rproc->dev;
1850 ret = mutex_lock_interruptible(&rproc->lock);
1851 if (ret) {
1852 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1853 return ret;
1854 }
1856 if (rproc->state == RPROC_DELETED) {
1857 ret = -ENODEV;
1858 dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
1859 goto unlock_mutex;
1860 }
1862 /* skip the boot process if rproc is already powered up */
1863 if (atomic_inc_return(&rproc->power) > 1) {
1864 ret = 0;
1865 goto unlock_mutex;
1866 }
1868 dev_info(dev, "powering up %s\n", rproc->name);
1870 if (!rproc->skip_firmware_request) {
1871 /* load firmware */
1872 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1873 if (ret < 0) {
1874 dev_err(dev, "request_firmware failed: %d\n", ret);
1875 goto downref_rproc;
1876 }
1877 }
1879 ret = rproc_fw_boot(rproc, firmware_p);
1881 if (!rproc->skip_firmware_request)
1882 release_firmware(firmware_p);
1884 downref_rproc:
1885 if (ret)
1886 atomic_dec(&rproc->power);
1887 unlock_mutex:
1888 mutex_unlock(&rproc->lock);
1889 return ret;
1890 }
1891 EXPORT_SYMBOL(rproc_boot);
1893 /**
1894 * rproc_shutdown() - power off the remote processor
1895 * @rproc: the remote processor
1896 *
1897 * Power off a remote processor (previously booted with rproc_boot()).
1898 *
1899 * In case @rproc is still being used by an additional user(s), then
1900 * this function will just decrement the power refcount and exit,
1901 * without really powering off the device.
1902 *
1903 * Every call to rproc_boot() must (eventually) be accompanied by a call
1904 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1905 *
1906 * Notes:
1907 * - we're not decrementing the rproc's refcount, only the power refcount.
1908 * which means that the @rproc handle stays valid even after rproc_shutdown()
1909 * returns, and users can still use it with a subsequent rproc_boot(), if
1910 * needed.
1911 */
1912 void rproc_shutdown(struct rproc *rproc)
1913 {
1914 struct device *dev = &rproc->dev;
1915 int ret;
1917 ret = mutex_lock_interruptible(&rproc->lock);
1918 if (ret) {
1919 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1920 return;
1921 }
1923 /* if the remote proc is still needed, bail out */
1924 if (!atomic_dec_and_test(&rproc->power))
1925 goto out;
1927 ret = rproc_stop(rproc, false);
1928 if (ret) {
1929 atomic_inc(&rproc->power);
1930 goto out;
1931 }
1933 /* clean up all acquired resources */
1934 rproc_resource_cleanup(rproc);
1936 /* release HW resources if needed */
1937 if (rproc->ops->unprepare)
1938 rproc->ops->unprepare(rproc);
1940 rproc_disable_iommu(rproc);
1942 /* Free the copy of the resource table */
1943 kfree(rproc->cached_table);
1944 rproc->cached_table = NULL;
1945 rproc->table_ptr = NULL;
1946 out:
1947 mutex_unlock(&rproc->lock);
1948 }
1949 EXPORT_SYMBOL(rproc_shutdown);
1951 /**
1952 * rproc_get_by_phandle() - find a remote processor by phandle
1953 * @phandle: phandle to the rproc
1954 *
1955 * Finds an rproc handle using the remote processor's phandle, and then
1956 * return a handle to the rproc.
1957 *
1958 * This function increments the remote processor's refcount, so always
1959 * use rproc_put() to decrement it back once rproc isn't needed anymore.
1960 *
1961 * Returns the rproc handle on success, and NULL on failure.
1962 */
1963 #ifdef CONFIG_OF
1964 struct rproc *rproc_get_by_phandle(phandle phandle)
1965 {
1966 struct rproc *rproc = NULL, *r;
1967 struct device_node *np;
1969 np = of_find_node_by_phandle(phandle);
1970 if (!np)
1971 return NULL;
1973 mutex_lock(&rproc_list_mutex);
1974 list_for_each_entry(r, &rproc_list, node) {
1975 if (r->dev.parent && r->dev.parent->of_node == np) {
1976 /* prevent underlying implementation from being removed */
1977 if (!try_module_get(r->dev.parent->driver->owner)) {
1978 dev_err(&r->dev, "can't get owner\n");
1979 break;
1980 }
1982 rproc = r;
1983 get_device(&rproc->dev);
1984 break;
1985 }
1986 }
1987 mutex_unlock(&rproc_list_mutex);
1989 of_node_put(np);
1991 return rproc;
1992 }
1993 #else
1994 struct rproc *rproc_get_by_phandle(phandle phandle)
1995 {
1996 return NULL;
1997 }
1998 #endif
1999 EXPORT_SYMBOL(rproc_get_by_phandle);
2001 /**
2002 * rproc_add() - register a remote processor
2003 * @rproc: the remote processor handle to register
2004 *
2005 * Registers @rproc with the remoteproc framework, after it has been
2006 * allocated with rproc_alloc().
2007 *
2008 * This is called by the platform-specific rproc implementation, whenever
2009 * a new remote processor device is probed.
2010 *
2011 * Returns 0 on success and an appropriate error code otherwise.
2012 *
2013 * Note: this function initiates an asynchronous firmware loading
2014 * context, which will look for virtio devices supported by the rproc's
2015 * firmware.
2016 *
2017 * If found, those virtio devices will be created and added, so as a result
2018 * of registering this remote processor, additional virtio drivers might be
2019 * probed.
2020 */
2021 int rproc_add(struct rproc *rproc)
2022 {
2023 struct device *dev = &rproc->dev;
2024 int ret;
2026 ret = device_add(dev);
2027 if (ret < 0)
2028 return ret;
2030 dev_info(dev, "%s is available\n", rproc->name);
2032 /* create debugfs entries */
2033 rproc_create_debug_dir(rproc);
2035 /* if rproc is marked always-on, request it to boot */
2036 if (rproc->auto_boot) {
2037 ret = rproc_trigger_auto_boot(rproc);
2038 if (ret < 0)
2039 return ret;
2040 }
2042 /* expose to rproc_get_by_phandle users */
2043 mutex_lock(&rproc_list_mutex);
2044 list_add(&rproc->node, &rproc_list);
2045 mutex_unlock(&rproc_list_mutex);
2047 return 0;
2048 }
2049 EXPORT_SYMBOL(rproc_add);
2051 /**
2052 * rproc_type_release() - release a remote processor instance
2053 * @dev: the rproc's device
2054 *
2055 * This function should _never_ be called directly.
2056 *
2057 * It will be called by the driver core when no one holds a valid pointer
2058 * to @dev anymore.
2059 */
2060 static void rproc_type_release(struct device *dev)
2061 {
2062 struct rproc *rproc = container_of(dev, struct rproc, dev);
2064 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
2066 idr_destroy(&rproc->notifyids);
2068 if (rproc->index >= 0)
2069 ida_simple_remove(&rproc_dev_index, rproc->index);
2071 kfree(rproc->firmware);
2072 kfree(rproc->ops);
2073 kfree(rproc->name);
2074 kfree(rproc);
2075 }
2077 static const struct device_type rproc_type = {
2078 .name = "remoteproc",
2079 .release = rproc_type_release,
2080 };
2082 /**
2083 * rproc_alloc() - allocate a remote processor handle
2084 * @dev: the underlying device
2085 * @name: name of this remote processor
2086 * @ops: platform-specific handlers (mainly start/stop)
2087 * @firmware: name of firmware file to load, can be NULL
2088 * @len: length of private data needed by the rproc driver (in bytes)
2089 *
2090 * Allocates a new remote processor handle, but does not register
2091 * it yet. if @firmware is NULL, a default name is used.
2092 *
2093 * This function should be used by rproc implementations during initialization
2094 * of the remote processor.
2095 *
2096 * After creating an rproc handle using this function, and when ready,
2097 * implementations should then call rproc_add() to complete
2098 * the registration of the remote processor.
2099 *
2100 * On success the new rproc is returned, and on failure, NULL.
2101 *
2102 * Note: _never_ directly deallocate @rproc, even if it was not registered
2103 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2104 */
2105 struct rproc *rproc_alloc(struct device *dev, const char *name,
2106 const struct rproc_ops *ops,
2107 const char *firmware, int len)
2108 {
2109 struct rproc *rproc;
2110 char *p, *template = "rproc-%s-fw";
2111 int name_len;
2113 if (!dev || !name || !ops)
2114 return NULL;
2116 if (!firmware) {
2117 /*
2118 * If the caller didn't pass in a firmware name then
2119 * construct a default name.
2120 */
2121 name_len = strlen(name) + strlen(template) - 2 + 1;
2122 p = kmalloc(name_len, GFP_KERNEL);
2123 if (!p)
2124 return NULL;
2125 snprintf(p, name_len, template, name);
2126 } else {
2127 p = kstrdup(firmware, GFP_KERNEL);
2128 if (!p)
2129 return NULL;
2130 }
2132 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
2133 if (!rproc) {
2134 kfree(p);
2135 return NULL;
2136 }
2138 rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2139 if (!rproc->ops) {
2140 kfree(p);
2141 kfree(rproc);
2142 return NULL;
2143 }
2145 rproc->firmware = p;
2146 rproc->name = kstrdup(name, GFP_KERNEL);
2147 if (!rproc->name) {
2148 kfree(p);
2149 kfree(rproc->ops);
2150 kfree(rproc);
2151 return NULL;
2152 }
2153 rproc->priv = &rproc[1];
2154 rproc->auto_boot = true;
2156 device_initialize(&rproc->dev);
2157 rproc->dev.parent = dev;
2158 rproc->dev.type = &rproc_type;
2159 rproc->dev.class = &rproc_class;
2160 rproc->dev.driver_data = rproc;
2162 /* Assign a unique device index and name */
2163 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
2164 if (rproc->index < 0) {
2165 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
2166 put_device(&rproc->dev);
2167 return NULL;
2168 }
2170 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2172 atomic_set(&rproc->power, 0);
2174 /* Default to ELF loader if no load function is specified */
2175 if (!rproc->ops->load) {
2176 rproc->ops->load = rproc_elf_load_segments;
2177 rproc->ops->parse_fw = rproc_elf_load_rsc_table;
2178 rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
2179 rproc->ops->sanity_check = rproc_elf_sanity_check;
2180 rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
2181 }
2183 mutex_init(&rproc->lock);
2185 idr_init(&rproc->notifyids);
2187 INIT_LIST_HEAD(&rproc->carveouts);
2188 INIT_LIST_HEAD(&rproc->mappings);
2189 INIT_LIST_HEAD(&rproc->traces);
2190 INIT_LIST_HEAD(&rproc->rvdevs);
2191 INIT_LIST_HEAD(&rproc->subdevs);
2192 INIT_LIST_HEAD(&rproc->dump_segments);
2194 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
2196 rproc->state = RPROC_OFFLINE;
2198 return rproc;
2199 }
2200 EXPORT_SYMBOL(rproc_alloc);
2202 /**
2203 * rproc_free() - unroll rproc_alloc()
2204 * @rproc: the remote processor handle
2205 *
2206 * This function decrements the rproc dev refcount.
2207 *
2208 * If no one holds any reference to rproc anymore, then its refcount would
2209 * now drop to zero, and it would be freed.
2210 */
2211 void rproc_free(struct rproc *rproc)
2212 {
2213 put_device(&rproc->dev);
2214 }
2215 EXPORT_SYMBOL(rproc_free);
2217 /**
2218 * rproc_put() - release rproc reference
2219 * @rproc: the remote processor handle
2220 *
2221 * This function decrements the rproc dev refcount.
2222 *
2223 * If no one holds any reference to rproc anymore, then its refcount would
2224 * now drop to zero, and it would be freed.
2225 */
2226 void rproc_put(struct rproc *rproc)
2227 {
2228 module_put(rproc->dev.parent->driver->owner);
2229 put_device(&rproc->dev);
2230 }
2231 EXPORT_SYMBOL(rproc_put);
2233 /**
2234 * rproc_del() - unregister a remote processor
2235 * @rproc: rproc handle to unregister
2236 *
2237 * This function should be called when the platform specific rproc
2238 * implementation decides to remove the rproc device. it should
2239 * _only_ be called if a previous invocation of rproc_add()
2240 * has completed successfully.
2241 *
2242 * After rproc_del() returns, @rproc isn't freed yet, because
2243 * of the outstanding reference created by rproc_alloc. To decrement that
2244 * one last refcount, one still needs to call rproc_free().
2245 *
2246 * Returns 0 on success and -EINVAL if @rproc isn't valid.
2247 */
2248 int rproc_del(struct rproc *rproc)
2249 {
2250 if (!rproc)
2251 return -EINVAL;
2253 /* if rproc is marked always-on, rproc_add() booted it */
2254 /* TODO: make sure this works with rproc->power > 1 */
2255 if (rproc->auto_boot)
2256 rproc_shutdown(rproc);
2258 mutex_lock(&rproc->lock);
2259 rproc->state = RPROC_DELETED;
2260 mutex_unlock(&rproc->lock);
2262 rproc_delete_debug_dir(rproc);
2264 /* the rproc is downref'ed as soon as it's removed from the klist */
2265 mutex_lock(&rproc_list_mutex);
2266 list_del(&rproc->node);
2267 mutex_unlock(&rproc_list_mutex);
2269 device_del(&rproc->dev);
2271 return 0;
2272 }
2273 EXPORT_SYMBOL(rproc_del);
2275 /**
2276 * rproc_add_subdev() - add a subdevice to a remoteproc
2277 * @rproc: rproc handle to add the subdevice to
2278 * @subdev: subdev handle to register
2279 *
2280 * Caller is responsible for populating optional subdevice function pointers.
2281 */
2282 void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
2283 {
2284 list_add_tail(&subdev->node, &rproc->subdevs);
2285 }
2286 EXPORT_SYMBOL(rproc_add_subdev);
2288 /**
2289 * rproc_remove_subdev() - remove a subdevice from a remoteproc
2290 * @rproc: rproc handle to remove the subdevice from
2291 * @subdev: subdev handle, previously registered with rproc_add_subdev()
2292 */
2293 void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
2294 {
2295 list_del(&subdev->node);
2296 }
2297 EXPORT_SYMBOL(rproc_remove_subdev);
2299 /**
2300 * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
2301 * @dev: child device to find ancestor of
2302 *
2303 * Returns the ancestor rproc instance, or NULL if not found.
2304 */
2305 struct rproc *rproc_get_by_child(struct device *dev)
2306 {
2307 for (dev = dev->parent; dev; dev = dev->parent) {
2308 if (dev->type == &rproc_type)
2309 return dev->driver_data;
2310 }
2312 return NULL;
2313 }
2314 EXPORT_SYMBOL(rproc_get_by_child);
2316 /**
2317 * rproc_report_crash() - rproc crash reporter function
2318 * @rproc: remote processor
2319 * @type: crash type
2320 *
2321 * This function must be called every time a crash is detected by the low-level
2322 * drivers implementing a specific remoteproc. This should not be called from a
2323 * non-remoteproc driver.
2324 *
2325 * This function can be called from atomic/interrupt context.
2326 */
2327 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
2328 {
2329 if (!rproc) {
2330 pr_err("NULL rproc pointer\n");
2331 return;
2332 }
2334 dev_err(&rproc->dev, "crash detected in %s: type %s\n",
2335 rproc->name, rproc_crash_to_string(type));
2337 /* create a new task to handle the error */
2338 schedule_work(&rproc->crash_handler);
2339 }
2340 EXPORT_SYMBOL(rproc_report_crash);
2342 static int __init remoteproc_init(void)
2343 {
2344 rproc_init_sysfs();
2345 rproc_init_debugfs();
2347 return 0;
2348 }
2349 module_init(remoteproc_init);
2351 static void __exit remoteproc_exit(void)
2352 {
2353 ida_destroy(&rproc_dev_index);
2355 rproc_exit_debugfs();
2356 rproc_exit_sysfs();
2357 }
2358 module_exit(remoteproc_exit);
2360 MODULE_LICENSE("GPL v2");
2361 MODULE_DESCRIPTION("Generic Remote Processor Framework");