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 */
16
17 #define pr_fmt(fmt) "%s: " fmt, __func__
18
19 #include <linux/delay.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/panic_notifier.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/dma-map-ops.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/dma-direct.h> /* XXX: pokes into bus_dma_range */
29 #include <linux/firmware.h>
30 #include <linux/string.h>
31 #include <linux/debugfs.h>
32 #include <linux/rculist.h>
33 #include <linux/remoteproc.h>
34 #include <linux/iommu.h>
35 #include <linux/idr.h>
36 #include <linux/elf.h>
37 #include <linux/crc32.h>
38 #include <linux/of_reserved_mem.h>
39 #include <linux/virtio_ids.h>
40 #include <linux/virtio_ring.h>
41 #include <asm/byteorder.h>
42 #include <linux/platform_device.h>
43
44 #include "remoteproc_internal.h"
45
46 #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
47
48 static DEFINE_MUTEX(rproc_list_mutex);
49 static LIST_HEAD(rproc_list);
50 static struct notifier_block rproc_panic_nb;
51
52 typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
53 void *, int offset, int avail);
54
55 static int rproc_alloc_carveout(struct rproc *rproc,
56 struct rproc_mem_entry *mem);
57 static int rproc_release_carveout(struct rproc *rproc,
58 struct rproc_mem_entry *mem);
59
60 /* Unique indices for remoteproc devices */
61 static DEFINE_IDA(rproc_dev_index);
62
63 static const char * const rproc_crash_names[] = {
64 [RPROC_MMUFAULT] = "mmufault",
65 [RPROC_WATCHDOG] = "watchdog",
66 [RPROC_FATAL_ERROR] = "fatal error",
67 };
68
69 /* translate rproc_crash_type to string */
rproc_crash_to_string(enum rproc_crash_type type)70 static const char *rproc_crash_to_string(enum rproc_crash_type type)
71 {
72 if (type < ARRAY_SIZE(rproc_crash_names))
73 return rproc_crash_names[type];
74 return "unknown";
75 }
76
77 /*
78 * This is the IOMMU fault handler we register with the IOMMU API
79 * (when relevant; not all remote processors access memory through
80 * an IOMMU).
81 *
82 * IOMMU core will invoke this handler whenever the remote processor
83 * will try to access an unmapped device address.
84 */
rproc_iommu_fault(struct iommu_domain * domain,struct device * dev,unsigned long iova,int flags,void * token)85 static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
86 unsigned long iova, int flags, void *token)
87 {
88 struct rproc *rproc = token;
89
90 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
91
92 rproc_report_crash(rproc, RPROC_MMUFAULT);
93
94 /*
95 * Let the iommu core know we're not really handling this fault;
96 * we just used it as a recovery trigger.
97 */
98 return -ENOSYS;
99 }
100
rproc_enable_iommu(struct rproc * rproc)101 static int rproc_enable_iommu(struct rproc *rproc)
102 {
103 struct iommu_domain *domain;
104 struct device *dev = rproc->dev.parent;
105 int ret;
106
107 if (!rproc->has_iommu) {
108 dev_dbg(dev, "iommu not present\n");
109 return 0;
110 }
111
112 domain = iommu_domain_alloc(dev->bus);
113 if (!domain) {
114 dev_err(dev, "can't alloc iommu domain\n");
115 return -ENOMEM;
116 }
117
118 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
119
120 ret = iommu_attach_device(domain, dev);
121 if (ret) {
122 dev_err(dev, "can't attach iommu device: %d\n", ret);
123 goto free_domain;
124 }
125
126 rproc->domain = domain;
127
128 return 0;
129
130 free_domain:
131 iommu_domain_free(domain);
132 return ret;
133 }
134
rproc_disable_iommu(struct rproc * rproc)135 static void rproc_disable_iommu(struct rproc *rproc)
136 {
137 struct iommu_domain *domain = rproc->domain;
138 struct device *dev = rproc->dev.parent;
139
140 if (!domain)
141 return;
142
143 iommu_detach_device(domain, dev);
144 iommu_domain_free(domain);
145 }
146
rproc_va_to_pa(void * cpu_addr)147 phys_addr_t rproc_va_to_pa(void *cpu_addr)
148 {
149 /*
150 * Return physical address according to virtual address location
151 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
152 * - in kernel: if region allocated in generic dma memory pool
153 */
154 if (is_vmalloc_addr(cpu_addr)) {
155 return page_to_phys(vmalloc_to_page(cpu_addr)) +
156 offset_in_page(cpu_addr);
157 }
158
159 WARN_ON(!virt_addr_valid(cpu_addr));
160 return virt_to_phys(cpu_addr);
161 }
162 EXPORT_SYMBOL(rproc_va_to_pa);
163
164 /**
165 * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
166 * @rproc: handle of a remote processor
167 * @da: remoteproc device address to translate
168 * @len: length of the memory region @da is pointing to
169 * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory
170 *
171 * Some remote processors will ask us to allocate them physically contiguous
172 * memory regions (which we call "carveouts"), and map them to specific
173 * device addresses (which are hardcoded in the firmware). They may also have
174 * dedicated memory regions internal to the processors, and use them either
175 * exclusively or alongside carveouts.
176 *
177 * They may then ask us to copy objects into specific device addresses (e.g.
178 * code/data sections) or expose us certain symbols in other device address
179 * (e.g. their trace buffer).
180 *
181 * This function is a helper function with which we can go over the allocated
182 * carveouts and translate specific device addresses to kernel virtual addresses
183 * so we can access the referenced memory. This function also allows to perform
184 * translations on the internal remoteproc memory regions through a platform
185 * implementation specific da_to_va ops, if present.
186 *
187 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
188 * but only on kernel direct mapped RAM memory. Instead, we're just using
189 * here the output of the DMA API for the carveouts, which should be more
190 * correct.
191 *
192 * Return: a valid kernel address on success or NULL on failure
193 */
rproc_da_to_va(struct rproc * rproc,u64 da,size_t len,bool * is_iomem)194 void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
195 {
196 struct rproc_mem_entry *carveout;
197 void *ptr = NULL;
198
199 if (rproc->ops->da_to_va) {
200 ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem);
201 if (ptr)
202 goto out;
203 }
204
205 list_for_each_entry(carveout, &rproc->carveouts, node) {
206 int offset = da - carveout->da;
207
208 /* Verify that carveout is allocated */
209 if (!carveout->va)
210 continue;
211
212 /* try next carveout if da is too small */
213 if (offset < 0)
214 continue;
215
216 /* try next carveout if da is too large */
217 if (offset + len > carveout->len)
218 continue;
219
220 ptr = carveout->va + offset;
221
222 if (is_iomem)
223 *is_iomem = carveout->is_iomem;
224
225 break;
226 }
227
228 out:
229 return ptr;
230 }
231 EXPORT_SYMBOL(rproc_da_to_va);
232
233 /**
234 * rproc_find_carveout_by_name() - lookup the carveout region by a name
235 * @rproc: handle of a remote processor
236 * @name: carveout name to find (format string)
237 * @...: optional parameters matching @name string
238 *
239 * Platform driver has the capability to register some pre-allacoted carveout
240 * (physically contiguous memory regions) before rproc firmware loading and
241 * associated resource table analysis. These regions may be dedicated memory
242 * regions internal to the coprocessor or specified DDR region with specific
243 * attributes
244 *
245 * This function is a helper function with which we can go over the
246 * allocated carveouts and return associated region characteristics like
247 * coprocessor address, length or processor virtual address.
248 *
249 * Return: a valid pointer on carveout entry on success or NULL on failure.
250 */
251 __printf(2, 3)
252 struct rproc_mem_entry *
rproc_find_carveout_by_name(struct rproc * rproc,const char * name,...)253 rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
254 {
255 va_list args;
256 char _name[32];
257 struct rproc_mem_entry *carveout, *mem = NULL;
258
259 if (!name)
260 return NULL;
261
262 va_start(args, name);
263 vsnprintf(_name, sizeof(_name), name, args);
264 va_end(args);
265
266 list_for_each_entry(carveout, &rproc->carveouts, node) {
267 /* Compare carveout and requested names */
268 if (!strcmp(carveout->name, _name)) {
269 mem = carveout;
270 break;
271 }
272 }
273
274 return mem;
275 }
276
277 /**
278 * rproc_check_carveout_da() - Check specified carveout da configuration
279 * @rproc: handle of a remote processor
280 * @mem: pointer on carveout to check
281 * @da: area device address
282 * @len: associated area size
283 *
284 * This function is a helper function to verify requested device area (couple
285 * da, len) is part of specified carveout.
286 * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
287 * checked.
288 *
289 * Return: 0 if carveout matches request else error
290 */
rproc_check_carveout_da(struct rproc * rproc,struct rproc_mem_entry * mem,u32 da,u32 len)291 static int rproc_check_carveout_da(struct rproc *rproc,
292 struct rproc_mem_entry *mem, u32 da, u32 len)
293 {
294 struct device *dev = &rproc->dev;
295 int delta;
296
297 /* Check requested resource length */
298 if (len > mem->len) {
299 dev_err(dev, "Registered carveout doesn't fit len request\n");
300 return -EINVAL;
301 }
302
303 if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
304 /* Address doesn't match registered carveout configuration */
305 return -EINVAL;
306 } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
307 delta = da - mem->da;
308
309 /* Check requested resource belongs to registered carveout */
310 if (delta < 0) {
311 dev_err(dev,
312 "Registered carveout doesn't fit da request\n");
313 return -EINVAL;
314 }
315
316 if (delta + len > mem->len) {
317 dev_err(dev,
318 "Registered carveout doesn't fit len request\n");
319 return -EINVAL;
320 }
321 }
322
323 return 0;
324 }
325
rproc_alloc_vring(struct rproc_vdev * rvdev,int i)326 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
327 {
328 struct rproc *rproc = rvdev->rproc;
329 struct device *dev = &rproc->dev;
330 struct rproc_vring *rvring = &rvdev->vring[i];
331 struct fw_rsc_vdev *rsc;
332 int ret, notifyid;
333 struct rproc_mem_entry *mem;
334 size_t size;
335
336 /* actual size of vring (in bytes) */
337 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
338
339 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
340
341 /* Search for pre-registered carveout */
342 mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
343 i);
344 if (mem) {
345 if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
346 return -ENOMEM;
347 } else {
348 /* Register carveout in in list */
349 mem = rproc_mem_entry_init(dev, NULL, 0,
350 size, rsc->vring[i].da,
351 rproc_alloc_carveout,
352 rproc_release_carveout,
353 "vdev%dvring%d",
354 rvdev->index, i);
355 if (!mem) {
356 dev_err(dev, "Can't allocate memory entry structure\n");
357 return -ENOMEM;
358 }
359
360 rproc_add_carveout(rproc, mem);
361 }
362
363 /*
364 * Assign an rproc-wide unique index for this vring
365 * TODO: assign a notifyid for rvdev updates as well
366 * TODO: support predefined notifyids (via resource table)
367 */
368 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
369 if (ret < 0) {
370 dev_err(dev, "idr_alloc failed: %d\n", ret);
371 return ret;
372 }
373 notifyid = ret;
374
375 /* Potentially bump max_notifyid */
376 if (notifyid > rproc->max_notifyid)
377 rproc->max_notifyid = notifyid;
378
379 rvring->notifyid = notifyid;
380
381 /* Let the rproc know the notifyid of this vring.*/
382 rsc->vring[i].notifyid = notifyid;
383 return 0;
384 }
385
386 static int
rproc_parse_vring(struct rproc_vdev * rvdev,struct fw_rsc_vdev * rsc,int i)387 rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
388 {
389 struct rproc *rproc = rvdev->rproc;
390 struct device *dev = &rproc->dev;
391 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
392 struct rproc_vring *rvring = &rvdev->vring[i];
393
394 dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
395 i, vring->da, vring->num, vring->align);
396
397 /* verify queue size and vring alignment are sane */
398 if (!vring->num || !vring->align) {
399 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
400 vring->num, vring->align);
401 return -EINVAL;
402 }
403
404 rvring->len = vring->num;
405 rvring->align = vring->align;
406 rvring->rvdev = rvdev;
407
408 return 0;
409 }
410
rproc_free_vring(struct rproc_vring * rvring)411 void rproc_free_vring(struct rproc_vring *rvring)
412 {
413 struct rproc *rproc = rvring->rvdev->rproc;
414 int idx = rvring - rvring->rvdev->vring;
415 struct fw_rsc_vdev *rsc;
416
417 idr_remove(&rproc->notifyids, rvring->notifyid);
418
419 /*
420 * At this point rproc_stop() has been called and the installed resource
421 * table in the remote processor memory may no longer be accessible. As
422 * such and as per rproc_stop(), rproc->table_ptr points to the cached
423 * resource table (rproc->cached_table). The cached resource table is
424 * only available when a remote processor has been booted by the
425 * remoteproc core, otherwise it is NULL.
426 *
427 * Based on the above, reset the virtio device section in the cached
428 * resource table only if there is one to work with.
429 */
430 if (rproc->table_ptr) {
431 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
432 rsc->vring[idx].da = 0;
433 rsc->vring[idx].notifyid = -1;
434 }
435 }
436
rproc_vdev_do_start(struct rproc_subdev * subdev)437 static int rproc_vdev_do_start(struct rproc_subdev *subdev)
438 {
439 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
440
441 return rproc_add_virtio_dev(rvdev, rvdev->id);
442 }
443
rproc_vdev_do_stop(struct rproc_subdev * subdev,bool crashed)444 static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
445 {
446 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
447 int ret;
448
449 ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev);
450 if (ret)
451 dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret);
452 }
453
454 /**
455 * rproc_rvdev_release() - release the existence of a rvdev
456 *
457 * @dev: the subdevice's dev
458 */
rproc_rvdev_release(struct device * dev)459 static void rproc_rvdev_release(struct device *dev)
460 {
461 struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev);
462
463 of_reserved_mem_device_release(dev);
464
465 kfree(rvdev);
466 }
467
copy_dma_range_map(struct device * to,struct device * from)468 static int copy_dma_range_map(struct device *to, struct device *from)
469 {
470 const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;
471 int num_ranges = 0;
472
473 if (!map)
474 return 0;
475
476 for (r = map; r->size; r++)
477 num_ranges++;
478
479 new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)),
480 GFP_KERNEL);
481 if (!new_map)
482 return -ENOMEM;
483 to->dma_range_map = new_map;
484 return 0;
485 }
486
487 /**
488 * rproc_handle_vdev() - handle a vdev fw resource
489 * @rproc: the remote processor
490 * @ptr: the vring resource descriptor
491 * @offset: offset of the resource entry
492 * @avail: size of available data (for sanity checking the image)
493 *
494 * This resource entry requests the host to statically register a virtio
495 * device (vdev), and setup everything needed to support it. It contains
496 * everything needed to make it possible: the virtio device id, virtio
497 * device features, vrings information, virtio config space, etc...
498 *
499 * Before registering the vdev, the vrings are allocated from non-cacheable
500 * physically contiguous memory. Currently we only support two vrings per
501 * remote processor (temporary limitation). We might also want to consider
502 * doing the vring allocation only later when ->find_vqs() is invoked, and
503 * then release them upon ->del_vqs().
504 *
505 * Note: @da is currently not really handled correctly: we dynamically
506 * allocate it using the DMA API, ignoring requested hard coded addresses,
507 * and we don't take care of any required IOMMU programming. This is all
508 * going to be taken care of when the generic iommu-based DMA API will be
509 * merged. Meanwhile, statically-addressed iommu-based firmware images should
510 * use RSC_DEVMEM resource entries to map their required @da to the physical
511 * address of their base CMA region (ouch, hacky!).
512 *
513 * Return: 0 on success, or an appropriate error code otherwise
514 */
rproc_handle_vdev(struct rproc * rproc,void * ptr,int offset,int avail)515 static int rproc_handle_vdev(struct rproc *rproc, void *ptr,
516 int offset, int avail)
517 {
518 struct fw_rsc_vdev *rsc = ptr;
519 struct device *dev = &rproc->dev;
520 struct rproc_vdev *rvdev;
521 int i, ret;
522 char name[16];
523
524 /* make sure resource isn't truncated */
525 if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len >
526 avail) {
527 dev_err(dev, "vdev rsc is truncated\n");
528 return -EINVAL;
529 }
530
531 /* make sure reserved bytes are zeroes */
532 if (rsc->reserved[0] || rsc->reserved[1]) {
533 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
534 return -EINVAL;
535 }
536
537 dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
538 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
539
540 /* we currently support only two vrings per rvdev */
541 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
542 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
543 return -EINVAL;
544 }
545
546 rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
547 if (!rvdev)
548 return -ENOMEM;
549
550 kref_init(&rvdev->refcount);
551
552 rvdev->id = rsc->id;
553 rvdev->rproc = rproc;
554 rvdev->index = rproc->nb_vdev++;
555
556 /* Initialise vdev subdevice */
557 snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index);
558 rvdev->dev.parent = &rproc->dev;
559 rvdev->dev.release = rproc_rvdev_release;
560 dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name);
561 dev_set_drvdata(&rvdev->dev, rvdev);
562
563 ret = device_register(&rvdev->dev);
564 if (ret) {
565 put_device(&rvdev->dev);
566 return ret;
567 }
568
569 ret = copy_dma_range_map(&rvdev->dev, rproc->dev.parent);
570 if (ret)
571 goto free_rvdev;
572
573 /* Make device dma capable by inheriting from parent's capabilities */
574 set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent));
575
576 ret = dma_coerce_mask_and_coherent(&rvdev->dev,
577 dma_get_mask(rproc->dev.parent));
578 if (ret) {
579 dev_warn(dev,
580 "Failed to set DMA mask %llx. Trying to continue... (%pe)\n",
581 dma_get_mask(rproc->dev.parent), ERR_PTR(ret));
582 }
583
584 /* parse the vrings */
585 for (i = 0; i < rsc->num_of_vrings; i++) {
586 ret = rproc_parse_vring(rvdev, rsc, i);
587 if (ret)
588 goto free_rvdev;
589 }
590
591 /* remember the resource offset*/
592 rvdev->rsc_offset = offset;
593
594 /* allocate the vring resources */
595 for (i = 0; i < rsc->num_of_vrings; i++) {
596 ret = rproc_alloc_vring(rvdev, i);
597 if (ret)
598 goto unwind_vring_allocations;
599 }
600
601 list_add_tail(&rvdev->node, &rproc->rvdevs);
602
603 rvdev->subdev.start = rproc_vdev_do_start;
604 rvdev->subdev.stop = rproc_vdev_do_stop;
605
606 rproc_add_subdev(rproc, &rvdev->subdev);
607
608 return 0;
609
610 unwind_vring_allocations:
611 for (i--; i >= 0; i--)
612 rproc_free_vring(&rvdev->vring[i]);
613 free_rvdev:
614 device_unregister(&rvdev->dev);
615 return ret;
616 }
617
rproc_vdev_release(struct kref * ref)618 void rproc_vdev_release(struct kref *ref)
619 {
620 struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
621 struct rproc_vring *rvring;
622 struct rproc *rproc = rvdev->rproc;
623 int id;
624
625 for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
626 rvring = &rvdev->vring[id];
627 rproc_free_vring(rvring);
628 }
629
630 rproc_remove_subdev(rproc, &rvdev->subdev);
631 list_del(&rvdev->node);
632 device_unregister(&rvdev->dev);
633 }
634
635 /**
636 * rproc_handle_trace() - handle a shared trace buffer resource
637 * @rproc: the remote processor
638 * @ptr: the trace resource descriptor
639 * @offset: offset of the resource entry
640 * @avail: size of available data (for sanity checking the image)
641 *
642 * In case the remote processor dumps trace logs into memory,
643 * export it via debugfs.
644 *
645 * Currently, the 'da' member of @rsc should contain the device address
646 * where the remote processor is dumping the traces. Later we could also
647 * support dynamically allocating this address using the generic
648 * DMA API (but currently there isn't a use case for that).
649 *
650 * Return: 0 on success, or an appropriate error code otherwise
651 */
rproc_handle_trace(struct rproc * rproc,void * ptr,int offset,int avail)652 static int rproc_handle_trace(struct rproc *rproc, void *ptr,
653 int offset, int avail)
654 {
655 struct fw_rsc_trace *rsc = ptr;
656 struct rproc_debug_trace *trace;
657 struct device *dev = &rproc->dev;
658 char name[15];
659
660 if (sizeof(*rsc) > avail) {
661 dev_err(dev, "trace rsc is truncated\n");
662 return -EINVAL;
663 }
664
665 /* make sure reserved bytes are zeroes */
666 if (rsc->reserved) {
667 dev_err(dev, "trace rsc has non zero reserved bytes\n");
668 return -EINVAL;
669 }
670
671 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
672 if (!trace)
673 return -ENOMEM;
674
675 /* set the trace buffer dma properties */
676 trace->trace_mem.len = rsc->len;
677 trace->trace_mem.da = rsc->da;
678
679 /* set pointer on rproc device */
680 trace->rproc = rproc;
681
682 /* make sure snprintf always null terminates, even if truncating */
683 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
684
685 /* create the debugfs entry */
686 trace->tfile = rproc_create_trace_file(name, rproc, trace);
687
688 list_add_tail(&trace->node, &rproc->traces);
689
690 rproc->num_traces++;
691
692 dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
693 name, rsc->da, rsc->len);
694
695 return 0;
696 }
697
698 /**
699 * rproc_handle_devmem() - handle devmem resource entry
700 * @rproc: remote processor handle
701 * @ptr: the devmem resource entry
702 * @offset: offset of the resource entry
703 * @avail: size of available data (for sanity checking the image)
704 *
705 * Remote processors commonly need to access certain on-chip peripherals.
706 *
707 * Some of these remote processors access memory via an iommu device,
708 * and might require us to configure their iommu before they can access
709 * the on-chip peripherals they need.
710 *
711 * This resource entry is a request to map such a peripheral device.
712 *
713 * These devmem entries will contain the physical address of the device in
714 * the 'pa' member. If a specific device address is expected, then 'da' will
715 * contain it (currently this is the only use case supported). 'len' will
716 * contain the size of the physical region we need to map.
717 *
718 * Currently we just "trust" those devmem entries to contain valid physical
719 * addresses, but this is going to change: we want the implementations to
720 * tell us ranges of physical addresses the firmware is allowed to request,
721 * and not allow firmwares to request access to physical addresses that
722 * are outside those ranges.
723 *
724 * Return: 0 on success, or an appropriate error code otherwise
725 */
rproc_handle_devmem(struct rproc * rproc,void * ptr,int offset,int avail)726 static int rproc_handle_devmem(struct rproc *rproc, void *ptr,
727 int offset, int avail)
728 {
729 struct fw_rsc_devmem *rsc = ptr;
730 struct rproc_mem_entry *mapping;
731 struct device *dev = &rproc->dev;
732 int ret;
733
734 /* no point in handling this resource without a valid iommu domain */
735 if (!rproc->domain)
736 return -EINVAL;
737
738 if (sizeof(*rsc) > avail) {
739 dev_err(dev, "devmem rsc is truncated\n");
740 return -EINVAL;
741 }
742
743 /* make sure reserved bytes are zeroes */
744 if (rsc->reserved) {
745 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
746 return -EINVAL;
747 }
748
749 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
750 if (!mapping)
751 return -ENOMEM;
752
753 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
754 if (ret) {
755 dev_err(dev, "failed to map devmem: %d\n", ret);
756 goto out;
757 }
758
759 /*
760 * We'll need this info later when we'll want to unmap everything
761 * (e.g. on shutdown).
762 *
763 * We can't trust the remote processor not to change the resource
764 * table, so we must maintain this info independently.
765 */
766 mapping->da = rsc->da;
767 mapping->len = rsc->len;
768 list_add_tail(&mapping->node, &rproc->mappings);
769
770 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
771 rsc->pa, rsc->da, rsc->len);
772
773 return 0;
774
775 out:
776 kfree(mapping);
777 return ret;
778 }
779
780 /**
781 * rproc_alloc_carveout() - allocated specified carveout
782 * @rproc: rproc handle
783 * @mem: the memory entry to allocate
784 *
785 * This function allocate specified memory entry @mem using
786 * dma_alloc_coherent() as default allocator
787 *
788 * Return: 0 on success, or an appropriate error code otherwise
789 */
rproc_alloc_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)790 static int rproc_alloc_carveout(struct rproc *rproc,
791 struct rproc_mem_entry *mem)
792 {
793 struct rproc_mem_entry *mapping = NULL;
794 struct device *dev = &rproc->dev;
795 dma_addr_t dma;
796 void *va;
797 int ret;
798
799 va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
800 if (!va) {
801 dev_err(dev->parent,
802 "failed to allocate dma memory: len 0x%zx\n",
803 mem->len);
804 return -ENOMEM;
805 }
806
807 dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n",
808 va, &dma, mem->len);
809
810 if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
811 /*
812 * Check requested da is equal to dma address
813 * and print a warn message in case of missalignment.
814 * Don't stop rproc_start sequence as coprocessor may
815 * build pa to da translation on its side.
816 */
817 if (mem->da != (u32)dma)
818 dev_warn(dev->parent,
819 "Allocated carveout doesn't fit device address request\n");
820 }
821
822 /*
823 * Ok, this is non-standard.
824 *
825 * Sometimes we can't rely on the generic iommu-based DMA API
826 * to dynamically allocate the device address and then set the IOMMU
827 * tables accordingly, because some remote processors might
828 * _require_ us to use hard coded device addresses that their
829 * firmware was compiled with.
830 *
831 * In this case, we must use the IOMMU API directly and map
832 * the memory to the device address as expected by the remote
833 * processor.
834 *
835 * Obviously such remote processor devices should not be configured
836 * to use the iommu-based DMA API: we expect 'dma' to contain the
837 * physical address in this case.
838 */
839 if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
840 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
841 if (!mapping) {
842 ret = -ENOMEM;
843 goto dma_free;
844 }
845
846 ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
847 mem->flags);
848 if (ret) {
849 dev_err(dev, "iommu_map failed: %d\n", ret);
850 goto free_mapping;
851 }
852
853 /*
854 * We'll need this info later when we'll want to unmap
855 * everything (e.g. on shutdown).
856 *
857 * We can't trust the remote processor not to change the
858 * resource table, so we must maintain this info independently.
859 */
860 mapping->da = mem->da;
861 mapping->len = mem->len;
862 list_add_tail(&mapping->node, &rproc->mappings);
863
864 dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
865 mem->da, &dma);
866 }
867
868 if (mem->da == FW_RSC_ADDR_ANY) {
869 /* Update device address as undefined by requester */
870 if ((u64)dma & HIGH_BITS_MASK)
871 dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
872
873 mem->da = (u32)dma;
874 }
875
876 mem->dma = dma;
877 mem->va = va;
878
879 return 0;
880
881 free_mapping:
882 kfree(mapping);
883 dma_free:
884 dma_free_coherent(dev->parent, mem->len, va, dma);
885 return ret;
886 }
887
888 /**
889 * rproc_release_carveout() - release acquired carveout
890 * @rproc: rproc handle
891 * @mem: the memory entry to release
892 *
893 * This function releases specified memory entry @mem allocated via
894 * rproc_alloc_carveout() function by @rproc.
895 *
896 * Return: 0 on success, or an appropriate error code otherwise
897 */
rproc_release_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)898 static int rproc_release_carveout(struct rproc *rproc,
899 struct rproc_mem_entry *mem)
900 {
901 struct device *dev = &rproc->dev;
902
903 /* clean up carveout allocations */
904 dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
905 return 0;
906 }
907
908 /**
909 * rproc_handle_carveout() - handle phys contig memory allocation requests
910 * @rproc: rproc handle
911 * @ptr: the resource entry
912 * @offset: offset of the resource entry
913 * @avail: size of available data (for image validation)
914 *
915 * This function will handle firmware requests for allocation of physically
916 * contiguous memory regions.
917 *
918 * These request entries should come first in the firmware's resource table,
919 * as other firmware entries might request placing other data objects inside
920 * these memory regions (e.g. data/code segments, trace resource entries, ...).
921 *
922 * Allocating memory this way helps utilizing the reserved physical memory
923 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
924 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
925 * pressure is important; it may have a substantial impact on performance.
926 *
927 * Return: 0 on success, or an appropriate error code otherwise
928 */
rproc_handle_carveout(struct rproc * rproc,void * ptr,int offset,int avail)929 static int rproc_handle_carveout(struct rproc *rproc,
930 void *ptr, int offset, int avail)
931 {
932 struct fw_rsc_carveout *rsc = ptr;
933 struct rproc_mem_entry *carveout;
934 struct device *dev = &rproc->dev;
935
936 if (sizeof(*rsc) > avail) {
937 dev_err(dev, "carveout rsc is truncated\n");
938 return -EINVAL;
939 }
940
941 /* make sure reserved bytes are zeroes */
942 if (rsc->reserved) {
943 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
944 return -EINVAL;
945 }
946
947 dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
948 rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
949
950 /*
951 * Check carveout rsc already part of a registered carveout,
952 * Search by name, then check the da and length
953 */
954 carveout = rproc_find_carveout_by_name(rproc, rsc->name);
955
956 if (carveout) {
957 if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
958 dev_err(dev,
959 "Carveout already associated to resource table\n");
960 return -ENOMEM;
961 }
962
963 if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
964 return -ENOMEM;
965
966 /* Update memory carveout with resource table info */
967 carveout->rsc_offset = offset;
968 carveout->flags = rsc->flags;
969
970 return 0;
971 }
972
973 /* Register carveout in in list */
974 carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
975 rproc_alloc_carveout,
976 rproc_release_carveout, rsc->name);
977 if (!carveout) {
978 dev_err(dev, "Can't allocate memory entry structure\n");
979 return -ENOMEM;
980 }
981
982 carveout->flags = rsc->flags;
983 carveout->rsc_offset = offset;
984 rproc_add_carveout(rproc, carveout);
985
986 return 0;
987 }
988
989 /**
990 * rproc_add_carveout() - register an allocated carveout region
991 * @rproc: rproc handle
992 * @mem: memory entry to register
993 *
994 * This function registers specified memory entry in @rproc carveouts list.
995 * Specified carveout should have been allocated before registering.
996 */
rproc_add_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)997 void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
998 {
999 list_add_tail(&mem->node, &rproc->carveouts);
1000 }
1001 EXPORT_SYMBOL(rproc_add_carveout);
1002
1003 /**
1004 * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
1005 * @dev: pointer on device struct
1006 * @va: virtual address
1007 * @dma: dma address
1008 * @len: memory carveout length
1009 * @da: device address
1010 * @alloc: memory carveout allocation function
1011 * @release: memory carveout release function
1012 * @name: carveout name
1013 *
1014 * This function allocates a rproc_mem_entry struct and fill it with parameters
1015 * provided by client.
1016 *
1017 * Return: a valid pointer on success, or NULL on failure
1018 */
1019 __printf(8, 9)
1020 struct rproc_mem_entry *
rproc_mem_entry_init(struct device * dev,void * va,dma_addr_t dma,size_t len,u32 da,int (* alloc)(struct rproc *,struct rproc_mem_entry *),int (* release)(struct rproc *,struct rproc_mem_entry *),const char * name,...)1021 rproc_mem_entry_init(struct device *dev,
1022 void *va, dma_addr_t dma, size_t len, u32 da,
1023 int (*alloc)(struct rproc *, struct rproc_mem_entry *),
1024 int (*release)(struct rproc *, struct rproc_mem_entry *),
1025 const char *name, ...)
1026 {
1027 struct rproc_mem_entry *mem;
1028 va_list args;
1029
1030 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
1031 if (!mem)
1032 return mem;
1033
1034 mem->va = va;
1035 mem->dma = dma;
1036 mem->da = da;
1037 mem->len = len;
1038 mem->alloc = alloc;
1039 mem->release = release;
1040 mem->rsc_offset = FW_RSC_ADDR_ANY;
1041 mem->of_resm_idx = -1;
1042
1043 va_start(args, name);
1044 vsnprintf(mem->name, sizeof(mem->name), name, args);
1045 va_end(args);
1046
1047 return mem;
1048 }
1049 EXPORT_SYMBOL(rproc_mem_entry_init);
1050
1051 /**
1052 * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
1053 * from a reserved memory phandle
1054 * @dev: pointer on device struct
1055 * @of_resm_idx: reserved memory phandle index in "memory-region"
1056 * @len: memory carveout length
1057 * @da: device address
1058 * @name: carveout name
1059 *
1060 * This function allocates a rproc_mem_entry struct and fill it with parameters
1061 * provided by client.
1062 *
1063 * Return: a valid pointer on success, or NULL on failure
1064 */
1065 __printf(5, 6)
1066 struct rproc_mem_entry *
rproc_of_resm_mem_entry_init(struct device * dev,u32 of_resm_idx,size_t len,u32 da,const char * name,...)1067 rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
1068 u32 da, const char *name, ...)
1069 {
1070 struct rproc_mem_entry *mem;
1071 va_list args;
1072
1073 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
1074 if (!mem)
1075 return mem;
1076
1077 mem->da = da;
1078 mem->len = len;
1079 mem->rsc_offset = FW_RSC_ADDR_ANY;
1080 mem->of_resm_idx = of_resm_idx;
1081
1082 va_start(args, name);
1083 vsnprintf(mem->name, sizeof(mem->name), name, args);
1084 va_end(args);
1085
1086 return mem;
1087 }
1088 EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
1089
1090 /**
1091 * rproc_of_parse_firmware() - parse and return the firmware-name
1092 * @dev: pointer on device struct representing a rproc
1093 * @index: index to use for the firmware-name retrieval
1094 * @fw_name: pointer to a character string, in which the firmware
1095 * name is returned on success and unmodified otherwise.
1096 *
1097 * This is an OF helper function that parses a device's DT node for
1098 * the "firmware-name" property and returns the firmware name pointer
1099 * in @fw_name on success.
1100 *
1101 * Return: 0 on success, or an appropriate failure.
1102 */
rproc_of_parse_firmware(struct device * dev,int index,const char ** fw_name)1103 int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name)
1104 {
1105 int ret;
1106
1107 ret = of_property_read_string_index(dev->of_node, "firmware-name",
1108 index, fw_name);
1109 return ret ? ret : 0;
1110 }
1111 EXPORT_SYMBOL(rproc_of_parse_firmware);
1112
1113 /*
1114 * A lookup table for resource handlers. The indices are defined in
1115 * enum fw_resource_type.
1116 */
1117 static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
1118 [RSC_CARVEOUT] = rproc_handle_carveout,
1119 [RSC_DEVMEM] = rproc_handle_devmem,
1120 [RSC_TRACE] = rproc_handle_trace,
1121 [RSC_VDEV] = rproc_handle_vdev,
1122 };
1123
1124 /* handle firmware resource entries before booting the remote processor */
rproc_handle_resources(struct rproc * rproc,rproc_handle_resource_t handlers[RSC_LAST])1125 static int rproc_handle_resources(struct rproc *rproc,
1126 rproc_handle_resource_t handlers[RSC_LAST])
1127 {
1128 struct device *dev = &rproc->dev;
1129 rproc_handle_resource_t handler;
1130 int ret = 0, i;
1131
1132 if (!rproc->table_ptr)
1133 return 0;
1134
1135 for (i = 0; i < rproc->table_ptr->num; i++) {
1136 int offset = rproc->table_ptr->offset[i];
1137 struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1138 int avail = rproc->table_sz - offset - sizeof(*hdr);
1139 void *rsc = (void *)hdr + sizeof(*hdr);
1140
1141 /* make sure table isn't truncated */
1142 if (avail < 0) {
1143 dev_err(dev, "rsc table is truncated\n");
1144 return -EINVAL;
1145 }
1146
1147 dev_dbg(dev, "rsc: type %d\n", hdr->type);
1148
1149 if (hdr->type >= RSC_VENDOR_START &&
1150 hdr->type <= RSC_VENDOR_END) {
1151 ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1152 offset + sizeof(*hdr), avail);
1153 if (ret == RSC_HANDLED)
1154 continue;
1155 else if (ret < 0)
1156 break;
1157
1158 dev_warn(dev, "unsupported vendor resource %d\n",
1159 hdr->type);
1160 continue;
1161 }
1162
1163 if (hdr->type >= RSC_LAST) {
1164 dev_warn(dev, "unsupported resource %d\n", hdr->type);
1165 continue;
1166 }
1167
1168 handler = handlers[hdr->type];
1169 if (!handler)
1170 continue;
1171
1172 ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
1173 if (ret)
1174 break;
1175 }
1176
1177 return ret;
1178 }
1179
rproc_prepare_subdevices(struct rproc * rproc)1180 static int rproc_prepare_subdevices(struct rproc *rproc)
1181 {
1182 struct rproc_subdev *subdev;
1183 int ret;
1184
1185 list_for_each_entry(subdev, &rproc->subdevs, node) {
1186 if (subdev->prepare) {
1187 ret = subdev->prepare(subdev);
1188 if (ret)
1189 goto unroll_preparation;
1190 }
1191 }
1192
1193 return 0;
1194
1195 unroll_preparation:
1196 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1197 if (subdev->unprepare)
1198 subdev->unprepare(subdev);
1199 }
1200
1201 return ret;
1202 }
1203
rproc_start_subdevices(struct rproc * rproc)1204 static int rproc_start_subdevices(struct rproc *rproc)
1205 {
1206 struct rproc_subdev *subdev;
1207 int ret;
1208
1209 list_for_each_entry(subdev, &rproc->subdevs, node) {
1210 if (subdev->start) {
1211 ret = subdev->start(subdev);
1212 if (ret)
1213 goto unroll_registration;
1214 }
1215 }
1216
1217 return 0;
1218
1219 unroll_registration:
1220 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1221 if (subdev->stop)
1222 subdev->stop(subdev, true);
1223 }
1224
1225 return ret;
1226 }
1227
rproc_stop_subdevices(struct rproc * rproc,bool crashed)1228 static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
1229 {
1230 struct rproc_subdev *subdev;
1231
1232 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1233 if (subdev->stop)
1234 subdev->stop(subdev, crashed);
1235 }
1236 }
1237
rproc_unprepare_subdevices(struct rproc * rproc)1238 static void rproc_unprepare_subdevices(struct rproc *rproc)
1239 {
1240 struct rproc_subdev *subdev;
1241
1242 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1243 if (subdev->unprepare)
1244 subdev->unprepare(subdev);
1245 }
1246 }
1247
1248 /**
1249 * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1250 * in the list
1251 * @rproc: the remote processor handle
1252 *
1253 * This function parses registered carveout list, performs allocation
1254 * if alloc() ops registered and updates resource table information
1255 * if rsc_offset set.
1256 *
1257 * Return: 0 on success
1258 */
rproc_alloc_registered_carveouts(struct rproc * rproc)1259 static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1260 {
1261 struct rproc_mem_entry *entry, *tmp;
1262 struct fw_rsc_carveout *rsc;
1263 struct device *dev = &rproc->dev;
1264 u64 pa;
1265 int ret;
1266
1267 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1268 if (entry->alloc) {
1269 ret = entry->alloc(rproc, entry);
1270 if (ret) {
1271 dev_err(dev, "Unable to allocate carveout %s: %d\n",
1272 entry->name, ret);
1273 return -ENOMEM;
1274 }
1275 }
1276
1277 if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1278 /* update resource table */
1279 rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1280
1281 /*
1282 * Some remote processors might need to know the pa
1283 * even though they are behind an IOMMU. E.g., OMAP4's
1284 * remote M3 processor needs this so it can control
1285 * on-chip hardware accelerators that are not behind
1286 * the IOMMU, and therefor must know the pa.
1287 *
1288 * Generally we don't want to expose physical addresses
1289 * if we don't have to (remote processors are generally
1290 * _not_ trusted), so we might want to do this only for
1291 * remote processor that _must_ have this (e.g. OMAP4's
1292 * dual M3 subsystem).
1293 *
1294 * Non-IOMMU processors might also want to have this info.
1295 * In this case, the device address and the physical address
1296 * are the same.
1297 */
1298
1299 /* Use va if defined else dma to generate pa */
1300 if (entry->va)
1301 pa = (u64)rproc_va_to_pa(entry->va);
1302 else
1303 pa = (u64)entry->dma;
1304
1305 if (((u64)pa) & HIGH_BITS_MASK)
1306 dev_warn(dev,
1307 "Physical address cast in 32bit to fit resource table format\n");
1308
1309 rsc->pa = (u32)pa;
1310 rsc->da = entry->da;
1311 rsc->len = entry->len;
1312 }
1313 }
1314
1315 return 0;
1316 }
1317
1318
1319 /**
1320 * rproc_resource_cleanup() - clean up and free all acquired resources
1321 * @rproc: rproc handle
1322 *
1323 * This function will free all resources acquired for @rproc, and it
1324 * is called whenever @rproc either shuts down or fails to boot.
1325 */
rproc_resource_cleanup(struct rproc * rproc)1326 void rproc_resource_cleanup(struct rproc *rproc)
1327 {
1328 struct rproc_mem_entry *entry, *tmp;
1329 struct rproc_debug_trace *trace, *ttmp;
1330 struct rproc_vdev *rvdev, *rvtmp;
1331 struct device *dev = &rproc->dev;
1332
1333 /* clean up debugfs trace entries */
1334 list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1335 rproc_remove_trace_file(trace->tfile);
1336 rproc->num_traces--;
1337 list_del(&trace->node);
1338 kfree(trace);
1339 }
1340
1341 /* clean up iommu mapping entries */
1342 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1343 size_t unmapped;
1344
1345 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1346 if (unmapped != entry->len) {
1347 /* nothing much to do besides complaining */
1348 dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
1349 unmapped);
1350 }
1351
1352 list_del(&entry->node);
1353 kfree(entry);
1354 }
1355
1356 /* clean up carveout allocations */
1357 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1358 if (entry->release)
1359 entry->release(rproc, entry);
1360 list_del(&entry->node);
1361 kfree(entry);
1362 }
1363
1364 /* clean up remote vdev entries */
1365 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
1366 kref_put(&rvdev->refcount, rproc_vdev_release);
1367
1368 rproc_coredump_cleanup(rproc);
1369 }
1370 EXPORT_SYMBOL(rproc_resource_cleanup);
1371
rproc_start(struct rproc * rproc,const struct firmware * fw)1372 static int rproc_start(struct rproc *rproc, const struct firmware *fw)
1373 {
1374 struct resource_table *loaded_table;
1375 struct device *dev = &rproc->dev;
1376 int ret;
1377
1378 /* load the ELF segments to memory */
1379 ret = rproc_load_segments(rproc, fw);
1380 if (ret) {
1381 dev_err(dev, "Failed to load program segments: %d\n", ret);
1382 return ret;
1383 }
1384
1385 /*
1386 * The starting device has been given the rproc->cached_table as the
1387 * resource table. The address of the vring along with the other
1388 * allocated resources (carveouts etc) is stored in cached_table.
1389 * In order to pass this information to the remote device we must copy
1390 * this information to device memory. We also update the table_ptr so
1391 * that any subsequent changes will be applied to the loaded version.
1392 */
1393 loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
1394 if (loaded_table) {
1395 memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
1396 rproc->table_ptr = loaded_table;
1397 }
1398
1399 ret = rproc_prepare_subdevices(rproc);
1400 if (ret) {
1401 dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1402 rproc->name, ret);
1403 goto reset_table_ptr;
1404 }
1405
1406 /* power up the remote processor */
1407 ret = rproc->ops->start(rproc);
1408 if (ret) {
1409 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1410 goto unprepare_subdevices;
1411 }
1412
1413 /* Start any subdevices for the remote processor */
1414 ret = rproc_start_subdevices(rproc);
1415 if (ret) {
1416 dev_err(dev, "failed to probe subdevices for %s: %d\n",
1417 rproc->name, ret);
1418 goto stop_rproc;
1419 }
1420
1421 rproc->state = RPROC_RUNNING;
1422
1423 dev_info(dev, "remote processor %s is now up\n", rproc->name);
1424
1425 return 0;
1426
1427 stop_rproc:
1428 rproc->ops->stop(rproc);
1429 unprepare_subdevices:
1430 rproc_unprepare_subdevices(rproc);
1431 reset_table_ptr:
1432 rproc->table_ptr = rproc->cached_table;
1433
1434 return ret;
1435 }
1436
__rproc_attach(struct rproc * rproc)1437 static int __rproc_attach(struct rproc *rproc)
1438 {
1439 struct device *dev = &rproc->dev;
1440 int ret;
1441
1442 ret = rproc_prepare_subdevices(rproc);
1443 if (ret) {
1444 dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1445 rproc->name, ret);
1446 goto out;
1447 }
1448
1449 /* Attach to the remote processor */
1450 ret = rproc_attach_device(rproc);
1451 if (ret) {
1452 dev_err(dev, "can't attach to rproc %s: %d\n",
1453 rproc->name, ret);
1454 goto unprepare_subdevices;
1455 }
1456
1457 /* Start any subdevices for the remote processor */
1458 ret = rproc_start_subdevices(rproc);
1459 if (ret) {
1460 dev_err(dev, "failed to probe subdevices for %s: %d\n",
1461 rproc->name, ret);
1462 goto stop_rproc;
1463 }
1464
1465 rproc->state = RPROC_ATTACHED;
1466
1467 dev_info(dev, "remote processor %s is now attached\n", rproc->name);
1468
1469 return 0;
1470
1471 stop_rproc:
1472 rproc->ops->stop(rproc);
1473 unprepare_subdevices:
1474 rproc_unprepare_subdevices(rproc);
1475 out:
1476 return ret;
1477 }
1478
1479 /*
1480 * take a firmware and boot a remote processor with it.
1481 */
rproc_fw_boot(struct rproc * rproc,const struct firmware * fw)1482 static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1483 {
1484 struct device *dev = &rproc->dev;
1485 const char *name = rproc->firmware;
1486 int ret;
1487
1488 ret = rproc_fw_sanity_check(rproc, fw);
1489 if (ret)
1490 return ret;
1491
1492 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1493
1494 /*
1495 * if enabling an IOMMU isn't relevant for this rproc, this is
1496 * just a nop
1497 */
1498 ret = rproc_enable_iommu(rproc);
1499 if (ret) {
1500 dev_err(dev, "can't enable iommu: %d\n", ret);
1501 return ret;
1502 }
1503
1504 /* Prepare rproc for firmware loading if needed */
1505 ret = rproc_prepare_device(rproc);
1506 if (ret) {
1507 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
1508 goto disable_iommu;
1509 }
1510
1511 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1512
1513 /* Load resource table, core dump segment list etc from the firmware */
1514 ret = rproc_parse_fw(rproc, fw);
1515 if (ret)
1516 goto unprepare_rproc;
1517
1518 /* reset max_notifyid */
1519 rproc->max_notifyid = -1;
1520
1521 /* reset handled vdev */
1522 rproc->nb_vdev = 0;
1523
1524 /* handle fw resources which are required to boot rproc */
1525 ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1526 if (ret) {
1527 dev_err(dev, "Failed to process resources: %d\n", ret);
1528 goto clean_up_resources;
1529 }
1530
1531 /* Allocate carveout resources associated to rproc */
1532 ret = rproc_alloc_registered_carveouts(rproc);
1533 if (ret) {
1534 dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1535 ret);
1536 goto clean_up_resources;
1537 }
1538
1539 ret = rproc_start(rproc, fw);
1540 if (ret)
1541 goto clean_up_resources;
1542
1543 return 0;
1544
1545 clean_up_resources:
1546 rproc_resource_cleanup(rproc);
1547 kfree(rproc->cached_table);
1548 rproc->cached_table = NULL;
1549 rproc->table_ptr = NULL;
1550 unprepare_rproc:
1551 /* release HW resources if needed */
1552 rproc_unprepare_device(rproc);
1553 disable_iommu:
1554 rproc_disable_iommu(rproc);
1555 return ret;
1556 }
1557
rproc_set_rsc_table(struct rproc * rproc)1558 static int rproc_set_rsc_table(struct rproc *rproc)
1559 {
1560 struct resource_table *table_ptr;
1561 struct device *dev = &rproc->dev;
1562 size_t table_sz;
1563 int ret;
1564
1565 table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz);
1566 if (!table_ptr) {
1567 /* Not having a resource table is acceptable */
1568 return 0;
1569 }
1570
1571 if (IS_ERR(table_ptr)) {
1572 ret = PTR_ERR(table_ptr);
1573 dev_err(dev, "can't load resource table: %d\n", ret);
1574 return ret;
1575 }
1576
1577 /*
1578 * If it is possible to detach the remote processor, keep an untouched
1579 * copy of the resource table. That way we can start fresh again when
1580 * the remote processor is re-attached, that is:
1581 *
1582 * DETACHED -> ATTACHED -> DETACHED -> ATTACHED
1583 *
1584 * Free'd in rproc_reset_rsc_table_on_detach() and
1585 * rproc_reset_rsc_table_on_stop().
1586 */
1587 if (rproc->ops->detach) {
1588 rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL);
1589 if (!rproc->clean_table)
1590 return -ENOMEM;
1591 } else {
1592 rproc->clean_table = NULL;
1593 }
1594
1595 rproc->cached_table = NULL;
1596 rproc->table_ptr = table_ptr;
1597 rproc->table_sz = table_sz;
1598
1599 return 0;
1600 }
1601
rproc_reset_rsc_table_on_detach(struct rproc * rproc)1602 static int rproc_reset_rsc_table_on_detach(struct rproc *rproc)
1603 {
1604 struct resource_table *table_ptr;
1605
1606 /* A resource table was never retrieved, nothing to do here */
1607 if (!rproc->table_ptr)
1608 return 0;
1609
1610 /*
1611 * If we made it to this point a clean_table _must_ have been
1612 * allocated in rproc_set_rsc_table(). If one isn't present
1613 * something went really wrong and we must complain.
1614 */
1615 if (WARN_ON(!rproc->clean_table))
1616 return -EINVAL;
1617
1618 /* Remember where the external entity installed the resource table */
1619 table_ptr = rproc->table_ptr;
1620
1621 /*
1622 * If we made it here the remote processor was started by another
1623 * entity and a cache table doesn't exist. As such make a copy of
1624 * the resource table currently used by the remote processor and
1625 * use that for the rest of the shutdown process. The memory
1626 * allocated here is free'd in rproc_detach().
1627 */
1628 rproc->cached_table = kmemdup(rproc->table_ptr,
1629 rproc->table_sz, GFP_KERNEL);
1630 if (!rproc->cached_table)
1631 return -ENOMEM;
1632
1633 /*
1634 * Use a copy of the resource table for the remainder of the
1635 * shutdown process.
1636 */
1637 rproc->table_ptr = rproc->cached_table;
1638
1639 /*
1640 * Reset the memory area where the firmware loaded the resource table
1641 * to its original value. That way when we re-attach the remote
1642 * processor the resource table is clean and ready to be used again.
1643 */
1644 memcpy(table_ptr, rproc->clean_table, rproc->table_sz);
1645
1646 /*
1647 * The clean resource table is no longer needed. Allocated in
1648 * rproc_set_rsc_table().
1649 */
1650 kfree(rproc->clean_table);
1651
1652 return 0;
1653 }
1654
rproc_reset_rsc_table_on_stop(struct rproc * rproc)1655 static int rproc_reset_rsc_table_on_stop(struct rproc *rproc)
1656 {
1657 /* A resource table was never retrieved, nothing to do here */
1658 if (!rproc->table_ptr)
1659 return 0;
1660
1661 /*
1662 * If a cache table exists the remote processor was started by
1663 * the remoteproc core. That cache table should be used for
1664 * the rest of the shutdown process.
1665 */
1666 if (rproc->cached_table)
1667 goto out;
1668
1669 /*
1670 * If we made it here the remote processor was started by another
1671 * entity and a cache table doesn't exist. As such make a copy of
1672 * the resource table currently used by the remote processor and
1673 * use that for the rest of the shutdown process. The memory
1674 * allocated here is free'd in rproc_shutdown().
1675 */
1676 rproc->cached_table = kmemdup(rproc->table_ptr,
1677 rproc->table_sz, GFP_KERNEL);
1678 if (!rproc->cached_table)
1679 return -ENOMEM;
1680
1681 /*
1682 * Since the remote processor is being switched off the clean table
1683 * won't be needed. Allocated in rproc_set_rsc_table().
1684 */
1685 kfree(rproc->clean_table);
1686
1687 out:
1688 /*
1689 * Use a copy of the resource table for the remainder of the
1690 * shutdown process.
1691 */
1692 rproc->table_ptr = rproc->cached_table;
1693 return 0;
1694 }
1695
1696 /*
1697 * Attach to remote processor - similar to rproc_fw_boot() but without
1698 * the steps that deal with the firmware image.
1699 */
rproc_attach(struct rproc * rproc)1700 static int rproc_attach(struct rproc *rproc)
1701 {
1702 struct device *dev = &rproc->dev;
1703 int ret;
1704
1705 /*
1706 * if enabling an IOMMU isn't relevant for this rproc, this is
1707 * just a nop
1708 */
1709 ret = rproc_enable_iommu(rproc);
1710 if (ret) {
1711 dev_err(dev, "can't enable iommu: %d\n", ret);
1712 return ret;
1713 }
1714
1715 /* Do anything that is needed to boot the remote processor */
1716 ret = rproc_prepare_device(rproc);
1717 if (ret) {
1718 dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
1719 goto disable_iommu;
1720 }
1721
1722 ret = rproc_set_rsc_table(rproc);
1723 if (ret) {
1724 dev_err(dev, "can't load resource table: %d\n", ret);
1725 goto unprepare_device;
1726 }
1727
1728 /* reset max_notifyid */
1729 rproc->max_notifyid = -1;
1730
1731 /* reset handled vdev */
1732 rproc->nb_vdev = 0;
1733
1734 /*
1735 * Handle firmware resources required to attach to a remote processor.
1736 * Because we are attaching rather than booting the remote processor,
1737 * we expect the platform driver to properly set rproc->table_ptr.
1738 */
1739 ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1740 if (ret) {
1741 dev_err(dev, "Failed to process resources: %d\n", ret);
1742 goto unprepare_device;
1743 }
1744
1745 /* Allocate carveout resources associated to rproc */
1746 ret = rproc_alloc_registered_carveouts(rproc);
1747 if (ret) {
1748 dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1749 ret);
1750 goto clean_up_resources;
1751 }
1752
1753 ret = __rproc_attach(rproc);
1754 if (ret)
1755 goto clean_up_resources;
1756
1757 return 0;
1758
1759 clean_up_resources:
1760 rproc_resource_cleanup(rproc);
1761 unprepare_device:
1762 /* release HW resources if needed */
1763 rproc_unprepare_device(rproc);
1764 disable_iommu:
1765 rproc_disable_iommu(rproc);
1766 return ret;
1767 }
1768
1769 /*
1770 * take a firmware and boot it up.
1771 *
1772 * Note: this function is called asynchronously upon registration of the
1773 * remote processor (so we must wait until it completes before we try
1774 * to unregister the device. one other option is just to use kref here,
1775 * that might be cleaner).
1776 */
rproc_auto_boot_callback(const struct firmware * fw,void * context)1777 static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1778 {
1779 struct rproc *rproc = context;
1780
1781 rproc_boot(rproc);
1782
1783 release_firmware(fw);
1784 }
1785
rproc_trigger_auto_boot(struct rproc * rproc)1786 static int rproc_trigger_auto_boot(struct rproc *rproc)
1787 {
1788 int ret;
1789
1790 /*
1791 * Since the remote processor is in a detached state, it has already
1792 * been booted by another entity. As such there is no point in waiting
1793 * for a firmware image to be loaded, we can simply initiate the process
1794 * of attaching to it immediately.
1795 */
1796 if (rproc->state == RPROC_DETACHED)
1797 return rproc_boot(rproc);
1798
1799 /*
1800 * We're initiating an asynchronous firmware loading, so we can
1801 * be built-in kernel code, without hanging the boot process.
1802 */
1803 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
1804 rproc->firmware, &rproc->dev, GFP_KERNEL,
1805 rproc, rproc_auto_boot_callback);
1806 if (ret < 0)
1807 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
1808
1809 return ret;
1810 }
1811
rproc_stop(struct rproc * rproc,bool crashed)1812 static int rproc_stop(struct rproc *rproc, bool crashed)
1813 {
1814 struct device *dev = &rproc->dev;
1815 int ret;
1816
1817 /* No need to continue if a stop() operation has not been provided */
1818 if (!rproc->ops->stop)
1819 return -EINVAL;
1820
1821 /* Stop any subdevices for the remote processor */
1822 rproc_stop_subdevices(rproc, crashed);
1823
1824 /* the installed resource table is no longer accessible */
1825 ret = rproc_reset_rsc_table_on_stop(rproc);
1826 if (ret) {
1827 dev_err(dev, "can't reset resource table: %d\n", ret);
1828 return ret;
1829 }
1830
1831
1832 /* power off the remote processor */
1833 ret = rproc->ops->stop(rproc);
1834 if (ret) {
1835 dev_err(dev, "can't stop rproc: %d\n", ret);
1836 return ret;
1837 }
1838
1839 rproc_unprepare_subdevices(rproc);
1840
1841 rproc->state = RPROC_OFFLINE;
1842
1843 dev_info(dev, "stopped remote processor %s\n", rproc->name);
1844
1845 return 0;
1846 }
1847
1848 /*
1849 * __rproc_detach(): Does the opposite of __rproc_attach()
1850 */
__rproc_detach(struct rproc * rproc)1851 static int __rproc_detach(struct rproc *rproc)
1852 {
1853 struct device *dev = &rproc->dev;
1854 int ret;
1855
1856 /* No need to continue if a detach() operation has not been provided */
1857 if (!rproc->ops->detach)
1858 return -EINVAL;
1859
1860 /* Stop any subdevices for the remote processor */
1861 rproc_stop_subdevices(rproc, false);
1862
1863 /* the installed resource table is no longer accessible */
1864 ret = rproc_reset_rsc_table_on_detach(rproc);
1865 if (ret) {
1866 dev_err(dev, "can't reset resource table: %d\n", ret);
1867 return ret;
1868 }
1869
1870 /* Tell the remote processor the core isn't available anymore */
1871 ret = rproc->ops->detach(rproc);
1872 if (ret) {
1873 dev_err(dev, "can't detach from rproc: %d\n", ret);
1874 return ret;
1875 }
1876
1877 rproc_unprepare_subdevices(rproc);
1878
1879 rproc->state = RPROC_DETACHED;
1880
1881 dev_info(dev, "detached remote processor %s\n", rproc->name);
1882
1883 return 0;
1884 }
1885
1886 /**
1887 * rproc_trigger_recovery() - recover a remoteproc
1888 * @rproc: the remote processor
1889 *
1890 * The recovery is done by resetting all the virtio devices, that way all the
1891 * rpmsg drivers will be reseted along with the remote processor making the
1892 * remoteproc functional again.
1893 *
1894 * This function can sleep, so it cannot be called from atomic context.
1895 *
1896 * Return: 0 on success or a negative value upon failure
1897 */
rproc_trigger_recovery(struct rproc * rproc)1898 int rproc_trigger_recovery(struct rproc *rproc)
1899 {
1900 const struct firmware *firmware_p;
1901 struct device *dev = &rproc->dev;
1902 int ret;
1903
1904 ret = mutex_lock_interruptible(&rproc->lock);
1905 if (ret)
1906 return ret;
1907
1908 /* State could have changed before we got the mutex */
1909 if (rproc->state != RPROC_CRASHED)
1910 goto unlock_mutex;
1911
1912 dev_err(dev, "recovering %s\n", rproc->name);
1913
1914 ret = rproc_stop(rproc, true);
1915 if (ret)
1916 goto unlock_mutex;
1917
1918 /* generate coredump */
1919 rproc->ops->coredump(rproc);
1920
1921 /* load firmware */
1922 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1923 if (ret < 0) {
1924 dev_err(dev, "request_firmware failed: %d\n", ret);
1925 goto unlock_mutex;
1926 }
1927
1928 /* boot the remote processor up again */
1929 ret = rproc_start(rproc, firmware_p);
1930
1931 release_firmware(firmware_p);
1932
1933 unlock_mutex:
1934 mutex_unlock(&rproc->lock);
1935 return ret;
1936 }
1937
1938 /**
1939 * rproc_crash_handler_work() - handle a crash
1940 * @work: work treating the crash
1941 *
1942 * This function needs to handle everything related to a crash, like cpu
1943 * registers and stack dump, information to help to debug the fatal error, etc.
1944 */
rproc_crash_handler_work(struct work_struct * work)1945 static void rproc_crash_handler_work(struct work_struct *work)
1946 {
1947 struct rproc *rproc = container_of(work, struct rproc, crash_handler);
1948 struct device *dev = &rproc->dev;
1949
1950 dev_dbg(dev, "enter %s\n", __func__);
1951
1952 mutex_lock(&rproc->lock);
1953
1954 if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
1955 /* handle only the first crash detected */
1956 mutex_unlock(&rproc->lock);
1957 return;
1958 }
1959
1960 rproc->state = RPROC_CRASHED;
1961 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
1962 rproc->name);
1963
1964 mutex_unlock(&rproc->lock);
1965
1966 if (!rproc->recovery_disabled)
1967 rproc_trigger_recovery(rproc);
1968
1969 pm_relax(rproc->dev.parent);
1970 }
1971
1972 /**
1973 * rproc_boot() - boot a remote processor
1974 * @rproc: handle of a remote processor
1975 *
1976 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1977 *
1978 * If the remote processor is already powered on, this function immediately
1979 * returns (successfully).
1980 *
1981 * Return: 0 on success, and an appropriate error value otherwise
1982 */
rproc_boot(struct rproc * rproc)1983 int rproc_boot(struct rproc *rproc)
1984 {
1985 const struct firmware *firmware_p;
1986 struct device *dev;
1987 int ret;
1988
1989 if (!rproc) {
1990 pr_err("invalid rproc handle\n");
1991 return -EINVAL;
1992 }
1993
1994 dev = &rproc->dev;
1995
1996 ret = mutex_lock_interruptible(&rproc->lock);
1997 if (ret) {
1998 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1999 return ret;
2000 }
2001
2002 if (rproc->state == RPROC_DELETED) {
2003 ret = -ENODEV;
2004 dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
2005 goto unlock_mutex;
2006 }
2007
2008 /* skip the boot or attach process if rproc is already powered up */
2009 if (atomic_inc_return(&rproc->power) > 1) {
2010 ret = 0;
2011 goto unlock_mutex;
2012 }
2013
2014 if (rproc->state == RPROC_DETACHED) {
2015 dev_info(dev, "attaching to %s\n", rproc->name);
2016
2017 ret = rproc_attach(rproc);
2018 } else {
2019 dev_info(dev, "powering up %s\n", rproc->name);
2020
2021 /* load firmware */
2022 ret = request_firmware(&firmware_p, rproc->firmware, dev);
2023 if (ret < 0) {
2024 dev_err(dev, "request_firmware failed: %d\n", ret);
2025 goto downref_rproc;
2026 }
2027
2028 ret = rproc_fw_boot(rproc, firmware_p);
2029
2030 release_firmware(firmware_p);
2031 }
2032
2033 downref_rproc:
2034 if (ret)
2035 atomic_dec(&rproc->power);
2036 unlock_mutex:
2037 mutex_unlock(&rproc->lock);
2038 return ret;
2039 }
2040 EXPORT_SYMBOL(rproc_boot);
2041
2042 /**
2043 * rproc_shutdown() - power off the remote processor
2044 * @rproc: the remote processor
2045 *
2046 * Power off a remote processor (previously booted with rproc_boot()).
2047 *
2048 * In case @rproc is still being used by an additional user(s), then
2049 * this function will just decrement the power refcount and exit,
2050 * without really powering off the device.
2051 *
2052 * Every call to rproc_boot() must (eventually) be accompanied by a call
2053 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
2054 *
2055 * Notes:
2056 * - we're not decrementing the rproc's refcount, only the power refcount.
2057 * which means that the @rproc handle stays valid even after rproc_shutdown()
2058 * returns, and users can still use it with a subsequent rproc_boot(), if
2059 * needed.
2060 *
2061 * Return: 0 on success, and an appropriate error value otherwise
2062 */
rproc_shutdown(struct rproc * rproc)2063 int rproc_shutdown(struct rproc *rproc)
2064 {
2065 struct device *dev = &rproc->dev;
2066 int ret = 0;
2067
2068 ret = mutex_lock_interruptible(&rproc->lock);
2069 if (ret) {
2070 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2071 return ret;
2072 }
2073
2074 if (rproc->state != RPROC_RUNNING &&
2075 rproc->state != RPROC_ATTACHED) {
2076 ret = -EINVAL;
2077 goto out;
2078 }
2079
2080 /* if the remote proc is still needed, bail out */
2081 if (!atomic_dec_and_test(&rproc->power))
2082 goto out;
2083
2084 ret = rproc_stop(rproc, false);
2085 if (ret) {
2086 atomic_inc(&rproc->power);
2087 goto out;
2088 }
2089
2090 /* clean up all acquired resources */
2091 rproc_resource_cleanup(rproc);
2092
2093 /* release HW resources if needed */
2094 rproc_unprepare_device(rproc);
2095
2096 rproc_disable_iommu(rproc);
2097
2098 /* Free the copy of the resource table */
2099 kfree(rproc->cached_table);
2100 rproc->cached_table = NULL;
2101 rproc->table_ptr = NULL;
2102 out:
2103 mutex_unlock(&rproc->lock);
2104 return ret;
2105 }
2106 EXPORT_SYMBOL(rproc_shutdown);
2107
2108 /**
2109 * rproc_detach() - Detach the remote processor from the
2110 * remoteproc core
2111 *
2112 * @rproc: the remote processor
2113 *
2114 * Detach a remote processor (previously attached to with rproc_attach()).
2115 *
2116 * In case @rproc is still being used by an additional user(s), then
2117 * this function will just decrement the power refcount and exit,
2118 * without disconnecting the device.
2119 *
2120 * Function rproc_detach() calls __rproc_detach() in order to let a remote
2121 * processor know that services provided by the application processor are
2122 * no longer available. From there it should be possible to remove the
2123 * platform driver and even power cycle the application processor (if the HW
2124 * supports it) without needing to switch off the remote processor.
2125 *
2126 * Return: 0 on success, and an appropriate error value otherwise
2127 */
rproc_detach(struct rproc * rproc)2128 int rproc_detach(struct rproc *rproc)
2129 {
2130 struct device *dev = &rproc->dev;
2131 int ret;
2132
2133 ret = mutex_lock_interruptible(&rproc->lock);
2134 if (ret) {
2135 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2136 return ret;
2137 }
2138
2139 if (rproc->state != RPROC_ATTACHED) {
2140 ret = -EINVAL;
2141 goto out;
2142 }
2143
2144 /* if the remote proc is still needed, bail out */
2145 if (!atomic_dec_and_test(&rproc->power)) {
2146 ret = 0;
2147 goto out;
2148 }
2149
2150 ret = __rproc_detach(rproc);
2151 if (ret) {
2152 atomic_inc(&rproc->power);
2153 goto out;
2154 }
2155
2156 /* clean up all acquired resources */
2157 rproc_resource_cleanup(rproc);
2158
2159 /* release HW resources if needed */
2160 rproc_unprepare_device(rproc);
2161
2162 rproc_disable_iommu(rproc);
2163
2164 /* Free the copy of the resource table */
2165 kfree(rproc->cached_table);
2166 rproc->cached_table = NULL;
2167 rproc->table_ptr = NULL;
2168 out:
2169 mutex_unlock(&rproc->lock);
2170 return ret;
2171 }
2172 EXPORT_SYMBOL(rproc_detach);
2173
2174 /**
2175 * rproc_get_by_phandle() - find a remote processor by phandle
2176 * @phandle: phandle to the rproc
2177 *
2178 * Finds an rproc handle using the remote processor's phandle, and then
2179 * return a handle to the rproc.
2180 *
2181 * This function increments the remote processor's refcount, so always
2182 * use rproc_put() to decrement it back once rproc isn't needed anymore.
2183 *
2184 * Return: rproc handle on success, and NULL on failure
2185 */
2186 #ifdef CONFIG_OF
rproc_get_by_phandle(phandle phandle)2187 struct rproc *rproc_get_by_phandle(phandle phandle)
2188 {
2189 struct rproc *rproc = NULL, *r;
2190 struct device_node *np;
2191
2192 np = of_find_node_by_phandle(phandle);
2193 if (!np)
2194 return NULL;
2195
2196 rcu_read_lock();
2197 list_for_each_entry_rcu(r, &rproc_list, node) {
2198 if (r->dev.parent && r->dev.parent->of_node == np) {
2199 /* prevent underlying implementation from being removed */
2200 if (!try_module_get(r->dev.parent->driver->owner)) {
2201 dev_err(&r->dev, "can't get owner\n");
2202 break;
2203 }
2204
2205 rproc = r;
2206 get_device(&rproc->dev);
2207 break;
2208 }
2209 }
2210 rcu_read_unlock();
2211
2212 of_node_put(np);
2213
2214 return rproc;
2215 }
2216 #else
rproc_get_by_phandle(phandle phandle)2217 struct rproc *rproc_get_by_phandle(phandle phandle)
2218 {
2219 return NULL;
2220 }
2221 #endif
2222 EXPORT_SYMBOL(rproc_get_by_phandle);
2223
2224 /**
2225 * rproc_set_firmware() - assign a new firmware
2226 * @rproc: rproc handle to which the new firmware is being assigned
2227 * @fw_name: new firmware name to be assigned
2228 *
2229 * This function allows remoteproc drivers or clients to configure a custom
2230 * firmware name that is different from the default name used during remoteproc
2231 * registration. The function does not trigger a remote processor boot,
2232 * only sets the firmware name used for a subsequent boot. This function
2233 * should also be called only when the remote processor is offline.
2234 *
2235 * This allows either the userspace to configure a different name through
2236 * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
2237 * a specific firmware when it is controlling the boot and shutdown of the
2238 * remote processor.
2239 *
2240 * Return: 0 on success or a negative value upon failure
2241 */
rproc_set_firmware(struct rproc * rproc,const char * fw_name)2242 int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
2243 {
2244 struct device *dev;
2245 int ret, len;
2246 char *p;
2247
2248 if (!rproc || !fw_name)
2249 return -EINVAL;
2250
2251 dev = rproc->dev.parent;
2252
2253 ret = mutex_lock_interruptible(&rproc->lock);
2254 if (ret) {
2255 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2256 return -EINVAL;
2257 }
2258
2259 if (rproc->state != RPROC_OFFLINE) {
2260 dev_err(dev, "can't change firmware while running\n");
2261 ret = -EBUSY;
2262 goto out;
2263 }
2264
2265 len = strcspn(fw_name, "\n");
2266 if (!len) {
2267 dev_err(dev, "can't provide empty string for firmware name\n");
2268 ret = -EINVAL;
2269 goto out;
2270 }
2271
2272 p = kstrndup(fw_name, len, GFP_KERNEL);
2273 if (!p) {
2274 ret = -ENOMEM;
2275 goto out;
2276 }
2277
2278 kfree_const(rproc->firmware);
2279 rproc->firmware = p;
2280
2281 out:
2282 mutex_unlock(&rproc->lock);
2283 return ret;
2284 }
2285 EXPORT_SYMBOL(rproc_set_firmware);
2286
rproc_validate(struct rproc * rproc)2287 static int rproc_validate(struct rproc *rproc)
2288 {
2289 switch (rproc->state) {
2290 case RPROC_OFFLINE:
2291 /*
2292 * An offline processor without a start()
2293 * function makes no sense.
2294 */
2295 if (!rproc->ops->start)
2296 return -EINVAL;
2297 break;
2298 case RPROC_DETACHED:
2299 /*
2300 * A remote processor in a detached state without an
2301 * attach() function makes not sense.
2302 */
2303 if (!rproc->ops->attach)
2304 return -EINVAL;
2305 /*
2306 * When attaching to a remote processor the device memory
2307 * is already available and as such there is no need to have a
2308 * cached table.
2309 */
2310 if (rproc->cached_table)
2311 return -EINVAL;
2312 break;
2313 default:
2314 /*
2315 * When adding a remote processor, the state of the device
2316 * can be offline or detached, nothing else.
2317 */
2318 return -EINVAL;
2319 }
2320
2321 return 0;
2322 }
2323
2324 /**
2325 * rproc_add() - register a remote processor
2326 * @rproc: the remote processor handle to register
2327 *
2328 * Registers @rproc with the remoteproc framework, after it has been
2329 * allocated with rproc_alloc().
2330 *
2331 * This is called by the platform-specific rproc implementation, whenever
2332 * a new remote processor device is probed.
2333 *
2334 * Note: this function initiates an asynchronous firmware loading
2335 * context, which will look for virtio devices supported by the rproc's
2336 * firmware.
2337 *
2338 * If found, those virtio devices will be created and added, so as a result
2339 * of registering this remote processor, additional virtio drivers might be
2340 * probed.
2341 *
2342 * Return: 0 on success and an appropriate error code otherwise
2343 */
rproc_add(struct rproc * rproc)2344 int rproc_add(struct rproc *rproc)
2345 {
2346 struct device *dev = &rproc->dev;
2347 int ret;
2348
2349 ret = rproc_validate(rproc);
2350 if (ret < 0)
2351 return ret;
2352
2353 /* add char device for this remoteproc */
2354 ret = rproc_char_device_add(rproc);
2355 if (ret < 0)
2356 return ret;
2357
2358 ret = device_add(dev);
2359 if (ret < 0) {
2360 put_device(dev);
2361 goto rproc_remove_cdev;
2362 }
2363
2364 dev_info(dev, "%s is available\n", rproc->name);
2365
2366 /* create debugfs entries */
2367 rproc_create_debug_dir(rproc);
2368
2369 /* if rproc is marked always-on, request it to boot */
2370 if (rproc->auto_boot) {
2371 ret = rproc_trigger_auto_boot(rproc);
2372 if (ret < 0)
2373 goto rproc_remove_dev;
2374 }
2375
2376 /* expose to rproc_get_by_phandle users */
2377 mutex_lock(&rproc_list_mutex);
2378 list_add_rcu(&rproc->node, &rproc_list);
2379 mutex_unlock(&rproc_list_mutex);
2380
2381 return 0;
2382
2383 rproc_remove_dev:
2384 rproc_delete_debug_dir(rproc);
2385 device_del(dev);
2386 rproc_remove_cdev:
2387 rproc_char_device_remove(rproc);
2388 return ret;
2389 }
2390 EXPORT_SYMBOL(rproc_add);
2391
devm_rproc_remove(void * rproc)2392 static void devm_rproc_remove(void *rproc)
2393 {
2394 rproc_del(rproc);
2395 }
2396
2397 /**
2398 * devm_rproc_add() - resource managed rproc_add()
2399 * @dev: the underlying device
2400 * @rproc: the remote processor handle to register
2401 *
2402 * This function performs like rproc_add() but the registered rproc device will
2403 * automatically be removed on driver detach.
2404 *
2405 * Return: 0 on success, negative errno on failure
2406 */
devm_rproc_add(struct device * dev,struct rproc * rproc)2407 int devm_rproc_add(struct device *dev, struct rproc *rproc)
2408 {
2409 int err;
2410
2411 err = rproc_add(rproc);
2412 if (err)
2413 return err;
2414
2415 return devm_add_action_or_reset(dev, devm_rproc_remove, rproc);
2416 }
2417 EXPORT_SYMBOL(devm_rproc_add);
2418
2419 /**
2420 * rproc_type_release() - release a remote processor instance
2421 * @dev: the rproc's device
2422 *
2423 * This function should _never_ be called directly.
2424 *
2425 * It will be called by the driver core when no one holds a valid pointer
2426 * to @dev anymore.
2427 */
rproc_type_release(struct device * dev)2428 static void rproc_type_release(struct device *dev)
2429 {
2430 struct rproc *rproc = container_of(dev, struct rproc, dev);
2431
2432 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
2433
2434 idr_destroy(&rproc->notifyids);
2435
2436 if (rproc->index >= 0)
2437 ida_simple_remove(&rproc_dev_index, rproc->index);
2438
2439 kfree_const(rproc->firmware);
2440 kfree_const(rproc->name);
2441 kfree(rproc->ops);
2442 kfree(rproc);
2443 }
2444
2445 static const struct device_type rproc_type = {
2446 .name = "remoteproc",
2447 .release = rproc_type_release,
2448 };
2449
rproc_alloc_firmware(struct rproc * rproc,const char * name,const char * firmware)2450 static int rproc_alloc_firmware(struct rproc *rproc,
2451 const char *name, const char *firmware)
2452 {
2453 const char *p;
2454
2455 /*
2456 * Allocate a firmware name if the caller gave us one to work
2457 * with. Otherwise construct a new one using a default pattern.
2458 */
2459 if (firmware)
2460 p = kstrdup_const(firmware, GFP_KERNEL);
2461 else
2462 p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name);
2463
2464 if (!p)
2465 return -ENOMEM;
2466
2467 rproc->firmware = p;
2468
2469 return 0;
2470 }
2471
rproc_alloc_ops(struct rproc * rproc,const struct rproc_ops * ops)2472 static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops)
2473 {
2474 rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2475 if (!rproc->ops)
2476 return -ENOMEM;
2477
2478 /* Default to rproc_coredump if no coredump function is specified */
2479 if (!rproc->ops->coredump)
2480 rproc->ops->coredump = rproc_coredump;
2481
2482 if (rproc->ops->load)
2483 return 0;
2484
2485 /* Default to ELF loader if no load function is specified */
2486 rproc->ops->load = rproc_elf_load_segments;
2487 rproc->ops->parse_fw = rproc_elf_load_rsc_table;
2488 rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
2489 rproc->ops->sanity_check = rproc_elf_sanity_check;
2490 rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
2491
2492 return 0;
2493 }
2494
2495 /**
2496 * rproc_alloc() - allocate a remote processor handle
2497 * @dev: the underlying device
2498 * @name: name of this remote processor
2499 * @ops: platform-specific handlers (mainly start/stop)
2500 * @firmware: name of firmware file to load, can be NULL
2501 * @len: length of private data needed by the rproc driver (in bytes)
2502 *
2503 * Allocates a new remote processor handle, but does not register
2504 * it yet. if @firmware is NULL, a default name is used.
2505 *
2506 * This function should be used by rproc implementations during initialization
2507 * of the remote processor.
2508 *
2509 * After creating an rproc handle using this function, and when ready,
2510 * implementations should then call rproc_add() to complete
2511 * the registration of the remote processor.
2512 *
2513 * Note: _never_ directly deallocate @rproc, even if it was not registered
2514 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2515 *
2516 * Return: new rproc pointer on success, and NULL on failure
2517 */
rproc_alloc(struct device * dev,const char * name,const struct rproc_ops * ops,const char * firmware,int len)2518 struct rproc *rproc_alloc(struct device *dev, const char *name,
2519 const struct rproc_ops *ops,
2520 const char *firmware, int len)
2521 {
2522 struct rproc *rproc;
2523
2524 if (!dev || !name || !ops)
2525 return NULL;
2526
2527 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
2528 if (!rproc)
2529 return NULL;
2530
2531 rproc->priv = &rproc[1];
2532 rproc->auto_boot = true;
2533 rproc->elf_class = ELFCLASSNONE;
2534 rproc->elf_machine = EM_NONE;
2535
2536 device_initialize(&rproc->dev);
2537 rproc->dev.parent = dev;
2538 rproc->dev.type = &rproc_type;
2539 rproc->dev.class = &rproc_class;
2540 rproc->dev.driver_data = rproc;
2541 idr_init(&rproc->notifyids);
2542
2543 rproc->name = kstrdup_const(name, GFP_KERNEL);
2544 if (!rproc->name)
2545 goto put_device;
2546
2547 if (rproc_alloc_firmware(rproc, name, firmware))
2548 goto put_device;
2549
2550 if (rproc_alloc_ops(rproc, ops))
2551 goto put_device;
2552
2553 /* Assign a unique device index and name */
2554 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
2555 if (rproc->index < 0) {
2556 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
2557 goto put_device;
2558 }
2559
2560 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2561
2562 atomic_set(&rproc->power, 0);
2563
2564 mutex_init(&rproc->lock);
2565
2566 INIT_LIST_HEAD(&rproc->carveouts);
2567 INIT_LIST_HEAD(&rproc->mappings);
2568 INIT_LIST_HEAD(&rproc->traces);
2569 INIT_LIST_HEAD(&rproc->rvdevs);
2570 INIT_LIST_HEAD(&rproc->subdevs);
2571 INIT_LIST_HEAD(&rproc->dump_segments);
2572
2573 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
2574
2575 rproc->state = RPROC_OFFLINE;
2576
2577 return rproc;
2578
2579 put_device:
2580 put_device(&rproc->dev);
2581 return NULL;
2582 }
2583 EXPORT_SYMBOL(rproc_alloc);
2584
2585 /**
2586 * rproc_free() - unroll rproc_alloc()
2587 * @rproc: the remote processor handle
2588 *
2589 * This function decrements the rproc dev refcount.
2590 *
2591 * If no one holds any reference to rproc anymore, then its refcount would
2592 * now drop to zero, and it would be freed.
2593 */
rproc_free(struct rproc * rproc)2594 void rproc_free(struct rproc *rproc)
2595 {
2596 put_device(&rproc->dev);
2597 }
2598 EXPORT_SYMBOL(rproc_free);
2599
2600 /**
2601 * rproc_put() - release rproc reference
2602 * @rproc: the remote processor handle
2603 *
2604 * This function decrements the rproc dev refcount.
2605 *
2606 * If no one holds any reference to rproc anymore, then its refcount would
2607 * now drop to zero, and it would be freed.
2608 */
rproc_put(struct rproc * rproc)2609 void rproc_put(struct rproc *rproc)
2610 {
2611 module_put(rproc->dev.parent->driver->owner);
2612 put_device(&rproc->dev);
2613 }
2614 EXPORT_SYMBOL(rproc_put);
2615
2616 /**
2617 * rproc_del() - unregister a remote processor
2618 * @rproc: rproc handle to unregister
2619 *
2620 * This function should be called when the platform specific rproc
2621 * implementation decides to remove the rproc device. it should
2622 * _only_ be called if a previous invocation of rproc_add()
2623 * has completed successfully.
2624 *
2625 * After rproc_del() returns, @rproc isn't freed yet, because
2626 * of the outstanding reference created by rproc_alloc. To decrement that
2627 * one last refcount, one still needs to call rproc_free().
2628 *
2629 * Return: 0 on success and -EINVAL if @rproc isn't valid
2630 */
rproc_del(struct rproc * rproc)2631 int rproc_del(struct rproc *rproc)
2632 {
2633 if (!rproc)
2634 return -EINVAL;
2635
2636 /* TODO: make sure this works with rproc->power > 1 */
2637 rproc_shutdown(rproc);
2638
2639 mutex_lock(&rproc->lock);
2640 rproc->state = RPROC_DELETED;
2641 mutex_unlock(&rproc->lock);
2642
2643 rproc_delete_debug_dir(rproc);
2644
2645 /* the rproc is downref'ed as soon as it's removed from the klist */
2646 mutex_lock(&rproc_list_mutex);
2647 list_del_rcu(&rproc->node);
2648 mutex_unlock(&rproc_list_mutex);
2649
2650 /* Ensure that no readers of rproc_list are still active */
2651 synchronize_rcu();
2652
2653 device_del(&rproc->dev);
2654 rproc_char_device_remove(rproc);
2655
2656 return 0;
2657 }
2658 EXPORT_SYMBOL(rproc_del);
2659
devm_rproc_free(struct device * dev,void * res)2660 static void devm_rproc_free(struct device *dev, void *res)
2661 {
2662 rproc_free(*(struct rproc **)res);
2663 }
2664
2665 /**
2666 * devm_rproc_alloc() - resource managed rproc_alloc()
2667 * @dev: the underlying device
2668 * @name: name of this remote processor
2669 * @ops: platform-specific handlers (mainly start/stop)
2670 * @firmware: name of firmware file to load, can be NULL
2671 * @len: length of private data needed by the rproc driver (in bytes)
2672 *
2673 * This function performs like rproc_alloc() but the acquired rproc device will
2674 * automatically be released on driver detach.
2675 *
2676 * Return: new rproc instance, or NULL on failure
2677 */
devm_rproc_alloc(struct device * dev,const char * name,const struct rproc_ops * ops,const char * firmware,int len)2678 struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
2679 const struct rproc_ops *ops,
2680 const char *firmware, int len)
2681 {
2682 struct rproc **ptr, *rproc;
2683
2684 ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL);
2685 if (!ptr)
2686 return NULL;
2687
2688 rproc = rproc_alloc(dev, name, ops, firmware, len);
2689 if (rproc) {
2690 *ptr = rproc;
2691 devres_add(dev, ptr);
2692 } else {
2693 devres_free(ptr);
2694 }
2695
2696 return rproc;
2697 }
2698 EXPORT_SYMBOL(devm_rproc_alloc);
2699
2700 /**
2701 * rproc_add_subdev() - add a subdevice to a remoteproc
2702 * @rproc: rproc handle to add the subdevice to
2703 * @subdev: subdev handle to register
2704 *
2705 * Caller is responsible for populating optional subdevice function pointers.
2706 */
rproc_add_subdev(struct rproc * rproc,struct rproc_subdev * subdev)2707 void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
2708 {
2709 list_add_tail(&subdev->node, &rproc->subdevs);
2710 }
2711 EXPORT_SYMBOL(rproc_add_subdev);
2712
2713 /**
2714 * rproc_remove_subdev() - remove a subdevice from a remoteproc
2715 * @rproc: rproc handle to remove the subdevice from
2716 * @subdev: subdev handle, previously registered with rproc_add_subdev()
2717 */
rproc_remove_subdev(struct rproc * rproc,struct rproc_subdev * subdev)2718 void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
2719 {
2720 list_del(&subdev->node);
2721 }
2722 EXPORT_SYMBOL(rproc_remove_subdev);
2723
2724 /**
2725 * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
2726 * @dev: child device to find ancestor of
2727 *
2728 * Return: the ancestor rproc instance, or NULL if not found
2729 */
rproc_get_by_child(struct device * dev)2730 struct rproc *rproc_get_by_child(struct device *dev)
2731 {
2732 for (dev = dev->parent; dev; dev = dev->parent) {
2733 if (dev->type == &rproc_type)
2734 return dev->driver_data;
2735 }
2736
2737 return NULL;
2738 }
2739 EXPORT_SYMBOL(rproc_get_by_child);
2740
2741 /**
2742 * rproc_report_crash() - rproc crash reporter function
2743 * @rproc: remote processor
2744 * @type: crash type
2745 *
2746 * This function must be called every time a crash is detected by the low-level
2747 * drivers implementing a specific remoteproc. This should not be called from a
2748 * non-remoteproc driver.
2749 *
2750 * This function can be called from atomic/interrupt context.
2751 */
rproc_report_crash(struct rproc * rproc,enum rproc_crash_type type)2752 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
2753 {
2754 if (!rproc) {
2755 pr_err("NULL rproc pointer\n");
2756 return;
2757 }
2758
2759 /* Prevent suspend while the remoteproc is being recovered */
2760 pm_stay_awake(rproc->dev.parent);
2761
2762 dev_err(&rproc->dev, "crash detected in %s: type %s\n",
2763 rproc->name, rproc_crash_to_string(type));
2764
2765 /* Have a worker handle the error; ensure system is not suspended */
2766 queue_work(system_freezable_wq, &rproc->crash_handler);
2767 }
2768 EXPORT_SYMBOL(rproc_report_crash);
2769
rproc_panic_handler(struct notifier_block * nb,unsigned long event,void * ptr)2770 static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
2771 void *ptr)
2772 {
2773 unsigned int longest = 0;
2774 struct rproc *rproc;
2775 unsigned int d;
2776
2777 rcu_read_lock();
2778 list_for_each_entry_rcu(rproc, &rproc_list, node) {
2779 if (!rproc->ops->panic)
2780 continue;
2781
2782 if (rproc->state != RPROC_RUNNING &&
2783 rproc->state != RPROC_ATTACHED)
2784 continue;
2785
2786 d = rproc->ops->panic(rproc);
2787 longest = max(longest, d);
2788 }
2789 rcu_read_unlock();
2790
2791 /*
2792 * Delay for the longest requested duration before returning. This can
2793 * be used by the remoteproc drivers to give the remote processor time
2794 * to perform any requested operations (such as flush caches), when
2795 * it's not possible to signal the Linux side due to the panic.
2796 */
2797 mdelay(longest);
2798
2799 return NOTIFY_DONE;
2800 }
2801
rproc_init_panic(void)2802 static void __init rproc_init_panic(void)
2803 {
2804 rproc_panic_nb.notifier_call = rproc_panic_handler;
2805 atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
2806 }
2807
rproc_exit_panic(void)2808 static void __exit rproc_exit_panic(void)
2809 {
2810 atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
2811 }
2812
remoteproc_init(void)2813 static int __init remoteproc_init(void)
2814 {
2815 rproc_init_sysfs();
2816 rproc_init_debugfs();
2817 rproc_init_cdev();
2818 rproc_init_panic();
2819
2820 return 0;
2821 }
2822 subsys_initcall(remoteproc_init);
2823
remoteproc_exit(void)2824 static void __exit remoteproc_exit(void)
2825 {
2826 ida_destroy(&rproc_dev_index);
2827
2828 rproc_exit_panic();
2829 rproc_exit_debugfs();
2830 rproc_exit_sysfs();
2831 }
2832 module_exit(remoteproc_exit);
2833
2834 MODULE_LICENSE("GPL v2");
2835 MODULE_DESCRIPTION("Generic Remote Processor Framework");
2836