virtio_rpmsg_bus.c 29 KB

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