blob: 70ab496d0431c521f008dbf004761de4f3cbfb63 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +02002/*
3 * Remote processor messaging transport (OMAP platform-specific bits)
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Brian Swetland <swetland@google.com>
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020010 */
11
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020012#include <linux/dma-map-ops.h>
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020013#include <linux/export.h>
Loic Pallardy086d0872019-01-10 14:50:49 +010014#include <linux/of_reserved_mem.h>
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020015#include <linux/remoteproc.h>
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020016#include <linux/virtio.h>
17#include <linux/virtio_config.h>
18#include <linux/virtio_ids.h>
19#include <linux/virtio_ring.h>
20#include <linux/err.h>
21#include <linux/kref.h>
22#include <linux/slab.h>
23
24#include "remoteproc_internal.h"
25
Arnaud Pouliquen99555482021-10-01 12:12:30 +020026static struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
27{
28 return container_of(vdev->dev.parent, struct rproc_vdev, dev);
29}
30
31static struct rproc *vdev_to_rproc(struct virtio_device *vdev)
32{
33 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
34
35 return rvdev->rproc;
36}
37
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020038/* kick the remote processor, and let it know which virtqueue to poke at */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103039static bool rproc_virtio_notify(struct virtqueue *vq)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020040{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010041 struct rproc_vring *rvring = vq->priv;
42 struct rproc *rproc = rvring->rvdev->rproc;
43 int notifyid = rvring->notifyid;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020044
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030045 dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020046
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010047 rproc->ops->kick(rproc, notifyid);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103048 return true;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020049}
50
51/**
52 * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
53 * @rproc: handle to the remote processor
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010054 * @notifyid: index of the signalled virtqueue (unique per this @rproc)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020055 *
56 * This function should be called by the platform-specific rproc driver,
57 * when the remote processor signals that a specific virtqueue has pending
58 * messages available.
59 *
Suman Annaf28674342021-05-19 13:03:04 -050060 * Return: IRQ_NONE if no message was found in the @notifyid virtqueue,
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020061 * and otherwise returns IRQ_HANDLED.
62 */
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010063irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020064{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010065 struct rproc_vring *rvring;
66
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030067 dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010068
69 rvring = idr_find(&rproc->notifyids, notifyid);
70 if (!rvring || !rvring->vq)
71 return IRQ_NONE;
72
73 return vring_interrupt(0, rvring->vq);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020074}
75EXPORT_SYMBOL(rproc_vq_interrupt);
76
77static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
Anna, Sumanf1459282016-08-12 18:42:19 -050078 unsigned int id,
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020079 void (*callback)(struct virtqueue *vq),
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +020080 const char *name, bool ctx)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020081{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010082 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020083 struct rproc *rproc = vdev_to_rproc(vdev);
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +030084 struct device *dev = &rproc->dev;
Loic Pallardyc6aed2382018-07-27 15:14:47 +020085 struct rproc_mem_entry *mem;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010086 struct rproc_vring *rvring;
Loic Pallardyc6aed2382018-07-27 15:14:47 +020087 struct fw_rsc_vdev *rsc;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020088 struct virtqueue *vq;
89 void *addr;
Bjorn Anderssona863af52016-10-19 19:40:07 -070090 int len, size;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020091
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +010092 /* we're temporarily limited to two virtqueues per rvdev */
93 if (id >= ARRAY_SIZE(rvdev->vring))
94 return ERR_PTR(-EINVAL);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +020095
Michael S. Tsirkin6457f122012-09-05 21:47:45 +030096 if (!name)
97 return NULL;
98
Loic Pallardyc6aed2382018-07-27 15:14:47 +020099 /* Search allocated memory region by name */
100 mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
101 id);
102 if (!mem || !mem->va)
103 return ERR_PTR(-ENOMEM);
104
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300105 rvring = &rvdev->vring[id];
Loic Pallardyc6aed2382018-07-27 15:14:47 +0200106 addr = mem->va;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100107 len = rvring->len;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200108
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100109 /* zero vring */
110 size = vring_size(len, rvring->align);
111 memset(addr, 0, size);
112
Loic Pallardy276ec992018-07-06 14:38:27 +0200113 dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n",
Anna, Suman730f84c2016-08-12 18:42:20 -0500114 id, addr, len, rvring->notifyid);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200115
Ohad Ben-Cohendd6da1c2012-01-12 09:26:57 +0200116 /*
117 * Create the new vq, and tell virtio we're not interested in
118 * the 'weak' smp barriers, since we're talking with a real device.
119 */
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200120 vq = vring_new_virtqueue(id, len, rvring->align, vdev, false, ctx,
121 addr, rproc_virtio_notify, callback, name);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200122 if (!vq) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300123 dev_err(dev, "vring_new_virtqueue %s failed\n", name);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300124 rproc_free_vring(rvring);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100125 return ERR_PTR(-ENOMEM);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200126 }
127
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100128 rvring->vq = vq;
129 vq->priv = rvring;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200130
Loic Pallardyc6aed2382018-07-27 15:14:47 +0200131 /* Update vring in resource table */
132 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
133 rsc->vring[id].da = mem->da;
134
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200135 return vq;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200136}
137
Ohad Ben-Cohendab55bb2012-11-12 11:13:51 +0200138static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200139{
140 struct virtqueue *vq, *n;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100141 struct rproc_vring *rvring;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200142
143 list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100144 rvring = vq->priv;
145 rvring->vq = NULL;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200146 vring_del_virtqueue(vq);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200147 }
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200148}
149
Ohad Ben-Cohendab55bb2012-11-12 11:13:51 +0200150static void rproc_virtio_del_vqs(struct virtio_device *vdev)
151{
Ohad Ben-Cohendab55bb2012-11-12 11:13:51 +0200152 __rproc_virtio_del_vqs(vdev);
153}
154
Anna, Sumanf1459282016-08-12 18:42:19 -0500155static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
Anna, Suman730f84c2016-08-12 18:42:20 -0500156 struct virtqueue *vqs[],
157 vq_callback_t *callbacks[],
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100158 const char * const names[],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200159 const bool * ctx,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100160 struct irq_affinity *desc)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200161{
Wei Wanga2299892018-12-28 10:26:26 +0800162 int i, ret, queue_idx = 0;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200163
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200164 for (i = 0; i < nvqs; ++i) {
Wei Wanga2299892018-12-28 10:26:26 +0800165 if (!names[i]) {
166 vqs[i] = NULL;
167 continue;
168 }
169
170 vqs[i] = rp_find_vq(vdev, queue_idx++, callbacks[i], names[i],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200171 ctx ? ctx[i] : false);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200172 if (IS_ERR(vqs[i])) {
173 ret = PTR_ERR(vqs[i]);
174 goto error;
175 }
176 }
177
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200178 return 0;
179
180error:
Ohad Ben-Cohendab55bb2012-11-12 11:13:51 +0200181 __rproc_virtio_del_vqs(vdev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200182 return ret;
183}
184
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200185static u8 rproc_virtio_get_status(struct virtio_device *vdev)
186{
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100187 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
188 struct fw_rsc_vdev *rsc;
189
190 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
191
192 return rsc->status;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200193}
194
195static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
196{
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100197 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
198 struct fw_rsc_vdev *rsc;
199
200 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
201
202 rsc->status = status;
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100203 dev_dbg(&vdev->dev, "status: %d\n", status);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200204}
205
206static void rproc_virtio_reset(struct virtio_device *vdev)
207{
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100208 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
209 struct fw_rsc_vdev *rsc;
210
211 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
212
213 rsc->status = 0;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200214 dev_dbg(&vdev->dev, "reset !\n");
215}
216
217/* provide the vdev features as retrieved from the firmware */
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200218static u64 rproc_virtio_get_features(struct virtio_device *vdev)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200219{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100220 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100221 struct fw_rsc_vdev *rsc;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200222
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100223 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
224
225 return rsc->dfeatures;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200226}
227
Tiwei Bie3a814fd2018-11-21 18:03:29 +0800228static void rproc_transport_features(struct virtio_device *vdev)
229{
230 /*
231 * Packed ring isn't enabled on remoteproc for now,
232 * because remoteproc uses vring_new_virtqueue() which
233 * creates virtio rings on preallocated memory.
234 */
235 __virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED);
236}
237
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200238static int rproc_virtio_finalize_features(struct virtio_device *vdev)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200239{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100240 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100241 struct fw_rsc_vdev *rsc;
242
243 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200244
245 /* Give virtio_ring a chance to accept features */
246 vring_transport_features(vdev);
247
Tiwei Bie3a814fd2018-11-21 18:03:29 +0800248 /* Give virtio_rproc a chance to accept features. */
249 rproc_transport_features(vdev);
250
Michael S. Tsirkin93d389f2014-11-27 13:45:58 +0200251 /* Make sure we don't have any features > 32 bits! */
252 BUG_ON((u32)vdev->features != vdev->features);
253
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200254 /*
255 * Remember the finalized features of our vdev, and provide it
256 * to the remote processor once it is powered on.
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200257 */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200258 rsc->gfeatures = vdev->features;
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200259
260 return 0;
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100261}
262
Anna, Sumanf1459282016-08-12 18:42:19 -0500263static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
264 void *buf, unsigned int len)
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100265{
266 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
267 struct fw_rsc_vdev *rsc;
268 void *cfg;
269
270 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
271 cfg = &rsc->vring[rsc->num_of_vrings];
272
273 if (offset + len > rsc->config_len || offset + len < len) {
274 dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
275 return;
276 }
277
278 memcpy(buf, cfg + offset, len);
279}
280
Anna, Sumanf1459282016-08-12 18:42:19 -0500281static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset,
282 const void *buf, unsigned int len)
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100283{
284 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
285 struct fw_rsc_vdev *rsc;
286 void *cfg;
287
288 rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
289 cfg = &rsc->vring[rsc->num_of_vrings];
290
291 if (offset + len > rsc->config_len || offset + len < len) {
292 dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
293 return;
294 }
295
296 memcpy(cfg + offset, buf, len);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200297}
298
Stephen Hemminger93503932013-02-10 15:57:38 +1030299static const struct virtio_config_ops rproc_virtio_config_ops = {
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200300 .get_features = rproc_virtio_get_features,
301 .finalize_features = rproc_virtio_finalize_features,
302 .find_vqs = rproc_virtio_find_vqs,
303 .del_vqs = rproc_virtio_del_vqs,
304 .reset = rproc_virtio_reset,
305 .set_status = rproc_virtio_set_status,
306 .get_status = rproc_virtio_get_status,
Sjur Brændeland92b38f82013-02-21 18:15:39 +0100307 .get = rproc_virtio_get,
308 .set = rproc_virtio_set,
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200309};
310
311/*
312 * This function is called whenever vdev is released, and is responsible
Ohad Ben-Cohen7183a2a2012-05-30 22:02:24 +0300313 * to decrement the remote processor's refcount which was taken when vdev was
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200314 * added.
315 *
316 * Never call this function directly; it will be called by the driver
317 * core when needed.
318 */
Bjorn Anderssonaab8d802016-10-19 19:40:06 -0700319static void rproc_virtio_dev_release(struct device *dev)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200320{
321 struct virtio_device *vdev = dev_to_virtio(dev);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300322 struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200323 struct rproc *rproc = vdev_to_rproc(vdev);
324
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100325 kfree(vdev);
326
Bjorn Anderssonaab8d802016-10-19 19:40:06 -0700327 kref_put(&rvdev->refcount, rproc_vdev_release);
Ohad Ben-Cohen6db20ea2012-05-17 14:23:59 +0300328
Ohad Ben-Cohen7183a2a2012-05-30 22:02:24 +0300329 put_device(&rproc->dev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200330}
331
332/**
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100333 * rproc_add_virtio_dev() - register an rproc-induced virtio device
334 * @rvdev: the remote vdev
Arnaud Pouliquen2e7d4c22020-02-12 17:19:56 +0100335 * @id: the device type identification (used to match it with a driver).
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200336 *
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100337 * This function registers a virtio device. This vdev's partent is
338 * the rproc device.
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200339 *
Suman Annaf28674342021-05-19 13:03:04 -0500340 * Return: 0 on success or an appropriate error value otherwise
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200341 */
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100342int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200343{
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100344 struct rproc *rproc = rvdev->rproc;
Loic Pallardy086d0872019-01-10 14:50:49 +0100345 struct device *dev = &rvdev->dev;
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100346 struct virtio_device *vdev;
Loic Pallardy086d0872019-01-10 14:50:49 +0100347 struct rproc_mem_entry *mem;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200348 int ret;
349
Nikita Shubin791c13b2020-03-06 10:24:53 +0300350 if (rproc->ops->kick == NULL) {
351 ret = -EINVAL;
Christophe JAILLET2fb75ce2020-04-11 18:07:50 +0200352 dev_err(dev, ".kick method not defined for %s\n", rproc->name);
Nikita Shubin791c13b2020-03-06 10:24:53 +0300353 goto out;
354 }
355
Loic Pallardy086d0872019-01-10 14:50:49 +0100356 /* Try to find dedicated vdev buffer carveout */
357 mem = rproc_find_carveout_by_name(rproc, "vdev%dbuffer", rvdev->index);
358 if (mem) {
359 phys_addr_t pa;
360
361 if (mem->of_resm_idx != -1) {
362 struct device_node *np = rproc->dev.parent->of_node;
363
364 /* Associate reserved memory to vdev device */
365 ret = of_reserved_mem_device_init_by_idx(dev, np,
366 mem->of_resm_idx);
367 if (ret) {
368 dev_err(dev, "Can't associate reserved memory\n");
369 goto out;
370 }
371 } else {
372 if (mem->va) {
373 dev_warn(dev, "vdev %d buffer already mapped\n",
374 rvdev->index);
375 pa = rproc_va_to_pa(mem->va);
376 } else {
377 /* Use dma address as carveout no memmapped yet */
378 pa = (phys_addr_t)mem->dma;
379 }
380
381 /* Associate vdev buffer memory pool to vdev subdev */
382 ret = dma_declare_coherent_memory(dev, pa,
383 mem->da,
Stephen Rothwelld664ce72019-02-22 16:14:45 +1100384 mem->len);
Loic Pallardy086d0872019-01-10 14:50:49 +0100385 if (ret < 0) {
386 dev_err(dev, "Failed to associate buffer\n");
387 goto out;
388 }
389 }
Tero Kristodb9178a2020-04-20 11:05:59 -0500390 } else {
391 struct device_node *np = rproc->dev.parent->of_node;
392
393 /*
394 * If we don't have dedicated buffer, just attempt to re-assign
395 * the reserved memory from our parent. A default memory-region
396 * at index 0 from the parent's memory-regions is assigned for
397 * the rvdev dev to allocate from. Failure is non-critical and
398 * the allocations will fall back to global pools, so don't
399 * check return value either.
400 */
401 of_reserved_mem_device_init_by_idx(dev, np, 0);
Loic Pallardy086d0872019-01-10 14:50:49 +0100402 }
403
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100404 /* Allocate virtio device */
405 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
406 if (!vdev) {
407 ret = -ENOMEM;
408 goto out;
409 }
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100410 vdev->id.device = id,
411 vdev->config = &rproc_virtio_config_ops,
412 vdev->dev.parent = dev;
Bjorn Anderssonaab8d802016-10-19 19:40:06 -0700413 vdev->dev.release = rproc_virtio_dev_release;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200414
415 /*
416 * We're indirectly making a non-temporary copy of the rproc pointer
417 * here, because drivers probed with this vdev will indirectly
418 * access the wrapping rproc.
419 *
420 * Therefore we must increment the rproc refcount here, and decrement
421 * it _only_ when the vdev is released.
422 */
Ohad Ben-Cohen7183a2a2012-05-30 22:02:24 +0300423 get_device(&rproc->dev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200424
Bjorn Anderssonaab8d802016-10-19 19:40:06 -0700425 /* Reference the vdev and vring allocations */
426 kref_get(&rvdev->refcount);
427
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100428 ret = register_virtio_device(vdev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200429 if (ret) {
weiping zhang900a1632017-12-21 20:40:58 +0800430 put_device(&vdev->dev);
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200431 dev_err(dev, "failed to register vdev: %d\n", ret);
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100432 goto out;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200433 }
434
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100435 dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
436
437out:
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200438 return ret;
439}
440
441/**
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100442 * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100443 * @dev: the virtio device
444 * @data: must be null
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200445 *
Ohad Ben-Cohen7a186942012-02-13 22:30:39 +0100446 * This function unregisters an existing virtio device.
Suman Annaf28674342021-05-19 13:03:04 -0500447 *
448 * Return: 0
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200449 */
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100450int rproc_remove_virtio_dev(struct device *dev, void *data)
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200451{
Loic Pallardyd4c036f2019-01-21 14:55:15 +0100452 struct virtio_device *vdev = dev_to_virtio(dev);
453
454 unregister_virtio_device(vdev);
455 return 0;
Ohad Ben-Cohenac8954a2011-10-20 18:15:13 +0200456}