vfio-container-base.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * VFIO BASE CONTAINER
  3. *
  4. * Copyright (C) 2023 Intel Corporation.
  5. * Copyright Red Hat, Inc. 2023
  6. *
  7. * Authors: Yi Liu <yi.l.liu@intel.com>
  8. * Eric Auger <eric.auger@redhat.com>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0-or-later
  11. */
  12. #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H
  13. #define HW_VFIO_VFIO_CONTAINER_BASE_H
  14. #include "exec/memory.h"
  15. typedef struct VFIODevice VFIODevice;
  16. typedef struct VFIOIOMMUClass VFIOIOMMUClass;
  17. typedef struct {
  18. unsigned long *bitmap;
  19. hwaddr size;
  20. hwaddr pages;
  21. } VFIOBitmap;
  22. typedef struct VFIOAddressSpace {
  23. AddressSpace *as;
  24. QLIST_HEAD(, VFIOContainerBase) containers;
  25. QLIST_ENTRY(VFIOAddressSpace) list;
  26. } VFIOAddressSpace;
  27. /*
  28. * This is the base object for vfio container backends
  29. */
  30. typedef struct VFIOContainerBase {
  31. Object parent;
  32. VFIOAddressSpace *space;
  33. MemoryListener listener;
  34. Error *error;
  35. bool initialized;
  36. uint64_t dirty_pgsizes;
  37. uint64_t max_dirty_bitmap_size;
  38. unsigned long pgsizes;
  39. unsigned int dma_max_mappings;
  40. bool dirty_pages_supported;
  41. bool dirty_pages_started; /* Protected by BQL */
  42. QLIST_HEAD(, VFIOGuestIOMMU) giommu_list;
  43. QLIST_HEAD(, VFIORamDiscardListener) vrdl_list;
  44. QLIST_ENTRY(VFIOContainerBase) next;
  45. QLIST_HEAD(, VFIODevice) device_list;
  46. GList *iova_ranges;
  47. NotifierWithReturn cpr_reboot_notifier;
  48. } VFIOContainerBase;
  49. typedef struct VFIOGuestIOMMU {
  50. VFIOContainerBase *bcontainer;
  51. IOMMUMemoryRegion *iommu_mr;
  52. hwaddr iommu_offset;
  53. IOMMUNotifier n;
  54. QLIST_ENTRY(VFIOGuestIOMMU) giommu_next;
  55. } VFIOGuestIOMMU;
  56. typedef struct VFIORamDiscardListener {
  57. VFIOContainerBase *bcontainer;
  58. MemoryRegion *mr;
  59. hwaddr offset_within_address_space;
  60. hwaddr size;
  61. uint64_t granularity;
  62. RamDiscardListener listener;
  63. QLIST_ENTRY(VFIORamDiscardListener) next;
  64. } VFIORamDiscardListener;
  65. int vfio_container_dma_map(VFIOContainerBase *bcontainer,
  66. hwaddr iova, ram_addr_t size,
  67. void *vaddr, bool readonly);
  68. int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
  69. hwaddr iova, ram_addr_t size,
  70. IOMMUTLBEntry *iotlb);
  71. bool vfio_container_add_section_window(VFIOContainerBase *bcontainer,
  72. MemoryRegionSection *section,
  73. Error **errp);
  74. void vfio_container_del_section_window(VFIOContainerBase *bcontainer,
  75. MemoryRegionSection *section);
  76. int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer,
  77. bool start, Error **errp);
  78. int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
  79. VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
  80. GList *vfio_container_get_iova_ranges(const VFIOContainerBase *bcontainer);
  81. static inline uint64_t
  82. vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)
  83. {
  84. assert(bcontainer);
  85. return bcontainer->pgsizes;
  86. }
  87. #define TYPE_VFIO_IOMMU "vfio-iommu"
  88. #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"
  89. #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"
  90. #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd"
  91. OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)
  92. struct VFIOIOMMUClass {
  93. ObjectClass parent_class;
  94. /* Properties */
  95. const char *hiod_typename;
  96. /* basic feature */
  97. bool (*setup)(VFIOContainerBase *bcontainer, Error **errp);
  98. int (*dma_map)(const VFIOContainerBase *bcontainer,
  99. hwaddr iova, ram_addr_t size,
  100. void *vaddr, bool readonly);
  101. int (*dma_unmap)(const VFIOContainerBase *bcontainer,
  102. hwaddr iova, ram_addr_t size,
  103. IOMMUTLBEntry *iotlb);
  104. bool (*attach_device)(const char *name, VFIODevice *vbasedev,
  105. AddressSpace *as, Error **errp);
  106. void (*detach_device)(VFIODevice *vbasedev);
  107. /* migration feature */
  108. /**
  109. * @set_dirty_page_tracking
  110. *
  111. * Start or stop dirty pages tracking on VFIO container
  112. *
  113. * @bcontainer: #VFIOContainerBase on which to de/activate dirty
  114. * page tracking
  115. * @start: indicates whether to start or stop dirty pages tracking
  116. * @errp: pointer to Error*, to store an error if it happens.
  117. *
  118. * Returns zero to indicate success and negative for error
  119. */
  120. int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
  121. bool start, Error **errp);
  122. /**
  123. * @query_dirty_bitmap
  124. *
  125. * Get bitmap of dirty pages from container
  126. *
  127. * @bcontainer: #VFIOContainerBase from which to get dirty pages
  128. * @vbmap: #VFIOBitmap internal bitmap structure
  129. * @iova: iova base address
  130. * @size: size of iova range
  131. * @errp: pointer to Error*, to store an error if it happens.
  132. *
  133. * Returns zero to indicate success and negative for error
  134. */
  135. int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer,
  136. VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp);
  137. /* PCI specific */
  138. int (*pci_hot_reset)(VFIODevice *vbasedev, bool single);
  139. /* SPAPR specific */
  140. bool (*add_window)(VFIOContainerBase *bcontainer,
  141. MemoryRegionSection *section,
  142. Error **errp);
  143. void (*del_window)(VFIOContainerBase *bcontainer,
  144. MemoryRegionSection *section);
  145. void (*release)(VFIOContainerBase *bcontainer);
  146. };
  147. #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */