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