1 /* 2 * VFIO Device interface 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 VFIODeviceIOOps VFIODeviceIOOps; 45 typedef struct VFIOMigration VFIOMigration; 46 47 typedef struct IOMMUFDBackend IOMMUFDBackend; 48 typedef struct VFIOIOASHwpt VFIOIOASHwpt; 49 50 typedef struct VFIODevice { 51 QLIST_ENTRY(VFIODevice) next; 52 QLIST_ENTRY(VFIODevice) container_next; 53 QLIST_ENTRY(VFIODevice) global_next; 54 struct VFIOGroup *group; 55 VFIOContainerBase *bcontainer; 56 char *sysfsdev; 57 char *name; 58 DeviceState *dev; 59 int fd; 60 int type; 61 bool mdev; 62 bool reset_works; 63 bool needs_reset; 64 bool no_mmap; 65 bool ram_block_discard_allowed; 66 OnOffAuto enable_migration; 67 OnOffAuto migration_multifd_transfer; 68 bool migration_events; 69 VFIODeviceOps *ops; 70 VFIODeviceIOOps *io_ops; 71 unsigned int num_irqs; 72 unsigned int num_regions; 73 unsigned int flags; 74 VFIOMigration *migration; 75 Error *migration_blocker; 76 OnOffAuto pre_copy_dirty_page_tracking; 77 OnOffAuto device_dirty_page_tracking; 78 bool dirty_pages_supported; 79 bool dirty_tracking; /* Protected by BQL */ 80 bool iommu_dirty_tracking; 81 HostIOMMUDevice *hiod; 82 int devid; 83 IOMMUFDBackend *iommufd; 84 VFIOIOASHwpt *hwpt; 85 QLIST_ENTRY(VFIODevice) hwpt_next; 86 struct vfio_region_info **reginfo; 87 } VFIODevice; 88 89 struct VFIODeviceOps { 90 void (*vfio_compute_needs_reset)(VFIODevice *vdev); 91 int (*vfio_hot_reset_multi)(VFIODevice *vdev); 92 void (*vfio_eoi)(VFIODevice *vdev); 93 Object *(*vfio_get_object)(VFIODevice *vdev); 94 95 /** 96 * @vfio_save_config 97 * 98 * Save device config state 99 * 100 * @vdev: #VFIODevice for which to save the config 101 * @f: #QEMUFile where to send the data 102 * @errp: pointer to Error*, to store an error if it happens. 103 * 104 * Returns zero to indicate success and negative for error 105 */ 106 int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); 107 108 /** 109 * @vfio_load_config 110 * 111 * Load device config state 112 * 113 * @vdev: #VFIODevice for which to load the config 114 * @f: #QEMUFile where to get the data 115 * 116 * Returns zero to indicate success and negative for error 117 */ 118 int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); 119 }; 120 121 /* 122 * Given a return value of either a short number of bytes read or -errno, 123 * construct a meaningful error message. 124 */ 125 #define strreaderror(ret) \ 126 (ret < 0 ? strerror(-ret) : "short read") 127 128 /* 129 * Given a return value of either a short number of bytes written or -errno, 130 * construct a meaningful error message. 131 */ 132 #define strwriteerror(ret) \ 133 (ret < 0 ? strerror(-ret) : "short write") 134 135 void vfio_device_irq_disable(VFIODevice *vbasedev, int index); 136 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index); 137 void vfio_device_irq_mask(VFIODevice *vbasedev, int index); 138 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex, 139 int action, int fd, Error **errp); 140 141 void vfio_device_reset_handler(void *opaque); 142 bool vfio_device_is_mdev(VFIODevice *vbasedev); 143 bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, 144 const char *typename, Error **errp); 145 bool vfio_device_attach(char *name, VFIODevice *vbasedev, 146 AddressSpace *as, Error **errp); 147 bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name, 148 VFIODevice *vbasedev, AddressSpace *as, 149 Error **errp); 150 void vfio_device_detach(VFIODevice *vbasedev); 151 VFIODevice *vfio_get_vfio_device(Object *obj); 152 153 typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; 154 extern VFIODeviceList vfio_device_list; 155 156 #ifdef CONFIG_LINUX 157 /* 158 * How devices communicate with the server. The default option is through 159 * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote 160 * process. 161 */ 162 struct VFIODeviceIOOps { 163 /** 164 * @device_feature 165 * 166 * Fill in feature info for the given device. 167 */ 168 int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *); 169 170 /** 171 * @get_region_info 172 * 173 * Fill in @info with information on the region given by @info->index. 174 */ 175 int (*get_region_info)(VFIODevice *vdev, 176 struct vfio_region_info *info); 177 178 /** 179 * @get_irq_info 180 * 181 * Fill in @irq with information on the IRQ given by @info->index. 182 */ 183 int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq); 184 185 /** 186 * @set_irqs 187 * 188 * Configure IRQs as defined by @irqs. 189 */ 190 int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs); 191 192 /** 193 * @region_read 194 * 195 * Read @size bytes from the region @nr at offset @off into the buffer 196 * @data. 197 */ 198 int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size, 199 void *data); 200 201 /** 202 * @region_write 203 * 204 * Write @size bytes to the region @nr at offset @off from the buffer 205 * @data. 206 */ 207 int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size, 208 void *data); 209 }; 210 211 void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, 212 struct vfio_device_info *info); 213 214 void vfio_device_unprepare(VFIODevice *vbasedev); 215 216 int vfio_device_get_region_info(VFIODevice *vbasedev, int index, 217 struct vfio_region_info **info); 218 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, 219 uint32_t subtype, struct vfio_region_info **info); 220 bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); 221 222 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, 223 struct vfio_irq_info *info); 224 #endif 225 226 /* Returns 0 on success, or a negative errno. */ 227 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); 228 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); 229 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 230 DeviceState *dev, bool ram_discard); 231 int vfio_device_get_aw_bits(VFIODevice *vdev); 232 #endif /* HW_VFIO_VFIO_COMMON_H */ 233