virtio_rpmsg_bus.c 29 KB

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