aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/remoteproc.txt58
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/remoteproc/Makefile1
-rw-r--r--drivers/remoteproc/omap_remoteproc.c26
-rw-r--r--drivers/remoteproc/remoteproc_core.c726
-rw-r--r--drivers/remoteproc/remoteproc_debugfs.c4
-rw-r--r--drivers/remoteproc/remoteproc_elf_loader.c295
-rw-r--r--drivers/remoteproc/remoteproc_internal.h62
-rw-r--r--drivers/remoteproc/remoteproc_virtio.c34
-rw-r--r--drivers/rpmsg/virtio_rpmsg_bus.c3
-rw-r--r--include/linux/remoteproc.h20
11 files changed, 613 insertions, 617 deletions
diff --git a/Documentation/remoteproc.txt b/Documentation/remoteproc.txt
index 70a048cd3fa3..23a09b884bc7 100644
--- a/Documentation/remoteproc.txt
+++ b/Documentation/remoteproc.txt
@@ -36,8 +36,7 @@ cost.
36 Note: to use this function you should already have a valid rproc 36 Note: to use this function you should already have a valid rproc
37 handle. There are several ways to achieve that cleanly (devres, pdata, 37 handle. There are several ways to achieve that cleanly (devres, pdata,
38 the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we 38 the way remoteproc_rpmsg.c does this, or, if this becomes prevalent, we
39 might also consider using dev_archdata for this). See also 39 might also consider using dev_archdata for this).
40 rproc_get_by_name() below.
41 40
42 void rproc_shutdown(struct rproc *rproc) 41 void rproc_shutdown(struct rproc *rproc)
43 - Power off a remote processor (previously booted with rproc_boot()). 42 - Power off a remote processor (previously booted with rproc_boot()).
@@ -51,30 +50,6 @@ cost.
51 which means that the @rproc handle stays valid even after 50 which means that the @rproc handle stays valid even after
52 rproc_shutdown() returns, and users can still use it with a subsequent 51 rproc_shutdown() returns, and users can still use it with a subsequent
53 rproc_boot(), if needed. 52 rproc_boot(), if needed.
54 - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
55 because rproc_shutdown() _does not_ decrement the refcount of @rproc.
56 To decrement the refcount of @rproc, use rproc_put() (but _only_ if
57 you acquired @rproc using rproc_get_by_name()).
58
59 struct rproc *rproc_get_by_name(const char *name)
60 - Find an rproc handle using the remote processor's name, and then
61 boot it. If it's already powered on, then just immediately return
62 (successfully). Returns the rproc handle on success, and NULL on failure.
63 This function increments the remote processor's refcount, so always
64 use rproc_put() to decrement it back once rproc isn't needed anymore.
65 Note: currently rproc_get_by_name() and rproc_put() are not used anymore
66 by the rpmsg bus and its drivers. We need to scrutinize the use cases
67 that still need them, and see if we can migrate them to use the non
68 name-based boot/shutdown interface.
69
70 void rproc_put(struct rproc *rproc)
71 - Decrement @rproc's power refcount and shut it down if it reaches zero
72 (essentially by just calling rproc_shutdown), and then decrement @rproc's
73 validity refcount too.
74 After this function returns, @rproc may _not_ be used anymore, and its
75 handle should be considered invalid.
76 This function should be called _iff_ the @rproc handle was grabbed by
77 calling rproc_get_by_name().
78 53
793. Typical usage 543. Typical usage
80 55
@@ -115,21 +90,21 @@ int dummy_rproc_example(struct rproc *my_rproc)
115 This function should be used by rproc implementations during 90 This function should be used by rproc implementations during
116 initialization of the remote processor. 91 initialization of the remote processor.
117 After creating an rproc handle using this function, and when ready, 92 After creating an rproc handle using this function, and when ready,
118 implementations should then call rproc_register() to complete 93 implementations should then call rproc_add() to complete
119 the registration of the remote processor. 94 the registration of the remote processor.
120 On success, the new rproc is returned, and on failure, NULL. 95 On success, the new rproc is returned, and on failure, NULL.
121 96
122 Note: _never_ directly deallocate @rproc, even if it was not registered 97 Note: _never_ directly deallocate @rproc, even if it was not registered
123 yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free(). 98 yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
124 99
125 void rproc_free(struct rproc *rproc) 100 void rproc_put(struct rproc *rproc)
126 - Free an rproc handle that was allocated by rproc_alloc. 101 - Free an rproc handle that was allocated by rproc_alloc.
127 This function should _only_ be used if @rproc was only allocated, 102 This function essentially unrolls rproc_alloc(), by decrementing the
128 but not registered yet. 103 rproc's refcount. It doesn't directly free rproc; that would happen
129 If @rproc was already successfully registered (by calling 104 only if there are no other references to rproc and its refcount now
130 rproc_register()), then use rproc_unregister() instead. 105 dropped to zero.
131 106
132 int rproc_register(struct rproc *rproc) 107 int rproc_add(struct rproc *rproc)
133 - Register @rproc with the remoteproc framework, after it has been 108 - Register @rproc with the remoteproc framework, after it has been
134 allocated with rproc_alloc(). 109 allocated with rproc_alloc().
135 This is called by the platform-specific rproc implementation, whenever 110 This is called by the platform-specific rproc implementation, whenever
@@ -142,20 +117,15 @@ int dummy_rproc_example(struct rproc *my_rproc)
142 of registering this remote processor, additional virtio drivers might get 117 of registering this remote processor, additional virtio drivers might get
143 probed. 118 probed.
144 119
145 int rproc_unregister(struct rproc *rproc) 120 int rproc_del(struct rproc *rproc)
146 - Unregister a remote processor, and decrement its refcount. 121 - Unroll rproc_add().
147 If its refcount drops to zero, then @rproc will be freed. If not,
148 it will be freed later once the last reference is dropped.
149
150 This function should be called when the platform specific rproc 122 This function should be called when the platform specific rproc
151 implementation decides to remove the rproc device. it should 123 implementation decides to remove the rproc device. it should
152 _only_ be called if a previous invocation of rproc_register() 124 _only_ be called if a previous invocation of rproc_add()
153 has completed successfully. 125 has completed successfully.
154 126
155 After rproc_unregister() returns, @rproc is _not_ valid anymore and 127 After rproc_del() returns, @rproc is still valid, and its
156 it shouldn't be used. More specifically, don't call rproc_free() 128 last refcount should be decremented by calling rproc_put().
157 or try to directly free @rproc after rproc_unregister() returns;
158 none of these are needed, and calling them is a bug.
159 129
160 Returns 0 on success and -EINVAL if @rproc isn't valid. 130 Returns 0 on success and -EINVAL if @rproc isn't valid.
161 131
diff --git a/MAINTAINERS b/MAINTAINERS
index 48778c985dc8..8f363b005226 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5725,6 +5725,7 @@ F: include/linux/regmap.h
5725 5725
5726REMOTE PROCESSOR (REMOTEPROC) SUBSYSTEM 5726REMOTE PROCESSOR (REMOTEPROC) SUBSYSTEM
5727M: Ohad Ben-Cohen <ohad@wizery.com> 5727M: Ohad Ben-Cohen <ohad@wizery.com>
5728T: git git://git.kernel.org/pub/scm/linux/kernel/git/ohad/remoteproc.git
5728S: Maintained 5729S: Maintained
5729F: drivers/remoteproc/ 5730F: drivers/remoteproc/
5730F: Documentation/remoteproc.txt 5731F: Documentation/remoteproc.txt
diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
index 5445d9b23294..934ce6e2c66b 100644
--- a/drivers/remoteproc/Makefile
+++ b/drivers/remoteproc/Makefile
@@ -6,4 +6,5 @@ obj-$(CONFIG_REMOTEPROC) += remoteproc.o
6remoteproc-y := remoteproc_core.o 6remoteproc-y := remoteproc_core.o
7remoteproc-y += remoteproc_debugfs.o 7remoteproc-y += remoteproc_debugfs.o
8remoteproc-y += remoteproc_virtio.o 8remoteproc-y += remoteproc_virtio.o
9remoteproc-y += remoteproc_elf_loader.o
9obj-$(CONFIG_OMAP_REMOTEPROC) += omap_remoteproc.o 10obj-$(CONFIG_OMAP_REMOTEPROC) += omap_remoteproc.o
diff --git a/drivers/remoteproc/omap_remoteproc.c b/drivers/remoteproc/omap_remoteproc.c
index de138e30d3e6..a1f7ac1f8cf6 100644
--- a/drivers/remoteproc/omap_remoteproc.c
+++ b/drivers/remoteproc/omap_remoteproc.c
@@ -66,7 +66,7 @@ static int omap_rproc_mbox_callback(struct notifier_block *this,
66{ 66{
67 mbox_msg_t msg = (mbox_msg_t) data; 67 mbox_msg_t msg = (mbox_msg_t) data;
68 struct omap_rproc *oproc = container_of(this, struct omap_rproc, nb); 68 struct omap_rproc *oproc = container_of(this, struct omap_rproc, nb);
69 struct device *dev = oproc->rproc->dev; 69 struct device *dev = oproc->rproc->dev.parent;
70 const char *name = oproc->rproc->name; 70 const char *name = oproc->rproc->name;
71 71
72 dev_dbg(dev, "mbox msg: 0x%x\n", msg); 72 dev_dbg(dev, "mbox msg: 0x%x\n", msg);
@@ -92,12 +92,13 @@ static int omap_rproc_mbox_callback(struct notifier_block *this,
92static void omap_rproc_kick(struct rproc *rproc, int vqid) 92static void omap_rproc_kick(struct rproc *rproc, int vqid)
93{ 93{
94 struct omap_rproc *oproc = rproc->priv; 94 struct omap_rproc *oproc = rproc->priv;
95 struct device *dev = rproc->dev.parent;
95 int ret; 96 int ret;
96 97
97 /* send the index of the triggered virtqueue in the mailbox payload */ 98 /* send the index of the triggered virtqueue in the mailbox payload */
98 ret = omap_mbox_msg_send(oproc->mbox, vqid); 99 ret = omap_mbox_msg_send(oproc->mbox, vqid);
99 if (ret) 100 if (ret)
100 dev_err(rproc->dev, "omap_mbox_msg_send failed: %d\n", ret); 101 dev_err(dev, "omap_mbox_msg_send failed: %d\n", ret);
101} 102}
102 103
103/* 104/*
@@ -110,7 +111,8 @@ static void omap_rproc_kick(struct rproc *rproc, int vqid)
110static int omap_rproc_start(struct rproc *rproc) 111static int omap_rproc_start(struct rproc *rproc)
111{ 112{
112 struct omap_rproc *oproc = rproc->priv; 113 struct omap_rproc *oproc = rproc->priv;
113 struct platform_device *pdev = to_platform_device(rproc->dev); 114 struct device *dev = rproc->dev.parent;
115 struct platform_device *pdev = to_platform_device(dev);
114 struct omap_rproc_pdata *pdata = pdev->dev.platform_data; 116 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
115 int ret; 117 int ret;
116 118
@@ -120,7 +122,7 @@ static int omap_rproc_start(struct rproc *rproc)
120 oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb); 122 oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb);
121 if (IS_ERR(oproc->mbox)) { 123 if (IS_ERR(oproc->mbox)) {
122 ret = PTR_ERR(oproc->mbox); 124 ret = PTR_ERR(oproc->mbox);
123 dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret); 125 dev_err(dev, "omap_mbox_get failed: %d\n", ret);
124 return ret; 126 return ret;
125 } 127 }
126 128
@@ -133,13 +135,13 @@ static int omap_rproc_start(struct rproc *rproc)
133 */ 135 */
134 ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST); 136 ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST);
135 if (ret) { 137 if (ret) {
136 dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret); 138 dev_err(dev, "omap_mbox_get failed: %d\n", ret);
137 goto put_mbox; 139 goto put_mbox;
138 } 140 }
139 141
140 ret = pdata->device_enable(pdev); 142 ret = pdata->device_enable(pdev);
141 if (ret) { 143 if (ret) {
142 dev_err(rproc->dev, "omap_device_enable failed: %d\n", ret); 144 dev_err(dev, "omap_device_enable failed: %d\n", ret);
143 goto put_mbox; 145 goto put_mbox;
144 } 146 }
145 147
@@ -153,7 +155,8 @@ put_mbox:
153/* power off the remote processor */ 155/* power off the remote processor */
154static int omap_rproc_stop(struct rproc *rproc) 156static int omap_rproc_stop(struct rproc *rproc)
155{ 157{
156 struct platform_device *pdev = to_platform_device(rproc->dev); 158 struct device *dev = rproc->dev.parent;
159 struct platform_device *pdev = to_platform_device(dev);
157 struct omap_rproc_pdata *pdata = pdev->dev.platform_data; 160 struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
158 struct omap_rproc *oproc = rproc->priv; 161 struct omap_rproc *oproc = rproc->priv;
159 int ret; 162 int ret;
@@ -196,14 +199,14 @@ static int __devinit omap_rproc_probe(struct platform_device *pdev)
196 199
197 platform_set_drvdata(pdev, rproc); 200 platform_set_drvdata(pdev, rproc);
198 201
199 ret = rproc_register(rproc); 202 ret = rproc_add(rproc);
200 if (ret) 203 if (ret)
201 goto free_rproc; 204 goto free_rproc;
202 205
203 return 0; 206 return 0;
204 207
205free_rproc: 208free_rproc:
206 rproc_free(rproc); 209 rproc_put(rproc);
207 return ret; 210 return ret;
208} 211}
209 212
@@ -211,7 +214,10 @@ static int __devexit omap_rproc_remove(struct platform_device *pdev)
211{ 214{
212 struct rproc *rproc = platform_get_drvdata(pdev); 215 struct rproc *rproc = platform_get_drvdata(pdev);
213 216
214 return rproc_unregister(rproc); 217 rproc_del(rproc);
218 rproc_put(rproc);
219
220 return 0;
215} 221}
216 222
217static struct platform_driver omap_rproc_driver = { 223static struct platform_driver omap_rproc_driver = {
diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
index 66324ee4678f..d5c2dbfc7443 100644
--- a/drivers/remoteproc/remoteproc_core.c
+++ b/drivers/remoteproc/remoteproc_core.c
@@ -35,7 +35,7 @@
35#include <linux/debugfs.h> 35#include <linux/debugfs.h>
36#include <linux/remoteproc.h> 36#include <linux/remoteproc.h>
37#include <linux/iommu.h> 37#include <linux/iommu.h>
38#include <linux/klist.h> 38#include <linux/idr.h>
39#include <linux/elf.h> 39#include <linux/elf.h>
40#include <linux/virtio_ids.h> 40#include <linux/virtio_ids.h>
41#include <linux/virtio_ring.h> 41#include <linux/virtio_ring.h>
@@ -43,29 +43,13 @@
43 43
44#include "remoteproc_internal.h" 44#include "remoteproc_internal.h"
45 45
46static void klist_rproc_get(struct klist_node *n);
47static void klist_rproc_put(struct klist_node *n);
48
49/*
50 * klist of the available remote processors.
51 *
52 * We need this in order to support name-based lookups (needed by the
53 * rproc_get_by_name()).
54 *
55 * That said, we don't use rproc_get_by_name() at this point.
56 * The use cases that do require its existence should be
57 * scrutinized, and hopefully migrated to rproc_boot() using device-based
58 * binding.
59 *
60 * If/when this materializes, we could drop the klist (and the by_name
61 * API).
62 */
63static DEFINE_KLIST(rprocs, klist_rproc_get, klist_rproc_put);
64
65typedef int (*rproc_handle_resources_t)(struct rproc *rproc, 46typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
66 struct resource_table *table, int len); 47 struct resource_table *table, int len);
67typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail); 48typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
68 49
50/* Unique indices for remoteproc devices */
51static DEFINE_IDA(rproc_dev_index);
52
69/* 53/*
70 * This is the IOMMU fault handler we register with the IOMMU API 54 * This is the IOMMU fault handler we register with the IOMMU API
71 * (when relevant; not all remote processors access memory through 55 * (when relevant; not all remote processors access memory through
@@ -92,7 +76,7 @@ static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
92static int rproc_enable_iommu(struct rproc *rproc) 76static int rproc_enable_iommu(struct rproc *rproc)
93{ 77{
94 struct iommu_domain *domain; 78 struct iommu_domain *domain;
95 struct device *dev = rproc->dev; 79 struct device *dev = rproc->dev.parent;
96 int ret; 80 int ret;
97 81
98 /* 82 /*
@@ -137,7 +121,7 @@ free_domain:
137static void rproc_disable_iommu(struct rproc *rproc) 121static void rproc_disable_iommu(struct rproc *rproc)
138{ 122{
139 struct iommu_domain *domain = rproc->domain; 123 struct iommu_domain *domain = rproc->domain;
140 struct device *dev = rproc->dev; 124 struct device *dev = rproc->dev.parent;
141 125
142 if (!domain) 126 if (!domain)
143 return; 127 return;
@@ -165,7 +149,7 @@ static void rproc_disable_iommu(struct rproc *rproc)
165 * but only on kernel direct mapped RAM memory. Instead, we're just using 149 * but only on kernel direct mapped RAM memory. Instead, we're just using
166 * here the output of the DMA API, which should be more correct. 150 * here the output of the DMA API, which should be more correct.
167 */ 151 */
168static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) 152void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
169{ 153{
170 struct rproc_mem_entry *carveout; 154 struct rproc_mem_entry *carveout;
171 void *ptr = NULL; 155 void *ptr = NULL;
@@ -188,125 +172,19 @@ static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
188 172
189 return ptr; 173 return ptr;
190} 174}
175EXPORT_SYMBOL(rproc_da_to_va);
191 176
192/** 177int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
193 * rproc_load_segments() - load firmware segments to memory
194 * @rproc: remote processor which will be booted using these fw segments
195 * @elf_data: the content of the ELF firmware image
196 * @len: firmware size (in bytes)
197 *
198 * This function loads the firmware segments to memory, where the remote
199 * processor expects them.
200 *
201 * Some remote processors will expect their code and data to be placed
202 * in specific device addresses, and can't have them dynamically assigned.
203 *
204 * We currently support only those kind of remote processors, and expect
205 * the program header's paddr member to contain those addresses. We then go
206 * through the physically contiguous "carveout" memory regions which we
207 * allocated (and mapped) earlier on behalf of the remote processor,
208 * and "translate" device address to kernel addresses, so we can copy the
209 * segments where they are expected.
210 *
211 * Currently we only support remote processors that required carveout
212 * allocations and got them mapped onto their iommus. Some processors
213 * might be different: they might not have iommus, and would prefer to
214 * directly allocate memory for every segment/resource. This is not yet
215 * supported, though.
216 */
217static int
218rproc_load_segments(struct rproc *rproc, const u8 *elf_data, size_t len)
219{
220 struct device *dev = rproc->dev;
221 struct elf32_hdr *ehdr;
222 struct elf32_phdr *phdr;
223 int i, ret = 0;
224
225 ehdr = (struct elf32_hdr *)elf_data;
226 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
227
228 /* go through the available ELF segments */
229 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
230 u32 da = phdr->p_paddr;
231 u32 memsz = phdr->p_memsz;
232 u32 filesz = phdr->p_filesz;
233 u32 offset = phdr->p_offset;
234 void *ptr;
235
236 if (phdr->p_type != PT_LOAD)
237 continue;
238
239 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
240 phdr->p_type, da, memsz, filesz);
241
242 if (filesz > memsz) {
243 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
244 filesz, memsz);
245 ret = -EINVAL;
246 break;
247 }
248
249 if (offset + filesz > len) {
250 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
251 offset + filesz, len);
252 ret = -EINVAL;
253 break;
254 }
255
256 /* grab the kernel address for this device address */
257 ptr = rproc_da_to_va(rproc, da, memsz);
258 if (!ptr) {
259 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
260 ret = -EINVAL;
261 break;
262 }
263
264 /* put the segment where the remote processor expects it */
265 if (phdr->p_filesz)
266 memcpy(ptr, elf_data + phdr->p_offset, filesz);
267
268 /*
269 * Zero out remaining memory for this segment.
270 *
271 * This isn't strictly required since dma_alloc_coherent already
272 * did this for us. albeit harmless, we may consider removing
273 * this.
274 */
275 if (memsz > filesz)
276 memset(ptr + filesz, 0, memsz - filesz);
277 }
278
279 return ret;
280}
281
282static int
283__rproc_handle_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
284{ 178{
285 struct rproc *rproc = rvdev->rproc; 179 struct rproc *rproc = rvdev->rproc;
286 struct device *dev = rproc->dev; 180 struct device *dev = &rproc->dev;
287 struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 181 struct rproc_vring *rvring = &rvdev->vring[i];
288 dma_addr_t dma; 182 dma_addr_t dma;
289 void *va; 183 void *va;
290 int ret, size, notifyid; 184 int ret, size, notifyid;
291 185
292 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
293 i, vring->da, vring->num, vring->align);
294
295 /* make sure reserved bytes are zeroes */
296 if (vring->reserved) {
297 dev_err(dev, "vring rsc has non zero reserved bytes\n");
298 return -EINVAL;
299 }
300
301 /* verify queue size and vring alignment are sane */
302 if (!vring->num || !vring->align) {
303 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
304 vring->num, vring->align);
305 return -EINVAL;
306 }
307
308 /* actual size of vring (in bytes) */ 186 /* actual size of vring (in bytes) */
309 size = PAGE_ALIGN(vring_size(vring->num, vring->align)); 187 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
310 188
311 if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) { 189 if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
312 dev_err(dev, "idr_pre_get failed\n"); 190 dev_err(dev, "idr_pre_get failed\n");
@@ -316,51 +194,75 @@ __rproc_handle_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
316 /* 194 /*
317 * Allocate non-cacheable memory for the vring. In the future 195 * Allocate non-cacheable memory for the vring. In the future
318 * this call will also configure the IOMMU for us 196 * this call will also configure the IOMMU for us
197 * TODO: let the rproc know the da of this vring
319 */ 198 */
320 va = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL); 199 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
321 if (!va) { 200 if (!va) {
322 dev_err(dev, "dma_alloc_coherent failed\n"); 201 dev_err(dev->parent, "dma_alloc_coherent failed\n");
323 return -EINVAL; 202 return -EINVAL;
324 } 203 }
325 204
326 /* assign an rproc-wide unique index for this vring */ 205 /*
327 /* TODO: assign a notifyid for rvdev updates as well */ 206 * Assign an rproc-wide unique index for this vring
328 ret = idr_get_new(&rproc->notifyids, &rvdev->vring[i], &notifyid); 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);
329 if (ret) { 212 if (ret) {
330 dev_err(dev, "idr_get_new failed: %d\n", ret); 213 dev_err(dev, "idr_get_new failed: %d\n", ret);
331 dma_free_coherent(dev, size, va, dma); 214 dma_free_coherent(dev->parent, size, va, dma);
332 return ret; 215 return ret;
333 } 216 }
334 217
335 /* let the rproc know the da and notifyid of this vring */
336 /* TODO: expose this to remote processor */
337 vring->da = dma;
338 vring->notifyid = notifyid;
339
340 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va, 218 dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
341 dma, size, notifyid); 219 dma, size, notifyid);
342 220
343 rvdev->vring[i].len = vring->num; 221 rvring->va = va;
344 rvdev->vring[i].align = vring->align; 222 rvring->dma = dma;
345 rvdev->vring[i].va = va; 223 rvring->notifyid = notifyid;
346 rvdev->vring[i].dma = dma;
347 rvdev->vring[i].notifyid = notifyid;
348 rvdev->vring[i].rvdev = rvdev;
349 224
350 return 0; 225 return 0;
351} 226}
352 227
353static void __rproc_free_vrings(struct rproc_vdev *rvdev, int i) 228static int
229rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
354{ 230{
355 struct rproc *rproc = rvdev->rproc; 231 struct rproc *rproc = rvdev->rproc;
232 struct device *dev = &rproc->dev;
233 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
234 struct rproc_vring *rvring = &rvdev->vring[i];
235
236 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
237 i, vring->da, vring->num, vring->align);
356 238
357 for (i--; i >= 0; i--) { 239 /* make sure reserved bytes are zeroes */
358 struct rproc_vring *rvring = &rvdev->vring[i]; 240 if (vring->reserved) {
359 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 241 dev_err(dev, "vring rsc has non zero reserved bytes\n");
242 return -EINVAL;
243 }
360 244
361 dma_free_coherent(rproc->dev, size, rvring->va, rvring->dma); 245 /* verify queue size and vring alignment are sane */
362 idr_remove(&rproc->notifyids, rvring->notifyid); 246 if (!vring->num || !vring->align) {
247 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
248 vring->num, vring->align);
249 return -EINVAL;
363 } 250 }
251
252 rvring->len = vring->num;
253 rvring->align = vring->align;
254 rvring->rvdev = rvdev;
255
256 return 0;
257}
258
259void rproc_free_vring(struct rproc_vring *rvring)
260{
261 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
262 struct rproc *rproc = rvring->rvdev->rproc;
263
264 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
265 idr_remove(&rproc->notifyids, rvring->notifyid);
364} 266}
365 267
366/** 268/**
@@ -393,14 +295,14 @@ static void __rproc_free_vrings(struct rproc_vdev *rvdev, int i)
393static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 295static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
394 int avail) 296 int avail)
395{ 297{
396 struct device *dev = rproc->dev; 298 struct device *dev = &rproc->dev;
397 struct rproc_vdev *rvdev; 299 struct rproc_vdev *rvdev;
398 int i, ret; 300 int i, ret;
399 301
400 /* make sure resource isn't truncated */ 302 /* make sure resource isn't truncated */
401 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) 303 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
402 + rsc->config_len > avail) { 304 + rsc->config_len > avail) {
403 dev_err(rproc->dev, "vdev rsc is truncated\n"); 305 dev_err(dev, "vdev rsc is truncated\n");
404 return -EINVAL; 306 return -EINVAL;
405 } 307 }
406 308
@@ -425,11 +327,11 @@ static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
425 327
426 rvdev->rproc = rproc; 328 rvdev->rproc = rproc;
427 329
428 /* allocate the vrings */ 330 /* parse the vrings */
429 for (i = 0; i < rsc->num_of_vrings; i++) { 331 for (i = 0; i < rsc->num_of_vrings; i++) {
430 ret = __rproc_handle_vring(rvdev, rsc, i); 332 ret = rproc_parse_vring(rvdev, rsc, i);
431 if (ret) 333 if (ret)
432 goto free_vrings; 334 goto free_rvdev;
433 } 335 }
434 336
435 /* remember the device features */ 337 /* remember the device features */
@@ -440,12 +342,11 @@ static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
440 /* it is now safe to add the virtio device */ 342 /* it is now safe to add the virtio device */
441 ret = rproc_add_virtio_dev(rvdev, rsc->id); 343 ret = rproc_add_virtio_dev(rvdev, rsc->id);
442 if (ret) 344 if (ret)
443 goto free_vrings; 345 goto free_rvdev;
444 346
445 return 0; 347 return 0;
446 348
447free_vrings: 349free_rvdev:
448 __rproc_free_vrings(rvdev, i);
449 kfree(rvdev); 350 kfree(rvdev);
450 return ret; 351 return ret;
451} 352}
@@ -470,12 +371,12 @@ static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
470 int avail) 371 int avail)
471{ 372{
472 struct rproc_mem_entry *trace; 373 struct rproc_mem_entry *trace;
473 struct device *dev = rproc->dev; 374 struct device *dev = &rproc->dev;
474 void *ptr; 375 void *ptr;
475 char name[15]; 376 char name[15];
476 377
477 if (sizeof(*rsc) > avail) { 378 if (sizeof(*rsc) > avail) {
478 dev_err(rproc->dev, "trace rsc is truncated\n"); 379 dev_err(dev, "trace rsc is truncated\n");
479 return -EINVAL; 380 return -EINVAL;
480 } 381 }
481 382
@@ -552,6 +453,7 @@ static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
552 int avail) 453 int avail)
553{ 454{
554 struct rproc_mem_entry *mapping; 455 struct rproc_mem_entry *mapping;
456 struct device *dev = &rproc->dev;
555 int ret; 457 int ret;
556 458
557 /* no point in handling this resource without a valid iommu domain */ 459 /* no point in handling this resource without a valid iommu domain */
@@ -559,25 +461,25 @@ static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
559 return -EINVAL; 461 return -EINVAL;
560 462
561 if (sizeof(*rsc) > avail) { 463 if (sizeof(*rsc) > avail) {
562 dev_err(rproc->dev, "devmem rsc is truncated\n"); 464 dev_err(dev, "devmem rsc is truncated\n");
563 return -EINVAL; 465 return -EINVAL;
564 } 466 }
565 467
566 /* make sure reserved bytes are zeroes */ 468 /* make sure reserved bytes are zeroes */
567 if (rsc->reserved) { 469 if (rsc->reserved) {
568 dev_err(rproc->dev, "devmem rsc has non zero reserved bytes\n"); 470 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
569 return -EINVAL; 471 return -EINVAL;
570 } 472 }
571 473
572 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 474 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
573 if (!mapping) { 475 if (!mapping) {
574 dev_err(rproc->dev, "kzalloc mapping failed\n"); 476 dev_err(dev, "kzalloc mapping failed\n");
575 return -ENOMEM; 477 return -ENOMEM;
576 } 478 }
577 479
578 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 480 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
579 if (ret) { 481 if (ret) {
580 dev_err(rproc->dev, "failed to map devmem: %d\n", ret); 482 dev_err(dev, "failed to map devmem: %d\n", ret);
581 goto out; 483 goto out;
582 } 484 }
583 485
@@ -592,7 +494,7 @@ static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
592 mapping->len = rsc->len; 494 mapping->len = rsc->len;
593 list_add_tail(&mapping->node, &rproc->mappings); 495 list_add_tail(&mapping->node, &rproc->mappings);
594 496
595 dev_dbg(rproc->dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 497 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
596 rsc->pa, rsc->da, rsc->len); 498 rsc->pa, rsc->da, rsc->len);
597 499
598 return 0; 500 return 0;
@@ -624,13 +526,13 @@ static int rproc_handle_carveout(struct rproc *rproc,
624 struct fw_rsc_carveout *rsc, int avail) 526 struct fw_rsc_carveout *rsc, int avail)
625{ 527{
626 struct rproc_mem_entry *carveout, *mapping; 528 struct rproc_mem_entry *carveout, *mapping;
627 struct device *dev = rproc->dev; 529 struct device *dev = &rproc->dev;
628 dma_addr_t dma; 530 dma_addr_t dma;
629 void *va; 531 void *va;
630 int ret; 532 int ret;
631 533
632 if (sizeof(*rsc) > avail) { 534 if (sizeof(*rsc) > avail) {
633 dev_err(rproc->dev, "carveout rsc is truncated\n"); 535 dev_err(dev, "carveout rsc is truncated\n");
634 return -EINVAL; 536 return -EINVAL;
635 } 537 }
636 538
@@ -656,9 +558,9 @@ static int rproc_handle_carveout(struct rproc *rproc,
656 goto free_mapping; 558 goto free_mapping;
657 } 559 }
658 560
659 va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL); 561 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
660 if (!va) { 562 if (!va) {
661 dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len); 563 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
662 ret = -ENOMEM; 564 ret = -ENOMEM;
663 goto free_carv; 565 goto free_carv;
664 } 566 }
@@ -702,23 +604,27 @@ static int rproc_handle_carveout(struct rproc *rproc,
702 list_add_tail(&mapping->node, &rproc->mappings); 604 list_add_tail(&mapping->node, &rproc->mappings);
703 605
704 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma); 606 dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
705
706 /*
707 * Some remote processors might need to know the pa
708 * even though they are behind an IOMMU. E.g., OMAP4's
709 * remote M3 processor needs this so it can control
710 * on-chip hardware accelerators that are not behind
711 * the IOMMU, and therefor must know the pa.
712 *
713 * Generally we don't want to expose physical addresses
714 * if we don't have to (remote processors are generally
715 * _not_ trusted), so we might want to do this only for
716 * remote processor that _must_ have this (e.g. OMAP4's
717 * dual M3 subsystem).
718 */
719 rsc->pa = dma;
720 } 607 }
721 608
609 /*
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
722 carveout->va = va; 628 carveout->va = va;
723 carveout->len = rsc->len; 629 carveout->len = rsc->len;
724 carveout->dma = dma; 630 carveout->dma = dma;
@@ -729,7 +635,7 @@ static int rproc_handle_carveout(struct rproc *rproc,
729 return 0; 635 return 0;
730 636
731dma_free: 637dma_free:
732 dma_free_coherent(dev, rsc->len, va, dma); 638 dma_free_coherent(dev->parent, rsc->len, va, dma);
733free_carv: 639free_carv:
734 kfree(carveout); 640 kfree(carveout);
735free_mapping: 641free_mapping:
@@ -752,7 +658,7 @@ static rproc_handle_resource_t rproc_handle_rsc[] = {
752static int 658static int
753rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len) 659rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
754{ 660{
755 struct device *dev = rproc->dev; 661 struct device *dev = &rproc->dev;
756 rproc_handle_resource_t handler; 662 rproc_handle_resource_t handler;
757 int ret = 0, i; 663 int ret = 0, i;
758 664
@@ -791,7 +697,7 @@ rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len
791static int 697static int
792rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len) 698rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
793{ 699{
794 struct device *dev = rproc->dev; 700 struct device *dev = &rproc->dev;
795 int ret = 0, i; 701 int ret = 0, i;
796 702
797 for (i = 0; i < table->num; i++) { 703 for (i = 0; i < table->num; i++) {
@@ -822,85 +728,6 @@ rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int l
822} 728}
823 729
824/** 730/**
825 * rproc_find_rsc_table() - find the resource table
826 * @rproc: the rproc handle
827 * @elf_data: the content of the ELF firmware image
828 * @len: firmware size (in bytes)
829 * @tablesz: place holder for providing back the table size
830 *
831 * This function finds the resource table inside the remote processor's
832 * firmware. It is used both upon the registration of @rproc (in order
833 * to look for and register the supported virito devices), and when the
834 * @rproc is booted.
835 *
836 * Returns the pointer to the resource table if it is found, and write its
837 * size into @tablesz. If a valid table isn't found, NULL is returned
838 * (and @tablesz isn't set).
839 */
840static struct resource_table *
841rproc_find_rsc_table(struct rproc *rproc, const u8 *elf_data, size_t len,
842 int *tablesz)
843{
844 struct elf32_hdr *ehdr;
845 struct elf32_shdr *shdr;
846 const char *name_table;
847 struct device *dev = rproc->dev;
848 struct resource_table *table = NULL;
849 int i;
850
851 ehdr = (struct elf32_hdr *)elf_data;
852 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
853 name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
854
855 /* look for the resource table and handle it */
856 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
857 int size = shdr->sh_size;
858 int offset = shdr->sh_offset;
859
860 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
861 continue;
862
863 table = (struct resource_table *)(elf_data + offset);
864
865 /* make sure we have the entire table */
866 if (offset + size > len) {
867 dev_err(dev, "resource table truncated\n");
868 return NULL;
869 }
870
871 /* make sure table has at least the header */
872 if (sizeof(struct resource_table) > size) {
873 dev_err(dev, "header-less resource table\n");
874 return NULL;
875 }
876
877 /* we don't support any version beyond the first */
878 if (table->ver != 1) {
879 dev_err(dev, "unsupported fw ver: %d\n", table->ver);
880 return NULL;
881 }
882
883 /* make sure reserved bytes are zeroes */
884 if (table->reserved[0] || table->reserved[1]) {
885 dev_err(dev, "non zero reserved bytes\n");
886 return NULL;
887 }
888
889 /* make sure the offsets array isn't truncated */
890 if (table->num * sizeof(table->offset[0]) +
891 sizeof(struct resource_table) > size) {
892 dev_err(dev, "resource table incomplete\n");
893 return NULL;
894 }
895
896 *tablesz = shdr->sh_size;
897 break;
898 }
899
900 return table;
901}
902
903/**
904 * rproc_resource_cleanup() - clean up and free all acquired resources 731 * rproc_resource_cleanup() - clean up and free all acquired resources
905 * @rproc: rproc handle 732 * @rproc: rproc handle
906 * 733 *
@@ -910,7 +737,7 @@ rproc_find_rsc_table(struct rproc *rproc, const u8 *elf_data, size_t len,
910static void rproc_resource_cleanup(struct rproc *rproc) 737static void rproc_resource_cleanup(struct rproc *rproc)
911{ 738{
912 struct rproc_mem_entry *entry, *tmp; 739 struct rproc_mem_entry *entry, *tmp;
913 struct device *dev = rproc->dev; 740 struct device *dev = &rproc->dev;
914 741
915 /* clean up debugfs trace entries */ 742 /* clean up debugfs trace entries */
916 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) { 743 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
@@ -922,7 +749,7 @@ static void rproc_resource_cleanup(struct rproc *rproc)
922 749
923 /* clean up carveout allocations */ 750 /* clean up carveout allocations */
924 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 751 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
925 dma_free_coherent(dev, entry->len, entry->va, entry->dma); 752 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
926 list_del(&entry->node); 753 list_del(&entry->node);
927 kfree(entry); 754 kfree(entry);
928 } 755 }
@@ -943,74 +770,13 @@ static void rproc_resource_cleanup(struct rproc *rproc)
943 } 770 }
944} 771}
945 772
946/* make sure this fw image is sane */
947static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
948{
949 const char *name = rproc->firmware;
950 struct device *dev = rproc->dev;
951 struct elf32_hdr *ehdr;
952 char class;
953
954 if (!fw) {
955 dev_err(dev, "failed to load %s\n", name);
956 return -EINVAL;
957 }
958
959 if (fw->size < sizeof(struct elf32_hdr)) {
960 dev_err(dev, "Image is too small\n");
961 return -EINVAL;
962 }
963
964 ehdr = (struct elf32_hdr *)fw->data;
965
966 /* We only support ELF32 at this point */
967 class = ehdr->e_ident[EI_CLASS];
968 if (class != ELFCLASS32) {
969 dev_err(dev, "Unsupported class: %d\n", class);
970 return -EINVAL;
971 }
972
973 /* We assume the firmware has the same endianess as the host */
974# ifdef __LITTLE_ENDIAN
975 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
976# else /* BIG ENDIAN */
977 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
978# endif
979 dev_err(dev, "Unsupported firmware endianess\n");
980 return -EINVAL;
981 }
982
983 if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
984 dev_err(dev, "Image is too small\n");
985 return -EINVAL;
986 }
987
988 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
989 dev_err(dev, "Image is corrupted (bad magic)\n");
990 return -EINVAL;
991 }
992
993 if (ehdr->e_phnum == 0) {
994 dev_err(dev, "No loadable segments\n");
995 return -EINVAL;
996 }
997
998 if (ehdr->e_phoff > fw->size) {
999 dev_err(dev, "Firmware size is too small\n");
1000 return -EINVAL;
1001 }
1002
1003 return 0;
1004}
1005
1006/* 773/*
1007 * take a firmware and boot a remote processor with it. 774 * take a firmware and boot a remote processor with it.
1008 */ 775 */
1009static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 776static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1010{ 777{
1011 struct device *dev = rproc->dev; 778 struct device *dev = &rproc->dev;
1012 const char *name = rproc->firmware; 779 const char *name = rproc->firmware;
1013 struct elf32_hdr *ehdr;
1014 struct resource_table *table; 780 struct resource_table *table;
1015 int ret, tablesz; 781 int ret, tablesz;
1016 782
@@ -1018,8 +784,6 @@ static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1018 if (ret) 784 if (ret)
1019 return ret; 785 return ret;
1020 786
1021 ehdr = (struct elf32_hdr *)fw->data;
1022
1023 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 787 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1024 788
1025 /* 789 /*
@@ -1032,15 +796,10 @@ static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1032 return ret; 796 return ret;
1033 } 797 }
1034 798
1035 /* 799 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1036 * The ELF entry point is the rproc's boot addr (though this is not
1037 * a configurable property of all remote processors: some will always
1038 * boot at a specific hardcoded address).
1039 */
1040 rproc->bootaddr = ehdr->e_entry;
1041 800
1042 /* look for the resource table */ 801 /* look for the resource table */
1043 table = rproc_find_rsc_table(rproc, fw->data, fw->size, &tablesz); 802 table = rproc_find_rsc_table(rproc, fw, &tablesz);
1044 if (!table) { 803 if (!table) {
1045 ret = -EINVAL; 804 ret = -EINVAL;
1046 goto clean_up; 805 goto clean_up;
@@ -1054,7 +813,7 @@ static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1054 } 813 }
1055 814
1056 /* load the ELF segments to memory */ 815 /* load the ELF segments to memory */
1057 ret = rproc_load_segments(rproc, fw->data, fw->size); 816 ret = rproc_load_segments(rproc, fw);
1058 if (ret) { 817 if (ret) {
1059 dev_err(dev, "Failed to load program segments: %d\n", ret); 818 dev_err(dev, "Failed to load program segments: %d\n", ret);
1060 goto clean_up; 819 goto clean_up;
@@ -1097,7 +856,7 @@ static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
1097 goto out; 856 goto out;
1098 857
1099 /* look for the resource table */ 858 /* look for the resource table */
1100 table = rproc_find_rsc_table(rproc, fw->data, fw->size, &tablesz); 859 table = rproc_find_rsc_table(rproc, fw, &tablesz);
1101 if (!table) 860 if (!table)
1102 goto out; 861 goto out;
1103 862
@@ -1108,7 +867,7 @@ static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
1108 867
1109out: 868out:
1110 release_firmware(fw); 869 release_firmware(fw);
1111 /* allow rproc_unregister() contexts, if any, to proceed */ 870 /* allow rproc_del() contexts, if any, to proceed */
1112 complete_all(&rproc->firmware_loading_complete); 871 complete_all(&rproc->firmware_loading_complete);
1113} 872}
1114 873
@@ -1134,7 +893,7 @@ int rproc_boot(struct rproc *rproc)
1134 return -EINVAL; 893 return -EINVAL;
1135 } 894 }
1136 895
1137 dev = rproc->dev; 896 dev = &rproc->dev;
1138 897
1139 ret = mutex_lock_interruptible(&rproc->lock); 898 ret = mutex_lock_interruptible(&rproc->lock);
1140 if (ret) { 899 if (ret) {
@@ -1150,7 +909,7 @@ int rproc_boot(struct rproc *rproc)
1150 } 909 }
1151 910
1152 /* prevent underlying implementation from being removed */ 911 /* prevent underlying implementation from being removed */
1153 if (!try_module_get(dev->driver->owner)) { 912 if (!try_module_get(dev->parent->driver->owner)) {
1154 dev_err(dev, "%s: can't get owner\n", __func__); 913 dev_err(dev, "%s: can't get owner\n", __func__);
1155 ret = -EINVAL; 914 ret = -EINVAL;
1156 goto unlock_mutex; 915 goto unlock_mutex;
@@ -1177,7 +936,7 @@ int rproc_boot(struct rproc *rproc)
1177 936
1178downref_rproc: 937downref_rproc:
1179 if (ret) { 938 if (ret) {
1180 module_put(dev->driver->owner); 939 module_put(dev->parent->driver->owner);
1181 atomic_dec(&rproc->power); 940 atomic_dec(&rproc->power);
1182 } 941 }
1183unlock_mutex: 942unlock_mutex:
@@ -1204,14 +963,10 @@ EXPORT_SYMBOL(rproc_boot);
1204 * which means that the @rproc handle stays valid even after rproc_shutdown() 963 * which means that the @rproc handle stays valid even after rproc_shutdown()
1205 * returns, and users can still use it with a subsequent rproc_boot(), if 964 * returns, and users can still use it with a subsequent rproc_boot(), if
1206 * needed. 965 * needed.
1207 * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
1208 * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
1209 * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
1210 * you acquired @rproc using rproc_get_by_name()).
1211 */ 966 */
1212void rproc_shutdown(struct rproc *rproc) 967void rproc_shutdown(struct rproc *rproc)
1213{ 968{
1214 struct device *dev = rproc->dev; 969 struct device *dev = &rproc->dev;
1215 int ret; 970 int ret;
1216 971
1217 ret = mutex_lock_interruptible(&rproc->lock); 972 ret = mutex_lock_interruptible(&rproc->lock);
@@ -1244,148 +999,12 @@ void rproc_shutdown(struct rproc *rproc)
1244out: 999out:
1245 mutex_unlock(&rproc->lock); 1000 mutex_unlock(&rproc->lock);
1246 if (!ret) 1001 if (!ret)
1247 module_put(dev->driver->owner); 1002 module_put(dev->parent->driver->owner);
1248} 1003}
1249EXPORT_SYMBOL(rproc_shutdown); 1004EXPORT_SYMBOL(rproc_shutdown);
1250 1005
1251/** 1006/**
1252 * rproc_release() - completely deletes the existence of a remote processor 1007 * rproc_add() - register a remote processor
1253 * @kref: the rproc's kref
1254 *
1255 * This function should _never_ be called directly.
1256 *
1257 * The only reasonable location to use it is as an argument when kref_put'ing
1258 * @rproc's refcount.
1259 *
1260 * This way it will be called when no one holds a valid pointer to this @rproc
1261 * anymore (and obviously after it is removed from the rprocs klist).
1262 *
1263 * Note: this function is not static because rproc_vdev_release() needs it when
1264 * it decrements @rproc's refcount.
1265 */
1266void rproc_release(struct kref *kref)
1267{
1268 struct rproc *rproc = container_of(kref, struct rproc, refcount);
1269 struct rproc_vdev *rvdev, *rvtmp;
1270
1271 dev_info(rproc->dev, "removing %s\n", rproc->name);
1272
1273 rproc_delete_debug_dir(rproc);
1274
1275 /* clean up remote vdev entries */
1276 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) {
1277 __rproc_free_vrings(rvdev, RVDEV_NUM_VRINGS);
1278 list_del(&rvdev->node);
1279 }
1280
1281 /*
1282 * At this point no one holds a reference to rproc anymore,
1283 * so we can directly unroll rproc_alloc()
1284 */
1285 rproc_free(rproc);
1286}
1287
1288/* will be called when an rproc is added to the rprocs klist */
1289static void klist_rproc_get(struct klist_node *n)
1290{
1291 struct rproc *rproc = container_of(n, struct rproc, node);
1292
1293 kref_get(&rproc->refcount);
1294}
1295
1296/* will be called when an rproc is removed from the rprocs klist */
1297static void klist_rproc_put(struct klist_node *n)
1298{
1299 struct rproc *rproc = container_of(n, struct rproc, node);
1300
1301 kref_put(&rproc->refcount, rproc_release);
1302}
1303
1304static struct rproc *next_rproc(struct klist_iter *i)
1305{
1306 struct klist_node *n;
1307
1308 n = klist_next(i);
1309 if (!n)
1310 return NULL;
1311
1312 return container_of(n, struct rproc, node);
1313}
1314
1315/**
1316 * rproc_get_by_name() - find a remote processor by name and boot it
1317 * @name: name of the remote processor
1318 *
1319 * Finds an rproc handle using the remote processor's name, and then
1320 * boot it. If it's already powered on, then just immediately return
1321 * (successfully).
1322 *
1323 * Returns the rproc handle on success, and NULL on failure.
1324 *
1325 * This function increments the remote processor's refcount, so always
1326 * use rproc_put() to decrement it back once rproc isn't needed anymore.
1327 *
1328 * Note: currently this function (and its counterpart rproc_put()) are not
1329 * being used. We need to scrutinize the use cases
1330 * that still need them, and see if we can migrate them to use the non
1331 * name-based boot/shutdown interface.
1332 */
1333struct rproc *rproc_get_by_name(const char *name)
1334{
1335 struct rproc *rproc;
1336 struct klist_iter i;
1337 int ret;
1338
1339 /* find the remote processor, and upref its refcount */
1340 klist_iter_init(&rprocs, &i);
1341 while ((rproc = next_rproc(&i)) != NULL)
1342 if (!strcmp(rproc->name, name)) {
1343 kref_get(&rproc->refcount);
1344 break;
1345 }
1346 klist_iter_exit(&i);
1347
1348 /* can't find this rproc ? */
1349 if (!rproc) {
1350 pr_err("can't find remote processor %s\n", name);
1351 return NULL;
1352 }
1353
1354 ret = rproc_boot(rproc);
1355 if (ret < 0) {
1356 kref_put(&rproc->refcount, rproc_release);
1357 return NULL;
1358 }
1359
1360 return rproc;
1361}
1362EXPORT_SYMBOL(rproc_get_by_name);
1363
1364/**
1365 * rproc_put() - decrement the refcount of a remote processor, and shut it down
1366 * @rproc: the remote processor
1367 *
1368 * This function tries to shutdown @rproc, and it then decrements its
1369 * refcount.
1370 *
1371 * After this function returns, @rproc may _not_ be used anymore, and its
1372 * handle should be considered invalid.
1373 *
1374 * This function should be called _iff_ the @rproc handle was grabbed by
1375 * calling rproc_get_by_name().
1376 */
1377void rproc_put(struct rproc *rproc)
1378{
1379 /* try to power off the remote processor */
1380 rproc_shutdown(rproc);
1381
1382 /* downref rproc's refcount */
1383 kref_put(&rproc->refcount, rproc_release);
1384}
1385EXPORT_SYMBOL(rproc_put);
1386
1387/**
1388 * rproc_register() - register a remote processor
1389 * @rproc: the remote processor handle to register 1008 * @rproc: the remote processor handle to register
1390 * 1009 *
1391 * Registers @rproc with the remoteproc framework, after it has been 1010 * Registers @rproc with the remoteproc framework, after it has been
@@ -1404,15 +1023,16 @@ EXPORT_SYMBOL(rproc_put);
1404 * of registering this remote processor, additional virtio drivers might be 1023 * of registering this remote processor, additional virtio drivers might be
1405 * probed. 1024 * probed.
1406 */ 1025 */
1407int rproc_register(struct rproc *rproc) 1026int rproc_add(struct rproc *rproc)
1408{ 1027{
1409 struct device *dev = rproc->dev; 1028 struct device *dev = &rproc->dev;
1410 int ret = 0; 1029 int ret = 0;
1411 1030
1412 /* expose to rproc_get_by_name users */ 1031 ret = device_add(dev);
1413 klist_add_tail(&rproc->node, &rprocs); 1032 if (ret < 0)
1033 return ret;
1414 1034
1415 dev_info(rproc->dev, "%s is available\n", rproc->name); 1035 dev_info(dev, "%s is available\n", rproc->name);
1416 1036
1417 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n"); 1037 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1418 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n"); 1038 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
@@ -1420,7 +1040,7 @@ int rproc_register(struct rproc *rproc)
1420 /* create debugfs entries */ 1040 /* create debugfs entries */
1421 rproc_create_debug_dir(rproc); 1041 rproc_create_debug_dir(rproc);
1422 1042
1423 /* rproc_unregister() calls must wait until async loader completes */ 1043 /* rproc_del() calls must wait until async loader completes */
1424 init_completion(&rproc->firmware_loading_complete); 1044 init_completion(&rproc->firmware_loading_complete);
1425 1045
1426 /* 1046 /*
@@ -1437,12 +1057,42 @@ int rproc_register(struct rproc *rproc)
1437 if (ret < 0) { 1057 if (ret < 0) {
1438 dev_err(dev, "request_firmware_nowait failed: %d\n", ret); 1058 dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
1439 complete_all(&rproc->firmware_loading_complete); 1059 complete_all(&rproc->firmware_loading_complete);
1440 klist_remove(&rproc->node);
1441 } 1060 }
1442 1061
1443 return ret; 1062 return ret;
1444} 1063}
1445EXPORT_SYMBOL(rproc_register); 1064EXPORT_SYMBOL(rproc_add);
1065
1066/**
1067 * 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
1079 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1080
1081 rproc_delete_debug_dir(rproc);
1082
1083 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};
1446 1096
1447/** 1097/**
1448 * rproc_alloc() - allocate a remote processor handle 1098 * rproc_alloc() - allocate a remote processor handle
@@ -1459,13 +1109,13 @@ EXPORT_SYMBOL(rproc_register);
1459 * of the remote processor. 1109 * of the remote processor.
1460 * 1110 *
1461 * After creating an rproc handle using this function, and when ready, 1111 * After creating an rproc handle using this function, and when ready,
1462 * implementations should then call rproc_register() to complete 1112 * implementations should then call rproc_add() to complete
1463 * the registration of the remote processor. 1113 * the registration of the remote processor.
1464 * 1114 *
1465 * On success the new rproc is returned, and on failure, NULL. 1115 * On success the new rproc is returned, and on failure, NULL.
1466 * 1116 *
1467 * Note: _never_ directly deallocate @rproc, even if it was not registered 1117 * Note: _never_ directly deallocate @rproc, even if it was not registered
1468 * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free(). 1118 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1469 */ 1119 */
1470struct rproc *rproc_alloc(struct device *dev, const char *name, 1120struct rproc *rproc_alloc(struct device *dev, const char *name,
1471 const struct rproc_ops *ops, 1121 const struct rproc_ops *ops,
@@ -1482,15 +1132,29 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
1482 return NULL; 1132 return NULL;
1483 } 1133 }
1484 1134
1485 rproc->dev = dev;
1486 rproc->name = name; 1135 rproc->name = name;
1487 rproc->ops = ops; 1136 rproc->ops = ops;
1488 rproc->firmware = firmware; 1137 rproc->firmware = firmware;
1489 rproc->priv = &rproc[1]; 1138 rproc->priv = &rproc[1];
1490 1139
1140 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
1491 atomic_set(&rproc->power, 0); 1154 atomic_set(&rproc->power, 0);
1492 1155
1493 kref_init(&rproc->refcount); 1156 /* Set ELF as the default fw_ops handler */
1157 rproc->fw_ops = &rproc_elf_fw_ops;
1494 1158
1495 mutex_init(&rproc->lock); 1159 mutex_init(&rproc->lock);
1496 1160
@@ -1508,47 +1172,38 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
1508EXPORT_SYMBOL(rproc_alloc); 1172EXPORT_SYMBOL(rproc_alloc);
1509 1173
1510/** 1174/**
1511 * rproc_free() - free an rproc handle that was allocated by rproc_alloc 1175 * rproc_put() - unroll rproc_alloc()
1512 * @rproc: the remote processor handle 1176 * @rproc: the remote processor handle
1513 * 1177 *
1514 * This function should _only_ be used if @rproc was only allocated, 1178 * This function decrements the rproc dev refcount.
1515 * but not registered yet.
1516 * 1179 *
1517 * If @rproc was already successfully registered (by calling rproc_register()), 1180 * If no one holds any reference to rproc anymore, then its refcount would
1518 * then use rproc_unregister() instead. 1181 * now drop to zero, and it would be freed.
1519 */ 1182 */
1520void rproc_free(struct rproc *rproc) 1183void rproc_put(struct rproc *rproc)
1521{ 1184{
1522 idr_remove_all(&rproc->notifyids); 1185 put_device(&rproc->dev);
1523 idr_destroy(&rproc->notifyids);
1524
1525 kfree(rproc);
1526} 1186}
1527EXPORT_SYMBOL(rproc_free); 1187EXPORT_SYMBOL(rproc_put);
1528 1188
1529/** 1189/**
1530 * rproc_unregister() - unregister a remote processor 1190 * rproc_del() - unregister a remote processor
1531 * @rproc: rproc handle to unregister 1191 * @rproc: rproc handle to unregister
1532 * 1192 *
1533 * Unregisters a remote processor, and decrements its refcount.
1534 * If its refcount drops to zero, then @rproc will be freed. If not,
1535 * it will be freed later once the last reference is dropped.
1536 *
1537 * This function should be called when the platform specific rproc 1193 * This function should be called when the platform specific rproc
1538 * implementation decides to remove the rproc device. it should 1194 * implementation decides to remove the rproc device. it should
1539 * _only_ be called if a previous invocation of rproc_register() 1195 * _only_ be called if a previous invocation of rproc_add()
1540 * has completed successfully. 1196 * has completed successfully.
1541 * 1197 *
1542 * After rproc_unregister() returns, @rproc is _not_ valid anymore and 1198 * After rproc_del() returns, @rproc isn't freed yet, because
1543 * it shouldn't be used. More specifically, don't call rproc_free() 1199 * of the outstanding reference created by rproc_alloc. To decrement that
1544 * or try to directly free @rproc after rproc_unregister() returns; 1200 * one last refcount, one still needs to call rproc_put().
1545 * none of these are needed, and calling them is a bug.
1546 * 1201 *
1547 * Returns 0 on success and -EINVAL if @rproc isn't valid. 1202 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1548 */ 1203 */
1549int rproc_unregister(struct rproc *rproc) 1204int rproc_del(struct rproc *rproc)
1550{ 1205{
1551 struct rproc_vdev *rvdev; 1206 struct rproc_vdev *rvdev, *tmp;
1552 1207
1553 if (!rproc) 1208 if (!rproc)
1554 return -EINVAL; 1209 return -EINVAL;
@@ -1557,22 +1212,19 @@ int rproc_unregister(struct rproc *rproc)
1557 wait_for_completion(&rproc->firmware_loading_complete); 1212 wait_for_completion(&rproc->firmware_loading_complete);
1558 1213
1559 /* clean up remote vdev entries */ 1214 /* clean up remote vdev entries */
1560 list_for_each_entry(rvdev, &rproc->rvdevs, node) 1215 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
1561 rproc_remove_virtio_dev(rvdev); 1216 rproc_remove_virtio_dev(rvdev);
1562 1217
1563 /* the rproc is downref'ed as soon as it's removed from the klist */ 1218 device_del(&rproc->dev);
1564 klist_del(&rproc->node);
1565
1566 /* the rproc will only be released after its refcount drops to zero */
1567 kref_put(&rproc->refcount, rproc_release);
1568 1219
1569 return 0; 1220 return 0;
1570} 1221}
1571EXPORT_SYMBOL(rproc_unregister); 1222EXPORT_SYMBOL(rproc_del);
1572 1223
1573static int __init remoteproc_init(void) 1224static int __init remoteproc_init(void)
1574{ 1225{
1575 rproc_init_debugfs(); 1226 rproc_init_debugfs();
1227
1576 return 0; 1228 return 0;
1577} 1229}
1578module_init(remoteproc_init); 1230module_init(remoteproc_init);
diff --git a/drivers/remoteproc/remoteproc_debugfs.c b/drivers/remoteproc/remoteproc_debugfs.c
index 85d31a69e117..03833850f214 100644
--- a/drivers/remoteproc/remoteproc_debugfs.c
+++ b/drivers/remoteproc/remoteproc_debugfs.c
@@ -124,7 +124,7 @@ struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
124 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, 124 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir,
125 trace, &trace_rproc_ops); 125 trace, &trace_rproc_ops);
126 if (!tfile) { 126 if (!tfile) {
127 dev_err(rproc->dev, "failed to create debugfs trace entry\n"); 127 dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
128 return NULL; 128 return NULL;
129 } 129 }
130 130
@@ -141,7 +141,7 @@ void rproc_delete_debug_dir(struct rproc *rproc)
141 141
142void rproc_create_debug_dir(struct rproc *rproc) 142void rproc_create_debug_dir(struct rproc *rproc)
143{ 143{
144 struct device *dev = rproc->dev; 144 struct device *dev = &rproc->dev;
145 145
146 if (!rproc_dbg) 146 if (!rproc_dbg)
147 return; 147 return;
diff --git a/drivers/remoteproc/remoteproc_elf_loader.c b/drivers/remoteproc/remoteproc_elf_loader.c
new file mode 100644
index 000000000000..e1f89d649733
--- /dev/null
+++ b/drivers/remoteproc/remoteproc_elf_loader.c
@@ -0,0 +1,295 @@
1/*
2 * Remote Processor Framework Elf loader
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 * Sjur Brændeland <sjur.brandeland@stericsson.com>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * version 2 as published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#define pr_fmt(fmt) "%s: " fmt, __func__
27
28#include <linux/module.h>
29#include <linux/firmware.h>
30#include <linux/remoteproc.h>
31#include <linux/elf.h>
32
33#include "remoteproc_internal.h"
34
35/**
36 * rproc_elf_sanity_check() - Sanity Check ELF firmware image
37 * @rproc: the remote processor handle
38 * @fw: the ELF firmware image
39 *
40 * Make sure this fw image is sane.
41 */
42static int
43rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
44{
45 const char *name = rproc->firmware;
46 struct device *dev = &rproc->dev;
47 struct elf32_hdr *ehdr;
48 char class;
49
50 if (!fw) {
51 dev_err(dev, "failed to load %s\n", name);
52 return -EINVAL;
53 }
54
55 if (fw->size < sizeof(struct elf32_hdr)) {
56 dev_err(dev, "Image is too small\n");
57 return -EINVAL;
58 }
59
60 ehdr = (struct elf32_hdr *)fw->data;
61
62 /* We only support ELF32 at this point */
63 class = ehdr->e_ident[EI_CLASS];
64 if (class != ELFCLASS32) {
65 dev_err(dev, "Unsupported class: %d\n", class);
66 return -EINVAL;
67 }
68
69 /* We assume the firmware has the same endianess as the host */
70# ifdef __LITTLE_ENDIAN
71 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
72# else /* BIG ENDIAN */
73 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
74# endif
75 dev_err(dev, "Unsupported firmware endianess\n");
76 return -EINVAL;
77 }
78
79 if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
80 dev_err(dev, "Image is too small\n");
81 return -EINVAL;
82 }
83
84 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
85 dev_err(dev, "Image is corrupted (bad magic)\n");
86 return -EINVAL;
87 }
88
89 if (ehdr->e_phnum == 0) {
90 dev_err(dev, "No loadable segments\n");
91 return -EINVAL;
92 }
93
94 if (ehdr->e_phoff > fw->size) {
95 dev_err(dev, "Firmware size is too small\n");
96 return -EINVAL;
97 }
98
99 return 0;
100}
101
102/**
103 * rproc_elf_get_boot_addr() - Get rproc's boot address.
104 * @rproc: the remote processor handle
105 * @fw: the ELF firmware image
106 *
107 * This function returns the entry point address of the ELF
108 * image.
109 *
110 * Note that the boot address is not a configurable property of all remote
111 * processors. Some will always boot at a specific hard-coded address.
112 */
113static
114u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
115{
116 struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
117
118 return ehdr->e_entry;
119}
120
121/**
122 * rproc_elf_load_segments() - load firmware segments to memory
123 * @rproc: remote processor which will be booted using these fw segments
124 * @fw: the ELF firmware image
125 *
126 * This function loads the firmware segments to memory, where the remote
127 * processor expects them.
128 *
129 * Some remote processors will expect their code and data to be placed
130 * in specific device addresses, and can't have them dynamically assigned.
131 *
132 * We currently support only those kind of remote processors, and expect
133 * the program header's paddr member to contain those addresses. We then go
134 * through the physically contiguous "carveout" memory regions which we
135 * allocated (and mapped) earlier on behalf of the remote processor,
136 * and "translate" device address to kernel addresses, so we can copy the
137 * segments where they are expected.
138 *
139 * Currently we only support remote processors that required carveout
140 * allocations and got them mapped onto their iommus. Some processors
141 * might be different: they might not have iommus, and would prefer to
142 * directly allocate memory for every segment/resource. This is not yet
143 * supported, though.
144 */
145static int
146rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
147{
148 struct device *dev = &rproc->dev;
149 struct elf32_hdr *ehdr;
150 struct elf32_phdr *phdr;
151 int i, ret = 0;
152 const u8 *elf_data = fw->data;
153
154 ehdr = (struct elf32_hdr *)elf_data;
155 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
156
157 /* go through the available ELF segments */
158 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
159 u32 da = phdr->p_paddr;
160 u32 memsz = phdr->p_memsz;
161 u32 filesz = phdr->p_filesz;
162 u32 offset = phdr->p_offset;
163 void *ptr;
164
165 if (phdr->p_type != PT_LOAD)
166 continue;
167
168 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
169 phdr->p_type, da, memsz, filesz);
170
171 if (filesz > memsz) {
172 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
173 filesz, memsz);
174 ret = -EINVAL;
175 break;
176 }
177
178 if (offset + filesz > fw->size) {
179 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
180 offset + filesz, fw->size);
181 ret = -EINVAL;
182 break;
183 }
184
185 /* grab the kernel address for this device address */
186 ptr = rproc_da_to_va(rproc, da, memsz);
187 if (!ptr) {
188 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
189 ret = -EINVAL;
190 break;
191 }
192
193 /* put the segment where the remote processor expects it */
194 if (phdr->p_filesz)
195 memcpy(ptr, elf_data + phdr->p_offset, filesz);
196
197 /*
198 * Zero out remaining memory for this segment.
199 *
200 * This isn't strictly required since dma_alloc_coherent already
201 * did this for us. albeit harmless, we may consider removing
202 * this.
203 */
204 if (memsz > filesz)
205 memset(ptr + filesz, 0, memsz - filesz);
206 }
207
208 return ret;
209}
210
211/**
212 * rproc_elf_find_rsc_table() - find the resource table
213 * @rproc: the rproc handle
214 * @fw: the ELF firmware image
215 * @tablesz: place holder for providing back the table size
216 *
217 * This function finds the resource table inside the remote processor's
218 * firmware. It is used both upon the registration of @rproc (in order
219 * to look for and register the supported virito devices), and when the
220 * @rproc is booted.
221 *
222 * Returns the pointer to the resource table if it is found, and write its
223 * size into @tablesz. If a valid table isn't found, NULL is returned
224 * (and @tablesz isn't set).
225 */
226static struct resource_table *
227rproc_elf_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
228 int *tablesz)
229{
230 struct elf32_hdr *ehdr;
231 struct elf32_shdr *shdr;
232 const char *name_table;
233 struct device *dev = &rproc->dev;
234 struct resource_table *table = NULL;
235 int i;
236 const u8 *elf_data = fw->data;
237
238 ehdr = (struct elf32_hdr *)elf_data;
239 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
240 name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
241
242 /* look for the resource table and handle it */
243 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
244 int size = shdr->sh_size;
245 int offset = shdr->sh_offset;
246
247 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
248 continue;
249
250 table = (struct resource_table *)(elf_data + offset);
251
252 /* make sure we have the entire table */
253 if (offset + size > fw->size) {
254 dev_err(dev, "resource table truncated\n");
255 return NULL;
256 }
257
258 /* make sure table has at least the header */
259 if (sizeof(struct resource_table) > size) {
260 dev_err(dev, "header-less resource table\n");
261 return NULL;
262 }
263
264 /* we don't support any version beyond the first */
265 if (table->ver != 1) {
266 dev_err(dev, "unsupported fw ver: %d\n", table->ver);
267 return NULL;
268 }
269
270 /* make sure reserved bytes are zeroes */
271 if (table->reserved[0] || table->reserved[1]) {
272 dev_err(dev, "non zero reserved bytes\n");
273 return NULL;
274 }
275
276 /* make sure the offsets array isn't truncated */
277 if (table->num * sizeof(table->offset[0]) +
278 sizeof(struct resource_table) > size) {
279 dev_err(dev, "resource table incomplete\n");
280 return NULL;
281 }
282
283 *tablesz = shdr->sh_size;
284 break;
285 }
286
287 return table;
288}
289
290const struct rproc_fw_ops rproc_elf_fw_ops = {
291 .load = rproc_elf_load_segments,
292 .find_rsc_table = rproc_elf_find_rsc_table,
293 .sanity_check = rproc_elf_sanity_check,
294 .get_boot_addr = rproc_elf_get_boot_addr
295};
diff --git a/drivers/remoteproc/remoteproc_internal.h b/drivers/remoteproc/remoteproc_internal.h
index 9f336d6bdef3..a690ebe7aa51 100644
--- a/drivers/remoteproc/remoteproc_internal.h
+++ b/drivers/remoteproc/remoteproc_internal.h
@@ -21,9 +21,27 @@
21#define REMOTEPROC_INTERNAL_H 21#define REMOTEPROC_INTERNAL_H
22 22
23#include <linux/irqreturn.h> 23#include <linux/irqreturn.h>
24#include <linux/firmware.h>
24 25
25struct rproc; 26struct rproc;
26 27
28/**
29 * struct rproc_fw_ops - firmware format specific operations.
30 * @find_rsc_table: finds the resource table inside the firmware image
31 * @load: load firmeware to memory, where the remote processor
32 * expects to find it
33 * @sanity_check: sanity check the fw image
34 * @get_boot_addr: get boot address to entry point specified in firmware
35 */
36struct rproc_fw_ops {
37 struct resource_table *(*find_rsc_table) (struct rproc *rproc,
38 const struct firmware *fw,
39 int *tablesz);
40 int (*load)(struct rproc *rproc, const struct firmware *fw);
41 int (*sanity_check)(struct rproc *rproc, const struct firmware *fw);
42 u32 (*get_boot_addr)(struct rproc *rproc, const struct firmware *fw);
43};
44
27/* from remoteproc_core.c */ 45/* from remoteproc_core.c */
28void rproc_release(struct kref *kref); 46void rproc_release(struct kref *kref);
29irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int vq_id); 47irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int vq_id);
@@ -41,4 +59,48 @@ void rproc_create_debug_dir(struct rproc *rproc);
41void rproc_init_debugfs(void); 59void rproc_init_debugfs(void);
42void rproc_exit_debugfs(void); 60void rproc_exit_debugfs(void);
43 61
62void rproc_free_vring(struct rproc_vring *rvring);
63int rproc_alloc_vring(struct rproc_vdev *rvdev, int i);
64
65void *rproc_da_to_va(struct rproc *rproc, u64 da, int len);
66
67static inline
68int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
69{
70 if (rproc->fw_ops->sanity_check)
71 return rproc->fw_ops->sanity_check(rproc, fw);
72
73 return 0;
74}
75
76static inline
77u32 rproc_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
78{
79 if (rproc->fw_ops->get_boot_addr)
80 return rproc->fw_ops->get_boot_addr(rproc, fw);
81
82 return 0;
83}
84
85static inline
86int rproc_load_segments(struct rproc *rproc, const struct firmware *fw)
87{
88 if (rproc->fw_ops->load)
89 return rproc->fw_ops->load(rproc, fw);
90
91 return -EINVAL;
92}
93
94static inline
95struct resource_table *rproc_find_rsc_table(struct rproc *rproc,
96 const struct firmware *fw, int *tablesz)
97{
98 if (rproc->fw_ops->find_rsc_table)
99 return rproc->fw_ops->find_rsc_table(rproc, fw, tablesz);
100
101 return NULL;
102}
103
104extern const struct rproc_fw_ops rproc_elf_fw_ops;
105
44#endif /* REMOTEPROC_INTERNAL_H */ 106#endif /* REMOTEPROC_INTERNAL_H */
diff --git a/drivers/remoteproc/remoteproc_virtio.c b/drivers/remoteproc/remoteproc_virtio.c
index ecf612130750..3541b4492f64 100644
--- a/drivers/remoteproc/remoteproc_virtio.c
+++ b/drivers/remoteproc/remoteproc_virtio.c
@@ -36,7 +36,7 @@ static void rproc_virtio_notify(struct virtqueue *vq)
36 struct rproc *rproc = rvring->rvdev->rproc; 36 struct rproc *rproc = rvring->rvdev->rproc;
37 int notifyid = rvring->notifyid; 37 int notifyid = rvring->notifyid;
38 38
39 dev_dbg(rproc->dev, "kicking vq index: %d\n", notifyid); 39 dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
40 40
41 rproc->ops->kick(rproc, notifyid); 41 rproc->ops->kick(rproc, notifyid);
42} 42}
@@ -57,7 +57,7 @@ irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
57{ 57{
58 struct rproc_vring *rvring; 58 struct rproc_vring *rvring;
59 59
60 dev_dbg(rproc->dev, "vq index %d is interrupted\n", notifyid); 60 dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
61 61
62 rvring = idr_find(&rproc->notifyids, notifyid); 62 rvring = idr_find(&rproc->notifyids, notifyid);
63 if (!rvring || !rvring->vq) 63 if (!rvring || !rvring->vq)
@@ -74,17 +74,21 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
74{ 74{
75 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 75 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
76 struct rproc *rproc = vdev_to_rproc(vdev); 76 struct rproc *rproc = vdev_to_rproc(vdev);
77 struct device *dev = &rproc->dev;
77 struct rproc_vring *rvring; 78 struct rproc_vring *rvring;
78 struct virtqueue *vq; 79 struct virtqueue *vq;
79 void *addr; 80 void *addr;
80 int len, size; 81 int len, size, ret;
81 82
82 /* we're temporarily limited to two virtqueues per rvdev */ 83 /* we're temporarily limited to two virtqueues per rvdev */
83 if (id >= ARRAY_SIZE(rvdev->vring)) 84 if (id >= ARRAY_SIZE(rvdev->vring))
84 return ERR_PTR(-EINVAL); 85 return ERR_PTR(-EINVAL);
85 86
86 rvring = &rvdev->vring[id]; 87 ret = rproc_alloc_vring(rvdev, id);
88 if (ret)
89 return ERR_PTR(ret);
87 90
91 rvring = &rvdev->vring[id];
88 addr = rvring->va; 92 addr = rvring->va;
89 len = rvring->len; 93 len = rvring->len;
90 94
@@ -92,7 +96,7 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
92 size = vring_size(len, rvring->align); 96 size = vring_size(len, rvring->align);
93 memset(addr, 0, size); 97 memset(addr, 0, size);
94 98
95 dev_dbg(rproc->dev, "vring%d: va %p qsz %d notifyid %d\n", 99 dev_dbg(dev, "vring%d: va %p qsz %d notifyid %d\n",
96 id, addr, len, rvring->notifyid); 100 id, addr, len, rvring->notifyid);
97 101
98 /* 102 /*
@@ -102,7 +106,8 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
102 vq = vring_new_virtqueue(len, rvring->align, vdev, false, addr, 106 vq = vring_new_virtqueue(len, rvring->align, vdev, false, addr,
103 rproc_virtio_notify, callback, name); 107 rproc_virtio_notify, callback, name);
104 if (!vq) { 108 if (!vq) {
105 dev_err(rproc->dev, "vring_new_virtqueue %s failed\n", name); 109 dev_err(dev, "vring_new_virtqueue %s failed\n", name);
110 rproc_free_vring(rvring);
106 return ERR_PTR(-ENOMEM); 111 return ERR_PTR(-ENOMEM);
107 } 112 }
108 113
@@ -125,6 +130,7 @@ static void rproc_virtio_del_vqs(struct virtio_device *vdev)
125 rvring = vq->priv; 130 rvring = vq->priv;
126 rvring->vq = NULL; 131 rvring->vq = NULL;
127 vring_del_virtqueue(vq); 132 vring_del_virtqueue(vq);
133 rproc_free_vring(rvring);
128 } 134 }
129} 135}
130 136
@@ -147,7 +153,7 @@ static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
147 /* now that the vqs are all set, boot the remote processor */ 153 /* now that the vqs are all set, boot the remote processor */
148 ret = rproc_boot(rproc); 154 ret = rproc_boot(rproc);
149 if (ret) { 155 if (ret) {
150 dev_err(rproc->dev, "rproc_boot() failed %d\n", ret); 156 dev_err(&rproc->dev, "rproc_boot() failed %d\n", ret);
151 goto error; 157 goto error;
152 } 158 }
153 159
@@ -219,7 +225,7 @@ static struct virtio_config_ops rproc_virtio_config_ops = {
219 225
220/* 226/*
221 * This function is called whenever vdev is released, and is responsible 227 * This function is called whenever vdev is released, and is responsible
222 * to decrement the remote processor's refcount taken when vdev was 228 * to decrement the remote processor's refcount which was taken when vdev was
223 * added. 229 * added.
224 * 230 *
225 * Never call this function directly; it will be called by the driver 231 * Never call this function directly; it will be called by the driver
@@ -228,9 +234,13 @@ static struct virtio_config_ops rproc_virtio_config_ops = {
228static void rproc_vdev_release(struct device *dev) 234static void rproc_vdev_release(struct device *dev)
229{ 235{
230 struct virtio_device *vdev = dev_to_virtio(dev); 236 struct virtio_device *vdev = dev_to_virtio(dev);
237 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
231 struct rproc *rproc = vdev_to_rproc(vdev); 238 struct rproc *rproc = vdev_to_rproc(vdev);
232 239
233 kref_put(&rproc->refcount, rproc_release); 240 list_del(&rvdev->node);
241 kfree(rvdev);
242
243 put_device(&rproc->dev);
234} 244}
235 245
236/** 246/**
@@ -245,7 +255,7 @@ static void rproc_vdev_release(struct device *dev)
245int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id) 255int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
246{ 256{
247 struct rproc *rproc = rvdev->rproc; 257 struct rproc *rproc = rvdev->rproc;
248 struct device *dev = rproc->dev; 258 struct device *dev = &rproc->dev;
249 struct virtio_device *vdev = &rvdev->vdev; 259 struct virtio_device *vdev = &rvdev->vdev;
250 int ret; 260 int ret;
251 261
@@ -262,11 +272,11 @@ int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
262 * Therefore we must increment the rproc refcount here, and decrement 272 * Therefore we must increment the rproc refcount here, and decrement
263 * it _only_ when the vdev is released. 273 * it _only_ when the vdev is released.
264 */ 274 */
265 kref_get(&rproc->refcount); 275 get_device(&rproc->dev);
266 276
267 ret = register_virtio_device(vdev); 277 ret = register_virtio_device(vdev);
268 if (ret) { 278 if (ret) {
269 kref_put(&rproc->refcount, rproc_release); 279 put_device(&rproc->dev);
270 dev_err(dev, "failed to register vdev: %d\n", ret); 280 dev_err(dev, "failed to register vdev: %d\n", ret);
271 goto out; 281 goto out;
272 } 282 }
diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index f56c8ba3a861..590cfafc7c17 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -956,7 +956,8 @@ static int rpmsg_probe(struct virtio_device *vdev)
956 vrp->svq = vqs[1]; 956 vrp->svq = vqs[1];
957 957
958 /* allocate coherent memory for the buffers */ 958 /* allocate coherent memory for the buffers */
959 bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, 959 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
960 RPMSG_TOTAL_BUF_SPACE,
960 &vrp->bufs_dma, GFP_KERNEL); 961 &vrp->bufs_dma, GFP_KERNEL);
961 if (!bufs_va) 962 if (!bufs_va)
962 goto vqs_del; 963 goto vqs_del;
diff --git a/include/linux/remoteproc.h b/include/linux/remoteproc.h
index f1ffabb978d3..131b53957b9f 100644
--- a/include/linux/remoteproc.h
+++ b/include/linux/remoteproc.h
@@ -36,7 +36,6 @@
36#define REMOTEPROC_H 36#define REMOTEPROC_H
37 37
38#include <linux/types.h> 38#include <linux/types.h>
39#include <linux/kref.h>
40#include <linux/klist.h> 39#include <linux/klist.h>
41#include <linux/mutex.h> 40#include <linux/mutex.h>
42#include <linux/virtio.h> 41#include <linux/virtio.h>
@@ -369,8 +368,8 @@ enum rproc_state {
369 * @firmware: name of firmware file to be loaded 368 * @firmware: name of firmware file to be loaded
370 * @priv: private data which belongs to the platform-specific rproc module 369 * @priv: private data which belongs to the platform-specific rproc module
371 * @ops: platform-specific start/stop rproc handlers 370 * @ops: platform-specific start/stop rproc handlers
372 * @dev: underlying device 371 * @dev: virtual device for refcounting and common remoteproc behavior
373 * @refcount: refcount of users that have a valid pointer to this rproc 372 * @fw_ops: firmware-specific handlers
374 * @power: refcount of users who need this rproc powered up 373 * @power: refcount of users who need this rproc powered up
375 * @state: state of the device 374 * @state: state of the device
376 * @lock: lock which protects concurrent manipulations of the rproc 375 * @lock: lock which protects concurrent manipulations of the rproc
@@ -383,6 +382,7 @@ enum rproc_state {
383 * @bootaddr: address of first instruction to boot rproc with (optional) 382 * @bootaddr: address of first instruction to boot rproc with (optional)
384 * @rvdevs: list of remote virtio devices 383 * @rvdevs: list of remote virtio devices
385 * @notifyids: idr for dynamically assigning rproc-wide unique notify ids 384 * @notifyids: idr for dynamically assigning rproc-wide unique notify ids
385 * @index: index of this rproc device
386 */ 386 */
387struct rproc { 387struct rproc {
388 struct klist_node node; 388 struct klist_node node;
@@ -391,8 +391,8 @@ struct rproc {
391 const char *firmware; 391 const char *firmware;
392 void *priv; 392 void *priv;
393 const struct rproc_ops *ops; 393 const struct rproc_ops *ops;
394 struct device *dev; 394 struct device dev;
395 struct kref refcount; 395 const struct rproc_fw_ops *fw_ops;
396 atomic_t power; 396 atomic_t power;
397 unsigned int state; 397 unsigned int state;
398 struct mutex lock; 398 struct mutex lock;
@@ -405,6 +405,7 @@ struct rproc {
405 u32 bootaddr; 405 u32 bootaddr;
406 struct list_head rvdevs; 406 struct list_head rvdevs;
407 struct idr notifyids; 407 struct idr notifyids;
408 int index;
408}; 409};
409 410
410/* we currently support only two vrings per rvdev */ 411/* we currently support only two vrings per rvdev */
@@ -450,15 +451,12 @@ struct rproc_vdev {
450 unsigned long gfeatures; 451 unsigned long gfeatures;
451}; 452};
452 453
453struct rproc *rproc_get_by_name(const char *name);
454void rproc_put(struct rproc *rproc);
455
456struct rproc *rproc_alloc(struct device *dev, const char *name, 454struct rproc *rproc_alloc(struct device *dev, const char *name,
457 const struct rproc_ops *ops, 455 const struct rproc_ops *ops,
458 const char *firmware, int len); 456 const char *firmware, int len);
459void rproc_free(struct rproc *rproc); 457void rproc_put(struct rproc *rproc);
460int rproc_register(struct rproc *rproc); 458int rproc_add(struct rproc *rproc);
461int rproc_unregister(struct rproc *rproc); 459int rproc_del(struct rproc *rproc);
462 460
463int rproc_boot(struct rproc *rproc); 461int rproc_boot(struct rproc *rproc);
464void rproc_shutdown(struct rproc *rproc); 462void rproc_shutdown(struct rproc *rproc);