blob: f5aa1d8e65af8d1b4bb03c39d43d93e90ba3d83f [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
77 return (uintptr_t)(SLOT_VIRT + (GRANULE_SIZE * slot));
78}
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{
181 unsigned int i;
182
183 for (i = 0; i < NR_CPU_SLOTS; i++) {
Javier Almansa Sobrino66af7f72023-02-16 11:42:05 +0000184 assert(slot_to_descriptor(i) == TRANSIENT_DESC);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 }
186}
187
188static inline bool is_ns_slot(enum buffer_slot slot)
189{
190 return slot == SLOT_NS;
191}
192
193static inline bool is_realm_slot(enum buffer_slot slot)
194{
195 return (slot != SLOT_NS) && (slot < NR_CPU_SLOTS);
196}
197
198static void *ns_granule_map(enum buffer_slot slot, struct granule *granule)
199{
200 unsigned long addr = granule_addr(granule);
201
202 assert(is_ns_slot(slot));
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000203 return buffer_arch_map(slot, addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000204}
205
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000206static inline void ns_buffer_unmap(void *buf)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000207{
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000208 buffer_arch_unmap(buf);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000209}
210
211/*
212 * Maps a granule @g into the provided @slot, returning
213 * the virtual address.
214 *
215 * The caller must either hold @g::lock or hold a reference.
216 */
217void *granule_map(struct granule *g, enum buffer_slot slot)
218{
219 unsigned long addr = granule_addr(g);
220
221 assert(is_realm_slot(slot));
222
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000223 return buffer_arch_map(slot, addr);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000224}
225
226void buffer_unmap(void *buf)
227{
228 buffer_arch_unmap(buf);
229}
230
231bool memcpy_ns_read(void *dest, const void *ns_src, unsigned long size);
232bool memcpy_ns_write(void *ns_dest, const void *src, unsigned long size);
233
234/*
235 * Map a Non secure granule @g into the slot @slot and read data from
236 * this granule to @dest. Unmap the granule once the read is done.
237 *
238 * It returns 'true' on success or `false` if not all data are copied.
239 * Only the least significant bits of @offset are considered, which allows the
240 * full PA of a non-granule aligned buffer to be used for the @offset parameter.
241 */
242bool ns_buffer_read(enum buffer_slot slot,
243 struct granule *ns_gr,
244 unsigned int offset,
245 unsigned int size,
246 void *dest)
247{
248 uintptr_t src;
249 bool retval;
250
251 assert(is_ns_slot(slot));
252 assert(ns_gr != NULL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000253 assert(dest != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000254
255 /*
256 * To simplify the trapping mechanism around NS access,
257 * memcpy_ns_read uses a single 8-byte LDR instruction and
258 * all parameters must be aligned accordingly.
259 */
260 assert(ALIGNED(size, 8));
261 assert(ALIGNED(offset, 8));
262 assert(ALIGNED(dest, 8));
263
264 offset &= ~GRANULE_MASK;
265 assert(offset + size <= GRANULE_SIZE);
266
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000267 src = (uintptr_t)ns_granule_map(slot, ns_gr);
268 retval = memcpy_ns_read(dest, (void *)(src + offset), size);
269 ns_buffer_unmap((void *)src);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000270
271 return retval;
272}
273
274/*
275 * Map a Non secure granule @g into the slot @slot and write data from
276 * this granule to @dest. Unmap the granule once the write is done.
277 *
278 * It returns 'true' on success or `false` if not all data are copied.
279 * Only the least significant bits of @offset are considered, which allows the
280 * full PA of a non-granule aligned buffer to be used for the @offset parameter.
281 */
282bool ns_buffer_write(enum buffer_slot slot,
283 struct granule *ns_gr,
284 unsigned int offset,
285 unsigned int size,
286 void *src)
287{
288 uintptr_t dest;
289 bool retval;
290
291 assert(is_ns_slot(slot));
292 assert(ns_gr != NULL);
Javier Almansa Sobrino1948e692023-01-16 17:10:38 +0000293 assert(src != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000294
295 /*
296 * To simplify the trapping mechanism around NS access,
297 * memcpy_ns_write uses a single 8-byte STR instruction and
298 * all parameters must be aligned accordingly.
299 */
300 assert(ALIGNED(size, 8));
301 assert(ALIGNED(offset, 8));
302 assert(ALIGNED(src, 8));
303
304 offset &= ~GRANULE_MASK;
305 assert(offset + size <= GRANULE_SIZE);
306
Javier Almansa Sobrinobb66f8a2023-01-05 16:43:43 +0000307 dest = (uintptr_t)ns_granule_map(slot, ns_gr);
308 retval = memcpy_ns_write((void *)(dest + offset), src, size);
309 ns_buffer_unmap((void *)dest);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000310
311 return retval;
312}
313
314/******************************************************************************
315 * Internal helpers
316 ******************************************************************************/
317
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000318void *buffer_map_internal(enum buffer_slot slot, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000319{
320 uint64_t attr = SLOT_DESC_ATTR;
321 uintptr_t va = slot_to_va(slot);
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +0000322 struct xlat_llt_info *entry = get_cached_llt_info();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000323
324 assert(GRANULE_ALIGNED(addr));
325
Javier Almansa Sobrinod528efd2023-01-05 16:23:54 +0000326 attr |= (slot == SLOT_NS ? MT_NS : MT_REALM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000327
328 if (xlat_map_memory_page_with_attrs(entry, va,
329 (uintptr_t)addr, attr) != 0) {
330 /* Error mapping the buffer */
331 return NULL;
332 }
333
334 return (void *)va;
335}
336
337void buffer_unmap_internal(void *buf)
338{
339 /*
340 * Prevent the compiler from moving prior loads/stores to buf after the
341 * update to the translation table. Otherwise, those could fault.
342 */
343 COMPILER_BARRIER();
344
Javier Almansa Sobrinoe7aa1ab2023-03-09 17:38:02 +0000345 xlat_unmap_memory_page(get_cached_llt_info(), (uintptr_t)buf);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000346}