1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved. 4 * Author: Alex Williamson <alex.williamson@redhat.com> 5 */ 6 #ifndef __VFIO_VFIO_H__ 7 #define __VFIO_VFIO_H__ 8 9 #include <linux/file.h> 10 #include <linux/device.h> 11 #include <linux/cdev.h> 12 #include <linux/module.h> 13 #include <linux/vfio.h> 14 15 struct iommufd_ctx; 16 struct iommu_group; 17 struct vfio_container; 18 19 struct vfio_device_file { 20 struct vfio_device *device; 21 struct vfio_group *group; 22 23 u8 access_granted; 24 u32 devid; /* only valid when iommufd is valid */ 25 spinlock_t kvm_ref_lock; /* protect kvm field */ 26 struct kvm *kvm; 27 struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ 28 }; 29 30 void vfio_device_put_registration(struct vfio_device *device); 31 bool vfio_device_try_get_registration(struct vfio_device *device); 32 int vfio_df_open(struct vfio_device_file *df); 33 void vfio_df_close(struct vfio_device_file *df); 34 struct vfio_device_file * 35 vfio_allocate_device_file(struct vfio_device *device); 36 37 extern const struct file_operations vfio_device_fops; 38 39 #ifdef CONFIG_VFIO_NOIOMMU 40 extern bool vfio_noiommu __read_mostly; 41 #else 42 enum { vfio_noiommu = false }; 43 #endif 44 45 enum vfio_group_type { 46 /* 47 * Physical device with IOMMU backing. 48 */ 49 VFIO_IOMMU, 50 51 /* 52 * Virtual device without IOMMU backing. The VFIO core fakes up an 53 * iommu_group as the iommu_group sysfs interface is part of the 54 * userspace ABI. The user of these devices must not be able to 55 * directly trigger unmediated DMA. 56 */ 57 VFIO_EMULATED_IOMMU, 58 59 /* 60 * Physical device without IOMMU backing. The VFIO core fakes up an 61 * iommu_group as the iommu_group sysfs interface is part of the 62 * userspace ABI. Users can trigger unmediated DMA by the device, 63 * usage is highly dangerous, requires an explicit opt-in and will 64 * taint the kernel. 65 */ 66 VFIO_NO_IOMMU, 67 }; 68 69 #if IS_ENABLED(CONFIG_VFIO_GROUP) 70 struct vfio_group { 71 struct device dev; 72 struct cdev cdev; 73 /* 74 * When drivers is non-zero a driver is attached to the struct device 75 * that provided the iommu_group and thus the iommu_group is a valid 76 * pointer. When drivers is 0 the driver is being detached. Once users 77 * reaches 0 then the iommu_group is invalid. 78 */ 79 refcount_t drivers; 80 unsigned int container_users; 81 struct iommu_group *iommu_group; 82 struct vfio_container *container; 83 struct list_head device_list; 84 struct mutex device_lock; 85 struct list_head vfio_next; 86 #if IS_ENABLED(CONFIG_VFIO_CONTAINER) 87 struct list_head container_next; 88 #endif 89 enum vfio_group_type type; 90 struct mutex group_lock; 91 struct kvm *kvm; 92 struct file *opened_file; 93 struct iommufd_ctx *iommufd; 94 spinlock_t kvm_ref_lock; 95 unsigned int cdev_device_open_cnt; 96 }; 97 98 int vfio_device_block_group(struct vfio_device *device); 99 void vfio_device_unblock_group(struct vfio_device *device); 100 int vfio_device_set_group(struct vfio_device *device, 101 enum vfio_group_type type); 102 void vfio_device_remove_group(struct vfio_device *device); 103 void vfio_device_group_register(struct vfio_device *device); 104 void vfio_device_group_unregister(struct vfio_device *device); 105 int vfio_device_group_use_iommu(struct vfio_device *device); 106 void vfio_device_group_unuse_iommu(struct vfio_device *device); 107 void vfio_df_group_close(struct vfio_device_file *df); 108 struct vfio_group *vfio_group_from_file(struct file *file); 109 bool vfio_group_enforced_coherent(struct vfio_group *group); 110 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); 111 bool vfio_device_has_container(struct vfio_device *device); 112 int __init vfio_group_init(void); 113 void vfio_group_cleanup(void); 114 115 static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) 116 { 117 return IS_ENABLED(CONFIG_VFIO_NOIOMMU) && 118 vdev->group->type == VFIO_NO_IOMMU; 119 } 120 #else 121 struct vfio_group; 122 123 static inline int vfio_device_block_group(struct vfio_device *device) 124 { 125 return 0; 126 } 127 128 static inline void vfio_device_unblock_group(struct vfio_device *device) 129 { 130 } 131 132 static inline int vfio_device_set_group(struct vfio_device *device, 133 enum vfio_group_type type) 134 { 135 return 0; 136 } 137 138 static inline void vfio_device_remove_group(struct vfio_device *device) 139 { 140 } 141 142 static inline void vfio_device_group_register(struct vfio_device *device) 143 { 144 } 145 146 static inline void vfio_device_group_unregister(struct vfio_device *device) 147 { 148 } 149 150 static inline int vfio_device_group_use_iommu(struct vfio_device *device) 151 { 152 return -EOPNOTSUPP; 153 } 154 155 static inline void vfio_device_group_unuse_iommu(struct vfio_device *device) 156 { 157 } 158 159 static inline void vfio_df_group_close(struct vfio_device_file *df) 160 { 161 } 162 163 static inline struct vfio_group *vfio_group_from_file(struct file *file) 164 { 165 return NULL; 166 } 167 168 static inline bool vfio_group_enforced_coherent(struct vfio_group *group) 169 { 170 return true; 171 } 172 173 static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) 174 { 175 } 176 177 static inline bool vfio_device_has_container(struct vfio_device *device) 178 { 179 return false; 180 } 181 182 static inline int __init vfio_group_init(void) 183 { 184 return 0; 185 } 186 187 static inline void vfio_group_cleanup(void) 188 { 189 } 190 191 static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) 192 { 193 return false; 194 } 195 #endif /* CONFIG_VFIO_GROUP */ 196 197 #if IS_ENABLED(CONFIG_VFIO_CONTAINER) 198 /** 199 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks 200 */ 201 struct vfio_iommu_driver_ops { 202 char *name; 203 struct module *owner; 204 void *(*open)(unsigned long arg); 205 void (*release)(void *iommu_data); 206 long (*ioctl)(void *iommu_data, unsigned int cmd, 207 unsigned long arg); 208 int (*attach_group)(void *iommu_data, 209 struct iommu_group *group, 210 enum vfio_group_type); 211 void (*detach_group)(void *iommu_data, 212 struct iommu_group *group); 213 int (*pin_pages)(void *iommu_data, 214 struct iommu_group *group, 215 dma_addr_t user_iova, 216 int npage, int prot, 217 struct page **pages); 218 void (*unpin_pages)(void *iommu_data, 219 dma_addr_t user_iova, int npage); 220 void (*register_device)(void *iommu_data, 221 struct vfio_device *vdev); 222 void (*unregister_device)(void *iommu_data, 223 struct vfio_device *vdev); 224 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova, 225 void *data, size_t count, bool write); 226 struct iommu_domain *(*group_iommu_domain)(void *iommu_data, 227 struct iommu_group *group); 228 }; 229 230 struct vfio_iommu_driver { 231 const struct vfio_iommu_driver_ops *ops; 232 struct list_head vfio_next; 233 }; 234 235 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); 236 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops); 237 238 struct vfio_container *vfio_container_from_file(struct file *filep); 239 int vfio_group_use_container(struct vfio_group *group); 240 void vfio_group_unuse_container(struct vfio_group *group); 241 int vfio_container_attach_group(struct vfio_container *container, 242 struct vfio_group *group); 243 void vfio_group_detach_container(struct vfio_group *group); 244 void vfio_device_container_register(struct vfio_device *device); 245 void vfio_device_container_unregister(struct vfio_device *device); 246 int vfio_device_container_pin_pages(struct vfio_device *device, 247 dma_addr_t iova, int npage, 248 int prot, struct page **pages); 249 void vfio_device_container_unpin_pages(struct vfio_device *device, 250 dma_addr_t iova, int npage); 251 int vfio_device_container_dma_rw(struct vfio_device *device, 252 dma_addr_t iova, void *data, 253 size_t len, bool write); 254 255 int __init vfio_container_init(void); 256 void vfio_container_cleanup(void); 257 #else 258 static inline struct vfio_container * 259 vfio_container_from_file(struct file *filep) 260 { 261 return NULL; 262 } 263 264 static inline int vfio_group_use_container(struct vfio_group *group) 265 { 266 return -EOPNOTSUPP; 267 } 268 269 static inline void vfio_group_unuse_container(struct vfio_group *group) 270 { 271 } 272 273 static inline int vfio_container_attach_group(struct vfio_container *container, 274 struct vfio_group *group) 275 { 276 return -EOPNOTSUPP; 277 } 278 279 static inline void vfio_group_detach_container(struct vfio_group *group) 280 { 281 } 282 283 static inline void vfio_device_container_register(struct vfio_device *device) 284 { 285 } 286 287 static inline void vfio_device_container_unregister(struct vfio_device *device) 288 { 289 } 290 291 static inline int vfio_device_container_pin_pages(struct vfio_device *device, 292 dma_addr_t iova, int npage, 293 int prot, struct page **pages) 294 { 295 return -EOPNOTSUPP; 296 } 297 298 static inline void vfio_device_container_unpin_pages(struct vfio_device *device, 299 dma_addr_t iova, int npage) 300 { 301 } 302 303 static inline int vfio_device_container_dma_rw(struct vfio_device *device, 304 dma_addr_t iova, void *data, 305 size_t len, bool write) 306 { 307 return -EOPNOTSUPP; 308 } 309 310 static inline int vfio_container_init(void) 311 { 312 return 0; 313 } 314 static inline void vfio_container_cleanup(void) 315 { 316 } 317 #endif 318 319 #if IS_ENABLED(CONFIG_IOMMUFD) 320 bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev, 321 struct iommufd_ctx *ictx); 322 int vfio_df_iommufd_bind(struct vfio_device_file *df); 323 void vfio_df_iommufd_unbind(struct vfio_device_file *df); 324 int vfio_iommufd_compat_attach_ioas(struct vfio_device *device, 325 struct iommufd_ctx *ictx); 326 #else 327 static inline bool 328 vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev, 329 struct iommufd_ctx *ictx) 330 { 331 return false; 332 } 333 334 static inline int vfio_df_iommufd_bind(struct vfio_device_file *fd) 335 { 336 return -EOPNOTSUPP; 337 } 338 339 static inline void vfio_df_iommufd_unbind(struct vfio_device_file *df) 340 { 341 } 342 343 static inline int 344 vfio_iommufd_compat_attach_ioas(struct vfio_device *device, 345 struct iommufd_ctx *ictx) 346 { 347 return -EOPNOTSUPP; 348 } 349 #endif 350 351 int vfio_df_ioctl_attach_pt(struct vfio_device_file *df, 352 struct vfio_device_attach_iommufd_pt __user *arg); 353 int vfio_df_ioctl_detach_pt(struct vfio_device_file *df, 354 struct vfio_device_detach_iommufd_pt __user *arg); 355 356 #if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) 357 void vfio_init_device_cdev(struct vfio_device *device); 358 359 static inline int vfio_device_add(struct vfio_device *device) 360 { 361 /* cdev does not support noiommu device */ 362 if (vfio_device_is_noiommu(device)) 363 return device_add(&device->device); 364 vfio_init_device_cdev(device); 365 return cdev_device_add(&device->cdev, &device->device); 366 } 367 368 static inline void vfio_device_del(struct vfio_device *device) 369 { 370 if (vfio_device_is_noiommu(device)) 371 device_del(&device->device); 372 else 373 cdev_device_del(&device->cdev, &device->device); 374 } 375 376 int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep); 377 long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, 378 struct vfio_device_bind_iommufd __user *arg); 379 void vfio_df_unbind_iommufd(struct vfio_device_file *df); 380 int vfio_cdev_init(struct class *device_class); 381 void vfio_cdev_cleanup(void); 382 #else 383 static inline void vfio_init_device_cdev(struct vfio_device *device) 384 { 385 } 386 387 static inline int vfio_device_add(struct vfio_device *device) 388 { 389 return device_add(&device->device); 390 } 391 392 static inline void vfio_device_del(struct vfio_device *device) 393 { 394 device_del(&device->device); 395 } 396 397 static inline int vfio_device_fops_cdev_open(struct inode *inode, 398 struct file *filep) 399 { 400 return 0; 401 } 402 403 static inline long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, 404 struct vfio_device_bind_iommufd __user *arg) 405 { 406 return -ENOTTY; 407 } 408 409 static inline void vfio_df_unbind_iommufd(struct vfio_device_file *df) 410 { 411 } 412 413 static inline int vfio_cdev_init(struct class *device_class) 414 { 415 return 0; 416 } 417 418 static inline void vfio_cdev_cleanup(void) 419 { 420 } 421 #endif /* CONFIG_VFIO_DEVICE_CDEV */ 422 423 #if IS_ENABLED(CONFIG_VFIO_VIRQFD) 424 int __init vfio_virqfd_init(void); 425 void vfio_virqfd_exit(void); 426 #else 427 static inline int __init vfio_virqfd_init(void) 428 { 429 return 0; 430 } 431 static inline void vfio_virqfd_exit(void) 432 { 433 } 434 #endif 435 436 #if IS_ENABLED(CONFIG_KVM) 437 void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm); 438 void vfio_device_put_kvm(struct vfio_device *device); 439 #else 440 static inline void vfio_device_get_kvm_safe(struct vfio_device *device, 441 struct kvm *kvm) 442 { 443 } 444 445 static inline void vfio_device_put_kvm(struct vfio_device *device) 446 { 447 } 448 #endif 449 450 #ifdef CONFIG_VFIO_DEBUGFS 451 void vfio_debugfs_create_root(void); 452 void vfio_debugfs_remove_root(void); 453 454 void vfio_device_debugfs_init(struct vfio_device *vdev); 455 void vfio_device_debugfs_exit(struct vfio_device *vdev); 456 #else 457 static inline void vfio_debugfs_create_root(void) { } 458 static inline void vfio_debugfs_remove_root(void) { } 459 460 static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { } 461 static inline void vfio_device_debugfs_exit(struct vfio_device *vdev) { } 462 #endif /* CONFIG_VFIO_DEBUGFS */ 463 464 #endif 465