virtio_rpmsg_bus.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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, 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. strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
  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. strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
  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(rpdev->driver_override);
  332. kfree(vch);
  333. }
  334. /*
  335. * create an rpmsg channel using its name and address info.
  336. * this function will be used to create both static and dynamic
  337. * channels.
  338. */
  339. static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
  340. struct rpmsg_channel_info *chinfo)
  341. {
  342. struct virtio_rpmsg_channel *vch;
  343. struct rpmsg_device *rpdev;
  344. struct device *tmp, *dev = &vrp->vdev->dev;
  345. int ret;
  346. /* make sure a similar channel doesn't already exist */
  347. tmp = rpmsg_find_device(dev, chinfo);
  348. if (tmp) {
  349. /* decrement the matched device's refcount back */
  350. put_device(tmp);
  351. dev_err(dev, "channel %s:%x:%x already exist\n",
  352. chinfo->name, chinfo->src, chinfo->dst);
  353. return NULL;
  354. }
  355. vch = kzalloc(sizeof(*vch), GFP_KERNEL);
  356. if (!vch)
  357. return NULL;
  358. /* Link the channel to our vrp */
  359. vch->vrp = vrp;
  360. /* Assign public information to the rpmsg_device */
  361. rpdev = &vch->rpdev;
  362. rpdev->src = chinfo->src;
  363. rpdev->dst = chinfo->dst;
  364. rpdev->ops = &virtio_rpmsg_ops;
  365. rpdev->little_endian = virtio_is_little_endian(vrp->vdev);
  366. /*
  367. * rpmsg server channels has predefined local address (for now),
  368. * and their existence needs to be announced remotely
  369. */
  370. rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
  371. strscpy(rpdev->id.name, chinfo->name, sizeof(rpdev->id.name));
  372. rpdev->dev.parent = &vrp->vdev->dev;
  373. rpdev->dev.release = virtio_rpmsg_release_device;
  374. ret = rpmsg_register_device(rpdev);
  375. if (ret)
  376. return NULL;
  377. return rpdev;
  378. }
  379. /* super simple buffer "allocator" that is just enough for now */
  380. static void *get_a_tx_buf(struct virtproc_info *vrp)
  381. {
  382. unsigned int len;
  383. void *ret;
  384. /* support multiple concurrent senders */
  385. mutex_lock(&vrp->tx_lock);
  386. /*
  387. * either pick the next unused tx buffer
  388. * (half of our buffers are used for sending messages)
  389. */
  390. if (vrp->last_sbuf < vrp->num_bufs / 2)
  391. ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
  392. /* or recycle a used one */
  393. else
  394. ret = virtqueue_get_buf(vrp->svq, &len);
  395. mutex_unlock(&vrp->tx_lock);
  396. return ret;
  397. }
  398. /**
  399. * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
  400. * @vrp: virtual remote processor state
  401. *
  402. * This function is called before a sender is blocked, waiting for
  403. * a tx buffer to become available.
  404. *
  405. * If we already have blocking senders, this function merely increases
  406. * the "sleepers" reference count, and exits.
  407. *
  408. * Otherwise, if this is the first sender to block, we also enable
  409. * virtio's tx callbacks, so we'd be immediately notified when a tx
  410. * buffer is consumed (we rely on virtio's tx callback in order
  411. * to wake up sleeping senders as soon as a tx buffer is used by the
  412. * remote processor).
  413. */
  414. static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
  415. {
  416. /* support multiple concurrent senders */
  417. mutex_lock(&vrp->tx_lock);
  418. /* are we the first sleeping context waiting for tx buffers ? */
  419. if (atomic_inc_return(&vrp->sleepers) == 1)
  420. /* enable "tx-complete" interrupts before dozing off */
  421. virtqueue_enable_cb(vrp->svq);
  422. mutex_unlock(&vrp->tx_lock);
  423. }
  424. /**
  425. * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
  426. * @vrp: virtual remote processor state
  427. *
  428. * This function is called after a sender, that waited for a tx buffer
  429. * to become available, is unblocked.
  430. *
  431. * If we still have blocking senders, this function merely decreases
  432. * the "sleepers" reference count, and exits.
  433. *
  434. * Otherwise, if there are no more blocking senders, we also disable
  435. * virtio's tx callbacks, to avoid the overhead incurred with handling
  436. * those (now redundant) interrupts.
  437. */
  438. static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
  439. {
  440. /* support multiple concurrent senders */
  441. mutex_lock(&vrp->tx_lock);
  442. /* are we the last sleeping context waiting for tx buffers ? */
  443. if (atomic_dec_and_test(&vrp->sleepers))
  444. /* disable "tx-complete" interrupts */
  445. virtqueue_disable_cb(vrp->svq);
  446. mutex_unlock(&vrp->tx_lock);
  447. }
  448. /**
  449. * rpmsg_send_offchannel_raw() - send a message across to the remote processor
  450. * @rpdev: the rpmsg channel
  451. * @src: source address
  452. * @dst: destination address
  453. * @data: payload of message
  454. * @len: length of payload
  455. * @wait: indicates whether caller should block in case no TX buffers available
  456. *
  457. * This function is the base implementation for all of the rpmsg sending API.
  458. *
  459. * It will send @data of length @len to @dst, and say it's from @src. The
  460. * message will be sent to the remote processor which the @rpdev channel
  461. * belongs to.
  462. *
  463. * The message is sent using one of the TX buffers that are available for
  464. * communication with this remote processor.
  465. *
  466. * If @wait is true, the caller will be blocked until either a TX buffer is
  467. * available, or 15 seconds elapses (we don't want callers to
  468. * sleep indefinitely due to misbehaving remote processors), and in that
  469. * case -ERESTARTSYS is returned. The number '15' itself was picked
  470. * arbitrarily; there's little point in asking drivers to provide a timeout
  471. * value themselves.
  472. *
  473. * Otherwise, if @wait is false, and there are no TX buffers available,
  474. * the function will immediately fail, and -ENOMEM will be returned.
  475. *
  476. * Normally drivers shouldn't use this function directly; instead, drivers
  477. * should use the appropriate rpmsg_{try}send{to, _offchannel} API
  478. * (see include/linux/rpmsg.h).
  479. *
  480. * Return: 0 on success and an appropriate error value on failure.
  481. */
  482. static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
  483. u32 src, u32 dst,
  484. void *data, int len, bool wait)
  485. {
  486. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  487. struct virtproc_info *vrp = vch->vrp;
  488. struct device *dev = &rpdev->dev;
  489. struct scatterlist sg;
  490. struct rpmsg_hdr *msg;
  491. int err;
  492. /* bcasting isn't allowed */
  493. if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
  494. dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
  495. return -EINVAL;
  496. }
  497. /*
  498. * We currently use fixed-sized buffers, and therefore the payload
  499. * length is limited.
  500. *
  501. * One of the possible improvements here is either to support
  502. * user-provided buffers (and then we can also support zero-copy
  503. * messaging), or to improve the buffer allocator, to support
  504. * variable-length buffer sizes.
  505. */
  506. if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
  507. dev_err(dev, "message is too big (%d)\n", len);
  508. return -EMSGSIZE;
  509. }
  510. /* grab a buffer */
  511. msg = get_a_tx_buf(vrp);
  512. if (!msg && !wait)
  513. return -ENOMEM;
  514. /* no free buffer ? wait for one (but bail after 15 seconds) */
  515. while (!msg) {
  516. /* enable "tx-complete" interrupts, if not already enabled */
  517. rpmsg_upref_sleepers(vrp);
  518. /*
  519. * sleep until a free buffer is available or 15 secs elapse.
  520. * the timeout period is not configurable because there's
  521. * little point in asking drivers to specify that.
  522. * if later this happens to be required, it'd be easy to add.
  523. */
  524. err = wait_event_interruptible_timeout(vrp->sendq,
  525. (msg = get_a_tx_buf(vrp)),
  526. msecs_to_jiffies(15000));
  527. /* disable "tx-complete" interrupts if we're the last sleeper */
  528. rpmsg_downref_sleepers(vrp);
  529. /* timeout ? */
  530. if (!err) {
  531. dev_err(dev, "timeout waiting for a tx buffer\n");
  532. return -ERESTARTSYS;
  533. }
  534. }
  535. msg->len = cpu_to_rpmsg16(rpdev, len);
  536. msg->flags = 0;
  537. msg->src = cpu_to_rpmsg32(rpdev, src);
  538. msg->dst = cpu_to_rpmsg32(rpdev, dst);
  539. msg->reserved = 0;
  540. memcpy(msg->data, data, len);
  541. dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
  542. src, dst, len, msg->flags, msg->reserved);
  543. #if defined(CONFIG_DYNAMIC_DEBUG)
  544. dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
  545. msg, sizeof(*msg) + len, true);
  546. #endif
  547. rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
  548. mutex_lock(&vrp->tx_lock);
  549. /* add message to the remote processor's virtqueue */
  550. err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
  551. if (err) {
  552. /*
  553. * need to reclaim the buffer here, otherwise it's lost
  554. * (memory won't leak, but rpmsg won't use it again for TX).
  555. * this will wait for a buffer management overhaul.
  556. */
  557. dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
  558. goto out;
  559. }
  560. /* tell the remote processor it has a pending message to read */
  561. virtqueue_kick(vrp->svq);
  562. out:
  563. mutex_unlock(&vrp->tx_lock);
  564. return err;
  565. }
  566. static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
  567. {
  568. struct rpmsg_device *rpdev = ept->rpdev;
  569. u32 src = ept->addr, dst = rpdev->dst;
  570. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  571. }
  572. static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
  573. u32 dst)
  574. {
  575. struct rpmsg_device *rpdev = ept->rpdev;
  576. u32 src = ept->addr;
  577. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  578. }
  579. static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
  580. u32 dst, void *data, int len)
  581. {
  582. struct rpmsg_device *rpdev = ept->rpdev;
  583. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  584. }
  585. static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  586. {
  587. struct rpmsg_device *rpdev = ept->rpdev;
  588. u32 src = ept->addr, dst = rpdev->dst;
  589. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  590. }
  591. static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
  592. int len, u32 dst)
  593. {
  594. struct rpmsg_device *rpdev = ept->rpdev;
  595. u32 src = ept->addr;
  596. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  597. }
  598. static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
  599. u32 dst, void *data, int len)
  600. {
  601. struct rpmsg_device *rpdev = ept->rpdev;
  602. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  603. }
  604. static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept)
  605. {
  606. struct rpmsg_device *rpdev = ept->rpdev;
  607. struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
  608. return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
  609. }
  610. static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
  611. struct rpmsg_hdr *msg, unsigned int len)
  612. {
  613. struct rpmsg_endpoint *ept;
  614. struct scatterlist sg;
  615. bool little_endian = virtio_is_little_endian(vrp->vdev);
  616. unsigned int msg_len = __rpmsg16_to_cpu(little_endian, msg->len);
  617. int err;
  618. dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
  619. __rpmsg32_to_cpu(little_endian, msg->src),
  620. __rpmsg32_to_cpu(little_endian, msg->dst), msg_len,
  621. __rpmsg16_to_cpu(little_endian, msg->flags),
  622. __rpmsg32_to_cpu(little_endian, msg->reserved));
  623. #if defined(CONFIG_DYNAMIC_DEBUG)
  624. dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
  625. msg, sizeof(*msg) + msg_len, true);
  626. #endif
  627. /*
  628. * We currently use fixed-sized buffers, so trivially sanitize
  629. * the reported payload length.
  630. */
  631. if (len > vrp->buf_size ||
  632. msg_len > (len - sizeof(struct rpmsg_hdr))) {
  633. dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
  634. return -EINVAL;
  635. }
  636. /* use the dst addr to fetch the callback of the appropriate user */
  637. mutex_lock(&vrp->endpoints_lock);
  638. ept = idr_find(&vrp->endpoints, __rpmsg32_to_cpu(little_endian, msg->dst));
  639. /* let's make sure no one deallocates ept while we use it */
  640. if (ept)
  641. kref_get(&ept->refcount);
  642. mutex_unlock(&vrp->endpoints_lock);
  643. if (ept) {
  644. /* make sure ept->cb doesn't go away while we use it */
  645. mutex_lock(&ept->cb_lock);
  646. if (ept->cb)
  647. ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
  648. __rpmsg32_to_cpu(little_endian, msg->src));
  649. mutex_unlock(&ept->cb_lock);
  650. /* farewell, ept, we don't need you anymore */
  651. kref_put(&ept->refcount, __ept_release);
  652. } else
  653. dev_warn_ratelimited(dev, "msg received with no recipient\n");
  654. /* publish the real size of the buffer */
  655. rpmsg_sg_init(&sg, msg, vrp->buf_size);
  656. /* add the buffer back to the remote processor's virtqueue */
  657. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
  658. if (err < 0) {
  659. dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
  660. return err;
  661. }
  662. return 0;
  663. }
  664. /* called when an rx buffer is used, and it's time to digest a message */
  665. static void rpmsg_recv_done(struct virtqueue *rvq)
  666. {
  667. struct virtproc_info *vrp = rvq->vdev->priv;
  668. struct device *dev = &rvq->vdev->dev;
  669. struct rpmsg_hdr *msg;
  670. unsigned int len, msgs_received = 0;
  671. int err;
  672. msg = virtqueue_get_buf(rvq, &len);
  673. if (!msg) {
  674. dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
  675. return;
  676. }
  677. while (msg) {
  678. err = rpmsg_recv_single(vrp, dev, msg, len);
  679. if (err)
  680. break;
  681. msgs_received++;
  682. msg = virtqueue_get_buf(rvq, &len);
  683. }
  684. dev_dbg(dev, "Received %u messages\n", msgs_received);
  685. /* tell the remote processor we added another available rx buffer */
  686. if (msgs_received)
  687. virtqueue_kick(vrp->rvq);
  688. }
  689. /*
  690. * This is invoked whenever the remote processor completed processing
  691. * a TX msg we just sent it, and the buffer is put back to the used ring.
  692. *
  693. * Normally, though, we suppress this "tx complete" interrupt in order to
  694. * avoid the incurred overhead.
  695. */
  696. static void rpmsg_xmit_done(struct virtqueue *svq)
  697. {
  698. struct virtproc_info *vrp = svq->vdev->priv;
  699. dev_dbg(&svq->vdev->dev, "%s\n", __func__);
  700. /* wake up potential senders that are waiting for a tx buffer */
  701. wake_up_interruptible(&vrp->sendq);
  702. }
  703. /*
  704. * Called to expose to user a /dev/rpmsg_ctrlX interface allowing to
  705. * create endpoint-to-endpoint communication without associated RPMsg channel.
  706. * The endpoints are rattached to the ctrldev RPMsg device.
  707. */
  708. static struct rpmsg_device *rpmsg_virtio_add_ctrl_dev(struct virtio_device *vdev)
  709. {
  710. struct virtproc_info *vrp = vdev->priv;
  711. struct virtio_rpmsg_channel *vch;
  712. struct rpmsg_device *rpdev_ctrl;
  713. int err = 0;
  714. vch = kzalloc(sizeof(*vch), GFP_KERNEL);
  715. if (!vch)
  716. return ERR_PTR(-ENOMEM);
  717. /* Link the channel to the vrp */
  718. vch->vrp = vrp;
  719. /* Assign public information to the rpmsg_device */
  720. rpdev_ctrl = &vch->rpdev;
  721. rpdev_ctrl->ops = &virtio_rpmsg_ops;
  722. rpdev_ctrl->dev.parent = &vrp->vdev->dev;
  723. rpdev_ctrl->dev.release = virtio_rpmsg_release_device;
  724. rpdev_ctrl->little_endian = virtio_is_little_endian(vrp->vdev);
  725. err = rpmsg_ctrldev_register_device(rpdev_ctrl);
  726. if (err) {
  727. /* vch will be free in virtio_rpmsg_release_device() */
  728. return ERR_PTR(err);
  729. }
  730. return rpdev_ctrl;
  731. }
  732. static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
  733. {
  734. if (!rpdev_ctrl)
  735. return;
  736. device_unregister(&rpdev_ctrl->dev);
  737. }
  738. static int rpmsg_probe(struct virtio_device *vdev)
  739. {
  740. struct virtqueue_info vqs_info[] = {
  741. { "input", rpmsg_recv_done },
  742. { "output", rpmsg_xmit_done },
  743. };
  744. struct virtqueue *vqs[2];
  745. struct virtproc_info *vrp;
  746. struct virtio_rpmsg_channel *vch = NULL;
  747. struct rpmsg_device *rpdev_ns, *rpdev_ctrl;
  748. void *bufs_va;
  749. int err = 0, i;
  750. size_t total_buf_space;
  751. bool notify;
  752. vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
  753. if (!vrp)
  754. return -ENOMEM;
  755. vrp->vdev = vdev;
  756. idr_init(&vrp->endpoints);
  757. mutex_init(&vrp->endpoints_lock);
  758. mutex_init(&vrp->tx_lock);
  759. init_waitqueue_head(&vrp->sendq);
  760. /* We expect two virtqueues, rx and tx (and in this order) */
  761. err = virtio_find_vqs(vdev, 2, vqs, vqs_info, NULL);
  762. if (err)
  763. goto free_vrp;
  764. vrp->rvq = vqs[0];
  765. vrp->svq = vqs[1];
  766. /* we expect symmetric tx/rx vrings */
  767. WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
  768. virtqueue_get_vring_size(vrp->svq));
  769. /* we need less buffers if vrings are small */
  770. if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
  771. vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
  772. else
  773. vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
  774. vrp->buf_size = MAX_RPMSG_BUF_SIZE;
  775. total_buf_space = vrp->num_bufs * vrp->buf_size;
  776. /* allocate coherent memory for the buffers */
  777. bufs_va = dma_alloc_coherent(vdev->dev.parent,
  778. total_buf_space, &vrp->bufs_dma,
  779. GFP_KERNEL);
  780. if (!bufs_va) {
  781. err = -ENOMEM;
  782. goto vqs_del;
  783. }
  784. dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
  785. bufs_va, &vrp->bufs_dma);
  786. /* half of the buffers is dedicated for RX */
  787. vrp->rbufs = bufs_va;
  788. /* and half is dedicated for TX */
  789. vrp->sbufs = bufs_va + total_buf_space / 2;
  790. /* set up the receive buffers */
  791. for (i = 0; i < vrp->num_bufs / 2; i++) {
  792. struct scatterlist sg;
  793. void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
  794. rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
  795. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
  796. GFP_KERNEL);
  797. WARN_ON(err); /* sanity check; this can't really happen */
  798. }
  799. /* suppress "tx-complete" interrupts */
  800. virtqueue_disable_cb(vrp->svq);
  801. vdev->priv = vrp;
  802. rpdev_ctrl = rpmsg_virtio_add_ctrl_dev(vdev);
  803. if (IS_ERR(rpdev_ctrl)) {
  804. err = PTR_ERR(rpdev_ctrl);
  805. goto free_coherent;
  806. }
  807. /* if supported by the remote processor, enable the name service */
  808. if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
  809. vch = kzalloc(sizeof(*vch), GFP_KERNEL);
  810. if (!vch) {
  811. err = -ENOMEM;
  812. goto free_ctrldev;
  813. }
  814. /* Link the channel to our vrp */
  815. vch->vrp = vrp;
  816. /* Assign public information to the rpmsg_device */
  817. rpdev_ns = &vch->rpdev;
  818. rpdev_ns->ops = &virtio_rpmsg_ops;
  819. rpdev_ns->little_endian = virtio_is_little_endian(vrp->vdev);
  820. rpdev_ns->dev.parent = &vrp->vdev->dev;
  821. rpdev_ns->dev.release = virtio_rpmsg_release_device;
  822. err = rpmsg_ns_register_device(rpdev_ns);
  823. if (err)
  824. /* vch will be free in virtio_rpmsg_release_device() */
  825. goto free_ctrldev;
  826. }
  827. /*
  828. * Prepare to kick but don't notify yet - we can't do this before
  829. * device is ready.
  830. */
  831. notify = virtqueue_kick_prepare(vrp->rvq);
  832. /* From this point on, we can notify and get callbacks. */
  833. virtio_device_ready(vdev);
  834. /* tell the remote processor it can start sending messages */
  835. /*
  836. * this might be concurrent with callbacks, but we are only
  837. * doing notify, not a full kick here, so that's ok.
  838. */
  839. if (notify)
  840. virtqueue_notify(vrp->rvq);
  841. dev_info(&vdev->dev, "rpmsg host is online\n");
  842. return 0;
  843. free_ctrldev:
  844. rpmsg_virtio_del_ctrl_dev(rpdev_ctrl);
  845. free_coherent:
  846. dma_free_coherent(vdev->dev.parent, total_buf_space,
  847. bufs_va, vrp->bufs_dma);
  848. vqs_del:
  849. vdev->config->del_vqs(vrp->vdev);
  850. free_vrp:
  851. kfree(vrp);
  852. return err;
  853. }
  854. static int rpmsg_remove_device(struct device *dev, void *data)
  855. {
  856. device_unregister(dev);
  857. return 0;
  858. }
  859. static void rpmsg_remove(struct virtio_device *vdev)
  860. {
  861. struct virtproc_info *vrp = vdev->priv;
  862. size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
  863. int ret;
  864. virtio_reset_device(vdev);
  865. ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
  866. if (ret)
  867. dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
  868. idr_destroy(&vrp->endpoints);
  869. vdev->config->del_vqs(vrp->vdev);
  870. dma_free_coherent(vdev->dev.parent, total_buf_space,
  871. vrp->rbufs, vrp->bufs_dma);
  872. kfree(vrp);
  873. }
  874. static struct virtio_device_id id_table[] = {
  875. { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
  876. { 0 },
  877. };
  878. static unsigned int features[] = {
  879. VIRTIO_RPMSG_F_NS,
  880. };
  881. static struct virtio_driver virtio_ipc_driver = {
  882. .feature_table = features,
  883. .feature_table_size = ARRAY_SIZE(features),
  884. .driver.name = KBUILD_MODNAME,
  885. .id_table = id_table,
  886. .probe = rpmsg_probe,
  887. .remove = rpmsg_remove,
  888. };
  889. static int __init rpmsg_init(void)
  890. {
  891. int ret;
  892. ret = register_virtio_driver(&virtio_ipc_driver);
  893. if (ret)
  894. pr_err("failed to register virtio driver: %d\n", ret);
  895. return ret;
  896. }
  897. subsys_initcall(rpmsg_init);
  898. static void __exit rpmsg_fini(void)
  899. {
  900. unregister_virtio_driver(&virtio_ipc_driver);
  901. }
  902. module_exit(rpmsg_fini);
  903. MODULE_DEVICE_TABLE(virtio, id_table);
  904. MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
  905. MODULE_LICENSE("GPL v2");