virtio_rpmsg_bus.c 30 KB

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