blob: 94ff319ae8acc5fc306a70e99f8ca5f7bf4e13ab [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
Jean-Philippe Brucker7cef39d2020-09-18 12:18:45 +020023#include "io-pgtable-arm.h"
24
Robin Murphy6c899282018-03-26 13:35:13 +010025#define ARM_LPAE_MAX_ADDR_BITS 52
Will Deacone1d3c0f2014-11-14 17:18:23 +000026#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
27#define ARM_LPAE_MAX_LEVELS 4
28
29/* Struct accessors */
30#define io_pgtable_to_data(x) \
31 container_of((x), struct arm_lpae_io_pgtable, iop)
32
Will Deacone1d3c0f2014-11-14 17:18:23 +000033#define io_pgtable_ops_to_data(x) \
34 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
35
36/*
Will Deacone1d3c0f2014-11-14 17:18:23 +000037 * Calculate the right shift amount to get to the portion describing level l
38 * in a virtual address mapped by the pagetable in d.
39 */
40#define ARM_LPAE_LVL_SHIFT(l,d) \
Robin Murphy5fb190b2019-10-25 19:08:35 +010041 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
42 ilog2(sizeof(arm_lpae_iopte)))
Will Deacone1d3c0f2014-11-14 17:18:23 +000043
Robin Murphy5fb190b2019-10-25 19:08:35 +010044#define ARM_LPAE_GRANULE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
Robin Murphyc79278c2019-10-25 19:08:34 +010046#define ARM_LPAE_PGD_SIZE(d) \
47 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
Will Deacone1d3c0f2014-11-14 17:18:23 +000048
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -070049#define ARM_LPAE_PTES_PER_TABLE(d) \
50 (ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte)))
51
Will Deacone1d3c0f2014-11-14 17:18:23 +000052/*
53 * Calculate the index at level l used to map virtual address a using the
54 * pagetable in d.
55 */
56#define ARM_LPAE_PGD_IDX(l,d) \
Robin Murphyc79278c2019-10-25 19:08:34 +010057 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
Will Deacone1d3c0f2014-11-14 17:18:23 +000058
59#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000060 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000061 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
62
63/* Calculate the block/page mapping size at level l for pagetable in d. */
Robin Murphy5fb190b2019-10-25 19:08:35 +010064#define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000065
66/* Page table bits */
67#define ARM_LPAE_PTE_TYPE_SHIFT 0
68#define ARM_LPAE_PTE_TYPE_MASK 0x3
69
70#define ARM_LPAE_PTE_TYPE_BLOCK 1
71#define ARM_LPAE_PTE_TYPE_TABLE 3
72#define ARM_LPAE_PTE_TYPE_PAGE 3
73
Robin Murphy6c899282018-03-26 13:35:13 +010074#define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
75
Laurent Pinchartc896c1322014-12-14 23:34:50 +020076#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000077#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
78#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
79#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
80#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
81#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c1322014-12-14 23:34:50 +020082#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000083#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
84
85#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
86/* Ignore the contiguous bit for block splitting */
87#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
88#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
89 ARM_LPAE_PTE_ATTR_HI_MASK)
Robin Murphy2c3d2732017-06-22 16:53:54 +010090/* Software bit for solving coherency races */
91#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
Will Deacone1d3c0f2014-11-14 17:18:23 +000092
93/* Stage-1 PTE */
94#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
95#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
96#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
97#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
98
99/* Stage-2 PTE */
100#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
101#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
102#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
103#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
104#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
105#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
106
107/* Register bits */
Robin Murphyfb485eb2019-10-25 19:08:38 +0100108#define ARM_LPAE_VTCR_SL0_MASK 0x3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000109
110#define ARM_LPAE_TCR_T0SZ_SHIFT 0
Will Deacone1d3c0f2014-11-14 17:18:23 +0000111
Robin Murphyfb485eb2019-10-25 19:08:38 +0100112#define ARM_LPAE_VTCR_PS_SHIFT 16
113#define ARM_LPAE_VTCR_PS_MASK 0x7
Will Deacone1d3c0f2014-11-14 17:18:23 +0000114
Will Deacone1d3c0f2014-11-14 17:18:23 +0000115#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
116#define ARM_LPAE_MAIR_ATTR_MASK 0xff
117#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
118#define ARM_LPAE_MAIR_ATTR_NC 0x44
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530119#define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000120#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
121#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
122#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
123#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530124#define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000125
Rob Herringd08d42d2019-02-21 14:23:25 -0600126#define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
127#define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
128#define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
129
Robin Murphy52f325f2019-09-30 15:11:00 +0100130#define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
131#define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
132
Sven Peter892384c2021-08-03 14:16:49 +0200133#define APPLE_DART_PTE_PROT_NO_WRITE (1<<7)
134#define APPLE_DART_PTE_PROT_NO_READ (1<<8)
135
Will Deacone1d3c0f2014-11-14 17:18:23 +0000136/* IOPTE accessors */
Robin Murphy6c899282018-03-26 13:35:13 +0100137#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000138
Kunkun Jiangf37eb482020-12-07 20:01:50 +0800139#define iopte_type(pte) \
Will Deacone1d3c0f2014-11-14 17:18:23 +0000140 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
141
142#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
143
Will Deacone1d3c0f2014-11-14 17:18:23 +0000144struct arm_lpae_io_pgtable {
145 struct io_pgtable iop;
146
Robin Murphyc79278c2019-10-25 19:08:34 +0100147 int pgd_bits;
Robin Murphy594ab902019-10-25 19:08:33 +0100148 int start_level;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100149 int bits_per_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000150
151 void *pgd;
152};
153
154typedef u64 arm_lpae_iopte;
155
Rob Herringd08d42d2019-02-21 14:23:25 -0600156static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
157 enum io_pgtable_fmt fmt)
158{
159 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
Kunkun Jiangf37eb482020-12-07 20:01:50 +0800160 return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE;
Rob Herringd08d42d2019-02-21 14:23:25 -0600161
Kunkun Jiangf37eb482020-12-07 20:01:50 +0800162 return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK;
Rob Herringd08d42d2019-02-21 14:23:25 -0600163}
164
Robin Murphy6c899282018-03-26 13:35:13 +0100165static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
166 struct arm_lpae_io_pgtable *data)
167{
168 arm_lpae_iopte pte = paddr;
169
170 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
171 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
172}
173
174static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
175 struct arm_lpae_io_pgtable *data)
176{
Robin Murphy78688052018-03-29 12:24:52 +0100177 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
Robin Murphy6c899282018-03-26 13:35:13 +0100178
Robin Murphy5fb190b2019-10-25 19:08:35 +0100179 if (ARM_LPAE_GRANULE(data) < SZ_64K)
Robin Murphy6c899282018-03-26 13:35:13 +0100180 return paddr;
181
182 /* Rotate the packed high-order bits back to the top */
183 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
184}
185
Will Deaconfe4b9912014-11-17 23:31:12 +0000186static bool selftest_running = false;
187
Robin Murphyffcb6d12015-09-17 17:42:16 +0100188static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100189{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100190 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100191}
192
193static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
194 struct io_pgtable_cfg *cfg)
195{
196 struct device *dev = cfg->iommu_dev;
Robin Murphy4b123752018-05-22 12:50:09 +0100197 int order = get_order(size);
198 struct page *p;
Robin Murphyf8d54962015-07-29 19:46:04 +0100199 dma_addr_t dma;
Robin Murphy4b123752018-05-22 12:50:09 +0100200 void *pages;
Robin Murphyf8d54962015-07-29 19:46:04 +0100201
Robin Murphy4b123752018-05-22 12:50:09 +0100202 VM_BUG_ON((gfp & __GFP_HIGHMEM));
Jean-Philippe Bruckerfac83d22018-06-18 12:27:54 +0100203 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
204 gfp | __GFP_ZERO, order);
Robin Murphy4b123752018-05-22 12:50:09 +0100205 if (!p)
Robin Murphyf8d54962015-07-29 19:46:04 +0100206 return NULL;
207
Robin Murphy4b123752018-05-22 12:50:09 +0100208 pages = page_address(p);
Will Deacon4f418452019-06-25 12:51:25 +0100209 if (!cfg->coherent_walk) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100210 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
211 if (dma_mapping_error(dev, dma))
212 goto out_free;
213 /*
214 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100215 * address directly, so if the DMA layer suggests otherwise by
216 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100217 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100218 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100219 goto out_unmap;
220 }
221
222 return pages;
223
224out_unmap:
225 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
226 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
227out_free:
Robin Murphy4b123752018-05-22 12:50:09 +0100228 __free_pages(p, order);
Robin Murphyf8d54962015-07-29 19:46:04 +0100229 return NULL;
230}
231
232static void __arm_lpae_free_pages(void *pages, size_t size,
233 struct io_pgtable_cfg *cfg)
234{
Will Deacon4f418452019-06-25 12:51:25 +0100235 if (!cfg->coherent_walk)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100236 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100237 size, DMA_TO_DEVICE);
Robin Murphy4b123752018-05-22 12:50:09 +0100238 free_pages((unsigned long)pages, get_order(size));
Robin Murphyf8d54962015-07-29 19:46:04 +0100239}
240
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700241static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100242 struct io_pgtable_cfg *cfg)
243{
244 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700245 sizeof(*ptep) * num_entries, DMA_TO_DEVICE);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100246}
247
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700248static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100249{
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700250
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700251 *ptep = 0;
Robin Murphyf8d54962015-07-29 19:46:04 +0100252
Will Deacon4f418452019-06-25 12:51:25 +0100253 if (!cfg->coherent_walk)
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700254 __arm_lpae_sync_pte(ptep, 1, cfg);
Robin Murphyf8d54962015-07-29 19:46:04 +0100255}
256
Vivek Gautam193e67c2018-02-05 23:29:19 +0530257static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100258 struct iommu_iotlb_gather *gather,
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700259 unsigned long iova, size_t size, size_t pgcount,
260 int lvl, arm_lpae_iopte *ptep);
Will Deaconcf27ec92015-08-11 16:48:32 +0100261
Robin Murphyfb3a9572017-06-22 16:53:51 +0100262static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
263 phys_addr_t paddr, arm_lpae_iopte prot,
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700264 int lvl, int num_entries, arm_lpae_iopte *ptep)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100265{
266 arm_lpae_iopte pte = prot;
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700267 struct io_pgtable_cfg *cfg = &data->iop.cfg;
268 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
269 int i;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100270
Rob Herringd08d42d2019-02-21 14:23:25 -0600271 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100272 pte |= ARM_LPAE_PTE_TYPE_PAGE;
273 else
274 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
275
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700276 for (i = 0; i < num_entries; i++)
277 ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100278
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700279 if (!cfg->coherent_walk)
280 __arm_lpae_sync_pte(ptep, num_entries, cfg);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100281}
282
Will Deacone1d3c0f2014-11-14 17:18:23 +0000283static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
284 unsigned long iova, phys_addr_t paddr,
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700285 arm_lpae_iopte prot, int lvl, int num_entries,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000286 arm_lpae_iopte *ptep)
287{
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700288 int i;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000289
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700290 for (i = 0; i < num_entries; i++)
291 if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
292 /* We require an unmap first */
293 WARN_ON(!selftest_running);
294 return -EEXIST;
295 } else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
296 /*
297 * We need to unmap and free the old table before
298 * overwriting it with a block entry.
299 */
300 arm_lpae_iopte *tblp;
301 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
Will Deaconcf27ec92015-08-11 16:48:32 +0100302
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700303 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700304 if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1,
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700305 lvl, tblp) != sz) {
306 WARN_ON(1);
307 return -EINVAL;
308 }
Will Deacon3951c412019-07-02 16:45:15 +0100309 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000310
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700311 __arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000312 return 0;
313}
314
Robin Murphyfb3a9572017-06-22 16:53:51 +0100315static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
316 arm_lpae_iopte *ptep,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100317 arm_lpae_iopte curr,
Hector Martin9abe2ac2021-11-20 12:13:43 +0900318 struct arm_lpae_io_pgtable *data)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100319{
Robin Murphy2c3d2732017-06-22 16:53:54 +0100320 arm_lpae_iopte old, new;
Hector Martin9abe2ac2021-11-20 12:13:43 +0900321 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100322
Hector Martin9abe2ac2021-11-20 12:13:43 +0900323 new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100324 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
325 new |= ARM_LPAE_PTE_NSTABLE;
326
Will Deacon77f34452017-06-23 12:02:38 +0100327 /*
328 * Ensure the table itself is visible before its PTE can be.
329 * Whilst we could get away with cmpxchg64_release below, this
330 * doesn't have any ordering semantics when !CONFIG_SMP.
331 */
332 dma_wmb();
Robin Murphy2c3d2732017-06-22 16:53:54 +0100333
334 old = cmpxchg64_relaxed(ptep, curr, new);
335
Will Deacon4f418452019-06-25 12:51:25 +0100336 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
Robin Murphy2c3d2732017-06-22 16:53:54 +0100337 return old;
338
339 /* Even if it's not ours, there's no point waiting; just kick it */
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700340 __arm_lpae_sync_pte(ptep, 1, cfg);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100341 if (old == curr)
342 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
343
344 return old;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100345}
346
Will Deacone1d3c0f2014-11-14 17:18:23 +0000347static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700348 phys_addr_t paddr, size_t size, size_t pgcount,
349 arm_lpae_iopte prot, int lvl, arm_lpae_iopte *ptep,
350 gfp_t gfp, size_t *mapped)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000351{
352 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000353 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100354 size_t tblsz = ARM_LPAE_GRANULE(data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100355 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700356 int ret = 0, num_entries, max_entries, map_idx_start;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000357
358 /* Find our entry at the current level */
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700359 map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
360 ptep += map_idx_start;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000361
362 /* If we can install a leaf entry at this level, then do so */
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700363 if (size == block_size) {
364 max_entries = ARM_LPAE_PTES_PER_TABLE(data) - map_idx_start;
365 num_entries = min_t(int, pgcount, max_entries);
366 ret = arm_lpae_init_pte(data, iova, paddr, prot, lvl, num_entries, ptep);
367 if (!ret && mapped)
368 *mapped += num_entries * size;
369
370 return ret;
371 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000372
373 /* We can't allocate tables at the final level */
374 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
375 return -EINVAL;
376
377 /* Grab a pointer to the next level */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100378 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000379 if (!pte) {
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800380 cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000381 if (!cptep)
382 return -ENOMEM;
383
Hector Martin9abe2ac2021-11-20 12:13:43 +0900384 pte = arm_lpae_install_table(cptep, ptep, 0, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100385 if (pte)
386 __arm_lpae_free_pages(cptep, tblsz, cfg);
Will Deacon4f418452019-06-25 12:51:25 +0100387 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700388 __arm_lpae_sync_pte(ptep, 1, cfg);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100389 }
390
Rob Herringd08d42d2019-02-21 14:23:25 -0600391 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000392 cptep = iopte_deref(pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100393 } else if (pte) {
Oleksandr Tyshchenkoed46e662017-02-27 14:30:25 +0200394 /* We require an unmap first */
395 WARN_ON(!selftest_running);
396 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000397 }
398
399 /* Rinse, repeat */
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700400 return __arm_lpae_map(data, iova, paddr, size, pgcount, prot, lvl + 1,
401 cptep, gfp, mapped);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000402}
403
404static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
405 int prot)
406{
407 arm_lpae_iopte pte;
408
Sven Peter892384c2021-08-03 14:16:49 +0200409 if (data->iop.fmt == APPLE_DART) {
410 pte = 0;
411 if (!(prot & IOMMU_WRITE))
412 pte |= APPLE_DART_PTE_PROT_NO_WRITE;
413 if (!(prot & IOMMU_READ))
414 pte |= APPLE_DART_PTE_PROT_NO_READ;
415 return pte;
416 }
417
Will Deacone1d3c0f2014-11-14 17:18:23 +0000418 if (data->iop.fmt == ARM_64_LPAE_S1 ||
419 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530420 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000421 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
422 pte |= ARM_LPAE_PTE_AP_RDONLY;
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530423 if (!(prot & IOMMU_PRIV))
424 pte |= ARM_LPAE_PTE_AP_UNPRIV;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000425 } else {
426 pte = ARM_LPAE_PTE_HAP_FAULT;
427 if (prot & IOMMU_READ)
428 pte |= ARM_LPAE_PTE_HAP_READ;
429 if (prot & IOMMU_WRITE)
430 pte |= ARM_LPAE_PTE_HAP_WRITE;
Rob Herringd08d42d2019-02-21 14:23:25 -0600431 }
432
433 /*
434 * Note that this logic is structured to accommodate Mali LPAE
435 * having stage-1-like attributes but stage-2-like permissions.
436 */
437 if (data->iop.fmt == ARM_64_LPAE_S2 ||
438 data->iop.fmt == ARM_32_LPAE_S2) {
Robin Murphyfb948252016-04-05 12:39:31 +0100439 if (prot & IOMMU_MMIO)
440 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
441 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000442 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
443 else
444 pte |= ARM_LPAE_PTE_MEMATTR_NC;
Rob Herringd08d42d2019-02-21 14:23:25 -0600445 } else {
446 if (prot & IOMMU_MMIO)
447 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
448 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
449 else if (prot & IOMMU_CACHE)
450 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
451 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000452 }
453
Robin Murphy728da602020-09-22 15:16:48 +0100454 /*
455 * Also Mali has its own notions of shareability wherein its Inner
456 * domain covers the cores within the GPU, and its Outer domain is
457 * "outside the GPU" (i.e. either the Inner or System domain in CPU
458 * terms, depending on coherency).
459 */
460 if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE)
Robin Murphy7618e472020-01-10 15:21:51 +0000461 pte |= ARM_LPAE_PTE_SH_IS;
462 else
463 pte |= ARM_LPAE_PTE_SH_OS;
464
Will Deacone1d3c0f2014-11-14 17:18:23 +0000465 if (prot & IOMMU_NOEXEC)
466 pte |= ARM_LPAE_PTE_XN;
467
Robin Murphy7618e472020-01-10 15:21:51 +0000468 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
469 pte |= ARM_LPAE_PTE_NS;
470
471 if (data->iop.fmt != ARM_MALI_LPAE)
472 pte |= ARM_LPAE_PTE_AF;
473
Will Deacone1d3c0f2014-11-14 17:18:23 +0000474 return pte;
475}
476
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700477static int arm_lpae_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
478 phys_addr_t paddr, size_t pgsize, size_t pgcount,
479 int iommu_prot, gfp_t gfp, size_t *mapped)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000480{
481 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100482 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000483 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100484 int ret, lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000485 arm_lpae_iopte prot;
Robin Murphy08090742020-02-28 14:18:55 +0000486 long iaext = (s64)iova >> cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000487
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700488 if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize))
Robin Murphyf7b90d22019-10-25 19:08:31 +0100489 return -EINVAL;
490
Robin Murphydb690302019-10-25 19:08:39 +0100491 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
492 iaext = ~iaext;
493 if (WARN_ON(iaext || paddr >> cfg->oas))
Robin Murphy76557392017-07-03 14:52:24 +0100494 return -ERANGE;
495
Keqian Zhuf12e0d22020-12-07 19:57:58 +0800496 /* If no access, then nothing to do */
497 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
498 return 0;
499
Will Deacone1d3c0f2014-11-14 17:18:23 +0000500 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700501 ret = __arm_lpae_map(data, iova, paddr, pgsize, pgcount, prot, lvl,
502 ptep, gfp, mapped);
Robin Murphy87a91b12015-07-29 19:46:09 +0100503 /*
504 * Synchronise all PTE updates for the new mapping before there's
505 * a chance for anything to kick off a table walk for the new iova.
506 */
507 wmb();
508
509 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000510}
511
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700512static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
513 phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp)
514{
515 return arm_lpae_map_pages(ops, iova, paddr, size, 1, iommu_prot, gfp,
516 NULL);
517}
518
Will Deacone1d3c0f2014-11-14 17:18:23 +0000519static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
520 arm_lpae_iopte *ptep)
521{
522 arm_lpae_iopte *start, *end;
523 unsigned long table_size;
524
Robin Murphy594ab902019-10-25 19:08:33 +0100525 if (lvl == data->start_level)
Robin Murphyc79278c2019-10-25 19:08:34 +0100526 table_size = ARM_LPAE_PGD_SIZE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000527 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000528 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000529
530 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000531
532 /* Only leaf entries at the last level */
533 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
534 end = ptep;
535 else
536 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000537
538 while (ptep != end) {
539 arm_lpae_iopte pte = *ptep++;
540
Rob Herringd08d42d2019-02-21 14:23:25 -0600541 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000542 continue;
543
544 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
545 }
546
Robin Murphyf8d54962015-07-29 19:46:04 +0100547 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000548}
549
550static void arm_lpae_free_pgtable(struct io_pgtable *iop)
551{
552 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
553
Robin Murphy594ab902019-10-25 19:08:33 +0100554 __arm_lpae_free_pgtable(data, data->start_level, data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000555 kfree(data);
556}
557
Vivek Gautam193e67c2018-02-05 23:29:19 +0530558static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100559 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530560 unsigned long iova, size_t size,
561 arm_lpae_iopte blk_pte, int lvl,
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700562 arm_lpae_iopte *ptep, size_t pgcount)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000563{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100564 struct io_pgtable_cfg *cfg = &data->iop.cfg;
565 arm_lpae_iopte pte, *tablep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000566 phys_addr_t blk_paddr;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100567 size_t tablesz = ARM_LPAE_GRANULE(data);
568 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700569 int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data);
570 int i, unmap_idx_start = -1, num_entries = 0, max_entries;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000571
Robin Murphyfb3a9572017-06-22 16:53:51 +0100572 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
573 return 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000574
Robin Murphyfb3a9572017-06-22 16:53:51 +0100575 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
576 if (!tablep)
577 return 0; /* Bytes unmapped */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000578
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700579 if (size == split_sz) {
580 unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
581 max_entries = ptes_per_table - unmap_idx_start;
582 num_entries = min_t(int, pgcount, max_entries);
583 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100584
Robin Murphy6c899282018-03-26 13:35:13 +0100585 blk_paddr = iopte_to_paddr(blk_pte, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100586 pte = iopte_prot(blk_pte);
587
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700588 for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000589 /* Unmap! */
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700590 if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000591 continue;
592
Isaac J. Manjarres41e1eb22021-06-16 06:38:50 -0700593 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000594 }
595
Hector Martin9abe2ac2021-11-20 12:13:43 +0900596 pte = arm_lpae_install_table(tablep, ptep, blk_pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100597 if (pte != blk_pte) {
598 __arm_lpae_free_pages(tablep, tablesz, cfg);
599 /*
600 * We may race against someone unmapping another part of this
601 * block, but anything else is invalid. We can't misinterpret
602 * a page entry here since we're never at the last level.
603 */
Kunkun Jiangf37eb482020-12-07 20:01:50 +0800604 if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE)
Robin Murphy2c3d2732017-06-22 16:53:54 +0100605 return 0;
606
607 tablep = iopte_deref(pte, data);
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700608 } else if (unmap_idx_start >= 0) {
609 for (i = 0; i < num_entries; i++)
610 io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size);
611
612 return num_entries * size;
Robin Murphy2c3d2732017-06-22 16:53:54 +0100613 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100614
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700615 return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000616}
617
Vivek Gautam193e67c2018-02-05 23:29:19 +0530618static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100619 struct iommu_iotlb_gather *gather,
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700620 unsigned long iova, size_t size, size_t pgcount,
621 int lvl, arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000622{
623 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000624 struct io_pgtable *iop = &data->iop;
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700625 int i = 0, num_entries, max_entries, unmap_idx_start;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000626
Robin Murphy2eb97c72015-12-04 17:52:58 +0000627 /* Something went horribly wrong and we ran out of page table */
628 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
629 return 0;
630
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700631 unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
632 ptep += unmap_idx_start;
Robin Murphy2c3d2732017-06-22 16:53:54 +0100633 pte = READ_ONCE(*ptep);
Robin Murphy2eb97c72015-12-04 17:52:58 +0000634 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000635 return 0;
636
637 /* If the size matches this level, we're in the right place */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100638 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700639 max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start;
640 num_entries = min_t(int, pgcount, max_entries);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000641
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700642 while (i < num_entries) {
643 pte = READ_ONCE(*ptep);
644 if (WARN_ON(!pte))
645 break;
646
647 __arm_lpae_clear_pte(ptep, &iop->cfg);
648
649 if (!iopte_leaf(pte, lvl, iop->fmt)) {
650 /* Also flush any partial walks */
651 io_pgtable_tlb_flush_walk(iop, iova + i * size, size,
652 ARM_LPAE_GRANULE(data));
653 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
Robin Murphyf7403ab2021-08-20 14:14:42 +0100654 } else if (!iommu_iotlb_gather_queued(gather)) {
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700655 io_pgtable_tlb_add_page(iop, gather, iova + i * size, size);
656 }
657
658 ptep++;
659 i++;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000660 }
661
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700662 return i * size;
Rob Herringd08d42d2019-02-21 14:23:25 -0600663 } else if (iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000664 /*
665 * Insert a table at the next level to map the old region,
666 * minus the part we want to unmap
667 */
Will Deacon3951c412019-07-02 16:45:15 +0100668 return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700669 lvl + 1, ptep, pgcount);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000670 }
671
672 /* Keep on walkin' */
673 ptep = iopte_deref(pte, data);
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700674 return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000675}
676
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700677static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
678 size_t pgsize, size_t pgcount,
679 struct iommu_iotlb_gather *gather)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000680{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000681 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100682 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000683 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy08090742020-02-28 14:18:55 +0000684 long iaext = (s64)iova >> cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000685
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700686 if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount))
Robin Murphyf7b90d22019-10-25 19:08:31 +0100687 return 0;
688
Robin Murphydb690302019-10-25 19:08:39 +0100689 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
690 iaext = ~iaext;
691 if (WARN_ON(iaext))
Robin Murphy76557392017-07-03 14:52:24 +0100692 return 0;
693
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700694 return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount,
695 data->start_level, ptep);
696}
697
698static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
699 size_t size, struct iommu_iotlb_gather *gather)
700{
701 return arm_lpae_unmap_pages(ops, iova, size, 1, gather);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000702}
703
704static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
705 unsigned long iova)
706{
707 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
708 arm_lpae_iopte pte, *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100709 int lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000710
711 do {
712 /* Valid IOPTE pointer? */
713 if (!ptep)
714 return 0;
715
716 /* Grab the IOPTE we're interested in */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100717 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
718 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000719
720 /* Valid entry? */
721 if (!pte)
722 return 0;
723
724 /* Leaf entry? */
Rob Herringd08d42d2019-02-21 14:23:25 -0600725 if (iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000726 goto found_translation;
727
728 /* Take it to the next level */
729 ptep = iopte_deref(pte, data);
730 } while (++lvl < ARM_LPAE_MAX_LEVELS);
731
732 /* Ran out of page tables to walk */
733 return 0;
734
735found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100736 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Robin Murphy6c899282018-03-26 13:35:13 +0100737 return iopte_to_paddr(pte, data) | iova;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000738}
739
740static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
741{
Robin Murphy6c899282018-03-26 13:35:13 +0100742 unsigned long granule, page_sizes;
743 unsigned int max_addr_bits = 48;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000744
745 /*
746 * We need to restrict the supported page sizes to match the
747 * translation regime for a particular granule. Aim to match
748 * the CPU page size if possible, otherwise prefer smaller sizes.
749 * While we're at it, restrict the block sizes to match the
750 * chosen granule.
751 */
752 if (cfg->pgsize_bitmap & PAGE_SIZE)
753 granule = PAGE_SIZE;
754 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
755 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
756 else if (cfg->pgsize_bitmap & PAGE_MASK)
757 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
758 else
759 granule = 0;
760
761 switch (granule) {
762 case SZ_4K:
Robin Murphy6c899282018-03-26 13:35:13 +0100763 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000764 break;
765 case SZ_16K:
Robin Murphy6c899282018-03-26 13:35:13 +0100766 page_sizes = (SZ_16K | SZ_32M);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000767 break;
768 case SZ_64K:
Robin Murphy6c899282018-03-26 13:35:13 +0100769 max_addr_bits = 52;
770 page_sizes = (SZ_64K | SZ_512M);
771 if (cfg->oas > 48)
772 page_sizes |= 1ULL << 42; /* 4TB */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000773 break;
774 default:
Robin Murphy6c899282018-03-26 13:35:13 +0100775 page_sizes = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000776 }
Robin Murphy6c899282018-03-26 13:35:13 +0100777
778 cfg->pgsize_bitmap &= page_sizes;
779 cfg->ias = min(cfg->ias, max_addr_bits);
780 cfg->oas = min(cfg->oas, max_addr_bits);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000781}
782
783static struct arm_lpae_io_pgtable *
784arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
785{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000786 struct arm_lpae_io_pgtable *data;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100787 int levels, va_bits, pg_shift;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000788
789 arm_lpae_restrict_pgsizes(cfg);
790
791 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
792 return NULL;
793
794 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
795 return NULL;
796
797 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
798 return NULL;
799
800 data = kmalloc(sizeof(*data), GFP_KERNEL);
801 if (!data)
802 return NULL;
803
Robin Murphy5fb190b2019-10-25 19:08:35 +0100804 pg_shift = __ffs(cfg->pgsize_bitmap);
805 data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000806
Robin Murphy5fb190b2019-10-25 19:08:35 +0100807 va_bits = cfg->ias - pg_shift;
Robin Murphy594ab902019-10-25 19:08:33 +0100808 levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
809 data->start_level = ARM_LPAE_MAX_LEVELS - levels;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000810
811 /* Calculate the actual size of our pgd (without concatenation) */
Robin Murphyc79278c2019-10-25 19:08:34 +0100812 data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000813
814 data->iop.ops = (struct io_pgtable_ops) {
815 .map = arm_lpae_map,
Isaac J. Manjarres4a77b122021-06-16 06:38:52 -0700816 .map_pages = arm_lpae_map_pages,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000817 .unmap = arm_lpae_unmap,
Isaac J. Manjarres1fe27be2021-06-16 06:38:51 -0700818 .unmap_pages = arm_lpae_unmap_pages,
Will Deacone1d3c0f2014-11-14 17:18:23 +0000819 .iova_to_phys = arm_lpae_iova_to_phys,
820 };
821
822 return data;
823}
824
825static struct io_pgtable *
826arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
827{
828 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000829 struct arm_lpae_io_pgtable *data;
Robin Murphyfb485eb2019-10-25 19:08:38 +0100830 typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr;
Robin Murphydb690302019-10-25 19:08:39 +0100831 bool tg1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000832
Will Deacon4f418452019-06-25 12:51:25 +0100833 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
Sai Prakash Ranjane67890c2020-11-25 12:30:11 +0530834 IO_PGTABLE_QUIRK_ARM_TTBR1 |
835 IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
Robin Murphy3850db42016-02-12 17:09:46 +0000836 return NULL;
837
838 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000839 if (!data)
840 return NULL;
841
842 /* TCR */
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700843 if (cfg->coherent_walk) {
Robin Murphyfb485eb2019-10-25 19:08:38 +0100844 tcr->sh = ARM_LPAE_TCR_SH_IS;
845 tcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
846 tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
Sai Prakash Ranjane67890c2020-11-25 12:30:11 +0530847 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)
848 goto out_free_data;
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700849 } else {
Robin Murphyfb485eb2019-10-25 19:08:38 +0100850 tcr->sh = ARM_LPAE_TCR_SH_OS;
851 tcr->irgn = ARM_LPAE_TCR_RGN_NC;
Sai Prakash Ranjane67890c2020-11-25 12:30:11 +0530852 if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
853 tcr->orgn = ARM_LPAE_TCR_RGN_NC;
854 else
855 tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700856 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000857
Robin Murphydb690302019-10-25 19:08:39 +0100858 tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1;
Robin Murphy06c610e2015-12-07 18:18:53 +0000859 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000860 case SZ_4K:
Robin Murphydb690302019-10-25 19:08:39 +0100861 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000862 break;
863 case SZ_16K:
Robin Murphydb690302019-10-25 19:08:39 +0100864 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000865 break;
866 case SZ_64K:
Robin Murphydb690302019-10-25 19:08:39 +0100867 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000868 break;
869 }
870
871 switch (cfg->oas) {
872 case 32:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100873 tcr->ips = ARM_LPAE_TCR_PS_32_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000874 break;
875 case 36:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100876 tcr->ips = ARM_LPAE_TCR_PS_36_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000877 break;
878 case 40:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100879 tcr->ips = ARM_LPAE_TCR_PS_40_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000880 break;
881 case 42:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100882 tcr->ips = ARM_LPAE_TCR_PS_42_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000883 break;
884 case 44:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100885 tcr->ips = ARM_LPAE_TCR_PS_44_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000886 break;
887 case 48:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100888 tcr->ips = ARM_LPAE_TCR_PS_48_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000889 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100890 case 52:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100891 tcr->ips = ARM_LPAE_TCR_PS_52_BIT;
Robin Murphy6c899282018-03-26 13:35:13 +0100892 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000893 default:
894 goto out_free_data;
895 }
896
Robin Murphyfb485eb2019-10-25 19:08:38 +0100897 tcr->tsz = 64ULL - cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000898
899 /* MAIRs */
900 reg = (ARM_LPAE_MAIR_ATTR_NC
901 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
902 (ARM_LPAE_MAIR_ATTR_WBRWA
903 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
904 (ARM_LPAE_MAIR_ATTR_DEVICE
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530905 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
906 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
907 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000908
Robin Murphy205577a2019-10-25 19:08:36 +0100909 cfg->arm_lpae_s1_cfg.mair = reg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000910
911 /* Looking good; allocate a pgd */
Robin Murphyc79278c2019-10-25 19:08:34 +0100912 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
913 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000914 if (!data->pgd)
915 goto out_free_data;
916
Robin Murphy87a91b12015-07-29 19:46:09 +0100917 /* Ensure the empty pgd is visible before any actual TTBR write */
918 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000919
Robin Murphyd1e5f262019-10-25 19:08:37 +0100920 /* TTBR */
921 cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000922 return &data->iop;
923
924out_free_data:
925 kfree(data);
926 return NULL;
927}
928
929static struct io_pgtable *
930arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
931{
Will Deaconac4b80e2020-01-10 14:51:59 +0000932 u64 sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000933 struct arm_lpae_io_pgtable *data;
Will Deaconac4b80e2020-01-10 14:51:59 +0000934 typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000935
Robin Murphy3850db42016-02-12 17:09:46 +0000936 /* The NS quirk doesn't apply at stage 2 */
Robin Murphya8e5f042021-08-11 13:21:29 +0100937 if (cfg->quirks)
Robin Murphy3850db42016-02-12 17:09:46 +0000938 return NULL;
939
940 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000941 if (!data)
942 return NULL;
943
944 /*
945 * Concatenate PGDs at level 1 if possible in order to reduce
946 * the depth of the stage-2 walk.
947 */
Robin Murphy594ab902019-10-25 19:08:33 +0100948 if (data->start_level == 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000949 unsigned long pgd_pages;
950
Robin Murphyc79278c2019-10-25 19:08:34 +0100951 pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000952 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
Robin Murphyc79278c2019-10-25 19:08:34 +0100953 data->pgd_bits += data->bits_per_level;
Robin Murphy594ab902019-10-25 19:08:33 +0100954 data->start_level++;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000955 }
956 }
957
958 /* VTCR */
Will Deacon30d2acb2020-01-10 11:40:33 +0000959 if (cfg->coherent_walk) {
Will Deaconac4b80e2020-01-10 14:51:59 +0000960 vtcr->sh = ARM_LPAE_TCR_SH_IS;
961 vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
962 vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
Will Deacon30d2acb2020-01-10 11:40:33 +0000963 } else {
Will Deaconac4b80e2020-01-10 14:51:59 +0000964 vtcr->sh = ARM_LPAE_TCR_SH_OS;
965 vtcr->irgn = ARM_LPAE_TCR_RGN_NC;
966 vtcr->orgn = ARM_LPAE_TCR_RGN_NC;
Will Deacon30d2acb2020-01-10 11:40:33 +0000967 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000968
Robin Murphy594ab902019-10-25 19:08:33 +0100969 sl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000970
Robin Murphy06c610e2015-12-07 18:18:53 +0000971 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000972 case SZ_4K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000973 vtcr->tg = ARM_LPAE_TCR_TG0_4K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000974 sl++; /* SL0 format is different for 4K granule size */
975 break;
976 case SZ_16K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000977 vtcr->tg = ARM_LPAE_TCR_TG0_16K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000978 break;
979 case SZ_64K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000980 vtcr->tg = ARM_LPAE_TCR_TG0_64K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000981 break;
982 }
983
984 switch (cfg->oas) {
985 case 32:
Will Deaconac4b80e2020-01-10 14:51:59 +0000986 vtcr->ps = ARM_LPAE_TCR_PS_32_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000987 break;
988 case 36:
Will Deaconac4b80e2020-01-10 14:51:59 +0000989 vtcr->ps = ARM_LPAE_TCR_PS_36_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000990 break;
991 case 40:
Will Deaconac4b80e2020-01-10 14:51:59 +0000992 vtcr->ps = ARM_LPAE_TCR_PS_40_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000993 break;
994 case 42:
Will Deaconac4b80e2020-01-10 14:51:59 +0000995 vtcr->ps = ARM_LPAE_TCR_PS_42_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000996 break;
997 case 44:
Will Deaconac4b80e2020-01-10 14:51:59 +0000998 vtcr->ps = ARM_LPAE_TCR_PS_44_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000999 break;
1000 case 48:
Will Deaconac4b80e2020-01-10 14:51:59 +00001001 vtcr->ps = ARM_LPAE_TCR_PS_48_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001002 break;
Robin Murphy6c899282018-03-26 13:35:13 +01001003 case 52:
Will Deaconac4b80e2020-01-10 14:51:59 +00001004 vtcr->ps = ARM_LPAE_TCR_PS_52_BIT;
Robin Murphy6c899282018-03-26 13:35:13 +01001005 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001006 default:
1007 goto out_free_data;
1008 }
1009
Will Deaconac4b80e2020-01-10 14:51:59 +00001010 vtcr->tsz = 64ULL - cfg->ias;
1011 vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK;
Will Deacone1d3c0f2014-11-14 17:18:23 +00001012
1013 /* Allocate pgd pages */
Robin Murphyc79278c2019-10-25 19:08:34 +01001014 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
1015 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001016 if (!data->pgd)
1017 goto out_free_data;
1018
Robin Murphy87a91b12015-07-29 19:46:09 +01001019 /* Ensure the empty pgd is visible before any actual TTBR write */
1020 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +00001021
1022 /* VTTBR */
1023 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1024 return &data->iop;
1025
1026out_free_data:
1027 kfree(data);
1028 return NULL;
1029}
1030
1031static struct io_pgtable *
1032arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1033{
Will Deacone1d3c0f2014-11-14 17:18:23 +00001034 if (cfg->ias > 32 || cfg->oas > 40)
1035 return NULL;
1036
1037 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Robin Murphyfb485eb2019-10-25 19:08:38 +01001038 return arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001039}
1040
1041static struct io_pgtable *
1042arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1043{
Will Deacone1d3c0f2014-11-14 17:18:23 +00001044 if (cfg->ias > 40 || cfg->oas > 40)
1045 return NULL;
1046
1047 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Will Deaconac4b80e2020-01-10 14:51:59 +00001048 return arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +00001049}
1050
Rob Herringd08d42d2019-02-21 14:23:25 -06001051static struct io_pgtable *
1052arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1053{
Robin Murphy52f325f2019-09-30 15:11:00 +01001054 struct arm_lpae_io_pgtable *data;
Rob Herringd08d42d2019-02-21 14:23:25 -06001055
Robin Murphy52f325f2019-09-30 15:11:00 +01001056 /* No quirks for Mali (hopefully) */
1057 if (cfg->quirks)
1058 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001059
Robin Murphy1be08f42019-09-30 15:11:01 +01001060 if (cfg->ias > 48 || cfg->oas > 40)
Rob Herringd08d42d2019-02-21 14:23:25 -06001061 return NULL;
1062
1063 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Rob Herringd08d42d2019-02-21 14:23:25 -06001064
Robin Murphy52f325f2019-09-30 15:11:00 +01001065 data = arm_lpae_alloc_pgtable(cfg);
1066 if (!data)
1067 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001068
Robin Murphy1be08f42019-09-30 15:11:01 +01001069 /* Mali seems to need a full 4-level table regardless of IAS */
Robin Murphy594ab902019-10-25 19:08:33 +01001070 if (data->start_level > 0) {
1071 data->start_level = 0;
Robin Murphyc79278c2019-10-25 19:08:34 +01001072 data->pgd_bits = 0;
Rob Herringd08d42d2019-02-21 14:23:25 -06001073 }
Robin Murphy52f325f2019-09-30 15:11:00 +01001074 /*
1075 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1076 * best we can do is mimic the out-of-tree driver and hope that the
1077 * "implementation-defined caching policy" is good enough. Similarly,
1078 * we'll use it for the sake of a valid attribute for our 'device'
1079 * index, although callers should never request that in practice.
1080 */
1081 cfg->arm_mali_lpae_cfg.memattr =
1082 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1083 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1084 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1085 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1086 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1087 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
Rob Herringd08d42d2019-02-21 14:23:25 -06001088
Robin Murphyc79278c2019-10-25 19:08:34 +01001089 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1090 cfg);
Robin Murphy52f325f2019-09-30 15:11:00 +01001091 if (!data->pgd)
1092 goto out_free_data;
1093
1094 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1095 wmb();
1096
1097 cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1098 ARM_MALI_LPAE_TTBR_READ_INNER |
1099 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
Robin Murphy728da602020-09-22 15:16:48 +01001100 if (cfg->coherent_walk)
1101 cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER;
1102
Robin Murphy52f325f2019-09-30 15:11:00 +01001103 return &data->iop;
1104
1105out_free_data:
1106 kfree(data);
1107 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001108}
1109
Sven Peter892384c2021-08-03 14:16:49 +02001110static struct io_pgtable *
1111apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1112{
1113 struct arm_lpae_io_pgtable *data;
1114 int i;
1115
1116 if (cfg->oas > 36)
1117 return NULL;
1118
1119 data = arm_lpae_alloc_pgtable(cfg);
1120 if (!data)
1121 return NULL;
1122
1123 /*
1124 * The table format itself always uses two levels, but the total VA
1125 * space is mapped by four separate tables, making the MMIO registers
1126 * an effective "level 1". For simplicity, though, we treat this
1127 * equivalently to LPAE stage 2 concatenation at level 2, with the
1128 * additional TTBRs each just pointing at consecutive pages.
1129 */
1130 if (data->start_level < 1)
1131 goto out_free_data;
1132 if (data->start_level == 1 && data->pgd_bits > 2)
1133 goto out_free_data;
1134 if (data->start_level > 1)
1135 data->pgd_bits = 0;
1136 data->start_level = 2;
1137 cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits;
1138 data->pgd_bits += data->bits_per_level;
1139
1140 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1141 cfg);
1142 if (!data->pgd)
1143 goto out_free_data;
1144
1145 for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i)
1146 cfg->apple_dart_cfg.ttbr[i] =
1147 virt_to_phys(data->pgd + i * ARM_LPAE_GRANULE(data));
1148
1149 return &data->iop;
1150
1151out_free_data:
1152 kfree(data);
1153 return NULL;
1154}
1155
Will Deacone1d3c0f2014-11-14 17:18:23 +00001156struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1157 .alloc = arm_64_lpae_alloc_pgtable_s1,
1158 .free = arm_lpae_free_pgtable,
1159};
1160
1161struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1162 .alloc = arm_64_lpae_alloc_pgtable_s2,
1163 .free = arm_lpae_free_pgtable,
1164};
1165
1166struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1167 .alloc = arm_32_lpae_alloc_pgtable_s1,
1168 .free = arm_lpae_free_pgtable,
1169};
1170
1171struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1172 .alloc = arm_32_lpae_alloc_pgtable_s2,
1173 .free = arm_lpae_free_pgtable,
1174};
Will Deaconfe4b9912014-11-17 23:31:12 +00001175
Rob Herringd08d42d2019-02-21 14:23:25 -06001176struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1177 .alloc = arm_mali_lpae_alloc_pgtable,
1178 .free = arm_lpae_free_pgtable,
1179};
1180
Sven Peter892384c2021-08-03 14:16:49 +02001181struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = {
1182 .alloc = apple_dart_alloc_pgtable,
1183 .free = arm_lpae_free_pgtable,
1184};
1185
Will Deaconfe4b9912014-11-17 23:31:12 +00001186#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1187
Robin Murphyb5813c12019-10-25 19:08:30 +01001188static struct io_pgtable_cfg *cfg_cookie __initdata;
Will Deaconfe4b9912014-11-17 23:31:12 +00001189
Robin Murphyb5813c12019-10-25 19:08:30 +01001190static void __init dummy_tlb_flush_all(void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001191{
1192 WARN_ON(cookie != cfg_cookie);
1193}
1194
Robin Murphyb5813c12019-10-25 19:08:30 +01001195static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1196 size_t granule, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001197{
1198 WARN_ON(cookie != cfg_cookie);
1199 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1200}
1201
Robin Murphyb5813c12019-10-25 19:08:30 +01001202static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1203 unsigned long iova, size_t granule,
1204 void *cookie)
Will Deacon10b7a7d2019-07-02 16:44:32 +01001205{
Will Deaconabfd6fe2019-07-02 16:44:41 +01001206 dummy_tlb_flush(iova, granule, granule, cookie);
Will Deacon10b7a7d2019-07-02 16:44:32 +01001207}
1208
Will Deacon298f78892019-07-02 16:43:34 +01001209static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001210 .tlb_flush_all = dummy_tlb_flush_all,
Will Deacon10b7a7d2019-07-02 16:44:32 +01001211 .tlb_flush_walk = dummy_tlb_flush,
Will Deaconabfd6fe2019-07-02 16:44:41 +01001212 .tlb_add_page = dummy_tlb_add_page,
Will Deaconfe4b9912014-11-17 23:31:12 +00001213};
1214
1215static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1216{
1217 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1218 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1219
1220 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1221 cfg->pgsize_bitmap, cfg->ias);
Robin Murphy5fb190b2019-10-25 19:08:35 +01001222 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 +01001223 ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
Robin Murphy5fb190b2019-10-25 19:08:35 +01001224 ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
Will Deaconfe4b9912014-11-17 23:31:12 +00001225}
1226
1227#define __FAIL(ops, i) ({ \
1228 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1229 arm_lpae_dump_ops(ops); \
1230 selftest_running = false; \
1231 -EFAULT; \
1232})
1233
1234static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1235{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001236 static const enum io_pgtable_fmt fmts[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001237 ARM_64_LPAE_S1,
1238 ARM_64_LPAE_S2,
1239 };
1240
1241 int i, j;
1242 unsigned long iova;
1243 size_t size;
1244 struct io_pgtable_ops *ops;
1245
1246 selftest_running = true;
1247
1248 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1249 cfg_cookie = cfg;
1250 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1251 if (!ops) {
1252 pr_err("selftest: failed to allocate io pgtable ops\n");
1253 return -ENOMEM;
1254 }
1255
1256 /*
1257 * Initial sanity checks.
1258 * Empty page tables shouldn't provide any translations.
1259 */
1260 if (ops->iova_to_phys(ops, 42))
1261 return __FAIL(ops, i);
1262
1263 if (ops->iova_to_phys(ops, SZ_1G + 42))
1264 return __FAIL(ops, i);
1265
1266 if (ops->iova_to_phys(ops, SZ_2G + 42))
1267 return __FAIL(ops, i);
1268
1269 /*
1270 * Distinct mappings of different granule sizes.
1271 */
1272 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +08001273 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001274 size = 1UL << j;
1275
1276 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1277 IOMMU_WRITE |
1278 IOMMU_NOEXEC |
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001279 IOMMU_CACHE, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001280 return __FAIL(ops, i);
1281
1282 /* Overlapping mappings */
1283 if (!ops->map(ops, iova, iova + size, size,
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001284 IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001285 return __FAIL(ops, i);
1286
1287 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1288 return __FAIL(ops, i);
1289
1290 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001291 }
1292
1293 /* Partial unmap */
1294 size = 1UL << __ffs(cfg->pgsize_bitmap);
Will Deacona2d3a382019-07-02 16:44:58 +01001295 if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001296 return __FAIL(ops, i);
1297
1298 /* Remap of partial unmap */
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001299 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001300 return __FAIL(ops, i);
1301
1302 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1303 return __FAIL(ops, i);
1304
1305 /* Full unmap */
1306 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +08001307 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001308 size = 1UL << j;
1309
Will Deacona2d3a382019-07-02 16:44:58 +01001310 if (ops->unmap(ops, iova, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001311 return __FAIL(ops, i);
1312
1313 if (ops->iova_to_phys(ops, iova + 42))
1314 return __FAIL(ops, i);
1315
1316 /* Remap full block */
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001317 if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001318 return __FAIL(ops, i);
1319
1320 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1321 return __FAIL(ops, i);
1322
1323 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001324 }
1325
1326 free_io_pgtable_ops(ops);
1327 }
1328
1329 selftest_running = false;
1330 return 0;
1331}
1332
1333static int __init arm_lpae_do_selftests(void)
1334{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001335 static const unsigned long pgsize[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001336 SZ_4K | SZ_2M | SZ_1G,
1337 SZ_16K | SZ_32M,
1338 SZ_64K | SZ_512M,
1339 };
1340
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001341 static const unsigned int ias[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001342 32, 36, 40, 42, 44, 48,
1343 };
1344
1345 int i, j, pass = 0, fail = 0;
1346 struct io_pgtable_cfg cfg = {
1347 .tlb = &dummy_tlb_ops,
1348 .oas = 48,
Will Deacon4f418452019-06-25 12:51:25 +01001349 .coherent_walk = true,
Will Deaconfe4b9912014-11-17 23:31:12 +00001350 };
1351
1352 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1353 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1354 cfg.pgsize_bitmap = pgsize[i];
1355 cfg.ias = ias[j];
1356 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1357 pgsize[i], ias[j]);
1358 if (arm_lpae_run_tests(&cfg))
1359 fail++;
1360 else
1361 pass++;
1362 }
1363 }
1364
1365 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1366 return fail ? -EFAULT : 0;
1367}
1368subsys_initcall(arm_lpae_do_selftests);
1369#endif