blob: 90536e59f0562133edc03508885fa9a01d53cbf7 [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 */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700171#define iopte_deref(pte, d) \
172 (__va(iopte_val(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
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700204/*
205 * We'll use some ignored bits in table entries to keep track of the number
206 * of page mappings beneath the table. The maximum number of entries
207 * beneath any table mapping in armv8 is 8192 (which is possible at the
208 * 2nd- and 3rd-level when using a 64K granule size). The bits at our
209 * disposal are:
210 *
211 * 4k granule: [58..52], [11..2]
212 * 64k granule: [58..52], [15..2]
213 *
214 * [58..52], [11..2] is enough bits for tracking table mappings at any
215 * level for any granule, so we'll use those.
216 */
217#define BOTTOM_IGNORED_MASK 0x3ff
218#define BOTTOM_IGNORED_SHIFT 2
219#define BOTTOM_IGNORED_NUM_BITS 10
220#define TOP_IGNORED_MASK 0x7fULL
221#define TOP_IGNORED_SHIFT 52
222#define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \
223 (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
224
225static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte)
226{
227 return table_pte & ~IOPTE_RESERVED_MASK;
228}
229
230static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte)
231{
232 return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT))
233 >> BOTTOM_IGNORED_SHIFT;
234}
235
236static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte)
237{
238 return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
239 >> TOP_IGNORED_SHIFT;
240}
241
242static int iopte_tblcnt(arm_lpae_iopte table_pte)
243{
244 return (_iopte_bottom_ignored_val(table_pte) |
245 (_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS));
246}
247
248static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val)
249{
250 arm_lpae_iopte pte = iopte_val(*table_pte);
251
252 pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) |
253 (((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS))
254 >> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT);
255 *table_pte = pte;
256}
257
258static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt)
259{
260 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
261
262 current_cnt -= cnt;
263 iopte_tblcnt_set(table_ptep, current_cnt);
264}
265
266static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt)
267{
268 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
269
270 current_cnt += cnt;
271 iopte_tblcnt_set(table_ptep, current_cnt);
272}
273
Will Deaconfe4b9912014-11-17 23:31:12 +0000274static bool selftest_running = false;
275
Robin Murphyffcb6d12015-09-17 17:42:16 +0100276static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100277{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100278 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100279}
280
281static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
282 struct io_pgtable_cfg *cfg)
283{
284 struct device *dev = cfg->iommu_dev;
285 dma_addr_t dma;
286 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);
287
288 if (!pages)
289 return NULL;
290
Robin Murphy87a91b12015-07-29 19:46:09 +0100291 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100292 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
293 if (dma_mapping_error(dev, dma))
294 goto out_free;
295 /*
296 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100297 * address directly, so if the DMA layer suggests otherwise by
298 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100299 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100300 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100301 goto out_unmap;
302 }
303
304 return pages;
305
306out_unmap:
307 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
308 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
309out_free:
310 free_pages_exact(pages, size);
311 return NULL;
312}
313
314static void __arm_lpae_free_pages(void *pages, size_t size,
315 struct io_pgtable_cfg *cfg)
316{
Robin Murphy87a91b12015-07-29 19:46:09 +0100317 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100318 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100319 size, DMA_TO_DEVICE);
320 free_pages_exact(pages, size);
321}
322
323static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100324 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100325{
Robin Murphyf8d54962015-07-29 19:46:04 +0100326 *ptep = pte;
327
Robin Murphy87a91b12015-07-29 19:46:09 +0100328 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100329 dma_sync_single_for_device(cfg->iommu_dev,
330 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100331 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100332}
333
Will Deacone1d3c0f2014-11-14 17:18:23 +0000334static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
335 unsigned long iova, phys_addr_t paddr,
336 arm_lpae_iopte prot, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700337 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000338{
339 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100340 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000341
Patrick Daly67ba8eb2016-06-27 18:44:42 -0700342 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000343 if (iopte_leaf(*ptep, lvl)) {
344 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000345 return -EEXIST;
Will Deaconfe4b9912014-11-17 23:31:12 +0000346 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000347
Robin Murphyf8d54962015-07-29 19:46:04 +0100348 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200349 pte |= ARM_LPAE_PTE_NS;
350
Will Deacone1d3c0f2014-11-14 17:18:23 +0000351 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
352 pte |= ARM_LPAE_PTE_TYPE_PAGE;
353 else
354 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
355
356 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
357 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
358
Robin Murphy87a91b12015-07-29 19:46:09 +0100359 __arm_lpae_set_pte(ptep, pte, cfg);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700360
361 if (prev_ptep)
362 iopte_tblcnt_add(prev_ptep, 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000363 return 0;
364}
365
366static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
367 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700368 int lvl, arm_lpae_iopte *ptep,
369 arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000370{
371 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000372 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100373 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000374
375 /* Find our entry at the current level */
376 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
377
378 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100379 if (size == block_size && (size & cfg->pgsize_bitmap))
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700380 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep,
381 prev_ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000382
383 /* We can't allocate tables at the final level */
384 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
385 return -EINVAL;
386
387 /* Grab a pointer to the next level */
388 pte = *ptep;
389 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000390 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Robin Murphyf8d54962015-07-29 19:46:04 +0100391 GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000392 if (!cptep)
393 return -ENOMEM;
394
Will Deacone1d3c0f2014-11-14 17:18:23 +0000395 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100396 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200397 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100398 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000399 } else {
400 cptep = iopte_deref(pte, data);
401 }
402
403 /* Rinse, repeat */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700404 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep,
405 ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000406}
407
408static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
409 int prot)
410{
411 arm_lpae_iopte pte;
412
413 if (data->iop.fmt == ARM_64_LPAE_S1 ||
414 data->iop.fmt == ARM_32_LPAE_S1) {
415 pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
416
417 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
418 pte |= ARM_LPAE_PTE_AP_RDONLY;
419
Robin Murphyfb948252016-04-05 12:39:31 +0100420 if (prot & IOMMU_MMIO)
421 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
422 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
423 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000424 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
425 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
426 } else {
427 pte = ARM_LPAE_PTE_HAP_FAULT;
428 if (prot & IOMMU_READ)
429 pte |= ARM_LPAE_PTE_HAP_READ;
430 if (prot & IOMMU_WRITE)
431 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100432 if (prot & IOMMU_MMIO)
433 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
434 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000435 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
436 else
437 pte |= ARM_LPAE_PTE_MEMATTR_NC;
438 }
439
440 if (prot & IOMMU_NOEXEC)
441 pte |= ARM_LPAE_PTE_XN;
442
443 return pte;
444}
445
446static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
447 phys_addr_t paddr, size_t size, int iommu_prot)
448{
449 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
450 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100451 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000452 arm_lpae_iopte prot;
453
454 /* If no access, then nothing to do */
455 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
456 return 0;
457
458 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700459 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL);
Robin Murphy87a91b12015-07-29 19:46:09 +0100460 /*
461 * Synchronise all PTE updates for the new mapping before there's
462 * a chance for anything to kick off a table walk for the new iova.
463 */
464 wmb();
465
466 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000467}
468
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700469static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
470 struct scatterlist *sg, unsigned int nents,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700471 int iommu_prot, size_t *size)
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700472{
473 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
474 arm_lpae_iopte *ptep = data->pgd;
475 int lvl = ARM_LPAE_START_LVL(data);
476 arm_lpae_iopte prot;
477 struct scatterlist *s;
478 size_t mapped = 0;
479 int i, ret;
480 unsigned int min_pagesz;
481
482 /* If no access, then nothing to do */
483 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700484 goto out_err;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700485
486 prot = arm_lpae_prot_to_pte(data, iommu_prot);
487
488 min_pagesz = 1 << __ffs(data->iop.cfg.pgsize_bitmap);
489
490 for_each_sg(sg, s, nents, i) {
491 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
492 size_t size = s->length;
493
494 /*
495 * We are mapping on IOMMU page boundaries, so offset within
496 * the page must be 0. However, the IOMMU may support pages
497 * smaller than PAGE_SIZE, so s->offset may still represent
498 * an offset of that boundary within the CPU page.
499 */
500 if (!IS_ALIGNED(s->offset, min_pagesz))
501 goto out_err;
502
503 while (size) {
504 size_t pgsize = iommu_pgsize(
505 data->iop.cfg.pgsize_bitmap, iova | phys, size);
506 ret = __arm_lpae_map(data, iova, phys, pgsize, prot,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700507 lvl, ptep, NULL);
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700508 if (ret)
509 goto out_err;
510
511 iova += pgsize;
512 mapped += pgsize;
513 phys += pgsize;
514 size -= pgsize;
515 }
516 }
517
518 return mapped;
519
520out_err:
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700521 /* Return the size of the partial mapping so that they can be undone */
522 *size = mapped;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700523 return 0;
524}
525
Will Deacone1d3c0f2014-11-14 17:18:23 +0000526static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
527 arm_lpae_iopte *ptep)
528{
529 arm_lpae_iopte *start, *end;
530 unsigned long table_size;
531
Will Deacone1d3c0f2014-11-14 17:18:23 +0000532 if (lvl == ARM_LPAE_START_LVL(data))
533 table_size = data->pgd_size;
534 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000535 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000536
537 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000538
539 /* Only leaf entries at the last level */
540 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
541 end = ptep;
542 else
543 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000544
545 while (ptep != end) {
546 arm_lpae_iopte pte = *ptep++;
547
548 if (!pte || iopte_leaf(pte, lvl))
549 continue;
550
551 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
552 }
553
Robin Murphyf8d54962015-07-29 19:46:04 +0100554 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000555}
556
557static void arm_lpae_free_pgtable(struct io_pgtable *iop)
558{
559 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
560
561 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
562 kfree(data);
563}
564
565static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
566 unsigned long iova, size_t size,
567 arm_lpae_iopte prot, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700568 arm_lpae_iopte *ptep,
569 arm_lpae_iopte *prev_ptep, size_t blk_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000570{
571 unsigned long blk_start, blk_end;
572 phys_addr_t blk_paddr;
573 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000574
575 blk_start = iova & ~(blk_size - 1);
576 blk_end = blk_start + blk_size;
577 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700578 size = ARM_LPAE_BLOCK_SIZE(lvl + 1, data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000579
580 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
581 arm_lpae_iopte *tablep;
582
583 /* Unmap! */
584 if (blk_start == iova)
585 continue;
586
587 /* __arm_lpae_map expects a pointer to the start of the table */
588 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
589 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700590 tablep, prev_ptep) < 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000591 if (table) {
592 /* Free the table we allocated */
593 tablep = iopte_deref(table, data);
594 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
595 }
596 return 0; /* Bytes unmapped */
597 }
598 }
599
Robin Murphy507e4c92016-01-26 17:13:14 +0000600 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000601 iova &= ~(blk_size - 1);
Robin Murphy507e4c92016-01-26 17:13:14 +0000602 io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000603 return size;
604}
605
606static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
607 unsigned long iova, size_t size, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700608 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000609{
610 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000611 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000612 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
613
Robin Murphy2eb97c72015-12-04 17:52:58 +0000614 /* Something went horribly wrong and we ran out of page table */
615 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
616 return 0;
617
Will Deacone1d3c0f2014-11-14 17:18:23 +0000618 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
619 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000620 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000621 return 0;
622
623 /* If the size matches this level, we're in the right place */
624 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000625 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000626
627 if (!iopte_leaf(pte, lvl)) {
628 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000629 io_pgtable_tlb_add_flush(iop, iova, size,
630 ARM_LPAE_GRANULE(data), false);
631 io_pgtable_tlb_sync(iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000632 ptep = iopte_deref(pte, data);
633 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
634 } else {
Robin Murphy507e4c92016-01-26 17:13:14 +0000635 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000636 }
637
638 return size;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700639 } else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) {
640 arm_lpae_iopte *table = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700641 arm_lpae_iopte *table_base = table;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700642 int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data);
643 int entry_size = ARM_LPAE_GRANULE(data);
644 int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) / entry_size;
645 int entries = min_t(int, size / entry_size,
646 max_entries - tl_offset);
647 int table_len = entries * sizeof(*table);
648
649 /*
650 * This isn't a block mapping so it must be a table mapping
651 * and since it's the 2nd-to-last level the next level has
652 * to be all page mappings. Zero them all out in one fell
653 * swoop.
654 */
655
656 table += tl_offset;
657
658 memset(table, 0, table_len);
659 dma_sync_single_for_device(iop->cfg.iommu_dev,
660 __arm_lpae_dma_addr(table),
661 table_len, DMA_TO_DEVICE);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700662
663 iopte_tblcnt_sub(ptep, entries);
664 if (!iopte_tblcnt(*ptep)) {
665 /* no valid mappings left under this table. free it. */
666 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
667 io_pgtable_tlb_add_flush(iop, iova,
668 entries * entry_size,
669 ARM_LPAE_GRANULE(data),
670 false);
671 __arm_lpae_free_pgtable(data, lvl + 1, table_base);
672 } else {
673 io_pgtable_tlb_add_flush(iop, iova,
674 entries * entry_size,
675 ARM_LPAE_GRANULE(data),
676 true);
677 }
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700678
679 return entries * entry_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000680 } else if (iopte_leaf(pte, lvl)) {
681 /*
682 * Insert a table at the next level to map the old region,
683 * minus the part we want to unmap
684 */
685 return arm_lpae_split_blk_unmap(data, iova, size,
686 iopte_prot(pte), lvl, ptep,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700687 prev_ptep, blk_size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000688 }
689
690 /* Keep on walkin' */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700691 prev_ptep = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000692 ptep = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700693 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000694}
695
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700696static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000697 size_t size)
698{
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700699 size_t unmapped = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000700 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000701 arm_lpae_iopte *ptep = data->pgd;
702 int lvl = ARM_LPAE_START_LVL(data);
703
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700704 while (unmapped < size) {
705 size_t ret, size_to_unmap, remaining;
706
707 remaining = (size - unmapped);
Patrick Dalyf145f052016-06-27 18:38:09 -0700708 size_to_unmap = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova,
709 remaining);
710 size_to_unmap = size_to_unmap >= SZ_2M ?
711 size_to_unmap :
712 min_t(unsigned long, remaining,
713 (ALIGN(iova + 1, SZ_2M) - iova));
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700714 ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep,
715 NULL);
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700716 if (ret == 0)
717 break;
718 unmapped += ret;
719 iova += ret;
720 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000721 if (unmapped)
Robin Murphy507e4c92016-01-26 17:13:14 +0000722 io_pgtable_tlb_sync(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000723
724 return unmapped;
725}
726
727static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
728 unsigned long iova)
729{
730 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
731 arm_lpae_iopte pte, *ptep = data->pgd;
732 int lvl = ARM_LPAE_START_LVL(data);
733
734 do {
735 /* Valid IOPTE pointer? */
736 if (!ptep)
737 return 0;
738
739 /* Grab the IOPTE we're interested in */
740 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
741
742 /* Valid entry? */
743 if (!pte)
744 return 0;
745
746 /* Leaf entry? */
747 if (iopte_leaf(pte,lvl))
748 goto found_translation;
749
750 /* Take it to the next level */
751 ptep = iopte_deref(pte, data);
752 } while (++lvl < ARM_LPAE_MAX_LEVELS);
753
754 /* Ran out of page tables to walk */
755 return 0;
756
757found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100758 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000759 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
760}
761
762static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
763{
764 unsigned long granule;
765
766 /*
767 * We need to restrict the supported page sizes to match the
768 * translation regime for a particular granule. Aim to match
769 * the CPU page size if possible, otherwise prefer smaller sizes.
770 * While we're at it, restrict the block sizes to match the
771 * chosen granule.
772 */
773 if (cfg->pgsize_bitmap & PAGE_SIZE)
774 granule = PAGE_SIZE;
775 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
776 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
777 else if (cfg->pgsize_bitmap & PAGE_MASK)
778 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
779 else
780 granule = 0;
781
782 switch (granule) {
783 case SZ_4K:
784 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
785 break;
786 case SZ_16K:
787 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
788 break;
789 case SZ_64K:
790 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
791 break;
792 default:
793 cfg->pgsize_bitmap = 0;
794 }
795}
796
797static struct arm_lpae_io_pgtable *
798arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
799{
800 unsigned long va_bits, pgd_bits;
801 struct arm_lpae_io_pgtable *data;
802
803 arm_lpae_restrict_pgsizes(cfg);
804
805 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
806 return NULL;
807
808 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
809 return NULL;
810
811 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
812 return NULL;
813
Robin Murphyffcb6d12015-09-17 17:42:16 +0100814 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
815 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
816 return NULL;
817 }
818
Will Deacone1d3c0f2014-11-14 17:18:23 +0000819 data = kmalloc(sizeof(*data), GFP_KERNEL);
820 if (!data)
821 return NULL;
822
823 data->pg_shift = __ffs(cfg->pgsize_bitmap);
824 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
825
826 va_bits = cfg->ias - data->pg_shift;
827 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
828
829 /* Calculate the actual size of our pgd (without concatenation) */
830 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
831 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
832
833 data->iop.ops = (struct io_pgtable_ops) {
834 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700835 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000836 .unmap = arm_lpae_unmap,
837 .iova_to_phys = arm_lpae_iova_to_phys,
838 };
839
840 return data;
841}
842
843static struct io_pgtable *
844arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
845{
846 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000847 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000848
Robin Murphy3850db42016-02-12 17:09:46 +0000849 if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS)
850 return NULL;
851
852 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000853 if (!data)
854 return NULL;
855
856 /* TCR */
857 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
858 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
859 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
860
Robin Murphy06c610e2015-12-07 18:18:53 +0000861 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000862 case SZ_4K:
863 reg |= ARM_LPAE_TCR_TG0_4K;
864 break;
865 case SZ_16K:
866 reg |= ARM_LPAE_TCR_TG0_16K;
867 break;
868 case SZ_64K:
869 reg |= ARM_LPAE_TCR_TG0_64K;
870 break;
871 }
872
873 switch (cfg->oas) {
874 case 32:
875 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
876 break;
877 case 36:
878 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
879 break;
880 case 40:
881 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
882 break;
883 case 42:
884 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
885 break;
886 case 44:
887 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
888 break;
889 case 48:
890 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
891 break;
892 default:
893 goto out_free_data;
894 }
895
896 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000897
898 /* Disable speculative walks through TTBR1 */
899 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000900 cfg->arm_lpae_s1_cfg.tcr = reg;
901
902 /* MAIRs */
903 reg = (ARM_LPAE_MAIR_ATTR_NC
904 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
905 (ARM_LPAE_MAIR_ATTR_WBRWA
906 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
907 (ARM_LPAE_MAIR_ATTR_DEVICE
908 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
909
910 cfg->arm_lpae_s1_cfg.mair[0] = reg;
911 cfg->arm_lpae_s1_cfg.mair[1] = 0;
912
913 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100914 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000915 if (!data->pgd)
916 goto out_free_data;
917
Robin Murphy87a91b12015-07-29 19:46:09 +0100918 /* Ensure the empty pgd is visible before any actual TTBR write */
919 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000920
921 /* TTBRs */
922 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
923 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
924 return &data->iop;
925
926out_free_data:
927 kfree(data);
928 return NULL;
929}
930
931static struct io_pgtable *
932arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
933{
934 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000935 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000936
Robin Murphy3850db42016-02-12 17:09:46 +0000937 /* The NS quirk doesn't apply at stage 2 */
938 if (cfg->quirks)
939 return NULL;
940
941 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000942 if (!data)
943 return NULL;
944
945 /*
946 * Concatenate PGDs at level 1 if possible in order to reduce
947 * the depth of the stage-2 walk.
948 */
949 if (data->levels == ARM_LPAE_MAX_LEVELS) {
950 unsigned long pgd_pages;
951
952 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
953 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
954 data->pgd_size = pgd_pages << data->pg_shift;
955 data->levels--;
956 }
957 }
958
959 /* VTCR */
960 reg = ARM_64_LPAE_S2_TCR_RES1 |
961 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
962 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
963 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
964
965 sl = ARM_LPAE_START_LVL(data);
966
Robin Murphy06c610e2015-12-07 18:18:53 +0000967 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000968 case SZ_4K:
969 reg |= ARM_LPAE_TCR_TG0_4K;
970 sl++; /* SL0 format is different for 4K granule size */
971 break;
972 case SZ_16K:
973 reg |= ARM_LPAE_TCR_TG0_16K;
974 break;
975 case SZ_64K:
976 reg |= ARM_LPAE_TCR_TG0_64K;
977 break;
978 }
979
980 switch (cfg->oas) {
981 case 32:
982 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
983 break;
984 case 36:
985 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
986 break;
987 case 40:
988 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
989 break;
990 case 42:
991 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
992 break;
993 case 44:
994 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
995 break;
996 case 48:
997 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
998 break;
999 default:
1000 goto out_free_data;
1001 }
1002
1003 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
1004 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
1005 cfg->arm_lpae_s2_cfg.vtcr = reg;
1006
1007 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +01001008 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001009 if (!data->pgd)
1010 goto out_free_data;
1011
Robin Murphy87a91b12015-07-29 19:46:09 +01001012 /* Ensure the empty pgd is visible before any actual TTBR write */
1013 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001014
1015 /* VTTBR */
1016 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1017 return &data->iop;
1018
1019out_free_data:
1020 kfree(data);
1021 return NULL;
1022}
1023
1024static struct io_pgtable *
1025arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1026{
1027 struct io_pgtable *iop;
1028
1029 if (cfg->ias > 32 || cfg->oas > 40)
1030 return NULL;
1031
1032 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1033 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1034 if (iop) {
1035 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
1036 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1037 }
1038
1039 return iop;
1040}
1041
1042static struct io_pgtable *
1043arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1044{
1045 struct io_pgtable *iop;
1046
1047 if (cfg->ias > 40 || cfg->oas > 40)
1048 return NULL;
1049
1050 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1051 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1052 if (iop)
1053 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1054
1055 return iop;
1056}
1057
1058struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1059 .alloc = arm_64_lpae_alloc_pgtable_s1,
1060 .free = arm_lpae_free_pgtable,
1061};
1062
1063struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1064 .alloc = arm_64_lpae_alloc_pgtable_s2,
1065 .free = arm_lpae_free_pgtable,
1066};
1067
1068struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1069 .alloc = arm_32_lpae_alloc_pgtable_s1,
1070 .free = arm_lpae_free_pgtable,
1071};
1072
1073struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1074 .alloc = arm_32_lpae_alloc_pgtable_s2,
1075 .free = arm_lpae_free_pgtable,
1076};
Will Deaconfe4b9912014-11-17 23:31:12 +00001077
1078#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1079
1080static struct io_pgtable_cfg *cfg_cookie;
1081
1082static void dummy_tlb_flush_all(void *cookie)
1083{
1084 WARN_ON(cookie != cfg_cookie);
1085}
1086
Robin Murphy06c610e2015-12-07 18:18:53 +00001087static void dummy_tlb_add_flush(unsigned long iova, size_t size,
1088 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001089{
1090 WARN_ON(cookie != cfg_cookie);
1091 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1092}
1093
1094static void dummy_tlb_sync(void *cookie)
1095{
1096 WARN_ON(cookie != cfg_cookie);
1097}
1098
Will Deaconfe4b9912014-11-17 23:31:12 +00001099static struct iommu_gather_ops dummy_tlb_ops __initdata = {
1100 .tlb_flush_all = dummy_tlb_flush_all,
1101 .tlb_add_flush = dummy_tlb_add_flush,
1102 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +00001103};
1104
1105static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1106{
1107 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1108 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1109
1110 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1111 cfg->pgsize_bitmap, cfg->ias);
1112 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1113 data->levels, data->pgd_size, data->pg_shift,
1114 data->bits_per_level, data->pgd);
1115}
1116
1117#define __FAIL(ops, i) ({ \
1118 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1119 arm_lpae_dump_ops(ops); \
1120 selftest_running = false; \
1121 -EFAULT; \
1122})
1123
1124static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1125{
1126 static const enum io_pgtable_fmt fmts[] = {
1127 ARM_64_LPAE_S1,
1128 ARM_64_LPAE_S2,
1129 };
1130
1131 int i, j;
1132 unsigned long iova;
1133 size_t size;
1134 struct io_pgtable_ops *ops;
1135
1136 selftest_running = true;
1137
1138 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1139 cfg_cookie = cfg;
1140 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1141 if (!ops) {
1142 pr_err("selftest: failed to allocate io pgtable ops\n");
1143 return -ENOMEM;
1144 }
1145
1146 /*
1147 * Initial sanity checks.
1148 * Empty page tables shouldn't provide any translations.
1149 */
1150 if (ops->iova_to_phys(ops, 42))
1151 return __FAIL(ops, i);
1152
1153 if (ops->iova_to_phys(ops, SZ_1G + 42))
1154 return __FAIL(ops, i);
1155
1156 if (ops->iova_to_phys(ops, SZ_2G + 42))
1157 return __FAIL(ops, i);
1158
1159 /*
1160 * Distinct mappings of different granule sizes.
1161 */
1162 iova = 0;
1163 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1164 while (j != BITS_PER_LONG) {
1165 size = 1UL << j;
1166
1167 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1168 IOMMU_WRITE |
1169 IOMMU_NOEXEC |
1170 IOMMU_CACHE))
1171 return __FAIL(ops, i);
1172
1173 /* Overlapping mappings */
1174 if (!ops->map(ops, iova, iova + size, size,
1175 IOMMU_READ | IOMMU_NOEXEC))
1176 return __FAIL(ops, i);
1177
1178 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1179 return __FAIL(ops, i);
1180
1181 iova += SZ_1G;
1182 j++;
1183 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1184 }
1185
1186 /* Partial unmap */
1187 size = 1UL << __ffs(cfg->pgsize_bitmap);
1188 if (ops->unmap(ops, SZ_1G + size, size) != size)
1189 return __FAIL(ops, i);
1190
1191 /* Remap of partial unmap */
1192 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1193 return __FAIL(ops, i);
1194
1195 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1196 return __FAIL(ops, i);
1197
1198 /* Full unmap */
1199 iova = 0;
1200 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1201 while (j != BITS_PER_LONG) {
1202 size = 1UL << j;
1203
1204 if (ops->unmap(ops, iova, size) != size)
1205 return __FAIL(ops, i);
1206
1207 if (ops->iova_to_phys(ops, iova + 42))
1208 return __FAIL(ops, i);
1209
1210 /* Remap full block */
1211 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1212 return __FAIL(ops, i);
1213
1214 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1215 return __FAIL(ops, i);
1216
1217 iova += SZ_1G;
1218 j++;
1219 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1220 }
1221
1222 free_io_pgtable_ops(ops);
1223 }
1224
1225 selftest_running = false;
1226 return 0;
1227}
1228
1229static int __init arm_lpae_do_selftests(void)
1230{
1231 static const unsigned long pgsize[] = {
1232 SZ_4K | SZ_2M | SZ_1G,
1233 SZ_16K | SZ_32M,
1234 SZ_64K | SZ_512M,
1235 };
1236
1237 static const unsigned int ias[] = {
1238 32, 36, 40, 42, 44, 48,
1239 };
1240
1241 int i, j, pass = 0, fail = 0;
1242 struct io_pgtable_cfg cfg = {
1243 .tlb = &dummy_tlb_ops,
1244 .oas = 48,
1245 };
1246
1247 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1248 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1249 cfg.pgsize_bitmap = pgsize[i];
1250 cfg.ias = ias[j];
1251 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1252 pgsize[i], ias[j]);
1253 if (arm_lpae_run_tests(&cfg))
1254 fail++;
1255 else
1256 pass++;
1257 }
1258 }
1259
1260 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1261 return fail ? -EFAULT : 0;
1262}
1263subsys_initcall(arm_lpae_do_selftests);
1264#endif