virtio_rpmsg_bus.c 30 KB

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