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 void vfio_disable_irqindex(VFIODevice *vbasedev, int index); 124 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index); 125 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index); 126 bool vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, 127 int action, int fd, Error **errp); 128 129 void vfio_reset_handler(void *opaque); 130 struct vfio_device_info *vfio_get_device_info(int fd); 131 bool vfio_device_is_mdev(VFIODevice *vbasedev); 132 bool vfio_device_hiod_realize(VFIODevice *vbasedev, Error **errp); 133 bool vfio_attach_device(char *name, VFIODevice *vbasedev, 134 AddressSpace *as, Error **errp); 135 void vfio_detach_device(VFIODevice *vbasedev); 136 VFIODevice *vfio_get_vfio_device(Object *obj); 137 138 int vfio_kvm_device_add_fd(int fd, Error **errp); 139 int vfio_kvm_device_del_fd(int fd, Error **errp); 140 141 bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp); 142 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer); 143 144 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 145 extern VFIODeviceList vfio_device_list; 146 extern const MemoryListener vfio_memory_listener; 147 extern int vfio_kvm_device_fd; 148 149 #ifdef CONFIG_LINUX 150 int vfio_get_region_info(VFIODevice *vbasedev, int index, 151 struct vfio_region_info **info); 152 int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, 153 uint32_t subtype, struct vfio_region_info **info); 154 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 155 struct vfio_info_cap_header * 156 vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); 157 bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, 158 unsigned int *avail); 159 struct vfio_info_cap_header * 160 vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id); 161 struct vfio_info_cap_header * 162 vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id); 163 #endif 164 165 int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size); 166 bool vfio_devices_all_dirty_tracking_started( 167 const VFIOContainerBase *bcontainer); 168 bool 169 vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer); 170 int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer, 171 VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp); 172 int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova, 173 uint64_t size, ram_addr_t ram_addr, Error **errp); 174 175 /* Returns 0 on success, or a negative errno. */ 176 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 177 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 178 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 179 DeviceState *dev, bool ram_discard); 180 int vfio_device_get_aw_bits(VFIODevice *vdev); 181 #endif /* HW_VFIO_VFIO_COMMON_H */ 182