virtio_rpmsg_bus.c 31 KB

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