1b77cf11fSRob Herring /* SPDX-License-Identifier: GPL-2.0 */
2b77cf11fSRob Herring #ifndef __IO_PGTABLE_H
3b77cf11fSRob Herring #define __IO_PGTABLE_H
4a2d3a382SWill Deacon
5b77cf11fSRob Herring #include <linux/bitops.h>
6a2d3a382SWill Deacon #include <linux/iommu.h>
7b77cf11fSRob Herring
8b77cf11fSRob Herring /*
9b77cf11fSRob Herring * Public API for use by IOMMU drivers
10b77cf11fSRob Herring */
11b77cf11fSRob Herring enum io_pgtable_fmt {
12b77cf11fSRob Herring ARM_32_LPAE_S1,
13b77cf11fSRob Herring ARM_32_LPAE_S2,
14b77cf11fSRob Herring ARM_64_LPAE_S1,
15b77cf11fSRob Herring ARM_64_LPAE_S2,
16b77cf11fSRob Herring ARM_V7S,
17d08d42deSRob Herring ARM_MALI_LPAE,
18c9b258c6SSuravee Suthikulpanit AMD_IOMMU_V1,
19aaac38f6SVasant Hegde AMD_IOMMU_V2,
20892384cdSSven Peter APPLE_DART,
21dc09fe1cSSven Peter APPLE_DART2,
22b77cf11fSRob Herring IO_PGTABLE_NUM_FMTS,
23b77cf11fSRob Herring };
24b77cf11fSRob Herring
25b77cf11fSRob Herring /**
26298f7889SWill Deacon * struct iommu_flush_ops - IOMMU callbacks for TLB and page table management.
27b77cf11fSRob Herring *
28b77cf11fSRob Herring * @tlb_flush_all: Synchronously invalidate the entire TLB context.
293445545bSWill Deacon * @tlb_flush_walk: Synchronously invalidate all intermediate TLB state
303445545bSWill Deacon * (sometimes referred to as the "walk cache") for a virtual
313445545bSWill Deacon * address range.
32abfd6fe0SWill Deacon * @tlb_add_page: Optional callback to queue up leaf TLB invalidation for a
333951c41aSWill Deacon * single page. IOMMUs that cannot batch TLB invalidation
343951c41aSWill Deacon * operations efficiently will typically issue them here, but
353951c41aSWill Deacon * others may decide to update the iommu_iotlb_gather structure
36aae4c8e2STom Murphy * and defer the invalidation until iommu_iotlb_sync() instead.
37b77cf11fSRob Herring *
38b77cf11fSRob Herring * Note that these can all be called in atomic context and must therefore
39b77cf11fSRob Herring * not block.
40b77cf11fSRob Herring */
41298f7889SWill Deacon struct iommu_flush_ops {
42b77cf11fSRob Herring void (*tlb_flush_all)(void *cookie);
433445545bSWill Deacon void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule,
443445545bSWill Deacon void *cookie);
453951c41aSWill Deacon void (*tlb_add_page)(struct iommu_iotlb_gather *gather,
463951c41aSWill Deacon unsigned long iova, size_t granule, void *cookie);
47b77cf11fSRob Herring };
48b77cf11fSRob Herring
49b77cf11fSRob Herring /**
50b77cf11fSRob Herring * struct io_pgtable_cfg - Configuration data for a set of page tables.
51b77cf11fSRob Herring *
52b77cf11fSRob Herring * @quirks: A bitmap of hardware quirks that require some special
53b77cf11fSRob Herring * action by the low-level page table allocator.
54b77cf11fSRob Herring * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
55b77cf11fSRob Herring * tables.
56b77cf11fSRob Herring * @ias: Input address (iova) size, in bits.
57b77cf11fSRob Herring * @oas: Output address (paddr) size, in bits.
584f41845bSWill Deacon * @coherent_walk A flag to indicate whether or not page table walks made
594f41845bSWill Deacon * by the IOMMU are coherent with the CPU caches.
60b77cf11fSRob Herring * @tlb: TLB management callbacks for this set of tables.
61b77cf11fSRob Herring * @iommu_dev: The device representing the DMA configuration for the
62b77cf11fSRob Herring * page table walker.
63b77cf11fSRob Herring */
64b77cf11fSRob Herring struct io_pgtable_cfg {
65b77cf11fSRob Herring /*
66b77cf11fSRob Herring * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
67b77cf11fSRob Herring * stage 1 PTEs, for hardware which insists on validating them
68b77cf11fSRob Herring * even in non-secure state where they should normally be ignored.
69b77cf11fSRob Herring *
70b77cf11fSRob Herring * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
71b77cf11fSRob Herring * IOMMU_NOEXEC flags and map everything with full access, for
72b77cf11fSRob Herring * hardware which does not implement the permissions of a given
73b77cf11fSRob Herring * format, and/or requires some format-specific default value.
74b77cf11fSRob Herring *
754c019de6SYong Wu * IO_PGTABLE_QUIRK_ARM_MTK_EXT: (ARM v7s format) MediaTek IOMMUs extend
7640596d2fSYong Wu * to support up to 35 bits PA where the bit32, bit33 and bit34 are
7740596d2fSYong Wu * encoded in the bit9, bit4 and bit5 of the PTE respectively.
78b77cf11fSRob Herring *
79bfdd2313SYunfei Wang * IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT: (ARM v7s format) MediaTek IOMMUs
80bfdd2313SYunfei Wang * extend the translation table base support up to 35 bits PA, the
81bfdd2313SYunfei Wang * encoding format is same with IO_PGTABLE_QUIRK_ARM_MTK_EXT.
82bfdd2313SYunfei Wang *
83db690301SRobin Murphy * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table
84db690301SRobin Murphy * for use in the upper half of a split address space.
85e67890c9SSai Prakash Ranjan *
86e67890c9SSai Prakash Ranjan * IO_PGTABLE_QUIRK_ARM_OUTER_WBWA: Override the outer-cacheability
87e67890c9SSai Prakash Ranjan * attributes set in the TCR for a non-coherent page-table walker.
88eb054d67SJoao Martins *
89eb054d67SJoao Martins * IO_PGTABLE_QUIRK_ARM_HD: Enables dirty tracking in stage 1 pagetable.
9067e4fe39SJason Gunthorpe * IO_PGTABLE_QUIRK_ARM_S2FWB: Use the FWB format for the MemAttrs bits
91b77cf11fSRob Herring */
92b77cf11fSRob Herring #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
93b77cf11fSRob Herring #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
9473d50811SYong Wu #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3)
95bfdd2313SYunfei Wang #define IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT BIT(4)
96db690301SRobin Murphy #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5)
97e67890c9SSai Prakash Ranjan #define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA BIT(6)
98eb054d67SJoao Martins #define IO_PGTABLE_QUIRK_ARM_HD BIT(7)
9967e4fe39SJason Gunthorpe #define IO_PGTABLE_QUIRK_ARM_S2FWB BIT(8)
100b77cf11fSRob Herring unsigned long quirks;
101b77cf11fSRob Herring unsigned long pgsize_bitmap;
102b77cf11fSRob Herring unsigned int ias;
103b77cf11fSRob Herring unsigned int oas;
1044f41845bSWill Deacon bool coherent_walk;
105298f7889SWill Deacon const struct iommu_flush_ops *tlb;
106b77cf11fSRob Herring struct device *iommu_dev;
107b77cf11fSRob Herring
10817b226dcSBoris Brezillon /**
10917b226dcSBoris Brezillon * @alloc: Custom page allocator.
11017b226dcSBoris Brezillon *
11117b226dcSBoris Brezillon * Optional hook used to allocate page tables. If this function is NULL,
11217b226dcSBoris Brezillon * @free must be NULL too.
11317b226dcSBoris Brezillon *
11417b226dcSBoris Brezillon * Memory returned should be zeroed and suitable for dma_map_single() and
11517b226dcSBoris Brezillon * virt_to_phys().
11617b226dcSBoris Brezillon *
11717b226dcSBoris Brezillon * Not all formats support custom page allocators. Before considering
11817b226dcSBoris Brezillon * passing a non-NULL value, make sure the chosen page format supports
11917b226dcSBoris Brezillon * this feature.
12017b226dcSBoris Brezillon */
12117b226dcSBoris Brezillon void *(*alloc)(void *cookie, size_t size, gfp_t gfp);
12217b226dcSBoris Brezillon
12317b226dcSBoris Brezillon /**
12417b226dcSBoris Brezillon * @free: Custom page de-allocator.
12517b226dcSBoris Brezillon *
12617b226dcSBoris Brezillon * Optional hook used to free page tables allocated with the @alloc
12717b226dcSBoris Brezillon * hook. Must be non-NULL if @alloc is not NULL, must be NULL
12817b226dcSBoris Brezillon * otherwise.
12917b226dcSBoris Brezillon */
13017b226dcSBoris Brezillon void (*free)(void *cookie, void *pages, size_t size);
13117b226dcSBoris Brezillon
132b77cf11fSRob Herring /* Low-level data specific to the table format */
133b77cf11fSRob Herring union {
134b77cf11fSRob Herring struct {
135d1e5f26fSRobin Murphy u64 ttbr;
136fb485eb1SRobin Murphy struct {
137fb485eb1SRobin Murphy u32 ips:3;
138fb485eb1SRobin Murphy u32 tg:2;
139fb485eb1SRobin Murphy u32 sh:2;
140fb485eb1SRobin Murphy u32 orgn:2;
141fb485eb1SRobin Murphy u32 irgn:2;
142fb485eb1SRobin Murphy u32 tsz:6;
143fb485eb1SRobin Murphy } tcr;
144205577abSRobin Murphy u64 mair;
145b77cf11fSRob Herring } arm_lpae_s1_cfg;
146b77cf11fSRob Herring
147b77cf11fSRob Herring struct {
148b77cf11fSRob Herring u64 vttbr;
149ac4b80e5SWill Deacon struct {
150ac4b80e5SWill Deacon u32 ps:3;
151ac4b80e5SWill Deacon u32 tg:2;
152ac4b80e5SWill Deacon u32 sh:2;
153ac4b80e5SWill Deacon u32 orgn:2;
154ac4b80e5SWill Deacon u32 irgn:2;
155ac4b80e5SWill Deacon u32 sl:2;
156ac4b80e5SWill Deacon u32 tsz:6;
157ac4b80e5SWill Deacon } vtcr;
158b77cf11fSRob Herring } arm_lpae_s2_cfg;
159b77cf11fSRob Herring
160b77cf11fSRob Herring struct {
161d1e5f26fSRobin Murphy u32 ttbr;
162b77cf11fSRob Herring u32 tcr;
163b77cf11fSRob Herring u32 nmrr;
164b77cf11fSRob Herring u32 prrr;
165b77cf11fSRob Herring } arm_v7s_cfg;
166d08d42deSRob Herring
167d08d42deSRob Herring struct {
168d08d42deSRob Herring u64 transtab;
169d08d42deSRob Herring u64 memattr;
170d08d42deSRob Herring } arm_mali_lpae_cfg;
171892384cdSSven Peter
172892384cdSSven Peter struct {
173892384cdSSven Peter u64 ttbr[4];
174892384cdSSven Peter u32 n_ttbrs;
175892384cdSSven Peter } apple_dart_cfg;
17647f218d1SJason Gunthorpe
17747f218d1SJason Gunthorpe struct {
17847f218d1SJason Gunthorpe int nid;
17947f218d1SJason Gunthorpe } amd;
180b77cf11fSRob Herring };
181b77cf11fSRob Herring };
182b77cf11fSRob Herring
183b77cf11fSRob Herring /**
184*aff028a8SRob Clark * struct arm_lpae_io_pgtable_walk_data - information from a pgtable walk
185*aff028a8SRob Clark *
186*aff028a8SRob Clark * @ptes: The recorded PTE values from the walk
187*aff028a8SRob Clark */
188*aff028a8SRob Clark struct arm_lpae_io_pgtable_walk_data {
189*aff028a8SRob Clark u64 ptes[4];
190*aff028a8SRob Clark };
191*aff028a8SRob Clark
192*aff028a8SRob Clark /**
193b77cf11fSRob Herring * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
194b77cf11fSRob Herring *
195ca073b55SIsaac J. Manjarres * @map_pages: Map a physically contiguous range of pages of the same size.
196374c1559SIsaac J. Manjarres * @unmap_pages: Unmap a range of virtually contiguous pages of the same size.
197b77cf11fSRob Herring * @iova_to_phys: Translate iova to physical address.
198*aff028a8SRob Clark * @pgtable_walk: (optional) Perform a page table walk for a given iova.
199b77cf11fSRob Herring *
200b77cf11fSRob Herring * These functions map directly onto the iommu_ops member functions with
201b77cf11fSRob Herring * the same names.
202b77cf11fSRob Herring */
203b77cf11fSRob Herring struct io_pgtable_ops {
204ca073b55SIsaac J. Manjarres int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova,
205ca073b55SIsaac J. Manjarres phys_addr_t paddr, size_t pgsize, size_t pgcount,
206ca073b55SIsaac J. Manjarres int prot, gfp_t gfp, size_t *mapped);
207374c1559SIsaac J. Manjarres size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova,
208374c1559SIsaac J. Manjarres size_t pgsize, size_t pgcount,
209374c1559SIsaac J. Manjarres struct iommu_iotlb_gather *gather);
210b77cf11fSRob Herring phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
211b77cf11fSRob Herring unsigned long iova);
212*aff028a8SRob Clark int (*pgtable_walk)(struct io_pgtable_ops *ops, unsigned long iova, void *wd);
213750e2e90SJoao Martins int (*read_and_clear_dirty)(struct io_pgtable_ops *ops,
214750e2e90SJoao Martins unsigned long iova, size_t size,
215750e2e90SJoao Martins unsigned long flags,
216750e2e90SJoao Martins struct iommu_dirty_bitmap *dirty);
217b77cf11fSRob Herring };
218b77cf11fSRob Herring
219b77cf11fSRob Herring /**
220b77cf11fSRob Herring * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
221b77cf11fSRob Herring *
222b77cf11fSRob Herring * @fmt: The page table format.
223b77cf11fSRob Herring * @cfg: The page table configuration. This will be modified to represent
224b77cf11fSRob Herring * the configuration actually provided by the allocator (e.g. the
225b77cf11fSRob Herring * pgsize_bitmap may be restricted).
226b77cf11fSRob Herring * @cookie: An opaque token provided by the IOMMU driver and passed back to
227b77cf11fSRob Herring * the callback routines in cfg->tlb.
228b77cf11fSRob Herring */
229b77cf11fSRob Herring struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
230b77cf11fSRob Herring struct io_pgtable_cfg *cfg,
231b77cf11fSRob Herring void *cookie);
232b77cf11fSRob Herring
233b77cf11fSRob Herring /**
234b77cf11fSRob Herring * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
235b77cf11fSRob Herring * *must* ensure that the page table is no longer
236b77cf11fSRob Herring * live, but the TLB can be dirty.
237b77cf11fSRob Herring *
238b77cf11fSRob Herring * @ops: The ops returned from alloc_io_pgtable_ops.
239b77cf11fSRob Herring */
240b77cf11fSRob Herring void free_io_pgtable_ops(struct io_pgtable_ops *ops);
241b77cf11fSRob Herring
242b77cf11fSRob Herring
243b77cf11fSRob Herring /*
244b77cf11fSRob Herring * Internal structures for page table allocator implementations.
245b77cf11fSRob Herring */
246b77cf11fSRob Herring
247b77cf11fSRob Herring /**
248b77cf11fSRob Herring * struct io_pgtable - Internal structure describing a set of page tables.
249b77cf11fSRob Herring *
250b77cf11fSRob Herring * @fmt: The page table format.
251b77cf11fSRob Herring * @cookie: An opaque token provided by the IOMMU driver and passed back to
252b77cf11fSRob Herring * any callback routines.
253b77cf11fSRob Herring * @cfg: A copy of the page table configuration.
254b77cf11fSRob Herring * @ops: The page table operations in use for this set of page tables.
255b77cf11fSRob Herring */
256b77cf11fSRob Herring struct io_pgtable {
257b77cf11fSRob Herring enum io_pgtable_fmt fmt;
258b77cf11fSRob Herring void *cookie;
259b77cf11fSRob Herring struct io_pgtable_cfg cfg;
260b77cf11fSRob Herring struct io_pgtable_ops ops;
261b77cf11fSRob Herring };
262b77cf11fSRob Herring
263b77cf11fSRob Herring #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
264b77cf11fSRob Herring
io_pgtable_tlb_flush_all(struct io_pgtable * iop)265b77cf11fSRob Herring static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
266b77cf11fSRob Herring {
26777e0992aSYong Wu if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all)
268b77cf11fSRob Herring iop->cfg.tlb->tlb_flush_all(iop->cookie);
269b77cf11fSRob Herring }
270b77cf11fSRob Herring
27110b7a7d9SWill Deacon static inline void
io_pgtable_tlb_flush_walk(struct io_pgtable * iop,unsigned long iova,size_t size,size_t granule)27210b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova,
27310b7a7d9SWill Deacon size_t size, size_t granule)
274b77cf11fSRob Herring {
27577e0992aSYong Wu if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_walk)
27610b7a7d9SWill Deacon iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie);
277b77cf11fSRob Herring }
278b77cf11fSRob Herring
27910b7a7d9SWill Deacon static inline void
io_pgtable_tlb_add_page(struct io_pgtable * iop,struct iommu_iotlb_gather * gather,unsigned long iova,size_t granule)2803951c41aSWill Deacon io_pgtable_tlb_add_page(struct io_pgtable *iop,
2813951c41aSWill Deacon struct iommu_iotlb_gather * gather, unsigned long iova,
282abfd6fe0SWill Deacon size_t granule)
283b77cf11fSRob Herring {
28477e0992aSYong Wu if (iop->cfg.tlb && iop->cfg.tlb->tlb_add_page)
2853951c41aSWill Deacon iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
286b77cf11fSRob Herring }
287b77cf11fSRob Herring
288b77cf11fSRob Herring /**
28917b226dcSBoris Brezillon * enum io_pgtable_caps - IO page table backend capabilities.
29017b226dcSBoris Brezillon */
29117b226dcSBoris Brezillon enum io_pgtable_caps {
29217b226dcSBoris Brezillon /** @IO_PGTABLE_CAP_CUSTOM_ALLOCATOR: Backend accepts custom page table allocators. */
29317b226dcSBoris Brezillon IO_PGTABLE_CAP_CUSTOM_ALLOCATOR = BIT(0),
29417b226dcSBoris Brezillon };
29517b226dcSBoris Brezillon
29617b226dcSBoris Brezillon /**
297b77cf11fSRob Herring * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
298b77cf11fSRob Herring * particular format.
299b77cf11fSRob Herring *
300b77cf11fSRob Herring * @alloc: Allocate a set of page tables described by cfg.
301b77cf11fSRob Herring * @free: Free the page tables associated with iop.
30217b226dcSBoris Brezillon * @caps: Combination of @io_pgtable_caps flags encoding the backend capabilities.
303b77cf11fSRob Herring */
304b77cf11fSRob Herring struct io_pgtable_init_fns {
305b77cf11fSRob Herring struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
306b77cf11fSRob Herring void (*free)(struct io_pgtable *iop);
30717b226dcSBoris Brezillon u32 caps;
308b77cf11fSRob Herring };
309b77cf11fSRob Herring
310b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
311b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
312b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
313b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
314b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
315d08d42deSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
316c9b258c6SSuravee Suthikulpanit extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns;
317aaac38f6SVasant Hegde extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns;
318892384cdSSven Peter extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns;
319b77cf11fSRob Herring
320b77cf11fSRob Herring #endif /* __IO_PGTABLE_H */
321