blob: e4bc2b23ab96e312cb540a7a0d0813871748e6b8 [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>
25#include <linux/sizes.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28
Robin Murphy87a91b12015-07-29 19:46:09 +010029#include <asm/barrier.h>
30
Will Deacone1d3c0f2014-11-14 17:18:23 +000031#include "io-pgtable.h"
32
33#define ARM_LPAE_MAX_ADDR_BITS 48
34#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
35#define ARM_LPAE_MAX_LEVELS 4
36
37/* Struct accessors */
38#define io_pgtable_to_data(x) \
39 container_of((x), struct arm_lpae_io_pgtable, iop)
40
41#define io_pgtable_ops_to_pgtable(x) \
42 container_of((x), struct io_pgtable, ops)
43
44#define io_pgtable_ops_to_data(x) \
45 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
46
47/*
48 * For consistency with the architecture, we always consider
49 * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
50 */
51#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
52
53/*
54 * Calculate the right shift amount to get to the portion describing level l
55 * in a virtual address mapped by the pagetable in d.
56 */
57#define ARM_LPAE_LVL_SHIFT(l,d) \
58 ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
59 * (d)->bits_per_level) + (d)->pg_shift)
60
Will Deacon367bd972015-02-16 18:38:20 +000061#define ARM_LPAE_PAGES_PER_PGD(d) \
62 DIV_ROUND_UP((d)->pgd_size, 1UL << (d)->pg_shift)
Will Deacone1d3c0f2014-11-14 17:18:23 +000063
64/*
65 * Calculate the index at level l used to map virtual address a using the
66 * pagetable in d.
67 */
68#define ARM_LPAE_PGD_IDX(l,d) \
69 ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
70
71#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000072 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000073 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
74
75/* Calculate the block/page mapping size at level l for pagetable in d. */
76#define ARM_LPAE_BLOCK_SIZE(l,d) \
77 (1 << (ilog2(sizeof(arm_lpae_iopte)) + \
78 ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
79
80/* Page table bits */
81#define ARM_LPAE_PTE_TYPE_SHIFT 0
82#define ARM_LPAE_PTE_TYPE_MASK 0x3
83
84#define ARM_LPAE_PTE_TYPE_BLOCK 1
85#define ARM_LPAE_PTE_TYPE_TABLE 3
86#define ARM_LPAE_PTE_TYPE_PAGE 3
87
Laurent Pinchartc896c1322014-12-14 23:34:50 +020088#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000089#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
90#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
91#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
92#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
93#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c1322014-12-14 23:34:50 +020094#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000095#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
96
97#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
98/* Ignore the contiguous bit for block splitting */
99#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
100#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
101 ARM_LPAE_PTE_ATTR_HI_MASK)
102
103/* Stage-1 PTE */
104#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
105#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
106#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
107#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
108
109/* Stage-2 PTE */
110#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
111#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
112#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
113#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
114#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
115#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
116
117/* Register bits */
118#define ARM_32_LPAE_TCR_EAE (1 << 31)
119#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
120
Will Deacon63979b82015-03-18 10:22:18 +0000121#define ARM_LPAE_TCR_EPD1 (1 << 23)
122
Will Deacone1d3c0f2014-11-14 17:18:23 +0000123#define ARM_LPAE_TCR_TG0_4K (0 << 14)
124#define ARM_LPAE_TCR_TG0_64K (1 << 14)
125#define ARM_LPAE_TCR_TG0_16K (2 << 14)
126
127#define ARM_LPAE_TCR_SH0_SHIFT 12
128#define ARM_LPAE_TCR_SH0_MASK 0x3
129#define ARM_LPAE_TCR_SH_NS 0
130#define ARM_LPAE_TCR_SH_OS 2
131#define ARM_LPAE_TCR_SH_IS 3
132
133#define ARM_LPAE_TCR_ORGN0_SHIFT 10
134#define ARM_LPAE_TCR_IRGN0_SHIFT 8
135#define ARM_LPAE_TCR_RGN_MASK 0x3
136#define ARM_LPAE_TCR_RGN_NC 0
137#define ARM_LPAE_TCR_RGN_WBWA 1
138#define ARM_LPAE_TCR_RGN_WT 2
139#define ARM_LPAE_TCR_RGN_WB 3
140
141#define ARM_LPAE_TCR_SL0_SHIFT 6
142#define ARM_LPAE_TCR_SL0_MASK 0x3
143
144#define ARM_LPAE_TCR_T0SZ_SHIFT 0
145#define ARM_LPAE_TCR_SZ_MASK 0xf
146
147#define ARM_LPAE_TCR_PS_SHIFT 16
148#define ARM_LPAE_TCR_PS_MASK 0x7
149
150#define ARM_LPAE_TCR_IPS_SHIFT 32
151#define ARM_LPAE_TCR_IPS_MASK 0x7
152
153#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
154#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
155#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
156#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
157#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
158#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
159
160#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
161#define ARM_LPAE_MAIR_ATTR_MASK 0xff
162#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
163#define ARM_LPAE_MAIR_ATTR_NC 0x44
164#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
165#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
166#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
167#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
168
169/* IOPTE accessors */
170#define iopte_deref(pte,d) \
171 (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
172 & ~((1ULL << (d)->pg_shift) - 1)))
173
174#define iopte_type(pte,l) \
175 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
176
177#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
178
179#define iopte_leaf(pte,l) \
180 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
181 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
182 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
183
184#define iopte_to_pfn(pte,d) \
185 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
186
187#define pfn_to_iopte(pfn,d) \
188 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
189
190struct arm_lpae_io_pgtable {
191 struct io_pgtable iop;
192
193 int levels;
194 size_t pgd_size;
195 unsigned long pg_shift;
196 unsigned long bits_per_level;
197
198 void *pgd;
199};
200
201typedef u64 arm_lpae_iopte;
202
Will Deaconfe4b9912014-11-17 23:31:12 +0000203static bool selftest_running = false;
204
Robin Murphyf8d54962015-07-29 19:46:04 +0100205static dma_addr_t __arm_lpae_dma_addr(struct device *dev, void *pages)
206{
207 return phys_to_dma(dev, virt_to_phys(pages));
208}
209
210static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
211 struct io_pgtable_cfg *cfg)
212{
213 struct device *dev = cfg->iommu_dev;
214 dma_addr_t dma;
215 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);
216
217 if (!pages)
218 return NULL;
219
Robin Murphy87a91b12015-07-29 19:46:09 +0100220 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100221 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
222 if (dma_mapping_error(dev, dma))
223 goto out_free;
224 /*
225 * We depend on the IOMMU being able to work with any physical
226 * address directly, so if the DMA layer suggests it can't by
227 * giving us back some translation, that bodes very badly...
228 */
229 if (dma != __arm_lpae_dma_addr(dev, pages))
230 goto out_unmap;
231 }
232
233 return pages;
234
235out_unmap:
236 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
237 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
238out_free:
239 free_pages_exact(pages, size);
240 return NULL;
241}
242
243static void __arm_lpae_free_pages(void *pages, size_t size,
244 struct io_pgtable_cfg *cfg)
245{
246 struct device *dev = cfg->iommu_dev;
247
Robin Murphy87a91b12015-07-29 19:46:09 +0100248 if (!selftest_running)
Robin Murphyf8d54962015-07-29 19:46:04 +0100249 dma_unmap_single(dev, __arm_lpae_dma_addr(dev, pages),
250 size, DMA_TO_DEVICE);
251 free_pages_exact(pages, size);
252}
253
254static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100255 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100256{
257 struct device *dev = cfg->iommu_dev;
258
259 *ptep = pte;
260
Robin Murphy87a91b12015-07-29 19:46:09 +0100261 if (!selftest_running)
Robin Murphyf8d54962015-07-29 19:46:04 +0100262 dma_sync_single_for_device(dev, __arm_lpae_dma_addr(dev, ptep),
263 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100264}
265
Will Deacone1d3c0f2014-11-14 17:18:23 +0000266static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
267 unsigned long iova, phys_addr_t paddr,
268 arm_lpae_iopte prot, int lvl,
269 arm_lpae_iopte *ptep)
270{
271 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100272 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000273
274 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000275 if (iopte_leaf(*ptep, lvl)) {
276 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000277 return -EEXIST;
Will Deaconfe4b9912014-11-17 23:31:12 +0000278 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000279
Robin Murphyf8d54962015-07-29 19:46:04 +0100280 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c1322014-12-14 23:34:50 +0200281 pte |= ARM_LPAE_PTE_NS;
282
Will Deacone1d3c0f2014-11-14 17:18:23 +0000283 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
284 pte |= ARM_LPAE_PTE_TYPE_PAGE;
285 else
286 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
287
288 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
289 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
290
Robin Murphy87a91b12015-07-29 19:46:09 +0100291 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000292 return 0;
293}
294
295static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
296 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
297 int lvl, arm_lpae_iopte *ptep)
298{
299 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000300 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100301 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000302
303 /* Find our entry at the current level */
304 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
305
306 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100307 if (size == block_size && (size & cfg->pgsize_bitmap))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000308 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
309
310 /* We can't allocate tables at the final level */
311 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
312 return -EINVAL;
313
314 /* Grab a pointer to the next level */
315 pte = *ptep;
316 if (!pte) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100317 cptep = __arm_lpae_alloc_pages(1UL << data->pg_shift,
318 GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000319 if (!cptep)
320 return -ENOMEM;
321
Will Deacone1d3c0f2014-11-14 17:18:23 +0000322 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100323 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c1322014-12-14 23:34:50 +0200324 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100325 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000326 } else {
327 cptep = iopte_deref(pte, data);
328 }
329
330 /* Rinse, repeat */
331 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
332}
333
334static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
335 int prot)
336{
337 arm_lpae_iopte pte;
338
339 if (data->iop.fmt == ARM_64_LPAE_S1 ||
340 data->iop.fmt == ARM_32_LPAE_S1) {
341 pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
342
343 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
344 pte |= ARM_LPAE_PTE_AP_RDONLY;
345
346 if (prot & IOMMU_CACHE)
347 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
348 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
349 } else {
350 pte = ARM_LPAE_PTE_HAP_FAULT;
351 if (prot & IOMMU_READ)
352 pte |= ARM_LPAE_PTE_HAP_READ;
353 if (prot & IOMMU_WRITE)
354 pte |= ARM_LPAE_PTE_HAP_WRITE;
355 if (prot & IOMMU_CACHE)
356 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
357 else
358 pte |= ARM_LPAE_PTE_MEMATTR_NC;
359 }
360
361 if (prot & IOMMU_NOEXEC)
362 pte |= ARM_LPAE_PTE_XN;
363
364 return pte;
365}
366
367static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
368 phys_addr_t paddr, size_t size, int iommu_prot)
369{
370 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
371 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100372 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000373 arm_lpae_iopte prot;
374
375 /* If no access, then nothing to do */
376 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
377 return 0;
378
379 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100380 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
381 /*
382 * Synchronise all PTE updates for the new mapping before there's
383 * a chance for anything to kick off a table walk for the new iova.
384 */
385 wmb();
386
387 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000388}
389
390static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
391 arm_lpae_iopte *ptep)
392{
393 arm_lpae_iopte *start, *end;
394 unsigned long table_size;
395
396 /* Only leaf entries at the last level */
397 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
398 return;
399
400 if (lvl == ARM_LPAE_START_LVL(data))
401 table_size = data->pgd_size;
402 else
403 table_size = 1UL << data->pg_shift;
404
405 start = ptep;
406 end = (void *)ptep + table_size;
407
408 while (ptep != end) {
409 arm_lpae_iopte pte = *ptep++;
410
411 if (!pte || iopte_leaf(pte, lvl))
412 continue;
413
414 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
415 }
416
Robin Murphyf8d54962015-07-29 19:46:04 +0100417 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000418}
419
420static void arm_lpae_free_pgtable(struct io_pgtable *iop)
421{
422 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
423
424 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
425 kfree(data);
426}
427
428static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
429 unsigned long iova, size_t size,
430 arm_lpae_iopte prot, int lvl,
431 arm_lpae_iopte *ptep, size_t blk_size)
432{
433 unsigned long blk_start, blk_end;
434 phys_addr_t blk_paddr;
435 arm_lpae_iopte table = 0;
Robin Murphyf8d54962015-07-29 19:46:04 +0100436 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000437
438 blk_start = iova & ~(blk_size - 1);
439 blk_end = blk_start + blk_size;
440 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
441
442 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
443 arm_lpae_iopte *tablep;
444
445 /* Unmap! */
446 if (blk_start == iova)
447 continue;
448
449 /* __arm_lpae_map expects a pointer to the start of the table */
450 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
451 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
452 tablep) < 0) {
453 if (table) {
454 /* Free the table we allocated */
455 tablep = iopte_deref(table, data);
456 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
457 }
458 return 0; /* Bytes unmapped */
459 }
460 }
461
Robin Murphy87a91b12015-07-29 19:46:09 +0100462 __arm_lpae_set_pte(ptep, table, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000463 iova &= ~(blk_size - 1);
Robin Murphy87a91b12015-07-29 19:46:09 +0100464 cfg->tlb->tlb_add_flush(iova, blk_size, true, data->iop.cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000465 return size;
466}
467
468static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
469 unsigned long iova, size_t size, int lvl,
470 arm_lpae_iopte *ptep)
471{
472 arm_lpae_iopte pte;
473 const struct iommu_gather_ops *tlb = data->iop.cfg.tlb;
474 void *cookie = data->iop.cookie;
475 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
476
477 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
478 pte = *ptep;
479
480 /* Something went horribly wrong and we ran out of page table */
481 if (WARN_ON(!pte || (lvl == ARM_LPAE_MAX_LEVELS)))
482 return 0;
483
484 /* If the size matches this level, we're in the right place */
485 if (size == blk_size) {
Robin Murphy87a91b12015-07-29 19:46:09 +0100486 __arm_lpae_set_pte(ptep, 0, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000487
488 if (!iopte_leaf(pte, lvl)) {
489 /* Also flush any partial walks */
490 tlb->tlb_add_flush(iova, size, false, cookie);
Robin Murphyf8d54962015-07-29 19:46:04 +0100491 tlb->tlb_sync(cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000492 ptep = iopte_deref(pte, data);
493 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
494 } else {
495 tlb->tlb_add_flush(iova, size, true, cookie);
496 }
497
498 return size;
499 } else if (iopte_leaf(pte, lvl)) {
500 /*
501 * Insert a table at the next level to map the old region,
502 * minus the part we want to unmap
503 */
504 return arm_lpae_split_blk_unmap(data, iova, size,
505 iopte_prot(pte), lvl, ptep,
506 blk_size);
507 }
508
509 /* Keep on walkin' */
510 ptep = iopte_deref(pte, data);
511 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
512}
513
514static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
515 size_t size)
516{
517 size_t unmapped;
518 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
519 struct io_pgtable *iop = &data->iop;
520 arm_lpae_iopte *ptep = data->pgd;
521 int lvl = ARM_LPAE_START_LVL(data);
522
523 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
524 if (unmapped)
525 iop->cfg.tlb->tlb_sync(iop->cookie);
526
527 return unmapped;
528}
529
530static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
531 unsigned long iova)
532{
533 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
534 arm_lpae_iopte pte, *ptep = data->pgd;
535 int lvl = ARM_LPAE_START_LVL(data);
536
537 do {
538 /* Valid IOPTE pointer? */
539 if (!ptep)
540 return 0;
541
542 /* Grab the IOPTE we're interested in */
543 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
544
545 /* Valid entry? */
546 if (!pte)
547 return 0;
548
549 /* Leaf entry? */
550 if (iopte_leaf(pte,lvl))
551 goto found_translation;
552
553 /* Take it to the next level */
554 ptep = iopte_deref(pte, data);
555 } while (++lvl < ARM_LPAE_MAX_LEVELS);
556
557 /* Ran out of page tables to walk */
558 return 0;
559
560found_translation:
561 iova &= ((1 << data->pg_shift) - 1);
562 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
563}
564
565static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
566{
567 unsigned long granule;
568
569 /*
570 * We need to restrict the supported page sizes to match the
571 * translation regime for a particular granule. Aim to match
572 * the CPU page size if possible, otherwise prefer smaller sizes.
573 * While we're at it, restrict the block sizes to match the
574 * chosen granule.
575 */
576 if (cfg->pgsize_bitmap & PAGE_SIZE)
577 granule = PAGE_SIZE;
578 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
579 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
580 else if (cfg->pgsize_bitmap & PAGE_MASK)
581 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
582 else
583 granule = 0;
584
585 switch (granule) {
586 case SZ_4K:
587 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
588 break;
589 case SZ_16K:
590 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
591 break;
592 case SZ_64K:
593 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
594 break;
595 default:
596 cfg->pgsize_bitmap = 0;
597 }
598}
599
600static struct arm_lpae_io_pgtable *
601arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
602{
603 unsigned long va_bits, pgd_bits;
604 struct arm_lpae_io_pgtable *data;
605
606 arm_lpae_restrict_pgsizes(cfg);
607
608 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
609 return NULL;
610
611 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
612 return NULL;
613
614 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
615 return NULL;
616
617 data = kmalloc(sizeof(*data), GFP_KERNEL);
618 if (!data)
619 return NULL;
620
621 data->pg_shift = __ffs(cfg->pgsize_bitmap);
622 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
623
624 va_bits = cfg->ias - data->pg_shift;
625 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
626
627 /* Calculate the actual size of our pgd (without concatenation) */
628 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
629 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
630
631 data->iop.ops = (struct io_pgtable_ops) {
632 .map = arm_lpae_map,
633 .unmap = arm_lpae_unmap,
634 .iova_to_phys = arm_lpae_iova_to_phys,
635 };
636
637 return data;
638}
639
640static struct io_pgtable *
641arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
642{
643 u64 reg;
644 struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
645
646 if (!data)
647 return NULL;
648
649 /* TCR */
650 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
651 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
652 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
653
654 switch (1 << data->pg_shift) {
655 case SZ_4K:
656 reg |= ARM_LPAE_TCR_TG0_4K;
657 break;
658 case SZ_16K:
659 reg |= ARM_LPAE_TCR_TG0_16K;
660 break;
661 case SZ_64K:
662 reg |= ARM_LPAE_TCR_TG0_64K;
663 break;
664 }
665
666 switch (cfg->oas) {
667 case 32:
668 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
669 break;
670 case 36:
671 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
672 break;
673 case 40:
674 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
675 break;
676 case 42:
677 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
678 break;
679 case 44:
680 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
681 break;
682 case 48:
683 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
684 break;
685 default:
686 goto out_free_data;
687 }
688
689 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000690
691 /* Disable speculative walks through TTBR1 */
692 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000693 cfg->arm_lpae_s1_cfg.tcr = reg;
694
695 /* MAIRs */
696 reg = (ARM_LPAE_MAIR_ATTR_NC
697 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
698 (ARM_LPAE_MAIR_ATTR_WBRWA
699 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
700 (ARM_LPAE_MAIR_ATTR_DEVICE
701 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
702
703 cfg->arm_lpae_s1_cfg.mair[0] = reg;
704 cfg->arm_lpae_s1_cfg.mair[1] = 0;
705
706 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100707 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000708 if (!data->pgd)
709 goto out_free_data;
710
Robin Murphy87a91b12015-07-29 19:46:09 +0100711 /* Ensure the empty pgd is visible before any actual TTBR write */
712 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000713
714 /* TTBRs */
715 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
716 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
717 return &data->iop;
718
719out_free_data:
720 kfree(data);
721 return NULL;
722}
723
724static struct io_pgtable *
725arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
726{
727 u64 reg, sl;
728 struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
729
730 if (!data)
731 return NULL;
732
733 /*
734 * Concatenate PGDs at level 1 if possible in order to reduce
735 * the depth of the stage-2 walk.
736 */
737 if (data->levels == ARM_LPAE_MAX_LEVELS) {
738 unsigned long pgd_pages;
739
740 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
741 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
742 data->pgd_size = pgd_pages << data->pg_shift;
743 data->levels--;
744 }
745 }
746
747 /* VTCR */
748 reg = ARM_64_LPAE_S2_TCR_RES1 |
749 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
750 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
751 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
752
753 sl = ARM_LPAE_START_LVL(data);
754
755 switch (1 << data->pg_shift) {
756 case SZ_4K:
757 reg |= ARM_LPAE_TCR_TG0_4K;
758 sl++; /* SL0 format is different for 4K granule size */
759 break;
760 case SZ_16K:
761 reg |= ARM_LPAE_TCR_TG0_16K;
762 break;
763 case SZ_64K:
764 reg |= ARM_LPAE_TCR_TG0_64K;
765 break;
766 }
767
768 switch (cfg->oas) {
769 case 32:
770 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
771 break;
772 case 36:
773 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
774 break;
775 case 40:
776 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
777 break;
778 case 42:
779 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
780 break;
781 case 44:
782 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
783 break;
784 case 48:
785 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
786 break;
787 default:
788 goto out_free_data;
789 }
790
791 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
792 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
793 cfg->arm_lpae_s2_cfg.vtcr = reg;
794
795 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100796 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000797 if (!data->pgd)
798 goto out_free_data;
799
Robin Murphy87a91b12015-07-29 19:46:09 +0100800 /* Ensure the empty pgd is visible before any actual TTBR write */
801 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000802
803 /* VTTBR */
804 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
805 return &data->iop;
806
807out_free_data:
808 kfree(data);
809 return NULL;
810}
811
812static struct io_pgtable *
813arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
814{
815 struct io_pgtable *iop;
816
817 if (cfg->ias > 32 || cfg->oas > 40)
818 return NULL;
819
820 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
821 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
822 if (iop) {
823 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
824 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
825 }
826
827 return iop;
828}
829
830static struct io_pgtable *
831arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
832{
833 struct io_pgtable *iop;
834
835 if (cfg->ias > 40 || cfg->oas > 40)
836 return NULL;
837
838 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
839 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
840 if (iop)
841 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
842
843 return iop;
844}
845
846struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
847 .alloc = arm_64_lpae_alloc_pgtable_s1,
848 .free = arm_lpae_free_pgtable,
849};
850
851struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
852 .alloc = arm_64_lpae_alloc_pgtable_s2,
853 .free = arm_lpae_free_pgtable,
854};
855
856struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
857 .alloc = arm_32_lpae_alloc_pgtable_s1,
858 .free = arm_lpae_free_pgtable,
859};
860
861struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
862 .alloc = arm_32_lpae_alloc_pgtable_s2,
863 .free = arm_lpae_free_pgtable,
864};
Will Deaconfe4b9912014-11-17 23:31:12 +0000865
866#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
867
868static struct io_pgtable_cfg *cfg_cookie;
869
870static void dummy_tlb_flush_all(void *cookie)
871{
872 WARN_ON(cookie != cfg_cookie);
873}
874
875static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf,
876 void *cookie)
877{
878 WARN_ON(cookie != cfg_cookie);
879 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
880}
881
882static void dummy_tlb_sync(void *cookie)
883{
884 WARN_ON(cookie != cfg_cookie);
885}
886
Will Deaconfe4b9912014-11-17 23:31:12 +0000887static struct iommu_gather_ops dummy_tlb_ops __initdata = {
888 .tlb_flush_all = dummy_tlb_flush_all,
889 .tlb_add_flush = dummy_tlb_add_flush,
890 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000891};
892
893static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
894{
895 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
896 struct io_pgtable_cfg *cfg = &data->iop.cfg;
897
898 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
899 cfg->pgsize_bitmap, cfg->ias);
900 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
901 data->levels, data->pgd_size, data->pg_shift,
902 data->bits_per_level, data->pgd);
903}
904
905#define __FAIL(ops, i) ({ \
906 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
907 arm_lpae_dump_ops(ops); \
908 selftest_running = false; \
909 -EFAULT; \
910})
911
912static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
913{
914 static const enum io_pgtable_fmt fmts[] = {
915 ARM_64_LPAE_S1,
916 ARM_64_LPAE_S2,
917 };
918
919 int i, j;
920 unsigned long iova;
921 size_t size;
922 struct io_pgtable_ops *ops;
923
924 selftest_running = true;
925
926 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
927 cfg_cookie = cfg;
928 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
929 if (!ops) {
930 pr_err("selftest: failed to allocate io pgtable ops\n");
931 return -ENOMEM;
932 }
933
934 /*
935 * Initial sanity checks.
936 * Empty page tables shouldn't provide any translations.
937 */
938 if (ops->iova_to_phys(ops, 42))
939 return __FAIL(ops, i);
940
941 if (ops->iova_to_phys(ops, SZ_1G + 42))
942 return __FAIL(ops, i);
943
944 if (ops->iova_to_phys(ops, SZ_2G + 42))
945 return __FAIL(ops, i);
946
947 /*
948 * Distinct mappings of different granule sizes.
949 */
950 iova = 0;
951 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
952 while (j != BITS_PER_LONG) {
953 size = 1UL << j;
954
955 if (ops->map(ops, iova, iova, size, IOMMU_READ |
956 IOMMU_WRITE |
957 IOMMU_NOEXEC |
958 IOMMU_CACHE))
959 return __FAIL(ops, i);
960
961 /* Overlapping mappings */
962 if (!ops->map(ops, iova, iova + size, size,
963 IOMMU_READ | IOMMU_NOEXEC))
964 return __FAIL(ops, i);
965
966 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
967 return __FAIL(ops, i);
968
969 iova += SZ_1G;
970 j++;
971 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
972 }
973
974 /* Partial unmap */
975 size = 1UL << __ffs(cfg->pgsize_bitmap);
976 if (ops->unmap(ops, SZ_1G + size, size) != size)
977 return __FAIL(ops, i);
978
979 /* Remap of partial unmap */
980 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
981 return __FAIL(ops, i);
982
983 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
984 return __FAIL(ops, i);
985
986 /* Full unmap */
987 iova = 0;
988 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
989 while (j != BITS_PER_LONG) {
990 size = 1UL << j;
991
992 if (ops->unmap(ops, iova, size) != size)
993 return __FAIL(ops, i);
994
995 if (ops->iova_to_phys(ops, iova + 42))
996 return __FAIL(ops, i);
997
998 /* Remap full block */
999 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1000 return __FAIL(ops, i);
1001
1002 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1003 return __FAIL(ops, i);
1004
1005 iova += SZ_1G;
1006 j++;
1007 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1008 }
1009
1010 free_io_pgtable_ops(ops);
1011 }
1012
1013 selftest_running = false;
1014 return 0;
1015}
1016
1017static int __init arm_lpae_do_selftests(void)
1018{
1019 static const unsigned long pgsize[] = {
1020 SZ_4K | SZ_2M | SZ_1G,
1021 SZ_16K | SZ_32M,
1022 SZ_64K | SZ_512M,
1023 };
1024
1025 static const unsigned int ias[] = {
1026 32, 36, 40, 42, 44, 48,
1027 };
1028
1029 int i, j, pass = 0, fail = 0;
1030 struct io_pgtable_cfg cfg = {
1031 .tlb = &dummy_tlb_ops,
1032 .oas = 48,
1033 };
1034
1035 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1036 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1037 cfg.pgsize_bitmap = pgsize[i];
1038 cfg.ias = ias[j];
1039 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1040 pgsize[i], ias[j]);
1041 if (arm_lpae_run_tests(&cfg))
1042 fail++;
1043 else
1044 pass++;
1045 }
1046 }
1047
1048 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1049 return fail ? -EFAULT : 0;
1050}
1051subsys_initcall(arm_lpae_do_selftests);
1052#endif