virtio_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
  4. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * virtio ring implementation
  7. */
  8. #include <bouncebuf.h>
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <virtio_types.h>
  14. #include <virtio.h>
  15. #include <virtio_ring.h>
  16. #include <linux/bug.h>
  17. #include <linux/compat.h>
  18. #include <linux/kernel.h>
  19. static void *virtio_alloc_pages(struct udevice *vdev, u32 npages)
  20. {
  21. return memalign(PAGE_SIZE, npages * PAGE_SIZE);
  22. }
  23. static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages)
  24. {
  25. free(ptr);
  26. }
  27. static int __bb_force_page_align(struct bounce_buffer *state)
  28. {
  29. const ulong align_mask = PAGE_SIZE - 1;
  30. if ((ulong)state->user_buffer & align_mask)
  31. return 0;
  32. if (state->len != state->len_aligned)
  33. return 0;
  34. return 1;
  35. }
  36. static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
  37. struct virtio_sg *sg, u16 flags)
  38. {
  39. struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i];
  40. struct vring_desc *desc = &vq->vring.desc[i];
  41. void *addr;
  42. if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && vq->vring.bouncebufs) {
  43. struct bounce_buffer *bb = &vq->vring.bouncebufs[i];
  44. unsigned int bbflags;
  45. int ret;
  46. if (flags & VRING_DESC_F_WRITE)
  47. bbflags = GEN_BB_WRITE;
  48. else
  49. bbflags = GEN_BB_READ;
  50. ret = bounce_buffer_start_extalign(bb, sg->addr, sg->length,
  51. bbflags, PAGE_SIZE,
  52. __bb_force_page_align);
  53. if (ret) {
  54. debug("%s: failed to allocate bounce buffer (length 0x%zx)\n",
  55. vq->vdev->name, sg->length);
  56. }
  57. addr = bb->bounce_buffer;
  58. } else {
  59. addr = sg->addr;
  60. }
  61. /* Update the shadow descriptor. */
  62. desc_shadow->addr = (u64)(uintptr_t)addr;
  63. desc_shadow->len = sg->length;
  64. desc_shadow->flags = flags;
  65. /* Update the shared descriptor to match the shadow. */
  66. desc->addr = cpu_to_virtio64(vq->vdev, desc_shadow->addr);
  67. desc->len = cpu_to_virtio32(vq->vdev, desc_shadow->len);
  68. desc->flags = cpu_to_virtio16(vq->vdev, desc_shadow->flags);
  69. desc->next = cpu_to_virtio16(vq->vdev, desc_shadow->next);
  70. return desc_shadow->next;
  71. }
  72. static void virtqueue_detach_desc(struct virtqueue *vq, unsigned int idx)
  73. {
  74. struct vring_desc *desc = &vq->vring.desc[idx];
  75. struct bounce_buffer *bb;
  76. if (!IS_ENABLED(CONFIG_BOUNCE_BUFFER) || !vq->vring.bouncebufs)
  77. return;
  78. bb = &vq->vring.bouncebufs[idx];
  79. bounce_buffer_stop(bb);
  80. desc->addr = cpu_to_virtio64(vq->vdev, (u64)(uintptr_t)bb->user_buffer);
  81. }
  82. int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
  83. unsigned int out_sgs, unsigned int in_sgs)
  84. {
  85. struct vring_desc *desc;
  86. unsigned int descs_used = out_sgs + in_sgs;
  87. unsigned int i, n, avail, uninitialized_var(prev);
  88. int head;
  89. WARN_ON(descs_used == 0);
  90. head = vq->free_head;
  91. desc = vq->vring.desc;
  92. i = head;
  93. if (vq->num_free < descs_used) {
  94. debug("Can't add buf len %i - avail = %i\n",
  95. descs_used, vq->num_free);
  96. /*
  97. * FIXME: for historical reasons, we force a notify here if
  98. * there are outgoing parts to the buffer. Presumably the
  99. * host should service the ring ASAP.
  100. */
  101. if (out_sgs)
  102. virtio_notify(vq->vdev, vq);
  103. return -ENOSPC;
  104. }
  105. for (n = 0; n < descs_used; n++) {
  106. u16 flags = VRING_DESC_F_NEXT;
  107. if (n >= out_sgs)
  108. flags |= VRING_DESC_F_WRITE;
  109. prev = i;
  110. i = virtqueue_attach_desc(vq, i, sgs[n], flags);
  111. }
  112. /* Last one doesn't continue */
  113. vq->vring_desc_shadow[prev].flags &= ~VRING_DESC_F_NEXT;
  114. desc[prev].flags = cpu_to_virtio16(vq->vdev, vq->vring_desc_shadow[prev].flags);
  115. /* We're using some buffers from the free list. */
  116. vq->num_free -= descs_used;
  117. /* Update free pointer */
  118. vq->free_head = i;
  119. /* Mark the descriptor as the head of a chain. */
  120. vq->vring_desc_shadow[head].chain_head = true;
  121. /*
  122. * Put entry in available array (but don't update avail->idx
  123. * until they do sync).
  124. */
  125. avail = vq->avail_idx_shadow & (vq->vring.num - 1);
  126. vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head);
  127. /*
  128. * Descriptors and available array need to be set before we expose the
  129. * new available array entries.
  130. */
  131. virtio_wmb();
  132. vq->avail_idx_shadow++;
  133. vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow);
  134. vq->num_added++;
  135. /*
  136. * This is very unlikely, but theoretically possible.
  137. * Kick just in case.
  138. */
  139. if (unlikely(vq->num_added == (1 << 16) - 1))
  140. virtqueue_kick(vq);
  141. return 0;
  142. }
  143. static bool virtqueue_kick_prepare(struct virtqueue *vq)
  144. {
  145. u16 new, old;
  146. bool needs_kick;
  147. /*
  148. * We need to expose available array entries before checking
  149. * avail event.
  150. */
  151. virtio_mb();
  152. old = vq->avail_idx_shadow - vq->num_added;
  153. new = vq->avail_idx_shadow;
  154. vq->num_added = 0;
  155. if (vq->event) {
  156. needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev,
  157. vring_avail_event(&vq->vring)), new, old);
  158. } else {
  159. needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev,
  160. VRING_USED_F_NO_NOTIFY));
  161. }
  162. return needs_kick;
  163. }
  164. void virtqueue_kick(struct virtqueue *vq)
  165. {
  166. if (virtqueue_kick_prepare(vq))
  167. virtio_notify(vq->vdev, vq);
  168. }
  169. static void detach_buf(struct virtqueue *vq, unsigned int head)
  170. {
  171. unsigned int i;
  172. /* Unmark the descriptor as the head of a chain. */
  173. vq->vring_desc_shadow[head].chain_head = false;
  174. /* Put back on free list: unmap first-level descriptors and find end */
  175. i = head;
  176. while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) {
  177. virtqueue_detach_desc(vq, i);
  178. i = vq->vring_desc_shadow[i].next;
  179. vq->num_free++;
  180. }
  181. virtqueue_detach_desc(vq, i);
  182. vq->vring_desc_shadow[i].next = vq->free_head;
  183. vq->free_head = head;
  184. /* Plus final descriptor */
  185. vq->num_free++;
  186. }
  187. static inline bool more_used(const struct virtqueue *vq)
  188. {
  189. return vq->last_used_idx != virtio16_to_cpu(vq->vdev,
  190. vq->vring.used->idx);
  191. }
  192. void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len)
  193. {
  194. unsigned int i;
  195. u16 last_used;
  196. if (!more_used(vq)) {
  197. debug("(%s.%d): No more buffers in queue\n",
  198. vq->vdev->name, vq->index);
  199. return NULL;
  200. }
  201. /* Only get used array entries after they have been exposed by host */
  202. virtio_rmb();
  203. last_used = (vq->last_used_idx & (vq->vring.num - 1));
  204. i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id);
  205. if (len) {
  206. *len = virtio32_to_cpu(vq->vdev,
  207. vq->vring.used->ring[last_used].len);
  208. debug("(%s.%d): last used idx %u with len %u\n",
  209. vq->vdev->name, vq->index, i, *len);
  210. }
  211. if (unlikely(i >= vq->vring.num)) {
  212. printf("(%s.%d): id %u out of range\n",
  213. vq->vdev->name, vq->index, i);
  214. return NULL;
  215. }
  216. if (unlikely(!vq->vring_desc_shadow[i].chain_head)) {
  217. printf("(%s.%d): id %u is not a head\n",
  218. vq->vdev->name, vq->index, i);
  219. return NULL;
  220. }
  221. detach_buf(vq, i);
  222. vq->last_used_idx++;
  223. /*
  224. * If we expect an interrupt for the next entry, tell host
  225. * by writing event index and flush out the write before
  226. * the read in the next get_buf call.
  227. */
  228. if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
  229. virtio_store_mb(&vring_used_event(&vq->vring),
  230. cpu_to_virtio16(vq->vdev, vq->last_used_idx));
  231. return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr;
  232. }
  233. static struct virtqueue *__vring_new_virtqueue(unsigned int index,
  234. struct vring vring,
  235. struct udevice *udev)
  236. {
  237. unsigned int i;
  238. struct virtqueue *vq;
  239. struct vring_desc_shadow *vring_desc_shadow;
  240. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
  241. struct udevice *vdev = uc_priv->vdev;
  242. vq = malloc(sizeof(*vq));
  243. if (!vq)
  244. return NULL;
  245. vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow));
  246. if (!vring_desc_shadow) {
  247. free(vq);
  248. return NULL;
  249. }
  250. vq->vdev = vdev;
  251. vq->index = index;
  252. vq->num_free = vring.num;
  253. vq->vring = vring;
  254. vq->vring_desc_shadow = vring_desc_shadow;
  255. vq->last_used_idx = 0;
  256. vq->avail_flags_shadow = 0;
  257. vq->avail_idx_shadow = 0;
  258. vq->num_added = 0;
  259. list_add_tail(&vq->list, &uc_priv->vqs);
  260. vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
  261. /* Tell other side not to bother us */
  262. vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
  263. if (!vq->event)
  264. vq->vring.avail->flags = cpu_to_virtio16(vdev,
  265. vq->avail_flags_shadow);
  266. /* Put everything in free lists */
  267. vq->free_head = 0;
  268. for (i = 0; i < vring.num - 1; i++)
  269. vq->vring_desc_shadow[i].next = i + 1;
  270. return vq;
  271. }
  272. struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
  273. unsigned int vring_align,
  274. struct udevice *udev)
  275. {
  276. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
  277. struct udevice *vdev = uc_priv->vdev;
  278. struct virtqueue *vq;
  279. void *queue = NULL;
  280. struct bounce_buffer *bbs = NULL;
  281. struct vring vring;
  282. /* We assume num is a power of 2 */
  283. if (num & (num - 1)) {
  284. printf("Bad virtqueue length %u\n", num);
  285. return NULL;
  286. }
  287. /* TODO: allocate each queue chunk individually */
  288. for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
  289. size_t sz = vring_size(num, vring_align);
  290. queue = virtio_alloc_pages(vdev, DIV_ROUND_UP(sz, PAGE_SIZE));
  291. if (queue)
  292. break;
  293. }
  294. if (!num)
  295. return NULL;
  296. if (!queue) {
  297. /* Try to get a single page. You are my only hope! */
  298. queue = virtio_alloc_pages(vdev, 1);
  299. }
  300. if (!queue)
  301. return NULL;
  302. memset(queue, 0, vring_size(num, vring_align));
  303. if (virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
  304. bbs = calloc(num, sizeof(*bbs));
  305. if (!bbs)
  306. goto err_free_queue;
  307. }
  308. vring_init(&vring, num, queue, vring_align, bbs);
  309. vq = __vring_new_virtqueue(index, vring, udev);
  310. if (!vq)
  311. goto err_free_bbs;
  312. debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name,
  313. queue, vq, num);
  314. return vq;
  315. err_free_bbs:
  316. free(bbs);
  317. err_free_queue:
  318. virtio_free_pages(vdev, queue, DIV_ROUND_UP(vring.size, PAGE_SIZE));
  319. return NULL;
  320. }
  321. void vring_del_virtqueue(struct virtqueue *vq)
  322. {
  323. virtio_free_pages(vq->vdev, vq->vring.desc,
  324. DIV_ROUND_UP(vq->vring.size, PAGE_SIZE));
  325. free(vq->vring_desc_shadow);
  326. list_del(&vq->list);
  327. free(vq->vring.bouncebufs);
  328. free(vq);
  329. }
  330. unsigned int virtqueue_get_vring_size(struct virtqueue *vq)
  331. {
  332. return vq->vring.num;
  333. }
  334. ulong virtqueue_get_desc_addr(struct virtqueue *vq)
  335. {
  336. return (ulong)vq->vring.desc;
  337. }
  338. ulong virtqueue_get_avail_addr(struct virtqueue *vq)
  339. {
  340. return (ulong)vq->vring.desc +
  341. ((char *)vq->vring.avail - (char *)vq->vring.desc);
  342. }
  343. ulong virtqueue_get_used_addr(struct virtqueue *vq)
  344. {
  345. return (ulong)vq->vring.desc +
  346. ((char *)vq->vring.used - (char *)vq->vring.desc);
  347. }
  348. bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx)
  349. {
  350. virtio_mb();
  351. return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx);
  352. }
  353. void virtqueue_dump(struct virtqueue *vq)
  354. {
  355. unsigned int i;
  356. printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name);
  357. printf("\tindex %u, phys addr %p num %u\n",
  358. vq->index, vq->vring.desc, vq->vring.num);
  359. printf("\tfree_head %u, num_added %u, num_free %u\n",
  360. vq->free_head, vq->num_added, vq->num_free);
  361. printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n",
  362. vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow);
  363. printf("Shadow descriptor dump:\n");
  364. for (i = 0; i < vq->vring.num; i++) {
  365. struct vring_desc_shadow *desc = &vq->vring_desc_shadow[i];
  366. printf("\tdesc_shadow[%u] = { 0x%llx, len %u, flags %u, next %u }\n",
  367. i, desc->addr, desc->len, desc->flags, desc->next);
  368. }
  369. printf("Avail ring dump:\n");
  370. printf("\tflags %u, idx %u\n",
  371. vq->vring.avail->flags, vq->vring.avail->idx);
  372. for (i = 0; i < vq->vring.num; i++) {
  373. printf("\tavail[%u] = %u\n",
  374. i, vq->vring.avail->ring[i]);
  375. }
  376. printf("Used ring dump:\n");
  377. printf("\tflags %u, idx %u\n",
  378. vq->vring.used->flags, vq->vring.used->idx);
  379. for (i = 0; i < vq->vring.num; i++) {
  380. printf("\tused[%u] = { %u, %u }\n", i,
  381. vq->vring.used->ring[i].id, vq->vring.used->ring[i].len);
  382. }
  383. }