| 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_DEVICE_H |
| 22 | #define HW_VFIO_VFIO_DEVICE_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.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_UNUSED = 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 | typedef struct VFIOUserProxy VFIOUserProxy; |
| 51 | |
| 52 | typedef struct VFIODevice { |
| 53 | QLIST_ENTRY(VFIODevice) next; |
| 54 | QLIST_ENTRY(VFIODevice) container_next; |
| 55 | QLIST_ENTRY(VFIODevice) global_next; |
| 56 | struct VFIOGroup *group; |
| 57 | VFIOContainer *bcontainer; |
| 58 | char *sysfsdev; |
| 59 | char *name; |
| 60 | DeviceState *dev; |
| 61 | int fd; |
| 62 | int type; |
| 63 | bool mdev; |
| 64 | bool reset_works; |
| 65 | bool needs_reset; |
| 66 | bool no_mmap; |
| 67 | bool ram_block_discard_allowed; |
| 68 | OnOffAuto enable_migration; |
| 69 | OnOffAuto migration_multifd_transfer; |
| 70 | OnOffAuto migration_load_config_after_iter; |
| 71 | uint64_t migration_max_queued_buffers_size; |
| 72 | bool migration_events; |
| 73 | bool use_region_fds; |
| 74 | VFIODeviceOps *ops; |
| 75 | VFIODeviceIOOps *io_ops; |
| 76 | unsigned int num_irqs; |
| 77 | unsigned int num_initial_regions; |
| 78 | unsigned int flags; |
| 79 | VFIOMigration *migration; |
| 80 | Error *migration_blocker; |
| 81 | OnOffAuto pre_copy_dirty_page_tracking; |
| 82 | OnOffAuto device_dirty_page_tracking; |
| 83 | bool dirty_pages_supported; |
| 84 | bool dirty_tracking; /* Protected by BQL */ |
| 85 | bool iommu_dirty_tracking; |
| 86 | HostIOMMUDevice *hiod; |
| 87 | int devid; |
| 88 | IOMMUFDBackend *iommufd; |
| 89 | VFIOIOASHwpt *hwpt; |
| 90 | QLIST_ENTRY(VFIODevice) hwpt_next; |
| 91 | struct vfio_region_info **reginfo; |
| 92 | int *region_fds; |
| 93 | VFIODeviceCPR cpr; |
| 94 | VFIOUserProxy *proxy; |
| 95 | } VFIODevice; |
| 96 | |
| 97 | struct VFIODeviceOps { |
| 98 | void (*vfio_compute_needs_reset)(VFIODevice *vdev); |
| 99 | int (*vfio_hot_reset_multi)(VFIODevice *vdev); |
| 100 | void (*vfio_eoi)(VFIODevice *vdev); |
| 101 | Object *(*vfio_get_object)(VFIODevice *vdev); |
| 102 | |
| 103 | /** |
| 104 | * @vfio_save_config |
| 105 | * |
| 106 | * Save device config state |
| 107 | * |
| 108 | * @vdev: #VFIODevice for which to save the config |
| 109 | * @f: #QEMUFile where to send the data |
| 110 | * @errp: pointer to Error*, to store an error if it happens. |
| 111 | * |
| 112 | * Returns zero to indicate success and negative for error |
| 113 | */ |
| 114 | int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp); |
| 115 | |
| 116 | /** |
| 117 | * @vfio_load_config |
| 118 | * |
| 119 | * Load device config state |
| 120 | * |
| 121 | * @vdev: #VFIODevice for which to load the config |
| 122 | * @f: #QEMUFile where to get the data |
| 123 | * |
| 124 | * Returns zero to indicate success and negative for error |
| 125 | */ |
| 126 | int (*vfio_load_config)(VFIODevice *vdev, QEMUFile *f); |
| 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * Given a return value of either a short number of bytes read or -errno, |
| 131 | * construct a meaningful error message. |
| 132 | */ |
| 133 | #define strreaderror(ret) \ |
| 134 | (ret < 0 ? strerror(-ret) : "short read") |
| 135 | |
| 136 | /* |
| 137 | * Given a return value of either a short number of bytes written or -errno, |
| 138 | * construct a meaningful error message. |
| 139 | */ |
| 140 | #define strwriteerror(ret) \ |
| 141 | (ret < 0 ? strerror(-ret) : "short write") |
| 142 | |
| 143 | void vfio_device_irq_disable(VFIODevice *vbasedev, int index); |
| 144 | void vfio_device_irq_unmask(VFIODevice *vbasedev, int index); |
| 145 | void vfio_device_irq_mask(VFIODevice *vbasedev, int index); |
| 146 | bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex, |
| 147 | int action, int fd, Error **errp); |
| 148 | |
| 149 | void vfio_device_reset_handler(void *opaque); |
| 150 | bool vfio_device_is_mdev(VFIODevice *vbasedev); |
| 151 | /** |
| 152 | * vfio_device_dirty_pages_disabled: Check if device dirty tracking will be |
| 153 | * used for a VFIO device |
| 154 | * |
| 155 | * @vbasedev: The VFIODevice to transform |
| 156 | * |
| 157 | * Return: true if either @vbasedev doesn't support device dirty tracking or |
| 158 | * is forcedly disabled from command line, otherwise false. |
| 159 | */ |
| 160 | bool vfio_device_dirty_pages_disabled(VFIODevice *vbasedev); |
| 161 | bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, |
| 162 | const char *typename, Error **errp); |
| 163 | bool vfio_device_attach(char *name, VFIODevice *vbasedev, |
| 164 | AddressSpace *as, Error **errp); |
| 165 | bool vfio_device_attach_by_iommu_type(const char *iommu_type, char *name, |
| 166 | VFIODevice *vbasedev, AddressSpace *as, |
| 167 | Error **errp); |
| 168 | void vfio_device_detach(VFIODevice *vbasedev); |
| 169 | VFIODevice *vfio_get_vfio_device(Object *obj); |
| 170 | |
| 171 | typedef QLIST_HEAD(VFIODeviceList, VFIODevice) VFIODeviceList; |
| 172 | extern VFIODeviceList vfio_device_list; |
| 173 | |
| 174 | #ifdef CONFIG_LINUX |
| 175 | /* |
| 176 | * VFIO backend I/O operation capabilities |
| 177 | */ |
| 178 | #define VFIO_IO_CAP_DMA_BUF (1ULL << 0) |
| 179 | |
| 180 | /* |
| 181 | * How devices communicate with the server. The default option is through |
| 182 | * ioctl() to the kernel VFIO driver, but vfio-user can use a socket to a remote |
| 183 | * process. |
| 184 | */ |
| 185 | struct VFIODeviceIOOps { |
| 186 | /** |
| 187 | * @capabilities |
| 188 | * |
| 189 | * Bitmask of VFIO_IO_CAP_* flags indicating which features this |
| 190 | * backend supports. |
| 191 | */ |
| 192 | uint64_t capabilities; |
| 193 | |
| 194 | /** |
| 195 | * @device_feature |
| 196 | * |
| 197 | * Fill in feature info for the given device. |
| 198 | * |
| 199 | * @vdev: #VFIODevice to use |
| 200 | * @feat: feature information to fill in |
| 201 | * |
| 202 | * Returns 0 on success or -errno. |
| 203 | */ |
| 204 | int (*device_feature)(VFIODevice *vdev, struct vfio_device_feature *feat); |
| 205 | |
| 206 | /** |
| 207 | * @get_region_info |
| 208 | * |
| 209 | * Get the information for a given region on the device. |
| 210 | * |
| 211 | * @vdev: #VFIODevice to use |
| 212 | * @info: set @info->index to the region index to look up; the rest of the |
| 213 | * struct will be filled in on success |
| 214 | * @fd: pointer to the fd for the region; will be -1 if not found |
| 215 | * |
| 216 | * Returns 0 on success or -errno. |
| 217 | */ |
| 218 | int (*get_region_info)(VFIODevice *vdev, |
| 219 | struct vfio_region_info *info, int *fd); |
| 220 | |
| 221 | /** |
| 222 | * @get_irq_info |
| 223 | * |
| 224 | * @vdev: #VFIODevice to use |
| 225 | * @irq: set @irq->index to the IRQ index to look up; the rest of the struct |
| 226 | * will be filled in on success |
| 227 | * |
| 228 | * Returns 0 on success or -errno. |
| 229 | */ |
| 230 | int (*get_irq_info)(VFIODevice *vdev, struct vfio_irq_info *irq); |
| 231 | |
| 232 | /** |
| 233 | * @set_irqs |
| 234 | * |
| 235 | * Configure IRQs. |
| 236 | * |
| 237 | * @vdev: #VFIODevice to use |
| 238 | * @irqs: IRQ configuration as defined by VFIO docs. |
| 239 | * |
| 240 | * Returns 0 on success or -errno. |
| 241 | */ |
| 242 | int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs); |
| 243 | |
| 244 | /** |
| 245 | * @region_read |
| 246 | * |
| 247 | * Read part of a region. |
| 248 | * |
| 249 | * @vdev: #VFIODevice to use |
| 250 | * @nr: region index |
| 251 | * @off: offset within the region |
| 252 | * @size: size in bytes to read |
| 253 | * @data: buffer to read into |
| 254 | * |
| 255 | * Returns number of bytes read on success or -errno. |
| 256 | */ |
| 257 | int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size, |
| 258 | void *data); |
| 259 | |
| 260 | /** |
| 261 | * @region_write |
| 262 | * |
| 263 | * Write part of a region. |
| 264 | * |
| 265 | * @vdev: #VFIODevice to use |
| 266 | * @nr: region index |
| 267 | * @off: offset within the region |
| 268 | * @size: size in bytes to write |
| 269 | * @data: buffer to write from |
| 270 | * @post: true if this is a posted write |
| 271 | * |
| 272 | * Returns number of bytes write on success or -errno. |
| 273 | */ |
| 274 | int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size, |
| 275 | void *data, bool post); |
| 276 | }; |
| 277 | |
| 278 | void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainer *bcontainer, |
| 279 | struct vfio_device_info *info); |
| 280 | |
| 281 | void vfio_device_unprepare(VFIODevice *vbasedev); |
| 282 | |
| 283 | bool vfio_device_get_viommu_flags_want_nesting(VFIODevice *vbasedev); |
| 284 | bool vfio_device_get_viommu_flags_want_nesting_dirty(VFIODevice *vbasedev); |
| 285 | bool vfio_device_get_viommu_flags_want_pasid_attach(VFIODevice *vbasedev); |
| 286 | bool vfio_device_get_host_iommu_quirk_bypass_ro(VFIODevice *vbasedev, |
| 287 | uint32_t type, void *caps, |
| 288 | uint32_t size); |
| 289 | |
| 290 | int vfio_device_get_feature(VFIODevice *vbasedev, |
| 291 | struct vfio_device_feature *feature); |
| 292 | |
| 293 | /** |
| 294 | * Return the region info for a given region index. The region info includes |
| 295 | * details such as size, offset, and capabilities. Note that the returned |
| 296 | * info pointer is either a cached copy or newly allocated by |
| 297 | * vfio_device_get_region_info(), so the caller is not expected to allocate |
| 298 | * or free it. |
| 299 | * |
| 300 | * @vbasedev: #VFIODevice to use |
| 301 | * @index: region index |
| 302 | * @info: pointer to store the region info |
| 303 | * |
| 304 | * Returns 0 on success or a negative value on error. |
| 305 | */ |
| 306 | int vfio_device_get_region_info(VFIODevice *vbasedev, int index, |
| 307 | struct vfio_region_info **info); |
| 308 | int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, |
| 309 | uint32_t subtype, struct vfio_region_info **info); |
| 310 | |
| 311 | /** |
| 312 | * Return the fd for mapping this region. This is either the device's fd (for |
| 313 | * e.g. kernel vfio), or a per-region fd (for vfio-user). |
| 314 | * |
| 315 | * @vbasedev: #VFIODevice to use |
| 316 | * @index: region index |
| 317 | * |
| 318 | * Returns the fd. |
| 319 | */ |
| 320 | int vfio_device_get_region_fd(VFIODevice *vbasedev, int index); |
| 321 | |
| 322 | bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); |
| 323 | |
| 324 | int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, |
| 325 | struct vfio_irq_info *info); |
| 326 | #endif |
| 327 | |
| 328 | /* Returns 0 on success, or a negative errno. */ |
| 329 | bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); |
| 330 | void vfio_device_free_name(VFIODevice *vbasedev); |
| 331 | void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp); |
| 332 | void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, |
| 333 | DeviceState *dev, bool ram_discard); |
| 334 | int vfio_device_get_aw_bits(VFIODevice *vdev); |
| 335 | |
| 336 | void vfio_kvm_device_close(void); |
| 337 | #endif /* HW_VFIO_VFIO_DEVICE_H */ |