blob: 402f913b6f6dc96b34bca2fc7dd951795b4a8ca1 [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/iommu.h>
16#include <linux/kernel.h>
17#include <linux/sizes.h>
18#include <linux/slab.h>
19#include <linux/types.h>
Lada Trimasova8f6aff92016-01-27 11:10:32 +000020#include <linux/dma-mapping.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000021
Robin Murphy87a91b12015-07-29 19:46:09 +010022#include <asm/barrier.h>
23
Robin Murphy6c899282018-03-26 13:35:13 +010024#define ARM_LPAE_MAX_ADDR_BITS 52
Will Deacone1d3c0f2014-11-14 17:18:23 +000025#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
26#define ARM_LPAE_MAX_LEVELS 4
27
28/* Struct accessors */
29#define io_pgtable_to_data(x) \
30 container_of((x), struct arm_lpae_io_pgtable, iop)
31
Will Deacone1d3c0f2014-11-14 17:18:23 +000032#define io_pgtable_ops_to_data(x) \
33 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
34
35/*
36 * For consistency with the architecture, we always consider
37 * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
38 */
39#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
40
41/*
42 * Calculate the right shift amount to get to the portion describing level l
43 * in a virtual address mapped by the pagetable in d.
44 */
45#define ARM_LPAE_LVL_SHIFT(l,d) \
46 ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
47 * (d)->bits_per_level) + (d)->pg_shift)
48
Robin Murphy06c610e2015-12-07 18:18:53 +000049#define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift)
50
Will Deacon367bd972015-02-16 18:38:20 +000051#define ARM_LPAE_PAGES_PER_PGD(d) \
Robin Murphy06c610e2015-12-07 18:18:53 +000052 DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000053
54/*
55 * Calculate the index at level l used to map virtual address a using the
56 * pagetable in d.
57 */
58#define ARM_LPAE_PGD_IDX(l,d) \
59 ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
60
61#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000062 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000063 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
64
65/* Calculate the block/page mapping size at level l for pagetable in d. */
66#define ARM_LPAE_BLOCK_SIZE(l,d) \
Robin Murphy022f4e42017-04-03 13:12:10 +010067 (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \
Will Deacone1d3c0f2014-11-14 17:18:23 +000068 ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
69
70/* Page table bits */
71#define ARM_LPAE_PTE_TYPE_SHIFT 0
72#define ARM_LPAE_PTE_TYPE_MASK 0x3
73
74#define ARM_LPAE_PTE_TYPE_BLOCK 1
75#define ARM_LPAE_PTE_TYPE_TABLE 3
76#define ARM_LPAE_PTE_TYPE_PAGE 3
77
Robin Murphy6c899282018-03-26 13:35:13 +010078#define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
79
Laurent Pinchartc896c1322014-12-14 23:34:50 +020080#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000081#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
82#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
83#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
84#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
85#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c1322014-12-14 23:34:50 +020086#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000087#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
88
89#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
90/* Ignore the contiguous bit for block splitting */
91#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
92#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
93 ARM_LPAE_PTE_ATTR_HI_MASK)
Robin Murphy2c3d2732017-06-22 16:53:54 +010094/* Software bit for solving coherency races */
95#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
Will Deacone1d3c0f2014-11-14 17:18:23 +000096
97/* Stage-1 PTE */
98#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
99#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
100#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
101#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
102
103/* Stage-2 PTE */
104#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
105#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
106#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
107#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
108#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
109#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
110
111/* Register bits */
112#define ARM_32_LPAE_TCR_EAE (1 << 31)
113#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
114
Will Deacon63979b82015-03-18 10:22:18 +0000115#define ARM_LPAE_TCR_EPD1 (1 << 23)
116
Will Deacone1d3c0f2014-11-14 17:18:23 +0000117#define ARM_LPAE_TCR_TG0_4K (0 << 14)
118#define ARM_LPAE_TCR_TG0_64K (1 << 14)
119#define ARM_LPAE_TCR_TG0_16K (2 << 14)
120
121#define ARM_LPAE_TCR_SH0_SHIFT 12
122#define ARM_LPAE_TCR_SH0_MASK 0x3
123#define ARM_LPAE_TCR_SH_NS 0
124#define ARM_LPAE_TCR_SH_OS 2
125#define ARM_LPAE_TCR_SH_IS 3
126
127#define ARM_LPAE_TCR_ORGN0_SHIFT 10
128#define ARM_LPAE_TCR_IRGN0_SHIFT 8
129#define ARM_LPAE_TCR_RGN_MASK 0x3
130#define ARM_LPAE_TCR_RGN_NC 0
131#define ARM_LPAE_TCR_RGN_WBWA 1
132#define ARM_LPAE_TCR_RGN_WT 2
133#define ARM_LPAE_TCR_RGN_WB 3
134
135#define ARM_LPAE_TCR_SL0_SHIFT 6
136#define ARM_LPAE_TCR_SL0_MASK 0x3
137
138#define ARM_LPAE_TCR_T0SZ_SHIFT 0
139#define ARM_LPAE_TCR_SZ_MASK 0xf
140
141#define ARM_LPAE_TCR_PS_SHIFT 16
142#define ARM_LPAE_TCR_PS_MASK 0x7
143
144#define ARM_LPAE_TCR_IPS_SHIFT 32
145#define ARM_LPAE_TCR_IPS_MASK 0x7
146
147#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
148#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
149#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
150#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
151#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
152#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
Robin Murphy6c899282018-03-26 13:35:13 +0100153#define ARM_LPAE_TCR_PS_52_BIT 0x6ULL
Will Deacone1d3c0f2014-11-14 17:18:23 +0000154
155#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
156#define ARM_LPAE_MAIR_ATTR_MASK 0xff
157#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
158#define ARM_LPAE_MAIR_ATTR_NC 0x44
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530159#define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000160#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
161#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
162#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
163#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530164#define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000165
Rob Herringd08d42d2019-02-21 14:23:25 -0600166#define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
167#define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
168#define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
169
Will Deacone1d3c0f2014-11-14 17:18:23 +0000170/* IOPTE accessors */
Robin Murphy6c899282018-03-26 13:35:13 +0100171#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000172
173#define iopte_type(pte,l) \
174 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
175
176#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
177
Will Deacone1d3c0f2014-11-14 17:18:23 +0000178struct arm_lpae_io_pgtable {
179 struct io_pgtable iop;
180
181 int levels;
182 size_t pgd_size;
183 unsigned long pg_shift;
184 unsigned long bits_per_level;
185
186 void *pgd;
187};
188
189typedef u64 arm_lpae_iopte;
190
Rob Herringd08d42d2019-02-21 14:23:25 -0600191static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
192 enum io_pgtable_fmt fmt)
193{
194 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
195 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE;
196
197 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK;
198}
199
Robin Murphy6c899282018-03-26 13:35:13 +0100200static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
201 struct arm_lpae_io_pgtable *data)
202{
203 arm_lpae_iopte pte = paddr;
204
205 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
206 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
207}
208
209static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
210 struct arm_lpae_io_pgtable *data)
211{
Robin Murphy78688052018-03-29 12:24:52 +0100212 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
Robin Murphy6c899282018-03-26 13:35:13 +0100213
214 if (data->pg_shift < 16)
215 return paddr;
216
217 /* Rotate the packed high-order bits back to the top */
218 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
219}
220
Will Deaconfe4b9912014-11-17 23:31:12 +0000221static bool selftest_running = false;
222
Robin Murphyffcb6d12015-09-17 17:42:16 +0100223static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100224{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100225 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100226}
227
228static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
229 struct io_pgtable_cfg *cfg)
230{
231 struct device *dev = cfg->iommu_dev;
Robin Murphy4b123752018-05-22 12:50:09 +0100232 int order = get_order(size);
233 struct page *p;
Robin Murphyf8d54962015-07-29 19:46:04 +0100234 dma_addr_t dma;
Robin Murphy4b123752018-05-22 12:50:09 +0100235 void *pages;
Robin Murphyf8d54962015-07-29 19:46:04 +0100236
Robin Murphy4b123752018-05-22 12:50:09 +0100237 VM_BUG_ON((gfp & __GFP_HIGHMEM));
Jean-Philippe Bruckerfac83d22018-06-18 12:27:54 +0100238 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
239 gfp | __GFP_ZERO, order);
Robin Murphy4b123752018-05-22 12:50:09 +0100240 if (!p)
Robin Murphyf8d54962015-07-29 19:46:04 +0100241 return NULL;
242
Robin Murphy4b123752018-05-22 12:50:09 +0100243 pages = page_address(p);
Will Deacon4f418452019-06-25 12:51:25 +0100244 if (!cfg->coherent_walk) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100245 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
246 if (dma_mapping_error(dev, dma))
247 goto out_free;
248 /*
249 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100250 * address directly, so if the DMA layer suggests otherwise by
251 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100252 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100253 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100254 goto out_unmap;
255 }
256
257 return pages;
258
259out_unmap:
260 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
261 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
262out_free:
Robin Murphy4b123752018-05-22 12:50:09 +0100263 __free_pages(p, order);
Robin Murphyf8d54962015-07-29 19:46:04 +0100264 return NULL;
265}
266
267static void __arm_lpae_free_pages(void *pages, size_t size,
268 struct io_pgtable_cfg *cfg)
269{
Will Deacon4f418452019-06-25 12:51:25 +0100270 if (!cfg->coherent_walk)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100271 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100272 size, DMA_TO_DEVICE);
Robin Murphy4b123752018-05-22 12:50:09 +0100273 free_pages((unsigned long)pages, get_order(size));
Robin Murphyf8d54962015-07-29 19:46:04 +0100274}
275
Robin Murphy2c3d2732017-06-22 16:53:54 +0100276static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
277 struct io_pgtable_cfg *cfg)
278{
279 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
280 sizeof(*ptep), DMA_TO_DEVICE);
281}
282
Robin Murphyf8d54962015-07-29 19:46:04 +0100283static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100284 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100285{
Robin Murphyf8d54962015-07-29 19:46:04 +0100286 *ptep = pte;
287
Will Deacon4f418452019-06-25 12:51:25 +0100288 if (!cfg->coherent_walk)
Robin Murphy2c3d2732017-06-22 16:53:54 +0100289 __arm_lpae_sync_pte(ptep, cfg);
Robin Murphyf8d54962015-07-29 19:46:04 +0100290}
291
Vivek Gautam193e67c2018-02-05 23:29:19 +0530292static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
293 unsigned long iova, size_t size, int lvl,
294 arm_lpae_iopte *ptep);
Will Deaconcf27ec92015-08-11 16:48:32 +0100295
Robin Murphyfb3a9572017-06-22 16:53:51 +0100296static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
297 phys_addr_t paddr, arm_lpae_iopte prot,
298 int lvl, arm_lpae_iopte *ptep)
299{
300 arm_lpae_iopte pte = prot;
301
302 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
303 pte |= ARM_LPAE_PTE_NS;
304
Rob Herringd08d42d2019-02-21 14:23:25 -0600305 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100306 pte |= ARM_LPAE_PTE_TYPE_PAGE;
307 else
308 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
309
Rob Herringd08d42d2019-02-21 14:23:25 -0600310 if (data->iop.fmt != ARM_MALI_LPAE)
311 pte |= ARM_LPAE_PTE_AF;
312 pte |= ARM_LPAE_PTE_SH_IS;
Robin Murphy6c899282018-03-26 13:35:13 +0100313 pte |= paddr_to_iopte(paddr, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100314
315 __arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
316}
317
Will Deacone1d3c0f2014-11-14 17:18:23 +0000318static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
319 unsigned long iova, phys_addr_t paddr,
320 arm_lpae_iopte prot, int lvl,
321 arm_lpae_iopte *ptep)
322{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100323 arm_lpae_iopte pte = *ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000324
Rob Herringd08d42d2019-02-21 14:23:25 -0600325 if (iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100326 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000327 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000328 return -EEXIST;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100329 } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100330 /*
331 * We need to unmap and free the old table before
332 * overwriting it with a block entry.
333 */
334 arm_lpae_iopte *tblp;
335 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
336
337 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
338 if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz))
339 return -EINVAL;
Will Deaconfe4b9912014-11-17 23:31:12 +0000340 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000341
Robin Murphyfb3a9572017-06-22 16:53:51 +0100342 __arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000343 return 0;
344}
345
Robin Murphyfb3a9572017-06-22 16:53:51 +0100346static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
347 arm_lpae_iopte *ptep,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100348 arm_lpae_iopte curr,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100349 struct io_pgtable_cfg *cfg)
350{
Robin Murphy2c3d2732017-06-22 16:53:54 +0100351 arm_lpae_iopte old, new;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100352
353 new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
354 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
355 new |= ARM_LPAE_PTE_NSTABLE;
356
Will Deacon77f34452017-06-23 12:02:38 +0100357 /*
358 * Ensure the table itself is visible before its PTE can be.
359 * Whilst we could get away with cmpxchg64_release below, this
360 * doesn't have any ordering semantics when !CONFIG_SMP.
361 */
362 dma_wmb();
Robin Murphy2c3d2732017-06-22 16:53:54 +0100363
364 old = cmpxchg64_relaxed(ptep, curr, new);
365
Will Deacon4f418452019-06-25 12:51:25 +0100366 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
Robin Murphy2c3d2732017-06-22 16:53:54 +0100367 return old;
368
369 /* Even if it's not ours, there's no point waiting; just kick it */
370 __arm_lpae_sync_pte(ptep, cfg);
371 if (old == curr)
372 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
373
374 return old;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100375}
376
Will Deacone1d3c0f2014-11-14 17:18:23 +0000377static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
378 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
379 int lvl, arm_lpae_iopte *ptep)
380{
381 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000382 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100383 size_t tblsz = ARM_LPAE_GRANULE(data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100384 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000385
386 /* Find our entry at the current level */
387 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
388
389 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100390 if (size == block_size && (size & cfg->pgsize_bitmap))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000391 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
392
393 /* We can't allocate tables at the final level */
394 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
395 return -EINVAL;
396
397 /* Grab a pointer to the next level */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100398 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000399 if (!pte) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100400 cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000401 if (!cptep)
402 return -ENOMEM;
403
Robin Murphy2c3d2732017-06-22 16:53:54 +0100404 pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
405 if (pte)
406 __arm_lpae_free_pages(cptep, tblsz, cfg);
Will Deacon4f418452019-06-25 12:51:25 +0100407 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100408 __arm_lpae_sync_pte(ptep, cfg);
409 }
410
Rob Herringd08d42d2019-02-21 14:23:25 -0600411 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000412 cptep = iopte_deref(pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100413 } else if (pte) {
Oleksandr Tyshchenkoed46e662017-02-27 14:30:25 +0200414 /* We require an unmap first */
415 WARN_ON(!selftest_running);
416 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000417 }
418
419 /* Rinse, repeat */
420 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
421}
422
423static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
424 int prot)
425{
426 arm_lpae_iopte pte;
427
428 if (data->iop.fmt == ARM_64_LPAE_S1 ||
429 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530430 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000431 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
432 pte |= ARM_LPAE_PTE_AP_RDONLY;
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530433 if (!(prot & IOMMU_PRIV))
434 pte |= ARM_LPAE_PTE_AP_UNPRIV;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000435 } else {
436 pte = ARM_LPAE_PTE_HAP_FAULT;
437 if (prot & IOMMU_READ)
438 pte |= ARM_LPAE_PTE_HAP_READ;
439 if (prot & IOMMU_WRITE)
440 pte |= ARM_LPAE_PTE_HAP_WRITE;
Rob Herringd08d42d2019-02-21 14:23:25 -0600441 }
442
443 /*
444 * Note that this logic is structured to accommodate Mali LPAE
445 * having stage-1-like attributes but stage-2-like permissions.
446 */
447 if (data->iop.fmt == ARM_64_LPAE_S2 ||
448 data->iop.fmt == ARM_32_LPAE_S2) {
Robin Murphyfb948252016-04-05 12:39:31 +0100449 if (prot & IOMMU_MMIO)
450 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
451 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000452 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
453 else
454 pte |= ARM_LPAE_PTE_MEMATTR_NC;
Rob Herringd08d42d2019-02-21 14:23:25 -0600455 } else {
456 if (prot & IOMMU_MMIO)
457 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
458 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
459 else if (prot & IOMMU_CACHE)
460 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
461 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530462 else if (prot & IOMMU_QCOM_SYS_CACHE)
463 pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
464 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000465 }
466
467 if (prot & IOMMU_NOEXEC)
468 pte |= ARM_LPAE_PTE_XN;
469
470 return pte;
471}
472
473static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
474 phys_addr_t paddr, size_t size, int iommu_prot)
475{
476 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
477 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100478 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000479 arm_lpae_iopte prot;
480
481 /* If no access, then nothing to do */
482 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
483 return 0;
484
Robin Murphy76557392017-07-03 14:52:24 +0100485 if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) ||
486 paddr >= (1ULL << data->iop.cfg.oas)))
487 return -ERANGE;
488
Will Deacone1d3c0f2014-11-14 17:18:23 +0000489 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100490 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
491 /*
492 * Synchronise all PTE updates for the new mapping before there's
493 * a chance for anything to kick off a table walk for the new iova.
494 */
495 wmb();
496
497 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000498}
499
500static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
501 arm_lpae_iopte *ptep)
502{
503 arm_lpae_iopte *start, *end;
504 unsigned long table_size;
505
Will Deacone1d3c0f2014-11-14 17:18:23 +0000506 if (lvl == ARM_LPAE_START_LVL(data))
507 table_size = data->pgd_size;
508 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000509 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000510
511 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000512
513 /* Only leaf entries at the last level */
514 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
515 end = ptep;
516 else
517 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000518
519 while (ptep != end) {
520 arm_lpae_iopte pte = *ptep++;
521
Rob Herringd08d42d2019-02-21 14:23:25 -0600522 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000523 continue;
524
525 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
526 }
527
Robin Murphyf8d54962015-07-29 19:46:04 +0100528 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000529}
530
531static void arm_lpae_free_pgtable(struct io_pgtable *iop)
532{
533 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
534
535 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
536 kfree(data);
537}
538
Vivek Gautam193e67c2018-02-05 23:29:19 +0530539static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
540 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) {
585 io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true);
586 return size;
Robin Murphy2c3d2732017-06-22 16:53:54 +0100587 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100588
Robin Murphy85c7a0f2018-09-06 17:59:50 +0100589 return __arm_lpae_unmap(data, 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,
593 unsigned long iova, size_t size, int lvl,
594 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000595{
596 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000597 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000598
Robin Murphy2eb97c72015-12-04 17:52:58 +0000599 /* Something went horribly wrong and we ran out of page table */
600 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
601 return 0;
602
Will Deacone1d3c0f2014-11-14 17:18:23 +0000603 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100604 pte = READ_ONCE(*ptep);
Robin Murphy2eb97c72015-12-04 17:52:58 +0000605 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000606 return 0;
607
608 /* If the size matches this level, we're in the right place */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100609 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000610 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000611
Rob Herringd08d42d2019-02-21 14:23:25 -0600612 if (!iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000613 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000614 io_pgtable_tlb_add_flush(iop, iova, size,
615 ARM_LPAE_GRANULE(data), false);
616 io_pgtable_tlb_sync(iop);
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 {
Robin Murphy507e4c92016-01-26 17:13:14 +0000627 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
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 */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100636 return arm_lpae_split_blk_unmap(data, iova, size, pte,
637 lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000638 }
639
640 /* Keep on walkin' */
641 ptep = iopte_deref(pte, data);
642 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
643}
644
Vivek Gautam193e67c2018-02-05 23:29:19 +0530645static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
646 size_t size)
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);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000649 arm_lpae_iopte *ptep = data->pgd;
650 int lvl = ARM_LPAE_START_LVL(data);
651
Robin Murphy76557392017-07-03 14:52:24 +0100652 if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias)))
653 return 0;
654
Robin Murphy32b12442017-09-28 15:55:01 +0100655 return __arm_lpae_unmap(data, iova, size, lvl, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000656}
657
658static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
659 unsigned long iova)
660{
661 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
662 arm_lpae_iopte pte, *ptep = data->pgd;
663 int lvl = ARM_LPAE_START_LVL(data);
664
665 do {
666 /* Valid IOPTE pointer? */
667 if (!ptep)
668 return 0;
669
670 /* Grab the IOPTE we're interested in */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100671 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
672 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000673
674 /* Valid entry? */
675 if (!pte)
676 return 0;
677
678 /* Leaf entry? */
Rob Herringd08d42d2019-02-21 14:23:25 -0600679 if (iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000680 goto found_translation;
681
682 /* Take it to the next level */
683 ptep = iopte_deref(pte, data);
684 } while (++lvl < ARM_LPAE_MAX_LEVELS);
685
686 /* Ran out of page tables to walk */
687 return 0;
688
689found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100690 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Robin Murphy6c899282018-03-26 13:35:13 +0100691 return iopte_to_paddr(pte, data) | iova;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000692}
693
694static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
695{
Robin Murphy6c899282018-03-26 13:35:13 +0100696 unsigned long granule, page_sizes;
697 unsigned int max_addr_bits = 48;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000698
699 /*
700 * We need to restrict the supported page sizes to match the
701 * translation regime for a particular granule. Aim to match
702 * the CPU page size if possible, otherwise prefer smaller sizes.
703 * While we're at it, restrict the block sizes to match the
704 * chosen granule.
705 */
706 if (cfg->pgsize_bitmap & PAGE_SIZE)
707 granule = PAGE_SIZE;
708 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
709 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
710 else if (cfg->pgsize_bitmap & PAGE_MASK)
711 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
712 else
713 granule = 0;
714
715 switch (granule) {
716 case SZ_4K:
Robin Murphy6c899282018-03-26 13:35:13 +0100717 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000718 break;
719 case SZ_16K:
Robin Murphy6c899282018-03-26 13:35:13 +0100720 page_sizes = (SZ_16K | SZ_32M);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000721 break;
722 case SZ_64K:
Robin Murphy6c899282018-03-26 13:35:13 +0100723 max_addr_bits = 52;
724 page_sizes = (SZ_64K | SZ_512M);
725 if (cfg->oas > 48)
726 page_sizes |= 1ULL << 42; /* 4TB */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000727 break;
728 default:
Robin Murphy6c899282018-03-26 13:35:13 +0100729 page_sizes = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000730 }
Robin Murphy6c899282018-03-26 13:35:13 +0100731
732 cfg->pgsize_bitmap &= page_sizes;
733 cfg->ias = min(cfg->ias, max_addr_bits);
734 cfg->oas = min(cfg->oas, max_addr_bits);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000735}
736
737static struct arm_lpae_io_pgtable *
738arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
739{
740 unsigned long va_bits, pgd_bits;
741 struct arm_lpae_io_pgtable *data;
742
743 arm_lpae_restrict_pgsizes(cfg);
744
745 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
746 return NULL;
747
748 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
749 return NULL;
750
751 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
752 return NULL;
753
Robin Murphyffcb6d12015-09-17 17:42:16 +0100754 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
755 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
756 return NULL;
757 }
758
Will Deacone1d3c0f2014-11-14 17:18:23 +0000759 data = kmalloc(sizeof(*data), GFP_KERNEL);
760 if (!data)
761 return NULL;
762
763 data->pg_shift = __ffs(cfg->pgsize_bitmap);
764 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
765
766 va_bits = cfg->ias - data->pg_shift;
767 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
768
769 /* Calculate the actual size of our pgd (without concatenation) */
770 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
771 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
772
773 data->iop.ops = (struct io_pgtable_ops) {
774 .map = arm_lpae_map,
775 .unmap = arm_lpae_unmap,
776 .iova_to_phys = arm_lpae_iova_to_phys,
777 };
778
779 return data;
780}
781
782static struct io_pgtable *
783arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
784{
785 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000786 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000787
Will Deacon4f418452019-06-25 12:51:25 +0100788 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
Zhen Leib6b65ca2018-09-20 17:10:24 +0100789 IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000790 return NULL;
791
792 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000793 if (!data)
794 return NULL;
795
796 /* TCR */
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700797 if (cfg->coherent_walk) {
798 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
799 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
800 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
801 } else {
802 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
803 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
804 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
805 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000806
Robin Murphy06c610e2015-12-07 18:18:53 +0000807 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000808 case SZ_4K:
809 reg |= ARM_LPAE_TCR_TG0_4K;
810 break;
811 case SZ_16K:
812 reg |= ARM_LPAE_TCR_TG0_16K;
813 break;
814 case SZ_64K:
815 reg |= ARM_LPAE_TCR_TG0_64K;
816 break;
817 }
818
819 switch (cfg->oas) {
820 case 32:
821 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
822 break;
823 case 36:
824 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
825 break;
826 case 40:
827 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
828 break;
829 case 42:
830 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
831 break;
832 case 44:
833 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
834 break;
835 case 48:
836 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
837 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100838 case 52:
839 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT);
840 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000841 default:
842 goto out_free_data;
843 }
844
845 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000846
847 /* Disable speculative walks through TTBR1 */
848 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000849 cfg->arm_lpae_s1_cfg.tcr = reg;
850
851 /* MAIRs */
852 reg = (ARM_LPAE_MAIR_ATTR_NC
853 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
854 (ARM_LPAE_MAIR_ATTR_WBRWA
855 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
856 (ARM_LPAE_MAIR_ATTR_DEVICE
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530857 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
858 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
859 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000860
861 cfg->arm_lpae_s1_cfg.mair[0] = reg;
862 cfg->arm_lpae_s1_cfg.mair[1] = 0;
863
864 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100865 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000866 if (!data->pgd)
867 goto out_free_data;
868
Robin Murphy87a91b12015-07-29 19:46:09 +0100869 /* Ensure the empty pgd is visible before any actual TTBR write */
870 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000871
872 /* TTBRs */
873 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
874 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
875 return &data->iop;
876
877out_free_data:
878 kfree(data);
879 return NULL;
880}
881
882static struct io_pgtable *
883arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
884{
885 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000886 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000887
Robin Murphy3850db42016-02-12 17:09:46 +0000888 /* The NS quirk doesn't apply at stage 2 */
Will Deacon4f418452019-06-25 12:51:25 +0100889 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000890 return NULL;
891
892 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000893 if (!data)
894 return NULL;
895
896 /*
897 * Concatenate PGDs at level 1 if possible in order to reduce
898 * the depth of the stage-2 walk.
899 */
900 if (data->levels == ARM_LPAE_MAX_LEVELS) {
901 unsigned long pgd_pages;
902
903 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
904 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
905 data->pgd_size = pgd_pages << data->pg_shift;
906 data->levels--;
907 }
908 }
909
910 /* VTCR */
911 reg = ARM_64_LPAE_S2_TCR_RES1 |
912 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
913 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
914 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
915
916 sl = ARM_LPAE_START_LVL(data);
917
Robin Murphy06c610e2015-12-07 18:18:53 +0000918 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000919 case SZ_4K:
920 reg |= ARM_LPAE_TCR_TG0_4K;
921 sl++; /* SL0 format is different for 4K granule size */
922 break;
923 case SZ_16K:
924 reg |= ARM_LPAE_TCR_TG0_16K;
925 break;
926 case SZ_64K:
927 reg |= ARM_LPAE_TCR_TG0_64K;
928 break;
929 }
930
931 switch (cfg->oas) {
932 case 32:
933 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
934 break;
935 case 36:
936 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
937 break;
938 case 40:
939 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
940 break;
941 case 42:
942 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
943 break;
944 case 44:
945 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
946 break;
947 case 48:
948 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
949 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100950 case 52:
951 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT);
952 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000953 default:
954 goto out_free_data;
955 }
956
957 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
958 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
959 cfg->arm_lpae_s2_cfg.vtcr = reg;
960
961 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100962 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000963 if (!data->pgd)
964 goto out_free_data;
965
Robin Murphy87a91b12015-07-29 19:46:09 +0100966 /* Ensure the empty pgd is visible before any actual TTBR write */
967 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000968
969 /* VTTBR */
970 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
971 return &data->iop;
972
973out_free_data:
974 kfree(data);
975 return NULL;
976}
977
978static struct io_pgtable *
979arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
980{
981 struct io_pgtable *iop;
982
983 if (cfg->ias > 32 || cfg->oas > 40)
984 return NULL;
985
986 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
987 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
988 if (iop) {
989 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
990 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
991 }
992
993 return iop;
994}
995
996static struct io_pgtable *
997arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
998{
999 struct io_pgtable *iop;
1000
1001 if (cfg->ias > 40 || cfg->oas > 40)
1002 return NULL;
1003
1004 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1005 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1006 if (iop)
1007 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1008
1009 return iop;
1010}
1011
Rob Herringd08d42d2019-02-21 14:23:25 -06001012static struct io_pgtable *
1013arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1014{
1015 struct io_pgtable *iop;
1016
1017 if (cfg->ias != 48 || cfg->oas > 40)
1018 return NULL;
1019
1020 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1021 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1022 if (iop) {
1023 u64 mair, ttbr;
1024
1025 /* Copy values as union fields overlap */
1026 mair = cfg->arm_lpae_s1_cfg.mair[0];
1027 ttbr = cfg->arm_lpae_s1_cfg.ttbr[0];
1028
1029 cfg->arm_mali_lpae_cfg.memattr = mair;
1030 cfg->arm_mali_lpae_cfg.transtab = ttbr |
1031 ARM_MALI_LPAE_TTBR_READ_INNER |
1032 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1033 }
1034
1035 return iop;
1036}
1037
Will Deacone1d3c0f2014-11-14 17:18:23 +00001038struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1039 .alloc = arm_64_lpae_alloc_pgtable_s1,
1040 .free = arm_lpae_free_pgtable,
1041};
1042
1043struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1044 .alloc = arm_64_lpae_alloc_pgtable_s2,
1045 .free = arm_lpae_free_pgtable,
1046};
1047
1048struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1049 .alloc = arm_32_lpae_alloc_pgtable_s1,
1050 .free = arm_lpae_free_pgtable,
1051};
1052
1053struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1054 .alloc = arm_32_lpae_alloc_pgtable_s2,
1055 .free = arm_lpae_free_pgtable,
1056};
Will Deaconfe4b9912014-11-17 23:31:12 +00001057
Rob Herringd08d42d2019-02-21 14:23:25 -06001058struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1059 .alloc = arm_mali_lpae_alloc_pgtable,
1060 .free = arm_lpae_free_pgtable,
1061};
1062
Will Deaconfe4b9912014-11-17 23:31:12 +00001063#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1064
1065static struct io_pgtable_cfg *cfg_cookie;
1066
1067static void dummy_tlb_flush_all(void *cookie)
1068{
1069 WARN_ON(cookie != cfg_cookie);
1070}
1071
Robin Murphy06c610e2015-12-07 18:18:53 +00001072static void dummy_tlb_add_flush(unsigned long iova, size_t size,
1073 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001074{
1075 WARN_ON(cookie != cfg_cookie);
1076 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1077}
1078
1079static void dummy_tlb_sync(void *cookie)
1080{
1081 WARN_ON(cookie != cfg_cookie);
1082}
1083
Will Deacon298f78892019-07-02 16:43:34 +01001084static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001085 .tlb_flush_all = dummy_tlb_flush_all,
1086 .tlb_add_flush = dummy_tlb_add_flush,
1087 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +00001088};
1089
1090static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1091{
1092 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1093 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1094
1095 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1096 cfg->pgsize_bitmap, cfg->ias);
1097 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1098 data->levels, data->pgd_size, data->pg_shift,
1099 data->bits_per_level, data->pgd);
1100}
1101
1102#define __FAIL(ops, i) ({ \
1103 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1104 arm_lpae_dump_ops(ops); \
1105 selftest_running = false; \
1106 -EFAULT; \
1107})
1108
1109static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1110{
1111 static const enum io_pgtable_fmt fmts[] = {
1112 ARM_64_LPAE_S1,
1113 ARM_64_LPAE_S2,
1114 };
1115
1116 int i, j;
1117 unsigned long iova;
1118 size_t size;
1119 struct io_pgtable_ops *ops;
1120
1121 selftest_running = true;
1122
1123 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1124 cfg_cookie = cfg;
1125 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1126 if (!ops) {
1127 pr_err("selftest: failed to allocate io pgtable ops\n");
1128 return -ENOMEM;
1129 }
1130
1131 /*
1132 * Initial sanity checks.
1133 * Empty page tables shouldn't provide any translations.
1134 */
1135 if (ops->iova_to_phys(ops, 42))
1136 return __FAIL(ops, i);
1137
1138 if (ops->iova_to_phys(ops, SZ_1G + 42))
1139 return __FAIL(ops, i);
1140
1141 if (ops->iova_to_phys(ops, SZ_2G + 42))
1142 return __FAIL(ops, i);
1143
1144 /*
1145 * Distinct mappings of different granule sizes.
1146 */
1147 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +08001148 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001149 size = 1UL << j;
1150
1151 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1152 IOMMU_WRITE |
1153 IOMMU_NOEXEC |
1154 IOMMU_CACHE))
1155 return __FAIL(ops, i);
1156
1157 /* Overlapping mappings */
1158 if (!ops->map(ops, iova, iova + size, size,
1159 IOMMU_READ | IOMMU_NOEXEC))
1160 return __FAIL(ops, i);
1161
1162 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1163 return __FAIL(ops, i);
1164
1165 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001166 }
1167
1168 /* Partial unmap */
1169 size = 1UL << __ffs(cfg->pgsize_bitmap);
1170 if (ops->unmap(ops, SZ_1G + size, size) != size)
1171 return __FAIL(ops, i);
1172
1173 /* Remap of partial unmap */
1174 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1175 return __FAIL(ops, i);
1176
1177 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1178 return __FAIL(ops, i);
1179
1180 /* Full unmap */
1181 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +08001182 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001183 size = 1UL << j;
1184
1185 if (ops->unmap(ops, iova, size) != size)
1186 return __FAIL(ops, i);
1187
1188 if (ops->iova_to_phys(ops, iova + 42))
1189 return __FAIL(ops, i);
1190
1191 /* Remap full block */
1192 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1193 return __FAIL(ops, i);
1194
1195 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1196 return __FAIL(ops, i);
1197
1198 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001199 }
1200
1201 free_io_pgtable_ops(ops);
1202 }
1203
1204 selftest_running = false;
1205 return 0;
1206}
1207
1208static int __init arm_lpae_do_selftests(void)
1209{
1210 static const unsigned long pgsize[] = {
1211 SZ_4K | SZ_2M | SZ_1G,
1212 SZ_16K | SZ_32M,
1213 SZ_64K | SZ_512M,
1214 };
1215
1216 static const unsigned int ias[] = {
1217 32, 36, 40, 42, 44, 48,
1218 };
1219
1220 int i, j, pass = 0, fail = 0;
1221 struct io_pgtable_cfg cfg = {
1222 .tlb = &dummy_tlb_ops,
1223 .oas = 48,
Will Deacon4f418452019-06-25 12:51:25 +01001224 .coherent_walk = true,
Will Deaconfe4b9912014-11-17 23:31:12 +00001225 };
1226
1227 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1228 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1229 cfg.pgsize_bitmap = pgsize[i];
1230 cfg.ias = ias[j];
1231 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1232 pgsize[i], ias[j]);
1233 if (arm_lpae_run_tests(&cfg))
1234 fail++;
1235 else
1236 pass++;
1237 }
1238 }
1239
1240 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1241 return fail ? -EFAULT : 0;
1242}
1243subsys_initcall(arm_lpae_do_selftests);
1244#endif