virtio_ring.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*-
  2. * Copyright Rusty Russell IBM Corporation 2007.
  3. * Copyright 2019,2022 NXP
  4. * This header is BSD licensed so anyone can use the definitions to implement
  5. * compatible drivers/servers.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of IBM nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * $FreeBSD$
  31. */
  32. #ifndef VIRTIO_RING_H
  33. #define VIRTIO_RING_H
  34. /* This marks a buffer as continuing via the next field. */
  35. #define VRING_DESC_F_NEXT 1U
  36. /* This marks a buffer as write-only (otherwise read-only). */
  37. #define VRING_DESC_F_WRITE 2U
  38. /* This means the buffer contains a list of buffer descriptors. */
  39. #define VRING_DESC_F_INDIRECT 4U
  40. /* The Host uses this in used->flags to advise the Guest: don't kick me
  41. * when you add a buffer. It's unreliable, so it's simply an
  42. * optimization. Guest will still kick if it's out of buffers. */
  43. #define VRING_USED_F_NO_NOTIFY 1U
  44. /* The Guest uses this in avail->flags to advise the Host: don't
  45. * interrupt me when you consume a buffer. It's unreliable, so it's
  46. * simply an optimization. */
  47. #define VRING_AVAIL_F_NO_INTERRUPT 1U
  48. /* VirtIO ring descriptors: 16 bytes.
  49. * These can chain together via "next". */
  50. struct vring_desc
  51. {
  52. /* Address (guest-physical). */
  53. uint64_t addr;
  54. /* Length. */
  55. uint32_t len;
  56. /* The flags as indicated above. */
  57. uint16_t flags;
  58. /* We chain unused descriptors via this, too. */
  59. uint16_t next;
  60. };
  61. struct vring_avail
  62. {
  63. uint16_t flags;
  64. uint16_t idx;
  65. uint16_t ring[1];
  66. };
  67. /* uint32_t is used here for ids for padding reasons. */
  68. struct vring_used_elem
  69. {
  70. /* Index of start of used descriptor chain. */
  71. uint32_t id;
  72. /* Total length of the descriptor chain which was written to. */
  73. uint32_t len;
  74. };
  75. struct vring_used
  76. {
  77. uint16_t flags;
  78. uint16_t idx;
  79. struct vring_used_elem ring[1];
  80. };
  81. struct vring
  82. {
  83. uint32_t num;
  84. struct vring_desc *desc;
  85. struct vring_avail *avail;
  86. struct vring_used *used;
  87. };
  88. /* The standard layout for the ring is a continuous chunk of memory which
  89. * looks like this. We assume num is a power of 2.
  90. *
  91. * struct vring {
  92. * # The actual descriptors (16 bytes each)
  93. * struct vring_desc desc[num];
  94. *
  95. * # A ring of available descriptor heads with free-running index.
  96. * __u16 avail_flags;
  97. * __u16 avail_idx;
  98. * __u16 available[num];
  99. * __u16 used_event_idx;
  100. *
  101. * # Padding to the next align boundary.
  102. * char pad[];
  103. *
  104. * # A ring of used descriptor heads with free-running index.
  105. * __u16 used_flags;
  106. * __u16 used_idx;
  107. * struct vring_used_elem used[num];
  108. * __u16 avail_event_idx;
  109. * };
  110. *
  111. * NOTE: for VirtIO PCI, align is 4096.
  112. */
  113. /*
  114. * We publish the used event index at the end of the available ring, and vice
  115. * versa. They are at the end for backwards compatibility.
  116. */
  117. #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
  118. #define vring_avail_event(vr) ((vr)->used->ring[(vr)->num].id)
  119. static inline int32_t vring_size(uint32_t num, uint32_t align)
  120. {
  121. uint32_t size;
  122. size = num * sizeof(struct vring_desc);
  123. size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) + sizeof(uint16_t);
  124. size = (size + align - 1UL) & ~(align - 1UL);
  125. size += sizeof(struct vring_used) + (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
  126. return ((int32_t)size);
  127. }
  128. static inline void vring_init(struct vring *vr, uint32_t num, uint8_t *p, uint32_t align)
  129. {
  130. vr->num = num;
  131. vr->desc = (struct vring_desc *)(void *)p;
  132. vr->avail = (struct vring_avail *)(void *)(p + num * sizeof(struct vring_desc));
  133. vr->used = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] + align - 1UL) & ~(align - 1UL));
  134. }
  135. /*
  136. * The following is used with VIRTIO_RING_F_EVENT_IDX.
  137. *
  138. * Assuming a given event_idx value from the other size, if we have
  139. * just incremented index from old to new_idx, should we trigger an
  140. * event?
  141. */
  142. static inline int32_t vring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
  143. {
  144. /* coco begin validated: This function does not need to be tested because it is not used in rpmsg_lite
  145. * implementation (only called from unused part of vq_ring_must_notify_host() ). */
  146. if ((uint16_t)(new_idx - event_idx - 1U) < (uint16_t)(new_idx - old))
  147. {
  148. return 1;
  149. }
  150. else
  151. {
  152. return 0;
  153. }
  154. }
  155. /* coco end */
  156. #endif /* VIRTIO_RING_H */