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 enum vfio_group_type { 40 /* 41 * Physical device with IOMMU backing. 42 */ 43 VFIO_IOMMU, 44 45 /* 46 * Virtual device without IOMMU backing. The VFIO core fakes up an 47 * iommu_group as the iommu_group sysfs interface is part of the 48 * userspace ABI. The user of these devices must not be able to 49 * directly trigger unmediated DMA. 50 */ 51 VFIO_EMULATED_IOMMU, 52 53 /* 54 * Physical device without IOMMU backing. The VFIO core fakes up an 55 * iommu_group as the iommu_group sysfs interface is part of the 56 * userspace ABI. Users can trigger unmediated DMA by the device, 57 * usage is highly dangerous, requires an explicit opt-in and will 58 * taint the kernel. 59 */ 60 VFIO_NO_IOMMU, 61 }; 62 63 struct vfio_group { 64 struct device dev; 65 struct cdev cdev; 66 /* 67 * When drivers is non-zero a driver is attached to the struct device 68 * that provided the iommu_group and thus the iommu_group is a valid 69 * pointer. When drivers is 0 the driver is being detached. Once users 70 * reaches 0 then the iommu_group is invalid. 71 */ 72 refcount_t drivers; 73 unsigned int container_users; 74 struct iommu_group *iommu_group; 75 struct vfio_container *container; 76 struct list_head device_list; 77 struct mutex device_lock; 78 struct list_head vfio_next; 79 #if IS_ENABLED(CONFIG_VFIO_CONTAINER) 80 struct list_head container_next; 81 #endif 82 enum vfio_group_type type; 83 struct mutex group_lock; 84 struct kvm *kvm; 85 struct file *opened_file; 86 struct blocking_notifier_head notifier; 87 struct iommufd_ctx *iommufd; 88 spinlock_t kvm_ref_lock; 89 unsigned int cdev_device_open_cnt; 90 }; 91 92 int vfio_device_block_group(struct vfio_device *device); 93 void vfio_device_unblock_group(struct vfio_device *device); 94 int vfio_device_set_group(struct vfio_device *device, 95 enum vfio_group_type type); 96 void vfio_device_remove_group(struct vfio_device *device); 97 void vfio_device_group_register(struct vfio_device *device); 98 void vfio_device_group_unregister(struct vfio_device *device); 99 int vfio_device_group_use_iommu(struct vfio_device *device); 100 void vfio_device_group_unuse_iommu(struct vfio_device *device); 101 void vfio_df_group_close(struct vfio_device_file *df); 102 struct vfio_group *vfio_group_from_file(struct file *file); 103 bool vfio_group_enforced_coherent(struct vfio_group *group); 104 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); 105 bool vfio_device_has_container(struct vfio_device *device); 106 int __init vfio_group_init(void); 107 void vfio_group_cleanup(void); 108 109 static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) 110 { 111 return IS_ENABLED(CONFIG_VFIO_NOIOMMU) && 112 vdev->group->type == VFIO_NO_IOMMU; 113 } 114 115 #if IS_ENABLED(CONFIG_VFIO_CONTAINER) 116 /** 117 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks 118 */ 119 struct vfio_iommu_driver_ops { 120 char *name; 121 struct module *owner; 122 void *(*open)(unsigned long arg); 123 void (*release)(void *iommu_data); 124 long (*ioctl)(void *iommu_data, unsigned int cmd, 125 unsigned long arg); 126 int (*attach_group)(void *iommu_data, 127 struct iommu_group *group, 128 enum vfio_group_type); 129 void (*detach_group)(void *iommu_data, 130 struct iommu_group *group); 131 int (*pin_pages)(void *iommu_data, 132 struct iommu_group *group, 133 dma_addr_t user_iova, 134 int npage, int prot, 135 struct page **pages); 136 void (*unpin_pages)(void *iommu_data, 137 dma_addr_t user_iova, int npage); 138 void (*register_device)(void *iommu_data, 139 struct vfio_device *vdev); 140 void (*unregister_device)(void *iommu_data, 141 struct vfio_device *vdev); 142 int (*dma_rw)(void *iommu_data, dma_addr_t user_iova, 143 void *data, size_t count, bool write); 144 struct iommu_domain *(*group_iommu_domain)(void *iommu_data, 145 struct iommu_group *group); 146 }; 147 148 struct vfio_iommu_driver { 149 const struct vfio_iommu_driver_ops *ops; 150 struct list_head vfio_next; 151 }; 152 153 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); 154 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops); 155 156 struct vfio_container *vfio_container_from_file(struct file *filep); 157 int vfio_group_use_container(struct vfio_group *group); 158 void vfio_group_unuse_container(struct vfio_group *group); 159 int vfio_container_attach_group(struct vfio_container *container, 160 struct vfio_group *group); 161 void vfio_group_detach_container(struct vfio_group *group); 162 void vfio_device_container_register(struct vfio_device *device); 163 void vfio_device_container_unregister(struct vfio_device *device); 164 int vfio_device_container_pin_pages(struct vfio_device *device, 165 dma_addr_t iova, int npage, 166 int prot, struct page **pages); 167 void vfio_device_container_unpin_pages(struct vfio_device *device, 168 dma_addr_t iova, int npage); 169 int vfio_device_container_dma_rw(struct vfio_device *device, 170 dma_addr_t iova, void *data, 171 size_t len, bool write); 172 173 int __init vfio_container_init(void); 174 void vfio_container_cleanup(void); 175 #else 176 static inline struct vfio_container * 177 vfio_container_from_file(struct file *filep) 178 { 179 return NULL; 180 } 181 182 static inline int vfio_group_use_container(struct vfio_group *group) 183 { 184 return -EOPNOTSUPP; 185 } 186 187 static inline void vfio_group_unuse_container(struct vfio_group *group) 188 { 189 } 190 191 static inline int vfio_container_attach_group(struct vfio_container *container, 192 struct vfio_group *group) 193 { 194 return -EOPNOTSUPP; 195 } 196 197 static inline void vfio_group_detach_container(struct vfio_group *group) 198 { 199 } 200 201 static inline void vfio_device_container_register(struct vfio_device *device) 202 { 203 } 204 205 static inline void vfio_device_container_unregister(struct vfio_device *device) 206 { 207 } 208 209 static inline int vfio_device_container_pin_pages(struct vfio_device *device, 210 dma_addr_t iova, int npage, 211 int prot, struct page **pages) 212 { 213 return -EOPNOTSUPP; 214 } 215 216 static inline void vfio_device_container_unpin_pages(struct vfio_device *device, 217 dma_addr_t iova, int npage) 218 { 219 } 220 221 static inline int vfio_device_container_dma_rw(struct vfio_device *device, 222 dma_addr_t iova, void *data, 223 size_t len, bool write) 224 { 225 return -EOPNOTSUPP; 226 } 227 228 static inline int vfio_container_init(void) 229 { 230 return 0; 231 } 232 static inline void vfio_container_cleanup(void) 233 { 234 } 235 #endif 236 237 #if IS_ENABLED(CONFIG_IOMMUFD) 238 bool vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev, 239 struct iommufd_ctx *ictx); 240 int vfio_df_iommufd_bind(struct vfio_device_file *df); 241 void vfio_df_iommufd_unbind(struct vfio_device_file *df); 242 int vfio_iommufd_compat_attach_ioas(struct vfio_device *device, 243 struct iommufd_ctx *ictx); 244 #else 245 static inline bool 246 vfio_iommufd_device_has_compat_ioas(struct vfio_device *vdev, 247 struct iommufd_ctx *ictx) 248 { 249 return false; 250 } 251 252 static inline int vfio_df_iommufd_bind(struct vfio_device_file *fd) 253 { 254 return -EOPNOTSUPP; 255 } 256 257 static inline void vfio_df_iommufd_unbind(struct vfio_device_file *df) 258 { 259 } 260 261 static inline int 262 vfio_iommufd_compat_attach_ioas(struct vfio_device *device, 263 struct iommufd_ctx *ictx) 264 { 265 return -EOPNOTSUPP; 266 } 267 #endif 268 269 #if IS_ENABLED(CONFIG_VFIO_VIRQFD) 270 int __init vfio_virqfd_init(void); 271 void vfio_virqfd_exit(void); 272 #else 273 static inline int __init vfio_virqfd_init(void) 274 { 275 return 0; 276 } 277 static inline void vfio_virqfd_exit(void) 278 { 279 } 280 #endif 281 282 #ifdef CONFIG_VFIO_NOIOMMU 283 extern bool vfio_noiommu __read_mostly; 284 #else 285 enum { vfio_noiommu = false }; 286 #endif 287 288 #ifdef CONFIG_HAVE_KVM 289 void _vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm); 290 void vfio_device_put_kvm(struct vfio_device *device); 291 #else 292 static inline void _vfio_device_get_kvm_safe(struct vfio_device *device, 293 struct kvm *kvm) 294 { 295 } 296 297 static inline void vfio_device_put_kvm(struct vfio_device *device) 298 { 299 } 300 #endif 301 302 #endif 303