blob: 17f0d7a1c2f1c6b37f4adbe7ea89a68381ee6b18 [file] [log] [blame]
Will Deacone1d3c0f2014-11-14 17:18:23 +00001/*
2 * CPU-agnostic ARM page table allocator.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Copyright (C) 2014 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
21#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
22
23#include <linux/iommu.h>
24#include <linux/kernel.h>
Mitchel Humpherysdaab0412015-04-23 16:19:05 -070025#include <linux/scatterlist.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000026#include <linux/sizes.h>
27#include <linux/slab.h>
28#include <linux/types.h>
Lada Trimasova8f6aff92016-01-27 11:10:32 +000029#include <linux/dma-mapping.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000030
Robin Murphy87a91b12015-07-29 19:46:09 +010031#include <asm/barrier.h>
32
Will Deacone1d3c0f2014-11-14 17:18:23 +000033#include "io-pgtable.h"
34
35#define ARM_LPAE_MAX_ADDR_BITS 48
36#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
37#define ARM_LPAE_MAX_LEVELS 4
38
39/* Struct accessors */
40#define io_pgtable_to_data(x) \
41 container_of((x), struct arm_lpae_io_pgtable, iop)
42
Will Deacone1d3c0f2014-11-14 17:18:23 +000043#define io_pgtable_ops_to_data(x) \
44 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
45
46/*
47 * For consistency with the architecture, we always consider
48 * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
49 */
50#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
51
52/*
53 * Calculate the right shift amount to get to the portion describing level l
54 * in a virtual address mapped by the pagetable in d.
55 */
56#define ARM_LPAE_LVL_SHIFT(l,d) \
57 ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
58 * (d)->bits_per_level) + (d)->pg_shift)
59
Robin Murphy06c610e2015-12-07 18:18:53 +000060#define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift)
61
Will Deacon367bd972015-02-16 18:38:20 +000062#define ARM_LPAE_PAGES_PER_PGD(d) \
Robin Murphy06c610e2015-12-07 18:18:53 +000063 DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000064
65/*
66 * Calculate the index at level l used to map virtual address a using the
67 * pagetable in d.
68 */
69#define ARM_LPAE_PGD_IDX(l,d) \
70 ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
71
72#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000073 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000074 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
75
76/* Calculate the block/page mapping size at level l for pagetable in d. */
77#define ARM_LPAE_BLOCK_SIZE(l,d) \
78 (1 << (ilog2(sizeof(arm_lpae_iopte)) + \
79 ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
80
81/* Page table bits */
82#define ARM_LPAE_PTE_TYPE_SHIFT 0
83#define ARM_LPAE_PTE_TYPE_MASK 0x3
84
85#define ARM_LPAE_PTE_TYPE_BLOCK 1
86#define ARM_LPAE_PTE_TYPE_TABLE 3
87#define ARM_LPAE_PTE_TYPE_PAGE 3
88
Laurent Pinchartc896c132014-12-14 23:34:50 +020089#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000090#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
91#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
92#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
93#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
94#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c132014-12-14 23:34:50 +020095#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000096#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
97
98#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
99/* Ignore the contiguous bit for block splitting */
100#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
101#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
102 ARM_LPAE_PTE_ATTR_HI_MASK)
103
104/* Stage-1 PTE */
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600105#define ARM_LPAE_PTE_AP_PRIV_RW (((arm_lpae_iopte)0) << 6)
106#define ARM_LPAE_PTE_AP_RW (((arm_lpae_iopte)1) << 6)
107#define ARM_LPAE_PTE_AP_PRIV_RO (((arm_lpae_iopte)2) << 6)
108#define ARM_LPAE_PTE_AP_RO (((arm_lpae_iopte)3) << 6)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000109#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
110#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
111
112/* Stage-2 PTE */
113#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
114#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
115#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
116#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
117#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
118#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
119
120/* Register bits */
121#define ARM_32_LPAE_TCR_EAE (1 << 31)
122#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
123
Will Deacon63979b82015-03-18 10:22:18 +0000124#define ARM_LPAE_TCR_EPD1 (1 << 23)
125
Will Deacone1d3c0f2014-11-14 17:18:23 +0000126#define ARM_LPAE_TCR_TG0_4K (0 << 14)
127#define ARM_LPAE_TCR_TG0_64K (1 << 14)
128#define ARM_LPAE_TCR_TG0_16K (2 << 14)
129
130#define ARM_LPAE_TCR_SH0_SHIFT 12
131#define ARM_LPAE_TCR_SH0_MASK 0x3
132#define ARM_LPAE_TCR_SH_NS 0
133#define ARM_LPAE_TCR_SH_OS 2
134#define ARM_LPAE_TCR_SH_IS 3
135
136#define ARM_LPAE_TCR_ORGN0_SHIFT 10
137#define ARM_LPAE_TCR_IRGN0_SHIFT 8
138#define ARM_LPAE_TCR_RGN_MASK 0x3
139#define ARM_LPAE_TCR_RGN_NC 0
140#define ARM_LPAE_TCR_RGN_WBWA 1
141#define ARM_LPAE_TCR_RGN_WT 2
142#define ARM_LPAE_TCR_RGN_WB 3
143
144#define ARM_LPAE_TCR_SL0_SHIFT 6
145#define ARM_LPAE_TCR_SL0_MASK 0x3
146
147#define ARM_LPAE_TCR_T0SZ_SHIFT 0
148#define ARM_LPAE_TCR_SZ_MASK 0xf
149
150#define ARM_LPAE_TCR_PS_SHIFT 16
151#define ARM_LPAE_TCR_PS_MASK 0x7
152
153#define ARM_LPAE_TCR_IPS_SHIFT 32
154#define ARM_LPAE_TCR_IPS_MASK 0x7
155
156#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
157#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
158#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
159#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
160#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
161#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
162
163#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
164#define ARM_LPAE_MAIR_ATTR_MASK 0xff
165#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
166#define ARM_LPAE_MAIR_ATTR_NC 0x44
167#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
168#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
169#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
170#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
171
172/* IOPTE accessors */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700173#define iopte_deref(pte, d) \
174 (__va(iopte_val(pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000175 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000176
177#define iopte_type(pte,l) \
178 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
179
180#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
181
182#define iopte_leaf(pte,l) \
183 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
184 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
185 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
186
187#define iopte_to_pfn(pte,d) \
188 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
189
190#define pfn_to_iopte(pfn,d) \
191 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
192
193struct arm_lpae_io_pgtable {
194 struct io_pgtable iop;
195
196 int levels;
197 size_t pgd_size;
198 unsigned long pg_shift;
199 unsigned long bits_per_level;
200
201 void *pgd;
202};
203
204typedef u64 arm_lpae_iopte;
205
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700206/*
207 * We'll use some ignored bits in table entries to keep track of the number
208 * of page mappings beneath the table. The maximum number of entries
209 * beneath any table mapping in armv8 is 8192 (which is possible at the
210 * 2nd- and 3rd-level when using a 64K granule size). The bits at our
211 * disposal are:
212 *
213 * 4k granule: [58..52], [11..2]
214 * 64k granule: [58..52], [15..2]
215 *
216 * [58..52], [11..2] is enough bits for tracking table mappings at any
217 * level for any granule, so we'll use those.
218 */
219#define BOTTOM_IGNORED_MASK 0x3ff
220#define BOTTOM_IGNORED_SHIFT 2
221#define BOTTOM_IGNORED_NUM_BITS 10
222#define TOP_IGNORED_MASK 0x7fULL
223#define TOP_IGNORED_SHIFT 52
224#define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \
225 (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
226
227static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte)
228{
229 return table_pte & ~IOPTE_RESERVED_MASK;
230}
231
232static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte)
233{
234 return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT))
235 >> BOTTOM_IGNORED_SHIFT;
236}
237
238static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte)
239{
240 return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
241 >> TOP_IGNORED_SHIFT;
242}
243
244static int iopte_tblcnt(arm_lpae_iopte table_pte)
245{
246 return (_iopte_bottom_ignored_val(table_pte) |
247 (_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS));
248}
249
250static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val)
251{
252 arm_lpae_iopte pte = iopte_val(*table_pte);
253
254 pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) |
255 (((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS))
256 >> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT);
257 *table_pte = pte;
258}
259
260static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt)
261{
262 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
263
264 current_cnt -= cnt;
265 iopte_tblcnt_set(table_ptep, current_cnt);
266}
267
268static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt)
269{
270 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
271
272 current_cnt += cnt;
273 iopte_tblcnt_set(table_ptep, current_cnt);
274}
275
Will Deaconfe4b9912014-11-17 23:31:12 +0000276static bool selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700277static bool suppress_map_failures;
Will Deaconfe4b9912014-11-17 23:31:12 +0000278
Robin Murphyffcb6d12015-09-17 17:42:16 +0100279static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100280{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100281 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100282}
283
284static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700285 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100286{
287 struct device *dev = cfg->iommu_dev;
288 dma_addr_t dma;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700289 void *pages = io_pgtable_alloc_pages_exact(cfg, cookie, size,
290 gfp | __GFP_ZERO);
Robin Murphyf8d54962015-07-29 19:46:04 +0100291
292 if (!pages)
293 return NULL;
294
Robin Murphy87a91b12015-07-29 19:46:09 +0100295 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100296 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
297 if (dma_mapping_error(dev, dma))
298 goto out_free;
299 /*
300 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100301 * address directly, so if the DMA layer suggests otherwise by
302 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100303 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100304 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100305 goto out_unmap;
306 }
307
308 return pages;
309
310out_unmap:
311 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
312 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
313out_free:
Patrick Dalyc11d1082016-09-01 15:52:44 -0700314 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100315 return NULL;
316}
317
318static void __arm_lpae_free_pages(void *pages, size_t size,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700319 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100320{
Robin Murphy87a91b12015-07-29 19:46:09 +0100321 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100322 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100323 size, DMA_TO_DEVICE);
Patrick Dalyc11d1082016-09-01 15:52:44 -0700324 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100325}
326
327static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100328 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100329{
Robin Murphyf8d54962015-07-29 19:46:04 +0100330 *ptep = pte;
331
Robin Murphy87a91b12015-07-29 19:46:09 +0100332 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100333 dma_sync_single_for_device(cfg->iommu_dev,
334 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100335 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100336}
337
Will Deacone1d3c0f2014-11-14 17:18:23 +0000338static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
339 unsigned long iova, phys_addr_t paddr,
340 arm_lpae_iopte prot, int lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700341 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep,
342 bool flush)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000343{
344 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100345 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000346
Patrick Daly67ba8eb2016-06-27 18:44:42 -0700347 /* We require an unmap first */
Mitchel Humpherys1b0313e2015-09-23 13:56:27 -0700348 if (*ptep & ARM_LPAE_PTE_VALID) {
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700349 BUG_ON(!suppress_map_failures);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000350 return -EEXIST;
Will Deaconfe4b9912014-11-17 23:31:12 +0000351 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000352
Robin Murphyf8d54962015-07-29 19:46:04 +0100353 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200354 pte |= ARM_LPAE_PTE_NS;
355
Will Deacone1d3c0f2014-11-14 17:18:23 +0000356 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
357 pte |= ARM_LPAE_PTE_TYPE_PAGE;
358 else
359 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
360
361 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
362 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
363
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700364 if (flush)
365 __arm_lpae_set_pte(ptep, pte, cfg);
366 else
367 *ptep = pte;
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700368
369 if (prev_ptep)
370 iopte_tblcnt_add(prev_ptep, 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000371 return 0;
372}
373
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700374struct map_state {
375 unsigned long iova_end;
376 unsigned int pgsize;
377 arm_lpae_iopte *pgtable;
378 arm_lpae_iopte *prev_pgtable;
379 arm_lpae_iopte *pte_start;
380 unsigned int num_pte;
381};
382/* map state optimization works at level 3 (the 2nd-to-last level) */
383#define MAP_STATE_LVL 3
384
Will Deacone1d3c0f2014-11-14 17:18:23 +0000385static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
386 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700387 int lvl, arm_lpae_iopte *ptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700388 arm_lpae_iopte *prev_ptep, struct map_state *ms)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000389{
390 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000391 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100392 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700393 void *cookie = data->iop.cookie;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700394 arm_lpae_iopte *pgtable = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000395
396 /* Find our entry at the current level */
397 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
398
399 /* If we can install a leaf entry at this level, then do so */
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700400 if (size == block_size && (size & cfg->pgsize_bitmap)) {
401 if (!ms)
402 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
403 ptep, prev_ptep, true);
404
405 if (lvl == MAP_STATE_LVL) {
406 if (ms->pgtable)
407 dma_sync_single_for_device(
408 cfg->iommu_dev,
409 __arm_lpae_dma_addr(ms->pte_start),
410 ms->num_pte * sizeof(*ptep),
411 DMA_TO_DEVICE);
412
413 ms->iova_end = round_down(iova, SZ_2M) + SZ_2M;
414 ms->pgtable = pgtable;
415 ms->prev_pgtable = prev_ptep;
416 ms->pgsize = size;
417 ms->pte_start = ptep;
418 ms->num_pte = 1;
419 } else {
420 /*
421 * We have some map state from previous page
422 * mappings, but we're about to set up a block
423 * mapping. Flush out the previous page mappings.
424 */
425 if (ms->pgtable)
426 dma_sync_single_for_device(
427 cfg->iommu_dev,
428 __arm_lpae_dma_addr(ms->pte_start),
429 ms->num_pte * sizeof(*ptep),
430 DMA_TO_DEVICE);
431 memset(ms, 0, sizeof(*ms));
432 ms = NULL;
433 }
434
435 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
436 ptep, prev_ptep, ms == NULL);
437 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000438
439 /* We can't allocate tables at the final level */
440 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
441 return -EINVAL;
442
443 /* Grab a pointer to the next level */
444 pte = *ptep;
445 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000446 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Patrick Dalyc11d1082016-09-01 15:52:44 -0700447 GFP_ATOMIC, cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000448 if (!cptep)
449 return -ENOMEM;
450
Will Deacone1d3c0f2014-11-14 17:18:23 +0000451 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100452 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200453 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100454 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000455 } else {
456 cptep = iopte_deref(pte, data);
457 }
458
459 /* Rinse, repeat */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700460 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700461 ptep, ms);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000462}
463
464static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
465 int prot)
466{
467 arm_lpae_iopte pte;
468
469 if (data->iop.fmt == ARM_64_LPAE_S1 ||
470 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600471 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000472
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600473 if (prot & IOMMU_WRITE)
474 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RW
475 : ARM_LPAE_PTE_AP_RW;
476 else
477 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RO
478 : ARM_LPAE_PTE_AP_RO;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000479
Robin Murphyfb948252016-04-05 12:39:31 +0100480 if (prot & IOMMU_MMIO)
481 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
482 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
483 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000484 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
485 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
486 } else {
487 pte = ARM_LPAE_PTE_HAP_FAULT;
488 if (prot & IOMMU_READ)
489 pte |= ARM_LPAE_PTE_HAP_READ;
490 if (prot & IOMMU_WRITE)
491 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100492 if (prot & IOMMU_MMIO)
493 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
494 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000495 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
496 else
497 pte |= ARM_LPAE_PTE_MEMATTR_NC;
498 }
499
500 if (prot & IOMMU_NOEXEC)
501 pte |= ARM_LPAE_PTE_XN;
502
503 return pte;
504}
505
506static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
507 phys_addr_t paddr, size_t size, int iommu_prot)
508{
509 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
510 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100511 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000512 arm_lpae_iopte prot;
513
514 /* If no access, then nothing to do */
515 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
516 return 0;
517
518 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700519 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL,
520 NULL);
Robin Murphy87a91b12015-07-29 19:46:09 +0100521 /*
522 * Synchronise all PTE updates for the new mapping before there's
523 * a chance for anything to kick off a table walk for the new iova.
524 */
525 wmb();
526
527 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000528}
529
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700530static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
531 struct scatterlist *sg, unsigned int nents,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700532 int iommu_prot, size_t *size)
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700533{
534 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
535 arm_lpae_iopte *ptep = data->pgd;
536 int lvl = ARM_LPAE_START_LVL(data);
537 arm_lpae_iopte prot;
538 struct scatterlist *s;
539 size_t mapped = 0;
540 int i, ret;
541 unsigned int min_pagesz;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700542 struct io_pgtable_cfg *cfg = &data->iop.cfg;
543 struct map_state ms;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700544
545 /* If no access, then nothing to do */
546 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700547 goto out_err;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700548
549 prot = arm_lpae_prot_to_pte(data, iommu_prot);
550
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700551 min_pagesz = 1 << __ffs(cfg->pgsize_bitmap);
552
553 memset(&ms, 0, sizeof(ms));
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700554
555 for_each_sg(sg, s, nents, i) {
556 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
557 size_t size = s->length;
558
559 /*
560 * We are mapping on IOMMU page boundaries, so offset within
561 * the page must be 0. However, the IOMMU may support pages
562 * smaller than PAGE_SIZE, so s->offset may still represent
563 * an offset of that boundary within the CPU page.
564 */
565 if (!IS_ALIGNED(s->offset, min_pagesz))
566 goto out_err;
567
568 while (size) {
569 size_t pgsize = iommu_pgsize(
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700570 cfg->pgsize_bitmap, iova | phys, size);
571
572 if (ms.pgtable && (iova < ms.iova_end)) {
573 arm_lpae_iopte *ptep = ms.pgtable +
574 ARM_LPAE_LVL_IDX(iova, MAP_STATE_LVL,
575 data);
576 arm_lpae_init_pte(
577 data, iova, phys, prot, MAP_STATE_LVL,
578 ptep, ms.prev_pgtable, false);
579 ms.num_pte++;
580 } else {
581 ret = __arm_lpae_map(data, iova, phys, pgsize,
582 prot, lvl, ptep, NULL, &ms);
583 if (ret)
584 goto out_err;
585 }
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700586
587 iova += pgsize;
588 mapped += pgsize;
589 phys += pgsize;
590 size -= pgsize;
591 }
592 }
593
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700594 if (ms.pgtable)
595 dma_sync_single_for_device(
596 cfg->iommu_dev, __arm_lpae_dma_addr(ms.pte_start),
597 ms.num_pte * sizeof(*ptep), DMA_TO_DEVICE);
598
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700599 return mapped;
600
601out_err:
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700602 /* Return the size of the partial mapping so that they can be undone */
603 *size = mapped;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700604 return 0;
605}
606
Will Deacone1d3c0f2014-11-14 17:18:23 +0000607static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
608 arm_lpae_iopte *ptep)
609{
610 arm_lpae_iopte *start, *end;
611 unsigned long table_size;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700612 void *cookie = data->iop.cookie;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000613
Will Deacone1d3c0f2014-11-14 17:18:23 +0000614 if (lvl == ARM_LPAE_START_LVL(data))
615 table_size = data->pgd_size;
616 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000617 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000618
619 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000620
621 /* Only leaf entries at the last level */
622 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
623 end = ptep;
624 else
625 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000626
627 while (ptep != end) {
628 arm_lpae_iopte pte = *ptep++;
629
630 if (!pte || iopte_leaf(pte, lvl))
631 continue;
632
633 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
634 }
635
Patrick Dalyc11d1082016-09-01 15:52:44 -0700636 __arm_lpae_free_pages(start, table_size, &data->iop.cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000637}
638
639static void arm_lpae_free_pgtable(struct io_pgtable *iop)
640{
641 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
642
643 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
644 kfree(data);
645}
646
647static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
648 unsigned long iova, size_t size,
649 arm_lpae_iopte prot, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700650 arm_lpae_iopte *ptep,
651 arm_lpae_iopte *prev_ptep, size_t blk_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000652{
653 unsigned long blk_start, blk_end;
654 phys_addr_t blk_paddr;
655 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000656
657 blk_start = iova & ~(blk_size - 1);
658 blk_end = blk_start + blk_size;
659 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700660 size = ARM_LPAE_BLOCK_SIZE(lvl + 1, data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000661
662 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
663 arm_lpae_iopte *tablep;
664
665 /* Unmap! */
666 if (blk_start == iova)
667 continue;
668
669 /* __arm_lpae_map expects a pointer to the start of the table */
670 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
671 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700672 tablep, prev_ptep, NULL) < 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000673 if (table) {
674 /* Free the table we allocated */
675 tablep = iopte_deref(table, data);
676 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
677 }
678 return 0; /* Bytes unmapped */
679 }
680 }
681
Robin Murphy507e4c92016-01-26 17:13:14 +0000682 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000683 return size;
684}
685
686static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
687 unsigned long iova, size_t size, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700688 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000689{
690 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000691 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000692 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
693
Robin Murphy2eb97c72015-12-04 17:52:58 +0000694 /* Something went horribly wrong and we ran out of page table */
695 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
696 return 0;
697
Will Deacone1d3c0f2014-11-14 17:18:23 +0000698 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
699 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000700 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000701 return 0;
702
703 /* If the size matches this level, we're in the right place */
704 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000705 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000706
707 if (!iopte_leaf(pte, lvl)) {
708 /* Also flush any partial walks */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000709 ptep = iopte_deref(pte, data);
710 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000711 }
712
713 return size;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700714 } else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) {
715 arm_lpae_iopte *table = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700716 arm_lpae_iopte *table_base = table;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700717 int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data);
718 int entry_size = ARM_LPAE_GRANULE(data);
719 int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) / entry_size;
720 int entries = min_t(int, size / entry_size,
721 max_entries - tl_offset);
722 int table_len = entries * sizeof(*table);
723
724 /*
725 * This isn't a block mapping so it must be a table mapping
726 * and since it's the 2nd-to-last level the next level has
727 * to be all page mappings. Zero them all out in one fell
728 * swoop.
729 */
730
731 table += tl_offset;
732
733 memset(table, 0, table_len);
734 dma_sync_single_for_device(iop->cfg.iommu_dev,
735 __arm_lpae_dma_addr(table),
736 table_len, DMA_TO_DEVICE);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700737
738 iopte_tblcnt_sub(ptep, entries);
739 if (!iopte_tblcnt(*ptep)) {
740 /* no valid mappings left under this table. free it. */
741 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
742 io_pgtable_tlb_add_flush(iop, iova,
743 entries * entry_size,
744 ARM_LPAE_GRANULE(data),
745 false);
746 __arm_lpae_free_pgtable(data, lvl + 1, table_base);
747 } else {
748 io_pgtable_tlb_add_flush(iop, iova,
749 entries * entry_size,
750 ARM_LPAE_GRANULE(data),
751 true);
752 }
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700753
754 return entries * entry_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000755 } else if (iopte_leaf(pte, lvl)) {
756 /*
757 * Insert a table at the next level to map the old region,
758 * minus the part we want to unmap
759 */
760 return arm_lpae_split_blk_unmap(data, iova, size,
761 iopte_prot(pte), lvl, ptep,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700762 prev_ptep, blk_size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000763 }
764
765 /* Keep on walkin' */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700766 prev_ptep = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000767 ptep = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700768 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000769}
770
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700771static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000772 size_t size)
773{
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700774 size_t unmapped = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000775 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000776 arm_lpae_iopte *ptep = data->pgd;
777 int lvl = ARM_LPAE_START_LVL(data);
778
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700779 while (unmapped < size) {
780 size_t ret, size_to_unmap, remaining;
781
782 remaining = (size - unmapped);
Patrick Dalyf145f052016-06-27 18:38:09 -0700783 size_to_unmap = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova,
784 remaining);
785 size_to_unmap = size_to_unmap >= SZ_2M ?
786 size_to_unmap :
787 min_t(unsigned long, remaining,
788 (ALIGN(iova + 1, SZ_2M) - iova));
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700789 ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep,
790 NULL);
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700791 if (ret == 0)
792 break;
793 unmapped += ret;
794 iova += ret;
795 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000796 if (unmapped)
Mitchel Humpherysfaa87fc2015-04-24 17:10:59 -0700797 io_pgtable_tlb_flush_all(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000798
799 return unmapped;
800}
801
802static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
803 unsigned long iova)
804{
805 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
806 arm_lpae_iopte pte, *ptep = data->pgd;
807 int lvl = ARM_LPAE_START_LVL(data);
808
809 do {
810 /* Valid IOPTE pointer? */
811 if (!ptep)
812 return 0;
813
814 /* Grab the IOPTE we're interested in */
815 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
816
817 /* Valid entry? */
818 if (!pte)
819 return 0;
820
821 /* Leaf entry? */
822 if (iopte_leaf(pte,lvl))
823 goto found_translation;
824
825 /* Take it to the next level */
826 ptep = iopte_deref(pte, data);
827 } while (++lvl < ARM_LPAE_MAX_LEVELS);
828
829 /* Ran out of page tables to walk */
830 return 0;
831
832found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100833 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000834 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
835}
836
837static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
838{
839 unsigned long granule;
840
841 /*
842 * We need to restrict the supported page sizes to match the
843 * translation regime for a particular granule. Aim to match
844 * the CPU page size if possible, otherwise prefer smaller sizes.
845 * While we're at it, restrict the block sizes to match the
846 * chosen granule.
847 */
848 if (cfg->pgsize_bitmap & PAGE_SIZE)
849 granule = PAGE_SIZE;
850 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
851 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
852 else if (cfg->pgsize_bitmap & PAGE_MASK)
853 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
854 else
855 granule = 0;
856
857 switch (granule) {
858 case SZ_4K:
859 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
860 break;
861 case SZ_16K:
862 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
863 break;
864 case SZ_64K:
865 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
866 break;
867 default:
868 cfg->pgsize_bitmap = 0;
869 }
870}
871
872static struct arm_lpae_io_pgtable *
873arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
874{
875 unsigned long va_bits, pgd_bits;
876 struct arm_lpae_io_pgtable *data;
877
878 arm_lpae_restrict_pgsizes(cfg);
879
880 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
881 return NULL;
882
883 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
884 return NULL;
885
886 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
887 return NULL;
888
Robin Murphyffcb6d12015-09-17 17:42:16 +0100889 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
890 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
891 return NULL;
892 }
893
Will Deacone1d3c0f2014-11-14 17:18:23 +0000894 data = kmalloc(sizeof(*data), GFP_KERNEL);
895 if (!data)
896 return NULL;
897
898 data->pg_shift = __ffs(cfg->pgsize_bitmap);
899 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
900
901 va_bits = cfg->ias - data->pg_shift;
902 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
903
904 /* Calculate the actual size of our pgd (without concatenation) */
905 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
906 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
907
908 data->iop.ops = (struct io_pgtable_ops) {
909 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700910 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000911 .unmap = arm_lpae_unmap,
912 .iova_to_phys = arm_lpae_iova_to_phys,
913 };
914
915 return data;
916}
917
918static struct io_pgtable *
919arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
920{
921 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000922 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000923
Robin Murphy3850db42016-02-12 17:09:46 +0000924 if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS)
925 return NULL;
926
927 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000928 if (!data)
929 return NULL;
930
931 /* TCR */
932 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
933 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
934 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
935
Robin Murphy06c610e2015-12-07 18:18:53 +0000936 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000937 case SZ_4K:
938 reg |= ARM_LPAE_TCR_TG0_4K;
939 break;
940 case SZ_16K:
941 reg |= ARM_LPAE_TCR_TG0_16K;
942 break;
943 case SZ_64K:
944 reg |= ARM_LPAE_TCR_TG0_64K;
945 break;
946 }
947
948 switch (cfg->oas) {
949 case 32:
950 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
951 break;
952 case 36:
953 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
954 break;
955 case 40:
956 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
957 break;
958 case 42:
959 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
960 break;
961 case 44:
962 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
963 break;
964 case 48:
965 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
966 break;
967 default:
968 goto out_free_data;
969 }
970
971 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000972
973 /* Disable speculative walks through TTBR1 */
974 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000975 cfg->arm_lpae_s1_cfg.tcr = reg;
976
977 /* MAIRs */
978 reg = (ARM_LPAE_MAIR_ATTR_NC
979 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
980 (ARM_LPAE_MAIR_ATTR_WBRWA
981 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
982 (ARM_LPAE_MAIR_ATTR_DEVICE
983 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
984
985 cfg->arm_lpae_s1_cfg.mair[0] = reg;
986 cfg->arm_lpae_s1_cfg.mair[1] = 0;
987
988 /* Looking good; allocate a pgd */
Patrick Dalyc11d1082016-09-01 15:52:44 -0700989 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
990 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000991 if (!data->pgd)
992 goto out_free_data;
993
Robin Murphy87a91b12015-07-29 19:46:09 +0100994 /* Ensure the empty pgd is visible before any actual TTBR write */
995 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000996
997 /* TTBRs */
998 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
999 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
1000 return &data->iop;
1001
1002out_free_data:
1003 kfree(data);
1004 return NULL;
1005}
1006
1007static struct io_pgtable *
1008arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1009{
1010 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +00001011 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001012
Robin Murphy3850db42016-02-12 17:09:46 +00001013 /* The NS quirk doesn't apply at stage 2 */
1014 if (cfg->quirks)
1015 return NULL;
1016
1017 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001018 if (!data)
1019 return NULL;
1020
1021 /*
1022 * Concatenate PGDs at level 1 if possible in order to reduce
1023 * the depth of the stage-2 walk.
1024 */
1025 if (data->levels == ARM_LPAE_MAX_LEVELS) {
1026 unsigned long pgd_pages;
1027
1028 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
1029 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
1030 data->pgd_size = pgd_pages << data->pg_shift;
1031 data->levels--;
1032 }
1033 }
1034
1035 /* VTCR */
1036 reg = ARM_64_LPAE_S2_TCR_RES1 |
1037 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
1038 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
1039 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
1040
1041 sl = ARM_LPAE_START_LVL(data);
1042
Robin Murphy06c610e2015-12-07 18:18:53 +00001043 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +00001044 case SZ_4K:
1045 reg |= ARM_LPAE_TCR_TG0_4K;
1046 sl++; /* SL0 format is different for 4K granule size */
1047 break;
1048 case SZ_16K:
1049 reg |= ARM_LPAE_TCR_TG0_16K;
1050 break;
1051 case SZ_64K:
1052 reg |= ARM_LPAE_TCR_TG0_64K;
1053 break;
1054 }
1055
1056 switch (cfg->oas) {
1057 case 32:
1058 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
1059 break;
1060 case 36:
1061 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
1062 break;
1063 case 40:
1064 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
1065 break;
1066 case 42:
1067 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
1068 break;
1069 case 44:
1070 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
1071 break;
1072 case 48:
1073 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
1074 break;
1075 default:
1076 goto out_free_data;
1077 }
1078
1079 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
1080 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
1081 cfg->arm_lpae_s2_cfg.vtcr = reg;
1082
1083 /* Allocate pgd pages */
Patrick Dalyc11d1082016-09-01 15:52:44 -07001084 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
1085 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001086 if (!data->pgd)
1087 goto out_free_data;
1088
Robin Murphy87a91b12015-07-29 19:46:09 +01001089 /* Ensure the empty pgd is visible before any actual TTBR write */
1090 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001091
1092 /* VTTBR */
1093 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1094 return &data->iop;
1095
1096out_free_data:
1097 kfree(data);
1098 return NULL;
1099}
1100
1101static struct io_pgtable *
1102arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1103{
1104 struct io_pgtable *iop;
1105
1106 if (cfg->ias > 32 || cfg->oas > 40)
1107 return NULL;
1108
1109 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1110 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1111 if (iop) {
1112 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
1113 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1114 }
1115
1116 return iop;
1117}
1118
1119static struct io_pgtable *
1120arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1121{
1122 struct io_pgtable *iop;
1123
1124 if (cfg->ias > 40 || cfg->oas > 40)
1125 return NULL;
1126
1127 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1128 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1129 if (iop)
1130 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1131
1132 return iop;
1133}
1134
1135struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1136 .alloc = arm_64_lpae_alloc_pgtable_s1,
1137 .free = arm_lpae_free_pgtable,
1138};
1139
1140struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1141 .alloc = arm_64_lpae_alloc_pgtable_s2,
1142 .free = arm_lpae_free_pgtable,
1143};
1144
1145struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1146 .alloc = arm_32_lpae_alloc_pgtable_s1,
1147 .free = arm_lpae_free_pgtable,
1148};
1149
1150struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1151 .alloc = arm_32_lpae_alloc_pgtable_s2,
1152 .free = arm_lpae_free_pgtable,
1153};
Will Deaconfe4b9912014-11-17 23:31:12 +00001154
1155#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1156
1157static struct io_pgtable_cfg *cfg_cookie;
1158
1159static void dummy_tlb_flush_all(void *cookie)
1160{
1161 WARN_ON(cookie != cfg_cookie);
1162}
1163
Robin Murphy06c610e2015-12-07 18:18:53 +00001164static void dummy_tlb_add_flush(unsigned long iova, size_t size,
1165 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001166{
1167 WARN_ON(cookie != cfg_cookie);
Will Deaconfe4b9912014-11-17 23:31:12 +00001168}
1169
1170static void dummy_tlb_sync(void *cookie)
1171{
1172 WARN_ON(cookie != cfg_cookie);
1173}
1174
Will Deaconfe4b9912014-11-17 23:31:12 +00001175static struct iommu_gather_ops dummy_tlb_ops __initdata = {
1176 .tlb_flush_all = dummy_tlb_flush_all,
1177 .tlb_add_flush = dummy_tlb_add_flush,
1178 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +00001179};
1180
1181static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1182{
1183 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1184 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1185
1186 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1187 cfg->pgsize_bitmap, cfg->ias);
1188 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1189 data->levels, data->pgd_size, data->pg_shift,
1190 data->bits_per_level, data->pgd);
1191}
1192
1193#define __FAIL(ops, i) ({ \
1194 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1195 arm_lpae_dump_ops(ops); \
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001196 suppress_map_failures = false; \
Will Deaconfe4b9912014-11-17 23:31:12 +00001197 selftest_running = false; \
1198 -EFAULT; \
1199})
1200
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001201/*
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001202 * Returns true if there's any mapping in the given iova range in ops.
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001203 */
1204static bool arm_lpae_range_has_mapping(struct io_pgtable_ops *ops,
1205 unsigned long iova_start, size_t size)
1206{
1207 unsigned long iova = iova_start;
1208
1209 while (iova < (iova_start + size)) {
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001210 if (ops->iova_to_phys(ops, iova + 42))
1211 return true;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001212 iova += SZ_4K;
1213 }
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001214 return false;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001215}
1216
1217/*
1218 * Returns true if the iova range is successfully mapped to the contiguous
1219 * phys range in ops.
1220 */
1221static bool arm_lpae_range_has_specific_mapping(struct io_pgtable_ops *ops,
1222 const unsigned long iova_start,
1223 const phys_addr_t phys_start,
1224 const size_t size)
1225{
1226 unsigned long iova = iova_start;
1227 phys_addr_t phys = phys_start;
1228
1229 while (iova < (iova_start + size)) {
1230 if (ops->iova_to_phys(ops, iova + 42) != (phys + 42))
1231 return false;
1232 iova += SZ_4K;
1233 phys += SZ_4K;
1234 }
1235 return true;
1236}
1237
Will Deaconfe4b9912014-11-17 23:31:12 +00001238static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1239{
1240 static const enum io_pgtable_fmt fmts[] = {
1241 ARM_64_LPAE_S1,
1242 ARM_64_LPAE_S2,
1243 };
1244
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001245 int i, j, k;
Will Deaconfe4b9912014-11-17 23:31:12 +00001246 unsigned long iova;
1247 size_t size;
1248 struct io_pgtable_ops *ops;
Will Deaconfe4b9912014-11-17 23:31:12 +00001249 selftest_running = true;
1250
1251 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001252 unsigned long test_sg_sizes[] = { SZ_4K, SZ_64K, SZ_2M,
1253 SZ_1M * 12, SZ_1M * 20 };
1254
Will Deaconfe4b9912014-11-17 23:31:12 +00001255 cfg_cookie = cfg;
1256 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1257 if (!ops) {
1258 pr_err("selftest: failed to allocate io pgtable ops\n");
1259 return -ENOMEM;
1260 }
1261
1262 /*
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001263 * Initial sanity checks. Empty page tables shouldn't
1264 * provide any translations. TODO: check entire supported
1265 * range for these ops rather than first 2G
Will Deaconfe4b9912014-11-17 23:31:12 +00001266 */
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001267 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
Will Deaconfe4b9912014-11-17 23:31:12 +00001268 return __FAIL(ops, i);
1269
1270 /*
1271 * Distinct mappings of different granule sizes.
1272 */
1273 iova = 0;
1274 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1275 while (j != BITS_PER_LONG) {
1276 size = 1UL << j;
1277
1278 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1279 IOMMU_WRITE |
1280 IOMMU_NOEXEC |
1281 IOMMU_CACHE))
1282 return __FAIL(ops, i);
1283
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001284 suppress_map_failures = true;
Will Deaconfe4b9912014-11-17 23:31:12 +00001285 /* Overlapping mappings */
1286 if (!ops->map(ops, iova, iova + size, size,
1287 IOMMU_READ | IOMMU_NOEXEC))
1288 return __FAIL(ops, i);
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001289 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001290
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001291 if (!arm_lpae_range_has_specific_mapping(ops, iova,
1292 iova, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001293 return __FAIL(ops, i);
1294
1295 iova += SZ_1G;
1296 j++;
1297 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1298 }
1299
1300 /* Partial unmap */
1301 size = 1UL << __ffs(cfg->pgsize_bitmap);
1302 if (ops->unmap(ops, SZ_1G + size, size) != size)
1303 return __FAIL(ops, i);
1304
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001305 if (arm_lpae_range_has_mapping(ops, SZ_1G + size, size))
1306 return __FAIL(ops, i);
1307
Will Deaconfe4b9912014-11-17 23:31:12 +00001308 /* Remap of partial unmap */
1309 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1310 return __FAIL(ops, i);
1311
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001312 if (!arm_lpae_range_has_specific_mapping(ops, SZ_1G + size,
1313 size, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001314 return __FAIL(ops, i);
1315
1316 /* Full unmap */
1317 iova = 0;
1318 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1319 while (j != BITS_PER_LONG) {
1320 size = 1UL << j;
1321
1322 if (ops->unmap(ops, iova, size) != size)
1323 return __FAIL(ops, i);
1324
1325 if (ops->iova_to_phys(ops, iova + 42))
1326 return __FAIL(ops, i);
1327
1328 /* Remap full block */
1329 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1330 return __FAIL(ops, i);
1331
1332 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1333 return __FAIL(ops, i);
1334
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001335 if (ops->unmap(ops, iova, size) != size)
1336 return __FAIL(ops, i);
1337
Will Deaconfe4b9912014-11-17 23:31:12 +00001338 iova += SZ_1G;
1339 j++;
1340 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1341 }
1342
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001343 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1344 return __FAIL(ops, i);
1345
Mitchel Humpheryse4012a62015-06-01 15:44:49 -07001346 if ((cfg->pgsize_bitmap & SZ_2M) &&
1347 (cfg->pgsize_bitmap & SZ_4K)) {
1348 /* mixed block + page mappings */
1349 iova = 0;
1350 if (ops->map(ops, iova, iova, SZ_2M, IOMMU_READ))
1351 return __FAIL(ops, i);
1352
1353 if (ops->map(ops, iova + SZ_2M, iova + SZ_2M, SZ_4K,
1354 IOMMU_READ))
1355 return __FAIL(ops, i);
1356
1357 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1358 return __FAIL(ops, i);
1359
1360 if (ops->iova_to_phys(ops, iova + SZ_2M + 42) !=
1361 (iova + SZ_2M + 42))
1362 return __FAIL(ops, i);
1363
1364 /* unmap both mappings at once */
1365 if (ops->unmap(ops, iova, SZ_2M + SZ_4K) !=
1366 (SZ_2M + SZ_4K))
1367 return __FAIL(ops, i);
1368
1369 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1370 return __FAIL(ops, i);
1371 }
1372
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001373 /* map_sg */
1374 for (j = 0; j < ARRAY_SIZE(test_sg_sizes); ++j) {
1375 size_t mapped;
1376 size_t unused;
1377 struct page *page;
1378 phys_addr_t page_phys;
1379 struct sg_table table;
1380 struct scatterlist *sg;
1381 unsigned long total_size = test_sg_sizes[j];
1382 int chunk_size = 1UL << find_first_bit(
1383 &cfg->pgsize_bitmap, BITS_PER_LONG);
1384 int nents = total_size / chunk_size;
1385
1386 if (total_size < chunk_size)
1387 continue;
1388
1389 page = alloc_pages(GFP_KERNEL, get_order(chunk_size));
1390 page_phys = page_to_phys(page);
1391
1392 iova = 0;
1393 BUG_ON(sg_alloc_table(&table, nents, GFP_KERNEL));
1394 BUG_ON(!page);
1395 for_each_sg(table.sgl, sg, table.nents, k)
1396 sg_set_page(sg, page, chunk_size, 0);
1397
1398 mapped = ops->map_sg(ops, iova, table.sgl, table.nents,
1399 IOMMU_READ | IOMMU_WRITE, &unused);
1400
1401 if (mapped != total_size)
1402 return __FAIL(ops, i);
1403
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001404 if (!arm_lpae_range_has_mapping(ops, iova, total_size))
1405 return __FAIL(ops, i);
1406
1407 if (arm_lpae_range_has_mapping(ops, iova + total_size,
1408 SZ_2G - (iova + total_size)))
1409 return __FAIL(ops, i);
1410
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001411 for_each_sg(table.sgl, sg, table.nents, k) {
1412 dma_addr_t newphys =
1413 ops->iova_to_phys(ops, iova + 42);
1414 if (newphys != (page_phys + 42))
1415 return __FAIL(ops, i);
1416 iova += chunk_size;
1417 }
1418
1419 if (ops->unmap(ops, 0, total_size) != total_size)
1420 return __FAIL(ops, i);
1421
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001422 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1423 return __FAIL(ops, i);
1424
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001425 sg_free_table(&table);
1426 __free_pages(page, get_order(chunk_size));
1427 }
1428
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001429 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1430 return __FAIL(ops, i);
1431
Will Deaconfe4b9912014-11-17 23:31:12 +00001432 free_io_pgtable_ops(ops);
1433 }
1434
1435 selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001436 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001437 return 0;
1438}
1439
1440static int __init arm_lpae_do_selftests(void)
1441{
1442 static const unsigned long pgsize[] = {
1443 SZ_4K | SZ_2M | SZ_1G,
Will Deaconfe4b9912014-11-17 23:31:12 +00001444 };
1445
1446 static const unsigned int ias[] = {
1447 32, 36, 40, 42, 44, 48,
1448 };
1449
1450 int i, j, pass = 0, fail = 0;
1451 struct io_pgtable_cfg cfg = {
1452 .tlb = &dummy_tlb_ops,
1453 .oas = 48,
1454 };
1455
1456 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1457 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1458 cfg.pgsize_bitmap = pgsize[i];
1459 cfg.ias = ias[j];
1460 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1461 pgsize[i], ias[j]);
1462 if (arm_lpae_run_tests(&cfg))
1463 fail++;
1464 else
1465 pass++;
1466 }
1467 }
1468
1469 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1470 return fail ? -EFAULT : 0;
1471}
1472subsys_initcall(arm_lpae_do_selftests);
1473#endif