blob: 35568804b53044779dc53406083bd7641f8fec54 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 * SPDX-FileCopyrightText: Copyright Arm Limited and Contributors.
5 */
6
7/* This file is derived from xlat_table_v2 library in TF-A project */
8
9#ifndef XLAT_TABLES_H
10#define XLAT_TABLES_H
11
12#ifndef __ASSEMBLER__
13
14#include <memory.h>
15#include <stddef.h>
16#include <stdint.h>
17
18#endif
19
20#include <xlat_contexts.h>
21#include <xlat_defs.h>
22
23#ifndef __ASSEMBLER__
24
25/*
26 * Default granularity size for a struct xlat_mmap_region.
27 * Useful when no specific granularity is required.
28 *
29 * By default, choose the biggest possible block size allowed by the
30 * architectural state and granule size in order to minimize the number of page
31 * tables required for the mapping.
32 */
33#define REGION_DEFAULT_GRANULARITY XLAT_BLOCK_SIZE(MIN_LVL_BLOCK_DESC)
34
35/*
36 * Helper macro to define a struct xlat_mmap_region. This macro allows to
37 * specify all the fields of the structure but its parameter list is not
38 * guaranteed to remain stable as we add members to struct xlat_mmap_region.
39 */
40#define MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr) \
41 { \
42 .base_pa = (_pa), \
43 .base_va = (_va), \
44 .size = (_sz), \
45 .attr = (_attr), \
46 .granularity = (_gr), \
47 }
48
49/* Helper macro to define anstruct xlat_mmap_region. */
50#define MAP_REGION(_pa, _va, _sz, _attr) \
51 MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, REGION_DEFAULT_GRANULARITY)
52
53/* Helper macro to define anstruct xlat_mmap_region with an identity mapping. */
54#define MAP_REGION_FLAT(_adr, _sz, _attr) \
55 MAP_REGION(_adr, _adr, _sz, _attr)
56
57/*
58 * Helper macro to define an mmap_region_t to map with the desired granularity
59 * of translation tables but with invalid page descriptors.
60 *
61 * The granularity value passed to this macro must be a valid block or page
62 * size. When using a 4KB translation granule, this might be 4KB, 2MB or 1GB.
63 * Passing REGION_DEFAULT_GRANULARITY is also allowed and means that the library
64 * is free to choose the granularity for this region.
65 *
66 * This macro can be used to define transient regions where memory used to
67 * reserve VA can be assigned to a PA dynamically. These VA will fault if it
68 * is accessed before a valid PA is assigned to it.
69 */
70
71#define MAP_REGION_TRANSIENT(_va, _sz, _gr) \
72 MAP_REGION_FULL_SPEC(ULL(0), _va, _sz, MT_TRANSIENT, _gr)
73
74/* Definition of an invalid descriptor */
75#define INVALID_DESC UL(0x0)
76
77/*
78 * Shifts and masks to access fields of an mmap attribute
79 */
80#define MT_TYPE_SHIFT UL(0)
81#define MT_TYPE_WIDTH UL(4)
82#define MT_TYPE_MASK MASK(MT_TYPE)
83#define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK)
84/* Access permissions (RO/RW) */
85#define MT_PERM_SHIFT (MT_TYPE_SHIFT + MT_TYPE_WIDTH)
86/* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */
87#define MT_EXECUTE_FLAG_SHIFT (MT_PERM_SHIFT + 1UL)
88
89/* Contiguos descriptor flag */
90#define MT_CONT_SHIFT (MT_EXECUTE_FLAG_SHIFT + 1UL)
91
92/* NG Flag */
93#define MT_NG_SHIFT (MT_CONT_SHIFT + 1UL)
94
95/* Shareability attribute for the memory region */
96#define MT_SHAREABILITY_SHIFT (MT_NG_SHIFT + 1UL)
97#define MT_SHAREABILITY_WIDTH UL(2)
98#define MT_SHAREABILITY_MASK MASK(MT_SHAREABILITY)
99#define MT_SHAREABILITY(_attr) ((_attr) & MT_SHAREABILITY_MASK)
100
101/* Physical address space (REALM/NS, as ROOT/SECURE do not apply to R-EL2) */
102#define MT_PAS_SHIFT (MT_SHAREABILITY_SHIFT + MT_SHAREABILITY_WIDTH)
103#define MT_PAS_WIDTH UL(1)
104#define MT_PAS_MASK MASK(MT_PAS)
105#define MT_PAS(_attr) ((_attr) & MT_PAS_MASK)
106
107/* All other bits are reserved */
108
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000109/* Macro to access translatio table lower attributes */
110#define LOWER_ATTRS(x) (((x) & UL(0xfff)) << UL(2))
111
112/* Public definitions to use with the LOWER_ATTRS() macro*/
113#define NS (U(0x1) << UL(3)) /* Bit[5] absolutely */
114
Soby Mathewb4c6df42022-11-09 11:13:29 +0000115/*
116 * Memory mapping attributes
117 */
118
119/*
120 * Memory types supported.
121 * These are organised so that, going down the list, the memory types are
122 * getting weaker; conversely going up the list the memory types are getting
123 * stronger.
124 */
125#define MT_DEVICE UL(0)
126#define MT_NON_CACHEABLE UL(1)
127#define MT_MEMORY UL(2)
128#define MT_TRANSIENT UL(3)
129/* Values up to 7 are reserved to add new memory types in the future */
130
131#define MT_RO INPLACE(MT_PERM, 0UL)
132#define MT_RW INPLACE(MT_PERM, 1UL)
133
134#define MT_REALM INPLACE(MT_PAS, 0UL)
135#define MT_NS INPLACE(MT_PAS, 1UL)
136
137/*
138 * Access permissions for instruction execution are only relevant for normal
139 * read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored (and potentially
140 * overridden) otherwise:
141 * - Device memory is always marked as execute-never.
142 * - Read-write normal memory is always marked as execute-never.
143 */
144#define MT_EXECUTE INPLACE(MT_EXECUTE_FLAG, 0UL)
145#define MT_EXECUTE_NEVER INPLACE(MT_EXECUTE_FLAG, 1UL)
146
147/*
148 * Shareability defines the visibility of any cache changes to
149 * all masters belonging to a shareable domain.
150 *
151 * MT_SHAREABILITY_ISH: For inner shareable domain
152 * MT_SHAREABILITY_OSH: For outer shareable domain
153 * MT_SHAREABILITY_NSH: For non shareable domain
154 */
155#define MT_SHAREABILITY_ISH INPLACE(MT_SHAREABILITY, 1UL)
156#define MT_SHAREABILITY_OSH INPLACE(MT_SHAREABILITY, 2UL)
157#define MT_SHAREABILITY_NSH INPLACE(MT_SHAREABILITY, 3UL)
158
159#define MT_CONT INPLACE(MT_CONT, 1UL)
160#define MT_NG INPLACE(MT_NG, 1UL)
161
162/* Compound attributes for most common usages */
163#define MT_CODE (MT_MEMORY | MT_SHAREABILITY_ISH \
164 | MT_RO | MT_EXECUTE)
165#define MT_RO_DATA (MT_MEMORY | MT_SHAREABILITY_ISH \
166 | MT_RO | MT_EXECUTE_NEVER)
167#define MT_RW_DATA (MT_MEMORY | MT_SHAREABILITY_ISH \
168 | MT_RW | MT_EXECUTE_NEVER)
169
170/*
171 * Structure for specifying a single region of memory.
172 */
173struct xlat_mmap_region {
174 uintptr_t base_pa; /* Base PA for the current region. */
175 uintptr_t base_va; /* Base VA for the current region. */
176 size_t size; /* Size of the current region. */
177 uint64_t attr; /* Attrs for the current region. */
178 size_t granularity; /* Region granularity. */
179};
180
181/*
182 * Structure containing a table entry and its related information.
183 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000184struct xlat_tbl_info {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 uint64_t *table; /* Pointer to the translation table. */
186 uintptr_t base_va; /* Context base VA for the current entry. */
187 unsigned int level; /* Table level of the current entry. */
188 unsigned int entries; /* Number of entries used by this table. */
189};
190
191/******************************************************************************
192 * Generic translation table APIs.
193 *****************************************************************************/
194
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000195static inline void xlat_write_tte(uint64_t *entry, uint64_t desc)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000196{
197 SCA_WRITE64(entry, desc);
198}
199
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000200static inline uint64_t xlat_read_tte(uint64_t *entry)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000201{
202 return SCA_READ64(entry);
203}
204
205/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000206 * Return a table entry structure given a context and a VA.
207 * The return structure is populated on the retval field.
208 *
209 * This function returns 0 on success or a negative error code otherwise.
210 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000211int xlat_get_table_from_va(struct xlat_tbl_info * const retval,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000212 const struct xlat_ctx * const ctx,
213 const uintptr_t va);
214
215/*
216 * Function to unmap a physical memory page from the descriptor entry and
217 * VA given.
218 * This function implements the "Break" part of the Break-Before-Make semantics
219 * mandated by the Armv8.x architecture in order to update the page descriptors.
220 *
221 * This function returns 0 on success or a negative error code otherwise.
222 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000223int xlat_unmap_memory_page(struct xlat_tbl_info * const table,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000224 const uintptr_t va);
225
226/*
227 * Function to map a physical memory page from the descriptor table entry
228 * and VA given. This function implements the "Make" part of the
229 * Break-Before-Make semantics mandated by the armv8.x architecture in order
230 * to update the page descriptors.
231 *
232 * This function returns 0 on success or a negative error code otherwise.
233 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000234int xlat_map_memory_page_with_attrs(const struct xlat_tbl_info * const table,
Soby Mathewb4c6df42022-11-09 11:13:29 +0000235 const uintptr_t va,
236 const uintptr_t pa,
237 const uint64_t attrs);
238
239/*
240 * This function finds the descriptor entry on a table given the corresponding
241 * table entry structure and the VA for that descriptor.
242 *
243 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000244uint64_t *xlat_get_tte_ptr(const struct xlat_tbl_info * const table,
245 const uintptr_t va);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000246
247/*
248 * Set up the MMU configuration registers for the specified platform parameters.
249 *
250 * This function must be called for each context as it configures the
251 * appropriate TTBRx register depending on it.
252 *
253 * This function also assumes that the contexts for high and low VA halfs share
254 * the same virtual address space as well as the same physical address space,
255 * so it is safe to call it for each context initialization.
256 *
257 * Returns 0 on success or a negative error code otherwise.
258 */
259int xlat_arch_setup_mmu_cfg(struct xlat_ctx * const ctx);
260
261/* MMU control */
262void xlat_enable_mmu_el2(void);
263
Soby Mathewb4c6df42022-11-09 11:13:29 +0000264#endif /*__ASSEMBLER__*/
265#endif /* XLAT_TABLES_H */