at v6.17 14 kB view raw
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 <alex.williamson@redhat.com> 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 <linux/cdev.h> 17#include <uapi/linux/vfio.h> 18#include <linux/iova_bitmap.h> 19 20struct kvm; 21struct iommufd_ctx; 22struct iommufd_device; 23struct iommufd_access; 24 25/* 26 * VFIO devices can be placed in a set, this allows all devices to share this 27 * structure and the VFIO core will provide a lock that is held around 28 * open_device()/close_device() for all devices in the set. 29 */ 30struct vfio_device_set { 31 void *set_id; 32 struct mutex lock; 33 struct list_head device_list; 34 unsigned int device_count; 35}; 36 37struct vfio_device { 38 struct device *dev; 39 const struct vfio_device_ops *ops; 40 /* 41 * mig_ops/log_ops is a static property of the vfio_device which must 42 * be set prior to registering the vfio_device. 43 */ 44 const struct vfio_migration_ops *mig_ops; 45 const struct vfio_log_ops *log_ops; 46#if IS_ENABLED(CONFIG_VFIO_GROUP) 47 struct vfio_group *group; 48 struct list_head group_next; 49 struct list_head iommu_entry; 50#endif 51 struct vfio_device_set *dev_set; 52 struct list_head dev_set_list; 53 unsigned int migration_flags; 54 struct kvm *kvm; 55 56 /* Members below here are private, not for driver use */ 57 unsigned int index; 58 struct device device; /* device.kref covers object life circle */ 59#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) 60 struct cdev cdev; 61#endif 62 refcount_t refcount; /* user count on registered device*/ 63 unsigned int open_count; 64 struct completion comp; 65 struct iommufd_access *iommufd_access; 66 void (*put_kvm)(struct kvm *kvm); 67 struct inode *inode; 68#if IS_ENABLED(CONFIG_IOMMUFD) 69 struct iommufd_device *iommufd_device; 70 struct ida pasids; 71 u8 iommufd_attached:1; 72#endif 73 u8 cdev_opened:1; 74#ifdef CONFIG_DEBUG_FS 75 /* 76 * debug_root is a static property of the vfio_device 77 * which must be set prior to registering the vfio_device. 78 */ 79 struct dentry *debug_root; 80#endif 81}; 82 83/** 84 * struct vfio_device_ops - VFIO bus driver device callbacks 85 * 86 * @name: Name of the device driver. 87 * @init: initialize private fields in device structure 88 * @release: Reclaim private fields in device structure 89 * @bind_iommufd: Called when binding the device to an iommufd 90 * @unbind_iommufd: Opposite of bind_iommufd 91 * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the 92 * bound iommufd. Undo in unbind_iommufd if @detach_ioas is not 93 * called. 94 * @detach_ioas: Opposite of attach_ioas 95 * @pasid_attach_ioas: The pasid variation of attach_ioas 96 * @pasid_detach_ioas: Opposite of pasid_attach_ioas 97 * @open_device: Called when the first file descriptor is opened for this device 98 * @close_device: Opposite of open_device 99 * @read: Perform read(2) on device file descriptor 100 * @write: Perform write(2) on device file descriptor 101 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* 102 * operations documented below 103 * @mmap: Perform mmap(2) on a region of the device file descriptor 104 * @request: Request for the bus driver to release the device 105 * @match: Optional device name match callback (return: 0 for no-match, >0 for 106 * match, -errno for abort (ex. match with insufficient or incorrect 107 * additional args) 108 * @match_token_uuid: Optional device token match/validation. Return 0 109 * if the uuid is valid for the device, -errno otherwise. uuid is NULL 110 * if none was provided. 111 * @dma_unmap: Called when userspace unmaps IOVA from the container 112 * this device is attached to. 113 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl 114 */ 115struct vfio_device_ops { 116 char *name; 117 int (*init)(struct vfio_device *vdev); 118 void (*release)(struct vfio_device *vdev); 119 int (*bind_iommufd)(struct vfio_device *vdev, 120 struct iommufd_ctx *ictx, u32 *out_device_id); 121 void (*unbind_iommufd)(struct vfio_device *vdev); 122 int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); 123 void (*detach_ioas)(struct vfio_device *vdev); 124 int (*pasid_attach_ioas)(struct vfio_device *vdev, u32 pasid, 125 u32 *pt_id); 126 void (*pasid_detach_ioas)(struct vfio_device *vdev, u32 pasid); 127 int (*open_device)(struct vfio_device *vdev); 128 void (*close_device)(struct vfio_device *vdev); 129 ssize_t (*read)(struct vfio_device *vdev, char __user *buf, 130 size_t count, loff_t *ppos); 131 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, 132 size_t count, loff_t *size); 133 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, 134 unsigned long arg); 135 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma); 136 void (*request)(struct vfio_device *vdev, unsigned int count); 137 int (*match)(struct vfio_device *vdev, char *buf); 138 int (*match_token_uuid)(struct vfio_device *vdev, const uuid_t *uuid); 139 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length); 140 int (*device_feature)(struct vfio_device *device, u32 flags, 141 void __user *arg, size_t argsz); 142}; 143 144#if IS_ENABLED(CONFIG_IOMMUFD) 145struct iommufd_ctx *vfio_iommufd_device_ictx(struct vfio_device *vdev); 146int vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx); 147int vfio_iommufd_physical_bind(struct vfio_device *vdev, 148 struct iommufd_ctx *ictx, u32 *out_device_id); 149void vfio_iommufd_physical_unbind(struct vfio_device *vdev); 150int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); 151void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev); 152int vfio_iommufd_physical_pasid_attach_ioas(struct vfio_device *vdev, 153 u32 pasid, u32 *pt_id); 154void vfio_iommufd_physical_pasid_detach_ioas(struct vfio_device *vdev, 155 u32 pasid); 156int vfio_iommufd_emulated_bind(struct vfio_device *vdev, 157 struct iommufd_ctx *ictx, u32 *out_device_id); 158void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); 159int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); 160void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev); 161#else 162static inline struct iommufd_ctx * 163vfio_iommufd_device_ictx(struct vfio_device *vdev) 164{ 165 return NULL; 166} 167 168static inline int 169vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx) 170{ 171 return VFIO_PCI_DEVID_NOT_OWNED; 172} 173 174#define vfio_iommufd_physical_bind \ 175 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ 176 u32 *out_device_id)) NULL) 177#define vfio_iommufd_physical_unbind \ 178 ((void (*)(struct vfio_device *vdev)) NULL) 179#define vfio_iommufd_physical_attach_ioas \ 180 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) 181#define vfio_iommufd_physical_detach_ioas \ 182 ((void (*)(struct vfio_device *vdev)) NULL) 183#define vfio_iommufd_physical_pasid_attach_ioas \ 184 ((int (*)(struct vfio_device *vdev, u32 pasid, u32 *pt_id)) NULL) 185#define vfio_iommufd_physical_pasid_detach_ioas \ 186 ((void (*)(struct vfio_device *vdev, u32 pasid)) NULL) 187#define vfio_iommufd_emulated_bind \ 188 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ 189 u32 *out_device_id)) NULL) 190#define vfio_iommufd_emulated_unbind \ 191 ((void (*)(struct vfio_device *vdev)) NULL) 192#define vfio_iommufd_emulated_attach_ioas \ 193 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) 194#define vfio_iommufd_emulated_detach_ioas \ 195 ((void (*)(struct vfio_device *vdev)) NULL) 196#endif 197 198static inline bool vfio_device_cdev_opened(struct vfio_device *device) 199{ 200 return device->cdev_opened; 201} 202 203/** 204 * struct vfio_migration_ops - VFIO bus device driver migration callbacks 205 * 206 * @migration_set_state: Optional callback to change the migration state for 207 * devices that support migration. It's mandatory for 208 * VFIO_DEVICE_FEATURE_MIGRATION migration support. 209 * The returned FD is used for data transfer according to the FSM 210 * definition. The driver is responsible to ensure that FD reaches end 211 * of stream or error whenever the migration FSM leaves a data transfer 212 * state or before close_device() returns. 213 * @migration_get_state: Optional callback to get the migration state for 214 * devices that support migration. It's mandatory for 215 * VFIO_DEVICE_FEATURE_MIGRATION migration support. 216 * @migration_get_data_size: Optional callback to get the estimated data 217 * length that will be required to complete stop copy. It's mandatory for 218 * VFIO_DEVICE_FEATURE_MIGRATION migration support. 219 */ 220struct vfio_migration_ops { 221 struct file *(*migration_set_state)( 222 struct vfio_device *device, 223 enum vfio_device_mig_state new_state); 224 int (*migration_get_state)(struct vfio_device *device, 225 enum vfio_device_mig_state *curr_state); 226 int (*migration_get_data_size)(struct vfio_device *device, 227 unsigned long *stop_copy_length); 228}; 229 230/** 231 * struct vfio_log_ops - VFIO bus device driver logging callbacks 232 * 233 * @log_start: Optional callback to ask the device start DMA logging. 234 * @log_stop: Optional callback to ask the device stop DMA logging. 235 * @log_read_and_clear: Optional callback to ask the device read 236 * and clear the dirty DMAs in some given range. 237 * 238 * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set 239 * of features does not track logging state relative to the device, 240 * therefore the device implementation of vfio_log_ops must handle 241 * arbitrary user requests. This includes rejecting subsequent calls 242 * to log_start without an intervening log_stop, as well as graceful 243 * handling of log_stop and log_read_and_clear from invalid states. 244 */ 245struct vfio_log_ops { 246 int (*log_start)(struct vfio_device *device, 247 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size); 248 int (*log_stop)(struct vfio_device *device); 249 int (*log_read_and_clear)(struct vfio_device *device, 250 unsigned long iova, unsigned long length, 251 struct iova_bitmap *dirty); 252}; 253 254/** 255 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl 256 * @flags: Arg from the device_feature op 257 * @argsz: Arg from the device_feature op 258 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver 259 * supports 260 * @minsz: Minimum data size the driver accepts 261 * 262 * For use in a driver's device_feature op. Checks that the inputs to the 263 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if 264 * the driver should execute the get or set, otherwise the relevant 265 * value should be returned. 266 */ 267static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops, 268 size_t minsz) 269{ 270 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) & 271 ~supported_ops) 272 return -EINVAL; 273 if (flags & VFIO_DEVICE_FEATURE_PROBE) 274 return 0; 275 /* Without PROBE one of GET or SET must be requested */ 276 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET))) 277 return -EINVAL; 278 if (argsz < minsz) 279 return -EINVAL; 280 return 1; 281} 282 283struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, 284 const struct vfio_device_ops *ops); 285#define vfio_alloc_device(dev_struct, member, dev, ops) \ 286 container_of(_vfio_alloc_device(sizeof(struct dev_struct) + \ 287 BUILD_BUG_ON_ZERO(offsetof( \ 288 struct dev_struct, member)), \ 289 dev, ops), \ 290 struct dev_struct, member) 291 292static inline void vfio_put_device(struct vfio_device *device) 293{ 294 put_device(&device->device); 295} 296 297int vfio_register_group_dev(struct vfio_device *device); 298int vfio_register_emulated_iommu_dev(struct vfio_device *device); 299void vfio_unregister_group_dev(struct vfio_device *device); 300 301int vfio_assign_device_set(struct vfio_device *device, void *set_id); 302unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set); 303struct vfio_device * 304vfio_find_device_in_devset(struct vfio_device_set *dev_set, 305 struct device *dev); 306 307int vfio_mig_get_next_state(struct vfio_device *device, 308 enum vfio_device_mig_state cur_fsm, 309 enum vfio_device_mig_state new_fsm, 310 enum vfio_device_mig_state *next_fsm); 311 312void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes, 313 u32 req_nodes); 314 315/* 316 * External user API 317 */ 318struct iommu_group *vfio_file_iommu_group(struct file *file); 319 320#if IS_ENABLED(CONFIG_VFIO_GROUP) 321bool vfio_file_is_group(struct file *file); 322bool vfio_file_has_dev(struct file *file, struct vfio_device *device); 323#else 324static inline bool vfio_file_is_group(struct file *file) 325{ 326 return false; 327} 328 329static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *device) 330{ 331 return false; 332} 333#endif 334bool vfio_file_is_valid(struct file *file); 335bool vfio_file_enforced_coherent(struct file *file); 336void vfio_file_set_kvm(struct file *file, struct kvm *kvm); 337 338#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) 339 340int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, 341 int npage, int prot, struct page **pages); 342void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); 343int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, 344 void *data, size_t len, bool write); 345 346/* 347 * Sub-module helpers 348 */ 349struct vfio_info_cap { 350 struct vfio_info_cap_header *buf; 351 size_t size; 352}; 353struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, 354 size_t size, u16 id, 355 u16 version); 356void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); 357 358int vfio_info_add_capability(struct vfio_info_cap *caps, 359 struct vfio_info_cap_header *cap, size_t size); 360 361int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, 362 int num_irqs, int max_irq_type, 363 size_t *data_size); 364 365/* 366 * IRQfd - generic 367 */ 368struct virqfd { 369 void *opaque; 370 struct eventfd_ctx *eventfd; 371 int (*handler)(void *, void *); 372 void (*thread)(void *, void *); 373 void *data; 374 struct work_struct inject; 375 wait_queue_entry_t wait; 376 poll_table pt; 377 struct work_struct shutdown; 378 struct work_struct flush_inject; 379 struct virqfd **pvirqfd; 380}; 381 382int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *), 383 void (*thread)(void *, void *), void *data, 384 struct virqfd **pvirqfd, int fd); 385void vfio_virqfd_disable(struct virqfd **pvirqfd); 386void vfio_virqfd_flush_thread(struct virqfd **pvirqfd); 387 388#endif /* VFIO_H */