virtio.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-9-16 GuEe-GUI the first version
  9. * 2021-11-11 GuEe-GUI modify to virtio common interface
  10. */
  11. #ifndef __VIRTIO_H__
  12. #define __VIRTIO_H__
  13. #include <rthw.h>
  14. #include <rtdef.h>
  15. #ifdef RT_USING_SMART
  16. #include <mmu.h>
  17. #include <ioremap.h>
  18. #endif
  19. #if RT_NAME_MAX < 16
  20. #error "Please set RT_NAME_MAX to at lest 16"
  21. #endif
  22. /* VirtIO version configuration */
  23. #if defined(RT_USING_VIRTIO_LEGACY) || defined(RT_USING_VIRTIO10)
  24. #define RT_USING_VIRTIO_VERSION 0x1 /* Legacy interface */
  25. #elif defined(RT_USING_VIRTIO_MODERN)
  26. #define RT_USING_VIRTIO_VERSION 0x2 /* Modern interface (1.0+) */
  27. #else
  28. #define RT_USING_VIRTIO_VERSION 0x1 /* Default to legacy */
  29. #endif
  30. #include <virtio_mmio.h>
  31. #include <virtio_queue.h>
  32. #define VIRTIO_MAGIC_VALUE 0x74726976 /* "virt" */
  33. #define VIRTIO_STATUS_ACKNOWLEDGE (1 << 0)
  34. #define VIRTIO_STATUS_DRIVER (1 << 1)
  35. #define VIRTIO_STATUS_DRIVER_OK (1 << 2)
  36. #define VIRTIO_STATUS_FEATURES_OK (1 << 3)
  37. #define VIRTIO_STATUS_NEEDS_RESET (1 << 6)
  38. #define VIRTIO_STATUS_FAILED (1 << 7)
  39. #define VIRTIO_F_NOTIFY_ON_EMPTY 24
  40. #define VIRTIO_F_ANY_LAYOUT 27
  41. #define VIRTIO_F_RING_INDIRECT_DESC 28
  42. #define VIRTIO_F_RING_EVENT_IDX 29
  43. #define VIRTIO_F_VERSION_1 32
  44. #define VIRTIO_F_RING_PACKED 34
  45. #ifdef RT_USING_SMART
  46. #define VIRTIO_VA2PA(vaddr) ((rt_ubase_t)rt_kmem_v2p(vaddr))
  47. #define VIRTIO_PA2VA(paddr) ((rt_ubase_t)rt_ioremap((void *)paddr, ARCH_PAGE_SIZE))
  48. #else
  49. #define VIRTIO_VA2PA(vaddr) ((rt_ubase_t)vaddr)
  50. #define VIRTIO_PA2VA(paddr) ((rt_ubase_t)paddr)
  51. #endif /* RT_USING_SMART */
  52. #define VIRTIO_PAGE_SHIFT 12
  53. #define VIRTIO_PAGE_SIZE (1 << VIRTIO_PAGE_SHIFT)
  54. #define VIRTIO_PAGE_ALIGN(addr) (RT_ALIGN(addr, VIRTIO_PAGE_SIZE))
  55. enum
  56. {
  57. /* virtio 1.0 */
  58. VIRTIO_DEVICE_ID_INVALID = 0, /* Invalid device */
  59. VIRTIO_DEVICE_ID_NET = 1, /* Net */
  60. VIRTIO_DEVICE_ID_BLOCK = 2, /* Block */
  61. VIRTIO_DEVICE_ID_CONSOLE = 3, /* Console */
  62. VIRTIO_DEVICE_ID_RNG = 4, /* Rng */
  63. VIRTIO_DEVICE_ID_BALLOON = 5, /* Balloon */
  64. VIRTIO_DEVICE_ID_IOMEM = 6, /* IO memory */
  65. VIRTIO_DEVICE_ID_RPMSG = 7, /* Remote processor messaging */
  66. VIRTIO_DEVICE_ID_SCSI = 8, /* SCSI */
  67. VIRTIO_DEVICE_ID_9P = 9, /* 9p console */
  68. VIRTIO_DEVICE_ID_MAC80211_WLAN = 10, /* Mac80211 wlan */
  69. VIRTIO_DEVICE_ID_RPROC_SERIAL = 11, /* Remoteproc serial link */
  70. VIRTIO_DEVICE_ID_CAIF = 12, /* CAIF */
  71. VIRTIO_DEVICE_ID_MEM_BALLOON = 13, /* Memory balloon */
  72. VIRTIO_DEVICE_ID_GPU = 16, /* GPU */
  73. VIRTIO_DEVICE_ID_TIME = 17, /* Timer/clock device */
  74. VIRTIO_DEVICE_ID_INPUT = 18, /* Input */
  75. /* virtio 1.1 */
  76. VIRTIO_DEVICE_ID_SOCKET = 19, /* Socket device */
  77. VIRTIO_DEVICE_ID_CRYPTO = 20, /* Crypto device */
  78. VIRTIO_DEVICE_ID_SIG_DIS_MOD = 21, /* Signal Distribution Module */
  79. VIRTIO_DEVICE_ID_PSTORE = 22, /* Pstore device */
  80. VIRTIO_DEVICE_ID_IOMMU = 23, /* IOMMU device */
  81. VIRTIO_DEVICE_ID_MEM = 24, /* Memory device */
  82. /* virtio 1.2 */
  83. VIRTIO_DEVICE_ID_AUDIO = 25, /* Audio device */
  84. VIRTIO_DEVICE_ID_FS = 26, /* File system device */
  85. VIRTIO_DEVICE_ID_PMEM = 27, /* PMEM device */
  86. VIRTIO_DEVICE_ID_RPMB = 28, /* RPMB device */
  87. VIRTIO_DEVICE_ID_MAC80211_HWSIM = 29, /* Mac80211 hwsim wireless simulation device */
  88. VIRTIO_DEVICE_ID_VIDEO_ENCODER = 30, /* Video encoder device */
  89. VIRTIO_DEVICE_ID_VIDEO_DECODER = 31, /* Video decoder device */
  90. VIRTIO_DEVICE_ID_SCMI = 32, /* SCMI device */
  91. VIRTIO_DEVICE_ID_NITRO_SEC_MOD = 33, /* NitroSecureModule */
  92. VIRTIO_DEVICE_ID_I2C_ADAPTER = 34, /* I2C adapter */
  93. VIRTIO_DEVICE_ID_WATCHDOG = 35, /* Watchdog */
  94. VIRTIO_DEVICE_ID_CAN = 36, /* CAN device */
  95. VIRTIO_DEVICE_ID_DMABUF = 37, /* Virtio dmabuf */
  96. VIRTIO_DEVICE_ID_PARAM_SERV = 38, /* Parameter Server */
  97. VIRTIO_DEVICE_ID_AUDIO_POLICY = 39, /* Audio policy device */
  98. VIRTIO_DEVICE_ID_BT = 40, /* Bluetooth device */
  99. VIRTIO_DEVICE_ID_GPIO = 41, /* GPIO device */
  100. VIRTIO_DEVICE_ID_RDMA = 42, /* RDMA device */
  101. VIRTIO_DEVICE_TYPE_SIZE
  102. };
  103. struct virtio_device
  104. {
  105. rt_uint32_t irq;
  106. rt_uint32_t version; /* VirtIO version from MMIO config */
  107. struct virtq *queues;
  108. rt_size_t queues_num;
  109. union
  110. {
  111. rt_ubase_t *mmio_base;
  112. struct virtio_mmio_config *mmio_config;
  113. };
  114. #ifdef RT_USING_SMP
  115. struct rt_spinlock spinlock;
  116. #endif
  117. void *priv;
  118. };
  119. typedef rt_err_t (*virtio_device_init_handler)(rt_ubase_t *mmio_base, rt_uint32_t irq);
  120. void virtio_reset_device(struct virtio_device *dev);
  121. void virtio_status_acknowledge_driver(struct virtio_device *dev);
  122. void virtio_status_driver_ok(struct virtio_device *dev);
  123. void virtio_interrupt_ack(struct virtio_device *dev);
  124. rt_bool_t virtio_has_feature(struct virtio_device *dev, rt_uint32_t feature_bit);
  125. /* Modern VirtIO feature negotiation (64-bit features) */
  126. rt_uint64_t virtio_get_features(struct virtio_device *dev);
  127. void virtio_set_features(struct virtio_device *dev, rt_uint64_t features);
  128. rt_bool_t virtio_has_feature_64(struct virtio_device *dev, rt_uint64_t features, rt_uint32_t feature_bit);
  129. rt_err_t virtio_queues_alloc(struct virtio_device *dev, rt_size_t queues_num);
  130. void virtio_queues_free(struct virtio_device *dev);
  131. rt_err_t virtio_queue_init(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t ring_size);
  132. void virtio_queue_destroy(struct virtio_device *dev, rt_uint32_t queue_index);
  133. void virtio_queue_notify(struct virtio_device *dev, rt_uint32_t queue_index);
  134. void virtio_submit_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index);
  135. rt_uint16_t virtio_alloc_desc(struct virtio_device *dev, rt_uint32_t queue_index);
  136. void virtio_free_desc(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index);
  137. rt_err_t virtio_alloc_desc_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_size_t count,
  138. rt_uint16_t *indexs);
  139. void virtio_free_desc_chain(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index);
  140. void virtio_fill_desc(struct virtio_device *dev, rt_uint32_t queue_index, rt_uint16_t desc_index,
  141. rt_uint64_t addr, rt_uint32_t len, rt_uint16_t flags, rt_uint16_t next);
  142. #endif /* __VIRTIO_H__ */