1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES 3 */ 4 #ifndef __IOMMUFD_PRIVATE_H 5 #define __IOMMUFD_PRIVATE_H 6 7 #include <linux/rwsem.h> 8 #include <linux/xarray.h> 9 #include <linux/refcount.h> 10 #include <linux/uaccess.h> 11 12 struct iommu_domain; 13 struct iommu_group; 14 struct iommu_option; 15 struct iommufd_device; 16 17 struct iommufd_ctx { 18 struct file *file; 19 struct xarray objects; 20 struct xarray groups; 21 22 u8 account_mode; 23 /* Compatibility with VFIO no iommu */ 24 u8 no_iommu_mode; 25 struct iommufd_ioas *vfio_ioas; 26 }; 27 28 /* 29 * The IOVA to PFN map. The map automatically copies the PFNs into multiple 30 * domains and permits sharing of PFNs between io_pagetable instances. This 31 * supports both a design where IOAS's are 1:1 with a domain (eg because the 32 * domain is HW customized), or where the IOAS is 1:N with multiple generic 33 * domains. The io_pagetable holds an interval tree of iopt_areas which point 34 * to shared iopt_pages which hold the pfns mapped to the page table. 35 * 36 * The locking order is domains_rwsem -> iova_rwsem -> pages::mutex 37 */ 38 struct io_pagetable { 39 struct rw_semaphore domains_rwsem; 40 struct xarray domains; 41 struct xarray access_list; 42 unsigned int next_domain_id; 43 44 struct rw_semaphore iova_rwsem; 45 struct rb_root_cached area_itree; 46 /* IOVA that cannot become reserved, struct iopt_allowed */ 47 struct rb_root_cached allowed_itree; 48 /* IOVA that cannot be allocated, struct iopt_reserved */ 49 struct rb_root_cached reserved_itree; 50 u8 disable_large_pages; 51 unsigned long iova_alignment; 52 }; 53 54 void iopt_init_table(struct io_pagetable *iopt); 55 void iopt_destroy_table(struct io_pagetable *iopt); 56 int iopt_get_pages(struct io_pagetable *iopt, unsigned long iova, 57 unsigned long length, struct list_head *pages_list); 58 void iopt_free_pages_list(struct list_head *pages_list); 59 enum { 60 IOPT_ALLOC_IOVA = 1 << 0, 61 }; 62 int iopt_map_user_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt, 63 unsigned long *iova, void __user *uptr, 64 unsigned long length, int iommu_prot, 65 unsigned int flags); 66 int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list, 67 unsigned long length, unsigned long *dst_iova, 68 int iommu_prot, unsigned int flags); 69 int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, 70 unsigned long length, unsigned long *unmapped); 71 int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped); 72 73 void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova, 74 unsigned long length); 75 int iopt_table_add_domain(struct io_pagetable *iopt, 76 struct iommu_domain *domain); 77 void iopt_table_remove_domain(struct io_pagetable *iopt, 78 struct iommu_domain *domain); 79 int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, 80 struct device *dev, 81 phys_addr_t *sw_msi_start); 82 int iopt_set_allow_iova(struct io_pagetable *iopt, 83 struct rb_root_cached *allowed_iova); 84 int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, 85 unsigned long last, void *owner); 86 void iopt_remove_reserved_iova(struct io_pagetable *iopt, void *owner); 87 int iopt_cut_iova(struct io_pagetable *iopt, unsigned long *iovas, 88 size_t num_iovas); 89 void iopt_enable_large_pages(struct io_pagetable *iopt); 90 int iopt_disable_large_pages(struct io_pagetable *iopt); 91 92 struct iommufd_ucmd { 93 struct iommufd_ctx *ictx; 94 void __user *ubuffer; 95 u32 user_size; 96 void *cmd; 97 }; 98 99 int iommufd_vfio_ioctl(struct iommufd_ctx *ictx, unsigned int cmd, 100 unsigned long arg); 101 102 /* Copy the response in ucmd->cmd back to userspace. */ 103 static inline int iommufd_ucmd_respond(struct iommufd_ucmd *ucmd, 104 size_t cmd_len) 105 { 106 if (copy_to_user(ucmd->ubuffer, ucmd->cmd, 107 min_t(size_t, ucmd->user_size, cmd_len))) 108 return -EFAULT; 109 return 0; 110 } 111 112 enum iommufd_object_type { 113 IOMMUFD_OBJ_NONE, 114 IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE, 115 IOMMUFD_OBJ_DEVICE, 116 IOMMUFD_OBJ_HW_PAGETABLE, 117 IOMMUFD_OBJ_IOAS, 118 IOMMUFD_OBJ_ACCESS, 119 #ifdef CONFIG_IOMMUFD_TEST 120 IOMMUFD_OBJ_SELFTEST, 121 #endif 122 IOMMUFD_OBJ_MAX, 123 }; 124 125 /* Base struct for all objects with a userspace ID handle. */ 126 struct iommufd_object { 127 struct rw_semaphore destroy_rwsem; 128 refcount_t users; 129 enum iommufd_object_type type; 130 unsigned int id; 131 }; 132 133 static inline bool iommufd_lock_obj(struct iommufd_object *obj) 134 { 135 if (!down_read_trylock(&obj->destroy_rwsem)) 136 return false; 137 if (!refcount_inc_not_zero(&obj->users)) { 138 up_read(&obj->destroy_rwsem); 139 return false; 140 } 141 return true; 142 } 143 144 struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id, 145 enum iommufd_object_type type); 146 static inline void iommufd_put_object(struct iommufd_object *obj) 147 { 148 refcount_dec(&obj->users); 149 up_read(&obj->destroy_rwsem); 150 } 151 152 /** 153 * iommufd_ref_to_users() - Switch from destroy_rwsem to users refcount 154 * protection 155 * @obj - Object to release 156 * 157 * Objects have two refcount protections (destroy_rwsem and the refcount_t 158 * users). Holding either of these will prevent the object from being destroyed. 159 * 160 * Depending on the use case, one protection or the other is appropriate. In 161 * most cases references are being protected by the destroy_rwsem. This allows 162 * orderly destruction of the object because iommufd_object_destroy_user() will 163 * wait for it to become unlocked. However, as a rwsem, it cannot be held across 164 * a system call return. So cases that have longer term needs must switch 165 * to the weaker users refcount_t. 166 * 167 * With users protection iommufd_object_destroy_user() will return false, 168 * refusing to destroy the object, causing -EBUSY to userspace. 169 */ 170 static inline void iommufd_ref_to_users(struct iommufd_object *obj) 171 { 172 up_read(&obj->destroy_rwsem); 173 /* iommufd_lock_obj() obtains users as well */ 174 } 175 void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj); 176 void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx, 177 struct iommufd_object *obj); 178 void iommufd_object_finalize(struct iommufd_ctx *ictx, 179 struct iommufd_object *obj); 180 bool iommufd_object_destroy_user(struct iommufd_ctx *ictx, 181 struct iommufd_object *obj); 182 struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, 183 size_t size, 184 enum iommufd_object_type type); 185 186 #define iommufd_object_alloc(ictx, ptr, type) \ 187 container_of(_iommufd_object_alloc( \ 188 ictx, \ 189 sizeof(*(ptr)) + BUILD_BUG_ON_ZERO( \ 190 offsetof(typeof(*(ptr)), \ 191 obj) != 0), \ 192 type), \ 193 typeof(*(ptr)), obj) 194 195 /* 196 * The IO Address Space (IOAS) pagetable is a virtual page table backed by the 197 * io_pagetable object. It is a user controlled mapping of IOVA -> PFNs. The 198 * mapping is copied into all of the associated domains and made available to 199 * in-kernel users. 200 * 201 * Every iommu_domain that is created is wrapped in a iommufd_hw_pagetable 202 * object. When we go to attach a device to an IOAS we need to get an 203 * iommu_domain and wrapping iommufd_hw_pagetable for it. 204 * 205 * An iommu_domain & iommfd_hw_pagetable will be automatically selected 206 * for a device based on the hwpt_list. If no suitable iommu_domain 207 * is found a new iommu_domain will be created. 208 */ 209 struct iommufd_ioas { 210 struct iommufd_object obj; 211 struct io_pagetable iopt; 212 struct mutex mutex; 213 struct list_head hwpt_list; 214 }; 215 216 static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx, 217 u32 id) 218 { 219 return container_of(iommufd_get_object(ictx, id, 220 IOMMUFD_OBJ_IOAS), 221 struct iommufd_ioas, obj); 222 } 223 224 struct iommufd_ioas *iommufd_ioas_alloc(struct iommufd_ctx *ictx); 225 int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd); 226 void iommufd_ioas_destroy(struct iommufd_object *obj); 227 int iommufd_ioas_iova_ranges(struct iommufd_ucmd *ucmd); 228 int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd); 229 int iommufd_ioas_map(struct iommufd_ucmd *ucmd); 230 int iommufd_ioas_copy(struct iommufd_ucmd *ucmd); 231 int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd); 232 int iommufd_ioas_option(struct iommufd_ucmd *ucmd); 233 int iommufd_option_rlimit_mode(struct iommu_option *cmd, 234 struct iommufd_ctx *ictx); 235 236 int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd); 237 238 /* 239 * A HW pagetable is called an iommu_domain inside the kernel. This user object 240 * allows directly creating and inspecting the domains. Domains that have kernel 241 * owned page tables will be associated with an iommufd_ioas that provides the 242 * IOVA to PFN map. 243 */ 244 struct iommufd_hw_pagetable { 245 struct iommufd_object obj; 246 struct iommufd_ioas *ioas; 247 struct iommu_domain *domain; 248 bool auto_domain : 1; 249 bool enforce_cache_coherency : 1; 250 bool msi_cookie : 1; 251 /* Head at iommufd_ioas::hwpt_list */ 252 struct list_head hwpt_item; 253 }; 254 255 struct iommufd_hw_pagetable * 256 iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, 257 struct iommufd_device *idev, bool immediate_attach); 258 int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); 259 int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, 260 struct iommufd_device *idev); 261 struct iommufd_hw_pagetable * 262 iommufd_hw_pagetable_detach(struct iommufd_device *idev); 263 void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); 264 void iommufd_hw_pagetable_abort(struct iommufd_object *obj); 265 int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); 266 267 static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, 268 struct iommufd_hw_pagetable *hwpt) 269 { 270 lockdep_assert_not_held(&hwpt->ioas->mutex); 271 if (hwpt->auto_domain) 272 iommufd_object_destroy_user(ictx, &hwpt->obj); 273 else 274 refcount_dec(&hwpt->obj.users); 275 } 276 277 struct iommufd_group { 278 struct kref ref; 279 struct mutex lock; 280 struct iommufd_ctx *ictx; 281 struct iommu_group *group; 282 struct iommufd_hw_pagetable *hwpt; 283 struct list_head device_list; 284 phys_addr_t sw_msi_start; 285 }; 286 287 /* 288 * A iommufd_device object represents the binding relationship between a 289 * consuming driver and the iommufd. These objects are created/destroyed by 290 * external drivers, not by userspace. 291 */ 292 struct iommufd_device { 293 struct iommufd_object obj; 294 struct iommufd_ctx *ictx; 295 struct iommufd_group *igroup; 296 struct list_head group_item; 297 /* always the physical device */ 298 struct device *dev; 299 bool enforce_cache_coherency; 300 }; 301 302 static inline struct iommufd_device * 303 iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) 304 { 305 return container_of(iommufd_get_object(ucmd->ictx, id, 306 IOMMUFD_OBJ_DEVICE), 307 struct iommufd_device, obj); 308 } 309 310 void iommufd_device_destroy(struct iommufd_object *obj); 311 312 struct iommufd_access { 313 struct iommufd_object obj; 314 struct iommufd_ctx *ictx; 315 struct iommufd_ioas *ioas; 316 struct iommufd_ioas *ioas_unpin; 317 struct mutex ioas_lock; 318 const struct iommufd_access_ops *ops; 319 void *data; 320 unsigned long iova_alignment; 321 u32 iopt_access_list_id; 322 }; 323 324 int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access); 325 void iopt_remove_access(struct io_pagetable *iopt, 326 struct iommufd_access *access, 327 u32 iopt_access_list_id); 328 void iommufd_access_destroy_object(struct iommufd_object *obj); 329 330 #ifdef CONFIG_IOMMUFD_TEST 331 int iommufd_test(struct iommufd_ucmd *ucmd); 332 void iommufd_selftest_destroy(struct iommufd_object *obj); 333 extern size_t iommufd_test_memory_limit; 334 void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, 335 unsigned int ioas_id, u64 *iova, u32 *flags); 336 bool iommufd_should_fail(void); 337 int __init iommufd_test_init(void); 338 void iommufd_test_exit(void); 339 bool iommufd_selftest_is_mock_dev(struct device *dev); 340 #else 341 static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, 342 unsigned int ioas_id, 343 u64 *iova, u32 *flags) 344 { 345 } 346 static inline bool iommufd_should_fail(void) 347 { 348 return false; 349 } 350 static inline int __init iommufd_test_init(void) 351 { 352 return 0; 353 } 354 static inline void iommufd_test_exit(void) 355 { 356 } 357 static inline bool iommufd_selftest_is_mock_dev(struct device *dev) 358 { 359 return false; 360 } 361 #endif 362 #endif 363