virtio_queue.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-11 GuEe-GUI the first version
  9. */
  10. #ifndef __VIRTIO_QUEUE_H__
  11. #define __VIRTIO_QUEUE_H__
  12. #include <rtdef.h>
  13. #define VIRTQ_DESC_F_NEXT 1 /* This marks a buffer as continuing via the next field. */
  14. #define VIRTQ_DESC_F_WRITE 2 /* This marks a buffer as write-only (otherwise read-only). */
  15. #define VIRTQ_DESC_F_INDIRECT 4 /* This means the buffer contains a list of buffer descriptors. */
  16. /*
  17. * The device uses this in used->flags to advise the driver: don't kick me
  18. * when you add a buffer. It's unreliable, so it's simply an optimization.
  19. */
  20. #define VIRTQ_USED_F_NO_NOTIFY 1
  21. /*
  22. * The driver uses this in avail->flags to advise the device: don't
  23. * interrupt me when you consume a buffer. It's unreliable, so it's
  24. * simply an optimization.
  25. */
  26. #define VIRTQ_AVAIL_F_NO_INTERRUPT 1
  27. /* Virtqueue descriptors: 16 bytes. These can chain together via "next". */
  28. struct virtq_desc
  29. {
  30. rt_uint64_t addr; /* Address (guest-physical). */
  31. rt_uint32_t len; /* Length. */
  32. rt_uint16_t flags; /* The flags as indicated above. */
  33. rt_uint16_t next; /* We chain unused descriptors via this, too */
  34. };
  35. struct virtq_avail
  36. {
  37. rt_uint16_t flags; /* Notifications */
  38. rt_uint16_t idx; /* Where the driver would put the next descriptor entry in the ring (modulo the queue size) */
  39. rt_uint16_t ring[];
  40. /*
  41. * Only if VIRTIO_F_RING_EVENT_IDX
  42. * rt_uint16_t used_event;
  43. */
  44. };
  45. struct virtq_used_elem
  46. {
  47. rt_uint32_t id; /* Index of start of used descriptor chain. */
  48. rt_uint32_t len; /* Total length of the descriptor chain which was written to. */
  49. };
  50. struct virtq_used
  51. {
  52. rt_uint16_t flags;
  53. rt_uint16_t idx;
  54. struct virtq_used_elem ring[];
  55. /*
  56. * Only if VIRTIO_F_RING_EVENT_IDX
  57. * rt_uint16_t avail_event;
  58. */
  59. };
  60. struct virtq
  61. {
  62. rt_uint32_t num;
  63. struct virtq_desc *desc;
  64. struct virtq_avail *avail;
  65. struct virtq_used *used;
  66. /* Helper of driver */
  67. rt_uint16_t used_idx;
  68. rt_bool_t *free;
  69. rt_size_t free_count;
  70. };
  71. #define VIRTQ_DESC_TOTAL_SIZE(ring_size) (sizeof(struct virtq_desc) * (ring_size))
  72. /* flags, idx, used_event + ring * ring_size */
  73. #define VIRTQ_AVAIL_TOTAL_SIZE(ring_size) (sizeof(rt_uint16_t) * 3 + sizeof(rt_uint16_t) * (ring_size))
  74. /* flags, idx, avail_event + ring * ring_size */
  75. #define VIRTQ_USED_TOTAL_SIZE(ring_size) (sizeof(rt_uint16_t) * 3 + sizeof(struct virtq_used_elem) * (ring_size))
  76. #define VIRTQ_AVAIL_RES_SIZE (sizeof(rt_uint16_t)) /* used_event */
  77. #define VIRTQ_USED_RES_SIZE (sizeof(rt_uint16_t)) /* avail_event */
  78. #define VIRTQ_INVALID_DESC_ID RT_UINT16_MAX
  79. #endif /* __VIRTIO_QUEUE_H__ */