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