virtio_rpmsg_bus.c 30 KB

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