1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. 4 * Author: Joerg Roedel <joerg.roedel@amd.com> 5 */ 6 7 #ifndef __LINUX_IOMMU_H 8 #define __LINUX_IOMMU_H 9 10 #include <linux/scatterlist.h> 11 #include <linux/device.h> 12 #include <linux/types.h> 13 #include <linux/errno.h> 14 #include <linux/err.h> 15 #include <linux/of.h> 16 #include <linux/iova_bitmap.h> 17 #include <uapi/linux/iommufd.h> 18 19 #define IOMMU_READ (1 << 0) 20 #define IOMMU_WRITE (1 << 1) 21 #define IOMMU_CACHE (1 << 2) /* DMA cache coherency */ 22 #define IOMMU_NOEXEC (1 << 3) 23 #define IOMMU_MMIO (1 << 4) /* e.g. things like MSI doorbells */ 24 /* 25 * Where the bus hardware includes a privilege level as part of its access type 26 * markings, and certain devices are capable of issuing transactions marked as 27 * either 'supervisor' or 'user', the IOMMU_PRIV flag requests that the other 28 * given permission flags only apply to accesses at the higher privilege level, 29 * and that unprivileged transactions should have as little access as possible. 30 * This would usually imply the same permissions as kernel mappings on the CPU, 31 * if the IOMMU page table format is equivalent. 32 */ 33 #define IOMMU_PRIV (1 << 5) 34 35 struct iommu_ops; 36 struct iommu_group; 37 struct bus_type; 38 struct device; 39 struct iommu_domain; 40 struct iommu_domain_ops; 41 struct iommu_dirty_ops; 42 struct notifier_block; 43 struct iommu_sva; 44 struct iommu_dma_cookie; 45 struct iommu_dma_msi_cookie; 46 struct iommu_fault_param; 47 struct iommufd_ctx; 48 struct iommufd_viommu; 49 struct msi_desc; 50 struct msi_msg; 51 52 #define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ 53 #define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ 54 #define IOMMU_FAULT_PERM_EXEC (1 << 2) /* exec */ 55 #define IOMMU_FAULT_PERM_PRIV (1 << 3) /* privileged */ 56 57 /* Generic fault types, can be expanded IRQ remapping fault */ 58 enum iommu_fault_type { 59 IOMMU_FAULT_PAGE_REQ = 1, /* page request fault */ 60 }; 61 62 /** 63 * struct iommu_fault_page_request - Page Request data 64 * @flags: encodes whether the corresponding fields are valid and whether this 65 * is the last page in group (IOMMU_FAULT_PAGE_REQUEST_* values). 66 * When IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID is set, the page response 67 * must have the same PASID value as the page request. When it is clear, 68 * the page response should not have a PASID. 69 * @pasid: Process Address Space ID 70 * @grpid: Page Request Group Index 71 * @perm: requested page permissions (IOMMU_FAULT_PERM_* values) 72 * @addr: page address 73 * @private_data: device-specific private information 74 */ 75 struct iommu_fault_page_request { 76 #define IOMMU_FAULT_PAGE_REQUEST_PASID_VALID (1 << 0) 77 #define IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE (1 << 1) 78 #define IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID (1 << 2) 79 u32 flags; 80 u32 pasid; 81 u32 grpid; 82 u32 perm; 83 u64 addr; 84 u64 private_data[2]; 85 }; 86 87 /** 88 * struct iommu_fault - Generic fault data 89 * @type: fault type from &enum iommu_fault_type 90 * @prm: Page Request message, when @type is %IOMMU_FAULT_PAGE_REQ 91 */ 92 struct iommu_fault { 93 u32 type; 94 struct iommu_fault_page_request prm; 95 }; 96 97 /** 98 * enum iommu_page_response_code - Return status of fault handlers 99 * @IOMMU_PAGE_RESP_SUCCESS: Fault has been handled and the page tables 100 * populated, retry the access. This is "Success" in PCI PRI. 101 * @IOMMU_PAGE_RESP_FAILURE: General error. Drop all subsequent faults from 102 * this device if possible. This is "Response Failure" in PCI PRI. 103 * @IOMMU_PAGE_RESP_INVALID: Could not handle this fault, don't retry the 104 * access. This is "Invalid Request" in PCI PRI. 105 */ 106 enum iommu_page_response_code { 107 IOMMU_PAGE_RESP_SUCCESS = 0, 108 IOMMU_PAGE_RESP_INVALID, 109 IOMMU_PAGE_RESP_FAILURE, 110 }; 111 112 /** 113 * struct iommu_page_response - Generic page response information 114 * @pasid: Process Address Space ID 115 * @grpid: Page Request Group Index 116 * @code: response code from &enum iommu_page_response_code 117 */ 118 struct iommu_page_response { 119 u32 pasid; 120 u32 grpid; 121 u32 code; 122 }; 123 124 struct iopf_fault { 125 struct iommu_fault fault; 126 /* node for pending lists */ 127 struct list_head list; 128 }; 129 130 struct iopf_group { 131 struct iopf_fault last_fault; 132 struct list_head faults; 133 size_t fault_count; 134 /* list node for iommu_fault_param::faults */ 135 struct list_head pending_node; 136 struct work_struct work; 137 struct iommu_attach_handle *attach_handle; 138 /* The device's fault data parameter. */ 139 struct iommu_fault_param *fault_param; 140 /* Used by handler provider to hook the group on its own lists. */ 141 struct list_head node; 142 u32 cookie; 143 }; 144 145 /** 146 * struct iopf_queue - IO Page Fault queue 147 * @wq: the fault workqueue 148 * @devices: devices attached to this queue 149 * @lock: protects the device list 150 */ 151 struct iopf_queue { 152 struct workqueue_struct *wq; 153 struct list_head devices; 154 struct mutex lock; 155 }; 156 157 /* iommu fault flags */ 158 #define IOMMU_FAULT_READ 0x0 159 #define IOMMU_FAULT_WRITE 0x1 160 161 typedef int (*iommu_fault_handler_t)(struct iommu_domain *, 162 struct device *, unsigned long, int, void *); 163 164 struct iommu_domain_geometry { 165 dma_addr_t aperture_start; /* First address that can be mapped */ 166 dma_addr_t aperture_end; /* Last address that can be mapped */ 167 bool force_aperture; /* DMA only allowed in mappable range? */ 168 }; 169 170 enum iommu_domain_cookie_type { 171 IOMMU_COOKIE_NONE, 172 IOMMU_COOKIE_DMA_IOVA, 173 IOMMU_COOKIE_DMA_MSI, 174 IOMMU_COOKIE_FAULT_HANDLER, 175 IOMMU_COOKIE_SVA, 176 IOMMU_COOKIE_IOMMUFD, 177 }; 178 179 /* Domain feature flags */ 180 #define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */ 181 #define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API 182 implementation */ 183 #define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */ 184 #define __IOMMU_DOMAIN_DMA_FQ (1U << 3) /* DMA-API uses flush queue */ 185 186 #define __IOMMU_DOMAIN_SVA (1U << 4) /* Shared process address space */ 187 #define __IOMMU_DOMAIN_PLATFORM (1U << 5) 188 189 #define __IOMMU_DOMAIN_NESTED (1U << 6) /* User-managed address space nested 190 on a stage-2 translation */ 191 192 #define IOMMU_DOMAIN_ALLOC_FLAGS ~__IOMMU_DOMAIN_DMA_FQ 193 /* 194 * This are the possible domain-types 195 * 196 * IOMMU_DOMAIN_BLOCKED - All DMA is blocked, can be used to isolate 197 * devices 198 * IOMMU_DOMAIN_IDENTITY - DMA addresses are system physical addresses 199 * IOMMU_DOMAIN_UNMANAGED - DMA mappings managed by IOMMU-API user, used 200 * for VMs 201 * IOMMU_DOMAIN_DMA - Internally used for DMA-API implementations. 202 * This flag allows IOMMU drivers to implement 203 * certain optimizations for these domains 204 * IOMMU_DOMAIN_DMA_FQ - As above, but definitely using batched TLB 205 * invalidation. 206 * IOMMU_DOMAIN_SVA - DMA addresses are shared process addresses 207 * represented by mm_struct's. 208 * IOMMU_DOMAIN_PLATFORM - Legacy domain for drivers that do their own 209 * dma_api stuff. Do not use in new drivers. 210 */ 211 #define IOMMU_DOMAIN_BLOCKED (0U) 212 #define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT) 213 #define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING) 214 #define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \ 215 __IOMMU_DOMAIN_DMA_API) 216 #define IOMMU_DOMAIN_DMA_FQ (__IOMMU_DOMAIN_PAGING | \ 217 __IOMMU_DOMAIN_DMA_API | \ 218 __IOMMU_DOMAIN_DMA_FQ) 219 #define IOMMU_DOMAIN_SVA (__IOMMU_DOMAIN_SVA) 220 #define IOMMU_DOMAIN_PLATFORM (__IOMMU_DOMAIN_PLATFORM) 221 #define IOMMU_DOMAIN_NESTED (__IOMMU_DOMAIN_NESTED) 222 223 struct iommu_domain { 224 unsigned type; 225 enum iommu_domain_cookie_type cookie_type; 226 bool is_iommupt; 227 const struct iommu_domain_ops *ops; 228 const struct iommu_dirty_ops *dirty_ops; 229 const struct iommu_ops *owner; /* Whose domain_alloc we came from */ 230 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ 231 struct iommu_domain_geometry geometry; 232 int (*iopf_handler)(struct iopf_group *group); 233 234 union { /* cookie */ 235 struct iommu_dma_cookie *iova_cookie; 236 struct iommu_dma_msi_cookie *msi_cookie; 237 struct iommufd_hw_pagetable *iommufd_hwpt; 238 struct { 239 iommu_fault_handler_t handler; 240 void *handler_token; 241 }; 242 struct { /* IOMMU_DOMAIN_SVA */ 243 struct mm_struct *mm; 244 int users; 245 /* 246 * Next iommu_domain in mm->iommu_mm->sva-domains list 247 * protected by iommu_sva_lock. 248 */ 249 struct list_head next; 250 }; 251 }; 252 }; 253 254 static inline bool iommu_is_dma_domain(struct iommu_domain *domain) 255 { 256 return domain->type & __IOMMU_DOMAIN_DMA_API; 257 } 258 259 enum iommu_cap { 260 IOMMU_CAP_CACHE_COHERENCY, /* IOMMU_CACHE is supported */ 261 IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */ 262 IOMMU_CAP_PRE_BOOT_PROTECTION, /* Firmware says it used the IOMMU for 263 DMA protection and we should too */ 264 /* 265 * Per-device flag indicating if enforce_cache_coherency() will work on 266 * this device. 267 */ 268 IOMMU_CAP_ENFORCE_CACHE_COHERENCY, 269 /* 270 * IOMMU driver does not issue TLB maintenance during .unmap, so can 271 * usefully support the non-strict DMA flush queue. 272 */ 273 IOMMU_CAP_DEFERRED_FLUSH, 274 IOMMU_CAP_DIRTY_TRACKING, /* IOMMU supports dirty tracking */ 275 /* ATS is supported and may be enabled for this device */ 276 IOMMU_CAP_PCI_ATS_SUPPORTED, 277 }; 278 279 /* These are the possible reserved region types */ 280 enum iommu_resv_type { 281 /* Memory regions which must be mapped 1:1 at all times */ 282 IOMMU_RESV_DIRECT, 283 /* 284 * Memory regions which are advertised to be 1:1 but are 285 * commonly considered relaxable in some conditions, 286 * for instance in device assignment use case (USB, Graphics) 287 */ 288 IOMMU_RESV_DIRECT_RELAXABLE, 289 /* Arbitrary "never map this or give it to a device" address ranges */ 290 IOMMU_RESV_RESERVED, 291 /* Hardware MSI region (untranslated) */ 292 IOMMU_RESV_MSI, 293 /* Software-managed MSI translation window */ 294 IOMMU_RESV_SW_MSI, 295 }; 296 297 /** 298 * struct iommu_resv_region - descriptor for a reserved memory region 299 * @list: Linked list pointers 300 * @start: System physical start address of the region 301 * @length: Length of the region in bytes 302 * @prot: IOMMU Protection flags (READ/WRITE/...) 303 * @type: Type of the reserved region 304 * @free: Callback to free associated memory allocations 305 */ 306 struct iommu_resv_region { 307 struct list_head list; 308 phys_addr_t start; 309 size_t length; 310 int prot; 311 enum iommu_resv_type type; 312 void (*free)(struct device *dev, struct iommu_resv_region *region); 313 }; 314 315 struct iommu_iort_rmr_data { 316 struct iommu_resv_region rr; 317 318 /* Stream IDs associated with IORT RMR entry */ 319 const u32 *sids; 320 u32 num_sids; 321 }; 322 323 #define IOMMU_NO_PASID (0U) /* Reserved for DMA w/o PASID */ 324 #define IOMMU_FIRST_GLOBAL_PASID (1U) /*starting range for allocation */ 325 #define IOMMU_PASID_INVALID (-1U) 326 typedef unsigned int ioasid_t; 327 328 /* Read but do not clear any dirty bits */ 329 #define IOMMU_DIRTY_NO_CLEAR (1 << 0) 330 331 /* 332 * Pages allocated through iommu_alloc_pages_node_sz() can be placed on this 333 * list using iommu_pages_list_add(). Note: ONLY pages from 334 * iommu_alloc_pages_node_sz() can be used this way! 335 */ 336 struct iommu_pages_list { 337 struct list_head pages; 338 }; 339 340 #define IOMMU_PAGES_LIST_INIT(name) \ 341 ((struct iommu_pages_list){ .pages = LIST_HEAD_INIT(name.pages) }) 342 343 #ifdef CONFIG_IOMMU_API 344 345 /** 346 * struct iommu_iotlb_gather - Range information for a pending IOTLB flush 347 * 348 * This structure is intended to be updated by multiple calls to the 349 * ->unmap() function in struct iommu_ops before eventually being passed 350 * into ->iotlb_sync(). Drivers can add pages to @freelist to be freed after 351 * ->iotlb_sync() or ->iotlb_flush_all() have cleared all cached references to 352 * them. @queued is set to indicate when ->iotlb_flush_all() will be called 353 * later instead of ->iotlb_sync(), so drivers may optimise accordingly. 354 */ 355 struct iommu_iotlb_gather { 356 /** @start: IOVA representing the start of the range to be flushed */ 357 unsigned long start; 358 /** 359 * @end: IOVA representing the end of the range to be 360 * flushed (inclusive) 361 */ 362 unsigned long end; 363 364 union { 365 /** 366 * @pgsize: The interval at which to perform the flush, only 367 * used by arm-smmu-v3 368 */ 369 size_t pgsize; 370 struct { 371 /** 372 * @pt.leaf_levels_bitmap: Bitmap of generic_pt 373 * levels where leaf entries were unmapped. Bit 0 374 * means the leaf only level. If 0 no leafs 375 * were unmapped. 376 */ 377 u8 leaf_levels_bitmap; 378 /** 379 * @pt.table_levels_bitmap: Bitmap of generic_pt levels 380 * of table entries that were removed. Bit 0 is never 381 * set, bit 1 means a table of all leafs was removed. 382 * When freelist is empty this must be 0. 383 */ 384 u8 table_levels_bitmap; 385 } pt; 386 }; 387 388 /** 389 * @freelist: Removed pages to free after sync, only used by 390 * iommupt 391 */ 392 struct iommu_pages_list freelist; 393 /** @queued: True if the gather will be completed with a flush all */ 394 bool queued; 395 }; 396 397 /** 398 * struct iommu_dirty_bitmap - Dirty IOVA bitmap state 399 * @bitmap: IOVA bitmap 400 * @gather: Range information for a pending IOTLB flush 401 */ 402 struct iommu_dirty_bitmap { 403 struct iova_bitmap *bitmap; 404 struct iommu_iotlb_gather *gather; 405 }; 406 407 /** 408 * struct iommu_dirty_ops - domain specific dirty tracking operations 409 * @set_dirty_tracking: Enable or Disable dirty tracking on the iommu domain 410 * @read_and_clear_dirty: Walk IOMMU page tables for dirtied PTEs marshalled 411 * into a bitmap, with a bit represented as a page. 412 * Reads the dirty PTE bits and clears it from IO 413 * pagetables. 414 */ 415 struct iommu_dirty_ops { 416 int (*set_dirty_tracking)(struct iommu_domain *domain, bool enabled); 417 int (*read_and_clear_dirty)(struct iommu_domain *domain, 418 unsigned long iova, size_t size, 419 unsigned long flags, 420 struct iommu_dirty_bitmap *dirty); 421 }; 422 423 /** 424 * struct iommu_user_data - iommu driver specific user space data info 425 * @type: The data type of the user buffer 426 * @uptr: Pointer to the user buffer for copy_from_user() 427 * @len: The length of the user buffer in bytes 428 * 429 * A user space data is an uAPI that is defined in include/uapi/linux/iommufd.h 430 * @type, @uptr and @len should be just copied from an iommufd core uAPI struct. 431 */ 432 struct iommu_user_data { 433 unsigned int type; 434 void __user *uptr; 435 size_t len; 436 }; 437 438 /** 439 * struct iommu_user_data_array - iommu driver specific user space data array 440 * @type: The data type of all the entries in the user buffer array 441 * @uptr: Pointer to the user buffer array 442 * @entry_len: The fixed-width length of an entry in the array, in bytes 443 * @entry_num: The number of total entries in the array 444 * 445 * The user buffer includes an array of requests with format defined in 446 * include/uapi/linux/iommufd.h 447 */ 448 struct iommu_user_data_array { 449 unsigned int type; 450 void __user *uptr; 451 size_t entry_len; 452 u32 entry_num; 453 }; 454 455 /** 456 * __iommu_copy_struct_from_user - Copy iommu driver specific user space data 457 * @dst_data: Pointer to an iommu driver specific user data that is defined in 458 * include/uapi/linux/iommufd.h 459 * @src_data: Pointer to a struct iommu_user_data for user space data info 460 * @data_type: The data type of the @dst_data. Must match with @src_data.type 461 * @data_len: Length of current user data structure, i.e. sizeof(struct _dst) 462 * @min_len: Initial length of user data structure for backward compatibility. 463 * This should be offsetofend using the last member in the user data 464 * struct that was initially added to include/uapi/linux/iommufd.h 465 */ 466 static inline int __iommu_copy_struct_from_user( 467 void *dst_data, const struct iommu_user_data *src_data, 468 unsigned int data_type, size_t data_len, size_t min_len) 469 { 470 if (WARN_ON(!dst_data || !src_data)) 471 return -EINVAL; 472 if (src_data->type != data_type) 473 return -EINVAL; 474 if (src_data->len < min_len || data_len < src_data->len) 475 return -EINVAL; 476 return copy_struct_from_user(dst_data, data_len, src_data->uptr, 477 src_data->len); 478 } 479 480 /** 481 * iommu_copy_struct_from_user - Copy iommu driver specific user space data 482 * @kdst: Pointer to an iommu driver specific user data that is defined in 483 * include/uapi/linux/iommufd.h 484 * @user_data: Pointer to a struct iommu_user_data for user space data info 485 * @data_type: The data type of the @kdst. Must match with @user_data->type 486 * @min_last: The last member of the data structure @kdst points in the initial 487 * version. 488 * Return 0 for success, otherwise -error. 489 */ 490 #define iommu_copy_struct_from_user(kdst, user_data, data_type, min_last) \ 491 __iommu_copy_struct_from_user(kdst, user_data, data_type, \ 492 sizeof(*kdst), \ 493 offsetofend(typeof(*kdst), min_last)) 494 495 /** 496 * __iommu_copy_struct_from_user_array - Copy iommu driver specific user space 497 * data from an iommu_user_data_array 498 * @dst_data: Pointer to an iommu driver specific user data that is defined in 499 * include/uapi/linux/iommufd.h 500 * @src_array: Pointer to a struct iommu_user_data_array for a user space array 501 * @data_type: The data type of the @dst_data. Must match with @src_array.type 502 * @index: Index to the location in the array to copy user data from 503 * @data_len: Length of current user data structure, i.e. sizeof(struct _dst) 504 * @min_len: Initial length of user data structure for backward compatibility. 505 * This should be offsetofend using the last member in the user data 506 * struct that was initially added to include/uapi/linux/iommufd.h 507 */ 508 static inline int __iommu_copy_struct_from_user_array( 509 void *dst_data, const struct iommu_user_data_array *src_array, 510 unsigned int data_type, unsigned int index, size_t data_len, 511 size_t min_len) 512 { 513 struct iommu_user_data src_data; 514 515 if (WARN_ON(!src_array || index >= src_array->entry_num)) 516 return -EINVAL; 517 if (!src_array->entry_num) 518 return -EINVAL; 519 src_data.uptr = src_array->uptr + src_array->entry_len * index; 520 src_data.len = src_array->entry_len; 521 src_data.type = src_array->type; 522 523 return __iommu_copy_struct_from_user(dst_data, &src_data, data_type, 524 data_len, min_len); 525 } 526 527 /** 528 * iommu_copy_struct_from_user_array - Copy iommu driver specific user space 529 * data from an iommu_user_data_array 530 * @kdst: Pointer to an iommu driver specific user data that is defined in 531 * include/uapi/linux/iommufd.h 532 * @user_array: Pointer to a struct iommu_user_data_array for a user space 533 * array 534 * @data_type: The data type of the @kdst. Must match with @user_array->type 535 * @index: Index to the location in the array to copy user data from 536 * @min_last: The last member of the data structure @kdst points in the 537 * initial version. 538 * 539 * Copy a single entry from a user array. Return 0 for success, otherwise 540 * -error. 541 */ 542 #define iommu_copy_struct_from_user_array(kdst, user_array, data_type, index, \ 543 min_last) \ 544 __iommu_copy_struct_from_user_array( \ 545 kdst, user_array, data_type, index, sizeof(*(kdst)), \ 546 offsetofend(typeof(*(kdst)), min_last)) 547 548 /** 549 * iommu_copy_struct_from_full_user_array - Copy iommu driver specific user 550 * space data from an iommu_user_data_array 551 * @kdst: Pointer to an iommu driver specific user data that is defined in 552 * include/uapi/linux/iommufd.h 553 * @kdst_entry_size: sizeof(*kdst) 554 * @user_array: Pointer to a struct iommu_user_data_array for a user space 555 * array 556 * @data_type: The data type of the @kdst. Must match with @user_array->type 557 * 558 * Copy the entire user array. kdst must have room for kdst_entry_size * 559 * user_array->entry_num bytes. Return 0 for success, otherwise -error. 560 */ 561 static inline int 562 iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, 563 struct iommu_user_data_array *user_array, 564 unsigned int data_type) 565 { 566 unsigned int i; 567 int ret; 568 569 if (user_array->type != data_type) 570 return -EINVAL; 571 if (!user_array->entry_num) 572 return -EINVAL; 573 if (likely(user_array->entry_len == kdst_entry_size)) { 574 if (copy_from_user(kdst, user_array->uptr, 575 user_array->entry_num * 576 user_array->entry_len)) 577 return -EFAULT; 578 return 0; 579 } 580 581 /* Copy item by item */ 582 for (i = 0; i != user_array->entry_num; i++) { 583 ret = copy_struct_from_user( 584 kdst + kdst_entry_size * i, kdst_entry_size, 585 user_array->uptr + user_array->entry_len * i, 586 user_array->entry_len); 587 if (ret) 588 return ret; 589 } 590 return 0; 591 } 592 593 /** 594 * __iommu_copy_struct_to_user - Report iommu driver specific user space data 595 * @dst_data: Pointer to a struct iommu_user_data for user space data location 596 * @src_data: Pointer to an iommu driver specific user data that is defined in 597 * include/uapi/linux/iommufd.h 598 * @data_type: The data type of the @src_data. Must match with @dst_data.type 599 * @data_len: Length of current user data structure, i.e. sizeof(struct _src) 600 * @min_len: Initial length of user data structure for backward compatibility. 601 * This should be offsetofend using the last member in the user data 602 * struct that was initially added to include/uapi/linux/iommufd.h 603 */ 604 static inline int 605 __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data, 606 void *src_data, unsigned int data_type, 607 size_t data_len, size_t min_len) 608 { 609 if (WARN_ON(!dst_data || !src_data)) 610 return -EINVAL; 611 if (dst_data->type != data_type) 612 return -EINVAL; 613 if (dst_data->len < min_len || data_len < dst_data->len) 614 return -EINVAL; 615 return copy_struct_to_user(dst_data->uptr, dst_data->len, src_data, 616 data_len, NULL); 617 } 618 619 /** 620 * iommu_copy_struct_to_user - Report iommu driver specific user space data 621 * @user_data: Pointer to a struct iommu_user_data for user space data location 622 * @ksrc: Pointer to an iommu driver specific user data that is defined in 623 * include/uapi/linux/iommufd.h 624 * @data_type: The data type of the @ksrc. Must match with @user_data->type 625 * @min_last: The last member of the data structure @ksrc points in the initial 626 * version. 627 * Return 0 for success, otherwise -error. 628 */ 629 #define iommu_copy_struct_to_user(user_data, ksrc, data_type, min_last) \ 630 __iommu_copy_struct_to_user(user_data, ksrc, data_type, sizeof(*ksrc), \ 631 offsetofend(typeof(*ksrc), min_last)) 632 633 /** 634 * struct iommu_ops - iommu ops and capabilities 635 * @capable: check capability 636 * @hw_info: report iommu hardware information. The data buffer returned by this 637 * op is allocated in the iommu driver and freed by the caller after 638 * use. @type can input a requested type and output a supported type. 639 * Driver should reject an unsupported data @type input 640 * @domain_alloc: Do not use in new drivers 641 * @domain_alloc_identity: allocate an IDENTITY domain. Drivers should prefer to 642 * use identity_domain instead. This should only be used 643 * if dynamic logic is necessary. 644 * @domain_alloc_paging_flags: Allocate an iommu domain corresponding to the 645 * input parameters as defined in 646 * include/uapi/linux/iommufd.h. The @user_data can be 647 * optionally provided, the new domain must support 648 * __IOMMU_DOMAIN_PAGING. Upon failure, ERR_PTR must be 649 * returned. 650 * @domain_alloc_paging: Allocate an iommu_domain that can be used for 651 * UNMANAGED, DMA, and DMA_FQ domain types. This is the 652 * same as invoking domain_alloc_paging_flags() with 653 * @flags=0, @user_data=NULL. A driver should implement 654 * only one of the two ops. 655 * @domain_alloc_sva: Allocate an iommu_domain for Shared Virtual Addressing. 656 * @domain_alloc_nested: Allocate an iommu_domain for nested translation. 657 * @probe_device: Add device to iommu driver handling 658 * @release_device: Remove device from iommu driver handling 659 * @probe_finalize: Do final setup work after the device is added to an IOMMU 660 * group and attached to the groups domain 661 * @device_group: find iommu group for a particular device 662 * @get_resv_regions: Request list of reserved regions for a device 663 * @of_xlate: add OF master IDs to iommu grouping 664 * @is_attach_deferred: Check if domain attach should be deferred from iommu 665 * driver init to device driver init (default no) 666 * @page_response: handle page request response 667 * @def_domain_type: device default domain type, return value: 668 * - IOMMU_DOMAIN_IDENTITY: must use an identity domain 669 * - IOMMU_DOMAIN_DMA: must use a dma domain 670 * - 0: use the default setting 671 * @default_domain_ops: the default ops for domains 672 * @get_viommu_size: Get the size of a driver-level vIOMMU structure for a given 673 * @dev corresponding to @viommu_type. Driver should return 0 674 * if vIOMMU isn't supported accordingly. It is required for 675 * driver to use the VIOMMU_STRUCT_SIZE macro to sanitize the 676 * driver-level vIOMMU structure related to the core one 677 * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical 678 * IOMMU instance @viommu->iommu_dev, as the set of virtualization 679 * resources shared/passed to user space IOMMU instance. Associate 680 * it with a nesting @parent_domain. It is required for driver to 681 * set @viommu->ops pointing to its own viommu_ops 682 * @owner: Driver module providing these ops 683 * @identity_domain: An always available, always attachable identity 684 * translation. 685 * @blocked_domain: An always available, always attachable blocking 686 * translation. 687 * @default_domain: If not NULL this will always be set as the default domain. 688 * This should be an IDENTITY/BLOCKED/PLATFORM domain. 689 * Do not use in new drivers. 690 * @user_pasid_table: IOMMU driver supports user-managed PASID table. There is 691 * no user domain for each PASID and the I/O page faults are 692 * forwarded through the user domain attached to the device 693 * RID. 694 */ 695 struct iommu_ops { 696 bool (*capable)(struct device *dev, enum iommu_cap); 697 void *(*hw_info)(struct device *dev, u32 *length, 698 enum iommu_hw_info_type *type); 699 700 /* Domain allocation and freeing by the iommu driver */ 701 #if IS_ENABLED(CONFIG_FSL_PAMU) 702 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); 703 #endif 704 struct iommu_domain *(*domain_alloc_identity)(struct device *dev); 705 struct iommu_domain *(*domain_alloc_paging_flags)( 706 struct device *dev, u32 flags, 707 const struct iommu_user_data *user_data); 708 struct iommu_domain *(*domain_alloc_paging)(struct device *dev); 709 struct iommu_domain *(*domain_alloc_sva)(struct device *dev, 710 struct mm_struct *mm); 711 struct iommu_domain *(*domain_alloc_nested)( 712 struct device *dev, struct iommu_domain *parent, u32 flags, 713 const struct iommu_user_data *user_data); 714 715 struct iommu_device *(*probe_device)(struct device *dev); 716 void (*release_device)(struct device *dev); 717 void (*probe_finalize)(struct device *dev); 718 struct iommu_group *(*device_group)(struct device *dev); 719 720 /* Request/Free a list of reserved regions for a device */ 721 void (*get_resv_regions)(struct device *dev, struct list_head *list); 722 723 int (*of_xlate)(struct device *dev, const struct of_phandle_args *args); 724 bool (*is_attach_deferred)(struct device *dev); 725 726 /* Per device IOMMU features */ 727 void (*page_response)(struct device *dev, struct iopf_fault *evt, 728 struct iommu_page_response *msg); 729 730 int (*def_domain_type)(struct device *dev); 731 732 size_t (*get_viommu_size)(struct device *dev, 733 enum iommu_viommu_type viommu_type); 734 int (*viommu_init)(struct iommufd_viommu *viommu, 735 struct iommu_domain *parent_domain, 736 const struct iommu_user_data *user_data); 737 738 const struct iommu_domain_ops *default_domain_ops; 739 struct module *owner; 740 struct iommu_domain *identity_domain; 741 struct iommu_domain *blocked_domain; 742 struct iommu_domain *release_domain; 743 struct iommu_domain *default_domain; 744 u8 user_pasid_table:1; 745 }; 746 747 /** 748 * struct iommu_domain_ops - domain specific operations 749 * @attach_dev: attach an iommu domain to a device 750 * Return: 751 * * 0 - success 752 * * EINVAL - can indicate that device and domain are incompatible due to 753 * some previous configuration of the domain, in which case the 754 * driver shouldn't log an error, since it is legitimate for a 755 * caller to test reuse of existing domains. Otherwise, it may 756 * still represent some other fundamental problem 757 * * ENOMEM - out of memory 758 * * ENOSPC - non-ENOMEM type of resource allocation failures 759 * * EBUSY - device is attached to a domain and cannot be changed 760 * * ENODEV - device specific errors, not able to be attached 761 * * <others> - treated as ENODEV by the caller. Use is discouraged 762 * @set_dev_pasid: set or replace an iommu domain to a pasid of device. The pasid of 763 * the device should be left in the old config in error case. 764 * @map_pages: map a physically contiguous set of pages of the same size to 765 * an iommu domain. 766 * @unmap_pages: unmap a number of pages of the same size from an iommu domain 767 * @flush_iotlb_all: Synchronously flush all hardware TLBs for this domain 768 * @iotlb_sync_map: Sync mappings created recently using @map to the hardware 769 * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush 770 * queue 771 * @cache_invalidate_user: Flush hardware cache for user space IO page table. 772 * The @domain must be IOMMU_DOMAIN_NESTED. The @array 773 * passes in the cache invalidation requests, in form 774 * of a driver data structure. The driver must update 775 * array->entry_num to report the number of handled 776 * invalidation requests. A driver may handle fewer than 777 * the requested, in which case the core re-invokes the 778 * op for the remainder. The driver data structure must 779 * be defined in include/uapi/linux/iommufd.h 780 * @iova_to_phys: translate iova to physical address 781 * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, 782 * including no-snoop TLPs on PCIe or other platform 783 * specific mechanisms. 784 * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) 785 * @free: Release the domain after use. 786 */ 787 struct iommu_domain_ops { 788 int (*attach_dev)(struct iommu_domain *domain, struct device *dev, 789 struct iommu_domain *old); 790 int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev, 791 ioasid_t pasid, struct iommu_domain *old); 792 793 int (*map_pages)(struct iommu_domain *domain, unsigned long iova, 794 phys_addr_t paddr, size_t pgsize, size_t pgcount, 795 int prot, gfp_t gfp, size_t *mapped); 796 size_t (*unmap_pages)(struct iommu_domain *domain, unsigned long iova, 797 size_t pgsize, size_t pgcount, 798 struct iommu_iotlb_gather *iotlb_gather); 799 800 void (*flush_iotlb_all)(struct iommu_domain *domain); 801 int (*iotlb_sync_map)(struct iommu_domain *domain, unsigned long iova, 802 size_t size); 803 void (*iotlb_sync)(struct iommu_domain *domain, 804 struct iommu_iotlb_gather *iotlb_gather); 805 int (*cache_invalidate_user)(struct iommu_domain *domain, 806 struct iommu_user_data_array *array); 807 808 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, 809 dma_addr_t iova); 810 811 bool (*enforce_cache_coherency)(struct iommu_domain *domain); 812 int (*set_pgtable_quirks)(struct iommu_domain *domain, 813 unsigned long quirks); 814 815 void (*free)(struct iommu_domain *domain); 816 }; 817 818 /** 819 * struct iommu_device - IOMMU core representation of one IOMMU hardware 820 * instance 821 * @list: Used by the iommu-core to keep a list of registered iommus 822 * @ops: iommu-ops for talking to this iommu 823 * @dev: struct device for sysfs handling 824 * @singleton_group: Used internally for drivers that have only one group 825 * @max_pasids: number of supported PASIDs 826 * @ready: set once iommu_device_register() has completed successfully 827 */ 828 struct iommu_device { 829 struct list_head list; 830 const struct iommu_ops *ops; 831 struct fwnode_handle *fwnode; 832 struct device *dev; 833 struct iommu_group *singleton_group; 834 u32 max_pasids; 835 bool ready; 836 }; 837 838 /** 839 * struct iommu_fault_param - per-device IOMMU fault data 840 * @lock: protect pending faults list 841 * @users: user counter to manage the lifetime of the data 842 * @rcu: rcu head for kfree_rcu() 843 * @dev: the device that owns this param 844 * @queue: IOPF queue 845 * @queue_list: index into queue->devices 846 * @partial: faults that are part of a Page Request Group for which the last 847 * request hasn't been submitted yet. 848 * @faults: holds the pending faults which need response 849 */ 850 struct iommu_fault_param { 851 struct mutex lock; 852 refcount_t users; 853 struct rcu_head rcu; 854 855 struct device *dev; 856 struct iopf_queue *queue; 857 struct list_head queue_list; 858 859 struct list_head partial; 860 struct list_head faults; 861 }; 862 863 /** 864 * struct dev_iommu - Collection of per-device IOMMU data 865 * 866 * @fault_param: IOMMU detected device fault reporting data 867 * @fwspec: IOMMU fwspec data 868 * @iommu_dev: IOMMU device this device is linked to 869 * @priv: IOMMU Driver private data 870 * @max_pasids: number of PASIDs this device can consume 871 * @attach_deferred: the dma domain attachment is deferred 872 * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs 873 * @require_direct: device requires IOMMU_RESV_DIRECT regions 874 * @shadow_on_flush: IOTLB flushes are used to sync shadow tables 875 * 876 * TODO: migrate other per device data pointers under iommu_dev_data, e.g. 877 * struct iommu_group *iommu_group; 878 */ 879 struct dev_iommu { 880 struct mutex lock; 881 struct iommu_fault_param __rcu *fault_param; 882 struct iommu_fwspec *fwspec; 883 struct iommu_device *iommu_dev; 884 void *priv; 885 u32 max_pasids; 886 u32 attach_deferred:1; 887 u32 pci_32bit_workaround:1; 888 u32 require_direct:1; 889 u32 shadow_on_flush:1; 890 }; 891 892 int iommu_device_register(struct iommu_device *iommu, 893 const struct iommu_ops *ops, 894 struct device *hwdev); 895 void iommu_device_unregister(struct iommu_device *iommu); 896 int iommu_device_sysfs_add(struct iommu_device *iommu, 897 struct device *parent, 898 const struct attribute_group **groups, 899 const char *fmt, ...) __printf(4, 5); 900 void iommu_device_sysfs_remove(struct iommu_device *iommu); 901 int iommu_device_link(struct iommu_device *iommu, struct device *link); 902 void iommu_device_unlink(struct iommu_device *iommu, struct device *link); 903 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain); 904 905 static inline struct iommu_device *dev_to_iommu_device(struct device *dev) 906 { 907 return (struct iommu_device *)dev_get_drvdata(dev); 908 } 909 910 /** 911 * iommu_get_iommu_dev - Get iommu_device for a device 912 * @dev: an end-point device 913 * 914 * Note that this function must be called from the iommu_ops 915 * to retrieve the iommu_device for a device, which the core code 916 * guarentees it will not invoke the op without an attached iommu. 917 */ 918 static inline struct iommu_device *__iommu_get_iommu_dev(struct device *dev) 919 { 920 return dev->iommu->iommu_dev; 921 } 922 923 #define iommu_get_iommu_dev(dev, type, member) \ 924 container_of(__iommu_get_iommu_dev(dev), type, member) 925 926 static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather) 927 { 928 *gather = (struct iommu_iotlb_gather) { 929 .start = ULONG_MAX, 930 .freelist = IOMMU_PAGES_LIST_INIT(gather->freelist), 931 }; 932 } 933 934 extern bool device_iommu_capable(struct device *dev, enum iommu_cap cap); 935 extern bool iommu_group_has_isolated_msi(struct iommu_group *group); 936 struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, unsigned int flags); 937 static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev) 938 { 939 return iommu_paging_domain_alloc_flags(dev, 0); 940 } 941 extern void iommu_domain_free(struct iommu_domain *domain); 942 extern int iommu_attach_device(struct iommu_domain *domain, 943 struct device *dev); 944 extern void iommu_detach_device(struct iommu_domain *domain, 945 struct device *dev); 946 extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev); 947 struct iommu_domain *iommu_driver_get_domain_for_dev(struct device *dev); 948 extern struct iommu_domain *iommu_get_dma_domain(struct device *dev); 949 extern int iommu_map(struct iommu_domain *domain, unsigned long iova, 950 phys_addr_t paddr, size_t size, int prot, gfp_t gfp); 951 int iommu_map_nosync(struct iommu_domain *domain, unsigned long iova, 952 phys_addr_t paddr, size_t size, int prot, gfp_t gfp); 953 int iommu_sync_map(struct iommu_domain *domain, unsigned long iova, 954 size_t size); 955 extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, 956 size_t size); 957 extern size_t iommu_unmap_fast(struct iommu_domain *domain, 958 unsigned long iova, size_t size, 959 struct iommu_iotlb_gather *iotlb_gather); 960 extern ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, 961 struct scatterlist *sg, unsigned int nents, 962 int prot, gfp_t gfp); 963 extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova); 964 extern void iommu_set_fault_handler(struct iommu_domain *domain, 965 iommu_fault_handler_t handler, void *token); 966 967 extern void iommu_get_resv_regions(struct device *dev, struct list_head *list); 968 extern void iommu_put_resv_regions(struct device *dev, struct list_head *list); 969 extern void iommu_set_default_passthrough(bool cmd_line); 970 extern void iommu_set_default_translated(bool cmd_line); 971 extern bool iommu_default_passthrough(void); 972 extern struct iommu_resv_region * 973 iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot, 974 enum iommu_resv_type type, gfp_t gfp); 975 extern int iommu_get_group_resv_regions(struct iommu_group *group, 976 struct list_head *head); 977 978 extern int iommu_attach_group(struct iommu_domain *domain, 979 struct iommu_group *group); 980 extern void iommu_detach_group(struct iommu_domain *domain, 981 struct iommu_group *group); 982 extern struct iommu_group *iommu_group_alloc(void); 983 extern void *iommu_group_get_iommudata(struct iommu_group *group); 984 extern void iommu_group_set_iommudata(struct iommu_group *group, 985 void *iommu_data, 986 void (*release)(void *iommu_data)); 987 extern int iommu_group_set_name(struct iommu_group *group, const char *name); 988 extern int iommu_group_add_device(struct iommu_group *group, 989 struct device *dev); 990 extern void iommu_group_remove_device(struct device *dev); 991 extern int iommu_group_for_each_dev(struct iommu_group *group, void *data, 992 int (*fn)(struct device *, void *)); 993 extern struct iommu_group *iommu_group_get(struct device *dev); 994 extern struct iommu_group *iommu_group_ref_get(struct iommu_group *group); 995 extern void iommu_group_put(struct iommu_group *group); 996 997 extern int iommu_group_id(struct iommu_group *group); 998 extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *); 999 1000 int iommu_set_pgtable_quirks(struct iommu_domain *domain, 1001 unsigned long quirks); 1002 1003 void iommu_set_dma_strict(void); 1004 1005 extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev, 1006 unsigned long iova, int flags); 1007 1008 static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) 1009 { 1010 if (domain->ops->flush_iotlb_all) 1011 domain->ops->flush_iotlb_all(domain); 1012 } 1013 1014 static inline void iommu_iotlb_sync(struct iommu_domain *domain, 1015 struct iommu_iotlb_gather *iotlb_gather) 1016 { 1017 if (domain->ops->iotlb_sync && 1018 likely(iotlb_gather->start < iotlb_gather->end)) 1019 domain->ops->iotlb_sync(domain, iotlb_gather); 1020 1021 iommu_iotlb_gather_init(iotlb_gather); 1022 } 1023 1024 /** 1025 * iommu_iotlb_gather_is_disjoint - Checks whether a new range is disjoint 1026 * 1027 * @gather: TLB gather data 1028 * @iova: start of page to invalidate 1029 * @size: size of page to invalidate 1030 * 1031 * Helper for IOMMU drivers to check whether a new range and the gathered range 1032 * are disjoint. For many IOMMUs, flushing the IOMMU in this case is better 1033 * than merging the two, which might lead to unnecessary invalidations. 1034 */ 1035 static inline 1036 bool iommu_iotlb_gather_is_disjoint(struct iommu_iotlb_gather *gather, 1037 unsigned long iova, size_t size) 1038 { 1039 unsigned long start = iova, end = start + size - 1; 1040 1041 return gather->end != 0 && 1042 (end + 1 < gather->start || start > gather->end + 1); 1043 } 1044 1045 1046 /** 1047 * iommu_iotlb_gather_add_range - Gather for address-based TLB invalidation 1048 * @gather: TLB gather data 1049 * @iova: start of page to invalidate 1050 * @size: size of page to invalidate 1051 * 1052 * Helper for IOMMU drivers to build arbitrarily-sized invalidation commands 1053 * where only the address range matters, and simply minimising intermediate 1054 * syncs is preferred. 1055 */ 1056 static inline void iommu_iotlb_gather_add_range(struct iommu_iotlb_gather *gather, 1057 unsigned long iova, size_t size) 1058 { 1059 unsigned long end = iova + size - 1; 1060 1061 if (gather->start > iova) 1062 gather->start = iova; 1063 if (gather->end < end) 1064 gather->end = end; 1065 } 1066 1067 /** 1068 * iommu_iotlb_gather_add_page - Gather for page-based TLB invalidation 1069 * @domain: IOMMU domain to be invalidated 1070 * @gather: TLB gather data 1071 * @iova: start of page to invalidate 1072 * @size: size of page to invalidate 1073 * 1074 * Helper for IOMMU drivers to build invalidation commands based on individual 1075 * pages, or with page size/table level hints which cannot be gathered if they 1076 * differ. 1077 */ 1078 static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain, 1079 struct iommu_iotlb_gather *gather, 1080 unsigned long iova, size_t size) 1081 { 1082 /* 1083 * If the new page is disjoint from the current range or is mapped at 1084 * a different granularity, then sync the TLB so that the gather 1085 * structure can be rewritten. 1086 */ 1087 if ((gather->pgsize && gather->pgsize != size) || 1088 iommu_iotlb_gather_is_disjoint(gather, iova, size)) 1089 iommu_iotlb_sync(domain, gather); 1090 1091 gather->pgsize = size; 1092 iommu_iotlb_gather_add_range(gather, iova, size); 1093 } 1094 1095 static inline bool iommu_iotlb_gather_queued(struct iommu_iotlb_gather *gather) 1096 { 1097 return gather && gather->queued; 1098 } 1099 1100 static inline void iommu_dirty_bitmap_init(struct iommu_dirty_bitmap *dirty, 1101 struct iova_bitmap *bitmap, 1102 struct iommu_iotlb_gather *gather) 1103 { 1104 if (gather) 1105 iommu_iotlb_gather_init(gather); 1106 1107 dirty->bitmap = bitmap; 1108 dirty->gather = gather; 1109 } 1110 1111 static inline void iommu_dirty_bitmap_record(struct iommu_dirty_bitmap *dirty, 1112 unsigned long iova, 1113 unsigned long length) 1114 { 1115 if (dirty->bitmap) 1116 iova_bitmap_set(dirty->bitmap, iova, length); 1117 1118 if (dirty->gather) 1119 iommu_iotlb_gather_add_range(dirty->gather, iova, length); 1120 } 1121 1122 /* PCI device grouping function */ 1123 extern struct iommu_group *pci_device_group(struct device *dev); 1124 /* Generic device grouping function */ 1125 extern struct iommu_group *generic_device_group(struct device *dev); 1126 /* FSL-MC device grouping function */ 1127 struct iommu_group *fsl_mc_device_group(struct device *dev); 1128 extern struct iommu_group *generic_single_device_group(struct device *dev); 1129 1130 /** 1131 * struct iommu_fwspec - per-device IOMMU instance data 1132 * @iommu_fwnode: firmware handle for this device's IOMMU 1133 * @flags: IOMMU_FWSPEC_* flags 1134 * @num_ids: number of associated device IDs 1135 * @ids: IDs which this device may present to the IOMMU 1136 * 1137 * Note that the IDs (and any other information, really) stored in this structure should be 1138 * considered private to the IOMMU device driver and are not to be used directly by IOMMU 1139 * consumers. 1140 */ 1141 struct iommu_fwspec { 1142 struct fwnode_handle *iommu_fwnode; 1143 u32 flags; 1144 unsigned int num_ids; 1145 u32 ids[]; 1146 }; 1147 1148 /* ATS is supported */ 1149 #define IOMMU_FWSPEC_PCI_RC_ATS (1 << 0) 1150 /* CANWBS is supported */ 1151 #define IOMMU_FWSPEC_PCI_RC_CANWBS (1 << 1) 1152 1153 /* 1154 * An iommu attach handle represents a relationship between an iommu domain 1155 * and a PASID or RID of a device. It is allocated and managed by the component 1156 * that manages the domain and is stored in the iommu group during the time the 1157 * domain is attached. 1158 */ 1159 struct iommu_attach_handle { 1160 struct iommu_domain *domain; 1161 }; 1162 1163 /** 1164 * struct iommu_sva - handle to a device-mm bond 1165 */ 1166 struct iommu_sva { 1167 struct iommu_attach_handle handle; 1168 struct device *dev; 1169 refcount_t users; 1170 }; 1171 1172 struct iommu_mm_data { 1173 u32 pasid; 1174 struct mm_struct *mm; 1175 struct list_head sva_domains; 1176 struct list_head mm_list_elm; 1177 }; 1178 1179 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode); 1180 int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids); 1181 1182 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) 1183 { 1184 if (dev->iommu) 1185 return dev->iommu->fwspec; 1186 else 1187 return NULL; 1188 } 1189 1190 static inline void dev_iommu_fwspec_set(struct device *dev, 1191 struct iommu_fwspec *fwspec) 1192 { 1193 dev->iommu->fwspec = fwspec; 1194 } 1195 1196 static inline void *dev_iommu_priv_get(struct device *dev) 1197 { 1198 if (dev->iommu) 1199 return dev->iommu->priv; 1200 else 1201 return NULL; 1202 } 1203 1204 void dev_iommu_priv_set(struct device *dev, void *priv); 1205 1206 extern struct mutex iommu_probe_device_lock; 1207 int iommu_probe_device(struct device *dev); 1208 1209 int iommu_device_use_default_domain(struct device *dev); 1210 void iommu_device_unuse_default_domain(struct device *dev); 1211 1212 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner); 1213 void iommu_group_release_dma_owner(struct iommu_group *group); 1214 bool iommu_group_dma_owner_claimed(struct iommu_group *group); 1215 1216 int iommu_device_claim_dma_owner(struct device *dev, void *owner); 1217 void iommu_device_release_dma_owner(struct device *dev); 1218 1219 int iommu_attach_device_pasid(struct iommu_domain *domain, 1220 struct device *dev, ioasid_t pasid, 1221 struct iommu_attach_handle *handle); 1222 void iommu_detach_device_pasid(struct iommu_domain *domain, 1223 struct device *dev, ioasid_t pasid); 1224 ioasid_t iommu_alloc_global_pasid(struct device *dev); 1225 void iommu_free_global_pasid(ioasid_t pasid); 1226 1227 /* PCI device reset functions */ 1228 int pci_dev_reset_iommu_prepare(struct pci_dev *pdev); 1229 void pci_dev_reset_iommu_done(struct pci_dev *pdev); 1230 #else /* CONFIG_IOMMU_API */ 1231 1232 struct iommu_ops {}; 1233 struct iommu_group {}; 1234 struct iommu_fwspec {}; 1235 struct iommu_device {}; 1236 struct iommu_fault_param {}; 1237 struct iommu_iotlb_gather {}; 1238 struct iommu_dirty_bitmap {}; 1239 struct iommu_dirty_ops {}; 1240 1241 static inline bool device_iommu_capable(struct device *dev, enum iommu_cap cap) 1242 { 1243 return false; 1244 } 1245 1246 static inline struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, 1247 unsigned int flags) 1248 { 1249 return ERR_PTR(-ENODEV); 1250 } 1251 1252 static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev) 1253 { 1254 return ERR_PTR(-ENODEV); 1255 } 1256 1257 static inline void iommu_domain_free(struct iommu_domain *domain) 1258 { 1259 } 1260 1261 static inline int iommu_attach_device(struct iommu_domain *domain, 1262 struct device *dev) 1263 { 1264 return -ENODEV; 1265 } 1266 1267 static inline void iommu_detach_device(struct iommu_domain *domain, 1268 struct device *dev) 1269 { 1270 } 1271 1272 static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) 1273 { 1274 return NULL; 1275 } 1276 1277 static inline int iommu_map(struct iommu_domain *domain, unsigned long iova, 1278 phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 1279 { 1280 return -ENODEV; 1281 } 1282 1283 static inline size_t iommu_unmap(struct iommu_domain *domain, 1284 unsigned long iova, size_t size) 1285 { 1286 return 0; 1287 } 1288 1289 static inline size_t iommu_unmap_fast(struct iommu_domain *domain, 1290 unsigned long iova, int gfp_order, 1291 struct iommu_iotlb_gather *iotlb_gather) 1292 { 1293 return 0; 1294 } 1295 1296 static inline ssize_t iommu_map_sg(struct iommu_domain *domain, 1297 unsigned long iova, struct scatterlist *sg, 1298 unsigned int nents, int prot, gfp_t gfp) 1299 { 1300 return -ENODEV; 1301 } 1302 1303 static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) 1304 { 1305 } 1306 1307 static inline void iommu_iotlb_sync(struct iommu_domain *domain, 1308 struct iommu_iotlb_gather *iotlb_gather) 1309 { 1310 } 1311 1312 static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) 1313 { 1314 return 0; 1315 } 1316 1317 static inline void iommu_set_fault_handler(struct iommu_domain *domain, 1318 iommu_fault_handler_t handler, void *token) 1319 { 1320 } 1321 1322 static inline void iommu_get_resv_regions(struct device *dev, 1323 struct list_head *list) 1324 { 1325 } 1326 1327 static inline void iommu_put_resv_regions(struct device *dev, 1328 struct list_head *list) 1329 { 1330 } 1331 1332 static inline int iommu_get_group_resv_regions(struct iommu_group *group, 1333 struct list_head *head) 1334 { 1335 return -ENODEV; 1336 } 1337 1338 static inline void iommu_set_default_passthrough(bool cmd_line) 1339 { 1340 } 1341 1342 static inline void iommu_set_default_translated(bool cmd_line) 1343 { 1344 } 1345 1346 static inline bool iommu_default_passthrough(void) 1347 { 1348 return true; 1349 } 1350 1351 static inline int iommu_attach_group(struct iommu_domain *domain, 1352 struct iommu_group *group) 1353 { 1354 return -ENODEV; 1355 } 1356 1357 static inline void iommu_detach_group(struct iommu_domain *domain, 1358 struct iommu_group *group) 1359 { 1360 } 1361 1362 static inline struct iommu_group *iommu_group_alloc(void) 1363 { 1364 return ERR_PTR(-ENODEV); 1365 } 1366 1367 static inline void *iommu_group_get_iommudata(struct iommu_group *group) 1368 { 1369 return NULL; 1370 } 1371 1372 static inline void iommu_group_set_iommudata(struct iommu_group *group, 1373 void *iommu_data, 1374 void (*release)(void *iommu_data)) 1375 { 1376 } 1377 1378 static inline int iommu_group_set_name(struct iommu_group *group, 1379 const char *name) 1380 { 1381 return -ENODEV; 1382 } 1383 1384 static inline int iommu_group_add_device(struct iommu_group *group, 1385 struct device *dev) 1386 { 1387 return -ENODEV; 1388 } 1389 1390 static inline void iommu_group_remove_device(struct device *dev) 1391 { 1392 } 1393 1394 static inline int iommu_group_for_each_dev(struct iommu_group *group, 1395 void *data, 1396 int (*fn)(struct device *, void *)) 1397 { 1398 return -ENODEV; 1399 } 1400 1401 static inline struct iommu_group *iommu_group_get(struct device *dev) 1402 { 1403 return NULL; 1404 } 1405 1406 static inline void iommu_group_put(struct iommu_group *group) 1407 { 1408 } 1409 1410 static inline int iommu_group_id(struct iommu_group *group) 1411 { 1412 return -ENODEV; 1413 } 1414 1415 static inline int iommu_set_pgtable_quirks(struct iommu_domain *domain, 1416 unsigned long quirks) 1417 { 1418 return 0; 1419 } 1420 1421 static inline int iommu_device_register(struct iommu_device *iommu, 1422 const struct iommu_ops *ops, 1423 struct device *hwdev) 1424 { 1425 return -ENODEV; 1426 } 1427 1428 static inline struct iommu_device *dev_to_iommu_device(struct device *dev) 1429 { 1430 return NULL; 1431 } 1432 1433 static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather) 1434 { 1435 } 1436 1437 static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain, 1438 struct iommu_iotlb_gather *gather, 1439 unsigned long iova, size_t size) 1440 { 1441 } 1442 1443 static inline bool iommu_iotlb_gather_queued(struct iommu_iotlb_gather *gather) 1444 { 1445 return false; 1446 } 1447 1448 static inline void iommu_dirty_bitmap_init(struct iommu_dirty_bitmap *dirty, 1449 struct iova_bitmap *bitmap, 1450 struct iommu_iotlb_gather *gather) 1451 { 1452 } 1453 1454 static inline void iommu_dirty_bitmap_record(struct iommu_dirty_bitmap *dirty, 1455 unsigned long iova, 1456 unsigned long length) 1457 { 1458 } 1459 1460 static inline void iommu_device_unregister(struct iommu_device *iommu) 1461 { 1462 } 1463 1464 static inline int iommu_device_sysfs_add(struct iommu_device *iommu, 1465 struct device *parent, 1466 const struct attribute_group **groups, 1467 const char *fmt, ...) 1468 { 1469 return -ENODEV; 1470 } 1471 1472 static inline void iommu_device_sysfs_remove(struct iommu_device *iommu) 1473 { 1474 } 1475 1476 static inline int iommu_device_link(struct device *dev, struct device *link) 1477 { 1478 return -EINVAL; 1479 } 1480 1481 static inline void iommu_device_unlink(struct device *dev, struct device *link) 1482 { 1483 } 1484 1485 static inline int iommu_fwspec_init(struct device *dev, 1486 struct fwnode_handle *iommu_fwnode) 1487 { 1488 return -ENODEV; 1489 } 1490 1491 static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids, 1492 int num_ids) 1493 { 1494 return -ENODEV; 1495 } 1496 1497 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) 1498 { 1499 return NULL; 1500 } 1501 1502 static inline int iommu_device_use_default_domain(struct device *dev) 1503 { 1504 return 0; 1505 } 1506 1507 static inline void iommu_device_unuse_default_domain(struct device *dev) 1508 { 1509 } 1510 1511 static inline int 1512 iommu_group_claim_dma_owner(struct iommu_group *group, void *owner) 1513 { 1514 return -ENODEV; 1515 } 1516 1517 static inline void iommu_group_release_dma_owner(struct iommu_group *group) 1518 { 1519 } 1520 1521 static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group) 1522 { 1523 return false; 1524 } 1525 1526 static inline void iommu_device_release_dma_owner(struct device *dev) 1527 { 1528 } 1529 1530 static inline int iommu_device_claim_dma_owner(struct device *dev, void *owner) 1531 { 1532 return -ENODEV; 1533 } 1534 1535 static inline int iommu_attach_device_pasid(struct iommu_domain *domain, 1536 struct device *dev, ioasid_t pasid, 1537 struct iommu_attach_handle *handle) 1538 { 1539 return -ENODEV; 1540 } 1541 1542 static inline void iommu_detach_device_pasid(struct iommu_domain *domain, 1543 struct device *dev, ioasid_t pasid) 1544 { 1545 } 1546 1547 static inline ioasid_t iommu_alloc_global_pasid(struct device *dev) 1548 { 1549 return IOMMU_PASID_INVALID; 1550 } 1551 1552 static inline void iommu_free_global_pasid(ioasid_t pasid) {} 1553 1554 static inline int pci_dev_reset_iommu_prepare(struct pci_dev *pdev) 1555 { 1556 return 0; 1557 } 1558 1559 static inline void pci_dev_reset_iommu_done(struct pci_dev *pdev) 1560 { 1561 } 1562 #endif /* CONFIG_IOMMU_API */ 1563 1564 #ifdef CONFIG_IRQ_MSI_IOMMU 1565 #ifdef CONFIG_IOMMU_API 1566 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr); 1567 #else 1568 static inline int iommu_dma_prepare_msi(struct msi_desc *desc, 1569 phys_addr_t msi_addr) 1570 { 1571 return 0; 1572 } 1573 #endif /* CONFIG_IOMMU_API */ 1574 #endif /* CONFIG_IRQ_MSI_IOMMU */ 1575 1576 #if IS_ENABLED(CONFIG_LOCKDEP) && IS_ENABLED(CONFIG_IOMMU_API) 1577 void iommu_group_mutex_assert(struct device *dev); 1578 #else 1579 static inline void iommu_group_mutex_assert(struct device *dev) 1580 { 1581 } 1582 #endif 1583 1584 /** 1585 * iommu_map_sgtable - Map the given buffer to the IOMMU domain 1586 * @domain: The IOMMU domain to perform the mapping 1587 * @iova: The start address to map the buffer 1588 * @sgt: The sg_table object describing the buffer 1589 * @prot: IOMMU protection bits 1590 * 1591 * Creates a mapping at @iova for the buffer described by a scatterlist 1592 * stored in the given sg_table object in the provided IOMMU domain. 1593 */ 1594 static inline ssize_t iommu_map_sgtable(struct iommu_domain *domain, 1595 unsigned long iova, struct sg_table *sgt, int prot) 1596 { 1597 return iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, prot, 1598 GFP_KERNEL); 1599 } 1600 1601 #ifdef CONFIG_IOMMU_DEBUGFS 1602 extern struct dentry *iommu_debugfs_dir; 1603 void iommu_debugfs_setup(void); 1604 #else 1605 static inline void iommu_debugfs_setup(void) {} 1606 #endif 1607 1608 #ifdef CONFIG_IOMMU_DMA 1609 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); 1610 #else /* CONFIG_IOMMU_DMA */ 1611 static inline int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) 1612 { 1613 return -ENODEV; 1614 } 1615 #endif /* CONFIG_IOMMU_DMA */ 1616 1617 /* 1618 * Newer generations of Tegra SoCs require devices' stream IDs to be directly programmed into 1619 * some registers. These are always paired with a Tegra SMMU or ARM SMMU, for which the contents 1620 * of the struct iommu_fwspec are known. Use this helper to formalize access to these internals. 1621 */ 1622 #define TEGRA_STREAM_ID_BYPASS 0x7f 1623 1624 static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream_id) 1625 { 1626 #ifdef CONFIG_IOMMU_API 1627 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1628 1629 if (fwspec && fwspec->num_ids == 1) { 1630 *stream_id = fwspec->ids[0] & 0xffff; 1631 return true; 1632 } 1633 #endif 1634 1635 return false; 1636 } 1637 1638 #ifdef CONFIG_IOMMU_MM_DATA 1639 static inline void mm_pasid_init(struct mm_struct *mm) 1640 { 1641 /* 1642 * During dup_mm(), a new mm will be memcpy'd from an old one and that makes 1643 * the new mm and the old one point to a same iommu_mm instance. When either 1644 * one of the two mms gets released, the iommu_mm instance is freed, leaving 1645 * the other mm running into a use-after-free/double-free problem. To avoid 1646 * the problem, zeroing the iommu_mm pointer of a new mm is needed here. 1647 */ 1648 mm->iommu_mm = NULL; 1649 } 1650 1651 static inline bool mm_valid_pasid(struct mm_struct *mm) 1652 { 1653 return READ_ONCE(mm->iommu_mm); 1654 } 1655 1656 static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm) 1657 { 1658 struct iommu_mm_data *iommu_mm = READ_ONCE(mm->iommu_mm); 1659 1660 if (!iommu_mm) 1661 return IOMMU_PASID_INVALID; 1662 return iommu_mm->pasid; 1663 } 1664 1665 void mm_pasid_drop(struct mm_struct *mm); 1666 struct iommu_sva *iommu_sva_bind_device(struct device *dev, 1667 struct mm_struct *mm); 1668 void iommu_sva_unbind_device(struct iommu_sva *handle); 1669 u32 iommu_sva_get_pasid(struct iommu_sva *handle); 1670 void iommu_sva_invalidate_kva_range(unsigned long start, unsigned long end); 1671 #else 1672 static inline struct iommu_sva * 1673 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm) 1674 { 1675 return ERR_PTR(-ENODEV); 1676 } 1677 1678 static inline void iommu_sva_unbind_device(struct iommu_sva *handle) 1679 { 1680 } 1681 1682 static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle) 1683 { 1684 return IOMMU_PASID_INVALID; 1685 } 1686 static inline void mm_pasid_init(struct mm_struct *mm) {} 1687 static inline bool mm_valid_pasid(struct mm_struct *mm) { return false; } 1688 1689 static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm) 1690 { 1691 return IOMMU_PASID_INVALID; 1692 } 1693 1694 static inline void mm_pasid_drop(struct mm_struct *mm) {} 1695 static inline void iommu_sva_invalidate_kva_range(unsigned long start, unsigned long end) {} 1696 #endif /* CONFIG_IOMMU_SVA */ 1697 1698 #ifdef CONFIG_IOMMU_IOPF 1699 int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev); 1700 void iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev); 1701 int iopf_queue_flush_dev(struct device *dev); 1702 struct iopf_queue *iopf_queue_alloc(const char *name); 1703 void iopf_queue_free(struct iopf_queue *queue); 1704 int iopf_queue_discard_partial(struct iopf_queue *queue); 1705 void iopf_free_group(struct iopf_group *group); 1706 int iommu_report_device_fault(struct device *dev, struct iopf_fault *evt); 1707 void iopf_group_response(struct iopf_group *group, 1708 enum iommu_page_response_code status); 1709 void iopf_group_dequeue(struct iopf_group *group); 1710 #else 1711 static inline int 1712 iopf_queue_add_device(struct iopf_queue *queue, struct device *dev) 1713 { 1714 return -ENODEV; 1715 } 1716 1717 static inline void 1718 iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) 1719 { 1720 } 1721 1722 static inline int iopf_queue_flush_dev(struct device *dev) 1723 { 1724 return -ENODEV; 1725 } 1726 1727 static inline struct iopf_queue *iopf_queue_alloc(const char *name) 1728 { 1729 return NULL; 1730 } 1731 1732 static inline void iopf_queue_free(struct iopf_queue *queue) 1733 { 1734 } 1735 1736 static inline int iopf_queue_discard_partial(struct iopf_queue *queue) 1737 { 1738 return -ENODEV; 1739 } 1740 1741 static inline void iopf_free_group(struct iopf_group *group) 1742 { 1743 } 1744 1745 static inline int 1746 iommu_report_device_fault(struct device *dev, struct iopf_fault *evt) 1747 { 1748 return -ENODEV; 1749 } 1750 1751 static inline void iopf_group_response(struct iopf_group *group, 1752 enum iommu_page_response_code status) 1753 { 1754 } 1755 1756 static inline void iopf_group_dequeue(struct iopf_group *group) 1757 { 1758 } 1759 #endif /* CONFIG_IOMMU_IOPF */ 1760 #endif /* __LINUX_IOMMU_H */ 1761