1
2
3
4
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
17#define IOMMU_READ (1 << 0)
18#define IOMMU_WRITE (1 << 1)
19#define IOMMU_CACHE (1 << 2)
20#define IOMMU_NOEXEC (1 << 3)
21#define IOMMU_MMIO (1 << 4)
22
23
24
25
26
27
28
29
30
31#define IOMMU_PRIV (1 << 5)
32
33struct iommu_ops;
34struct iommu_group;
35struct bus_type;
36struct device;
37struct iommu_domain;
38struct notifier_block;
39struct iommu_sva;
40
41
42#define IOMMU_FAULT_READ 0x0
43#define IOMMU_FAULT_WRITE 0x1
44
45typedef int (*iommu_fault_handler_t)(struct iommu_domain *,
46 struct device *, unsigned long, int, void *);
47typedef int (*iommu_mm_exit_handler_t)(struct device *dev, struct iommu_sva *,
48 void *);
49
50struct iommu_domain_geometry {
51 dma_addr_t aperture_start;
52 dma_addr_t aperture_end;
53 bool force_aperture;
54};
55
56
57#define __IOMMU_DOMAIN_PAGING (1U << 0)
58#define __IOMMU_DOMAIN_DMA_API (1U << 1)
59
60#define __IOMMU_DOMAIN_PT (1U << 2)
61
62
63
64
65
66
67
68
69
70
71
72
73
74#define IOMMU_DOMAIN_BLOCKED (0U)
75#define IOMMU_DOMAIN_IDENTITY (__IOMMU_DOMAIN_PT)
76#define IOMMU_DOMAIN_UNMANAGED (__IOMMU_DOMAIN_PAGING)
77#define IOMMU_DOMAIN_DMA (__IOMMU_DOMAIN_PAGING | \
78 __IOMMU_DOMAIN_DMA_API)
79
80struct iommu_domain {
81 unsigned type;
82 const struct iommu_ops *ops;
83 unsigned long pgsize_bitmap;
84 iommu_fault_handler_t handler;
85 void *handler_token;
86 struct iommu_domain_geometry geometry;
87 void *iova_cookie;
88};
89
90enum iommu_cap {
91 IOMMU_CAP_CACHE_COHERENCY,
92
93 IOMMU_CAP_INTR_REMAP,
94 IOMMU_CAP_NOEXEC,
95};
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110enum iommu_attr {
111 DOMAIN_ATTR_GEOMETRY,
112 DOMAIN_ATTR_PAGING,
113 DOMAIN_ATTR_WINDOWS,
114 DOMAIN_ATTR_FSL_PAMU_STASH,
115 DOMAIN_ATTR_FSL_PAMU_ENABLE,
116 DOMAIN_ATTR_FSL_PAMUV1,
117 DOMAIN_ATTR_NESTING,
118 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
119 DOMAIN_ATTR_MAX,
120};
121
122
123enum iommu_resv_type {
124
125 IOMMU_RESV_DIRECT,
126
127 IOMMU_RESV_RESERVED,
128
129 IOMMU_RESV_MSI,
130
131 IOMMU_RESV_SW_MSI,
132};
133
134
135
136
137
138
139
140
141
142struct iommu_resv_region {
143 struct list_head list;
144 phys_addr_t start;
145 size_t length;
146 int prot;
147 enum iommu_resv_type type;
148};
149
150
151enum iommu_dev_features {
152 IOMMU_DEV_FEAT_AUX,
153 IOMMU_DEV_FEAT_SVA,
154};
155
156#define IOMMU_PASID_INVALID (-1U)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173struct iommu_sva_ops {
174 iommu_mm_exit_handler_t mm_exit;
175};
176
177#ifdef CONFIG_IOMMU_API
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217struct iommu_ops {
218 bool (*capable)(enum iommu_cap);
219
220
221 struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type);
222 void (*domain_free)(struct iommu_domain *);
223
224 int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
225 void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
226 int (*map)(struct iommu_domain *domain, unsigned long iova,
227 phys_addr_t paddr, size_t size, int prot);
228 size_t (*unmap)(struct iommu_domain *domain, unsigned long iova,
229 size_t size);
230 void (*flush_iotlb_all)(struct iommu_domain *domain);
231 void (*iotlb_range_add)(struct iommu_domain *domain,
232 unsigned long iova, size_t size);
233 void (*iotlb_sync_map)(struct iommu_domain *domain);
234 void (*iotlb_sync)(struct iommu_domain *domain);
235 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova);
236 int (*add_device)(struct device *dev);
237 void (*remove_device)(struct device *dev);
238 struct iommu_group *(*device_group)(struct device *dev);
239 int (*domain_get_attr)(struct iommu_domain *domain,
240 enum iommu_attr attr, void *data);
241 int (*domain_set_attr)(struct iommu_domain *domain,
242 enum iommu_attr attr, void *data);
243
244
245 void (*get_resv_regions)(struct device *dev, struct list_head *list);
246 void (*put_resv_regions)(struct device *dev, struct list_head *list);
247 void (*apply_resv_region)(struct device *dev,
248 struct iommu_domain *domain,
249 struct iommu_resv_region *region);
250
251
252 int (*domain_window_enable)(struct iommu_domain *domain, u32 wnd_nr,
253 phys_addr_t paddr, u64 size, int prot);
254 void (*domain_window_disable)(struct iommu_domain *domain, u32 wnd_nr);
255
256 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
257 bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev);
258
259
260 bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);
261 bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);
262 int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
263 int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
264
265
266 int (*aux_attach_dev)(struct iommu_domain *domain, struct device *dev);
267 void (*aux_detach_dev)(struct iommu_domain *domain, struct device *dev);
268 int (*aux_get_pasid)(struct iommu_domain *domain, struct device *dev);
269
270 struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,
271 void *drvdata);
272 void (*sva_unbind)(struct iommu_sva *handle);
273 int (*sva_get_pasid)(struct iommu_sva *handle);
274
275 unsigned long pgsize_bitmap;
276};
277
278
279
280
281
282
283
284
285struct iommu_device {
286 struct list_head list;
287 const struct iommu_ops *ops;
288 struct fwnode_handle *fwnode;
289 struct device *dev;
290};
291
292int iommu_device_register(struct iommu_device *iommu);
293void iommu_device_unregister(struct iommu_device *iommu);
294int iommu_device_sysfs_add(struct iommu_device *iommu,
295 struct device *parent,
296 const struct attribute_group **groups,
297 const char *fmt, ...) __printf(4, 5);
298void iommu_device_sysfs_remove(struct iommu_device *iommu);
299int iommu_device_link(struct iommu_device *iommu, struct device *link);
300void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
301
302static inline void iommu_device_set_ops(struct iommu_device *iommu,
303 const struct iommu_ops *ops)
304{
305 iommu->ops = ops;
306}
307
308static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
309 struct fwnode_handle *fwnode)
310{
311 iommu->fwnode = fwnode;
312}
313
314static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
315{
316 return (struct iommu_device *)dev_get_drvdata(dev);
317}
318
319#define IOMMU_GROUP_NOTIFY_ADD_DEVICE 1
320#define IOMMU_GROUP_NOTIFY_DEL_DEVICE 2
321#define IOMMU_GROUP_NOTIFY_BIND_DRIVER 3
322#define IOMMU_GROUP_NOTIFY_BOUND_DRIVER 4
323#define IOMMU_GROUP_NOTIFY_UNBIND_DRIVER 5
324#define IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER 6
325
326extern int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops);
327extern bool iommu_present(struct bus_type *bus);
328extern bool iommu_capable(struct bus_type *bus, enum iommu_cap cap);
329extern struct iommu_domain *iommu_domain_alloc(struct bus_type *bus);
330extern struct iommu_group *iommu_group_get_by_id(int id);
331extern void iommu_domain_free(struct iommu_domain *domain);
332extern int iommu_attach_device(struct iommu_domain *domain,
333 struct device *dev);
334extern void iommu_detach_device(struct iommu_domain *domain,
335 struct device *dev);
336extern struct iommu_domain *iommu_get_domain_for_dev(struct device *dev);
337extern struct iommu_domain *iommu_get_dma_domain(struct device *dev);
338extern int iommu_map(struct iommu_domain *domain, unsigned long iova,
339 phys_addr_t paddr, size_t size, int prot);
340extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova,
341 size_t size);
342extern size_t iommu_unmap_fast(struct iommu_domain *domain,
343 unsigned long iova, size_t size);
344extern size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
345 struct scatterlist *sg,unsigned int nents, int prot);
346extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova);
347extern void iommu_set_fault_handler(struct iommu_domain *domain,
348 iommu_fault_handler_t handler, void *token);
349
350extern void iommu_get_resv_regions(struct device *dev, struct list_head *list);
351extern void iommu_put_resv_regions(struct device *dev, struct list_head *list);
352extern int iommu_request_dm_for_dev(struct device *dev);
353extern struct iommu_resv_region *
354iommu_alloc_resv_region(phys_addr_t start, size_t length, int prot,
355 enum iommu_resv_type type);
356extern int iommu_get_group_resv_regions(struct iommu_group *group,
357 struct list_head *head);
358
359extern int iommu_attach_group(struct iommu_domain *domain,
360 struct iommu_group *group);
361extern void iommu_detach_group(struct iommu_domain *domain,
362 struct iommu_group *group);
363extern struct iommu_group *iommu_group_alloc(void);
364extern void *iommu_group_get_iommudata(struct iommu_group *group);
365extern void iommu_group_set_iommudata(struct iommu_group *group,
366 void *iommu_data,
367 void (*release)(void *iommu_data));
368extern int iommu_group_set_name(struct iommu_group *group, const char *name);
369extern int iommu_group_add_device(struct iommu_group *group,
370 struct device *dev);
371extern void iommu_group_remove_device(struct device *dev);
372extern int iommu_group_for_each_dev(struct iommu_group *group, void *data,
373 int (*fn)(struct device *, void *));
374extern struct iommu_group *iommu_group_get(struct device *dev);
375extern struct iommu_group *iommu_group_ref_get(struct iommu_group *group);
376extern void iommu_group_put(struct iommu_group *group);
377extern int iommu_group_register_notifier(struct iommu_group *group,
378 struct notifier_block *nb);
379extern int iommu_group_unregister_notifier(struct iommu_group *group,
380 struct notifier_block *nb);
381extern int iommu_group_id(struct iommu_group *group);
382extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
383extern struct iommu_domain *iommu_group_default_domain(struct iommu_group *);
384
385extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
386 void *data);
387extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
388 void *data);
389
390
391extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
392 phys_addr_t offset, u64 size,
393 int prot);
394extern void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr);
395
396extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
397 unsigned long iova, int flags);
398
399static inline void iommu_flush_tlb_all(struct iommu_domain *domain)
400{
401 if (domain->ops->flush_iotlb_all)
402 domain->ops->flush_iotlb_all(domain);
403}
404
405static inline void iommu_tlb_range_add(struct iommu_domain *domain,
406 unsigned long iova, size_t size)
407{
408 if (domain->ops->iotlb_range_add)
409 domain->ops->iotlb_range_add(domain, iova, size);
410}
411
412static inline void iommu_tlb_sync(struct iommu_domain *domain)
413{
414 if (domain->ops->iotlb_sync)
415 domain->ops->iotlb_sync(domain);
416}
417
418
419extern struct iommu_group *pci_device_group(struct device *dev);
420
421extern struct iommu_group *generic_device_group(struct device *dev);
422
423struct iommu_group *fsl_mc_device_group(struct device *dev);
424
425
426
427
428
429
430
431
432
433struct iommu_fwspec {
434 const struct iommu_ops *ops;
435 struct fwnode_handle *iommu_fwnode;
436 void *iommu_priv;
437 u32 flags;
438 unsigned int num_ids;
439 u32 ids[1];
440};
441
442
443#define IOMMU_FWSPEC_PCI_RC_ATS (1 << 0)
444
445
446
447
448struct iommu_sva {
449 struct device *dev;
450 const struct iommu_sva_ops *ops;
451};
452
453int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
454 const struct iommu_ops *ops);
455void iommu_fwspec_free(struct device *dev);
456int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids);
457const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode);
458
459static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
460{
461 return dev->iommu_fwspec;
462}
463
464static inline void dev_iommu_fwspec_set(struct device *dev,
465 struct iommu_fwspec *fwspec)
466{
467 dev->iommu_fwspec = fwspec;
468}
469
470int iommu_probe_device(struct device *dev);
471void iommu_release_device(struct device *dev);
472
473bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features f);
474int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
475int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
476bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);
477int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev);
478void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev);
479int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev);
480
481struct iommu_sva *iommu_sva_bind_device(struct device *dev,
482 struct mm_struct *mm,
483 void *drvdata);
484void iommu_sva_unbind_device(struct iommu_sva *handle);
485int iommu_sva_set_ops(struct iommu_sva *handle,
486 const struct iommu_sva_ops *ops);
487int iommu_sva_get_pasid(struct iommu_sva *handle);
488
489#else
490
491struct iommu_ops {};
492struct iommu_group {};
493struct iommu_fwspec {};
494struct iommu_device {};
495
496static inline bool iommu_present(struct bus_type *bus)
497{
498 return false;
499}
500
501static inline bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
502{
503 return false;
504}
505
506static inline struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
507{
508 return NULL;
509}
510
511static inline struct iommu_group *iommu_group_get_by_id(int id)
512{
513 return NULL;
514}
515
516static inline void iommu_domain_free(struct iommu_domain *domain)
517{
518}
519
520static inline int iommu_attach_device(struct iommu_domain *domain,
521 struct device *dev)
522{
523 return -ENODEV;
524}
525
526static inline void iommu_detach_device(struct iommu_domain *domain,
527 struct device *dev)
528{
529}
530
531static inline struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
532{
533 return NULL;
534}
535
536static inline int iommu_map(struct iommu_domain *domain, unsigned long iova,
537 phys_addr_t paddr, size_t size, int prot)
538{
539 return -ENODEV;
540}
541
542static inline size_t iommu_unmap(struct iommu_domain *domain,
543 unsigned long iova, size_t size)
544{
545 return 0;
546}
547
548static inline size_t iommu_unmap_fast(struct iommu_domain *domain,
549 unsigned long iova, int gfp_order)
550{
551 return 0;
552}
553
554static inline size_t iommu_map_sg(struct iommu_domain *domain,
555 unsigned long iova, struct scatterlist *sg,
556 unsigned int nents, int prot)
557{
558 return 0;
559}
560
561static inline void iommu_flush_tlb_all(struct iommu_domain *domain)
562{
563}
564
565static inline void iommu_tlb_range_add(struct iommu_domain *domain,
566 unsigned long iova, size_t size)
567{
568}
569
570static inline void iommu_tlb_sync(struct iommu_domain *domain)
571{
572}
573
574static inline int iommu_domain_window_enable(struct iommu_domain *domain,
575 u32 wnd_nr, phys_addr_t paddr,
576 u64 size, int prot)
577{
578 return -ENODEV;
579}
580
581static inline void iommu_domain_window_disable(struct iommu_domain *domain,
582 u32 wnd_nr)
583{
584}
585
586static inline phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
587{
588 return 0;
589}
590
591static inline void iommu_set_fault_handler(struct iommu_domain *domain,
592 iommu_fault_handler_t handler, void *token)
593{
594}
595
596static inline void iommu_get_resv_regions(struct device *dev,
597 struct list_head *list)
598{
599}
600
601static inline void iommu_put_resv_regions(struct device *dev,
602 struct list_head *list)
603{
604}
605
606static inline int iommu_get_group_resv_regions(struct iommu_group *group,
607 struct list_head *head)
608{
609 return -ENODEV;
610}
611
612static inline int iommu_request_dm_for_dev(struct device *dev)
613{
614 return -ENODEV;
615}
616
617static inline int iommu_attach_group(struct iommu_domain *domain,
618 struct iommu_group *group)
619{
620 return -ENODEV;
621}
622
623static inline void iommu_detach_group(struct iommu_domain *domain,
624 struct iommu_group *group)
625{
626}
627
628static inline struct iommu_group *iommu_group_alloc(void)
629{
630 return ERR_PTR(-ENODEV);
631}
632
633static inline void *iommu_group_get_iommudata(struct iommu_group *group)
634{
635 return NULL;
636}
637
638static inline void iommu_group_set_iommudata(struct iommu_group *group,
639 void *iommu_data,
640 void (*release)(void *iommu_data))
641{
642}
643
644static inline int iommu_group_set_name(struct iommu_group *group,
645 const char *name)
646{
647 return -ENODEV;
648}
649
650static inline int iommu_group_add_device(struct iommu_group *group,
651 struct device *dev)
652{
653 return -ENODEV;
654}
655
656static inline void iommu_group_remove_device(struct device *dev)
657{
658}
659
660static inline int iommu_group_for_each_dev(struct iommu_group *group,
661 void *data,
662 int (*fn)(struct device *, void *))
663{
664 return -ENODEV;
665}
666
667static inline struct iommu_group *iommu_group_get(struct device *dev)
668{
669 return NULL;
670}
671
672static inline void iommu_group_put(struct iommu_group *group)
673{
674}
675
676static inline int iommu_group_register_notifier(struct iommu_group *group,
677 struct notifier_block *nb)
678{
679 return -ENODEV;
680}
681
682static inline int iommu_group_unregister_notifier(struct iommu_group *group,
683 struct notifier_block *nb)
684{
685 return 0;
686}
687
688static inline int iommu_group_id(struct iommu_group *group)
689{
690 return -ENODEV;
691}
692
693static inline int iommu_domain_get_attr(struct iommu_domain *domain,
694 enum iommu_attr attr, void *data)
695{
696 return -EINVAL;
697}
698
699static inline int iommu_domain_set_attr(struct iommu_domain *domain,
700 enum iommu_attr attr, void *data)
701{
702 return -EINVAL;
703}
704
705static inline int iommu_device_register(struct iommu_device *iommu)
706{
707 return -ENODEV;
708}
709
710static inline void iommu_device_set_ops(struct iommu_device *iommu,
711 const struct iommu_ops *ops)
712{
713}
714
715static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
716 struct fwnode_handle *fwnode)
717{
718}
719
720static inline struct iommu_device *dev_to_iommu_device(struct device *dev)
721{
722 return NULL;
723}
724
725static inline void iommu_device_unregister(struct iommu_device *iommu)
726{
727}
728
729static inline int iommu_device_sysfs_add(struct iommu_device *iommu,
730 struct device *parent,
731 const struct attribute_group **groups,
732 const char *fmt, ...)
733{
734 return -ENODEV;
735}
736
737static inline void iommu_device_sysfs_remove(struct iommu_device *iommu)
738{
739}
740
741static inline int iommu_device_link(struct device *dev, struct device *link)
742{
743 return -EINVAL;
744}
745
746static inline void iommu_device_unlink(struct device *dev, struct device *link)
747{
748}
749
750static inline int iommu_fwspec_init(struct device *dev,
751 struct fwnode_handle *iommu_fwnode,
752 const struct iommu_ops *ops)
753{
754 return -ENODEV;
755}
756
757static inline void iommu_fwspec_free(struct device *dev)
758{
759}
760
761static inline int iommu_fwspec_add_ids(struct device *dev, u32 *ids,
762 int num_ids)
763{
764 return -ENODEV;
765}
766
767static inline
768const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
769{
770 return NULL;
771}
772
773static inline bool
774iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
775{
776 return false;
777}
778
779static inline bool
780iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
781{
782 return false;
783}
784
785static inline int
786iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
787{
788 return -ENODEV;
789}
790
791static inline int
792iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
793{
794 return -ENODEV;
795}
796
797static inline int
798iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
799{
800 return -ENODEV;
801}
802
803static inline void
804iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
805{
806}
807
808static inline int
809iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
810{
811 return -ENODEV;
812}
813
814static inline struct iommu_sva *
815iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
816{
817 return NULL;
818}
819
820static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
821{
822}
823
824static inline int iommu_sva_set_ops(struct iommu_sva *handle,
825 const struct iommu_sva_ops *ops)
826{
827 return -EINVAL;
828}
829
830static inline int iommu_sva_get_pasid(struct iommu_sva *handle)
831{
832 return IOMMU_PASID_INVALID;
833}
834
835#endif
836
837#ifdef CONFIG_IOMMU_DEBUGFS
838extern struct dentry *iommu_debugfs_dir;
839void iommu_debugfs_setup(void);
840#else
841static inline void iommu_debugfs_setup(void) {}
842#endif
843
844#endif
845