virtio_rpmsg_bus.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. *
  389. * Returns 0 on success, and an appropriate error value on failure.
  390. */
  391. int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
  392. {
  393. rpdrv->drv.bus = &rpmsg_bus;
  394. return driver_register(&rpdrv->drv);
  395. }
  396. EXPORT_SYMBOL(register_rpmsg_driver);
  397. /**
  398. * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
  399. * @rpdrv: pointer to a struct rpmsg_driver
  400. *
  401. * Returns 0 on success, and an appropriate error value on failure.
  402. */
  403. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
  404. {
  405. driver_unregister(&rpdrv->drv);
  406. }
  407. EXPORT_SYMBOL(unregister_rpmsg_driver);
  408. static void rpmsg_release_device(struct device *dev)
  409. {
  410. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  411. kfree(rpdev);
  412. }
  413. /*
  414. * match an rpmsg channel with a channel info struct.
  415. * this is used to make sure we're not creating rpmsg devices for channels
  416. * that already exist.
  417. */
  418. static int rpmsg_channel_match(struct device *dev, void *data)
  419. {
  420. struct rpmsg_channel_info *chinfo = data;
  421. struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
  422. if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
  423. return 0;
  424. if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
  425. return 0;
  426. if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
  427. return 0;
  428. /* found a match ! */
  429. return 1;
  430. }
  431. /*
  432. * create an rpmsg channel using its name and address info.
  433. * this function will be used to create both static and dynamic
  434. * channels.
  435. */
  436. static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
  437. struct rpmsg_channel_info *chinfo)
  438. {
  439. struct rpmsg_channel *rpdev;
  440. struct device *tmp, *dev = &vrp->vdev->dev;
  441. int ret;
  442. /* make sure a similar channel doesn't already exist */
  443. tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
  444. if (tmp) {
  445. /* decrement the matched device's refcount back */
  446. put_device(tmp);
  447. dev_err(dev, "channel %s:%x:%x already exist\n",
  448. chinfo->name, chinfo->src, chinfo->dst);
  449. return NULL;
  450. }
  451. rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
  452. if (!rpdev) {
  453. pr_err("kzalloc failed\n");
  454. return NULL;
  455. }
  456. rpdev->vrp = vrp;
  457. rpdev->src = chinfo->src;
  458. rpdev->dst = chinfo->dst;
  459. /*
  460. * rpmsg server channels has predefined local address (for now),
  461. * and their existence needs to be announced remotely
  462. */
  463. rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
  464. strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
  465. /* very simple device indexing plumbing which is enough for now */
  466. dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
  467. rpdev->dev.parent = &vrp->vdev->dev;
  468. rpdev->dev.bus = &rpmsg_bus;
  469. rpdev->dev.release = rpmsg_release_device;
  470. ret = device_register(&rpdev->dev);
  471. if (ret) {
  472. dev_err(dev, "device_register failed: %d\n", ret);
  473. put_device(&rpdev->dev);
  474. return NULL;
  475. }
  476. return rpdev;
  477. }
  478. /*
  479. * find an existing channel using its name + address properties,
  480. * and destroy it
  481. */
  482. static int rpmsg_destroy_channel(struct virtproc_info *vrp,
  483. struct rpmsg_channel_info *chinfo)
  484. {
  485. struct virtio_device *vdev = vrp->vdev;
  486. struct device *dev;
  487. dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
  488. if (!dev)
  489. return -EINVAL;
  490. device_unregister(dev);
  491. put_device(dev);
  492. return 0;
  493. }
  494. /* super simple buffer "allocator" that is just enough for now */
  495. static void *get_a_tx_buf(struct virtproc_info *vrp)
  496. {
  497. unsigned int len;
  498. void *ret;
  499. /* support multiple concurrent senders */
  500. mutex_lock(&vrp->tx_lock);
  501. /*
  502. * either pick the next unused tx buffer
  503. * (half of our buffers are used for sending messages)
  504. */
  505. if (vrp->last_sbuf < vrp->num_bufs / 2)
  506. ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
  507. /* or recycle a used one */
  508. else
  509. ret = virtqueue_get_buf(vrp->svq, &len);
  510. mutex_unlock(&vrp->tx_lock);
  511. return ret;
  512. }
  513. /**
  514. * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
  515. * @vrp: virtual remote processor state
  516. *
  517. * This function is called before a sender is blocked, waiting for
  518. * a tx buffer to become available.
  519. *
  520. * If we already have blocking senders, this function merely increases
  521. * the "sleepers" reference count, and exits.
  522. *
  523. * Otherwise, if this is the first sender to block, we also enable
  524. * virtio's tx callbacks, so we'd be immediately notified when a tx
  525. * buffer is consumed (we rely on virtio's tx callback in order
  526. * to wake up sleeping senders as soon as a tx buffer is used by the
  527. * remote processor).
  528. */
  529. static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
  530. {
  531. /* support multiple concurrent senders */
  532. mutex_lock(&vrp->tx_lock);
  533. /* are we the first sleeping context waiting for tx buffers ? */
  534. if (atomic_inc_return(&vrp->sleepers) == 1)
  535. /* enable "tx-complete" interrupts before dozing off */
  536. virtqueue_enable_cb(vrp->svq);
  537. mutex_unlock(&vrp->tx_lock);
  538. }
  539. /**
  540. * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
  541. * @vrp: virtual remote processor state
  542. *
  543. * This function is called after a sender, that waited for a tx buffer
  544. * to become available, is unblocked.
  545. *
  546. * If we still have blocking senders, this function merely decreases
  547. * the "sleepers" reference count, and exits.
  548. *
  549. * Otherwise, if there are no more blocking senders, we also disable
  550. * virtio's tx callbacks, to avoid the overhead incurred with handling
  551. * those (now redundant) interrupts.
  552. */
  553. static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
  554. {
  555. /* support multiple concurrent senders */
  556. mutex_lock(&vrp->tx_lock);
  557. /* are we the last sleeping context waiting for tx buffers ? */
  558. if (atomic_dec_and_test(&vrp->sleepers))
  559. /* disable "tx-complete" interrupts */
  560. virtqueue_disable_cb(vrp->svq);
  561. mutex_unlock(&vrp->tx_lock);
  562. }
  563. /**
  564. * rpmsg_send_offchannel_raw() - send a message across to the remote processor
  565. * @rpdev: the rpmsg channel
  566. * @src: source address
  567. * @dst: destination address
  568. * @data: payload of message
  569. * @len: length of payload
  570. * @wait: indicates whether caller should block in case no TX buffers available
  571. *
  572. * This function is the base implementation for all of the rpmsg sending API.
  573. *
  574. * It will send @data of length @len to @dst, and say it's from @src. The
  575. * message will be sent to the remote processor which the @rpdev channel
  576. * belongs to.
  577. *
  578. * The message is sent using one of the TX buffers that are available for
  579. * communication with this remote processor.
  580. *
  581. * If @wait is true, the caller will be blocked until either a TX buffer is
  582. * available, or 15 seconds elapses (we don't want callers to
  583. * sleep indefinitely due to misbehaving remote processors), and in that
  584. * case -ERESTARTSYS is returned. The number '15' itself was picked
  585. * arbitrarily; there's little point in asking drivers to provide a timeout
  586. * value themselves.
  587. *
  588. * Otherwise, if @wait is false, and there are no TX buffers available,
  589. * the function will immediately fail, and -ENOMEM will be returned.
  590. *
  591. * Normally drivers shouldn't use this function directly; instead, drivers
  592. * should use the appropriate rpmsg_{try}send{to, _offchannel} API
  593. * (see include/linux/rpmsg.h).
  594. *
  595. * Returns 0 on success and an appropriate error value on failure.
  596. */
  597. int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  598. void *data, int len, bool wait)
  599. {
  600. struct virtproc_info *vrp = rpdev->vrp;
  601. struct device *dev = &rpdev->dev;
  602. struct scatterlist sg;
  603. struct rpmsg_hdr *msg;
  604. int err;
  605. /* bcasting isn't allowed */
  606. if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
  607. dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
  608. return -EINVAL;
  609. }
  610. /*
  611. * We currently use fixed-sized buffers, and therefore the payload
  612. * length is limited.
  613. *
  614. * One of the possible improvements here is either to support
  615. * user-provided buffers (and then we can also support zero-copy
  616. * messaging), or to improve the buffer allocator, to support
  617. * variable-length buffer sizes.
  618. */
  619. if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
  620. dev_err(dev, "message is too big (%d)\n", len);
  621. return -EMSGSIZE;
  622. }
  623. /* grab a buffer */
  624. msg = get_a_tx_buf(vrp);
  625. if (!msg && !wait)
  626. return -ENOMEM;
  627. /* no free buffer ? wait for one (but bail after 15 seconds) */
  628. while (!msg) {
  629. /* enable "tx-complete" interrupts, if not already enabled */
  630. rpmsg_upref_sleepers(vrp);
  631. /*
  632. * sleep until a free buffer is available or 15 secs elapse.
  633. * the timeout period is not configurable because there's
  634. * little point in asking drivers to specify that.
  635. * if later this happens to be required, it'd be easy to add.
  636. */
  637. err = wait_event_interruptible_timeout(vrp->sendq,
  638. (msg = get_a_tx_buf(vrp)),
  639. msecs_to_jiffies(15000));
  640. /* disable "tx-complete" interrupts if we're the last sleeper */
  641. rpmsg_downref_sleepers(vrp);
  642. /* timeout ? */
  643. if (!err) {
  644. dev_err(dev, "timeout waiting for a tx buffer\n");
  645. return -ERESTARTSYS;
  646. }
  647. }
  648. msg->len = len;
  649. msg->flags = 0;
  650. msg->src = src;
  651. msg->dst = dst;
  652. msg->reserved = 0;
  653. memcpy(msg->data, data, len);
  654. dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
  655. msg->src, msg->dst, msg->len,
  656. msg->flags, msg->reserved);
  657. print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
  658. msg, sizeof(*msg) + msg->len, true);
  659. sg_init_one(&sg, msg, sizeof(*msg) + len);
  660. mutex_lock(&vrp->tx_lock);
  661. /* add message to the remote processor's virtqueue */
  662. err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
  663. if (err) {
  664. /*
  665. * need to reclaim the buffer here, otherwise it's lost
  666. * (memory won't leak, but rpmsg won't use it again for TX).
  667. * this will wait for a buffer management overhaul.
  668. */
  669. dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
  670. goto out;
  671. }
  672. /* tell the remote processor it has a pending message to read */
  673. virtqueue_kick(vrp->svq);
  674. out:
  675. mutex_unlock(&vrp->tx_lock);
  676. return err;
  677. }
  678. EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
  679. static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
  680. struct rpmsg_hdr *msg, unsigned int len)
  681. {
  682. struct rpmsg_endpoint *ept;
  683. struct scatterlist sg;
  684. int err;
  685. dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
  686. msg->src, msg->dst, msg->len,
  687. msg->flags, msg->reserved);
  688. print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
  689. msg, sizeof(*msg) + msg->len, true);
  690. /*
  691. * We currently use fixed-sized buffers, so trivially sanitize
  692. * the reported payload length.
  693. */
  694. if (len > RPMSG_BUF_SIZE ||
  695. msg->len > (len - sizeof(struct rpmsg_hdr))) {
  696. dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
  697. return -EINVAL;
  698. }
  699. /* use the dst addr to fetch the callback of the appropriate user */
  700. mutex_lock(&vrp->endpoints_lock);
  701. ept = idr_find(&vrp->endpoints, msg->dst);
  702. /* let's make sure no one deallocates ept while we use it */
  703. if (ept)
  704. kref_get(&ept->refcount);
  705. mutex_unlock(&vrp->endpoints_lock);
  706. if (ept) {
  707. /* make sure ept->cb doesn't go away while we use it */
  708. mutex_lock(&ept->cb_lock);
  709. if (ept->cb)
  710. ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
  711. msg->src);
  712. mutex_unlock(&ept->cb_lock);
  713. /* farewell, ept, we don't need you anymore */
  714. kref_put(&ept->refcount, __ept_release);
  715. } else
  716. dev_warn(dev, "msg received with no recipient\n");
  717. /* publish the real size of the buffer */
  718. sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
  719. /* add the buffer back to the remote processor's virtqueue */
  720. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
  721. if (err < 0) {
  722. dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
  723. return err;
  724. }
  725. return 0;
  726. }
  727. /* called when an rx buffer is used, and it's time to digest a message */
  728. static void rpmsg_recv_done(struct virtqueue *rvq)
  729. {
  730. struct virtproc_info *vrp = rvq->vdev->priv;
  731. struct device *dev = &rvq->vdev->dev;
  732. struct rpmsg_hdr *msg;
  733. unsigned int len, msgs_received = 0;
  734. int err;
  735. msg = virtqueue_get_buf(rvq, &len);
  736. if (!msg) {
  737. dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
  738. return;
  739. }
  740. while (msg) {
  741. err = rpmsg_recv_single(vrp, dev, msg, len);
  742. if (err)
  743. break;
  744. msgs_received++;
  745. msg = virtqueue_get_buf(rvq, &len);
  746. };
  747. dev_dbg(dev, "Received %u messages\n", msgs_received);
  748. /* tell the remote processor we added another available rx buffer */
  749. if (msgs_received)
  750. virtqueue_kick(vrp->rvq);
  751. }
  752. /*
  753. * This is invoked whenever the remote processor completed processing
  754. * a TX msg we just sent it, and the buffer is put back to the used ring.
  755. *
  756. * Normally, though, we suppress this "tx complete" interrupt in order to
  757. * avoid the incurred overhead.
  758. */
  759. static void rpmsg_xmit_done(struct virtqueue *svq)
  760. {
  761. struct virtproc_info *vrp = svq->vdev->priv;
  762. dev_dbg(&svq->vdev->dev, "%s\n", __func__);
  763. /* wake up potential senders that are waiting for a tx buffer */
  764. wake_up_interruptible(&vrp->sendq);
  765. }
  766. /* invoked when a name service announcement arrives */
  767. static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
  768. void *priv, u32 src)
  769. {
  770. struct rpmsg_ns_msg *msg = data;
  771. struct rpmsg_channel *newch;
  772. struct rpmsg_channel_info chinfo;
  773. struct virtproc_info *vrp = priv;
  774. struct device *dev = &vrp->vdev->dev;
  775. int ret;
  776. print_hex_dump(KERN_DEBUG, "NS announcement: ",
  777. DUMP_PREFIX_NONE, 16, 1,
  778. data, len, true);
  779. if (len != sizeof(*msg)) {
  780. dev_err(dev, "malformed ns msg (%d)\n", len);
  781. return;
  782. }
  783. /*
  784. * the name service ept does _not_ belong to a real rpmsg channel,
  785. * and is handled by the rpmsg bus itself.
  786. * for sanity reasons, make sure a valid rpdev has _not_ sneaked
  787. * in somehow.
  788. */
  789. if (rpdev) {
  790. dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
  791. return;
  792. }
  793. /* don't trust the remote processor for null terminating the name */
  794. msg->name[RPMSG_NAME_SIZE - 1] = '\0';
  795. dev_info(dev, "%sing channel %s addr 0x%x\n",
  796. msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
  797. msg->name, msg->addr);
  798. strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
  799. chinfo.src = RPMSG_ADDR_ANY;
  800. chinfo.dst = msg->addr;
  801. if (msg->flags & RPMSG_NS_DESTROY) {
  802. ret = rpmsg_destroy_channel(vrp, &chinfo);
  803. if (ret)
  804. dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
  805. } else {
  806. newch = rpmsg_create_channel(vrp, &chinfo);
  807. if (!newch)
  808. dev_err(dev, "rpmsg_create_channel failed\n");
  809. }
  810. }
  811. static int rpmsg_probe(struct virtio_device *vdev)
  812. {
  813. vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
  814. static const char * const names[] = { "input", "output" };
  815. struct virtqueue *vqs[2];
  816. struct virtproc_info *vrp;
  817. void *bufs_va;
  818. int err = 0, i;
  819. size_t total_buf_space;
  820. bool notify;
  821. vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
  822. if (!vrp)
  823. return -ENOMEM;
  824. vrp->vdev = vdev;
  825. idr_init(&vrp->endpoints);
  826. mutex_init(&vrp->endpoints_lock);
  827. mutex_init(&vrp->tx_lock);
  828. init_waitqueue_head(&vrp->sendq);
  829. /* We expect two virtqueues, rx and tx (and in this order) */
  830. err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
  831. if (err)
  832. goto free_vrp;
  833. vrp->rvq = vqs[0];
  834. vrp->svq = vqs[1];
  835. /* we expect symmetric tx/rx vrings */
  836. WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
  837. virtqueue_get_vring_size(vrp->svq));
  838. /* we need less buffers if vrings are small */
  839. if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
  840. vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
  841. else
  842. vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
  843. total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
  844. /* allocate coherent memory for the buffers */
  845. bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
  846. total_buf_space, &vrp->bufs_dma,
  847. GFP_KERNEL);
  848. if (!bufs_va) {
  849. err = -ENOMEM;
  850. goto vqs_del;
  851. }
  852. dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
  853. (unsigned long long)vrp->bufs_dma);
  854. /* half of the buffers is dedicated for RX */
  855. vrp->rbufs = bufs_va;
  856. /* and half is dedicated for TX */
  857. vrp->sbufs = bufs_va + total_buf_space / 2;
  858. /* set up the receive buffers */
  859. for (i = 0; i < vrp->num_bufs / 2; i++) {
  860. struct scatterlist sg;
  861. void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
  862. sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
  863. err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
  864. GFP_KERNEL);
  865. WARN_ON(err); /* sanity check; this can't really happen */
  866. }
  867. /* suppress "tx-complete" interrupts */
  868. virtqueue_disable_cb(vrp->svq);
  869. vdev->priv = vrp;
  870. /* if supported by the remote processor, enable the name service */
  871. if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
  872. /* a dedicated endpoint handles the name service msgs */
  873. vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
  874. vrp, RPMSG_NS_ADDR);
  875. if (!vrp->ns_ept) {
  876. dev_err(&vdev->dev, "failed to create the ns ept\n");
  877. err = -ENOMEM;
  878. goto free_coherent;
  879. }
  880. }
  881. /*
  882. * Prepare to kick but don't notify yet - we can't do this before
  883. * device is ready.
  884. */
  885. notify = virtqueue_kick_prepare(vrp->rvq);
  886. /* From this point on, we can notify and get callbacks. */
  887. virtio_device_ready(vdev);
  888. /* tell the remote processor it can start sending messages */
  889. /*
  890. * this might be concurrent with callbacks, but we are only
  891. * doing notify, not a full kick here, so that's ok.
  892. */
  893. if (notify)
  894. virtqueue_notify(vrp->rvq);
  895. dev_info(&vdev->dev, "rpmsg host is online\n");
  896. return 0;
  897. free_coherent:
  898. dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
  899. bufs_va, vrp->bufs_dma);
  900. vqs_del:
  901. vdev->config->del_vqs(vrp->vdev);
  902. free_vrp:
  903. kfree(vrp);
  904. return err;
  905. }
  906. static int rpmsg_remove_device(struct device *dev, void *data)
  907. {
  908. device_unregister(dev);
  909. return 0;
  910. }
  911. static void rpmsg_remove(struct virtio_device *vdev)
  912. {
  913. struct virtproc_info *vrp = vdev->priv;
  914. size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
  915. int ret;
  916. vdev->config->reset(vdev);
  917. ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
  918. if (ret)
  919. dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
  920. if (vrp->ns_ept)
  921. __rpmsg_destroy_ept(vrp, vrp->ns_ept);
  922. idr_destroy(&vrp->endpoints);
  923. vdev->config->del_vqs(vrp->vdev);
  924. dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
  925. vrp->rbufs, vrp->bufs_dma);
  926. kfree(vrp);
  927. }
  928. static struct virtio_device_id id_table[] = {
  929. { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
  930. { 0 },
  931. };
  932. static unsigned int features[] = {
  933. VIRTIO_RPMSG_F_NS,
  934. };
  935. static struct virtio_driver virtio_ipc_driver = {
  936. .feature_table = features,
  937. .feature_table_size = ARRAY_SIZE(features),
  938. .driver.name = KBUILD_MODNAME,
  939. .driver.owner = THIS_MODULE,
  940. .id_table = id_table,
  941. .probe = rpmsg_probe,
  942. .remove = rpmsg_remove,
  943. };
  944. static int __init rpmsg_init(void)
  945. {
  946. int ret;
  947. ret = bus_register(&rpmsg_bus);
  948. if (ret) {
  949. pr_err("failed to register rpmsg bus: %d\n", ret);
  950. return ret;
  951. }
  952. ret = register_virtio_driver(&virtio_ipc_driver);
  953. if (ret) {
  954. pr_err("failed to register virtio driver: %d\n", ret);
  955. bus_unregister(&rpmsg_bus);
  956. }
  957. return ret;
  958. }
  959. subsys_initcall(rpmsg_init);
  960. static void __exit rpmsg_fini(void)
  961. {
  962. unregister_virtio_driver(&virtio_ipc_driver);
  963. bus_unregister(&rpmsg_bus);
  964. }
  965. module_exit(rpmsg_fini);
  966. MODULE_DEVICE_TABLE(virtio, id_table);
  967. MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
  968. MODULE_LICENSE("GPL v2");