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 VFIORegion region; 65 uint32_t device_state; 66 int vm_running; 67 Notifier migration_state; 68 uint64_t pending_bytes; 69 } VFIOMigration; 70 71 typedef struct VFIOAddressSpace { 72 AddressSpace *as; 73 QLIST_HEAD(, VFIOContainer) containers; 74 QLIST_ENTRY(VFIOAddressSpace) list; 75 } VFIOAddressSpace; 76 77 struct VFIOGroup; 78 79 typedef struct VFIOContainer { 80 VFIOAddressSpace *space; 81 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 82 MemoryListener listener; 83 MemoryListener prereg_listener; 84 unsigned iommu_type; 85 Error *error; 86 bool initialized; 87 bool dirty_pages_supported; 88 uint64_t dirty_pgsizes; 89 uint64_t max_dirty_bitmap_size; 90 unsigned long pgsizes; 91 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; 92 QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; 93 QLIST_HEAD(, VFIOGroup) group_list; 94 QLIST_ENTRY(VFIOContainer) next; 95 } VFIOContainer; 96 97 typedef struct VFIOGuestIOMMU { 98 VFIOContainer *container; 99 IOMMUMemoryRegion *iommu; 100 hwaddr iommu_offset; 101 IOMMUNotifier n; 102 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; 103 } VFIOGuestIOMMU; 104 105 typedef struct VFIOHostDMAWindow { 106 hwaddr min_iova; 107 hwaddr max_iova; 108 uint64_t iova_pgsizes; 109 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 110 } VFIOHostDMAWindow; 111 112 typedef struct VFIODeviceOps VFIODeviceOps; 113 114 typedef struct VFIODevice { 115 QLIST_ENTRY(VFIODevice) next; 116 struct VFIOGroup *group; 117 char *sysfsdev; 118 char *name; 119 DeviceState *dev; 120 int fd; 121 int type; 122 bool reset_works; 123 bool needs_reset; 124 bool no_mmap; 125 bool ram_block_discard_allowed; 126 VFIODeviceOps *ops; 127 unsigned int num_irqs; 128 unsigned int num_regions; 129 unsigned int flags; 130 VFIOMigration *migration; 131 Error *migration_blocker; 132 } VFIODevice; 133 134 struct VFIODeviceOps { 135 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 136 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 137 void (*vfio_eoi)(VFIODevice *vdev); 138 Object *(*vfio_get_object)(VFIODevice *vdev); 139 void (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f); 140 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 141 }; 142 143 typedef struct VFIOGroup { 144 int fd; 145 int groupid; 146 VFIOContainer *container; 147 QLIST_HEAD(, VFIODevice) device_list; 148 QLIST_ENTRY(VFIOGroup) next; 149 QLIST_ENTRY(VFIOGroup) container_next; 150 bool ram_block_discard_allowed; 151 } VFIOGroup; 152 153 typedef struct VFIODMABuf { 154 QemuDmaBuf buf; 155 uint32_t pos_x, pos_y, pos_updates; 156 uint32_t hot_x, hot_y, hot_updates; 157 int dmabuf_id; 158 QTAILQ_ENTRY(VFIODMABuf) next; 159 } VFIODMABuf; 160 161 typedef struct VFIODisplay { 162 QemuConsole *con; 163 RAMFBState *ramfb; 164 struct vfio_region_info *edid_info; 165 struct vfio_region_gfx_edid *edid_regs; 166 uint8_t *edid_blob; 167 QEMUTimer *edid_link_timer; 168 struct { 169 VFIORegion buffer; 170 DisplaySurface *surface; 171 } region; 172 struct { 173 QTAILQ_HEAD(, VFIODMABuf) bufs; 174 VFIODMABuf *primary; 175 VFIODMABuf *cursor; 176 } dmabuf; 177 } VFIODisplay; 178 179 void vfio_put_base_device(VFIODevice *vbasedev); 180 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 181 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 182 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 183 int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 184 int action, int fd, Error **errp); 185 void vfio_region_write(void *opaque, hwaddr addr, 186 uint64_t data, unsigned size); 187 uint64_t vfio_region_read(void *opaque, 188 hwaddr addr, unsigned size); 189 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 190 int index, const char *name); 191 int vfio_region_mmap(VFIORegion *region); 192 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 193 void vfio_region_unmap(VFIORegion *region); 194 void vfio_region_exit(VFIORegion *region); 195 void vfio_region_finalize(VFIORegion *region); 196 void vfio_reset_handler(void *opaque); 197 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp); 198 void vfio_put_group(VFIOGroup *group); 199 int vfio_get_device(VFIOGroup *group, const char *name, 200 VFIODevice *vbasedev, Error **errp); 201 202 extern const MemoryRegionOps vfio_region_ops; 203 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 204 extern VFIOGroupList vfio_group_list; 205 206 bool vfio_mig_active(void); 207 int64_t vfio_mig_bytes_transferred(void); 208 209 #ifdef CONFIG_LINUX 210 int vfio_get_region_info(VFIODevice *vbasedev, int index, 211 struct vfio_region_info **info); 212 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 213 uint32_t subtype, struct vfio_region_info **info); 214 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 215 struct vfio_info_cap_header * 216 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 217 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 218 unsigned int *avail); 219 struct vfio_info_cap_header * 220 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 221 #endif 222 extern const MemoryListener vfio_prereg_listener; 223 224 int vfio_spapr_create_window(VFIOContainer *container, 225 MemoryRegionSection *section, 226 hwaddr *pgsize); 227 int vfio_spapr_remove_window(VFIOContainer *container, 228 hwaddr offset_within_address_space); 229 230 int vfio_migration_probe(VFIODevice *vbasedev, Error **errp); 231 void vfio_migration_finalize(VFIODevice *vbasedev); 232 233 #endif /* HW_VFIO_VFIO_COMMON_H */ 234