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