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