blob: 53fc874d8797565e56f7f90545450df4b6081b2f [file] [log] [blame]
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001/*
Antonio Nino Diaz3f518922018-01-05 11:30:36 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00005 */
6
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00007#include <arch_helpers.h>
8#include <assert.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00009#include <debug.h>
10#include <errno.h>
11#include <platform_def.h>
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010012#include <stdbool.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010013#include <stdint.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000014#include <string.h>
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010015#include <utils_def.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010016#include <xlat_tables_defs.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000017#include <xlat_tables_v2.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010018
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000019#include "xlat_tables_private.h"
20
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +010021/* Helper function that cleans the data cache only if it is enabled. */
22static inline void xlat_clean_dcache_range(uintptr_t addr, size_t size)
23{
24 if (is_dcache_enabled())
25 clean_dcache_range(addr, size);
26}
27
Antonio Nino Diazac998032017-02-27 17:23:54 +000028#if PLAT_XLAT_TABLES_DYNAMIC
29
30/*
31 * The following functions assume that they will be called using subtables only.
32 * The base table can't be unmapped, so it is not needed to do any special
33 * handling for it.
34 */
35
36/*
37 * Returns the index of the array corresponding to the specified translation
38 * table.
39 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010040static int xlat_table_get_index(const xlat_ctx_t *ctx, const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000041{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010042 for (int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000043 if (ctx->tables[i] == table)
44 return i;
45
46 /*
47 * Maybe we were asked to get the index of the base level table, which
48 * should never happen.
49 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010050 assert(false);
Antonio Nino Diazac998032017-02-27 17:23:54 +000051
52 return -1;
53}
54
55/* Returns a pointer to an empty translation table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010056static uint64_t *xlat_table_get_empty(const xlat_ctx_t *ctx)
Antonio Nino Diazac998032017-02-27 17:23:54 +000057{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010058 for (int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000059 if (ctx->tables_mapped_regions[i] == 0)
60 return ctx->tables[i];
61
62 return NULL;
63}
64
65/* Increments region count for a given table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010066static void xlat_table_inc_regions_count(const xlat_ctx_t *ctx,
67 const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000068{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010069 int idx = xlat_table_get_index(ctx, table);
70
71 ctx->tables_mapped_regions[idx]++;
Antonio Nino Diazac998032017-02-27 17:23:54 +000072}
73
74/* Decrements region count for a given table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010075static void xlat_table_dec_regions_count(const xlat_ctx_t *ctx,
76 const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000077{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010078 int idx = xlat_table_get_index(ctx, table);
79
80 ctx->tables_mapped_regions[idx]--;
Antonio Nino Diazac998032017-02-27 17:23:54 +000081}
82
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010083/* Returns 0 if the specified table isn't empty, otherwise 1. */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010084static bool xlat_table_is_empty(const xlat_ctx_t *ctx, const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000085{
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010086 return ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)] == 0;
Antonio Nino Diazac998032017-02-27 17:23:54 +000087}
88
89#else /* PLAT_XLAT_TABLES_DYNAMIC */
90
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000091/* Returns a pointer to the first empty translation table. */
92static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
93{
94 assert(ctx->next_table < ctx->tables_num);
95
96 return ctx->tables[ctx->next_table++];
97}
98
Antonio Nino Diazac998032017-02-27 17:23:54 +000099#endif /* PLAT_XLAT_TABLES_DYNAMIC */
100
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100101/*
102 * Returns a block/page table descriptor for the given level and attributes.
103 */
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100104uint64_t xlat_desc(const xlat_ctx_t *ctx, uint32_t attr,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100105 unsigned long long addr_pa, unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000106{
107 uint64_t desc;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100108 uint32_t mem_type;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000109
110 /* Make sure that the granularity is fine enough to map this address. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100111 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000112
113 desc = addr_pa;
114 /*
115 * There are different translation table descriptors for level 3 and the
116 * rest.
117 */
118 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
119 /*
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100120 * Always set the access flag, as this library assumes access flag
121 * faults aren't managed.
122 */
123 desc |= LOWER_ATTRS(ACCESS_FLAG);
124 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000125 * Deduce other fields of the descriptor based on the MT_NS and MT_RW
126 * memory region attributes.
127 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100128 desc |= ((attr & MT_NS) != 0U) ? LOWER_ATTRS(NS) : 0U;
129 desc |= ((attr & MT_RW) != 0U) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000130
131 /*
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100132 * Do not allow unprivileged access when the mapping is for a privileged
133 * EL. For translation regimes that do not have mappings for access for
134 * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
135 */
136 if (ctx->xlat_regime == EL1_EL0_REGIME) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100137 if ((attr & MT_USER) != 0U) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100138 /* EL0 mapping requested, so we give User access */
139 desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
140 } else {
141 /* EL1 mapping requested, no User access granted */
142 desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
143 }
144 } else {
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100145 assert((ctx->xlat_regime == EL2_REGIME) ||
146 (ctx->xlat_regime == EL3_REGIME));
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100147 desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100148 }
149
150 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000151 * Deduce shareability domain and executability of the memory region
152 * from the memory type of the attributes (MT_TYPE).
153 *
154 * Data accesses to device memory and non-cacheable normal memory are
155 * coherent for all observers in the system, and correspondingly are
156 * always treated as being Outer Shareable. Therefore, for these 2 types
157 * of memory, it is not strictly needed to set the shareability field
158 * in the translation tables.
159 */
160 mem_type = MT_TYPE(attr);
161 if (mem_type == MT_DEVICE) {
162 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
163 /*
164 * Always map device memory as execute-never.
165 * This is to avoid the possibility of a speculative instruction
166 * fetch, which could be an issue if this memory region
167 * corresponds to a read-sensitive peripheral.
168 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100169 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100170
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000171 } else { /* Normal memory */
172 /*
173 * Always map read-write normal memory as execute-never.
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100174 * This library assumes that it is used by software that does
175 * not self-modify its code, therefore R/W memory is reserved
176 * for data storage, which must not be executable.
177 *
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000178 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100179 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000180 * which makes any writable memory region to be treated as
181 * execute-never, regardless of the value of the XN bit in the
182 * translation table.
183 *
184 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100185 * attribute to figure out the value of the XN bit. The actual
186 * XN bit(s) to set in the descriptor depends on the context's
187 * translation regime and the policy applied in
188 * xlat_arch_regime_get_xn_desc().
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000189 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100190 if (((attr & MT_RW) != 0U) || ((attr & MT_EXECUTE_NEVER) != 0U)) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100191 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100192 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000193
194 if (mem_type == MT_MEMORY) {
195 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
196 } else {
197 assert(mem_type == MT_NON_CACHEABLE);
198 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
199 }
200 }
201
202 return desc;
203}
204
205/*
206 * Enumeration of actions that can be made when mapping table entries depending
207 * on the previous value in that entry and information about the region being
208 * mapped.
209 */
210typedef enum {
211
212 /* Do nothing */
213 ACTION_NONE,
214
215 /* Write a block (or page, if in level 3) entry. */
216 ACTION_WRITE_BLOCK_ENTRY,
217
218 /*
219 * Create a new table and write a table entry pointing to it. Recurse
220 * into it for further processing.
221 */
222 ACTION_CREATE_NEW_TABLE,
223
224 /*
225 * There is a table descriptor in this entry, read it and recurse into
226 * that table for further processing.
227 */
228 ACTION_RECURSE_INTO_TABLE,
229
230} action_t;
231
Antonio Nino Diazac998032017-02-27 17:23:54 +0000232#if PLAT_XLAT_TABLES_DYNAMIC
233
234/*
235 * Recursive function that writes to the translation tables and unmaps the
236 * specified region.
237 */
238static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
239 const uintptr_t table_base_va,
240 uint64_t *const table_base,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100241 const unsigned int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700242 const unsigned int level)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000243{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100244 assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
Antonio Nino Diazac998032017-02-27 17:23:54 +0000245
246 uint64_t *subtable;
247 uint64_t desc;
248
249 uintptr_t table_idx_va;
250 uintptr_t table_idx_end_va; /* End VA of this entry */
251
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100252 uintptr_t region_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000253
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100254 unsigned int table_idx;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000255
256 if (mm->base_va > table_base_va) {
257 /* Find the first index of the table affected by the region. */
258 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
259
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100260 table_idx = (unsigned int)((table_idx_va - table_base_va) >>
261 XLAT_ADDR_SHIFT(level));
Antonio Nino Diazac998032017-02-27 17:23:54 +0000262
263 assert(table_idx < table_entries);
264 } else {
265 /* Start from the beginning of the table. */
266 table_idx_va = table_base_va;
267 table_idx = 0;
268 }
269
270 while (table_idx < table_entries) {
271
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100272 table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(level) - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000273
274 desc = table_base[table_idx];
275 uint64_t desc_type = desc & DESC_MASK;
276
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100277 action_t action;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000278
279 if ((mm->base_va <= table_idx_va) &&
280 (region_end_va >= table_idx_end_va)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000281 /* Region covers all block */
282
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100283 if (level == 3U) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000284 /*
285 * Last level, only page descriptors allowed,
286 * erase it.
287 */
288 assert(desc_type == PAGE_DESC);
289
290 action = ACTION_WRITE_BLOCK_ENTRY;
291 } else {
292 /*
293 * Other levels can have table descriptors. If
294 * so, recurse into it and erase descriptors
295 * inside it as needed. If there is a block
296 * descriptor, just erase it. If an invalid
297 * descriptor is found, this table isn't
298 * actually mapped, which shouldn't happen.
299 */
300 if (desc_type == TABLE_DESC) {
301 action = ACTION_RECURSE_INTO_TABLE;
302 } else {
303 assert(desc_type == BLOCK_DESC);
304 action = ACTION_WRITE_BLOCK_ENTRY;
305 }
306 }
307
308 } else if ((mm->base_va <= table_idx_end_va) ||
309 (region_end_va >= table_idx_va)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000310 /*
311 * Region partially covers block.
312 *
313 * It can't happen in level 3.
314 *
315 * There must be a table descriptor here, if not there
316 * was a problem when mapping the region.
317 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100318 assert(level < 3U);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000319 assert(desc_type == TABLE_DESC);
320
321 action = ACTION_RECURSE_INTO_TABLE;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100322 } else {
323 /* The region doesn't cover the block at all */
324 action = ACTION_NONE;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000325 }
326
327 if (action == ACTION_WRITE_BLOCK_ENTRY) {
328
329 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100330 xlat_arch_tlbi_va(table_idx_va, ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000331
332 } else if (action == ACTION_RECURSE_INTO_TABLE) {
333
334 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
335
336 /* Recurse to write into subtable */
337 xlat_tables_unmap_region(ctx, mm, table_idx_va,
338 subtable, XLAT_TABLE_ENTRIES,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100339 level + 1U);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100340#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
341 xlat_clean_dcache_range((uintptr_t)subtable,
342 XLAT_TABLE_ENTRIES * sizeof(uint64_t));
343#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +0000344 /*
345 * If the subtable is now empty, remove its reference.
346 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100347 if (xlat_table_is_empty(ctx, subtable)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000348 table_base[table_idx] = INVALID_DESC;
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100349 xlat_arch_tlbi_va(table_idx_va,
350 ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000351 }
352
353 } else {
354 assert(action == ACTION_NONE);
355 }
356
357 table_idx++;
358 table_idx_va += XLAT_BLOCK_SIZE(level);
359
360 /* If reached the end of the region, exit */
361 if (region_end_va <= table_idx_va)
362 break;
363 }
364
365 if (level > ctx->base_level)
366 xlat_table_dec_regions_count(ctx, table_base);
367}
368
369#endif /* PLAT_XLAT_TABLES_DYNAMIC */
370
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000371/*
372 * From the given arguments, it decides which action to take when mapping the
373 * specified region.
374 */
375static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100376 unsigned int desc_type, unsigned long long dest_pa,
377 uintptr_t table_entry_base_va, unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000378{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100379 uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000380 uintptr_t table_entry_end_va =
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100381 table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000382
383 /*
384 * The descriptor types allowed depend on the current table level.
385 */
386
387 if ((mm->base_va <= table_entry_base_va) &&
388 (mm_end_va >= table_entry_end_va)) {
389
390 /*
391 * Table entry is covered by region
392 * --------------------------------
393 *
394 * This means that this table entry can describe the whole
395 * translation with this granularity in principle.
396 */
397
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100398 if (level == 3U) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000399 /*
400 * Last level, only page descriptors are allowed.
401 */
402 if (desc_type == PAGE_DESC) {
403 /*
404 * There's another region mapped here, don't
405 * overwrite.
406 */
407 return ACTION_NONE;
408 } else {
409 assert(desc_type == INVALID_DESC);
410 return ACTION_WRITE_BLOCK_ENTRY;
411 }
412
413 } else {
414
415 /*
416 * Other levels. Table descriptors are allowed. Block
417 * descriptors too, but they have some limitations.
418 */
419
420 if (desc_type == TABLE_DESC) {
421 /* There's already a table, recurse into it. */
422 return ACTION_RECURSE_INTO_TABLE;
423
424 } else if (desc_type == INVALID_DESC) {
425 /*
426 * There's nothing mapped here, create a new
427 * entry.
428 *
429 * Check if the destination granularity allows
430 * us to use a block descriptor or we need a
431 * finer table for it.
432 *
433 * Also, check if the current level allows block
434 * descriptors. If not, create a table instead.
435 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100436 if (((dest_pa & XLAT_BLOCK_MASK(level)) != 0U)
437 || (level < MIN_LVL_BLOCK_DESC) ||
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100438 (mm->granularity < XLAT_BLOCK_SIZE(level)))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000439 return ACTION_CREATE_NEW_TABLE;
440 else
441 return ACTION_WRITE_BLOCK_ENTRY;
442
443 } else {
444 /*
445 * There's another region mapped here, don't
446 * overwrite.
447 */
448 assert(desc_type == BLOCK_DESC);
449
450 return ACTION_NONE;
451 }
452 }
453
454 } else if ((mm->base_va <= table_entry_end_va) ||
455 (mm_end_va >= table_entry_base_va)) {
456
457 /*
458 * Region partially covers table entry
459 * -----------------------------------
460 *
461 * This means that this table entry can't describe the whole
462 * translation, a finer table is needed.
463
464 * There cannot be partial block overlaps in level 3. If that
465 * happens, some of the preliminary checks when adding the
466 * mmap region failed to detect that PA and VA must at least be
467 * aligned to PAGE_SIZE.
468 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100469 assert(level < 3U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000470
471 if (desc_type == INVALID_DESC) {
472 /*
473 * The block is not fully covered by the region. Create
474 * a new table, recurse into it and try to map the
475 * region with finer granularity.
476 */
477 return ACTION_CREATE_NEW_TABLE;
478
479 } else {
480 assert(desc_type == TABLE_DESC);
481 /*
482 * The block is not fully covered by the region, but
483 * there is already a table here. Recurse into it and
484 * try to map with finer granularity.
485 *
486 * PAGE_DESC for level 3 has the same value as
487 * TABLE_DESC, but this code can't run on a level 3
488 * table because there can't be overlaps in level 3.
489 */
490 return ACTION_RECURSE_INTO_TABLE;
491 }
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100492 } else {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000493
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100494 /*
495 * This table entry is outside of the region specified in the
496 * arguments, don't write anything to it.
497 */
498 return ACTION_NONE;
499 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000500}
501
502/*
503 * Recursive function that writes to the translation tables and maps the
Antonio Nino Diazac998032017-02-27 17:23:54 +0000504 * specified region. On success, it returns the VA of the last byte that was
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100505 * successfully mapped. On error, it returns the VA of the next entry that
Antonio Nino Diazac998032017-02-27 17:23:54 +0000506 * should have been mapped.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000507 */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000508static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100509 uintptr_t table_base_va,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000510 uint64_t *const table_base,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100511 unsigned int table_entries,
512 unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000513{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100514 assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000515
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100516 uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000517
518 uintptr_t table_idx_va;
519 unsigned long long table_idx_pa;
520
521 uint64_t *subtable;
522 uint64_t desc;
523
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100524 unsigned int table_idx;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000525
526 if (mm->base_va > table_base_va) {
527 /* Find the first index of the table affected by the region. */
528 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
529
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100530 table_idx = (unsigned int)((table_idx_va - table_base_va) >>
531 XLAT_ADDR_SHIFT(level));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000532
533 assert(table_idx < table_entries);
534 } else {
535 /* Start from the beginning of the table. */
536 table_idx_va = table_base_va;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100537 table_idx = 0U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000538 }
539
Antonio Nino Diazac998032017-02-27 17:23:54 +0000540#if PLAT_XLAT_TABLES_DYNAMIC
541 if (level > ctx->base_level)
542 xlat_table_inc_regions_count(ctx, table_base);
543#endif
544
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000545 while (table_idx < table_entries) {
546
547 desc = table_base[table_idx];
548
549 table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
550
551 action_t action = xlat_tables_map_region_action(mm,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100552 (uint32_t)(desc & DESC_MASK), table_idx_pa,
553 table_idx_va, level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000554
555 if (action == ACTION_WRITE_BLOCK_ENTRY) {
556
557 table_base[table_idx] =
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100558 xlat_desc(ctx, (uint32_t)mm->attr, table_idx_pa,
559 level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000560
561 } else if (action == ACTION_CREATE_NEW_TABLE) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100562 uintptr_t end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000563
564 subtable = xlat_table_get_empty(ctx);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000565 if (subtable == NULL) {
566 /* Not enough free tables to map this region */
567 return table_idx_va;
568 }
569
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000570 /* Point to new subtable from this one. */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000571 table_base[table_idx] = TABLE_DESC | (unsigned long)subtable;
572
573 /* Recurse to write into subtable */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100574 end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
Antonio Nino Diazac998032017-02-27 17:23:54 +0000575 subtable, XLAT_TABLE_ENTRIES,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100576 level + 1U);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100577#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
578 xlat_clean_dcache_range((uintptr_t)subtable,
579 XLAT_TABLE_ENTRIES * sizeof(uint64_t));
580#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100581 if (end_va !=
582 (table_idx_va + XLAT_BLOCK_SIZE(level) - 1U))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000583 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000584
585 } else if (action == ACTION_RECURSE_INTO_TABLE) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100586 uintptr_t end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000587
588 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
589 /* Recurse to write into subtable */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100590 end_va = xlat_tables_map_region(ctx, mm, table_idx_va,
Antonio Nino Diazac998032017-02-27 17:23:54 +0000591 subtable, XLAT_TABLE_ENTRIES,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100592 level + 1U);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100593#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
594 xlat_clean_dcache_range((uintptr_t)subtable,
595 XLAT_TABLE_ENTRIES * sizeof(uint64_t));
596#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100597 if (end_va !=
598 (table_idx_va + XLAT_BLOCK_SIZE(level) - 1U))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000599 return end_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000600
601 } else {
602
603 assert(action == ACTION_NONE);
604
605 }
606
607 table_idx++;
608 table_idx_va += XLAT_BLOCK_SIZE(level);
609
610 /* If reached the end of the region, exit */
611 if (mm_end_va <= table_idx_va)
612 break;
613 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000614
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100615 return table_idx_va - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000616}
617
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000618/*
619 * Function that verifies that a region can be mapped.
620 * Returns:
621 * 0: Success, the mapping is allowed.
622 * EINVAL: Invalid values were used as arguments.
623 * ERANGE: The memory limits were surpassed.
624 * ENOMEM: There is not enough memory in the mmap array.
625 * EPERM: Region overlaps another one in an invalid way.
626 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100627static int mmap_add_region_check(const xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000628{
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100629 unsigned long long base_pa = mm->base_pa;
630 uintptr_t base_va = mm->base_va;
631 size_t size = mm->size;
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100632 size_t granularity = mm->granularity;
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100633
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100634 unsigned long long end_pa = base_pa + size - 1U;
635 uintptr_t end_va = base_va + size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000636
637 if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
638 !IS_PAGE_ALIGNED(size))
639 return -EINVAL;
640
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100641 if ((granularity != XLAT_BLOCK_SIZE(1U)) &&
642 (granularity != XLAT_BLOCK_SIZE(2U)) &&
643 (granularity != XLAT_BLOCK_SIZE(3U))) {
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100644 return -EINVAL;
645 }
646
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000647 /* Check for overflows */
648 if ((base_pa > end_pa) || (base_va > end_va))
649 return -ERANGE;
650
651 if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address)
652 return -ERANGE;
653
654 if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address)
655 return -ERANGE;
656
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100657 /* Check that there is space in the ctx->mmap array */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100658 if (ctx->mmap[ctx->mmap_num - 1].size != 0U)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000659 return -ENOMEM;
660
661 /* Check for PAs and VAs overlaps with all other regions */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100662 for (const mmap_region_t *mm_cursor = ctx->mmap;
663 mm_cursor->size != 0U; ++mm_cursor) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000664
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100665 uintptr_t mm_cursor_end_va = mm_cursor->base_va
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100666 + mm_cursor->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000667
668 /*
669 * Check if one of the regions is completely inside the other
670 * one.
671 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100672 bool fully_overlapped_va =
673 ((base_va >= mm_cursor->base_va) &&
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100674 (end_va <= mm_cursor_end_va)) ||
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100675 ((mm_cursor->base_va >= base_va) &&
676 (mm_cursor_end_va <= end_va));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000677
678 /*
679 * Full VA overlaps are only allowed if both regions are
680 * identity mapped (zero offset) or have the same VA to PA
681 * offset. Also, make sure that it's not the exact same area.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000682 * This can only be done with static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000683 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100684 if (fully_overlapped_va) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000685
Antonio Nino Diazac998032017-02-27 17:23:54 +0000686#if PLAT_XLAT_TABLES_DYNAMIC
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100687 if (((mm->attr & MT_DYNAMIC) != 0U) ||
688 ((mm_cursor->attr & MT_DYNAMIC) != 0U))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000689 return -EPERM;
690#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100691 if ((mm_cursor->base_va - mm_cursor->base_pa) !=
692 (base_va - base_pa))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000693 return -EPERM;
694
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100695 if ((base_va == mm_cursor->base_va) &&
696 (size == mm_cursor->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000697 return -EPERM;
698
699 } else {
700 /*
701 * If the regions do not have fully overlapping VAs,
702 * then they must have fully separated VAs and PAs.
703 * Partial overlaps are not allowed
704 */
705
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100706 unsigned long long mm_cursor_end_pa =
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100707 mm_cursor->base_pa + mm_cursor->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000708
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100709 bool separated_pa = (end_pa < mm_cursor->base_pa) ||
710 (base_pa > mm_cursor_end_pa);
711 bool separated_va = (end_va < mm_cursor->base_va) ||
712 (base_va > mm_cursor_end_va);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000713
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100714 if (!separated_va || !separated_pa)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000715 return -EPERM;
716 }
717 }
718
719 return 0;
720}
721
Sandrine Bailleux66342932017-07-18 13:26:36 +0100722void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000723{
John Tsichritzisfdd92482018-05-25 09:12:48 +0100724 mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700725 const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100726 const mmap_region_t *mm_last;
727 unsigned long long end_pa = mm->base_pa + mm->size - 1U;
728 uintptr_t end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000729 int ret;
730
731 /* Ignore empty regions */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100732 if (mm->size == 0U)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000733 return;
734
Antonio Nino Diazac998032017-02-27 17:23:54 +0000735 /* Static regions must be added before initializing the xlat tables. */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100736 assert(!ctx->initialized);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000737
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100738 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000739 if (ret != 0) {
740 ERROR("mmap_add_region_check() failed. error %d\n", ret);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100741 assert(false);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000742 return;
743 }
744
745 /*
746 * Find correct place in mmap to insert new region.
747 *
748 * 1 - Lower region VA end first.
749 * 2 - Smaller region size first.
750 *
751 * VA 0 0xFF
752 *
753 * 1st |------|
754 * 2nd |------------|
755 * 3rd |------|
756 * 4th |---|
757 * 5th |---|
758 * 6th |----------|
759 * 7th |-------------------------------------|
760 *
761 * This is required for overlapping regions only. It simplifies adding
762 * regions with the loop in xlat_tables_init_internal because the outer
763 * ones won't overwrite block or page descriptors of regions added
764 * previously.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000765 *
766 * Overlapping is only allowed for static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000767 */
768
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100769 while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
770 && (mm_cursor->size != 0U)) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000771 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100772 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000773
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100774 while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
775 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000776 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100777 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000778
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700779 /*
780 * Find the last entry marker in the mmap
781 */
782 mm_last = ctx->mmap;
783 while ((mm_last->size != 0U) && (mm_last < mm_end)) {
784 ++mm_last;
785 }
786
787 /*
788 * Check if we have enough space in the memory mapping table.
789 * This shouldn't happen as we have checked in mmap_add_region_check
790 * that there is free space.
791 */
792 assert(mm_last->size == 0U);
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100793
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000794 /* Make room for new region by moving other regions up by one place */
John Tsichritzisfdd92482018-05-25 09:12:48 +0100795 mm_destination = mm_cursor + 1;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100796 (void)memmove(mm_destination, mm_cursor,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000797 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
798
799 /*
800 * Check we haven't lost the empty sentinel from the end of the array.
801 * This shouldn't happen as we have checked in mmap_add_region_check
802 * that there is free space.
803 */
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700804 assert(mm_end->size == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000805
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100806 *mm_cursor = *mm;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000807
808 if (end_pa > ctx->max_pa)
809 ctx->max_pa = end_pa;
810 if (end_va > ctx->max_va)
811 ctx->max_va = end_va;
812}
813
Antonio Nino Diazc0033282018-11-20 16:03:11 +0000814/*
815 * Determine the table level closest to the initial lookup level that
816 * can describe this translation. Then, align base VA to the next block
817 * at the determined level.
818 */
819static void mmap_alloc_va_align_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
820{
821 /*
822 * By or'ing the size and base PA the alignment will be the one
823 * corresponding to the smallest boundary of the two of them.
824 *
825 * There are three different cases. For example (for 4 KiB page size):
826 *
827 * +--------------+------------------++--------------+
828 * | PA alignment | Size multiple of || VA alignment |
829 * +--------------+------------------++--------------+
830 * | 2 MiB | 2 MiB || 2 MiB | (1)
831 * | 2 MiB | 4 KiB || 4 KiB | (2)
832 * | 4 KiB | 2 MiB || 4 KiB | (3)
833 * +--------------+------------------++--------------+
834 *
835 * - In (1), it is possible to take advantage of the alignment of the PA
836 * and the size of the region to use a level 2 translation table
837 * instead of a level 3 one.
838 *
839 * - In (2), the size is smaller than a block entry of level 2, so it is
840 * needed to use a level 3 table to describe the region or the library
841 * will map more memory than the desired one.
842 *
843 * - In (3), even though the region has the size of one level 2 block
844 * entry, it isn't possible to describe the translation with a level 2
845 * block entry because of the alignment of the base PA.
846 *
847 * Only bits 47:21 of a level 2 block descriptor are used by the MMU,
848 * bits 20:0 of the resulting address are 0 in this case. Because of
849 * this, the PA generated as result of this translation is aligned to
850 * 2 MiB. The PA that was requested to be mapped is aligned to 4 KiB,
851 * though, which means that the resulting translation is incorrect.
852 * The only way to prevent this is by using a finer granularity.
853 */
854 unsigned long long align_check;
855
856 align_check = mm->base_pa | (unsigned long long)mm->size;
857
858 /*
859 * Assume it is always aligned to level 3. There's no need to check that
860 * level because its block size is PAGE_SIZE. The checks to verify that
861 * the addresses and size are aligned to PAGE_SIZE are inside
862 * mmap_add_region.
863 */
864 for (unsigned int level = ctx->base_level; level <= 2U; ++level) {
865
866 if ((align_check & XLAT_BLOCK_MASK(level)) != 0U)
867 continue;
868
869 mm->base_va = round_up(mm->base_va, XLAT_BLOCK_SIZE(level));
870 return;
871 }
872}
873
874void mmap_add_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
875{
876 mm->base_va = ctx->max_va + 1UL;
877
878 assert(mm->size > 0U);
879
880 mmap_alloc_va_align_ctx(ctx, mm);
881
882 /* Detect overflows. More checks are done in mmap_add_region_check(). */
883 assert(mm->base_va > ctx->max_va);
884
885 mmap_add_region_ctx(ctx, mm);
886}
887
Sandrine Bailleux66342932017-07-18 13:26:36 +0100888void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
889{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100890 const mmap_region_t *mm_cursor = mm;
891
Antonio Nino Diaz2cb864c2018-10-08 16:11:11 +0100892 while (mm_cursor->granularity != 0U) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100893 mmap_add_region_ctx(ctx, mm_cursor);
894 mm_cursor++;
Sandrine Bailleux66342932017-07-18 13:26:36 +0100895 }
896}
897
Antonio Nino Diazac998032017-02-27 17:23:54 +0000898#if PLAT_XLAT_TABLES_DYNAMIC
899
900int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
901{
902 mmap_region_t *mm_cursor = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100903 const mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
904 unsigned long long end_pa = mm->base_pa + mm->size - 1U;
905 uintptr_t end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000906 int ret;
907
908 /* Nothing to do */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100909 if (mm->size == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000910 return 0;
911
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100912 /* Now this region is a dynamic one */
913 mm->attr |= MT_DYNAMIC;
914
915 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000916 if (ret != 0)
917 return ret;
918
919 /*
920 * Find the adequate entry in the mmap array in the same way done for
921 * static regions in mmap_add_region_ctx().
922 */
923
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100924 while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
925 && (mm_cursor->size != 0U)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000926 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100927 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000928
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100929 while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
930 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000931 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100932 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000933
934 /* Make room for new region by moving other regions up by one place */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100935 (void)memmove(mm_cursor + 1U, mm_cursor,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100936 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000937
938 /*
939 * Check we haven't lost the empty sentinal from the end of the array.
940 * This shouldn't happen as we have checked in mmap_add_region_check
941 * that there is free space.
942 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100943 assert(mm_last->size == 0U);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000944
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100945 *mm_cursor = *mm;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000946
947 /*
948 * Update the translation tables if the xlat tables are initialized. If
949 * not, this region will be mapped when they are initialized.
950 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100951 if (ctx->initialized) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100952 end_va = xlat_tables_map_region(ctx, mm_cursor,
953 0U, ctx->base_table, ctx->base_table_entries,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100954 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100955#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
956 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
957 ctx->base_table_entries * sizeof(uint64_t));
958#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +0000959 /* Failed to map, remove mmap entry, unmap and return error. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100960 if (end_va != (mm_cursor->base_va + mm_cursor->size - 1U)) {
961 (void)memmove(mm_cursor, mm_cursor + 1U,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100962 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000963
964 /*
965 * Check if the mapping function actually managed to map
966 * anything. If not, just return now.
967 */
Antonio Nino Diaz3f518922018-01-05 11:30:36 +0000968 if (mm->base_va >= end_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000969 return -ENOMEM;
970
971 /*
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100972 * Something went wrong after mapping some table
973 * entries, undo every change done up to this point.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000974 */
975 mmap_region_t unmap_mm = {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100976 .base_pa = 0U,
Antonio Nino Diazac998032017-02-27 17:23:54 +0000977 .base_va = mm->base_va,
978 .size = end_va - mm->base_va,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100979 .attr = 0U
Antonio Nino Diazac998032017-02-27 17:23:54 +0000980 };
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100981 xlat_tables_unmap_region(ctx, &unmap_mm, 0U,
982 ctx->base_table, ctx->base_table_entries,
983 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100984#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
985 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
986 ctx->base_table_entries * sizeof(uint64_t));
987#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +0000988 return -ENOMEM;
989 }
990
991 /*
992 * Make sure that all entries are written to the memory. There
993 * is no need to invalidate entries when mapping dynamic regions
994 * because new table/block/page descriptors only replace old
995 * invalid descriptors, that aren't TLB cached.
996 */
997 dsbishst();
998 }
999
1000 if (end_pa > ctx->max_pa)
1001 ctx->max_pa = end_pa;
1002 if (end_va > ctx->max_va)
1003 ctx->max_va = end_va;
1004
1005 return 0;
1006}
1007
Antonio Nino Diazc0033282018-11-20 16:03:11 +00001008int mmap_add_dynamic_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
1009{
1010 mm->base_va = ctx->max_va + 1UL;
1011
1012 if (mm->size == 0U)
1013 return 0;
1014
1015 mmap_alloc_va_align_ctx(ctx, mm);
1016
1017 /* Detect overflows. More checks are done in mmap_add_region_check(). */
1018 if (mm->base_va < ctx->max_va) {
1019 return -ENOMEM;
1020 }
1021
1022 return mmap_add_dynamic_region_ctx(ctx, mm);
1023}
1024
Antonio Nino Diazac998032017-02-27 17:23:54 +00001025/*
1026 * Removes the region with given base Virtual Address and size from the given
1027 * context.
1028 *
1029 * Returns:
1030 * 0: Success.
1031 * EINVAL: Invalid values were used as arguments (region not found).
1032 * EPERM: Tried to remove a static region.
1033 */
1034int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
1035 size_t size)
1036{
1037 mmap_region_t *mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001038 const mmap_region_t *mm_last = mm + ctx->mmap_num;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001039 int update_max_va_needed = 0;
1040 int update_max_pa_needed = 0;
1041
1042 /* Check sanity of mmap array. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001043 assert(mm[ctx->mmap_num].size == 0U);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001044
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001045 while (mm->size != 0U) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001046 if ((mm->base_va == base_va) && (mm->size == size))
1047 break;
1048 ++mm;
1049 }
1050
1051 /* Check that the region was found */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001052 if (mm->size == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001053 return -EINVAL;
1054
1055 /* If the region is static it can't be removed */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001056 if ((mm->attr & MT_DYNAMIC) == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001057 return -EPERM;
1058
1059 /* Check if this region is using the top VAs or PAs. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001060 if ((mm->base_va + mm->size - 1U) == ctx->max_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001061 update_max_va_needed = 1;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001062 if ((mm->base_pa + mm->size - 1U) == ctx->max_pa)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001063 update_max_pa_needed = 1;
1064
1065 /* Update the translation tables if needed */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001066 if (ctx->initialized) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001067 xlat_tables_unmap_region(ctx, mm, 0U, ctx->base_table,
Antonio Nino Diazac998032017-02-27 17:23:54 +00001068 ctx->base_table_entries,
1069 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001070#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1071 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1072 ctx->base_table_entries * sizeof(uint64_t));
1073#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +00001074 xlat_arch_tlbi_va_sync();
1075 }
1076
1077 /* Remove this region by moving the rest down by one place. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001078 (void)memmove(mm, mm + 1U, (uintptr_t)mm_last - (uintptr_t)mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001079
1080 /* Check if we need to update the max VAs and PAs */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001081 if (update_max_va_needed == 1) {
1082 ctx->max_va = 0U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001083 mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001084 while (mm->size != 0U) {
1085 if ((mm->base_va + mm->size - 1U) > ctx->max_va)
1086 ctx->max_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001087 ++mm;
1088 }
1089 }
1090
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001091 if (update_max_pa_needed == 1) {
1092 ctx->max_pa = 0U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001093 mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001094 while (mm->size != 0U) {
1095 if ((mm->base_pa + mm->size - 1U) > ctx->max_pa)
1096 ctx->max_pa = mm->base_pa + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001097 ++mm;
1098 }
1099 }
1100
1101 return 0;
1102}
1103
Antonio Nino Diaz675d1552018-10-30 11:36:47 +00001104void xlat_setup_dynamic_ctx(xlat_ctx_t *ctx, unsigned long long pa_max,
1105 uintptr_t va_max, struct mmap_region *mmap,
1106 unsigned int mmap_num, uint64_t **tables,
1107 unsigned int tables_num, uint64_t *base_table,
1108 int xlat_regime, int *mapped_regions)
1109{
1110 ctx->xlat_regime = xlat_regime;
1111
1112 ctx->pa_max_address = pa_max;
1113 ctx->va_max_address = va_max;
1114
1115 ctx->mmap = mmap;
1116 ctx->mmap_num = mmap_num;
1117 memset(ctx->mmap, 0, sizeof(struct mmap_region) * mmap_num);
1118
1119 ctx->tables = (void *) tables;
1120 ctx->tables_num = tables_num;
1121
1122 uintptr_t va_space_size = va_max + 1;
1123 ctx->base_level = GET_XLAT_TABLE_LEVEL_BASE(va_space_size);
1124 ctx->base_table = base_table;
1125 ctx->base_table_entries = GET_NUM_BASE_LEVEL_ENTRIES(va_space_size);
1126
1127 ctx->tables_mapped_regions = mapped_regions;
1128
1129 ctx->max_pa = 0;
1130 ctx->max_va = 0;
1131 ctx->initialized = 0;
1132}
1133
Antonio Nino Diazac998032017-02-27 17:23:54 +00001134#endif /* PLAT_XLAT_TABLES_DYNAMIC */
1135
Daniel Boulby5a03a252018-08-30 16:48:56 +01001136void __init init_xlat_tables_ctx(xlat_ctx_t *ctx)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001137{
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +01001138 assert(ctx != NULL);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001139 assert(!ctx->initialized);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001140 assert((ctx->xlat_regime == EL3_REGIME) ||
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +01001141 (ctx->xlat_regime == EL2_REGIME) ||
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001142 (ctx->xlat_regime == EL1_EL0_REGIME));
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001143 assert(!is_mmu_enabled_ctx(ctx));
Sandrine Bailleux66342932017-07-18 13:26:36 +01001144
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +01001145 mmap_region_t *mm = ctx->mmap;
Sandrine Bailleux66342932017-07-18 13:26:36 +01001146
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01001147 xlat_mmap_print(mm);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001148
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001149 /* All tables must be zeroed before mapping any region. */
1150
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001151 for (unsigned int i = 0U; i < ctx->base_table_entries; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001152 ctx->base_table[i] = INVALID_DESC;
1153
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001154 for (int j = 0; j < ctx->tables_num; j++) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001155#if PLAT_XLAT_TABLES_DYNAMIC
1156 ctx->tables_mapped_regions[j] = 0;
1157#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001158 for (unsigned int i = 0U; i < XLAT_TABLE_ENTRIES; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001159 ctx->tables[j][i] = INVALID_DESC;
1160 }
1161
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001162 while (mm->size != 0U) {
1163 uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0U,
1164 ctx->base_table, ctx->base_table_entries,
1165 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001166#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1167 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1168 ctx->base_table_entries * sizeof(uint64_t));
1169#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001170 if (end_va != (mm->base_va + mm->size - 1U)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001171 ERROR("Not enough memory to map region:\n"
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001172 " VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x\n",
1173 mm->base_va, mm->base_pa, mm->size, mm->attr);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001174 panic();
1175 }
1176
1177 mm++;
1178 }
1179
Sandrine Bailleux46c53a22017-07-11 15:11:10 +01001180 assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
Sandrine Bailleux66342932017-07-18 13:26:36 +01001181 assert(ctx->max_va <= ctx->va_max_address);
1182 assert(ctx->max_pa <= ctx->pa_max_address);
1183
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001184 ctx->initialized = true;
Sandrine Bailleuxc5b63772017-05-31 13:31:48 +01001185
1186 xlat_tables_print(ctx);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001187}