Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Virtio-based remote processor messaging bus |
| 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 | * |
| 10 | * This software is licensed under the terms of the GNU General Public |
| 11 | * License version 2, as published by the Free Software Foundation, and |
| 12 | * may be copied, distributed, and modified under those terms. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/virtio.h> |
| 25 | #include <linux/virtio_ids.h> |
| 26 | #include <linux/virtio_config.h> |
| 27 | #include <linux/scatterlist.h> |
| 28 | #include <linux/dma-mapping.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/idr.h> |
| 31 | #include <linux/jiffies.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/wait.h> |
| 34 | #include <linux/rpmsg.h> |
| 35 | #include <linux/mutex.h> |
Bjorn Andersson | a16644c | 2016-09-01 15:27:53 -0700 | [diff] [blame] | 36 | #include <linux/of_device.h> |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 37 | |
Bjorn Andersson | 8b881c0 | 2016-09-01 15:28:02 -0700 | [diff] [blame] | 38 | #include "rpmsg_internal.h" |
| 39 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 40 | /** |
| 41 | * struct virtproc_info - virtual remote processor state |
| 42 | * @vdev: the virtio device |
| 43 | * @rvq: rx virtqueue |
| 44 | * @svq: tx virtqueue |
| 45 | * @rbufs: kernel address of rx buffers |
| 46 | * @sbufs: kernel address of tx buffers |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 47 | * @num_bufs: total number of buffers for rx and tx |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 48 | * @last_sbuf: index of last tx buffer used |
| 49 | * @bufs_dma: dma base addr of the buffers |
| 50 | * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders. |
| 51 | * sending a message might require waking up a dozing remote |
| 52 | * processor, which involves sleeping, hence the mutex. |
| 53 | * @endpoints: idr of local endpoints, allows fast retrieval |
| 54 | * @endpoints_lock: lock of the endpoints set |
| 55 | * @sendq: wait queue of sending contexts waiting for a tx buffers |
| 56 | * @sleepers: number of senders that are waiting for a tx buffer |
| 57 | * @ns_ept: the bus's name service endpoint |
| 58 | * |
| 59 | * This structure stores the rpmsg state of a given virtio remote processor |
| 60 | * device (there might be several virtio proc devices for each physical |
| 61 | * remote processor). |
| 62 | */ |
| 63 | struct virtproc_info { |
| 64 | struct virtio_device *vdev; |
| 65 | struct virtqueue *rvq, *svq; |
| 66 | void *rbufs, *sbufs; |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 67 | unsigned int num_bufs; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 68 | int last_sbuf; |
| 69 | dma_addr_t bufs_dma; |
| 70 | struct mutex tx_lock; |
| 71 | struct idr endpoints; |
| 72 | struct mutex endpoints_lock; |
| 73 | wait_queue_head_t sendq; |
| 74 | atomic_t sleepers; |
| 75 | struct rpmsg_endpoint *ns_ept; |
| 76 | }; |
| 77 | |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 78 | #define to_rpmsg_device(d) container_of(d, struct rpmsg_device, dev) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 79 | #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv) |
| 80 | |
| 81 | /* |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 82 | * We're allocating buffers of 512 bytes each for communications. The |
| 83 | * number of buffers will be computed from the number of buffers supported |
| 84 | * by the vring, upto a maximum of 512 buffers (256 in each direction). |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 85 | * |
| 86 | * Each buffer will have 16 bytes for the msg header and 496 bytes for |
| 87 | * the payload. |
| 88 | * |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 89 | * This will utilize a maximum total space of 256KB for the buffers. |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 90 | * |
| 91 | * We might also want to add support for user-provided buffers in time. |
| 92 | * This will allow bigger buffer size flexibility, and can also be used |
| 93 | * to achieve zero-copy messaging. |
| 94 | * |
| 95 | * Note that these numbers are purely a decision of this driver - we |
| 96 | * can change this without changing anything in the firmware of the remote |
| 97 | * processor. |
| 98 | */ |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 99 | #define MAX_RPMSG_NUM_BUFS (512) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 100 | #define RPMSG_BUF_SIZE (512) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * Local addresses are dynamically allocated on-demand. |
| 104 | * We do not dynamically assign addresses from the low 1024 range, |
| 105 | * in order to reserve that address range for predefined services. |
| 106 | */ |
| 107 | #define RPMSG_RESERVED_ADDRESSES (1024) |
| 108 | |
| 109 | /* Address 53 is reserved for advertising remote services */ |
| 110 | #define RPMSG_NS_ADDR (53) |
| 111 | |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 112 | static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept); |
| 113 | static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len); |
| 114 | static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, |
| 115 | u32 dst); |
| 116 | static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, |
| 117 | u32 dst, void *data, int len); |
| 118 | static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len); |
| 119 | static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, |
| 120 | int len, u32 dst); |
| 121 | static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, |
| 122 | u32 dst, void *data, int len); |
Bjorn Andersson | 6eed598 | 2016-09-01 15:28:03 -0700 | [diff] [blame^] | 123 | static int rpmsg_register_device(struct rpmsg_device *rpdev); |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 124 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 125 | /* sysfs show configuration fields */ |
| 126 | #define rpmsg_show_attr(field, path, format_string) \ |
| 127 | static ssize_t \ |
| 128 | field##_show(struct device *dev, \ |
| 129 | struct device_attribute *attr, char *buf) \ |
| 130 | { \ |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 131 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 132 | \ |
| 133 | return sprintf(buf, format_string, rpdev->path); \ |
| 134 | } |
| 135 | |
| 136 | /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */ |
| 137 | rpmsg_show_attr(name, id.name, "%s\n"); |
| 138 | rpmsg_show_attr(src, src, "0x%x\n"); |
| 139 | rpmsg_show_attr(dst, dst, "0x%x\n"); |
| 140 | rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n"); |
| 141 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 142 | static ssize_t modalias_show(struct device *dev, |
| 143 | struct device_attribute *attr, char *buf) |
| 144 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 145 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 146 | |
| 147 | return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name); |
| 148 | } |
| 149 | |
| 150 | static struct device_attribute rpmsg_dev_attrs[] = { |
| 151 | __ATTR_RO(name), |
| 152 | __ATTR_RO(modalias), |
| 153 | __ATTR_RO(dst), |
| 154 | __ATTR_RO(src), |
| 155 | __ATTR_RO(announce), |
| 156 | __ATTR_NULL |
| 157 | }; |
| 158 | |
| 159 | /* rpmsg devices and drivers are matched using the service name */ |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 160 | static inline int rpmsg_id_match(const struct rpmsg_device *rpdev, |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 161 | const struct rpmsg_device_id *id) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 162 | { |
| 163 | return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0; |
| 164 | } |
| 165 | |
| 166 | /* match rpmsg channel and rpmsg driver */ |
| 167 | static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) |
| 168 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 169 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 170 | struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv); |
| 171 | const struct rpmsg_device_id *ids = rpdrv->id_table; |
| 172 | unsigned int i; |
| 173 | |
Bjorn Andersson | a16644c | 2016-09-01 15:27:53 -0700 | [diff] [blame] | 174 | if (ids) |
| 175 | for (i = 0; ids[i].name[0]; i++) |
| 176 | if (rpmsg_id_match(rpdev, &ids[i])) |
| 177 | return 1; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 178 | |
Bjorn Andersson | a16644c | 2016-09-01 15:27:53 -0700 | [diff] [blame] | 179 | return of_driver_match_device(dev, drv); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 183 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 184 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 185 | |
| 186 | return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT, |
| 187 | rpdev->id.name); |
| 188 | } |
| 189 | |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 190 | static const struct rpmsg_endpoint_ops virtio_endpoint_ops = { |
| 191 | .destroy_ept = virtio_rpmsg_destroy_ept, |
| 192 | .send = virtio_rpmsg_send, |
| 193 | .sendto = virtio_rpmsg_sendto, |
| 194 | .send_offchannel = virtio_rpmsg_send_offchannel, |
| 195 | .trysend = virtio_rpmsg_trysend, |
| 196 | .trysendto = virtio_rpmsg_trysendto, |
| 197 | .trysend_offchannel = virtio_rpmsg_trysend_offchannel, |
| 198 | }; |
| 199 | |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 200 | /** |
| 201 | * __ept_release() - deallocate an rpmsg endpoint |
| 202 | * @kref: the ept's reference count |
| 203 | * |
| 204 | * This function deallocates an ept, and is invoked when its @kref refcount |
| 205 | * drops to zero. |
| 206 | * |
| 207 | * Never invoke this function directly! |
| 208 | */ |
| 209 | static void __ept_release(struct kref *kref) |
| 210 | { |
| 211 | struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint, |
| 212 | refcount); |
| 213 | /* |
| 214 | * At this point no one holds a reference to ept anymore, |
| 215 | * so we can directly free it |
| 216 | */ |
| 217 | kfree(ept); |
| 218 | } |
| 219 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 220 | /* for more info, see below documentation of rpmsg_create_ept() */ |
| 221 | static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp, |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 222 | struct rpmsg_device *rpdev, |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 223 | rpmsg_rx_cb_t cb, |
| 224 | void *priv, u32 addr) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 225 | { |
Tejun Heo | d0ffce7 | 2013-02-27 17:04:40 -0800 | [diff] [blame] | 226 | int id_min, id_max, id; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 227 | struct rpmsg_endpoint *ept; |
| 228 | struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev; |
| 229 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 230 | ept = kzalloc(sizeof(*ept), GFP_KERNEL); |
Anna, Suman | a8bb3fd | 2016-08-12 18:42:24 -0500 | [diff] [blame] | 231 | if (!ept) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 232 | return NULL; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 233 | |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 234 | kref_init(&ept->refcount); |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 235 | mutex_init(&ept->cb_lock); |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 236 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 237 | ept->rpdev = rpdev; |
| 238 | ept->cb = cb; |
| 239 | ept->priv = priv; |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 240 | ept->ops = &virtio_endpoint_ops; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 241 | |
| 242 | /* do we need to allocate a local address ? */ |
Tejun Heo | d0ffce7 | 2013-02-27 17:04:40 -0800 | [diff] [blame] | 243 | if (addr == RPMSG_ADDR_ANY) { |
| 244 | id_min = RPMSG_RESERVED_ADDRESSES; |
| 245 | id_max = 0; |
| 246 | } else { |
| 247 | id_min = addr; |
| 248 | id_max = addr + 1; |
| 249 | } |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 250 | |
| 251 | mutex_lock(&vrp->endpoints_lock); |
| 252 | |
| 253 | /* bind the endpoint to an rpmsg address (and allocate one if needed) */ |
Tejun Heo | d0ffce7 | 2013-02-27 17:04:40 -0800 | [diff] [blame] | 254 | id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL); |
| 255 | if (id < 0) { |
| 256 | dev_err(dev, "idr_alloc failed: %d\n", id); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 257 | goto free_ept; |
| 258 | } |
Tejun Heo | d0ffce7 | 2013-02-27 17:04:40 -0800 | [diff] [blame] | 259 | ept->addr = id; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 260 | |
| 261 | mutex_unlock(&vrp->endpoints_lock); |
| 262 | |
| 263 | return ept; |
| 264 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 265 | free_ept: |
| 266 | mutex_unlock(&vrp->endpoints_lock); |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 267 | kref_put(&ept->refcount, __ept_release); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
| 270 | |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 271 | static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev, |
| 272 | rpmsg_rx_cb_t cb, |
| 273 | void *priv, |
| 274 | struct rpmsg_channel_info chinfo) |
| 275 | { |
| 276 | return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, chinfo.src); |
| 277 | } |
| 278 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 279 | /** |
Ohad Ben-Cohen | fa2d779 | 2012-02-09 15:16:41 +0200 | [diff] [blame] | 280 | * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint |
| 281 | * @vrp: virtproc which owns this ept |
| 282 | * @ept: endpoing to destroy |
| 283 | * |
| 284 | * An internal function which destroy an ept without assuming it is |
| 285 | * bound to an rpmsg channel. This is needed for handling the internal |
| 286 | * name service endpoint, which isn't bound to an rpmsg channel. |
| 287 | * See also __rpmsg_create_ept(). |
| 288 | */ |
| 289 | static void |
| 290 | __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept) |
| 291 | { |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 292 | /* make sure new inbound messages can't find this ept anymore */ |
Ohad Ben-Cohen | fa2d779 | 2012-02-09 15:16:41 +0200 | [diff] [blame] | 293 | mutex_lock(&vrp->endpoints_lock); |
| 294 | idr_remove(&vrp->endpoints, ept->addr); |
| 295 | mutex_unlock(&vrp->endpoints_lock); |
| 296 | |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 297 | /* make sure in-flight inbound messages won't invoke cb anymore */ |
| 298 | mutex_lock(&ept->cb_lock); |
| 299 | ept->cb = NULL; |
| 300 | mutex_unlock(&ept->cb_lock); |
| 301 | |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 302 | kref_put(&ept->refcount, __ept_release); |
Ohad Ben-Cohen | fa2d779 | 2012-02-09 15:16:41 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 305 | static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept) |
| 306 | { |
| 307 | __rpmsg_destroy_ept(ept->rpdev->vrp, ept); |
| 308 | } |
| 309 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 310 | /* |
| 311 | * when an rpmsg driver is probed with a channel, we seamlessly create |
| 312 | * it an endpoint, binding its rx callback to a unique local rpmsg |
| 313 | * address. |
| 314 | * |
| 315 | * if we need to, we also announce about this channel to the remote |
| 316 | * processor (needed in case the driver is exposing an rpmsg service). |
| 317 | */ |
| 318 | static int rpmsg_dev_probe(struct device *dev) |
| 319 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 320 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 321 | struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); |
Bjorn Andersson | 2b263d2 | 2016-09-01 15:27:56 -0700 | [diff] [blame] | 322 | struct rpmsg_channel_info chinfo = {}; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 323 | struct rpmsg_endpoint *ept; |
| 324 | int err; |
| 325 | |
Bjorn Andersson | 2b263d2 | 2016-09-01 15:27:56 -0700 | [diff] [blame] | 326 | strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); |
| 327 | chinfo.src = rpdev->src; |
| 328 | chinfo.dst = RPMSG_ADDR_ANY; |
| 329 | |
| 330 | ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 331 | if (!ept) { |
| 332 | dev_err(dev, "failed to create endpoint\n"); |
| 333 | err = -ENOMEM; |
| 334 | goto out; |
| 335 | } |
| 336 | |
| 337 | rpdev->ept = ept; |
| 338 | rpdev->src = ept->addr; |
| 339 | |
| 340 | err = rpdrv->probe(rpdev); |
| 341 | if (err) { |
| 342 | dev_err(dev, "%s: failed: %d\n", __func__, err); |
| 343 | rpmsg_destroy_ept(ept); |
| 344 | goto out; |
| 345 | } |
| 346 | |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 347 | if (rpdev->ops->announce_create) |
| 348 | err = rpdev->ops->announce_create(rpdev); |
| 349 | out: |
| 350 | return err; |
| 351 | } |
| 352 | |
| 353 | static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev) |
| 354 | { |
| 355 | struct virtproc_info *vrp = rpdev->vrp; |
| 356 | struct device *dev = &rpdev->dev; |
| 357 | int err = 0; |
| 358 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 359 | /* need to tell remote processor's name service about this channel ? */ |
| 360 | if (rpdev->announce && |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 361 | virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 362 | struct rpmsg_ns_msg nsm; |
| 363 | |
| 364 | strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); |
Bjorn Andersson | 2a48d73 | 2016-09-01 15:27:55 -0700 | [diff] [blame] | 365 | nsm.addr = rpdev->ept->addr; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 366 | nsm.flags = RPMSG_NS_CREATE; |
| 367 | |
Bjorn Andersson | 2a48d73 | 2016-09-01 15:27:55 -0700 | [diff] [blame] | 368 | err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 369 | if (err) |
| 370 | dev_err(dev, "failed to announce service %d\n", err); |
| 371 | } |
| 372 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 373 | return err; |
| 374 | } |
| 375 | |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 376 | static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 377 | { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 378 | struct virtproc_info *vrp = rpdev->vrp; |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 379 | struct device *dev = &rpdev->dev; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 380 | int err = 0; |
| 381 | |
| 382 | /* tell remote processor's name service we're removing this channel */ |
| 383 | if (rpdev->announce && |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 384 | virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 385 | struct rpmsg_ns_msg nsm; |
| 386 | |
| 387 | strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE); |
| 388 | nsm.addr = rpdev->src; |
| 389 | nsm.flags = RPMSG_NS_DESTROY; |
| 390 | |
Bjorn Andersson | 2a48d73 | 2016-09-01 15:27:55 -0700 | [diff] [blame] | 391 | err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 392 | if (err) |
| 393 | dev_err(dev, "failed to announce service %d\n", err); |
| 394 | } |
| 395 | |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 396 | return err; |
| 397 | } |
| 398 | |
| 399 | static int rpmsg_dev_remove(struct device *dev) |
| 400 | { |
| 401 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
| 402 | struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); |
| 403 | int err = 0; |
| 404 | |
| 405 | if (rpdev->ops->announce_destroy) |
| 406 | err = rpdev->ops->announce_destroy(rpdev); |
| 407 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 408 | rpdrv->remove(rpdev); |
| 409 | |
| 410 | rpmsg_destroy_ept(rpdev->ept); |
| 411 | |
| 412 | return err; |
| 413 | } |
| 414 | |
| 415 | static struct bus_type rpmsg_bus = { |
| 416 | .name = "rpmsg", |
| 417 | .match = rpmsg_dev_match, |
| 418 | .dev_attrs = rpmsg_dev_attrs, |
| 419 | .uevent = rpmsg_uevent, |
| 420 | .probe = rpmsg_dev_probe, |
| 421 | .remove = rpmsg_dev_remove, |
| 422 | }; |
| 423 | |
| 424 | /** |
Andrew F. Davis | bc3c57c | 2016-05-04 17:01:36 -0500 | [diff] [blame] | 425 | * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 426 | * @rpdrv: pointer to a struct rpmsg_driver |
Andrew F. Davis | bc3c57c | 2016-05-04 17:01:36 -0500 | [diff] [blame] | 427 | * @owner: owning module/driver |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 428 | * |
| 429 | * Returns 0 on success, and an appropriate error value on failure. |
| 430 | */ |
Andrew F. Davis | bc3c57c | 2016-05-04 17:01:36 -0500 | [diff] [blame] | 431 | int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 432 | { |
| 433 | rpdrv->drv.bus = &rpmsg_bus; |
Andrew F. Davis | bc3c57c | 2016-05-04 17:01:36 -0500 | [diff] [blame] | 434 | rpdrv->drv.owner = owner; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 435 | return driver_register(&rpdrv->drv); |
| 436 | } |
Andrew F. Davis | bc3c57c | 2016-05-04 17:01:36 -0500 | [diff] [blame] | 437 | EXPORT_SYMBOL(__register_rpmsg_driver); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 438 | |
| 439 | /** |
| 440 | * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus |
| 441 | * @rpdrv: pointer to a struct rpmsg_driver |
| 442 | * |
| 443 | * Returns 0 on success, and an appropriate error value on failure. |
| 444 | */ |
| 445 | void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv) |
| 446 | { |
| 447 | driver_unregister(&rpdrv->drv); |
| 448 | } |
| 449 | EXPORT_SYMBOL(unregister_rpmsg_driver); |
| 450 | |
| 451 | static void rpmsg_release_device(struct device *dev) |
| 452 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 453 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 454 | |
| 455 | kfree(rpdev); |
| 456 | } |
| 457 | |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 458 | static const struct rpmsg_device_ops virtio_rpmsg_ops = { |
| 459 | .create_ept = virtio_rpmsg_create_ept, |
| 460 | .announce_create = virtio_rpmsg_announce_create, |
| 461 | .announce_destroy = virtio_rpmsg_announce_destroy, |
| 462 | }; |
| 463 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 464 | /* |
| 465 | * create an rpmsg channel using its name and address info. |
| 466 | * this function will be used to create both static and dynamic |
| 467 | * channels. |
| 468 | */ |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 469 | static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp, |
| 470 | struct rpmsg_channel_info *chinfo) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 471 | { |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 472 | struct rpmsg_device *rpdev; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 473 | struct device *tmp, *dev = &vrp->vdev->dev; |
| 474 | int ret; |
| 475 | |
| 476 | /* make sure a similar channel doesn't already exist */ |
Bjorn Andersson | 8b881c0 | 2016-09-01 15:28:02 -0700 | [diff] [blame] | 477 | tmp = rpmsg_find_device(dev, chinfo); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 478 | if (tmp) { |
| 479 | /* decrement the matched device's refcount back */ |
| 480 | put_device(tmp); |
| 481 | dev_err(dev, "channel %s:%x:%x already exist\n", |
| 482 | chinfo->name, chinfo->src, chinfo->dst); |
| 483 | return NULL; |
| 484 | } |
| 485 | |
Anna, Suman | a8bb3fd | 2016-08-12 18:42:24 -0500 | [diff] [blame] | 486 | rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL); |
| 487 | if (!rpdev) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 488 | return NULL; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 489 | |
| 490 | rpdev->vrp = vrp; |
| 491 | rpdev->src = chinfo->src; |
| 492 | rpdev->dst = chinfo->dst; |
Bjorn Andersson | 36b72c7 | 2016-09-01 15:27:58 -0700 | [diff] [blame] | 493 | rpdev->ops = &virtio_rpmsg_ops; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * rpmsg server channels has predefined local address (for now), |
| 497 | * and their existence needs to be announced remotely |
| 498 | */ |
Andrew F. Davis | c8ced11 | 2016-07-01 09:24:58 -0500 | [diff] [blame] | 499 | rpdev->announce = rpdev->src != RPMSG_ADDR_ANY; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 500 | |
| 501 | strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE); |
| 502 | |
Bjorn Andersson | 6eed598 | 2016-09-01 15:28:03 -0700 | [diff] [blame^] | 503 | rpdev->dev.parent = &vrp->vdev->dev; |
| 504 | ret = rpmsg_register_device(rpdev); |
| 505 | if (ret) |
| 506 | return NULL; |
| 507 | |
| 508 | return rpdev; |
| 509 | } |
| 510 | |
| 511 | static int rpmsg_register_device(struct rpmsg_device *rpdev) |
| 512 | { |
| 513 | struct device *dev = &rpdev->dev; |
| 514 | int ret; |
| 515 | |
Bjorn Andersson | 4dffed5 | 2016-09-01 15:27:54 -0700 | [diff] [blame] | 516 | dev_set_name(&rpdev->dev, "%s:%s", |
| 517 | dev_name(dev->parent), rpdev->id.name); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 518 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 519 | rpdev->dev.bus = &rpmsg_bus; |
| 520 | rpdev->dev.release = rpmsg_release_device; |
| 521 | |
| 522 | ret = device_register(&rpdev->dev); |
| 523 | if (ret) { |
| 524 | dev_err(dev, "device_register failed: %d\n", ret); |
| 525 | put_device(&rpdev->dev); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Bjorn Andersson | 6eed598 | 2016-09-01 15:28:03 -0700 | [diff] [blame^] | 528 | return ret; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
| 532 | * find an existing channel using its name + address properties, |
| 533 | * and destroy it |
| 534 | */ |
| 535 | static int rpmsg_destroy_channel(struct virtproc_info *vrp, |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 536 | struct rpmsg_channel_info *chinfo) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 537 | { |
| 538 | struct virtio_device *vdev = vrp->vdev; |
| 539 | struct device *dev; |
| 540 | |
Bjorn Andersson | 8b881c0 | 2016-09-01 15:28:02 -0700 | [diff] [blame] | 541 | dev = rpmsg_find_device(&vdev->dev, chinfo); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 542 | if (!dev) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | device_unregister(dev); |
| 546 | |
| 547 | put_device(dev); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* super simple buffer "allocator" that is just enough for now */ |
| 553 | static void *get_a_tx_buf(struct virtproc_info *vrp) |
| 554 | { |
| 555 | unsigned int len; |
| 556 | void *ret; |
| 557 | |
| 558 | /* support multiple concurrent senders */ |
| 559 | mutex_lock(&vrp->tx_lock); |
| 560 | |
| 561 | /* |
| 562 | * either pick the next unused tx buffer |
| 563 | * (half of our buffers are used for sending messages) |
| 564 | */ |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 565 | if (vrp->last_sbuf < vrp->num_bufs / 2) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 566 | ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++; |
| 567 | /* or recycle a used one */ |
| 568 | else |
| 569 | ret = virtqueue_get_buf(vrp->svq, &len); |
| 570 | |
| 571 | mutex_unlock(&vrp->tx_lock); |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed |
| 578 | * @vrp: virtual remote processor state |
| 579 | * |
| 580 | * This function is called before a sender is blocked, waiting for |
| 581 | * a tx buffer to become available. |
| 582 | * |
| 583 | * If we already have blocking senders, this function merely increases |
| 584 | * the "sleepers" reference count, and exits. |
| 585 | * |
| 586 | * Otherwise, if this is the first sender to block, we also enable |
| 587 | * virtio's tx callbacks, so we'd be immediately notified when a tx |
| 588 | * buffer is consumed (we rely on virtio's tx callback in order |
| 589 | * to wake up sleeping senders as soon as a tx buffer is used by the |
| 590 | * remote processor). |
| 591 | */ |
| 592 | static void rpmsg_upref_sleepers(struct virtproc_info *vrp) |
| 593 | { |
| 594 | /* support multiple concurrent senders */ |
| 595 | mutex_lock(&vrp->tx_lock); |
| 596 | |
| 597 | /* are we the first sleeping context waiting for tx buffers ? */ |
| 598 | if (atomic_inc_return(&vrp->sleepers) == 1) |
| 599 | /* enable "tx-complete" interrupts before dozing off */ |
| 600 | virtqueue_enable_cb(vrp->svq); |
| 601 | |
| 602 | mutex_unlock(&vrp->tx_lock); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed |
| 607 | * @vrp: virtual remote processor state |
| 608 | * |
| 609 | * This function is called after a sender, that waited for a tx buffer |
| 610 | * to become available, is unblocked. |
| 611 | * |
| 612 | * If we still have blocking senders, this function merely decreases |
| 613 | * the "sleepers" reference count, and exits. |
| 614 | * |
| 615 | * Otherwise, if there are no more blocking senders, we also disable |
| 616 | * virtio's tx callbacks, to avoid the overhead incurred with handling |
| 617 | * those (now redundant) interrupts. |
| 618 | */ |
| 619 | static void rpmsg_downref_sleepers(struct virtproc_info *vrp) |
| 620 | { |
| 621 | /* support multiple concurrent senders */ |
| 622 | mutex_lock(&vrp->tx_lock); |
| 623 | |
| 624 | /* are we the last sleeping context waiting for tx buffers ? */ |
| 625 | if (atomic_dec_and_test(&vrp->sleepers)) |
| 626 | /* disable "tx-complete" interrupts */ |
| 627 | virtqueue_disable_cb(vrp->svq); |
| 628 | |
| 629 | mutex_unlock(&vrp->tx_lock); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * rpmsg_send_offchannel_raw() - send a message across to the remote processor |
| 634 | * @rpdev: the rpmsg channel |
| 635 | * @src: source address |
| 636 | * @dst: destination address |
| 637 | * @data: payload of message |
| 638 | * @len: length of payload |
| 639 | * @wait: indicates whether caller should block in case no TX buffers available |
| 640 | * |
| 641 | * This function is the base implementation for all of the rpmsg sending API. |
| 642 | * |
| 643 | * It will send @data of length @len to @dst, and say it's from @src. The |
| 644 | * message will be sent to the remote processor which the @rpdev channel |
| 645 | * belongs to. |
| 646 | * |
| 647 | * The message is sent using one of the TX buffers that are available for |
| 648 | * communication with this remote processor. |
| 649 | * |
| 650 | * If @wait is true, the caller will be blocked until either a TX buffer is |
| 651 | * available, or 15 seconds elapses (we don't want callers to |
| 652 | * sleep indefinitely due to misbehaving remote processors), and in that |
| 653 | * case -ERESTARTSYS is returned. The number '15' itself was picked |
| 654 | * arbitrarily; there's little point in asking drivers to provide a timeout |
| 655 | * value themselves. |
| 656 | * |
| 657 | * Otherwise, if @wait is false, and there are no TX buffers available, |
| 658 | * the function will immediately fail, and -ENOMEM will be returned. |
| 659 | * |
| 660 | * Normally drivers shouldn't use this function directly; instead, drivers |
| 661 | * should use the appropriate rpmsg_{try}send{to, _offchannel} API |
| 662 | * (see include/linux/rpmsg.h). |
| 663 | * |
| 664 | * Returns 0 on success and an appropriate error value on failure. |
| 665 | */ |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 666 | static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev, |
| 667 | u32 src, u32 dst, |
| 668 | void *data, int len, bool wait) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 669 | { |
| 670 | struct virtproc_info *vrp = rpdev->vrp; |
| 671 | struct device *dev = &rpdev->dev; |
| 672 | struct scatterlist sg; |
| 673 | struct rpmsg_hdr *msg; |
| 674 | int err; |
| 675 | |
| 676 | /* bcasting isn't allowed */ |
| 677 | if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) { |
| 678 | dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst); |
| 679 | return -EINVAL; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * We currently use fixed-sized buffers, and therefore the payload |
| 684 | * length is limited. |
| 685 | * |
| 686 | * One of the possible improvements here is either to support |
| 687 | * user-provided buffers (and then we can also support zero-copy |
| 688 | * messaging), or to improve the buffer allocator, to support |
| 689 | * variable-length buffer sizes. |
| 690 | */ |
| 691 | if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) { |
| 692 | dev_err(dev, "message is too big (%d)\n", len); |
| 693 | return -EMSGSIZE; |
| 694 | } |
| 695 | |
| 696 | /* grab a buffer */ |
| 697 | msg = get_a_tx_buf(vrp); |
| 698 | if (!msg && !wait) |
| 699 | return -ENOMEM; |
| 700 | |
| 701 | /* no free buffer ? wait for one (but bail after 15 seconds) */ |
| 702 | while (!msg) { |
| 703 | /* enable "tx-complete" interrupts, if not already enabled */ |
| 704 | rpmsg_upref_sleepers(vrp); |
| 705 | |
| 706 | /* |
| 707 | * sleep until a free buffer is available or 15 secs elapse. |
| 708 | * the timeout period is not configurable because there's |
| 709 | * little point in asking drivers to specify that. |
| 710 | * if later this happens to be required, it'd be easy to add. |
| 711 | */ |
| 712 | err = wait_event_interruptible_timeout(vrp->sendq, |
| 713 | (msg = get_a_tx_buf(vrp)), |
| 714 | msecs_to_jiffies(15000)); |
| 715 | |
| 716 | /* disable "tx-complete" interrupts if we're the last sleeper */ |
| 717 | rpmsg_downref_sleepers(vrp); |
| 718 | |
| 719 | /* timeout ? */ |
| 720 | if (!err) { |
| 721 | dev_err(dev, "timeout waiting for a tx buffer\n"); |
| 722 | return -ERESTARTSYS; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | msg->len = len; |
| 727 | msg->flags = 0; |
| 728 | msg->src = src; |
| 729 | msg->dst = dst; |
| 730 | msg->reserved = 0; |
| 731 | memcpy(msg->data, data, len); |
| 732 | |
| 733 | dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n", |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 734 | msg->src, msg->dst, msg->len, msg->flags, msg->reserved); |
Anna, Suman | 211e3a9 | 2016-08-12 18:42:27 -0500 | [diff] [blame] | 735 | #if defined(CONFIG_DYNAMIC_DEBUG) |
| 736 | dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 737 | msg, sizeof(*msg) + msg->len, true); |
| 738 | #endif |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 739 | |
| 740 | sg_init_one(&sg, msg, sizeof(*msg) + len); |
| 741 | |
| 742 | mutex_lock(&vrp->tx_lock); |
| 743 | |
| 744 | /* add message to the remote processor's virtqueue */ |
Rusty Russell | cee51d6 | 2013-03-20 15:44:29 +1030 | [diff] [blame] | 745 | err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL); |
Rusty Russell | 57e1a37 | 2012-10-16 23:56:15 +1030 | [diff] [blame] | 746 | if (err) { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 747 | /* |
| 748 | * need to reclaim the buffer here, otherwise it's lost |
| 749 | * (memory won't leak, but rpmsg won't use it again for TX). |
| 750 | * this will wait for a buffer management overhaul. |
| 751 | */ |
Rusty Russell | cee51d6 | 2013-03-20 15:44:29 +1030 | [diff] [blame] | 752 | dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | /* tell the remote processor it has a pending message to read */ |
| 757 | virtqueue_kick(vrp->svq); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 758 | out: |
| 759 | mutex_unlock(&vrp->tx_lock); |
| 760 | return err; |
| 761 | } |
| 762 | EXPORT_SYMBOL(rpmsg_send_offchannel_raw); |
| 763 | |
Bjorn Andersson | 8a228ec | 2016-09-01 15:28:00 -0700 | [diff] [blame] | 764 | static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len) |
| 765 | { |
| 766 | struct rpmsg_device *rpdev = ept->rpdev; |
| 767 | u32 src = ept->addr, dst = rpdev->dst; |
| 768 | |
| 769 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); |
| 770 | } |
| 771 | |
| 772 | static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, |
| 773 | u32 dst) |
| 774 | { |
| 775 | struct rpmsg_device *rpdev = ept->rpdev; |
| 776 | u32 src = ept->addr; |
| 777 | |
| 778 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); |
| 779 | } |
| 780 | |
| 781 | static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, |
| 782 | u32 dst, void *data, int len) |
| 783 | { |
| 784 | struct rpmsg_device *rpdev = ept->rpdev; |
| 785 | |
| 786 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true); |
| 787 | } |
| 788 | |
| 789 | static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len) |
| 790 | { |
| 791 | struct rpmsg_device *rpdev = ept->rpdev; |
| 792 | u32 src = ept->addr, dst = rpdev->dst; |
| 793 | |
| 794 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); |
| 795 | } |
| 796 | |
| 797 | static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, |
| 798 | int len, u32 dst) |
| 799 | { |
| 800 | struct rpmsg_device *rpdev = ept->rpdev; |
| 801 | u32 src = ept->addr; |
| 802 | |
| 803 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); |
| 804 | } |
| 805 | |
| 806 | static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, |
| 807 | u32 dst, void *data, int len) |
| 808 | { |
| 809 | struct rpmsg_device *rpdev = ept->rpdev; |
| 810 | |
| 811 | return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false); |
| 812 | } |
| 813 | |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 814 | static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev, |
| 815 | struct rpmsg_hdr *msg, unsigned int len) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 816 | { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 817 | struct rpmsg_endpoint *ept; |
| 818 | struct scatterlist sg; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 819 | int err; |
| 820 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 821 | dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n", |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 822 | msg->src, msg->dst, msg->len, msg->flags, msg->reserved); |
Anna, Suman | 211e3a9 | 2016-08-12 18:42:27 -0500 | [diff] [blame] | 823 | #if defined(CONFIG_DYNAMIC_DEBUG) |
| 824 | dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1, |
| 825 | msg, sizeof(*msg) + msg->len, true); |
| 826 | #endif |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 827 | |
Ohad Ben-Cohen | 9648224 | 2012-02-28 16:16:48 +0200 | [diff] [blame] | 828 | /* |
| 829 | * We currently use fixed-sized buffers, so trivially sanitize |
| 830 | * the reported payload length. |
| 831 | */ |
| 832 | if (len > RPMSG_BUF_SIZE || |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 833 | msg->len > (len - sizeof(struct rpmsg_hdr))) { |
Ohad Ben-Cohen | 9648224 | 2012-02-28 16:16:48 +0200 | [diff] [blame] | 834 | dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len); |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 835 | return -EINVAL; |
Ohad Ben-Cohen | 9648224 | 2012-02-28 16:16:48 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 838 | /* use the dst addr to fetch the callback of the appropriate user */ |
| 839 | mutex_lock(&vrp->endpoints_lock); |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 840 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 841 | ept = idr_find(&vrp->endpoints, msg->dst); |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 842 | |
| 843 | /* let's make sure no one deallocates ept while we use it */ |
| 844 | if (ept) |
| 845 | kref_get(&ept->refcount); |
| 846 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 847 | mutex_unlock(&vrp->endpoints_lock); |
| 848 | |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 849 | if (ept) { |
| 850 | /* make sure ept->cb doesn't go away while we use it */ |
| 851 | mutex_lock(&ept->cb_lock); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 852 | |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 853 | if (ept->cb) |
| 854 | ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, |
| 855 | msg->src); |
| 856 | |
| 857 | mutex_unlock(&ept->cb_lock); |
| 858 | |
| 859 | /* farewell, ept, we don't need you anymore */ |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 860 | kref_put(&ept->refcount, __ept_release); |
Ohad Ben-Cohen | 15fd943 | 2012-06-07 15:39:35 +0300 | [diff] [blame] | 861 | } else |
Masanari Iida | 8a168ca | 2012-12-29 02:00:09 +0900 | [diff] [blame] | 862 | dev_warn(dev, "msg received with no recipient\n"); |
Ohad Ben-Cohen | 5a081ca | 2012-06-06 10:09:25 +0300 | [diff] [blame] | 863 | |
Ohad Ben-Cohen | f1d9e9c | 2012-02-28 16:11:28 +0200 | [diff] [blame] | 864 | /* publish the real size of the buffer */ |
| 865 | sg_init_one(&sg, msg, RPMSG_BUF_SIZE); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 866 | |
| 867 | /* add the buffer back to the remote processor's virtqueue */ |
Rusty Russell | cee51d6 | 2013-03-20 15:44:29 +1030 | [diff] [blame] | 868 | err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 869 | if (err < 0) { |
| 870 | dev_err(dev, "failed to add a virtqueue buffer: %d\n", err); |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 871 | return err; |
| 872 | } |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* called when an rx buffer is used, and it's time to digest a message */ |
| 878 | static void rpmsg_recv_done(struct virtqueue *rvq) |
| 879 | { |
| 880 | struct virtproc_info *vrp = rvq->vdev->priv; |
| 881 | struct device *dev = &rvq->vdev->dev; |
| 882 | struct rpmsg_hdr *msg; |
| 883 | unsigned int len, msgs_received = 0; |
| 884 | int err; |
| 885 | |
| 886 | msg = virtqueue_get_buf(rvq, &len); |
| 887 | if (!msg) { |
| 888 | dev_err(dev, "uhm, incoming signal, but no used buffer ?\n"); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 892 | while (msg) { |
| 893 | err = rpmsg_recv_single(vrp, dev, msg, len); |
| 894 | if (err) |
| 895 | break; |
| 896 | |
| 897 | msgs_received++; |
| 898 | |
| 899 | msg = virtqueue_get_buf(rvq, &len); |
Lee Jones | 6c49fbe | 2016-07-20 10:29:35 +0100 | [diff] [blame] | 900 | } |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 901 | |
| 902 | dev_dbg(dev, "Received %u messages\n", msgs_received); |
| 903 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 904 | /* tell the remote processor we added another available rx buffer */ |
Robert Tivy | 1aa7d6a | 2013-04-05 17:38:52 +0300 | [diff] [blame] | 905 | if (msgs_received) |
| 906 | virtqueue_kick(vrp->rvq); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* |
| 910 | * This is invoked whenever the remote processor completed processing |
| 911 | * a TX msg we just sent it, and the buffer is put back to the used ring. |
| 912 | * |
| 913 | * Normally, though, we suppress this "tx complete" interrupt in order to |
| 914 | * avoid the incurred overhead. |
| 915 | */ |
| 916 | static void rpmsg_xmit_done(struct virtqueue *svq) |
| 917 | { |
| 918 | struct virtproc_info *vrp = svq->vdev->priv; |
| 919 | |
| 920 | dev_dbg(&svq->vdev->dev, "%s\n", __func__); |
| 921 | |
| 922 | /* wake up potential senders that are waiting for a tx buffer */ |
| 923 | wake_up_interruptible(&vrp->sendq); |
| 924 | } |
| 925 | |
| 926 | /* invoked when a name service announcement arrives */ |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 927 | static void rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len, |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 928 | void *priv, u32 src) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 929 | { |
| 930 | struct rpmsg_ns_msg *msg = data; |
Bjorn Andersson | 92e1de5 | 2016-09-01 15:27:57 -0700 | [diff] [blame] | 931 | struct rpmsg_device *newch; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 932 | struct rpmsg_channel_info chinfo; |
| 933 | struct virtproc_info *vrp = priv; |
| 934 | struct device *dev = &vrp->vdev->dev; |
| 935 | int ret; |
| 936 | |
Anna, Suman | 211e3a9 | 2016-08-12 18:42:27 -0500 | [diff] [blame] | 937 | #if defined(CONFIG_DYNAMIC_DEBUG) |
| 938 | dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1, |
| 939 | data, len, true); |
| 940 | #endif |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 941 | |
| 942 | if (len != sizeof(*msg)) { |
| 943 | dev_err(dev, "malformed ns msg (%d)\n", len); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * the name service ept does _not_ belong to a real rpmsg channel, |
| 949 | * and is handled by the rpmsg bus itself. |
| 950 | * for sanity reasons, make sure a valid rpdev has _not_ sneaked |
| 951 | * in somehow. |
| 952 | */ |
| 953 | if (rpdev) { |
| 954 | dev_err(dev, "anomaly: ns ept has an rpdev handle\n"); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | /* don't trust the remote processor for null terminating the name */ |
| 959 | msg->name[RPMSG_NAME_SIZE - 1] = '\0'; |
| 960 | |
| 961 | dev_info(dev, "%sing channel %s addr 0x%x\n", |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 962 | msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat", |
| 963 | msg->name, msg->addr); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 964 | |
| 965 | strncpy(chinfo.name, msg->name, sizeof(chinfo.name)); |
| 966 | chinfo.src = RPMSG_ADDR_ANY; |
| 967 | chinfo.dst = msg->addr; |
| 968 | |
| 969 | if (msg->flags & RPMSG_NS_DESTROY) { |
| 970 | ret = rpmsg_destroy_channel(vrp, &chinfo); |
| 971 | if (ret) |
| 972 | dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret); |
| 973 | } else { |
| 974 | newch = rpmsg_create_channel(vrp, &chinfo); |
| 975 | if (!newch) |
| 976 | dev_err(dev, "rpmsg_create_channel failed\n"); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | static int rpmsg_probe(struct virtio_device *vdev) |
| 981 | { |
| 982 | vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done }; |
Stefan Hajnoczi | f7ad26f | 2015-12-17 16:53:43 +0800 | [diff] [blame] | 983 | static const char * const names[] = { "input", "output" }; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 984 | struct virtqueue *vqs[2]; |
| 985 | struct virtproc_info *vrp; |
| 986 | void *bufs_va; |
| 987 | int err = 0, i; |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 988 | size_t total_buf_space; |
Michael S. Tsirkin | 71e4b8b | 2015-03-12 11:54:41 +1030 | [diff] [blame] | 989 | bool notify; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 990 | |
| 991 | vrp = kzalloc(sizeof(*vrp), GFP_KERNEL); |
| 992 | if (!vrp) |
| 993 | return -ENOMEM; |
| 994 | |
| 995 | vrp->vdev = vdev; |
| 996 | |
| 997 | idr_init(&vrp->endpoints); |
| 998 | mutex_init(&vrp->endpoints_lock); |
| 999 | mutex_init(&vrp->tx_lock); |
| 1000 | init_waitqueue_head(&vrp->sendq); |
| 1001 | |
| 1002 | /* We expect two virtqueues, rx and tx (and in this order) */ |
| 1003 | err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names); |
| 1004 | if (err) |
| 1005 | goto free_vrp; |
| 1006 | |
| 1007 | vrp->rvq = vqs[0]; |
| 1008 | vrp->svq = vqs[1]; |
| 1009 | |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1010 | /* we expect symmetric tx/rx vrings */ |
| 1011 | WARN_ON(virtqueue_get_vring_size(vrp->rvq) != |
| 1012 | virtqueue_get_vring_size(vrp->svq)); |
| 1013 | |
| 1014 | /* we need less buffers if vrings are small */ |
| 1015 | if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2) |
| 1016 | vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2; |
| 1017 | else |
| 1018 | vrp->num_bufs = MAX_RPMSG_NUM_BUFS; |
| 1019 | |
| 1020 | total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE; |
| 1021 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1022 | /* allocate coherent memory for the buffers */ |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 1023 | bufs_va = dma_alloc_coherent(vdev->dev.parent->parent, |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1024 | total_buf_space, &vrp->bufs_dma, |
| 1025 | GFP_KERNEL); |
Wei Yongjun | 3119b48 | 2013-04-29 16:17:09 -0700 | [diff] [blame] | 1026 | if (!bufs_va) { |
| 1027 | err = -ENOMEM; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1028 | goto vqs_del; |
Wei Yongjun | 3119b48 | 2013-04-29 16:17:09 -0700 | [diff] [blame] | 1029 | } |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1030 | |
Anna, Suman | 8d95b32 | 2016-08-12 18:42:25 -0500 | [diff] [blame] | 1031 | dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n", |
| 1032 | bufs_va, &vrp->bufs_dma); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* half of the buffers is dedicated for RX */ |
| 1035 | vrp->rbufs = bufs_va; |
| 1036 | |
| 1037 | /* and half is dedicated for TX */ |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1038 | vrp->sbufs = bufs_va + total_buf_space / 2; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* set up the receive buffers */ |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1041 | for (i = 0; i < vrp->num_bufs / 2; i++) { |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1042 | struct scatterlist sg; |
| 1043 | void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE; |
| 1044 | |
| 1045 | sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE); |
| 1046 | |
Rusty Russell | cee51d6 | 2013-03-20 15:44:29 +1030 | [diff] [blame] | 1047 | err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr, |
Anna, Suman | 0963679 | 2016-08-12 18:42:26 -0500 | [diff] [blame] | 1048 | GFP_KERNEL); |
Rusty Russell | 57e1a37 | 2012-10-16 23:56:15 +1030 | [diff] [blame] | 1049 | WARN_ON(err); /* sanity check; this can't really happen */ |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* suppress "tx-complete" interrupts */ |
| 1053 | virtqueue_disable_cb(vrp->svq); |
| 1054 | |
| 1055 | vdev->priv = vrp; |
| 1056 | |
| 1057 | /* if supported by the remote processor, enable the name service */ |
| 1058 | if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) { |
| 1059 | /* a dedicated endpoint handles the name service msgs */ |
| 1060 | vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb, |
| 1061 | vrp, RPMSG_NS_ADDR); |
| 1062 | if (!vrp->ns_ept) { |
| 1063 | dev_err(&vdev->dev, "failed to create the ns ept\n"); |
| 1064 | err = -ENOMEM; |
| 1065 | goto free_coherent; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Michael S. Tsirkin | 71e4b8b | 2015-03-12 11:54:41 +1030 | [diff] [blame] | 1069 | /* |
| 1070 | * Prepare to kick but don't notify yet - we can't do this before |
| 1071 | * device is ready. |
| 1072 | */ |
| 1073 | notify = virtqueue_kick_prepare(vrp->rvq); |
| 1074 | |
| 1075 | /* From this point on, we can notify and get callbacks. */ |
| 1076 | virtio_device_ready(vdev); |
| 1077 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1078 | /* tell the remote processor it can start sending messages */ |
Michael S. Tsirkin | 71e4b8b | 2015-03-12 11:54:41 +1030 | [diff] [blame] | 1079 | /* |
| 1080 | * this might be concurrent with callbacks, but we are only |
| 1081 | * doing notify, not a full kick here, so that's ok. |
| 1082 | */ |
| 1083 | if (notify) |
| 1084 | virtqueue_notify(vrp->rvq); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1085 | |
| 1086 | dev_info(&vdev->dev, "rpmsg host is online\n"); |
| 1087 | |
| 1088 | return 0; |
| 1089 | |
| 1090 | free_coherent: |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1091 | dma_free_coherent(vdev->dev.parent->parent, total_buf_space, |
| 1092 | bufs_va, vrp->bufs_dma); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1093 | vqs_del: |
| 1094 | vdev->config->del_vqs(vrp->vdev); |
| 1095 | free_vrp: |
| 1096 | kfree(vrp); |
| 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | static int rpmsg_remove_device(struct device *dev, void *data) |
| 1101 | { |
| 1102 | device_unregister(dev); |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1107 | static void rpmsg_remove(struct virtio_device *vdev) |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1108 | { |
| 1109 | struct virtproc_info *vrp = vdev->priv; |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1110 | size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE; |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1111 | int ret; |
| 1112 | |
| 1113 | vdev->config->reset(vdev); |
| 1114 | |
| 1115 | ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device); |
| 1116 | if (ret) |
| 1117 | dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret); |
| 1118 | |
Ohad Ben-Cohen | fa2d779 | 2012-02-09 15:16:41 +0200 | [diff] [blame] | 1119 | if (vrp->ns_ept) |
| 1120 | __rpmsg_destroy_ept(vrp, vrp->ns_ept); |
| 1121 | |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1122 | idr_destroy(&vrp->endpoints); |
| 1123 | |
| 1124 | vdev->config->del_vqs(vrp->vdev); |
| 1125 | |
Suman Anna | b1b9891 | 2014-09-16 13:33:07 -0500 | [diff] [blame] | 1126 | dma_free_coherent(vdev->dev.parent->parent, total_buf_space, |
| 1127 | vrp->rbufs, vrp->bufs_dma); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1128 | |
| 1129 | kfree(vrp); |
| 1130 | } |
| 1131 | |
| 1132 | static struct virtio_device_id id_table[] = { |
| 1133 | { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID }, |
| 1134 | { 0 }, |
| 1135 | }; |
| 1136 | |
| 1137 | static unsigned int features[] = { |
| 1138 | VIRTIO_RPMSG_F_NS, |
| 1139 | }; |
| 1140 | |
| 1141 | static struct virtio_driver virtio_ipc_driver = { |
| 1142 | .feature_table = features, |
| 1143 | .feature_table_size = ARRAY_SIZE(features), |
| 1144 | .driver.name = KBUILD_MODNAME, |
| 1145 | .driver.owner = THIS_MODULE, |
| 1146 | .id_table = id_table, |
| 1147 | .probe = rpmsg_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 1148 | .remove = rpmsg_remove, |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1149 | }; |
| 1150 | |
| 1151 | static int __init rpmsg_init(void) |
| 1152 | { |
| 1153 | int ret; |
| 1154 | |
| 1155 | ret = bus_register(&rpmsg_bus); |
| 1156 | if (ret) { |
| 1157 | pr_err("failed to register rpmsg bus: %d\n", ret); |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
| 1161 | ret = register_virtio_driver(&virtio_ipc_driver); |
| 1162 | if (ret) { |
| 1163 | pr_err("failed to register virtio driver: %d\n", ret); |
| 1164 | bus_unregister(&rpmsg_bus); |
| 1165 | } |
| 1166 | |
| 1167 | return ret; |
| 1168 | } |
Federico Fuga | 9634252 | 2012-07-16 10:36:51 +0300 | [diff] [blame] | 1169 | subsys_initcall(rpmsg_init); |
Ohad Ben-Cohen | bcabbcc | 2011-10-20 21:10:55 +0200 | [diff] [blame] | 1170 | |
| 1171 | static void __exit rpmsg_fini(void) |
| 1172 | { |
| 1173 | unregister_virtio_driver(&virtio_ipc_driver); |
| 1174 | bus_unregister(&rpmsg_bus); |
| 1175 | } |
| 1176 | module_exit(rpmsg_fini); |
| 1177 | |
| 1178 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 1179 | MODULE_DESCRIPTION("Virtio-based remote processor messaging bus"); |
| 1180 | MODULE_LICENSE("GPL v2"); |