blob: b35016ede9cf909f9f08572ad52c832ae78a04e2 [file] [log] [blame]
Will Deaconfdb1d7b2014-11-14 17:16:49 +00001#ifndef __IO_PGTABLE_H
2#define __IO_PGTABLE_H
Robin Murphye5fc9752016-01-26 17:13:13 +00003#include <linux/bitops.h>
Will Deaconfdb1d7b2014-11-14 17:16:49 +00004
Mitchel Humpheryscd6ff132015-04-23 16:16:56 -07005#include <linux/scatterlist.h>
6
Will Deaconfdb1d7b2014-11-14 17:16:49 +00007/*
8 * Public API for use by IOMMU drivers
9 */
10enum io_pgtable_fmt {
Will Deacone1d3c0f2014-11-14 17:18:23 +000011 ARM_32_LPAE_S1,
12 ARM_32_LPAE_S2,
13 ARM_64_LPAE_S1,
14 ARM_64_LPAE_S2,
Robin Murphye5fc9752016-01-26 17:13:13 +000015 ARM_V7S,
Mitchel Humpherys86a560e2015-09-30 14:23:58 -070016 ARM_V8L_FAST,
Will Deaconfdb1d7b2014-11-14 17:16:49 +000017 IO_PGTABLE_NUM_FMTS,
18};
19
20/**
21 * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management.
22 *
23 * @tlb_flush_all: Synchronously invalidate the entire TLB context.
24 * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.
Robin Murphy87a91b12015-07-29 19:46:09 +010025 * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and
26 * any corresponding page table updates are visible to the
27 * IOMMU.
Patrick Dalyc11d1082016-09-01 15:52:44 -070028 * @alloc_pages_exact: Allocate page table memory (optional, defaults to
29 * alloc_pages_exact)
30 * @free_pages_exact: Free page table memory (optional, defaults to
31 * free_pages_exact)
Will Deaconfdb1d7b2014-11-14 17:16:49 +000032 *
33 * Note that these can all be called in atomic context and must therefore
34 * not block.
35 */
36struct iommu_gather_ops {
37 void (*tlb_flush_all)(void *cookie);
Robin Murphy06c610e2015-12-07 18:18:53 +000038 void (*tlb_add_flush)(unsigned long iova, size_t size, size_t granule,
39 bool leaf, void *cookie);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000040 void (*tlb_sync)(void *cookie);
Patrick Dalyc11d1082016-09-01 15:52:44 -070041 void *(*alloc_pages_exact)(void *cookie, size_t size, gfp_t gfp_mask);
42 void (*free_pages_exact)(void *cookie, void *virt, size_t size);
Will Deaconfdb1d7b2014-11-14 17:16:49 +000043};
44
45/**
46 * struct io_pgtable_cfg - Configuration data for a set of page tables.
47 *
48 * @quirks: A bitmap of hardware quirks that require some special
49 * action by the low-level page table allocator.
50 * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
51 * tables.
52 * @ias: Input address (iova) size, in bits.
53 * @oas: Output address (paddr) size, in bits.
54 * @tlb: TLB management callbacks for this set of tables.
Robin Murphyf8d54962015-07-29 19:46:04 +010055 * @iommu_dev: The device representing the DMA configuration for the
56 * page table walker.
Will Deaconfdb1d7b2014-11-14 17:16:49 +000057 */
58struct io_pgtable_cfg {
Robin Murphy3850db42016-02-12 17:09:46 +000059 /*
60 * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in
61 * stage 1 PTEs, for hardware which insists on validating them
62 * even in non-secure state where they should normally be ignored.
63 *
64 * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and
65 * IOMMU_NOEXEC flags and map everything with full access, for
66 * hardware which does not implement the permissions of a given
67 * format, and/or requires some format-specific default value.
68 *
69 * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid
70 * (unmapped) entries but the hardware might do so anyway, perform
71 * TLB maintenance when mapping as well as when unmapping.
Yong Wu1afe2312016-03-14 06:01:10 +080072 *
73 * IO_PGTABLE_QUIRK_ARM_MTK_4GB: (ARM v7s format) Set bit 9 in all
74 * PTEs, for Mediatek IOMMUs which treat it as a 33rd address bit
75 * when the SoC is in "4GB mode" and they can only access the high
76 * remap of DRAM (0x1_00000000 to 0x1_ffffffff).
Patrick Dalyce6786f2016-11-09 14:19:23 -080077 *
78 * IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT: Override the attributes
79 * set in TCR for the page table walker. Use attributes specified
80 * by the upstream hw instead.
Liam Markb4223cf2016-12-20 11:32:56 -080081 *
82 * IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT: Set the page table as
83 * coherent.
Patrick Daly49ccf332017-09-27 15:10:29 -070084 *
85 * IO_PGTABLE_QUIRK_QSMMUV500_NON_SHAREABLE:
86 * Having page tables which are non coherent, but cached in a
87 * system cache requires SH=Non-Shareable. This applies to the
88 * qsmmuv500 model. For data buffers SH=Non-Shareable is not
89 * required.
Robin Murphy3850db42016-02-12 17:09:46 +000090 */
91 #define IO_PGTABLE_QUIRK_ARM_NS BIT(0)
92 #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1)
93 #define IO_PGTABLE_QUIRK_TLBI_ON_MAP BIT(2)
Yong Wu1afe2312016-03-14 06:01:10 +080094 #define IO_PGTABLE_QUIRK_ARM_MTK_4GB BIT(3)
Patrick Dalyce6786f2016-11-09 14:19:23 -080095 #define IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT BIT(4)
Liam Markb4223cf2016-12-20 11:32:56 -080096 #define IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT BIT(5)
Patrick Daly49ccf332017-09-27 15:10:29 -070097 #define IO_PGTABLE_QUIRK_QSMMUV500_NON_SHAREABLE BIT(6)
Robin Murphy3850db42016-02-12 17:09:46 +000098 unsigned long quirks;
Will Deaconfdb1d7b2014-11-14 17:16:49 +000099 unsigned long pgsize_bitmap;
100 unsigned int ias;
101 unsigned int oas;
102 const struct iommu_gather_ops *tlb;
Robin Murphyf8d54962015-07-29 19:46:04 +0100103 struct device *iommu_dev;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000104
105 /* Low-level data specific to the table format */
106 union {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000107 struct {
108 u64 ttbr[2];
109 u64 tcr;
110 u64 mair[2];
111 } arm_lpae_s1_cfg;
112
113 struct {
114 u64 vttbr;
115 u64 vtcr;
116 } arm_lpae_s2_cfg;
Robin Murphye5fc9752016-01-26 17:13:13 +0000117
118 struct {
119 u32 ttbr[2];
120 u32 tcr;
121 u32 nmrr;
122 u32 prrr;
123 } arm_v7s_cfg;
Mitchel Humpherys86a560e2015-09-30 14:23:58 -0700124
125 struct {
126 u64 ttbr[2];
127 u64 tcr;
128 u64 mair[2];
129 void *pmds;
130 } av8l_fast_cfg;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000131 };
132};
133
134/**
135 * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
136 *
137 * @map: Map a physically contiguous memory region.
Mitchel Humpherys5123b492015-09-30 14:22:39 -0700138 * @map_sg: Map a scatterlist. Returns the number of bytes mapped,
139 * or 0 on failure. The size parameter contains the size
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700140 * of the partial mapping in case of failure.
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000141 * @unmap: Unmap a physically contiguous memory region.
142 * @iova_to_phys: Translate iova to physical address.
143 *
144 * These functions map directly onto the iommu_ops member functions with
145 * the same names.
146 */
147struct io_pgtable_ops {
148 int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
149 phys_addr_t paddr, size_t size, int prot);
Mitchel Humpheryscd6ff132015-04-23 16:16:56 -0700150 int (*map_sg)(struct io_pgtable_ops *ops, unsigned long iova,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700151 struct scatterlist *sg, unsigned int nents,
152 int prot, size_t *size);
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700153 size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000154 size_t size);
155 phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
156 unsigned long iova);
Liam Mark17f31802016-12-09 14:30:10 -0800157 bool (*is_iova_coherent)(struct io_pgtable_ops *ops,
158 unsigned long iova);
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700159 uint64_t (*iova_to_pte)(struct io_pgtable_ops *ops,
160 unsigned long iova);
Liam Mark17f31802016-12-09 14:30:10 -0800161
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000162};
163
164/**
165 * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
166 *
167 * @fmt: The page table format.
168 * @cfg: The page table configuration. This will be modified to represent
169 * the configuration actually provided by the allocator (e.g. the
170 * pgsize_bitmap may be restricted).
171 * @cookie: An opaque token provided by the IOMMU driver and passed back to
172 * the callback routines in cfg->tlb.
173 */
174struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
175 struct io_pgtable_cfg *cfg,
176 void *cookie);
177
178/**
179 * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
180 * *must* ensure that the page table is no longer
181 * live, but the TLB can be dirty.
182 *
183 * @ops: The ops returned from alloc_io_pgtable_ops.
184 */
185void free_io_pgtable_ops(struct io_pgtable_ops *ops);
186
187
188/*
189 * Internal structures for page table allocator implementations.
190 */
191
192/**
193 * struct io_pgtable - Internal structure describing a set of page tables.
194 *
195 * @fmt: The page table format.
196 * @cookie: An opaque token provided by the IOMMU driver and passed back to
197 * any callback routines.
Robin Murphy88492a42016-01-26 17:13:15 +0000198 * @tlb_sync_pending: Private flag for optimising out redundant syncs.
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000199 * @cfg: A copy of the page table configuration.
200 * @ops: The page table operations in use for this set of page tables.
201 */
202struct io_pgtable {
203 enum io_pgtable_fmt fmt;
204 void *cookie;
Robin Murphy88492a42016-01-26 17:13:15 +0000205 bool tlb_sync_pending;
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000206 struct io_pgtable_cfg cfg;
207 struct io_pgtable_ops ops;
208};
209
Robin Murphyfdc38962015-12-04 17:53:01 +0000210#define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops)
211
Robin Murphy507e4c92016-01-26 17:13:14 +0000212static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop)
213{
214 iop->cfg.tlb->tlb_flush_all(iop->cookie);
Robin Murphy88492a42016-01-26 17:13:15 +0000215 iop->tlb_sync_pending = true;
Robin Murphy507e4c92016-01-26 17:13:14 +0000216}
217
218static inline void io_pgtable_tlb_add_flush(struct io_pgtable *iop,
219 unsigned long iova, size_t size, size_t granule, bool leaf)
220{
221 iop->cfg.tlb->tlb_add_flush(iova, size, granule, leaf, iop->cookie);
Robin Murphy88492a42016-01-26 17:13:15 +0000222 iop->tlb_sync_pending = true;
Robin Murphy507e4c92016-01-26 17:13:14 +0000223}
224
225static inline void io_pgtable_tlb_sync(struct io_pgtable *iop)
226{
Robin Murphy88492a42016-01-26 17:13:15 +0000227 if (iop->tlb_sync_pending) {
228 iop->cfg.tlb->tlb_sync(iop->cookie);
229 iop->tlb_sync_pending = false;
230 }
Robin Murphy507e4c92016-01-26 17:13:14 +0000231}
232
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000233/**
234 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
235 * particular format.
236 *
237 * @alloc: Allocate a set of page tables described by cfg.
238 * @free: Free the page tables associated with iop.
239 */
240struct io_pgtable_init_fns {
241 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
242 void (*free)(struct io_pgtable *iop);
243};
244
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200245extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
246extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
247extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
248extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
Robin Murphye5fc9752016-01-26 17:13:13 +0000249extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;
Mitchel Humpherys86a560e2015-09-30 14:23:58 -0700250extern struct io_pgtable_init_fns io_pgtable_av8l_fast_init_fns;
Joerg Roedel2e169bb2015-08-13 12:01:10 +0200251
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -0700252/**
253 * io_pgtable_alloc_pages_exact:
254 * allocate an exact number of physically-contiguous pages.
255 * @size: the number of bytes to allocate
256 * @gfp_mask: GFP flags for the allocation
257 *
258 * Like alloc_pages_exact(), but with some additional accounting for debug
259 * purposes.
260 */
Patrick Dalyc11d1082016-09-01 15:52:44 -0700261void *io_pgtable_alloc_pages_exact(struct io_pgtable_cfg *cfg, void *cookie,
262 size_t size, gfp_t gfp_mask);
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -0700263
264/**
265 * io_pgtable_free_pages_exact:
266 * release memory allocated via io_pgtable_alloc_pages_exact()
267 * @virt: the value returned by alloc_pages_exact.
268 * @size: size of allocation, same value as passed to alloc_pages_exact().
269 *
270 * Like free_pages_exact(), but with some additional accounting for debug
271 * purposes.
272 */
Patrick Dalyc11d1082016-09-01 15:52:44 -0700273void io_pgtable_free_pages_exact(struct io_pgtable_cfg *cfg, void *cookie,
274 void *virt, size_t size);
Mitchel Humpherysf01d6e32015-07-15 18:25:07 -0700275
Will Deaconfdb1d7b2014-11-14 17:16:49 +0000276#endif /* __IO_PGTABLE_H */