blob: 7957b61a266275bc802da4edf86a57379ccaff1f [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 <assert.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00008#include <errno.h>
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01009#include <stdbool.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010010#include <stdint.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000011#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <platform_def.h>
14
15#include <arch_helpers.h>
16#include <common/debug.h>
17#include <lib/utils_def.h>
18#include <lib/xlat_tables/xlat_tables_defs.h>
19#include <lib/xlat_tables/xlat_tables_v2.h>
Sandrine Bailleux090c8492017-05-19 09:59:37 +010020
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000021#include "xlat_tables_private.h"
22
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +010023/* Helper function that cleans the data cache only if it is enabled. */
Varun Wadekar6bd85492019-01-30 08:31:07 -080024static inline __attribute__((unused)) void xlat_clean_dcache_range(uintptr_t addr, size_t size)
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +010025{
26 if (is_dcache_enabled())
27 clean_dcache_range(addr, size);
28}
29
Antonio Nino Diazac998032017-02-27 17:23:54 +000030#if PLAT_XLAT_TABLES_DYNAMIC
31
32/*
33 * The following functions assume that they will be called using subtables only.
34 * The base table can't be unmapped, so it is not needed to do any special
35 * handling for it.
36 */
37
38/*
39 * Returns the index of the array corresponding to the specified translation
40 * table.
41 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010042static int xlat_table_get_index(const xlat_ctx_t *ctx, const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000043{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010044 for (int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000045 if (ctx->tables[i] == table)
46 return i;
47
48 /*
49 * Maybe we were asked to get the index of the base level table, which
50 * should never happen.
51 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010052 assert(false);
Antonio Nino Diazac998032017-02-27 17:23:54 +000053
54 return -1;
55}
56
57/* Returns a pointer to an empty translation table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010058static uint64_t *xlat_table_get_empty(const xlat_ctx_t *ctx)
Antonio Nino Diazac998032017-02-27 17:23:54 +000059{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010060 for (int i = 0; i < ctx->tables_num; i++)
Antonio Nino Diazac998032017-02-27 17:23:54 +000061 if (ctx->tables_mapped_regions[i] == 0)
62 return ctx->tables[i];
63
64 return NULL;
65}
66
67/* Increments region count for a given table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010068static void xlat_table_inc_regions_count(const xlat_ctx_t *ctx,
69 const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000070{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010071 int idx = xlat_table_get_index(ctx, table);
72
73 ctx->tables_mapped_regions[idx]++;
Antonio Nino Diazac998032017-02-27 17:23:54 +000074}
75
76/* Decrements region count for a given table. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010077static void xlat_table_dec_regions_count(const xlat_ctx_t *ctx,
78 const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000079{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010080 int idx = xlat_table_get_index(ctx, table);
81
82 ctx->tables_mapped_regions[idx]--;
Antonio Nino Diazac998032017-02-27 17:23:54 +000083}
84
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +010085/* Returns 0 if the specified table isn't empty, otherwise 1. */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010086static bool xlat_table_is_empty(const xlat_ctx_t *ctx, const uint64_t *table)
Antonio Nino Diazac998032017-02-27 17:23:54 +000087{
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +010088 return ctx->tables_mapped_regions[xlat_table_get_index(ctx, table)] == 0;
Antonio Nino Diazac998032017-02-27 17:23:54 +000089}
90
91#else /* PLAT_XLAT_TABLES_DYNAMIC */
92
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000093/* Returns a pointer to the first empty translation table. */
94static uint64_t *xlat_table_get_empty(xlat_ctx_t *ctx)
95{
96 assert(ctx->next_table < ctx->tables_num);
97
98 return ctx->tables[ctx->next_table++];
99}
100
Antonio Nino Diazac998032017-02-27 17:23:54 +0000101#endif /* PLAT_XLAT_TABLES_DYNAMIC */
102
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100103/*
104 * Returns a block/page table descriptor for the given level and attributes.
105 */
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100106uint64_t xlat_desc(const xlat_ctx_t *ctx, uint32_t attr,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100107 unsigned long long addr_pa, unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000108{
109 uint64_t desc;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100110 uint32_t mem_type;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000111
112 /* Make sure that the granularity is fine enough to map this address. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100113 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000114
115 desc = addr_pa;
116 /*
117 * There are different translation table descriptors for level 3 and the
118 * rest.
119 */
120 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
121 /*
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100122 * Always set the access flag, as this library assumes access flag
123 * faults aren't managed.
124 */
125 desc |= LOWER_ATTRS(ACCESS_FLAG);
126 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000127 * Deduce other fields of the descriptor based on the MT_NS and MT_RW
128 * memory region attributes.
129 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100130 desc |= ((attr & MT_NS) != 0U) ? LOWER_ATTRS(NS) : 0U;
131 desc |= ((attr & MT_RW) != 0U) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000132
133 /*
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100134 * Do not allow unprivileged access when the mapping is for a privileged
135 * EL. For translation regimes that do not have mappings for access for
136 * lower exception levels, set AP[2] to AP_NO_ACCESS_UNPRIVILEGED.
137 */
138 if (ctx->xlat_regime == EL1_EL0_REGIME) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100139 if ((attr & MT_USER) != 0U) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100140 /* EL0 mapping requested, so we give User access */
141 desc |= LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED);
142 } else {
143 /* EL1 mapping requested, no User access granted */
144 desc |= LOWER_ATTRS(AP_NO_ACCESS_UNPRIVILEGED);
145 }
146 } else {
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +0100147 assert((ctx->xlat_regime == EL2_REGIME) ||
148 (ctx->xlat_regime == EL3_REGIME));
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100149 desc |= LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100150 }
151
152 /*
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000153 * Deduce shareability domain and executability of the memory region
154 * from the memory type of the attributes (MT_TYPE).
155 *
156 * Data accesses to device memory and non-cacheable normal memory are
157 * coherent for all observers in the system, and correspondingly are
158 * always treated as being Outer Shareable. Therefore, for these 2 types
159 * of memory, it is not strictly needed to set the shareability field
160 * in the translation tables.
161 */
162 mem_type = MT_TYPE(attr);
163 if (mem_type == MT_DEVICE) {
164 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
165 /*
166 * Always map device memory as execute-never.
167 * This is to avoid the possibility of a speculative instruction
168 * fetch, which could be an issue if this memory region
169 * corresponds to a read-sensitive peripheral.
170 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100171 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100172
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000173 } else { /* Normal memory */
174 /*
175 * Always map read-write normal memory as execute-never.
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100176 * This library assumes that it is used by software that does
177 * not self-modify its code, therefore R/W memory is reserved
178 * for data storage, which must not be executable.
179 *
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000180 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100181 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000182 * which makes any writable memory region to be treated as
183 * execute-never, regardless of the value of the XN bit in the
184 * translation table.
185 *
186 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100187 * attribute to figure out the value of the XN bit. The actual
188 * XN bit(s) to set in the descriptor depends on the context's
189 * translation regime and the policy applied in
190 * xlat_arch_regime_get_xn_desc().
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000191 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100192 if (((attr & MT_RW) != 0U) || ((attr & MT_EXECUTE_NEVER) != 0U)) {
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100193 desc |= xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100194 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000195
196 if (mem_type == MT_MEMORY) {
197 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
198 } else {
199 assert(mem_type == MT_NON_CACHEABLE);
200 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
201 }
202 }
203
204 return desc;
205}
206
207/*
208 * Enumeration of actions that can be made when mapping table entries depending
209 * on the previous value in that entry and information about the region being
210 * mapped.
211 */
212typedef enum {
213
214 /* Do nothing */
215 ACTION_NONE,
216
217 /* Write a block (or page, if in level 3) entry. */
218 ACTION_WRITE_BLOCK_ENTRY,
219
220 /*
221 * Create a new table and write a table entry pointing to it. Recurse
222 * into it for further processing.
223 */
224 ACTION_CREATE_NEW_TABLE,
225
226 /*
227 * There is a table descriptor in this entry, read it and recurse into
228 * that table for further processing.
229 */
230 ACTION_RECURSE_INTO_TABLE,
231
232} action_t;
233
David Pu5619c802019-02-22 02:23:57 -0800234/*
235 * Function that returns the first VA of the table affected by the specified
236 * mmap region.
237 */
238static uintptr_t xlat_tables_find_start_va(mmap_region_t *mm,
239 const uintptr_t table_base_va,
240 const unsigned int level)
241{
242 uintptr_t table_idx_va;
243
244 if (mm->base_va > table_base_va) {
245 /* Find the first index of the table affected by the region. */
246 table_idx_va = mm->base_va & ~XLAT_BLOCK_MASK(level);
247 } else {
248 /* Start from the beginning of the table. */
249 table_idx_va = table_base_va;
250 }
251
252 return table_idx_va;
253}
254
255/*
256 * Function that returns table index for the given VA and level arguments.
257 */
258static inline unsigned int xlat_tables_va_to_index(const uintptr_t table_base_va,
259 const uintptr_t va,
260 const unsigned int level)
261{
262 return (unsigned int)((va - table_base_va) >> XLAT_ADDR_SHIFT(level));
263}
264
Antonio Nino Diazac998032017-02-27 17:23:54 +0000265#if PLAT_XLAT_TABLES_DYNAMIC
266
267/*
David Pu1507d412019-02-22 02:15:57 -0800268 * From the given arguments, it decides which action to take when unmapping the
269 * specified region.
270 */
271static action_t xlat_tables_unmap_region_action(const mmap_region_t *mm,
272 const uintptr_t table_idx_va, const uintptr_t table_idx_end_va,
273 const unsigned int level, const uint64_t desc_type)
274{
275 action_t action;
276 uintptr_t region_end_va = mm->base_va + mm->size - 1U;
277
278 if ((mm->base_va <= table_idx_va) &&
279 (region_end_va >= table_idx_end_va)) {
280 /* Region covers all block */
281
282 if (level == 3U) {
283 /*
284 * Last level, only page descriptors allowed,
285 * erase it.
286 */
287 assert(desc_type == PAGE_DESC);
288
289 action = ACTION_WRITE_BLOCK_ENTRY;
290 } else {
291 /*
292 * Other levels can have table descriptors. If
293 * so, recurse into it and erase descriptors
294 * inside it as needed. If there is a block
295 * descriptor, just erase it. If an invalid
296 * descriptor is found, this table isn't
297 * actually mapped, which shouldn't happen.
298 */
299 if (desc_type == TABLE_DESC) {
300 action = ACTION_RECURSE_INTO_TABLE;
301 } else {
302 assert(desc_type == BLOCK_DESC);
303 action = ACTION_WRITE_BLOCK_ENTRY;
304 }
305 }
306
307 } else if ((mm->base_va <= table_idx_end_va) ||
308 (region_end_va >= table_idx_va)) {
309 /*
310 * Region partially covers block.
311 *
312 * It can't happen in level 3.
313 *
314 * There must be a table descriptor here, if not there
315 * was a problem when mapping the region.
316 */
317 assert(level < 3U);
318 assert(desc_type == TABLE_DESC);
319
320 action = ACTION_RECURSE_INTO_TABLE;
321 } else {
322 /* The region doesn't cover the block at all */
323 action = ACTION_NONE;
324 }
325
326 return action;
327}
David Puce81ba12019-02-22 02:36:20 -0800328
David Pu1507d412019-02-22 02:15:57 -0800329/*
David Puce81ba12019-02-22 02:36:20 -0800330 * Function that writes to the translation tables and unmaps the
Antonio Nino Diazac998032017-02-27 17:23:54 +0000331 * specified region.
332 */
333static void xlat_tables_unmap_region(xlat_ctx_t *ctx, mmap_region_t *mm,
334 const uintptr_t table_base_va,
335 uint64_t *const table_base,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100336 const unsigned int table_entries,
Varun Wadekar66231d12017-06-07 09:57:42 -0700337 const unsigned int level)
Antonio Nino Diazac998032017-02-27 17:23:54 +0000338{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100339 assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
Antonio Nino Diazac998032017-02-27 17:23:54 +0000340
David Puce81ba12019-02-22 02:36:20 -0800341 /*
342 * data structure to track DESC_TABLE entry before iterate into subtable
343 * of next translation level. it will be used to restore previous level
344 * after finish subtable iteration.
345 */
346 struct desc_table_unmap {
347 uint64_t *table_base;
348 uintptr_t table_idx_va;
349 unsigned int idx;
350 } desc_tables[XLAT_TABLE_LEVEL_MAX + 1] = {
351 {NULL, 0U, XLAT_TABLE_ENTRIES}, };
Antonio Nino Diazac998032017-02-27 17:23:54 +0000352
David Puce81ba12019-02-22 02:36:20 -0800353 unsigned int this_level = level;
354 uint64_t *this_base = table_base;
355 unsigned int max_entries = table_entries;
356 size_t level_size = XLAT_BLOCK_SIZE(this_level);
357 unsigned int table_idx;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000358 uintptr_t table_idx_va;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000359
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100360 uintptr_t region_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000361
David Pu5619c802019-02-22 02:23:57 -0800362 table_idx_va = xlat_tables_find_start_va(mm, table_base_va, level);
363 table_idx = xlat_tables_va_to_index(table_base_va, table_idx_va, level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000364
David Puce81ba12019-02-22 02:36:20 -0800365 while (this_base != NULL) {
Antonio Nino Diazac998032017-02-27 17:23:54 +0000366
David Puce81ba12019-02-22 02:36:20 -0800367 uint64_t desc;
368 uint64_t desc_type;
369 uintptr_t table_idx_end_va; /* End VA of this entry */
370 action_t action;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000371
David Puce81ba12019-02-22 02:36:20 -0800372 /* finish current xlat level iteration. */
373 if (table_idx >= max_entries) {
374 if (this_level > ctx->base_level) {
375 xlat_table_dec_regions_count(ctx, this_base);
376 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000377
David Puce81ba12019-02-22 02:36:20 -0800378 if (this_level > level) {
379 uint64_t *subtable;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000380
David Puce81ba12019-02-22 02:36:20 -0800381 /* back from subtable iteration, restore
382 * previous DESC_TABLE entry.
383 */
384 this_level--;
385 this_base = desc_tables[this_level].table_base;
386 table_idx = desc_tables[this_level].idx;
387 table_idx_va =
388 desc_tables[this_level].table_idx_va;
389 level_size = XLAT_BLOCK_SIZE(this_level);
390
391 if (this_level == level) {
392 max_entries = table_entries;
393 } else {
394 max_entries = XLAT_TABLE_ENTRIES;
395 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000396
David Puce81ba12019-02-22 02:36:20 -0800397 desc = this_base[table_idx];
398 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
399 /*
400 * If the subtable is now empty, remove its reference.
401 */
402 if (xlat_table_is_empty(ctx, subtable)) {
403 this_base[table_idx] = INVALID_DESC;
404 xlat_arch_tlbi_va(table_idx_va,
405 ctx->xlat_regime);
406 }
407 table_idx++;
408 table_idx_va += level_size;
409
410 } else {
411 /* reached end of top level, exit.*/
412 this_base = NULL;
413 break;
414 }
415
416 }
417
418 /* If reached the end of the region, stop iterating entries in
419 * current xlat level.
420 */
421 if (region_end_va <= table_idx_va) {
422 table_idx = max_entries;
423 continue;
424 }
425
426
427 table_idx_end_va = table_idx_va + XLAT_BLOCK_SIZE(this_level) - 1U;
428
429 desc = this_base[table_idx];
430 desc_type = desc & DESC_MASK;
431
432 action = xlat_tables_unmap_region_action(mm, table_idx_va,
433 table_idx_end_va,
434 this_level,
435 desc_type);
436
437 if (action == ACTION_WRITE_BLOCK_ENTRY) {
438 this_base[table_idx] = INVALID_DESC;
Antonio Nino Diazad5dc7f2018-07-11 09:46:45 +0100439 xlat_arch_tlbi_va(table_idx_va, ctx->xlat_regime);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000440
David Puce81ba12019-02-22 02:36:20 -0800441 table_idx++;
442 table_idx_va += level_size;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000443 } else if (action == ACTION_RECURSE_INTO_TABLE) {
444
David Puce81ba12019-02-22 02:36:20 -0800445 uint64_t *subtable;
446 uintptr_t base_va;
447
Antonio Nino Diazac998032017-02-27 17:23:54 +0000448 subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
449
David Puce81ba12019-02-22 02:36:20 -0800450 desc_tables[this_level].table_base = this_base;
451 desc_tables[this_level].table_idx_va = table_idx_va;
452 base_va = table_idx_va;
453 desc_tables[this_level].idx = table_idx;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000454
David Puce81ba12019-02-22 02:36:20 -0800455 this_base = subtable;
456 this_level++;
457
458 max_entries = XLAT_TABLE_ENTRIES;
459 level_size = XLAT_BLOCK_SIZE(this_level);
460
461 table_idx_va = xlat_tables_find_start_va(mm,
462 base_va, this_level);
463 table_idx = xlat_tables_va_to_index(base_va,
464 table_idx_va, this_level);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000465 } else {
466 assert(action == ACTION_NONE);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000467
David Puce81ba12019-02-22 02:36:20 -0800468 table_idx++;
469 table_idx_va += level_size;
470 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000471 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000472}
473
474#endif /* PLAT_XLAT_TABLES_DYNAMIC */
475
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000476/*
477 * From the given arguments, it decides which action to take when mapping the
478 * specified region.
479 */
480static action_t xlat_tables_map_region_action(const mmap_region_t *mm,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100481 unsigned int desc_type, unsigned long long dest_pa,
482 uintptr_t table_entry_base_va, unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000483{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100484 uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000485 uintptr_t table_entry_end_va =
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100486 table_entry_base_va + XLAT_BLOCK_SIZE(level) - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000487
488 /*
489 * The descriptor types allowed depend on the current table level.
490 */
491
492 if ((mm->base_va <= table_entry_base_va) &&
493 (mm_end_va >= table_entry_end_va)) {
494
495 /*
496 * Table entry is covered by region
497 * --------------------------------
498 *
499 * This means that this table entry can describe the whole
500 * translation with this granularity in principle.
501 */
502
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100503 if (level == 3U) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000504 /*
505 * Last level, only page descriptors are allowed.
506 */
507 if (desc_type == PAGE_DESC) {
508 /*
509 * There's another region mapped here, don't
510 * overwrite.
511 */
512 return ACTION_NONE;
513 } else {
514 assert(desc_type == INVALID_DESC);
515 return ACTION_WRITE_BLOCK_ENTRY;
516 }
517
518 } else {
519
520 /*
521 * Other levels. Table descriptors are allowed. Block
522 * descriptors too, but they have some limitations.
523 */
524
525 if (desc_type == TABLE_DESC) {
526 /* There's already a table, recurse into it. */
527 return ACTION_RECURSE_INTO_TABLE;
528
529 } else if (desc_type == INVALID_DESC) {
530 /*
531 * There's nothing mapped here, create a new
532 * entry.
533 *
534 * Check if the destination granularity allows
535 * us to use a block descriptor or we need a
536 * finer table for it.
537 *
538 * Also, check if the current level allows block
539 * descriptors. If not, create a table instead.
540 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100541 if (((dest_pa & XLAT_BLOCK_MASK(level)) != 0U)
542 || (level < MIN_LVL_BLOCK_DESC) ||
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100543 (mm->granularity < XLAT_BLOCK_SIZE(level)))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000544 return ACTION_CREATE_NEW_TABLE;
545 else
546 return ACTION_WRITE_BLOCK_ENTRY;
547
548 } else {
549 /*
550 * There's another region mapped here, don't
551 * overwrite.
552 */
553 assert(desc_type == BLOCK_DESC);
554
555 return ACTION_NONE;
556 }
557 }
558
559 } else if ((mm->base_va <= table_entry_end_va) ||
560 (mm_end_va >= table_entry_base_va)) {
561
562 /*
563 * Region partially covers table entry
564 * -----------------------------------
565 *
566 * This means that this table entry can't describe the whole
567 * translation, a finer table is needed.
568
569 * There cannot be partial block overlaps in level 3. If that
570 * happens, some of the preliminary checks when adding the
571 * mmap region failed to detect that PA and VA must at least be
572 * aligned to PAGE_SIZE.
573 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100574 assert(level < 3U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000575
576 if (desc_type == INVALID_DESC) {
577 /*
578 * The block is not fully covered by the region. Create
579 * a new table, recurse into it and try to map the
580 * region with finer granularity.
581 */
582 return ACTION_CREATE_NEW_TABLE;
583
584 } else {
585 assert(desc_type == TABLE_DESC);
586 /*
587 * The block is not fully covered by the region, but
588 * there is already a table here. Recurse into it and
589 * try to map with finer granularity.
590 *
591 * PAGE_DESC for level 3 has the same value as
592 * TABLE_DESC, but this code can't run on a level 3
593 * table because there can't be overlaps in level 3.
594 */
595 return ACTION_RECURSE_INTO_TABLE;
596 }
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100597 } else {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000598
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100599 /*
600 * This table entry is outside of the region specified in the
601 * arguments, don't write anything to it.
602 */
603 return ACTION_NONE;
604 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000605}
606
607/*
David Pud1b7aa12019-02-22 02:31:40 -0800608 * Function that writes to the translation tables and maps the
Antonio Nino Diazac998032017-02-27 17:23:54 +0000609 * specified region. On success, it returns the VA of the last byte that was
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100610 * successfully mapped. On error, it returns the VA of the next entry that
Antonio Nino Diazac998032017-02-27 17:23:54 +0000611 * should have been mapped.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000612 */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000613static uintptr_t xlat_tables_map_region(xlat_ctx_t *ctx, mmap_region_t *mm,
David Pud1b7aa12019-02-22 02:31:40 -0800614 const uintptr_t table_base_va,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000615 uint64_t *const table_base,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100616 unsigned int table_entries,
617 unsigned int level)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000618{
David Pud1b7aa12019-02-22 02:31:40 -0800619
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100620 assert((level >= ctx->base_level) && (level <= XLAT_TABLE_LEVEL_MAX));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000621
David Pud1b7aa12019-02-22 02:31:40 -0800622 /*
623 * data structure to track DESC_TABLE entry before iterate into subtable
624 * of next translation level. it will be used to restore previous level
625 * after finish subtable iteration.
626 */
627 struct desc_table_map {
628 uint64_t *table_base;
629 uintptr_t table_idx_va;
630 unsigned int idx;
631 } desc_tables[XLAT_TABLE_LEVEL_MAX + 1] = {
632 {NULL, 0U, XLAT_TABLE_ENTRIES}, };
633
634 unsigned int this_level = level;
635 uint64_t *this_base = table_base;
636 unsigned int max_entries = table_entries;
637 size_t level_size = XLAT_BLOCK_SIZE(this_level);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100638 uintptr_t mm_end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000639
640 uintptr_t table_idx_va;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100641 unsigned int table_idx;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000642
David Pu5619c802019-02-22 02:23:57 -0800643 table_idx_va = xlat_tables_find_start_va(mm, table_base_va, level);
644 table_idx = xlat_tables_va_to_index(table_base_va, table_idx_va, level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000645
David Pud1b7aa12019-02-22 02:31:40 -0800646 while (this_base != NULL) {
647
648 uint64_t desc;
649 uint64_t desc_type;
650 unsigned long long table_idx_pa;
651 action_t action;
652
653 /* finish current xlat level iteration. */
654 if (table_idx >= max_entries) {
655 if (this_level <= level) {
656 this_base = NULL;
657 break;
658 } else {
659
660 /* back from subtable iteration, restore
661 * previous DESC_TABLE entry.
662 */
663 this_level--;
664 level_size = XLAT_BLOCK_SIZE(this_level);
665 this_base = desc_tables[this_level].table_base;
666 table_idx = desc_tables[this_level].idx;
667 if (this_level == level) {
668 max_entries = table_entries;
669 } else {
670 max_entries = XLAT_TABLE_ENTRIES;
671 }
672#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
673 uintptr_t subtable;
674 desc = this_base[table_idx];
675 subtable = (uintptr_t)(desc & TABLE_ADDR_MASK);
676 xlat_clean_dcache_range(subtable,
677 XLAT_TABLE_ENTRIES * sizeof(uint64_t));
Antonio Nino Diazac998032017-02-27 17:23:54 +0000678#endif
679
David Pud1b7aa12019-02-22 02:31:40 -0800680 table_idx++;
681 table_idx_va =
682 desc_tables[this_level].table_idx_va +
683 level_size;
684 }
685 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000686
David Pud1b7aa12019-02-22 02:31:40 -0800687 desc = this_base[table_idx];
688 desc_type = desc & DESC_MASK;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000689
690 table_idx_pa = mm->base_pa + table_idx_va - mm->base_va;
691
David Pud1b7aa12019-02-22 02:31:40 -0800692 /* If reached the end of the region, simply exit since we
693 * already write all BLOCK entries and create all required
694 * subtables.
695 */
696 if (mm_end_va <= table_idx_va) {
697 this_base = NULL;
698 break;
699 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000700
David Pud1b7aa12019-02-22 02:31:40 -0800701 action = xlat_tables_map_region_action(mm, desc_type,
702 table_idx_pa, table_idx_va, this_level);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000703
David Pud1b7aa12019-02-22 02:31:40 -0800704 if (action == ACTION_WRITE_BLOCK_ENTRY) {
705 this_base[table_idx] = xlat_desc(ctx, mm->attr,
706 table_idx_pa, this_level);
707 table_idx++;
708 table_idx_va += level_size;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000709 } else if (action == ACTION_CREATE_NEW_TABLE) {
David Pud1b7aa12019-02-22 02:31:40 -0800710
711 uintptr_t base_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000712
David Pud1b7aa12019-02-22 02:31:40 -0800713 uint64_t *subtable = xlat_table_get_empty(ctx);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000714 if (subtable == NULL) {
David Pud1b7aa12019-02-22 02:31:40 -0800715 /* Not enough free tables to map this region. */
Antonio Nino Diazac998032017-02-27 17:23:54 +0000716 return table_idx_va;
717 }
718
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000719 /* Point to new subtable from this one. */
David Pud1b7aa12019-02-22 02:31:40 -0800720 this_base[table_idx] = TABLE_DESC | (unsigned long)subtable;
Antonio Nino Diazac998032017-02-27 17:23:54 +0000721
David Pud1b7aa12019-02-22 02:31:40 -0800722 desc_tables[this_level].table_base = this_base;
723 desc_tables[this_level].table_idx_va = table_idx_va;
724 desc_tables[this_level].idx = table_idx;
725 base_va = table_idx_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000726
David Pud1b7aa12019-02-22 02:31:40 -0800727 this_level++;
728 this_base = subtable;
729 level_size = XLAT_BLOCK_SIZE(this_level);
730 table_idx_va = xlat_tables_find_start_va(mm, base_va,
731 this_level);
732 table_idx = xlat_tables_va_to_index(base_va,
733 table_idx_va, this_level);
734 max_entries = XLAT_TABLE_ENTRIES;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000735
David Pud1b7aa12019-02-22 02:31:40 -0800736#if PLAT_XLAT_TABLES_DYNAMIC
737 if (this_level > ctx->base_level) {
738 xlat_table_inc_regions_count(ctx, subtable);
739 }
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +0100740#endif
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000741
David Pud1b7aa12019-02-22 02:31:40 -0800742 } else if (action == ACTION_RECURSE_INTO_TABLE) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000743
David Pud1b7aa12019-02-22 02:31:40 -0800744 uintptr_t base_va;
745 uint64_t *subtable = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000746
David Pud1b7aa12019-02-22 02:31:40 -0800747 desc_tables[this_level].table_base = this_base;
748 desc_tables[this_level].table_idx_va = table_idx_va;
749 desc_tables[this_level].idx = table_idx;
750 base_va = table_idx_va;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000751
David Pud1b7aa12019-02-22 02:31:40 -0800752 this_level++;
753 level_size = XLAT_BLOCK_SIZE(this_level);
754 table_idx_va = xlat_tables_find_start_va(mm, base_va,
755 this_level);
756 table_idx = xlat_tables_va_to_index(base_va,
757 table_idx_va, this_level);
758 this_base = subtable;
759 max_entries = XLAT_TABLE_ENTRIES;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000760
David Pud1b7aa12019-02-22 02:31:40 -0800761#if PLAT_XLAT_TABLES_DYNAMIC
762 if (this_level > ctx->base_level) {
763 xlat_table_inc_regions_count(ctx, subtable);
764 }
765#endif
766 } else {
767 assert(action == ACTION_NONE);
768 table_idx++;
769 table_idx_va += level_size;
770 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000771 }
Antonio Nino Diazac998032017-02-27 17:23:54 +0000772
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100773 return table_idx_va - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000774}
775
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000776/*
777 * Function that verifies that a region can be mapped.
778 * Returns:
779 * 0: Success, the mapping is allowed.
780 * EINVAL: Invalid values were used as arguments.
781 * ERANGE: The memory limits were surpassed.
782 * ENOMEM: There is not enough memory in the mmap array.
783 * EPERM: Region overlaps another one in an invalid way.
784 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100785static int mmap_add_region_check(const xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000786{
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100787 unsigned long long base_pa = mm->base_pa;
788 uintptr_t base_va = mm->base_va;
789 size_t size = mm->size;
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100790 size_t granularity = mm->granularity;
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100791
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100792 unsigned long long end_pa = base_pa + size - 1U;
793 uintptr_t end_va = base_va + size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000794
795 if (!IS_PAGE_ALIGNED(base_pa) || !IS_PAGE_ALIGNED(base_va) ||
796 !IS_PAGE_ALIGNED(size))
797 return -EINVAL;
798
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100799 if ((granularity != XLAT_BLOCK_SIZE(1U)) &&
800 (granularity != XLAT_BLOCK_SIZE(2U)) &&
801 (granularity != XLAT_BLOCK_SIZE(3U))) {
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +0100802 return -EINVAL;
803 }
804
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000805 /* Check for overflows */
806 if ((base_pa > end_pa) || (base_va > end_va))
807 return -ERANGE;
808
809 if ((base_va + (uintptr_t)size - (uintptr_t)1) > ctx->va_max_address)
810 return -ERANGE;
811
812 if ((base_pa + (unsigned long long)size - 1ULL) > ctx->pa_max_address)
813 return -ERANGE;
814
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100815 /* Check that there is space in the ctx->mmap array */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100816 if (ctx->mmap[ctx->mmap_num - 1].size != 0U)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000817 return -ENOMEM;
818
819 /* Check for PAs and VAs overlaps with all other regions */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100820 for (const mmap_region_t *mm_cursor = ctx->mmap;
821 mm_cursor->size != 0U; ++mm_cursor) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000822
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100823 uintptr_t mm_cursor_end_va = mm_cursor->base_va
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100824 + mm_cursor->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000825
826 /*
827 * Check if one of the regions is completely inside the other
828 * one.
829 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100830 bool fully_overlapped_va =
831 ((base_va >= mm_cursor->base_va) &&
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100832 (end_va <= mm_cursor_end_va)) ||
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100833 ((mm_cursor->base_va >= base_va) &&
834 (mm_cursor_end_va <= end_va));
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000835
836 /*
837 * Full VA overlaps are only allowed if both regions are
838 * identity mapped (zero offset) or have the same VA to PA
839 * offset. Also, make sure that it's not the exact same area.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000840 * This can only be done with static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000841 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100842 if (fully_overlapped_va) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000843
Antonio Nino Diazac998032017-02-27 17:23:54 +0000844#if PLAT_XLAT_TABLES_DYNAMIC
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100845 if (((mm->attr & MT_DYNAMIC) != 0U) ||
846 ((mm_cursor->attr & MT_DYNAMIC) != 0U))
Antonio Nino Diazac998032017-02-27 17:23:54 +0000847 return -EPERM;
848#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100849 if ((mm_cursor->base_va - mm_cursor->base_pa) !=
850 (base_va - base_pa))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000851 return -EPERM;
852
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100853 if ((base_va == mm_cursor->base_va) &&
854 (size == mm_cursor->size))
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000855 return -EPERM;
856
857 } else {
858 /*
859 * If the regions do not have fully overlapping VAs,
860 * then they must have fully separated VAs and PAs.
861 * Partial overlaps are not allowed
862 */
863
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100864 unsigned long long mm_cursor_end_pa =
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100865 mm_cursor->base_pa + mm_cursor->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000866
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100867 bool separated_pa = (end_pa < mm_cursor->base_pa) ||
868 (base_pa > mm_cursor_end_pa);
869 bool separated_va = (end_va < mm_cursor->base_va) ||
870 (base_va > mm_cursor_end_va);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000871
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100872 if (!separated_va || !separated_pa)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000873 return -EPERM;
874 }
875 }
876
877 return 0;
878}
879
Sandrine Bailleux66342932017-07-18 13:26:36 +0100880void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000881{
John Tsichritzisfdd92482018-05-25 09:12:48 +0100882 mmap_region_t *mm_cursor = ctx->mmap, *mm_destination;
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700883 const mmap_region_t *mm_end = ctx->mmap + ctx->mmap_num;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100884 const mmap_region_t *mm_last;
885 unsigned long long end_pa = mm->base_pa + mm->size - 1U;
886 uintptr_t end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000887 int ret;
888
889 /* Ignore empty regions */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100890 if (mm->size == 0U)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000891 return;
892
Antonio Nino Diazac998032017-02-27 17:23:54 +0000893 /* Static regions must be added before initializing the xlat tables. */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100894 assert(!ctx->initialized);
Antonio Nino Diazac998032017-02-27 17:23:54 +0000895
Douglas Raillard6a5f8f12017-09-21 08:42:21 +0100896 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000897 if (ret != 0) {
898 ERROR("mmap_add_region_check() failed. error %d\n", ret);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +0100899 assert(false);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000900 return;
901 }
902
903 /*
904 * Find correct place in mmap to insert new region.
905 *
906 * 1 - Lower region VA end first.
907 * 2 - Smaller region size first.
908 *
909 * VA 0 0xFF
910 *
911 * 1st |------|
912 * 2nd |------------|
913 * 3rd |------|
914 * 4th |---|
915 * 5th |---|
916 * 6th |----------|
917 * 7th |-------------------------------------|
918 *
919 * This is required for overlapping regions only. It simplifies adding
920 * regions with the loop in xlat_tables_init_internal because the outer
921 * ones won't overwrite block or page descriptors of regions added
922 * previously.
Antonio Nino Diazac998032017-02-27 17:23:54 +0000923 *
924 * Overlapping is only allowed for static regions.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000925 */
926
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100927 while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
928 && (mm_cursor->size != 0U)) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000929 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100930 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000931
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100932 while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
933 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000934 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100935 }
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000936
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700937 /*
938 * Find the last entry marker in the mmap
939 */
940 mm_last = ctx->mmap;
941 while ((mm_last->size != 0U) && (mm_last < mm_end)) {
942 ++mm_last;
943 }
944
945 /*
946 * Check if we have enough space in the memory mapping table.
947 * This shouldn't happen as we have checked in mmap_add_region_check
948 * that there is free space.
949 */
950 assert(mm_last->size == 0U);
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100951
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000952 /* Make room for new region by moving other regions up by one place */
John Tsichritzisfdd92482018-05-25 09:12:48 +0100953 mm_destination = mm_cursor + 1;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100954 (void)memmove(mm_destination, mm_cursor,
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000955 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
956
957 /*
958 * Check we haven't lost the empty sentinel from the end of the array.
959 * This shouldn't happen as we have checked in mmap_add_region_check
960 * that there is free space.
961 */
Varun Wadekarccbd2e32018-04-03 10:44:41 -0700962 assert(mm_end->size == 0U);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000963
Douglas Raillardf68d2ed2017-09-12 10:31:49 +0100964 *mm_cursor = *mm;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000965
966 if (end_pa > ctx->max_pa)
967 ctx->max_pa = end_pa;
968 if (end_va > ctx->max_va)
969 ctx->max_va = end_va;
970}
971
Antonio Nino Diazc0033282018-11-20 16:03:11 +0000972/*
973 * Determine the table level closest to the initial lookup level that
974 * can describe this translation. Then, align base VA to the next block
975 * at the determined level.
976 */
977static void mmap_alloc_va_align_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
978{
979 /*
980 * By or'ing the size and base PA the alignment will be the one
981 * corresponding to the smallest boundary of the two of them.
982 *
983 * There are three different cases. For example (for 4 KiB page size):
984 *
985 * +--------------+------------------++--------------+
986 * | PA alignment | Size multiple of || VA alignment |
987 * +--------------+------------------++--------------+
988 * | 2 MiB | 2 MiB || 2 MiB | (1)
989 * | 2 MiB | 4 KiB || 4 KiB | (2)
990 * | 4 KiB | 2 MiB || 4 KiB | (3)
991 * +--------------+------------------++--------------+
992 *
993 * - In (1), it is possible to take advantage of the alignment of the PA
994 * and the size of the region to use a level 2 translation table
995 * instead of a level 3 one.
996 *
997 * - In (2), the size is smaller than a block entry of level 2, so it is
998 * needed to use a level 3 table to describe the region or the library
999 * will map more memory than the desired one.
1000 *
1001 * - In (3), even though the region has the size of one level 2 block
1002 * entry, it isn't possible to describe the translation with a level 2
1003 * block entry because of the alignment of the base PA.
1004 *
1005 * Only bits 47:21 of a level 2 block descriptor are used by the MMU,
1006 * bits 20:0 of the resulting address are 0 in this case. Because of
1007 * this, the PA generated as result of this translation is aligned to
1008 * 2 MiB. The PA that was requested to be mapped is aligned to 4 KiB,
1009 * though, which means that the resulting translation is incorrect.
1010 * The only way to prevent this is by using a finer granularity.
1011 */
1012 unsigned long long align_check;
1013
1014 align_check = mm->base_pa | (unsigned long long)mm->size;
1015
1016 /*
1017 * Assume it is always aligned to level 3. There's no need to check that
1018 * level because its block size is PAGE_SIZE. The checks to verify that
1019 * the addresses and size are aligned to PAGE_SIZE are inside
1020 * mmap_add_region.
1021 */
1022 for (unsigned int level = ctx->base_level; level <= 2U; ++level) {
1023
1024 if ((align_check & XLAT_BLOCK_MASK(level)) != 0U)
1025 continue;
1026
1027 mm->base_va = round_up(mm->base_va, XLAT_BLOCK_SIZE(level));
1028 return;
1029 }
1030}
1031
1032void mmap_add_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
1033{
1034 mm->base_va = ctx->max_va + 1UL;
1035
1036 assert(mm->size > 0U);
1037
1038 mmap_alloc_va_align_ctx(ctx, mm);
1039
1040 /* Detect overflows. More checks are done in mmap_add_region_check(). */
1041 assert(mm->base_va > ctx->max_va);
1042
1043 mmap_add_region_ctx(ctx, mm);
1044}
1045
Sandrine Bailleux66342932017-07-18 13:26:36 +01001046void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm)
1047{
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001048 const mmap_region_t *mm_cursor = mm;
1049
Antonio Nino Diaz2cb864c2018-10-08 16:11:11 +01001050 while (mm_cursor->granularity != 0U) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001051 mmap_add_region_ctx(ctx, mm_cursor);
1052 mm_cursor++;
Sandrine Bailleux66342932017-07-18 13:26:36 +01001053 }
1054}
1055
Antonio Nino Diazac998032017-02-27 17:23:54 +00001056#if PLAT_XLAT_TABLES_DYNAMIC
1057
1058int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
1059{
1060 mmap_region_t *mm_cursor = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001061 const mmap_region_t *mm_last = mm_cursor + ctx->mmap_num;
1062 unsigned long long end_pa = mm->base_pa + mm->size - 1U;
1063 uintptr_t end_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001064 int ret;
1065
1066 /* Nothing to do */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001067 if (mm->size == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001068 return 0;
1069
Douglas Raillard6a5f8f12017-09-21 08:42:21 +01001070 /* Now this region is a dynamic one */
1071 mm->attr |= MT_DYNAMIC;
1072
1073 ret = mmap_add_region_check(ctx, mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001074 if (ret != 0)
1075 return ret;
1076
1077 /*
1078 * Find the adequate entry in the mmap array in the same way done for
1079 * static regions in mmap_add_region_ctx().
1080 */
1081
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001082 while (((mm_cursor->base_va + mm_cursor->size - 1U) < end_va)
1083 && (mm_cursor->size != 0U)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001084 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001085 }
Antonio Nino Diazac998032017-02-27 17:23:54 +00001086
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001087 while (((mm_cursor->base_va + mm_cursor->size - 1U) == end_va) &&
1088 (mm_cursor->size != 0U) && (mm_cursor->size < mm->size)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001089 ++mm_cursor;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001090 }
Antonio Nino Diazac998032017-02-27 17:23:54 +00001091
1092 /* Make room for new region by moving other regions up by one place */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001093 (void)memmove(mm_cursor + 1U, mm_cursor,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +01001094 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001095
1096 /*
1097 * Check we haven't lost the empty sentinal from the end of the array.
1098 * This shouldn't happen as we have checked in mmap_add_region_check
1099 * that there is free space.
1100 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001101 assert(mm_last->size == 0U);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001102
Douglas Raillardf68d2ed2017-09-12 10:31:49 +01001103 *mm_cursor = *mm;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001104
1105 /*
1106 * Update the translation tables if the xlat tables are initialized. If
1107 * not, this region will be mapped when they are initialized.
1108 */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001109 if (ctx->initialized) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001110 end_va = xlat_tables_map_region(ctx, mm_cursor,
1111 0U, ctx->base_table, ctx->base_table_entries,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +01001112 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001113#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1114 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1115 ctx->base_table_entries * sizeof(uint64_t));
1116#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +00001117 /* Failed to map, remove mmap entry, unmap and return error. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001118 if (end_va != (mm_cursor->base_va + mm_cursor->size - 1U)) {
1119 (void)memmove(mm_cursor, mm_cursor + 1U,
Douglas Raillard6a5f8f12017-09-21 08:42:21 +01001120 (uintptr_t)mm_last - (uintptr_t)mm_cursor);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001121
1122 /*
1123 * Check if the mapping function actually managed to map
1124 * anything. If not, just return now.
1125 */
Antonio Nino Diaz3f518922018-01-05 11:30:36 +00001126 if (mm->base_va >= end_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001127 return -ENOMEM;
1128
1129 /*
Douglas Raillard6a5f8f12017-09-21 08:42:21 +01001130 * Something went wrong after mapping some table
1131 * entries, undo every change done up to this point.
Antonio Nino Diazac998032017-02-27 17:23:54 +00001132 */
1133 mmap_region_t unmap_mm = {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001134 .base_pa = 0U,
Antonio Nino Diazac998032017-02-27 17:23:54 +00001135 .base_va = mm->base_va,
1136 .size = end_va - mm->base_va,
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001137 .attr = 0U
Antonio Nino Diazac998032017-02-27 17:23:54 +00001138 };
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001139 xlat_tables_unmap_region(ctx, &unmap_mm, 0U,
1140 ctx->base_table, ctx->base_table_entries,
1141 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001142#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1143 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1144 ctx->base_table_entries * sizeof(uint64_t));
1145#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +00001146 return -ENOMEM;
1147 }
1148
1149 /*
1150 * Make sure that all entries are written to the memory. There
1151 * is no need to invalidate entries when mapping dynamic regions
1152 * because new table/block/page descriptors only replace old
1153 * invalid descriptors, that aren't TLB cached.
1154 */
1155 dsbishst();
1156 }
1157
1158 if (end_pa > ctx->max_pa)
1159 ctx->max_pa = end_pa;
1160 if (end_va > ctx->max_va)
1161 ctx->max_va = end_va;
1162
1163 return 0;
1164}
1165
Antonio Nino Diazc0033282018-11-20 16:03:11 +00001166int mmap_add_dynamic_region_alloc_va_ctx(xlat_ctx_t *ctx, mmap_region_t *mm)
1167{
1168 mm->base_va = ctx->max_va + 1UL;
1169
1170 if (mm->size == 0U)
1171 return 0;
1172
1173 mmap_alloc_va_align_ctx(ctx, mm);
1174
1175 /* Detect overflows. More checks are done in mmap_add_region_check(). */
1176 if (mm->base_va < ctx->max_va) {
1177 return -ENOMEM;
1178 }
1179
1180 return mmap_add_dynamic_region_ctx(ctx, mm);
1181}
1182
Antonio Nino Diazac998032017-02-27 17:23:54 +00001183/*
1184 * Removes the region with given base Virtual Address and size from the given
1185 * context.
1186 *
1187 * Returns:
1188 * 0: Success.
1189 * EINVAL: Invalid values were used as arguments (region not found).
1190 * EPERM: Tried to remove a static region.
1191 */
1192int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
1193 size_t size)
1194{
1195 mmap_region_t *mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001196 const mmap_region_t *mm_last = mm + ctx->mmap_num;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001197 int update_max_va_needed = 0;
1198 int update_max_pa_needed = 0;
1199
1200 /* Check sanity of mmap array. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001201 assert(mm[ctx->mmap_num].size == 0U);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001202
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001203 while (mm->size != 0U) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001204 if ((mm->base_va == base_va) && (mm->size == size))
1205 break;
1206 ++mm;
1207 }
1208
1209 /* Check that the region was found */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001210 if (mm->size == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001211 return -EINVAL;
1212
1213 /* If the region is static it can't be removed */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001214 if ((mm->attr & MT_DYNAMIC) == 0U)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001215 return -EPERM;
1216
1217 /* Check if this region is using the top VAs or PAs. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001218 if ((mm->base_va + mm->size - 1U) == ctx->max_va)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001219 update_max_va_needed = 1;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001220 if ((mm->base_pa + mm->size - 1U) == ctx->max_pa)
Antonio Nino Diazac998032017-02-27 17:23:54 +00001221 update_max_pa_needed = 1;
1222
1223 /* Update the translation tables if needed */
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001224 if (ctx->initialized) {
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001225 xlat_tables_unmap_region(ctx, mm, 0U, ctx->base_table,
Antonio Nino Diazac998032017-02-27 17:23:54 +00001226 ctx->base_table_entries,
1227 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001228#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1229 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1230 ctx->base_table_entries * sizeof(uint64_t));
1231#endif
Antonio Nino Diazac998032017-02-27 17:23:54 +00001232 xlat_arch_tlbi_va_sync();
1233 }
1234
1235 /* Remove this region by moving the rest down by one place. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001236 (void)memmove(mm, mm + 1U, (uintptr_t)mm_last - (uintptr_t)mm);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001237
1238 /* Check if we need to update the max VAs and PAs */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001239 if (update_max_va_needed == 1) {
1240 ctx->max_va = 0U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001241 mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001242 while (mm->size != 0U) {
1243 if ((mm->base_va + mm->size - 1U) > ctx->max_va)
1244 ctx->max_va = mm->base_va + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001245 ++mm;
1246 }
1247 }
1248
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001249 if (update_max_pa_needed == 1) {
1250 ctx->max_pa = 0U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001251 mm = ctx->mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001252 while (mm->size != 0U) {
1253 if ((mm->base_pa + mm->size - 1U) > ctx->max_pa)
1254 ctx->max_pa = mm->base_pa + mm->size - 1U;
Antonio Nino Diazac998032017-02-27 17:23:54 +00001255 ++mm;
1256 }
1257 }
1258
1259 return 0;
1260}
1261
Antonio Nino Diaz675d1552018-10-30 11:36:47 +00001262void xlat_setup_dynamic_ctx(xlat_ctx_t *ctx, unsigned long long pa_max,
1263 uintptr_t va_max, struct mmap_region *mmap,
1264 unsigned int mmap_num, uint64_t **tables,
1265 unsigned int tables_num, uint64_t *base_table,
1266 int xlat_regime, int *mapped_regions)
1267{
1268 ctx->xlat_regime = xlat_regime;
1269
1270 ctx->pa_max_address = pa_max;
1271 ctx->va_max_address = va_max;
1272
1273 ctx->mmap = mmap;
1274 ctx->mmap_num = mmap_num;
1275 memset(ctx->mmap, 0, sizeof(struct mmap_region) * mmap_num);
1276
1277 ctx->tables = (void *) tables;
1278 ctx->tables_num = tables_num;
1279
1280 uintptr_t va_space_size = va_max + 1;
1281 ctx->base_level = GET_XLAT_TABLE_LEVEL_BASE(va_space_size);
1282 ctx->base_table = base_table;
1283 ctx->base_table_entries = GET_NUM_BASE_LEVEL_ENTRIES(va_space_size);
1284
1285 ctx->tables_mapped_regions = mapped_regions;
1286
1287 ctx->max_pa = 0;
1288 ctx->max_va = 0;
1289 ctx->initialized = 0;
1290}
1291
Antonio Nino Diazac998032017-02-27 17:23:54 +00001292#endif /* PLAT_XLAT_TABLES_DYNAMIC */
1293
Daniel Boulby5a03a252018-08-30 16:48:56 +01001294void __init init_xlat_tables_ctx(xlat_ctx_t *ctx)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001295{
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +01001296 assert(ctx != NULL);
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001297 assert(!ctx->initialized);
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001298 assert((ctx->xlat_regime == EL3_REGIME) ||
Antonio Nino Diaz128de8d2018-08-07 19:59:49 +01001299 (ctx->xlat_regime == EL2_REGIME) ||
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001300 (ctx->xlat_regime == EL1_EL0_REGIME));
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001301 assert(!is_mmu_enabled_ctx(ctx));
Sandrine Bailleux66342932017-07-18 13:26:36 +01001302
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +01001303 mmap_region_t *mm = ctx->mmap;
Sandrine Bailleux66342932017-07-18 13:26:36 +01001304
Sathees Balya74155972019-01-25 11:36:01 +00001305 assert(ctx->va_max_address >=
1306 (xlat_get_min_virt_addr_space_size() - 1U));
1307 assert(ctx->va_max_address <= (MAX_VIRT_ADDR_SPACE_SIZE - 1U));
1308 assert(IS_POWER_OF_TWO(ctx->va_max_address + 1U));
1309
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +01001310 xlat_mmap_print(mm);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001311
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001312 /* All tables must be zeroed before mapping any region. */
1313
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001314 for (unsigned int i = 0U; i < ctx->base_table_entries; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001315 ctx->base_table[i] = INVALID_DESC;
1316
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001317 for (int j = 0; j < ctx->tables_num; j++) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001318#if PLAT_XLAT_TABLES_DYNAMIC
1319 ctx->tables_mapped_regions[j] = 0;
1320#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001321 for (unsigned int i = 0U; i < XLAT_TABLE_ENTRIES; i++)
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001322 ctx->tables[j][i] = INVALID_DESC;
1323 }
1324
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001325 while (mm->size != 0U) {
1326 uintptr_t end_va = xlat_tables_map_region(ctx, mm, 0U,
1327 ctx->base_table, ctx->base_table_entries,
1328 ctx->base_level);
Antonio Nino Diaz37a5efa2018-08-07 12:47:12 +01001329#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
1330 xlat_clean_dcache_range((uintptr_t)ctx->base_table,
1331 ctx->base_table_entries * sizeof(uint64_t));
1332#endif
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001333 if (end_va != (mm->base_va + mm->size - 1U)) {
Antonio Nino Diazac998032017-02-27 17:23:54 +00001334 ERROR("Not enough memory to map region:\n"
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +01001335 " VA:0x%lx PA:0x%llx size:0x%zx attr:0x%x\n",
1336 mm->base_va, mm->base_pa, mm->size, mm->attr);
Antonio Nino Diazac998032017-02-27 17:23:54 +00001337 panic();
1338 }
1339
1340 mm++;
1341 }
1342
Sandrine Bailleux46c53a22017-07-11 15:11:10 +01001343 assert(ctx->pa_max_address <= xlat_arch_get_max_supported_pa());
Sandrine Bailleux66342932017-07-18 13:26:36 +01001344 assert(ctx->max_va <= ctx->va_max_address);
1345 assert(ctx->max_pa <= ctx->pa_max_address);
1346
Antonio Nino Diaz5c97bd12018-08-02 09:57:29 +01001347 ctx->initialized = true;
Sandrine Bailleuxc5b63772017-05-31 13:31:48 +01001348
1349 xlat_tables_print(ctx);
Sandrine Bailleux66342932017-07-18 13:26:36 +01001350}