blob: ab440b52a5f45ddb89de624302e0015ffb523c79 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Will Deacone1d3c0f2014-11-14 17:18:23 +00002/*
3 * CPU-agnostic ARM page table allocator.
4 *
Will Deacone1d3c0f2014-11-14 17:18:23 +00005 * Copyright (C) 2014 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
10#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
11
Robin Murphy2c3d2732017-06-22 16:53:54 +010012#include <linux/atomic.h>
Robin Murphy6c899282018-03-26 13:35:13 +010013#include <linux/bitops.h>
Rob Herringb77cf112019-02-05 10:37:31 -060014#include <linux/io-pgtable.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000015#include <linux/kernel.h>
16#include <linux/sizes.h>
17#include <linux/slab.h>
18#include <linux/types.h>
Lada Trimasova8f6aff92016-01-27 11:10:32 +000019#include <linux/dma-mapping.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000020
Robin Murphy87a91b12015-07-29 19:46:09 +010021#include <asm/barrier.h>
22
Robin Murphy6c899282018-03-26 13:35:13 +010023#define ARM_LPAE_MAX_ADDR_BITS 52
Will Deacone1d3c0f2014-11-14 17:18:23 +000024#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
25#define ARM_LPAE_MAX_LEVELS 4
26
27/* Struct accessors */
28#define io_pgtable_to_data(x) \
29 container_of((x), struct arm_lpae_io_pgtable, iop)
30
Will Deacone1d3c0f2014-11-14 17:18:23 +000031#define io_pgtable_ops_to_data(x) \
32 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
33
34/*
Will Deacone1d3c0f2014-11-14 17:18:23 +000035 * Calculate the right shift amount to get to the portion describing level l
36 * in a virtual address mapped by the pagetable in d.
37 */
38#define ARM_LPAE_LVL_SHIFT(l,d) \
Robin Murphy5fb190b2019-10-25 19:08:35 +010039 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
40 ilog2(sizeof(arm_lpae_iopte)))
Will Deacone1d3c0f2014-11-14 17:18:23 +000041
Robin Murphy5fb190b2019-10-25 19:08:35 +010042#define ARM_LPAE_GRANULE(d) \
43 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
Robin Murphyc79278c2019-10-25 19:08:34 +010044#define ARM_LPAE_PGD_SIZE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
Will Deacone1d3c0f2014-11-14 17:18:23 +000046
47/*
48 * Calculate the index at level l used to map virtual address a using the
49 * pagetable in d.
50 */
51#define ARM_LPAE_PGD_IDX(l,d) \
Robin Murphyc79278c2019-10-25 19:08:34 +010052 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
Will Deacone1d3c0f2014-11-14 17:18:23 +000053
54#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000055 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000056 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
57
58/* Calculate the block/page mapping size at level l for pagetable in d. */
Robin Murphy5fb190b2019-10-25 19:08:35 +010059#define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000060
61/* Page table bits */
62#define ARM_LPAE_PTE_TYPE_SHIFT 0
63#define ARM_LPAE_PTE_TYPE_MASK 0x3
64
65#define ARM_LPAE_PTE_TYPE_BLOCK 1
66#define ARM_LPAE_PTE_TYPE_TABLE 3
67#define ARM_LPAE_PTE_TYPE_PAGE 3
68
Robin Murphy6c899282018-03-26 13:35:13 +010069#define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
70
Laurent Pinchartc896c1322014-12-14 23:34:50 +020071#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000072#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
73#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
74#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
75#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
76#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c1322014-12-14 23:34:50 +020077#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000078#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
79
80#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
81/* Ignore the contiguous bit for block splitting */
82#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
83#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
84 ARM_LPAE_PTE_ATTR_HI_MASK)
Robin Murphy2c3d2732017-06-22 16:53:54 +010085/* Software bit for solving coherency races */
86#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
Will Deacone1d3c0f2014-11-14 17:18:23 +000087
88/* Stage-1 PTE */
89#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
90#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
91#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
92#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
93
94/* Stage-2 PTE */
95#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
96#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
97#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
98#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
99#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
100#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
101
102/* Register bits */
103#define ARM_32_LPAE_TCR_EAE (1 << 31)
104#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
105
Will Deacon63979b82015-03-18 10:22:18 +0000106#define ARM_LPAE_TCR_EPD1 (1 << 23)
107
Will Deacone1d3c0f2014-11-14 17:18:23 +0000108#define ARM_LPAE_TCR_TG0_4K (0 << 14)
109#define ARM_LPAE_TCR_TG0_64K (1 << 14)
110#define ARM_LPAE_TCR_TG0_16K (2 << 14)
111
112#define ARM_LPAE_TCR_SH0_SHIFT 12
113#define ARM_LPAE_TCR_SH0_MASK 0x3
114#define ARM_LPAE_TCR_SH_NS 0
115#define ARM_LPAE_TCR_SH_OS 2
116#define ARM_LPAE_TCR_SH_IS 3
117
118#define ARM_LPAE_TCR_ORGN0_SHIFT 10
119#define ARM_LPAE_TCR_IRGN0_SHIFT 8
120#define ARM_LPAE_TCR_RGN_MASK 0x3
121#define ARM_LPAE_TCR_RGN_NC 0
122#define ARM_LPAE_TCR_RGN_WBWA 1
123#define ARM_LPAE_TCR_RGN_WT 2
124#define ARM_LPAE_TCR_RGN_WB 3
125
126#define ARM_LPAE_TCR_SL0_SHIFT 6
127#define ARM_LPAE_TCR_SL0_MASK 0x3
128
129#define ARM_LPAE_TCR_T0SZ_SHIFT 0
130#define ARM_LPAE_TCR_SZ_MASK 0xf
131
132#define ARM_LPAE_TCR_PS_SHIFT 16
133#define ARM_LPAE_TCR_PS_MASK 0x7
134
135#define ARM_LPAE_TCR_IPS_SHIFT 32
136#define ARM_LPAE_TCR_IPS_MASK 0x7
137
138#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
139#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
140#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
141#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
142#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
143#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
Robin Murphy6c899282018-03-26 13:35:13 +0100144#define ARM_LPAE_TCR_PS_52_BIT 0x6ULL
Will Deacone1d3c0f2014-11-14 17:18:23 +0000145
146#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
147#define ARM_LPAE_MAIR_ATTR_MASK 0xff
148#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
149#define ARM_LPAE_MAIR_ATTR_NC 0x44
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530150#define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000151#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
152#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
153#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
154#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530155#define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000156
Rob Herringd08d42d2019-02-21 14:23:25 -0600157#define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
158#define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
159#define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
160
Robin Murphy52f325f2019-09-30 15:11:00 +0100161#define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
162#define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
163
Will Deacone1d3c0f2014-11-14 17:18:23 +0000164/* IOPTE accessors */
Robin Murphy6c899282018-03-26 13:35:13 +0100165#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000166
167#define iopte_type(pte,l) \
168 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
169
170#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
171
Will Deacone1d3c0f2014-11-14 17:18:23 +0000172struct arm_lpae_io_pgtable {
173 struct io_pgtable iop;
174
Robin Murphyc79278c2019-10-25 19:08:34 +0100175 int pgd_bits;
Robin Murphy594ab902019-10-25 19:08:33 +0100176 int start_level;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100177 int bits_per_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000178
179 void *pgd;
180};
181
182typedef u64 arm_lpae_iopte;
183
Rob Herringd08d42d2019-02-21 14:23:25 -0600184static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
185 enum io_pgtable_fmt fmt)
186{
187 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
188 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE;
189
190 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK;
191}
192
Robin Murphy6c899282018-03-26 13:35:13 +0100193static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
194 struct arm_lpae_io_pgtable *data)
195{
196 arm_lpae_iopte pte = paddr;
197
198 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
199 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
200}
201
202static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
203 struct arm_lpae_io_pgtable *data)
204{
Robin Murphy78688052018-03-29 12:24:52 +0100205 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
Robin Murphy6c899282018-03-26 13:35:13 +0100206
Robin Murphy5fb190b2019-10-25 19:08:35 +0100207 if (ARM_LPAE_GRANULE(data) < SZ_64K)
Robin Murphy6c899282018-03-26 13:35:13 +0100208 return paddr;
209
210 /* Rotate the packed high-order bits back to the top */
211 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
212}
213
Will Deaconfe4b9912014-11-17 23:31:12 +0000214static bool selftest_running = false;
215
Robin Murphyffcb6d12015-09-17 17:42:16 +0100216static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100217{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100218 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100219}
220
221static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
222 struct io_pgtable_cfg *cfg)
223{
224 struct device *dev = cfg->iommu_dev;
Robin Murphy4b123752018-05-22 12:50:09 +0100225 int order = get_order(size);
226 struct page *p;
Robin Murphyf8d54962015-07-29 19:46:04 +0100227 dma_addr_t dma;
Robin Murphy4b123752018-05-22 12:50:09 +0100228 void *pages;
Robin Murphyf8d54962015-07-29 19:46:04 +0100229
Robin Murphy4b123752018-05-22 12:50:09 +0100230 VM_BUG_ON((gfp & __GFP_HIGHMEM));
Jean-Philippe Bruckerfac83d22018-06-18 12:27:54 +0100231 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
232 gfp | __GFP_ZERO, order);
Robin Murphy4b123752018-05-22 12:50:09 +0100233 if (!p)
Robin Murphyf8d54962015-07-29 19:46:04 +0100234 return NULL;
235
Robin Murphy4b123752018-05-22 12:50:09 +0100236 pages = page_address(p);
Will Deacon4f418452019-06-25 12:51:25 +0100237 if (!cfg->coherent_walk) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100238 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
239 if (dma_mapping_error(dev, dma))
240 goto out_free;
241 /*
242 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100243 * address directly, so if the DMA layer suggests otherwise by
244 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100245 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100246 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100247 goto out_unmap;
248 }
249
250 return pages;
251
252out_unmap:
253 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
254 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
255out_free:
Robin Murphy4b123752018-05-22 12:50:09 +0100256 __free_pages(p, order);
Robin Murphyf8d54962015-07-29 19:46:04 +0100257 return NULL;
258}
259
260static void __arm_lpae_free_pages(void *pages, size_t size,
261 struct io_pgtable_cfg *cfg)
262{
Will Deacon4f418452019-06-25 12:51:25 +0100263 if (!cfg->coherent_walk)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100264 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100265 size, DMA_TO_DEVICE);
Robin Murphy4b123752018-05-22 12:50:09 +0100266 free_pages((unsigned long)pages, get_order(size));
Robin Murphyf8d54962015-07-29 19:46:04 +0100267}
268
Robin Murphy2c3d2732017-06-22 16:53:54 +0100269static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
270 struct io_pgtable_cfg *cfg)
271{
272 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
273 sizeof(*ptep), DMA_TO_DEVICE);
274}
275
Robin Murphyf8d54962015-07-29 19:46:04 +0100276static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100277 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100278{
Robin Murphyf8d54962015-07-29 19:46:04 +0100279 *ptep = pte;
280
Will Deacon4f418452019-06-25 12:51:25 +0100281 if (!cfg->coherent_walk)
Robin Murphy2c3d2732017-06-22 16:53:54 +0100282 __arm_lpae_sync_pte(ptep, cfg);
Robin Murphyf8d54962015-07-29 19:46:04 +0100283}
284
Vivek Gautam193e67c2018-02-05 23:29:19 +0530285static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100286 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530287 unsigned long iova, size_t size, int lvl,
288 arm_lpae_iopte *ptep);
Will Deaconcf27ec92015-08-11 16:48:32 +0100289
Robin Murphyfb3a9572017-06-22 16:53:51 +0100290static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
291 phys_addr_t paddr, arm_lpae_iopte prot,
292 int lvl, arm_lpae_iopte *ptep)
293{
294 arm_lpae_iopte pte = prot;
295
296 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
297 pte |= ARM_LPAE_PTE_NS;
298
Rob Herringd08d42d2019-02-21 14:23:25 -0600299 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100300 pte |= ARM_LPAE_PTE_TYPE_PAGE;
301 else
302 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
303
Rob Herringd08d42d2019-02-21 14:23:25 -0600304 if (data->iop.fmt != ARM_MALI_LPAE)
305 pte |= ARM_LPAE_PTE_AF;
306 pte |= ARM_LPAE_PTE_SH_IS;
Robin Murphy6c899282018-03-26 13:35:13 +0100307 pte |= paddr_to_iopte(paddr, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100308
309 __arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
310}
311
Will Deacone1d3c0f2014-11-14 17:18:23 +0000312static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
313 unsigned long iova, phys_addr_t paddr,
314 arm_lpae_iopte prot, int lvl,
315 arm_lpae_iopte *ptep)
316{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100317 arm_lpae_iopte pte = *ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000318
Rob Herringd08d42d2019-02-21 14:23:25 -0600319 if (iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100320 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000321 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000322 return -EEXIST;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100323 } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100324 /*
325 * We need to unmap and free the old table before
326 * overwriting it with a block entry.
327 */
328 arm_lpae_iopte *tblp;
329 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
330
331 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
Will Deacon3951c412019-07-02 16:45:15 +0100332 if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) {
333 WARN_ON(1);
Will Deaconcf27ec92015-08-11 16:48:32 +0100334 return -EINVAL;
Will Deacon3951c412019-07-02 16:45:15 +0100335 }
Will Deaconfe4b9912014-11-17 23:31:12 +0000336 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000337
Robin Murphyfb3a9572017-06-22 16:53:51 +0100338 __arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000339 return 0;
340}
341
Robin Murphyfb3a9572017-06-22 16:53:51 +0100342static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
343 arm_lpae_iopte *ptep,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100344 arm_lpae_iopte curr,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100345 struct io_pgtable_cfg *cfg)
346{
Robin Murphy2c3d2732017-06-22 16:53:54 +0100347 arm_lpae_iopte old, new;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100348
349 new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
350 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
351 new |= ARM_LPAE_PTE_NSTABLE;
352
Will Deacon77f34452017-06-23 12:02:38 +0100353 /*
354 * Ensure the table itself is visible before its PTE can be.
355 * Whilst we could get away with cmpxchg64_release below, this
356 * doesn't have any ordering semantics when !CONFIG_SMP.
357 */
358 dma_wmb();
Robin Murphy2c3d2732017-06-22 16:53:54 +0100359
360 old = cmpxchg64_relaxed(ptep, curr, new);
361
Will Deacon4f418452019-06-25 12:51:25 +0100362 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
Robin Murphy2c3d2732017-06-22 16:53:54 +0100363 return old;
364
365 /* Even if it's not ours, there's no point waiting; just kick it */
366 __arm_lpae_sync_pte(ptep, cfg);
367 if (old == curr)
368 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
369
370 return old;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100371}
372
Will Deacone1d3c0f2014-11-14 17:18:23 +0000373static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
374 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
375 int lvl, arm_lpae_iopte *ptep)
376{
377 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000378 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100379 size_t tblsz = ARM_LPAE_GRANULE(data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100380 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000381
382 /* Find our entry at the current level */
383 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
384
385 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf7b90d22019-10-25 19:08:31 +0100386 if (size == block_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000387 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
388
389 /* We can't allocate tables at the final level */
390 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
391 return -EINVAL;
392
393 /* Grab a pointer to the next level */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100394 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000395 if (!pte) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100396 cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000397 if (!cptep)
398 return -ENOMEM;
399
Robin Murphy2c3d2732017-06-22 16:53:54 +0100400 pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
401 if (pte)
402 __arm_lpae_free_pages(cptep, tblsz, cfg);
Will Deacon4f418452019-06-25 12:51:25 +0100403 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100404 __arm_lpae_sync_pte(ptep, cfg);
405 }
406
Rob Herringd08d42d2019-02-21 14:23:25 -0600407 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000408 cptep = iopte_deref(pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100409 } else if (pte) {
Oleksandr Tyshchenkoed46e662017-02-27 14:30:25 +0200410 /* We require an unmap first */
411 WARN_ON(!selftest_running);
412 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000413 }
414
415 /* Rinse, repeat */
416 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
417}
418
419static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
420 int prot)
421{
422 arm_lpae_iopte pte;
423
424 if (data->iop.fmt == ARM_64_LPAE_S1 ||
425 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530426 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000427 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
428 pte |= ARM_LPAE_PTE_AP_RDONLY;
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530429 if (!(prot & IOMMU_PRIV))
430 pte |= ARM_LPAE_PTE_AP_UNPRIV;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000431 } else {
432 pte = ARM_LPAE_PTE_HAP_FAULT;
433 if (prot & IOMMU_READ)
434 pte |= ARM_LPAE_PTE_HAP_READ;
435 if (prot & IOMMU_WRITE)
436 pte |= ARM_LPAE_PTE_HAP_WRITE;
Rob Herringd08d42d2019-02-21 14:23:25 -0600437 }
438
439 /*
440 * Note that this logic is structured to accommodate Mali LPAE
441 * having stage-1-like attributes but stage-2-like permissions.
442 */
443 if (data->iop.fmt == ARM_64_LPAE_S2 ||
444 data->iop.fmt == ARM_32_LPAE_S2) {
Robin Murphyfb948252016-04-05 12:39:31 +0100445 if (prot & IOMMU_MMIO)
446 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
447 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000448 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
449 else
450 pte |= ARM_LPAE_PTE_MEMATTR_NC;
Rob Herringd08d42d2019-02-21 14:23:25 -0600451 } else {
452 if (prot & IOMMU_MMIO)
453 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
454 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
455 else if (prot & IOMMU_CACHE)
456 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
457 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacondd5ddd32019-10-24 16:57:39 +0100458 else if (prot & IOMMU_SYS_CACHE_ONLY)
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530459 pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
460 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000461 }
462
463 if (prot & IOMMU_NOEXEC)
464 pte |= ARM_LPAE_PTE_XN;
465
466 return pte;
467}
468
469static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
470 phys_addr_t paddr, size_t size, int iommu_prot)
471{
472 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100473 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000474 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100475 int ret, lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000476 arm_lpae_iopte prot;
477
478 /* If no access, then nothing to do */
479 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
480 return 0;
481
Robin Murphyf7b90d22019-10-25 19:08:31 +0100482 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
483 return -EINVAL;
484
Robin Murphy67f3e532019-10-25 19:08:32 +0100485 if (WARN_ON(iova >> data->iop.cfg.ias || paddr >> data->iop.cfg.oas))
Robin Murphy76557392017-07-03 14:52:24 +0100486 return -ERANGE;
487
Will Deacone1d3c0f2014-11-14 17:18:23 +0000488 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100489 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
490 /*
491 * Synchronise all PTE updates for the new mapping before there's
492 * a chance for anything to kick off a table walk for the new iova.
493 */
494 wmb();
495
496 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000497}
498
499static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
500 arm_lpae_iopte *ptep)
501{
502 arm_lpae_iopte *start, *end;
503 unsigned long table_size;
504
Robin Murphy594ab902019-10-25 19:08:33 +0100505 if (lvl == data->start_level)
Robin Murphyc79278c2019-10-25 19:08:34 +0100506 table_size = ARM_LPAE_PGD_SIZE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000507 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000508 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000509
510 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000511
512 /* Only leaf entries at the last level */
513 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
514 end = ptep;
515 else
516 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000517
518 while (ptep != end) {
519 arm_lpae_iopte pte = *ptep++;
520
Rob Herringd08d42d2019-02-21 14:23:25 -0600521 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000522 continue;
523
524 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
525 }
526
Robin Murphyf8d54962015-07-29 19:46:04 +0100527 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000528}
529
530static void arm_lpae_free_pgtable(struct io_pgtable *iop)
531{
532 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
533
Robin Murphy594ab902019-10-25 19:08:33 +0100534 __arm_lpae_free_pgtable(data, data->start_level, data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000535 kfree(data);
536}
537
Vivek Gautam193e67c2018-02-05 23:29:19 +0530538static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100539 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530540 unsigned long iova, size_t size,
541 arm_lpae_iopte blk_pte, int lvl,
542 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000543{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100544 struct io_pgtable_cfg *cfg = &data->iop.cfg;
545 arm_lpae_iopte pte, *tablep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000546 phys_addr_t blk_paddr;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100547 size_t tablesz = ARM_LPAE_GRANULE(data);
548 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
549 int i, unmap_idx = -1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000550
Robin Murphyfb3a9572017-06-22 16:53:51 +0100551 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
552 return 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000553
Robin Murphyfb3a9572017-06-22 16:53:51 +0100554 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
555 if (!tablep)
556 return 0; /* Bytes unmapped */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000557
Robin Murphyfb3a9572017-06-22 16:53:51 +0100558 if (size == split_sz)
559 unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data);
560
Robin Murphy6c899282018-03-26 13:35:13 +0100561 blk_paddr = iopte_to_paddr(blk_pte, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100562 pte = iopte_prot(blk_pte);
563
564 for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000565 /* Unmap! */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100566 if (i == unmap_idx)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000567 continue;
568
Robin Murphyfb3a9572017-06-22 16:53:51 +0100569 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000570 }
571
Robin Murphy2c3d2732017-06-22 16:53:54 +0100572 pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
573 if (pte != blk_pte) {
574 __arm_lpae_free_pages(tablep, tablesz, cfg);
575 /*
576 * We may race against someone unmapping another part of this
577 * block, but anything else is invalid. We can't misinterpret
578 * a page entry here since we're never at the last level.
579 */
580 if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE)
581 return 0;
582
583 tablep = iopte_deref(pte, data);
Robin Murphy85c7a0f2018-09-06 17:59:50 +0100584 } else if (unmap_idx >= 0) {
Will Deacon3951c412019-07-02 16:45:15 +0100585 io_pgtable_tlb_add_page(&data->iop, gather, iova, size);
Robin Murphy85c7a0f2018-09-06 17:59:50 +0100586 return size;
Robin Murphy2c3d2732017-06-22 16:53:54 +0100587 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100588
Will Deacon3951c412019-07-02 16:45:15 +0100589 return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000590}
591
Vivek Gautam193e67c2018-02-05 23:29:19 +0530592static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100593 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530594 unsigned long iova, size_t size, int lvl,
595 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000596{
597 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000598 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000599
Robin Murphy2eb97c72015-12-04 17:52:58 +0000600 /* Something went horribly wrong and we ran out of page table */
601 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
602 return 0;
603
Will Deacone1d3c0f2014-11-14 17:18:23 +0000604 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100605 pte = READ_ONCE(*ptep);
Robin Murphy2eb97c72015-12-04 17:52:58 +0000606 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000607 return 0;
608
609 /* If the size matches this level, we're in the right place */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100610 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000611 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000612
Rob Herringd08d42d2019-02-21 14:23:25 -0600613 if (!iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000614 /* Also flush any partial walks */
Will Deacon10b7a7d2019-07-02 16:44:32 +0100615 io_pgtable_tlb_flush_walk(iop, iova, size,
616 ARM_LPAE_GRANULE(data));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000617 ptep = iopte_deref(pte, data);
618 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
Zhen Leib6b65ca2018-09-20 17:10:24 +0100619 } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
620 /*
621 * Order the PTE update against queueing the IOVA, to
622 * guarantee that a flush callback from a different CPU
623 * has observed it before the TLBIALL can be issued.
624 */
625 smp_wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000626 } else {
Will Deacon3951c412019-07-02 16:45:15 +0100627 io_pgtable_tlb_add_page(iop, gather, iova, size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000628 }
629
630 return size;
Rob Herringd08d42d2019-02-21 14:23:25 -0600631 } else if (iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000632 /*
633 * Insert a table at the next level to map the old region,
634 * minus the part we want to unmap
635 */
Will Deacon3951c412019-07-02 16:45:15 +0100636 return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100637 lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000638 }
639
640 /* Keep on walkin' */
641 ptep = iopte_deref(pte, data);
Will Deacon3951c412019-07-02 16:45:15 +0100642 return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000643}
644
Vivek Gautam193e67c2018-02-05 23:29:19 +0530645static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacona2d3a382019-07-02 16:44:58 +0100646 size_t size, struct iommu_iotlb_gather *gather)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000647{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000648 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100649 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000650 arm_lpae_iopte *ptep = data->pgd;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000651
Robin Murphyf7b90d22019-10-25 19:08:31 +0100652 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
653 return 0;
654
Robin Murphy67f3e532019-10-25 19:08:32 +0100655 if (WARN_ON(iova >> data->iop.cfg.ias))
Robin Murphy76557392017-07-03 14:52:24 +0100656 return 0;
657
Robin Murphy594ab902019-10-25 19:08:33 +0100658 return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000659}
660
661static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
662 unsigned long iova)
663{
664 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
665 arm_lpae_iopte pte, *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100666 int lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000667
668 do {
669 /* Valid IOPTE pointer? */
670 if (!ptep)
671 return 0;
672
673 /* Grab the IOPTE we're interested in */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100674 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
675 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000676
677 /* Valid entry? */
678 if (!pte)
679 return 0;
680
681 /* Leaf entry? */
Rob Herringd08d42d2019-02-21 14:23:25 -0600682 if (iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000683 goto found_translation;
684
685 /* Take it to the next level */
686 ptep = iopte_deref(pte, data);
687 } while (++lvl < ARM_LPAE_MAX_LEVELS);
688
689 /* Ran out of page tables to walk */
690 return 0;
691
692found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100693 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Robin Murphy6c899282018-03-26 13:35:13 +0100694 return iopte_to_paddr(pte, data) | iova;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000695}
696
697static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
698{
Robin Murphy6c899282018-03-26 13:35:13 +0100699 unsigned long granule, page_sizes;
700 unsigned int max_addr_bits = 48;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000701
702 /*
703 * We need to restrict the supported page sizes to match the
704 * translation regime for a particular granule. Aim to match
705 * the CPU page size if possible, otherwise prefer smaller sizes.
706 * While we're at it, restrict the block sizes to match the
707 * chosen granule.
708 */
709 if (cfg->pgsize_bitmap & PAGE_SIZE)
710 granule = PAGE_SIZE;
711 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
712 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
713 else if (cfg->pgsize_bitmap & PAGE_MASK)
714 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
715 else
716 granule = 0;
717
718 switch (granule) {
719 case SZ_4K:
Robin Murphy6c899282018-03-26 13:35:13 +0100720 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000721 break;
722 case SZ_16K:
Robin Murphy6c899282018-03-26 13:35:13 +0100723 page_sizes = (SZ_16K | SZ_32M);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000724 break;
725 case SZ_64K:
Robin Murphy6c899282018-03-26 13:35:13 +0100726 max_addr_bits = 52;
727 page_sizes = (SZ_64K | SZ_512M);
728 if (cfg->oas > 48)
729 page_sizes |= 1ULL << 42; /* 4TB */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000730 break;
731 default:
Robin Murphy6c899282018-03-26 13:35:13 +0100732 page_sizes = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000733 }
Robin Murphy6c899282018-03-26 13:35:13 +0100734
735 cfg->pgsize_bitmap &= page_sizes;
736 cfg->ias = min(cfg->ias, max_addr_bits);
737 cfg->oas = min(cfg->oas, max_addr_bits);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000738}
739
740static struct arm_lpae_io_pgtable *
741arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
742{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000743 struct arm_lpae_io_pgtable *data;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100744 int levels, va_bits, pg_shift;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000745
746 arm_lpae_restrict_pgsizes(cfg);
747
748 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
749 return NULL;
750
751 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
752 return NULL;
753
754 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
755 return NULL;
756
Robin Murphyffcb6d12015-09-17 17:42:16 +0100757 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
758 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
759 return NULL;
760 }
761
Will Deacone1d3c0f2014-11-14 17:18:23 +0000762 data = kmalloc(sizeof(*data), GFP_KERNEL);
763 if (!data)
764 return NULL;
765
Robin Murphy5fb190b2019-10-25 19:08:35 +0100766 pg_shift = __ffs(cfg->pgsize_bitmap);
767 data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000768
Robin Murphy5fb190b2019-10-25 19:08:35 +0100769 va_bits = cfg->ias - pg_shift;
Robin Murphy594ab902019-10-25 19:08:33 +0100770 levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
771 data->start_level = ARM_LPAE_MAX_LEVELS - levels;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000772
773 /* Calculate the actual size of our pgd (without concatenation) */
Robin Murphyc79278c2019-10-25 19:08:34 +0100774 data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000775
776 data->iop.ops = (struct io_pgtable_ops) {
777 .map = arm_lpae_map,
778 .unmap = arm_lpae_unmap,
779 .iova_to_phys = arm_lpae_iova_to_phys,
780 };
781
782 return data;
783}
784
785static struct io_pgtable *
786arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
787{
788 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000789 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000790
Will Deacon4f418452019-06-25 12:51:25 +0100791 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
Zhen Leib6b65ca2018-09-20 17:10:24 +0100792 IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000793 return NULL;
794
795 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000796 if (!data)
797 return NULL;
798
799 /* TCR */
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700800 if (cfg->coherent_walk) {
801 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
802 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
803 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
804 } else {
805 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
806 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
807 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
808 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000809
Robin Murphy06c610e2015-12-07 18:18:53 +0000810 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000811 case SZ_4K:
812 reg |= ARM_LPAE_TCR_TG0_4K;
813 break;
814 case SZ_16K:
815 reg |= ARM_LPAE_TCR_TG0_16K;
816 break;
817 case SZ_64K:
818 reg |= ARM_LPAE_TCR_TG0_64K;
819 break;
820 }
821
822 switch (cfg->oas) {
823 case 32:
824 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
825 break;
826 case 36:
827 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
828 break;
829 case 40:
830 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
831 break;
832 case 42:
833 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
834 break;
835 case 44:
836 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
837 break;
838 case 48:
839 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
840 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100841 case 52:
842 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT);
843 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000844 default:
845 goto out_free_data;
846 }
847
848 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000849
850 /* Disable speculative walks through TTBR1 */
851 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000852 cfg->arm_lpae_s1_cfg.tcr = reg;
853
854 /* MAIRs */
855 reg = (ARM_LPAE_MAIR_ATTR_NC
856 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
857 (ARM_LPAE_MAIR_ATTR_WBRWA
858 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
859 (ARM_LPAE_MAIR_ATTR_DEVICE
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530860 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
861 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
862 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000863
Robin Murphy205577a2019-10-25 19:08:36 +0100864 cfg->arm_lpae_s1_cfg.mair = reg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000865
866 /* Looking good; allocate a pgd */
Robin Murphyc79278c2019-10-25 19:08:34 +0100867 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
868 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000869 if (!data->pgd)
870 goto out_free_data;
871
Robin Murphy87a91b12015-07-29 19:46:09 +0100872 /* Ensure the empty pgd is visible before any actual TTBR write */
873 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000874
Robin Murphyd1e5f262019-10-25 19:08:37 +0100875 /* TTBR */
876 cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000877 return &data->iop;
878
879out_free_data:
880 kfree(data);
881 return NULL;
882}
883
884static struct io_pgtable *
885arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
886{
887 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000888 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000889
Robin Murphy3850db42016-02-12 17:09:46 +0000890 /* The NS quirk doesn't apply at stage 2 */
Will Deacon4f418452019-06-25 12:51:25 +0100891 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000892 return NULL;
893
894 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000895 if (!data)
896 return NULL;
897
898 /*
899 * Concatenate PGDs at level 1 if possible in order to reduce
900 * the depth of the stage-2 walk.
901 */
Robin Murphy594ab902019-10-25 19:08:33 +0100902 if (data->start_level == 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000903 unsigned long pgd_pages;
904
Robin Murphyc79278c2019-10-25 19:08:34 +0100905 pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000906 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
Robin Murphyc79278c2019-10-25 19:08:34 +0100907 data->pgd_bits += data->bits_per_level;
Robin Murphy594ab902019-10-25 19:08:33 +0100908 data->start_level++;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000909 }
910 }
911
912 /* VTCR */
Will Deacon30d2acb2020-01-10 11:40:33 +0000913 reg = ARM_64_LPAE_S2_TCR_RES1;
914 if (cfg->coherent_walk) {
915 reg |= (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
916 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
917 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
918 } else {
919 reg |= (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
920 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
921 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
922 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000923
Robin Murphy594ab902019-10-25 19:08:33 +0100924 sl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000925
Robin Murphy06c610e2015-12-07 18:18:53 +0000926 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000927 case SZ_4K:
928 reg |= ARM_LPAE_TCR_TG0_4K;
929 sl++; /* SL0 format is different for 4K granule size */
930 break;
931 case SZ_16K:
932 reg |= ARM_LPAE_TCR_TG0_16K;
933 break;
934 case SZ_64K:
935 reg |= ARM_LPAE_TCR_TG0_64K;
936 break;
937 }
938
939 switch (cfg->oas) {
940 case 32:
941 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
942 break;
943 case 36:
944 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
945 break;
946 case 40:
947 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
948 break;
949 case 42:
950 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
951 break;
952 case 44:
953 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
954 break;
955 case 48:
956 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
957 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100958 case 52:
959 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT);
960 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000961 default:
962 goto out_free_data;
963 }
964
965 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
966 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
967 cfg->arm_lpae_s2_cfg.vtcr = reg;
968
969 /* Allocate pgd pages */
Robin Murphyc79278c2019-10-25 19:08:34 +0100970 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
971 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000972 if (!data->pgd)
973 goto out_free_data;
974
Robin Murphy87a91b12015-07-29 19:46:09 +0100975 /* Ensure the empty pgd is visible before any actual TTBR write */
976 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000977
978 /* VTTBR */
979 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
980 return &data->iop;
981
982out_free_data:
983 kfree(data);
984 return NULL;
985}
986
987static struct io_pgtable *
988arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
989{
990 struct io_pgtable *iop;
991
992 if (cfg->ias > 32 || cfg->oas > 40)
993 return NULL;
994
995 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
996 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
997 if (iop) {
998 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
999 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1000 }
1001
1002 return iop;
1003}
1004
1005static struct io_pgtable *
1006arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1007{
1008 struct io_pgtable *iop;
1009
1010 if (cfg->ias > 40 || cfg->oas > 40)
1011 return NULL;
1012
1013 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1014 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1015 if (iop)
1016 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1017
1018 return iop;
1019}
1020
Rob Herringd08d42d2019-02-21 14:23:25 -06001021static struct io_pgtable *
1022arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1023{
Robin Murphy52f325f2019-09-30 15:11:00 +01001024 struct arm_lpae_io_pgtable *data;
Rob Herringd08d42d2019-02-21 14:23:25 -06001025
Robin Murphy52f325f2019-09-30 15:11:00 +01001026 /* No quirks for Mali (hopefully) */
1027 if (cfg->quirks)
1028 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001029
Robin Murphy1be08f42019-09-30 15:11:01 +01001030 if (cfg->ias > 48 || cfg->oas > 40)
Rob Herringd08d42d2019-02-21 14:23:25 -06001031 return NULL;
1032
1033 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Rob Herringd08d42d2019-02-21 14:23:25 -06001034
Robin Murphy52f325f2019-09-30 15:11:00 +01001035 data = arm_lpae_alloc_pgtable(cfg);
1036 if (!data)
1037 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001038
Robin Murphy1be08f42019-09-30 15:11:01 +01001039 /* Mali seems to need a full 4-level table regardless of IAS */
Robin Murphy594ab902019-10-25 19:08:33 +01001040 if (data->start_level > 0) {
1041 data->start_level = 0;
Robin Murphyc79278c2019-10-25 19:08:34 +01001042 data->pgd_bits = 0;
Rob Herringd08d42d2019-02-21 14:23:25 -06001043 }
Robin Murphy52f325f2019-09-30 15:11:00 +01001044 /*
1045 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1046 * best we can do is mimic the out-of-tree driver and hope that the
1047 * "implementation-defined caching policy" is good enough. Similarly,
1048 * we'll use it for the sake of a valid attribute for our 'device'
1049 * index, although callers should never request that in practice.
1050 */
1051 cfg->arm_mali_lpae_cfg.memattr =
1052 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1053 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1054 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1055 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1056 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1057 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
Rob Herringd08d42d2019-02-21 14:23:25 -06001058
Robin Murphyc79278c2019-10-25 19:08:34 +01001059 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1060 cfg);
Robin Murphy52f325f2019-09-30 15:11:00 +01001061 if (!data->pgd)
1062 goto out_free_data;
1063
1064 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1065 wmb();
1066
1067 cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1068 ARM_MALI_LPAE_TTBR_READ_INNER |
1069 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1070 return &data->iop;
1071
1072out_free_data:
1073 kfree(data);
1074 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001075}
1076
Will Deacone1d3c0f2014-11-14 17:18:23 +00001077struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1078 .alloc = arm_64_lpae_alloc_pgtable_s1,
1079 .free = arm_lpae_free_pgtable,
1080};
1081
1082struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1083 .alloc = arm_64_lpae_alloc_pgtable_s2,
1084 .free = arm_lpae_free_pgtable,
1085};
1086
1087struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1088 .alloc = arm_32_lpae_alloc_pgtable_s1,
1089 .free = arm_lpae_free_pgtable,
1090};
1091
1092struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1093 .alloc = arm_32_lpae_alloc_pgtable_s2,
1094 .free = arm_lpae_free_pgtable,
1095};
Will Deaconfe4b9912014-11-17 23:31:12 +00001096
Rob Herringd08d42d2019-02-21 14:23:25 -06001097struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1098 .alloc = arm_mali_lpae_alloc_pgtable,
1099 .free = arm_lpae_free_pgtable,
1100};
1101
Will Deaconfe4b9912014-11-17 23:31:12 +00001102#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1103
Robin Murphyb5813c12019-10-25 19:08:30 +01001104static struct io_pgtable_cfg *cfg_cookie __initdata;
Will Deaconfe4b9912014-11-17 23:31:12 +00001105
Robin Murphyb5813c12019-10-25 19:08:30 +01001106static void __init dummy_tlb_flush_all(void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001107{
1108 WARN_ON(cookie != cfg_cookie);
1109}
1110
Robin Murphyb5813c12019-10-25 19:08:30 +01001111static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1112 size_t granule, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001113{
1114 WARN_ON(cookie != cfg_cookie);
1115 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1116}
1117
Robin Murphyb5813c12019-10-25 19:08:30 +01001118static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1119 unsigned long iova, size_t granule,
1120 void *cookie)
Will Deacon10b7a7d2019-07-02 16:44:32 +01001121{
Will Deaconabfd6fe2019-07-02 16:44:41 +01001122 dummy_tlb_flush(iova, granule, granule, cookie);
Will Deacon10b7a7d2019-07-02 16:44:32 +01001123}
1124
Will Deacon298f78892019-07-02 16:43:34 +01001125static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001126 .tlb_flush_all = dummy_tlb_flush_all,
Will Deacon10b7a7d2019-07-02 16:44:32 +01001127 .tlb_flush_walk = dummy_tlb_flush,
1128 .tlb_flush_leaf = dummy_tlb_flush,
Will Deaconabfd6fe2019-07-02 16:44:41 +01001129 .tlb_add_page = dummy_tlb_add_page,
Will Deaconfe4b9912014-11-17 23:31:12 +00001130};
1131
1132static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1133{
1134 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1135 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1136
1137 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1138 cfg->pgsize_bitmap, cfg->ias);
Robin Murphy5fb190b2019-10-25 19:08:35 +01001139 pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
Robin Murphyc79278c2019-10-25 19:08:34 +01001140 ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
Robin Murphy5fb190b2019-10-25 19:08:35 +01001141 ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
Will Deaconfe4b9912014-11-17 23:31:12 +00001142}
1143
1144#define __FAIL(ops, i) ({ \
1145 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1146 arm_lpae_dump_ops(ops); \
1147 selftest_running = false; \
1148 -EFAULT; \
1149})
1150
1151static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1152{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001153 static const enum io_pgtable_fmt fmts[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001154 ARM_64_LPAE_S1,
1155 ARM_64_LPAE_S2,
1156 };
1157
1158 int i, j;
1159 unsigned long iova;
1160 size_t size;
1161 struct io_pgtable_ops *ops;
1162
1163 selftest_running = true;
1164
1165 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1166 cfg_cookie = cfg;
1167 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1168 if (!ops) {
1169 pr_err("selftest: failed to allocate io pgtable ops\n");
1170 return -ENOMEM;
1171 }
1172
1173 /*
1174 * Initial sanity checks.
1175 * Empty page tables shouldn't provide any translations.
1176 */
1177 if (ops->iova_to_phys(ops, 42))
1178 return __FAIL(ops, i);
1179
1180 if (ops->iova_to_phys(ops, SZ_1G + 42))
1181 return __FAIL(ops, i);
1182
1183 if (ops->iova_to_phys(ops, SZ_2G + 42))
1184 return __FAIL(ops, i);
1185
1186 /*
1187 * Distinct mappings of different granule sizes.
1188 */
1189 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +08001190 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001191 size = 1UL << j;
1192
1193 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1194 IOMMU_WRITE |
1195 IOMMU_NOEXEC |
1196 IOMMU_CACHE))
1197 return __FAIL(ops, i);
1198
1199 /* Overlapping mappings */
1200 if (!ops->map(ops, iova, iova + size, size,
1201 IOMMU_READ | IOMMU_NOEXEC))
1202 return __FAIL(ops, i);
1203
1204 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1205 return __FAIL(ops, i);
1206
1207 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001208 }
1209
1210 /* Partial unmap */
1211 size = 1UL << __ffs(cfg->pgsize_bitmap);
Will Deacona2d3a382019-07-02 16:44:58 +01001212 if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001213 return __FAIL(ops, i);
1214
1215 /* Remap of partial unmap */
1216 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1217 return __FAIL(ops, i);
1218
1219 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1220 return __FAIL(ops, i);
1221
1222 /* Full unmap */
1223 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +08001224 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001225 size = 1UL << j;
1226
Will Deacona2d3a382019-07-02 16:44:58 +01001227 if (ops->unmap(ops, iova, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001228 return __FAIL(ops, i);
1229
1230 if (ops->iova_to_phys(ops, iova + 42))
1231 return __FAIL(ops, i);
1232
1233 /* Remap full block */
1234 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1235 return __FAIL(ops, i);
1236
1237 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1238 return __FAIL(ops, i);
1239
1240 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001241 }
1242
1243 free_io_pgtable_ops(ops);
1244 }
1245
1246 selftest_running = false;
1247 return 0;
1248}
1249
1250static int __init arm_lpae_do_selftests(void)
1251{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001252 static const unsigned long pgsize[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001253 SZ_4K | SZ_2M | SZ_1G,
1254 SZ_16K | SZ_32M,
1255 SZ_64K | SZ_512M,
1256 };
1257
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001258 static const unsigned int ias[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001259 32, 36, 40, 42, 44, 48,
1260 };
1261
1262 int i, j, pass = 0, fail = 0;
1263 struct io_pgtable_cfg cfg = {
1264 .tlb = &dummy_tlb_ops,
1265 .oas = 48,
Will Deacon4f418452019-06-25 12:51:25 +01001266 .coherent_walk = true,
Will Deaconfe4b9912014-11-17 23:31:12 +00001267 };
1268
1269 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1270 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1271 cfg.pgsize_bitmap = pgsize[i];
1272 cfg.ias = ias[j];
1273 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1274 pgsize[i], ias[j]);
1275 if (arm_lpae_run_tests(&cfg))
1276 fail++;
1277 else
1278 pass++;
1279 }
1280 }
1281
1282 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1283 return fail ? -EFAULT : 0;
1284}
1285subsys_initcall(arm_lpae_do_selftests);
1286#endif