virtio_rpmsg_bus.c 30 KB

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