remoteproc: create a 'recovery' debugfs entry
[deliverable/linux.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>
36#include <linux/remoteproc.h>
37#include <linux/iommu.h>
b5ab5e24 38#include <linux/idr.h>
400e64df
OBC
39#include <linux/elf.h>
40#include <linux/virtio_ids.h>
41#include <linux/virtio_ring.h>
cf59d3e9 42#include <asm/byteorder.h>
400e64df
OBC
43
44#include "remoteproc_internal.h"
45
400e64df 46typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
fd2c15ec
OBC
47 struct resource_table *table, int len);
48typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
400e64df 49
b5ab5e24
OBC
50/* Unique indices for remoteproc devices */
51static DEFINE_IDA(rproc_dev_index);
52
8afd519c
FGL
53static const char * const rproc_crash_names[] = {
54 [RPROC_MMUFAULT] = "mmufault",
55};
56
57/* translate rproc_crash_type to string */
58static const char *rproc_crash_to_string(enum rproc_crash_type type)
59{
60 if (type < ARRAY_SIZE(rproc_crash_names))
61 return rproc_crash_names[type];
62 return "unkown";
63}
64
400e64df
OBC
65/*
66 * This is the IOMMU fault handler we register with the IOMMU API
67 * (when relevant; not all remote processors access memory through
68 * an IOMMU).
69 *
70 * IOMMU core will invoke this handler whenever the remote processor
71 * will try to access an unmapped device address.
400e64df
OBC
72 */
73static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
77ca2332 74 unsigned long iova, int flags, void *token)
400e64df 75{
8afd519c
FGL
76 struct rproc *rproc = token;
77
400e64df
OBC
78 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
79
8afd519c
FGL
80 rproc_report_crash(rproc, RPROC_MMUFAULT);
81
400e64df
OBC
82 /*
83 * Let the iommu core know we're not really handling this fault;
8afd519c 84 * we just used it as a recovery trigger.
400e64df
OBC
85 */
86 return -ENOSYS;
87}
88
89static int rproc_enable_iommu(struct rproc *rproc)
90{
91 struct iommu_domain *domain;
b5ab5e24 92 struct device *dev = rproc->dev.parent;
400e64df
OBC
93 int ret;
94
95 /*
96 * We currently use iommu_present() to decide if an IOMMU
97 * setup is needed.
98 *
99 * This works for simple cases, but will easily fail with
100 * platforms that do have an IOMMU, but not for this specific
101 * rproc.
102 *
103 * This will be easily solved by introducing hw capabilities
104 * that will be set by the remoteproc driver.
105 */
106 if (!iommu_present(dev->bus)) {
0798e1da
MG
107 dev_dbg(dev, "iommu not found\n");
108 return 0;
400e64df
OBC
109 }
110
111 domain = iommu_domain_alloc(dev->bus);
112 if (!domain) {
113 dev_err(dev, "can't alloc iommu domain\n");
114 return -ENOMEM;
115 }
116
77ca2332 117 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
400e64df
OBC
118
119 ret = iommu_attach_device(domain, dev);
120 if (ret) {
121 dev_err(dev, "can't attach iommu device: %d\n", ret);
122 goto free_domain;
123 }
124
125 rproc->domain = domain;
126
127 return 0;
128
129free_domain:
130 iommu_domain_free(domain);
131 return ret;
132}
133
134static void rproc_disable_iommu(struct rproc *rproc)
135{
136 struct iommu_domain *domain = rproc->domain;
b5ab5e24 137 struct device *dev = rproc->dev.parent;
400e64df
OBC
138
139 if (!domain)
140 return;
141
142 iommu_detach_device(domain, dev);
143 iommu_domain_free(domain);
144
145 return;
146}
147
148/*
149 * Some remote processors will ask us to allocate them physically contiguous
150 * memory regions (which we call "carveouts"), and map them to specific
151 * device addresses (which are hardcoded in the firmware).
152 *
153 * They may then ask us to copy objects into specific device addresses (e.g.
154 * code/data sections) or expose us certain symbols in other device address
155 * (e.g. their trace buffer).
156 *
157 * This function is an internal helper with which we can go over the allocated
158 * carveouts and translate specific device address to kernel virtual addresses
159 * so we can access the referenced memory.
160 *
161 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
162 * but only on kernel direct mapped RAM memory. Instead, we're just using
163 * here the output of the DMA API, which should be more correct.
164 */
72854fb0 165void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
400e64df
OBC
166{
167 struct rproc_mem_entry *carveout;
168 void *ptr = NULL;
169
170 list_for_each_entry(carveout, &rproc->carveouts, node) {
171 int offset = da - carveout->da;
172
173 /* try next carveout if da is too small */
174 if (offset < 0)
175 continue;
176
177 /* try next carveout if da is too large */
178 if (offset + len > carveout->len)
179 continue;
180
181 ptr = carveout->va + offset;
182
183 break;
184 }
185
186 return ptr;
187}
4afc89d6 188EXPORT_SYMBOL(rproc_da_to_va);
400e64df 189
6db20ea8 190int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
400e64df 191{
7a186941 192 struct rproc *rproc = rvdev->rproc;
b5ab5e24 193 struct device *dev = &rproc->dev;
6db20ea8 194 struct rproc_vring *rvring = &rvdev->vring[i];
7a186941
OBC
195 dma_addr_t dma;
196 void *va;
197 int ret, size, notifyid;
400e64df 198
7a186941 199 /* actual size of vring (in bytes) */
6db20ea8 200 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
7a186941
OBC
201
202 if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
203 dev_err(dev, "idr_pre_get failed\n");
204 return -ENOMEM;
205 }
206
207 /*
208 * Allocate non-cacheable memory for the vring. In the future
209 * this call will also configure the IOMMU for us
6db20ea8 210 * TODO: let the rproc know the da of this vring
7a186941 211 */
b5ab5e24 212 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
7a186941 213 if (!va) {
b5ab5e24 214 dev_err(dev->parent, "dma_alloc_coherent failed\n");
400e64df
OBC
215 return -EINVAL;
216 }
217
6db20ea8
OBC
218 /*
219 * Assign an rproc-wide unique index for this vring
220 * TODO: assign a notifyid for rvdev updates as well
221 * TODO: let the rproc know the notifyid of this vring
222 * TODO: support predefined notifyids (via resource table)
223 */
224 ret = idr_get_new(&rproc->notifyids, rvring, &notifyid);
7a186941
OBC
225 if (ret) {
226 dev_err(dev, "idr_get_new failed: %d\n", ret);
b5ab5e24 227 dma_free_coherent(dev->parent, size, va, dma);
7a186941
OBC
228 return ret;
229 }
400e64df 230
7a186941
OBC
231 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
232 dma, size, notifyid);
233
6db20ea8
OBC
234 rvring->va = va;
235 rvring->dma = dma;
236 rvring->notifyid = notifyid;
400e64df
OBC
237
238 return 0;
239}
240
6db20ea8
OBC
241static int
242rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
7a186941
OBC
243{
244 struct rproc *rproc = rvdev->rproc;
b5ab5e24 245 struct device *dev = &rproc->dev;
6db20ea8
OBC
246 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
247 struct rproc_vring *rvring = &rvdev->vring[i];
7a186941 248
6db20ea8
OBC
249 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
250 i, vring->da, vring->num, vring->align);
7a186941 251
6db20ea8
OBC
252 /* make sure reserved bytes are zeroes */
253 if (vring->reserved) {
254 dev_err(dev, "vring rsc has non zero reserved bytes\n");
255 return -EINVAL;
256 }
7a186941 257
6db20ea8
OBC
258 /* verify queue size and vring alignment are sane */
259 if (!vring->num || !vring->align) {
260 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
261 vring->num, vring->align);
262 return -EINVAL;
7a186941 263 }
6db20ea8
OBC
264
265 rvring->len = vring->num;
266 rvring->align = vring->align;
267 rvring->rvdev = rvdev;
268
269 return 0;
270}
271
272void rproc_free_vring(struct rproc_vring *rvring)
273{
274 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
275 struct rproc *rproc = rvring->rvdev->rproc;
276
b5ab5e24 277 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
6db20ea8 278 idr_remove(&rproc->notifyids, rvring->notifyid);
7a186941
OBC
279}
280
400e64df 281/**
fd2c15ec 282 * rproc_handle_vdev() - handle a vdev fw resource
400e64df
OBC
283 * @rproc: the remote processor
284 * @rsc: the vring resource descriptor
fd2c15ec 285 * @avail: size of available data (for sanity checking the image)
400e64df 286 *
7a186941
OBC
287 * This resource entry requests the host to statically register a virtio
288 * device (vdev), and setup everything needed to support it. It contains
289 * everything needed to make it possible: the virtio device id, virtio
290 * device features, vrings information, virtio config space, etc...
291 *
292 * Before registering the vdev, the vrings are allocated from non-cacheable
293 * physically contiguous memory. Currently we only support two vrings per
294 * remote processor (temporary limitation). We might also want to consider
295 * doing the vring allocation only later when ->find_vqs() is invoked, and
296 * then release them upon ->del_vqs().
297 *
298 * Note: @da is currently not really handled correctly: we dynamically
299 * allocate it using the DMA API, ignoring requested hard coded addresses,
300 * and we don't take care of any required IOMMU programming. This is all
301 * going to be taken care of when the generic iommu-based DMA API will be
302 * merged. Meanwhile, statically-addressed iommu-based firmware images should
303 * use RSC_DEVMEM resource entries to map their required @da to the physical
304 * address of their base CMA region (ouch, hacky!).
400e64df
OBC
305 *
306 * Returns 0 on success, or an appropriate error code otherwise
307 */
fd2c15ec
OBC
308static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
309 int avail)
400e64df 310{
b5ab5e24 311 struct device *dev = &rproc->dev;
7a186941
OBC
312 struct rproc_vdev *rvdev;
313 int i, ret;
400e64df 314
fd2c15ec
OBC
315 /* make sure resource isn't truncated */
316 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
317 + rsc->config_len > avail) {
b5ab5e24 318 dev_err(dev, "vdev rsc is truncated\n");
400e64df
OBC
319 return -EINVAL;
320 }
321
fd2c15ec
OBC
322 /* make sure reserved bytes are zeroes */
323 if (rsc->reserved[0] || rsc->reserved[1]) {
324 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
400e64df
OBC
325 return -EINVAL;
326 }
327
fd2c15ec
OBC
328 dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
329 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
330
7a186941
OBC
331 /* we currently support only two vrings per rvdev */
332 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
fd2c15ec 333 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
400e64df
OBC
334 return -EINVAL;
335 }
336
7a186941
OBC
337 rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
338 if (!rvdev)
339 return -ENOMEM;
400e64df 340
7a186941 341 rvdev->rproc = rproc;
400e64df 342
6db20ea8 343 /* parse the vrings */
7a186941 344 for (i = 0; i < rsc->num_of_vrings; i++) {
6db20ea8 345 ret = rproc_parse_vring(rvdev, rsc, i);
7a186941 346 if (ret)
6db20ea8 347 goto free_rvdev;
7a186941 348 }
400e64df 349
7a186941
OBC
350 /* remember the device features */
351 rvdev->dfeatures = rsc->dfeatures;
fd2c15ec 352
7a186941 353 list_add_tail(&rvdev->node, &rproc->rvdevs);
fd2c15ec 354
7a186941
OBC
355 /* it is now safe to add the virtio device */
356 ret = rproc_add_virtio_dev(rvdev, rsc->id);
357 if (ret)
6db20ea8 358 goto free_rvdev;
400e64df
OBC
359
360 return 0;
7a186941 361
6db20ea8 362free_rvdev:
7a186941
OBC
363 kfree(rvdev);
364 return ret;
400e64df
OBC
365}
366
367/**
368 * rproc_handle_trace() - handle a shared trace buffer resource
369 * @rproc: the remote processor
370 * @rsc: the trace resource descriptor
fd2c15ec 371 * @avail: size of available data (for sanity checking the image)
400e64df
OBC
372 *
373 * In case the remote processor dumps trace logs into memory,
374 * export it via debugfs.
375 *
376 * Currently, the 'da' member of @rsc should contain the device address
377 * where the remote processor is dumping the traces. Later we could also
378 * support dynamically allocating this address using the generic
379 * DMA API (but currently there isn't a use case for that).
380 *
381 * Returns 0 on success, or an appropriate error code otherwise
382 */
fd2c15ec
OBC
383static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
384 int avail)
400e64df
OBC
385{
386 struct rproc_mem_entry *trace;
b5ab5e24 387 struct device *dev = &rproc->dev;
400e64df
OBC
388 void *ptr;
389 char name[15];
390
fd2c15ec 391 if (sizeof(*rsc) > avail) {
b5ab5e24 392 dev_err(dev, "trace rsc is truncated\n");
fd2c15ec
OBC
393 return -EINVAL;
394 }
395
396 /* make sure reserved bytes are zeroes */
397 if (rsc->reserved) {
398 dev_err(dev, "trace rsc has non zero reserved bytes\n");
399 return -EINVAL;
400 }
401
400e64df
OBC
402 /* what's the kernel address of this resource ? */
403 ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
404 if (!ptr) {
405 dev_err(dev, "erroneous trace resource entry\n");
406 return -EINVAL;
407 }
408
409 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
410 if (!trace) {
411 dev_err(dev, "kzalloc trace failed\n");
412 return -ENOMEM;
413 }
414
415 /* set the trace buffer dma properties */
416 trace->len = rsc->len;
417 trace->va = ptr;
418
419 /* make sure snprintf always null terminates, even if truncating */
420 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
421
422 /* create the debugfs entry */
423 trace->priv = rproc_create_trace_file(name, rproc, trace);
424 if (!trace->priv) {
425 trace->va = NULL;
426 kfree(trace);
427 return -EINVAL;
428 }
429
430 list_add_tail(&trace->node, &rproc->traces);
431
432 rproc->num_traces++;
433
fd2c15ec 434 dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
400e64df
OBC
435 rsc->da, rsc->len);
436
437 return 0;
438}
439
440/**
441 * rproc_handle_devmem() - handle devmem resource entry
442 * @rproc: remote processor handle
443 * @rsc: the devmem resource entry
fd2c15ec 444 * @avail: size of available data (for sanity checking the image)
400e64df
OBC
445 *
446 * Remote processors commonly need to access certain on-chip peripherals.
447 *
448 * Some of these remote processors access memory via an iommu device,
449 * and might require us to configure their iommu before they can access
450 * the on-chip peripherals they need.
451 *
452 * This resource entry is a request to map such a peripheral device.
453 *
454 * These devmem entries will contain the physical address of the device in
455 * the 'pa' member. If a specific device address is expected, then 'da' will
456 * contain it (currently this is the only use case supported). 'len' will
457 * contain the size of the physical region we need to map.
458 *
459 * Currently we just "trust" those devmem entries to contain valid physical
460 * addresses, but this is going to change: we want the implementations to
461 * tell us ranges of physical addresses the firmware is allowed to request,
462 * and not allow firmwares to request access to physical addresses that
463 * are outside those ranges.
464 */
fd2c15ec
OBC
465static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
466 int avail)
400e64df
OBC
467{
468 struct rproc_mem_entry *mapping;
b5ab5e24 469 struct device *dev = &rproc->dev;
400e64df
OBC
470 int ret;
471
472 /* no point in handling this resource without a valid iommu domain */
473 if (!rproc->domain)
474 return -EINVAL;
475
fd2c15ec 476 if (sizeof(*rsc) > avail) {
b5ab5e24 477 dev_err(dev, "devmem rsc is truncated\n");
fd2c15ec
OBC
478 return -EINVAL;
479 }
480
481 /* make sure reserved bytes are zeroes */
482 if (rsc->reserved) {
b5ab5e24 483 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
fd2c15ec
OBC
484 return -EINVAL;
485 }
486
400e64df
OBC
487 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
488 if (!mapping) {
b5ab5e24 489 dev_err(dev, "kzalloc mapping failed\n");
400e64df
OBC
490 return -ENOMEM;
491 }
492
493 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
494 if (ret) {
b5ab5e24 495 dev_err(dev, "failed to map devmem: %d\n", ret);
400e64df
OBC
496 goto out;
497 }
498
499 /*
500 * We'll need this info later when we'll want to unmap everything
501 * (e.g. on shutdown).
502 *
503 * We can't trust the remote processor not to change the resource
504 * table, so we must maintain this info independently.
505 */
506 mapping->da = rsc->da;
507 mapping->len = rsc->len;
508 list_add_tail(&mapping->node, &rproc->mappings);
509
b5ab5e24 510 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
400e64df
OBC
511 rsc->pa, rsc->da, rsc->len);
512
513 return 0;
514
515out:
516 kfree(mapping);
517 return ret;
518}
519
520/**
521 * rproc_handle_carveout() - handle phys contig memory allocation requests
522 * @rproc: rproc handle
523 * @rsc: the resource entry
fd2c15ec 524 * @avail: size of available data (for image validation)
400e64df
OBC
525 *
526 * This function will handle firmware requests for allocation of physically
527 * contiguous memory regions.
528 *
529 * These request entries should come first in the firmware's resource table,
530 * as other firmware entries might request placing other data objects inside
531 * these memory regions (e.g. data/code segments, trace resource entries, ...).
532 *
533 * Allocating memory this way helps utilizing the reserved physical memory
534 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
535 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
536 * pressure is important; it may have a substantial impact on performance.
537 */
fd2c15ec
OBC
538static int rproc_handle_carveout(struct rproc *rproc,
539 struct fw_rsc_carveout *rsc, int avail)
400e64df
OBC
540{
541 struct rproc_mem_entry *carveout, *mapping;
b5ab5e24 542 struct device *dev = &rproc->dev;
400e64df
OBC
543 dma_addr_t dma;
544 void *va;
545 int ret;
546
fd2c15ec 547 if (sizeof(*rsc) > avail) {
b5ab5e24 548 dev_err(dev, "carveout rsc is truncated\n");
fd2c15ec
OBC
549 return -EINVAL;
550 }
551
552 /* make sure reserved bytes are zeroes */
553 if (rsc->reserved) {
554 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
555 return -EINVAL;
556 }
557
558 dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
559 rsc->da, rsc->pa, rsc->len, rsc->flags);
560
400e64df
OBC
561 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
562 if (!mapping) {
563 dev_err(dev, "kzalloc mapping failed\n");
564 return -ENOMEM;
565 }
566
567 carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
568 if (!carveout) {
569 dev_err(dev, "kzalloc carveout failed\n");
570 ret = -ENOMEM;
571 goto free_mapping;
572 }
573
b5ab5e24 574 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
400e64df 575 if (!va) {
b5ab5e24 576 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
400e64df
OBC
577 ret = -ENOMEM;
578 goto free_carv;
579 }
580
581 dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
582
583 /*
584 * Ok, this is non-standard.
585 *
586 * Sometimes we can't rely on the generic iommu-based DMA API
587 * to dynamically allocate the device address and then set the IOMMU
588 * tables accordingly, because some remote processors might
589 * _require_ us to use hard coded device addresses that their
590 * firmware was compiled with.
591 *
592 * In this case, we must use the IOMMU API directly and map
593 * the memory to the device address as expected by the remote
594 * processor.
595 *
596 * Obviously such remote processor devices should not be configured
597 * to use the iommu-based DMA API: we expect 'dma' to contain the
598 * physical address in this case.
599 */
600 if (rproc->domain) {
601 ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
602 rsc->flags);
603 if (ret) {
604 dev_err(dev, "iommu_map failed: %d\n", ret);
605 goto dma_free;
606 }
607
608 /*
609 * We'll need this info later when we'll want to unmap
610 * everything (e.g. on shutdown).
611 *
612 * We can't trust the remote processor not to change the
613 * resource table, so we must maintain this info independently.
614 */
615 mapping->da = rsc->da;
616 mapping->len = rsc->len;
617 list_add_tail(&mapping->node, &rproc->mappings);
618
fd2c15ec 619 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
400e64df
OBC
620 }
621
0e49b72c
OBC
622 /*
623 * Some remote processors might need to know the pa
624 * even though they are behind an IOMMU. E.g., OMAP4's
625 * remote M3 processor needs this so it can control
626 * on-chip hardware accelerators that are not behind
627 * the IOMMU, and therefor must know the pa.
628 *
629 * Generally we don't want to expose physical addresses
630 * if we don't have to (remote processors are generally
631 * _not_ trusted), so we might want to do this only for
632 * remote processor that _must_ have this (e.g. OMAP4's
633 * dual M3 subsystem).
634 *
635 * Non-IOMMU processors might also want to have this info.
636 * In this case, the device address and the physical address
637 * are the same.
638 */
639 rsc->pa = dma;
640
400e64df
OBC
641 carveout->va = va;
642 carveout->len = rsc->len;
643 carveout->dma = dma;
644 carveout->da = rsc->da;
645
646 list_add_tail(&carveout->node, &rproc->carveouts);
647
648 return 0;
649
650dma_free:
b5ab5e24 651 dma_free_coherent(dev->parent, rsc->len, va, dma);
400e64df
OBC
652free_carv:
653 kfree(carveout);
654free_mapping:
655 kfree(mapping);
656 return ret;
657}
658
e12bc14b
OBC
659/*
660 * A lookup table for resource handlers. The indices are defined in
661 * enum fw_resource_type.
662 */
663static rproc_handle_resource_t rproc_handle_rsc[] = {
fd2c15ec
OBC
664 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
665 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
666 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
7a186941 667 [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
e12bc14b
OBC
668};
669
400e64df
OBC
670/* handle firmware resource entries before booting the remote processor */
671static int
fd2c15ec 672rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
400e64df 673{
b5ab5e24 674 struct device *dev = &rproc->dev;
e12bc14b 675 rproc_handle_resource_t handler;
fd2c15ec
OBC
676 int ret = 0, i;
677
678 for (i = 0; i < table->num; i++) {
679 int offset = table->offset[i];
680 struct fw_rsc_hdr *hdr = (void *)table + offset;
681 int avail = len - offset - sizeof(*hdr);
682 void *rsc = (void *)hdr + sizeof(*hdr);
683
684 /* make sure table isn't truncated */
685 if (avail < 0) {
686 dev_err(dev, "rsc table is truncated\n");
687 return -EINVAL;
688 }
400e64df 689
fd2c15ec 690 dev_dbg(dev, "rsc: type %d\n", hdr->type);
400e64df 691
fd2c15ec
OBC
692 if (hdr->type >= RSC_LAST) {
693 dev_warn(dev, "unsupported resource %d\n", hdr->type);
e12bc14b 694 continue;
400e64df
OBC
695 }
696
fd2c15ec 697 handler = rproc_handle_rsc[hdr->type];
e12bc14b
OBC
698 if (!handler)
699 continue;
700
fd2c15ec 701 ret = handler(rproc, rsc, avail);
400e64df
OBC
702 if (ret)
703 break;
400e64df
OBC
704 }
705
706 return ret;
707}
708
709/* handle firmware resource entries while registering the remote processor */
710static int
fd2c15ec 711rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
400e64df 712{
b5ab5e24 713 struct device *dev = &rproc->dev;
fd2c15ec
OBC
714 int ret = 0, i;
715
716 for (i = 0; i < table->num; i++) {
717 int offset = table->offset[i];
718 struct fw_rsc_hdr *hdr = (void *)table + offset;
719 int avail = len - offset - sizeof(*hdr);
7a186941 720 struct fw_rsc_vdev *vrsc;
400e64df 721
fd2c15ec
OBC
722 /* make sure table isn't truncated */
723 if (avail < 0) {
724 dev_err(dev, "rsc table is truncated\n");
725 return -EINVAL;
726 }
727
728 dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
729
7a186941
OBC
730 if (hdr->type != RSC_VDEV)
731 continue;
732
733 vrsc = (struct fw_rsc_vdev *)hdr->data;
734
735 ret = rproc_handle_vdev(rproc, vrsc, avail);
736 if (ret)
400e64df 737 break;
fd2c15ec 738 }
400e64df
OBC
739
740 return ret;
741}
742
400e64df
OBC
743/**
744 * rproc_resource_cleanup() - clean up and free all acquired resources
745 * @rproc: rproc handle
746 *
747 * This function will free all resources acquired for @rproc, and it
7a186941 748 * is called whenever @rproc either shuts down or fails to boot.
400e64df
OBC
749 */
750static void rproc_resource_cleanup(struct rproc *rproc)
751{
752 struct rproc_mem_entry *entry, *tmp;
b5ab5e24 753 struct device *dev = &rproc->dev;
400e64df
OBC
754
755 /* clean up debugfs trace entries */
756 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
757 rproc_remove_trace_file(entry->priv);
758 rproc->num_traces--;
759 list_del(&entry->node);
760 kfree(entry);
761 }
762
400e64df
OBC
763 /* clean up carveout allocations */
764 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
b5ab5e24 765 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
400e64df
OBC
766 list_del(&entry->node);
767 kfree(entry);
768 }
769
770 /* clean up iommu mapping entries */
771 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
772 size_t unmapped;
773
774 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
775 if (unmapped != entry->len) {
776 /* nothing much to do besides complaining */
e981f6d4 777 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
400e64df
OBC
778 unmapped);
779 }
780
781 list_del(&entry->node);
782 kfree(entry);
783 }
784}
785
400e64df
OBC
786/*
787 * take a firmware and boot a remote processor with it.
788 */
789static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
790{
b5ab5e24 791 struct device *dev = &rproc->dev;
400e64df 792 const char *name = rproc->firmware;
1e3e2c7c
OBC
793 struct resource_table *table;
794 int ret, tablesz;
400e64df
OBC
795
796 ret = rproc_fw_sanity_check(rproc, fw);
797 if (ret)
798 return ret;
799
e981f6d4 800 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
400e64df
OBC
801
802 /*
803 * if enabling an IOMMU isn't relevant for this rproc, this is
804 * just a nop
805 */
806 ret = rproc_enable_iommu(rproc);
807 if (ret) {
808 dev_err(dev, "can't enable iommu: %d\n", ret);
809 return ret;
810 }
811
3e5f9eb5 812 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
400e64df 813
1e3e2c7c 814 /* look for the resource table */
bd484984 815 table = rproc_find_rsc_table(rproc, fw, &tablesz);
30338cf0
SB
816 if (!table) {
817 ret = -EINVAL;
1e3e2c7c 818 goto clean_up;
30338cf0 819 }
1e3e2c7c 820
400e64df 821 /* handle fw resources which are required to boot rproc */
1e3e2c7c 822 ret = rproc_handle_boot_rsc(rproc, table, tablesz);
400e64df
OBC
823 if (ret) {
824 dev_err(dev, "Failed to process resources: %d\n", ret);
825 goto clean_up;
826 }
827
828 /* load the ELF segments to memory */
bd484984 829 ret = rproc_load_segments(rproc, fw);
400e64df
OBC
830 if (ret) {
831 dev_err(dev, "Failed to load program segments: %d\n", ret);
832 goto clean_up;
833 }
834
835 /* power up the remote processor */
836 ret = rproc->ops->start(rproc);
837 if (ret) {
838 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
839 goto clean_up;
840 }
841
842 rproc->state = RPROC_RUNNING;
843
844 dev_info(dev, "remote processor %s is now up\n", rproc->name);
845
846 return 0;
847
848clean_up:
849 rproc_resource_cleanup(rproc);
850 rproc_disable_iommu(rproc);
851 return ret;
852}
853
854/*
855 * take a firmware and look for virtio devices to register.
856 *
857 * Note: this function is called asynchronously upon registration of the
858 * remote processor (so we must wait until it completes before we try
859 * to unregister the device. one other option is just to use kref here,
860 * that might be cleaner).
861 */
862static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
863{
864 struct rproc *rproc = context;
1e3e2c7c
OBC
865 struct resource_table *table;
866 int ret, tablesz;
400e64df
OBC
867
868 if (rproc_fw_sanity_check(rproc, fw) < 0)
869 goto out;
870
1e3e2c7c 871 /* look for the resource table */
bd484984 872 table = rproc_find_rsc_table(rproc, fw, &tablesz);
1e3e2c7c
OBC
873 if (!table)
874 goto out;
875
876 /* look for virtio devices and register them */
877 ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
878 if (ret)
400e64df 879 goto out;
400e64df 880
400e64df 881out:
3cc6e787 882 release_firmware(fw);
160e7c84 883 /* allow rproc_del() contexts, if any, to proceed */
400e64df
OBC
884 complete_all(&rproc->firmware_loading_complete);
885}
886
70b85ef8
FGL
887static int rproc_add_virtio_devices(struct rproc *rproc)
888{
889 int ret;
890
891 /* rproc_del() calls must wait until async loader completes */
892 init_completion(&rproc->firmware_loading_complete);
893
894 /*
895 * We must retrieve early virtio configuration info from
896 * the firmware (e.g. whether to register a virtio device,
897 * what virtio features does it support, ...).
898 *
899 * We're initiating an asynchronous firmware loading, so we can
900 * be built-in kernel code, without hanging the boot process.
901 */
902 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
903 rproc->firmware, &rproc->dev, GFP_KERNEL,
904 rproc, rproc_fw_config_virtio);
905 if (ret < 0) {
906 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
907 complete_all(&rproc->firmware_loading_complete);
908 }
909
910 return ret;
911}
912
913/**
914 * rproc_trigger_recovery() - recover a remoteproc
915 * @rproc: the remote processor
916 *
917 * The recovery is done by reseting all the virtio devices, that way all the
918 * rpmsg drivers will be reseted along with the remote processor making the
919 * remoteproc functional again.
920 *
921 * This function can sleep, so it cannot be called from atomic context.
922 */
923int rproc_trigger_recovery(struct rproc *rproc)
924{
925 struct rproc_vdev *rvdev, *rvtmp;
926
927 dev_err(&rproc->dev, "recovering %s\n", rproc->name);
928
929 init_completion(&rproc->crash_comp);
930
931 /* clean up remote vdev entries */
932 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
933 rproc_remove_virtio_dev(rvdev);
934
935 /* wait until there is no more rproc users */
936 wait_for_completion(&rproc->crash_comp);
937
938 return rproc_add_virtio_devices(rproc);
939}
940
8afd519c
FGL
941/**
942 * rproc_crash_handler_work() - handle a crash
943 *
944 * This function needs to handle everything related to a crash, like cpu
945 * registers and stack dump, information to help to debug the fatal error, etc.
946 */
947static void rproc_crash_handler_work(struct work_struct *work)
948{
949 struct rproc *rproc = container_of(work, struct rproc, crash_handler);
950 struct device *dev = &rproc->dev;
951
952 dev_dbg(dev, "enter %s\n", __func__);
953
954 mutex_lock(&rproc->lock);
955
956 if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
957 /* handle only the first crash detected */
958 mutex_unlock(&rproc->lock);
959 return;
960 }
961
962 rproc->state = RPROC_CRASHED;
963 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
964 rproc->name);
965
966 mutex_unlock(&rproc->lock);
967
2e37abb8
FGL
968 if (!rproc->recovery_disabled)
969 rproc_trigger_recovery(rproc);
8afd519c
FGL
970}
971
400e64df
OBC
972/**
973 * rproc_boot() - boot a remote processor
974 * @rproc: handle of a remote processor
975 *
976 * Boot a remote processor (i.e. load its firmware, power it on, ...).
977 *
978 * If the remote processor is already powered on, this function immediately
979 * returns (successfully).
980 *
981 * Returns 0 on success, and an appropriate error value otherwise.
982 */
983int rproc_boot(struct rproc *rproc)
984{
985 const struct firmware *firmware_p;
986 struct device *dev;
987 int ret;
988
989 if (!rproc) {
990 pr_err("invalid rproc handle\n");
991 return -EINVAL;
992 }
993
b5ab5e24 994 dev = &rproc->dev;
400e64df
OBC
995
996 ret = mutex_lock_interruptible(&rproc->lock);
997 if (ret) {
998 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
999 return ret;
1000 }
1001
1002 /* loading a firmware is required */
1003 if (!rproc->firmware) {
1004 dev_err(dev, "%s: no firmware to load\n", __func__);
1005 ret = -EINVAL;
1006 goto unlock_mutex;
1007 }
1008
1009 /* prevent underlying implementation from being removed */
b5ab5e24 1010 if (!try_module_get(dev->parent->driver->owner)) {
400e64df
OBC
1011 dev_err(dev, "%s: can't get owner\n", __func__);
1012 ret = -EINVAL;
1013 goto unlock_mutex;
1014 }
1015
1016 /* skip the boot process if rproc is already powered up */
1017 if (atomic_inc_return(&rproc->power) > 1) {
1018 ret = 0;
1019 goto unlock_mutex;
1020 }
1021
1022 dev_info(dev, "powering up %s\n", rproc->name);
1023
1024 /* load firmware */
1025 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1026 if (ret < 0) {
1027 dev_err(dev, "request_firmware failed: %d\n", ret);
1028 goto downref_rproc;
1029 }
1030
1031 ret = rproc_fw_boot(rproc, firmware_p);
1032
1033 release_firmware(firmware_p);
1034
1035downref_rproc:
1036 if (ret) {
b5ab5e24 1037 module_put(dev->parent->driver->owner);
400e64df
OBC
1038 atomic_dec(&rproc->power);
1039 }
1040unlock_mutex:
1041 mutex_unlock(&rproc->lock);
1042 return ret;
1043}
1044EXPORT_SYMBOL(rproc_boot);
1045
1046/**
1047 * rproc_shutdown() - power off the remote processor
1048 * @rproc: the remote processor
1049 *
1050 * Power off a remote processor (previously booted with rproc_boot()).
1051 *
1052 * In case @rproc is still being used by an additional user(s), then
1053 * this function will just decrement the power refcount and exit,
1054 * without really powering off the device.
1055 *
1056 * Every call to rproc_boot() must (eventually) be accompanied by a call
1057 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1058 *
1059 * Notes:
1060 * - we're not decrementing the rproc's refcount, only the power refcount.
1061 * which means that the @rproc handle stays valid even after rproc_shutdown()
1062 * returns, and users can still use it with a subsequent rproc_boot(), if
1063 * needed.
400e64df
OBC
1064 */
1065void rproc_shutdown(struct rproc *rproc)
1066{
b5ab5e24 1067 struct device *dev = &rproc->dev;
400e64df
OBC
1068 int ret;
1069
1070 ret = mutex_lock_interruptible(&rproc->lock);
1071 if (ret) {
1072 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1073 return;
1074 }
1075
1076 /* if the remote proc is still needed, bail out */
1077 if (!atomic_dec_and_test(&rproc->power))
1078 goto out;
1079
1080 /* power off the remote processor */
1081 ret = rproc->ops->stop(rproc);
1082 if (ret) {
1083 atomic_inc(&rproc->power);
1084 dev_err(dev, "can't stop rproc: %d\n", ret);
1085 goto out;
1086 }
1087
1088 /* clean up all acquired resources */
1089 rproc_resource_cleanup(rproc);
1090
1091 rproc_disable_iommu(rproc);
1092
70b85ef8
FGL
1093 /* if in crash state, unlock crash handler */
1094 if (rproc->state == RPROC_CRASHED)
1095 complete_all(&rproc->crash_comp);
1096
400e64df
OBC
1097 rproc->state = RPROC_OFFLINE;
1098
1099 dev_info(dev, "stopped remote processor %s\n", rproc->name);
1100
1101out:
1102 mutex_unlock(&rproc->lock);
1103 if (!ret)
b5ab5e24 1104 module_put(dev->parent->driver->owner);
400e64df
OBC
1105}
1106EXPORT_SYMBOL(rproc_shutdown);
1107
1108/**
160e7c84 1109 * rproc_add() - register a remote processor
400e64df
OBC
1110 * @rproc: the remote processor handle to register
1111 *
1112 * Registers @rproc with the remoteproc framework, after it has been
1113 * allocated with rproc_alloc().
1114 *
1115 * This is called by the platform-specific rproc implementation, whenever
1116 * a new remote processor device is probed.
1117 *
1118 * Returns 0 on success and an appropriate error code otherwise.
1119 *
1120 * Note: this function initiates an asynchronous firmware loading
1121 * context, which will look for virtio devices supported by the rproc's
1122 * firmware.
1123 *
1124 * If found, those virtio devices will be created and added, so as a result
7a186941 1125 * of registering this remote processor, additional virtio drivers might be
400e64df 1126 * probed.
400e64df 1127 */
160e7c84 1128int rproc_add(struct rproc *rproc)
400e64df 1129{
b5ab5e24 1130 struct device *dev = &rproc->dev;
70b85ef8 1131 int ret;
400e64df 1132
b5ab5e24
OBC
1133 ret = device_add(dev);
1134 if (ret < 0)
1135 return ret;
400e64df 1136
b5ab5e24 1137 dev_info(dev, "%s is available\n", rproc->name);
400e64df 1138
489d129a
OBC
1139 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1140 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1141
400e64df
OBC
1142 /* create debugfs entries */
1143 rproc_create_debug_dir(rproc);
1144
70b85ef8 1145 return rproc_add_virtio_devices(rproc);
400e64df 1146}
160e7c84 1147EXPORT_SYMBOL(rproc_add);
400e64df 1148
b5ab5e24
OBC
1149/**
1150 * rproc_type_release() - release a remote processor instance
1151 * @dev: the rproc's device
1152 *
1153 * This function should _never_ be called directly.
1154 *
1155 * It will be called by the driver core when no one holds a valid pointer
1156 * to @dev anymore.
1157 */
1158static void rproc_type_release(struct device *dev)
1159{
1160 struct rproc *rproc = container_of(dev, struct rproc, dev);
1161
7183a2a7
OBC
1162 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1163
1164 rproc_delete_debug_dir(rproc);
1165
b5ab5e24
OBC
1166 idr_remove_all(&rproc->notifyids);
1167 idr_destroy(&rproc->notifyids);
1168
1169 if (rproc->index >= 0)
1170 ida_simple_remove(&rproc_dev_index, rproc->index);
1171
1172 kfree(rproc);
1173}
1174
1175static struct device_type rproc_type = {
1176 .name = "remoteproc",
1177 .release = rproc_type_release,
1178};
400e64df
OBC
1179
1180/**
1181 * rproc_alloc() - allocate a remote processor handle
1182 * @dev: the underlying device
1183 * @name: name of this remote processor
1184 * @ops: platform-specific handlers (mainly start/stop)
1185 * @firmware: name of firmware file to load
1186 * @len: length of private data needed by the rproc driver (in bytes)
1187 *
1188 * Allocates a new remote processor handle, but does not register
1189 * it yet.
1190 *
1191 * This function should be used by rproc implementations during initialization
1192 * of the remote processor.
1193 *
1194 * After creating an rproc handle using this function, and when ready,
160e7c84 1195 * implementations should then call rproc_add() to complete
400e64df
OBC
1196 * the registration of the remote processor.
1197 *
1198 * On success the new rproc is returned, and on failure, NULL.
1199 *
1200 * Note: _never_ directly deallocate @rproc, even if it was not registered
160e7c84 1201 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
400e64df
OBC
1202 */
1203struct rproc *rproc_alloc(struct device *dev, const char *name,
1204 const struct rproc_ops *ops,
1205 const char *firmware, int len)
1206{
1207 struct rproc *rproc;
1208
1209 if (!dev || !name || !ops)
1210 return NULL;
1211
1212 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1213 if (!rproc) {
1214 dev_err(dev, "%s: kzalloc failed\n", __func__);
1215 return NULL;
1216 }
1217
400e64df
OBC
1218 rproc->name = name;
1219 rproc->ops = ops;
1220 rproc->firmware = firmware;
1221 rproc->priv = &rproc[1];
1222
b5ab5e24
OBC
1223 device_initialize(&rproc->dev);
1224 rproc->dev.parent = dev;
1225 rproc->dev.type = &rproc_type;
1226
1227 /* Assign a unique device index and name */
1228 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1229 if (rproc->index < 0) {
1230 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1231 put_device(&rproc->dev);
1232 return NULL;
1233 }
1234
1235 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1236
400e64df
OBC
1237 atomic_set(&rproc->power, 0);
1238
4afc89d6
SB
1239 /* Set ELF as the default fw_ops handler */
1240 rproc->fw_ops = &rproc_elf_fw_ops;
400e64df
OBC
1241
1242 mutex_init(&rproc->lock);
1243
7a186941
OBC
1244 idr_init(&rproc->notifyids);
1245
400e64df
OBC
1246 INIT_LIST_HEAD(&rproc->carveouts);
1247 INIT_LIST_HEAD(&rproc->mappings);
1248 INIT_LIST_HEAD(&rproc->traces);
7a186941 1249 INIT_LIST_HEAD(&rproc->rvdevs);
400e64df 1250
8afd519c 1251 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
70b85ef8 1252 init_completion(&rproc->crash_comp);
8afd519c 1253
400e64df
OBC
1254 rproc->state = RPROC_OFFLINE;
1255
1256 return rproc;
1257}
1258EXPORT_SYMBOL(rproc_alloc);
1259
1260/**
160e7c84 1261 * rproc_put() - unroll rproc_alloc()
400e64df
OBC
1262 * @rproc: the remote processor handle
1263 *
c6b5a276 1264 * This function decrements the rproc dev refcount.
400e64df 1265 *
c6b5a276
OBC
1266 * If no one holds any reference to rproc anymore, then its refcount would
1267 * now drop to zero, and it would be freed.
400e64df 1268 */
160e7c84 1269void rproc_put(struct rproc *rproc)
400e64df 1270{
b5ab5e24 1271 put_device(&rproc->dev);
400e64df 1272}
160e7c84 1273EXPORT_SYMBOL(rproc_put);
400e64df
OBC
1274
1275/**
160e7c84 1276 * rproc_del() - unregister a remote processor
400e64df
OBC
1277 * @rproc: rproc handle to unregister
1278 *
400e64df
OBC
1279 * This function should be called when the platform specific rproc
1280 * implementation decides to remove the rproc device. it should
160e7c84 1281 * _only_ be called if a previous invocation of rproc_add()
400e64df
OBC
1282 * has completed successfully.
1283 *
160e7c84 1284 * After rproc_del() returns, @rproc isn't freed yet, because
c6b5a276 1285 * of the outstanding reference created by rproc_alloc. To decrement that
160e7c84 1286 * one last refcount, one still needs to call rproc_put().
400e64df
OBC
1287 *
1288 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1289 */
160e7c84 1290int rproc_del(struct rproc *rproc)
400e64df 1291{
6db20ea8 1292 struct rproc_vdev *rvdev, *tmp;
7a186941 1293
400e64df
OBC
1294 if (!rproc)
1295 return -EINVAL;
1296
1297 /* if rproc is just being registered, wait */
1298 wait_for_completion(&rproc->firmware_loading_complete);
1299
7a186941 1300 /* clean up remote vdev entries */
6db20ea8 1301 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
7a186941 1302 rproc_remove_virtio_dev(rvdev);
400e64df 1303
b5ab5e24 1304 device_del(&rproc->dev);
400e64df
OBC
1305
1306 return 0;
1307}
160e7c84 1308EXPORT_SYMBOL(rproc_del);
400e64df 1309
8afd519c
FGL
1310/**
1311 * rproc_report_crash() - rproc crash reporter function
1312 * @rproc: remote processor
1313 * @type: crash type
1314 *
1315 * This function must be called every time a crash is detected by the low-level
1316 * drivers implementing a specific remoteproc. This should not be called from a
1317 * non-remoteproc driver.
1318 *
1319 * This function can be called from atomic/interrupt context.
1320 */
1321void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
1322{
1323 if (!rproc) {
1324 pr_err("NULL rproc pointer\n");
1325 return;
1326 }
1327
1328 dev_err(&rproc->dev, "crash detected in %s: type %s\n",
1329 rproc->name, rproc_crash_to_string(type));
1330
1331 /* create a new task to handle the error */
1332 schedule_work(&rproc->crash_handler);
1333}
1334EXPORT_SYMBOL(rproc_report_crash);
1335
400e64df
OBC
1336static int __init remoteproc_init(void)
1337{
1338 rproc_init_debugfs();
b5ab5e24 1339
400e64df
OBC
1340 return 0;
1341}
1342module_init(remoteproc_init);
1343
1344static void __exit remoteproc_exit(void)
1345{
1346 rproc_exit_debugfs();
1347}
1348module_exit(remoteproc_exit);
1349
1350MODULE_LICENSE("GPL v2");
1351MODULE_DESCRIPTION("Generic Remote Processor Framework");
This page took 0.135894 seconds and 5 git commands to generate.