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