.. | .. |
---|
1 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
---|
2 | 2 | #ifndef __IO_PGTABLE_H |
---|
3 | 3 | #define __IO_PGTABLE_H |
---|
4 | | -#include <linux/bitops.h> |
---|
5 | 4 | |
---|
6 | | -#include <linux/scatterlist.h> |
---|
| 5 | +#include <linux/bitops.h> |
---|
| 6 | +#include <linux/iommu.h> |
---|
7 | 7 | |
---|
8 | 8 | /* |
---|
9 | 9 | * Public API for use by IOMMU drivers |
---|
.. | .. |
---|
14 | 14 | ARM_64_LPAE_S1, |
---|
15 | 15 | ARM_64_LPAE_S2, |
---|
16 | 16 | ARM_V7S, |
---|
17 | | - ARM_V8L_FAST, |
---|
| 17 | + ARM_MALI_LPAE, |
---|
18 | 18 | IO_PGTABLE_NUM_FMTS, |
---|
19 | 19 | }; |
---|
20 | 20 | |
---|
21 | 21 | /** |
---|
22 | | - * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management. |
---|
| 22 | + * struct iommu_flush_ops - IOMMU callbacks for TLB and page table management. |
---|
23 | 23 | * |
---|
24 | | - * @tlb_flush_all: Synchronously invalidate the entire TLB context. |
---|
25 | | - * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range. |
---|
26 | | - * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and |
---|
27 | | - * any corresponding page table updates are visible to the |
---|
28 | | - * IOMMU. |
---|
29 | | - * @alloc_pages_exact: Allocate page table memory (optional, defaults to |
---|
30 | | - * alloc_pages_exact) |
---|
31 | | - * @free_pages_exact: Free page table memory (optional, defaults to |
---|
32 | | - * free_pages_exact) |
---|
| 24 | + * @tlb_flush_all: Synchronously invalidate the entire TLB context. |
---|
| 25 | + * @tlb_flush_walk: Synchronously invalidate all intermediate TLB state |
---|
| 26 | + * (sometimes referred to as the "walk cache") for a virtual |
---|
| 27 | + * address range. |
---|
| 28 | + * @tlb_add_page: Optional callback to queue up leaf TLB invalidation for a |
---|
| 29 | + * single page. IOMMUs that cannot batch TLB invalidation |
---|
| 30 | + * operations efficiently will typically issue them here, but |
---|
| 31 | + * others may decide to update the iommu_iotlb_gather structure |
---|
| 32 | + * and defer the invalidation until iommu_iotlb_sync() instead. |
---|
33 | 33 | * |
---|
34 | 34 | * Note that these can all be called in atomic context and must therefore |
---|
35 | 35 | * not block. |
---|
36 | 36 | */ |
---|
37 | | -struct iommu_gather_ops { |
---|
| 37 | +struct iommu_flush_ops { |
---|
38 | 38 | void (*tlb_flush_all)(void *cookie); |
---|
39 | | - void (*tlb_add_flush)(unsigned long iova, size_t size, size_t granule, |
---|
40 | | - bool leaf, void *cookie); |
---|
41 | | - void (*tlb_sync)(void *cookie); |
---|
42 | | - void *(*alloc_pages_exact)(void *cookie, size_t size, gfp_t gfp_mask); |
---|
43 | | - void (*free_pages_exact)(void *cookie, void *virt, size_t size); |
---|
| 39 | + void (*tlb_flush_walk)(unsigned long iova, size_t size, size_t granule, |
---|
| 40 | + void *cookie); |
---|
| 41 | + void (*tlb_add_page)(struct iommu_iotlb_gather *gather, |
---|
| 42 | + unsigned long iova, size_t granule, void *cookie); |
---|
44 | 43 | }; |
---|
45 | 44 | |
---|
46 | 45 | /** |
---|
.. | .. |
---|
52 | 51 | * tables. |
---|
53 | 52 | * @ias: Input address (iova) size, in bits. |
---|
54 | 53 | * @oas: Output address (paddr) size, in bits. |
---|
| 54 | + * @coherent_walk A flag to indicate whether or not page table walks made |
---|
| 55 | + * by the IOMMU are coherent with the CPU caches. |
---|
55 | 56 | * @tlb: TLB management callbacks for this set of tables. |
---|
56 | 57 | * @iommu_dev: The device representing the DMA configuration for the |
---|
57 | 58 | * page table walker. |
---|
.. | .. |
---|
67 | 68 | * hardware which does not implement the permissions of a given |
---|
68 | 69 | * format, and/or requires some format-specific default value. |
---|
69 | 70 | * |
---|
70 | | - * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid |
---|
71 | | - * (unmapped) entries but the hardware might do so anyway, perform |
---|
72 | | - * TLB maintenance when mapping as well as when unmapping. |
---|
| 71 | + * IO_PGTABLE_QUIRK_ARM_MTK_EXT: (ARM v7s format) MediaTek IOMMUs extend |
---|
| 72 | + * to support up to 35 bits PA where the bit32, bit33 and bit34 are |
---|
| 73 | + * encoded in the bit9, bit4 and bit5 of the PTE respectively. |
---|
73 | 74 | * |
---|
74 | | - * IO_PGTABLE_QUIRK_ARM_MTK_4GB: (ARM v7s format) Set bit 9 in all |
---|
75 | | - * PTEs, for Mediatek IOMMUs which treat it as a 33rd address bit |
---|
76 | | - * when the SoC is in "4GB mode" and they can only access the high |
---|
77 | | - * remap of DRAM (0x1_00000000 to 0x1_ffffffff). |
---|
| 75 | + * IO_PGTABLE_QUIRK_NON_STRICT: Skip issuing synchronous leaf TLBIs |
---|
| 76 | + * on unmap, for DMA domains using the flush queue mechanism for |
---|
| 77 | + * delayed invalidation. |
---|
78 | 78 | * |
---|
79 | | - |
---|
80 | | - * IO_PGTABLE_QUIRK_NO_DMA: Guarantees that the tables will only ever |
---|
81 | | - * be accessed by a fully cache-coherent IOMMU or CPU (e.g. for a |
---|
82 | | - * software-emulated IOMMU), such that pagetable updates need not |
---|
83 | | - * be treated as explicit DMA data. |
---|
84 | | - * |
---|
85 | | - |
---|
86 | | - * IO_PGTABLE_QUIRK_QSMMUV500_NON_SHAREABLE: |
---|
87 | | - * Having page tables which are non coherent, but cached in a |
---|
88 | | - * system cache requires SH=Non-Shareable. This applies to the |
---|
89 | | - * qsmmuv500 model. For data buffers SH=Non-Shareable is not |
---|
90 | | - * required. |
---|
91 | | - |
---|
92 | | - * IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT: Override the attributes |
---|
93 | | - * set in TCR for the page table walker. Use attributes specified |
---|
94 | | - * by the upstream hw instead. |
---|
95 | | - * |
---|
96 | | - * IO_PGTABLE_QUIRK_QCOM_USE_LLC_NWA: Override the attributes |
---|
97 | | - * set in TCR for the page table walker with Write-Back, |
---|
98 | | - * no Write-Allocate cacheable encoding. |
---|
99 | | - * |
---|
| 79 | + * IO_PGTABLE_QUIRK_ARM_TTBR1: (ARM LPAE format) Configure the table |
---|
| 80 | + * for use in the upper half of a split address space. |
---|
100 | 81 | */ |
---|
101 | 82 | #define IO_PGTABLE_QUIRK_ARM_NS BIT(0) |
---|
102 | 83 | #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1) |
---|
103 | | - #define IO_PGTABLE_QUIRK_TLBI_ON_MAP BIT(2) |
---|
104 | | - #define IO_PGTABLE_QUIRK_ARM_MTK_4GB BIT(3) |
---|
105 | | - #define IO_PGTABLE_QUIRK_NO_DMA BIT(4) |
---|
106 | | - #define IO_PGTABLE_QUIRK_QSMMUV500_NON_SHAREABLE BIT(5) |
---|
107 | | - #define IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT BIT(6) |
---|
108 | | - #define IO_PGTABLE_QUIRK_QCOM_USE_LLC_NWA BIT(7) |
---|
| 84 | + #define IO_PGTABLE_QUIRK_ARM_MTK_EXT BIT(3) |
---|
| 85 | + #define IO_PGTABLE_QUIRK_NON_STRICT BIT(4) |
---|
| 86 | + #define IO_PGTABLE_QUIRK_ARM_TTBR1 BIT(5) |
---|
109 | 87 | unsigned long quirks; |
---|
110 | 88 | unsigned long pgsize_bitmap; |
---|
111 | 89 | unsigned int ias; |
---|
112 | 90 | unsigned int oas; |
---|
113 | | - const struct iommu_gather_ops *tlb; |
---|
| 91 | + bool coherent_walk; |
---|
| 92 | + const struct iommu_flush_ops *tlb; |
---|
114 | 93 | struct device *iommu_dev; |
---|
115 | | - dma_addr_t iova_base; |
---|
116 | | - dma_addr_t iova_end; |
---|
117 | 94 | |
---|
118 | 95 | /* Low-level data specific to the table format */ |
---|
119 | 96 | union { |
---|
120 | 97 | struct { |
---|
121 | | - u64 ttbr[2]; |
---|
122 | | - u64 tcr; |
---|
123 | | - u64 mair[2]; |
---|
| 98 | + u64 ttbr; |
---|
| 99 | + struct { |
---|
| 100 | + u32 ips:3; |
---|
| 101 | + u32 tg:2; |
---|
| 102 | + u32 sh:2; |
---|
| 103 | + u32 orgn:2; |
---|
| 104 | + u32 irgn:2; |
---|
| 105 | + u32 tsz:6; |
---|
| 106 | + } tcr; |
---|
| 107 | + u64 mair; |
---|
124 | 108 | } arm_lpae_s1_cfg; |
---|
125 | 109 | |
---|
126 | 110 | struct { |
---|
127 | 111 | u64 vttbr; |
---|
128 | | - u64 vtcr; |
---|
| 112 | + struct { |
---|
| 113 | + u32 ps:3; |
---|
| 114 | + u32 tg:2; |
---|
| 115 | + u32 sh:2; |
---|
| 116 | + u32 orgn:2; |
---|
| 117 | + u32 irgn:2; |
---|
| 118 | + u32 sl:2; |
---|
| 119 | + u32 tsz:6; |
---|
| 120 | + } vtcr; |
---|
129 | 121 | } arm_lpae_s2_cfg; |
---|
130 | 122 | |
---|
131 | 123 | struct { |
---|
132 | | - u32 ttbr[2]; |
---|
| 124 | + u32 ttbr; |
---|
133 | 125 | u32 tcr; |
---|
134 | 126 | u32 nmrr; |
---|
135 | 127 | u32 prrr; |
---|
136 | 128 | } arm_v7s_cfg; |
---|
137 | 129 | |
---|
138 | 130 | struct { |
---|
139 | | - u64 ttbr[2]; |
---|
140 | | - u64 tcr; |
---|
141 | | - u64 mair[2]; |
---|
142 | | - void *pmds; |
---|
143 | | - } av8l_fast_cfg; |
---|
| 131 | + u64 transtab; |
---|
| 132 | + u64 memattr; |
---|
| 133 | + } arm_mali_lpae_cfg; |
---|
144 | 134 | }; |
---|
145 | 135 | }; |
---|
146 | 136 | |
---|
147 | 137 | /** |
---|
148 | 138 | * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers. |
---|
149 | 139 | * |
---|
150 | | - * @map: Map a physically contiguous memory region. |
---|
151 | | - * @map_sg: Map a scatterlist. Returns the number of bytes mapped, |
---|
152 | | - * or -ve val on failure. The size parameter contains the |
---|
153 | | - * size of the partial mapping in case of failure. |
---|
154 | | - * @unmap: Unmap a physically contiguous memory region. |
---|
155 | | - * @iova_to_phys: Translate iova to physical address. |
---|
156 | | - * @is_iova_coherent: Checks coherency of given IOVA. Returns True if coherent |
---|
157 | | - * and False if non-coherent. |
---|
158 | | - * @iova_to_pte: Translate iova to Page Table Entry (PTE). |
---|
| 140 | + * @map: Map a physically contiguous memory region. |
---|
| 141 | + * @map_pages: Map a physically contiguous range of pages of the same size. |
---|
| 142 | + * @map_sg: Map a scatter-gather list of physically contiguous memory |
---|
| 143 | + * chunks. The mapped pointer argument is used to store how |
---|
| 144 | + * many bytes are mapped. |
---|
| 145 | + * @unmap: Unmap a physically contiguous memory region. |
---|
| 146 | + * @unmap_pages: Unmap a range of virtually contiguous pages of the same size. |
---|
| 147 | + * @iova_to_phys: Translate iova to physical address. |
---|
159 | 148 | * |
---|
160 | 149 | * These functions map directly onto the iommu_ops member functions with |
---|
161 | 150 | * the same names. |
---|
162 | 151 | */ |
---|
163 | 152 | struct io_pgtable_ops { |
---|
164 | 153 | int (*map)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
165 | | - phys_addr_t paddr, size_t size, int prot); |
---|
166 | | - size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
167 | | - size_t size); |
---|
| 154 | + phys_addr_t paddr, size_t size, int prot, gfp_t gfp); |
---|
| 155 | + int (*map_pages)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
| 156 | + phys_addr_t paddr, size_t pgsize, size_t pgcount, |
---|
| 157 | + int prot, gfp_t gfp, size_t *mapped); |
---|
168 | 158 | int (*map_sg)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
169 | | - struct scatterlist *sg, unsigned int nents, |
---|
170 | | - int prot, size_t *size); |
---|
| 159 | + struct scatterlist *sg, unsigned int nents, int prot, |
---|
| 160 | + gfp_t gfp, size_t *mapped); |
---|
| 161 | + size_t (*unmap)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
| 162 | + size_t size, struct iommu_iotlb_gather *gather); |
---|
| 163 | + size_t (*unmap_pages)(struct io_pgtable_ops *ops, unsigned long iova, |
---|
| 164 | + size_t pgsize, size_t pgcount, |
---|
| 165 | + struct iommu_iotlb_gather *gather); |
---|
171 | 166 | phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops, |
---|
172 | 167 | unsigned long iova); |
---|
173 | | - bool (*is_iova_coherent)(struct io_pgtable_ops *ops, |
---|
174 | | - unsigned long iova); |
---|
175 | | - uint64_t (*iova_to_pte)(struct io_pgtable_ops *ops, |
---|
176 | | - unsigned long iova); |
---|
177 | | - |
---|
178 | 168 | }; |
---|
179 | 169 | |
---|
180 | 170 | /** |
---|
.. | .. |
---|
225 | 215 | |
---|
226 | 216 | static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop) |
---|
227 | 217 | { |
---|
228 | | - if (!iop->cfg.tlb) |
---|
229 | | - return; |
---|
230 | | - iop->cfg.tlb->tlb_flush_all(iop->cookie); |
---|
| 218 | + if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_all) |
---|
| 219 | + iop->cfg.tlb->tlb_flush_all(iop->cookie); |
---|
231 | 220 | } |
---|
232 | 221 | |
---|
233 | | -static inline void io_pgtable_tlb_add_flush(struct io_pgtable *iop, |
---|
234 | | - unsigned long iova, size_t size, size_t granule, bool leaf) |
---|
| 222 | +static inline void |
---|
| 223 | +io_pgtable_tlb_flush_walk(struct io_pgtable *iop, unsigned long iova, |
---|
| 224 | + size_t size, size_t granule) |
---|
235 | 225 | { |
---|
236 | | - if (!iop->cfg.tlb) |
---|
237 | | - return; |
---|
238 | | - iop->cfg.tlb->tlb_add_flush(iova, size, granule, leaf, iop->cookie); |
---|
| 226 | + if (iop->cfg.tlb && iop->cfg.tlb->tlb_flush_walk) |
---|
| 227 | + iop->cfg.tlb->tlb_flush_walk(iova, size, granule, iop->cookie); |
---|
239 | 228 | } |
---|
240 | 229 | |
---|
241 | | -static inline void io_pgtable_tlb_sync(struct io_pgtable *iop) |
---|
| 230 | +static inline void |
---|
| 231 | +io_pgtable_tlb_add_page(struct io_pgtable *iop, |
---|
| 232 | + struct iommu_iotlb_gather * gather, unsigned long iova, |
---|
| 233 | + size_t granule) |
---|
242 | 234 | { |
---|
243 | | - if (!iop->cfg.tlb) |
---|
244 | | - return; |
---|
245 | | - iop->cfg.tlb->tlb_sync(iop->cookie); |
---|
| 235 | + if (iop->cfg.tlb && iop->cfg.tlb->tlb_add_page) |
---|
| 236 | + iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie); |
---|
246 | 237 | } |
---|
247 | 238 | |
---|
248 | 239 | /** |
---|
.. | .. |
---|
262 | 253 | extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns; |
---|
263 | 254 | extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; |
---|
264 | 255 | extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns; |
---|
265 | | -extern struct io_pgtable_init_fns io_pgtable_av8l_fast_init_fns; |
---|
266 | | -extern struct io_pgtable_init_fns io_pgtable_arm_msm_secure_init_fns; |
---|
267 | | - |
---|
268 | | -/** |
---|
269 | | - * io_pgtable_alloc_pages_exact: |
---|
270 | | - * allocate an exact number of physically-contiguous pages. |
---|
271 | | - * @size: the number of bytes to allocate |
---|
272 | | - * @gfp_mask: GFP flags for the allocation |
---|
273 | | - * |
---|
274 | | - * Like alloc_pages_exact(), but with some additional accounting for debug |
---|
275 | | - * purposes. |
---|
276 | | - */ |
---|
277 | | -void *io_pgtable_alloc_pages_exact(struct io_pgtable_cfg *cfg, void *cookie, |
---|
278 | | - size_t size, gfp_t gfp_mask); |
---|
279 | | - |
---|
280 | | -/** |
---|
281 | | - * io_pgtable_free_pages_exact: |
---|
282 | | - * release memory allocated via io_pgtable_alloc_pages_exact() |
---|
283 | | - * @virt: the value returned by alloc_pages_exact. |
---|
284 | | - * @size: size of allocation, same value as passed to alloc_pages_exact(). |
---|
285 | | - * |
---|
286 | | - * Like free_pages_exact(), but with some additional accounting for debug |
---|
287 | | - * purposes. |
---|
288 | | - */ |
---|
289 | | -void io_pgtable_free_pages_exact(struct io_pgtable_cfg *cfg, void *cookie, |
---|
290 | | - void *virt, size_t size); |
---|
| 256 | +extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns; |
---|
291 | 257 | |
---|
292 | 258 | #endif /* __IO_PGTABLE_H */ |
---|