1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * VFIO API definition 4 * 5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 6 * Author: Alex Williamson <[email protected]> 7 */ 8 #ifndef VFIO_H 9 #define VFIO_H 10 11 12 #include <linux/iommu.h> 13 #include <linux/mm.h> 14 #include <linux/workqueue.h> 15 #include <linux/poll.h> 16 #include <uapi/linux/vfio.h> 17 #include <linux/iova_bitmap.h> 18 19 struct kvm; 20 struct iommufd_ctx; 21 struct iommufd_device; 22 struct iommufd_access; 23 24 /* 25 * VFIO devices can be placed in a set, this allows all devices to share this 26 * structure and the VFIO core will provide a lock that is held around 27 * open_device()/close_device() for all devices in the set. 28 */ 29 struct vfio_device_set { 30 void *set_id; 31 struct mutex lock; 32 struct list_head device_list; 33 unsigned int device_count; 34 }; 35 36 struct vfio_device { 37 struct device *dev; 38 const struct vfio_device_ops *ops; 39 /* 40 * mig_ops/log_ops is a static property of the vfio_device which must 41 * be set prior to registering the vfio_device. 42 */ 43 const struct vfio_migration_ops *mig_ops; 44 const struct vfio_log_ops *log_ops; 45 struct vfio_group *group; 46 struct vfio_device_set *dev_set; 47 struct list_head dev_set_list; 48 unsigned int migration_flags; 49 /* Driver must reference the kvm during open_device or never touch it */ 50 struct kvm *kvm; 51 52 /* Members below here are private, not for driver use */ 53 unsigned int index; 54 struct device device; /* device.kref covers object life circle */ 55 refcount_t refcount; /* user count on registered device*/ 56 unsigned int open_count; 57 struct completion comp; 58 struct list_head group_next; 59 struct list_head iommu_entry; 60 struct iommufd_access *iommufd_access; 61 #if IS_ENABLED(CONFIG_IOMMUFD) 62 struct iommufd_device *iommufd_device; 63 struct iommufd_ctx *iommufd_ictx; 64 bool iommufd_attached; 65 #endif 66 }; 67 68 /** 69 * struct vfio_device_ops - VFIO bus driver device callbacks 70 * 71 * @init: initialize private fields in device structure 72 * @release: Reclaim private fields in device structure 73 * @open_device: Called when the first file descriptor is opened for this device 74 * @close_device: Opposite of open_device 75 * @read: Perform read(2) on device file descriptor 76 * @write: Perform write(2) on device file descriptor 77 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* 78 * operations documented below 79 * @mmap: Perform mmap(2) on a region of the device file descriptor 80 * @request: Request for the bus driver to release the device 81 * @match: Optional device name match callback (return: 0 for no-match, >0 for 82 * match, -errno for abort (ex. match with insufficient or incorrect 83 * additional args) 84 * @dma_unmap: Called when userspace unmaps IOVA from the container 85 * this device is attached to. 86 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl 87 */ 88 struct vfio_device_ops { 89 char *name; 90 int (*init)(struct vfio_device *vdev); 91 void (*release)(struct vfio_device *vdev); 92 int (*bind_iommufd)(struct vfio_device *vdev, 93 struct iommufd_ctx *ictx, u32 *out_device_id); 94 void (*unbind_iommufd)(struct vfio_device *vdev); 95 int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); 96 int (*open_device)(struct vfio_device *vdev); 97 void (*close_device)(struct vfio_device *vdev); 98 ssize_t (*read)(struct vfio_device *vdev, char __user *buf, 99 size_t count, loff_t *ppos); 100 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, 101 size_t count, loff_t *size); 102 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, 103 unsigned long arg); 104 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma); 105 void (*request)(struct vfio_device *vdev, unsigned int count); 106 int (*match)(struct vfio_device *vdev, char *buf); 107 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length); 108 int (*device_feature)(struct vfio_device *device, u32 flags, 109 void __user *arg, size_t argsz); 110 }; 111 112 #if IS_ENABLED(CONFIG_IOMMUFD) 113 int vfio_iommufd_physical_bind(struct vfio_device *vdev, 114 struct iommufd_ctx *ictx, u32 *out_device_id); 115 void vfio_iommufd_physical_unbind(struct vfio_device *vdev); 116 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); 117 int vfio_iommufd_emulated_bind(struct vfio_device *vdev, 118 struct iommufd_ctx *ictx, u32 *out_device_id); 119 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); 120 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); 121 #else 122 #define vfio_iommufd_physical_bind \ 123 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ 124 u32 *out_device_id)) NULL) 125 #define vfio_iommufd_physical_unbind \ 126 ((void (*)(struct vfio_device *vdev)) NULL) 127 #define vfio_iommufd_physical_attach_ioas \ 128 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) 129 #define vfio_iommufd_emulated_bind \ 130 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ 131 u32 *out_device_id)) NULL) 132 #define vfio_iommufd_emulated_unbind \ 133 ((void (*)(struct vfio_device *vdev)) NULL) 134 #define vfio_iommufd_emulated_attach_ioas \ 135 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) 136 #endif 137 138 /** 139 * @migration_set_state: Optional callback to change the migration state for 140 * devices that support migration. It's mandatory for 141 * VFIO_DEVICE_FEATURE_MIGRATION migration support. 142 * The returned FD is used for data transfer according to the FSM 143 * definition. The driver is responsible to ensure that FD reaches end 144 * of stream or error whenever the migration FSM leaves a data transfer 145 * state or before close_device() returns. 146 * @migration_get_state: Optional callback to get the migration state for 147 * devices that support migration. It's mandatory for 148 * VFIO_DEVICE_FEATURE_MIGRATION migration support. 149 */ 150 struct vfio_migration_ops { 151 struct file *(*migration_set_state)( 152 struct vfio_device *device, 153 enum vfio_device_mig_state new_state); 154 int (*migration_get_state)(struct vfio_device *device, 155 enum vfio_device_mig_state *curr_state); 156 }; 157 158 /** 159 * @log_start: Optional callback to ask the device start DMA logging. 160 * @log_stop: Optional callback to ask the device stop DMA logging. 161 * @log_read_and_clear: Optional callback to ask the device read 162 * and clear the dirty DMAs in some given range. 163 * 164 * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set 165 * of features does not track logging state relative to the device, 166 * therefore the device implementation of vfio_log_ops must handle 167 * arbitrary user requests. This includes rejecting subsequent calls 168 * to log_start without an intervening log_stop, as well as graceful 169 * handling of log_stop and log_read_and_clear from invalid states. 170 */ 171 struct vfio_log_ops { 172 int (*log_start)(struct vfio_device *device, 173 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size); 174 int (*log_stop)(struct vfio_device *device); 175 int (*log_read_and_clear)(struct vfio_device *device, 176 unsigned long iova, unsigned long length, 177 struct iova_bitmap *dirty); 178 }; 179 180 /** 181 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl 182 * @flags: Arg from the device_feature op 183 * @argsz: Arg from the device_feature op 184 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver 185 * supports 186 * @minsz: Minimum data size the driver accepts 187 * 188 * For use in a driver's device_feature op. Checks that the inputs to the 189 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if 190 * the driver should execute the get or set, otherwise the relevant 191 * value should be returned. 192 */ 193 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops, 194 size_t minsz) 195 { 196 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) & 197 ~supported_ops) 198 return -EINVAL; 199 if (flags & VFIO_DEVICE_FEATURE_PROBE) 200 return 0; 201 /* Without PROBE one of GET or SET must be requested */ 202 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET))) 203 return -EINVAL; 204 if (argsz < minsz) 205 return -EINVAL; 206 return 1; 207 } 208 209 struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, 210 const struct vfio_device_ops *ops); 211 #define vfio_alloc_device(dev_struct, member, dev, ops) \ 212 container_of(_vfio_alloc_device(sizeof(struct dev_struct) + \ 213 BUILD_BUG_ON_ZERO(offsetof( \ 214 struct dev_struct, member)), \ 215 dev, ops), \ 216 struct dev_struct, member) 217 218 int vfio_init_device(struct vfio_device *device, struct device *dev, 219 const struct vfio_device_ops *ops); 220 void vfio_free_device(struct vfio_device *device); 221 static inline void vfio_put_device(struct vfio_device *device) 222 { 223 put_device(&device->device); 224 } 225 226 int vfio_register_group_dev(struct vfio_device *device); 227 int vfio_register_emulated_iommu_dev(struct vfio_device *device); 228 void vfio_unregister_group_dev(struct vfio_device *device); 229 230 int vfio_assign_device_set(struct vfio_device *device, void *set_id); 231 unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set); 232 233 int vfio_mig_get_next_state(struct vfio_device *device, 234 enum vfio_device_mig_state cur_fsm, 235 enum vfio_device_mig_state new_fsm, 236 enum vfio_device_mig_state *next_fsm); 237 238 /* 239 * External user API 240 */ 241 struct iommu_group *vfio_file_iommu_group(struct file *file); 242 bool vfio_file_is_group(struct file *file); 243 bool vfio_file_enforced_coherent(struct file *file); 244 void vfio_file_set_kvm(struct file *file, struct kvm *kvm); 245 bool vfio_file_has_dev(struct file *file, struct vfio_device *device); 246 247 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) 248 249 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, 250 int npage, int prot, struct page **pages); 251 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); 252 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, 253 void *data, size_t len, bool write); 254 255 /* 256 * Sub-module helpers 257 */ 258 struct vfio_info_cap { 259 struct vfio_info_cap_header *buf; 260 size_t size; 261 }; 262 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, 263 size_t size, u16 id, 264 u16 version); 265 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); 266 267 int vfio_info_add_capability(struct vfio_info_cap *caps, 268 struct vfio_info_cap_header *cap, size_t size); 269 270 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, 271 int num_irqs, int max_irq_type, 272 size_t *data_size); 273 274 struct pci_dev; 275 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH) 276 void vfio_spapr_pci_eeh_open(struct pci_dev *pdev); 277 void vfio_spapr_pci_eeh_release(struct pci_dev *pdev); 278 long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd, 279 unsigned long arg); 280 #else 281 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev) 282 { 283 } 284 285 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev) 286 { 287 } 288 289 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, 290 unsigned int cmd, 291 unsigned long arg) 292 { 293 return -ENOTTY; 294 } 295 #endif /* CONFIG_VFIO_SPAPR_EEH */ 296 297 /* 298 * IRQfd - generic 299 */ 300 struct virqfd { 301 void *opaque; 302 struct eventfd_ctx *eventfd; 303 int (*handler)(void *, void *); 304 void (*thread)(void *, void *); 305 void *data; 306 struct work_struct inject; 307 wait_queue_entry_t wait; 308 poll_table pt; 309 struct work_struct shutdown; 310 struct virqfd **pvirqfd; 311 }; 312 313 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *), 314 void (*thread)(void *, void *), void *data, 315 struct virqfd **pvirqfd, int fd); 316 void vfio_virqfd_disable(struct virqfd **pvirqfd); 317 318 #endif /* VFIO_H */ 319