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