remoteproc: introduce rproc_find_carveout_by_name function
[linux-2.6-block.git] / drivers / remoteproc / remoteproc_core.c
CommitLineData
400e64df
OBC
1/*
2 * Remote Processor Framework
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 *
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
9 * Mark Grosen <mgrosen@ti.com>
10 * Fernando Guzman Lugo <fernando.lugo@ti.com>
11 * Suman Anna <s-anna@ti.com>
12 * Robert Tivy <rtivy@ti.com>
13 * Armando Uribe De Leon <x0095078@ti.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25#define pr_fmt(fmt) "%s: " fmt, __func__
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/slab.h>
31#include <linux/mutex.h>
32#include <linux/dma-mapping.h>
33#include <linux/firmware.h>
34#include <linux/string.h>
35#include <linux/debugfs.h>
2666ca91 36#include <linux/devcoredump.h>
400e64df
OBC
37#include <linux/remoteproc.h>
38#include <linux/iommu.h>
b5ab5e24 39#include <linux/idr.h>
400e64df 40#include <linux/elf.h>
a2b950ac 41#include <linux/crc32.h>
400e64df
OBC
42#include <linux/virtio_ids.h>
43#include <linux/virtio_ring.h>
cf59d3e9 44#include <asm/byteorder.h>
400e64df
OBC
45
46#include "remoteproc_internal.h"
47
fec47d86
DG
48static DEFINE_MUTEX(rproc_list_mutex);
49static LIST_HEAD(rproc_list);
50
400e64df 51typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
fd2c15ec 52 struct resource_table *table, int len);
a2b950ac
OBC
53typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
54 void *, int offset, int avail);
400e64df 55
b5ab5e24
OBC
56/* Unique indices for remoteproc devices */
57static DEFINE_IDA(rproc_dev_index);
58
8afd519c
FGL
59static const char * const rproc_crash_names[] = {
60 [RPROC_MMUFAULT] = "mmufault",
b3d39032
BA
61 [RPROC_WATCHDOG] = "watchdog",
62 [RPROC_FATAL_ERROR] = "fatal error",
8afd519c
FGL
63};
64
65/* translate rproc_crash_type to string */
66static 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];
b23f7a09 70 return "unknown";
8afd519c
FGL
71}
72
400e64df
OBC
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.
400e64df
OBC
80 */
81static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
730f84ce 82 unsigned long iova, int flags, void *token)
400e64df 83{
8afd519c
FGL
84 struct rproc *rproc = token;
85
400e64df
OBC
86 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
87
8afd519c
FGL
88 rproc_report_crash(rproc, RPROC_MMUFAULT);
89
400e64df
OBC
90 /*
91 * Let the iommu core know we're not really handling this fault;
8afd519c 92 * we just used it as a recovery trigger.
400e64df
OBC
93 */
94 return -ENOSYS;
95}
96
97static int rproc_enable_iommu(struct rproc *rproc)
98{
99 struct iommu_domain *domain;
b5ab5e24 100 struct device *dev = rproc->dev.parent;
400e64df
OBC
101 int ret;
102
315491e5
SA
103 if (!rproc->has_iommu) {
104 dev_dbg(dev, "iommu not present\n");
0798e1da 105 return 0;
400e64df
OBC
106 }
107
108 domain = iommu_domain_alloc(dev->bus);
109 if (!domain) {
110 dev_err(dev, "can't alloc iommu domain\n");
111 return -ENOMEM;
112 }
113
77ca2332 114 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
400e64df
OBC
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
126free_domain:
127 iommu_domain_free(domain);
128 return ret;
129}
130
131static void rproc_disable_iommu(struct rproc *rproc)
132{
133 struct iommu_domain *domain = rproc->domain;
b5ab5e24 134 struct device *dev = rproc->dev.parent;
400e64df
OBC
135
136 if (!domain)
137 return;
138
139 iommu_detach_device(domain, dev);
140 iommu_domain_free(domain);
400e64df
OBC
141}
142
eb30596e
LP
143static 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
a01f7cd6
SA
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 *
400e64df
OBC
165 * Some remote processors will ask us to allocate them physically contiguous
166 * memory regions (which we call "carveouts"), and map them to specific
a01f7cd6
SA
167 * device addresses (which are hardcoded in the firmware). They may also have
168 * dedicated memory regions internal to the processors, and use them either
169 * exclusively or alongside carveouts.
400e64df
OBC
170 *
171 * They may then ask us to copy objects into specific device addresses (e.g.
172 * code/data sections) or expose us certain symbols in other device address
173 * (e.g. their trace buffer).
174 *
a01f7cd6
SA
175 * This function is a helper function with which we can go over the allocated
176 * carveouts and translate specific device addresses to kernel virtual addresses
177 * so we can access the referenced memory. This function also allows to perform
178 * translations on the internal remoteproc memory regions through a platform
179 * implementation specific da_to_va ops, if present.
180 *
181 * The function returns a valid kernel address on success or NULL on failure.
400e64df
OBC
182 *
183 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
184 * but only on kernel direct mapped RAM memory. Instead, we're just using
a01f7cd6
SA
185 * here the output of the DMA API for the carveouts, which should be more
186 * correct.
400e64df 187 */
72854fb0 188void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
400e64df
OBC
189{
190 struct rproc_mem_entry *carveout;
191 void *ptr = NULL;
192
a01f7cd6
SA
193 if (rproc->ops->da_to_va) {
194 ptr = rproc->ops->da_to_va(rproc, da, len);
195 if (ptr)
196 goto out;
197 }
198
400e64df
OBC
199 list_for_each_entry(carveout, &rproc->carveouts, node) {
200 int offset = da - carveout->da;
201
202 /* try next carveout if da is too small */
203 if (offset < 0)
204 continue;
205
206 /* try next carveout if da is too large */
207 if (offset + len > carveout->len)
208 continue;
209
210 ptr = carveout->va + offset;
211
212 break;
213 }
214
a01f7cd6 215out:
400e64df
OBC
216 return ptr;
217}
4afc89d6 218EXPORT_SYMBOL(rproc_da_to_va);
400e64df 219
b0019ccd
LP
220/**
221 * rproc_find_carveout_by_name() - lookup the carveout region by a name
222 * @rproc: handle of a remote processor
223 * @name,..: carveout name to find (standard printf format)
224 *
225 * Platform driver has the capability to register some pre-allacoted carveout
226 * (physically contiguous memory regions) before rproc firmware loading and
227 * associated resource table analysis. These regions may be dedicated memory
228 * regions internal to the coprocessor or specified DDR region with specific
229 * attributes
230 *
231 * This function is a helper function with which we can go over the
232 * allocated carveouts and return associated region characteristics like
233 * coprocessor address, length or processor virtual address.
234 *
235 * Return: a valid pointer on carveout entry on success or NULL on failure.
236 */
237struct rproc_mem_entry *
238rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
239{
240 va_list args;
241 char _name[32];
242 struct rproc_mem_entry *carveout, *mem = NULL;
243
244 if (!name)
245 return NULL;
246
247 va_start(args, name);
248 vsnprintf(_name, sizeof(_name), name, args);
249 va_end(args);
250
251 list_for_each_entry(carveout, &rproc->carveouts, node) {
252 /* Compare carveout and requested names */
253 if (!strcmp(carveout->name, _name)) {
254 mem = carveout;
255 break;
256 }
257 }
258
259 return mem;
260}
261
6db20ea8 262int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
400e64df 263{
7a186941 264 struct rproc *rproc = rvdev->rproc;
b5ab5e24 265 struct device *dev = &rproc->dev;
6db20ea8 266 struct rproc_vring *rvring = &rvdev->vring[i];
c0d63157 267 struct fw_rsc_vdev *rsc;
7a186941
OBC
268 dma_addr_t dma;
269 void *va;
270 int ret, size, notifyid;
400e64df 271
7a186941 272 /* actual size of vring (in bytes) */
6db20ea8 273 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
7a186941 274
7a186941
OBC
275 /*
276 * Allocate non-cacheable memory for the vring. In the future
277 * this call will also configure the IOMMU for us
278 */
b5ab5e24 279 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
7a186941 280 if (!va) {
b5ab5e24 281 dev_err(dev->parent, "dma_alloc_coherent failed\n");
400e64df
OBC
282 return -EINVAL;
283 }
284
6db20ea8
OBC
285 /*
286 * Assign an rproc-wide unique index for this vring
287 * TODO: assign a notifyid for rvdev updates as well
6db20ea8
OBC
288 * TODO: support predefined notifyids (via resource table)
289 */
15fc6110 290 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
b39599b7 291 if (ret < 0) {
15fc6110 292 dev_err(dev, "idr_alloc failed: %d\n", ret);
b5ab5e24 293 dma_free_coherent(dev->parent, size, va, dma);
7a186941
OBC
294 return ret;
295 }
15fc6110 296 notifyid = ret;
400e64df 297
48f18f89
BA
298 /* Potentially bump max_notifyid */
299 if (notifyid > rproc->max_notifyid)
300 rproc->max_notifyid = notifyid;
301
276ec993 302 dev_dbg(dev, "vring%d: va %pK dma %pad size 0x%x idr %d\n",
b605ed8b 303 i, va, &dma, size, notifyid);
7a186941 304
6db20ea8
OBC
305 rvring->va = va;
306 rvring->dma = dma;
307 rvring->notifyid = notifyid;
400e64df 308
c0d63157
SB
309 /*
310 * Let the rproc know the notifyid and da of this vring.
311 * Not all platforms use dma_alloc_coherent to automatically
312 * set up the iommu. In this case the device address (da) will
313 * hold the physical address and not the device address.
314 */
315 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
316 rsc->vring[i].da = dma;
317 rsc->vring[i].notifyid = notifyid;
400e64df
OBC
318 return 0;
319}
320
6db20ea8
OBC
321static int
322rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
7a186941
OBC
323{
324 struct rproc *rproc = rvdev->rproc;
b5ab5e24 325 struct device *dev = &rproc->dev;
6db20ea8
OBC
326 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
327 struct rproc_vring *rvring = &rvdev->vring[i];
7a186941 328
9d7814a9 329 dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
730f84ce 330 i, vring->da, vring->num, vring->align);
7a186941 331
6db20ea8
OBC
332 /* verify queue size and vring alignment are sane */
333 if (!vring->num || !vring->align) {
334 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
730f84ce 335 vring->num, vring->align);
6db20ea8 336 return -EINVAL;
7a186941 337 }
6db20ea8
OBC
338
339 rvring->len = vring->num;
340 rvring->align = vring->align;
341 rvring->rvdev = rvdev;
342
343 return 0;
344}
345
346void rproc_free_vring(struct rproc_vring *rvring)
347{
348 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
349 struct rproc *rproc = rvring->rvdev->rproc;
c0d63157
SB
350 int idx = rvring->rvdev->vring - rvring;
351 struct fw_rsc_vdev *rsc;
6db20ea8 352
b5ab5e24 353 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
6db20ea8 354 idr_remove(&rproc->notifyids, rvring->notifyid);
099a3f33 355
c0d63157
SB
356 /* reset resource entry info */
357 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
358 rsc->vring[idx].da = 0;
359 rsc->vring[idx].notifyid = -1;
7a186941
OBC
360}
361
6f8b0373 362static int rproc_vdev_do_start(struct rproc_subdev *subdev)
f5bcb353
BA
363{
364 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
365
366 return rproc_add_virtio_dev(rvdev, rvdev->id);
367}
368
6f8b0373 369static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
f5bcb353
BA
370{
371 struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
372
373 rproc_remove_virtio_dev(rvdev);
374}
375
400e64df 376/**
fd2c15ec 377 * rproc_handle_vdev() - handle a vdev fw resource
400e64df
OBC
378 * @rproc: the remote processor
379 * @rsc: the vring resource descriptor
fd2c15ec 380 * @avail: size of available data (for sanity checking the image)
400e64df 381 *
7a186941
OBC
382 * This resource entry requests the host to statically register a virtio
383 * device (vdev), and setup everything needed to support it. It contains
384 * everything needed to make it possible: the virtio device id, virtio
385 * device features, vrings information, virtio config space, etc...
386 *
387 * Before registering the vdev, the vrings are allocated from non-cacheable
388 * physically contiguous memory. Currently we only support two vrings per
389 * remote processor (temporary limitation). We might also want to consider
390 * doing the vring allocation only later when ->find_vqs() is invoked, and
391 * then release them upon ->del_vqs().
392 *
393 * Note: @da is currently not really handled correctly: we dynamically
394 * allocate it using the DMA API, ignoring requested hard coded addresses,
395 * and we don't take care of any required IOMMU programming. This is all
396 * going to be taken care of when the generic iommu-based DMA API will be
397 * merged. Meanwhile, statically-addressed iommu-based firmware images should
398 * use RSC_DEVMEM resource entries to map their required @da to the physical
399 * address of their base CMA region (ouch, hacky!).
400e64df
OBC
400 *
401 * Returns 0 on success, or an appropriate error code otherwise
402 */
fd2c15ec 403static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
730f84ce 404 int offset, int avail)
400e64df 405{
b5ab5e24 406 struct device *dev = &rproc->dev;
7a186941
OBC
407 struct rproc_vdev *rvdev;
408 int i, ret;
400e64df 409
fd2c15ec
OBC
410 /* make sure resource isn't truncated */
411 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
412 + rsc->config_len > avail) {
b5ab5e24 413 dev_err(dev, "vdev rsc is truncated\n");
400e64df
OBC
414 return -EINVAL;
415 }
416
fd2c15ec
OBC
417 /* make sure reserved bytes are zeroes */
418 if (rsc->reserved[0] || rsc->reserved[1]) {
419 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
400e64df
OBC
420 return -EINVAL;
421 }
422
9d7814a9 423 dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
fd2c15ec
OBC
424 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
425
7a186941
OBC
426 /* we currently support only two vrings per rvdev */
427 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
fd2c15ec 428 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
400e64df
OBC
429 return -EINVAL;
430 }
431
899585ad 432 rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
7a186941
OBC
433 if (!rvdev)
434 return -ENOMEM;
400e64df 435
aab8d802
BA
436 kref_init(&rvdev->refcount);
437
f5bcb353 438 rvdev->id = rsc->id;
7a186941 439 rvdev->rproc = rproc;
400e64df 440
6db20ea8 441 /* parse the vrings */
7a186941 442 for (i = 0; i < rsc->num_of_vrings; i++) {
6db20ea8 443 ret = rproc_parse_vring(rvdev, rsc, i);
7a186941 444 if (ret)
6db20ea8 445 goto free_rvdev;
7a186941 446 }
400e64df 447
a2b950ac
OBC
448 /* remember the resource offset*/
449 rvdev->rsc_offset = offset;
fd2c15ec 450
a863af5d
BA
451 /* allocate the vring resources */
452 for (i = 0; i < rsc->num_of_vrings; i++) {
453 ret = rproc_alloc_vring(rvdev, i);
454 if (ret)
455 goto unwind_vring_allocations;
456 }
457
7a186941 458 list_add_tail(&rvdev->node, &rproc->rvdevs);
fd2c15ec 459
6f8b0373
AE
460 rvdev->subdev.start = rproc_vdev_do_start;
461 rvdev->subdev.stop = rproc_vdev_do_stop;
4902676f
BA
462
463 rproc_add_subdev(rproc, &rvdev->subdev);
400e64df
OBC
464
465 return 0;
7a186941 466
a863af5d
BA
467unwind_vring_allocations:
468 for (i--; i >= 0; i--)
469 rproc_free_vring(&rvdev->vring[i]);
6db20ea8 470free_rvdev:
7a186941
OBC
471 kfree(rvdev);
472 return ret;
400e64df
OBC
473}
474
aab8d802
BA
475void rproc_vdev_release(struct kref *ref)
476{
477 struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
a863af5d 478 struct rproc_vring *rvring;
f5bcb353 479 struct rproc *rproc = rvdev->rproc;
a863af5d
BA
480 int id;
481
482 for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
483 rvring = &rvdev->vring[id];
484 if (!rvring->va)
485 continue;
486
487 rproc_free_vring(rvring);
488 }
aab8d802 489
f5bcb353 490 rproc_remove_subdev(rproc, &rvdev->subdev);
aab8d802
BA
491 list_del(&rvdev->node);
492 kfree(rvdev);
493}
494
400e64df
OBC
495/**
496 * rproc_handle_trace() - handle a shared trace buffer resource
497 * @rproc: the remote processor
498 * @rsc: the trace resource descriptor
fd2c15ec 499 * @avail: size of available data (for sanity checking the image)
400e64df
OBC
500 *
501 * In case the remote processor dumps trace logs into memory,
502 * export it via debugfs.
503 *
504 * Currently, the 'da' member of @rsc should contain the device address
505 * where the remote processor is dumping the traces. Later we could also
506 * support dynamically allocating this address using the generic
507 * DMA API (but currently there isn't a use case for that).
508 *
509 * Returns 0 on success, or an appropriate error code otherwise
510 */
fd2c15ec 511static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
730f84ce 512 int offset, int avail)
400e64df
OBC
513{
514 struct rproc_mem_entry *trace;
b5ab5e24 515 struct device *dev = &rproc->dev;
400e64df
OBC
516 void *ptr;
517 char name[15];
518
fd2c15ec 519 if (sizeof(*rsc) > avail) {
b5ab5e24 520 dev_err(dev, "trace rsc is truncated\n");
fd2c15ec
OBC
521 return -EINVAL;
522 }
523
524 /* make sure reserved bytes are zeroes */
525 if (rsc->reserved) {
526 dev_err(dev, "trace rsc has non zero reserved bytes\n");
527 return -EINVAL;
528 }
529
400e64df
OBC
530 /* what's the kernel address of this resource ? */
531 ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
532 if (!ptr) {
533 dev_err(dev, "erroneous trace resource entry\n");
534 return -EINVAL;
535 }
536
537 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
172e6ab1 538 if (!trace)
400e64df 539 return -ENOMEM;
400e64df
OBC
540
541 /* set the trace buffer dma properties */
542 trace->len = rsc->len;
543 trace->va = ptr;
544
545 /* make sure snprintf always null terminates, even if truncating */
546 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
547
548 /* create the debugfs entry */
549 trace->priv = rproc_create_trace_file(name, rproc, trace);
550 if (!trace->priv) {
551 trace->va = NULL;
552 kfree(trace);
553 return -EINVAL;
554 }
555
556 list_add_tail(&trace->node, &rproc->traces);
557
558 rproc->num_traces++;
559
276ec993 560 dev_dbg(dev, "%s added: va %pK, da 0x%x, len 0x%x\n",
35386166 561 name, ptr, rsc->da, rsc->len);
400e64df
OBC
562
563 return 0;
564}
565
566/**
567 * rproc_handle_devmem() - handle devmem resource entry
568 * @rproc: remote processor handle
569 * @rsc: the devmem resource entry
fd2c15ec 570 * @avail: size of available data (for sanity checking the image)
400e64df
OBC
571 *
572 * Remote processors commonly need to access certain on-chip peripherals.
573 *
574 * Some of these remote processors access memory via an iommu device,
575 * and might require us to configure their iommu before they can access
576 * the on-chip peripherals they need.
577 *
578 * This resource entry is a request to map such a peripheral device.
579 *
580 * These devmem entries will contain the physical address of the device in
581 * the 'pa' member. If a specific device address is expected, then 'da' will
582 * contain it (currently this is the only use case supported). 'len' will
583 * contain the size of the physical region we need to map.
584 *
585 * Currently we just "trust" those devmem entries to contain valid physical
586 * addresses, but this is going to change: we want the implementations to
587 * tell us ranges of physical addresses the firmware is allowed to request,
588 * and not allow firmwares to request access to physical addresses that
589 * are outside those ranges.
590 */
fd2c15ec 591static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
730f84ce 592 int offset, int avail)
400e64df
OBC
593{
594 struct rproc_mem_entry *mapping;
b5ab5e24 595 struct device *dev = &rproc->dev;
400e64df
OBC
596 int ret;
597
598 /* no point in handling this resource without a valid iommu domain */
599 if (!rproc->domain)
600 return -EINVAL;
601
fd2c15ec 602 if (sizeof(*rsc) > avail) {
b5ab5e24 603 dev_err(dev, "devmem rsc is truncated\n");
fd2c15ec
OBC
604 return -EINVAL;
605 }
606
607 /* make sure reserved bytes are zeroes */
608 if (rsc->reserved) {
b5ab5e24 609 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
fd2c15ec
OBC
610 return -EINVAL;
611 }
612
400e64df 613 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
172e6ab1 614 if (!mapping)
400e64df 615 return -ENOMEM;
400e64df
OBC
616
617 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
618 if (ret) {
b5ab5e24 619 dev_err(dev, "failed to map devmem: %d\n", ret);
400e64df
OBC
620 goto out;
621 }
622
623 /*
624 * We'll need this info later when we'll want to unmap everything
625 * (e.g. on shutdown).
626 *
627 * We can't trust the remote processor not to change the resource
628 * table, so we must maintain this info independently.
629 */
630 mapping->da = rsc->da;
631 mapping->len = rsc->len;
632 list_add_tail(&mapping->node, &rproc->mappings);
633
b5ab5e24 634 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
730f84ce 635 rsc->pa, rsc->da, rsc->len);
400e64df
OBC
636
637 return 0;
638
639out:
640 kfree(mapping);
641 return ret;
642}
643
f2e74abf
LP
644/**
645 * rproc_release_carveout() - release acquired carveout
646 * @rproc: rproc handle
647 * @mem: the memory entry to release
648 *
649 * This function releases specified memory entry @mem allocated via
650 * dma_alloc_coherent() function by @rproc.
651 */
652static int rproc_release_carveout(struct rproc *rproc,
653 struct rproc_mem_entry *mem)
654{
655 struct device *dev = &rproc->dev;
656
657 /* clean up carveout allocations */
658 dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
659 return 0;
660}
661
400e64df
OBC
662/**
663 * rproc_handle_carveout() - handle phys contig memory allocation requests
664 * @rproc: rproc handle
665 * @rsc: the resource entry
fd2c15ec 666 * @avail: size of available data (for image validation)
400e64df
OBC
667 *
668 * This function will handle firmware requests for allocation of physically
669 * contiguous memory regions.
670 *
671 * These request entries should come first in the firmware's resource table,
672 * as other firmware entries might request placing other data objects inside
673 * these memory regions (e.g. data/code segments, trace resource entries, ...).
674 *
675 * Allocating memory this way helps utilizing the reserved physical memory
676 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
677 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
678 * pressure is important; it may have a substantial impact on performance.
679 */
fd2c15ec 680static int rproc_handle_carveout(struct rproc *rproc,
730f84ce
AS
681 struct fw_rsc_carveout *rsc,
682 int offset, int avail)
400e64df 683{
72029c90 684 struct rproc_mem_entry *carveout, *mapping = NULL;
b5ab5e24 685 struct device *dev = &rproc->dev;
400e64df
OBC
686 dma_addr_t dma;
687 void *va;
688 int ret;
689
fd2c15ec 690 if (sizeof(*rsc) > avail) {
b5ab5e24 691 dev_err(dev, "carveout rsc is truncated\n");
fd2c15ec
OBC
692 return -EINVAL;
693 }
694
695 /* make sure reserved bytes are zeroes */
696 if (rsc->reserved) {
697 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
698 return -EINVAL;
699 }
700
9d7814a9 701 dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
35386166 702 rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
fd2c15ec 703
b5ab5e24 704 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
400e64df 705 if (!va) {
9c219b23
LJ
706 dev_err(dev->parent,
707 "failed to allocate dma memory: len 0x%x\n", rsc->len);
72029c90 708 return -ENOMEM;
400e64df
OBC
709 }
710
276ec993 711 dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n",
b605ed8b 712 va, &dma, rsc->len);
400e64df
OBC
713
714 /*
715 * Ok, this is non-standard.
716 *
717 * Sometimes we can't rely on the generic iommu-based DMA API
718 * to dynamically allocate the device address and then set the IOMMU
719 * tables accordingly, because some remote processors might
720 * _require_ us to use hard coded device addresses that their
721 * firmware was compiled with.
722 *
723 * In this case, we must use the IOMMU API directly and map
724 * the memory to the device address as expected by the remote
725 * processor.
726 *
727 * Obviously such remote processor devices should not be configured
728 * to use the iommu-based DMA API: we expect 'dma' to contain the
729 * physical address in this case.
730 */
3bc8140b
LP
731
732 if (rsc->da != FW_RSC_ADDR_ANY && !rproc->domain) {
733 dev_err(dev->parent,
734 "Bad carveout rsc configuration\n");
735 ret = -ENOMEM;
736 goto dma_free;
737 }
738
739 if (rsc->da != FW_RSC_ADDR_ANY && rproc->domain) {
7168d914
DC
740 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
741 if (!mapping) {
7168d914
DC
742 ret = -ENOMEM;
743 goto dma_free;
744 }
745
400e64df 746 ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
730f84ce 747 rsc->flags);
400e64df
OBC
748 if (ret) {
749 dev_err(dev, "iommu_map failed: %d\n", ret);
7168d914 750 goto free_mapping;
400e64df
OBC
751 }
752
753 /*
754 * We'll need this info later when we'll want to unmap
755 * everything (e.g. on shutdown).
756 *
757 * We can't trust the remote processor not to change the
758 * resource table, so we must maintain this info independently.
759 */
760 mapping->da = rsc->da;
761 mapping->len = rsc->len;
762 list_add_tail(&mapping->node, &rproc->mappings);
763
b605ed8b
AS
764 dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
765 rsc->da, &dma);
400e64df
OBC
766 }
767
0e49b72c
OBC
768 /*
769 * Some remote processors might need to know the pa
770 * even though they are behind an IOMMU. E.g., OMAP4's
771 * remote M3 processor needs this so it can control
772 * on-chip hardware accelerators that are not behind
773 * the IOMMU, and therefor must know the pa.
774 *
775 * Generally we don't want to expose physical addresses
776 * if we don't have to (remote processors are generally
777 * _not_ trusted), so we might want to do this only for
778 * remote processor that _must_ have this (e.g. OMAP4's
779 * dual M3 subsystem).
780 *
781 * Non-IOMMU processors might also want to have this info.
782 * In this case, the device address and the physical address
783 * are the same.
784 */
eb30596e 785 rsc->pa = (u32)rproc_va_to_pa(va);
0e49b72c 786
72029c90
LP
787 carveout = rproc_mem_entry_init(dev, va, dma, rsc->len, rsc->da,
788 rproc_release_carveout, rsc->name);
789 if (!carveout)
790 goto free_carv;
400e64df 791
15c0b025 792 rproc_add_carveout(rproc, carveout);
400e64df
OBC
793
794 return 0;
795
72029c90
LP
796free_carv:
797 kfree(carveout);
7168d914
DC
798free_mapping:
799 kfree(mapping);
400e64df 800dma_free:
b5ab5e24 801 dma_free_coherent(dev->parent, rsc->len, va, dma);
400e64df
OBC
802 return ret;
803}
804
15c0b025
LP
805/**
806 * rproc_add_carveout() - register an allocated carveout region
807 * @rproc: rproc handle
808 * @mem: memory entry to register
809 *
810 * This function registers specified memory entry in @rproc carveouts list.
811 * Specified carveout should have been allocated before registering.
812 */
813void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
814{
815 list_add_tail(&mem->node, &rproc->carveouts);
816}
817EXPORT_SYMBOL(rproc_add_carveout);
818
72029c90
LP
819/**
820 * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
821 * @dev: pointer on device struct
822 * @va: virtual address
823 * @dma: dma address
824 * @len: memory carveout length
825 * @da: device address
826 * @release: memory carveout function
827 * @name: carveout name
828 *
829 * This function allocates a rproc_mem_entry struct and fill it with parameters
830 * provided by client.
831 */
832struct rproc_mem_entry *
833rproc_mem_entry_init(struct device *dev,
834 void *va, dma_addr_t dma, int len, u32 da,
835 int (*release)(struct rproc *, struct rproc_mem_entry *),
836 const char *name, ...)
837{
838 struct rproc_mem_entry *mem;
839 va_list args;
840
841 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
842 if (!mem)
843 return mem;
844
845 mem->va = va;
846 mem->dma = dma;
847 mem->da = da;
848 mem->len = len;
849 mem->release = release;
850
851 va_start(args, name);
852 vsnprintf(mem->name, sizeof(mem->name), name, args);
853 va_end(args);
854
855 return mem;
856}
857EXPORT_SYMBOL(rproc_mem_entry_init);
858
859/**
e12bc14b
OBC
860 * A lookup table for resource handlers. The indices are defined in
861 * enum fw_resource_type.
862 */
232fcdbb 863static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
fd2c15ec
OBC
864 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
865 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
866 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
232fcdbb
SB
867 [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
868};
869
400e64df 870/* handle firmware resource entries before booting the remote processor */
a4b24c75 871static int rproc_handle_resources(struct rproc *rproc,
232fcdbb 872 rproc_handle_resource_t handlers[RSC_LAST])
400e64df 873{
b5ab5e24 874 struct device *dev = &rproc->dev;
e12bc14b 875 rproc_handle_resource_t handler;
fd2c15ec
OBC
876 int ret = 0, i;
877
d4bb86f2
BA
878 if (!rproc->table_ptr)
879 return 0;
880
a2b950ac
OBC
881 for (i = 0; i < rproc->table_ptr->num; i++) {
882 int offset = rproc->table_ptr->offset[i];
883 struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
a4b24c75 884 int avail = rproc->table_sz - offset - sizeof(*hdr);
fd2c15ec
OBC
885 void *rsc = (void *)hdr + sizeof(*hdr);
886
887 /* make sure table isn't truncated */
888 if (avail < 0) {
889 dev_err(dev, "rsc table is truncated\n");
890 return -EINVAL;
891 }
400e64df 892
fd2c15ec 893 dev_dbg(dev, "rsc: type %d\n", hdr->type);
400e64df 894
fd2c15ec
OBC
895 if (hdr->type >= RSC_LAST) {
896 dev_warn(dev, "unsupported resource %d\n", hdr->type);
e12bc14b 897 continue;
400e64df
OBC
898 }
899
232fcdbb 900 handler = handlers[hdr->type];
e12bc14b
OBC
901 if (!handler)
902 continue;
903
a2b950ac 904 ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
7a186941 905 if (ret)
400e64df 906 break;
fd2c15ec 907 }
400e64df
OBC
908
909 return ret;
910}
911
c455daa4
BA
912static int rproc_prepare_subdevices(struct rproc *rproc)
913{
914 struct rproc_subdev *subdev;
915 int ret;
916
917 list_for_each_entry(subdev, &rproc->subdevs, node) {
918 if (subdev->prepare) {
919 ret = subdev->prepare(subdev);
920 if (ret)
921 goto unroll_preparation;
922 }
923 }
924
925 return 0;
926
927unroll_preparation:
928 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
929 if (subdev->unprepare)
930 subdev->unprepare(subdev);
931 }
932
933 return ret;
934}
935
618fcff3 936static int rproc_start_subdevices(struct rproc *rproc)
7bdc9650
BA
937{
938 struct rproc_subdev *subdev;
939 int ret;
940
941 list_for_each_entry(subdev, &rproc->subdevs, node) {
be37b1e0
BA
942 if (subdev->start) {
943 ret = subdev->start(subdev);
944 if (ret)
945 goto unroll_registration;
946 }
7bdc9650
BA
947 }
948
949 return 0;
950
951unroll_registration:
be37b1e0
BA
952 list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
953 if (subdev->stop)
954 subdev->stop(subdev, true);
955 }
7bdc9650
BA
956
957 return ret;
958}
959
618fcff3 960static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
7bdc9650
BA
961{
962 struct rproc_subdev *subdev;
963
be37b1e0
BA
964 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
965 if (subdev->stop)
966 subdev->stop(subdev, crashed);
967 }
7bdc9650
BA
968}
969
c455daa4
BA
970static void rproc_unprepare_subdevices(struct rproc *rproc)
971{
972 struct rproc_subdev *subdev;
973
974 list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
975 if (subdev->unprepare)
976 subdev->unprepare(subdev);
977 }
978}
979
2666ca91
SJ
980/**
981 * rproc_coredump_cleanup() - clean up dump_segments list
982 * @rproc: the remote processor handle
983 */
984static void rproc_coredump_cleanup(struct rproc *rproc)
985{
986 struct rproc_dump_segment *entry, *tmp;
987
988 list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
989 list_del(&entry->node);
990 kfree(entry);
991 }
992}
993
400e64df
OBC
994/**
995 * rproc_resource_cleanup() - clean up and free all acquired resources
996 * @rproc: rproc handle
997 *
998 * This function will free all resources acquired for @rproc, and it
7a186941 999 * is called whenever @rproc either shuts down or fails to boot.
400e64df
OBC
1000 */
1001static void rproc_resource_cleanup(struct rproc *rproc)
1002{
1003 struct rproc_mem_entry *entry, *tmp;
d81fb32f 1004 struct rproc_vdev *rvdev, *rvtmp;
b5ab5e24 1005 struct device *dev = &rproc->dev;
400e64df
OBC
1006
1007 /* clean up debugfs trace entries */
1008 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
1009 rproc_remove_trace_file(entry->priv);
1010 rproc->num_traces--;
1011 list_del(&entry->node);
1012 kfree(entry);
1013 }
1014
400e64df
OBC
1015 /* clean up iommu mapping entries */
1016 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1017 size_t unmapped;
1018
1019 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1020 if (unmapped != entry->len) {
1021 /* nothing much to do besides complaining */
e981f6d4 1022 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
730f84ce 1023 unmapped);
400e64df
OBC
1024 }
1025
1026 list_del(&entry->node);
1027 kfree(entry);
1028 }
b6356a01
SA
1029
1030 /* clean up carveout allocations */
1031 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
f2e74abf
LP
1032 if (entry->release)
1033 entry->release(rproc, entry);
b6356a01
SA
1034 list_del(&entry->node);
1035 kfree(entry);
1036 }
d81fb32f
BA
1037
1038 /* clean up remote vdev entries */
f5bcb353 1039 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
2b45cef5 1040 kref_put(&rvdev->refcount, rproc_vdev_release);
2666ca91
SJ
1041
1042 rproc_coredump_cleanup(rproc);
400e64df
OBC
1043}
1044
1efa30d0
SJ
1045static int rproc_start(struct rproc *rproc, const struct firmware *fw)
1046{
a4b24c75 1047 struct resource_table *loaded_table;
1efa30d0 1048 struct device *dev = &rproc->dev;
a4b24c75 1049 int ret;
1efa30d0
SJ
1050
1051 /* load the ELF segments to memory */
1052 ret = rproc_load_segments(rproc, fw);
1053 if (ret) {
1054 dev_err(dev, "Failed to load program segments: %d\n", ret);
1055 return ret;
1056 }
1057
1058 /*
1059 * The starting device has been given the rproc->cached_table as the
1060 * resource table. The address of the vring along with the other
1061 * allocated resources (carveouts etc) is stored in cached_table.
1062 * In order to pass this information to the remote device we must copy
1063 * this information to device memory. We also update the table_ptr so
1064 * that any subsequent changes will be applied to the loaded version.
1065 */
1066 loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
1067 if (loaded_table) {
a4b24c75 1068 memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
1efa30d0
SJ
1069 rproc->table_ptr = loaded_table;
1070 }
1071
c455daa4
BA
1072 ret = rproc_prepare_subdevices(rproc);
1073 if (ret) {
1074 dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1075 rproc->name, ret);
f68d51bd 1076 goto reset_table_ptr;
c455daa4
BA
1077 }
1078
1efa30d0
SJ
1079 /* power up the remote processor */
1080 ret = rproc->ops->start(rproc);
1081 if (ret) {
1082 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
c455daa4 1083 goto unprepare_subdevices;
1efa30d0
SJ
1084 }
1085
618fcff3
BA
1086 /* Start any subdevices for the remote processor */
1087 ret = rproc_start_subdevices(rproc);
1efa30d0
SJ
1088 if (ret) {
1089 dev_err(dev, "failed to probe subdevices for %s: %d\n",
1090 rproc->name, ret);
c455daa4 1091 goto stop_rproc;
1efa30d0
SJ
1092 }
1093
1094 rproc->state = RPROC_RUNNING;
1095
1096 dev_info(dev, "remote processor %s is now up\n", rproc->name);
1097
1098 return 0;
c455daa4
BA
1099
1100stop_rproc:
1101 rproc->ops->stop(rproc);
c455daa4
BA
1102unprepare_subdevices:
1103 rproc_unprepare_subdevices(rproc);
f68d51bd
SA
1104reset_table_ptr:
1105 rproc->table_ptr = rproc->cached_table;
c455daa4
BA
1106
1107 return ret;
1efa30d0
SJ
1108}
1109
400e64df
OBC
1110/*
1111 * take a firmware and boot a remote processor with it.
1112 */
1113static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1114{
b5ab5e24 1115 struct device *dev = &rproc->dev;
400e64df 1116 const char *name = rproc->firmware;
58b64090 1117 int ret;
400e64df
OBC
1118
1119 ret = rproc_fw_sanity_check(rproc, fw);
1120 if (ret)
1121 return ret;
1122
e981f6d4 1123 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
400e64df
OBC
1124
1125 /*
1126 * if enabling an IOMMU isn't relevant for this rproc, this is
1127 * just a nop
1128 */
1129 ret = rproc_enable_iommu(rproc);
1130 if (ret) {
1131 dev_err(dev, "can't enable iommu: %d\n", ret);
1132 return ret;
1133 }
1134
3e5f9eb5 1135 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
988d204c 1136
c1d35c1a
BA
1137 /* Load resource table, core dump segment list etc from the firmware */
1138 ret = rproc_parse_fw(rproc, fw);
58b64090
BA
1139 if (ret)
1140 goto disable_iommu;
a0c10687 1141
b35d7afc
BA
1142 /* reset max_notifyid */
1143 rproc->max_notifyid = -1;
1144
400e64df 1145 /* handle fw resources which are required to boot rproc */
a4b24c75 1146 ret = rproc_handle_resources(rproc, rproc_loading_handlers);
400e64df
OBC
1147 if (ret) {
1148 dev_err(dev, "Failed to process resources: %d\n", ret);
229b85a6 1149 goto clean_up_resources;
400e64df
OBC
1150 }
1151
1efa30d0
SJ
1152 ret = rproc_start(rproc, fw);
1153 if (ret)
229b85a6 1154 goto clean_up_resources;
400e64df
OBC
1155
1156 return 0;
1157
229b85a6
BA
1158clean_up_resources:
1159 rproc_resource_cleanup(rproc);
a0c10687
BA
1160 kfree(rproc->cached_table);
1161 rproc->cached_table = NULL;
988d204c 1162 rproc->table_ptr = NULL;
58b64090 1163disable_iommu:
400e64df
OBC
1164 rproc_disable_iommu(rproc);
1165 return ret;
1166}
1167
1168/*
5e6533f7 1169 * take a firmware and boot it up.
400e64df
OBC
1170 *
1171 * Note: this function is called asynchronously upon registration of the
1172 * remote processor (so we must wait until it completes before we try
1173 * to unregister the device. one other option is just to use kref here,
1174 * that might be cleaner).
1175 */
5e6533f7 1176static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
400e64df
OBC
1177{
1178 struct rproc *rproc = context;
a2b950ac 1179
7a20c64d 1180 rproc_boot(rproc);
ddf71187 1181
3cc6e787 1182 release_firmware(fw);
400e64df
OBC
1183}
1184
5e6533f7 1185static int rproc_trigger_auto_boot(struct rproc *rproc)
70b85ef8
FGL
1186{
1187 int ret;
1188
70b85ef8 1189 /*
70b85ef8
FGL
1190 * We're initiating an asynchronous firmware loading, so we can
1191 * be built-in kernel code, without hanging the boot process.
1192 */
1193 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1194 rproc->firmware, &rproc->dev, GFP_KERNEL,
5e6533f7 1195 rproc, rproc_auto_boot_callback);
2099c77d 1196 if (ret < 0)
70b85ef8 1197 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
70b85ef8
FGL
1198
1199 return ret;
1200}
1201
880f5b38 1202static int rproc_stop(struct rproc *rproc, bool crashed)
1efa30d0
SJ
1203{
1204 struct device *dev = &rproc->dev;
1205 int ret;
1206
618fcff3
BA
1207 /* Stop any subdevices for the remote processor */
1208 rproc_stop_subdevices(rproc, crashed);
1efa30d0 1209
0a8b81cb
BA
1210 /* the installed resource table is no longer accessible */
1211 rproc->table_ptr = rproc->cached_table;
1212
1efa30d0
SJ
1213 /* power off the remote processor */
1214 ret = rproc->ops->stop(rproc);
1215 if (ret) {
1216 dev_err(dev, "can't stop rproc: %d\n", ret);
1217 return ret;
1218 }
1219
c455daa4
BA
1220 rproc_unprepare_subdevices(rproc);
1221
1efa30d0
SJ
1222 rproc->state = RPROC_OFFLINE;
1223
1224 dev_info(dev, "stopped remote processor %s\n", rproc->name);
1225
1226 return 0;
1227}
1228
2666ca91
SJ
1229/**
1230 * rproc_coredump_add_segment() - add segment of device memory to coredump
1231 * @rproc: handle of a remote processor
1232 * @da: device address
1233 * @size: size of segment
1234 *
1235 * Add device memory to the list of segments to be included in a coredump for
1236 * the remoteproc.
1237 *
1238 * Return: 0 on success, negative errno on error.
1239 */
1240int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
1241{
1242 struct rproc_dump_segment *segment;
1243
1244 segment = kzalloc(sizeof(*segment), GFP_KERNEL);
1245 if (!segment)
1246 return -ENOMEM;
1247
1248 segment->da = da;
1249 segment->size = size;
1250
1251 list_add_tail(&segment->node, &rproc->dump_segments);
1252
1253 return 0;
1254}
1255EXPORT_SYMBOL(rproc_coredump_add_segment);
1256
1257/**
1258 * rproc_coredump() - perform coredump
1259 * @rproc: rproc handle
1260 *
1261 * This function will generate an ELF header for the registered segments
1262 * and create a devcoredump device associated with rproc.
1263 */
1264static void rproc_coredump(struct rproc *rproc)
1265{
1266 struct rproc_dump_segment *segment;
1267 struct elf32_phdr *phdr;
1268 struct elf32_hdr *ehdr;
1269 size_t data_size;
1270 size_t offset;
1271 void *data;
1272 void *ptr;
1273 int phnum = 0;
1274
1275 if (list_empty(&rproc->dump_segments))
1276 return;
1277
1278 data_size = sizeof(*ehdr);
1279 list_for_each_entry(segment, &rproc->dump_segments, node) {
1280 data_size += sizeof(*phdr) + segment->size;
1281
1282 phnum++;
1283 }
1284
1285 data = vmalloc(data_size);
1286 if (!data)
1287 return;
1288
1289 ehdr = data;
1290
1291 memset(ehdr, 0, sizeof(*ehdr));
1292 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1293 ehdr->e_ident[EI_CLASS] = ELFCLASS32;
1294 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1295 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1296 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1297 ehdr->e_type = ET_CORE;
1298 ehdr->e_machine = EM_NONE;
1299 ehdr->e_version = EV_CURRENT;
1300 ehdr->e_entry = rproc->bootaddr;
1301 ehdr->e_phoff = sizeof(*ehdr);
1302 ehdr->e_ehsize = sizeof(*ehdr);
1303 ehdr->e_phentsize = sizeof(*phdr);
1304 ehdr->e_phnum = phnum;
1305
1306 phdr = data + ehdr->e_phoff;
1307 offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum;
1308 list_for_each_entry(segment, &rproc->dump_segments, node) {
1309 memset(phdr, 0, sizeof(*phdr));
1310 phdr->p_type = PT_LOAD;
1311 phdr->p_offset = offset;
1312 phdr->p_vaddr = segment->da;
1313 phdr->p_paddr = segment->da;
1314 phdr->p_filesz = segment->size;
1315 phdr->p_memsz = segment->size;
1316 phdr->p_flags = PF_R | PF_W | PF_X;
1317 phdr->p_align = 0;
1318
1319 ptr = rproc_da_to_va(rproc, segment->da, segment->size);
1320 if (!ptr) {
1321 dev_err(&rproc->dev,
1322 "invalid coredump segment (%pad, %zu)\n",
1323 &segment->da, segment->size);
1324 memset(data + offset, 0xff, segment->size);
1325 } else {
1326 memcpy(data + offset, ptr, segment->size);
1327 }
1328
1329 offset += phdr->p_filesz;
1330 phdr++;
1331 }
1332
1333 dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
1334}
1335
70b85ef8
FGL
1336/**
1337 * rproc_trigger_recovery() - recover a remoteproc
1338 * @rproc: the remote processor
1339 *
56324d7a 1340 * The recovery is done by resetting all the virtio devices, that way all the
70b85ef8
FGL
1341 * rpmsg drivers will be reseted along with the remote processor making the
1342 * remoteproc functional again.
1343 *
1344 * This function can sleep, so it cannot be called from atomic context.
1345 */
1346int rproc_trigger_recovery(struct rproc *rproc)
1347{
7e83cab8
SJ
1348 const struct firmware *firmware_p;
1349 struct device *dev = &rproc->dev;
1350 int ret;
1351
1352 dev_err(dev, "recovering %s\n", rproc->name);
70b85ef8 1353
7e83cab8
SJ
1354 ret = mutex_lock_interruptible(&rproc->lock);
1355 if (ret)
1356 return ret;
1357
fcd58037 1358 ret = rproc_stop(rproc, true);
7e83cab8
SJ
1359 if (ret)
1360 goto unlock_mutex;
ddf71187 1361
2666ca91
SJ
1362 /* generate coredump */
1363 rproc_coredump(rproc);
1364
7e83cab8
SJ
1365 /* load firmware */
1366 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1367 if (ret < 0) {
1368 dev_err(dev, "request_firmware failed: %d\n", ret);
1369 goto unlock_mutex;
1370 }
ddf71187 1371
7e83cab8
SJ
1372 /* boot the remote processor up again */
1373 ret = rproc_start(rproc, firmware_p);
1374
1375 release_firmware(firmware_p);
1376
1377unlock_mutex:
1378 mutex_unlock(&rproc->lock);
1379 return ret;
70b85ef8
FGL
1380}
1381
8afd519c
FGL
1382/**
1383 * rproc_crash_handler_work() - handle a crash
1384 *
1385 * This function needs to handle everything related to a crash, like cpu
1386 * registers and stack dump, information to help to debug the fatal error, etc.
1387 */
1388static void rproc_crash_handler_work(struct work_struct *work)
1389{
1390 struct rproc *rproc = container_of(work, struct rproc, crash_handler);
1391 struct device *dev = &rproc->dev;
1392
1393 dev_dbg(dev, "enter %s\n", __func__);
1394
1395 mutex_lock(&rproc->lock);
1396
1397 if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
1398 /* handle only the first crash detected */
1399 mutex_unlock(&rproc->lock);
1400 return;
1401 }
1402
1403 rproc->state = RPROC_CRASHED;
1404 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
1405 rproc->name);
1406
1407 mutex_unlock(&rproc->lock);
1408
2e37abb8
FGL
1409 if (!rproc->recovery_disabled)
1410 rproc_trigger_recovery(rproc);
8afd519c
FGL
1411}
1412
400e64df 1413/**
1b0ef906 1414 * rproc_boot() - boot a remote processor
400e64df
OBC
1415 * @rproc: handle of a remote processor
1416 *
1417 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1418 *
1419 * If the remote processor is already powered on, this function immediately
1420 * returns (successfully).
1421 *
1422 * Returns 0 on success, and an appropriate error value otherwise.
1423 */
1b0ef906 1424int rproc_boot(struct rproc *rproc)
400e64df
OBC
1425{
1426 const struct firmware *firmware_p;
1427 struct device *dev;
1428 int ret;
1429
1430 if (!rproc) {
1431 pr_err("invalid rproc handle\n");
1432 return -EINVAL;
1433 }
1434
b5ab5e24 1435 dev = &rproc->dev;
400e64df
OBC
1436
1437 ret = mutex_lock_interruptible(&rproc->lock);
1438 if (ret) {
1439 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1440 return ret;
1441 }
1442
2099c77d
SJ
1443 if (rproc->state == RPROC_DELETED) {
1444 ret = -ENODEV;
1445 dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
1446 goto unlock_mutex;
1447 }
1448
400e64df
OBC
1449 /* skip the boot process if rproc is already powered up */
1450 if (atomic_inc_return(&rproc->power) > 1) {
1451 ret = 0;
1452 goto unlock_mutex;
1453 }
1454
1455 dev_info(dev, "powering up %s\n", rproc->name);
1456
1457 /* load firmware */
1458 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1459 if (ret < 0) {
1460 dev_err(dev, "request_firmware failed: %d\n", ret);
1461 goto downref_rproc;
1462 }
1463
1464 ret = rproc_fw_boot(rproc, firmware_p);
1465
1466 release_firmware(firmware_p);
1467
1468downref_rproc:
fbb6aacb 1469 if (ret)
400e64df 1470 atomic_dec(&rproc->power);
400e64df
OBC
1471unlock_mutex:
1472 mutex_unlock(&rproc->lock);
1473 return ret;
1474}
1475EXPORT_SYMBOL(rproc_boot);
1476
1477/**
1478 * rproc_shutdown() - power off the remote processor
1479 * @rproc: the remote processor
1480 *
1481 * Power off a remote processor (previously booted with rproc_boot()).
1482 *
1483 * In case @rproc is still being used by an additional user(s), then
1484 * this function will just decrement the power refcount and exit,
1485 * without really powering off the device.
1486 *
1487 * Every call to rproc_boot() must (eventually) be accompanied by a call
1488 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1489 *
1490 * Notes:
1491 * - we're not decrementing the rproc's refcount, only the power refcount.
1492 * which means that the @rproc handle stays valid even after rproc_shutdown()
1493 * returns, and users can still use it with a subsequent rproc_boot(), if
1494 * needed.
400e64df
OBC
1495 */
1496void rproc_shutdown(struct rproc *rproc)
1497{
b5ab5e24 1498 struct device *dev = &rproc->dev;
400e64df
OBC
1499 int ret;
1500
1501 ret = mutex_lock_interruptible(&rproc->lock);
1502 if (ret) {
1503 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1504 return;
1505 }
1506
1507 /* if the remote proc is still needed, bail out */
1508 if (!atomic_dec_and_test(&rproc->power))
1509 goto out;
1510
fcd58037 1511 ret = rproc_stop(rproc, false);
400e64df
OBC
1512 if (ret) {
1513 atomic_inc(&rproc->power);
400e64df
OBC
1514 goto out;
1515 }
1516
1517 /* clean up all acquired resources */
1518 rproc_resource_cleanup(rproc);
1519
1520 rproc_disable_iommu(rproc);
1521
988d204c 1522 /* Free the copy of the resource table */
a0c10687
BA
1523 kfree(rproc->cached_table);
1524 rproc->cached_table = NULL;
988d204c 1525 rproc->table_ptr = NULL;
400e64df
OBC
1526out:
1527 mutex_unlock(&rproc->lock);
400e64df
OBC
1528}
1529EXPORT_SYMBOL(rproc_shutdown);
1530
fec47d86
DG
1531/**
1532 * rproc_get_by_phandle() - find a remote processor by phandle
1533 * @phandle: phandle to the rproc
1534 *
1535 * Finds an rproc handle using the remote processor's phandle, and then
1536 * return a handle to the rproc.
1537 *
1538 * This function increments the remote processor's refcount, so always
1539 * use rproc_put() to decrement it back once rproc isn't needed anymore.
1540 *
1541 * Returns the rproc handle on success, and NULL on failure.
1542 */
8de3dbd0 1543#ifdef CONFIG_OF
fec47d86
DG
1544struct rproc *rproc_get_by_phandle(phandle phandle)
1545{
1546 struct rproc *rproc = NULL, *r;
1547 struct device_node *np;
1548
1549 np = of_find_node_by_phandle(phandle);
1550 if (!np)
1551 return NULL;
1552
1553 mutex_lock(&rproc_list_mutex);
1554 list_for_each_entry(r, &rproc_list, node) {
1555 if (r->dev.parent && r->dev.parent->of_node == np) {
fbb6aacb
BA
1556 /* prevent underlying implementation from being removed */
1557 if (!try_module_get(r->dev.parent->driver->owner)) {
1558 dev_err(&r->dev, "can't get owner\n");
1559 break;
1560 }
1561
fec47d86
DG
1562 rproc = r;
1563 get_device(&rproc->dev);
1564 break;
1565 }
1566 }
1567 mutex_unlock(&rproc_list_mutex);
1568
1569 of_node_put(np);
1570
1571 return rproc;
1572}
8de3dbd0
OBC
1573#else
1574struct rproc *rproc_get_by_phandle(phandle phandle)
1575{
1576 return NULL;
1577}
1578#endif
fec47d86
DG
1579EXPORT_SYMBOL(rproc_get_by_phandle);
1580
400e64df 1581/**
160e7c84 1582 * rproc_add() - register a remote processor
400e64df
OBC
1583 * @rproc: the remote processor handle to register
1584 *
1585 * Registers @rproc with the remoteproc framework, after it has been
1586 * allocated with rproc_alloc().
1587 *
1588 * This is called by the platform-specific rproc implementation, whenever
1589 * a new remote processor device is probed.
1590 *
1591 * Returns 0 on success and an appropriate error code otherwise.
1592 *
1593 * Note: this function initiates an asynchronous firmware loading
1594 * context, which will look for virtio devices supported by the rproc's
1595 * firmware.
1596 *
1597 * If found, those virtio devices will be created and added, so as a result
7a186941 1598 * of registering this remote processor, additional virtio drivers might be
400e64df 1599 * probed.
400e64df 1600 */
160e7c84 1601int rproc_add(struct rproc *rproc)
400e64df 1602{
b5ab5e24 1603 struct device *dev = &rproc->dev;
70b85ef8 1604 int ret;
400e64df 1605
b5ab5e24
OBC
1606 ret = device_add(dev);
1607 if (ret < 0)
1608 return ret;
400e64df 1609
b5ab5e24 1610 dev_info(dev, "%s is available\n", rproc->name);
400e64df
OBC
1611
1612 /* create debugfs entries */
1613 rproc_create_debug_dir(rproc);
7a20c64d
SJ
1614
1615 /* if rproc is marked always-on, request it to boot */
1616 if (rproc->auto_boot) {
5e6533f7 1617 ret = rproc_trigger_auto_boot(rproc);
7a20c64d
SJ
1618 if (ret < 0)
1619 return ret;
1620 }
400e64df 1621
d2e12e66
DG
1622 /* expose to rproc_get_by_phandle users */
1623 mutex_lock(&rproc_list_mutex);
1624 list_add(&rproc->node, &rproc_list);
1625 mutex_unlock(&rproc_list_mutex);
1626
1627 return 0;
400e64df 1628}
160e7c84 1629EXPORT_SYMBOL(rproc_add);
400e64df 1630
b5ab5e24
OBC
1631/**
1632 * rproc_type_release() - release a remote processor instance
1633 * @dev: the rproc's device
1634 *
1635 * This function should _never_ be called directly.
1636 *
1637 * It will be called by the driver core when no one holds a valid pointer
1638 * to @dev anymore.
1639 */
1640static void rproc_type_release(struct device *dev)
1641{
1642 struct rproc *rproc = container_of(dev, struct rproc, dev);
1643
7183a2a7
OBC
1644 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1645
b5ab5e24
OBC
1646 idr_destroy(&rproc->notifyids);
1647
1648 if (rproc->index >= 0)
1649 ida_simple_remove(&rproc_dev_index, rproc->index);
1650
0f57dc6a 1651 kfree(rproc->firmware);
fb98e2bd 1652 kfree(rproc->ops);
b5ab5e24
OBC
1653 kfree(rproc);
1654}
1655
c42ca04d 1656static const struct device_type rproc_type = {
b5ab5e24
OBC
1657 .name = "remoteproc",
1658 .release = rproc_type_release,
1659};
400e64df
OBC
1660
1661/**
1662 * rproc_alloc() - allocate a remote processor handle
1663 * @dev: the underlying device
1664 * @name: name of this remote processor
1665 * @ops: platform-specific handlers (mainly start/stop)
8b4aec9a 1666 * @firmware: name of firmware file to load, can be NULL
400e64df
OBC
1667 * @len: length of private data needed by the rproc driver (in bytes)
1668 *
1669 * Allocates a new remote processor handle, but does not register
8b4aec9a 1670 * it yet. if @firmware is NULL, a default name is used.
400e64df
OBC
1671 *
1672 * This function should be used by rproc implementations during initialization
1673 * of the remote processor.
1674 *
1675 * After creating an rproc handle using this function, and when ready,
160e7c84 1676 * implementations should then call rproc_add() to complete
400e64df
OBC
1677 * the registration of the remote processor.
1678 *
1679 * On success the new rproc is returned, and on failure, NULL.
1680 *
1681 * Note: _never_ directly deallocate @rproc, even if it was not registered
433c0e04 1682 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
400e64df
OBC
1683 */
1684struct rproc *rproc_alloc(struct device *dev, const char *name,
730f84ce
AS
1685 const struct rproc_ops *ops,
1686 const char *firmware, int len)
400e64df
OBC
1687{
1688 struct rproc *rproc;
8b4aec9a 1689 char *p, *template = "rproc-%s-fw";
0f57dc6a 1690 int name_len;
400e64df
OBC
1691
1692 if (!dev || !name || !ops)
1693 return NULL;
1694
0f57dc6a 1695 if (!firmware) {
8b4aec9a 1696 /*
8b4aec9a 1697 * If the caller didn't pass in a firmware name then
0f57dc6a 1698 * construct a default name.
8b4aec9a
RT
1699 */
1700 name_len = strlen(name) + strlen(template) - 2 + 1;
0f57dc6a
MR
1701 p = kmalloc(name_len, GFP_KERNEL);
1702 if (!p)
1703 return NULL;
8b4aec9a
RT
1704 snprintf(p, name_len, template, name);
1705 } else {
0f57dc6a
MR
1706 p = kstrdup(firmware, GFP_KERNEL);
1707 if (!p)
1708 return NULL;
1709 }
1710
1711 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1712 if (!rproc) {
1713 kfree(p);
1714 return NULL;
8b4aec9a
RT
1715 }
1716
fb98e2bd
BA
1717 rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
1718 if (!rproc->ops) {
1719 kfree(p);
1720 kfree(rproc);
1721 return NULL;
1722 }
1723
8b4aec9a 1724 rproc->firmware = p;
400e64df 1725 rproc->name = name;
400e64df 1726 rproc->priv = &rproc[1];
ddf71187 1727 rproc->auto_boot = true;
400e64df 1728
b5ab5e24
OBC
1729 device_initialize(&rproc->dev);
1730 rproc->dev.parent = dev;
1731 rproc->dev.type = &rproc_type;
2aefbef0 1732 rproc->dev.class = &rproc_class;
7c89717f 1733 rproc->dev.driver_data = rproc;
b5ab5e24
OBC
1734
1735 /* Assign a unique device index and name */
1736 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1737 if (rproc->index < 0) {
1738 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1739 put_device(&rproc->dev);
1740 return NULL;
1741 }
1742
1743 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1744
400e64df
OBC
1745 atomic_set(&rproc->power, 0);
1746
0f21f9cc
BA
1747 /* Default to ELF loader if no load function is specified */
1748 if (!rproc->ops->load) {
1749 rproc->ops->load = rproc_elf_load_segments;
c1d35c1a 1750 rproc->ops->parse_fw = rproc_elf_load_rsc_table;
0f21f9cc
BA
1751 rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
1752 rproc->ops->sanity_check = rproc_elf_sanity_check;
1753 rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
1754 }
400e64df
OBC
1755
1756 mutex_init(&rproc->lock);
1757
7a186941
OBC
1758 idr_init(&rproc->notifyids);
1759
400e64df
OBC
1760 INIT_LIST_HEAD(&rproc->carveouts);
1761 INIT_LIST_HEAD(&rproc->mappings);
1762 INIT_LIST_HEAD(&rproc->traces);
7a186941 1763 INIT_LIST_HEAD(&rproc->rvdevs);
7bdc9650 1764 INIT_LIST_HEAD(&rproc->subdevs);
2666ca91 1765 INIT_LIST_HEAD(&rproc->dump_segments);
400e64df 1766
8afd519c
FGL
1767 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
1768
400e64df
OBC
1769 rproc->state = RPROC_OFFLINE;
1770
1771 return rproc;
1772}
1773EXPORT_SYMBOL(rproc_alloc);
1774
1775/**
433c0e04
BA
1776 * rproc_free() - unroll rproc_alloc()
1777 * @rproc: the remote processor handle
1778 *
1779 * This function decrements the rproc dev refcount.
1780 *
1781 * If no one holds any reference to rproc anymore, then its refcount would
1782 * now drop to zero, and it would be freed.
1783 */
1784void rproc_free(struct rproc *rproc)
1785{
1786 put_device(&rproc->dev);
1787}
1788EXPORT_SYMBOL(rproc_free);
1789
1790/**
1791 * rproc_put() - release rproc reference
400e64df
OBC
1792 * @rproc: the remote processor handle
1793 *
c6b5a276 1794 * This function decrements the rproc dev refcount.
400e64df 1795 *
c6b5a276
OBC
1796 * If no one holds any reference to rproc anymore, then its refcount would
1797 * now drop to zero, and it would be freed.
400e64df 1798 */
160e7c84 1799void rproc_put(struct rproc *rproc)
400e64df 1800{
fbb6aacb 1801 module_put(rproc->dev.parent->driver->owner);
b5ab5e24 1802 put_device(&rproc->dev);
400e64df 1803}
160e7c84 1804EXPORT_SYMBOL(rproc_put);
400e64df
OBC
1805
1806/**
160e7c84 1807 * rproc_del() - unregister a remote processor
400e64df
OBC
1808 * @rproc: rproc handle to unregister
1809 *
400e64df
OBC
1810 * This function should be called when the platform specific rproc
1811 * implementation decides to remove the rproc device. it should
160e7c84 1812 * _only_ be called if a previous invocation of rproc_add()
400e64df
OBC
1813 * has completed successfully.
1814 *
160e7c84 1815 * After rproc_del() returns, @rproc isn't freed yet, because
c6b5a276 1816 * of the outstanding reference created by rproc_alloc. To decrement that
433c0e04 1817 * one last refcount, one still needs to call rproc_free().
400e64df
OBC
1818 *
1819 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1820 */
160e7c84 1821int rproc_del(struct rproc *rproc)
400e64df
OBC
1822{
1823 if (!rproc)
1824 return -EINVAL;
1825
ddf71187
BA
1826 /* if rproc is marked always-on, rproc_add() booted it */
1827 /* TODO: make sure this works with rproc->power > 1 */
1828 if (rproc->auto_boot)
1829 rproc_shutdown(rproc);
1830
2099c77d
SJ
1831 mutex_lock(&rproc->lock);
1832 rproc->state = RPROC_DELETED;
1833 mutex_unlock(&rproc->lock);
1834
b003d45b
SJ
1835 rproc_delete_debug_dir(rproc);
1836
fec47d86
DG
1837 /* the rproc is downref'ed as soon as it's removed from the klist */
1838 mutex_lock(&rproc_list_mutex);
1839 list_del(&rproc->node);
1840 mutex_unlock(&rproc_list_mutex);
1841
b5ab5e24 1842 device_del(&rproc->dev);
400e64df
OBC
1843
1844 return 0;
1845}
160e7c84 1846EXPORT_SYMBOL(rproc_del);
400e64df 1847
7bdc9650
BA
1848/**
1849 * rproc_add_subdev() - add a subdevice to a remoteproc
1850 * @rproc: rproc handle to add the subdevice to
1851 * @subdev: subdev handle to register
4902676f
BA
1852 *
1853 * Caller is responsible for populating optional subdevice function pointers.
7bdc9650 1854 */
4902676f 1855void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
7bdc9650 1856{
7bdc9650
BA
1857 list_add_tail(&subdev->node, &rproc->subdevs);
1858}
1859EXPORT_SYMBOL(rproc_add_subdev);
1860
1861/**
1862 * rproc_remove_subdev() - remove a subdevice from a remoteproc
1863 * @rproc: rproc handle to remove the subdevice from
1864 * @subdev: subdev handle, previously registered with rproc_add_subdev()
1865 */
1866void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
1867{
1868 list_del(&subdev->node);
1869}
1870EXPORT_SYMBOL(rproc_remove_subdev);
1871
7c89717f
BA
1872/**
1873 * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
1874 * @dev: child device to find ancestor of
1875 *
1876 * Returns the ancestor rproc instance, or NULL if not found.
1877 */
1878struct rproc *rproc_get_by_child(struct device *dev)
1879{
1880 for (dev = dev->parent; dev; dev = dev->parent) {
1881 if (dev->type == &rproc_type)
1882 return dev->driver_data;
1883 }
1884
1885 return NULL;
1886}
1887EXPORT_SYMBOL(rproc_get_by_child);
1888
8afd519c
FGL
1889/**
1890 * rproc_report_crash() - rproc crash reporter function
1891 * @rproc: remote processor
1892 * @type: crash type
1893 *
1894 * This function must be called every time a crash is detected by the low-level
1895 * drivers implementing a specific remoteproc. This should not be called from a
1896 * non-remoteproc driver.
1897 *
1898 * This function can be called from atomic/interrupt context.
1899 */
1900void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
1901{
1902 if (!rproc) {
1903 pr_err("NULL rproc pointer\n");
1904 return;
1905 }
1906
1907 dev_err(&rproc->dev, "crash detected in %s: type %s\n",
1908 rproc->name, rproc_crash_to_string(type));
1909
1910 /* create a new task to handle the error */
1911 schedule_work(&rproc->crash_handler);
1912}
1913EXPORT_SYMBOL(rproc_report_crash);
1914
400e64df
OBC
1915static int __init remoteproc_init(void)
1916{
2aefbef0 1917 rproc_init_sysfs();
400e64df 1918 rproc_init_debugfs();
b5ab5e24 1919
400e64df
OBC
1920 return 0;
1921}
1922module_init(remoteproc_init);
1923
1924static void __exit remoteproc_exit(void)
1925{
f42f79af
SA
1926 ida_destroy(&rproc_dev_index);
1927
400e64df 1928 rproc_exit_debugfs();
2aefbef0 1929 rproc_exit_sysfs();
400e64df
OBC
1930}
1931module_exit(remoteproc_exit);
1932
1933MODULE_LICENSE("GPL v2");
1934MODULE_DESCRIPTION("Generic Remote Processor Framework");