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