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