1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __IO_PGTABLE_H
3 #define __IO_PGTABLE_H
4
5 #include <linux/android_kabi.h>
6 #include <linux/bitops.h>
7 #include <linux/iommu.h>
8
9 /*
10 * Public API for use by IOMMU drivers
11 */
12 enum io_pgtable_fmt {
13 ARM_32_LPAE_S1,
14 ARM_32_LPAE_S2,
15 ARM_64_LPAE_S1,
16 ARM_64_LPAE_S2,
17 ARM_V7S,
18 ARM_MALI_LPAE,
19 AMD_IOMMU_V1,
20 AMD_IOMMU_V2,
21 APPLE_DART,
22 APPLE_DART2,
23 IO_PGTABLE_NUM_FMTS,
24 };
25
26 /**
27 * struct iommu_flush_ops - IOMMU callbacks for TLB and page table management.
28 *
29 * @tlb_flush_all: Synchronously invalidate the entire TLB context.
30 * @tlb_flush_walk: Synchronously invalidate all intermediate TLB state
31 * (sometimes referred to as the "walk cache") for a virtual
32 * address range.
33 * @tlb_add_page: Optional callback to queue up leaf TLB invalidation for a
34 * single page. IOMMUs that cannot batch TLB invalidation
35 * operations efficiently will typically issue them here, but
36 * others may decide to update the iommu_iotlb_gather structure
37 * and defer the invalidation until iommu_iotlb_sync() instead.
38 * @free_leaf: Called when a valid leaf is removed when page table is freed.
39 *
40 * Note that these can all be called in atomic context and must therefore
41 * not block.
42 */
43 struct iommu_flush_ops {
44 void (*tlb_flush_all)(void *cookie);
45 void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule,
46 void *cookie);
47 void (*tlb_add_page)(struct iommu_iotlb_gather *gather,
48 unsigned long iova, size_t granule, void *cookie);
49 void (*free_leaf)(unsigned long phys, size_t granule, void *cookie);
50 };
51
52 /**
53 * struct io_pgtable_cfg - Configuration data for a set of page tables.
54 *
55 * @fmt: Format used for these page tables
56 * @quirks: A bitmap of hardware quirks that require some special
57 * action by the low-level page table allocator.
58 * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
59 * tables.
60 * @ias: Input address (iova) size, in bits.
61 * @oas: Output address (paddr) size, in bits.
62 * @coherent_walk A flag to indicate whether or not page table walks made
63 * by the IOMMU are coherent with the CPU caches.
64 * @tlb: TLB management callbacks for this set of tables.
65 * @iommu_dev: The device representing the DMA configuration for the
66 * page table walker.
67 */
68 struct io_pgtable_cfg {
69 enum io_pgtable_fmt fmt;
70 /*
71 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
72 * stage 1 PTEs, for hardware which insists on validating them
73 * even in non-secure state where they should normally be ignored.
74 *
75 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
76 * IOMMU_NOEXEC flags and map everything with full access, for
77 * hardware which does not implement the permissions of a given
78 * format, and/or requires some format-specific default value.
79 *
80 * IO_PGTABLE_QUIRK_ARM_MTK_EXT: (ARM v7s format) MediaTek IOMMUs extend
81 * to support up to 35 bits PA where the bit32, bit33 and bit34 are
82 * encoded in the bit9, bit4 and bit5 of the PTE respectively.
83 *
84 * IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT: (ARM v7s format) MediaTek IOMMUs
85 * extend the translation table base support up to 35 bits PA, the
86 * encoding format is same with IO_PGTABLE_QUIRK_ARM_MTK_EXT.
87 *
88 * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table
89 * for use in the upper half of a split address space.
90 *
91 * IO_PGTABLE_QUIRK_ARM_OUTER_WBWA: Override the outer-cacheability
92 * attributes set in the TCR for a non-coherent page-table walker.
93 *
94 * IO_PGTABLE_QUIRK_ARM_HD: Enables dirty tracking in stage 1 pagetable.
95 *
96 * IO_PGTABLE_QUIRK_UNMAP_INVAL: Only invalidate PTE on unmap, don't clear it.
97 */
98 #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
99 #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
100 #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3)
101 #define IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT BIT(4)
102 #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5)
103 #define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)
104 #define IO_PGTABLE_QUIRK_ARM_HD BIT(7)
105 #define IO_PGTABLE_QUIRK_UNMAP_INVAL BIT(8)
106 unsigned long quirks;
107 unsigned long pgsize_bitmap;
108 unsigned int ias;
109 unsigned int oas;
110 bool coherent_walk;
111 const struct iommu_flush_ops *tlb;
112 struct device *iommu_dev;
113
114 /**
115 * @alloc: Custom page allocator.
116 *
117 * Optional hook used to allocate page tables. If this function is NULL,
118 * @free must be NULL too.
119 *
120 * Memory returned should be zeroed and suitable for dma_map_single() and
121 * virt_to_phys().
122 *
123 * Not all formats support custom page allocators. Before considering
124 * passing a non-NULL value, make sure the chosen page format supports
125 * this feature.
126 */
127 void *(*alloc)(void *cookie, size_t size, gfp_t gfp);
128
129 /**
130 * @free: Custom page de-allocator.
131 *
132 * Optional hook used to free page tables allocated with the @alloc
133 * hook. Must be non-NULL if @alloc is not NULL, must be NULL
134 * otherwise.
135 */
136 void (*free)(void *cookie, void *pages, size_t size);
137
138 /* Low-level data specific to the table format */
139 union {
140 struct {
141 u64 ttbr;
142 struct {
143 u32 ips:3;
144 u32 tg:2;
145 u32 sh:2;
146 u32 orgn:2;
147 u32 irgn:2;
148 u32 tsz:6;
149 } tcr;
150 u64 mair;
151 } arm_lpae_s1_cfg;
152
153 struct {
154 u64 vttbr;
155 struct {
156 u32 ps:3;
157 u32 tg:2;
158 u32 sh:2;
159 u32 orgn:2;
160 u32 irgn:2;
161 u32 sl:2;
162 u32 tsz:6;
163 } vtcr;
164 } arm_lpae_s2_cfg;
165
166 struct {
167 u32 ttbr;
168 u32 tcr;
169 u32 nmrr;
170 u32 prrr;
171 } arm_v7s_cfg;
172
173 struct {
174 u64 transtab;
175 u64 memattr;
176 } arm_mali_lpae_cfg;
177
178 struct {
179 u64 ttbr[4];
180 u32 n_ttbrs;
181 } apple_dart_cfg;
182
183 struct {
184 int nid;
185 } amd;
186 };
187 };
188
189 /**
190 * struct arm_lpae_io_pgtable_walk_data - information from a pgtable walk
191 *
192 * @ptes: The recorded PTE values from the walk
193 * @level: The level of the last PTE
194 * @cookie: Cookie set by caller to identify it
195 *
196 * @level also specifies the last valid index in @ptes
197 */
198 struct arm_lpae_io_pgtable_walk_data {
199 u64 ptes[4];
200 int level;
201 void *cookie;
202 };
203
204 /**
205 * struct io_pgtable_walk_common - common information from a pgtable walk
206 * @visit_leaf: callback for each leaf providing it's physical address and size
207 */
208 struct io_pgtable_walk_common {
209 void (*visit_leaf)(phys_addr_t paddr, size_t size,
210 struct io_pgtable_walk_common *data,
211 void *wd);
212 void *data; /* pointer to walk data as arm_lpae_io_pgtable_walk_data*/
213 };
214
215 /**
216 * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
217 *
218 * @map_pages: Map a physically contiguous range of pages of the same size.
219 * @unmap_pages: Unmap a range of virtually contiguous pages of the same size.
220 * @iova_to_phys: Translate iova to physical address.
221 * @pgtable_walk: (optional) Perform a page table walk for a given iova and size.
222 *
223 * These functions map directly onto the iommu_ops member functions with
224 * the same names.
225 */
226 struct io_pgtable_ops {
227 int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova,
228 phys_addr_t paddr, size_t pgsize, size_t pgcount,
229 int prot, gfp_t gfp, size_t *mapped);
230 size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova,
231 size_t pgsize, size_t pgcount,
232 struct iommu_iotlb_gather *gather);
233 phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
234 unsigned long iova);
235 int (*pgtable_walk)(struct io_pgtable_ops *ops, unsigned long iova,
236 size_t size, struct io_pgtable_walk_common *walker);
237 int (*read_and_clear_dirty)(struct io_pgtable_ops *ops,
238 unsigned long iova, size_t size,
239 unsigned long flags,
240 struct iommu_dirty_bitmap *dirty);
241 ANDROID_KABI_RESERVE(1);
242 ANDROID_KABI_RESERVE(2);
243 ANDROID_KABI_RESERVE(3);
244 ANDROID_KABI_RESERVE(4);
245 };
246
247 /**
248 * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
249 *
250 * @fmt: The page table format.
251 * @cfg: The page table configuration. This will be modified to represent
252 * the configuration actually provided by the allocator (e.g. the
253 * pgsize_bitmap may be restricted).
254 * @cookie: An opaque token provided by the IOMMU driver and passed back to
255 * the callback routines in cfg->tlb.
256 */
257 struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
258 struct io_pgtable_cfg *cfg,
259 void *cookie);
260
261 /**
262 * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
263 * *must* ensure that the page table is no longer
264 * live, but the TLB can be dirty.
265 *
266 * @ops: The ops returned from alloc_io_pgtable_ops.
267 */
268 void free_io_pgtable_ops(struct io_pgtable_ops *ops);
269
270 /**
271 * io_pgtable_configure - Create page table config
272 *
273 * @cfg: The page table configuration.
274 *
275 * Initialize @cfg in the same way as alloc_io_pgtable_ops(), without allocating
276 * anything.
277 *
278 * Not all io_pgtable drivers implement this operation.
279 */
280 int io_pgtable_configure(struct io_pgtable_cfg *cfg);
281
282 /*
283 * Internal structures for page table allocator implementations.
284 */
285
286 /**
287 * struct io_pgtable - Internal structure describing a set of page tables.
288 *
289 * @fmt: The page table format.
290 * @cookie: An opaque token provided by the IOMMU driver and passed back to
291 * any callback routines.
292 * @cfg: A copy of the page table configuration.
293 * @ops: The page table operations in use for this set of page tables.
294 */
295 struct io_pgtable {
296 enum io_pgtable_fmt fmt;
297 void *cookie;
298 struct io_pgtable_cfg cfg;
299 struct io_pgtable_ops ops;
300 };
301
302 #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
303
io_pgtable_tlb_flush_all(struct io_pgtable * iop)304 static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
305 {
306 if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all)
307 iop->cfg.tlb->tlb_flush_all(iop->cookie);
308 }
309
310 static inline void
io_pgtable_tlb_flush_walk(struct io_pgtable * iop,unsigned long iova,size_t size,size_t granule)311 io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova,
312 size_t size, size_t granule)
313 {
314 if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_walk)
315 iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie);
316 }
317
318 static inline void
io_pgtable_tlb_add_page(struct io_pgtable * iop,struct iommu_iotlb_gather * gather,unsigned long iova,size_t granule)319 io_pgtable_tlb_add_page(struct io_pgtable *iop,
320 struct iommu_iotlb_gather * gather, unsigned long iova,
321 size_t granule)
322 {
323 if (iop->cfg.tlb && iop->cfg.tlb->tlb_add_page)
324 iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
325 }
326
327 static inline void
io_pgtable_free_leaf(struct io_pgtable * iop,unsigned long phys,size_t granule)328 io_pgtable_free_leaf(struct io_pgtable *iop, unsigned long phys, size_t granule)
329 {
330 if (iop->cfg.tlb && iop->cfg.tlb->free_leaf)
331 iop->cfg.tlb->free_leaf(phys, granule, iop->cookie);
332 }
333
334 /**
335 * enum io_pgtable_caps - IO page table backend capabilities.
336 */
337 enum io_pgtable_caps {
338 /** @IO_PGTABLE_CAP_CUSTOM_ALLOCATOR: Backend accepts custom page table allocators. */
339 IO_PGTABLE_CAP_CUSTOM_ALLOCATOR = BIT(0),
340 };
341
342 /**
343 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
344 * particular format.
345 *
346 * @alloc: Allocate a set of page tables described by cfg.
347 * @free: Free the page tables associated with iop.
348 * @configure: Create the configuration without allocating anything. Optional.
349 * @caps: Combination of @io_pgtable_caps flags encoding the backend capabilities.
350 */
351 struct io_pgtable_init_fns {
352 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
353 void (*free)(struct io_pgtable *iop);
354 int (*configure)(struct io_pgtable_cfg *cfg);
355 u32 caps;
356 };
357
358 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
359 extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
360 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
361 extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
362 extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
363 extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
364 extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns;
365 extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns;
366 extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns;
367
368 #endif /* __IO_PGTABLE_H */
369