virtio_ring.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef _LINUX_VIRTIO_RING_H
  2. #define _LINUX_VIRTIO_RING_H
  3. /* An interface for efficient virtio implementation, currently for use by KVM
  4. * and lguest, but hopefully others soon. Do NOT change this since it will
  5. * break existing servers and clients.
  6. *
  7. * This header is BSD licensed so anyone can use the definitions to implement
  8. * compatible drivers/servers.
  9. *
  10. * Copyright Rusty Russell IBM Corporation 2007. */
  11. #include <linux/types.h>
  12. /* This marks a buffer as continuing via the next field. */
  13. #define VRING_DESC_F_NEXT 1
  14. /* This marks a buffer as write-only (otherwise read-only). */
  15. #define VRING_DESC_F_WRITE 2
  16. /* This means the buffer contains a list of buffer descriptors. */
  17. #define VRING_DESC_F_INDIRECT 4
  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. #define VRING_USED_F_NO_NOTIFY 1
  22. /* The Guest uses this in avail->flags to advise the Host: don't interrupt me
  23. * when you consume a buffer. It's unreliable, so it's simply an
  24. * optimization. */
  25. #define VRING_AVAIL_F_NO_INTERRUPT 1
  26. /* We support indirect buffer descriptors */
  27. #define VIRTIO_RING_F_INDIRECT_DESC 28
  28. /* The Guest publishes the used index for which it expects an interrupt
  29. * at the end of the avail ring. Host should ignore the avail->flags field. */
  30. /* The Host publishes the avail index for which it expects a kick
  31. * at the end of the used ring. Guest should ignore the used->flags field. */
  32. #define VIRTIO_RING_F_EVENT_IDX 29
  33. /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
  34. struct vring_desc {
  35. /* Address (guest-physical). */
  36. __u64 addr;
  37. /* Length. */
  38. __u32 len;
  39. /* The flags as indicated above. */
  40. __u16 flags;
  41. /* We chain unused descriptors via this, too */
  42. __u16 next;
  43. };
  44. struct vring_avail {
  45. __u16 flags;
  46. __u16 idx;
  47. __u16 ring[];
  48. };
  49. /* u32 is used here for ids for padding reasons. */
  50. struct vring_used_elem {
  51. /* Index of start of used descriptor chain. */
  52. __u32 id;
  53. /* Total length of the descriptor chain which was used (written to) */
  54. __u32 len;
  55. };
  56. struct vring_used {
  57. __u16 flags;
  58. __u16 idx;
  59. struct vring_used_elem ring[];
  60. };
  61. struct vring {
  62. unsigned int num;
  63. struct vring_desc *desc;
  64. struct vring_avail *avail;
  65. struct vring_used *used;
  66. };
  67. /* The standard layout for the ring is a continuous chunk of memory which looks
  68. * like this. We assume num is a power of 2.
  69. *
  70. * struct vring
  71. * {
  72. * // The actual descriptors (16 bytes each)
  73. * struct vring_desc desc[num];
  74. *
  75. * // A ring of available descriptor heads with free-running index.
  76. * __u16 avail_flags;
  77. * __u16 avail_idx;
  78. * __u16 available[num];
  79. * __u16 used_event_idx;
  80. *
  81. * // Padding to the next align boundary.
  82. * char pad[];
  83. *
  84. * // A ring of used descriptor heads with free-running index.
  85. * __u16 used_flags;
  86. * __u16 used_idx;
  87. * struct vring_used_elem used[num];
  88. * __u16 avail_event_idx;
  89. * };
  90. */
  91. /* We publish the used event index at the end of the available ring, and vice
  92. * versa. They are at the end for backwards compatibility. */
  93. #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
  94. #define vring_avail_event(vr) (*(__u16 *)&(vr)->used->ring[(vr)->num])
  95. static __inline__ void vring_init(struct vring *vr, unsigned int num, void *p,
  96. unsigned long align)
  97. {
  98. vr->num = num;
  99. vr->desc = p;
  100. vr->avail = p + num*sizeof(struct vring_desc);
  101. vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
  102. & ~(align - 1));
  103. }
  104. static __inline__ unsigned vring_size(unsigned int num, unsigned long align)
  105. {
  106. return ((sizeof(struct vring_desc) * num + sizeof(__u16) * (2 + num)
  107. + align - 1) & ~(align - 1))
  108. + sizeof(__u16) * 3 + sizeof(struct vring_used_elem) * num;
  109. }
  110. /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
  111. /* Assuming a given event_idx value from the other size, if
  112. * we have just incremented index from old to new_idx,
  113. * should we trigger an event? */
  114. static __inline__ int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
  115. {
  116. /* Note: Xen has similar logic for notification hold-off
  117. * in include/xen/interface/io/ring.h with req_event and req_prod
  118. * corresponding to event_idx + 1 and new_idx respectively.
  119. * Note also that req_event and req_prod in Xen start at 1,
  120. * event indexes in virtio start at 0. */
  121. return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old);
  122. }
  123. #endif /* _LINUX_VIRTIO_RING_H */