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