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 #ifndef HW_VFIO_VFIO_COMMON_H 21 #define HW_VFIO_VFIO_COMMON_H 22 23 #include "qemu-common.h" 24 #include "exec/address-spaces.h" 25 #include "exec/memory.h" 26 #include "qemu/queue.h" 27 #include "qemu/notify.h" 28 #ifdef CONFIG_LINUX 29 #include <linux/vfio.h> 30 #endif 31 32 /*#define DEBUG_VFIO*/ 33 #ifdef DEBUG_VFIO 34 #define DPRINTF(fmt, ...) \ 35 do { fprintf(stderr, "vfio: " fmt, ## __VA_ARGS__); } while (0) 36 #else 37 #define DPRINTF(fmt, ...) \ 38 do { } while (0) 39 #endif 40 41 enum { 42 VFIO_DEVICE_TYPE_PCI = 0, 43 VFIO_DEVICE_TYPE_PLATFORM = 1, 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 VFIOAddressSpace { 65 AddressSpace *as; 66 QLIST_HEAD(, VFIOContainer) containers; 67 QLIST_ENTRY(VFIOAddressSpace) list; 68 } VFIOAddressSpace; 69 70 struct VFIOGroup; 71 72 typedef struct VFIOContainer { 73 VFIOAddressSpace *space; 74 int fd; /* /dev/vfio/vfio, empowered by the attached groups */ 75 MemoryListener listener; 76 int error; 77 bool initialized; 78 /* 79 * This assumes the host IOMMU can support only a single 80 * contiguous IOVA window. We may need to generalize that in 81 * future 82 */ 83 hwaddr min_iova, max_iova; 84 uint64_t iova_pgsizes; 85 QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; 86 QLIST_HEAD(, VFIOGroup) group_list; 87 QLIST_ENTRY(VFIOContainer) next; 88 } VFIOContainer; 89 90 typedef struct VFIOGuestIOMMU { 91 VFIOContainer *container; 92 MemoryRegion *iommu; 93 Notifier n; 94 QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; 95 } VFIOGuestIOMMU; 96 97 typedef struct VFIODeviceOps VFIODeviceOps; 98 99 typedef struct VFIODevice { 100 QLIST_ENTRY(VFIODevice) next; 101 struct VFIOGroup *group; 102 char *sysfsdev; 103 char *name; 104 int fd; 105 int type; 106 bool reset_works; 107 bool needs_reset; 108 bool no_mmap; 109 VFIODeviceOps *ops; 110 unsigned int num_irqs; 111 unsigned int num_regions; 112 unsigned int flags; 113 } VFIODevice; 114 115 struct VFIODeviceOps { 116 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 117 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 118 void (*vfio_eoi)(VFIODevice *vdev); 119 }; 120 121 typedef struct VFIOGroup { 122 int fd; 123 int groupid; 124 VFIOContainer *container; 125 QLIST_HEAD(, VFIODevice) device_list; 126 QLIST_ENTRY(VFIOGroup) next; 127 QLIST_ENTRY(VFIOGroup) container_next; 128 } VFIOGroup; 129 130 void vfio_put_base_device(VFIODevice *vbasedev); 131 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 132 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 133 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 134 void vfio_region_write(void *opaque, hwaddr addr, 135 uint64_t data, unsigned size); 136 uint64_t vfio_region_read(void *opaque, 137 hwaddr addr, unsigned size); 138 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 139 int index, const char *name); 140 int vfio_region_mmap(VFIORegion *region); 141 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled); 142 void vfio_region_exit(VFIORegion *region); 143 void vfio_region_finalize(VFIORegion *region); 144 void vfio_reset_handler(void *opaque); 145 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as); 146 void vfio_put_group(VFIOGroup *group); 147 int vfio_get_device(VFIOGroup *group, const char *name, 148 VFIODevice *vbasedev); 149 150 extern const MemoryRegionOps vfio_region_ops; 151 extern QLIST_HEAD(vfio_group_head, VFIOGroup) vfio_group_list; 152 extern QLIST_HEAD(vfio_as_head, VFIOAddressSpace) vfio_address_spaces; 153 154 #ifdef CONFIG_LINUX 155 int vfio_get_region_info(VFIODevice *vbasedev, int index, 156 struct vfio_region_info **info); 157 #endif 158 #endif /* !HW_VFIO_VFIO_COMMON_H */ 159