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