virtio_rpmsg_bus.c 32 KB

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