virtio_rpmsg_bus.c 29 KB

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