virtio_rpmsg_bus.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. * @vrp: virtproc which owns this ept
  261. * @ept: endpoing to destroy
  262. *
  263. * An internal function which destroy an ept without assuming it is
  264. * bound to an rpmsg channel. This is needed for handling the internal
  265. * name service endpoint, which isn't bound to an rpmsg channel.
  266. * See also __rpmsg_create_ept().
  267. */
  268. static void
  269. __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
  270. {
  271. mutex_lock(&vrp->endpoints_lock);
  272. idr_remove(&vrp->endpoints, ept->addr);
  273. mutex_unlock(&vrp->endpoints_lock);
  274. kfree(ept);
  275. }
  276. /**
  277. * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
  278. * @ept: endpoing to destroy
  279. *
  280. * Should be used by drivers to destroy an rpmsg endpoint previously
  281. * created with rpmsg_create_ept().
  282. */
  283. void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
  284. {
  285. __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
  286. }
  287. EXPORT_SYMBOL(rpmsg_destroy_ept);
  288. /*
  289. * when an rpmsg driver is probed with a channel, we seamlessly create
  290. * it an endpoint, binding its rx callback to a unique local rpmsg
  291. * address.
  292. *
  293. * if we need to, we also announce about this channel to the remote
  294. * processor (needed in case the driver is exposing an rpmsg service).
  295. */
  296. static int rpmsg_dev_probe(struct device *dev)
  297. {
  298. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  299. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  300. struct virtproc_info *vrp = rpdev->vrp;
  301. struct rpmsg_endpoint *ept;
  302. int err;
  303. ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
  304. if (!ept) {
  305. dev_err(dev, "failed to create endpoint\n");
  306. err = -ENOMEM;
  307. goto out;
  308. }
  309. rpdev->ept = ept;
  310. rpdev->src = ept->addr;
  311. err = rpdrv->probe(rpdev);
  312. if (err) {
  313. dev_err(dev, "%s: failed: %d\n", __func__, err);
  314. rpmsg_destroy_ept(ept);
  315. goto out;
  316. }
  317. /* need to tell remote processor's name service about this channel ? */
  318. if (rpdev->announce &&
  319. virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
  320. struct rpmsg_ns_msg nsm;
  321. strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
  322. nsm.addr = rpdev->src;
  323. nsm.flags = RPMSG_NS_CREATE;
  324. err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
  325. if (err)
  326. dev_err(dev, "failed to announce service %d\n", err);
  327. }
  328. out:
  329. return err;
  330. }
  331. static int rpmsg_dev_remove(struct device *dev)
  332. {
  333. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  334. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
  335. struct virtproc_info *vrp = rpdev->vrp;
  336. int err = 0;
  337. /* tell remote processor's name service we're removing this channel */
  338. if (rpdev->announce &&
  339. virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
  340. struct rpmsg_ns_msg nsm;
  341. strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
  342. nsm.addr = rpdev->src;
  343. nsm.flags = RPMSG_NS_DESTROY;
  344. err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
  345. if (err)
  346. dev_err(dev, "failed to announce service %d\n", err);
  347. }
  348. rpdrv->remove(rpdev);
  349. rpmsg_destroy_ept(rpdev->ept);
  350. return err;
  351. }
  352. static struct bus_type rpmsg_bus = {
  353. .name = "rpmsg",
  354. .match = rpmsg_dev_match,
  355. .dev_attrs = rpmsg_dev_attrs,
  356. .uevent = rpmsg_uevent,
  357. .probe = rpmsg_dev_probe,
  358. .remove = rpmsg_dev_remove,
  359. };
  360. /**
  361. * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
  362. * @rpdrv: pointer to a struct rpmsg_driver
  363. *
  364. * Returns 0 on success, and an appropriate error value on failure.
  365. */
  366. int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
  367. {
  368. rpdrv->drv.bus = &rpmsg_bus;
  369. return driver_register(&rpdrv->drv);
  370. }
  371. EXPORT_SYMBOL(register_rpmsg_driver);
  372. /**
  373. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  374. * @rpdrv: pointer to a struct rpmsg_driver
  375. *
  376. * Returns 0 on success, and an appropriate error value on failure.
  377. */
  378. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  379. {
  380. driver_unregister(&rpdrv->drv);
  381. }
  382. EXPORT_SYMBOL(unregister_rpmsg_driver);
  383. static void rpmsg_release_device(struct device *dev)
  384. {
  385. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  386. kfree(rpdev);
  387. }
  388. /*
  389. * match an rpmsg channel with a channel info struct.
  390. * this is used to make sure we're not creating rpmsg devices for channels
  391. * that already exist.
  392. */
  393. static int rpmsg_channel_match(struct device *dev, void *data)
  394. {
  395. struct rpmsg_channel_info *chinfo = data;
  396. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  397. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  398. return 0;
  399. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  400. return 0;
  401. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  402. return 0;
  403. /* found a match ! */
  404. return 1;
  405. }
  406. /*
  407. * create an rpmsg channel using its name and address info.
  408. * this function will be used to create both static and dynamic
  409. * channels.
  410. */
  411. static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
  412. struct rpmsg_channel_info *chinfo)
  413. {
  414. struct rpmsg_channel *rpdev;
  415. struct device *tmp, *dev = &vrp->vdev->dev;
  416. int ret;
  417. /* make sure a similar channel doesn't already exist */
  418. tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
  419. if (tmp) {
  420. /* decrement the matched device's refcount back */
  421. put_device(tmp);
  422. dev_err(dev, "channel %s:%x:%x already exist\n",
  423. chinfo->name, chinfo->src, chinfo->dst);
  424. return NULL;
  425. }
  426. rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
  427. if (!rpdev) {
  428. pr_err("kzalloc failed\n");
  429. return NULL;
  430. }
  431. rpdev->vrp = vrp;
  432. rpdev->src = chinfo->src;
  433. rpdev->dst = chinfo->dst;
  434. /*
  435. * rpmsg server channels has predefined local address (for now),
  436. * and their existence needs to be announced remotely
  437. */
  438. rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
  439. strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
  440. /* very simple device indexing plumbing which is enough for now */
  441. dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
  442. rpdev->dev.parent = &vrp->vdev->dev;
  443. rpdev->dev.bus = &rpmsg_bus;
  444. rpdev->dev.release = rpmsg_release_device;
  445. ret = device_register(&rpdev->dev);
  446. if (ret) {
  447. dev_err(dev, "device_register failed: %d\n", ret);
  448. put_device(&rpdev->dev);
  449. return NULL;
  450. }
  451. return rpdev;
  452. }
  453. /*
  454. * find an existing channel using its name + address properties,
  455. * and destroy it
  456. */
  457. static int rpmsg_destroy_channel(struct virtproc_info *vrp,
  458. struct rpmsg_channel_info *chinfo)
  459. {
  460. struct virtio_device *vdev = vrp->vdev;
  461. struct device *dev;
  462. dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
  463. if (!dev)
  464. return -EINVAL;
  465. device_unregister(dev);
  466. put_device(dev);
  467. return 0;
  468. }
  469. /* super simple buffer "allocator" that is just enough for now */
  470. static void *get_a_tx_buf(struct virtproc_info *vrp)
  471. {
  472. unsigned int len;
  473. void *ret;
  474. /* support multiple concurrent senders */
  475. mutex_lock(&vrp->tx_lock);
  476. /*
  477. * either pick the next unused tx buffer
  478. * (half of our buffers are used for sending messages)
  479. */
  480. if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
  481. ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
  482. /* or recycle a used one */
  483. else
  484. ret = virtqueue_get_buf(vrp->svq, &len);
  485. mutex_unlock(&vrp->tx_lock);
  486. return ret;
  487. }
  488. /**
  489. * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
  490. * @vrp: virtual remote processor state
  491. *
  492. * This function is called before a sender is blocked, waiting for
  493. * a tx buffer to become available.
  494. *
  495. * If we already have blocking senders, this function merely increases
  496. * the "sleepers" reference count, and exits.
  497. *
  498. * Otherwise, if this is the first sender to block, we also enable
  499. * virtio's tx callbacks, so we'd be immediately notified when a tx
  500. * buffer is consumed (we rely on virtio's tx callback in order
  501. * to wake up sleeping senders as soon as a tx buffer is used by the
  502. * remote processor).
  503. */
  504. static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
  505. {
  506. /* support multiple concurrent senders */
  507. mutex_lock(&vrp->tx_lock);
  508. /* are we the first sleeping context waiting for tx buffers ? */
  509. if (atomic_inc_return(&vrp->sleepers) == 1)
  510. /* enable "tx-complete" interrupts before dozing off */
  511. virtqueue_enable_cb(vrp->svq);
  512. mutex_unlock(&vrp->tx_lock);
  513. }
  514. /**
  515. * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
  516. * @vrp: virtual remote processor state
  517. *
  518. * This function is called after a sender, that waited for a tx buffer
  519. * to become available, is unblocked.
  520. *
  521. * If we still have blocking senders, this function merely decreases
  522. * the "sleepers" reference count, and exits.
  523. *
  524. * Otherwise, if there are no more blocking senders, we also disable
  525. * virtio's tx callbacks, to avoid the overhead incurred with handling
  526. * those (now redundant) interrupts.
  527. */
  528. static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
  529. {
  530. /* support multiple concurrent senders */
  531. mutex_lock(&vrp->tx_lock);
  532. /* are we the last sleeping context waiting for tx buffers ? */
  533. if (atomic_dec_and_test(&vrp->sleepers))
  534. /* disable "tx-complete" interrupts */
  535. virtqueue_disable_cb(vrp->svq);
  536. mutex_unlock(&vrp->tx_lock);
  537. }
  538. /**
  539. * rpmsg_send_offchannel_raw() - send a message across to the remote processor
  540. * @rpdev: the rpmsg channel
  541. * @src: source address
  542. * @dst: destination address
  543. * @data: payload of message
  544. * @len: length of payload
  545. * @wait: indicates whether caller should block in case no TX buffers available
  546. *
  547. * This function is the base implementation for all of the rpmsg sending API.
  548. *
  549. * It will send @data of length @len to @dst, and say it's from @src. The
  550. * message will be sent to the remote processor which the @rpdev channel
  551. * belongs to.
  552. *
  553. * The message is sent using one of the TX buffers that are available for
  554. * communication with this remote processor.
  555. *
  556. * If @wait is true, the caller will be blocked until either a TX buffer is
  557. * available, or 15 seconds elapses (we don't want callers to
  558. * sleep indefinitely due to misbehaving remote processors), and in that
  559. * case -ERESTARTSYS is returned. The number '15' itself was picked
  560. * arbitrarily; there's little point in asking drivers to provide a timeout
  561. * value themselves.
  562. *
  563. * Otherwise, if @wait is false, and there are no TX buffers available,
  564. * the function will immediately fail, and -ENOMEM will be returned.
  565. *
  566. * Normally drivers shouldn't use this function directly; instead, drivers
  567. * should use the appropriate rpmsg_{try}send{to, _offchannel} API
  568. * (see include/linux/rpmsg.h).
  569. *
  570. * Returns 0 on success and an appropriate error value on failure.
  571. */
  572. int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  573. void *data, int len, bool wait)
  574. {
  575. struct virtproc_info *vrp = rpdev->vrp;
  576. struct device *dev = &rpdev->dev;
  577. struct scatterlist sg;
  578. struct rpmsg_hdr *msg;
  579. int err;
  580. /* bcasting isn't allowed */
  581. if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
  582. dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
  583. return -EINVAL;
  584. }
  585. /*
  586. * We currently use fixed-sized buffers, and therefore the payload
  587. * length is limited.
  588. *
  589. * One of the possible improvements here is either to support
  590. * user-provided buffers (and then we can also support zero-copy
  591. * messaging), or to improve the buffer allocator, to support
  592. * variable-length buffer sizes.
  593. */
  594. if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
  595. dev_err(dev, "message is too big (%d)\n", len);
  596. return -EMSGSIZE;
  597. }
  598. /* grab a buffer */
  599. msg = get_a_tx_buf(vrp);
  600. if (!msg && !wait)
  601. return -ENOMEM;
  602. /* no free buffer ? wait for one (but bail after 15 seconds) */
  603. while (!msg) {
  604. /* enable "tx-complete" interrupts, if not already enabled */
  605. rpmsg_upref_sleepers(vrp);
  606. /*
  607. * sleep until a free buffer is available or 15 secs elapse.
  608. * the timeout period is not configurable because there's
  609. * little point in asking drivers to specify that.
  610. * if later this happens to be required, it'd be easy to add.
  611. */
  612. err = wait_event_interruptible_timeout(vrp->sendq,
  613. (msg = get_a_tx_buf(vrp)),
  614. msecs_to_jiffies(15000));
  615. /* disable "tx-complete" interrupts if we're the last sleeper */
  616. rpmsg_downref_sleepers(vrp);
  617. /* timeout ? */
  618. if (!err) {
  619. dev_err(dev, "timeout waiting for a tx buffer\n");
  620. return -ERESTARTSYS;
  621. }
  622. }
  623. msg->len = len;
  624. msg->flags = 0;
  625. msg->src = src;
  626. msg->dst = dst;
  627. msg->reserved = 0;
  628. memcpy(msg->data, data, len);
  629. dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
  630. msg->src, msg->dst, msg->len,
  631. msg->flags, msg->reserved);
  632. print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
  633. msg, sizeof(*msg) + msg->len, true);
  634. sg_init_one(&sg, msg, sizeof(*msg) + len);
  635. mutex_lock(&vrp->tx_lock);
  636. /* add message to the remote processor's virtqueue */
  637. err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
  638. if (err < 0) {
  639. /*
  640. * need to reclaim the buffer here, otherwise it's lost
  641. * (memory won't leak, but rpmsg won't use it again for TX).
  642. * this will wait for a buffer management overhaul.
  643. */
  644. dev_err(dev, "virtqueue_add_buf failed: %d\n", err);
  645. goto out;
  646. }
  647. /* tell the remote processor it has a pending message to read */
  648. virtqueue_kick(vrp->svq);
  649. err = 0;
  650. out:
  651. mutex_unlock(&vrp->tx_lock);
  652. return err;
  653. }
  654. EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
  655. /* called when an rx buffer is used, and it's time to digest a message */
  656. static void rpmsg_recv_done(struct virtqueue *rvq)
  657. {
  658. struct rpmsg_hdr *msg;
  659. unsigned int len;
  660. struct rpmsg_endpoint *ept;
  661. struct scatterlist sg;
  662. struct virtproc_info *vrp = rvq->vdev->priv;
  663. struct device *dev = &rvq->vdev->dev;
  664. int err;
  665. msg = virtqueue_get_buf(rvq, &len);
  666. if (!msg) {
  667. dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
  668. return;
  669. }
  670. dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
  671. msg->src, msg->dst, msg->len,
  672. msg->flags, msg->reserved);
  673. print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
  674. msg, sizeof(*msg) + msg->len, true);
  675. /*
  676. * We currently use fixed-sized buffers, so trivially sanitize
  677. * the reported payload length.
  678. */
  679. if (len > RPMSG_BUF_SIZE ||
  680. msg->len > (len - sizeof(struct rpmsg_hdr))) {
  681. dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
  682. return;
  683. }
  684. /* use the dst addr to fetch the callback of the appropriate user */
  685. mutex_lock(&vrp->endpoints_lock);
  686. ept = idr_find(&vrp->endpoints, msg->dst);
  687. mutex_unlock(&vrp->endpoints_lock);
  688. if (ept && ept->cb)
  689. ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src);
  690. else
  691. dev_warn(dev, "msg received with no recepient\n");
  692. /* publish the real size of the buffer */
  693. sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
  694. /* add the buffer back to the remote processor's virtqueue */
  695. err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
  696. if (err < 0) {
  697. dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
  698. return;
  699. }
  700. /* tell the remote processor we added another available rx buffer */
  701. virtqueue_kick(vrp->rvq);
  702. }
  703. /*
  704. * This is invoked whenever the remote processor completed processing
  705. * a TX msg we just sent it, and the buffer is put back to the used ring.
  706. *
  707. * Normally, though, we suppress this "tx complete" interrupt in order to
  708. * avoid the incurred overhead.
  709. */
  710. static void rpmsg_xmit_done(struct virtqueue *svq)
  711. {
  712. struct virtproc_info *vrp = svq->vdev->priv;
  713. dev_dbg(&svq->vdev->dev, "%s\n", __func__);
  714. /* wake up potential senders that are waiting for a tx buffer */
  715. wake_up_interruptible(&vrp->sendq);
  716. }
  717. /* invoked when a name service announcement arrives */
  718. static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
  719. void *priv, u32 src)
  720. {
  721. struct rpmsg_ns_msg *msg = data;
  722. struct rpmsg_channel *newch;
  723. struct rpmsg_channel_info chinfo;
  724. struct virtproc_info *vrp = priv;
  725. struct device *dev = &vrp->vdev->dev;
  726. int ret;
  727. print_hex_dump(KERN_DEBUG, "NS announcement: ",
  728. DUMP_PREFIX_NONE, 16, 1,
  729. data, len, true);
  730. if (len != sizeof(*msg)) {
  731. dev_err(dev, "malformed ns msg (%d)\n", len);
  732. return;
  733. }
  734. /*
  735. * the name service ept does _not_ belong to a real rpmsg channel,
  736. * and is handled by the rpmsg bus itself.
  737. * for sanity reasons, make sure a valid rpdev has _not_ sneaked
  738. * in somehow.
  739. */
  740. if (rpdev) {
  741. dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
  742. return;
  743. }
  744. /* don't trust the remote processor for null terminating the name */
  745. msg->name[RPMSG_NAME_SIZE - 1] = '\0';
  746. dev_info(dev, "%sing channel %s addr 0x%x\n",
  747. msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
  748. msg->name, msg->addr);
  749. strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
  750. chinfo.src = RPMSG_ADDR_ANY;
  751. chinfo.dst = msg->addr;
  752. if (msg->flags & RPMSG_NS_DESTROY) {
  753. ret = rpmsg_destroy_channel(vrp, &chinfo);
  754. if (ret)
  755. dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
  756. } else {
  757. newch = rpmsg_create_channel(vrp, &chinfo);
  758. if (!newch)
  759. dev_err(dev, "rpmsg_create_channel failed\n");
  760. }
  761. }
  762. static int rpmsg_probe(struct virtio_device *vdev)
  763. {
  764. vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
  765. const char *names[] = { "input", "output" };
  766. struct virtqueue *vqs[2];
  767. struct virtproc_info *vrp;
  768. void *bufs_va;
  769. int err = 0, i;
  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. /* allocate coherent memory for the buffers */
  785. bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
  786. &vrp->bufs_dma, GFP_KERNEL);
  787. if (!bufs_va)
  788. goto vqs_del;
  789. dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
  790. (unsigned long long)vrp->bufs_dma);
  791. /* half of the buffers is dedicated for RX */
  792. vrp->rbufs = bufs_va;
  793. /* and half is dedicated for TX */
  794. vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
  795. /* set up the receive buffers */
  796. for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
  797. struct scatterlist sg;
  798. void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
  799. sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
  800. err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
  801. GFP_KERNEL);
  802. WARN_ON(err < 0); /* sanity check; this can't really happen */
  803. }
  804. /* suppress "tx-complete" interrupts */
  805. virtqueue_disable_cb(vrp->svq);
  806. vdev->priv = vrp;
  807. /* if supported by the remote processor, enable the name service */
  808. if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
  809. /* a dedicated endpoint handles the name service msgs */
  810. vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
  811. vrp, RPMSG_NS_ADDR);
  812. if (!vrp->ns_ept) {
  813. dev_err(&vdev->dev, "failed to create the ns ept\n");
  814. err = -ENOMEM;
  815. goto free_coherent;
  816. }
  817. }
  818. /* tell the remote processor it can start sending messages */
  819. virtqueue_kick(vrp->rvq);
  820. dev_info(&vdev->dev, "rpmsg host is online\n");
  821. return 0;
  822. free_coherent:
  823. dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, bufs_va,
  824. vrp->bufs_dma);
  825. vqs_del:
  826. vdev->config->del_vqs(vrp->vdev);
  827. free_vrp:
  828. kfree(vrp);
  829. return err;
  830. }
  831. static int rpmsg_remove_device(struct device *dev, void *data)
  832. {
  833. device_unregister(dev);
  834. return 0;
  835. }
  836. static void __devexit rpmsg_remove(struct virtio_device *vdev)
  837. {
  838. struct virtproc_info *vrp = vdev->priv;
  839. int ret;
  840. vdev->config->reset(vdev);
  841. ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
  842. if (ret)
  843. dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
  844. if (vrp->ns_ept)
  845. __rpmsg_destroy_ept(vrp, vrp->ns_ept);
  846. idr_remove_all(&vrp->endpoints);
  847. idr_destroy(&vrp->endpoints);
  848. vdev->config->del_vqs(vrp->vdev);
  849. dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
  850. vrp->rbufs, vrp->bufs_dma);
  851. kfree(vrp);
  852. }
  853. static struct virtio_device_id id_table[] = {
  854. { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
  855. { 0 },
  856. };
  857. static unsigned int features[] = {
  858. VIRTIO_RPMSG_F_NS,
  859. };
  860. static struct virtio_driver virtio_ipc_driver = {
  861. .feature_table = features,
  862. .feature_table_size = ARRAY_SIZE(features),
  863. .driver.name = KBUILD_MODNAME,
  864. .driver.owner = THIS_MODULE,
  865. .id_table = id_table,
  866. .probe = rpmsg_probe,
  867. .remove = __devexit_p(rpmsg_remove),
  868. };
  869. static int __init rpmsg_init(void)
  870. {
  871. int ret;
  872. ret = bus_register(&rpmsg_bus);
  873. if (ret) {
  874. pr_err("failed to register rpmsg bus: %d\n", ret);
  875. return ret;
  876. }
  877. ret = register_virtio_driver(&virtio_ipc_driver);
  878. if (ret) {
  879. pr_err("failed to register virtio driver: %d\n", ret);
  880. bus_unregister(&rpmsg_bus);
  881. }
  882. return ret;
  883. }
  884. module_init(rpmsg_init);
  885. static void __exit rpmsg_fini(void)
  886. {
  887. unregister_virtio_driver(&virtio_ipc_driver);
  888. bus_unregister(&rpmsg_bus);
  889. }
  890. module_exit(rpmsg_fini);
  891. MODULE_DEVICE_TABLE(virtio, id_table);
  892. MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
  893. MODULE_LICENSE("GPL v2");