blob: 6b09e589ec86680aa2250fe635adbf35aab35a34 [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 */
5
6#include <arch.h>
7#include <arch_helpers.h>
8#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00009#include <buffer.h>
Javier Almansa Sobrino68a593a2022-07-25 09:35:32 +010010#include <buffer_private.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <cpuid.h>
12#include <debug.h>
13#include <errno.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000014#include <granule.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <slot_buf_arch.h>
16#include <stdbool.h>
17#include <stdint.h>
18#include <table.h>
19#include <xlat_contexts.h>
20#include <xlat_tables.h>
21
22/*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000023 * All the slot buffers for a given PE must be mapped by a single translation
Soby Mathewb4c6df42022-11-09 11:13:29 +000024 * table, which means the max VA size should be <= 4KB * 512
25 */
26COMPILER_ASSERT((RMM_SLOT_BUF_VA_SIZE) <= (GRANULE_SIZE * XLAT_TABLE_ENTRIES));
27
28/*
29 * For all translation stages if FEAT_TTST is implemented, while
30 * the PE is executing in AArch64 state and is using 4KB
31 * translation granules, the min address space size is 64KB
32 */
33COMPILER_ASSERT((RMM_SLOT_BUF_VA_SIZE) >= (1 << 16U));
34
35#define RMM_SLOT_BUF_MMAP MAP_REGION_TRANSIENT( \
36 SLOT_VIRT, \
37 RMM_SLOT_BUF_VA_SIZE, \
38 PAGE_SIZE)
39
40#define SLOT_BUF_MMAP_REGIONS UL(1)
41
42/*
43 * Attributes for a buffer slot page descriptor.
44 * Note that the AF bit on the descriptor is handled by the translation
45 * library (it assumes that access faults are not handled) so it does not
46 * need to be specified here.
47 */
Javier Almansa Sobrino765a3162023-04-27 17:42:58 +010048#define SLOT_DESC_ATTR (MT_RW_DATA | MT_NG)
Soby Mathewb4c6df42022-11-09 11:13:29 +000049
50/*
51 * The base tables for all the contexts are manually allocated as a continous
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000052 * block of memory (one L3 table per PE).
Soby Mathewb4c6df42022-11-09 11:13:29 +000053 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000054static uint64_t slot_buf_s1tt[XLAT_TABLE_ENTRIES * MAX_CPUS]
55 __aligned(XLAT_TABLES_ALIGNMENT);
Soby Mathewb4c6df42022-11-09 11:13:29 +000056
57/* Allocate per-cpu xlat_ctx_tbls */
58static struct xlat_ctx_tbls slot_buf_tbls[MAX_CPUS];
59
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000060/* Allocate xlat_ctx_cfg for high VA which will be common to all PEs */
61static struct xlat_ctx_cfg slot_buf_xlat_ctx_cfg;
Soby Mathewb4c6df42022-11-09 11:13:29 +000062
63/* context definition */
64static struct xlat_ctx slot_buf_xlat_ctx[MAX_CPUS];
65
66/*
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000067 * Allocate a cache to store the last level table info where the slot buffers
Soby Mathewb4c6df42022-11-09 11:13:29 +000068 * are mapped to avoid needing to perform a table walk every time a buffer
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000069 * slot operation has to be done.
Soby Mathewb4c6df42022-11-09 11:13:29 +000070 */
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +000071static struct xlat_llt_info llt_info_cache[MAX_CPUS];
Soby Mathewb4c6df42022-11-09 11:13:29 +000072
Javier Almansa Sobrino68a593a2022-07-25 09:35:32 +010073uintptr_t slot_to_va(enum buffer_slot slot)
Soby Mathewb4c6df42022-11-09 11:13:29 +000074{
75 assert(slot < NR_CPU_SLOTS);
76
AlexeiFedorovb80042d2023-08-25 12:02:24 +010077 return (uintptr_t)(SLOT_VIRT + (GRANULE_SIZE * (unsigned int)slot));
Soby Mathewb4c6df42022-11-09 11:13:29 +000078}
79
80static inline struct xlat_ctx *get_slot_buf_xlat_ctx(void)
81{
82 return &slot_buf_xlat_ctx[my_cpuid()];
83}
84
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +000085struct xlat_llt_info *get_cached_llt_info(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +000086{
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +000087 return &llt_info_cache[my_cpuid()];
Soby Mathewb4c6df42022-11-09 11:13:29 +000088}
89
90__unused static uint64_t slot_to_descriptor(enum buffer_slot slot)
91{
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +000092 uint64_t *entry = xlat_get_tte_ptr(get_cached_llt_info(),
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000093 slot_to_va(slot));
AlexeiFedorov2bdd4752023-08-17 16:48:19 +010094 assert(entry != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +000095
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +000096 return xlat_read_tte(entry);
97}
98
99int slot_buf_coldboot_init(void)
100{
101 static struct xlat_mmap_region slot_buf_regions[] = {
102 RMM_SLOT_BUF_MMAP,
103 };
104
105 /*
106 * Initialize the common configuration used for all
107 * translation contexts
108 */
109 return xlat_ctx_cfg_init(&slot_buf_xlat_ctx_cfg, VA_HIGH_REGION,
110 &slot_buf_regions[0], 1U,
111 RMM_SLOT_BUF_VA_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000112}
113
114/*
115 * Setup xlat table for slot buffer mechanism for each PE.
116 * Must be called for every PE in the system
117 */
118void slot_buf_setup_xlat(void)
119{
120 unsigned int cpuid = my_cpuid();
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000121 struct xlat_ctx *slot_buf_ctx = get_slot_buf_xlat_ctx();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000122
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000123 /*
124 * Initialize the translation tables for the current context.
125 * This is done on the first boot of each PE.
126 */
127 int ret = xlat_ctx_init(slot_buf_ctx,
128 &slot_buf_xlat_ctx_cfg,
129 &slot_buf_tbls[cpuid],
130 &slot_buf_s1tt[XLAT_TABLE_ENTRIES * cpuid], 1U);
131
132 if (!((ret == 0) || (ret == -EALREADY))) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000133 /*
134 * If the context was already created, carry on with the
135 * initialization. If it cannot be created, panic.
136 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000137 ERROR("%s (%u): Failed to initialize the xlat context for the slot buffers (-%i)\n",
138 __func__, __LINE__, ret);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139 panic();
140 }
141
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000142 /* Configure MMU registers */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000143 if (xlat_arch_setup_mmu_cfg(get_slot_buf_xlat_ctx())) {
144 ERROR("%s (%u): MMU registers failed to initialize\n",
145 __func__, __LINE__);
146 panic();
147 }
148}
149
150/*
151 * Finishes initializing the slot buffer mechanism.
152 * This function must be called after the MMU is enabled.
153 */
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000154void slot_buf_finish_warmboot_init(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000155{
Javier Almansa Sobrinoed932592023-01-24 12:50:41 +0000156 assert(is_mmu_enabled() == true);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000157
158 /*
159 * Initialize (if not done yet) the internal cache with the last level
160 * translation table that holds the MMU descriptors for the slot
161 * buffers, so we can access them faster when we need to map/unmap.
162 */
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +0000163 if ((get_cached_llt_info())->table == NULL) {
164 if (xlat_get_llt_from_va(get_cached_llt_info(),
165 get_slot_buf_xlat_ctx(),
166 slot_to_va(SLOT_NS)) != 0) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000167 ERROR("%s (%u): Failed to initialize table entry cache for CPU %u\n",
168 __func__, __LINE__, my_cpuid());
169 panic();
170
171 }
172 }
173}
174
175/*
176 * Buffer slots are intended to be transient, and should not be live at
177 * entry/exit of the RMM.
178 */
179void assert_cpu_slots_empty(void)
180{
AlexeiFedorovb80042d2023-08-25 12:02:24 +0100181 for (unsigned int i = 0U; i < (unsigned int)NR_CPU_SLOTS; i++) {
AlexeiFedorov0b67df82023-08-29 15:16:06 +0100182 assert(slot_to_descriptor((enum buffer_slot)i) ==
183 TRANSIENT_DESC);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000184 }
185}
186
187static inline bool is_ns_slot(enum buffer_slot slot)
188{
189 return slot == SLOT_NS;
190}
191
192static inline bool is_realm_slot(enum buffer_slot slot)
193{
194 return (slot != SLOT_NS) && (slot < NR_CPU_SLOTS);
195}
196
197static void *ns_granule_map(enum buffer_slot slot, struct granule *granule)
198{
199 unsigned long addr = granule_addr(granule);
200
201 assert(is_ns_slot(slot));
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000202 return buffer_arch_map(slot, addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000203}
204
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000205static inline void ns_buffer_unmap(void *buf)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000206{
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000207 buffer_arch_unmap(buf);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000208}
209
210/*
211 * Maps a granule @g into the provided @slot, returning
212 * the virtual address.
213 *
214 * The caller must either hold @g::lock or hold a reference.
215 */
216void *granule_map(struct granule *g, enum buffer_slot slot)
217{
218 unsigned long addr = granule_addr(g);
219
220 assert(is_realm_slot(slot));
221
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000222 return buffer_arch_map(slot, addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000223}
224
225void buffer_unmap(void *buf)
226{
227 buffer_arch_unmap(buf);
228}
229
230bool memcpy_ns_read(void *dest, const void *ns_src, unsigned long size);
231bool memcpy_ns_write(void *ns_dest, const void *src, unsigned long size);
232
233/*
234 * Map a Non secure granule @g into the slot @slot and read data from
235 * this granule to @dest. Unmap the granule once the read is done.
236 *
237 * It returns 'true' on success or `false` if not all data are copied.
238 * Only the least significant bits of @offset are considered, which allows the
239 * full PA of a non-granule aligned buffer to be used for the @offset parameter.
240 */
241bool ns_buffer_read(enum buffer_slot slot,
242 struct granule *ns_gr,
243 unsigned int offset,
244 unsigned int size,
245 void *dest)
246{
247 uintptr_t src;
248 bool retval;
249
250 assert(is_ns_slot(slot));
251 assert(ns_gr != NULL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000252 assert(dest != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000253
254 /*
255 * To simplify the trapping mechanism around NS access,
256 * memcpy_ns_read uses a single 8-byte LDR instruction and
257 * all parameters must be aligned accordingly.
258 */
AlexeiFedorovb80042d2023-08-25 12:02:24 +0100259 assert(ALIGNED(size, 8U));
260 assert(ALIGNED(offset, 8U));
261 assert(ALIGNED(dest, 8U));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000262
AlexeiFedorov0b67df82023-08-29 15:16:06 +0100263 offset &= (unsigned int)(~GRANULE_MASK);
264 assert((offset + size) <= GRANULE_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000265
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000266 src = (uintptr_t)ns_granule_map(slot, ns_gr);
267 retval = memcpy_ns_read(dest, (void *)(src + offset), size);
268 ns_buffer_unmap((void *)src);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000269
270 return retval;
271}
272
273/*
274 * Map a Non secure granule @g into the slot @slot and write data from
275 * this granule to @dest. Unmap the granule once the write is done.
276 *
277 * It returns 'true' on success or `false` if not all data are copied.
278 * Only the least significant bits of @offset are considered, which allows the
279 * full PA of a non-granule aligned buffer to be used for the @offset parameter.
280 */
281bool ns_buffer_write(enum buffer_slot slot,
282 struct granule *ns_gr,
283 unsigned int offset,
284 unsigned int size,
285 void *src)
286{
287 uintptr_t dest;
288 bool retval;
289
290 assert(is_ns_slot(slot));
291 assert(ns_gr != NULL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000292 assert(src != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000293
294 /*
295 * To simplify the trapping mechanism around NS access,
296 * memcpy_ns_write uses a single 8-byte STR instruction and
297 * all parameters must be aligned accordingly.
298 */
AlexeiFedorovb80042d2023-08-25 12:02:24 +0100299 assert(ALIGNED(size, 8U));
300 assert(ALIGNED(offset, 8U));
301 assert(ALIGNED(src, 8U));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000302
AlexeiFedorov0b67df82023-08-29 15:16:06 +0100303 offset &= (unsigned int)(~GRANULE_MASK);
304 assert((offset + size) <= GRANULE_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000305
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000306 dest = (uintptr_t)ns_granule_map(slot, ns_gr);
307 retval = memcpy_ns_write((void *)(dest + offset), src, size);
308 ns_buffer_unmap((void *)dest);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000309
310 return retval;
311}
312
313/******************************************************************************
314 * Internal helpers
315 ******************************************************************************/
316
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000317void *buffer_map_internal(enum buffer_slot slot, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000318{
319 uint64_t attr = SLOT_DESC_ATTR;
320 uintptr_t va = slot_to_va(slot);
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +0000321 struct xlat_llt_info *entry = get_cached_llt_info();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000322
323 assert(GRANULE_ALIGNED(addr));
324
AlexeiFedorov0b67df82023-08-29 15:16:06 +0100325 attr |= ((slot == SLOT_NS) ? MT_NS : MT_REALM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000326
327 if (xlat_map_memory_page_with_attrs(entry, va,
328 (uintptr_t)addr, attr) != 0) {
329 /* Error mapping the buffer */
330 return NULL;
331 }
332
333 return (void *)va;
334}
335
336void buffer_unmap_internal(void *buf)
337{
338 /*
339 * Prevent the compiler from moving prior loads/stores to buf after the
340 * update to the translation table. Otherwise, those could fault.
341 */
342 COMPILER_BARRIER();
343
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +0000344 xlat_unmap_memory_page(get_cached_llt_info(), (uintptr_t)buf);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000345}