virtio_rpmsg_bus.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtio-based remote processor messaging bus
  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>
  10. */
  11. #define pr_fmt(fmt) "%s: " fmt, __func__
  12. #include <linux/dma-mapping.h>
  13. #include <linux/idr.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rpmsg.h>
  19. #include <linux/rpmsg/byteorder.h>
  20. #include <linux/rpmsg/ns.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched.h>
  24. #include <linux/virtio.h>
  25. #include <linux/virtio_ids.h>
  26. #include <linux/virtio_config.h>
  27. #include <linux/wait.h>
  28. #include "rpmsg_internal.h"
  29. /**
  30. * struct virtproc_info - virtual remote processor state
  31. * @vdev: the virtio device
  32. * @rvq: rx virtqueue
  33. * @svq: tx virtqueue
  34. * @rbufs: kernel address of rx buffers
  35. * @sbufs: kernel address of tx buffers
  36. * @num_bufs: total number of buffers for rx and tx
  37. * @buf_size: size of one rx or tx buffer
  38. * @last_sbuf: index of last tx buffer used
  39. * @bufs_dma: dma base addr of the buffers
  40. * @tx_lock: protects svq and sbufs, to allow concurrent senders.
  41. * sending a message might require waking up a dozing remote
  42. * processor, which involves sleeping, hence the mutex.
  43. * @endpoints: idr of local endpoints, allows fast retrieval
  44. * @endpoints_lock: lock of the endpoints set
  45. * @sendq: wait queue of sending contexts waiting for a tx buffers
  46. *
  47. * This structure stores the rpmsg state of a given virtio remote processor
  48. * device (there might be several virtio proc devices for each physical
  49. * remote processor).
  50. */
  51. struct virtproc_info {
  52. struct virtio_device *vdev;
  53. struct virtqueue *rvq, *svq;
  54. void *rbufs, *sbufs;
  55. unsigned int num_bufs;
  56. unsigned int buf_size;
  57. int last_sbuf;
  58. dma_addr_t bufs_dma;
  59. struct mutex tx_lock;
  60. struct idr endpoints;
  61. struct mutex endpoints_lock;
  62. wait_queue_head_t sendq;
  63. };
  64. /* The feature bitmap for virtio rpmsg */
  65. #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
  66. /**
  67. * struct rpmsg_hdr - common header for all rpmsg messages
  68. * @src: source address
  69. * @dst: destination address
  70. * @reserved: reserved for future use
  71. * @len: length of payload (in bytes)
  72. * @flags: message flags
  73. * @data: @len bytes of message payload data
  74. *
  75. * Every message sent(/received) on the rpmsg bus begins with this header.
  76. */
  77. struct rpmsg_hdr {
  78. __rpmsg32 src;
  79. __rpmsg32 dst;
  80. __rpmsg32 reserved;
  81. __rpmsg16 len;
  82. __rpmsg16 flags;
  83. u8 data[];
  84. } __packed;
  85. /**
  86. * struct virtio_rpmsg_channel - rpmsg channel descriptor
  87. * @rpdev: the rpmsg channel device
  88. * @vrp: the virtio remote processor device this channel belongs to
  89. *
  90. * This structure stores the channel that links the rpmsg device to the virtio
  91. * remote processor device.
  92. */
  93. struct virtio_rpmsg_channel {
  94. struct rpmsg_device rpdev;
  95. struct virtproc_info *vrp;
  96. };
  97. #define to_virtio_rpmsg_channel(_rpdev) \
  98. container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
  99. /*
  100. * We're allocating buffers of 512 bytes each for communications. The
  101. * number of buffers will be computed from the number of buffers supported
  102. * by the vring, upto a maximum of 512 buffers (256 in each direction).
  103. *
  104. * Each buffer will have 16 bytes for the msg header and 496 bytes for
  105. * the payload.
  106. *
  107. * This will utilize a maximum total space of 256KB for the buffers.
  108. *
  109. * We might also want to add support for user-provided buffers in time.
  110. * This will allow bigger buffer size flexibility, and can also be used
  111. * to achieve zero-copy messaging.
  112. *
  113. * Note that these numbers are purely a decision of this driver - we
  114. * can change this without changing anything in the firmware of the remote
  115. * processor.
  116. */
  117. #define MAX_RPMSG_NUM_BUFS (512)
  118. #define MAX_RPMSG_BUF_SIZE (512)
  119. /*
  120. * Local addresses are dynamically allocated on-demand.
  121. * We do not dynamically assign addresses from the low 1024 range,
  122. * in order to reserve that address range for predefined services.
  123. */
  124. #define RPMSG_RESERVED_ADDRESSES (1024)
  125. static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
  126. static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
  127. static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  128. u32 dst);
  129. static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
  130. static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  131. int len, u32 dst);
  132. static __poll_t virtio_rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  133. poll_table *wait);
  134. static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept);
  135. static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
  136. struct rpmsg_channel_info *chinfo);
  137. static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
  138. .destroy_ept = virtio_rpmsg_destroy_ept,
  139. .send = virtio_rpmsg_send,
  140. .sendto = virtio_rpmsg_sendto,
  141. .trysend = virtio_rpmsg_trysend,
  142. .trysendto = virtio_rpmsg_trysendto,
  143. .poll = virtio_rpmsg_poll,
  144. .get_mtu = virtio_rpmsg_get_mtu,
  145. };
  146. /**
  147. * rpmsg_sg_init - initialize scatterlist according to cpu address location
  148. * @sg: scatterlist to fill
  149. * @cpu_addr: virtual address of the buffer
  150. * @len: buffer length
  151. *
  152. * An internal function filling scatterlist according to virtual address
  153. * location (in vmalloc or in kernel).
  154. */
  155. static void
  156. rpmsg_sg_init(struct scatterlist *sg, void *cpu_addr, unsigned int len)
  157. {
  158. if (is_vmalloc_addr(cpu_addr)) {
  159. sg_init_table(sg, 1);
  160. sg_set_page(sg, vmalloc_to_page(cpu_addr), len,
  161. offset_in_page(cpu_addr));
  162. } else {
  163. WARN_ON(!virt_addr_valid(cpu_addr));
  164. sg_init_one(sg, cpu_addr, len);
  165. }
  166. }
  167. /**
  168. * __ept_release() - deallocate an rpmsg endpoint
  169. * @kref: the ept's reference count
  170. *
  171. * This function deallocates an ept, and is invoked when its @kref refcount
  172. * drops to zero.
  173. *
  174. * Never invoke this function directly!
  175. */
  176. static void __ept_release(struct kref *kref)
  177. {
  178. struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
  179. refcount);
  180. /*
  181. * At this point no one holds a reference to ept anymore,
  182. * so we can directly free it
  183. */
  184. kfree(ept);
  185. }
  186. /* for more info, see below documentation of rpmsg_create_ept() */
  187. static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
  188. struct rpmsg_device *rpdev,
  189. rpmsg_rx_cb_t cb,
  190. void *priv, u32 addr)
  191. {
  192. int id_min, id_max, id;
  193. struct rpmsg_endpoint *ept;
  194. struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
  195. ept = kzalloc_obj(*ept);
  196. if (!ept)
  197. return NULL;
  198. kref_init(&ept->refcount);
  199. mutex_init(&ept->cb_lock);
  200. ept->rpdev = rpdev;
  201. ept->cb = cb;
  202. ept->priv = priv;
  203. ept->ops = &virtio_endpoint_ops;
  204. /* do we need to allocate a local address ? */
  205. if (addr == RPMSG_ADDR_ANY) {
  206. id_min = RPMSG_RESERVED_ADDRESSES;
  207. id_max = 0;
  208. } else {
  209. id_min = addr;
  210. id_max = addr + 1;
  211. }
  212. mutex_lock(&vrp->endpoints_lock);
  213. /* bind the endpoint to an rpmsg address (and allocate one if needed) */
  214. id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
  215. if (id < 0) {
  216. dev_err(dev, "idr_alloc failed: %d\n", id);
  217. goto free_ept;
  218. }
  219. ept->addr = id;
  220. mutex_unlock(&vrp->endpoints_lock);
  221. return ept;
  222. free_ept:
  223. mutex_unlock(&vrp->endpoints_lock);
  224. kref_put(&ept->refcount, __ept_release);
  225. return NULL;
  226. }
  227. static struct rpmsg_device *virtio_rpmsg_create_channel(struct rpmsg_device *rpdev,
  228. struct rpmsg_channel_info *chinfo)
  229. {
  230. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  231. struct virtproc_info *vrp = vch->vrp;
  232. return __rpmsg_create_channel(vrp, chinfo);
  233. }
  234. static int virtio_rpmsg_release_channel(struct rpmsg_device *rpdev,
  235. struct rpmsg_channel_info *chinfo)
  236. {
  237. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  238. struct virtproc_info *vrp = vch->vrp;
  239. return rpmsg_unregister_device(&vrp->vdev->dev, chinfo);
  240. }
  241. static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
  242. rpmsg_rx_cb_t cb,
  243. void *priv,
  244. struct rpmsg_channel_info chinfo)
  245. {
  246. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  247. return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
  248. }
  249. /**
  250. * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
  251. * @vrp: virtproc which owns this ept
  252. * @ept: endpoing to destroy
  253. *
  254. * An internal function which destroy an ept without assuming it is
  255. * bound to an rpmsg channel. This is needed for handling the internal
  256. * name service endpoint, which isn't bound to an rpmsg channel.
  257. * See also __rpmsg_create_ept().
  258. */
  259. static void
  260. __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
  261. {
  262. /* make sure new inbound messages can't find this ept anymore */
  263. mutex_lock(&vrp->endpoints_lock);
  264. idr_remove(&vrp->endpoints, ept->addr);
  265. mutex_unlock(&vrp->endpoints_lock);
  266. /* make sure in-flight inbound messages won't invoke cb anymore */
  267. mutex_lock(&ept->cb_lock);
  268. ept->cb = NULL;
  269. mutex_unlock(&ept->cb_lock);
  270. kref_put(&ept->refcount, __ept_release);
  271. }
  272. static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  273. {
  274. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
  275. __rpmsg_destroy_ept(vch->vrp, ept);
  276. }
  277. static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
  278. {
  279. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  280. struct virtproc_info *vrp = vch->vrp;
  281. struct device *dev = &rpdev->dev;
  282. int err = 0;
  283. /* need to tell remote processor's name service about this channel ? */
  284. if (rpdev->announce && rpdev->ept &&
  285. virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
  286. struct rpmsg_ns_msg nsm;
  287. strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
  288. nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
  289. nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_CREATE);
  290. err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
  291. if (err)
  292. dev_err(dev, "failed to announce service %d\n", err);
  293. }
  294. return err;
  295. }
  296. static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
  297. {
  298. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  299. struct virtproc_info *vrp = vch->vrp;
  300. struct device *dev = &rpdev->dev;
  301. int err = 0;
  302. /* tell remote processor's name service we're removing this channel */
  303. if (rpdev->announce && rpdev->ept &&
  304. virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
  305. struct rpmsg_ns_msg nsm;
  306. strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
  307. nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
  308. nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_DESTROY);
  309. err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
  310. if (err)
  311. dev_err(dev, "failed to announce service %d\n", err);
  312. }
  313. return err;
  314. }
  315. static const struct rpmsg_device_ops virtio_rpmsg_ops = {
  316. .create_channel = virtio_rpmsg_create_channel,
  317. .release_channel = virtio_rpmsg_release_channel,
  318. .create_ept = virtio_rpmsg_create_ept,
  319. .announce_create = virtio_rpmsg_announce_create,
  320. .announce_destroy = virtio_rpmsg_announce_destroy,
  321. };
  322. static void virtio_rpmsg_release_device(struct device *dev)
  323. {
  324. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  325. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  326. kfree(rpdev->driver_override);
  327. kfree(vch);
  328. }
  329. /*
  330. * create an rpmsg channel using its name and address info.
  331. * this function will be used to create both static and dynamic
  332. * channels.
  333. */
  334. static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
  335. struct rpmsg_channel_info *chinfo)
  336. {
  337. struct virtio_rpmsg_channel *vch;
  338. struct rpmsg_device *rpdev;
  339. struct device *tmp, *dev = &vrp->vdev->dev;
  340. int ret;
  341. /* make sure a similar channel doesn't already exist */
  342. tmp = rpmsg_find_device(dev, chinfo);
  343. if (tmp) {
  344. /* decrement the matched device's refcount back */
  345. put_device(tmp);
  346. dev_err(dev, "channel %s:%x:%x already exist\n",
  347. chinfo->name, chinfo->src, chinfo->dst);
  348. return NULL;
  349. }
  350. vch = kzalloc_obj(*vch);
  351. if (!vch)
  352. return NULL;
  353. /* Link the channel to our vrp */
  354. vch->vrp = vrp;
  355. /* Assign public information to the rpmsg_device */
  356. rpdev = &vch->rpdev;
  357. rpdev->src = chinfo->src;
  358. rpdev->dst = chinfo->dst;
  359. rpdev->ops = &virtio_rpmsg_ops;
  360. rpdev->little_endian = virtio_is_little_endian(vrp->vdev);
  361. /*
  362. * rpmsg server channels has predefined local address (for now),
  363. * and their existence needs to be announced remotely
  364. */
  365. rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
  366. strscpy(rpdev->id.name, chinfo->name, sizeof(rpdev->id.name));
  367. rpdev->dev.parent = &vrp->vdev->dev;
  368. rpdev->dev.release = virtio_rpmsg_release_device;
  369. ret = rpmsg_register_device(rpdev);
  370. if (ret)
  371. return NULL;
  372. return rpdev;
  373. }
  374. /* super simple buffer "allocator" that is just enough for now */
  375. static void *get_a_tx_buf(struct virtproc_info *vrp)
  376. {
  377. unsigned int len;
  378. void *ret;
  379. mutex_lock(&vrp->tx_lock);
  380. /*
  381. * either pick the next unused tx buffer
  382. * (half of our buffers are used for sending messages)
  383. */
  384. if (vrp->last_sbuf < vrp->num_bufs / 2)
  385. ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
  386. /* or recycle a used one */
  387. else
  388. ret = virtqueue_get_buf(vrp->svq, &len);
  389. mutex_unlock(&vrp->tx_lock);
  390. return ret;
  391. }
  392. /**
  393. * rpmsg_send_offchannel_raw() - send a message across to the remote processor
  394. * @rpdev: the rpmsg channel
  395. * @src: source address
  396. * @dst: destination address
  397. * @data: payload of message
  398. * @len: length of payload
  399. * @wait: indicates whether caller should block in case no TX buffers available
  400. *
  401. * This function is the base implementation for all of the rpmsg sending API.
  402. *
  403. * It will send @data of length @len to @dst, and say it's from @src. The
  404. * message will be sent to the remote processor which the @rpdev channel
  405. * belongs to.
  406. *
  407. * The message is sent using one of the TX buffers that are available for
  408. * communication with this remote processor.
  409. *
  410. * If @wait is true, the caller will be blocked until either a TX buffer is
  411. * available, or 15 seconds elapses (we don't want callers to
  412. * sleep indefinitely due to misbehaving remote processors), and in that
  413. * case -ERESTARTSYS is returned. The number '15' itself was picked
  414. * arbitrarily; there's little point in asking drivers to provide a timeout
  415. * value themselves.
  416. *
  417. * Otherwise, if @wait is false, and there are no TX buffers available,
  418. * the function will immediately fail, and -ENOMEM will be returned.
  419. *
  420. * Normally drivers shouldn't use this function directly; instead, drivers
  421. * should use the appropriate rpmsg_{try}send{to} API
  422. * (see include/linux/rpmsg.h).
  423. *
  424. * Return: 0 on success and an appropriate error value on failure.
  425. */
  426. static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
  427. u32 src, u32 dst,
  428. void *data, int len, bool wait)
  429. {
  430. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  431. struct virtproc_info *vrp = vch->vrp;
  432. struct device *dev = &rpdev->dev;
  433. struct scatterlist sg;
  434. struct rpmsg_hdr *msg;
  435. int err;
  436. /* bcasting isn't allowed */
  437. if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
  438. dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
  439. return -EINVAL;
  440. }
  441. /*
  442. * We currently use fixed-sized buffers, and therefore the payload
  443. * length is limited.
  444. *
  445. * One of the possible improvements here is either to support
  446. * user-provided buffers (and then we can also support zero-copy
  447. * messaging), or to improve the buffer allocator, to support
  448. * variable-length buffer sizes.
  449. */
  450. if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
  451. dev_err(dev, "message is too big (%d)\n", len);
  452. return -EMSGSIZE;
  453. }
  454. /* grab a buffer */
  455. msg = get_a_tx_buf(vrp);
  456. if (!msg && !wait)
  457. return -ENOMEM;
  458. /* no free buffer ? wait for one (but bail after 15 seconds) */
  459. while (!msg) {
  460. /*
  461. * sleep until a free buffer is available or 15 secs elapse.
  462. * the timeout period is not configurable because there's
  463. * little point in asking drivers to specify that.
  464. * if later this happens to be required, it'd be easy to add.
  465. */
  466. err = wait_event_interruptible_timeout(vrp->sendq,
  467. (msg = get_a_tx_buf(vrp)),
  468. msecs_to_jiffies(15000));
  469. /* timeout ? */
  470. if (!err) {
  471. dev_err(dev, "timeout waiting for a tx buffer\n");
  472. return -ERESTARTSYS;
  473. }
  474. }
  475. msg->len = cpu_to_rpmsg16(rpdev, len);
  476. msg->flags = 0;
  477. msg->src = cpu_to_rpmsg32(rpdev, src);
  478. msg->dst = cpu_to_rpmsg32(rpdev, dst);
  479. msg->reserved = 0;
  480. memcpy(msg->data, data, len);
  481. dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
  482. src, dst, len, msg->flags, msg->reserved);
  483. #if defined(CONFIG_DYNAMIC_DEBUG)
  484. dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
  485. msg, sizeof(*msg) + len, true);
  486. #endif
  487. rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
  488. mutex_lock(&vrp->tx_lock);
  489. /* add message to the remote processor's virtqueue */
  490. err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
  491. if (err) {
  492. /*
  493. * need to reclaim the buffer here, otherwise it's lost
  494. * (memory won't leak, but rpmsg won't use it again for TX).
  495. * this will wait for a buffer management overhaul.
  496. */
  497. dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
  498. goto out;
  499. }
  500. /* tell the remote processor it has a pending message to read */
  501. virtqueue_kick(vrp->svq);
  502. out:
  503. mutex_unlock(&vrp->tx_lock);
  504. return err;
  505. }
  506. static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  507. {
  508. struct rpmsg_device *rpdev = ept->rpdev;
  509. u32 src = ept->addr, dst = rpdev->dst;
  510. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  511. }
  512. static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  513. u32 dst)
  514. {
  515. struct rpmsg_device *rpdev = ept->rpdev;
  516. u32 src = ept->addr;
  517. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  518. }
  519. static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  520. {
  521. struct rpmsg_device *rpdev = ept->rpdev;
  522. u32 src = ept->addr, dst = rpdev->dst;
  523. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  524. }
  525. static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  526. int len, u32 dst)
  527. {
  528. struct rpmsg_device *rpdev = ept->rpdev;
  529. u32 src = ept->addr;
  530. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  531. }
  532. static __poll_t virtio_rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp,
  533. poll_table *wait)
  534. {
  535. struct rpmsg_device *rpdev = ept->rpdev;
  536. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  537. struct virtproc_info *vrp = vch->vrp;
  538. __poll_t mask = 0;
  539. poll_wait(filp, &vrp->sendq, wait);
  540. /* support multiple concurrent senders */
  541. mutex_lock(&vrp->tx_lock);
  542. /*
  543. * check for a free buffer, either:
  544. * - we haven't used all of the available transmit buffers (half of the
  545. * allocated buffers are used for transmit, hence num_bufs / 2), or,
  546. * - we ask the virtqueue if there's a buffer available
  547. */
  548. if (vrp->last_sbuf < vrp->num_bufs / 2 ||
  549. !virtqueue_enable_cb(vrp->svq))
  550. mask |= EPOLLOUT;
  551. mutex_unlock(&vrp->tx_lock);
  552. return mask;
  553. }
  554. static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept)
  555. {
  556. struct rpmsg_device *rpdev = ept->rpdev;
  557. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  558. return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
  559. }
  560. static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
  561. struct rpmsg_hdr *msg, unsigned int len)
  562. {
  563. struct rpmsg_endpoint *ept;
  564. struct scatterlist sg;
  565. bool little_endian = virtio_is_little_endian(vrp->vdev);
  566. unsigned int msg_len = __rpmsg16_to_cpu(little_endian, msg->len);
  567. int err;
  568. dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
  569. __rpmsg32_to_cpu(little_endian, msg->src),
  570. __rpmsg32_to_cpu(little_endian, msg->dst), msg_len,
  571. __rpmsg16_to_cpu(little_endian, msg->flags),
  572. __rpmsg32_to_cpu(little_endian, msg->reserved));
  573. #if defined(CONFIG_DYNAMIC_DEBUG)
  574. dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
  575. msg, sizeof(*msg) + msg_len, true);
  576. #endif
  577. /*
  578. * We currently use fixed-sized buffers, so trivially sanitize
  579. * the reported payload length.
  580. */
  581. if (len > vrp->buf_size ||
  582. msg_len > (len - sizeof(struct rpmsg_hdr))) {
  583. dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
  584. return -EINVAL;
  585. }
  586. /* use the dst addr to fetch the callback of the appropriate user */
  587. mutex_lock(&vrp->endpoints_lock);
  588. ept = idr_find(&vrp->endpoints, __rpmsg32_to_cpu(little_endian, msg->dst));
  589. /* let's make sure no one deallocates ept while we use it */
  590. if (ept)
  591. kref_get(&ept->refcount);
  592. mutex_unlock(&vrp->endpoints_lock);
  593. if (ept) {
  594. /* make sure ept->cb doesn't go away while we use it */
  595. mutex_lock(&ept->cb_lock);
  596. if (ept->cb)
  597. ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
  598. __rpmsg32_to_cpu(little_endian, msg->src));
  599. mutex_unlock(&ept->cb_lock);
  600. /* farewell, ept, we don't need you anymore */
  601. kref_put(&ept->refcount, __ept_release);
  602. } else
  603. dev_warn_ratelimited(dev, "msg received with no recipient\n");
  604. /* publish the real size of the buffer */
  605. rpmsg_sg_init(&sg, msg, vrp->buf_size);
  606. /* add the buffer back to the remote processor's virtqueue */
  607. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
  608. if (err < 0) {
  609. dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
  610. return err;
  611. }
  612. return 0;
  613. }
  614. /* called when an rx buffer is used, and it's time to digest a message */
  615. static void rpmsg_recv_done(struct virtqueue *rvq)
  616. {
  617. struct virtproc_info *vrp = rvq->vdev->priv;
  618. struct device *dev = &rvq->vdev->dev;
  619. struct rpmsg_hdr *msg;
  620. unsigned int len, msgs_received = 0;
  621. int err;
  622. msg = virtqueue_get_buf(rvq, &len);
  623. if (!msg) {
  624. dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
  625. return;
  626. }
  627. while (msg) {
  628. err = rpmsg_recv_single(vrp, dev, msg, len);
  629. if (err)
  630. break;
  631. msgs_received++;
  632. msg = virtqueue_get_buf(rvq, &len);
  633. }
  634. dev_dbg(dev, "Received %u messages\n", msgs_received);
  635. /* tell the remote processor we added another available rx buffer */
  636. if (msgs_received)
  637. virtqueue_kick(vrp->rvq);
  638. }
  639. /*
  640. * This is invoked whenever the remote processor completed processing
  641. * a TX msg we just sent it, and the buffer is put back to the used ring.
  642. *
  643. * Normally, though, we suppress this "tx complete" interrupt in order to
  644. * avoid the incurred overhead.
  645. */
  646. static void rpmsg_xmit_done(struct virtqueue *svq)
  647. {
  648. struct virtproc_info *vrp = svq->vdev->priv;
  649. dev_dbg(&svq->vdev->dev, "%s\n", __func__);
  650. /* wake up potential senders that are waiting for a tx buffer */
  651. wake_up_interruptible(&vrp->sendq);
  652. }
  653. /*
  654. * Called to expose to user a /dev/rpmsg_ctrlX interface allowing to
  655. * create endpoint-to-endpoint communication without associated RPMsg channel.
  656. * The endpoints are rattached to the ctrldev RPMsg device.
  657. */
  658. static struct rpmsg_device *rpmsg_virtio_add_ctrl_dev(struct virtio_device *vdev)
  659. {
  660. struct virtproc_info *vrp = vdev->priv;
  661. struct virtio_rpmsg_channel *vch;
  662. struct rpmsg_device *rpdev_ctrl;
  663. int err = 0;
  664. vch = kzalloc_obj(*vch);
  665. if (!vch)
  666. return ERR_PTR(-ENOMEM);
  667. /* Link the channel to the vrp */
  668. vch->vrp = vrp;
  669. /* Assign public information to the rpmsg_device */
  670. rpdev_ctrl = &vch->rpdev;
  671. rpdev_ctrl->ops = &virtio_rpmsg_ops;
  672. rpdev_ctrl->dev.parent = &vrp->vdev->dev;
  673. rpdev_ctrl->dev.release = virtio_rpmsg_release_device;
  674. rpdev_ctrl->little_endian = virtio_is_little_endian(vrp->vdev);
  675. err = rpmsg_ctrldev_register_device(rpdev_ctrl);
  676. if (err) {
  677. /* vch will be free in virtio_rpmsg_release_device() */
  678. return ERR_PTR(err);
  679. }
  680. return rpdev_ctrl;
  681. }
  682. static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
  683. {
  684. if (!rpdev_ctrl)
  685. return;
  686. device_unregister(&rpdev_ctrl->dev);
  687. }
  688. static int rpmsg_probe(struct virtio_device *vdev)
  689. {
  690. struct virtqueue_info vqs_info[] = {
  691. { "input", rpmsg_recv_done },
  692. { "output", rpmsg_xmit_done },
  693. };
  694. struct virtqueue *vqs[2];
  695. struct virtproc_info *vrp;
  696. struct virtio_rpmsg_channel *vch = NULL;
  697. struct rpmsg_device *rpdev_ns, *rpdev_ctrl;
  698. void *bufs_va;
  699. int err = 0, i;
  700. size_t total_buf_space;
  701. bool notify;
  702. vrp = kzalloc_obj(*vrp);
  703. if (!vrp)
  704. return -ENOMEM;
  705. vrp->vdev = vdev;
  706. idr_init(&vrp->endpoints);
  707. mutex_init(&vrp->endpoints_lock);
  708. mutex_init(&vrp->tx_lock);
  709. init_waitqueue_head(&vrp->sendq);
  710. /* We expect two virtqueues, rx and tx (and in this order) */
  711. err = virtio_find_vqs(vdev, 2, vqs, vqs_info, NULL);
  712. if (err)
  713. goto free_vrp;
  714. vrp->rvq = vqs[0];
  715. vrp->svq = vqs[1];
  716. /* we expect symmetric tx/rx vrings */
  717. WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
  718. virtqueue_get_vring_size(vrp->svq));
  719. /* we need less buffers if vrings are small */
  720. if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
  721. vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
  722. else
  723. vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
  724. vrp->buf_size = MAX_RPMSG_BUF_SIZE;
  725. total_buf_space = vrp->num_bufs * vrp->buf_size;
  726. /* allocate coherent memory for the buffers */
  727. bufs_va = dma_alloc_coherent(vdev->dev.parent,
  728. total_buf_space, &vrp->bufs_dma,
  729. GFP_KERNEL);
  730. if (!bufs_va) {
  731. err = -ENOMEM;
  732. goto vqs_del;
  733. }
  734. dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
  735. bufs_va, &vrp->bufs_dma);
  736. /* half of the buffers is dedicated for RX */
  737. vrp->rbufs = bufs_va;
  738. /* and half is dedicated for TX */
  739. vrp->sbufs = bufs_va + total_buf_space / 2;
  740. /* set up the receive buffers */
  741. for (i = 0; i < vrp->num_bufs / 2; i++) {
  742. struct scatterlist sg;
  743. void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
  744. rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
  745. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
  746. GFP_KERNEL);
  747. WARN_ON(err); /* sanity check; this can't really happen */
  748. }
  749. vdev->priv = vrp;
  750. rpdev_ctrl = rpmsg_virtio_add_ctrl_dev(vdev);
  751. if (IS_ERR(rpdev_ctrl)) {
  752. err = PTR_ERR(rpdev_ctrl);
  753. goto free_coherent;
  754. }
  755. /* if supported by the remote processor, enable the name service */
  756. if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
  757. vch = kzalloc_obj(*vch);
  758. if (!vch) {
  759. err = -ENOMEM;
  760. goto free_ctrldev;
  761. }
  762. /* Link the channel to our vrp */
  763. vch->vrp = vrp;
  764. /* Assign public information to the rpmsg_device */
  765. rpdev_ns = &vch->rpdev;
  766. rpdev_ns->ops = &virtio_rpmsg_ops;
  767. rpdev_ns->little_endian = virtio_is_little_endian(vrp->vdev);
  768. rpdev_ns->dev.parent = &vrp->vdev->dev;
  769. rpdev_ns->dev.release = virtio_rpmsg_release_device;
  770. err = rpmsg_ns_register_device(rpdev_ns);
  771. if (err)
  772. /* vch will be free in virtio_rpmsg_release_device() */
  773. goto free_ctrldev;
  774. }
  775. /*
  776. * Prepare to kick but don't notify yet - we can't do this before
  777. * device is ready.
  778. */
  779. notify = virtqueue_kick_prepare(vrp->rvq);
  780. /* From this point on, we can notify and get callbacks. */
  781. virtio_device_ready(vdev);
  782. /* tell the remote processor it can start sending messages */
  783. /*
  784. * this might be concurrent with callbacks, but we are only
  785. * doing notify, not a full kick here, so that's ok.
  786. */
  787. if (notify)
  788. virtqueue_notify(vrp->rvq);
  789. dev_info(&vdev->dev, "rpmsg host is online\n");
  790. return 0;
  791. free_ctrldev:
  792. rpmsg_virtio_del_ctrl_dev(rpdev_ctrl);
  793. free_coherent:
  794. dma_free_coherent(vdev->dev.parent, total_buf_space,
  795. bufs_va, vrp->bufs_dma);
  796. vqs_del:
  797. vdev->config->del_vqs(vrp->vdev);
  798. free_vrp:
  799. kfree(vrp);
  800. return err;
  801. }
  802. static int rpmsg_remove_device(struct device *dev, void *data)
  803. {
  804. device_unregister(dev);
  805. return 0;
  806. }
  807. static void rpmsg_remove(struct virtio_device *vdev)
  808. {
  809. struct virtproc_info *vrp = vdev->priv;
  810. size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
  811. int ret;
  812. virtio_reset_device(vdev);
  813. ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
  814. if (ret)
  815. dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
  816. idr_destroy(&vrp->endpoints);
  817. vdev->config->del_vqs(vrp->vdev);
  818. dma_free_coherent(vdev->dev.parent, total_buf_space,
  819. vrp->rbufs, vrp->bufs_dma);
  820. kfree(vrp);
  821. }
  822. static struct virtio_device_id id_table[] = {
  823. { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
  824. { 0 },
  825. };
  826. static unsigned int features[] = {
  827. VIRTIO_RPMSG_F_NS,
  828. };
  829. static struct virtio_driver virtio_ipc_driver = {
  830. .feature_table = features,
  831. .feature_table_size = ARRAY_SIZE(features),
  832. .driver.name = KBUILD_MODNAME,
  833. .id_table = id_table,
  834. .probe = rpmsg_probe,
  835. .remove = rpmsg_remove,
  836. };
  837. static int __init rpmsg_init(void)
  838. {
  839. int ret;
  840. ret = register_virtio_driver(&virtio_ipc_driver);
  841. if (ret)
  842. pr_err("failed to register virtio driver: %d\n", ret);
  843. return ret;
  844. }
  845. subsys_initcall(rpmsg_init);
  846. static void __exit rpmsg_fini(void)
  847. {
  848. unregister_virtio_driver(&virtio_ipc_driver);
  849. }
  850. module_exit(rpmsg_fini);
  851. MODULE_DEVICE_TABLE(virtio, id_table);
  852. MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
  853. MODULE_LICENSE("GPL v2");