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