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 #include "qemu/notify.h" 27 #include "ui/console.h" 28 #include "hw/display/ramfb.h" 29 #ifdef CONFIG_LINUX 30 #include <linux/vfio.h> 31 #endif 32 #include "system/system.h" 33 #include "hw/vfio/vfio-container-base.h" 34 #include "system/host_iommu_device.h" 35 #include "system/iommufd.h" 36 37 #define VFIO_MSG_PREFIX "vfio %s: " 38 39 /* 40 * Flags to be used as unique delimiters for VFIO devices in the migration 41 * stream. These flags are composed as: 42 * 0xffffffff => MSB 32-bit all 1s 43 * 0xef10 => Magic ID, represents emulated (virtual) function IO 44 * 0x0000 => 16-bits reserved for flags 45 * 46 * The beginning of state information is marked by _DEV_CONFIG_STATE, 47 * _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a 48 * certain state information is marked by _END_OF_STATE. 49 */ 50 #define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL) 51 #define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL) 52 #define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL) 53 #define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL) 54 #define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL) 55 56 enum { 57 VFIO_DEVICE_TYPE_PCI = 0, 58 VFIO_DEVICE_TYPE_PLATFORM = 1, 59 VFIO_DEVICE_TYPE_CCW = 2, 60 VFIO_DEVICE_TYPE_AP = 3, 61 }; 62 63 typedef struct VFIOMmap { 64 MemoryRegion mem; 65 void *mmap; 66 off_t offset; 67 size_t size; 68 } VFIOMmap; 69 70 typedef struct VFIORegion { 71 struct VFIODevice *vbasedev; 72 off_t fd_offset; /* offset of region within device fd */ 73 MemoryRegion *mem; /* slow, read/write access */ 74 size_t size; 75 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */ 76 uint32_t nr_mmaps; 77 VFIOMmap *mmaps; 78 uint8_t nr; /* cache the region number for debug */ 79 } VFIORegion; 80 81 typedef struct VFIOMultifd VFIOMultifd; 82 83 typedef struct VFIOMigration { 84 struct VFIODevice *vbasedev; 85 VMChangeStateEntry *vm_state; 86 NotifierWithReturn migration_state; 87 uint32_t device_state; 88 int data_fd; 89 void *data_buffer; 90 size_t data_buffer_size; 91 uint64_t mig_flags; 92 uint64_t precopy_init_size; 93 uint64_t precopy_dirty_size; 94 bool multifd_transfer; 95 VFIOMultifd *multifd; 96 bool initial_data_sent; 97 98 bool event_save_iterate_started; 99 bool event_precopy_empty_hit; 100 } VFIOMigration; 101 102 struct VFIOGroup; 103 104 typedef struct VFIOContainer { 105 VFIOContainerBase bcontainer; 106 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 107 unsigned iommu_type; 108 QLIST_HEAD(, VFIOGroup) group_list; 109 } VFIOContainer; 110 111 OBJECT_DECLARE_SIMPLE_TYPE(VFIOContainer, VFIO_IOMMU_LEGACY); 112 113 typedef struct VFIOHostDMAWindow { 114 hwaddr min_iova; 115 hwaddr max_iova; 116 uint64_t iova_pgsizes; 117 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 118 } VFIOHostDMAWindow; 119 120 typedef struct IOMMUFDBackend IOMMUFDBackend; 121 122 typedef struct VFIOIOASHwpt { 123 uint32_t hwpt_id; 124 uint32_t hwpt_flags; 125 QLIST_HEAD(, VFIODevice) device_list; 126 QLIST_ENTRY(VFIOIOASHwpt) next; 127 } VFIOIOASHwpt; 128 129 typedef struct VFIOIOMMUFDContainer { 130 VFIOContainerBase bcontainer; 131 IOMMUFDBackend *be; 132 uint32_t ioas_id; 133 QLIST_HEAD(, VFIOIOASHwpt) hwpt_list; 134 } VFIOIOMMUFDContainer; 135 136 OBJECT_DECLARE_SIMPLE_TYPE(VFIOIOMMUFDContainer, VFIO_IOMMU_IOMMUFD); 137 138 typedef struct VFIODeviceOps VFIODeviceOps; 139 140 typedef struct VFIODevice { 141 QLIST_ENTRY(VFIODevice) next; 142 QLIST_ENTRY(VFIODevice) container_next; 143 QLIST_ENTRY(VFIODevice) global_next; 144 struct VFIOGroup *group; 145 VFIOContainerBase *bcontainer; 146 char *sysfsdev; 147 char *name; 148 DeviceState *dev; 149 int fd; 150 int type; 151 bool mdev; 152 bool reset_works; 153 bool needs_reset; 154 bool no_mmap; 155 bool ram_block_discard_allowed; 156 OnOffAuto enable_migration; 157 OnOffAuto migration_multifd_transfer; 158 bool migration_events; 159 VFIODeviceOps *ops; 160 unsigned int num_irqs; 161 unsigned int num_regions; 162 unsigned int flags; 163 VFIOMigration *migration; 164 Error *migration_blocker; 165 OnOffAuto pre_copy_dirty_page_tracking; 166 OnOffAuto device_dirty_page_tracking; 167 bool dirty_pages_supported; 168 bool dirty_tracking; /* Protected by BQL */ 169 bool iommu_dirty_tracking; 170 HostIOMMUDevice *hiod; 171 int devid; 172 IOMMUFDBackend *iommufd; 173 VFIOIOASHwpt *hwpt; 174 QLIST_ENTRY(VFIODevice) hwpt_next; 175 } VFIODevice; 176 177 struct VFIODeviceOps { 178 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 179 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 180 void (*vfio_eoi)(VFIODevice *vdev); 181 Object *(*vfio_get_object)(VFIODevice *vdev); 182 183 /** 184 * @vfio_save_config 185 * 186 * Save device config state 187 * 188 * @vdev: #VFIODevice for which to save the config 189 * @f: #QEMUFile where to send the data 190 * @errp: pointer to Error*, to store an error if it happens. 191 * 192 * Returns zero to indicate success and negative for error 193 */ 194 int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 195 196 /** 197 * @vfio_load_config 198 * 199 * Load device config state 200 * 201 * @vdev: #VFIODevice for which to load the config 202 * @f: #QEMUFile where to get the data 203 * 204 * Returns zero to indicate success and negative for error 205 */ 206 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 207 }; 208 209 typedef struct VFIOGroup { 210 int fd; 211 int groupid; 212 VFIOContainer *container; 213 QLIST_HEAD(, VFIODevice) device_list; 214 QLIST_ENTRY(VFIOGroup) next; 215 QLIST_ENTRY(VFIOGroup) container_next; 216 bool ram_block_discard_allowed; 217 } VFIOGroup; 218 219 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" 220 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \ 221 TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio" 222 223 typedef struct VFIODMABuf { 224 QemuDmaBuf *buf; 225 uint32_t pos_x, pos_y, pos_updates; 226 uint32_t hot_x, hot_y, hot_updates; 227 int dmabuf_id; 228 QTAILQ_ENTRY(VFIODMABuf) next; 229 } VFIODMABuf; 230 231 typedef struct VFIODisplay { 232 QemuConsole *con; 233 RAMFBState *ramfb; 234 struct vfio_region_info *edid_info; 235 struct vfio_region_gfx_edid *edid_regs; 236 uint8_t *edid_blob; 237 QEMUTimer *edid_link_timer; 238 struct { 239 VFIORegion buffer; 240 DisplaySurface *surface; 241 } region; 242 struct { 243 QTAILQ_HEAD(, VFIODMABuf) bufs; 244 VFIODMABuf *primary; 245 VFIODMABuf *cursor; 246 } dmabuf; 247 } VFIODisplay; 248 249 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 250 void vfio_put_address_space(VFIOAddressSpace *space); 251 void vfio_address_space_insert(VFIOAddressSpace *space, 252 VFIOContainerBase *bcontainer); 253 254 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 255 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 256 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 257 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 258 int action, int fd, Error **errp); 259 void vfio_region_write(void *opaque, hwaddr addr, 260 uint64_t data, unsigned size); 261 uint64_t vfio_region_read(void *opaque, 262 hwaddr addr, unsigned size); 263 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 264 int index, const char *name); 265 int vfio_region_mmap(VFIORegion *region); 266 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 267 void vfio_region_unmap(VFIORegion *region); 268 void vfio_region_exit(VFIORegion *region); 269 void vfio_region_finalize(VFIORegion *region); 270 void vfio_reset_handler(void *opaque); 271 struct vfio_device_info *vfio_get_device_info(int fd); 272 bool vfio_device_is_mdev(VFIODevice *vbasedev); 273 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); 274 bool vfio_attach_device(char *name, VFIODevice *vbasedev, 275 AddressSpace *as, Error **errp); 276 void vfio_detach_device(VFIODevice *vbasedev); 277 VFIODevice *vfio_get_vfio_device(Object *obj); 278 279 int vfio_kvm_device_add_fd(int fd, Error **errp); 280 int vfio_kvm_device_del_fd(int fd, Error **errp); 281 282 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 283 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 284 285 extern const MemoryRegionOps vfio_region_ops; 286 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 287 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 288 extern VFIOGroupList vfio_group_list; 289 extern VFIODeviceList vfio_device_list; 290 extern const MemoryListener vfio_memory_listener; 291 extern int vfio_kvm_device_fd; 292 293 void vfio_migration_add_bytes_transferred(unsigned long val); 294 bool vfio_device_state_is_running(VFIODevice *vbasedev); 295 bool vfio_device_state_is_precopy(VFIODevice *vbasedev); 296 297 int vfio_save_device_config_state(QEMUFile *f, void *opaque, Error **errp); 298 int vfio_load_device_config_state(QEMUFile *f, void *opaque); 299 300 #ifdef CONFIG_LINUX 301 int vfio_get_region_info(VFIODevice *vbasedev, int index, 302 struct vfio_region_info **info); 303 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 304 uint32_t subtype, struct vfio_region_info **info); 305 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 306 struct vfio_info_cap_header * 307 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 308 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 309 unsigned int *avail); 310 struct vfio_info_cap_header * 311 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 312 struct vfio_info_cap_header * 313 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 314 315 int vfio_migration_set_state(VFIODevice *vbasedev, 316 enum vfio_device_mig_state new_state, 317 enum vfio_device_mig_state recover_state, 318 Error **errp); 319 #endif 320 321 bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp); 322 void vfio_migration_exit(VFIODevice *vbasedev); 323 324 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 325 bool vfio_devices_all_dirty_tracking_started( 326 const VFIOContainerBase *bcontainer); 327 bool 328 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 329 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 330 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 331 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 332 uint64_t size, ram_addr_t ram_addr, Error **errp); 333 334 /* Returns 0 on success, or a negative errno. */ 335 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 336 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 337 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 338 DeviceState *dev, bool ram_discard); 339 int vfio_device_get_aw_bits(VFIODevice *vdev); 340 #endif /* HW_VFIO_VFIO_COMMON_H */ 341