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