xref: /linux/include/linux/io-pgtable.h (revision dc09fe1c5edd9c27a52cb6dc5a7bb4452d45c71c)
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,
19892384cdSSven Peter 	APPLE_DART,
20*dc09fe1cSSven Peter 	APPLE_DART2,
21b77cf11fSRob Herring 	IO_PGTABLE_NUM_FMTS,
22b77cf11fSRob Herring };
23b77cf11fSRob Herring 
24b77cf11fSRob Herring /**
25298f7889SWill Deacon  * struct iommu_flush_ops - IOMMU callbacks for TLB and page table management.
26b77cf11fSRob Herring  *
27b77cf11fSRob Herring  * @tlb_flush_all:  Synchronously invalidate the entire TLB context.
283445545bSWill Deacon  * @tlb_flush_walk: Synchronously invalidate all intermediate TLB state
293445545bSWill Deacon  *                  (sometimes referred to as the "walk cache") for a virtual
303445545bSWill Deacon  *                  address range.
31abfd6fe0SWill Deacon  * @tlb_add_page:   Optional callback to queue up leaf TLB invalidation for a
323951c41aSWill Deacon  *                  single page.  IOMMUs that cannot batch TLB invalidation
333951c41aSWill Deacon  *                  operations efficiently will typically issue them here, but
343951c41aSWill Deacon  *                  others may decide to update the iommu_iotlb_gather structure
35aae4c8e2STom Murphy  *                  and defer the invalidation until iommu_iotlb_sync() instead.
36b77cf11fSRob Herring  *
37b77cf11fSRob Herring  * Note that these can all be called in atomic context and must therefore
38b77cf11fSRob Herring  * not block.
39b77cf11fSRob Herring  */
40298f7889SWill Deacon struct iommu_flush_ops {
41b77cf11fSRob Herring 	void (*tlb_flush_all)(void *cookie);
423445545bSWill Deacon 	void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule,
433445545bSWill Deacon 			       void *cookie);
443951c41aSWill Deacon 	void (*tlb_add_page)(struct iommu_iotlb_gather *gather,
453951c41aSWill Deacon 			     unsigned long iova, size_t granule, void *cookie);
46b77cf11fSRob Herring };
47b77cf11fSRob Herring 
48b77cf11fSRob Herring /**
49b77cf11fSRob Herring  * struct io_pgtable_cfg - Configuration data for a set of page tables.
50b77cf11fSRob Herring  *
51b77cf11fSRob Herring  * @quirks:        A bitmap of hardware quirks that require some special
52b77cf11fSRob Herring  *                 action by the low-level page table allocator.
53b77cf11fSRob Herring  * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
54b77cf11fSRob Herring  *                 tables.
55b77cf11fSRob Herring  * @ias:           Input address (iova) size, in bits.
56b77cf11fSRob Herring  * @oas:           Output address (paddr) size, in bits.
574f41845bSWill Deacon  * @coherent_walk  A flag to indicate whether or not page table walks made
584f41845bSWill Deacon  *                 by the IOMMU are coherent with the CPU caches.
59b77cf11fSRob Herring  * @tlb:           TLB management callbacks for this set of tables.
60b77cf11fSRob Herring  * @iommu_dev:     The device representing the DMA configuration for the
61b77cf11fSRob Herring  *                 page table walker.
62b77cf11fSRob Herring  */
63b77cf11fSRob Herring struct io_pgtable_cfg {
64b77cf11fSRob Herring 	/*
65b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
66b77cf11fSRob Herring 	 *	stage 1 PTEs, for hardware which insists on validating them
67b77cf11fSRob Herring 	 *	even in	non-secure state where they should normally be ignored.
68b77cf11fSRob Herring 	 *
69b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
70b77cf11fSRob Herring 	 *	IOMMU_NOEXEC flags and map everything with full access, for
71b77cf11fSRob Herring 	 *	hardware which does not implement the permissions of a given
72b77cf11fSRob Herring 	 *	format, and/or requires some format-specific default value.
73b77cf11fSRob Herring 	 *
744c019de6SYong Wu 	 * IO_PGTABLE_QUIRK_ARM_MTK_EXT: (ARM v7s format) MediaTek IOMMUs extend
7540596d2fSYong Wu 	 *	to support up to 35 bits PA where the bit32, bit33 and bit34 are
7640596d2fSYong Wu 	 *	encoded in the bit9, bit4 and bit5 of the PTE respectively.
77b77cf11fSRob Herring 	 *
78bfdd2313SYunfei Wang 	 * IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT: (ARM v7s format) MediaTek IOMMUs
79bfdd2313SYunfei Wang 	 *	extend the translation table base support up to 35 bits PA, the
80bfdd2313SYunfei Wang 	 *	encoding format is same with IO_PGTABLE_QUIRK_ARM_MTK_EXT.
81bfdd2313SYunfei Wang 	 *
82db690301SRobin Murphy 	 * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table
83db690301SRobin Murphy 	 *	for use in the upper half of a split address space.
84e67890c9SSai Prakash Ranjan 	 *
85e67890c9SSai Prakash Ranjan 	 * IO_PGTABLE_QUIRK_ARM_OUTER_WBWA: Override the outer-cacheability
86e67890c9SSai Prakash Ranjan 	 *	attributes set in the TCR for a non-coherent page-table walker.
87b77cf11fSRob Herring 	 */
88b77cf11fSRob Herring 	#define IO_PGTABLE_QUIRK_ARM_NS			BIT(0)
89b77cf11fSRob Herring 	#define IO_PGTABLE_QUIRK_NO_PERMS		BIT(1)
9073d50811SYong Wu 	#define IO_PGTABLE_QUIRK_ARM_MTK_EXT		BIT(3)
91bfdd2313SYunfei Wang 	#define IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT	BIT(4)
92db690301SRobin Murphy 	#define IO_PGTABLE_QUIRK_ARM_TTBR1		BIT(5)
93e67890c9SSai Prakash Ranjan 	#define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA		BIT(6)
94b77cf11fSRob Herring 	unsigned long			quirks;
95b77cf11fSRob Herring 	unsigned long			pgsize_bitmap;
96b77cf11fSRob Herring 	unsigned int			ias;
97b77cf11fSRob Herring 	unsigned int			oas;
984f41845bSWill Deacon 	bool				coherent_walk;
99298f7889SWill Deacon 	const struct iommu_flush_ops	*tlb;
100b77cf11fSRob Herring 	struct device			*iommu_dev;
101b77cf11fSRob Herring 
102b77cf11fSRob Herring 	/* Low-level data specific to the table format */
103b77cf11fSRob Herring 	union {
104b77cf11fSRob Herring 		struct {
105d1e5f26fSRobin Murphy 			u64	ttbr;
106fb485eb1SRobin Murphy 			struct {
107fb485eb1SRobin Murphy 				u32	ips:3;
108fb485eb1SRobin Murphy 				u32	tg:2;
109fb485eb1SRobin Murphy 				u32	sh:2;
110fb485eb1SRobin Murphy 				u32	orgn:2;
111fb485eb1SRobin Murphy 				u32	irgn:2;
112fb485eb1SRobin Murphy 				u32	tsz:6;
113fb485eb1SRobin Murphy 			}	tcr;
114205577abSRobin Murphy 			u64	mair;
115b77cf11fSRob Herring 		} arm_lpae_s1_cfg;
116b77cf11fSRob Herring 
117b77cf11fSRob Herring 		struct {
118b77cf11fSRob Herring 			u64	vttbr;
119ac4b80e5SWill Deacon 			struct {
120ac4b80e5SWill Deacon 				u32	ps:3;
121ac4b80e5SWill Deacon 				u32	tg:2;
122ac4b80e5SWill Deacon 				u32	sh:2;
123ac4b80e5SWill Deacon 				u32	orgn:2;
124ac4b80e5SWill Deacon 				u32	irgn:2;
125ac4b80e5SWill Deacon 				u32	sl:2;
126ac4b80e5SWill Deacon 				u32	tsz:6;
127ac4b80e5SWill Deacon 			}	vtcr;
128b77cf11fSRob Herring 		} arm_lpae_s2_cfg;
129b77cf11fSRob Herring 
130b77cf11fSRob Herring 		struct {
131d1e5f26fSRobin Murphy 			u32	ttbr;
132b77cf11fSRob Herring 			u32	tcr;
133b77cf11fSRob Herring 			u32	nmrr;
134b77cf11fSRob Herring 			u32	prrr;
135b77cf11fSRob Herring 		} arm_v7s_cfg;
136d08d42deSRob Herring 
137d08d42deSRob Herring 		struct {
138d08d42deSRob Herring 			u64	transtab;
139d08d42deSRob Herring 			u64	memattr;
140d08d42deSRob Herring 		} arm_mali_lpae_cfg;
141892384cdSSven Peter 
142892384cdSSven Peter 		struct {
143892384cdSSven Peter 			u64 ttbr[4];
144892384cdSSven Peter 			u32 n_ttbrs;
145892384cdSSven Peter 		} apple_dart_cfg;
146b77cf11fSRob Herring 	};
147b77cf11fSRob Herring };
148b77cf11fSRob Herring 
149b77cf11fSRob Herring /**
150b77cf11fSRob Herring  * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
151b77cf11fSRob Herring  *
152b77cf11fSRob Herring  * @map:          Map a physically contiguous memory region.
153ca073b55SIsaac J. Manjarres  * @map_pages:    Map a physically contiguous range of pages of the same size.
154b77cf11fSRob Herring  * @unmap:        Unmap a physically contiguous memory region.
155374c1559SIsaac J. Manjarres  * @unmap_pages:  Unmap a range of virtually contiguous pages of the same size.
156b77cf11fSRob Herring  * @iova_to_phys: Translate iova to physical address.
157b77cf11fSRob Herring  *
158b77cf11fSRob Herring  * These functions map directly onto the iommu_ops member functions with
159b77cf11fSRob Herring  * the same names.
160b77cf11fSRob Herring  */
161b77cf11fSRob Herring struct io_pgtable_ops {
162b77cf11fSRob Herring 	int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
163f34ce7a7SBaolin Wang 		   phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
164ca073b55SIsaac J. Manjarres 	int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova,
165ca073b55SIsaac J. Manjarres 			 phys_addr_t paddr, size_t pgsize, size_t pgcount,
166ca073b55SIsaac J. Manjarres 			 int prot, gfp_t gfp, size_t *mapped);
167b77cf11fSRob Herring 	size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
168a2d3a382SWill Deacon 			size_t size, struct iommu_iotlb_gather *gather);
169374c1559SIsaac J. Manjarres 	size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova,
170374c1559SIsaac J. Manjarres 			      size_t pgsize, size_t pgcount,
171374c1559SIsaac J. Manjarres 			      struct iommu_iotlb_gather *gather);
172b77cf11fSRob Herring 	phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
173b77cf11fSRob Herring 				    unsigned long iova);
174b77cf11fSRob Herring };
175b77cf11fSRob Herring 
176b77cf11fSRob Herring /**
177b77cf11fSRob Herring  * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
178b77cf11fSRob Herring  *
179b77cf11fSRob Herring  * @fmt:    The page table format.
180b77cf11fSRob Herring  * @cfg:    The page table configuration. This will be modified to represent
181b77cf11fSRob Herring  *          the configuration actually provided by the allocator (e.g. the
182b77cf11fSRob Herring  *          pgsize_bitmap may be restricted).
183b77cf11fSRob Herring  * @cookie: An opaque token provided by the IOMMU driver and passed back to
184b77cf11fSRob Herring  *          the callback routines in cfg->tlb.
185b77cf11fSRob Herring  */
186b77cf11fSRob Herring struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
187b77cf11fSRob Herring 					    struct io_pgtable_cfg *cfg,
188b77cf11fSRob Herring 					    void *cookie);
189b77cf11fSRob Herring 
190b77cf11fSRob Herring /**
191b77cf11fSRob Herring  * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
192b77cf11fSRob Herring  *                         *must* ensure that the page table is no longer
193b77cf11fSRob Herring  *                         live, but the TLB can be dirty.
194b77cf11fSRob Herring  *
195b77cf11fSRob Herring  * @ops: The ops returned from alloc_io_pgtable_ops.
196b77cf11fSRob Herring  */
197b77cf11fSRob Herring void free_io_pgtable_ops(struct io_pgtable_ops *ops);
198b77cf11fSRob Herring 
199b77cf11fSRob Herring 
200b77cf11fSRob Herring /*
201b77cf11fSRob Herring  * Internal structures for page table allocator implementations.
202b77cf11fSRob Herring  */
203b77cf11fSRob Herring 
204b77cf11fSRob Herring /**
205b77cf11fSRob Herring  * struct io_pgtable - Internal structure describing a set of page tables.
206b77cf11fSRob Herring  *
207b77cf11fSRob Herring  * @fmt:    The page table format.
208b77cf11fSRob Herring  * @cookie: An opaque token provided by the IOMMU driver and passed back to
209b77cf11fSRob Herring  *          any callback routines.
210b77cf11fSRob Herring  * @cfg:    A copy of the page table configuration.
211b77cf11fSRob Herring  * @ops:    The page table operations in use for this set of page tables.
212b77cf11fSRob Herring  */
213b77cf11fSRob Herring struct io_pgtable {
214b77cf11fSRob Herring 	enum io_pgtable_fmt	fmt;
215b77cf11fSRob Herring 	void			*cookie;
216b77cf11fSRob Herring 	struct io_pgtable_cfg	cfg;
217b77cf11fSRob Herring 	struct io_pgtable_ops	ops;
218b77cf11fSRob Herring };
219b77cf11fSRob Herring 
220b77cf11fSRob Herring #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
221b77cf11fSRob Herring 
222b77cf11fSRob Herring static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
223b77cf11fSRob Herring {
22477e0992aSYong Wu 	if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all)
225b77cf11fSRob Herring 		iop->cfg.tlb->tlb_flush_all(iop->cookie);
226b77cf11fSRob Herring }
227b77cf11fSRob Herring 
22810b7a7d9SWill Deacon static inline void
22910b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova,
23010b7a7d9SWill Deacon 			  size_t size, size_t granule)
231b77cf11fSRob Herring {
23277e0992aSYong Wu 	if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_walk)
23310b7a7d9SWill Deacon 		iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie);
234b77cf11fSRob Herring }
235b77cf11fSRob Herring 
23610b7a7d9SWill Deacon static inline void
2373951c41aSWill Deacon io_pgtable_tlb_add_page(struct io_pgtable *iop,
2383951c41aSWill Deacon 			struct iommu_iotlb_gather * gather, unsigned long iova,
239abfd6fe0SWill Deacon 			size_t granule)
240b77cf11fSRob Herring {
24177e0992aSYong Wu 	if (iop->cfg.tlb && iop->cfg.tlb->tlb_add_page)
2423951c41aSWill Deacon 		iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
243b77cf11fSRob Herring }
244b77cf11fSRob Herring 
245b77cf11fSRob Herring /**
246b77cf11fSRob Herring  * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
247b77cf11fSRob Herring  *                              particular format.
248b77cf11fSRob Herring  *
249b77cf11fSRob Herring  * @alloc: Allocate a set of page tables described by cfg.
250b77cf11fSRob Herring  * @free:  Free the page tables associated with iop.
251b77cf11fSRob Herring  */
252b77cf11fSRob Herring struct io_pgtable_init_fns {
253b77cf11fSRob Herring 	struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
254b77cf11fSRob Herring 	void (*free)(struct io_pgtable *iop);
255b77cf11fSRob Herring };
256b77cf11fSRob Herring 
257b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
258b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
259b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
260b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
261b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
262d08d42deSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
263c9b258c6SSuravee Suthikulpanit extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns;
264892384cdSSven Peter extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns;
265b77cf11fSRob Herring 
266b77cf11fSRob Herring #endif /* __IO_PGTABLE_H */
267