blob: d04280f0f4dfad180f607c115de8cd7d248708ea [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * CPU-agnostic ARM page table allocator.
3 *
4 * ARMv7 Short-descriptor format, supporting
5 * - Basic memory attributes
6 * - Simplified access permissions (AP[2:1] model)
7 * - Backwards-compatible TEX remap
8 * - Large pages/supersections (if indicated by the caller)
9 *
10 * Not supporting:
11 * - Legacy access permissions (AP[2:0] model)
12 *
13 * Almost certainly never supporting:
14 * - PXN
15 * - Domains
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *
29 * Copyright (C) 2014-2015 ARM Limited
30 * Copyright (c) 2014-2015 MediaTek Inc.
31 */
32
33#define pr_fmt(fmt) "arm-v7s io-pgtable: " fmt
34
35#include <linux/atomic.h>
36#include <linux/dma-mapping.h>
37#include <linux/gfp.h>
38#include <linux/iommu.h>
39#include <linux/kernel.h>
40#include <linux/kmemleak.h>
41#include <linux/sizes.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/types.h>
45
46#include <asm/barrier.h>
47
48#include "io-pgtable.h"
49
50/* Struct accessors */
51#define io_pgtable_to_data(x) \
52 container_of((x), struct arm_v7s_io_pgtable, iop)
53
54#define io_pgtable_ops_to_data(x) \
55 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
56
57/*
58 * We have 32 bits total; 12 bits resolved at level 1, 8 bits at level 2,
59 * and 12 bits in a page. With some carefully-chosen coefficients we can
60 * hide the ugly inconsistencies behind these macros and at least let the
61 * rest of the code pretend to be somewhat sane.
62 */
63#define ARM_V7S_ADDR_BITS 32
64#define _ARM_V7S_LVL_BITS(lvl) (16 - (lvl) * 4)
65#define ARM_V7S_LVL_SHIFT(lvl) (ARM_V7S_ADDR_BITS - (4 + 8 * (lvl)))
66#define ARM_V7S_TABLE_SHIFT 10
67
68#define ARM_V7S_PTES_PER_LVL(lvl) (1 << _ARM_V7S_LVL_BITS(lvl))
69#define ARM_V7S_TABLE_SIZE(lvl) \
70 (ARM_V7S_PTES_PER_LVL(lvl) * sizeof(arm_v7s_iopte))
71
72#define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
73#define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
74#define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
75#define _ARM_V7S_IDX_MASK(lvl) (ARM_V7S_PTES_PER_LVL(lvl) - 1)
76#define ARM_V7S_LVL_IDX(addr, lvl) ({ \
77 int _l = lvl; \
78 ((u32)(addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l); \
79})
80
81/*
82 * Large page/supersection entries are effectively a block of 16 page/section
83 * entries, along the lines of the LPAE contiguous hint, but all with the
84 * same output address. For want of a better common name we'll call them
85 * "contiguous" versions of their respective page/section entries here, but
86 * noting the distinction (WRT to TLB maintenance) that they represent *one*
87 * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
88 */
89#define ARM_V7S_CONT_PAGES 16
90
91/* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
92#define ARM_V7S_PTE_TYPE_TABLE 0x1
93#define ARM_V7S_PTE_TYPE_PAGE 0x2
94#define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
95
96#define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
97#define ARM_V7S_PTE_IS_TABLE(pte, lvl) \
98 ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE))
99
100/* Page table bits */
101#define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
102#define ARM_V7S_ATTR_B BIT(2)
103#define ARM_V7S_ATTR_C BIT(3)
104#define ARM_V7S_ATTR_NS_TABLE BIT(3)
105#define ARM_V7S_ATTR_NS_SECTION BIT(19)
106
107#define ARM_V7S_CONT_SECTION BIT(18)
108#define ARM_V7S_CONT_PAGE_XN_SHIFT 15
109
110/*
111 * The attribute bits are consistently ordered*, but occupy bits [17:10] of
112 * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
113 * fields relative to that 8-bit block, plus a total shift relative to the PTE.
114 */
115#define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
116
117#define ARM_V7S_ATTR_MASK 0xff
118#define ARM_V7S_ATTR_AP0 BIT(0)
119#define ARM_V7S_ATTR_AP1 BIT(1)
120#define ARM_V7S_ATTR_AP2 BIT(5)
121#define ARM_V7S_ATTR_S BIT(6)
122#define ARM_V7S_ATTR_NG BIT(7)
123#define ARM_V7S_TEX_SHIFT 2
124#define ARM_V7S_TEX_MASK 0x7
125#define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
126
127/* MTK extend the two bits below for over 4GB mode */
128#define ARM_V7S_ATTR_MTK_PA_BIT32 BIT(9)
129#define ARM_V7S_ATTR_MTK_PA_BIT33 BIT(4)
130
131/* *well, except for TEX on level 2 large pages, of course :( */
132#define ARM_V7S_CONT_PAGE_TEX_SHIFT 6
133#define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT)
134
135/* Simplified access permissions */
136#define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0
137#define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1
138#define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2
139
140/* Register bits */
141#define ARM_V7S_RGN_NC 0
142#define ARM_V7S_RGN_WBWA 1
143#define ARM_V7S_RGN_WT 2
144#define ARM_V7S_RGN_WB 3
145
146#define ARM_V7S_PRRR_TYPE_DEVICE 1
147#define ARM_V7S_PRRR_TYPE_NORMAL 2
148#define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2))
149#define ARM_V7S_PRRR_DS0 BIT(16)
150#define ARM_V7S_PRRR_DS1 BIT(17)
151#define ARM_V7S_PRRR_NS0 BIT(18)
152#define ARM_V7S_PRRR_NS1 BIT(19)
153#define ARM_V7S_PRRR_NOS(n) BIT((n) + 24)
154
155#define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2))
156#define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16))
157
158#define ARM_V7S_TTBR_S BIT(1)
159#define ARM_V7S_TTBR_NOS BIT(5)
160#define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3)
161#define ARM_V7S_TTBR_IRGN_ATTR(attr) \
162 ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1))
163
164#define ARM_V7S_TCR_PD1 BIT(5)
165
166typedef u32 arm_v7s_iopte;
167
168static bool selftest_running;
169
170struct arm_v7s_io_pgtable {
171 struct io_pgtable iop;
172
173 arm_v7s_iopte *pgd;
174 struct kmem_cache *l2_tables;
175 spinlock_t split_lock;
176};
177
178static dma_addr_t __arm_v7s_dma_addr(void *pages)
179{
180 return (dma_addr_t)virt_to_phys(pages);
181}
182
183static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl)
184{
185 if (ARM_V7S_PTE_IS_TABLE(pte, lvl))
186 pte &= ARM_V7S_TABLE_MASK;
187 else
188 pte &= ARM_V7S_LVL_MASK(lvl);
189 return phys_to_virt(pte);
190}
191
192static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp,
193 struct arm_v7s_io_pgtable *data)
194{
195 struct io_pgtable_cfg *cfg = &data->iop.cfg;
196 struct device *dev = cfg->iommu_dev;
197 phys_addr_t phys;
198 dma_addr_t dma;
199 size_t size = ARM_V7S_TABLE_SIZE(lvl);
200 void *table = NULL;
201
202 if (lvl == 1)
203 table = (void *)__get_dma_pages(__GFP_ZERO, get_order(size));
204 else if (lvl == 2)
205 table = kmem_cache_zalloc(data->l2_tables, gfp | GFP_DMA);
206 phys = virt_to_phys(table);
207 if (phys != (arm_v7s_iopte)phys)
208 /* Doesn't fit in PTE */
209 goto out_free;
210 if (table && !(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) {
211 dma = dma_map_single(dev, table, size, DMA_TO_DEVICE);
212 if (dma_mapping_error(dev, dma))
213 goto out_free;
214 /*
215 * We depend on the IOMMU being able to work with any physical
216 * address directly, so if the DMA layer suggests otherwise by
217 * translating or truncating them, that bodes very badly...
218 */
219 if (dma != phys)
220 goto out_unmap;
221 }
222 if (lvl == 2)
223 kmemleak_ignore(table);
224 return table;
225
226out_unmap:
227 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
228 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
229out_free:
230 if (lvl == 1)
231 free_pages((unsigned long)table, get_order(size));
232 else
233 kmem_cache_free(data->l2_tables, table);
234 return NULL;
235}
236
237static void __arm_v7s_free_table(void *table, int lvl,
238 struct arm_v7s_io_pgtable *data)
239{
240 struct io_pgtable_cfg *cfg = &data->iop.cfg;
241 struct device *dev = cfg->iommu_dev;
242 size_t size = ARM_V7S_TABLE_SIZE(lvl);
243
244 if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
245 dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
246 DMA_TO_DEVICE);
247 if (lvl == 1)
248 free_pages((unsigned long)table, get_order(size));
249 else
250 kmem_cache_free(data->l2_tables, table);
251}
252
253static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
254 struct io_pgtable_cfg *cfg)
255{
256 if (cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)
257 return;
258
259 dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
260 num_entries * sizeof(*ptep), DMA_TO_DEVICE);
261}
262static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
263 int num_entries, struct io_pgtable_cfg *cfg)
264{
265 int i;
266
267 for (i = 0; i < num_entries; i++)
268 ptep[i] = pte;
269
270 __arm_v7s_pte_sync(ptep, num_entries, cfg);
271}
272
273static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
274 struct io_pgtable_cfg *cfg,
275 phys_addr_t paddr) /* Only for MTK */
276{
277 bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
278 arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
279
280 if (!(prot & IOMMU_MMIO))
281 pte |= ARM_V7S_ATTR_TEX(1);
282 if (ap) {
283 pte |= ARM_V7S_PTE_AF;
284 if (!(prot & IOMMU_PRIV))
285 pte |= ARM_V7S_PTE_AP_UNPRIV;
286 if (!(prot & IOMMU_WRITE))
287 pte |= ARM_V7S_PTE_AP_RDONLY;
288 }
289 pte <<= ARM_V7S_ATTR_SHIFT(lvl);
290
291 if ((prot & IOMMU_NOEXEC) && ap)
292 pte |= ARM_V7S_ATTR_XN(lvl);
293 if (prot & IOMMU_MMIO)
294 pte |= ARM_V7S_ATTR_B;
295 else if (prot & IOMMU_CACHE)
296 pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
297
298 pte |= ARM_V7S_PTE_TYPE_PAGE;
299 if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
300 pte |= ARM_V7S_ATTR_NS_SECTION;
301
302 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB) {
303 if (paddr & BIT_ULL(32))
304 pte |= ARM_V7S_ATTR_MTK_PA_BIT32;
305 if (paddr & BIT_ULL(33))
306 pte |= ARM_V7S_ATTR_MTK_PA_BIT33;
307 }
308
309 return pte;
310}
311
312static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
313{
314 int prot = IOMMU_READ;
315 arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
316
317 if (!(attr & ARM_V7S_PTE_AP_RDONLY))
318 prot |= IOMMU_WRITE;
319 if (!(attr & ARM_V7S_PTE_AP_UNPRIV))
320 prot |= IOMMU_PRIV;
321 if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
322 prot |= IOMMU_MMIO;
323 else if (pte & ARM_V7S_ATTR_C)
324 prot |= IOMMU_CACHE;
325 if (pte & ARM_V7S_ATTR_XN(lvl))
326 prot |= IOMMU_NOEXEC;
327
328 return prot;
329}
330
331static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
332{
333 if (lvl == 1) {
334 pte |= ARM_V7S_CONT_SECTION;
335 } else if (lvl == 2) {
336 arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
337 arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
338
339 pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
340 pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
341 (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
342 ARM_V7S_PTE_TYPE_CONT_PAGE;
343 }
344 return pte;
345}
346
347static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
348{
349 if (lvl == 1) {
350 pte &= ~ARM_V7S_CONT_SECTION;
351 } else if (lvl == 2) {
352 arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
353 arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
354 ARM_V7S_CONT_PAGE_TEX_SHIFT);
355
356 pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
357 pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
358 (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
359 ARM_V7S_PTE_TYPE_PAGE;
360 }
361 return pte;
362}
363
364static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
365{
366 if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
367 return pte & ARM_V7S_CONT_SECTION;
368 else if (lvl == 2)
369 return !(pte & ARM_V7S_PTE_TYPE_PAGE);
370 return false;
371}
372
373static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *, unsigned long,
374 size_t, int, arm_v7s_iopte *);
375
376static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
377 unsigned long iova, phys_addr_t paddr, int prot,
378 int lvl, int num_entries, arm_v7s_iopte *ptep)
379{
380 struct io_pgtable_cfg *cfg = &data->iop.cfg;
381 arm_v7s_iopte pte;
382 int i;
383
384 for (i = 0; i < num_entries; i++)
385 if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
386 /*
387 * We need to unmap and free the old table before
388 * overwriting it with a block entry.
389 */
390 arm_v7s_iopte *tblp;
391 size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
392
393 tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl);
394 if (WARN_ON(__arm_v7s_unmap(data, iova + i * sz,
395 sz, lvl, tblp) != sz))
396 return -EINVAL;
397 } else if (ptep[i]) {
398 /* We require an unmap first */
399 WARN_ON(!selftest_running);
400 return -EEXIST;
401 }
402
403 pte = arm_v7s_prot_to_pte(prot, lvl, cfg, paddr);
404 if (num_entries > 1)
405 pte = arm_v7s_pte_to_cont(pte, lvl);
406
407 pte |= paddr & ARM_V7S_LVL_MASK(lvl);
408
409 __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
410 return 0;
411}
412
413static arm_v7s_iopte arm_v7s_install_table(arm_v7s_iopte *table,
414 arm_v7s_iopte *ptep,
415 arm_v7s_iopte curr,
416 struct io_pgtable_cfg *cfg)
417{
418 arm_v7s_iopte old, new;
419
420 new = virt_to_phys(table) | ARM_V7S_PTE_TYPE_TABLE;
421 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
422 new |= ARM_V7S_ATTR_NS_TABLE;
423
424 /*
425 * Ensure the table itself is visible before its PTE can be.
426 * Whilst we could get away with cmpxchg64_release below, this
427 * doesn't have any ordering semantics when !CONFIG_SMP.
428 */
429 dma_wmb();
430
431 old = cmpxchg_relaxed(ptep, curr, new);
432 __arm_v7s_pte_sync(ptep, 1, cfg);
433
434 return old;
435}
436
437static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
438 phys_addr_t paddr, size_t size, int prot,
439 int lvl, arm_v7s_iopte *ptep)
440{
441 struct io_pgtable_cfg *cfg = &data->iop.cfg;
442 arm_v7s_iopte pte, *cptep;
443 int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
444
445 /* Find our entry at the current level */
446 ptep += ARM_V7S_LVL_IDX(iova, lvl);
447
448 /* If we can install a leaf entry at this level, then do so */
449 if (num_entries)
450 return arm_v7s_init_pte(data, iova, paddr, prot,
451 lvl, num_entries, ptep);
452
453 /* We can't allocate tables at the final level */
454 if (WARN_ON(lvl == 2))
455 return -EINVAL;
456
457 /* Grab a pointer to the next level */
458 pte = READ_ONCE(*ptep);
459 if (!pte) {
460 cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data);
461 if (!cptep)
462 return -ENOMEM;
463
464 pte = arm_v7s_install_table(cptep, ptep, 0, cfg);
465 if (pte)
466 __arm_v7s_free_table(cptep, lvl + 1, data);
467 } else {
468 /* We've no easy way of knowing if it's synced yet, so... */
469 __arm_v7s_pte_sync(ptep, 1, cfg);
470 }
471
472 if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
473 cptep = iopte_deref(pte, lvl);
474 } else if (pte) {
475 /* We require an unmap first */
476 WARN_ON(!selftest_running);
477 return -EEXIST;
478 }
479
480 /* Rinse, repeat */
481 return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep);
482}
483
484static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova,
485 phys_addr_t paddr, size_t size, int prot)
486{
487 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
488 struct io_pgtable *iop = &data->iop;
489 int ret;
490
491 /* If no access, then nothing to do */
492 if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
493 return 0;
494
495 if (WARN_ON(upper_32_bits(iova)))
496 return -ERANGE;
497
498 if (WARN_ON(upper_32_bits(paddr) &&
499 !(iop->cfg.quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB)))
500 return -ERANGE;
501
502 ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd);
503 /*
504 * Synchronise all PTE updates for the new mapping before there's
505 * a chance for anything to kick off a table walk for the new iova.
506 */
507 if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) {
508 io_pgtable_tlb_add_flush(iop, iova, size,
509 ARM_V7S_BLOCK_SIZE(2), false);
510 io_pgtable_tlb_sync(iop);
511 } else {
512 wmb();
513 }
514
515 return ret;
516}
517
518static void arm_v7s_free_pgtable(struct io_pgtable *iop)
519{
520 struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
521 int i;
522
523 for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) {
524 arm_v7s_iopte pte = data->pgd[i];
525
526 if (ARM_V7S_PTE_IS_TABLE(pte, 1))
527 __arm_v7s_free_table(iopte_deref(pte, 1), 2, data);
528 }
529 __arm_v7s_free_table(data->pgd, 1, data);
530 kmem_cache_destroy(data->l2_tables);
531 kfree(data);
532}
533
534static arm_v7s_iopte arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
535 unsigned long iova, int idx, int lvl,
536 arm_v7s_iopte *ptep)
537{
538 struct io_pgtable *iop = &data->iop;
539 arm_v7s_iopte pte;
540 size_t size = ARM_V7S_BLOCK_SIZE(lvl);
541 int i;
542
543 /* Check that we didn't lose a race to get the lock */
544 pte = *ptep;
545 if (!arm_v7s_pte_is_cont(pte, lvl))
546 return pte;
547
548 ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
549 pte = arm_v7s_cont_to_pte(pte, lvl);
550 for (i = 0; i < ARM_V7S_CONT_PAGES; i++)
551 ptep[i] = pte + i * size;
552
553 __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
554
555 size *= ARM_V7S_CONT_PAGES;
556 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
557 io_pgtable_tlb_sync(iop);
558 return pte;
559}
560
561static int arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
562 unsigned long iova, size_t size,
563 arm_v7s_iopte blk_pte, arm_v7s_iopte *ptep)
564{
565 struct io_pgtable_cfg *cfg = &data->iop.cfg;
566 arm_v7s_iopte pte, *tablep;
567 int i, unmap_idx, num_entries, num_ptes;
568
569 tablep = __arm_v7s_alloc_table(2, GFP_ATOMIC, data);
570 if (!tablep)
571 return 0; /* Bytes unmapped */
572
573 num_ptes = ARM_V7S_PTES_PER_LVL(2);
574 num_entries = size >> ARM_V7S_LVL_SHIFT(2);
575 unmap_idx = ARM_V7S_LVL_IDX(iova, 2);
576
577 pte = arm_v7s_prot_to_pte(arm_v7s_pte_to_prot(blk_pte, 1), 2, cfg, 0);
578 if (num_entries > 1)
579 pte = arm_v7s_pte_to_cont(pte, 2);
580
581 for (i = 0; i < num_ptes; i += num_entries, pte += size) {
582 /* Unmap! */
583 if (i == unmap_idx)
584 continue;
585
586 __arm_v7s_set_pte(&tablep[i], pte, num_entries, cfg);
587 }
588
589 pte = arm_v7s_install_table(tablep, ptep, blk_pte, cfg);
590 if (pte != blk_pte) {
591 __arm_v7s_free_table(tablep, 2, data);
592
593 if (!ARM_V7S_PTE_IS_TABLE(pte, 1))
594 return 0;
595
596 tablep = iopte_deref(pte, 1);
597 return __arm_v7s_unmap(data, iova, size, 2, tablep);
598 }
599
600 io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true);
601 return size;
602}
603
604static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
605 unsigned long iova, size_t size, int lvl,
606 arm_v7s_iopte *ptep)
607{
608 arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
609 struct io_pgtable *iop = &data->iop;
610 int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
611
612 /* Something went horribly wrong and we ran out of page table */
613 if (WARN_ON(lvl > 2))
614 return 0;
615
616 idx = ARM_V7S_LVL_IDX(iova, lvl);
617 ptep += idx;
618 do {
619 pte[i] = READ_ONCE(ptep[i]);
620 if (WARN_ON(!ARM_V7S_PTE_IS_VALID(pte[i])))
621 return 0;
622 } while (++i < num_entries);
623
624 /*
625 * If we've hit a contiguous 'large page' entry at this level, it
626 * needs splitting first, unless we're unmapping the whole lot.
627 *
628 * For splitting, we can't rewrite 16 PTEs atomically, and since we
629 * can't necessarily assume TEX remap we don't have a software bit to
630 * mark live entries being split. In practice (i.e. DMA API code), we
631 * will never be splitting large pages anyway, so just wrap this edge
632 * case in a lock for the sake of correctness and be done with it.
633 */
634 if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl)) {
635 unsigned long flags;
636
637 spin_lock_irqsave(&data->split_lock, flags);
638 pte[0] = arm_v7s_split_cont(data, iova, idx, lvl, ptep);
639 spin_unlock_irqrestore(&data->split_lock, flags);
640 }
641
642 /* If the size matches this level, we're in the right place */
643 if (num_entries) {
644 size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
645
646 __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
647
648 for (i = 0; i < num_entries; i++) {
649 if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
650 /* Also flush any partial walks */
651 io_pgtable_tlb_add_flush(iop, iova, blk_size,
652 ARM_V7S_BLOCK_SIZE(lvl + 1), false);
653 io_pgtable_tlb_sync(iop);
654 ptep = iopte_deref(pte[i], lvl);
655 __arm_v7s_free_table(ptep, lvl + 1, data);
656 } else {
657 io_pgtable_tlb_add_flush(iop, iova, blk_size,
658 blk_size, true);
659 }
660 iova += blk_size;
661 }
662 return size;
663 } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
664 /*
665 * Insert a table at the next level to map the old region,
666 * minus the part we want to unmap
667 */
668 return arm_v7s_split_blk_unmap(data, iova, size, pte[0], ptep);
669 }
670
671 /* Keep on walkin' */
672 ptep = iopte_deref(pte[0], lvl);
673 return __arm_v7s_unmap(data, iova, size, lvl + 1, ptep);
674}
675
676static int arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova,
677 size_t size)
678{
679 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
680
681 if (WARN_ON(upper_32_bits(iova)))
682 return 0;
683
684 return __arm_v7s_unmap(data, iova, size, 1, data->pgd);
685}
686
687static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
688 unsigned long iova)
689{
690 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
691 struct io_pgtable_cfg *cfg = &data->iop.cfg;
692 arm_v7s_iopte *ptep = data->pgd, pte;
693 phys_addr_t paddr;
694 int lvl = 0;
695 u32 mask;
696
697 do {
698 ptep += ARM_V7S_LVL_IDX(iova, ++lvl);
699 pte = READ_ONCE(*ptep);
700 ptep = iopte_deref(pte, lvl);
701 } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
702
703 if (!ARM_V7S_PTE_IS_VALID(pte))
704 return 0;
705
706 mask = ARM_V7S_LVL_MASK(lvl);
707 if (arm_v7s_pte_is_cont(pte, lvl))
708 mask *= ARM_V7S_CONT_PAGES;
709 paddr = (pte & mask) | (iova & ~mask);
710
711 if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
712 cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB) {
713 if (pte & ARM_V7S_ATTR_MTK_PA_BIT32)
714 paddr |= BIT_ULL(32);
715 if (pte & ARM_V7S_ATTR_MTK_PA_BIT33)
716 paddr |= BIT_ULL(33);
717 }
718 return paddr;
719}
720
721static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
722 void *cookie)
723{
724 struct arm_v7s_io_pgtable *data;
725
726#ifdef PHYS_OFFSET
727 if (upper_32_bits(PHYS_OFFSET))
728 return NULL;
729#endif
730 if (cfg->ias > ARM_V7S_ADDR_BITS || cfg->oas > ARM_V7S_ADDR_BITS)
731 return NULL;
732
733 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
734 IO_PGTABLE_QUIRK_NO_PERMS |
735 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
736 IO_PGTABLE_QUIRK_ARM_MTK_4GB |
737 IO_PGTABLE_QUIRK_NO_DMA))
738 return NULL;
739
740 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
741 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB &&
742 !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
743 return NULL;
744
745 data = kmalloc(sizeof(*data), GFP_KERNEL);
746 if (!data)
747 return NULL;
748
749 spin_lock_init(&data->split_lock);
750 data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
751 ARM_V7S_TABLE_SIZE(2),
752 ARM_V7S_TABLE_SIZE(2),
753 SLAB_CACHE_DMA, NULL);
754 if (!data->l2_tables)
755 goto out_free_data;
756
757 data->iop.ops = (struct io_pgtable_ops) {
758 .map = arm_v7s_map,
759 .unmap = arm_v7s_unmap,
760 .iova_to_phys = arm_v7s_iova_to_phys,
761 };
762
763 /* We have to do this early for __arm_v7s_alloc_table to work... */
764 data->iop.cfg = *cfg;
765
766 /*
767 * Unless the IOMMU driver indicates supersection support by
768 * having SZ_16M set in the initial bitmap, they won't be used.
769 */
770 cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
771
772 /* TCR: T0SZ=0, disable TTBR1 */
773 cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1;
774
775 /*
776 * TEX remap: the indices used map to the closest equivalent types
777 * under the non-TEX-remap interpretation of those attribute bits,
778 * excepting various implementation-defined aspects of shareability.
779 */
780 cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
781 ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
782 ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
783 ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
784 ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
785 cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
786 ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
787
788 /* Looking good; allocate a pgd */
789 data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
790 if (!data->pgd)
791 goto out_free_data;
792
793 /* Ensure the empty pgd is visible before any actual TTBR write */
794 wmb();
795
796 /* TTBRs */
797 cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) |
798 ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS |
799 ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
800 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA);
801 cfg->arm_v7s_cfg.ttbr[1] = 0;
802 return &data->iop;
803
804out_free_data:
805 kmem_cache_destroy(data->l2_tables);
806 kfree(data);
807 return NULL;
808}
809
810struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
811 .alloc = arm_v7s_alloc_pgtable,
812 .free = arm_v7s_free_pgtable,
813};
814
815#ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
816
817static struct io_pgtable_cfg *cfg_cookie;
818
819static void dummy_tlb_flush_all(void *cookie)
820{
821 WARN_ON(cookie != cfg_cookie);
822}
823
824static void dummy_tlb_add_flush(unsigned long iova, size_t size,
825 size_t granule, bool leaf, void *cookie)
826{
827 WARN_ON(cookie != cfg_cookie);
828 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
829}
830
831static void dummy_tlb_sync(void *cookie)
832{
833 WARN_ON(cookie != cfg_cookie);
834}
835
836static const struct iommu_gather_ops dummy_tlb_ops = {
837 .tlb_flush_all = dummy_tlb_flush_all,
838 .tlb_add_flush = dummy_tlb_add_flush,
839 .tlb_sync = dummy_tlb_sync,
840};
841
842#define __FAIL(ops) ({ \
843 WARN(1, "selftest: test failed\n"); \
844 selftest_running = false; \
845 -EFAULT; \
846})
847
848static int __init arm_v7s_do_selftests(void)
849{
850 struct io_pgtable_ops *ops;
851 struct io_pgtable_cfg cfg = {
852 .tlb = &dummy_tlb_ops,
853 .oas = 32,
854 .ias = 32,
855 .quirks = IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA,
856 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
857 };
858 unsigned int iova, size, iova_start;
859 unsigned int i, loopnr = 0;
860
861 selftest_running = true;
862
863 cfg_cookie = &cfg;
864
865 ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
866 if (!ops) {
867 pr_err("selftest: failed to allocate io pgtable ops\n");
868 return -EINVAL;
869 }
870
871 /*
872 * Initial sanity checks.
873 * Empty page tables shouldn't provide any translations.
874 */
875 if (ops->iova_to_phys(ops, 42))
876 return __FAIL(ops);
877
878 if (ops->iova_to_phys(ops, SZ_1G + 42))
879 return __FAIL(ops);
880
881 if (ops->iova_to_phys(ops, SZ_2G + 42))
882 return __FAIL(ops);
883
884 /*
885 * Distinct mappings of different granule sizes.
886 */
887 iova = 0;
888 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
889 size = 1UL << i;
890 if (ops->map(ops, iova, iova, size, IOMMU_READ |
891 IOMMU_WRITE |
892 IOMMU_NOEXEC |
893 IOMMU_CACHE))
894 return __FAIL(ops);
895
896 /* Overlapping mappings */
897 if (!ops->map(ops, iova, iova + size, size,
898 IOMMU_READ | IOMMU_NOEXEC))
899 return __FAIL(ops);
900
901 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
902 return __FAIL(ops);
903
904 iova += SZ_16M;
905 loopnr++;
906 }
907
908 /* Partial unmap */
909 i = 1;
910 size = 1UL << __ffs(cfg.pgsize_bitmap);
911 while (i < loopnr) {
912 iova_start = i * SZ_16M;
913 if (ops->unmap(ops, iova_start + size, size) != size)
914 return __FAIL(ops);
915
916 /* Remap of partial unmap */
917 if (ops->map(ops, iova_start + size, size, size, IOMMU_READ))
918 return __FAIL(ops);
919
920 if (ops->iova_to_phys(ops, iova_start + size + 42)
921 != (size + 42))
922 return __FAIL(ops);
923 i++;
924 }
925
926 /* Full unmap */
927 iova = 0;
928 i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG);
929 while (i != BITS_PER_LONG) {
930 size = 1UL << i;
931
932 if (ops->unmap(ops, iova, size) != size)
933 return __FAIL(ops);
934
935 if (ops->iova_to_phys(ops, iova + 42))
936 return __FAIL(ops);
937
938 /* Remap full block */
939 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
940 return __FAIL(ops);
941
942 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
943 return __FAIL(ops);
944
945 iova += SZ_16M;
946 i++;
947 i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
948 }
949
950 free_io_pgtable_ops(ops);
951
952 selftest_running = false;
953
954 pr_info("self test ok\n");
955 return 0;
956}
957subsys_initcall(arm_v7s_do_selftests);
958#endif