blob: 06ffc683b28feeb478e3a26a80a17e60c806932a [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,
Yong Wu00ab6f22021-01-11 19:18:52 +080047 * and 12 bits in a page.
Yong Wuf3a8a46d2021-01-11 19:18:54 +080048 * MediaTek extend 2 bits to reach 34bits, 14 bits at lvl1 and 8 bits at lvl2.
Robin Murphye5fc9752016-01-26 17:13:13 +000049 */
50#define ARM_V7S_ADDR_BITS 32
Yong Wuf3a8a46d2021-01-11 19:18:54 +080051#define _ARM_V7S_LVL_BITS(lvl, cfg) ((lvl) == 1 ? ((cfg)->ias - 20) : 8)
Yong Wu00ab6f22021-01-11 19:18:52 +080052#define ARM_V7S_LVL_SHIFT(lvl) ((lvl) == 1 ? 20 : 12)
Robin Murphye5fc9752016-01-26 17:13:13 +000053#define ARM_V7S_TABLE_SHIFT 10
54
Yong Wu468ea0b2021-01-11 19:18:53 +080055#define ARM_V7S_PTES_PER_LVL(lvl, cfg) (1 << _ARM_V7S_LVL_BITS(lvl, cfg))
56#define ARM_V7S_TABLE_SIZE(lvl, cfg) \
57 (ARM_V7S_PTES_PER_LVL(lvl, cfg) * sizeof(arm_v7s_iopte))
Robin Murphye5fc9752016-01-26 17:13:13 +000058
59#define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
60#define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
61#define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
Yong Wu468ea0b2021-01-11 19:18:53 +080062#define _ARM_V7S_IDX_MASK(lvl, cfg) (ARM_V7S_PTES_PER_LVL(lvl, cfg) - 1)
63#define ARM_V7S_LVL_IDX(addr, lvl, cfg) ({ \
Robin Murphye5fc9752016-01-26 17:13:13 +000064 int _l = lvl; \
Yong Wuf3a8a46d2021-01-11 19:18:54 +080065 ((addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l, cfg); \
Robin Murphye5fc9752016-01-26 17:13:13 +000066})
67
68/*
69 * Large page/supersection entries are effectively a block of 16 page/section
70 * entries, along the lines of the LPAE contiguous hint, but all with the
71 * same output address. For want of a better common name we'll call them
72 * "contiguous" versions of their respective page/section entries here, but
73 * noting the distinction (WRT to TLB maintenance) that they represent *one*
74 * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
75 */
76#define ARM_V7S_CONT_PAGES 16
77
78/* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
79#define ARM_V7S_PTE_TYPE_TABLE 0x1
80#define ARM_V7S_PTE_TYPE_PAGE 0x2
81#define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
82
83#define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
Robin Murphy9db829d2017-06-22 16:53:50 +010084#define ARM_V7S_PTE_IS_TABLE(pte, lvl) \
85 ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE))
Robin Murphye5fc9752016-01-26 17:13:13 +000086
87/* Page table bits */
88#define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
89#define ARM_V7S_ATTR_B BIT(2)
90#define ARM_V7S_ATTR_C BIT(3)
91#define ARM_V7S_ATTR_NS_TABLE BIT(3)
92#define ARM_V7S_ATTR_NS_SECTION BIT(19)
93
94#define ARM_V7S_CONT_SECTION BIT(18)
95#define ARM_V7S_CONT_PAGE_XN_SHIFT 15
96
97/*
98 * The attribute bits are consistently ordered*, but occupy bits [17:10] of
99 * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
100 * fields relative to that 8-bit block, plus a total shift relative to the PTE.
101 */
102#define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
103
104#define ARM_V7S_ATTR_MASK 0xff
105#define ARM_V7S_ATTR_AP0 BIT(0)
106#define ARM_V7S_ATTR_AP1 BIT(1)
107#define ARM_V7S_ATTR_AP2 BIT(5)
108#define ARM_V7S_ATTR_S BIT(6)
109#define ARM_V7S_ATTR_NG BIT(7)
110#define ARM_V7S_TEX_SHIFT 2
111#define ARM_V7S_TEX_MASK 0x7
112#define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
113
Yong Wu40596d22021-01-11 19:18:51 +0800114/* MediaTek extend the bits below for PA 32bit/33bit/34bit */
Yong Wu4c019de2019-08-24 11:01:54 +0800115#define ARM_V7S_ATTR_MTK_PA_BIT32 BIT(9)
116#define ARM_V7S_ATTR_MTK_PA_BIT33 BIT(4)
Yong Wu40596d22021-01-11 19:18:51 +0800117#define ARM_V7S_ATTR_MTK_PA_BIT34 BIT(5)
Yong Wu1afe2312016-03-14 06:01:10 +0800118
Robin Murphye5fc9752016-01-26 17:13:13 +0000119/* *well, except for TEX on level 2 large pages, of course :( */
120#define ARM_V7S_CONT_PAGE_TEX_SHIFT 6
121#define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT)
122
123/* Simplified access permissions */
124#define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0
125#define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1
126#define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2
127
128/* Register bits */
129#define ARM_V7S_RGN_NC 0
130#define ARM_V7S_RGN_WBWA 1
131#define ARM_V7S_RGN_WT 2
132#define ARM_V7S_RGN_WB 3
133
134#define ARM_V7S_PRRR_TYPE_DEVICE 1
135#define ARM_V7S_PRRR_TYPE_NORMAL 2
136#define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2))
137#define ARM_V7S_PRRR_DS0 BIT(16)
138#define ARM_V7S_PRRR_DS1 BIT(17)
139#define ARM_V7S_PRRR_NS0 BIT(18)
140#define ARM_V7S_PRRR_NS1 BIT(19)
141#define ARM_V7S_PRRR_NOS(n) BIT((n) + 24)
142
143#define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2))
144#define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16))
145
146#define ARM_V7S_TTBR_S BIT(1)
147#define ARM_V7S_TTBR_NOS BIT(5)
148#define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3)
149#define ARM_V7S_TTBR_IRGN_ATTR(attr) \
150 ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1))
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
Yong Wu5950b952019-08-24 11:01:51 +0800172static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl);
173
Robin Murphye5fc9752016-01-26 17:13:13 +0000174static dma_addr_t __arm_v7s_dma_addr(void *pages)
175{
176 return (dma_addr_t)virt_to_phys(pages);
177}
178
Yong Wu4c019de2019-08-24 11:01:54 +0800179static bool arm_v7s_is_mtk_enabled(struct io_pgtable_cfg *cfg)
180{
181 return IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
182 (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_EXT);
183}
184
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800185static arm_v7s_iopte to_mtk_iopte(phys_addr_t paddr, arm_v7s_iopte pte)
Robin Murphye5fc9752016-01-26 17:13:13 +0000186{
Yong Wu4c019de2019-08-24 11:01:54 +0800187 if (paddr & BIT_ULL(32))
188 pte |= ARM_V7S_ATTR_MTK_PA_BIT32;
189 if (paddr & BIT_ULL(33))
190 pte |= ARM_V7S_ATTR_MTK_PA_BIT33;
Yong Wu40596d22021-01-11 19:18:51 +0800191 if (paddr & BIT_ULL(34))
192 pte |= ARM_V7S_ATTR_MTK_PA_BIT34;
Yong Wu4c019de2019-08-24 11:01:54 +0800193 return pte;
Yong Wu5950b952019-08-24 11:01:51 +0800194}
195
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800196static arm_v7s_iopte paddr_to_iopte(phys_addr_t paddr, int lvl,
197 struct io_pgtable_cfg *cfg)
198{
199 arm_v7s_iopte pte = paddr & ARM_V7S_LVL_MASK(lvl);
200
201 if (arm_v7s_is_mtk_enabled(cfg))
202 return to_mtk_iopte(paddr, pte);
203
204 return pte;
205}
206
Yong Wu5950b952019-08-24 11:01:51 +0800207static phys_addr_t iopte_to_paddr(arm_v7s_iopte pte, int lvl,
208 struct io_pgtable_cfg *cfg)
209{
210 arm_v7s_iopte mask;
Yong Wu4c019de2019-08-24 11:01:54 +0800211 phys_addr_t paddr;
Yong Wu5950b952019-08-24 11:01:51 +0800212
Robin Murphye5fc9752016-01-26 17:13:13 +0000213 if (ARM_V7S_PTE_IS_TABLE(pte, lvl))
Yong Wu5950b952019-08-24 11:01:51 +0800214 mask = ARM_V7S_TABLE_MASK;
215 else if (arm_v7s_pte_is_cont(pte, lvl))
216 mask = ARM_V7S_LVL_MASK(lvl) * ARM_V7S_CONT_PAGES;
Robin Murphye5fc9752016-01-26 17:13:13 +0000217 else
Yong Wu5950b952019-08-24 11:01:51 +0800218 mask = ARM_V7S_LVL_MASK(lvl);
219
Yong Wu4c019de2019-08-24 11:01:54 +0800220 paddr = pte & mask;
221 if (!arm_v7s_is_mtk_enabled(cfg))
222 return paddr;
223
224 if (pte & ARM_V7S_ATTR_MTK_PA_BIT32)
225 paddr |= BIT_ULL(32);
226 if (pte & ARM_V7S_ATTR_MTK_PA_BIT33)
227 paddr |= BIT_ULL(33);
Yong Wu40596d22021-01-11 19:18:51 +0800228 if (pte & ARM_V7S_ATTR_MTK_PA_BIT34)
229 paddr |= BIT_ULL(34);
Yong Wu4c019de2019-08-24 11:01:54 +0800230 return paddr;
Yong Wu5950b952019-08-24 11:01:51 +0800231}
232
233static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl,
234 struct arm_v7s_io_pgtable *data)
235{
236 return phys_to_virt(iopte_to_paddr(pte, lvl, &data->iop.cfg));
Robin Murphye5fc9752016-01-26 17:13:13 +0000237}
238
239static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp,
240 struct arm_v7s_io_pgtable *data)
241{
Robin Murphy81b3c252017-06-22 16:53:53 +0100242 struct io_pgtable_cfg *cfg = &data->iop.cfg;
243 struct device *dev = cfg->iommu_dev;
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100244 phys_addr_t phys;
Robin Murphye5fc9752016-01-26 17:13:13 +0000245 dma_addr_t dma;
Yong Wu468ea0b2021-01-11 19:18:53 +0800246 size_t size = ARM_V7S_TABLE_SIZE(lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000247 void *table = NULL;
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800248 gfp_t gfp_l1;
249
250 /*
251 * ARM_MTK_TTBR_EXT extend the translation table base support larger
252 * memory address.
253 */
254 gfp_l1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?
255 GFP_KERNEL : ARM_V7S_TABLE_GFP_DMA;
Robin Murphye5fc9752016-01-26 17:13:13 +0000256
257 if (lvl == 1)
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800258 table = (void *)__get_free_pages(gfp_l1 | __GFP_ZERO, get_order(size));
Robin Murphye5fc9752016-01-26 17:13:13 +0000259 else if (lvl == 2)
Nicolas Boichat0a352552019-03-28 20:43:46 -0700260 table = kmem_cache_zalloc(data->l2_tables, gfp);
Yunfei Wanga556cfe2021-12-07 19:33:15 +0800261
262 if (!table)
263 return NULL;
264
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100265 phys = virt_to_phys(table);
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800266 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?
267 phys >= (1ULL << cfg->oas) : phys != (arm_v7s_iopte)phys) {
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100268 /* Doesn't fit in PTE */
Nicolas Boichat0a352552019-03-28 20:43:46 -0700269 dev_err(dev, "Page table does not fit in PTE: %pa", &phys);
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100270 goto out_free;
Nicolas Boichat0a352552019-03-28 20:43:46 -0700271 }
Yunfei Wanga556cfe2021-12-07 19:33:15 +0800272 if (!cfg->coherent_walk) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000273 dma = dma_map_single(dev, table, size, DMA_TO_DEVICE);
274 if (dma_mapping_error(dev, dma))
275 goto out_free;
276 /*
277 * We depend on the IOMMU being able to work with any physical
278 * address directly, so if the DMA layer suggests otherwise by
279 * translating or truncating them, that bodes very badly...
280 */
Jean-Philippe Brucker29859ae2018-06-19 13:52:24 +0100281 if (dma != phys)
Robin Murphye5fc9752016-01-26 17:13:13 +0000282 goto out_unmap;
283 }
Nicolas Boichat032ebd82019-01-28 17:43:01 +0800284 if (lvl == 2)
285 kmemleak_ignore(table);
Robin Murphye5fc9752016-01-26 17:13:13 +0000286 return table;
287
288out_unmap:
289 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
290 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
291out_free:
292 if (lvl == 1)
293 free_pages((unsigned long)table, get_order(size));
294 else
295 kmem_cache_free(data->l2_tables, table);
296 return NULL;
297}
298
299static void __arm_v7s_free_table(void *table, int lvl,
300 struct arm_v7s_io_pgtable *data)
301{
Robin Murphy81b3c252017-06-22 16:53:53 +0100302 struct io_pgtable_cfg *cfg = &data->iop.cfg;
303 struct device *dev = cfg->iommu_dev;
Yong Wu468ea0b2021-01-11 19:18:53 +0800304 size_t size = ARM_V7S_TABLE_SIZE(lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000305
Will Deacon4f418452019-06-25 12:51:25 +0100306 if (!cfg->coherent_walk)
Robin Murphye5fc9752016-01-26 17:13:13 +0000307 dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
308 DMA_TO_DEVICE);
309 if (lvl == 1)
310 free_pages((unsigned long)table, get_order(size));
311 else
312 kmem_cache_free(data->l2_tables, table);
313}
314
315static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
316 struct io_pgtable_cfg *cfg)
317{
Will Deacon4f418452019-06-25 12:51:25 +0100318 if (cfg->coherent_walk)
Robin Murphye5fc9752016-01-26 17:13:13 +0000319 return;
320
321 dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
322 num_entries * sizeof(*ptep), DMA_TO_DEVICE);
323}
324static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
325 int num_entries, struct io_pgtable_cfg *cfg)
326{
327 int i;
328
329 for (i = 0; i < num_entries; i++)
330 ptep[i] = pte;
331
332 __arm_v7s_pte_sync(ptep, num_entries, cfg);
333}
334
335static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
336 struct io_pgtable_cfg *cfg)
337{
338 bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
Robin Murphye88ccab2016-04-05 12:39:32 +0100339 arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
Robin Murphye5fc9752016-01-26 17:13:13 +0000340
Robin Murphye88ccab2016-04-05 12:39:32 +0100341 if (!(prot & IOMMU_MMIO))
342 pte |= ARM_V7S_ATTR_TEX(1);
Robin Murphye5fc9752016-01-26 17:13:13 +0000343 if (ap) {
Robin Murphy5baf1e92017-01-06 18:58:10 +0530344 pte |= ARM_V7S_PTE_AF;
345 if (!(prot & IOMMU_PRIV))
346 pte |= ARM_V7S_PTE_AP_UNPRIV;
Robin Murphye5fc9752016-01-26 17:13:13 +0000347 if (!(prot & IOMMU_WRITE))
348 pte |= ARM_V7S_PTE_AP_RDONLY;
349 }
350 pte <<= ARM_V7S_ATTR_SHIFT(lvl);
351
352 if ((prot & IOMMU_NOEXEC) && ap)
353 pte |= ARM_V7S_ATTR_XN(lvl);
Robin Murphye88ccab2016-04-05 12:39:32 +0100354 if (prot & IOMMU_MMIO)
355 pte |= ARM_V7S_ATTR_B;
356 else if (prot & IOMMU_CACHE)
Robin Murphye5fc9752016-01-26 17:13:13 +0000357 pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
358
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100359 pte |= ARM_V7S_PTE_TYPE_PAGE;
360 if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
361 pte |= ARM_V7S_ATTR_NS_SECTION;
362
Robin Murphye5fc9752016-01-26 17:13:13 +0000363 return pte;
364}
365
366static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
367{
368 int prot = IOMMU_READ;
Robin Murphye88ccab2016-04-05 12:39:32 +0100369 arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
Robin Murphye5fc9752016-01-26 17:13:13 +0000370
Robin Murphye633fc72016-08-11 17:44:05 +0100371 if (!(attr & ARM_V7S_PTE_AP_RDONLY))
Robin Murphye5fc9752016-01-26 17:13:13 +0000372 prot |= IOMMU_WRITE;
Robin Murphy5baf1e92017-01-06 18:58:10 +0530373 if (!(attr & ARM_V7S_PTE_AP_UNPRIV))
374 prot |= IOMMU_PRIV;
Robin Murphye88ccab2016-04-05 12:39:32 +0100375 if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
376 prot |= IOMMU_MMIO;
377 else if (pte & ARM_V7S_ATTR_C)
Robin Murphye5fc9752016-01-26 17:13:13 +0000378 prot |= IOMMU_CACHE;
Robin Murphye633fc72016-08-11 17:44:05 +0100379 if (pte & ARM_V7S_ATTR_XN(lvl))
380 prot |= IOMMU_NOEXEC;
Robin Murphye5fc9752016-01-26 17:13:13 +0000381
382 return prot;
383}
384
385static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
386{
387 if (lvl == 1) {
388 pte |= ARM_V7S_CONT_SECTION;
389 } else if (lvl == 2) {
390 arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
391 arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
392
393 pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
394 pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
395 (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
396 ARM_V7S_PTE_TYPE_CONT_PAGE;
397 }
398 return pte;
399}
400
401static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
402{
403 if (lvl == 1) {
404 pte &= ~ARM_V7S_CONT_SECTION;
405 } else if (lvl == 2) {
406 arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
407 arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
408 ARM_V7S_CONT_PAGE_TEX_SHIFT);
409
410 pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
411 pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
412 (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
413 ARM_V7S_PTE_TYPE_PAGE;
414 }
415 return pte;
416}
417
418static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
419{
420 if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
421 return pte & ARM_V7S_CONT_SECTION;
422 else if (lvl == 2)
423 return !(pte & ARM_V7S_PTE_TYPE_PAGE);
424 return false;
425}
426
Will Deacon3951c412019-07-02 16:45:15 +0100427static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *,
428 struct iommu_iotlb_gather *, unsigned long,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530429 size_t, int, arm_v7s_iopte *);
Robin Murphye5fc9752016-01-26 17:13:13 +0000430
431static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
432 unsigned long iova, phys_addr_t paddr, int prot,
433 int lvl, int num_entries, arm_v7s_iopte *ptep)
434{
435 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100436 arm_v7s_iopte pte;
Robin Murphye5fc9752016-01-26 17:13:13 +0000437 int i;
438
439 for (i = 0; i < num_entries; i++)
440 if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
441 /*
442 * We need to unmap and free the old table before
443 * overwriting it with a block entry.
444 */
445 arm_v7s_iopte *tblp;
446 size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
447
Yong Wu468ea0b2021-01-11 19:18:53 +0800448 tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl, cfg);
Will Deacon3951c412019-07-02 16:45:15 +0100449 if (WARN_ON(__arm_v7s_unmap(data, NULL, iova + i * sz,
Robin Murphye5fc9752016-01-26 17:13:13 +0000450 sz, lvl, tblp) != sz))
451 return -EINVAL;
452 } else if (ptep[i]) {
453 /* We require an unmap first */
454 WARN_ON(!selftest_running);
455 return -EEXIST;
456 }
457
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100458 pte = arm_v7s_prot_to_pte(prot, lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000459 if (num_entries > 1)
460 pte = arm_v7s_pte_to_cont(pte, lvl);
461
Yong Wu5950b952019-08-24 11:01:51 +0800462 pte |= paddr_to_iopte(paddr, lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000463
464 __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
465 return 0;
466}
467
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100468static arm_v7s_iopte arm_v7s_install_table(arm_v7s_iopte *table,
469 arm_v7s_iopte *ptep,
Robin Murphy119ff302017-06-22 16:53:55 +0100470 arm_v7s_iopte curr,
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100471 struct io_pgtable_cfg *cfg)
472{
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800473 phys_addr_t phys = virt_to_phys(table);
Robin Murphy119ff302017-06-22 16:53:55 +0100474 arm_v7s_iopte old, new;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100475
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800476 new = phys | ARM_V7S_PTE_TYPE_TABLE;
477
478 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT)
479 new = to_mtk_iopte(phys, new);
480
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100481 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
482 new |= ARM_V7S_ATTR_NS_TABLE;
483
Will Deacon77f34452017-06-23 12:02:38 +0100484 /*
485 * Ensure the table itself is visible before its PTE can be.
486 * Whilst we could get away with cmpxchg64_release below, this
487 * doesn't have any ordering semantics when !CONFIG_SMP.
488 */
489 dma_wmb();
Robin Murphy119ff302017-06-22 16:53:55 +0100490
491 old = cmpxchg_relaxed(ptep, curr, new);
492 __arm_v7s_pte_sync(ptep, 1, cfg);
493
494 return old;
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100495}
496
Robin Murphye5fc9752016-01-26 17:13:13 +0000497static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
498 phys_addr_t paddr, size_t size, int prot,
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800499 int lvl, arm_v7s_iopte *ptep, gfp_t gfp)
Robin Murphye5fc9752016-01-26 17:13:13 +0000500{
501 struct io_pgtable_cfg *cfg = &data->iop.cfg;
502 arm_v7s_iopte pte, *cptep;
503 int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
504
505 /* Find our entry at the current level */
Yong Wu468ea0b2021-01-11 19:18:53 +0800506 ptep += ARM_V7S_LVL_IDX(iova, lvl, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000507
508 /* If we can install a leaf entry at this level, then do so */
509 if (num_entries)
510 return arm_v7s_init_pte(data, iova, paddr, prot,
511 lvl, num_entries, ptep);
512
513 /* We can't allocate tables at the final level */
514 if (WARN_ON(lvl == 2))
515 return -EINVAL;
516
517 /* Grab a pointer to the next level */
Robin Murphy119ff302017-06-22 16:53:55 +0100518 pte = READ_ONCE(*ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000519 if (!pte) {
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800520 cptep = __arm_v7s_alloc_table(lvl + 1, gfp, data);
Robin Murphye5fc9752016-01-26 17:13:13 +0000521 if (!cptep)
522 return -ENOMEM;
523
Robin Murphy119ff302017-06-22 16:53:55 +0100524 pte = arm_v7s_install_table(cptep, ptep, 0, cfg);
525 if (pte)
526 __arm_v7s_free_table(cptep, lvl + 1, data);
Oleksandr Tyshchenkoa03849e2017-02-27 14:30:26 +0200527 } else {
Robin Murphy119ff302017-06-22 16:53:55 +0100528 /* We've no easy way of knowing if it's synced yet, so... */
529 __arm_v7s_pte_sync(ptep, 1, cfg);
530 }
531
532 if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
Yong Wu5950b952019-08-24 11:01:51 +0800533 cptep = iopte_deref(pte, lvl, data);
Robin Murphy119ff302017-06-22 16:53:55 +0100534 } else if (pte) {
Oleksandr Tyshchenkoa03849e2017-02-27 14:30:26 +0200535 /* We require an unmap first */
536 WARN_ON(!selftest_running);
537 return -EEXIST;
Robin Murphye5fc9752016-01-26 17:13:13 +0000538 }
539
540 /* Rinse, repeat */
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800541 return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep, gfp);
Robin Murphye5fc9752016-01-26 17:13:13 +0000542}
543
Isaac J. Manjarres23c30be2021-06-16 06:38:54 -0700544static int arm_v7s_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
545 phys_addr_t paddr, size_t pgsize, size_t pgcount,
546 int prot, gfp_t gfp, size_t *mapped)
Robin Murphye5fc9752016-01-26 17:13:13 +0000547{
548 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
Isaac J. Manjarres23c30be2021-06-16 06:38:54 -0700549 int ret = -EINVAL;
Robin Murphye5fc9752016-01-26 17:13:13 +0000550
Yong Wu7f315c92019-08-24 11:01:52 +0800551 if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) ||
552 paddr >= (1ULL << data->iop.cfg.oas)))
Robin Murphy76557392017-07-03 14:52:24 +0100553 return -ERANGE;
554
Keqian Zhuf12e0d22020-12-07 19:57:58 +0800555 if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
Jason Gunthorpe6093cd52024-08-22 11:45:55 -0300556 return -EINVAL;
Keqian Zhuf12e0d22020-12-07 19:57:58 +0800557
Isaac J. Manjarres23c30be2021-06-16 06:38:54 -0700558 while (pgcount--) {
559 ret = __arm_v7s_map(data, iova, paddr, pgsize, prot, 1, data->pgd,
560 gfp);
561 if (ret)
562 break;
563
564 iova += pgsize;
565 paddr += pgsize;
Robin Murphyb9bf41e2022-11-15 15:26:42 +0000566 *mapped += pgsize;
Isaac J. Manjarres23c30be2021-06-16 06:38:54 -0700567 }
Robin Murphye5fc9752016-01-26 17:13:13 +0000568 /*
569 * Synchronise all PTE updates for the new mapping before there's
570 * a chance for anything to kick off a table walk for the new iova.
571 */
Robin Murphy3d5eab42021-01-27 16:29:29 +0000572 wmb();
Robin Murphye5fc9752016-01-26 17:13:13 +0000573
574 return ret;
575}
576
577static void arm_v7s_free_pgtable(struct io_pgtable *iop)
578{
579 struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
580 int i;
581
Yong Wu468ea0b2021-01-11 19:18:53 +0800582 for (i = 0; i < ARM_V7S_PTES_PER_LVL(1, &data->iop.cfg); i++) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000583 arm_v7s_iopte pte = data->pgd[i];
584
585 if (ARM_V7S_PTE_IS_TABLE(pte, 1))
Yong Wu5950b952019-08-24 11:01:51 +0800586 __arm_v7s_free_table(iopte_deref(pte, 1, data),
587 2, data);
Robin Murphye5fc9752016-01-26 17:13:13 +0000588 }
589 __arm_v7s_free_table(data->pgd, 1, data);
590 kmem_cache_destroy(data->l2_tables);
591 kfree(data);
592}
593
Robin Murphy119ff302017-06-22 16:53:55 +0100594static arm_v7s_iopte arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
595 unsigned long iova, int idx, int lvl,
596 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000597{
Robin Murphy507e4c92016-01-26 17:13:14 +0000598 struct io_pgtable *iop = &data->iop;
Robin Murphye5fc9752016-01-26 17:13:13 +0000599 arm_v7s_iopte pte;
600 size_t size = ARM_V7S_BLOCK_SIZE(lvl);
601 int i;
602
Robin Murphy119ff302017-06-22 16:53:55 +0100603 /* Check that we didn't lose a race to get the lock */
604 pte = *ptep;
605 if (!arm_v7s_pte_is_cont(pte, lvl))
606 return pte;
607
Robin Murphye5fc9752016-01-26 17:13:13 +0000608 ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
Robin Murphy119ff302017-06-22 16:53:55 +0100609 pte = arm_v7s_cont_to_pte(pte, lvl);
610 for (i = 0; i < ARM_V7S_CONT_PAGES; i++)
611 ptep[i] = pte + i * size;
Robin Murphye5fc9752016-01-26 17:13:13 +0000612
Robin Murphy507e4c92016-01-26 17:13:14 +0000613 __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000614
615 size *= ARM_V7S_CONT_PAGES;
Robin Murphyfefe8522020-11-25 17:29:39 +0000616 io_pgtable_tlb_flush_walk(iop, iova, size, size);
Robin Murphy119ff302017-06-22 16:53:55 +0100617 return pte;
Robin Murphye5fc9752016-01-26 17:13:13 +0000618}
619
Vivek Gautam193e67c2018-02-05 23:29:19 +0530620static size_t arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100621 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530622 unsigned long iova, size_t size,
623 arm_v7s_iopte blk_pte,
624 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000625{
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100626 struct io_pgtable_cfg *cfg = &data->iop.cfg;
627 arm_v7s_iopte pte, *tablep;
628 int i, unmap_idx, num_entries, num_ptes;
Robin Murphye5fc9752016-01-26 17:13:13 +0000629
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100630 tablep = __arm_v7s_alloc_table(2, GFP_ATOMIC, data);
631 if (!tablep)
632 return 0; /* Bytes unmapped */
Robin Murphye5fc9752016-01-26 17:13:13 +0000633
Yong Wu468ea0b2021-01-11 19:18:53 +0800634 num_ptes = ARM_V7S_PTES_PER_LVL(2, cfg);
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100635 num_entries = size >> ARM_V7S_LVL_SHIFT(2);
Yong Wu468ea0b2021-01-11 19:18:53 +0800636 unmap_idx = ARM_V7S_LVL_IDX(iova, 2, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000637
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100638 pte = arm_v7s_prot_to_pte(arm_v7s_pte_to_prot(blk_pte, 1), 2, cfg);
639 if (num_entries > 1)
640 pte = arm_v7s_pte_to_cont(pte, 2);
641
642 for (i = 0; i < num_ptes; i += num_entries, pte += size) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000643 /* Unmap! */
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100644 if (i == unmap_idx)
Robin Murphye5fc9752016-01-26 17:13:13 +0000645 continue;
646
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100647 __arm_v7s_set_pte(&tablep[i], pte, num_entries, cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000648 }
649
Robin Murphy119ff302017-06-22 16:53:55 +0100650 pte = arm_v7s_install_table(tablep, ptep, blk_pte, cfg);
651 if (pte != blk_pte) {
652 __arm_v7s_free_table(tablep, 2, data);
653
654 if (!ARM_V7S_PTE_IS_TABLE(pte, 1))
655 return 0;
656
Yong Wu5950b952019-08-24 11:01:51 +0800657 tablep = iopte_deref(pte, 1, data);
Will Deacon3951c412019-07-02 16:45:15 +0100658 return __arm_v7s_unmap(data, gather, iova, size, 2, tablep);
Robin Murphy119ff302017-06-22 16:53:55 +0100659 }
Robin Murphyb9f1ef32017-06-22 16:53:52 +0100660
Will Deacon3951c412019-07-02 16:45:15 +0100661 io_pgtable_tlb_add_page(&data->iop, gather, iova, size);
Robin Murphye5fc9752016-01-26 17:13:13 +0000662 return size;
663}
664
Vivek Gautam193e67c2018-02-05 23:29:19 +0530665static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100666 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530667 unsigned long iova, size_t size, int lvl,
668 arm_v7s_iopte *ptep)
Robin Murphye5fc9752016-01-26 17:13:13 +0000669{
670 arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
Robin Murphy507e4c92016-01-26 17:13:14 +0000671 struct io_pgtable *iop = &data->iop;
Robin Murphye5fc9752016-01-26 17:13:13 +0000672 int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
673
674 /* Something went horribly wrong and we ran out of page table */
675 if (WARN_ON(lvl > 2))
676 return 0;
677
Yong Wu468ea0b2021-01-11 19:18:53 +0800678 idx = ARM_V7S_LVL_IDX(iova, lvl, &iop->cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000679 ptep += idx;
680 do {
Robin Murphy119ff302017-06-22 16:53:55 +0100681 pte[i] = READ_ONCE(ptep[i]);
682 if (WARN_ON(!ARM_V7S_PTE_IS_VALID(pte[i])))
Robin Murphye5fc9752016-01-26 17:13:13 +0000683 return 0;
Robin Murphye5fc9752016-01-26 17:13:13 +0000684 } while (++i < num_entries);
685
686 /*
687 * If we've hit a contiguous 'large page' entry at this level, it
688 * needs splitting first, unless we're unmapping the whole lot.
Robin Murphy119ff302017-06-22 16:53:55 +0100689 *
690 * For splitting, we can't rewrite 16 PTEs atomically, and since we
691 * can't necessarily assume TEX remap we don't have a software bit to
692 * mark live entries being split. In practice (i.e. DMA API code), we
693 * will never be splitting large pages anyway, so just wrap this edge
694 * case in a lock for the sake of correctness and be done with it.
Robin Murphye5fc9752016-01-26 17:13:13 +0000695 */
Robin Murphy119ff302017-06-22 16:53:55 +0100696 if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl)) {
697 unsigned long flags;
698
699 spin_lock_irqsave(&data->split_lock, flags);
700 pte[0] = arm_v7s_split_cont(data, iova, idx, lvl, ptep);
701 spin_unlock_irqrestore(&data->split_lock, flags);
702 }
Robin Murphye5fc9752016-01-26 17:13:13 +0000703
704 /* If the size matches this level, we're in the right place */
705 if (num_entries) {
706 size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
707
Robin Murphy507e4c92016-01-26 17:13:14 +0000708 __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
Robin Murphye5fc9752016-01-26 17:13:13 +0000709
710 for (i = 0; i < num_entries; i++) {
711 if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
712 /* Also flush any partial walks */
Will Deacon10b7a7d2019-07-02 16:44:32 +0100713 io_pgtable_tlb_flush_walk(iop, iova, blk_size,
714 ARM_V7S_BLOCK_SIZE(lvl + 1));
Yong Wu5950b952019-08-24 11:01:51 +0800715 ptep = iopte_deref(pte[i], lvl, data);
Robin Murphye5fc9752016-01-26 17:13:13 +0000716 __arm_v7s_free_table(ptep, lvl + 1, data);
Robin Murphyf7403ab2021-08-20 14:14:42 +0100717 } else if (!iommu_iotlb_gather_queued(gather)) {
Will Deacon3951c412019-07-02 16:45:15 +0100718 io_pgtable_tlb_add_page(iop, gather, iova, blk_size);
Robin Murphye5fc9752016-01-26 17:13:13 +0000719 }
720 iova += blk_size;
721 }
722 return size;
723 } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
724 /*
725 * Insert a table at the next level to map the old region,
726 * minus the part we want to unmap
727 */
Will Deacon3951c412019-07-02 16:45:15 +0100728 return arm_v7s_split_blk_unmap(data, gather, iova, size, pte[0],
729 ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000730 }
731
732 /* Keep on walkin' */
Yong Wu5950b952019-08-24 11:01:51 +0800733 ptep = iopte_deref(pte[0], lvl, data);
Will Deacon3951c412019-07-02 16:45:15 +0100734 return __arm_v7s_unmap(data, gather, iova, size, lvl + 1, ptep);
Robin Murphye5fc9752016-01-26 17:13:13 +0000735}
736
Isaac J. Manjarresf13eabc2021-06-16 06:38:53 -0700737static size_t arm_v7s_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
738 size_t pgsize, size_t pgcount,
739 struct iommu_iotlb_gather *gather)
Robin Murphye5fc9752016-01-26 17:13:13 +0000740{
Robin Murphye5fc9752016-01-26 17:13:13 +0000741 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
Isaac J. Manjarresf13eabc2021-06-16 06:38:53 -0700742 size_t unmapped = 0, ret;
Robin Murphye5fc9752016-01-26 17:13:13 +0000743
Yong Wu859da212021-01-11 19:18:50 +0800744 if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias)))
Robin Murphy76557392017-07-03 14:52:24 +0100745 return 0;
746
Isaac J. Manjarresf13eabc2021-06-16 06:38:53 -0700747 while (pgcount--) {
748 ret = __arm_v7s_unmap(data, gather, iova, pgsize, 1, data->pgd);
749 if (!ret)
750 break;
751
752 unmapped += pgsize;
753 iova += pgsize;
754 }
755
756 return unmapped;
757}
758
Robin Murphye5fc9752016-01-26 17:13:13 +0000759static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
760 unsigned long iova)
761{
762 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
763 arm_v7s_iopte *ptep = data->pgd, pte;
764 int lvl = 0;
765 u32 mask;
766
767 do {
Yong Wu468ea0b2021-01-11 19:18:53 +0800768 ptep += ARM_V7S_LVL_IDX(iova, ++lvl, &data->iop.cfg);
Robin Murphy119ff302017-06-22 16:53:55 +0100769 pte = READ_ONCE(*ptep);
Yong Wu5950b952019-08-24 11:01:51 +0800770 ptep = iopte_deref(pte, lvl, data);
Robin Murphye5fc9752016-01-26 17:13:13 +0000771 } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
772
773 if (!ARM_V7S_PTE_IS_VALID(pte))
774 return 0;
775
776 mask = ARM_V7S_LVL_MASK(lvl);
777 if (arm_v7s_pte_is_cont(pte, lvl))
778 mask *= ARM_V7S_CONT_PAGES;
Yong Wu5950b952019-08-24 11:01:51 +0800779 return iopte_to_paddr(pte, lvl, &data->iop.cfg) | (iova & ~mask);
Robin Murphye5fc9752016-01-26 17:13:13 +0000780}
781
782static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
783 void *cookie)
784{
785 struct arm_v7s_io_pgtable *data;
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800786 slab_flags_t slab_flag;
787 phys_addr_t paddr;
Robin Murphye5fc9752016-01-26 17:13:13 +0000788
Yong Wuf3a8a46d2021-01-11 19:18:54 +0800789 if (cfg->ias > (arm_v7s_is_mtk_enabled(cfg) ? 34 : ARM_V7S_ADDR_BITS))
Yong Wu4c019de2019-08-24 11:01:54 +0800790 return NULL;
791
Yong Wu40596d22021-01-11 19:18:51 +0800792 if (cfg->oas > (arm_v7s_is_mtk_enabled(cfg) ? 35 : ARM_V7S_ADDR_BITS))
Robin Murphye5fc9752016-01-26 17:13:13 +0000793 return NULL;
794
Robin Murphy3850db42016-02-12 17:09:46 +0000795 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
796 IO_PGTABLE_QUIRK_NO_PERMS |
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800797 IO_PGTABLE_QUIRK_ARM_MTK_EXT |
798 IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT))
Robin Murphy3850db42016-02-12 17:09:46 +0000799 return NULL;
800
Yong Wu1afe2312016-03-14 06:01:10 +0800801 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
Yong Wu73d50812019-08-24 11:01:53 +0800802 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_EXT &&
Yong Wu1afe2312016-03-14 06:01:10 +0800803 !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
804 return NULL;
805
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800806 if ((cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT) &&
807 !arm_v7s_is_mtk_enabled(cfg))
808 return NULL;
809
Robin Murphye5fc9752016-01-26 17:13:13 +0000810 data = kmalloc(sizeof(*data), GFP_KERNEL);
811 if (!data)
812 return NULL;
813
Robin Murphy119ff302017-06-22 16:53:55 +0100814 spin_lock_init(&data->split_lock);
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800815
816 /*
817 * ARM_MTK_TTBR_EXT extend the translation table base support larger
818 * memory address.
819 */
820 slab_flag = cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT ?
821 0 : ARM_V7S_TABLE_SLAB_FLAGS;
822
Robin Murphye5fc9752016-01-26 17:13:13 +0000823 data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
Yong Wu468ea0b2021-01-11 19:18:53 +0800824 ARM_V7S_TABLE_SIZE(2, cfg),
825 ARM_V7S_TABLE_SIZE(2, cfg),
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800826 slab_flag, NULL);
Robin Murphye5fc9752016-01-26 17:13:13 +0000827 if (!data->l2_tables)
828 goto out_free_data;
829
830 data->iop.ops = (struct io_pgtable_ops) {
Isaac J. Manjarres23c30be2021-06-16 06:38:54 -0700831 .map_pages = arm_v7s_map_pages,
Isaac J. Manjarresf13eabc2021-06-16 06:38:53 -0700832 .unmap_pages = arm_v7s_unmap_pages,
Robin Murphye5fc9752016-01-26 17:13:13 +0000833 .iova_to_phys = arm_v7s_iova_to_phys,
834 };
835
836 /* We have to do this early for __arm_v7s_alloc_table to work... */
837 data->iop.cfg = *cfg;
838
839 /*
840 * Unless the IOMMU driver indicates supersection support by
841 * having SZ_16M set in the initial bitmap, they won't be used.
842 */
843 cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
844
Robin Murphyfb485eb2019-10-25 19:08:38 +0100845 /* TCR: T0SZ=0, EAE=0 (if applicable) */
846 cfg->arm_v7s_cfg.tcr = 0;
Robin Murphye5fc9752016-01-26 17:13:13 +0000847
848 /*
849 * TEX remap: the indices used map to the closest equivalent types
850 * under the non-TEX-remap interpretation of those attribute bits,
851 * excepting various implementation-defined aspects of shareability.
852 */
853 cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
854 ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
855 ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
856 ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
857 ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
858 cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
859 ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
860
861 /* Looking good; allocate a pgd */
862 data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
863 if (!data->pgd)
864 goto out_free_data;
865
866 /* Ensure the empty pgd is visible before any actual TTBR write */
867 wmb();
868
Robin Murphyd1e5f262019-10-25 19:08:37 +0100869 /* TTBR */
Yunfei Wangbfdd2312022-06-30 17:29:25 +0800870 paddr = virt_to_phys(data->pgd);
871 if (arm_v7s_is_mtk_enabled(cfg))
872 cfg->arm_v7s_cfg.ttbr = paddr | upper_32_bits(paddr);
873 else
874 cfg->arm_v7s_cfg.ttbr = paddr | ARM_V7S_TTBR_S |
875 (cfg->coherent_walk ? (ARM_V7S_TTBR_NOS |
876 ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
877 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA)) :
878 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC) |
879 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC)));
Robin Murphye5fc9752016-01-26 17:13:13 +0000880 return &data->iop;
881
882out_free_data:
883 kmem_cache_destroy(data->l2_tables);
884 kfree(data);
885 return NULL;
886}
887
888struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
889 .alloc = arm_v7s_alloc_pgtable,
890 .free = arm_v7s_free_pgtable,
891};
892
893#ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
894
Robin Murphyb5813c12019-10-25 19:08:30 +0100895static struct io_pgtable_cfg *cfg_cookie __initdata;
Robin Murphye5fc9752016-01-26 17:13:13 +0000896
Robin Murphyb5813c12019-10-25 19:08:30 +0100897static void __init dummy_tlb_flush_all(void *cookie)
Robin Murphye5fc9752016-01-26 17:13:13 +0000898{
899 WARN_ON(cookie != cfg_cookie);
900}
901
Robin Murphyb5813c12019-10-25 19:08:30 +0100902static void __init dummy_tlb_flush(unsigned long iova, size_t size,
903 size_t granule, void *cookie)
Robin Murphye5fc9752016-01-26 17:13:13 +0000904{
905 WARN_ON(cookie != cfg_cookie);
906 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
907}
908
Robin Murphyb5813c12019-10-25 19:08:30 +0100909static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
910 unsigned long iova, size_t granule,
911 void *cookie)
Robin Murphye5fc9752016-01-26 17:13:13 +0000912{
Will Deaconabfd6fe2019-07-02 16:44:41 +0100913 dummy_tlb_flush(iova, granule, granule, cookie);
Robin Murphye5fc9752016-01-26 17:13:13 +0000914}
915
Robin Murphyb5813c12019-10-25 19:08:30 +0100916static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
Robin Murphye5fc9752016-01-26 17:13:13 +0000917 .tlb_flush_all = dummy_tlb_flush_all,
Will Deacon10b7a7d2019-07-02 16:44:32 +0100918 .tlb_flush_walk = dummy_tlb_flush,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100919 .tlb_add_page = dummy_tlb_add_page,
Robin Murphye5fc9752016-01-26 17:13:13 +0000920};
921
922#define __FAIL(ops) ({ \
923 WARN(1, "selftest: test failed\n"); \
924 selftest_running = false; \
925 -EFAULT; \
926})
927
928static int __init arm_v7s_do_selftests(void)
929{
930 struct io_pgtable_ops *ops;
931 struct io_pgtable_cfg cfg = {
932 .tlb = &dummy_tlb_ops,
933 .oas = 32,
934 .ias = 32,
Will Deacon4f418452019-06-25 12:51:25 +0100935 .coherent_walk = true,
936 .quirks = IO_PGTABLE_QUIRK_ARM_NS,
Robin Murphye5fc9752016-01-26 17:13:13 +0000937 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
938 };
939 unsigned int iova, size, iova_start;
940 unsigned int i, loopnr = 0;
Robin Murphyb9bf41e2022-11-15 15:26:42 +0000941 size_t mapped;
Robin Murphye5fc9752016-01-26 17:13:13 +0000942
943 selftest_running = true;
944
945 cfg_cookie = &cfg;
946
947 ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
948 if (!ops) {
949 pr_err("selftest: failed to allocate io pgtable ops\n");
950 return -EINVAL;
951 }
952
953 /*
954 * Initial sanity checks.
955 * Empty page tables shouldn't provide any translations.
956 */
957 if (ops->iova_to_phys(ops, 42))
958 return __FAIL(ops);
959
960 if (ops->iova_to_phys(ops, SZ_1G + 42))
961 return __FAIL(ops);
962
963 if (ops->iova_to_phys(ops, SZ_2G + 42))
964 return __FAIL(ops);
965
966 /*
967 * Distinct mappings of different granule sizes.
968 */
969 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +0800970 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
Robin Murphye5fc9752016-01-26 17:13:13 +0000971 size = 1UL << i;
Robin Murphyb9bf41e2022-11-15 15:26:42 +0000972 if (ops->map_pages(ops, iova, iova, size, 1,
973 IOMMU_READ | IOMMU_WRITE |
974 IOMMU_NOEXEC | IOMMU_CACHE,
975 GFP_KERNEL, &mapped))
Robin Murphye5fc9752016-01-26 17:13:13 +0000976 return __FAIL(ops);
977
978 /* Overlapping mappings */
Robin Murphyb9bf41e2022-11-15 15:26:42 +0000979 if (!ops->map_pages(ops, iova, iova + size, size, 1,
980 IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL,
981 &mapped))
Robin Murphye5fc9752016-01-26 17:13:13 +0000982 return __FAIL(ops);
983
984 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
985 return __FAIL(ops);
986
987 iova += SZ_16M;
Robin Murphye5fc9752016-01-26 17:13:13 +0000988 loopnr++;
989 }
990
991 /* Partial unmap */
992 i = 1;
993 size = 1UL << __ffs(cfg.pgsize_bitmap);
994 while (i < loopnr) {
995 iova_start = i * SZ_16M;
Robin Murphyb9bf41e2022-11-15 15:26:42 +0000996 if (ops->unmap_pages(ops, iova_start + size, size, 1, NULL) != size)
Robin Murphye5fc9752016-01-26 17:13:13 +0000997 return __FAIL(ops);
998
999 /* Remap of partial unmap */
Robin Murphyb9bf41e2022-11-15 15:26:42 +00001000 if (ops->map_pages(ops, iova_start + size, size, size, 1,
1001 IOMMU_READ, GFP_KERNEL, &mapped))
Robin Murphye5fc9752016-01-26 17:13:13 +00001002 return __FAIL(ops);
1003
1004 if (ops->iova_to_phys(ops, iova_start + size + 42)
1005 != (size + 42))
1006 return __FAIL(ops);
1007 i++;
1008 }
1009
1010 /* Full unmap */
1011 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +08001012 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
Robin Murphye5fc9752016-01-26 17:13:13 +00001013 size = 1UL << i;
1014
Robin Murphyb9bf41e2022-11-15 15:26:42 +00001015 if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
Robin Murphye5fc9752016-01-26 17:13:13 +00001016 return __FAIL(ops);
1017
1018 if (ops->iova_to_phys(ops, iova + 42))
1019 return __FAIL(ops);
1020
1021 /* Remap full block */
Robin Murphyb9bf41e2022-11-15 15:26:42 +00001022 if (ops->map_pages(ops, iova, iova, size, 1, IOMMU_WRITE,
1023 GFP_KERNEL, &mapped))
Robin Murphye5fc9752016-01-26 17:13:13 +00001024 return __FAIL(ops);
1025
1026 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1027 return __FAIL(ops);
1028
1029 iova += SZ_16M;
Robin Murphye5fc9752016-01-26 17:13:13 +00001030 }
1031
1032 free_io_pgtable_ops(ops);
1033
1034 selftest_running = false;
1035
1036 pr_info("self test ok\n");
1037 return 0;
1038}
1039subsys_initcall(arm_v7s_do_selftests);
1040#endif