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