blob: 52700fa958c209eadc39b7817b3ee6afecf443a8 [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
Robin Murphy2c3d2732017-06-22 16:53:54 +010023#include <linux/atomic.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000024#include <linux/iommu.h>
25#include <linux/kernel.h>
26#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) \
Robin Murphy022f4e42017-04-03 13:12:10 +010078 (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \
Will Deacone1d3c0f2014-11-14 17:18:23 +000079 ((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)
Robin Murphy2c3d2732017-06-22 16:53:54 +0100103/* Software bit for solving coherency races */
104#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000105
106/* Stage-1 PTE */
107#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
108#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
109#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
110#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
111
112/* Stage-2 PTE */
113#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
114#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
115#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
116#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
117#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
118#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
119
120/* Register bits */
121#define ARM_32_LPAE_TCR_EAE (1 << 31)
122#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
123
Will Deacon63979b82015-03-18 10:22:18 +0000124#define ARM_LPAE_TCR_EPD1 (1 << 23)
125
Will Deacone1d3c0f2014-11-14 17:18:23 +0000126#define ARM_LPAE_TCR_TG0_4K (0 << 14)
127#define ARM_LPAE_TCR_TG0_64K (1 << 14)
128#define ARM_LPAE_TCR_TG0_16K (2 << 14)
129
130#define ARM_LPAE_TCR_SH0_SHIFT 12
131#define ARM_LPAE_TCR_SH0_MASK 0x3
132#define ARM_LPAE_TCR_SH_NS 0
133#define ARM_LPAE_TCR_SH_OS 2
134#define ARM_LPAE_TCR_SH_IS 3
135
136#define ARM_LPAE_TCR_ORGN0_SHIFT 10
137#define ARM_LPAE_TCR_IRGN0_SHIFT 8
138#define ARM_LPAE_TCR_RGN_MASK 0x3
139#define ARM_LPAE_TCR_RGN_NC 0
140#define ARM_LPAE_TCR_RGN_WBWA 1
141#define ARM_LPAE_TCR_RGN_WT 2
142#define ARM_LPAE_TCR_RGN_WB 3
143
144#define ARM_LPAE_TCR_SL0_SHIFT 6
145#define ARM_LPAE_TCR_SL0_MASK 0x3
146
147#define ARM_LPAE_TCR_T0SZ_SHIFT 0
148#define ARM_LPAE_TCR_SZ_MASK 0xf
149
150#define ARM_LPAE_TCR_PS_SHIFT 16
151#define ARM_LPAE_TCR_PS_MASK 0x7
152
153#define ARM_LPAE_TCR_IPS_SHIFT 32
154#define ARM_LPAE_TCR_IPS_MASK 0x7
155
156#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
157#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
158#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
159#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
160#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
161#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
162
163#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
164#define ARM_LPAE_MAIR_ATTR_MASK 0xff
165#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
166#define ARM_LPAE_MAIR_ATTR_NC 0x44
167#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
168#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
169#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
170#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
171
172/* IOPTE accessors */
173#define iopte_deref(pte,d) \
174 (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000175 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000176
177#define iopte_type(pte,l) \
178 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
179
180#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
181
182#define iopte_leaf(pte,l) \
183 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
184 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
185 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
186
187#define iopte_to_pfn(pte,d) \
188 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
189
190#define pfn_to_iopte(pfn,d) \
191 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
192
193struct arm_lpae_io_pgtable {
194 struct io_pgtable iop;
195
196 int levels;
197 size_t pgd_size;
198 unsigned long pg_shift;
199 unsigned long bits_per_level;
200
201 void *pgd;
202};
203
204typedef u64 arm_lpae_iopte;
205
Will Deaconfe4b9912014-11-17 23:31:12 +0000206static bool selftest_running = false;
207
Robin Murphyffcb6d12015-09-17 17:42:16 +0100208static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100209{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100210 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100211}
212
213static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
214 struct io_pgtable_cfg *cfg)
215{
216 struct device *dev = cfg->iommu_dev;
217 dma_addr_t dma;
218 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);
219
220 if (!pages)
221 return NULL;
222
Robin Murphy81b3c252017-06-22 16:53:53 +0100223 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100224 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
225 if (dma_mapping_error(dev, dma))
226 goto out_free;
227 /*
228 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100229 * address directly, so if the DMA layer suggests otherwise by
230 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100231 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100232 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100233 goto out_unmap;
234 }
235
236 return pages;
237
238out_unmap:
239 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
240 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
241out_free:
242 free_pages_exact(pages, size);
243 return NULL;
244}
245
246static void __arm_lpae_free_pages(void *pages, size_t size,
247 struct io_pgtable_cfg *cfg)
248{
Robin Murphy81b3c252017-06-22 16:53:53 +0100249 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
Robin Murphyffcb6d12015-09-17 17:42:16 +0100250 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100251 size, DMA_TO_DEVICE);
252 free_pages_exact(pages, size);
253}
254
Robin Murphy2c3d2732017-06-22 16:53:54 +0100255static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
256 struct io_pgtable_cfg *cfg)
257{
258 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
259 sizeof(*ptep), DMA_TO_DEVICE);
260}
261
Robin Murphyf8d54962015-07-29 19:46:04 +0100262static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100263 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100264{
Robin Murphyf8d54962015-07-29 19:46:04 +0100265 *ptep = pte;
266
Robin Murphy81b3c252017-06-22 16:53:53 +0100267 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
Robin Murphy2c3d2732017-06-22 16:53:54 +0100268 __arm_lpae_sync_pte(ptep, cfg);
Robin Murphyf8d54962015-07-29 19:46:04 +0100269}
270
Will Deaconcf27ec92015-08-11 16:48:32 +0100271static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
272 unsigned long iova, size_t size, int lvl,
273 arm_lpae_iopte *ptep);
274
Robin Murphyfb3a9572017-06-22 16:53:51 +0100275static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
276 phys_addr_t paddr, arm_lpae_iopte prot,
277 int lvl, arm_lpae_iopte *ptep)
278{
279 arm_lpae_iopte pte = prot;
280
281 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
282 pte |= ARM_LPAE_PTE_NS;
283
284 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
285 pte |= ARM_LPAE_PTE_TYPE_PAGE;
286 else
287 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
288
289 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
290 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
291
292 __arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
293}
294
Will Deacone1d3c0f2014-11-14 17:18:23 +0000295static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
296 unsigned long iova, phys_addr_t paddr,
297 arm_lpae_iopte prot, int lvl,
298 arm_lpae_iopte *ptep)
299{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100300 arm_lpae_iopte pte = *ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000301
Robin Murphyfb3a9572017-06-22 16:53:51 +0100302 if (iopte_leaf(pte, lvl)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100303 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000304 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000305 return -EEXIST;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100306 } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100307 /*
308 * We need to unmap and free the old table before
309 * overwriting it with a block entry.
310 */
311 arm_lpae_iopte *tblp;
312 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
313
314 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
315 if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz))
316 return -EINVAL;
Will Deaconfe4b9912014-11-17 23:31:12 +0000317 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000318
Robin Murphyfb3a9572017-06-22 16:53:51 +0100319 __arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000320 return 0;
321}
322
Robin Murphyfb3a9572017-06-22 16:53:51 +0100323static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
324 arm_lpae_iopte *ptep,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100325 arm_lpae_iopte curr,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100326 struct io_pgtable_cfg *cfg)
327{
Robin Murphy2c3d2732017-06-22 16:53:54 +0100328 arm_lpae_iopte old, new;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100329
330 new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
331 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
332 new |= ARM_LPAE_PTE_NSTABLE;
333
Robin Murphy2c3d2732017-06-22 16:53:54 +0100334 /* Ensure the table itself is visible before its PTE can be */
335 wmb();
336
337 old = cmpxchg64_relaxed(ptep, curr, new);
338
339 if ((cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) ||
340 (old & ARM_LPAE_PTE_SW_SYNC))
341 return old;
342
343 /* Even if it's not ours, there's no point waiting; just kick it */
344 __arm_lpae_sync_pte(ptep, cfg);
345 if (old == curr)
346 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
347
348 return old;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100349}
350
Will Deacone1d3c0f2014-11-14 17:18:23 +0000351static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
352 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
353 int lvl, arm_lpae_iopte *ptep)
354{
355 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000356 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100357 size_t tblsz = ARM_LPAE_GRANULE(data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100358 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000359
360 /* Find our entry at the current level */
361 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
362
363 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100364 if (size == block_size && (size & cfg->pgsize_bitmap))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000365 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
366
367 /* We can't allocate tables at the final level */
368 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
369 return -EINVAL;
370
371 /* Grab a pointer to the next level */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100372 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000373 if (!pte) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100374 cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000375 if (!cptep)
376 return -ENOMEM;
377
Robin Murphy2c3d2732017-06-22 16:53:54 +0100378 pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
379 if (pte)
380 __arm_lpae_free_pages(cptep, tblsz, cfg);
381 } else if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) &&
382 !(pte & ARM_LPAE_PTE_SW_SYNC)) {
383 __arm_lpae_sync_pte(ptep, cfg);
384 }
385
386 if (pte && !iopte_leaf(pte, lvl)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000387 cptep = iopte_deref(pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100388 } else if (pte) {
Oleksandr Tyshchenkoed46e662017-02-27 14:30:25 +0200389 /* We require an unmap first */
390 WARN_ON(!selftest_running);
391 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000392 }
393
394 /* Rinse, repeat */
395 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
396}
397
398static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
399 int prot)
400{
401 arm_lpae_iopte pte;
402
403 if (data->iop.fmt == ARM_64_LPAE_S1 ||
404 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530405 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000406
407 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
408 pte |= ARM_LPAE_PTE_AP_RDONLY;
409
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530410 if (!(prot & IOMMU_PRIV))
411 pte |= ARM_LPAE_PTE_AP_UNPRIV;
412
Robin Murphyfb948252016-04-05 12:39:31 +0100413 if (prot & IOMMU_MMIO)
414 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
415 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
416 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000417 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
418 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
419 } else {
420 pte = ARM_LPAE_PTE_HAP_FAULT;
421 if (prot & IOMMU_READ)
422 pte |= ARM_LPAE_PTE_HAP_READ;
423 if (prot & IOMMU_WRITE)
424 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100425 if (prot & IOMMU_MMIO)
426 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
427 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000428 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
429 else
430 pte |= ARM_LPAE_PTE_MEMATTR_NC;
431 }
432
433 if (prot & IOMMU_NOEXEC)
434 pte |= ARM_LPAE_PTE_XN;
435
436 return pte;
437}
438
439static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
440 phys_addr_t paddr, size_t size, int iommu_prot)
441{
442 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
443 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100444 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000445 arm_lpae_iopte prot;
446
447 /* If no access, then nothing to do */
448 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
449 return 0;
450
451 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100452 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
453 /*
454 * Synchronise all PTE updates for the new mapping before there's
455 * a chance for anything to kick off a table walk for the new iova.
456 */
457 wmb();
458
459 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000460}
461
462static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
463 arm_lpae_iopte *ptep)
464{
465 arm_lpae_iopte *start, *end;
466 unsigned long table_size;
467
Will Deacone1d3c0f2014-11-14 17:18:23 +0000468 if (lvl == ARM_LPAE_START_LVL(data))
469 table_size = data->pgd_size;
470 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000471 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000472
473 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000474
475 /* Only leaf entries at the last level */
476 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
477 end = ptep;
478 else
479 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000480
481 while (ptep != end) {
482 arm_lpae_iopte pte = *ptep++;
483
484 if (!pte || iopte_leaf(pte, lvl))
485 continue;
486
487 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
488 }
489
Robin Murphyf8d54962015-07-29 19:46:04 +0100490 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000491}
492
493static void arm_lpae_free_pgtable(struct io_pgtable *iop)
494{
495 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
496
497 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
498 kfree(data);
499}
500
501static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
502 unsigned long iova, size_t size,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100503 arm_lpae_iopte blk_pte, int lvl,
504 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000505{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100506 struct io_pgtable_cfg *cfg = &data->iop.cfg;
507 arm_lpae_iopte pte, *tablep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000508 phys_addr_t blk_paddr;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100509 size_t tablesz = ARM_LPAE_GRANULE(data);
510 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
511 int i, unmap_idx = -1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000512
Robin Murphyfb3a9572017-06-22 16:53:51 +0100513 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
514 return 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000515
Robin Murphyfb3a9572017-06-22 16:53:51 +0100516 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
517 if (!tablep)
518 return 0; /* Bytes unmapped */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000519
Robin Murphyfb3a9572017-06-22 16:53:51 +0100520 if (size == split_sz)
521 unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data);
522
523 blk_paddr = iopte_to_pfn(blk_pte, data) << data->pg_shift;
524 pte = iopte_prot(blk_pte);
525
526 for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000527 /* Unmap! */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100528 if (i == unmap_idx)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000529 continue;
530
Robin Murphyfb3a9572017-06-22 16:53:51 +0100531 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000532 }
533
Robin Murphy2c3d2732017-06-22 16:53:54 +0100534 pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
535 if (pte != blk_pte) {
536 __arm_lpae_free_pages(tablep, tablesz, cfg);
537 /*
538 * We may race against someone unmapping another part of this
539 * block, but anything else is invalid. We can't misinterpret
540 * a page entry here since we're never at the last level.
541 */
542 if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE)
543 return 0;
544
545 tablep = iopte_deref(pte, data);
546 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100547
548 if (unmap_idx < 0)
549 return __arm_lpae_unmap(data, iova, size, lvl, tablep);
550
551 io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000552 return size;
553}
554
555static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
556 unsigned long iova, size_t size, int lvl,
557 arm_lpae_iopte *ptep)
558{
559 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000560 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000561
Robin Murphy2eb97c72015-12-04 17:52:58 +0000562 /* Something went horribly wrong and we ran out of page table */
563 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
564 return 0;
565
Will Deacone1d3c0f2014-11-14 17:18:23 +0000566 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100567 pte = READ_ONCE(*ptep);
Robin Murphy2eb97c72015-12-04 17:52:58 +0000568 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000569 return 0;
570
571 /* If the size matches this level, we're in the right place */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100572 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000573 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000574
575 if (!iopte_leaf(pte, lvl)) {
576 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000577 io_pgtable_tlb_add_flush(iop, iova, size,
578 ARM_LPAE_GRANULE(data), false);
579 io_pgtable_tlb_sync(iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000580 ptep = iopte_deref(pte, data);
581 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
582 } else {
Robin Murphy507e4c92016-01-26 17:13:14 +0000583 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000584 }
585
586 return size;
587 } else if (iopte_leaf(pte, lvl)) {
588 /*
589 * Insert a table at the next level to map the old region,
590 * minus the part we want to unmap
591 */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100592 return arm_lpae_split_blk_unmap(data, iova, size, pte,
593 lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000594 }
595
596 /* Keep on walkin' */
597 ptep = iopte_deref(pte, data);
598 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
599}
600
601static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
602 size_t size)
603{
604 size_t unmapped;
605 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000606 arm_lpae_iopte *ptep = data->pgd;
607 int lvl = ARM_LPAE_START_LVL(data);
608
609 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
610 if (unmapped)
Robin Murphy507e4c92016-01-26 17:13:14 +0000611 io_pgtable_tlb_sync(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000612
613 return unmapped;
614}
615
616static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
617 unsigned long iova)
618{
619 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
620 arm_lpae_iopte pte, *ptep = data->pgd;
621 int lvl = ARM_LPAE_START_LVL(data);
622
623 do {
624 /* Valid IOPTE pointer? */
625 if (!ptep)
626 return 0;
627
628 /* Grab the IOPTE we're interested in */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100629 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
630 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000631
632 /* Valid entry? */
633 if (!pte)
634 return 0;
635
636 /* Leaf entry? */
637 if (iopte_leaf(pte,lvl))
638 goto found_translation;
639
640 /* Take it to the next level */
641 ptep = iopte_deref(pte, data);
642 } while (++lvl < ARM_LPAE_MAX_LEVELS);
643
644 /* Ran out of page tables to walk */
645 return 0;
646
647found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100648 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000649 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
650}
651
652static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
653{
654 unsigned long granule;
655
656 /*
657 * We need to restrict the supported page sizes to match the
658 * translation regime for a particular granule. Aim to match
659 * the CPU page size if possible, otherwise prefer smaller sizes.
660 * While we're at it, restrict the block sizes to match the
661 * chosen granule.
662 */
663 if (cfg->pgsize_bitmap & PAGE_SIZE)
664 granule = PAGE_SIZE;
665 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
666 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
667 else if (cfg->pgsize_bitmap & PAGE_MASK)
668 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
669 else
670 granule = 0;
671
672 switch (granule) {
673 case SZ_4K:
674 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
675 break;
676 case SZ_16K:
677 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
678 break;
679 case SZ_64K:
680 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
681 break;
682 default:
683 cfg->pgsize_bitmap = 0;
684 }
685}
686
687static struct arm_lpae_io_pgtable *
688arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
689{
690 unsigned long va_bits, pgd_bits;
691 struct arm_lpae_io_pgtable *data;
692
693 arm_lpae_restrict_pgsizes(cfg);
694
695 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
696 return NULL;
697
698 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
699 return NULL;
700
701 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
702 return NULL;
703
Robin Murphyffcb6d12015-09-17 17:42:16 +0100704 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
705 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
706 return NULL;
707 }
708
Will Deacone1d3c0f2014-11-14 17:18:23 +0000709 data = kmalloc(sizeof(*data), GFP_KERNEL);
710 if (!data)
711 return NULL;
712
713 data->pg_shift = __ffs(cfg->pgsize_bitmap);
714 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
715
716 va_bits = cfg->ias - data->pg_shift;
717 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
718
719 /* Calculate the actual size of our pgd (without concatenation) */
720 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
721 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
722
723 data->iop.ops = (struct io_pgtable_ops) {
724 .map = arm_lpae_map,
725 .unmap = arm_lpae_unmap,
726 .iova_to_phys = arm_lpae_iova_to_phys,
727 };
728
729 return data;
730}
731
732static struct io_pgtable *
733arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
734{
735 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000736 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000737
Robin Murphy81b3c252017-06-22 16:53:53 +0100738 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA))
Robin Murphy3850db42016-02-12 17:09:46 +0000739 return NULL;
740
741 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000742 if (!data)
743 return NULL;
744
745 /* TCR */
746 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
747 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
748 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
749
Robin Murphy06c610e2015-12-07 18:18:53 +0000750 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000751 case SZ_4K:
752 reg |= ARM_LPAE_TCR_TG0_4K;
753 break;
754 case SZ_16K:
755 reg |= ARM_LPAE_TCR_TG0_16K;
756 break;
757 case SZ_64K:
758 reg |= ARM_LPAE_TCR_TG0_64K;
759 break;
760 }
761
762 switch (cfg->oas) {
763 case 32:
764 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
765 break;
766 case 36:
767 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
768 break;
769 case 40:
770 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
771 break;
772 case 42:
773 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
774 break;
775 case 44:
776 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
777 break;
778 case 48:
779 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
780 break;
781 default:
782 goto out_free_data;
783 }
784
785 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000786
787 /* Disable speculative walks through TTBR1 */
788 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000789 cfg->arm_lpae_s1_cfg.tcr = reg;
790
791 /* MAIRs */
792 reg = (ARM_LPAE_MAIR_ATTR_NC
793 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
794 (ARM_LPAE_MAIR_ATTR_WBRWA
795 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
796 (ARM_LPAE_MAIR_ATTR_DEVICE
797 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
798
799 cfg->arm_lpae_s1_cfg.mair[0] = reg;
800 cfg->arm_lpae_s1_cfg.mair[1] = 0;
801
802 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100803 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000804 if (!data->pgd)
805 goto out_free_data;
806
Robin Murphy87a91b12015-07-29 19:46:09 +0100807 /* Ensure the empty pgd is visible before any actual TTBR write */
808 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000809
810 /* TTBRs */
811 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
812 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
813 return &data->iop;
814
815out_free_data:
816 kfree(data);
817 return NULL;
818}
819
820static struct io_pgtable *
821arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
822{
823 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000824 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000825
Robin Murphy3850db42016-02-12 17:09:46 +0000826 /* The NS quirk doesn't apply at stage 2 */
Robin Murphy81b3c252017-06-22 16:53:53 +0100827 if (cfg->quirks & ~IO_PGTABLE_QUIRK_NO_DMA)
Robin Murphy3850db42016-02-12 17:09:46 +0000828 return NULL;
829
830 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000831 if (!data)
832 return NULL;
833
834 /*
835 * Concatenate PGDs at level 1 if possible in order to reduce
836 * the depth of the stage-2 walk.
837 */
838 if (data->levels == ARM_LPAE_MAX_LEVELS) {
839 unsigned long pgd_pages;
840
841 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
842 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
843 data->pgd_size = pgd_pages << data->pg_shift;
844 data->levels--;
845 }
846 }
847
848 /* VTCR */
849 reg = ARM_64_LPAE_S2_TCR_RES1 |
850 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
851 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
852 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
853
854 sl = ARM_LPAE_START_LVL(data);
855
Robin Murphy06c610e2015-12-07 18:18:53 +0000856 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000857 case SZ_4K:
858 reg |= ARM_LPAE_TCR_TG0_4K;
859 sl++; /* SL0 format is different for 4K granule size */
860 break;
861 case SZ_16K:
862 reg |= ARM_LPAE_TCR_TG0_16K;
863 break;
864 case SZ_64K:
865 reg |= ARM_LPAE_TCR_TG0_64K;
866 break;
867 }
868
869 switch (cfg->oas) {
870 case 32:
871 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
872 break;
873 case 36:
874 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
875 break;
876 case 40:
877 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
878 break;
879 case 42:
880 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
881 break;
882 case 44:
883 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
884 break;
885 case 48:
886 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
887 break;
888 default:
889 goto out_free_data;
890 }
891
892 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
893 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
894 cfg->arm_lpae_s2_cfg.vtcr = reg;
895
896 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100897 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000898 if (!data->pgd)
899 goto out_free_data;
900
Robin Murphy87a91b12015-07-29 19:46:09 +0100901 /* Ensure the empty pgd is visible before any actual TTBR write */
902 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000903
904 /* VTTBR */
905 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
906 return &data->iop;
907
908out_free_data:
909 kfree(data);
910 return NULL;
911}
912
913static struct io_pgtable *
914arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
915{
916 struct io_pgtable *iop;
917
918 if (cfg->ias > 32 || cfg->oas > 40)
919 return NULL;
920
921 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
922 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
923 if (iop) {
924 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
925 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
926 }
927
928 return iop;
929}
930
931static struct io_pgtable *
932arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
933{
934 struct io_pgtable *iop;
935
936 if (cfg->ias > 40 || cfg->oas > 40)
937 return NULL;
938
939 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
940 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
941 if (iop)
942 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
943
944 return iop;
945}
946
947struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
948 .alloc = arm_64_lpae_alloc_pgtable_s1,
949 .free = arm_lpae_free_pgtable,
950};
951
952struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
953 .alloc = arm_64_lpae_alloc_pgtable_s2,
954 .free = arm_lpae_free_pgtable,
955};
956
957struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
958 .alloc = arm_32_lpae_alloc_pgtable_s1,
959 .free = arm_lpae_free_pgtable,
960};
961
962struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
963 .alloc = arm_32_lpae_alloc_pgtable_s2,
964 .free = arm_lpae_free_pgtable,
965};
Will Deaconfe4b9912014-11-17 23:31:12 +0000966
967#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
968
969static struct io_pgtable_cfg *cfg_cookie;
970
971static void dummy_tlb_flush_all(void *cookie)
972{
973 WARN_ON(cookie != cfg_cookie);
974}
975
Robin Murphy06c610e2015-12-07 18:18:53 +0000976static void dummy_tlb_add_flush(unsigned long iova, size_t size,
977 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +0000978{
979 WARN_ON(cookie != cfg_cookie);
980 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
981}
982
983static void dummy_tlb_sync(void *cookie)
984{
985 WARN_ON(cookie != cfg_cookie);
986}
987
Bhumika Goyaldfed5f02016-10-25 23:36:12 +0530988static const struct iommu_gather_ops dummy_tlb_ops __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +0000989 .tlb_flush_all = dummy_tlb_flush_all,
990 .tlb_add_flush = dummy_tlb_add_flush,
991 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000992};
993
994static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
995{
996 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
997 struct io_pgtable_cfg *cfg = &data->iop.cfg;
998
999 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1000 cfg->pgsize_bitmap, cfg->ias);
1001 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1002 data->levels, data->pgd_size, data->pg_shift,
1003 data->bits_per_level, data->pgd);
1004}
1005
1006#define __FAIL(ops, i) ({ \
1007 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1008 arm_lpae_dump_ops(ops); \
1009 selftest_running = false; \
1010 -EFAULT; \
1011})
1012
1013static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1014{
1015 static const enum io_pgtable_fmt fmts[] = {
1016 ARM_64_LPAE_S1,
1017 ARM_64_LPAE_S2,
1018 };
1019
1020 int i, j;
1021 unsigned long iova;
1022 size_t size;
1023 struct io_pgtable_ops *ops;
1024
1025 selftest_running = true;
1026
1027 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1028 cfg_cookie = cfg;
1029 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1030 if (!ops) {
1031 pr_err("selftest: failed to allocate io pgtable ops\n");
1032 return -ENOMEM;
1033 }
1034
1035 /*
1036 * Initial sanity checks.
1037 * Empty page tables shouldn't provide any translations.
1038 */
1039 if (ops->iova_to_phys(ops, 42))
1040 return __FAIL(ops, i);
1041
1042 if (ops->iova_to_phys(ops, SZ_1G + 42))
1043 return __FAIL(ops, i);
1044
1045 if (ops->iova_to_phys(ops, SZ_2G + 42))
1046 return __FAIL(ops, i);
1047
1048 /*
1049 * Distinct mappings of different granule sizes.
1050 */
1051 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +08001052 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001053 size = 1UL << j;
1054
1055 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1056 IOMMU_WRITE |
1057 IOMMU_NOEXEC |
1058 IOMMU_CACHE))
1059 return __FAIL(ops, i);
1060
1061 /* Overlapping mappings */
1062 if (!ops->map(ops, iova, iova + size, size,
1063 IOMMU_READ | IOMMU_NOEXEC))
1064 return __FAIL(ops, i);
1065
1066 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1067 return __FAIL(ops, i);
1068
1069 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001070 }
1071
1072 /* Partial unmap */
1073 size = 1UL << __ffs(cfg->pgsize_bitmap);
1074 if (ops->unmap(ops, SZ_1G + size, size) != size)
1075 return __FAIL(ops, i);
1076
1077 /* Remap of partial unmap */
1078 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1079 return __FAIL(ops, i);
1080
1081 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1082 return __FAIL(ops, i);
1083
1084 /* Full unmap */
1085 iova = 0;
1086 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1087 while (j != BITS_PER_LONG) {
1088 size = 1UL << j;
1089
1090 if (ops->unmap(ops, iova, size) != size)
1091 return __FAIL(ops, i);
1092
1093 if (ops->iova_to_phys(ops, iova + 42))
1094 return __FAIL(ops, i);
1095
1096 /* Remap full block */
1097 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1098 return __FAIL(ops, i);
1099
1100 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1101 return __FAIL(ops, i);
1102
1103 iova += SZ_1G;
1104 j++;
1105 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1106 }
1107
1108 free_io_pgtable_ops(ops);
1109 }
1110
1111 selftest_running = false;
1112 return 0;
1113}
1114
1115static int __init arm_lpae_do_selftests(void)
1116{
1117 static const unsigned long pgsize[] = {
1118 SZ_4K | SZ_2M | SZ_1G,
1119 SZ_16K | SZ_32M,
1120 SZ_64K | SZ_512M,
1121 };
1122
1123 static const unsigned int ias[] = {
1124 32, 36, 40, 42, 44, 48,
1125 };
1126
1127 int i, j, pass = 0, fail = 0;
1128 struct io_pgtable_cfg cfg = {
1129 .tlb = &dummy_tlb_ops,
1130 .oas = 48,
Robin Murphy81b3c252017-06-22 16:53:53 +01001131 .quirks = IO_PGTABLE_QUIRK_NO_DMA,
Will Deaconfe4b9912014-11-17 23:31:12 +00001132 };
1133
1134 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1135 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1136 cfg.pgsize_bitmap = pgsize[i];
1137 cfg.ias = ias[j];
1138 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1139 pgsize[i], ias[j]);
1140 if (arm_lpae_run_tests(&cfg))
1141 fail++;
1142 else
1143 pass++;
1144 }
1145 }
1146
1147 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1148 return fail ? -EFAULT : 0;
1149}
1150subsys_initcall(arm_lpae_do_selftests);
1151#endif