blob: 2ef496d541dca45036fa42ce19df2dfc7360749a [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
Patrick Dalyd35ec7c2016-11-23 15:04:24 -080072#define ARM_LPAE_LVL_MASK(l, d) \
73 ((l) == ARM_LPAE_START_LVL(d) ? (1 << (d)->pgd_bits) - 1 : \
74 (1 << (d)->bits_per_level) - 1)
Will Deacone1d3c0f2014-11-14 17:18:23 +000075#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000076 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Patrick Dalyd35ec7c2016-11-23 15:04:24 -080077 ARM_LPAE_LVL_MASK(l, d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000078
79/* Calculate the block/page mapping size at level l for pagetable in d. */
80#define ARM_LPAE_BLOCK_SIZE(l,d) \
Patrick Daly3b264572017-04-03 18:20:37 -070081 (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \
Will Deacone1d3c0f2014-11-14 17:18:23 +000082 ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
83
84/* Page table bits */
85#define ARM_LPAE_PTE_TYPE_SHIFT 0
86#define ARM_LPAE_PTE_TYPE_MASK 0x3
87
88#define ARM_LPAE_PTE_TYPE_BLOCK 1
89#define ARM_LPAE_PTE_TYPE_TABLE 3
90#define ARM_LPAE_PTE_TYPE_PAGE 3
91
Liam Mark1402f942017-03-17 10:26:49 -070092#define ARM_LPAE_PTE_SH_MASK (((arm_lpae_iopte)0x3) << 8)
Laurent Pinchartc896c132014-12-14 23:34:50 +020093#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000094#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
95#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
96#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
97#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
98#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c132014-12-14 23:34:50 +020099#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000100#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
101
102#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
103/* Ignore the contiguous bit for block splitting */
104#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
105#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
106 ARM_LPAE_PTE_ATTR_HI_MASK)
107
108/* Stage-1 PTE */
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600109#define ARM_LPAE_PTE_AP_PRIV_RW (((arm_lpae_iopte)0) << 6)
110#define ARM_LPAE_PTE_AP_RW (((arm_lpae_iopte)1) << 6)
111#define ARM_LPAE_PTE_AP_PRIV_RO (((arm_lpae_iopte)2) << 6)
112#define ARM_LPAE_PTE_AP_RO (((arm_lpae_iopte)3) << 6)
Liam Mark17f31802016-12-09 14:30:10 -0800113#define ARM_LPAE_PTE_ATTRINDX_MASK 0x7
Will Deacone1d3c0f2014-11-14 17:18:23 +0000114#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
115#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
116
117/* Stage-2 PTE */
118#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
119#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
120#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
121#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
122#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
123#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
124
125/* Register bits */
126#define ARM_32_LPAE_TCR_EAE (1 << 31)
127#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
128
Will Deacon63979b82015-03-18 10:22:18 +0000129#define ARM_LPAE_TCR_EPD1 (1 << 23)
130
Will Deacone1d3c0f2014-11-14 17:18:23 +0000131#define ARM_LPAE_TCR_TG0_4K (0 << 14)
132#define ARM_LPAE_TCR_TG0_64K (1 << 14)
133#define ARM_LPAE_TCR_TG0_16K (2 << 14)
134
135#define ARM_LPAE_TCR_SH0_SHIFT 12
136#define ARM_LPAE_TCR_SH0_MASK 0x3
137#define ARM_LPAE_TCR_SH_NS 0
138#define ARM_LPAE_TCR_SH_OS 2
139#define ARM_LPAE_TCR_SH_IS 3
140
141#define ARM_LPAE_TCR_ORGN0_SHIFT 10
142#define ARM_LPAE_TCR_IRGN0_SHIFT 8
143#define ARM_LPAE_TCR_RGN_MASK 0x3
144#define ARM_LPAE_TCR_RGN_NC 0
145#define ARM_LPAE_TCR_RGN_WBWA 1
146#define ARM_LPAE_TCR_RGN_WT 2
147#define ARM_LPAE_TCR_RGN_WB 3
148
149#define ARM_LPAE_TCR_SL0_SHIFT 6
150#define ARM_LPAE_TCR_SL0_MASK 0x3
151
152#define ARM_LPAE_TCR_T0SZ_SHIFT 0
153#define ARM_LPAE_TCR_SZ_MASK 0xf
154
155#define ARM_LPAE_TCR_PS_SHIFT 16
156#define ARM_LPAE_TCR_PS_MASK 0x7
157
158#define ARM_LPAE_TCR_IPS_SHIFT 32
159#define ARM_LPAE_TCR_IPS_MASK 0x7
160
161#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
162#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
163#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
164#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
165#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
166#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
167
168#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
169#define ARM_LPAE_MAIR_ATTR_MASK 0xff
170#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
171#define ARM_LPAE_MAIR_ATTR_NC 0x44
172#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
Patrick Dalybf762272016-11-03 16:49:44 -0700173#define ARM_LPAE_MAIR_ATTR_UPSTREAM 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000174#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
175#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
176#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Patrick Dalybf762272016-11-03 16:49:44 -0700177#define ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000178
179/* IOPTE accessors */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700180#define iopte_deref(pte, d) \
181 (__va(iopte_val(pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000182 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000183
184#define iopte_type(pte,l) \
185 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
186
187#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
188
189#define iopte_leaf(pte,l) \
190 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
191 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
192 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
193
194#define iopte_to_pfn(pte,d) \
195 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
196
197#define pfn_to_iopte(pfn,d) \
198 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
199
200struct arm_lpae_io_pgtable {
201 struct io_pgtable iop;
202
203 int levels;
Patrick Dalyd35ec7c2016-11-23 15:04:24 -0800204 unsigned int pgd_bits;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000205 size_t pgd_size;
206 unsigned long pg_shift;
207 unsigned long bits_per_level;
208
209 void *pgd;
210};
211
212typedef u64 arm_lpae_iopte;
213
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700214/*
215 * We'll use some ignored bits in table entries to keep track of the number
216 * of page mappings beneath the table. The maximum number of entries
217 * beneath any table mapping in armv8 is 8192 (which is possible at the
218 * 2nd- and 3rd-level when using a 64K granule size). The bits at our
219 * disposal are:
220 *
221 * 4k granule: [58..52], [11..2]
222 * 64k granule: [58..52], [15..2]
223 *
224 * [58..52], [11..2] is enough bits for tracking table mappings at any
225 * level for any granule, so we'll use those.
226 */
227#define BOTTOM_IGNORED_MASK 0x3ff
228#define BOTTOM_IGNORED_SHIFT 2
229#define BOTTOM_IGNORED_NUM_BITS 10
230#define TOP_IGNORED_MASK 0x7fULL
231#define TOP_IGNORED_SHIFT 52
232#define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \
233 (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
234
235static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte)
236{
237 return table_pte & ~IOPTE_RESERVED_MASK;
238}
239
240static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte)
241{
242 return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT))
243 >> BOTTOM_IGNORED_SHIFT;
244}
245
246static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte)
247{
248 return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT))
249 >> TOP_IGNORED_SHIFT;
250}
251
252static int iopte_tblcnt(arm_lpae_iopte table_pte)
253{
254 return (_iopte_bottom_ignored_val(table_pte) |
255 (_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS));
256}
257
258static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val)
259{
260 arm_lpae_iopte pte = iopte_val(*table_pte);
261
262 pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) |
263 (((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS))
264 >> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT);
265 *table_pte = pte;
266}
267
268static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt)
269{
270 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
271
272 current_cnt -= cnt;
273 iopte_tblcnt_set(table_ptep, current_cnt);
274}
275
276static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt)
277{
278 arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep);
279
280 current_cnt += cnt;
281 iopte_tblcnt_set(table_ptep, current_cnt);
282}
283
Will Deaconfe4b9912014-11-17 23:31:12 +0000284static bool selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700285static bool suppress_map_failures;
Will Deaconfe4b9912014-11-17 23:31:12 +0000286
Robin Murphyffcb6d12015-09-17 17:42:16 +0100287static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100288{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100289 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100290}
291
Liam Mark04b0c852016-12-20 11:34:04 -0800292static inline void pgtable_dma_sync_single_for_device(
293 struct io_pgtable_cfg *cfg,
294 dma_addr_t addr, size_t size,
295 enum dma_data_direction dir)
296{
297 if (!(cfg->quirks & IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT))
298 dma_sync_single_for_device(cfg->iommu_dev, addr, size,
299 dir);
300}
301
Robin Murphyf8d54962015-07-29 19:46:04 +0100302static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700303 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100304{
305 struct device *dev = cfg->iommu_dev;
306 dma_addr_t dma;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700307 void *pages = io_pgtable_alloc_pages_exact(cfg, cookie, size,
308 gfp | __GFP_ZERO);
Robin Murphyf8d54962015-07-29 19:46:04 +0100309
310 if (!pages)
311 return NULL;
312
Robin Murphy87a91b12015-07-29 19:46:09 +0100313 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100314 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
315 if (dma_mapping_error(dev, dma))
316 goto out_free;
317 /*
318 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100319 * address directly, so if the DMA layer suggests otherwise by
320 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100321 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100322 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100323 goto out_unmap;
324 }
325
326 return pages;
327
328out_unmap:
329 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
330 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
331out_free:
Patrick Dalyc11d1082016-09-01 15:52:44 -0700332 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100333 return NULL;
334}
335
336static void __arm_lpae_free_pages(void *pages, size_t size,
Patrick Dalyc11d1082016-09-01 15:52:44 -0700337 struct io_pgtable_cfg *cfg, void *cookie)
Robin Murphyf8d54962015-07-29 19:46:04 +0100338{
Robin Murphy87a91b12015-07-29 19:46:09 +0100339 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100340 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100341 size, DMA_TO_DEVICE);
Patrick Dalyc11d1082016-09-01 15:52:44 -0700342 io_pgtable_free_pages_exact(cfg, cookie, pages, size);
Robin Murphyf8d54962015-07-29 19:46:04 +0100343}
344
345static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100346 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100347{
Robin Murphyf8d54962015-07-29 19:46:04 +0100348 *ptep = pte;
349
Robin Murphy87a91b12015-07-29 19:46:09 +0100350 if (!selftest_running)
Liam Mark04b0c852016-12-20 11:34:04 -0800351 pgtable_dma_sync_single_for_device(cfg,
Robin Murphyffcb6d12015-09-17 17:42:16 +0100352 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100353 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100354}
355
Will Deacone1d3c0f2014-11-14 17:18:23 +0000356static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
357 unsigned long iova, phys_addr_t paddr,
358 arm_lpae_iopte prot, int lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700359 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep,
360 bool flush)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000361{
362 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100363 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000364
Patrick Daly67ba8eb2016-06-27 18:44:42 -0700365 /* We require an unmap first */
Mitchel Humpherys1b0313e2015-09-23 13:56:27 -0700366 if (*ptep & ARM_LPAE_PTE_VALID) {
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -0700367 BUG_ON(!suppress_map_failures);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000368 return -EEXIST;
Will Deaconfe4b9912014-11-17 23:31:12 +0000369 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000370
Robin Murphyf8d54962015-07-29 19:46:04 +0100371 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200372 pte |= ARM_LPAE_PTE_NS;
373
Will Deacone1d3c0f2014-11-14 17:18:23 +0000374 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
375 pte |= ARM_LPAE_PTE_TYPE_PAGE;
376 else
377 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
378
Liam Marka8a228d2016-10-04 13:40:53 -0700379 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_OS;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000380 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
381
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700382 if (flush)
383 __arm_lpae_set_pte(ptep, pte, cfg);
384 else
385 *ptep = pte;
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700386
387 if (prev_ptep)
388 iopte_tblcnt_add(prev_ptep, 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000389 return 0;
390}
391
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700392struct map_state {
393 unsigned long iova_end;
394 unsigned int pgsize;
395 arm_lpae_iopte *pgtable;
396 arm_lpae_iopte *prev_pgtable;
397 arm_lpae_iopte *pte_start;
398 unsigned int num_pte;
399};
400/* map state optimization works at level 3 (the 2nd-to-last level) */
401#define MAP_STATE_LVL 3
402
Will Deacone1d3c0f2014-11-14 17:18:23 +0000403static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
404 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700405 int lvl, arm_lpae_iopte *ptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700406 arm_lpae_iopte *prev_ptep, struct map_state *ms)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000407{
408 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000409 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100410 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700411 void *cookie = data->iop.cookie;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700412 arm_lpae_iopte *pgtable = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000413
414 /* Find our entry at the current level */
415 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
416
417 /* If we can install a leaf entry at this level, then do so */
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700418 if (size == block_size && (size & cfg->pgsize_bitmap)) {
419 if (!ms)
420 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
421 ptep, prev_ptep, true);
422
423 if (lvl == MAP_STATE_LVL) {
424 if (ms->pgtable)
Liam Mark04b0c852016-12-20 11:34:04 -0800425 pgtable_dma_sync_single_for_device(cfg,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700426 __arm_lpae_dma_addr(ms->pte_start),
427 ms->num_pte * sizeof(*ptep),
428 DMA_TO_DEVICE);
429
430 ms->iova_end = round_down(iova, SZ_2M) + SZ_2M;
431 ms->pgtable = pgtable;
432 ms->prev_pgtable = prev_ptep;
433 ms->pgsize = size;
434 ms->pte_start = ptep;
435 ms->num_pte = 1;
436 } else {
437 /*
438 * We have some map state from previous page
439 * mappings, but we're about to set up a block
440 * mapping. Flush out the previous page mappings.
441 */
442 if (ms->pgtable)
Liam Mark04b0c852016-12-20 11:34:04 -0800443 pgtable_dma_sync_single_for_device(cfg,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700444 __arm_lpae_dma_addr(ms->pte_start),
445 ms->num_pte * sizeof(*ptep),
446 DMA_TO_DEVICE);
447 memset(ms, 0, sizeof(*ms));
448 ms = NULL;
449 }
450
451 return arm_lpae_init_pte(data, iova, paddr, prot, lvl,
452 ptep, prev_ptep, ms == NULL);
453 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000454
455 /* We can't allocate tables at the final level */
456 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
457 return -EINVAL;
458
459 /* Grab a pointer to the next level */
460 pte = *ptep;
461 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000462 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Patrick Dalyc11d1082016-09-01 15:52:44 -0700463 GFP_ATOMIC, cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000464 if (!cptep)
465 return -ENOMEM;
466
Will Deacone1d3c0f2014-11-14 17:18:23 +0000467 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100468 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200469 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100470 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000471 } else {
472 cptep = iopte_deref(pte, data);
473 }
474
475 /* Rinse, repeat */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700476 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700477 ptep, ms);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000478}
479
480static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
481 int prot)
482{
483 arm_lpae_iopte pte;
484
485 if (data->iop.fmt == ARM_64_LPAE_S1 ||
486 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600487 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000488
Jeremy Gebbenf96739f2015-09-16 14:04:42 -0600489 if (prot & IOMMU_WRITE)
490 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RW
491 : ARM_LPAE_PTE_AP_RW;
492 else
493 pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RO
494 : ARM_LPAE_PTE_AP_RO;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000495
Liam Marka8a228d2016-10-04 13:40:53 -0700496 if (prot & IOMMU_MMIO)
Robin Murphyfb948252016-04-05 12:39:31 +0100497 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
498 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Liam Marka8a228d2016-10-04 13:40:53 -0700499 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000500 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
501 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Patrick Dalybf762272016-11-03 16:49:44 -0700502 else if (prot & IOMMU_USE_UPSTREAM_HINT)
503 pte |= (ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM
504 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000505 } else {
506 pte = ARM_LPAE_PTE_HAP_FAULT;
507 if (prot & IOMMU_READ)
508 pte |= ARM_LPAE_PTE_HAP_READ;
509 if (prot & IOMMU_WRITE)
510 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100511 if (prot & IOMMU_MMIO)
512 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
513 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000514 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
515 else
516 pte |= ARM_LPAE_PTE_MEMATTR_NC;
517 }
518
519 if (prot & IOMMU_NOEXEC)
520 pte |= ARM_LPAE_PTE_XN;
521
522 return pte;
523}
524
525static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
526 phys_addr_t paddr, size_t size, int iommu_prot)
527{
528 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
529 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100530 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000531 arm_lpae_iopte prot;
532
533 /* If no access, then nothing to do */
534 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
535 return 0;
536
537 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700538 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL,
539 NULL);
Robin Murphy87a91b12015-07-29 19:46:09 +0100540 /*
541 * Synchronise all PTE updates for the new mapping before there's
542 * a chance for anything to kick off a table walk for the new iova.
543 */
544 wmb();
545
546 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000547}
548
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700549static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
550 struct scatterlist *sg, unsigned int nents,
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700551 int iommu_prot, size_t *size)
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700552{
553 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
554 arm_lpae_iopte *ptep = data->pgd;
555 int lvl = ARM_LPAE_START_LVL(data);
556 arm_lpae_iopte prot;
557 struct scatterlist *s;
558 size_t mapped = 0;
559 int i, ret;
560 unsigned int min_pagesz;
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700561 struct io_pgtable_cfg *cfg = &data->iop.cfg;
562 struct map_state ms;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700563
564 /* If no access, then nothing to do */
565 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700566 goto out_err;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700567
568 prot = arm_lpae_prot_to_pte(data, iommu_prot);
569
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700570 min_pagesz = 1 << __ffs(cfg->pgsize_bitmap);
571
572 memset(&ms, 0, sizeof(ms));
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700573
574 for_each_sg(sg, s, nents, i) {
575 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
576 size_t size = s->length;
577
578 /*
579 * We are mapping on IOMMU page boundaries, so offset within
580 * the page must be 0. However, the IOMMU may support pages
581 * smaller than PAGE_SIZE, so s->offset may still represent
582 * an offset of that boundary within the CPU page.
583 */
584 if (!IS_ALIGNED(s->offset, min_pagesz))
585 goto out_err;
586
587 while (size) {
588 size_t pgsize = iommu_pgsize(
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700589 cfg->pgsize_bitmap, iova | phys, size);
590
591 if (ms.pgtable && (iova < ms.iova_end)) {
592 arm_lpae_iopte *ptep = ms.pgtable +
593 ARM_LPAE_LVL_IDX(iova, MAP_STATE_LVL,
594 data);
595 arm_lpae_init_pte(
596 data, iova, phys, prot, MAP_STATE_LVL,
597 ptep, ms.prev_pgtable, false);
598 ms.num_pte++;
599 } else {
600 ret = __arm_lpae_map(data, iova, phys, pgsize,
601 prot, lvl, ptep, NULL, &ms);
602 if (ret)
603 goto out_err;
604 }
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700605
606 iova += pgsize;
607 mapped += pgsize;
608 phys += pgsize;
609 size -= pgsize;
610 }
611 }
612
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700613 if (ms.pgtable)
Liam Mark04b0c852016-12-20 11:34:04 -0800614 pgtable_dma_sync_single_for_device(cfg,
615 __arm_lpae_dma_addr(ms.pte_start),
616 ms.num_pte * sizeof(*ms.pte_start),
617 DMA_TO_DEVICE);
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700618
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700619 return mapped;
620
621out_err:
Rohit Vaswani4d7cdd92015-08-18 17:57:44 -0700622 /* Return the size of the partial mapping so that they can be undone */
623 *size = mapped;
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700624 return 0;
625}
626
Will Deacone1d3c0f2014-11-14 17:18:23 +0000627static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
628 arm_lpae_iopte *ptep)
629{
630 arm_lpae_iopte *start, *end;
631 unsigned long table_size;
Patrick Dalyc11d1082016-09-01 15:52:44 -0700632 void *cookie = data->iop.cookie;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000633
Will Deacone1d3c0f2014-11-14 17:18:23 +0000634 if (lvl == ARM_LPAE_START_LVL(data))
635 table_size = data->pgd_size;
636 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000637 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000638
639 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000640
641 /* Only leaf entries at the last level */
642 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
643 end = ptep;
644 else
645 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000646
647 while (ptep != end) {
648 arm_lpae_iopte pte = *ptep++;
649
650 if (!pte || iopte_leaf(pte, lvl))
651 continue;
652
653 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
654 }
655
Patrick Dalyc11d1082016-09-01 15:52:44 -0700656 __arm_lpae_free_pages(start, table_size, &data->iop.cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000657}
658
659static void arm_lpae_free_pgtable(struct io_pgtable *iop)
660{
661 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
662
663 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
664 kfree(data);
665}
666
667static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
668 unsigned long iova, size_t size,
669 arm_lpae_iopte prot, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700670 arm_lpae_iopte *ptep,
671 arm_lpae_iopte *prev_ptep, size_t blk_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000672{
673 unsigned long blk_start, blk_end;
674 phys_addr_t blk_paddr;
675 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000676
677 blk_start = iova & ~(blk_size - 1);
678 blk_end = blk_start + blk_size;
679 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
Patrick Daly781558f2016-10-13 16:03:27 -0700680 size = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova, size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000681
682 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
683 arm_lpae_iopte *tablep;
684
685 /* Unmap! */
686 if (blk_start == iova)
687 continue;
688
689 /* __arm_lpae_map expects a pointer to the start of the table */
690 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
691 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
Stepan Moskovchenko47b48362015-06-09 20:23:04 -0700692 tablep, prev_ptep, NULL) < 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000693 if (table) {
694 /* Free the table we allocated */
695 tablep = iopte_deref(table, data);
696 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
697 }
698 return 0; /* Bytes unmapped */
699 }
700 }
701
Robin Murphy507e4c92016-01-26 17:13:14 +0000702 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000703 return size;
704}
705
706static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
707 unsigned long iova, size_t size, int lvl,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700708 arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000709{
710 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000711 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000712 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
713
Robin Murphy2eb97c72015-12-04 17:52:58 +0000714 /* Something went horribly wrong and we ran out of page table */
715 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
716 return 0;
717
Will Deacone1d3c0f2014-11-14 17:18:23 +0000718 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
719 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000720 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000721 return 0;
722
723 /* If the size matches this level, we're in the right place */
724 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000725 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000726
727 if (!iopte_leaf(pte, lvl)) {
728 /* Also flush any partial walks */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000729 ptep = iopte_deref(pte, data);
730 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000731 }
732
733 return size;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700734 } else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) {
735 arm_lpae_iopte *table = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700736 arm_lpae_iopte *table_base = table;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700737 int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data);
738 int entry_size = ARM_LPAE_GRANULE(data);
Sudarshan Rajagopalanb1280402017-06-06 10:57:09 -0700739 int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) >>
740 data->pg_shift;
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700741 int entries = min_t(int, size / entry_size,
742 max_entries - tl_offset);
743 int table_len = entries * sizeof(*table);
744
745 /*
746 * This isn't a block mapping so it must be a table mapping
747 * and since it's the 2nd-to-last level the next level has
748 * to be all page mappings. Zero them all out in one fell
749 * swoop.
750 */
751
752 table += tl_offset;
753
754 memset(table, 0, table_len);
Liam Mark04b0c852016-12-20 11:34:04 -0800755 pgtable_dma_sync_single_for_device(&iop->cfg,
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700756 __arm_lpae_dma_addr(table),
757 table_len, DMA_TO_DEVICE);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700758
759 iopte_tblcnt_sub(ptep, entries);
760 if (!iopte_tblcnt(*ptep)) {
761 /* no valid mappings left under this table. free it. */
762 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
763 io_pgtable_tlb_add_flush(iop, iova,
764 entries * entry_size,
765 ARM_LPAE_GRANULE(data),
766 false);
767 __arm_lpae_free_pgtable(data, lvl + 1, table_base);
768 } else {
769 io_pgtable_tlb_add_flush(iop, iova,
770 entries * entry_size,
771 ARM_LPAE_GRANULE(data),
772 true);
773 }
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700774
775 return entries * entry_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000776 } else if (iopte_leaf(pte, lvl)) {
777 /*
778 * Insert a table at the next level to map the old region,
779 * minus the part we want to unmap
780 */
781 return arm_lpae_split_blk_unmap(data, iova, size,
782 iopte_prot(pte), lvl, ptep,
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700783 prev_ptep, blk_size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000784 }
785
786 /* Keep on walkin' */
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700787 prev_ptep = ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000788 ptep = iopte_deref(pte, data);
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700789 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000790}
791
Mitchel Humpherys5e050592015-05-21 14:11:22 -0700792static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000793 size_t size)
794{
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700795 size_t unmapped = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000796 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000797 arm_lpae_iopte *ptep = data->pgd;
798 int lvl = ARM_LPAE_START_LVL(data);
799
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700800 while (unmapped < size) {
801 size_t ret, size_to_unmap, remaining;
802
803 remaining = (size - unmapped);
Patrick Dalyf145f052016-06-27 18:38:09 -0700804 size_to_unmap = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova,
805 remaining);
806 size_to_unmap = size_to_unmap >= SZ_2M ?
807 size_to_unmap :
808 min_t(unsigned long, remaining,
809 (ALIGN(iova + 1, SZ_2M) - iova));
Mitchel Humpherysdeb3e832015-07-14 16:41:29 -0700810 ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep,
811 NULL);
Mitchel Humpherys5f92f322015-04-30 09:49:29 -0700812 if (ret == 0)
813 break;
814 unmapped += ret;
815 iova += ret;
816 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000817 if (unmapped)
Mitchel Humpherysfaa87fc2015-04-24 17:10:59 -0700818 io_pgtable_tlb_flush_all(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000819
820 return unmapped;
821}
822
Liam Mark17f31802016-12-09 14:30:10 -0800823static int arm_lpae_iova_to_pte(struct arm_lpae_io_pgtable *data,
824 unsigned long iova, int *plvl_ret,
825 arm_lpae_iopte *ptep_ret)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000826{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000827 arm_lpae_iopte pte, *ptep = data->pgd;
Liam Mark17f31802016-12-09 14:30:10 -0800828 *plvl_ret = ARM_LPAE_START_LVL(data);
829 *ptep_ret = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000830
831 do {
832 /* Valid IOPTE pointer? */
833 if (!ptep)
Liam Mark17f31802016-12-09 14:30:10 -0800834 return -EINVAL;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000835
836 /* Grab the IOPTE we're interested in */
Liam Mark17f31802016-12-09 14:30:10 -0800837 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, *plvl_ret, data));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000838
839 /* Valid entry? */
840 if (!pte)
Liam Mark17f31802016-12-09 14:30:10 -0800841 return -EINVAL;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000842
843 /* Leaf entry? */
Liam Mark17f31802016-12-09 14:30:10 -0800844 if (iopte_leaf(pte, *plvl_ret))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000845 goto found_translation;
846
847 /* Take it to the next level */
848 ptep = iopte_deref(pte, data);
Liam Mark17f31802016-12-09 14:30:10 -0800849 } while (++(*plvl_ret) < ARM_LPAE_MAX_LEVELS);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000850
851 /* Ran out of page tables to walk */
Liam Mark17f31802016-12-09 14:30:10 -0800852 return -EINVAL;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000853
854found_translation:
Liam Mark17f31802016-12-09 14:30:10 -0800855 *ptep_ret = pte;
856 return 0;
857}
858
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -0700859static uint64_t arm_lpae_iova_get_pte(struct io_pgtable_ops *ops,
860 unsigned long iova)
861{
862 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
863 arm_lpae_iopte pte;
864 int lvl;
865
866 if (!arm_lpae_iova_to_pte(data, iova, &lvl, &pte))
867 return pte;
868
869 return 0;
870}
871
Liam Mark17f31802016-12-09 14:30:10 -0800872static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
873 unsigned long iova)
874{
875 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
876 arm_lpae_iopte pte;
877 int lvl;
878 phys_addr_t phys = 0;
879
880 if (!arm_lpae_iova_to_pte(data, iova, &lvl, &pte)) {
881 iova &= ((1 << ARM_LPAE_LVL_SHIFT(lvl, data)) - 1);
882 phys = ((phys_addr_t)iopte_to_pfn(pte, data)
883 << data->pg_shift) | iova;
884 }
885
886 return phys;
887}
888
889static bool __arm_lpae_is_iova_coherent(struct arm_lpae_io_pgtable *data,
890 arm_lpae_iopte *ptep)
891{
892 if (data->iop.fmt == ARM_64_LPAE_S1 ||
893 data->iop.fmt == ARM_32_LPAE_S1) {
894 int attr_idx = (*ptep & (ARM_LPAE_PTE_ATTRINDX_MASK <<
895 ARM_LPAE_PTE_ATTRINDX_SHIFT)) >>
896 ARM_LPAE_PTE_ATTRINDX_SHIFT;
897 if ((attr_idx == ARM_LPAE_MAIR_ATTR_IDX_CACHE) &&
Liam Mark1402f942017-03-17 10:26:49 -0700898 (((*ptep & ARM_LPAE_PTE_SH_MASK) == ARM_LPAE_PTE_SH_IS)
899 ||
900 (*ptep & ARM_LPAE_PTE_SH_MASK) == ARM_LPAE_PTE_SH_OS))
Liam Mark17f31802016-12-09 14:30:10 -0800901 return true;
902 } else {
903 if (*ptep & ARM_LPAE_PTE_MEMATTR_OIWB)
904 return true;
905 }
906
907 return false;
908}
909
910static bool arm_lpae_is_iova_coherent(struct io_pgtable_ops *ops,
911 unsigned long iova)
912{
913 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
914 arm_lpae_iopte pte;
915 int lvl;
916 bool ret = false;
917
918 if (!arm_lpae_iova_to_pte(data, iova, &lvl, &pte))
919 ret = __arm_lpae_is_iova_coherent(data, &pte);
920
921 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000922}
923
924static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
925{
926 unsigned long granule;
927
928 /*
929 * We need to restrict the supported page sizes to match the
930 * translation regime for a particular granule. Aim to match
931 * the CPU page size if possible, otherwise prefer smaller sizes.
932 * While we're at it, restrict the block sizes to match the
933 * chosen granule.
934 */
935 if (cfg->pgsize_bitmap & PAGE_SIZE)
936 granule = PAGE_SIZE;
937 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
938 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
939 else if (cfg->pgsize_bitmap & PAGE_MASK)
940 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
941 else
942 granule = 0;
943
944 switch (granule) {
945 case SZ_4K:
946 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
947 break;
948 case SZ_16K:
949 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
950 break;
951 case SZ_64K:
952 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
953 break;
954 default:
955 cfg->pgsize_bitmap = 0;
956 }
957}
958
959static struct arm_lpae_io_pgtable *
960arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
961{
962 unsigned long va_bits, pgd_bits;
963 struct arm_lpae_io_pgtable *data;
964
965 arm_lpae_restrict_pgsizes(cfg);
966
967 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
968 return NULL;
969
970 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
971 return NULL;
972
973 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
974 return NULL;
975
Robin Murphyffcb6d12015-09-17 17:42:16 +0100976 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
977 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
978 return NULL;
979 }
980
Will Deacone1d3c0f2014-11-14 17:18:23 +0000981 data = kmalloc(sizeof(*data), GFP_KERNEL);
982 if (!data)
983 return NULL;
984
985 data->pg_shift = __ffs(cfg->pgsize_bitmap);
986 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
987
988 va_bits = cfg->ias - data->pg_shift;
989 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
990
991 /* Calculate the actual size of our pgd (without concatenation) */
992 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
Patrick Dalyd35ec7c2016-11-23 15:04:24 -0800993 data->pgd_bits = pgd_bits;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000994 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
995
996 data->iop.ops = (struct io_pgtable_ops) {
997 .map = arm_lpae_map,
Mitchel Humpherysdaab0412015-04-23 16:19:05 -0700998 .map_sg = arm_lpae_map_sg,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000999 .unmap = arm_lpae_unmap,
1000 .iova_to_phys = arm_lpae_iova_to_phys,
Liam Mark17f31802016-12-09 14:30:10 -08001001 .is_iova_coherent = arm_lpae_is_iova_coherent,
Sudarshan Rajagopalan7a0b4bb2017-04-04 19:10:06 -07001002 .iova_to_pte = arm_lpae_iova_get_pte,
Will Deacone1d3c0f2014-11-14 17:18:23 +00001003 };
1004
1005 return data;
1006}
1007
1008static struct io_pgtable *
1009arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1010{
1011 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +00001012 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001013
Robin Murphy3850db42016-02-12 17:09:46 +00001014 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001015 if (!data)
1016 return NULL;
1017
1018 /* TCR */
Liam Mark04b0c852016-12-20 11:34:04 -08001019 if (cfg->quirks & IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT)
Mitchel Humpherys45b2e972016-06-07 14:18:22 -07001020 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
1021 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
1022 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
Liam Mark5649c822016-12-19 14:35:08 -08001023 else if (cfg->quirks & IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT)
Patrick Dalyce6786f2016-11-09 14:19:23 -08001024 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
1025 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
1026 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
Mitchel Humpherys45b2e972016-06-07 14:18:22 -07001027 else
Liam Marka8a228d2016-10-04 13:40:53 -07001028 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
Mitchel Humpherys45b2e972016-06-07 14:18:22 -07001029 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
1030 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001031
Robin Murphy06c610e2015-12-07 18:18:53 +00001032 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +00001033 case SZ_4K:
1034 reg |= ARM_LPAE_TCR_TG0_4K;
1035 break;
1036 case SZ_16K:
1037 reg |= ARM_LPAE_TCR_TG0_16K;
1038 break;
1039 case SZ_64K:
1040 reg |= ARM_LPAE_TCR_TG0_64K;
1041 break;
1042 }
1043
1044 switch (cfg->oas) {
1045 case 32:
1046 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1047 break;
1048 case 36:
1049 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1050 break;
1051 case 40:
1052 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1053 break;
1054 case 42:
1055 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1056 break;
1057 case 44:
1058 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1059 break;
1060 case 48:
1061 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
1062 break;
1063 default:
1064 goto out_free_data;
1065 }
1066
1067 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +00001068
1069 /* Disable speculative walks through TTBR1 */
1070 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001071 cfg->arm_lpae_s1_cfg.tcr = reg;
1072
1073 /* MAIRs */
1074 reg = (ARM_LPAE_MAIR_ATTR_NC
1075 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1076 (ARM_LPAE_MAIR_ATTR_WBRWA
1077 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1078 (ARM_LPAE_MAIR_ATTR_DEVICE
Patrick Dalybf762272016-11-03 16:49:44 -07001079 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
1080 (ARM_LPAE_MAIR_ATTR_UPSTREAM
1081 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_UPSTREAM));
Will Deacone1d3c0f2014-11-14 17:18:23 +00001082
1083 cfg->arm_lpae_s1_cfg.mair[0] = reg;
1084 cfg->arm_lpae_s1_cfg.mair[1] = 0;
1085
1086 /* Looking good; allocate a pgd */
Patrick Dalyc11d1082016-09-01 15:52:44 -07001087 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
1088 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001089 if (!data->pgd)
1090 goto out_free_data;
1091
Robin Murphy87a91b12015-07-29 19:46:09 +01001092 /* Ensure the empty pgd is visible before any actual TTBR write */
1093 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001094
1095 /* TTBRs */
1096 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
1097 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
1098 return &data->iop;
1099
1100out_free_data:
1101 kfree(data);
1102 return NULL;
1103}
1104
1105static struct io_pgtable *
1106arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1107{
1108 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +00001109 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001110
Robin Murphy3850db42016-02-12 17:09:46 +00001111 /* The NS quirk doesn't apply at stage 2 */
1112 if (cfg->quirks)
1113 return NULL;
1114
1115 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001116 if (!data)
1117 return NULL;
1118
1119 /*
1120 * Concatenate PGDs at level 1 if possible in order to reduce
1121 * the depth of the stage-2 walk.
1122 */
1123 if (data->levels == ARM_LPAE_MAX_LEVELS) {
1124 unsigned long pgd_pages;
1125
1126 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
1127 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
1128 data->pgd_size = pgd_pages << data->pg_shift;
1129 data->levels--;
1130 }
1131 }
1132
1133 /* VTCR */
1134 reg = ARM_64_LPAE_S2_TCR_RES1 |
1135 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
1136 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
1137 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
1138
1139 sl = ARM_LPAE_START_LVL(data);
1140
Robin Murphy06c610e2015-12-07 18:18:53 +00001141 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +00001142 case SZ_4K:
1143 reg |= ARM_LPAE_TCR_TG0_4K;
1144 sl++; /* SL0 format is different for 4K granule size */
1145 break;
1146 case SZ_16K:
1147 reg |= ARM_LPAE_TCR_TG0_16K;
1148 break;
1149 case SZ_64K:
1150 reg |= ARM_LPAE_TCR_TG0_64K;
1151 break;
1152 }
1153
1154 switch (cfg->oas) {
1155 case 32:
1156 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
1157 break;
1158 case 36:
1159 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
1160 break;
1161 case 40:
1162 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
1163 break;
1164 case 42:
1165 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
1166 break;
1167 case 44:
1168 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
1169 break;
1170 case 48:
1171 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
1172 break;
1173 default:
1174 goto out_free_data;
1175 }
1176
1177 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
1178 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
1179 cfg->arm_lpae_s2_cfg.vtcr = reg;
1180
1181 /* Allocate pgd pages */
Patrick Dalyc11d1082016-09-01 15:52:44 -07001182 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL,
1183 cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001184 if (!data->pgd)
1185 goto out_free_data;
1186
Robin Murphy87a91b12015-07-29 19:46:09 +01001187 /* Ensure the empty pgd is visible before any actual TTBR write */
1188 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001189
1190 /* VTTBR */
1191 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1192 return &data->iop;
1193
1194out_free_data:
1195 kfree(data);
1196 return NULL;
1197}
1198
1199static struct io_pgtable *
1200arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1201{
1202 struct io_pgtable *iop;
1203
1204 if (cfg->ias > 32 || cfg->oas > 40)
1205 return NULL;
1206
1207 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1208 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1209 if (iop) {
1210 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
1211 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1212 }
1213
1214 return iop;
1215}
1216
1217static struct io_pgtable *
1218arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1219{
1220 struct io_pgtable *iop;
1221
1222 if (cfg->ias > 40 || cfg->oas > 40)
1223 return NULL;
1224
1225 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1226 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1227 if (iop)
1228 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1229
1230 return iop;
1231}
1232
1233struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1234 .alloc = arm_64_lpae_alloc_pgtable_s1,
1235 .free = arm_lpae_free_pgtable,
1236};
1237
1238struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1239 .alloc = arm_64_lpae_alloc_pgtable_s2,
1240 .free = arm_lpae_free_pgtable,
1241};
1242
1243struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1244 .alloc = arm_32_lpae_alloc_pgtable_s1,
1245 .free = arm_lpae_free_pgtable,
1246};
1247
1248struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1249 .alloc = arm_32_lpae_alloc_pgtable_s2,
1250 .free = arm_lpae_free_pgtable,
1251};
Will Deaconfe4b9912014-11-17 23:31:12 +00001252
1253#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1254
1255static struct io_pgtable_cfg *cfg_cookie;
1256
1257static void dummy_tlb_flush_all(void *cookie)
1258{
1259 WARN_ON(cookie != cfg_cookie);
1260}
1261
Robin Murphy06c610e2015-12-07 18:18:53 +00001262static void dummy_tlb_add_flush(unsigned long iova, size_t size,
1263 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001264{
1265 WARN_ON(cookie != cfg_cookie);
Will Deaconfe4b9912014-11-17 23:31:12 +00001266}
1267
1268static void dummy_tlb_sync(void *cookie)
1269{
1270 WARN_ON(cookie != cfg_cookie);
1271}
1272
Will Deaconfe4b9912014-11-17 23:31:12 +00001273static struct iommu_gather_ops dummy_tlb_ops __initdata = {
1274 .tlb_flush_all = dummy_tlb_flush_all,
1275 .tlb_add_flush = dummy_tlb_add_flush,
1276 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +00001277};
1278
1279static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1280{
1281 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1282 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1283
1284 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1285 cfg->pgsize_bitmap, cfg->ias);
1286 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1287 data->levels, data->pgd_size, data->pg_shift,
1288 data->bits_per_level, data->pgd);
1289}
1290
1291#define __FAIL(ops, i) ({ \
1292 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1293 arm_lpae_dump_ops(ops); \
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001294 suppress_map_failures = false; \
Will Deaconfe4b9912014-11-17 23:31:12 +00001295 selftest_running = false; \
1296 -EFAULT; \
1297})
1298
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001299/*
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001300 * Returns true if there's any mapping in the given iova range in ops.
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001301 */
1302static bool arm_lpae_range_has_mapping(struct io_pgtable_ops *ops,
1303 unsigned long iova_start, size_t size)
1304{
1305 unsigned long iova = iova_start;
1306
1307 while (iova < (iova_start + size)) {
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001308 if (ops->iova_to_phys(ops, iova + 42))
1309 return true;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001310 iova += SZ_4K;
1311 }
Mitchel Humpherys601ebd32015-06-01 16:12:26 -07001312 return false;
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001313}
1314
1315/*
1316 * Returns true if the iova range is successfully mapped to the contiguous
1317 * phys range in ops.
1318 */
1319static bool arm_lpae_range_has_specific_mapping(struct io_pgtable_ops *ops,
1320 const unsigned long iova_start,
1321 const phys_addr_t phys_start,
1322 const size_t size)
1323{
1324 unsigned long iova = iova_start;
1325 phys_addr_t phys = phys_start;
1326
1327 while (iova < (iova_start + size)) {
1328 if (ops->iova_to_phys(ops, iova + 42) != (phys + 42))
1329 return false;
1330 iova += SZ_4K;
1331 phys += SZ_4K;
1332 }
1333 return true;
1334}
1335
Will Deaconfe4b9912014-11-17 23:31:12 +00001336static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1337{
1338 static const enum io_pgtable_fmt fmts[] = {
1339 ARM_64_LPAE_S1,
1340 ARM_64_LPAE_S2,
1341 };
1342
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001343 int i, j, k;
Will Deaconfe4b9912014-11-17 23:31:12 +00001344 unsigned long iova;
1345 size_t size;
1346 struct io_pgtable_ops *ops;
Will Deaconfe4b9912014-11-17 23:31:12 +00001347 selftest_running = true;
1348
1349 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001350 unsigned long test_sg_sizes[] = { SZ_4K, SZ_64K, SZ_2M,
1351 SZ_1M * 12, SZ_1M * 20 };
1352
Will Deaconfe4b9912014-11-17 23:31:12 +00001353 cfg_cookie = cfg;
1354 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1355 if (!ops) {
1356 pr_err("selftest: failed to allocate io pgtable ops\n");
1357 return -ENOMEM;
1358 }
1359
1360 /*
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001361 * Initial sanity checks. Empty page tables shouldn't
1362 * provide any translations. TODO: check entire supported
1363 * range for these ops rather than first 2G
Will Deaconfe4b9912014-11-17 23:31:12 +00001364 */
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001365 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
Will Deaconfe4b9912014-11-17 23:31:12 +00001366 return __FAIL(ops, i);
1367
1368 /*
1369 * Distinct mappings of different granule sizes.
1370 */
1371 iova = 0;
1372 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1373 while (j != BITS_PER_LONG) {
1374 size = 1UL << j;
1375
1376 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1377 IOMMU_WRITE |
1378 IOMMU_NOEXEC |
1379 IOMMU_CACHE))
1380 return __FAIL(ops, i);
1381
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001382 suppress_map_failures = true;
Will Deaconfe4b9912014-11-17 23:31:12 +00001383 /* Overlapping mappings */
1384 if (!ops->map(ops, iova, iova + size, size,
1385 IOMMU_READ | IOMMU_NOEXEC))
1386 return __FAIL(ops, i);
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001387 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001388
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001389 if (!arm_lpae_range_has_specific_mapping(ops, iova,
1390 iova, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001391 return __FAIL(ops, i);
1392
1393 iova += SZ_1G;
1394 j++;
1395 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1396 }
1397
1398 /* Partial unmap */
1399 size = 1UL << __ffs(cfg->pgsize_bitmap);
1400 if (ops->unmap(ops, SZ_1G + size, size) != size)
1401 return __FAIL(ops, i);
1402
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001403 if (arm_lpae_range_has_mapping(ops, SZ_1G + size, size))
1404 return __FAIL(ops, i);
1405
Will Deaconfe4b9912014-11-17 23:31:12 +00001406 /* Remap of partial unmap */
1407 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1408 return __FAIL(ops, i);
1409
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001410 if (!arm_lpae_range_has_specific_mapping(ops, SZ_1G + size,
1411 size, size))
Will Deaconfe4b9912014-11-17 23:31:12 +00001412 return __FAIL(ops, i);
1413
1414 /* Full unmap */
1415 iova = 0;
1416 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1417 while (j != BITS_PER_LONG) {
1418 size = 1UL << j;
1419
1420 if (ops->unmap(ops, iova, size) != size)
1421 return __FAIL(ops, i);
1422
1423 if (ops->iova_to_phys(ops, iova + 42))
1424 return __FAIL(ops, i);
1425
1426 /* Remap full block */
1427 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1428 return __FAIL(ops, i);
1429
1430 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1431 return __FAIL(ops, i);
1432
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001433 if (ops->unmap(ops, iova, size) != size)
1434 return __FAIL(ops, i);
1435
Will Deaconfe4b9912014-11-17 23:31:12 +00001436 iova += SZ_1G;
1437 j++;
1438 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1439 }
1440
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001441 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1442 return __FAIL(ops, i);
1443
Mitchel Humpheryse4012a62015-06-01 15:44:49 -07001444 if ((cfg->pgsize_bitmap & SZ_2M) &&
1445 (cfg->pgsize_bitmap & SZ_4K)) {
1446 /* mixed block + page mappings */
1447 iova = 0;
1448 if (ops->map(ops, iova, iova, SZ_2M, IOMMU_READ))
1449 return __FAIL(ops, i);
1450
1451 if (ops->map(ops, iova + SZ_2M, iova + SZ_2M, SZ_4K,
1452 IOMMU_READ))
1453 return __FAIL(ops, i);
1454
1455 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1456 return __FAIL(ops, i);
1457
1458 if (ops->iova_to_phys(ops, iova + SZ_2M + 42) !=
1459 (iova + SZ_2M + 42))
1460 return __FAIL(ops, i);
1461
1462 /* unmap both mappings at once */
1463 if (ops->unmap(ops, iova, SZ_2M + SZ_4K) !=
1464 (SZ_2M + SZ_4K))
1465 return __FAIL(ops, i);
1466
1467 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1468 return __FAIL(ops, i);
1469 }
1470
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001471 /* map_sg */
1472 for (j = 0; j < ARRAY_SIZE(test_sg_sizes); ++j) {
1473 size_t mapped;
1474 size_t unused;
1475 struct page *page;
1476 phys_addr_t page_phys;
1477 struct sg_table table;
1478 struct scatterlist *sg;
1479 unsigned long total_size = test_sg_sizes[j];
1480 int chunk_size = 1UL << find_first_bit(
1481 &cfg->pgsize_bitmap, BITS_PER_LONG);
1482 int nents = total_size / chunk_size;
1483
1484 if (total_size < chunk_size)
1485 continue;
1486
1487 page = alloc_pages(GFP_KERNEL, get_order(chunk_size));
1488 page_phys = page_to_phys(page);
1489
1490 iova = 0;
1491 BUG_ON(sg_alloc_table(&table, nents, GFP_KERNEL));
1492 BUG_ON(!page);
1493 for_each_sg(table.sgl, sg, table.nents, k)
1494 sg_set_page(sg, page, chunk_size, 0);
1495
1496 mapped = ops->map_sg(ops, iova, table.sgl, table.nents,
1497 IOMMU_READ | IOMMU_WRITE, &unused);
1498
1499 if (mapped != total_size)
1500 return __FAIL(ops, i);
1501
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001502 if (!arm_lpae_range_has_mapping(ops, iova, total_size))
1503 return __FAIL(ops, i);
1504
1505 if (arm_lpae_range_has_mapping(ops, iova + total_size,
1506 SZ_2G - (iova + total_size)))
1507 return __FAIL(ops, i);
1508
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001509 for_each_sg(table.sgl, sg, table.nents, k) {
1510 dma_addr_t newphys =
1511 ops->iova_to_phys(ops, iova + 42);
1512 if (newphys != (page_phys + 42))
1513 return __FAIL(ops, i);
1514 iova += chunk_size;
1515 }
1516
1517 if (ops->unmap(ops, 0, total_size) != total_size)
1518 return __FAIL(ops, i);
1519
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001520 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1521 return __FAIL(ops, i);
1522
Mitchel Humpherysdf18a9a2015-04-23 13:41:31 -07001523 sg_free_table(&table);
1524 __free_pages(page, get_order(chunk_size));
1525 }
1526
Mitchel Humpherysfdf212a2015-05-11 13:40:38 -07001527 if (arm_lpae_range_has_mapping(ops, 0, SZ_2G))
1528 return __FAIL(ops, i);
1529
Will Deaconfe4b9912014-11-17 23:31:12 +00001530 free_io_pgtable_ops(ops);
1531 }
1532
1533 selftest_running = false;
Mitchel Humpherys9739d9b2015-06-01 16:10:20 -07001534 suppress_map_failures = false;
Will Deaconfe4b9912014-11-17 23:31:12 +00001535 return 0;
1536}
1537
1538static int __init arm_lpae_do_selftests(void)
1539{
1540 static const unsigned long pgsize[] = {
1541 SZ_4K | SZ_2M | SZ_1G,
Will Deaconfe4b9912014-11-17 23:31:12 +00001542 };
1543
1544 static const unsigned int ias[] = {
1545 32, 36, 40, 42, 44, 48,
1546 };
1547
1548 int i, j, pass = 0, fail = 0;
1549 struct io_pgtable_cfg cfg = {
1550 .tlb = &dummy_tlb_ops,
1551 .oas = 48,
1552 };
1553
1554 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1555 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1556 cfg.pgsize_bitmap = pgsize[i];
1557 cfg.ias = ias[j];
1558 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1559 pgsize[i], ias[j]);
1560 if (arm_lpae_run_tests(&cfg))
1561 fail++;
1562 else
1563 pass++;
1564 }
1565 }
1566
1567 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1568 return fail ? -EFAULT : 0;
1569}
1570subsys_initcall(arm_lpae_do_selftests);
1571#endif