xref: /linux/include/linux/io-pgtable.h (revision c9b258c6be09283663c6851725b322568d867c0b)
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,
18*c9b258c6SSuravee Suthikulpanit 	AMD_IOMMU_V1,
19b77cf11fSRob Herring 	IO_PGTABLE_NUM_FMTS,
20b77cf11fSRob Herring };
21b77cf11fSRob Herring 
22b77cf11fSRob Herring /**
23298f7889SWill Deacon  * struct iommu_flush_ops - IOMMU callbacks for TLB and page table management.
24b77cf11fSRob Herring  *
25b77cf11fSRob Herring  * @tlb_flush_all:  Synchronously invalidate the entire TLB context.
263445545bSWill Deacon  * @tlb_flush_walk: Synchronously invalidate all intermediate TLB state
273445545bSWill Deacon  *                  (sometimes referred to as the "walk cache") for a virtual
283445545bSWill Deacon  *                  address range.
29abfd6fe0SWill Deacon  * @tlb_add_page:   Optional callback to queue up leaf TLB invalidation for a
303951c41aSWill Deacon  *                  single page.  IOMMUs that cannot batch TLB invalidation
313951c41aSWill Deacon  *                  operations efficiently will typically issue them here, but
323951c41aSWill Deacon  *                  others may decide to update the iommu_iotlb_gather structure
33aae4c8e2STom Murphy  *                  and defer the invalidation until iommu_iotlb_sync() instead.
34b77cf11fSRob Herring  *
35b77cf11fSRob Herring  * Note that these can all be called in atomic context and must therefore
36b77cf11fSRob Herring  * not block.
37b77cf11fSRob Herring  */
38298f7889SWill Deacon struct iommu_flush_ops {
39b77cf11fSRob Herring 	void (*tlb_flush_all)(void *cookie);
403445545bSWill Deacon 	void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule,
413445545bSWill Deacon 			       void *cookie);
423951c41aSWill Deacon 	void (*tlb_add_page)(struct iommu_iotlb_gather *gather,
433951c41aSWill Deacon 			     unsigned long iova, size_t granule, void *cookie);
44b77cf11fSRob Herring };
45b77cf11fSRob Herring 
46b77cf11fSRob Herring /**
47b77cf11fSRob Herring  * struct io_pgtable_cfg - Configuration data for a set of page tables.
48b77cf11fSRob Herring  *
49b77cf11fSRob Herring  * @quirks:        A bitmap of hardware quirks that require some special
50b77cf11fSRob Herring  *                 action by the low-level page table allocator.
51b77cf11fSRob Herring  * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
52b77cf11fSRob Herring  *                 tables.
53b77cf11fSRob Herring  * @ias:           Input address (iova) size, in bits.
54b77cf11fSRob Herring  * @oas:           Output address (paddr) size, in bits.
554f41845bSWill Deacon  * @coherent_walk  A flag to indicate whether or not page table walks made
564f41845bSWill Deacon  *                 by the IOMMU are coherent with the CPU caches.
57b77cf11fSRob Herring  * @tlb:           TLB management callbacks for this set of tables.
58b77cf11fSRob Herring  * @iommu_dev:     The device representing the DMA configuration for the
59b77cf11fSRob Herring  *                 page table walker.
60b77cf11fSRob Herring  */
61b77cf11fSRob Herring struct io_pgtable_cfg {
62b77cf11fSRob Herring 	/*
63b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
64b77cf11fSRob Herring 	 *	stage 1 PTEs, for hardware which insists on validating them
65b77cf11fSRob Herring 	 *	even in	non-secure state where they should normally be ignored.
66b77cf11fSRob Herring 	 *
67b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
68b77cf11fSRob Herring 	 *	IOMMU_NOEXEC flags and map everything with full access, for
69b77cf11fSRob Herring 	 *	hardware which does not implement the permissions of a given
70b77cf11fSRob Herring 	 *	format, and/or requires some format-specific default value.
71b77cf11fSRob Herring 	 *
72b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid
73b77cf11fSRob Herring 	 *	(unmapped) entries but the hardware might do so anyway, perform
74b77cf11fSRob Herring 	 *	TLB maintenance when mapping as well as when unmapping.
75b77cf11fSRob Herring 	 *
764c019de6SYong Wu 	 * IO_PGTABLE_QUIRK_ARM_MTK_EXT: (ARM v7s format) MediaTek IOMMUs extend
774c019de6SYong Wu 	 *	to support up to 34 bits PA where the bit32 and bit33 are
784c019de6SYong Wu 	 *	encoded in the bit9 and bit4 of the PTE respectively.
79b77cf11fSRob Herring 	 *
80b77cf11fSRob Herring 	 * IO_PGTABLE_QUIRK_NON_STRICT: Skip issuing synchronous leaf TLBIs
81b77cf11fSRob Herring 	 *	on unmap, for DMA domains using the flush queue mechanism for
82b77cf11fSRob Herring 	 *	delayed invalidation.
83db690301SRobin Murphy 	 *
84db690301SRobin Murphy 	 * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table
85db690301SRobin Murphy 	 *	for use in the upper half of a split address space.
86e67890c9SSai Prakash Ranjan 	 *
87e67890c9SSai Prakash Ranjan 	 * IO_PGTABLE_QUIRK_ARM_OUTER_WBWA: Override the outer-cacheability
88e67890c9SSai Prakash Ranjan 	 *	attributes set in the TCR for a non-coherent page-table walker.
89b77cf11fSRob Herring 	 */
90b77cf11fSRob Herring 	#define IO_PGTABLE_QUIRK_ARM_NS		BIT(0)
91b77cf11fSRob Herring 	#define IO_PGTABLE_QUIRK_NO_PERMS	BIT(1)
92b77cf11fSRob Herring 	#define IO_PGTABLE_QUIRK_TLBI_ON_MAP	BIT(2)
9373d50811SYong Wu 	#define IO_PGTABLE_QUIRK_ARM_MTK_EXT	BIT(3)
944f41845bSWill Deacon 	#define IO_PGTABLE_QUIRK_NON_STRICT	BIT(4)
95db690301SRobin Murphy 	#define IO_PGTABLE_QUIRK_ARM_TTBR1	BIT(5)
96e67890c9SSai Prakash Ranjan 	#define IO_PGTABLE_QUIRK_ARM_OUTER_WBWA	BIT(6)
97b77cf11fSRob Herring 	unsigned long			quirks;
98b77cf11fSRob Herring 	unsigned long			pgsize_bitmap;
99b77cf11fSRob Herring 	unsigned int			ias;
100b77cf11fSRob Herring 	unsigned int			oas;
1014f41845bSWill Deacon 	bool				coherent_walk;
102298f7889SWill Deacon 	const struct iommu_flush_ops	*tlb;
103b77cf11fSRob Herring 	struct device			*iommu_dev;
104b77cf11fSRob Herring 
105b77cf11fSRob Herring 	/* Low-level data specific to the table format */
106b77cf11fSRob Herring 	union {
107b77cf11fSRob Herring 		struct {
108d1e5f26fSRobin Murphy 			u64	ttbr;
109fb485eb1SRobin Murphy 			struct {
110fb485eb1SRobin Murphy 				u32	ips:3;
111fb485eb1SRobin Murphy 				u32	tg:2;
112fb485eb1SRobin Murphy 				u32	sh:2;
113fb485eb1SRobin Murphy 				u32	orgn:2;
114fb485eb1SRobin Murphy 				u32	irgn:2;
115fb485eb1SRobin Murphy 				u32	tsz:6;
116fb485eb1SRobin Murphy 			}	tcr;
117205577abSRobin Murphy 			u64	mair;
118b77cf11fSRob Herring 		} arm_lpae_s1_cfg;
119b77cf11fSRob Herring 
120b77cf11fSRob Herring 		struct {
121b77cf11fSRob Herring 			u64	vttbr;
122ac4b80e5SWill Deacon 			struct {
123ac4b80e5SWill Deacon 				u32	ps:3;
124ac4b80e5SWill Deacon 				u32	tg:2;
125ac4b80e5SWill Deacon 				u32	sh:2;
126ac4b80e5SWill Deacon 				u32	orgn:2;
127ac4b80e5SWill Deacon 				u32	irgn:2;
128ac4b80e5SWill Deacon 				u32	sl:2;
129ac4b80e5SWill Deacon 				u32	tsz:6;
130ac4b80e5SWill Deacon 			}	vtcr;
131b77cf11fSRob Herring 		} arm_lpae_s2_cfg;
132b77cf11fSRob Herring 
133b77cf11fSRob Herring 		struct {
134d1e5f26fSRobin Murphy 			u32	ttbr;
135b77cf11fSRob Herring 			u32	tcr;
136b77cf11fSRob Herring 			u32	nmrr;
137b77cf11fSRob Herring 			u32	prrr;
138b77cf11fSRob Herring 		} arm_v7s_cfg;
139d08d42deSRob Herring 
140d08d42deSRob Herring 		struct {
141d08d42deSRob Herring 			u64	transtab;
142d08d42deSRob Herring 			u64	memattr;
143d08d42deSRob Herring 		} arm_mali_lpae_cfg;
144b77cf11fSRob Herring 	};
145b77cf11fSRob Herring };
146b77cf11fSRob Herring 
147b77cf11fSRob Herring /**
148b77cf11fSRob Herring  * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
149b77cf11fSRob Herring  *
150b77cf11fSRob Herring  * @map:          Map a physically contiguous memory region.
151b77cf11fSRob Herring  * @unmap:        Unmap a physically contiguous memory region.
152b77cf11fSRob Herring  * @iova_to_phys: Translate iova to physical address.
153b77cf11fSRob Herring  *
154b77cf11fSRob Herring  * These functions map directly onto the iommu_ops member functions with
155b77cf11fSRob Herring  * the same names.
156b77cf11fSRob Herring  */
157b77cf11fSRob Herring struct io_pgtable_ops {
158b77cf11fSRob Herring 	int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
159f34ce7a7SBaolin Wang 		   phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
160b77cf11fSRob Herring 	size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
161a2d3a382SWill Deacon 			size_t size, struct iommu_iotlb_gather *gather);
162b77cf11fSRob Herring 	phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
163b77cf11fSRob Herring 				    unsigned long iova);
164b77cf11fSRob Herring };
165b77cf11fSRob Herring 
166b77cf11fSRob Herring /**
167b77cf11fSRob Herring  * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
168b77cf11fSRob Herring  *
169b77cf11fSRob Herring  * @fmt:    The page table format.
170b77cf11fSRob Herring  * @cfg:    The page table configuration. This will be modified to represent
171b77cf11fSRob Herring  *          the configuration actually provided by the allocator (e.g. the
172b77cf11fSRob Herring  *          pgsize_bitmap may be restricted).
173b77cf11fSRob Herring  * @cookie: An opaque token provided by the IOMMU driver and passed back to
174b77cf11fSRob Herring  *          the callback routines in cfg->tlb.
175b77cf11fSRob Herring  */
176b77cf11fSRob Herring struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
177b77cf11fSRob Herring 					    struct io_pgtable_cfg *cfg,
178b77cf11fSRob Herring 					    void *cookie);
179b77cf11fSRob Herring 
180b77cf11fSRob Herring /**
181b77cf11fSRob Herring  * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
182b77cf11fSRob Herring  *                         *must* ensure that the page table is no longer
183b77cf11fSRob Herring  *                         live, but the TLB can be dirty.
184b77cf11fSRob Herring  *
185b77cf11fSRob Herring  * @ops: The ops returned from alloc_io_pgtable_ops.
186b77cf11fSRob Herring  */
187b77cf11fSRob Herring void free_io_pgtable_ops(struct io_pgtable_ops *ops);
188b77cf11fSRob Herring 
189b77cf11fSRob Herring 
190b77cf11fSRob Herring /*
191b77cf11fSRob Herring  * Internal structures for page table allocator implementations.
192b77cf11fSRob Herring  */
193b77cf11fSRob Herring 
194b77cf11fSRob Herring /**
195b77cf11fSRob Herring  * struct io_pgtable - Internal structure describing a set of page tables.
196b77cf11fSRob Herring  *
197b77cf11fSRob Herring  * @fmt:    The page table format.
198b77cf11fSRob Herring  * @cookie: An opaque token provided by the IOMMU driver and passed back to
199b77cf11fSRob Herring  *          any callback routines.
200b77cf11fSRob Herring  * @cfg:    A copy of the page table configuration.
201b77cf11fSRob Herring  * @ops:    The page table operations in use for this set of page tables.
202b77cf11fSRob Herring  */
203b77cf11fSRob Herring struct io_pgtable {
204b77cf11fSRob Herring 	enum io_pgtable_fmt	fmt;
205b77cf11fSRob Herring 	void			*cookie;
206b77cf11fSRob Herring 	struct io_pgtable_cfg	cfg;
207b77cf11fSRob Herring 	struct io_pgtable_ops	ops;
208b77cf11fSRob Herring };
209b77cf11fSRob Herring 
210b77cf11fSRob Herring #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
211b77cf11fSRob Herring 
212a7656ecfSSai Prakash Ranjan struct io_pgtable_domain_attr {
213a7656ecfSSai Prakash Ranjan 	unsigned long quirks;
214a7656ecfSSai Prakash Ranjan };
215a7656ecfSSai Prakash Ranjan 
216b77cf11fSRob Herring static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
217b77cf11fSRob Herring {
218b77cf11fSRob Herring 	iop->cfg.tlb->tlb_flush_all(iop->cookie);
219b77cf11fSRob Herring }
220b77cf11fSRob Herring 
22110b7a7d9SWill Deacon static inline void
22210b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova,
22310b7a7d9SWill Deacon 			  size_t size, size_t granule)
224b77cf11fSRob Herring {
22510b7a7d9SWill Deacon 	iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie);
226b77cf11fSRob Herring }
227b77cf11fSRob Herring 
22810b7a7d9SWill Deacon static inline void
2293951c41aSWill Deacon io_pgtable_tlb_add_page(struct io_pgtable *iop,
2303951c41aSWill Deacon 			struct iommu_iotlb_gather * gather, unsigned long iova,
231abfd6fe0SWill Deacon 			size_t granule)
232b77cf11fSRob Herring {
233abfd6fe0SWill Deacon 	if (iop->cfg.tlb->tlb_add_page)
2343951c41aSWill Deacon 		iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
235b77cf11fSRob Herring }
236b77cf11fSRob Herring 
237b77cf11fSRob Herring /**
238b77cf11fSRob Herring  * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
239b77cf11fSRob Herring  *                              particular format.
240b77cf11fSRob Herring  *
241b77cf11fSRob Herring  * @alloc: Allocate a set of page tables described by cfg.
242b77cf11fSRob Herring  * @free:  Free the page tables associated with iop.
243b77cf11fSRob Herring  */
244b77cf11fSRob Herring struct io_pgtable_init_fns {
245b77cf11fSRob Herring 	struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
246b77cf11fSRob Herring 	void (*free)(struct io_pgtable *iop);
247b77cf11fSRob Herring };
248b77cf11fSRob Herring 
249b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
250b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
251b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
252b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
253b77cf11fSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
254d08d42deSRob Herring extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
255*c9b258c6SSuravee Suthikulpanit extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns;
256b77cf11fSRob Herring 
257b77cf11fSRob Herring #endif /* __IO_PGTABLE_H */
258