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 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 typedef struct VFIOMigration { 62 struct VFIODevice *vbasedev; 63 VMChangeStateEntry *vm_state; 64 Notifier migration_state; 65 uint32_t device_state; 66 int data_fd; 67 void *data_buffer; 68 size_t data_buffer_size; 69 uint64_t mig_flags; 70 uint64_t precopy_init_size; 71 uint64_t precopy_dirty_size; 72 bool initial_data_sent; 73 } VFIOMigration; 74 75 typedef struct VFIOAddressSpace { 76 AddressSpace *as; 77 QLIST_HEAD(, VFIOContainer) containers; 78 QLIST_ENTRY(VFIOAddressSpace) list; 79 } VFIOAddressSpace; 80 81 struct VFIOGroup; 82 83 typedef struct VFIOContainer { 84 VFIOAddressSpace *space; 85 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 86 MemoryListener listener; 87 MemoryListener prereg_listener; 88 unsigned iommu_type; 89 Error *error; 90 bool initialized; 91 bool dirty_pages_supported; 92 uint64_t dirty_pgsizes; 93 uint64_t max_dirty_bitmap_size; 94 unsigned long pgsizes; 95 unsigned int dma_max_mappings; 96 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; 97 QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; 98 QLIST_HEAD(, VFIOGroup) group_list; 99 QLIST_HEAD(, VFIORamDiscardListener) vrdl_list; 100 QLIST_ENTRY(VFIOContainer) next; 101 QLIST_HEAD(, VFIODevice) device_list; 102 } VFIOContainer; 103 104 typedef struct VFIOGuestIOMMU { 105 VFIOContainer *container; 106 IOMMUMemoryRegion *iommu_mr; 107 hwaddr iommu_offset; 108 IOMMUNotifier n; 109 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; 110 } VFIOGuestIOMMU; 111 112 typedef struct VFIORamDiscardListener { 113 VFIOContainer *container; 114 MemoryRegion *mr; 115 hwaddr offset_within_address_space; 116 hwaddr size; 117 uint64_t granularity; 118 RamDiscardListener listener; 119 QLIST_ENTRY(VFIORamDiscardListener) next; 120 } VFIORamDiscardListener; 121 122 typedef struct VFIOHostDMAWindow { 123 hwaddr min_iova; 124 hwaddr max_iova; 125 uint64_t iova_pgsizes; 126 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 127 } VFIOHostDMAWindow; 128 129 typedef struct VFIODeviceOps VFIODeviceOps; 130 131 typedef struct VFIODevice { 132 QLIST_ENTRY(VFIODevice) next; 133 QLIST_ENTRY(VFIODevice) container_next; 134 QLIST_ENTRY(VFIODevice) global_next; 135 struct VFIOGroup *group; 136 VFIOContainer *container; 137 char *sysfsdev; 138 char *name; 139 DeviceState *dev; 140 int fd; 141 int type; 142 bool reset_works; 143 bool needs_reset; 144 bool no_mmap; 145 bool ram_block_discard_allowed; 146 OnOffAuto enable_migration; 147 VFIODeviceOps *ops; 148 unsigned int num_irqs; 149 unsigned int num_regions; 150 unsigned int flags; 151 VFIOMigration *migration; 152 Error *migration_blocker; 153 OnOffAuto pre_copy_dirty_page_tracking; 154 bool dirty_pages_supported; 155 bool dirty_tracking; 156 } VFIODevice; 157 158 struct VFIODeviceOps { 159 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 160 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 161 void (*vfio_eoi)(VFIODevice *vdev); 162 Object *(*vfio_get_object)(VFIODevice *vdev); 163 void (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f); 164 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 165 }; 166 167 typedef struct VFIOGroup { 168 int fd; 169 int groupid; 170 VFIOContainer *container; 171 QLIST_HEAD(, VFIODevice) device_list; 172 QLIST_ENTRY(VFIOGroup) next; 173 QLIST_ENTRY(VFIOGroup) container_next; 174 bool ram_block_discard_allowed; 175 } VFIOGroup; 176 177 typedef struct VFIODMABuf { 178 QemuDmaBuf buf; 179 uint32_t pos_x, pos_y, pos_updates; 180 uint32_t hot_x, hot_y, hot_updates; 181 int dmabuf_id; 182 QTAILQ_ENTRY(VFIODMABuf) next; 183 } VFIODMABuf; 184 185 typedef struct VFIODisplay { 186 QemuConsole *con; 187 RAMFBState *ramfb; 188 struct vfio_region_info *edid_info; 189 struct vfio_region_gfx_edid *edid_regs; 190 uint8_t *edid_blob; 191 QEMUTimer *edid_link_timer; 192 struct { 193 VFIORegion buffer; 194 DisplaySurface *surface; 195 } region; 196 struct { 197 QTAILQ_HEAD(, VFIODMABuf) bufs; 198 VFIODMABuf *primary; 199 VFIODMABuf *cursor; 200 } dmabuf; 201 } VFIODisplay; 202 203 typedef struct { 204 unsigned long *bitmap; 205 hwaddr size; 206 hwaddr pages; 207 } VFIOBitmap; 208 209 void vfio_host_win_add(VFIOContainer *container, 210 hwaddr min_iova, hwaddr max_iova, 211 uint64_t iova_pgsizes); 212 int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova, 213 hwaddr max_iova); 214 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 215 void vfio_put_address_space(VFIOAddressSpace *space); 216 bool vfio_devices_all_running_and_saving(VFIOContainer *container); 217 218 /* container->fd */ 219 int vfio_dma_unmap(VFIOContainer *container, hwaddr iova, 220 ram_addr_t size, IOMMUTLBEntry *iotlb); 221 int vfio_dma_map(VFIOContainer *container, hwaddr iova, 222 ram_addr_t size, void *vaddr, bool readonly); 223 int vfio_set_dirty_page_tracking(VFIOContainer *container, bool start); 224 int vfio_query_dirty_bitmap(VFIOContainer *container, VFIOBitmap *vbmap, 225 hwaddr iova, hwaddr size); 226 227 int vfio_container_add_section_window(VFIOContainer *container, 228 MemoryRegionSection *section, 229 Error **errp); 230 void vfio_container_del_section_window(VFIOContainer *container, 231 MemoryRegionSection *section); 232 233 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 234 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 235 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 236 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 237 int action, int fd, Error **errp); 238 void vfio_region_write(void *opaque, hwaddr addr, 239 uint64_t data, unsigned size); 240 uint64_t vfio_region_read(void *opaque, 241 hwaddr addr, unsigned size); 242 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 243 int index, const char *name); 244 int vfio_region_mmap(VFIORegion *region); 245 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 246 void vfio_region_unmap(VFIORegion *region); 247 void vfio_region_exit(VFIORegion *region); 248 void vfio_region_finalize(VFIORegion *region); 249 void vfio_reset_handler(void *opaque); 250 struct vfio_device_info *vfio_get_device_info(int fd); 251 int vfio_attach_device(char *name, VFIODevice *vbasedev, 252 AddressSpace *as, Error **errp); 253 void vfio_detach_device(VFIODevice *vbasedev); 254 255 int vfio_kvm_device_add_fd(int fd, Error **errp); 256 int vfio_kvm_device_del_fd(int fd, Error **errp); 257 258 extern const MemoryRegionOps vfio_region_ops; 259 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 260 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 261 extern VFIOGroupList vfio_group_list; 262 extern VFIODeviceList vfio_device_list; 263 264 extern const MemoryListener vfio_memory_listener; 265 extern int vfio_kvm_device_fd; 266 267 bool vfio_mig_active(void); 268 int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp); 269 void vfio_unblock_multiple_devices_migration(void); 270 bool vfio_viommu_preset(VFIODevice *vbasedev); 271 int64_t vfio_mig_bytes_transferred(void); 272 void vfio_reset_bytes_transferred(void); 273 bool vfio_device_state_is_running(VFIODevice *vbasedev); 274 bool vfio_device_state_is_precopy(VFIODevice *vbasedev); 275 276 #ifdef CONFIG_LINUX 277 int vfio_get_region_info(VFIODevice *vbasedev, int index, 278 struct vfio_region_info **info); 279 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 280 uint32_t subtype, struct vfio_region_info **info); 281 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 282 struct vfio_info_cap_header * 283 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 284 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 285 unsigned int *avail); 286 struct vfio_info_cap_header * 287 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 288 struct vfio_info_cap_header * 289 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 290 #endif 291 extern const MemoryListener vfio_prereg_listener; 292 293 int vfio_spapr_create_window(VFIOContainer *container, 294 MemoryRegionSection *section, 295 hwaddr *pgsize); 296 int vfio_spapr_remove_window(VFIOContainer *container, 297 hwaddr offset_within_address_space); 298 299 bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp); 300 void vfio_migration_exit(VFIODevice *vbasedev); 301 302 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 303 bool vfio_devices_all_running_and_mig_active(VFIOContainer *container); 304 bool vfio_devices_all_device_dirty_tracking(VFIOContainer *container); 305 int vfio_devices_query_dirty_bitmap(VFIOContainer *container, 306 VFIOBitmap *vbmap, hwaddr iova, 307 hwaddr size); 308 int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova, 309 uint64_t size, ram_addr_t ram_addr); 310 #endif /* HW_VFIO_VFIO_COMMON_H */ 311