blob: 41f98041cec04b4104bd69d5804813dfdde85c5f [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,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700410 int iommu_prot, size_t *size)
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700411{
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)))
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700423 goto out_err;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700424
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:
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700460 /* Return the size of the partial mapping so that they can be undone */
461 *size = mapped;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700462 return 0;
463}
464
Will Deacone1d3c0f2014-11-14 17:18:23 +0000465static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
466 arm_lpae_iopte *ptep)
467{
468 arm_lpae_iopte *start, *end;
469 unsigned long table_size;
470
Will Deacone1d3c0f2014-11-14 17:18:23 +0000471 if (lvl == ARM_LPAE_START_LVL(data))
472 table_size = data->pgd_size;
473 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000474 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000475
476 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000477
478 /* Only leaf entries at the last level */
479 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
480 end = ptep;
481 else
482 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000483
484 while (ptep != end) {
485 arm_lpae_iopte pte = *ptep++;
486
487 if (!pte || iopte_leaf(pte, lvl))
488 continue;
489
490 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
491 }
492
Robin Murphyf8d54962015-07-29 19:46:04 +0100493 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000494}
495
496static void arm_lpae_free_pgtable(struct io_pgtable *iop)
497{
498 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
499
500 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
501 kfree(data);
502}
503
504static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
505 unsigned long iova, size_t size,
506 arm_lpae_iopte prot, int lvl,
507 arm_lpae_iopte *ptep, size_t blk_size)
508{
509 unsigned long blk_start, blk_end;
510 phys_addr_t blk_paddr;
511 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000512
513 blk_start = iova & ~(blk_size - 1);
514 blk_end = blk_start + blk_size;
515 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
516
517 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
518 arm_lpae_iopte *tablep;
519
520 /* Unmap! */
521 if (blk_start == iova)
522 continue;
523
524 /* __arm_lpae_map expects a pointer to the start of the table */
525 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
526 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
527 tablep) < 0) {
528 if (table) {
529 /* Free the table we allocated */
530 tablep = iopte_deref(table, data);
531 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
532 }
533 return 0; /* Bytes unmapped */
534 }
535 }
536
Robin Murphy507e4c92016-01-26 17:13:14 +0000537 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000538 iova &= ~(blk_size - 1);
Robin Murphy507e4c92016-01-26 17:13:14 +0000539 io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000540 return size;
541}
542
543static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
544 unsigned long iova, size_t size, int lvl,
545 arm_lpae_iopte *ptep)
546{
547 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000548 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000549 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
550
Robin Murphy2eb97c72015-12-04 17:52:58 +0000551 /* Something went horribly wrong and we ran out of page table */
552 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
553 return 0;
554
Will Deacone1d3c0f2014-11-14 17:18:23 +0000555 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
556 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000557 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000558 return 0;
559
560 /* If the size matches this level, we're in the right place */
561 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000562 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000563
564 if (!iopte_leaf(pte, lvl)) {
565 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000566 io_pgtable_tlb_add_flush(iop, iova, size,
567 ARM_LPAE_GRANULE(data), false);
568 io_pgtable_tlb_sync(iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000569 ptep = iopte_deref(pte, data);
570 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
571 } else {
Robin Murphy507e4c92016-01-26 17:13:14 +0000572 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000573 }
574
575 return size;
576 } else if (iopte_leaf(pte, lvl)) {
577 /*
578 * Insert a table at the next level to map the old region,
579 * minus the part we want to unmap
580 */
581 return arm_lpae_split_blk_unmap(data, iova, size,
582 iopte_prot(pte), lvl, ptep,
583 blk_size);
584 }
585
586 /* Keep on walkin' */
587 ptep = iopte_deref(pte, data);
588 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
589}
590
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700591static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000592 size_t size)
593{
594 size_t unmapped;
595 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000596 arm_lpae_iopte *ptep = data->pgd;
597 int lvl = ARM_LPAE_START_LVL(data);
598
599 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
600 if (unmapped)
Robin Murphy507e4c92016-01-26 17:13:14 +0000601 io_pgtable_tlb_sync(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000602
603 return unmapped;
604}
605
606static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
607 unsigned long iova)
608{
609 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
610 arm_lpae_iopte pte, *ptep = data->pgd;
611 int lvl = ARM_LPAE_START_LVL(data);
612
613 do {
614 /* Valid IOPTE pointer? */
615 if (!ptep)
616 return 0;
617
618 /* Grab the IOPTE we're interested in */
619 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
620
621 /* Valid entry? */
622 if (!pte)
623 return 0;
624
625 /* Leaf entry? */
626 if (iopte_leaf(pte,lvl))
627 goto found_translation;
628
629 /* Take it to the next level */
630 ptep = iopte_deref(pte, data);
631 } while (++lvl < ARM_LPAE_MAX_LEVELS);
632
633 /* Ran out of page tables to walk */
634 return 0;
635
636found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100637 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000638 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
639}
640
641static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
642{
643 unsigned long granule;
644
645 /*
646 * We need to restrict the supported page sizes to match the
647 * translation regime for a particular granule. Aim to match
648 * the CPU page size if possible, otherwise prefer smaller sizes.
649 * While we're at it, restrict the block sizes to match the
650 * chosen granule.
651 */
652 if (cfg->pgsize_bitmap & PAGE_SIZE)
653 granule = PAGE_SIZE;
654 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
655 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
656 else if (cfg->pgsize_bitmap & PAGE_MASK)
657 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
658 else
659 granule = 0;
660
661 switch (granule) {
662 case SZ_4K:
663 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
664 break;
665 case SZ_16K:
666 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
667 break;
668 case SZ_64K:
669 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
670 break;
671 default:
672 cfg->pgsize_bitmap = 0;
673 }
674}
675
676static struct arm_lpae_io_pgtable *
677arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
678{
679 unsigned long va_bits, pgd_bits;
680 struct arm_lpae_io_pgtable *data;
681
682 arm_lpae_restrict_pgsizes(cfg);
683
684 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
685 return NULL;
686
687 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
688 return NULL;
689
690 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
691 return NULL;
692
Robin Murphyffcb6d12015-09-17 17:42:16 +0100693 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
694 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
695 return NULL;
696 }
697
Will Deacone1d3c0f2014-11-14 17:18:23 +0000698 data = kmalloc(sizeof(*data), GFP_KERNEL);
699 if (!data)
700 return NULL;
701
702 data->pg_shift = __ffs(cfg->pgsize_bitmap);
703 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
704
705 va_bits = cfg->ias - data->pg_shift;
706 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
707
708 /* Calculate the actual size of our pgd (without concatenation) */
709 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
710 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
711
712 data->iop.ops = (struct io_pgtable_ops) {
713 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700714 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000715 .unmap = arm_lpae_unmap,
716 .iova_to_phys = arm_lpae_iova_to_phys,
717 };
718
719 return data;
720}
721
722static struct io_pgtable *
723arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
724{
725 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000726 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000727
Robin Murphy3850db42016-02-12 17:09:46 +0000728 if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS)
729 return NULL;
730
731 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000732 if (!data)
733 return NULL;
734
735 /* TCR */
736 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
737 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
738 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
739
Robin Murphy06c610e2015-12-07 18:18:53 +0000740 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000741 case SZ_4K:
742 reg |= ARM_LPAE_TCR_TG0_4K;
743 break;
744 case SZ_16K:
745 reg |= ARM_LPAE_TCR_TG0_16K;
746 break;
747 case SZ_64K:
748 reg |= ARM_LPAE_TCR_TG0_64K;
749 break;
750 }
751
752 switch (cfg->oas) {
753 case 32:
754 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
755 break;
756 case 36:
757 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
758 break;
759 case 40:
760 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
761 break;
762 case 42:
763 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
764 break;
765 case 44:
766 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
767 break;
768 case 48:
769 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
770 break;
771 default:
772 goto out_free_data;
773 }
774
775 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000776
777 /* Disable speculative walks through TTBR1 */
778 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000779 cfg->arm_lpae_s1_cfg.tcr = reg;
780
781 /* MAIRs */
782 reg = (ARM_LPAE_MAIR_ATTR_NC
783 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
784 (ARM_LPAE_MAIR_ATTR_WBRWA
785 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
786 (ARM_LPAE_MAIR_ATTR_DEVICE
787 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
788
789 cfg->arm_lpae_s1_cfg.mair[0] = reg;
790 cfg->arm_lpae_s1_cfg.mair[1] = 0;
791
792 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100793 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000794 if (!data->pgd)
795 goto out_free_data;
796
Robin Murphy87a91b12015-07-29 19:46:09 +0100797 /* Ensure the empty pgd is visible before any actual TTBR write */
798 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000799
800 /* TTBRs */
801 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
802 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
803 return &data->iop;
804
805out_free_data:
806 kfree(data);
807 return NULL;
808}
809
810static struct io_pgtable *
811arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
812{
813 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000814 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000815
Robin Murphy3850db42016-02-12 17:09:46 +0000816 /* The NS quirk doesn't apply at stage 2 */
817 if (cfg->quirks)
818 return NULL;
819
820 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000821 if (!data)
822 return NULL;
823
824 /*
825 * Concatenate PGDs at level 1 if possible in order to reduce
826 * the depth of the stage-2 walk.
827 */
828 if (data->levels == ARM_LPAE_MAX_LEVELS) {
829 unsigned long pgd_pages;
830
831 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
832 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
833 data->pgd_size = pgd_pages << data->pg_shift;
834 data->levels--;
835 }
836 }
837
838 /* VTCR */
839 reg = ARM_64_LPAE_S2_TCR_RES1 |
840 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
841 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
842 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
843
844 sl = ARM_LPAE_START_LVL(data);
845
Robin Murphy06c610e2015-12-07 18:18:53 +0000846 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000847 case SZ_4K:
848 reg |= ARM_LPAE_TCR_TG0_4K;
849 sl++; /* SL0 format is different for 4K granule size */
850 break;
851 case SZ_16K:
852 reg |= ARM_LPAE_TCR_TG0_16K;
853 break;
854 case SZ_64K:
855 reg |= ARM_LPAE_TCR_TG0_64K;
856 break;
857 }
858
859 switch (cfg->oas) {
860 case 32:
861 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
862 break;
863 case 36:
864 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
865 break;
866 case 40:
867 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
868 break;
869 case 42:
870 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
871 break;
872 case 44:
873 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
874 break;
875 case 48:
876 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
877 break;
878 default:
879 goto out_free_data;
880 }
881
882 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
883 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
884 cfg->arm_lpae_s2_cfg.vtcr = reg;
885
886 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100887 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000888 if (!data->pgd)
889 goto out_free_data;
890
Robin Murphy87a91b12015-07-29 19:46:09 +0100891 /* Ensure the empty pgd is visible before any actual TTBR write */
892 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000893
894 /* VTTBR */
895 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
896 return &data->iop;
897
898out_free_data:
899 kfree(data);
900 return NULL;
901}
902
903static struct io_pgtable *
904arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
905{
906 struct io_pgtable *iop;
907
908 if (cfg->ias > 32 || cfg->oas > 40)
909 return NULL;
910
911 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
912 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
913 if (iop) {
914 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
915 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
916 }
917
918 return iop;
919}
920
921static struct io_pgtable *
922arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
923{
924 struct io_pgtable *iop;
925
926 if (cfg->ias > 40 || cfg->oas > 40)
927 return NULL;
928
929 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
930 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
931 if (iop)
932 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
933
934 return iop;
935}
936
937struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
938 .alloc = arm_64_lpae_alloc_pgtable_s1,
939 .free = arm_lpae_free_pgtable,
940};
941
942struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
943 .alloc = arm_64_lpae_alloc_pgtable_s2,
944 .free = arm_lpae_free_pgtable,
945};
946
947struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
948 .alloc = arm_32_lpae_alloc_pgtable_s1,
949 .free = arm_lpae_free_pgtable,
950};
951
952struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
953 .alloc = arm_32_lpae_alloc_pgtable_s2,
954 .free = arm_lpae_free_pgtable,
955};
Will Deaconfe4b9912014-11-17 23:31:12 +0000956
957#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
958
959static struct io_pgtable_cfg *cfg_cookie;
960
961static void dummy_tlb_flush_all(void *cookie)
962{
963 WARN_ON(cookie != cfg_cookie);
964}
965
Robin Murphy06c610e2015-12-07 18:18:53 +0000966static void dummy_tlb_add_flush(unsigned long iova, size_t size,
967 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +0000968{
969 WARN_ON(cookie != cfg_cookie);
970 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
971}
972
973static void dummy_tlb_sync(void *cookie)
974{
975 WARN_ON(cookie != cfg_cookie);
976}
977
Will Deaconfe4b9912014-11-17 23:31:12 +0000978static struct iommu_gather_ops dummy_tlb_ops __initdata = {
979 .tlb_flush_all = dummy_tlb_flush_all,
980 .tlb_add_flush = dummy_tlb_add_flush,
981 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000982};
983
984static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
985{
986 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
987 struct io_pgtable_cfg *cfg = &data->iop.cfg;
988
989 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
990 cfg->pgsize_bitmap, cfg->ias);
991 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
992 data->levels, data->pgd_size, data->pg_shift,
993 data->bits_per_level, data->pgd);
994}
995
996#define __FAIL(ops, i) ({ \
997 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
998 arm_lpae_dump_ops(ops); \
999 selftest_running = false; \
1000 -EFAULT; \
1001})
1002
1003static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1004{
1005 static const enum io_pgtable_fmt fmts[] = {
1006 ARM_64_LPAE_S1,
1007 ARM_64_LPAE_S2,
1008 };
1009
1010 int i, j;
1011 unsigned long iova;
1012 size_t size;
1013 struct io_pgtable_ops *ops;
1014
1015 selftest_running = true;
1016
1017 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1018 cfg_cookie = cfg;
1019 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1020 if (!ops) {
1021 pr_err("selftest: failed to allocate io pgtable ops\n");
1022 return -ENOMEM;
1023 }
1024
1025 /*
1026 * Initial sanity checks.
1027 * Empty page tables shouldn't provide any translations.
1028 */
1029 if (ops->iova_to_phys(ops, 42))
1030 return __FAIL(ops, i);
1031
1032 if (ops->iova_to_phys(ops, SZ_1G + 42))
1033 return __FAIL(ops, i);
1034
1035 if (ops->iova_to_phys(ops, SZ_2G + 42))
1036 return __FAIL(ops, i);
1037
1038 /*
1039 * Distinct mappings of different granule sizes.
1040 */
1041 iova = 0;
1042 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1043 while (j != BITS_PER_LONG) {
1044 size = 1UL << j;
1045
1046 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1047 IOMMU_WRITE |
1048 IOMMU_NOEXEC |
1049 IOMMU_CACHE))
1050 return __FAIL(ops, i);
1051
1052 /* Overlapping mappings */
1053 if (!ops->map(ops, iova, iova + size, size,
1054 IOMMU_READ | IOMMU_NOEXEC))
1055 return __FAIL(ops, i);
1056
1057 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1058 return __FAIL(ops, i);
1059
1060 iova += SZ_1G;
1061 j++;
1062 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1063 }
1064
1065 /* Partial unmap */
1066 size = 1UL << __ffs(cfg->pgsize_bitmap);
1067 if (ops->unmap(ops, SZ_1G + size, size) != size)
1068 return __FAIL(ops, i);
1069
1070 /* Remap of partial unmap */
1071 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1072 return __FAIL(ops, i);
1073
1074 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1075 return __FAIL(ops, i);
1076
1077 /* Full unmap */
1078 iova = 0;
1079 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1080 while (j != BITS_PER_LONG) {
1081 size = 1UL << j;
1082
1083 if (ops->unmap(ops, iova, size) != size)
1084 return __FAIL(ops, i);
1085
1086 if (ops->iova_to_phys(ops, iova + 42))
1087 return __FAIL(ops, i);
1088
1089 /* Remap full block */
1090 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1091 return __FAIL(ops, i);
1092
1093 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1094 return __FAIL(ops, i);
1095
1096 iova += SZ_1G;
1097 j++;
1098 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1099 }
1100
1101 free_io_pgtable_ops(ops);
1102 }
1103
1104 selftest_running = false;
1105 return 0;
1106}
1107
1108static int __init arm_lpae_do_selftests(void)
1109{
1110 static const unsigned long pgsize[] = {
1111 SZ_4K | SZ_2M | SZ_1G,
1112 SZ_16K | SZ_32M,
1113 SZ_64K | SZ_512M,
1114 };
1115
1116 static const unsigned int ias[] = {
1117 32, 36, 40, 42, 44, 48,
1118 };
1119
1120 int i, j, pass = 0, fail = 0;
1121 struct io_pgtable_cfg cfg = {
1122 .tlb = &dummy_tlb_ops,
1123 .oas = 48,
1124 };
1125
1126 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1127 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1128 cfg.pgsize_bitmap = pgsize[i];
1129 cfg.ias = ias[j];
1130 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1131 pgsize[i], ias[j]);
1132 if (arm_lpae_run_tests(&cfg))
1133 fail++;
1134 else
1135 pass++;
1136 }
1137 }
1138
1139 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1140 return fail ? -EFAULT : 0;
1141}
1142subsys_initcall(arm_lpae_do_selftests);
1143#endif