Linux-libre 4.4.228-gnu
[librecmc/linux-libre.git] / drivers / iommu / io-pgtable.h
1 #ifndef __IO_PGTABLE_H
2 #define __IO_PGTABLE_H
3
4 /*
5  * Public API for use by IOMMU drivers
6  */
7 enum io_pgtable_fmt {
8         ARM_32_LPAE_S1,
9         ARM_32_LPAE_S2,
10         ARM_64_LPAE_S1,
11         ARM_64_LPAE_S2,
12         IO_PGTABLE_NUM_FMTS,
13 };
14
15 /**
16  * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management.
17  *
18  * @tlb_flush_all: Synchronously invalidate the entire TLB context.
19  * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.
20  * @tlb_sync:      Ensure any queued TLB invalidation has taken effect, and
21  *                 any corresponding page table updates are visible to the
22  *                 IOMMU.
23  *
24  * Note that these can all be called in atomic context and must therefore
25  * not block.
26  */
27 struct iommu_gather_ops {
28         void (*tlb_flush_all)(void *cookie);
29         void (*tlb_add_flush)(unsigned long iova, size_t size, bool leaf,
30                               void *cookie);
31         void (*tlb_sync)(void *cookie);
32 };
33
34 /**
35  * struct io_pgtable_cfg - Configuration data for a set of page tables.
36  *
37  * @quirks:        A bitmap of hardware quirks that require some special
38  *                 action by the low-level page table allocator.
39  * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
40  *                 tables.
41  * @ias:           Input address (iova) size, in bits.
42  * @oas:           Output address (paddr) size, in bits.
43  * @tlb:           TLB management callbacks for this set of tables.
44  * @iommu_dev:     The device representing the DMA configuration for the
45  *                 page table walker.
46  */
47 struct io_pgtable_cfg {
48         #define IO_PGTABLE_QUIRK_ARM_NS (1 << 0)        /* Set NS bit in PTEs */
49         int                             quirks;
50         unsigned long                   pgsize_bitmap;
51         unsigned int                    ias;
52         unsigned int                    oas;
53         const struct iommu_gather_ops   *tlb;
54         struct device                   *iommu_dev;
55
56         /* Low-level data specific to the table format */
57         union {
58                 struct {
59                         u64     ttbr[2];
60                         u64     tcr;
61                         u64     mair[2];
62                 } arm_lpae_s1_cfg;
63
64                 struct {
65                         u64     vttbr;
66                         u64     vtcr;
67                 } arm_lpae_s2_cfg;
68         };
69 };
70
71 /**
72  * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
73  *
74  * @map:          Map a physically contiguous memory region.
75  * @unmap:        Unmap a physically contiguous memory region.
76  * @iova_to_phys: Translate iova to physical address.
77  *
78  * These functions map directly onto the iommu_ops member functions with
79  * the same names.
80  */
81 struct io_pgtable_ops {
82         int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
83                    phys_addr_t paddr, size_t size, int prot);
84         int (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
85                      size_t size);
86         phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
87                                     unsigned long iova);
88 };
89
90 /**
91  * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
92  *
93  * @fmt:    The page table format.
94  * @cfg:    The page table configuration. This will be modified to represent
95  *          the configuration actually provided by the allocator (e.g. the
96  *          pgsize_bitmap may be restricted).
97  * @cookie: An opaque token provided by the IOMMU driver and passed back to
98  *          the callback routines in cfg->tlb.
99  */
100 struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
101                                             struct io_pgtable_cfg *cfg,
102                                             void *cookie);
103
104 /**
105  * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
106  *                         *must* ensure that the page table is no longer
107  *                         live, but the TLB can be dirty.
108  *
109  * @ops: The ops returned from alloc_io_pgtable_ops.
110  */
111 void free_io_pgtable_ops(struct io_pgtable_ops *ops);
112
113
114 /*
115  * Internal structures for page table allocator implementations.
116  */
117
118 /**
119  * struct io_pgtable - Internal structure describing a set of page tables.
120  *
121  * @fmt:    The page table format.
122  * @cookie: An opaque token provided by the IOMMU driver and passed back to
123  *          any callback routines.
124  * @cfg:    A copy of the page table configuration.
125  * @ops:    The page table operations in use for this set of page tables.
126  */
127 struct io_pgtable {
128         enum io_pgtable_fmt     fmt;
129         void                    *cookie;
130         struct io_pgtable_cfg   cfg;
131         struct io_pgtable_ops   ops;
132 };
133
134 /**
135  * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
136  *                              particular format.
137  *
138  * @alloc: Allocate a set of page tables described by cfg.
139  * @free:  Free the page tables associated with iop.
140  */
141 struct io_pgtable_init_fns {
142         struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
143         void (*free)(struct io_pgtable *iop);
144 };
145
146 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
147 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
148 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
149 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
150
151 #endif /* __IO_PGTABLE_H */