1 /* 2 * common header for vfio based device assignment support 3 * 4 * Copyright Red Hat, Inc. 2012 5 * 6 * Authors: 7 * Alex Williamson <alex.williamson@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Based on qemu-kvm device-assignment: 13 * Adapted for KVM by Qumranet. 14 * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com) 15 * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com) 16 * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com) 17 * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com) 18 * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com) 19 */ 20 21 #ifndef HW_VFIO_VFIO_COMMON_H 22 #define HW_VFIO_VFIO_COMMON_H 23 24 #include "system/memory.h" 25 #include "qemu/queue.h" 26 #ifdef CONFIG_LINUX 27 #include <linux/vfio.h> 28 #endif 29 #include "system/system.h" 30 #include "hw/vfio/vfio-container-base.h" 31 #include "system/host_iommu_device.h" 32 #include "system/iommufd.h" 33 34 #define VFIO_MSG_PREFIX "vfio %s: " 35 36 enum { 37 VFIO_DEVICE_TYPE_PCI = 0, 38 VFIO_DEVICE_TYPE_PLATFORM = 1, 39 VFIO_DEVICE_TYPE_CCW = 2, 40 VFIO_DEVICE_TYPE_AP = 3, 41 }; 42 43 typedef struct VFIOMmap { 44 MemoryRegion mem; 45 void *mmap; 46 off_t offset; 47 size_t size; 48 } VFIOMmap; 49 50 typedef struct VFIORegion { 51 struct VFIODevice *vbasedev; 52 off_t fd_offset; /* offset of region within device fd */ 53 MemoryRegion *mem; /* slow, read/write access */ 54 size_t size; 55 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */ 56 uint32_t nr_mmaps; 57 VFIOMmap *mmaps; 58 uint8_t nr; /* cache the region number for debug */ 59 } VFIORegion; 60 61 struct VFIOGroup; 62 63 typedef struct VFIOContainer { 64 VFIOContainerBase bcontainer; 65 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 66 unsigned iommu_type; 67 QLIST_HEAD(, VFIOGroup) group_list; 68 } VFIOContainer; 69 70 OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY); 71 72 typedef struct VFIOHostDMAWindow { 73 hwaddr min_iova; 74 hwaddr max_iova; 75 uint64_t iova_pgsizes; 76 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 77 } VFIOHostDMAWindow; 78 79 typedef struct IOMMUFDBackend IOMMUFDBackend; 80 81 typedef struct VFIOIOASHwpt { 82 uint32_t hwpt_id; 83 uint32_t hwpt_flags; 84 QLIST_HEAD(, VFIODevice) device_list; 85 QLIST_ENTRY(VFIOIOASHwpt) next; 86 } VFIOIOASHwpt; 87 88 typedef struct VFIOIOMMUFDContainer { 89 VFIOContainerBase bcontainer; 90 IOMMUFDBackend *be; 91 uint32_t ioas_id; 92 QLIST_HEAD(, VFIOIOASHwpt) hwpt_list; 93 } VFIOIOMMUFDContainer; 94 95 OBJECT_DECLARE_SIMPLE_TYPE(VFIOIOMMUFDContainer, VFIO_IOMMU_IOMMUFD); 96 97 typedef struct VFIODeviceOps VFIODeviceOps; 98 typedef struct VFIOMigration VFIOMigration; 99 100 typedef struct VFIODevice { 101 QLIST_ENTRY(VFIODevice) next; 102 QLIST_ENTRY(VFIODevice) container_next; 103 QLIST_ENTRY(VFIODevice) global_next; 104 struct VFIOGroup *group; 105 VFIOContainerBase *bcontainer; 106 char *sysfsdev; 107 char *name; 108 DeviceState *dev; 109 int fd; 110 int type; 111 bool mdev; 112 bool reset_works; 113 bool needs_reset; 114 bool no_mmap; 115 bool ram_block_discard_allowed; 116 OnOffAuto enable_migration; 117 OnOffAuto migration_multifd_transfer; 118 bool migration_events; 119 VFIODeviceOps *ops; 120 unsigned int num_irqs; 121 unsigned int num_regions; 122 unsigned int flags; 123 VFIOMigration *migration; 124 Error *migration_blocker; 125 OnOffAuto pre_copy_dirty_page_tracking; 126 OnOffAuto device_dirty_page_tracking; 127 bool dirty_pages_supported; 128 bool dirty_tracking; /* Protected by BQL */ 129 bool iommu_dirty_tracking; 130 HostIOMMUDevice *hiod; 131 int devid; 132 IOMMUFDBackend *iommufd; 133 VFIOIOASHwpt *hwpt; 134 QLIST_ENTRY(VFIODevice) hwpt_next; 135 } VFIODevice; 136 137 struct VFIODeviceOps { 138 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 139 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 140 void (*vfio_eoi)(VFIODevice *vdev); 141 Object *(*vfio_get_object)(VFIODevice *vdev); 142 143 /** 144 * @vfio_save_config 145 * 146 * Save device config state 147 * 148 * @vdev: #VFIODevice for which to save the config 149 * @f: #QEMUFile where to send the data 150 * @errp: pointer to Error*, to store an error if it happens. 151 * 152 * Returns zero to indicate success and negative for error 153 */ 154 int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 155 156 /** 157 * @vfio_load_config 158 * 159 * Load device config state 160 * 161 * @vdev: #VFIODevice for which to load the config 162 * @f: #QEMUFile where to get the data 163 * 164 * Returns zero to indicate success and negative for error 165 */ 166 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 167 }; 168 169 typedef struct VFIOGroup { 170 int fd; 171 int groupid; 172 VFIOContainer *container; 173 QLIST_HEAD(, VFIODevice) device_list; 174 QLIST_ENTRY(VFIOGroup) next; 175 QLIST_ENTRY(VFIOGroup) container_next; 176 bool ram_block_discard_allowed; 177 } VFIOGroup; 178 179 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" 180 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \ 181 TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio" 182 183 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 184 void vfio_put_address_space(VFIOAddressSpace *space); 185 void vfio_address_space_insert(VFIOAddressSpace *space, 186 VFIOContainerBase *bcontainer); 187 188 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 189 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 190 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 191 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 192 int action, int fd, Error **errp); 193 void vfio_region_write(void *opaque, hwaddr addr, 194 uint64_t data, unsigned size); 195 uint64_t vfio_region_read(void *opaque, 196 hwaddr addr, unsigned size); 197 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 198 int index, const char *name); 199 int vfio_region_mmap(VFIORegion *region); 200 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 201 void vfio_region_unmap(VFIORegion *region); 202 void vfio_region_exit(VFIORegion *region); 203 void vfio_region_finalize(VFIORegion *region); 204 void vfio_reset_handler(void *opaque); 205 struct vfio_device_info *vfio_get_device_info(int fd); 206 bool vfio_device_is_mdev(VFIODevice *vbasedev); 207 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); 208 bool vfio_attach_device(char *name, VFIODevice *vbasedev, 209 AddressSpace *as, Error **errp); 210 void vfio_detach_device(VFIODevice *vbasedev); 211 VFIODevice *vfio_get_vfio_device(Object *obj); 212 213 int vfio_kvm_device_add_fd(int fd, Error **errp); 214 int vfio_kvm_device_del_fd(int fd, Error **errp); 215 216 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 217 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 218 219 extern const MemoryRegionOps vfio_region_ops; 220 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 221 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 222 extern VFIOGroupList vfio_group_list; 223 extern VFIODeviceList vfio_device_list; 224 extern const MemoryListener vfio_memory_listener; 225 extern int vfio_kvm_device_fd; 226 227 #ifdef CONFIG_LINUX 228 int vfio_get_region_info(VFIODevice *vbasedev, int index, 229 struct vfio_region_info **info); 230 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 231 uint32_t subtype, struct vfio_region_info **info); 232 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 233 struct vfio_info_cap_header * 234 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 235 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 236 unsigned int *avail); 237 struct vfio_info_cap_header * 238 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 239 struct vfio_info_cap_header * 240 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 241 #endif 242 243 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 244 bool vfio_devices_all_dirty_tracking_started( 245 const VFIOContainerBase *bcontainer); 246 bool 247 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 248 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 249 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 250 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 251 uint64_t size, ram_addr_t ram_addr, Error **errp); 252 253 /* Returns 0 on success, or a negative errno. */ 254 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 255 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 256 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 257 DeviceState *dev, bool ram_discard); 258 int vfio_device_get_aw_bits(VFIODevice *vdev); 259 #endif /* HW_VFIO_VFIO_COMMON_H */ 260