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 "qemu-common.h" 25 #include "exec/memory.h" 26 #include "qemu/queue.h" 27 #include "qemu/notify.h" 28 #include "ui/console.h" 29 #include "hw/display/ramfb.h" 30 #ifdef CONFIG_LINUX 31 #include <linux/vfio.h> 32 #endif 33 34 #define ERR_PREFIX "vfio error: %s: " 35 #define WARN_PREFIX "vfio warning: %s: " 36 37 enum { 38 VFIO_DEVICE_TYPE_PCI = 0, 39 VFIO_DEVICE_TYPE_PLATFORM = 1, 40 VFIO_DEVICE_TYPE_CCW = 2, 41 VFIO_DEVICE_TYPE_AP = 3, 42 }; 43 44 typedef struct VFIOMmap { 45 MemoryRegion mem; 46 void *mmap; 47 off_t offset; 48 size_t size; 49 } VFIOMmap; 50 51 typedef struct VFIORegion { 52 struct VFIODevice *vbasedev; 53 off_t fd_offset; /* offset of region within device fd */ 54 MemoryRegion *mem; /* slow, read/write access */ 55 size_t size; 56 uint32_t flags; /* VFIO region flags (rd/wr/mmap) */ 57 uint32_t nr_mmaps; 58 VFIOMmap *mmaps; 59 uint8_t nr; /* cache the region number for debug */ 60 } VFIORegion; 61 62 typedef struct VFIOAddressSpace { 63 AddressSpace *as; 64 QLIST_HEAD(, VFIOContainer) containers; 65 QLIST_ENTRY(VFIOAddressSpace) list; 66 } VFIOAddressSpace; 67 68 struct VFIOGroup; 69 70 typedef struct VFIOContainer { 71 VFIOAddressSpace *space; 72 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 73 MemoryListener listener; 74 MemoryListener prereg_listener; 75 unsigned iommu_type; 76 int error; 77 bool initialized; 78 unsigned long pgsizes; 79 /* 80 * This assumes the host IOMMU can support only a single 81 * contiguous IOVA window. We may need to generalize that in 82 * future 83 */ 84 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; 85 QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; 86 QLIST_HEAD(, VFIOGroup) group_list; 87 QLIST_ENTRY(VFIOContainer) next; 88 } VFIOContainer; 89 90 typedef struct VFIOGuestIOMMU { 91 VFIOContainer *container; 92 IOMMUMemoryRegion *iommu; 93 hwaddr iommu_offset; 94 IOMMUNotifier n; 95 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; 96 } VFIOGuestIOMMU; 97 98 typedef struct VFIOHostDMAWindow { 99 hwaddr min_iova; 100 hwaddr max_iova; 101 uint64_t iova_pgsizes; 102 QLIST_ENTRY(VFIOHostDMAWindow) hostwin_next; 103 } VFIOHostDMAWindow; 104 105 typedef struct VFIODeviceOps VFIODeviceOps; 106 107 typedef struct VFIODevice { 108 QLIST_ENTRY(VFIODevice) next; 109 struct VFIOGroup *group; 110 char *sysfsdev; 111 char *name; 112 DeviceState *dev; 113 int fd; 114 int type; 115 bool reset_works; 116 bool needs_reset; 117 bool no_mmap; 118 bool balloon_allowed; 119 VFIODeviceOps *ops; 120 unsigned int num_irqs; 121 unsigned int num_regions; 122 unsigned int flags; 123 } VFIODevice; 124 125 struct VFIODeviceOps { 126 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 127 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 128 void (*vfio_eoi)(VFIODevice *vdev); 129 }; 130 131 typedef struct VFIOGroup { 132 int fd; 133 int groupid; 134 VFIOContainer *container; 135 QLIST_HEAD(, VFIODevice) device_list; 136 QLIST_ENTRY(VFIOGroup) next; 137 QLIST_ENTRY(VFIOGroup) container_next; 138 bool balloon_allowed; 139 } VFIOGroup; 140 141 typedef struct VFIODMABuf { 142 QemuDmaBuf buf; 143 uint32_t pos_x, pos_y, pos_updates; 144 uint32_t hot_x, hot_y, hot_updates; 145 int dmabuf_id; 146 QTAILQ_ENTRY(VFIODMABuf) next; 147 } VFIODMABuf; 148 149 typedef struct VFIODisplay { 150 QemuConsole *con; 151 RAMFBState *ramfb; 152 struct { 153 VFIORegion buffer; 154 DisplaySurface *surface; 155 } region; 156 struct { 157 QTAILQ_HEAD(, VFIODMABuf) bufs; 158 VFIODMABuf *primary; 159 VFIODMABuf *cursor; 160 } dmabuf; 161 } VFIODisplay; 162 163 void vfio_put_base_device(VFIODevice *vbasedev); 164 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 165 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 166 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 167 void vfio_region_write(void *opaque, hwaddr addr, 168 uint64_t data, unsigned size); 169 uint64_t vfio_region_read(void *opaque, 170 hwaddr addr, unsigned size); 171 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 172 int index, const char *name); 173 int vfio_region_mmap(VFIORegion *region); 174 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 175 void vfio_region_exit(VFIORegion *region); 176 void vfio_region_finalize(VFIORegion *region); 177 void vfio_reset_handler(void *opaque); 178 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp); 179 void vfio_put_group(VFIOGroup *group); 180 int vfio_get_device(VFIOGroup *group, const char *name, 181 VFIODevice *vbasedev, Error **errp); 182 183 extern const MemoryRegionOps vfio_region_ops; 184 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list; 185 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces; 186 187 #ifdef CONFIG_LINUX 188 int vfio_get_region_info(VFIODevice *vbasedev, int index, 189 struct vfio_region_info **info); 190 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 191 uint32_t subtype, struct vfio_region_info **info); 192 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 193 #endif 194 extern const MemoryListener vfio_prereg_listener; 195 196 int vfio_spapr_create_window(VFIOContainer *container, 197 MemoryRegionSection *section, 198 hwaddr *pgsize); 199 int vfio_spapr_remove_window(VFIOContainer *container, 200 hwaddr offset_within_address_space); 201 202 #endif /* HW_VFIO_VFIO_COMMON_H */ 203