virtio_ring.h 5.8 KB

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