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 13 #ifndef HW_VFIO_VFIO_CONTAINER_BASE_H 14 #define HW_VFIO_VFIO_CONTAINER_BASE_H 15 16 #include "system/memory.h" 17 18 typedef struct VFIODevice VFIODevice; 19 typedef struct VFIOIOMMUClass VFIOIOMMUClass; 20 21 typedef struct { 22 unsigned long *bitmap; 23 hwaddr size; 24 hwaddr pages; 25 } VFIOBitmap; 26 27 typedef struct VFIOAddressSpace { 28 AddressSpace *as; 29 QLIST_HEAD(, VFIOContainerBase) containers; 30 QLIST_ENTRY(VFIOAddressSpace) list; 31 } VFIOAddressSpace; 32 33 /* 34 * This is the base object for vfio container backends 35 */ 36 typedef struct VFIOContainerBase { 37 Object parent; 38 VFIOAddressSpace *space; 39 MemoryListener listener; 40 Error *error; 41 bool initialized; 42 uint64_t dirty_pgsizes; 43 uint64_t max_dirty_bitmap_size; 44 unsigned long pgsizes; 45 unsigned int dma_max_mappings; 46 bool dirty_pages_supported; 47 bool dirty_pages_started; /* Protected by BQL */ 48 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; 49 QLIST_HEAD(, VFIORamDiscardListener) vrdl_list; 50 QLIST_ENTRY(VFIOContainerBase) next; 51 QLIST_HEAD(, VFIODevice) device_list; 52 GList *iova_ranges; 53 NotifierWithReturn cpr_reboot_notifier; 54 } VFIOContainerBase; 55 56 typedef struct VFIOGuestIOMMU { 57 VFIOContainerBase *bcontainer; 58 IOMMUMemoryRegion *iommu_mr; 59 hwaddr iommu_offset; 60 IOMMUNotifier n; 61 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; 62 } VFIOGuestIOMMU; 63 64 typedef struct VFIORamDiscardListener { 65 VFIOContainerBase *bcontainer; 66 MemoryRegion *mr; 67 hwaddr offset_within_address_space; 68 hwaddr size; 69 uint64_t granularity; 70 RamDiscardListener listener; 71 QLIST_ENTRY(VFIORamDiscardListener) next; 72 } VFIORamDiscardListener; 73 74 VFIOAddressSpace *vfio_address_space_get(AddressSpace *as); 75 void vfio_address_space_put(VFIOAddressSpace *space); 76 void vfio_address_space_insert(VFIOAddressSpace *space, 77 VFIOContainerBase *bcontainer); 78 79 int vfio_container_dma_map(VFIOContainerBase *bcontainer, 80 hwaddr iova, ram_addr_t size, 81 void *vaddr, bool readonly, MemoryRegion *mr); 82 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer, 83 hwaddr iova, ram_addr_t size, 84 IOMMUTLBEntry *iotlb, bool unmap_all); 85 bool vfio_container_add_section_window(VFIOContainerBase *bcontainer, 86 MemoryRegionSection *section, 87 Error **errp); 88 void vfio_container_del_section_window(VFIOContainerBase *bcontainer, 89 MemoryRegionSection *section); 90 int vfio_container_set_dirty_page_tracking(VFIOContainerBase *bcontainer, 91 bool start, Error **errp); 92 bool vfio_container_dirty_tracking_is_started( 93 const VFIOContainerBase *bcontainer); 94 bool vfio_container_devices_dirty_tracking_is_supported( 95 const VFIOContainerBase *bcontainer); 96 int vfio_container_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 97 uint64_t iova, uint64_t size, ram_addr_t ram_addr, Error **errp); 98 99 GList *vfio_container_get_iova_ranges(const VFIOContainerBase *bcontainer); 100 101 static inline uint64_t 102 vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer) 103 { 104 assert(bcontainer); 105 return bcontainer->pgsizes; 106 } 107 108 #define TYPE_VFIO_IOMMU "vfio-iommu" 109 #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy" 110 #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr" 111 #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" 112 #define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user" 113 114 OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU) 115 116 struct VFIOIOMMUClass { 117 ObjectClass parent_class; 118 119 /** 120 * @setup 121 * 122 * Perform basic setup of the container, including configuring IOMMU 123 * capabilities, IOVA ranges, supported page sizes, etc. 124 * 125 * @bcontainer: #VFIOContainerBase 126 * @errp: pointer to Error*, to store an error if it happens. 127 * 128 * Returns true to indicate success and false for error. 129 */ 130 bool (*setup)(VFIOContainerBase *bcontainer, Error **errp); 131 132 /** 133 * @listener_begin 134 * 135 * Called at the beginning of an address space update transaction. 136 * See #MemoryListener. 137 * 138 * @bcontainer: #VFIOContainerBase 139 */ 140 void (*listener_begin)(VFIOContainerBase *bcontainer); 141 142 /** 143 * @listener_commit 144 * 145 * Called at the end of an address space update transaction, 146 * See #MemoryListener. 147 * 148 * @bcontainer: #VFIOContainerBase 149 */ 150 void (*listener_commit)(VFIOContainerBase *bcontainer); 151 152 /** 153 * @dma_map 154 * 155 * Map an address range into the container. Note that the memory region is 156 * referenced within an RCU read lock region across this call. 157 * 158 * @bcontainer: #VFIOContainerBase to use 159 * @iova: start address to map 160 * @size: size of the range to map 161 * @vaddr: process virtual address of mapping 162 * @readonly: true if mapping should be readonly 163 * @mr: the memory region for this mapping 164 * 165 * Returns 0 to indicate success and -errno otherwise. 166 */ 167 int (*dma_map)(const VFIOContainerBase *bcontainer, 168 hwaddr iova, ram_addr_t size, 169 void *vaddr, bool readonly, MemoryRegion *mr); 170 /** 171 * @dma_map_file 172 * 173 * Map a file range for the container. 174 * 175 * @bcontainer: #VFIOContainerBase to use for map 176 * @iova: start address to map 177 * @size: size of the range to map 178 * @fd: descriptor of the file to map 179 * @start: starting file offset of the range to map 180 * @readonly: map read only if true 181 */ 182 int (*dma_map_file)(const VFIOContainerBase *bcontainer, 183 hwaddr iova, ram_addr_t size, 184 int fd, unsigned long start, bool readonly); 185 /** 186 * @dma_unmap 187 * 188 * Unmap an address range from the container. 189 * 190 * @bcontainer: #VFIOContainerBase to use for unmap 191 * @iova: start address to unmap 192 * @size: size of the range to unmap 193 * @iotlb: The IOMMU TLB mapping entry (or NULL) 194 * @unmap_all: if set, unmap the entire address space 195 * 196 * Returns 0 to indicate success and -errno otherwise. 197 */ 198 int (*dma_unmap)(const VFIOContainerBase *bcontainer, 199 hwaddr iova, ram_addr_t size, 200 IOMMUTLBEntry *iotlb, bool unmap_all); 201 202 203 /** 204 * @attach_device 205 * 206 * Associate the given device with a container and do some related 207 * initialization of the device context. 208 * 209 * @name: name of the device 210 * @vbasedev: the device 211 * @as: address space to use 212 * @errp: pointer to Error*, to store an error if it happens. 213 * 214 * Returns true to indicate success and false for error. 215 */ 216 bool (*attach_device)(const char *name, VFIODevice *vbasedev, 217 AddressSpace *as, Error **errp); 218 219 /* 220 * @detach_device 221 * 222 * Detach the given device from its container and clean up any necessary 223 * state. 224 * 225 * @vbasedev: the device to disassociate 226 */ 227 void (*detach_device)(VFIODevice *vbasedev); 228 229 /* migration feature */ 230 231 /** 232 * @set_dirty_page_tracking 233 * 234 * Start or stop dirty pages tracking on VFIO container 235 * 236 * @bcontainer: #VFIOContainerBase on which to de/activate dirty 237 * page tracking 238 * @start: indicates whether to start or stop dirty pages tracking 239 * @errp: pointer to Error*, to store an error if it happens. 240 * 241 * Returns zero to indicate success and negative for error. 242 */ 243 int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer, 244 bool start, Error **errp); 245 /** 246 * @query_dirty_bitmap 247 * 248 * Get bitmap of dirty pages from container 249 * 250 * @bcontainer: #VFIOContainerBase from which to get dirty pages 251 * @vbmap: #VFIOBitmap internal bitmap structure 252 * @iova: iova base address 253 * @size: size of iova range 254 * @errp: pointer to Error*, to store an error if it happens. 255 * 256 * Returns zero to indicate success and negative for error. 257 */ 258 int (*query_dirty_bitmap)(const VFIOContainerBase *bcontainer, 259 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 260 /* PCI specific */ 261 int (*pci_hot_reset)(VFIODevice *vbasedev, bool single); 262 263 /* SPAPR specific */ 264 bool (*add_window)(VFIOContainerBase *bcontainer, 265 MemoryRegionSection *section, 266 Error **errp); 267 void (*del_window)(VFIOContainerBase *bcontainer, 268 MemoryRegionSection *section); 269 void (*release)(VFIOContainerBase *bcontainer); 270 }; 271 272 VFIORamDiscardListener *vfio_find_ram_discard_listener( 273 VFIOContainerBase *bcontainer, MemoryRegionSection *section); 274 275 void vfio_container_region_add(VFIOContainerBase *bcontainer, 276 MemoryRegionSection *section, bool cpr_remap); 277 278 #endif /* HW_VFIO_VFIO_CONTAINER_BASE_H */ 279