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