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 "system/memory.h" 25 #include "qemu/queue.h" 26 #ifdef CONFIG_LINUX 27 #include <linux/vfio.h> 28 #endif 29 #include "system/system.h" 30 #include "hw/vfio/vfio-container-base.h" 31 #include "system/host_iommu_device.h" 32 #include "system/iommufd.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 VFIODeviceOps VFIODeviceOps; 44 typedef struct VFIOMigration VFIOMigration; 45 46 typedef struct IOMMUFDBackend IOMMUFDBackend; 47 typedef struct VFIOIOASHwpt VFIOIOASHwpt; 48 49 typedef struct VFIODevice { 50 QLIST_ENTRY(VFIODevice) next; 51 QLIST_ENTRY(VFIODevice) container_next; 52 QLIST_ENTRY(VFIODevice) global_next; 53 struct VFIOGroup *group; 54 VFIOContainerBase *bcontainer; 55 char *sysfsdev; 56 char *name; 57 DeviceState *dev; 58 int fd; 59 int type; 60 bool mdev; 61 bool reset_works; 62 bool needs_reset; 63 bool no_mmap; 64 bool ram_block_discard_allowed; 65 OnOffAuto enable_migration; 66 OnOffAuto migration_multifd_transfer; 67 bool migration_events; 68 VFIODeviceOps *ops; 69 unsigned int num_irqs; 70 unsigned int num_regions; 71 unsigned int flags; 72 VFIOMigration *migration; 73 Error *migration_blocker; 74 OnOffAuto pre_copy_dirty_page_tracking; 75 OnOffAuto device_dirty_page_tracking; 76 bool dirty_pages_supported; 77 bool dirty_tracking; /* Protected by BQL */ 78 bool iommu_dirty_tracking; 79 HostIOMMUDevice *hiod; 80 int devid; 81 IOMMUFDBackend *iommufd; 82 VFIOIOASHwpt *hwpt; 83 QLIST_ENTRY(VFIODevice) hwpt_next; 84 } VFIODevice; 85 86 struct VFIODeviceOps { 87 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 88 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 89 void (*vfio_eoi)(VFIODevice *vdev); 90 Object *(*vfio_get_object)(VFIODevice *vdev); 91 92 /** 93 * @vfio_save_config 94 * 95 * Save device config state 96 * 97 * @vdev: #VFIODevice for which to save the config 98 * @f: #QEMUFile where to send the data 99 * @errp: pointer to Error*, to store an error if it happens. 100 * 101 * Returns zero to indicate success and negative for error 102 */ 103 int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 104 105 /** 106 * @vfio_load_config 107 * 108 * Load device config state 109 * 110 * @vdev: #VFIODevice for which to load the config 111 * @f: #QEMUFile where to get the data 112 * 113 * Returns zero to indicate success and negative for error 114 */ 115 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 116 }; 117 118 119 #define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio" 120 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD_VFIO \ 121 TYPE_HOST_IOMMU_DEVICE_IOMMUFD "-vfio" 122 123 VFIOAddressSpace *vfio_get_address_space(AddressSpace *as); 124 void vfio_put_address_space(VFIOAddressSpace *space); 125 void vfio_address_space_insert(VFIOAddressSpace *space, 126 VFIOContainerBase *bcontainer); 127 128 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 129 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 130 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 131 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 132 int action, int fd, Error **errp); 133 134 void vfio_reset_handler(void *opaque); 135 struct vfio_device_info *vfio_get_device_info(int fd); 136 bool vfio_device_is_mdev(VFIODevice *vbasedev); 137 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); 138 bool vfio_attach_device(char *name, VFIODevice *vbasedev, 139 AddressSpace *as, Error **errp); 140 void vfio_detach_device(VFIODevice *vbasedev); 141 VFIODevice *vfio_get_vfio_device(Object *obj); 142 143 int vfio_kvm_device_add_fd(int fd, Error **errp); 144 int vfio_kvm_device_del_fd(int fd, Error **errp); 145 146 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 147 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 148 149 typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; 150 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 151 extern VFIOGroupList vfio_group_list; 152 extern VFIODeviceList vfio_device_list; 153 extern const MemoryListener vfio_memory_listener; 154 extern int vfio_kvm_device_fd; 155 156 #ifdef CONFIG_LINUX 157 int vfio_get_region_info(VFIODevice *vbasedev, int index, 158 struct vfio_region_info **info); 159 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 160 uint32_t subtype, struct vfio_region_info **info); 161 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 162 struct vfio_info_cap_header * 163 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 164 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 165 unsigned int *avail); 166 struct vfio_info_cap_header * 167 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 168 struct vfio_info_cap_header * 169 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 170 #endif 171 172 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 173 bool vfio_devices_all_dirty_tracking_started( 174 const VFIOContainerBase *bcontainer); 175 bool 176 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 177 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 178 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 179 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 180 uint64_t size, ram_addr_t ram_addr, Error **errp); 181 182 /* Returns 0 on success, or a negative errno. */ 183 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 184 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 185 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 186 DeviceState *dev, bool ram_discard); 187 int vfio_device_get_aw_bits(VFIODevice *vdev); 188 #endif /* HW_VFIO_VFIO_COMMON_H */ 189