iommu: Add probe_device() and release_device() call-backs
[linux-2.6-microblaze.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 <joerg.roedel@amd.com>
5  */
6
7 #ifndef __LINUX_IOMMU_H
8 #define __LINUX_IOMMU_H
9
10 #include <linux/scatterlist.h>
11 #include <linux/device.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/of.h>
16 #include <linux/ioasid.h>
17 #include <uapi/linux/iommu.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  * Non-coherent masters can use this page protection flag to set cacheable
36  * memory attributes for only a transparent outer level of cache, also known as
37  * the last-level or system cache.
38  */
39 #define IOMMU_SYS_CACHE_ONLY    (1 << 6)
40
41 struct iommu_ops;
42 struct iommu_group;
43 struct bus_type;
44 struct device;
45 struct iommu_domain;
46 struct notifier_block;
47 struct iommu_sva;
48 struct iommu_fault_event;
49
50 /* iommu fault flags */
51 #define IOMMU_FAULT_READ        0x0
52 #define IOMMU_FAULT_WRITE       0x1
53
54 typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
55                         struct device *, unsigned long, int, void *);
56 typedef int (*iommu_mm_exit_handler_t)(struct device *dev, struct iommu_sva *,
57                                        void *);
58 typedef int (*iommu_dev_fault_handler_t)(struct iommu_fault *, void *);
59
60 struct iommu_domain_geometry {
61         dma_addr_t aperture_start; /* First address that can be mapped    */
62         dma_addr_t aperture_end;   /* Last address that can be mapped     */
63         bool force_aperture;       /* DMA only allowed in mappable range? */
64 };
65
66 /* Domain feature flags */
67 #define __IOMMU_DOMAIN_PAGING   (1U << 0)  /* Support for iommu_map/unmap */
68 #define __IOMMU_DOMAIN_DMA_API  (1U << 1)  /* Domain for use in DMA-API
69                                               implementation              */
70 #define __IOMMU_DOMAIN_PT       (1U << 2)  /* Domain is identity mapped   */
71
72 /*
73  * This are the possible domain-types
74  *
75  *      IOMMU_DOMAIN_BLOCKED    - All DMA is blocked, can be used to isolate
76  *                                devices
77  *      IOMMU_DOMAIN_IDENTITY   - DMA addresses are system physical addresses
78  *      IOMMU_DOMAIN_UNMANAGED  - DMA mappings managed by IOMMU-API user, used
79  *                                for VMs
80  *      IOMMU_DOMAIN_DMA        - Internally used for DMA-API implementations.
81  *                                This flag allows IOMMU drivers to implement
82  *                                certain optimizations for these domains
83  */
84 #define IOMMU_DOMAIN_BLOCKED    (0U)
85 #define IOMMU_DOMAIN_IDENTITY   (__IOMMU_DOMAIN_PT)
86 #define IOMMU_DOMAIN_UNMANAGED  (__IOMMU_DOMAIN_PAGING)
87 #define IOMMU_DOMAIN_DMA        (__IOMMU_DOMAIN_PAGING |        \
88                                  __IOMMU_DOMAIN_DMA_API)
89
90 struct iommu_domain {
91         unsigned type;
92         const struct iommu_ops *ops;
93         unsigned long pgsize_bitmap;    /* Bitmap of page sizes in use */
94         iommu_fault_handler_t handler;
95         void *handler_token;
96         struct iommu_domain_geometry geometry;
97         void *iova_cookie;
98 };
99
100 enum iommu_cap {
101         IOMMU_CAP_CACHE_COHERENCY,      /* IOMMU can enforce cache coherent DMA
102                                            transactions */
103         IOMMU_CAP_INTR_REMAP,           /* IOMMU supports interrupt isolation */
104         IOMMU_CAP_NOEXEC,               /* IOMMU_NOEXEC flag */
105 };
106
107 /*
108  * Following constraints are specifc to FSL_PAMUV1:
109  *  -aperture must be power of 2, and naturally aligned
110  *  -number of windows must be power of 2, and address space size
111  *   of each window is determined by aperture size / # of windows
112  *  -the actual size of the mapped region of a window must be power
113  *   of 2 starting with 4KB and physical address must be naturally
114  *   aligned.
115  * DOMAIN_ATTR_FSL_PAMUV1 corresponds to the above mentioned contraints.
116  * The caller can invoke iommu_domain_get_attr to check if the underlying
117  * iommu implementation supports these constraints.
118  */
119
120 enum iommu_attr {
121         DOMAIN_ATTR_GEOMETRY,
122         DOMAIN_ATTR_PAGING,
123         DOMAIN_ATTR_WINDOWS,
124         DOMAIN_ATTR_FSL_PAMU_STASH,
125         DOMAIN_ATTR_FSL_PAMU_ENABLE,
126         DOMAIN_ATTR_FSL_PAMUV1,
127         DOMAIN_ATTR_NESTING,    /* two stages of translation */
128         DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
129         DOMAIN_ATTR_MAX,
130 };
131
132 /* These are the possible reserved region types */
133 enum iommu_resv_type {
134         /* Memory regions which must be mapped 1:1 at all times */
135         IOMMU_RESV_DIRECT,
136         /*
137          * Memory regions which are advertised to be 1:1 but are
138          * commonly considered relaxable in some conditions,
139          * for instance in device assignment use case (USB, Graphics)
140          */
141         IOMMU_RESV_DIRECT_RELAXABLE,
142         /* Arbitrary "never map this or give it to a device" address ranges */
143         IOMMU_RESV_RESERVED,
144         /* Hardware MSI region (untranslated) */
145         IOMMU_RESV_MSI,
146         /* Software-managed MSI translation window */
147         IOMMU_RESV_SW_MSI,
148 };
149
150 /**
151  * struct iommu_resv_region - descriptor for a reserved memory region
152  * @list: Linked list pointers
153  * @start: System physical start address of the region
154  * @length: Length of the region in bytes
155  * @prot: IOMMU Protection flags (READ/WRITE/...)
156  * @type: Type of the reserved region
157  */
158 struct iommu_resv_region {
159         struct list_head        list;
160         phys_addr_t             start;
161         size_t                  length;
162         int                     prot;
163         enum iommu_resv_type    type;
164 };
165
166 /* Per device IOMMU features */
167 enum iommu_dev_features {
168         IOMMU_DEV_FEAT_AUX,     /* Aux-domain feature */
169         IOMMU_DEV_FEAT_SVA,     /* Shared Virtual Addresses */
170 };
171
172 #define IOMMU_PASID_INVALID     (-1U)
173
174 /**
175  * struct iommu_sva_ops - device driver callbacks for an SVA context
176  *
177  * @mm_exit: called when the mm is about to be torn down by exit_mmap. After
178  *           @mm_exit returns, the device must not issue any more transaction
179  *           with the PASID given as argument.
180  *
181  *           The @mm_exit handler is allowed to sleep. Be careful about the
182  *           locks taken in @mm_exit, because they might lead to deadlocks if
183  *           they are also held when dropping references to the mm. Consider the
184  *           following call chain:
185  *           mutex_lock(A); mmput(mm) -> exit_mm() -> @mm_exit() -> mutex_lock(A)
186  *           Using mmput_async() prevents this scenario.
187  *
188  */
189 struct iommu_sva_ops {
190         iommu_mm_exit_handler_t mm_exit;
191 };
192
193 #ifdef CONFIG_IOMMU_API
194
195 /**
196  * struct iommu_iotlb_gather - Range information for a pending IOTLB flush
197  *
198  * @start: IOVA representing the start of the range to be flushed
199  * @end: IOVA representing the end of the range to be flushed (exclusive)
200  * @pgsize: The interval at which to perform the flush
201  *
202  * This structure is intended to be updated by multiple calls to the
203  * ->unmap() function in struct iommu_ops before eventually being passed
204  * into ->iotlb_sync().
205  */
206 struct iommu_iotlb_gather {
207         unsigned long           start;
208         unsigned long           end;
209         size_t                  pgsize;
210 };
211
212 /**
213  * struct iommu_ops - iommu ops and capabilities
214  * @capable: check capability
215  * @domain_alloc: allocate iommu domain
216  * @domain_free: free iommu domain
217  * @attach_dev: attach device to an iommu domain
218  * @detach_dev: detach device from an iommu domain
219  * @map: map a physically contiguous memory region to an iommu domain
220  * @unmap: unmap a physically contiguous memory region from an iommu domain
221  * @flush_iotlb_all: Synchronously flush all hardware TLBs for this domain
222  * @iotlb_sync_map: Sync mappings created recently using @map to the hardware
223  * @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush
224  *            queue
225  * @iova_to_phys: translate iova to physical address
226  * @add_device: add device to iommu grouping
227  * @remove_device: remove device from iommu grouping
228  * @probe_device: Add device to iommu driver handling
229  * @release_device: Remove device from iommu driver handling
230  * @probe_finalize: Do final setup work after the device is added to an IOMMU
231  *                  group and attached to the groups domain
232  * @device_group: find iommu group for a particular device
233  * @domain_get_attr: Query domain attributes
234  * @domain_set_attr: Change domain attributes
235  * @get_resv_regions: Request list of reserved regions for a device
236  * @put_resv_regions: Free list of reserved regions for a device
237  * @apply_resv_region: Temporary helper call-back for iova reserved ranges
238  * @domain_window_enable: Configure and enable a particular window for a domain
239  * @domain_window_disable: Disable a particular window for a domain
240  * @of_xlate: add OF master IDs to iommu grouping
241  * @is_attach_deferred: Check if domain attach should be deferred from iommu
242  *                      driver init to device driver init (default no)
243  * @dev_has/enable/disable_feat: per device entries to check/enable/disable
244  *                               iommu specific features.
245  * @dev_feat_enabled: check enabled feature
246  * @aux_attach/detach_dev: aux-domain specific attach/detach entries.
247  * @aux_get_pasid: get the pasid given an aux-domain
248  * @sva_bind: Bind process address space to device
249  * @sva_unbind: Unbind process address space from device
250  * @sva_get_pasid: Get PASID associated to a SVA handle
251  * @page_response: handle page request response
252  * @cache_invalidate: invalidate translation caches
253  * @sva_bind_gpasid: bind guest pasid and mm
254  * @sva_unbind_gpasid: unbind guest pasid and mm
255  * @def_domain_type: device default domain type, return value:
256  *              - IOMMU_DOMAIN_IDENTITY: must use an identity domain
257  *              - IOMMU_DOMAIN_DMA: must use a dma domain
258  *              - 0: use the default setting
259  * @pgsize_bitmap: bitmap of all possible supported page sizes
260  * @owner: Driver module providing these ops
261  */
262 struct iommu_ops {
263         bool (*capable)(enum iommu_cap);
264
265         /* Domain allocation and freeing by the iommu driver */
266         struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
267         void (*domain_free)(struct iommu_domain *);
268
269         int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
270         void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
271         int (*map)(struct iommu_domain *domain, unsigned long iova,
272                    phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
273         size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
274                      size_t size, struct iommu_iotlb_gather *iotlb_gather);
275         void (*flush_iotlb_all)(struct iommu_domain *domain);
276         void (*iotlb_sync_map)(struct iommu_domain *domain);
277         void (*iotlb_sync)(struct iommu_domain *domain,
278                            struct iommu_iotlb_gather *iotlb_gather);
279         phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
280         int (*add_device)(struct device *dev);
281         void (*remove_device)(struct device *dev);
282         struct iommu_device *(*probe_device)(struct device *dev);
283         void (*release_device)(struct device *dev);
284         void (*probe_finalize)(struct device *dev);
285         struct iommu_group *(*device_group)(struct device *dev);
286         int (*domain_get_attr)(struct iommu_domain *domain,
287                                enum iommu_attr attr, void *data);
288         int (*domain_set_attr)(struct iommu_domain *domain,
289                                enum iommu_attr attr, void *data);
290
291         /* Request/Free a list of reserved regions for a device */
292         void (*get_resv_regions)(struct device *dev, struct list_head *list);
293         void (*put_resv_regions)(struct device *dev, struct list_head *list);
294         void (*apply_resv_region)(struct device *dev,
295                                   struct iommu_domain *domain,
296                                   struct iommu_resv_region *region);
297
298         /* Window handling functions */
299         int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
300                                     phys_addr_t paddr, u64 size, int prot);
301         void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
302
303         int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
304         bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev);
305
306         /* Per device IOMMU features */
307         bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);
308         bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);
309         int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
310         int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
311
312         /* Aux-domain specific attach/detach entries */
313         int (*aux_attach_dev)(struct iommu_domain *domain, struct device *dev);
314         void (*aux_detach_dev)(struct iommu_domain *domain, struct device *dev);
315         int (*aux_get_pasid)(struct iommu_domain *domain, struct device *dev);
316
317         struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,
318                                       void *drvdata);
319         void (*sva_unbind)(struct iommu_sva *handle);
320         int (*sva_get_pasid)(struct iommu_sva *handle);
321
322         int (*page_response)(struct device *dev,
323                              struct iommu_fault_event *evt,
324                              struct iommu_page_response *msg);
325         int (*cache_invalidate)(struct iommu_domain *domain, struct device *dev,
326                                 struct iommu_cache_invalidate_info *inv_info);
327         int (*sva_bind_gpasid)(struct iommu_domain *domain,
328                         struct device *dev, struct iommu_gpasid_bind_data *data);
329
330         int (*sva_unbind_gpasid)(struct device *dev, int pasid);
331
332         int (*def_domain_type)(struct device *dev);
333
334         unsigned long pgsize_bitmap;
335         struct module *owner;
336 };
337
338 /**
339  * struct iommu_device - IOMMU core representation of one IOMMU hardware
340  *                       instance
341  * @list: Used by the iommu-core to keep a list of registered iommus
342  * @ops: iommu-ops for talking to this iommu
343  * @dev: struct device for sysfs handling
344  */
345 struct iommu_device {
346         struct list_head list;
347         const struct iommu_ops *ops;
348         struct fwnode_handle *fwnode;
349         struct device *dev;
350 };
351
352 /**
353  * struct iommu_fault_event - Generic fault event
354  *
355  * Can represent recoverable faults such as a page requests or
356  * unrecoverable faults such as DMA or IRQ remapping faults.
357  *
358  * @fault: fault descriptor
359  * @list: pending fault event list, used for tracking responses
360  */
361 struct iommu_fault_event {
362         struct iommu_fault fault;
363         struct list_head list;
364 };
365
366 /**
367  * struct iommu_fault_param - per-device IOMMU fault data
368  * @handler: Callback function to handle IOMMU faults at device level
369  * @data: handler private data
370  * @faults: holds the pending faults which needs response
371  * @lock: protect pending faults list
372  */
373 struct iommu_fault_param {
374         iommu_dev_fault_handler_t handler;
375         void *data;
376         struct list_head faults;
377         struct mutex lock;
378 };
379
380 /**
381  * struct dev_iommu - Collection of per-device IOMMU data
382  *
383  * @fault_param: IOMMU detected device fault reporting data
384  * @fwspec:      IOMMU fwspec data
385  * @iommu_dev:   IOMMU device this device is linked to
386  * @priv:        IOMMU Driver private data
387  *
388  * TODO: migrate other per device data pointers under iommu_dev_data, e.g.
389  *      struct iommu_group      *iommu_group;
390  */
391 struct dev_iommu {
392         struct mutex lock;
393         struct iommu_fault_param        *fault_param;
394         struct iommu_fwspec             *fwspec;
395         struct iommu_device             *iommu_dev;
396         void                            *priv;
397 };
398
399 int  iommu_device_register(struct iommu_device *iommu);
400 void iommu_device_unregister(struct iommu_device *iommu);
401 int  iommu_device_sysfs_add(struct iommu_device *iommu,
402                             struct device *parent,
403                             const struct attribute_group **groups,
404                             const char *fmt, ...) __printf(4, 5);
405 void iommu_device_sysfs_remove(struct iommu_device *iommu);
406 int  iommu_device_link(struct iommu_device   *iommu, struct device *link);
407 void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
408
409 static inline void __iommu_device_set_ops(struct iommu_device *iommu,
410                                           const struct iommu_ops *ops)
411 {
412         iommu->ops = ops;
413 }
414
415 #define iommu_device_set_ops(iommu, ops)                                \
416 do {                                                                    \
417         struct iommu_ops *__ops = (struct iommu_ops *)(ops);            \
418         __ops->owner = THIS_MODULE;                                     \
419         __iommu_device_set_ops(iommu, __ops);                           \
420 } while (0)
421
422 static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
423                                            struct fwnode_handle *fwnode)
424 {
425         iommu->fwnode = fwnode;
426 }
427
428 static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
429 {
430         return (struct iommu_device *)dev_get_drvdata(dev);
431 }
432
433 static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
434 {
435         *gather = (struct iommu_iotlb_gather) {
436                 .start  = ULONG_MAX,
437         };
438 }
439
440 #define IOMMU_GROUP_NOTIFY_ADD_DEVICE           1 /* Device added */
441 #define IOMMU_GROUP_NOTIFY_DEL_DEVICE           2 /* Pre Device removed */
442 #define IOMMU_GROUP_NOTIFY_BIND_DRIVER          3 /* Pre Driver bind */
443 #define IOMMU_GROUP_NOTIFY_BOUND_DRIVER         4 /* Post Driver bind */
444 #define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER        5 /* Pre Driver unbind */
445 #define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER       6 /* Post Driver unbind */
446
447 extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
448 extern bool iommu_present(struct bus_type *bus);
449 extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
450 extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
451 extern struct iommu_group *iommu_group_get_by_id(int id);
452 extern void iommu_domain_free(struct iommu_domain *domain);
453 extern int iommu_attach_device(struct iommu_domain *domain,
454                                struct device *dev);
455 extern void iommu_detach_device(struct iommu_domain *domain,
456                                 struct device *dev);
457 extern int iommu_cache_invalidate(struct iommu_domain *domain,
458                                   struct device *dev,
459                                   struct iommu_cache_invalidate_info *inv_info);
460 extern int iommu_sva_bind_gpasid(struct iommu_domain *domain,
461                 struct device *dev, struct iommu_gpasid_bind_data *data);
462 extern int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
463                                 struct device *dev, ioasid_t pasid);
464 extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
465 extern struct iommu_domain *iommu_get_dma_domain(struct device *dev);
466 extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
467                      phys_addr_t paddr, size_t size, int prot);
468 extern int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova,
469                             phys_addr_t paddr, size_t size, int prot);
470 extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
471                           size_t size);
472 extern size_t iommu_unmap_fast(struct iommu_domain *domain,
473                                unsigned long iova, size_t size,
474                                struct iommu_iotlb_gather *iotlb_gather);
475 extern size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
476                            struct scatterlist *sg,unsigned int nents, int prot);
477 extern size_t iommu_map_sg_atomic(struct iommu_domain *domain,
478                                   unsigned long iova, struct scatterlist *sg,
479                                   unsigned int nents, int prot);
480 extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
481 extern void iommu_set_fault_handler(struct iommu_domain *domain,
482                         iommu_fault_handler_t handler, void *token);
483
484 extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
485 extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
486 extern void generic_iommu_put_resv_regions(struct device *dev,
487                                            struct list_head *list);
488 extern int iommu_request_dm_for_dev(struct device *dev);
489 extern int iommu_request_dma_domain_for_dev(struct device *dev);
490 extern void iommu_set_default_passthrough(bool cmd_line);
491 extern void iommu_set_default_translated(bool cmd_line);
492 extern bool iommu_default_passthrough(void);
493 extern struct iommu_resv_region *
494 iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot,
495                         enum iommu_resv_type type);
496 extern int iommu_get_group_resv_regions(struct iommu_group *group,
497                                         struct list_head *head);
498
499 extern int iommu_attach_group(struct iommu_domain *domain,
500                               struct iommu_group *group);
501 extern void iommu_detach_group(struct iommu_domain *domain,
502                                struct iommu_group *group);
503 extern struct iommu_group *iommu_group_alloc(void);
504 extern void *iommu_group_get_iommudata(struct iommu_group *group);
505 extern void iommu_group_set_iommudata(struct iommu_group *group,
506                                       void *iommu_data,
507                                       void (*release)(void *iommu_data));
508 extern int iommu_group_set_name(struct iommu_group *group, const char *name);
509 extern int iommu_group_add_device(struct iommu_group *group,
510                                   struct device *dev);
511 extern void iommu_group_remove_device(struct device *dev);
512 extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
513                                     int (*fn)(struct device *, void *));
514 extern struct iommu_group *iommu_group_get(struct device *dev);
515 extern struct iommu_group *iommu_group_ref_get(struct iommu_group *group);
516 extern void iommu_group_put(struct iommu_group *group);
517 extern int iommu_group_register_notifier(struct iommu_group *group,
518                                          struct notifier_block *nb);
519 extern int iommu_group_unregister_notifier(struct iommu_group *group,
520                                            struct notifier_block *nb);
521 extern int iommu_register_device_fault_handler(struct device *dev,
522                                         iommu_dev_fault_handler_t handler,
523                                         void *data);
524
525 extern int iommu_unregister_device_fault_handler(struct device *dev);
526
527 extern int iommu_report_device_fault(struct device *dev,
528                                      struct iommu_fault_event *evt);
529 extern int iommu_page_response(struct device *dev,
530                                struct iommu_page_response *msg);
531
532 extern int iommu_group_id(struct iommu_group *group);
533 extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
534 extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
535
536 extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
537                                  void *data);
538 extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
539                                  void *data);
540
541 /* Window handling function prototypes */
542 extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
543                                       phys_addr_t offset, u64 size,
544                                       int prot);
545 extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
546
547 extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
548                               unsigned long iova, int flags);
549
550 static inline void iommu_flush_tlb_all(struct iommu_domain *domain)
551 {
552         if (domain->ops->flush_iotlb_all)
553                 domain->ops->flush_iotlb_all(domain);
554 }
555
556 static inline void iommu_tlb_sync(struct iommu_domain *domain,
557                                   struct iommu_iotlb_gather *iotlb_gather)
558 {
559         if (domain->ops->iotlb_sync)
560                 domain->ops->iotlb_sync(domain, iotlb_gather);
561
562         iommu_iotlb_gather_init(iotlb_gather);
563 }
564
565 static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain,
566                                                struct iommu_iotlb_gather *gather,
567                                                unsigned long iova, size_t size)
568 {
569         unsigned long start = iova, end = start + size;
570
571         /*
572          * If the new page is disjoint from the current range or is mapped at
573          * a different granularity, then sync the TLB so that the gather
574          * structure can be rewritten.
575          */
576         if (gather->pgsize != size ||
577             end < gather->start || start > gather->end) {
578                 if (gather->pgsize)
579                         iommu_tlb_sync(domain, gather);
580                 gather->pgsize = size;
581         }
582
583         if (gather->end < end)
584                 gather->end = end;
585
586         if (gather->start > start)
587                 gather->start = start;
588 }
589
590 /* PCI device grouping function */
591 extern struct iommu_group *pci_device_group(struct device *dev);
592 /* Generic device grouping function */
593 extern struct iommu_group *generic_device_group(struct device *dev);
594 /* FSL-MC device grouping function */
595 struct iommu_group *fsl_mc_device_group(struct device *dev);
596
597 /**
598  * struct iommu_fwspec - per-device IOMMU instance data
599  * @ops: ops for this device's IOMMU
600  * @iommu_fwnode: firmware handle for this device's IOMMU
601  * @iommu_priv: IOMMU driver private data for this device
602  * @num_pasid_bits: number of PASID bits supported by this device
603  * @num_ids: number of associated device IDs
604  * @ids: IDs which this device may present to the IOMMU
605  */
606 struct iommu_fwspec {
607         const struct iommu_ops  *ops;
608         struct fwnode_handle    *iommu_fwnode;
609         u32                     flags;
610         u32                     num_pasid_bits;
611         unsigned int            num_ids;
612         u32                     ids[];
613 };
614
615 /* ATS is supported */
616 #define IOMMU_FWSPEC_PCI_RC_ATS                 (1 << 0)
617
618 /**
619  * struct iommu_sva - handle to a device-mm bond
620  */
621 struct iommu_sva {
622         struct device                   *dev;
623         const struct iommu_sva_ops      *ops;
624 };
625
626 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
627                       const struct iommu_ops *ops);
628 void iommu_fwspec_free(struct device *dev);
629 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
630 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);
631
632 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
633 {
634         if (dev->iommu)
635                 return dev->iommu->fwspec;
636         else
637                 return NULL;
638 }
639
640 static inline void dev_iommu_fwspec_set(struct device *dev,
641                                         struct iommu_fwspec *fwspec)
642 {
643         dev->iommu->fwspec = fwspec;
644 }
645
646 static inline void *dev_iommu_priv_get(struct device *dev)
647 {
648         return dev->iommu->priv;
649 }
650
651 static inline void dev_iommu_priv_set(struct device *dev, void *priv)
652 {
653         dev->iommu->priv = priv;
654 }
655
656 int iommu_probe_device(struct device *dev);
657 void iommu_release_device(struct device *dev);
658
659 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features f);
660 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
661 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
662 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);
663 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev);
664 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev);
665 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev);
666
667 struct iommu_sva *iommu_sva_bind_device(struct device *dev,
668                                         struct mm_struct *mm,
669                                         void *drvdata);
670 void iommu_sva_unbind_device(struct iommu_sva *handle);
671 int iommu_sva_set_ops(struct iommu_sva *handle,
672                       const struct iommu_sva_ops *ops);
673 int iommu_sva_get_pasid(struct iommu_sva *handle);
674
675 #else /* CONFIG_IOMMU_API */
676
677 struct iommu_ops {};
678 struct iommu_group {};
679 struct iommu_fwspec {};
680 struct iommu_device {};
681 struct iommu_fault_param {};
682 struct iommu_iotlb_gather {};
683
684 static inline bool iommu_present(struct bus_type *bus)
685 {
686         return false;
687 }
688
689 static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
690 {
691         return false;
692 }
693
694 static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
695 {
696         return NULL;
697 }
698
699 static inline struct iommu_group *iommu_group_get_by_id(int id)
700 {
701         return NULL;
702 }
703
704 static inline void iommu_domain_free(struct iommu_domain *domain)
705 {
706 }
707
708 static inline int iommu_attach_device(struct iommu_domain *domain,
709                                       struct device *dev)
710 {
711         return -ENODEV;
712 }
713
714 static inline void iommu_detach_device(struct iommu_domain *domain,
715                                        struct device *dev)
716 {
717 }
718
719 static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
720 {
721         return NULL;
722 }
723
724 static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
725                             phys_addr_t paddr, size_t size, int prot)
726 {
727         return -ENODEV;
728 }
729
730 static inline int iommu_map_atomic(struct iommu_domain *domain,
731                                    unsigned long iova, phys_addr_t paddr,
732                                    size_t size, int prot)
733 {
734         return -ENODEV;
735 }
736
737 static inline size_t iommu_unmap(struct iommu_domain *domain,
738                                  unsigned long iova, size_t size)
739 {
740         return 0;
741 }
742
743 static inline size_t iommu_unmap_fast(struct iommu_domain *domain,
744                                       unsigned long iova, int gfp_order,
745                                       struct iommu_iotlb_gather *iotlb_gather)
746 {
747         return 0;
748 }
749
750 static inline size_t iommu_map_sg(struct iommu_domain *domain,
751                                   unsigned long iova, struct scatterlist *sg,
752                                   unsigned int nents, int prot)
753 {
754         return 0;
755 }
756
757 static inline size_t iommu_map_sg_atomic(struct iommu_domain *domain,
758                                   unsigned long iova, struct scatterlist *sg,
759                                   unsigned int nents, int prot)
760 {
761         return 0;
762 }
763
764 static inline void iommu_flush_tlb_all(struct iommu_domain *domain)
765 {
766 }
767
768 static inline void iommu_tlb_sync(struct iommu_domain *domain,
769                                   struct iommu_iotlb_gather *iotlb_gather)
770 {
771 }
772
773 static inline int iommu_domain_window_enable(struct iommu_domain *domain,
774                                              u32 wnd_nr, phys_addr_t paddr,
775                                              u64 size, int prot)
776 {
777         return -ENODEV;
778 }
779
780 static inline void iommu_domain_window_disable(struct iommu_domain *domain,
781                                                u32 wnd_nr)
782 {
783 }
784
785 static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
786 {
787         return 0;
788 }
789
790 static inline void iommu_set_fault_handler(struct iommu_domain *domain,
791                                 iommu_fault_handler_t handler, void *token)
792 {
793 }
794
795 static inline void iommu_get_resv_regions(struct device *dev,
796                                         struct list_head *list)
797 {
798 }
799
800 static inline void iommu_put_resv_regions(struct device *dev,
801                                         struct list_head *list)
802 {
803 }
804
805 static inline int iommu_get_group_resv_regions(struct iommu_group *group,
806                                                struct list_head *head)
807 {
808         return -ENODEV;
809 }
810
811 static inline int iommu_request_dm_for_dev(struct device *dev)
812 {
813         return -ENODEV;
814 }
815
816 static inline int iommu_request_dma_domain_for_dev(struct device *dev)
817 {
818         return -ENODEV;
819 }
820
821 static inline void iommu_set_default_passthrough(bool cmd_line)
822 {
823 }
824
825 static inline void iommu_set_default_translated(bool cmd_line)
826 {
827 }
828
829 static inline bool iommu_default_passthrough(void)
830 {
831         return true;
832 }
833
834 static inline int iommu_attach_group(struct iommu_domain *domain,
835                                      struct iommu_group *group)
836 {
837         return -ENODEV;
838 }
839
840 static inline void iommu_detach_group(struct iommu_domain *domain,
841                                       struct iommu_group *group)
842 {
843 }
844
845 static inline struct iommu_group *iommu_group_alloc(void)
846 {
847         return ERR_PTR(-ENODEV);
848 }
849
850 static inline void *iommu_group_get_iommudata(struct iommu_group *group)
851 {
852         return NULL;
853 }
854
855 static inline void iommu_group_set_iommudata(struct iommu_group *group,
856                                              void *iommu_data,
857                                              void (*release)(void *iommu_data))
858 {
859 }
860
861 static inline int iommu_group_set_name(struct iommu_group *group,
862                                        const char *name)
863 {
864         return -ENODEV;
865 }
866
867 static inline int iommu_group_add_device(struct iommu_group *group,
868                                          struct device *dev)
869 {
870         return -ENODEV;
871 }
872
873 static inline void iommu_group_remove_device(struct device *dev)
874 {
875 }
876
877 static inline int iommu_group_for_each_dev(struct iommu_group *group,
878                                            void *data,
879                                            int (*fn)(struct device *, void *))
880 {
881         return -ENODEV;
882 }
883
884 static inline struct iommu_group *iommu_group_get(struct device *dev)
885 {
886         return NULL;
887 }
888
889 static inline void iommu_group_put(struct iommu_group *group)
890 {
891 }
892
893 static inline int iommu_group_register_notifier(struct iommu_group *group,
894                                                 struct notifier_block *nb)
895 {
896         return -ENODEV;
897 }
898
899 static inline int iommu_group_unregister_notifier(struct iommu_group *group,
900                                                   struct notifier_block *nb)
901 {
902         return 0;
903 }
904
905 static inline
906 int iommu_register_device_fault_handler(struct device *dev,
907                                         iommu_dev_fault_handler_t handler,
908                                         void *data)
909 {
910         return -ENODEV;
911 }
912
913 static inline int iommu_unregister_device_fault_handler(struct device *dev)
914 {
915         return 0;
916 }
917
918 static inline
919 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
920 {
921         return -ENODEV;
922 }
923
924 static inline int iommu_page_response(struct device *dev,
925                                       struct iommu_page_response *msg)
926 {
927         return -ENODEV;
928 }
929
930 static inline int iommu_group_id(struct iommu_group *group)
931 {
932         return -ENODEV;
933 }
934
935 static inline int iommu_domain_get_attr(struct iommu_domain *domain,
936                                         enum iommu_attr attr, void *data)
937 {
938         return -EINVAL;
939 }
940
941 static inline int iommu_domain_set_attr(struct iommu_domain *domain,
942                                         enum iommu_attr attr, void *data)
943 {
944         return -EINVAL;
945 }
946
947 static inline int  iommu_device_register(struct iommu_device *iommu)
948 {
949         return -ENODEV;
950 }
951
952 static inline void iommu_device_set_ops(struct iommu_device *iommu,
953                                         const struct iommu_ops *ops)
954 {
955 }
956
957 static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
958                                            struct fwnode_handle *fwnode)
959 {
960 }
961
962 static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
963 {
964         return NULL;
965 }
966
967 static inline void iommu_iotlb_gather_init(struct iommu_iotlb_gather *gather)
968 {
969 }
970
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 }
976
977 static inline void iommu_device_unregister(struct iommu_device *iommu)
978 {
979 }
980
981 static inline int  iommu_device_sysfs_add(struct iommu_device *iommu,
982                                           struct device *parent,
983                                           const struct attribute_group **groups,
984                                           const char *fmt, ...)
985 {
986         return -ENODEV;
987 }
988
989 static inline void iommu_device_sysfs_remove(struct iommu_device *iommu)
990 {
991 }
992
993 static inline int iommu_device_link(struct device *dev, struct device *link)
994 {
995         return -EINVAL;
996 }
997
998 static inline void iommu_device_unlink(struct device *dev, struct device *link)
999 {
1000 }
1001
1002 static inline int iommu_fwspec_init(struct device *dev,
1003                                     struct fwnode_handle *iommu_fwnode,
1004                                     const struct iommu_ops *ops)
1005 {
1006         return -ENODEV;
1007 }
1008
1009 static inline void iommu_fwspec_free(struct device *dev)
1010 {
1011 }
1012
1013 static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
1014                                        int num_ids)
1015 {
1016         return -ENODEV;
1017 }
1018
1019 static inline
1020 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
1021 {
1022         return NULL;
1023 }
1024
1025 static inline bool
1026 iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
1027 {
1028         return false;
1029 }
1030
1031 static inline bool
1032 iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
1033 {
1034         return false;
1035 }
1036
1037 static inline int
1038 iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
1039 {
1040         return -ENODEV;
1041 }
1042
1043 static inline int
1044 iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
1045 {
1046         return -ENODEV;
1047 }
1048
1049 static inline int
1050 iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
1051 {
1052         return -ENODEV;
1053 }
1054
1055 static inline void
1056 iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
1057 {
1058 }
1059
1060 static inline int
1061 iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
1062 {
1063         return -ENODEV;
1064 }
1065
1066 static inline struct iommu_sva *
1067 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
1068 {
1069         return NULL;
1070 }
1071
1072 static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
1073 {
1074 }
1075
1076 static inline int iommu_sva_set_ops(struct iommu_sva *handle,
1077                                     const struct iommu_sva_ops *ops)
1078 {
1079         return -EINVAL;
1080 }
1081
1082 static inline int iommu_sva_get_pasid(struct iommu_sva *handle)
1083 {
1084         return IOMMU_PASID_INVALID;
1085 }
1086
1087 static inline int
1088 iommu_cache_invalidate(struct iommu_domain *domain,
1089                        struct device *dev,
1090                        struct iommu_cache_invalidate_info *inv_info)
1091 {
1092         return -ENODEV;
1093 }
1094 static inline int iommu_sva_bind_gpasid(struct iommu_domain *domain,
1095                                 struct device *dev, struct iommu_gpasid_bind_data *data)
1096 {
1097         return -ENODEV;
1098 }
1099
1100 static inline int iommu_sva_unbind_gpasid(struct iommu_domain *domain,
1101                                            struct device *dev, int pasid)
1102 {
1103         return -ENODEV;
1104 }
1105
1106 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
1107 {
1108         return NULL;
1109 }
1110 #endif /* CONFIG_IOMMU_API */
1111
1112 #ifdef CONFIG_IOMMU_DEBUGFS
1113 extern  struct dentry *iommu_debugfs_dir;
1114 void iommu_debugfs_setup(void);
1115 #else
1116 static inline void iommu_debugfs_setup(void) {}
1117 #endif
1118
1119 #endif /* __LINUX_IOMMU_H */