virtio_mmio.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  10. #ifndef VIRTIO_MMIO_H
  11. #define VIRTIO_MMIO_H
  12. #include <stdint.h>
  13. #include <stddef.h>
  14. #define VIRTIO_MMIO_MAGIC 0x74726976
  15. #define VIRTIO_MMIO_VENDOR 0x554d4551
  16. #define VIRTIO_MMIO_MAGIC_VALUE 0x000 /* VIRTIO_MMIO_MAGIC */
  17. #define VIRTIO_MMIO_VERSION 0x004 /* version: 1 is legacy */
  18. #define VIRTIO_MMIO_DEVICE_ID 0x008 /* device type: 1 is net, 2 is disk */
  19. #define VIRTIO_MMIO_VENDOR_ID 0x00c /* VIRTIO_MMIO_VENDOR */
  20. #define VIRTIO_MMIO_DEVICE_FEATURES 0x010
  21. #define VIRTIO_MMIO_DRIVER_FEATURES 0x020
  22. #define VIRTIO_MMIO_HOST_FEATURES 0x010
  23. #define VIRTIO_MMIO_HOST_FEATURES_SEL 0x014
  24. #define VIRTIO_MMIO_GUEST_FEATURES 0x020
  25. #define VIRTIO_MMIO_GUEST_FEATURES_SEL 0x024
  26. #define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 /* version 1 only */
  27. #define VIRTIO_MMIO_QUEUE_SEL 0x030
  28. #define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034
  29. #define VIRTIO_MMIO_QUEUE_NUM 0x038
  30. #define VIRTIO_MMIO_QUEUE_ALIGN 0x03c /* version 1 only */
  31. #define VIRTIO_MMIO_QUEUE_PFN 0x040 /* version 1 only */
  32. #define VIRTIO_MMIO_QUEUE_READY 0x044 /* requires version 2 */
  33. #define VIRTIO_MMIO_QUEUE_NOTIFY 0x050
  34. #define VIRTIO_MMIO_INTERRUPT_STATUS 0x060
  35. #define VIRTIO_MMIO_INTERRUPT_ACK 0x064
  36. #define VIRTIO_MMIO_STATUS 0x070
  37. #define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 /* requires version 2 */
  38. #define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 /* requires version 2 */
  39. #define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090 /* requires version 2 */
  40. #define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094 /* requires version 2 */
  41. #define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0 /* requires version 2 */
  42. #define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 /* requires version 2 */
  43. #define VIRTIO_MMIO_CONFIG_GENERATION 0x100 /* requires version 2 */
  44. #define VIRTIO_MMIO_CONFIG 0x100
  45. #define VIRTIO_MMIO_INT_VRING (1 << 0)
  46. #define VIRTIO_MMIO_INT_CONFIG (1 << 1)
  47. #define VIRTIO_MMIO_VRING_ALIGN 4096
  48. static inline uint32_t virtio_mmio_read32(uint32_t *base, size_t offset)
  49. {
  50. return *((volatile uint32_t*) (((uintptr_t) base) + offset));
  51. }
  52. static inline uint16_t virtio_mmio_read16(uint32_t *base, size_t offset)
  53. {
  54. return *((volatile uint16_t*) (((uintptr_t) base) + offset));
  55. }
  56. static inline uint8_t virtio_mmio_read8(uint32_t *base, size_t offset)
  57. {
  58. return *((volatile uint8_t*) (((uintptr_t) base) + offset));
  59. }
  60. static inline void virtio_mmio_write32(uint32_t *base, size_t offset, uint32_t val)
  61. {
  62. *((volatile uint32_t*) (((uintptr_t) base) + offset)) = val;
  63. }
  64. void virtio_mmio_print_configs(uint32_t *device_base);
  65. #endif /* VIRTIO_MMIO_H */