blob: b5a450a3bb47a6f25beafa9510902af070f14dbd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Will Deaconfdb1d7b2014-11-14 17:16:49 +00002#ifndef __IO_PGTABLE_H
3#define __IO_PGTABLE_H
Robin Murphye5fc9752016-01-26 17:13:13 +00004#include <linux/bitops.h>
Will Deaconfdb1d7b2014-11-14 17:16:49 +00005
6/*
7 * Public API for use by IOMMU drivers
8 */
9enum io_pgtable_fmt {
Will Deacone1d3c0f2014-11-14 17:18:23 +000010 ARM_32_LPAE_S1,
11 ARM_32_LPAE_S2,
12 ARM_64_LPAE_S1,
13 ARM_64_LPAE_S2,
Robin Murphye5fc9752016-01-26 17:13:13 +000014 ARM_V7S,
Rob Herringd08d42d2019-02-21 14:23:25 -060015 ARM_MALI_LPAE,
Will Deaconfdb1d7b2014-11-14 17:16:49 +000016 IO_PGTABLE_NUM_FMTS,
17};
18
19/**
20 * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management.
21 *
22 * @tlb_flush_all: Synchronously invalidate the entire TLB context.
23 * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.
Robin Murphy87a91b12015-07-29 19:46:09 +010024 * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and
25 * any corresponding page table updates are visible to the
26 * IOMMU.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000027 *
28 * Note that these can all be called in atomic context and must therefore
29 * not block.
30 */
31struct iommu_gather_ops {
32 void (*tlb_flush_all)(void *cookie);
Robin Murphy06c610e2015-12-07 18:18:53 +000033 void (*tlb_add_flush)(unsigned long iova, size_t size, size_t granule,
34 bool leaf, void *cookie);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000035 void (*tlb_sync)(void *cookie);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000036};
37
38/**
39 * struct io_pgtable_cfg - Configuration data for a set of page tables.
40 *
41 * @quirks: A bitmap of hardware quirks that require some special
42 * action by the low-level page table allocator.
43 * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
44 * tables.
45 * @ias: Input address (iova) size, in bits.
46 * @oas: Output address (paddr) size, in bits.
Will Deacon4f418452019-06-25 12:51:25 +010047 * @coherent_walk A flag to indicate whether or not page table walks made
48 * by the IOMMU are coherent with the CPU caches.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000049 * @tlb: TLB management callbacks for this set of tables.
Robin Murphyf8d54962015-07-29 19:46:04 +010050 * @iommu_dev: The device representing the DMA configuration for the
51 * page table walker.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000052 */
53struct io_pgtable_cfg {
Robin Murphy3850db42016-02-12 17:09:46 +000054 /*
55 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
56 * stage 1 PTEs, for hardware which insists on validating them
57 * even in non-secure state where they should normally be ignored.
58 *
59 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
60 * IOMMU_NOEXEC flags and map everything with full access, for
61 * hardware which does not implement the permissions of a given
62 * format, and/or requires some format-specific default value.
63 *
64 * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid
65 * (unmapped) entries but the hardware might do so anyway, perform
66 * TLB maintenance when mapping as well as when unmapping.
Yong Wu1afe2312016-03-14 06:01:10 +080067 *
68 * IO_PGTABLE_QUIRK_ARM_MTK_4GB: (ARM v7s format) Set bit 9 in all
69 * PTEs, for Mediatek IOMMUs which treat it as a 33rd address bit
70 * when the SoC is in "4GB mode" and they can only access the high
71 * remap of DRAM (0x1_00000000 to 0x1_ffffffff).
Robin Murphy81b3c252017-06-22 16:53:53 +010072 *
Zhen Leib6b65ca2018-09-20 17:10:24 +010073 * IO_PGTABLE_QUIRK_NON_STRICT: Skip issuing synchronous leaf TLBIs
74 * on unmap, for DMA domains using the flush queue mechanism for
75 * delayed invalidation.
Robin Murphy3850db42016-02-12 17:09:46 +000076 */
77 #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
78 #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
79 #define IO_PGTABLE_QUIRK_TLBI_ON_MAP BIT(2)
Yong Wu1afe2312016-03-14 06:01:10 +080080 #define IO_PGTABLE_QUIRK_ARM_MTK_4GB BIT(3)
Will Deacon4f418452019-06-25 12:51:25 +010081 #define IO_PGTABLE_QUIRK_NON_STRICT BIT(4)
Robin Murphy3850db42016-02-12 17:09:46 +000082 unsigned long quirks;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000083 unsigned long pgsize_bitmap;
84 unsigned int ias;
85 unsigned int oas;
Will Deacon4f418452019-06-25 12:51:25 +010086 bool coherent_walk;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000087 const struct iommu_gather_ops *tlb;
Robin Murphyf8d54962015-07-29 19:46:04 +010088 struct device *iommu_dev;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000089
90 /* Low-level data specific to the table format */
91 union {
Will Deacone1d3c0f2014-11-14 17:18:23 +000092 struct {
93 u64 ttbr[2];
94 u64 tcr;
95 u64 mair[2];
96 } arm_lpae_s1_cfg;
97
98 struct {
99 u64 vttbr;
100 u64 vtcr;
101 } arm_lpae_s2_cfg;
Robin Murphye5fc9752016-01-26 17:13:13 +0000102
103 struct {
104 u32 ttbr[2];
105 u32 tcr;
106 u32 nmrr;
107 u32 prrr;
108 } arm_v7s_cfg;
Rob Herringd08d42d2019-02-21 14:23:25 -0600109
110 struct {
111 u64 transtab;
112 u64 memattr;
113 } arm_mali_lpae_cfg;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000114 };
115};
116
117/**
118 * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
119 *
120 * @map: Map a physically contiguous memory region.
121 * @unmap: Unmap a physically contiguous memory region.
122 * @iova_to_phys: Translate iova to physical address.
123 *
124 * These functions map directly onto the iommu_ops member functions with
125 * the same names.
126 */
127struct io_pgtable_ops {
128 int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
129 phys_addr_t paddr, size_t size, int prot);
Vivek Gautam193e67c2018-02-05 23:29:19 +0530130 size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
131 size_t size);
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000132 phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
133 unsigned long iova);
134};
135
136/**
137 * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
138 *
139 * @fmt: The page table format.
140 * @cfg: The page table configuration. This will be modified to represent
141 * the configuration actually provided by the allocator (e.g. the
142 * pgsize_bitmap may be restricted).
143 * @cookie: An opaque token provided by the IOMMU driver and passed back to
144 * the callback routines in cfg->tlb.
145 */
146struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
147 struct io_pgtable_cfg *cfg,
148 void *cookie);
149
150/**
151 * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
152 * *must* ensure that the page table is no longer
153 * live, but the TLB can be dirty.
154 *
155 * @ops: The ops returned from alloc_io_pgtable_ops.
156 */
157void free_io_pgtable_ops(struct io_pgtable_ops *ops);
158
159
160/*
161 * Internal structures for page table allocator implementations.
162 */
163
164/**
165 * struct io_pgtable - Internal structure describing a set of page tables.
166 *
167 * @fmt: The page table format.
168 * @cookie: An opaque token provided by the IOMMU driver and passed back to
169 * any callback routines.
170 * @cfg: A copy of the page table configuration.
171 * @ops: The page table operations in use for this set of page tables.
172 */
173struct io_pgtable {
174 enum io_pgtable_fmt fmt;
175 void *cookie;
176 struct io_pgtable_cfg cfg;
177 struct io_pgtable_ops ops;
178};
179
Robin Murphyfdc38962015-12-04 17:53:01 +0000180#define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
181
Robin Murphy507e4c92016-01-26 17:13:14 +0000182static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
183{
184 iop->cfg.tlb->tlb_flush_all(iop->cookie);
185}
186
187static inline void io_pgtable_tlb_add_flush(struct io_pgtable *iop,
188 unsigned long iova, size_t size, size_t granule, bool leaf)
189{
190 iop->cfg.tlb->tlb_add_flush(iova, size, granule, leaf, iop->cookie);
191}
192
193static inline void io_pgtable_tlb_sync(struct io_pgtable *iop)
194{
Robin Murphy2984f7f2017-07-06 17:55:31 +0100195 iop->cfg.tlb->tlb_sync(iop->cookie);
Robin Murphy507e4c92016-01-26 17:13:14 +0000196}
197
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000198/**
199 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
200 * particular format.
201 *
202 * @alloc: Allocate a set of page tables described by cfg.
203 * @free: Free the page tables associated with iop.
204 */
205struct io_pgtable_init_fns {
206 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
207 void (*free)(struct io_pgtable *iop);
208};
209
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200210extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
211extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
212extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
213extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
Robin Murphye5fc9752016-01-26 17:13:13 +0000214extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
Rob Herringd08d42d2019-02-21 14:23:25 -0600215extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200216
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000217#endif /* __IO_PGTABLE_H */