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