blob: 4859da078f3f42343e1c782a710ae7aa58c719f3 [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 */
105#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
106#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
107#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
108#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
109
110/* Stage-2 PTE */
111#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
112#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
113#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
114#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
115#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
116#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
117
118/* Register bits */
119#define ARM_32_LPAE_TCR_EAE (1 << 31)
120#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
121
Will Deacon63979b82015-03-18 10:22:18 +0000122#define ARM_LPAE_TCR_EPD1 (1 << 23)
123
Will Deacone1d3c0f2014-11-14 17:18:23 +0000124#define ARM_LPAE_TCR_TG0_4K (0 << 14)
125#define ARM_LPAE_TCR_TG0_64K (1 << 14)
126#define ARM_LPAE_TCR_TG0_16K (2 << 14)
127
128#define ARM_LPAE_TCR_SH0_SHIFT 12
129#define ARM_LPAE_TCR_SH0_MASK 0x3
130#define ARM_LPAE_TCR_SH_NS 0
131#define ARM_LPAE_TCR_SH_OS 2
132#define ARM_LPAE_TCR_SH_IS 3
133
134#define ARM_LPAE_TCR_ORGN0_SHIFT 10
135#define ARM_LPAE_TCR_IRGN0_SHIFT 8
136#define ARM_LPAE_TCR_RGN_MASK 0x3
137#define ARM_LPAE_TCR_RGN_NC 0
138#define ARM_LPAE_TCR_RGN_WBWA 1
139#define ARM_LPAE_TCR_RGN_WT 2
140#define ARM_LPAE_TCR_RGN_WB 3
141
142#define ARM_LPAE_TCR_SL0_SHIFT 6
143#define ARM_LPAE_TCR_SL0_MASK 0x3
144
145#define ARM_LPAE_TCR_T0SZ_SHIFT 0
146#define ARM_LPAE_TCR_SZ_MASK 0xf
147
148#define ARM_LPAE_TCR_PS_SHIFT 16
149#define ARM_LPAE_TCR_PS_MASK 0x7
150
151#define ARM_LPAE_TCR_IPS_SHIFT 32
152#define ARM_LPAE_TCR_IPS_MASK 0x7
153
154#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
155#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
156#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
157#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
158#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
159#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
160
161#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
162#define ARM_LPAE_MAIR_ATTR_MASK 0xff
163#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
164#define ARM_LPAE_MAIR_ATTR_NC 0x44
165#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
166#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
167#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
168#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
169
170/* IOPTE accessors */
171#define iopte_deref(pte,d) \
172 (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000173 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000174
175#define iopte_type(pte,l) \
176 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
177
178#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
179
180#define iopte_leaf(pte,l) \
181 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
182 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
183 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
184
185#define iopte_to_pfn(pte,d) \
186 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
187
188#define pfn_to_iopte(pfn,d) \
189 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
190
191struct arm_lpae_io_pgtable {
192 struct io_pgtable iop;
193
194 int levels;
195 size_t pgd_size;
196 unsigned long pg_shift;
197 unsigned long bits_per_level;
198
199 void *pgd;
200};
201
202typedef u64 arm_lpae_iopte;
203
Will Deaconfe4b9912014-11-17 23:31:12 +0000204static bool selftest_running = false;
205
Robin Murphyffcb6d12015-09-17 17:42:16 +0100206static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100207{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100208 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100209}
210
211static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
212 struct io_pgtable_cfg *cfg)
213{
214 struct device *dev = cfg->iommu_dev;
215 dma_addr_t dma;
216 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);
217
218 if (!pages)
219 return NULL;
220
Robin Murphy87a91b12015-07-29 19:46:09 +0100221 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100222 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
223 if (dma_mapping_error(dev, dma))
224 goto out_free;
225 /*
226 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100227 * address directly, so if the DMA layer suggests otherwise by
228 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100229 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100230 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100231 goto out_unmap;
232 }
233
234 return pages;
235
236out_unmap:
237 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
238 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
239out_free:
240 free_pages_exact(pages, size);
241 return NULL;
242}
243
244static void __arm_lpae_free_pages(void *pages, size_t size,
245 struct io_pgtable_cfg *cfg)
246{
Robin Murphy87a91b12015-07-29 19:46:09 +0100247 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100248 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100249 size, DMA_TO_DEVICE);
250 free_pages_exact(pages, size);
251}
252
253static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100254 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100255{
Robin Murphyf8d54962015-07-29 19:46:04 +0100256 *ptep = pte;
257
Robin Murphy87a91b12015-07-29 19:46:09 +0100258 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100259 dma_sync_single_for_device(cfg->iommu_dev,
260 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100261 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100262}
263
Will Deaconcf27ec92015-08-11 16:48:32 +0100264static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
265 unsigned long iova, size_t size, int lvl,
266 arm_lpae_iopte *ptep);
267
Will Deacone1d3c0f2014-11-14 17:18:23 +0000268static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
269 unsigned long iova, phys_addr_t paddr,
270 arm_lpae_iopte prot, int lvl,
271 arm_lpae_iopte *ptep)
272{
273 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100274 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000275
Will Deaconfe4b9912014-11-17 23:31:12 +0000276 if (iopte_leaf(*ptep, lvl)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100277 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000278 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000279 return -EEXIST;
Will Deaconcf27ec92015-08-11 16:48:32 +0100280 } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
281 /*
282 * We need to unmap and free the old table before
283 * overwriting it with a block entry.
284 */
285 arm_lpae_iopte *tblp;
286 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
287
288 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
289 if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz))
290 return -EINVAL;
Will Deaconfe4b9912014-11-17 23:31:12 +0000291 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000292
Robin Murphyf8d54962015-07-29 19:46:04 +0100293 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200294 pte |= ARM_LPAE_PTE_NS;
295
Will Deacone1d3c0f2014-11-14 17:18:23 +0000296 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
297 pte |= ARM_LPAE_PTE_TYPE_PAGE;
298 else
299 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
300
301 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
302 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
303
Robin Murphy87a91b12015-07-29 19:46:09 +0100304 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000305 return 0;
306}
307
308static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
309 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
310 int lvl, arm_lpae_iopte *ptep)
311{
312 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000313 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100314 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000315
316 /* Find our entry at the current level */
317 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
318
319 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100320 if (size == block_size && (size & cfg->pgsize_bitmap))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000321 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
322
323 /* We can't allocate tables at the final level */
324 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
325 return -EINVAL;
326
327 /* Grab a pointer to the next level */
328 pte = *ptep;
329 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000330 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Robin Murphyf8d54962015-07-29 19:46:04 +0100331 GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000332 if (!cptep)
333 return -ENOMEM;
334
Will Deacone1d3c0f2014-11-14 17:18:23 +0000335 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100336 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200337 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100338 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000339 } else {
340 cptep = iopte_deref(pte, data);
341 }
342
343 /* Rinse, repeat */
344 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
345}
346
347static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
348 int prot)
349{
350 arm_lpae_iopte pte;
351
352 if (data->iop.fmt == ARM_64_LPAE_S1 ||
353 data->iop.fmt == ARM_32_LPAE_S1) {
354 pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
355
356 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
357 pte |= ARM_LPAE_PTE_AP_RDONLY;
358
Robin Murphyfb948252016-04-05 12:39:31 +0100359 if (prot & IOMMU_MMIO)
360 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
361 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
362 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000363 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
364 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
365 } else {
366 pte = ARM_LPAE_PTE_HAP_FAULT;
367 if (prot & IOMMU_READ)
368 pte |= ARM_LPAE_PTE_HAP_READ;
369 if (prot & IOMMU_WRITE)
370 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100371 if (prot & IOMMU_MMIO)
372 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
373 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000374 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
375 else
376 pte |= ARM_LPAE_PTE_MEMATTR_NC;
377 }
378
379 if (prot & IOMMU_NOEXEC)
380 pte |= ARM_LPAE_PTE_XN;
381
382 return pte;
383}
384
385static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
386 phys_addr_t paddr, size_t size, int iommu_prot)
387{
388 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
389 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100390 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000391 arm_lpae_iopte prot;
392
393 /* If no access, then nothing to do */
394 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
395 return 0;
396
397 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100398 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
399 /*
400 * Synchronise all PTE updates for the new mapping before there's
401 * a chance for anything to kick off a table walk for the new iova.
402 */
403 wmb();
404
405 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000406}
407
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700408static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
409 struct scatterlist *sg, unsigned int nents,
410 int iommu_prot)
411{
412 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
413 arm_lpae_iopte *ptep = data->pgd;
414 int lvl = ARM_LPAE_START_LVL(data);
415 arm_lpae_iopte prot;
416 struct scatterlist *s;
417 size_t mapped = 0;
418 int i, ret;
419 unsigned int min_pagesz;
420
421 /* If no access, then nothing to do */
422 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
423 return 0;
424
425 prot = arm_lpae_prot_to_pte(data, iommu_prot);
426
427 min_pagesz = 1 << __ffs(data->iop.cfg.pgsize_bitmap);
428
429 for_each_sg(sg, s, nents, i) {
430 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
431 size_t size = s->length;
432
433 /*
434 * We are mapping on IOMMU page boundaries, so offset within
435 * the page must be 0. However, the IOMMU may support pages
436 * smaller than PAGE_SIZE, so s->offset may still represent
437 * an offset of that boundary within the CPU page.
438 */
439 if (!IS_ALIGNED(s->offset, min_pagesz))
440 goto out_err;
441
442 while (size) {
443 size_t pgsize = iommu_pgsize(
444 data->iop.cfg.pgsize_bitmap, iova | phys, size);
445 ret = __arm_lpae_map(data, iova, phys, pgsize, prot,
446 lvl, ptep);
447 if (ret)
448 goto out_err;
449
450 iova += pgsize;
451 mapped += pgsize;
452 phys += pgsize;
453 size -= pgsize;
454 }
455 }
456
457 return mapped;
458
459out_err:
460 /* undo mappings already done */
461 if (mapped)
462 ops->unmap(ops, iova, mapped);
463
464 return 0;
465}
466
Will Deacone1d3c0f2014-11-14 17:18:23 +0000467static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
468 arm_lpae_iopte *ptep)
469{
470 arm_lpae_iopte *start, *end;
471 unsigned long table_size;
472
Will Deacone1d3c0f2014-11-14 17:18:23 +0000473 if (lvl == ARM_LPAE_START_LVL(data))
474 table_size = data->pgd_size;
475 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000476 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000477
478 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000479
480 /* Only leaf entries at the last level */
481 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
482 end = ptep;
483 else
484 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000485
486 while (ptep != end) {
487 arm_lpae_iopte pte = *ptep++;
488
489 if (!pte || iopte_leaf(pte, lvl))
490 continue;
491
492 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
493 }
494
Robin Murphyf8d54962015-07-29 19:46:04 +0100495 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000496}
497
498static void arm_lpae_free_pgtable(struct io_pgtable *iop)
499{
500 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
501
502 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
503 kfree(data);
504}
505
506static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
507 unsigned long iova, size_t size,
508 arm_lpae_iopte prot, int lvl,
509 arm_lpae_iopte *ptep, size_t blk_size)
510{
511 unsigned long blk_start, blk_end;
512 phys_addr_t blk_paddr;
513 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000514
515 blk_start = iova & ~(blk_size - 1);
516 blk_end = blk_start + blk_size;
517 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
518
519 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
520 arm_lpae_iopte *tablep;
521
522 /* Unmap! */
523 if (blk_start == iova)
524 continue;
525
526 /* __arm_lpae_map expects a pointer to the start of the table */
527 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
528 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
529 tablep) < 0) {
530 if (table) {
531 /* Free the table we allocated */
532 tablep = iopte_deref(table, data);
533 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
534 }
535 return 0; /* Bytes unmapped */
536 }
537 }
538
Robin Murphy507e4c92016-01-26 17:13:14 +0000539 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000540 iova &= ~(blk_size - 1);
Robin Murphy507e4c92016-01-26 17:13:14 +0000541 io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000542 return size;
543}
544
545static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
546 unsigned long iova, size_t size, int lvl,
547 arm_lpae_iopte *ptep)
548{
549 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000550 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000551 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
552
Robin Murphy2eb97c72015-12-04 17:52:58 +0000553 /* Something went horribly wrong and we ran out of page table */
554 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
555 return 0;
556
Will Deacone1d3c0f2014-11-14 17:18:23 +0000557 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
558 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000559 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000560 return 0;
561
562 /* If the size matches this level, we're in the right place */
563 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000564 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000565
566 if (!iopte_leaf(pte, lvl)) {
567 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000568 io_pgtable_tlb_add_flush(iop, iova, size,
569 ARM_LPAE_GRANULE(data), false);
570 io_pgtable_tlb_sync(iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000571 ptep = iopte_deref(pte, data);
572 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
573 } else {
Robin Murphy507e4c92016-01-26 17:13:14 +0000574 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000575 }
576
577 return size;
578 } else if (iopte_leaf(pte, lvl)) {
579 /*
580 * Insert a table at the next level to map the old region,
581 * minus the part we want to unmap
582 */
583 return arm_lpae_split_blk_unmap(data, iova, size,
584 iopte_prot(pte), lvl, ptep,
585 blk_size);
586 }
587
588 /* Keep on walkin' */
589 ptep = iopte_deref(pte, data);
590 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
591}
592
593static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
594 size_t size)
595{
596 size_t unmapped;
597 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000598 arm_lpae_iopte *ptep = data->pgd;
599 int lvl = ARM_LPAE_START_LVL(data);
600
601 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
602 if (unmapped)
Robin Murphy507e4c92016-01-26 17:13:14 +0000603 io_pgtable_tlb_sync(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000604
605 return unmapped;
606}
607
608static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
609 unsigned long iova)
610{
611 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
612 arm_lpae_iopte pte, *ptep = data->pgd;
613 int lvl = ARM_LPAE_START_LVL(data);
614
615 do {
616 /* Valid IOPTE pointer? */
617 if (!ptep)
618 return 0;
619
620 /* Grab the IOPTE we're interested in */
621 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
622
623 /* Valid entry? */
624 if (!pte)
625 return 0;
626
627 /* Leaf entry? */
628 if (iopte_leaf(pte,lvl))
629 goto found_translation;
630
631 /* Take it to the next level */
632 ptep = iopte_deref(pte, data);
633 } while (++lvl < ARM_LPAE_MAX_LEVELS);
634
635 /* Ran out of page tables to walk */
636 return 0;
637
638found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100639 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000640 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
641}
642
643static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
644{
645 unsigned long granule;
646
647 /*
648 * We need to restrict the supported page sizes to match the
649 * translation regime for a particular granule. Aim to match
650 * the CPU page size if possible, otherwise prefer smaller sizes.
651 * While we're at it, restrict the block sizes to match the
652 * chosen granule.
653 */
654 if (cfg->pgsize_bitmap & PAGE_SIZE)
655 granule = PAGE_SIZE;
656 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
657 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
658 else if (cfg->pgsize_bitmap & PAGE_MASK)
659 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
660 else
661 granule = 0;
662
663 switch (granule) {
664 case SZ_4K:
665 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
666 break;
667 case SZ_16K:
668 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
669 break;
670 case SZ_64K:
671 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
672 break;
673 default:
674 cfg->pgsize_bitmap = 0;
675 }
676}
677
678static struct arm_lpae_io_pgtable *
679arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
680{
681 unsigned long va_bits, pgd_bits;
682 struct arm_lpae_io_pgtable *data;
683
684 arm_lpae_restrict_pgsizes(cfg);
685
686 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
687 return NULL;
688
689 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
690 return NULL;
691
692 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
693 return NULL;
694
Robin Murphyffcb6d12015-09-17 17:42:16 +0100695 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
696 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
697 return NULL;
698 }
699
Will Deacone1d3c0f2014-11-14 17:18:23 +0000700 data = kmalloc(sizeof(*data), GFP_KERNEL);
701 if (!data)
702 return NULL;
703
704 data->pg_shift = __ffs(cfg->pgsize_bitmap);
705 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
706
707 va_bits = cfg->ias - data->pg_shift;
708 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
709
710 /* Calculate the actual size of our pgd (without concatenation) */
711 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
712 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
713
714 data->iop.ops = (struct io_pgtable_ops) {
715 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700716 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000717 .unmap = arm_lpae_unmap,
718 .iova_to_phys = arm_lpae_iova_to_phys,
719 };
720
721 return data;
722}
723
724static struct io_pgtable *
725arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
726{
727 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000728 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000729
Robin Murphy3850db42016-02-12 17:09:46 +0000730 if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS)
731 return NULL;
732
733 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000734 if (!data)
735 return NULL;
736
737 /* TCR */
738 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
739 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
740 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
741
Robin Murphy06c610e2015-12-07 18:18:53 +0000742 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000743 case SZ_4K:
744 reg |= ARM_LPAE_TCR_TG0_4K;
745 break;
746 case SZ_16K:
747 reg |= ARM_LPAE_TCR_TG0_16K;
748 break;
749 case SZ_64K:
750 reg |= ARM_LPAE_TCR_TG0_64K;
751 break;
752 }
753
754 switch (cfg->oas) {
755 case 32:
756 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
757 break;
758 case 36:
759 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
760 break;
761 case 40:
762 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
763 break;
764 case 42:
765 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
766 break;
767 case 44:
768 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
769 break;
770 case 48:
771 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
772 break;
773 default:
774 goto out_free_data;
775 }
776
777 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000778
779 /* Disable speculative walks through TTBR1 */
780 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000781 cfg->arm_lpae_s1_cfg.tcr = reg;
782
783 /* MAIRs */
784 reg = (ARM_LPAE_MAIR_ATTR_NC
785 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
786 (ARM_LPAE_MAIR_ATTR_WBRWA
787 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
788 (ARM_LPAE_MAIR_ATTR_DEVICE
789 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
790
791 cfg->arm_lpae_s1_cfg.mair[0] = reg;
792 cfg->arm_lpae_s1_cfg.mair[1] = 0;
793
794 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100795 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000796 if (!data->pgd)
797 goto out_free_data;
798
Robin Murphy87a91b12015-07-29 19:46:09 +0100799 /* Ensure the empty pgd is visible before any actual TTBR write */
800 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000801
802 /* TTBRs */
803 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
804 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
805 return &data->iop;
806
807out_free_data:
808 kfree(data);
809 return NULL;
810}
811
812static struct io_pgtable *
813arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
814{
815 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000816 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000817
Robin Murphy3850db42016-02-12 17:09:46 +0000818 /* The NS quirk doesn't apply at stage 2 */
819 if (cfg->quirks)
820 return NULL;
821
822 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000823 if (!data)
824 return NULL;
825
826 /*
827 * Concatenate PGDs at level 1 if possible in order to reduce
828 * the depth of the stage-2 walk.
829 */
830 if (data->levels == ARM_LPAE_MAX_LEVELS) {
831 unsigned long pgd_pages;
832
833 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
834 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
835 data->pgd_size = pgd_pages << data->pg_shift;
836 data->levels--;
837 }
838 }
839
840 /* VTCR */
841 reg = ARM_64_LPAE_S2_TCR_RES1 |
842 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
843 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
844 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
845
846 sl = ARM_LPAE_START_LVL(data);
847
Robin Murphy06c610e2015-12-07 18:18:53 +0000848 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000849 case SZ_4K:
850 reg |= ARM_LPAE_TCR_TG0_4K;
851 sl++; /* SL0 format is different for 4K granule size */
852 break;
853 case SZ_16K:
854 reg |= ARM_LPAE_TCR_TG0_16K;
855 break;
856 case SZ_64K:
857 reg |= ARM_LPAE_TCR_TG0_64K;
858 break;
859 }
860
861 switch (cfg->oas) {
862 case 32:
863 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
864 break;
865 case 36:
866 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
867 break;
868 case 40:
869 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
870 break;
871 case 42:
872 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
873 break;
874 case 44:
875 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
876 break;
877 case 48:
878 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
879 break;
880 default:
881 goto out_free_data;
882 }
883
884 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
885 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
886 cfg->arm_lpae_s2_cfg.vtcr = reg;
887
888 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100889 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000890 if (!data->pgd)
891 goto out_free_data;
892
Robin Murphy87a91b12015-07-29 19:46:09 +0100893 /* Ensure the empty pgd is visible before any actual TTBR write */
894 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000895
896 /* VTTBR */
897 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
898 return &data->iop;
899
900out_free_data:
901 kfree(data);
902 return NULL;
903}
904
905static struct io_pgtable *
906arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
907{
908 struct io_pgtable *iop;
909
910 if (cfg->ias > 32 || cfg->oas > 40)
911 return NULL;
912
913 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
914 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
915 if (iop) {
916 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
917 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
918 }
919
920 return iop;
921}
922
923static struct io_pgtable *
924arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
925{
926 struct io_pgtable *iop;
927
928 if (cfg->ias > 40 || cfg->oas > 40)
929 return NULL;
930
931 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
932 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
933 if (iop)
934 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
935
936 return iop;
937}
938
939struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
940 .alloc = arm_64_lpae_alloc_pgtable_s1,
941 .free = arm_lpae_free_pgtable,
942};
943
944struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
945 .alloc = arm_64_lpae_alloc_pgtable_s2,
946 .free = arm_lpae_free_pgtable,
947};
948
949struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
950 .alloc = arm_32_lpae_alloc_pgtable_s1,
951 .free = arm_lpae_free_pgtable,
952};
953
954struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
955 .alloc = arm_32_lpae_alloc_pgtable_s2,
956 .free = arm_lpae_free_pgtable,
957};
Will Deaconfe4b9912014-11-17 23:31:12 +0000958
959#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
960
961static struct io_pgtable_cfg *cfg_cookie;
962
963static void dummy_tlb_flush_all(void *cookie)
964{
965 WARN_ON(cookie != cfg_cookie);
966}
967
Robin Murphy06c610e2015-12-07 18:18:53 +0000968static void dummy_tlb_add_flush(unsigned long iova, size_t size,
969 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +0000970{
971 WARN_ON(cookie != cfg_cookie);
972 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
973}
974
975static void dummy_tlb_sync(void *cookie)
976{
977 WARN_ON(cookie != cfg_cookie);
978}
979
Will Deaconfe4b9912014-11-17 23:31:12 +0000980static struct iommu_gather_ops dummy_tlb_ops __initdata = {
981 .tlb_flush_all = dummy_tlb_flush_all,
982 .tlb_add_flush = dummy_tlb_add_flush,
983 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000984};
985
986static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
987{
988 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
989 struct io_pgtable_cfg *cfg = &data->iop.cfg;
990
991 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
992 cfg->pgsize_bitmap, cfg->ias);
993 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
994 data->levels, data->pgd_size, data->pg_shift,
995 data->bits_per_level, data->pgd);
996}
997
998#define __FAIL(ops, i) ({ \
999 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1000 arm_lpae_dump_ops(ops); \
1001 selftest_running = false; \
1002 -EFAULT; \
1003})
1004
1005static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1006{
1007 static const enum io_pgtable_fmt fmts[] = {
1008 ARM_64_LPAE_S1,
1009 ARM_64_LPAE_S2,
1010 };
1011
1012 int i, j;
1013 unsigned long iova;
1014 size_t size;
1015 struct io_pgtable_ops *ops;
1016
1017 selftest_running = true;
1018
1019 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1020 cfg_cookie = cfg;
1021 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1022 if (!ops) {
1023 pr_err("selftest: failed to allocate io pgtable ops\n");
1024 return -ENOMEM;
1025 }
1026
1027 /*
1028 * Initial sanity checks.
1029 * Empty page tables shouldn't provide any translations.
1030 */
1031 if (ops->iova_to_phys(ops, 42))
1032 return __FAIL(ops, i);
1033
1034 if (ops->iova_to_phys(ops, SZ_1G + 42))
1035 return __FAIL(ops, i);
1036
1037 if (ops->iova_to_phys(ops, SZ_2G + 42))
1038 return __FAIL(ops, i);
1039
1040 /*
1041 * Distinct mappings of different granule sizes.
1042 */
1043 iova = 0;
1044 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1045 while (j != BITS_PER_LONG) {
1046 size = 1UL << j;
1047
1048 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1049 IOMMU_WRITE |
1050 IOMMU_NOEXEC |
1051 IOMMU_CACHE))
1052 return __FAIL(ops, i);
1053
1054 /* Overlapping mappings */
1055 if (!ops->map(ops, iova, iova + size, size,
1056 IOMMU_READ | IOMMU_NOEXEC))
1057 return __FAIL(ops, i);
1058
1059 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1060 return __FAIL(ops, i);
1061
1062 iova += SZ_1G;
1063 j++;
1064 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1065 }
1066
1067 /* Partial unmap */
1068 size = 1UL << __ffs(cfg->pgsize_bitmap);
1069 if (ops->unmap(ops, SZ_1G + size, size) != size)
1070 return __FAIL(ops, i);
1071
1072 /* Remap of partial unmap */
1073 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1074 return __FAIL(ops, i);
1075
1076 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1077 return __FAIL(ops, i);
1078
1079 /* Full unmap */
1080 iova = 0;
1081 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1082 while (j != BITS_PER_LONG) {
1083 size = 1UL << j;
1084
1085 if (ops->unmap(ops, iova, size) != size)
1086 return __FAIL(ops, i);
1087
1088 if (ops->iova_to_phys(ops, iova + 42))
1089 return __FAIL(ops, i);
1090
1091 /* Remap full block */
1092 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1093 return __FAIL(ops, i);
1094
1095 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1096 return __FAIL(ops, i);
1097
1098 iova += SZ_1G;
1099 j++;
1100 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1101 }
1102
1103 free_io_pgtable_ops(ops);
1104 }
1105
1106 selftest_running = false;
1107 return 0;
1108}
1109
1110static int __init arm_lpae_do_selftests(void)
1111{
1112 static const unsigned long pgsize[] = {
1113 SZ_4K | SZ_2M | SZ_1G,
1114 SZ_16K | SZ_32M,
1115 SZ_64K | SZ_512M,
1116 };
1117
1118 static const unsigned int ias[] = {
1119 32, 36, 40, 42, 44, 48,
1120 };
1121
1122 int i, j, pass = 0, fail = 0;
1123 struct io_pgtable_cfg cfg = {
1124 .tlb = &dummy_tlb_ops,
1125 .oas = 48,
1126 };
1127
1128 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1129 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1130 cfg.pgsize_bitmap = pgsize[i];
1131 cfg.ias = ias[j];
1132 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1133 pgsize[i], ias[j]);
1134 if (arm_lpae_run_tests(&cfg))
1135 fail++;
1136 else
1137 pass++;
1138 }
1139 }
1140
1141 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1142 return fail ? -EFAULT : 0;
1143}
1144subsys_initcall(arm_lpae_do_selftests);
1145#endif