blob: b3f975c95f763b9f6bfab4b613de49c34dff096d [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Robin Murphye5fc9752016-01-26 17:13:13 +00002/*
3 * CPU-agnostic ARM page table allocator.
4 *
5 * ARMv7 Short-descriptor format, supporting
6 * - Basic memory attributes
7 * - Simplified access permissions (AP[2:1] model)
8 * - Backwards-compatible TEX remap
9 * - Large pages/supersections (if indicated by the caller)
10 *
11 * Not supporting:
12 * - Legacy access permissions (AP[2:0] model)
13 *
14 * Almost certainly never supporting:
15 * - PXN
16 * - Domains
17 *
Robin Murphye5fc9752016-01-26 17:13:13 +000018 * Copyright (C) 2014-2015 ARM Limited
19 * Copyright (c) 2014-2015 MediaTek Inc.
20 */
21
22#define pr_fmt(fmt) "arm-v7s io-pgtable: " fmt
23
Robin Murphy119ff302017-06-22 16:53:55 +010024#include <linux/atomic.h>
Robin Murphye5fc9752016-01-26 17:13:13 +000025#include <linux/dma-mapping.h>
26#include <linux/gfp.h>
Rob Herringb77cf112019-02-05 10:37:31 -060027#include <linux/io-pgtable.h>
Robin Murphye5fc9752016-01-26 17:13:13 +000028#include <linux/iommu.h>
29#include <linux/kernel.h>
30#include <linux/kmemleak.h>
31#include <linux/sizes.h>
32#include <linux/slab.h>
Robin Murphy119ff302017-06-22 16:53:55 +010033#include <linux/spinlock.h>
Robin Murphye5fc9752016-01-26 17:13:13 +000034#include <linux/types.h>
35
36#include <asm/barrier.h>
37
Robin Murphye5fc9752016-01-26 17:13:13 +000038/* Struct accessors */
39#define io_pgtable_to_data(x) \
40 container_of((x), struct arm_v7s_io_pgtable, iop)
41
42#define io_pgtable_ops_to_data(x) \
43 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
44
45/*
46 * We have 32 bits total; 12 bits resolved at level 1, 8 bits at level 2,
47 * and 12 bits in a page. With some carefully-chosen coefficients we can
48 * hide the ugly inconsistencies behind these macros and at least let the
49 * rest of the code pretend to be somewhat sane.
50 */
51#define ARM_V7S_ADDR_BITS 32
52#define _ARM_V7S_LVL_BITS(lvl) (16 - (lvl) * 4)
53#define ARM_V7S_LVL_SHIFT(lvl) (ARM_V7S_ADDR_BITS - (4 + 8 * (lvl)))
54#define ARM_V7S_TABLE_SHIFT 10
55
56#define ARM_V7S_PTES_PER_LVL(lvl) (1 << _ARM_V7S_LVL_BITS(lvl))
57#define ARM_V7S_TABLE_SIZE(lvl) \
58 (ARM_V7S_PTES_PER_LVL(lvl) * sizeof(arm_v7s_iopte))
59
60#define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
61#define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
62#define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
63#define _ARM_V7S_IDX_MASK(lvl) (ARM_V7S_PTES_PER_LVL(lvl) - 1)
64#define ARM_V7S_LVL_IDX(addr, lvl) ({ \
65 int _l = lvl; \
66 ((u32)(addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l); \
67})
68
69/*
70 * Large page/supersection entries are effectively a block of 16 page/section
71 * entries, along the lines of the LPAE contiguous hint, but all with the
72 * same output address. For want of a better common name we'll call them
73 * "contiguous" versions of their respective page/section entries here, but
74 * noting the distinction (WRT to TLB maintenance) that they represent *one*
75 * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
76 */
77#define ARM_V7S_CONT_PAGES 16
78
79/* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
80#define ARM_V7S_PTE_TYPE_TABLE 0x1
81#define ARM_V7S_PTE_TYPE_PAGE 0x2
82#define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
83
84#define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
Robin Murphy9db829d2017-06-22 16:53:50 +010085#define ARM_V7S_PTE_IS_TABLE(pte, lvl) \
86 ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE))
Robin Murphye5fc9752016-01-26 17:13:13 +000087
88/* Page table bits */
89#define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
90#define ARM_V7S_ATTR_B BIT(2)
91#define ARM_V7S_ATTR_C BIT(3)
92#define ARM_V7S_ATTR_NS_TABLE BIT(3)
93#define ARM_V7S_ATTR_NS_SECTION BIT(19)
94
95#define ARM_V7S_CONT_SECTION BIT(18)
96#define ARM_V7S_CONT_PAGE_XN_SHIFT 15
97
98/*
99 * The attribute bits are consistently ordered*, but occupy bits [17:10] of
100 * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
101 * fields relative to that 8-bit block, plus a total shift relative to the PTE.
102 */
103#define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
104
105#define ARM_V7S_ATTR_MASK 0xff
106#define ARM_V7S_ATTR_AP0 BIT(0)
107#define ARM_V7S_ATTR_AP1 BIT(1)
108#define ARM_V7S_ATTR_AP2 BIT(5)
109#define ARM_V7S_ATTR_S BIT(6)
110#define ARM_V7S_ATTR_NG BIT(7)
111#define ARM_V7S_TEX_SHIFT 2
112#define ARM_V7S_TEX_MASK 0x7
113#define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
114
Yong Wu1afe2312016-03-14 06:01:10 +0800115#define ARM_V7S_ATTR_MTK_4GB BIT(9) /* MTK extend it for 4GB mode */
116
Robin Murphye5fc9752016-01-26 17:13:13 +0000117/* *well, except for TEX on level 2 large pages, of course :( */
118#define ARM_V7S_CONT_PAGE_TEX_SHIFT 6
119#define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT)
120
121/* Simplified access permissions */
122#define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0
123#define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1
124#define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2
125
126/* Register bits */
127#define ARM_V7S_RGN_NC 0
128#define ARM_V7S_RGN_WBWA 1
129#define ARM_V7S_RGN_WT 2
130#define ARM_V7S_RGN_WB 3
131
132#define ARM_V7S_PRRR_TYPE_DEVICE 1
133#define ARM_V7S_PRRR_TYPE_NORMAL 2
134#define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2))
135#define ARM_V7S_PRRR_DS0 BIT(16)
136#define ARM_V7S_PRRR_DS1 BIT(17)
137#define ARM_V7S_PRRR_NS0 BIT(18)
138#define ARM_V7S_PRRR_NS1 BIT(19)
139#define ARM_V7S_PRRR_NOS(n) BIT((n) + 24)
140
141#define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2))
142#define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16))
143
144#define ARM_V7S_TTBR_S BIT(1)
145#define ARM_V7S_TTBR_NOS BIT(5)
146#define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3)
147#define ARM_V7S_TTBR_IRGN_ATTR(attr) \
148 ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1))
149
150#define ARM_V7S_TCR_PD1 BIT(5)
151
Nicolas Boichat0a352552019-03-28 20:43:46 -0700152#ifdef CONFIG_ZONE_DMA32
153#define ARM_V7S_TABLE_GFP_DMA GFP_DMA32
154#define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA32
155#else
156#define ARM_V7S_TABLE_GFP_DMA GFP_DMA
157#define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA
158#endif
159
Robin Murphye5fc9752016-01-26 17:13:13 +0000160typedef u32 arm_v7s_iopte;
161
162static bool selftest_running;
163
164struct arm_v7s_io_pgtable {
165 struct io_pgtable iop;
166
167 arm_v7s_iopte *pgd;
168 struct kmem_cache *l2_tables;
Robin Murphy119ff302017-06-22 16:53:55 +0100169 spinlock_t split_lock;
Robin Murphye5fc9752016-01-26 17:13:13 +0000170};
171
172static dma_addr_t __arm_v7s_dma_addr(void *pages)
173{
174 return (dma_addr_t)virt_to_phys(pages);
175}
176
177static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl)
178{
179 if (ARM_V7S_PTE_IS_TABLE(pte, lvl))
180 pte &= ARM_V7S_TABLE_MASK;
181 else
182 pte &= ARM_V7S_LVL_MASK(lvl);
183 return phys_to_virt(pte);
184}
185
186static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp,
187 struct arm_v7s_io_pgtable *data)
188{
Robin Murphy81b3c252017-06-22 16:53:53 +0100189 struct io_pgtable_cfg *cfg = &data->iop.cfg;
190 struct device *dev = cfg->iommu_dev;
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100191 phys_addr_t phys;
Robin Murphye5fc9752016-01-26 17:13:13 +0000192 dma_addr_t dma;
193 size_t size = ARM_V7S_TABLE_SIZE(lvl);
194 void *table = NULL;
195
196 if (lvl == 1)
Nicolas Boichat0a352552019-03-28 20:43:46 -0700197 table = (void *)__get_free_pages(
198 __GFP_ZERO | ARM_V7S_TABLE_GFP_DMA, get_order(size));
Robin Murphye5fc9752016-01-26 17:13:13 +0000199 else if (lvl == 2)
Nicolas Boichat0a352552019-03-28 20:43:46 -0700200 table = kmem_cache_zalloc(data->l2_tables, gfp);
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100201 phys = virt_to_phys(table);
Nicolas Boichat0a352552019-03-28 20:43:46 -0700202 if (phys != (arm_v7s_iopte)phys) {
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100203 /* Doesn't fit in PTE */
Nicolas Boichat0a352552019-03-28 20:43:46 -0700204 dev_err(dev, "Page table does not fit in PTE: %pa", &phys);
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100205 goto out_free;
Nicolas Boichat0a352552019-03-28 20:43:46 -0700206 }
Will Deacon4f418452019-06-25 12:51:25 +0100207 if (table && !cfg->coherent_walk) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000208 dma = dma_map_single(dev, table, size, DMA_TO_DEVICE);
209 if (dma_mapping_error(dev, dma))
210 goto out_free;
211 /*
212 * We depend on the IOMMU being able to work with any physical
213 * address directly, so if the DMA layer suggests otherwise by
214 * translating or truncating them, that bodes very badly...
215 */
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100216 if (dma != phys)
Robin Murphye5fc9752016-01-26 17:13:13 +0000217 goto out_unmap;
218 }
Nicolas Boichat032ebd82019-01-28 17:43:01 +0800219 if (lvl == 2)
220 kmemleak_ignore(table);
Robin Murphye5fc9752016-01-26 17:13:13 +0000221 return table;
222
223out_unmap:
224 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
225 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
226out_free:
227 if (lvl == 1)
228 free_pages((unsigned long)table, get_order(size));
229 else
230 kmem_cache_free(data->l2_tables, table);
231 return NULL;
232}
233
234static void __arm_v7s_free_table(void *table, int lvl,
235 struct arm_v7s_io_pgtable *data)
236{
Robin Murphy81b3c252017-06-22 16:53:53 +0100237 struct io_pgtable_cfg *cfg = &data->iop.cfg;
238 struct device *dev = cfg->iommu_dev;
Robin Murphye5fc9752016-01-26 17:13:13 +0000239 size_t size = ARM_V7S_TABLE_SIZE(lvl);
240
Will Deacon4f418452019-06-25 12:51:25 +0100241 if (!cfg->coherent_walk)
Robin Murphye5fc9752016-01-26 17:13:13 +0000242 dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
243 DMA_TO_DEVICE);
244 if (lvl == 1)
245 free_pages((unsigned long)table, get_order(size));
246 else
247 kmem_cache_free(data->l2_tables, table);
248}
249
250static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
251 struct io_pgtable_cfg *cfg)
252{
Will Deacon4f418452019-06-25 12:51:25 +0100253 if (cfg->coherent_walk)
Robin Murphye5fc9752016-01-26 17:13:13 +0000254 return;
255
256 dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
257 num_entries * sizeof(*ptep), DMA_TO_DEVICE);
258}
259static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
260 int num_entries, struct io_pgtable_cfg *cfg)
261{
262 int i;
263
264 for (i = 0; i < num_entries; i++)
265 ptep[i] = pte;
266
267 __arm_v7s_pte_sync(ptep, num_entries, cfg);
268}
269
270static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
271 struct io_pgtable_cfg *cfg)
272{
273 bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
Robin Murphye88ccab2016-04-05 12:39:32 +0100274 arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
Robin Murphye5fc9752016-01-26 17:13:13 +0000275
Robin Murphye88ccab2016-04-05 12:39:32 +0100276 if (!(prot & IOMMU_MMIO))
277 pte |= ARM_V7S_ATTR_TEX(1);
Robin Murphye5fc9752016-01-26 17:13:13 +0000278 if (ap) {
Robin Murphy5baf1e92017-01-06 18:58:10 +0530279 pte |= ARM_V7S_PTE_AF;
280 if (!(prot & IOMMU_PRIV))
281 pte |= ARM_V7S_PTE_AP_UNPRIV;
Robin Murphye5fc9752016-01-26 17:13:13 +0000282 if (!(prot & IOMMU_WRITE))
283 pte |= ARM_V7S_PTE_AP_RDONLY;
284 }
285 pte <<= ARM_V7S_ATTR_SHIFT(lvl);
286
287 if ((prot & IOMMU_NOEXEC) && ap)
288 pte |= ARM_V7S_ATTR_XN(lvl);
Robin Murphye88ccab2016-04-05 12:39:32 +0100289 if (prot & IOMMU_MMIO)
290 pte |= ARM_V7S_ATTR_B;
291 else if (prot & IOMMU_CACHE)
Robin Murphye5fc9752016-01-26 17:13:13 +0000292 pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
293
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100294 pte |= ARM_V7S_PTE_TYPE_PAGE;
295 if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
296 pte |= ARM_V7S_ATTR_NS_SECTION;
297
298 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB)
299 pte |= ARM_V7S_ATTR_MTK_4GB;
300
Robin Murphye5fc9752016-01-26 17:13:13 +0000301 return pte;
302}
303
304static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
305{
306 int prot = IOMMU_READ;
Robin Murphye88ccab2016-04-05 12:39:32 +0100307 arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
Robin Murphye5fc9752016-01-26 17:13:13 +0000308
Robin Murphye633fc72016-08-11 17:44:05 +0100309 if (!(attr & ARM_V7S_PTE_AP_RDONLY))
Robin Murphye5fc9752016-01-26 17:13:13 +0000310 prot |= IOMMU_WRITE;
Robin Murphy5baf1e92017-01-06 18:58:10 +0530311 if (!(attr & ARM_V7S_PTE_AP_UNPRIV))
312 prot |= IOMMU_PRIV;
Robin Murphye88ccab2016-04-05 12:39:32 +0100313 if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
314 prot |= IOMMU_MMIO;
315 else if (pte & ARM_V7S_ATTR_C)
Robin Murphye5fc9752016-01-26 17:13:13 +0000316 prot |= IOMMU_CACHE;
Robin Murphye633fc72016-08-11 17:44:05 +0100317 if (pte & ARM_V7S_ATTR_XN(lvl))
318 prot |= IOMMU_NOEXEC;
Robin Murphye5fc9752016-01-26 17:13:13 +0000319
320 return prot;
321}
322
323static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
324{
325 if (lvl == 1) {
326 pte |= ARM_V7S_CONT_SECTION;
327 } else if (lvl == 2) {
328 arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
329 arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
330
331 pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
332 pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
333 (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
334 ARM_V7S_PTE_TYPE_CONT_PAGE;
335 }
336 return pte;
337}
338
339static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
340{
341 if (lvl == 1) {
342 pte &= ~ARM_V7S_CONT_SECTION;
343 } else if (lvl == 2) {
344 arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
345 arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
346 ARM_V7S_CONT_PAGE_TEX_SHIFT);
347
348 pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
349 pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
350 (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
351 ARM_V7S_PTE_TYPE_PAGE;
352 }
353 return pte;
354}
355
356static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
357{
358 if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
359 return pte & ARM_V7S_CONT_SECTION;
360 else if (lvl == 2)
361 return !(pte & ARM_V7S_PTE_TYPE_PAGE);
362 return false;
363}
364
Vivek Gautam193e67c2018-02-05 23:29:19 +0530365static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *, unsigned long,
366 size_t, int, arm_v7s_iopte *);
Robin Murphye5fc9752016-01-26 17:13:13 +0000367
368static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
369 unsigned long iova, phys_addr_t paddr, int prot,
370 int lvl, int num_entries, arm_v7s_iopte *ptep)
371{
372 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100373 arm_v7s_iopte pte;
Robin Murphye5fc9752016-01-26 17:13:13 +0000374 int i;
375
376 for (i = 0; i < num_entries; i++)
377 if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
378 /*
379 * We need to unmap and free the old table before
380 * overwriting it with a block entry.
381 */
382 arm_v7s_iopte *tblp;
383 size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
384
385 tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl);
386 if (WARN_ON(__arm_v7s_unmap(data, iova + i * sz,
387 sz, lvl, tblp) != sz))
388 return -EINVAL;
389 } else if (ptep[i]) {
390 /* We require an unmap first */
391 WARN_ON(!selftest_running);
392 return -EEXIST;
393 }
394
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100395 pte = arm_v7s_prot_to_pte(prot, lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000396 if (num_entries > 1)
397 pte = arm_v7s_pte_to_cont(pte, lvl);
398
399 pte |= paddr & ARM_V7S_LVL_MASK(lvl);
400
401 __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
402 return 0;
403}
404
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100405static arm_v7s_iopte arm_v7s_install_table(arm_v7s_iopte *table,
406 arm_v7s_iopte *ptep,
Robin Murphy119ff302017-06-22 16:53:55 +0100407 arm_v7s_iopte curr,
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100408 struct io_pgtable_cfg *cfg)
409{
Robin Murphy119ff302017-06-22 16:53:55 +0100410 arm_v7s_iopte old, new;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100411
412 new = virt_to_phys(table) | ARM_V7S_PTE_TYPE_TABLE;
413 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
414 new |= ARM_V7S_ATTR_NS_TABLE;
415
Will Deacon77f34452017-06-23 12:02:38 +0100416 /*
417 * Ensure the table itself is visible before its PTE can be.
418 * Whilst we could get away with cmpxchg64_release below, this
419 * doesn't have any ordering semantics when !CONFIG_SMP.
420 */
421 dma_wmb();
Robin Murphy119ff302017-06-22 16:53:55 +0100422
423 old = cmpxchg_relaxed(ptep, curr, new);
424 __arm_v7s_pte_sync(ptep, 1, cfg);
425
426 return old;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100427}
428
Robin Murphye5fc9752016-01-26 17:13:13 +0000429static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
430 phys_addr_t paddr, size_t size, int prot,
431 int lvl, arm_v7s_iopte *ptep)
432{
433 struct io_pgtable_cfg *cfg = &data->iop.cfg;
434 arm_v7s_iopte pte, *cptep;
435 int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
436
437 /* Find our entry at the current level */
438 ptep += ARM_V7S_LVL_IDX(iova, lvl);
439
440 /* If we can install a leaf entry at this level, then do so */
441 if (num_entries)
442 return arm_v7s_init_pte(data, iova, paddr, prot,
443 lvl, num_entries, ptep);
444
445 /* We can't allocate tables at the final level */
446 if (WARN_ON(lvl == 2))
447 return -EINVAL;
448
449 /* Grab a pointer to the next level */
Robin Murphy119ff302017-06-22 16:53:55 +0100450 pte = READ_ONCE(*ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000451 if (!pte) {
452 cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data);
453 if (!cptep)
454 return -ENOMEM;
455
Robin Murphy119ff302017-06-22 16:53:55 +0100456 pte = arm_v7s_install_table(cptep, ptep, 0, cfg);
457 if (pte)
458 __arm_v7s_free_table(cptep, lvl + 1, data);
Oleksandr Tyshchenkoa03849e2017-02-27 14:30:26 +0200459 } else {
Robin Murphy119ff302017-06-22 16:53:55 +0100460 /* We've no easy way of knowing if it's synced yet, so... */
461 __arm_v7s_pte_sync(ptep, 1, cfg);
462 }
463
464 if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
465 cptep = iopte_deref(pte, lvl);
466 } else if (pte) {
Oleksandr Tyshchenkoa03849e2017-02-27 14:30:26 +0200467 /* We require an unmap first */
468 WARN_ON(!selftest_running);
469 return -EEXIST;
Robin Murphye5fc9752016-01-26 17:13:13 +0000470 }
471
472 /* Rinse, repeat */
473 return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep);
474}
475
476static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova,
477 phys_addr_t paddr, size_t size, int prot)
478{
479 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphy507e4c92016-01-26 17:13:14 +0000480 struct io_pgtable *iop = &data->iop;
Robin Murphye5fc9752016-01-26 17:13:13 +0000481 int ret;
482
483 /* If no access, then nothing to do */
484 if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
485 return 0;
486
Robin Murphy76557392017-07-03 14:52:24 +0100487 if (WARN_ON(upper_32_bits(iova) || upper_32_bits(paddr)))
488 return -ERANGE;
489
Robin Murphye5fc9752016-01-26 17:13:13 +0000490 ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd);
491 /*
492 * Synchronise all PTE updates for the new mapping before there's
493 * a chance for anything to kick off a table walk for the new iova.
494 */
Robin Murphy507e4c92016-01-26 17:13:14 +0000495 if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) {
Will Deacon10b7a7d2019-07-02 16:44:32 +0100496 io_pgtable_tlb_flush_walk(iop, iova, size,
497 ARM_V7S_BLOCK_SIZE(2));
Robin Murphye5fc9752016-01-26 17:13:13 +0000498 } else {
499 wmb();
500 }
501
502 return ret;
503}
504
505static void arm_v7s_free_pgtable(struct io_pgtable *iop)
506{
507 struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
508 int i;
509
510 for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) {
511 arm_v7s_iopte pte = data->pgd[i];
512
513 if (ARM_V7S_PTE_IS_TABLE(pte, 1))
514 __arm_v7s_free_table(iopte_deref(pte, 1), 2, data);
515 }
516 __arm_v7s_free_table(data->pgd, 1, data);
517 kmem_cache_destroy(data->l2_tables);
518 kfree(data);
519}
520
Robin Murphy119ff302017-06-22 16:53:55 +0100521static arm_v7s_iopte arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
522 unsigned long iova, int idx, int lvl,
523 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000524{
Robin Murphy507e4c92016-01-26 17:13:14 +0000525 struct io_pgtable *iop = &data->iop;
Robin Murphye5fc9752016-01-26 17:13:13 +0000526 arm_v7s_iopte pte;
527 size_t size = ARM_V7S_BLOCK_SIZE(lvl);
528 int i;
529
Robin Murphy119ff302017-06-22 16:53:55 +0100530 /* Check that we didn't lose a race to get the lock */
531 pte = *ptep;
532 if (!arm_v7s_pte_is_cont(pte, lvl))
533 return pte;
534
Robin Murphye5fc9752016-01-26 17:13:13 +0000535 ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
Robin Murphy119ff302017-06-22 16:53:55 +0100536 pte = arm_v7s_cont_to_pte(pte, lvl);
537 for (i = 0; i < ARM_V7S_CONT_PAGES; i++)
538 ptep[i] = pte + i * size;
Robin Murphye5fc9752016-01-26 17:13:13 +0000539
Robin Murphy507e4c92016-01-26 17:13:14 +0000540 __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000541
542 size *= ARM_V7S_CONT_PAGES;
Will Deacon10b7a7d2019-07-02 16:44:32 +0100543 io_pgtable_tlb_flush_leaf(iop, iova, size, size);
Robin Murphy119ff302017-06-22 16:53:55 +0100544 return pte;
Robin Murphye5fc9752016-01-26 17:13:13 +0000545}
546
Vivek Gautam193e67c2018-02-05 23:29:19 +0530547static size_t arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
548 unsigned long iova, size_t size,
549 arm_v7s_iopte blk_pte,
550 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000551{
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100552 struct io_pgtable_cfg *cfg = &data->iop.cfg;
553 arm_v7s_iopte pte, *tablep;
554 int i, unmap_idx, num_entries, num_ptes;
Robin Murphye5fc9752016-01-26 17:13:13 +0000555
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100556 tablep = __arm_v7s_alloc_table(2, GFP_ATOMIC, data);
557 if (!tablep)
558 return 0; /* Bytes unmapped */
Robin Murphye5fc9752016-01-26 17:13:13 +0000559
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100560 num_ptes = ARM_V7S_PTES_PER_LVL(2);
561 num_entries = size >> ARM_V7S_LVL_SHIFT(2);
562 unmap_idx = ARM_V7S_LVL_IDX(iova, 2);
Robin Murphye5fc9752016-01-26 17:13:13 +0000563
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100564 pte = arm_v7s_prot_to_pte(arm_v7s_pte_to_prot(blk_pte, 1), 2, cfg);
565 if (num_entries > 1)
566 pte = arm_v7s_pte_to_cont(pte, 2);
567
568 for (i = 0; i < num_ptes; i += num_entries, pte += size) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000569 /* Unmap! */
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100570 if (i == unmap_idx)
Robin Murphye5fc9752016-01-26 17:13:13 +0000571 continue;
572
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100573 __arm_v7s_set_pte(&tablep[i], pte, num_entries, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000574 }
575
Robin Murphy119ff302017-06-22 16:53:55 +0100576 pte = arm_v7s_install_table(tablep, ptep, blk_pte, cfg);
577 if (pte != blk_pte) {
578 __arm_v7s_free_table(tablep, 2, data);
579
580 if (!ARM_V7S_PTE_IS_TABLE(pte, 1))
581 return 0;
582
583 tablep = iopte_deref(pte, 1);
584 return __arm_v7s_unmap(data, iova, size, 2, tablep);
585 }
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100586
Will Deaconabfd6fe2019-07-02 16:44:41 +0100587 io_pgtable_tlb_add_page(&data->iop, iova, size);
Robin Murphye5fc9752016-01-26 17:13:13 +0000588 return size;
589}
590
Vivek Gautam193e67c2018-02-05 23:29:19 +0530591static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
592 unsigned long iova, size_t size, int lvl,
593 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000594{
595 arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
Robin Murphy507e4c92016-01-26 17:13:14 +0000596 struct io_pgtable *iop = &data->iop;
Robin Murphye5fc9752016-01-26 17:13:13 +0000597 int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
598
599 /* Something went horribly wrong and we ran out of page table */
600 if (WARN_ON(lvl > 2))
601 return 0;
602
603 idx = ARM_V7S_LVL_IDX(iova, lvl);
604 ptep += idx;
605 do {
Robin Murphy119ff302017-06-22 16:53:55 +0100606 pte[i] = READ_ONCE(ptep[i]);
607 if (WARN_ON(!ARM_V7S_PTE_IS_VALID(pte[i])))
Robin Murphye5fc9752016-01-26 17:13:13 +0000608 return 0;
Robin Murphye5fc9752016-01-26 17:13:13 +0000609 } while (++i < num_entries);
610
611 /*
612 * If we've hit a contiguous 'large page' entry at this level, it
613 * needs splitting first, unless we're unmapping the whole lot.
Robin Murphy119ff302017-06-22 16:53:55 +0100614 *
615 * For splitting, we can't rewrite 16 PTEs atomically, and since we
616 * can't necessarily assume TEX remap we don't have a software bit to
617 * mark live entries being split. In practice (i.e. DMA API code), we
618 * will never be splitting large pages anyway, so just wrap this edge
619 * case in a lock for the sake of correctness and be done with it.
Robin Murphye5fc9752016-01-26 17:13:13 +0000620 */
Robin Murphy119ff302017-06-22 16:53:55 +0100621 if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl)) {
622 unsigned long flags;
623
624 spin_lock_irqsave(&data->split_lock, flags);
625 pte[0] = arm_v7s_split_cont(data, iova, idx, lvl, ptep);
626 spin_unlock_irqrestore(&data->split_lock, flags);
627 }
Robin Murphye5fc9752016-01-26 17:13:13 +0000628
629 /* If the size matches this level, we're in the right place */
630 if (num_entries) {
631 size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
632
Robin Murphy507e4c92016-01-26 17:13:14 +0000633 __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000634
635 for (i = 0; i < num_entries; i++) {
636 if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
637 /* Also flush any partial walks */
Will Deacon10b7a7d2019-07-02 16:44:32 +0100638 io_pgtable_tlb_flush_walk(iop, iova, blk_size,
639 ARM_V7S_BLOCK_SIZE(lvl + 1));
Robin Murphye5fc9752016-01-26 17:13:13 +0000640 ptep = iopte_deref(pte[i], lvl);
641 __arm_v7s_free_table(ptep, lvl + 1, data);
Robin Murphyb2dfeba2018-09-20 17:10:26 +0100642 } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
643 /*
644 * Order the PTE update against queueing the IOVA, to
645 * guarantee that a flush callback from a different CPU
646 * has observed it before the TLBIALL can be issued.
647 */
648 smp_wmb();
Robin Murphye5fc9752016-01-26 17:13:13 +0000649 } else {
Will Deaconabfd6fe2019-07-02 16:44:41 +0100650 io_pgtable_tlb_add_page(iop, iova, blk_size);
Robin Murphye5fc9752016-01-26 17:13:13 +0000651 }
652 iova += blk_size;
653 }
654 return size;
655 } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
656 /*
657 * Insert a table at the next level to map the old region,
658 * minus the part we want to unmap
659 */
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100660 return arm_v7s_split_blk_unmap(data, iova, size, pte[0], ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000661 }
662
663 /* Keep on walkin' */
664 ptep = iopte_deref(pte[0], lvl);
665 return __arm_v7s_unmap(data, iova, size, lvl + 1, ptep);
666}
667
Vivek Gautam193e67c2018-02-05 23:29:19 +0530668static size_t arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova,
669 size_t size)
Robin Murphye5fc9752016-01-26 17:13:13 +0000670{
Robin Murphye5fc9752016-01-26 17:13:13 +0000671 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphye5fc9752016-01-26 17:13:13 +0000672
Robin Murphy76557392017-07-03 14:52:24 +0100673 if (WARN_ON(upper_32_bits(iova)))
674 return 0;
675
Robin Murphy4d689b62017-09-28 15:55:02 +0100676 return __arm_v7s_unmap(data, iova, size, 1, data->pgd);
Robin Murphye5fc9752016-01-26 17:13:13 +0000677}
678
679static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
680 unsigned long iova)
681{
682 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
683 arm_v7s_iopte *ptep = data->pgd, pte;
684 int lvl = 0;
685 u32 mask;
686
687 do {
Robin Murphy119ff302017-06-22 16:53:55 +0100688 ptep += ARM_V7S_LVL_IDX(iova, ++lvl);
689 pte = READ_ONCE(*ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000690 ptep = iopte_deref(pte, lvl);
691 } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
692
693 if (!ARM_V7S_PTE_IS_VALID(pte))
694 return 0;
695
696 mask = ARM_V7S_LVL_MASK(lvl);
697 if (arm_v7s_pte_is_cont(pte, lvl))
698 mask *= ARM_V7S_CONT_PAGES;
699 return (pte & mask) | (iova & ~mask);
700}
701
702static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
703 void *cookie)
704{
705 struct arm_v7s_io_pgtable *data;
706
707 if (cfg->ias > ARM_V7S_ADDR_BITS || cfg->oas > ARM_V7S_ADDR_BITS)
708 return NULL;
709
Robin Murphy3850db42016-02-12 17:09:46 +0000710 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
711 IO_PGTABLE_QUIRK_NO_PERMS |
Yong Wu1afe2312016-03-14 06:01:10 +0800712 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
Robin Murphy81b3c252017-06-22 16:53:53 +0100713 IO_PGTABLE_QUIRK_ARM_MTK_4GB |
Robin Murphyb2dfeba2018-09-20 17:10:26 +0100714 IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000715 return NULL;
716
Yong Wu1afe2312016-03-14 06:01:10 +0800717 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
718 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB &&
719 !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
720 return NULL;
721
Robin Murphye5fc9752016-01-26 17:13:13 +0000722 data = kmalloc(sizeof(*data), GFP_KERNEL);
723 if (!data)
724 return NULL;
725
Robin Murphy119ff302017-06-22 16:53:55 +0100726 spin_lock_init(&data->split_lock);
Robin Murphye5fc9752016-01-26 17:13:13 +0000727 data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
728 ARM_V7S_TABLE_SIZE(2),
729 ARM_V7S_TABLE_SIZE(2),
Nicolas Boichat0a352552019-03-28 20:43:46 -0700730 ARM_V7S_TABLE_SLAB_FLAGS, NULL);
Robin Murphye5fc9752016-01-26 17:13:13 +0000731 if (!data->l2_tables)
732 goto out_free_data;
733
734 data->iop.ops = (struct io_pgtable_ops) {
735 .map = arm_v7s_map,
736 .unmap = arm_v7s_unmap,
737 .iova_to_phys = arm_v7s_iova_to_phys,
738 };
739
740 /* We have to do this early for __arm_v7s_alloc_table to work... */
741 data->iop.cfg = *cfg;
742
743 /*
744 * Unless the IOMMU driver indicates supersection support by
745 * having SZ_16M set in the initial bitmap, they won't be used.
746 */
747 cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
748
749 /* TCR: T0SZ=0, disable TTBR1 */
750 cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1;
751
752 /*
753 * TEX remap: the indices used map to the closest equivalent types
754 * under the non-TEX-remap interpretation of those attribute bits,
755 * excepting various implementation-defined aspects of shareability.
756 */
757 cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
758 ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
759 ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
760 ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
761 ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
762 cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
763 ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
764
765 /* Looking good; allocate a pgd */
766 data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
767 if (!data->pgd)
768 goto out_free_data;
769
770 /* Ensure the empty pgd is visible before any actual TTBR write */
771 wmb();
772
773 /* TTBRs */
774 cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) |
775 ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS |
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700776 (cfg->coherent_walk ?
777 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
778 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA)) :
779 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC) |
780 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC)));
Robin Murphye5fc9752016-01-26 17:13:13 +0000781 cfg->arm_v7s_cfg.ttbr[1] = 0;
782 return &data->iop;
783
784out_free_data:
785 kmem_cache_destroy(data->l2_tables);
786 kfree(data);
787 return NULL;
788}
789
790struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
791 .alloc = arm_v7s_alloc_pgtable,
792 .free = arm_v7s_free_pgtable,
793};
794
795#ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
796
797static struct io_pgtable_cfg *cfg_cookie;
798
799static void dummy_tlb_flush_all(void *cookie)
800{
801 WARN_ON(cookie != cfg_cookie);
802}
803
Will Deacon10b7a7d2019-07-02 16:44:32 +0100804static void dummy_tlb_flush(unsigned long iova, size_t size, size_t granule,
805 void *cookie)
Robin Murphye5fc9752016-01-26 17:13:13 +0000806{
807 WARN_ON(cookie != cfg_cookie);
808 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
809}
810
Will Deaconabfd6fe2019-07-02 16:44:41 +0100811static void dummy_tlb_add_page(unsigned long iova, size_t granule, void *cookie)
Will Deacon10b7a7d2019-07-02 16:44:32 +0100812{
Will Deaconabfd6fe2019-07-02 16:44:41 +0100813 dummy_tlb_flush(iova, granule, granule, cookie);
Will Deacon10b7a7d2019-07-02 16:44:32 +0100814}
815
Robin Murphye5fc9752016-01-26 17:13:13 +0000816static void dummy_tlb_sync(void *cookie)
817{
818 WARN_ON(cookie != cfg_cookie);
819}
820
Will Deacon298f78892019-07-02 16:43:34 +0100821static const struct iommu_flush_ops dummy_tlb_ops = {
Robin Murphye5fc9752016-01-26 17:13:13 +0000822 .tlb_flush_all = dummy_tlb_flush_all,
Will Deacon10b7a7d2019-07-02 16:44:32 +0100823 .tlb_flush_walk = dummy_tlb_flush,
824 .tlb_flush_leaf = dummy_tlb_flush,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100825 .tlb_add_page = dummy_tlb_add_page,
Robin Murphye5fc9752016-01-26 17:13:13 +0000826 .tlb_sync = dummy_tlb_sync,
827};
828
829#define __FAIL(ops) ({ \
830 WARN(1, "selftest: test failed\n"); \
831 selftest_running = false; \
832 -EFAULT; \
833})
834
835static int __init arm_v7s_do_selftests(void)
836{
837 struct io_pgtable_ops *ops;
838 struct io_pgtable_cfg cfg = {
839 .tlb = &dummy_tlb_ops,
840 .oas = 32,
841 .ias = 32,
Will Deacon4f418452019-06-25 12:51:25 +0100842 .coherent_walk = true,
843 .quirks = IO_PGTABLE_QUIRK_ARM_NS,
Robin Murphye5fc9752016-01-26 17:13:13 +0000844 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
845 };
846 unsigned int iova, size, iova_start;
847 unsigned int i, loopnr = 0;
848
849 selftest_running = true;
850
851 cfg_cookie = &cfg;
852
853 ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
854 if (!ops) {
855 pr_err("selftest: failed to allocate io pgtable ops\n");
856 return -EINVAL;
857 }
858
859 /*
860 * Initial sanity checks.
861 * Empty page tables shouldn't provide any translations.
862 */
863 if (ops->iova_to_phys(ops, 42))
864 return __FAIL(ops);
865
866 if (ops->iova_to_phys(ops, SZ_1G + 42))
867 return __FAIL(ops);
868
869 if (ops->iova_to_phys(ops, SZ_2G + 42))
870 return __FAIL(ops);
871
872 /*
873 * Distinct mappings of different granule sizes.
874 */
875 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +0800876 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000877 size = 1UL << i;
878 if (ops->map(ops, iova, iova, size, IOMMU_READ |
879 IOMMU_WRITE |
880 IOMMU_NOEXEC |
881 IOMMU_CACHE))
882 return __FAIL(ops);
883
884 /* Overlapping mappings */
885 if (!ops->map(ops, iova, iova + size, size,
886 IOMMU_READ | IOMMU_NOEXEC))
887 return __FAIL(ops);
888
889 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
890 return __FAIL(ops);
891
892 iova += SZ_16M;
Robin Murphye5fc9752016-01-26 17:13:13 +0000893 loopnr++;
894 }
895
896 /* Partial unmap */
897 i = 1;
898 size = 1UL << __ffs(cfg.pgsize_bitmap);
899 while (i < loopnr) {
900 iova_start = i * SZ_16M;
901 if (ops->unmap(ops, iova_start + size, size) != size)
902 return __FAIL(ops);
903
904 /* Remap of partial unmap */
905 if (ops->map(ops, iova_start + size, size, size, IOMMU_READ))
906 return __FAIL(ops);
907
908 if (ops->iova_to_phys(ops, iova_start + size + 42)
909 != (size + 42))
910 return __FAIL(ops);
911 i++;
912 }
913
914 /* Full unmap */
915 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +0800916 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000917 size = 1UL << i;
918
919 if (ops->unmap(ops, iova, size) != size)
920 return __FAIL(ops);
921
922 if (ops->iova_to_phys(ops, iova + 42))
923 return __FAIL(ops);
924
925 /* Remap full block */
926 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
927 return __FAIL(ops);
928
929 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
930 return __FAIL(ops);
931
932 iova += SZ_16M;
Robin Murphye5fc9752016-01-26 17:13:13 +0000933 }
934
935 free_io_pgtable_ops(ops);
936
937 selftest_running = false;
938
939 pr_info("self test ok\n");
940 return 0;
941}
942subsys_initcall(arm_v7s_do_selftests);
943#endif