virtio_ring.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
  4. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * From Linux kernel include/uapi/linux/virtio_ring.h
  7. */
  8. #ifndef _LINUX_VIRTIO_RING_H
  9. #define _LINUX_VIRTIO_RING_H
  10. #include <virtio_types.h>
  11. /* This marks a buffer as continuing via the next field */
  12. #define VRING_DESC_F_NEXT 1
  13. /* This marks a buffer as write-only (otherwise read-only) */
  14. #define VRING_DESC_F_WRITE 2
  15. /* This means the buffer contains a list of buffer descriptors */
  16. #define VRING_DESC_F_INDIRECT 4
  17. /*
  18. * The Host uses this in used->flags to advise the Guest: don't kick me when
  19. * you add a buffer. It's unreliable, so it's simply an optimization. Guest
  20. * will still kick if it's out of buffers.
  21. */
  22. #define VRING_USED_F_NO_NOTIFY 1
  23. /*
  24. * The Guest uses this in avail->flags to advise the Host: don't interrupt me
  25. * when you consume a buffer. It's unreliable, so it's simply an optimization.
  26. */
  27. #define VRING_AVAIL_F_NO_INTERRUPT 1
  28. /* We support indirect buffer descriptors */
  29. #define VIRTIO_RING_F_INDIRECT_DESC 28
  30. /*
  31. * The Guest publishes the used index for which it expects an interrupt
  32. * at the end of the avail ring. Host should ignore the avail->flags field.
  33. *
  34. * The Host publishes the avail index for which it expects a kick
  35. * at the end of the used ring. Guest should ignore the used->flags field.
  36. */
  37. #define VIRTIO_RING_F_EVENT_IDX 29
  38. /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
  39. struct vring_desc {
  40. /* Address (guest-physical) */
  41. __virtio64 addr;
  42. /* Length */
  43. __virtio32 len;
  44. /* The flags as indicated above */
  45. __virtio16 flags;
  46. /* We chain unused descriptors via this, too */
  47. __virtio16 next;
  48. };
  49. /* Shadow of struct vring_desc in guest byte order. */
  50. struct vring_desc_shadow {
  51. u64 addr;
  52. u32 len;
  53. u16 flags;
  54. u16 next;
  55. /* Metadata about the descriptor. */
  56. bool chain_head;
  57. };
  58. struct vring_avail {
  59. __virtio16 flags;
  60. __virtio16 idx;
  61. __virtio16 ring[];
  62. };
  63. struct vring_used_elem {
  64. /* Index of start of used descriptor chain */
  65. __virtio32 id;
  66. /* Total length of the descriptor chain which was used (written to) */
  67. __virtio32 len;
  68. };
  69. struct vring_used {
  70. __virtio16 flags;
  71. __virtio16 idx;
  72. struct vring_used_elem ring[];
  73. };
  74. struct vring {
  75. unsigned int num;
  76. size_t size;
  77. struct bounce_buffer *bouncebufs;
  78. struct vring_desc *desc;
  79. struct vring_avail *avail;
  80. struct vring_used *used;
  81. };
  82. /**
  83. * virtqueue - a queue to register buffers for sending or receiving.
  84. *
  85. * @list: the chain of virtqueues for this device
  86. * @vdev: the virtio device this queue was created for
  87. * @index: the zero-based ordinal number for this queue
  88. * @num_free: number of elements we expect to be able to fit
  89. * @vring: actual memory layout for this queue
  90. * @vring_desc_shadow: guest-only copy of descriptors
  91. * @event: host publishes avail event idx
  92. * @free_head: head of free buffer list
  93. * @num_added: number we've added since last sync
  94. * @last_used_idx: last used index we've seen
  95. * @avail_flags_shadow: last written value to avail->flags
  96. * @avail_idx_shadow: last written value to avail->idx in guest byte order
  97. */
  98. struct virtqueue {
  99. struct list_head list;
  100. struct udevice *vdev;
  101. unsigned int index;
  102. unsigned int num_free;
  103. struct vring vring;
  104. struct vring_desc_shadow *vring_desc_shadow;
  105. bool event;
  106. unsigned int free_head;
  107. unsigned int num_added;
  108. u16 last_used_idx;
  109. u16 avail_flags_shadow;
  110. u16 avail_idx_shadow;
  111. };
  112. /*
  113. * Alignment requirements for vring elements.
  114. * When using pre-virtio 1.0 layout, these fall out naturally.
  115. */
  116. #define VRING_AVAIL_ALIGN_SIZE 2
  117. #define VRING_USED_ALIGN_SIZE 4
  118. #define VRING_DESC_ALIGN_SIZE 16
  119. /*
  120. * We publish the used event index at the end of the available ring,
  121. * and vice versa. They are at the end for backwards compatibility.
  122. */
  123. #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
  124. #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
  125. static inline unsigned int vring_size(unsigned int num, unsigned long align)
  126. {
  127. return ((sizeof(struct vring_desc) * num +
  128. sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) +
  129. sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
  130. }
  131. static inline void vring_init(struct vring *vr, unsigned int num, void *p,
  132. unsigned long align,
  133. struct bounce_buffer *bouncebufs)
  134. {
  135. vr->num = num;
  136. vr->size = vring_size(num, align);
  137. vr->bouncebufs = bouncebufs;
  138. vr->desc = p;
  139. vr->avail = p + num * sizeof(struct vring_desc);
  140. vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] +
  141. sizeof(__virtio16) + align - 1) & ~(align - 1));
  142. }
  143. /*
  144. * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX.
  145. * Assuming a given event_idx value from the other side, if we have just
  146. * incremented index from old to new_idx, should we trigger an event?
  147. */
  148. static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
  149. {
  150. /*
  151. * Note: Xen has similar logic for notification hold-off
  152. * in include/xen/interface/io/ring.h with req_event and req_prod
  153. * corresponding to event_idx + 1 and new_idx respectively.
  154. * Note also that req_event and req_prod in Xen start at 1,
  155. * event indexes in virtio start at 0.
  156. */
  157. return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old);
  158. }
  159. struct virtio_sg;
  160. /**
  161. * virtqueue_add - expose buffers to other end
  162. *
  163. * @vq: the struct virtqueue we're talking about
  164. * @sgs: array of terminated scatterlists
  165. * @out_sgs: the number of scatterlists readable by other side
  166. * @in_sgs: the number of scatterlists which are writable
  167. * (after readable ones)
  168. *
  169. * Caller must ensure we don't call this with other virtqueue operations
  170. * at the same time (except where noted).
  171. *
  172. * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
  173. */
  174. int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
  175. unsigned int out_sgs, unsigned int in_sgs);
  176. /**
  177. * virtqueue_kick - update after add_buf
  178. *
  179. * @vq: the struct virtqueue
  180. *
  181. * After one or more virtqueue_add() calls, invoke this to kick
  182. * the other side.
  183. *
  184. * Caller must ensure we don't call this with other virtqueue
  185. * operations at the same time (except where noted).
  186. */
  187. void virtqueue_kick(struct virtqueue *vq);
  188. /**
  189. * virtqueue_get_buf - get the next used buffer
  190. *
  191. * @vq: the struct virtqueue we're talking about
  192. * @len: the length written into the buffer
  193. *
  194. * If the device wrote data into the buffer, @len will be set to the
  195. * amount written. This means you don't need to clear the buffer
  196. * beforehand to ensure there's no data leakage in the case of short
  197. * writes.
  198. *
  199. * Caller must ensure we don't call this with other virtqueue
  200. * operations at the same time (except where noted).
  201. *
  202. * Returns NULL if there are no used buffers, or the memory buffer
  203. * handed to virtqueue_add_*().
  204. */
  205. void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
  206. /**
  207. * vring_create_virtqueue - create a virtqueue for a virtio device
  208. *
  209. * @index: the index of the queue
  210. * @num: number of elements of the queue
  211. * @vring_align:the alignment requirement of the descriptor ring
  212. * @udev: the virtio transport udevice
  213. * @return: the virtqueue pointer or NULL if failed
  214. *
  215. * This creates a virtqueue and allocates the descriptor ring for a virtio
  216. * device. The caller should query virtqueue_get_ring_size() to learn the
  217. * actual size of the ring.
  218. *
  219. * This API is supposed to be called by the virtio transport driver in the
  220. * virtio find_vqs() uclass method.
  221. */
  222. struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
  223. unsigned int vring_align,
  224. struct udevice *udev);
  225. /**
  226. * vring_del_virtqueue - destroy a virtqueue
  227. *
  228. * @vq: the struct virtqueue we're talking about
  229. *
  230. * This destroys a virtqueue. If created with vring_create_virtqueue(),
  231. * this also frees the descriptor ring.
  232. *
  233. * This API is supposed to be called by the virtio transport driver in the
  234. * virtio del_vqs() uclass method.
  235. */
  236. void vring_del_virtqueue(struct virtqueue *vq);
  237. /**
  238. * virtqueue_get_vring_size - get the size of the virtqueue's vring
  239. *
  240. * @vq: the struct virtqueue containing the vring of interest
  241. * @return: the size of the vring in a virtqueue.
  242. */
  243. unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
  244. /**
  245. * virtqueue_get_desc_addr - get the vring descriptor table address
  246. *
  247. * @vq: the struct virtqueue containing the vring of interest
  248. * @return: the descriptor table address of the vring in a virtqueue.
  249. */
  250. ulong virtqueue_get_desc_addr(struct virtqueue *vq);
  251. /**
  252. * virtqueue_get_avail_addr - get the vring available ring address
  253. *
  254. * @vq: the struct virtqueue containing the vring of interest
  255. * @return: the available ring address of the vring in a virtqueue.
  256. */
  257. ulong virtqueue_get_avail_addr(struct virtqueue *vq);
  258. /**
  259. * virtqueue_get_used_addr - get the vring used ring address
  260. *
  261. * @vq: the struct virtqueue containing the vring of interest
  262. * @return: the used ring address of the vring in a virtqueue.
  263. */
  264. ulong virtqueue_get_used_addr(struct virtqueue *vq);
  265. /**
  266. * virtqueue_poll - query pending used buffers
  267. *
  268. * @vq: the struct virtqueue we're talking about
  269. * @last_used_idx: virtqueue last used index
  270. *
  271. * Returns "true" if there are pending used buffers in the queue.
  272. */
  273. bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx);
  274. /**
  275. * virtqueue_dump - dump the virtqueue for debugging
  276. *
  277. * @vq: the struct virtqueue we're talking about
  278. *
  279. * Caller must ensure we don't call this with other virtqueue operations
  280. * at the same time (except where noted).
  281. */
  282. void virtqueue_dump(struct virtqueue *vq);
  283. /*
  284. * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
  285. * scenario, having these as nops is enough to work as expected.
  286. */
  287. static inline void virtio_mb(void)
  288. {
  289. }
  290. static inline void virtio_rmb(void)
  291. {
  292. }
  293. static inline void virtio_wmb(void)
  294. {
  295. }
  296. static inline void virtio_store_mb(__virtio16 *p, __virtio16 v)
  297. {
  298. WRITE_ONCE(*p, v);
  299. }
  300. #endif /* _LINUX_VIRTIO_RING_H */