blob: 37429c499b1bece717ea4b0db0b35ac44666a594 [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 */
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600105#define ARM_LPAE_PTE_AP_PRIV_RW (((arm_lpae_iopte)0) << 6)
106#define ARM_LPAE_PTE_AP_RW (((arm_lpae_iopte)1) << 6)
107#define ARM_LPAE_PTE_AP_PRIV_RO (((arm_lpae_iopte)2) << 6)
108#define ARM_LPAE_PTE_AP_RO (((arm_lpae_iopte)3) << 6)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000109#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
Patrick Dalybf762272016-11-03 16:49:44 -0700168#define ARM_LPAE_MAIR_ATTR_UPSTREAM 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000169#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
170#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
171#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Patrick Dalybf762272016-11-03 16:49:44 -0700172#define ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000173
174/* IOPTE accessors */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700175#define iopte_deref(pte, d) \
176 (__va(iopte_val(pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000177 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000178
179#define iopte_type(pte,l) \
180 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
181
182#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
183
184#define iopte_leaf(pte,l) \
185 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
186 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
187 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
188
189#define iopte_to_pfn(pte,d) \
190 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
191
192#define pfn_to_iopte(pfn,d) \
193 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
194
195struct arm_lpae_io_pgtable {
196 struct io_pgtable iop;
197
198 int levels;
199 size_t pgd_size;
200 unsigned long pg_shift;
201 unsigned long bits_per_level;
202
203 void *pgd;
204};
205
206typedef u64 arm_lpae_iopte;
207
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700208/*
209 * We'll use some ignored bits in table entries to keep track of the number
210 * of page mappings beneath the table. The maximum number of entries
211 * beneath any table mapping in armv8 is 8192 (which is possible at the
212 * 2nd- and 3rd-level when using a 64K granule size). The bits at our
213 * disposal are:
214 *
215 * 4k granule: [58..52], [11..2]
216 * 64k granule: [58..52], [15..2]
217 *
218 * [58..52], [11..2] is enough bits for tracking table mappings at any
219 * level for any granule, so we'll use those.
220 */
221#define BOTTOM_IGNORED_MASK 0x3ff
222#define BOTTOM_IGNORED_SHIFT 2
223#define BOTTOM_IGNORED_NUM_BITS 10
224#define TOP_IGNORED_MASK 0x7fULL
225#define TOP_IGNORED_SHIFT 52
226#define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \
227 (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
228
229static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte)
230{
231 return table_pte & ~IOPTE_RESERVED_MASK;
232}
233
234static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte)
235{
236 return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT))
237 >> BOTTOM_IGNORED_SHIFT;
238}
239
240static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte)
241{
242 return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
243 >> TOP_IGNORED_SHIFT;
244}
245
246static int iopte_tblcnt(arm_lpae_iopte table_pte)
247{
248 return (_iopte_bottom_ignored_val(table_pte) |
249 (_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS));
250}
251
252static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val)
253{
254 arm_lpae_iopte pte = iopte_val(*table_pte);
255
256 pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) |
257 (((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS))
258 >> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT);
259 *table_pte = pte;
260}
261
262static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt)
263{
264 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
265
266 current_cnt -= cnt;
267 iopte_tblcnt_set(table_ptep, current_cnt);
268}
269
270static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt)
271{
272 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
273
274 current_cnt += cnt;
275 iopte_tblcnt_set(table_ptep, current_cnt);
276}
277
Will Deaconfe4b9912014-11-17 23:31:12 +0000278static bool selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700279static bool suppress_map_failures;
Will Deaconfe4b9912014-11-17 23:31:12 +0000280
Robin Murphyffcb6d12015-09-17 17:42:16 +0100281static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100282{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100283 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100284}
285
286static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700287 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100288{
289 struct device *dev = cfg->iommu_dev;
290 dma_addr_t dma;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700291 void *pages = io_pgtable_alloc_pages_exact(cfg, cookie, size,
292 gfp | __GFP_ZERO);
Robin Murphyf8d54962015-07-29 19:46:04 +0100293
294 if (!pages)
295 return NULL;
296
Robin Murphy87a91b12015-07-29 19:46:09 +0100297 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100298 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
299 if (dma_mapping_error(dev, dma))
300 goto out_free;
301 /*
302 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100303 * address directly, so if the DMA layer suggests otherwise by
304 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100305 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100306 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100307 goto out_unmap;
308 }
309
310 return pages;
311
312out_unmap:
313 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
314 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
315out_free:
Patrick Dalyc11d1082016-09-01 15:52:44 -0700316 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100317 return NULL;
318}
319
320static void __arm_lpae_free_pages(void *pages, size_t size,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700321 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100322{
Robin Murphy87a91b12015-07-29 19:46:09 +0100323 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100324 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100325 size, DMA_TO_DEVICE);
Patrick Dalyc11d1082016-09-01 15:52:44 -0700326 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100327}
328
329static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100330 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100331{
Robin Murphyf8d54962015-07-29 19:46:04 +0100332 *ptep = pte;
333
Robin Murphy87a91b12015-07-29 19:46:09 +0100334 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100335 dma_sync_single_for_device(cfg->iommu_dev,
336 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100337 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100338}
339
Will Deacone1d3c0f2014-11-14 17:18:23 +0000340static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
341 unsigned long iova, phys_addr_t paddr,
342 arm_lpae_iopte prot, int lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700343 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep,
344 bool flush)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000345{
346 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100347 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000348
Patrick Daly67ba8eb2016-06-27 18:44:42 -0700349 /* We require an unmap first */
Mitchel Humpherys1b0313e2015-09-23 13:56:27 -0700350 if (*ptep & ARM_LPAE_PTE_VALID) {
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700351 BUG_ON(!suppress_map_failures);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000352 return -EEXIST;
Will Deaconfe4b9912014-11-17 23:31:12 +0000353 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000354
Robin Murphyf8d54962015-07-29 19:46:04 +0100355 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200356 pte |= ARM_LPAE_PTE_NS;
357
Will Deacone1d3c0f2014-11-14 17:18:23 +0000358 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
359 pte |= ARM_LPAE_PTE_TYPE_PAGE;
360 else
361 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
362
Liam Marka8a228d2016-10-04 13:40:53 -0700363 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_OS;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000364 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
365
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700366 if (flush)
367 __arm_lpae_set_pte(ptep, pte, cfg);
368 else
369 *ptep = pte;
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700370
371 if (prev_ptep)
372 iopte_tblcnt_add(prev_ptep, 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000373 return 0;
374}
375
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700376struct map_state {
377 unsigned long iova_end;
378 unsigned int pgsize;
379 arm_lpae_iopte *pgtable;
380 arm_lpae_iopte *prev_pgtable;
381 arm_lpae_iopte *pte_start;
382 unsigned int num_pte;
383};
384/* map state optimization works at level 3 (the 2nd-to-last level) */
385#define MAP_STATE_LVL 3
386
Will Deacone1d3c0f2014-11-14 17:18:23 +0000387static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
388 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700389 int lvl, arm_lpae_iopte *ptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700390 arm_lpae_iopte *prev_ptep, struct map_state *ms)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000391{
392 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000393 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100394 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700395 void *cookie = data->iop.cookie;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700396 arm_lpae_iopte *pgtable = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000397
398 /* Find our entry at the current level */
399 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
400
401 /* If we can install a leaf entry at this level, then do so */
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700402 if (size == block_size && (size & cfg->pgsize_bitmap)) {
403 if (!ms)
404 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
405 ptep, prev_ptep, true);
406
407 if (lvl == MAP_STATE_LVL) {
408 if (ms->pgtable)
409 dma_sync_single_for_device(
410 cfg->iommu_dev,
411 __arm_lpae_dma_addr(ms->pte_start),
412 ms->num_pte * sizeof(*ptep),
413 DMA_TO_DEVICE);
414
415 ms->iova_end = round_down(iova, SZ_2M) + SZ_2M;
416 ms->pgtable = pgtable;
417 ms->prev_pgtable = prev_ptep;
418 ms->pgsize = size;
419 ms->pte_start = ptep;
420 ms->num_pte = 1;
421 } else {
422 /*
423 * We have some map state from previous page
424 * mappings, but we're about to set up a block
425 * mapping. Flush out the previous page mappings.
426 */
427 if (ms->pgtable)
428 dma_sync_single_for_device(
429 cfg->iommu_dev,
430 __arm_lpae_dma_addr(ms->pte_start),
431 ms->num_pte * sizeof(*ptep),
432 DMA_TO_DEVICE);
433 memset(ms, 0, sizeof(*ms));
434 ms = NULL;
435 }
436
437 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
438 ptep, prev_ptep, ms == NULL);
439 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000440
441 /* We can't allocate tables at the final level */
442 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
443 return -EINVAL;
444
445 /* Grab a pointer to the next level */
446 pte = *ptep;
447 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000448 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Patrick Dalyc11d1082016-09-01 15:52:44 -0700449 GFP_ATOMIC, cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000450 if (!cptep)
451 return -ENOMEM;
452
Will Deacone1d3c0f2014-11-14 17:18:23 +0000453 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100454 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200455 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100456 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000457 } else {
458 cptep = iopte_deref(pte, data);
459 }
460
461 /* Rinse, repeat */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700462 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700463 ptep, ms);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000464}
465
466static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
467 int prot)
468{
469 arm_lpae_iopte pte;
470
471 if (data->iop.fmt == ARM_64_LPAE_S1 ||
472 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600473 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000474
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600475 if (prot & IOMMU_WRITE)
476 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RW
477 : ARM_LPAE_PTE_AP_RW;
478 else
479 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RO
480 : ARM_LPAE_PTE_AP_RO;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000481
Liam Marka8a228d2016-10-04 13:40:53 -0700482 if (prot & IOMMU_MMIO)
Robin Murphyfb948252016-04-05 12:39:31 +0100483 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
484 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Liam Marka8a228d2016-10-04 13:40:53 -0700485 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000486 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
487 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Patrick Dalybf762272016-11-03 16:49:44 -0700488 else if (prot & IOMMU_USE_UPSTREAM_HINT)
489 pte |= (ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM
490 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000491 } else {
492 pte = ARM_LPAE_PTE_HAP_FAULT;
493 if (prot & IOMMU_READ)
494 pte |= ARM_LPAE_PTE_HAP_READ;
495 if (prot & IOMMU_WRITE)
496 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100497 if (prot & IOMMU_MMIO)
498 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
499 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000500 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
501 else
502 pte |= ARM_LPAE_PTE_MEMATTR_NC;
503 }
504
505 if (prot & IOMMU_NOEXEC)
506 pte |= ARM_LPAE_PTE_XN;
507
508 return pte;
509}
510
511static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
512 phys_addr_t paddr, size_t size, int iommu_prot)
513{
514 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
515 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100516 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000517 arm_lpae_iopte prot;
518
519 /* If no access, then nothing to do */
520 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
521 return 0;
522
523 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700524 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL,
525 NULL);
Robin Murphy87a91b12015-07-29 19:46:09 +0100526 /*
527 * Synchronise all PTE updates for the new mapping before there's
528 * a chance for anything to kick off a table walk for the new iova.
529 */
530 wmb();
531
532 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000533}
534
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700535static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
536 struct scatterlist *sg, unsigned int nents,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700537 int iommu_prot, size_t *size)
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700538{
539 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
540 arm_lpae_iopte *ptep = data->pgd;
541 int lvl = ARM_LPAE_START_LVL(data);
542 arm_lpae_iopte prot;
543 struct scatterlist *s;
544 size_t mapped = 0;
545 int i, ret;
546 unsigned int min_pagesz;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700547 struct io_pgtable_cfg *cfg = &data->iop.cfg;
548 struct map_state ms;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700549
550 /* If no access, then nothing to do */
551 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700552 goto out_err;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700553
554 prot = arm_lpae_prot_to_pte(data, iommu_prot);
555
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700556 min_pagesz = 1 << __ffs(cfg->pgsize_bitmap);
557
558 memset(&ms, 0, sizeof(ms));
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700559
560 for_each_sg(sg, s, nents, i) {
561 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
562 size_t size = s->length;
563
564 /*
565 * We are mapping on IOMMU page boundaries, so offset within
566 * the page must be 0. However, the IOMMU may support pages
567 * smaller than PAGE_SIZE, so s->offset may still represent
568 * an offset of that boundary within the CPU page.
569 */
570 if (!IS_ALIGNED(s->offset, min_pagesz))
571 goto out_err;
572
573 while (size) {
574 size_t pgsize = iommu_pgsize(
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700575 cfg->pgsize_bitmap, iova | phys, size);
576
577 if (ms.pgtable && (iova < ms.iova_end)) {
578 arm_lpae_iopte *ptep = ms.pgtable +
579 ARM_LPAE_LVL_IDX(iova, MAP_STATE_LVL,
580 data);
581 arm_lpae_init_pte(
582 data, iova, phys, prot, MAP_STATE_LVL,
583 ptep, ms.prev_pgtable, false);
584 ms.num_pte++;
585 } else {
586 ret = __arm_lpae_map(data, iova, phys, pgsize,
587 prot, lvl, ptep, NULL, &ms);
588 if (ret)
589 goto out_err;
590 }
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700591
592 iova += pgsize;
593 mapped += pgsize;
594 phys += pgsize;
595 size -= pgsize;
596 }
597 }
598
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700599 if (ms.pgtable)
600 dma_sync_single_for_device(
601 cfg->iommu_dev, __arm_lpae_dma_addr(ms.pte_start),
602 ms.num_pte * sizeof(*ptep), DMA_TO_DEVICE);
603
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700604 return mapped;
605
606out_err:
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700607 /* Return the size of the partial mapping so that they can be undone */
608 *size = mapped;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700609 return 0;
610}
611
Will Deacone1d3c0f2014-11-14 17:18:23 +0000612static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
613 arm_lpae_iopte *ptep)
614{
615 arm_lpae_iopte *start, *end;
616 unsigned long table_size;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700617 void *cookie = data->iop.cookie;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000618
Will Deacone1d3c0f2014-11-14 17:18:23 +0000619 if (lvl == ARM_LPAE_START_LVL(data))
620 table_size = data->pgd_size;
621 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000622 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000623
624 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000625
626 /* Only leaf entries at the last level */
627 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
628 end = ptep;
629 else
630 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000631
632 while (ptep != end) {
633 arm_lpae_iopte pte = *ptep++;
634
635 if (!pte || iopte_leaf(pte, lvl))
636 continue;
637
638 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
639 }
640
Patrick Dalyc11d1082016-09-01 15:52:44 -0700641 __arm_lpae_free_pages(start, table_size, &data->iop.cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000642}
643
644static void arm_lpae_free_pgtable(struct io_pgtable *iop)
645{
646 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
647
648 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
649 kfree(data);
650}
651
652static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
653 unsigned long iova, size_t size,
654 arm_lpae_iopte prot, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700655 arm_lpae_iopte *ptep,
656 arm_lpae_iopte *prev_ptep, size_t blk_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000657{
658 unsigned long blk_start, blk_end;
659 phys_addr_t blk_paddr;
660 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000661
662 blk_start = iova & ~(blk_size - 1);
663 blk_end = blk_start + blk_size;
664 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
Patrick Daly781558f2016-10-13 16:03:27 -0700665 size = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova, size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000666
667 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
668 arm_lpae_iopte *tablep;
669
670 /* Unmap! */
671 if (blk_start == iova)
672 continue;
673
674 /* __arm_lpae_map expects a pointer to the start of the table */
675 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
676 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700677 tablep, prev_ptep, NULL) < 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000678 if (table) {
679 /* Free the table we allocated */
680 tablep = iopte_deref(table, data);
681 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
682 }
683 return 0; /* Bytes unmapped */
684 }
685 }
686
Robin Murphy507e4c92016-01-26 17:13:14 +0000687 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000688 return size;
689}
690
691static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
692 unsigned long iova, size_t size, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700693 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000694{
695 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000696 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000697 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
698
Robin Murphy2eb97c72015-12-04 17:52:58 +0000699 /* Something went horribly wrong and we ran out of page table */
700 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
701 return 0;
702
Will Deacone1d3c0f2014-11-14 17:18:23 +0000703 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
704 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000705 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000706 return 0;
707
708 /* If the size matches this level, we're in the right place */
709 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000710 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000711
712 if (!iopte_leaf(pte, lvl)) {
713 /* Also flush any partial walks */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000714 ptep = iopte_deref(pte, data);
715 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000716 }
717
718 return size;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700719 } else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) {
720 arm_lpae_iopte *table = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700721 arm_lpae_iopte *table_base = table;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700722 int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data);
723 int entry_size = ARM_LPAE_GRANULE(data);
724 int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) / entry_size;
725 int entries = min_t(int, size / entry_size,
726 max_entries - tl_offset);
727 int table_len = entries * sizeof(*table);
728
729 /*
730 * This isn't a block mapping so it must be a table mapping
731 * and since it's the 2nd-to-last level the next level has
732 * to be all page mappings. Zero them all out in one fell
733 * swoop.
734 */
735
736 table += tl_offset;
737
738 memset(table, 0, table_len);
739 dma_sync_single_for_device(iop->cfg.iommu_dev,
740 __arm_lpae_dma_addr(table),
741 table_len, DMA_TO_DEVICE);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700742
743 iopte_tblcnt_sub(ptep, entries);
744 if (!iopte_tblcnt(*ptep)) {
745 /* no valid mappings left under this table. free it. */
746 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
747 io_pgtable_tlb_add_flush(iop, iova,
748 entries * entry_size,
749 ARM_LPAE_GRANULE(data),
750 false);
751 __arm_lpae_free_pgtable(data, lvl + 1, table_base);
752 } else {
753 io_pgtable_tlb_add_flush(iop, iova,
754 entries * entry_size,
755 ARM_LPAE_GRANULE(data),
756 true);
757 }
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700758
759 return entries * entry_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000760 } else if (iopte_leaf(pte, lvl)) {
761 /*
762 * Insert a table at the next level to map the old region,
763 * minus the part we want to unmap
764 */
765 return arm_lpae_split_blk_unmap(data, iova, size,
766 iopte_prot(pte), lvl, ptep,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700767 prev_ptep, blk_size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000768 }
769
770 /* Keep on walkin' */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700771 prev_ptep = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000772 ptep = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700773 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000774}
775
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700776static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000777 size_t size)
778{
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700779 size_t unmapped = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000780 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000781 arm_lpae_iopte *ptep = data->pgd;
782 int lvl = ARM_LPAE_START_LVL(data);
783
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700784 while (unmapped < size) {
785 size_t ret, size_to_unmap, remaining;
786
787 remaining = (size - unmapped);
Patrick Dalyf145f052016-06-27 18:38:09 -0700788 size_to_unmap = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova,
789 remaining);
790 size_to_unmap = size_to_unmap >= SZ_2M ?
791 size_to_unmap :
792 min_t(unsigned long, remaining,
793 (ALIGN(iova + 1, SZ_2M) - iova));
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700794 ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep,
795 NULL);
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700796 if (ret == 0)
797 break;
798 unmapped += ret;
799 iova += ret;
800 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000801 if (unmapped)
Mitchel Humpherysfaa87fc2015-04-24 17:10:59 -0700802 io_pgtable_tlb_flush_all(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000803
804 return unmapped;
805}
806
807static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
808 unsigned long iova)
809{
810 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
811 arm_lpae_iopte pte, *ptep = data->pgd;
812 int lvl = ARM_LPAE_START_LVL(data);
813
814 do {
815 /* Valid IOPTE pointer? */
816 if (!ptep)
817 return 0;
818
819 /* Grab the IOPTE we're interested in */
820 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
821
822 /* Valid entry? */
823 if (!pte)
824 return 0;
825
826 /* Leaf entry? */
827 if (iopte_leaf(pte,lvl))
828 goto found_translation;
829
830 /* Take it to the next level */
831 ptep = iopte_deref(pte, data);
832 } while (++lvl < ARM_LPAE_MAX_LEVELS);
833
834 /* Ran out of page tables to walk */
835 return 0;
836
837found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100838 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000839 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
840}
841
842static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
843{
844 unsigned long granule;
845
846 /*
847 * We need to restrict the supported page sizes to match the
848 * translation regime for a particular granule. Aim to match
849 * the CPU page size if possible, otherwise prefer smaller sizes.
850 * While we're at it, restrict the block sizes to match the
851 * chosen granule.
852 */
853 if (cfg->pgsize_bitmap & PAGE_SIZE)
854 granule = PAGE_SIZE;
855 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
856 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
857 else if (cfg->pgsize_bitmap & PAGE_MASK)
858 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
859 else
860 granule = 0;
861
862 switch (granule) {
863 case SZ_4K:
864 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
865 break;
866 case SZ_16K:
867 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
868 break;
869 case SZ_64K:
870 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
871 break;
872 default:
873 cfg->pgsize_bitmap = 0;
874 }
875}
876
877static struct arm_lpae_io_pgtable *
878arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
879{
880 unsigned long va_bits, pgd_bits;
881 struct arm_lpae_io_pgtable *data;
882
883 arm_lpae_restrict_pgsizes(cfg);
884
885 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
886 return NULL;
887
888 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
889 return NULL;
890
891 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
892 return NULL;
893
Robin Murphyffcb6d12015-09-17 17:42:16 +0100894 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
895 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
896 return NULL;
897 }
898
Will Deacone1d3c0f2014-11-14 17:18:23 +0000899 data = kmalloc(sizeof(*data), GFP_KERNEL);
900 if (!data)
901 return NULL;
902
903 data->pg_shift = __ffs(cfg->pgsize_bitmap);
904 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
905
906 va_bits = cfg->ias - data->pg_shift;
907 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
908
909 /* Calculate the actual size of our pgd (without concatenation) */
910 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
911 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
912
913 data->iop.ops = (struct io_pgtable_ops) {
914 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700915 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000916 .unmap = arm_lpae_unmap,
917 .iova_to_phys = arm_lpae_iova_to_phys,
918 };
919
920 return data;
921}
922
923static struct io_pgtable *
924arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
925{
926 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000927 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000928
Robin Murphy3850db42016-02-12 17:09:46 +0000929 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000930 if (!data)
931 return NULL;
932
933 /* TCR */
Mitchel Humpherys45b2e972016-06-07 14:18:22 -0700934 if (cfg->iommu_dev && cfg->iommu_dev->archdata.dma_coherent)
935 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
936 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
937 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
Liam Mark5649c822016-12-19 14:35:08 -0800938 else if (cfg->quirks & IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT)
Patrick Dalyce6786f2016-11-09 14:19:23 -0800939 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
940 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
941 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
Mitchel Humpherys45b2e972016-06-07 14:18:22 -0700942 else
Liam Marka8a228d2016-10-04 13:40:53 -0700943 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
Mitchel Humpherys45b2e972016-06-07 14:18:22 -0700944 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
945 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000946
Robin Murphy06c610e2015-12-07 18:18:53 +0000947 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000948 case SZ_4K:
949 reg |= ARM_LPAE_TCR_TG0_4K;
950 break;
951 case SZ_16K:
952 reg |= ARM_LPAE_TCR_TG0_16K;
953 break;
954 case SZ_64K:
955 reg |= ARM_LPAE_TCR_TG0_64K;
956 break;
957 }
958
959 switch (cfg->oas) {
960 case 32:
961 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
962 break;
963 case 36:
964 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
965 break;
966 case 40:
967 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
968 break;
969 case 42:
970 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
971 break;
972 case 44:
973 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
974 break;
975 case 48:
976 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
977 break;
978 default:
979 goto out_free_data;
980 }
981
982 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000983
984 /* Disable speculative walks through TTBR1 */
985 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000986 cfg->arm_lpae_s1_cfg.tcr = reg;
987
988 /* MAIRs */
989 reg = (ARM_LPAE_MAIR_ATTR_NC
990 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
991 (ARM_LPAE_MAIR_ATTR_WBRWA
992 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
993 (ARM_LPAE_MAIR_ATTR_DEVICE
Patrick Dalybf762272016-11-03 16:49:44 -0700994 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
995 (ARM_LPAE_MAIR_ATTR_UPSTREAM
996 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000997
998 cfg->arm_lpae_s1_cfg.mair[0] = reg;
999 cfg->arm_lpae_s1_cfg.mair[1] = 0;
1000
1001 /* Looking good; allocate a pgd */
Patrick Dalyc11d1082016-09-01 15:52:44 -07001002 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
1003 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001004 if (!data->pgd)
1005 goto out_free_data;
1006
Robin Murphy87a91b12015-07-29 19:46:09 +01001007 /* Ensure the empty pgd is visible before any actual TTBR write */
1008 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001009
1010 /* TTBRs */
1011 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
1012 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
1013 return &data->iop;
1014
1015out_free_data:
1016 kfree(data);
1017 return NULL;
1018}
1019
1020static struct io_pgtable *
1021arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1022{
1023 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +00001024 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001025
Robin Murphy3850db42016-02-12 17:09:46 +00001026 /* The NS quirk doesn't apply at stage 2 */
1027 if (cfg->quirks)
1028 return NULL;
1029
1030 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001031 if (!data)
1032 return NULL;
1033
1034 /*
1035 * Concatenate PGDs at level 1 if possible in order to reduce
1036 * the depth of the stage-2 walk.
1037 */
1038 if (data->levels == ARM_LPAE_MAX_LEVELS) {
1039 unsigned long pgd_pages;
1040
1041 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
1042 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
1043 data->pgd_size = pgd_pages << data->pg_shift;
1044 data->levels--;
1045 }
1046 }
1047
1048 /* VTCR */
1049 reg = ARM_64_LPAE_S2_TCR_RES1 |
1050 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
1051 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
1052 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
1053
1054 sl = ARM_LPAE_START_LVL(data);
1055
Robin Murphy06c610e2015-12-07 18:18:53 +00001056 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +00001057 case SZ_4K:
1058 reg |= ARM_LPAE_TCR_TG0_4K;
1059 sl++; /* SL0 format is different for 4K granule size */
1060 break;
1061 case SZ_16K:
1062 reg |= ARM_LPAE_TCR_TG0_16K;
1063 break;
1064 case SZ_64K:
1065 reg |= ARM_LPAE_TCR_TG0_64K;
1066 break;
1067 }
1068
1069 switch (cfg->oas) {
1070 case 32:
1071 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
1072 break;
1073 case 36:
1074 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
1075 break;
1076 case 40:
1077 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
1078 break;
1079 case 42:
1080 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
1081 break;
1082 case 44:
1083 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
1084 break;
1085 case 48:
1086 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
1087 break;
1088 default:
1089 goto out_free_data;
1090 }
1091
1092 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
1093 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
1094 cfg->arm_lpae_s2_cfg.vtcr = reg;
1095
1096 /* Allocate pgd pages */
Patrick Dalyc11d1082016-09-01 15:52:44 -07001097 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
1098 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001099 if (!data->pgd)
1100 goto out_free_data;
1101
Robin Murphy87a91b12015-07-29 19:46:09 +01001102 /* Ensure the empty pgd is visible before any actual TTBR write */
1103 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001104
1105 /* VTTBR */
1106 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1107 return &data->iop;
1108
1109out_free_data:
1110 kfree(data);
1111 return NULL;
1112}
1113
1114static struct io_pgtable *
1115arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1116{
1117 struct io_pgtable *iop;
1118
1119 if (cfg->ias > 32 || cfg->oas > 40)
1120 return NULL;
1121
1122 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1123 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1124 if (iop) {
1125 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
1126 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1127 }
1128
1129 return iop;
1130}
1131
1132static struct io_pgtable *
1133arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1134{
1135 struct io_pgtable *iop;
1136
1137 if (cfg->ias > 40 || cfg->oas > 40)
1138 return NULL;
1139
1140 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1141 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1142 if (iop)
1143 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1144
1145 return iop;
1146}
1147
1148struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1149 .alloc = arm_64_lpae_alloc_pgtable_s1,
1150 .free = arm_lpae_free_pgtable,
1151};
1152
1153struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1154 .alloc = arm_64_lpae_alloc_pgtable_s2,
1155 .free = arm_lpae_free_pgtable,
1156};
1157
1158struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1159 .alloc = arm_32_lpae_alloc_pgtable_s1,
1160 .free = arm_lpae_free_pgtable,
1161};
1162
1163struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1164 .alloc = arm_32_lpae_alloc_pgtable_s2,
1165 .free = arm_lpae_free_pgtable,
1166};
Will Deaconfe4b9912014-11-17 23:31:12 +00001167
1168#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1169
1170static struct io_pgtable_cfg *cfg_cookie;
1171
1172static void dummy_tlb_flush_all(void *cookie)
1173{
1174 WARN_ON(cookie != cfg_cookie);
1175}
1176
Robin Murphy06c610e2015-12-07 18:18:53 +00001177static void dummy_tlb_add_flush(unsigned long iova, size_t size,
1178 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001179{
1180 WARN_ON(cookie != cfg_cookie);
Will Deaconfe4b9912014-11-17 23:31:12 +00001181}
1182
1183static void dummy_tlb_sync(void *cookie)
1184{
1185 WARN_ON(cookie != cfg_cookie);
1186}
1187
Will Deaconfe4b9912014-11-17 23:31:12 +00001188static struct iommu_gather_ops dummy_tlb_ops __initdata = {
1189 .tlb_flush_all = dummy_tlb_flush_all,
1190 .tlb_add_flush = dummy_tlb_add_flush,
1191 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +00001192};
1193
1194static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1195{
1196 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1197 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1198
1199 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1200 cfg->pgsize_bitmap, cfg->ias);
1201 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1202 data->levels, data->pgd_size, data->pg_shift,
1203 data->bits_per_level, data->pgd);
1204}
1205
1206#define __FAIL(ops, i) ({ \
1207 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1208 arm_lpae_dump_ops(ops); \
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001209 suppress_map_failures = false; \
Will Deaconfe4b9912014-11-17 23:31:12 +00001210 selftest_running = false; \
1211 -EFAULT; \
1212})
1213
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001214/*
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001215 * Returns true if there's any mapping in the given iova range in ops.
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001216 */
1217static bool arm_lpae_range_has_mapping(struct io_pgtable_ops *ops,
1218 unsigned long iova_start, size_t size)
1219{
1220 unsigned long iova = iova_start;
1221
1222 while (iova < (iova_start + size)) {
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001223 if (ops->iova_to_phys(ops, iova + 42))
1224 return true;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001225 iova += SZ_4K;
1226 }
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001227 return false;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001228}
1229
1230/*
1231 * Returns true if the iova range is successfully mapped to the contiguous
1232 * phys range in ops.
1233 */
1234static bool arm_lpae_range_has_specific_mapping(struct io_pgtable_ops *ops,
1235 const unsigned long iova_start,
1236 const phys_addr_t phys_start,
1237 const size_t size)
1238{
1239 unsigned long iova = iova_start;
1240 phys_addr_t phys = phys_start;
1241
1242 while (iova < (iova_start + size)) {
1243 if (ops->iova_to_phys(ops, iova + 42) != (phys + 42))
1244 return false;
1245 iova += SZ_4K;
1246 phys += SZ_4K;
1247 }
1248 return true;
1249}
1250
Will Deaconfe4b9912014-11-17 23:31:12 +00001251static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1252{
1253 static const enum io_pgtable_fmt fmts[] = {
1254 ARM_64_LPAE_S1,
1255 ARM_64_LPAE_S2,
1256 };
1257
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001258 int i, j, k;
Will Deaconfe4b9912014-11-17 23:31:12 +00001259 unsigned long iova;
1260 size_t size;
1261 struct io_pgtable_ops *ops;
Will Deaconfe4b9912014-11-17 23:31:12 +00001262 selftest_running = true;
1263
1264 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001265 unsigned long test_sg_sizes[] = { SZ_4K, SZ_64K, SZ_2M,
1266 SZ_1M * 12, SZ_1M * 20 };
1267
Will Deaconfe4b9912014-11-17 23:31:12 +00001268 cfg_cookie = cfg;
1269 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1270 if (!ops) {
1271 pr_err("selftest: failed to allocate io pgtable ops\n");
1272 return -ENOMEM;
1273 }
1274
1275 /*
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001276 * Initial sanity checks. Empty page tables shouldn't
1277 * provide any translations. TODO: check entire supported
1278 * range for these ops rather than first 2G
Will Deaconfe4b9912014-11-17 23:31:12 +00001279 */
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001280 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
Will Deaconfe4b9912014-11-17 23:31:12 +00001281 return __FAIL(ops, i);
1282
1283 /*
1284 * Distinct mappings of different granule sizes.
1285 */
1286 iova = 0;
1287 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1288 while (j != BITS_PER_LONG) {
1289 size = 1UL << j;
1290
1291 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1292 IOMMU_WRITE |
1293 IOMMU_NOEXEC |
1294 IOMMU_CACHE))
1295 return __FAIL(ops, i);
1296
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001297 suppress_map_failures = true;
Will Deaconfe4b9912014-11-17 23:31:12 +00001298 /* Overlapping mappings */
1299 if (!ops->map(ops, iova, iova + size, size,
1300 IOMMU_READ | IOMMU_NOEXEC))
1301 return __FAIL(ops, i);
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001302 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001303
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001304 if (!arm_lpae_range_has_specific_mapping(ops, iova,
1305 iova, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001306 return __FAIL(ops, i);
1307
1308 iova += SZ_1G;
1309 j++;
1310 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1311 }
1312
1313 /* Partial unmap */
1314 size = 1UL << __ffs(cfg->pgsize_bitmap);
1315 if (ops->unmap(ops, SZ_1G + size, size) != size)
1316 return __FAIL(ops, i);
1317
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001318 if (arm_lpae_range_has_mapping(ops, SZ_1G + size, size))
1319 return __FAIL(ops, i);
1320
Will Deaconfe4b9912014-11-17 23:31:12 +00001321 /* Remap of partial unmap */
1322 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1323 return __FAIL(ops, i);
1324
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001325 if (!arm_lpae_range_has_specific_mapping(ops, SZ_1G + size,
1326 size, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001327 return __FAIL(ops, i);
1328
1329 /* Full unmap */
1330 iova = 0;
1331 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1332 while (j != BITS_PER_LONG) {
1333 size = 1UL << j;
1334
1335 if (ops->unmap(ops, iova, size) != size)
1336 return __FAIL(ops, i);
1337
1338 if (ops->iova_to_phys(ops, iova + 42))
1339 return __FAIL(ops, i);
1340
1341 /* Remap full block */
1342 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1343 return __FAIL(ops, i);
1344
1345 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1346 return __FAIL(ops, i);
1347
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001348 if (ops->unmap(ops, iova, size) != size)
1349 return __FAIL(ops, i);
1350
Will Deaconfe4b9912014-11-17 23:31:12 +00001351 iova += SZ_1G;
1352 j++;
1353 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1354 }
1355
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001356 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1357 return __FAIL(ops, i);
1358
Mitchel Humpheryse4012a62015-06-01 15:44:49 -07001359 if ((cfg->pgsize_bitmap & SZ_2M) &&
1360 (cfg->pgsize_bitmap & SZ_4K)) {
1361 /* mixed block + page mappings */
1362 iova = 0;
1363 if (ops->map(ops, iova, iova, SZ_2M, IOMMU_READ))
1364 return __FAIL(ops, i);
1365
1366 if (ops->map(ops, iova + SZ_2M, iova + SZ_2M, SZ_4K,
1367 IOMMU_READ))
1368 return __FAIL(ops, i);
1369
1370 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1371 return __FAIL(ops, i);
1372
1373 if (ops->iova_to_phys(ops, iova + SZ_2M + 42) !=
1374 (iova + SZ_2M + 42))
1375 return __FAIL(ops, i);
1376
1377 /* unmap both mappings at once */
1378 if (ops->unmap(ops, iova, SZ_2M + SZ_4K) !=
1379 (SZ_2M + SZ_4K))
1380 return __FAIL(ops, i);
1381
1382 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1383 return __FAIL(ops, i);
1384 }
1385
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001386 /* map_sg */
1387 for (j = 0; j < ARRAY_SIZE(test_sg_sizes); ++j) {
1388 size_t mapped;
1389 size_t unused;
1390 struct page *page;
1391 phys_addr_t page_phys;
1392 struct sg_table table;
1393 struct scatterlist *sg;
1394 unsigned long total_size = test_sg_sizes[j];
1395 int chunk_size = 1UL << find_first_bit(
1396 &cfg->pgsize_bitmap, BITS_PER_LONG);
1397 int nents = total_size / chunk_size;
1398
1399 if (total_size < chunk_size)
1400 continue;
1401
1402 page = alloc_pages(GFP_KERNEL, get_order(chunk_size));
1403 page_phys = page_to_phys(page);
1404
1405 iova = 0;
1406 BUG_ON(sg_alloc_table(&table, nents, GFP_KERNEL));
1407 BUG_ON(!page);
1408 for_each_sg(table.sgl, sg, table.nents, k)
1409 sg_set_page(sg, page, chunk_size, 0);
1410
1411 mapped = ops->map_sg(ops, iova, table.sgl, table.nents,
1412 IOMMU_READ | IOMMU_WRITE, &unused);
1413
1414 if (mapped != total_size)
1415 return __FAIL(ops, i);
1416
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001417 if (!arm_lpae_range_has_mapping(ops, iova, total_size))
1418 return __FAIL(ops, i);
1419
1420 if (arm_lpae_range_has_mapping(ops, iova + total_size,
1421 SZ_2G - (iova + total_size)))
1422 return __FAIL(ops, i);
1423
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001424 for_each_sg(table.sgl, sg, table.nents, k) {
1425 dma_addr_t newphys =
1426 ops->iova_to_phys(ops, iova + 42);
1427 if (newphys != (page_phys + 42))
1428 return __FAIL(ops, i);
1429 iova += chunk_size;
1430 }
1431
1432 if (ops->unmap(ops, 0, total_size) != total_size)
1433 return __FAIL(ops, i);
1434
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001435 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1436 return __FAIL(ops, i);
1437
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001438 sg_free_table(&table);
1439 __free_pages(page, get_order(chunk_size));
1440 }
1441
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001442 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1443 return __FAIL(ops, i);
1444
Will Deaconfe4b9912014-11-17 23:31:12 +00001445 free_io_pgtable_ops(ops);
1446 }
1447
1448 selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001449 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001450 return 0;
1451}
1452
1453static int __init arm_lpae_do_selftests(void)
1454{
1455 static const unsigned long pgsize[] = {
1456 SZ_4K | SZ_2M | SZ_1G,
Will Deaconfe4b9912014-11-17 23:31:12 +00001457 };
1458
1459 static const unsigned int ias[] = {
1460 32, 36, 40, 42, 44, 48,
1461 };
1462
1463 int i, j, pass = 0, fail = 0;
1464 struct io_pgtable_cfg cfg = {
1465 .tlb = &dummy_tlb_ops,
1466 .oas = 48,
1467 };
1468
1469 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1470 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1471 cfg.pgsize_bitmap = pgsize[i];
1472 cfg.ias = ias[j];
1473 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1474 pgsize[i], ias[j]);
1475 if (arm_lpae_run_tests(&cfg))
1476 fail++;
1477 else
1478 pass++;
1479 }
1480 }
1481
1482 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1483 return fail ? -EFAULT : 0;
1484}
1485subsys_initcall(arm_lpae_do_selftests);
1486#endif