blob: d5c2dbfc7443c0ca4ced101fc0ab5447b0bf1e8f [file] [log] [blame]
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001/*
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>
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030038#include <linux/idr.h>
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020039#include <linux/elf.h>
40#include <linux/virtio_ids.h>
41#include <linux/virtio_ring.h>
Ohad Ben-Cohencf59d3e2012-01-31 15:23:41 +020042#include <asm/byteorder.h>
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020043
44#include "remoteproc_internal.h"
45
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020046typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +020047 struct resource_table *table, int len);
48typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020049
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030050/* Unique indices for remoteproc devices */
51static DEFINE_IDA(rproc_dev_index);
52
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020053/*
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,
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +030065 unsigned long iova, int flags, void *token)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020066{
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;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030079 struct device *dev = rproc->dev.parent;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020080 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)) {
Mark Grosen0798e1d2011-12-13 08:41:47 +020094 dev_dbg(dev, "iommu not found\n");
95 return 0;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +020096 }
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
Ohad Ben-Cohen77ca2332012-05-21 20:20:05 +0300104 iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200105
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;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300124 struct device *dev = rproc->dev.parent;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200125
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 */
Sjur Brændeland72854fb2012-07-15 11:25:27 +0300152void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200153{
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}
Sjur Brændeland4afc89d2012-06-19 10:08:18 +0300175EXPORT_SYMBOL(rproc_da_to_va);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200176
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300177int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200178{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100179 struct rproc *rproc = rvdev->rproc;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300180 struct device *dev = &rproc->dev;
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300181 struct rproc_vring *rvring = &rvdev->vring[i];
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100182 dma_addr_t dma;
183 void *va;
184 int ret, size, notifyid;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200185
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300186 /* actual size of vring (in bytes) */
187 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
188
189 if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
190 dev_err(dev, "idr_pre_get failed\n");
191 return -ENOMEM;
192 }
193
194 /*
195 * Allocate non-cacheable memory for the vring. In the future
196 * this call will also configure the IOMMU for us
197 * TODO: let the rproc know the da of this vring
198 */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300199 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300200 if (!va) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300201 dev_err(dev->parent, "dma_alloc_coherent failed\n");
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300202 return -EINVAL;
203 }
204
205 /*
206 * Assign an rproc-wide unique index for this vring
207 * TODO: assign a notifyid for rvdev updates as well
208 * TODO: let the rproc know the notifyid of this vring
209 * TODO: support predefined notifyids (via resource table)
210 */
211 ret = idr_get_new(&rproc->notifyids, rvring, &notifyid);
212 if (ret) {
213 dev_err(dev, "idr_get_new failed: %d\n", ret);
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300214 dma_free_coherent(dev->parent, size, va, dma);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300215 return ret;
216 }
217
218 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
219 dma, size, notifyid);
220
221 rvring->va = va;
222 rvring->dma = dma;
223 rvring->notifyid = notifyid;
224
225 return 0;
226}
227
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200228static int
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300229rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200230{
231 struct rproc *rproc = rvdev->rproc;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300232 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200233 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300234 struct rproc_vring *rvring = &rvdev->vring[i];
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200235
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100236 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
237 i, vring->da, vring->num, vring->align);
238
239 /* make sure reserved bytes are zeroes */
240 if (vring->reserved) {
241 dev_err(dev, "vring rsc has non zero reserved bytes\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200242 return -EINVAL;
243 }
244
Ohad Ben-Cohen63140e02012-02-29 14:42:13 +0200245 /* verify queue size and vring alignment are sane */
246 if (!vring->num || !vring->align) {
247 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
248 vring->num, vring->align);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200249 return -EINVAL;
250 }
251
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300252 rvring->len = vring->num;
253 rvring->align = vring->align;
254 rvring->rvdev = rvdev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200255
256 return 0;
257}
258
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300259void rproc_free_vring(struct rproc_vring *rvring)
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100260{
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300261 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
262 struct rproc *rproc = rvring->rvdev->rproc;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100263
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300264 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300265 idr_remove(&rproc->notifyids, rvring->notifyid);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100266}
267
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200268/**
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200269 * rproc_handle_vdev() - handle a vdev fw resource
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200270 * @rproc: the remote processor
271 * @rsc: the vring resource descriptor
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200272 * @avail: size of available data (for sanity checking the image)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200273 *
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100274 * This resource entry requests the host to statically register a virtio
275 * device (vdev), and setup everything needed to support it. It contains
276 * everything needed to make it possible: the virtio device id, virtio
277 * device features, vrings information, virtio config space, etc...
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200278 *
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100279 * Before registering the vdev, the vrings are allocated from non-cacheable
280 * physically contiguous memory. Currently we only support two vrings per
281 * remote processor (temporary limitation). We might also want to consider
282 * doing the vring allocation only later when ->find_vqs() is invoked, and
283 * then release them upon ->del_vqs().
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200284 *
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100285 * Note: @da is currently not really handled correctly: we dynamically
286 * allocate it using the DMA API, ignoring requested hard coded addresses,
287 * and we don't take care of any required IOMMU programming. This is all
288 * going to be taken care of when the generic iommu-based DMA API will be
289 * merged. Meanwhile, statically-addressed iommu-based firmware images should
290 * use RSC_DEVMEM resource entries to map their required @da to the physical
291 * address of their base CMA region (ouch, hacky!).
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200292 *
293 * Returns 0 on success, or an appropriate error code otherwise
294 */
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200295static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
296 int avail)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200297{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300298 struct device *dev = &rproc->dev;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100299 struct rproc_vdev *rvdev;
300 int i, ret;
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200301
302 /* make sure resource isn't truncated */
303 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
304 + rsc->config_len > avail) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300305 dev_err(dev, "vdev rsc is truncated\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200306 return -EINVAL;
307 }
308
309 /* make sure reserved bytes are zeroes */
310 if (rsc->reserved[0] || rsc->reserved[1]) {
311 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
312 return -EINVAL;
313 }
314
315 dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
316 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200317
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100318 /* we currently support only two vrings per rvdev */
319 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200320 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200321 return -EINVAL;
322 }
323
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100324 rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
325 if (!rvdev)
326 return -ENOMEM;
327
328 rvdev->rproc = rproc;
329
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300330 /* parse the vrings */
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200331 for (i = 0; i < rsc->num_of_vrings; i++) {
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300332 ret = rproc_parse_vring(rvdev, rsc, i);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100333 if (ret)
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300334 goto free_rvdev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200335 }
336
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100337 /* remember the device features */
338 rvdev->dfeatures = rsc->dfeatures;
339
340 list_add_tail(&rvdev->node, &rproc->rvdevs);
341
342 /* it is now safe to add the virtio device */
343 ret = rproc_add_virtio_dev(rvdev, rsc->id);
344 if (ret)
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300345 goto free_rvdev;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100346
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200347 return 0;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100348
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300349free_rvdev:
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100350 kfree(rvdev);
351 return ret;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200352}
353
354/**
355 * rproc_handle_trace() - handle a shared trace buffer resource
356 * @rproc: the remote processor
357 * @rsc: the trace resource descriptor
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200358 * @avail: size of available data (for sanity checking the image)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200359 *
360 * In case the remote processor dumps trace logs into memory,
361 * export it via debugfs.
362 *
363 * Currently, the 'da' member of @rsc should contain the device address
364 * where the remote processor is dumping the traces. Later we could also
365 * support dynamically allocating this address using the generic
366 * DMA API (but currently there isn't a use case for that).
367 *
368 * Returns 0 on success, or an appropriate error code otherwise
369 */
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200370static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
371 int avail)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200372{
373 struct rproc_mem_entry *trace;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300374 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200375 void *ptr;
376 char name[15];
377
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200378 if (sizeof(*rsc) > avail) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300379 dev_err(dev, "trace rsc is truncated\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200380 return -EINVAL;
381 }
382
383 /* make sure reserved bytes are zeroes */
384 if (rsc->reserved) {
385 dev_err(dev, "trace rsc has non zero reserved bytes\n");
386 return -EINVAL;
387 }
388
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200389 /* what's the kernel address of this resource ? */
390 ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
391 if (!ptr) {
392 dev_err(dev, "erroneous trace resource entry\n");
393 return -EINVAL;
394 }
395
396 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
397 if (!trace) {
398 dev_err(dev, "kzalloc trace failed\n");
399 return -ENOMEM;
400 }
401
402 /* set the trace buffer dma properties */
403 trace->len = rsc->len;
404 trace->va = ptr;
405
406 /* make sure snprintf always null terminates, even if truncating */
407 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
408
409 /* create the debugfs entry */
410 trace->priv = rproc_create_trace_file(name, rproc, trace);
411 if (!trace->priv) {
412 trace->va = NULL;
413 kfree(trace);
414 return -EINVAL;
415 }
416
417 list_add_tail(&trace->node, &rproc->traces);
418
419 rproc->num_traces++;
420
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200421 dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200422 rsc->da, rsc->len);
423
424 return 0;
425}
426
427/**
428 * rproc_handle_devmem() - handle devmem resource entry
429 * @rproc: remote processor handle
430 * @rsc: the devmem resource entry
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200431 * @avail: size of available data (for sanity checking the image)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200432 *
433 * Remote processors commonly need to access certain on-chip peripherals.
434 *
435 * Some of these remote processors access memory via an iommu device,
436 * and might require us to configure their iommu before they can access
437 * the on-chip peripherals they need.
438 *
439 * This resource entry is a request to map such a peripheral device.
440 *
441 * These devmem entries will contain the physical address of the device in
442 * the 'pa' member. If a specific device address is expected, then 'da' will
443 * contain it (currently this is the only use case supported). 'len' will
444 * contain the size of the physical region we need to map.
445 *
446 * Currently we just "trust" those devmem entries to contain valid physical
447 * addresses, but this is going to change: we want the implementations to
448 * tell us ranges of physical addresses the firmware is allowed to request,
449 * and not allow firmwares to request access to physical addresses that
450 * are outside those ranges.
451 */
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200452static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
453 int avail)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200454{
455 struct rproc_mem_entry *mapping;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300456 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200457 int ret;
458
459 /* no point in handling this resource without a valid iommu domain */
460 if (!rproc->domain)
461 return -EINVAL;
462
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200463 if (sizeof(*rsc) > avail) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300464 dev_err(dev, "devmem rsc is truncated\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200465 return -EINVAL;
466 }
467
468 /* make sure reserved bytes are zeroes */
469 if (rsc->reserved) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300470 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200471 return -EINVAL;
472 }
473
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200474 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
475 if (!mapping) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300476 dev_err(dev, "kzalloc mapping failed\n");
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200477 return -ENOMEM;
478 }
479
480 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
481 if (ret) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300482 dev_err(dev, "failed to map devmem: %d\n", ret);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200483 goto out;
484 }
485
486 /*
487 * We'll need this info later when we'll want to unmap everything
488 * (e.g. on shutdown).
489 *
490 * We can't trust the remote processor not to change the resource
491 * table, so we must maintain this info independently.
492 */
493 mapping->da = rsc->da;
494 mapping->len = rsc->len;
495 list_add_tail(&mapping->node, &rproc->mappings);
496
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300497 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200498 rsc->pa, rsc->da, rsc->len);
499
500 return 0;
501
502out:
503 kfree(mapping);
504 return ret;
505}
506
507/**
508 * rproc_handle_carveout() - handle phys contig memory allocation requests
509 * @rproc: rproc handle
510 * @rsc: the resource entry
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200511 * @avail: size of available data (for image validation)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200512 *
513 * This function will handle firmware requests for allocation of physically
514 * contiguous memory regions.
515 *
516 * These request entries should come first in the firmware's resource table,
517 * as other firmware entries might request placing other data objects inside
518 * these memory regions (e.g. data/code segments, trace resource entries, ...).
519 *
520 * Allocating memory this way helps utilizing the reserved physical memory
521 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
522 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
523 * pressure is important; it may have a substantial impact on performance.
524 */
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200525static int rproc_handle_carveout(struct rproc *rproc,
526 struct fw_rsc_carveout *rsc, int avail)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200527{
528 struct rproc_mem_entry *carveout, *mapping;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300529 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200530 dma_addr_t dma;
531 void *va;
532 int ret;
533
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200534 if (sizeof(*rsc) > avail) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300535 dev_err(dev, "carveout rsc is truncated\n");
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200536 return -EINVAL;
537 }
538
539 /* make sure reserved bytes are zeroes */
540 if (rsc->reserved) {
541 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
542 return -EINVAL;
543 }
544
545 dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
546 rsc->da, rsc->pa, rsc->len, rsc->flags);
547
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200548 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
549 if (!mapping) {
550 dev_err(dev, "kzalloc mapping failed\n");
551 return -ENOMEM;
552 }
553
554 carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
555 if (!carveout) {
556 dev_err(dev, "kzalloc carveout failed\n");
557 ret = -ENOMEM;
558 goto free_mapping;
559 }
560
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300561 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200562 if (!va) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300563 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200564 ret = -ENOMEM;
565 goto free_carv;
566 }
567
568 dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
569
570 /*
571 * Ok, this is non-standard.
572 *
573 * Sometimes we can't rely on the generic iommu-based DMA API
574 * to dynamically allocate the device address and then set the IOMMU
575 * tables accordingly, because some remote processors might
576 * _require_ us to use hard coded device addresses that their
577 * firmware was compiled with.
578 *
579 * In this case, we must use the IOMMU API directly and map
580 * the memory to the device address as expected by the remote
581 * processor.
582 *
583 * Obviously such remote processor devices should not be configured
584 * to use the iommu-based DMA API: we expect 'dma' to contain the
585 * physical address in this case.
586 */
587 if (rproc->domain) {
588 ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
589 rsc->flags);
590 if (ret) {
591 dev_err(dev, "iommu_map failed: %d\n", ret);
592 goto dma_free;
593 }
594
595 /*
596 * We'll need this info later when we'll want to unmap
597 * everything (e.g. on shutdown).
598 *
599 * We can't trust the remote processor not to change the
600 * resource 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
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200606 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200607 }
608
Ohad Ben-Cohen0e49b722012-07-01 11:30:57 +0300609 /*
610 * Some remote processors might need to know the pa
611 * even though they are behind an IOMMU. E.g., OMAP4's
612 * remote M3 processor needs this so it can control
613 * on-chip hardware accelerators that are not behind
614 * the IOMMU, and therefor must know the pa.
615 *
616 * Generally we don't want to expose physical addresses
617 * if we don't have to (remote processors are generally
618 * _not_ trusted), so we might want to do this only for
619 * remote processor that _must_ have this (e.g. OMAP4's
620 * dual M3 subsystem).
621 *
622 * Non-IOMMU processors might also want to have this info.
623 * In this case, the device address and the physical address
624 * are the same.
625 */
626 rsc->pa = dma;
627
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200628 carveout->va = va;
629 carveout->len = rsc->len;
630 carveout->dma = dma;
631 carveout->da = rsc->da;
632
633 list_add_tail(&carveout->node, &rproc->carveouts);
634
635 return 0;
636
637dma_free:
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300638 dma_free_coherent(dev->parent, rsc->len, va, dma);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200639free_carv:
640 kfree(carveout);
641free_mapping:
642 kfree(mapping);
643 return ret;
644}
645
Ohad Ben-Cohene12bc142012-01-31 16:07:27 +0200646/*
647 * A lookup table for resource handlers. The indices are defined in
648 * enum fw_resource_type.
649 */
650static rproc_handle_resource_t rproc_handle_rsc[] = {
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200651 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
652 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
653 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100654 [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
Ohad Ben-Cohene12bc142012-01-31 16:07:27 +0200655};
656
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200657/* handle firmware resource entries before booting the remote processor */
658static int
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200659rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200660{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300661 struct device *dev = &rproc->dev;
Ohad Ben-Cohene12bc142012-01-31 16:07:27 +0200662 rproc_handle_resource_t handler;
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200663 int ret = 0, i;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200664
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200665 for (i = 0; i < table->num; i++) {
666 int offset = table->offset[i];
667 struct fw_rsc_hdr *hdr = (void *)table + offset;
668 int avail = len - offset - sizeof(*hdr);
669 void *rsc = (void *)hdr + sizeof(*hdr);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200670
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200671 /* make sure table isn't truncated */
672 if (avail < 0) {
673 dev_err(dev, "rsc table is truncated\n");
674 return -EINVAL;
675 }
676
677 dev_dbg(dev, "rsc: type %d\n", hdr->type);
678
679 if (hdr->type >= RSC_LAST) {
680 dev_warn(dev, "unsupported resource %d\n", hdr->type);
Ohad Ben-Cohene12bc142012-01-31 16:07:27 +0200681 continue;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200682 }
683
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200684 handler = rproc_handle_rsc[hdr->type];
Ohad Ben-Cohene12bc142012-01-31 16:07:27 +0200685 if (!handler)
686 continue;
687
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200688 ret = handler(rproc, rsc, avail);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200689 if (ret)
690 break;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200691 }
692
693 return ret;
694}
695
696/* handle firmware resource entries while registering the remote processor */
697static int
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200698rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200699{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300700 struct device *dev = &rproc->dev;
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200701 int ret = 0, i;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200702
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200703 for (i = 0; i < table->num; i++) {
704 int offset = table->offset[i];
705 struct fw_rsc_hdr *hdr = (void *)table + offset;
706 int avail = len - offset - sizeof(*hdr);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100707 struct fw_rsc_vdev *vrsc;
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200708
709 /* make sure table isn't truncated */
710 if (avail < 0) {
711 dev_err(dev, "rsc table is truncated\n");
712 return -EINVAL;
713 }
714
715 dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
716
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100717 if (hdr->type != RSC_VDEV)
718 continue;
719
720 vrsc = (struct fw_rsc_vdev *)hdr->data;
721
722 ret = rproc_handle_vdev(rproc, vrsc, avail);
723 if (ret)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200724 break;
Ohad Ben-Cohenfd2c15e2012-02-01 21:56:16 +0200725 }
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200726
727 return ret;
728}
729
730/**
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200731 * rproc_resource_cleanup() - clean up and free all acquired resources
732 * @rproc: rproc handle
733 *
734 * This function will free all resources acquired for @rproc, and it
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100735 * is called whenever @rproc either shuts down or fails to boot.
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200736 */
737static void rproc_resource_cleanup(struct rproc *rproc)
738{
739 struct rproc_mem_entry *entry, *tmp;
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300740 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200741
742 /* clean up debugfs trace entries */
743 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
744 rproc_remove_trace_file(entry->priv);
745 rproc->num_traces--;
746 list_del(&entry->node);
747 kfree(entry);
748 }
749
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200750 /* clean up carveout allocations */
751 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300752 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200753 list_del(&entry->node);
754 kfree(entry);
755 }
756
757 /* clean up iommu mapping entries */
758 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
759 size_t unmapped;
760
761 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
762 if (unmapped != entry->len) {
763 /* nothing much to do besides complaining */
Sjur Brændelande981f6d2012-06-10 14:37:07 +0300764 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200765 unmapped);
766 }
767
768 list_del(&entry->node);
769 kfree(entry);
770 }
771}
772
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200773/*
774 * take a firmware and boot a remote processor with it.
775 */
776static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
777{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300778 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200779 const char *name = rproc->firmware;
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100780 struct resource_table *table;
781 int ret, tablesz;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200782
783 ret = rproc_fw_sanity_check(rproc, fw);
784 if (ret)
785 return ret;
786
Sjur Brændelande981f6d2012-06-10 14:37:07 +0300787 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200788
789 /*
790 * if enabling an IOMMU isn't relevant for this rproc, this is
791 * just a nop
792 */
793 ret = rproc_enable_iommu(rproc);
794 if (ret) {
795 dev_err(dev, "can't enable iommu: %d\n", ret);
796 return ret;
797 }
798
Sjur Brændeland3e5f9eb2012-06-19 09:56:44 +0300799 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200800
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100801 /* look for the resource table */
Sjur Brændelandbd484982012-06-19 09:55:34 +0300802 table = rproc_find_rsc_table(rproc, fw, &tablesz);
Sjur Brændeland30338cf2012-06-10 14:37:51 +0300803 if (!table) {
804 ret = -EINVAL;
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100805 goto clean_up;
Sjur Brændeland30338cf2012-06-10 14:37:51 +0300806 }
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100807
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200808 /* handle fw resources which are required to boot rproc */
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100809 ret = rproc_handle_boot_rsc(rproc, table, tablesz);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200810 if (ret) {
811 dev_err(dev, "Failed to process resources: %d\n", ret);
812 goto clean_up;
813 }
814
815 /* load the ELF segments to memory */
Sjur Brændelandbd484982012-06-19 09:55:34 +0300816 ret = rproc_load_segments(rproc, fw);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200817 if (ret) {
818 dev_err(dev, "Failed to load program segments: %d\n", ret);
819 goto clean_up;
820 }
821
822 /* power up the remote processor */
823 ret = rproc->ops->start(rproc);
824 if (ret) {
825 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
826 goto clean_up;
827 }
828
829 rproc->state = RPROC_RUNNING;
830
831 dev_info(dev, "remote processor %s is now up\n", rproc->name);
832
833 return 0;
834
835clean_up:
836 rproc_resource_cleanup(rproc);
837 rproc_disable_iommu(rproc);
838 return ret;
839}
840
841/*
842 * take a firmware and look for virtio devices to register.
843 *
844 * Note: this function is called asynchronously upon registration of the
845 * remote processor (so we must wait until it completes before we try
846 * to unregister the device. one other option is just to use kref here,
847 * that might be cleaner).
848 */
849static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
850{
851 struct rproc *rproc = context;
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100852 struct resource_table *table;
853 int ret, tablesz;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200854
855 if (rproc_fw_sanity_check(rproc, fw) < 0)
856 goto out;
857
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100858 /* look for the resource table */
Sjur Brændelandbd484982012-06-19 09:55:34 +0300859 table = rproc_find_rsc_table(rproc, fw, &tablesz);
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100860 if (!table)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200861 goto out;
Ohad Ben-Cohen1e3e2c72012-02-13 21:47:49 +0100862
863 /* look for virtio devices and register them */
864 ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
865 if (ret)
866 goto out;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200867
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200868out:
Jesper Juhl3cc6e782012-04-09 22:51:25 +0200869 release_firmware(fw);
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +0300870 /* allow rproc_del() contexts, if any, to proceed */
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200871 complete_all(&rproc->firmware_loading_complete);
872}
873
874/**
875 * rproc_boot() - boot a remote processor
876 * @rproc: handle of a remote processor
877 *
878 * Boot a remote processor (i.e. load its firmware, power it on, ...).
879 *
880 * If the remote processor is already powered on, this function immediately
881 * returns (successfully).
882 *
883 * Returns 0 on success, and an appropriate error value otherwise.
884 */
885int rproc_boot(struct rproc *rproc)
886{
887 const struct firmware *firmware_p;
888 struct device *dev;
889 int ret;
890
891 if (!rproc) {
892 pr_err("invalid rproc handle\n");
893 return -EINVAL;
894 }
895
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300896 dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200897
898 ret = mutex_lock_interruptible(&rproc->lock);
899 if (ret) {
900 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
901 return ret;
902 }
903
904 /* loading a firmware is required */
905 if (!rproc->firmware) {
906 dev_err(dev, "%s: no firmware to load\n", __func__);
907 ret = -EINVAL;
908 goto unlock_mutex;
909 }
910
911 /* prevent underlying implementation from being removed */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300912 if (!try_module_get(dev->parent->driver->owner)) {
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200913 dev_err(dev, "%s: can't get owner\n", __func__);
914 ret = -EINVAL;
915 goto unlock_mutex;
916 }
917
918 /* skip the boot process if rproc is already powered up */
919 if (atomic_inc_return(&rproc->power) > 1) {
920 ret = 0;
921 goto unlock_mutex;
922 }
923
924 dev_info(dev, "powering up %s\n", rproc->name);
925
926 /* load firmware */
927 ret = request_firmware(&firmware_p, rproc->firmware, dev);
928 if (ret < 0) {
929 dev_err(dev, "request_firmware failed: %d\n", ret);
930 goto downref_rproc;
931 }
932
933 ret = rproc_fw_boot(rproc, firmware_p);
934
935 release_firmware(firmware_p);
936
937downref_rproc:
938 if (ret) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300939 module_put(dev->parent->driver->owner);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200940 atomic_dec(&rproc->power);
941 }
942unlock_mutex:
943 mutex_unlock(&rproc->lock);
944 return ret;
945}
946EXPORT_SYMBOL(rproc_boot);
947
948/**
949 * rproc_shutdown() - power off the remote processor
950 * @rproc: the remote processor
951 *
952 * Power off a remote processor (previously booted with rproc_boot()).
953 *
954 * In case @rproc is still being used by an additional user(s), then
955 * this function will just decrement the power refcount and exit,
956 * without really powering off the device.
957 *
958 * Every call to rproc_boot() must (eventually) be accompanied by a call
959 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
960 *
961 * Notes:
962 * - we're not decrementing the rproc's refcount, only the power refcount.
963 * which means that the @rproc handle stays valid even after rproc_shutdown()
964 * returns, and users can still use it with a subsequent rproc_boot(), if
965 * needed.
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200966 */
967void rproc_shutdown(struct rproc *rproc)
968{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300969 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +0200970 int ret;
971
972 ret = mutex_lock_interruptible(&rproc->lock);
973 if (ret) {
974 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
975 return;
976 }
977
978 /* if the remote proc is still needed, bail out */
979 if (!atomic_dec_and_test(&rproc->power))
980 goto out;
981
982 /* power off the remote processor */
983 ret = rproc->ops->stop(rproc);
984 if (ret) {
985 atomic_inc(&rproc->power);
986 dev_err(dev, "can't stop rproc: %d\n", ret);
987 goto out;
988 }
989
990 /* clean up all acquired resources */
991 rproc_resource_cleanup(rproc);
992
993 rproc_disable_iommu(rproc);
994
995 rproc->state = RPROC_OFFLINE;
996
997 dev_info(dev, "stopped remote processor %s\n", rproc->name);
998
999out:
1000 mutex_unlock(&rproc->lock);
1001 if (!ret)
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001002 module_put(dev->parent->driver->owner);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001003}
1004EXPORT_SYMBOL(rproc_shutdown);
1005
1006/**
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001007 * rproc_add() - register a remote processor
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001008 * @rproc: the remote processor handle to register
1009 *
1010 * Registers @rproc with the remoteproc framework, after it has been
1011 * allocated with rproc_alloc().
1012 *
1013 * This is called by the platform-specific rproc implementation, whenever
1014 * a new remote processor device is probed.
1015 *
1016 * Returns 0 on success and an appropriate error code otherwise.
1017 *
1018 * Note: this function initiates an asynchronous firmware loading
1019 * context, which will look for virtio devices supported by the rproc's
1020 * firmware.
1021 *
1022 * If found, those virtio devices will be created and added, so as a result
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001023 * of registering this remote processor, additional virtio drivers might be
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001024 * probed.
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001025 */
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001026int rproc_add(struct rproc *rproc)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001027{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001028 struct device *dev = &rproc->dev;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001029 int ret = 0;
1030
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001031 ret = device_add(dev);
1032 if (ret < 0)
1033 return ret;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001034
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001035 dev_info(dev, "%s is available\n", rproc->name);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001036
Ohad Ben-Cohen489d1292011-12-21 11:25:43 +02001037 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1038 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1039
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001040 /* create debugfs entries */
1041 rproc_create_debug_dir(rproc);
1042
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001043 /* rproc_del() calls must wait until async loader completes */
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001044 init_completion(&rproc->firmware_loading_complete);
1045
1046 /*
1047 * We must retrieve early virtio configuration info from
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001048 * the firmware (e.g. whether to register a virtio device,
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001049 * what virtio features does it support, ...).
1050 *
1051 * We're initiating an asynchronous firmware loading, so we can
1052 * be built-in kernel code, without hanging the boot process.
1053 */
1054 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1055 rproc->firmware, dev, GFP_KERNEL,
1056 rproc, rproc_fw_config_virtio);
1057 if (ret < 0) {
1058 dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
1059 complete_all(&rproc->firmware_loading_complete);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001060 }
1061
1062 return ret;
1063}
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001064EXPORT_SYMBOL(rproc_add);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001065
1066/**
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001067 * rproc_type_release() - release a remote processor instance
1068 * @dev: the rproc's device
1069 *
1070 * This function should _never_ be called directly.
1071 *
1072 * It will be called by the driver core when no one holds a valid pointer
1073 * to @dev anymore.
1074 */
1075static void rproc_type_release(struct device *dev)
1076{
1077 struct rproc *rproc = container_of(dev, struct rproc, dev);
1078
Ohad Ben-Cohen7183a2a2012-05-30 22:02:24 +03001079 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1080
1081 rproc_delete_debug_dir(rproc);
1082
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001083 idr_remove_all(&rproc->notifyids);
1084 idr_destroy(&rproc->notifyids);
1085
1086 if (rproc->index >= 0)
1087 ida_simple_remove(&rproc_dev_index, rproc->index);
1088
1089 kfree(rproc);
1090}
1091
1092static struct device_type rproc_type = {
1093 .name = "remoteproc",
1094 .release = rproc_type_release,
1095};
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001096
1097/**
1098 * rproc_alloc() - allocate a remote processor handle
1099 * @dev: the underlying device
1100 * @name: name of this remote processor
1101 * @ops: platform-specific handlers (mainly start/stop)
1102 * @firmware: name of firmware file to load
1103 * @len: length of private data needed by the rproc driver (in bytes)
1104 *
1105 * Allocates a new remote processor handle, but does not register
1106 * it yet.
1107 *
1108 * This function should be used by rproc implementations during initialization
1109 * of the remote processor.
1110 *
1111 * After creating an rproc handle using this function, and when ready,
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001112 * implementations should then call rproc_add() to complete
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001113 * the registration of the remote processor.
1114 *
1115 * On success the new rproc is returned, and on failure, NULL.
1116 *
1117 * Note: _never_ directly deallocate @rproc, even if it was not registered
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001118 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001119 */
1120struct rproc *rproc_alloc(struct device *dev, const char *name,
1121 const struct rproc_ops *ops,
1122 const char *firmware, int len)
1123{
1124 struct rproc *rproc;
1125
1126 if (!dev || !name || !ops)
1127 return NULL;
1128
1129 rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1130 if (!rproc) {
1131 dev_err(dev, "%s: kzalloc failed\n", __func__);
1132 return NULL;
1133 }
1134
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001135 rproc->name = name;
1136 rproc->ops = ops;
1137 rproc->firmware = firmware;
1138 rproc->priv = &rproc[1];
1139
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001140 device_initialize(&rproc->dev);
1141 rproc->dev.parent = dev;
1142 rproc->dev.type = &rproc_type;
1143
1144 /* Assign a unique device index and name */
1145 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1146 if (rproc->index < 0) {
1147 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1148 put_device(&rproc->dev);
1149 return NULL;
1150 }
1151
1152 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1153
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001154 atomic_set(&rproc->power, 0);
1155
Sjur Brændeland4afc89d2012-06-19 10:08:18 +03001156 /* Set ELF as the default fw_ops handler */
1157 rproc->fw_ops = &rproc_elf_fw_ops;
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001158
1159 mutex_init(&rproc->lock);
1160
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001161 idr_init(&rproc->notifyids);
1162
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001163 INIT_LIST_HEAD(&rproc->carveouts);
1164 INIT_LIST_HEAD(&rproc->mappings);
1165 INIT_LIST_HEAD(&rproc->traces);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001166 INIT_LIST_HEAD(&rproc->rvdevs);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001167
1168 rproc->state = RPROC_OFFLINE;
1169
1170 return rproc;
1171}
1172EXPORT_SYMBOL(rproc_alloc);
1173
1174/**
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001175 * rproc_put() - unroll rproc_alloc()
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001176 * @rproc: the remote processor handle
1177 *
Ohad Ben-Cohenc6b5a272012-07-02 11:41:16 +03001178 * This function decrements the rproc dev refcount.
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001179 *
Ohad Ben-Cohenc6b5a272012-07-02 11:41:16 +03001180 * If no one holds any reference to rproc anymore, then its refcount would
1181 * now drop to zero, and it would be freed.
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001182 */
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001183void rproc_put(struct rproc *rproc)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001184{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001185 put_device(&rproc->dev);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001186}
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001187EXPORT_SYMBOL(rproc_put);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001188
1189/**
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001190 * rproc_del() - unregister a remote processor
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001191 * @rproc: rproc handle to unregister
1192 *
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001193 * This function should be called when the platform specific rproc
1194 * implementation decides to remove the rproc device. it should
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001195 * _only_ be called if a previous invocation of rproc_add()
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001196 * has completed successfully.
1197 *
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001198 * After rproc_del() returns, @rproc isn't freed yet, because
Ohad Ben-Cohenc6b5a272012-07-02 11:41:16 +03001199 * of the outstanding reference created by rproc_alloc. To decrement that
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001200 * one last refcount, one still needs to call rproc_put().
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001201 *
1202 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1203 */
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001204int rproc_del(struct rproc *rproc)
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001205{
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +03001206 struct rproc_vdev *rvdev, *tmp;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001207
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001208 if (!rproc)
1209 return -EINVAL;
1210
1211 /* if rproc is just being registered, wait */
1212 wait_for_completion(&rproc->firmware_loading_complete);
1213
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001214 /* clean up remote vdev entries */
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +03001215 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +01001216 rproc_remove_virtio_dev(rvdev);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001217
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001218 device_del(&rproc->dev);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001219
1220 return 0;
1221}
Ohad Ben-Cohen160e7c82012-07-04 16:25:06 +03001222EXPORT_SYMBOL(rproc_del);
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001223
1224static int __init remoteproc_init(void)
1225{
1226 rproc_init_debugfs();
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001227
Ohad Ben-Cohen400e64d2011-10-20 16:52:46 +02001228 return 0;
1229}
1230module_init(remoteproc_init);
1231
1232static void __exit remoteproc_exit(void)
1233{
1234 rproc_exit_debugfs();
1235}
1236module_exit(remoteproc_exit);
1237
1238MODULE_LICENSE("GPL v2");
1239MODULE_DESCRIPTION("Generic Remote Processor Framework");