xref: /rk3399_ARM-atf/lib/xlat_tables_v2/xlat_tables_utils.c (revision cd08e7883ccdb707f87301adae86f21b39a75d86)
1fd2299e6SAntonio Nino Diaz /*
29bfe78c2SLevi Yun  * Copyright (c) 2017-2025, Arm Limited and Contributors. All rights reserved.
3fd2299e6SAntonio Nino Diaz  *
4fd2299e6SAntonio Nino Diaz  * SPDX-License-Identifier: BSD-3-Clause
5fd2299e6SAntonio Nino Diaz  */
6fd2299e6SAntonio Nino Diaz 
7fd2299e6SAntonio Nino Diaz #include <assert.h>
8fd2299e6SAntonio Nino Diaz #include <errno.h>
9956d76f6SJavier Almansa Sobrino #include <inttypes.h>
105b395e37SAntonio Nino Diaz #include <stdbool.h>
1193c78ed2SAntonio Nino Diaz #include <stdint.h>
1239b6cc66SAntonio Nino Diaz #include <stdio.h>
1309d40e0eSAntonio Nino Diaz 
1409d40e0eSAntonio Nino Diaz #include <platform_def.h>
1509d40e0eSAntonio Nino Diaz 
16e3c0869fSManish V Badarkhe #include <arch_features.h>
1709d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1809d40e0eSAntonio Nino Diaz #include <common/debug.h>
1909d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
2009d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_defs.h>
2109d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2.h>
22fd2299e6SAntonio Nino Diaz 
23fd2299e6SAntonio Nino Diaz #include "xlat_tables_private.h"
24fd2299e6SAntonio Nino Diaz 
25fd2299e6SAntonio Nino Diaz #if LOG_LEVEL < LOG_LEVEL_VERBOSE
26fd2299e6SAntonio Nino Diaz 
xlat_mmap_print(__unused const mmap_region_t * mmap)27e7b9886cSAntonio Nino Diaz void xlat_mmap_print(__unused const mmap_region_t *mmap)
28fd2299e6SAntonio Nino Diaz {
29fd2299e6SAntonio Nino Diaz 	/* Empty */
30fd2299e6SAntonio Nino Diaz }
31fd2299e6SAntonio Nino Diaz 
xlat_tables_print(__unused xlat_ctx_t * ctx)32fd2299e6SAntonio Nino Diaz void xlat_tables_print(__unused xlat_ctx_t *ctx)
33fd2299e6SAntonio Nino Diaz {
34fd2299e6SAntonio Nino Diaz 	/* Empty */
35fd2299e6SAntonio Nino Diaz }
36fd2299e6SAntonio Nino Diaz 
37fd2299e6SAntonio Nino Diaz #else /* if LOG_LEVEL >= LOG_LEVEL_VERBOSE */
38fd2299e6SAntonio Nino Diaz 
xlat_mmap_print(const mmap_region_t * mmap)39e7b9886cSAntonio Nino Diaz void xlat_mmap_print(const mmap_region_t *mmap)
40fd2299e6SAntonio Nino Diaz {
4139b6cc66SAntonio Nino Diaz 	printf("mmap:\n");
42fd2299e6SAntonio Nino Diaz 	const mmap_region_t *mm = mmap;
43fd2299e6SAntonio Nino Diaz 
44fd2299e6SAntonio Nino Diaz 	while (mm->size != 0U) {
4539b6cc66SAntonio Nino Diaz 		printf(" VA:0x%lx  PA:0x%llx  size:0x%zx  attr:0x%x  granularity:0x%zx\n",
4639b6cc66SAntonio Nino Diaz 		       mm->base_va, mm->base_pa, mm->size, mm->attr,
4739b6cc66SAntonio Nino Diaz 		       mm->granularity);
48fd2299e6SAntonio Nino Diaz 		++mm;
49fd2299e6SAntonio Nino Diaz 	};
5039b6cc66SAntonio Nino Diaz 	printf("\n");
51fd2299e6SAntonio Nino Diaz }
52fd2299e6SAntonio Nino Diaz 
53fd2299e6SAntonio Nino Diaz /* Print the attributes of the specified block descriptor. */
xlat_desc_print(const xlat_ctx_t * ctx,uint64_t desc)54fd2299e6SAntonio Nino Diaz static void xlat_desc_print(const xlat_ctx_t *ctx, uint64_t desc)
55fd2299e6SAntonio Nino Diaz {
56e7b9886cSAntonio Nino Diaz 	uint64_t mem_type_index = ATTR_INDEX_GET(desc);
57fd2299e6SAntonio Nino Diaz 	int xlat_regime = ctx->xlat_regime;
58fd2299e6SAntonio Nino Diaz 
59fd2299e6SAntonio Nino Diaz 	if (mem_type_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
6039b6cc66SAntonio Nino Diaz 		printf("MEM");
61fd2299e6SAntonio Nino Diaz 	} else if (mem_type_index == ATTR_NON_CACHEABLE_INDEX) {
6239b6cc66SAntonio Nino Diaz 		printf("NC");
63fd2299e6SAntonio Nino Diaz 	} else {
64fd2299e6SAntonio Nino Diaz 		assert(mem_type_index == ATTR_DEVICE_INDEX);
6539b6cc66SAntonio Nino Diaz 		printf("DEV");
66fd2299e6SAntonio Nino Diaz 	}
67fd2299e6SAntonio Nino Diaz 
681a92a0e0SAntonio Nino Diaz 	if ((xlat_regime == EL3_REGIME) || (xlat_regime == EL2_REGIME)) {
691a92a0e0SAntonio Nino Diaz 		/* For EL3 and EL2 only check the AP[2] and XN bits. */
7039b6cc66SAntonio Nino Diaz 		printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
7139b6cc66SAntonio Nino Diaz 		printf(((desc & UPPER_ATTRS(XN)) != 0ULL) ? "-XN" : "-EXEC");
72fd2299e6SAntonio Nino Diaz 	} else {
73f9d58d17SAntonio Nino Diaz 		assert(xlat_regime == EL1_EL0_REGIME);
74fd2299e6SAntonio Nino Diaz 		/*
75f9d58d17SAntonio Nino Diaz 		 * For EL0 and EL1:
76f9d58d17SAntonio Nino Diaz 		 * - In AArch64 PXN and UXN can be set independently but in
77f9d58d17SAntonio Nino Diaz 		 *   AArch32 there is no UXN (XN affects both privilege levels).
78f9d58d17SAntonio Nino Diaz 		 *   For consistency, we set them simultaneously in both cases.
79f9d58d17SAntonio Nino Diaz 		 * - RO and RW permissions must be the same in EL1 and EL0. If
80f9d58d17SAntonio Nino Diaz 		 *   EL0 can access that memory region, so can EL1, with the
81f9d58d17SAntonio Nino Diaz 		 *   same permissions.
82fd2299e6SAntonio Nino Diaz 		 */
83f9d58d17SAntonio Nino Diaz #if ENABLE_ASSERTIONS
84f9d58d17SAntonio Nino Diaz 		uint64_t xn_mask = xlat_arch_regime_get_xn_desc(EL1_EL0_REGIME);
85f9d58d17SAntonio Nino Diaz 		uint64_t xn_perm = desc & xn_mask;
86fd2299e6SAntonio Nino Diaz 
87f9d58d17SAntonio Nino Diaz 		assert((xn_perm == xn_mask) || (xn_perm == 0ULL));
88f9d58d17SAntonio Nino Diaz #endif
8939b6cc66SAntonio Nino Diaz 		printf(((desc & LOWER_ATTRS(AP_RO)) != 0ULL) ? "-RO" : "-RW");
90f9d58d17SAntonio Nino Diaz 		/* Only check one of PXN and UXN, the other one is the same. */
9139b6cc66SAntonio Nino Diaz 		printf(((desc & UPPER_ATTRS(PXN)) != 0ULL) ? "-XN" : "-EXEC");
92f9d58d17SAntonio Nino Diaz 		/*
93f9d58d17SAntonio Nino Diaz 		 * Privileged regions can only be accessed from EL1, user
94f9d58d17SAntonio Nino Diaz 		 * regions can be accessed from EL1 and EL0.
95f9d58d17SAntonio Nino Diaz 		 */
9639b6cc66SAntonio Nino Diaz 		printf(((desc & LOWER_ATTRS(AP_ACCESS_UNPRIVILEGED)) != 0ULL)
97f9d58d17SAntonio Nino Diaz 			  ? "-USER" : "-PRIV");
98fd2299e6SAntonio Nino Diaz 	}
99fd2299e6SAntonio Nino Diaz 
10036218238SZelalem Aweke #if ENABLE_RME
10136218238SZelalem Aweke 	switch (desc & LOWER_ATTRS(EL3_S1_NSE | NS)) {
10236218238SZelalem Aweke 	case 0ULL:
10336218238SZelalem Aweke 		printf("-S");
10436218238SZelalem Aweke 		break;
10536218238SZelalem Aweke 	case LOWER_ATTRS(NS):
10636218238SZelalem Aweke 		printf("-NS");
10736218238SZelalem Aweke 		break;
10836218238SZelalem Aweke 	case LOWER_ATTRS(EL3_S1_NSE):
10936218238SZelalem Aweke 		printf("-RT");
11036218238SZelalem Aweke 		break;
11136218238SZelalem Aweke 	default: /* LOWER_ATTRS(EL3_S1_NSE | NS) */
11236218238SZelalem Aweke 		printf("-RL");
11336218238SZelalem Aweke 	}
11436218238SZelalem Aweke #else
11539b6cc66SAntonio Nino Diaz 	printf(((LOWER_ATTRS(NS) & desc) != 0ULL) ? "-NS" : "-S");
11636218238SZelalem Aweke #endif
1179fc59639SAlexei Fedorov 
118402b3cf8SJulius Werner #ifdef __aarch64__
1199fc59639SAlexei Fedorov 	/* Check Guarded Page bit */
1209fc59639SAlexei Fedorov 	if ((desc & GP) != 0ULL) {
1219fc59639SAlexei Fedorov 		printf("-GP");
1229fc59639SAlexei Fedorov 	}
1239fc59639SAlexei Fedorov #endif
124fd2299e6SAntonio Nino Diaz }
125fd2299e6SAntonio Nino Diaz 
126fd2299e6SAntonio Nino Diaz static const char * const level_spacers[] = {
127fd2299e6SAntonio Nino Diaz 	"[LV0] ",
128fd2299e6SAntonio Nino Diaz 	"  [LV1] ",
129fd2299e6SAntonio Nino Diaz 	"    [LV2] ",
130fd2299e6SAntonio Nino Diaz 	"      [LV3] "
131fd2299e6SAntonio Nino Diaz };
132fd2299e6SAntonio Nino Diaz 
133fd2299e6SAntonio Nino Diaz static const char *invalid_descriptors_ommited =
134fd2299e6SAntonio Nino Diaz 		"%s(%d invalid descriptors omitted)\n";
135fd2299e6SAntonio Nino Diaz 
136fd2299e6SAntonio Nino Diaz /*
137f253645dSAntonio Nino Diaz  * Recursive function that reads the translation tables passed as an argument
138fd2299e6SAntonio Nino Diaz  * and prints their status.
139fd2299e6SAntonio Nino Diaz  */
xlat_tables_print_internal(xlat_ctx_t * ctx,uintptr_t table_base_va,const uint64_t * table_base,unsigned int table_entries,unsigned int level)140e7b9886cSAntonio Nino Diaz static void xlat_tables_print_internal(xlat_ctx_t *ctx, uintptr_t table_base_va,
141e7b9886cSAntonio Nino Diaz 		const uint64_t *table_base, unsigned int table_entries,
142e7b9886cSAntonio Nino Diaz 		unsigned int level)
143fd2299e6SAntonio Nino Diaz {
144fd2299e6SAntonio Nino Diaz 	assert(level <= XLAT_TABLE_LEVEL_MAX);
145fd2299e6SAntonio Nino Diaz 
146f253645dSAntonio Nino Diaz 	uint64_t desc;
147c54c7fc3SDavid Pu 	uintptr_t table_idx_va = table_base_va;
148f253645dSAntonio Nino Diaz 	unsigned int table_idx = 0U;
149f253645dSAntonio Nino Diaz 	size_t level_size = XLAT_BLOCK_SIZE(level);
150fd2299e6SAntonio Nino Diaz 
151fd2299e6SAntonio Nino Diaz 	/*
152fd2299e6SAntonio Nino Diaz 	 * Keep track of how many invalid descriptors are counted in a row.
153fd2299e6SAntonio Nino Diaz 	 * Whenever multiple invalid descriptors are found, only the first one
154fd2299e6SAntonio Nino Diaz 	 * is printed, and a line is added to inform about how many descriptors
155fd2299e6SAntonio Nino Diaz 	 * have been omitted.
156fd2299e6SAntonio Nino Diaz 	 */
157fd2299e6SAntonio Nino Diaz 	int invalid_row_count = 0;
158fd2299e6SAntonio Nino Diaz 
159f253645dSAntonio Nino Diaz 	while (table_idx < table_entries) {
160fd2299e6SAntonio Nino Diaz 
161f253645dSAntonio Nino Diaz 		desc = table_base[table_idx];
162c54c7fc3SDavid Pu 
163c54c7fc3SDavid Pu 		if ((desc & DESC_MASK) == INVALID_DESC) {
164f253645dSAntonio Nino Diaz 
165c54c7fc3SDavid Pu 			if (invalid_row_count == 0) {
166c54c7fc3SDavid Pu 				printf("%sVA:0x%lx size:0x%zx\n",
167f253645dSAntonio Nino Diaz 				       level_spacers[level],
168c54c7fc3SDavid Pu 				       table_idx_va, level_size);
169c54c7fc3SDavid Pu 			}
170c54c7fc3SDavid Pu 			invalid_row_count++;
171f253645dSAntonio Nino Diaz 
172c54c7fc3SDavid Pu 		} else {
173f253645dSAntonio Nino Diaz 
174c54c7fc3SDavid Pu 			if (invalid_row_count > 1) {
175c54c7fc3SDavid Pu 				printf(invalid_descriptors_ommited,
176f253645dSAntonio Nino Diaz 				       level_spacers[level],
177c54c7fc3SDavid Pu 				       invalid_row_count - 1);
178c54c7fc3SDavid Pu 			}
179c54c7fc3SDavid Pu 			invalid_row_count = 0;
180f253645dSAntonio Nino Diaz 
181fd2299e6SAntonio Nino Diaz 			/*
182f253645dSAntonio Nino Diaz 			 * Check if this is a table or a block. Tables are only
183f253645dSAntonio Nino Diaz 			 * allowed in levels other than 3, but DESC_PAGE has the
184f253645dSAntonio Nino Diaz 			 * same value as DESC_TABLE, so we need to check.
185c54c7fc3SDavid Pu 			 */
186c54c7fc3SDavid Pu 			if (((desc & DESC_MASK) == TABLE_DESC) &&
187f253645dSAntonio Nino Diaz 					(level < XLAT_TABLE_LEVEL_MAX)) {
188c54c7fc3SDavid Pu 				/*
189f253645dSAntonio Nino Diaz 				 * Do not print any PA for a table descriptor,
190f253645dSAntonio Nino Diaz 				 * as it doesn't directly map physical memory
191f253645dSAntonio Nino Diaz 				 * but instead points to the next translation
192fd2299e6SAntonio Nino Diaz 				 * table in the translation table walk.
193fd2299e6SAntonio Nino Diaz 				 */
19439b6cc66SAntonio Nino Diaz 				printf("%sVA:0x%lx size:0x%zx\n",
195f253645dSAntonio Nino Diaz 				       level_spacers[level],
196e7b9886cSAntonio Nino Diaz 				       table_idx_va, level_size);
197fd2299e6SAntonio Nino Diaz 
198f253645dSAntonio Nino Diaz 				uintptr_t addr_inner = desc & TABLE_ADDR_MASK;
199fd2299e6SAntonio Nino Diaz 
200f253645dSAntonio Nino Diaz 				xlat_tables_print_internal(ctx, table_idx_va,
201f253645dSAntonio Nino Diaz 					(uint64_t *)addr_inner,
202f253645dSAntonio Nino Diaz 					XLAT_TABLE_ENTRIES, level + 1U);
203fd2299e6SAntonio Nino Diaz 			} else {
204956d76f6SJavier Almansa Sobrino 				printf("%sVA:0x%lx PA:0x%" PRIx64 " size:0x%zx ",
205f253645dSAntonio Nino Diaz 				       level_spacers[level], table_idx_va,
206e7b9886cSAntonio Nino Diaz 				       (uint64_t)(desc & TABLE_ADDR_MASK),
207fd2299e6SAntonio Nino Diaz 				       level_size);
208fd2299e6SAntonio Nino Diaz 				xlat_desc_print(ctx, desc);
20939b6cc66SAntonio Nino Diaz 				printf("\n");
210f253645dSAntonio Nino Diaz 			}
211f253645dSAntonio Nino Diaz 		}
212fd2299e6SAntonio Nino Diaz 
213fd2299e6SAntonio Nino Diaz 		table_idx++;
214fd2299e6SAntonio Nino Diaz 		table_idx_va += level_size;
215f253645dSAntonio Nino Diaz 	}
216fd2299e6SAntonio Nino Diaz 
217f253645dSAntonio Nino Diaz 	if (invalid_row_count > 1) {
218f253645dSAntonio Nino Diaz 		printf(invalid_descriptors_ommited,
219f253645dSAntonio Nino Diaz 		       level_spacers[level], invalid_row_count - 1);
220fd2299e6SAntonio Nino Diaz 	}
221fd2299e6SAntonio Nino Diaz }
222fd2299e6SAntonio Nino Diaz 
xlat_tables_print(xlat_ctx_t * ctx)223fd2299e6SAntonio Nino Diaz void xlat_tables_print(xlat_ctx_t *ctx)
224fd2299e6SAntonio Nino Diaz {
225fd2299e6SAntonio Nino Diaz 	const char *xlat_regime_str;
226e7b9886cSAntonio Nino Diaz 	int used_page_tables;
227e7b9886cSAntonio Nino Diaz 
228fd2299e6SAntonio Nino Diaz 	if (ctx->xlat_regime == EL1_EL0_REGIME) {
229fd2299e6SAntonio Nino Diaz 		xlat_regime_str = "1&0";
2301a92a0e0SAntonio Nino Diaz 	} else if (ctx->xlat_regime == EL2_REGIME) {
2311a92a0e0SAntonio Nino Diaz 		xlat_regime_str = "2";
232fd2299e6SAntonio Nino Diaz 	} else {
233fd2299e6SAntonio Nino Diaz 		assert(ctx->xlat_regime == EL3_REGIME);
234fd2299e6SAntonio Nino Diaz 		xlat_regime_str = "3";
235fd2299e6SAntonio Nino Diaz 	}
236fd2299e6SAntonio Nino Diaz 	VERBOSE("Translation tables state:\n");
237fd2299e6SAntonio Nino Diaz 	VERBOSE("  Xlat regime:     EL%s\n", xlat_regime_str);
238fd2299e6SAntonio Nino Diaz 	VERBOSE("  Max allowed PA:  0x%llx\n", ctx->pa_max_address);
239e7b9886cSAntonio Nino Diaz 	VERBOSE("  Max allowed VA:  0x%lx\n", ctx->va_max_address);
240fd2299e6SAntonio Nino Diaz 	VERBOSE("  Max mapped PA:   0x%llx\n", ctx->max_pa);
241e7b9886cSAntonio Nino Diaz 	VERBOSE("  Max mapped VA:   0x%lx\n", ctx->max_va);
242fd2299e6SAntonio Nino Diaz 
243e7b9886cSAntonio Nino Diaz 	VERBOSE("  Initial lookup level: %u\n", ctx->base_level);
244e7b9886cSAntonio Nino Diaz 	VERBOSE("  Entries @initial lookup level: %u\n",
245fd2299e6SAntonio Nino Diaz 		ctx->base_table_entries);
246fd2299e6SAntonio Nino Diaz 
247fd2299e6SAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC
248fd2299e6SAntonio Nino Diaz 	used_page_tables = 0;
249e7b9886cSAntonio Nino Diaz 	for (int i = 0; i < ctx->tables_num; ++i) {
250fd2299e6SAntonio Nino Diaz 		if (ctx->tables_mapped_regions[i] != 0)
251fd2299e6SAntonio Nino Diaz 			++used_page_tables;
252fd2299e6SAntonio Nino Diaz 	}
253fd2299e6SAntonio Nino Diaz #else
254fd2299e6SAntonio Nino Diaz 	used_page_tables = ctx->next_table;
255fd2299e6SAntonio Nino Diaz #endif
256e7b9886cSAntonio Nino Diaz 	VERBOSE("  Used %d sub-tables out of %d (spare: %d)\n",
257fd2299e6SAntonio Nino Diaz 		used_page_tables, ctx->tables_num,
258fd2299e6SAntonio Nino Diaz 		ctx->tables_num - used_page_tables);
259fd2299e6SAntonio Nino Diaz 
260e7b9886cSAntonio Nino Diaz 	xlat_tables_print_internal(ctx, 0U, ctx->base_table,
261fd2299e6SAntonio Nino Diaz 				   ctx->base_table_entries, ctx->base_level);
262fd2299e6SAntonio Nino Diaz }
263fd2299e6SAntonio Nino Diaz 
264fd2299e6SAntonio Nino Diaz #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
265fd2299e6SAntonio Nino Diaz 
266fd2299e6SAntonio Nino Diaz /*
267fd2299e6SAntonio Nino Diaz  * Do a translation table walk to find the block or page descriptor that maps
268fd2299e6SAntonio Nino Diaz  * virtual_addr.
269fd2299e6SAntonio Nino Diaz  *
270fd2299e6SAntonio Nino Diaz  * On success, return the address of the descriptor within the translation
271fd2299e6SAntonio Nino Diaz  * table. Its lookup level is stored in '*out_level'.
272fd2299e6SAntonio Nino Diaz  * On error, return NULL.
273fd2299e6SAntonio Nino Diaz  *
274fd2299e6SAntonio Nino Diaz  * xlat_table_base
275fd2299e6SAntonio Nino Diaz  *   Base address for the initial lookup level.
276fd2299e6SAntonio Nino Diaz  * xlat_table_base_entries
277fd2299e6SAntonio Nino Diaz  *   Number of entries in the translation table for the initial lookup level.
278fd2299e6SAntonio Nino Diaz  * virt_addr_space_size
279fd2299e6SAntonio Nino Diaz  *   Size in bytes of the virtual address space.
280fd2299e6SAntonio Nino Diaz  */
find_xlat_table_entry(uintptr_t virtual_addr,uint64_t * xlat_table_base,unsigned int xlat_table_base_entries,unsigned long long virt_addr_space_size,unsigned int * out_level)281fd2299e6SAntonio Nino Diaz static uint64_t *find_xlat_table_entry(uintptr_t virtual_addr,
282*b6c1cdf5SSaivardhan Thatikonda 				       uint64_t *xlat_table_base,
283e7b9886cSAntonio Nino Diaz 				       unsigned int xlat_table_base_entries,
284fd2299e6SAntonio Nino Diaz 				       unsigned long long virt_addr_space_size,
285e7b9886cSAntonio Nino Diaz 				       unsigned int *out_level)
286fd2299e6SAntonio Nino Diaz {
287fd2299e6SAntonio Nino Diaz 	unsigned int start_level;
288fd2299e6SAntonio Nino Diaz 	uint64_t *table;
289e7b9886cSAntonio Nino Diaz 	unsigned int entries;
290fd2299e6SAntonio Nino Diaz 
291fd2299e6SAntonio Nino Diaz 	start_level = GET_XLAT_TABLE_LEVEL_BASE(virt_addr_space_size);
292fd2299e6SAntonio Nino Diaz 
293fd2299e6SAntonio Nino Diaz 	table = xlat_table_base;
294fd2299e6SAntonio Nino Diaz 	entries = xlat_table_base_entries;
295fd2299e6SAntonio Nino Diaz 
296fd2299e6SAntonio Nino Diaz 	for (unsigned int level = start_level;
297fd2299e6SAntonio Nino Diaz 	     level <= XLAT_TABLE_LEVEL_MAX;
298fd2299e6SAntonio Nino Diaz 	     ++level) {
299e7b9886cSAntonio Nino Diaz 		uint64_t idx, desc, desc_type;
300fd2299e6SAntonio Nino Diaz 
301fd2299e6SAntonio Nino Diaz 		idx = XLAT_TABLE_IDX(virtual_addr, level);
302fd2299e6SAntonio Nino Diaz 		if (idx >= entries) {
3036a086061SAntonio Nino Diaz 			WARN("Missing xlat table entry at address 0x%lx\n",
3046a086061SAntonio Nino Diaz 			     virtual_addr);
305fd2299e6SAntonio Nino Diaz 			return NULL;
306fd2299e6SAntonio Nino Diaz 		}
307fd2299e6SAntonio Nino Diaz 
308fd2299e6SAntonio Nino Diaz 		desc = table[idx];
309fd2299e6SAntonio Nino Diaz 		desc_type = desc & DESC_MASK;
310fd2299e6SAntonio Nino Diaz 
311fd2299e6SAntonio Nino Diaz 		if (desc_type == INVALID_DESC) {
312fd2299e6SAntonio Nino Diaz 			VERBOSE("Invalid entry (memory not mapped)\n");
313fd2299e6SAntonio Nino Diaz 			return NULL;
314fd2299e6SAntonio Nino Diaz 		}
315fd2299e6SAntonio Nino Diaz 
316fd2299e6SAntonio Nino Diaz 		if (level == XLAT_TABLE_LEVEL_MAX) {
317fd2299e6SAntonio Nino Diaz 			/*
3186a086061SAntonio Nino Diaz 			 * Only page descriptors allowed at the final lookup
319fd2299e6SAntonio Nino Diaz 			 * level.
320fd2299e6SAntonio Nino Diaz 			 */
321fd2299e6SAntonio Nino Diaz 			assert(desc_type == PAGE_DESC);
322fd2299e6SAntonio Nino Diaz 			*out_level = level;
323fd2299e6SAntonio Nino Diaz 			return &table[idx];
324fd2299e6SAntonio Nino Diaz 		}
325fd2299e6SAntonio Nino Diaz 
326fd2299e6SAntonio Nino Diaz 		if (desc_type == BLOCK_DESC) {
327fd2299e6SAntonio Nino Diaz 			*out_level = level;
328fd2299e6SAntonio Nino Diaz 			return &table[idx];
329fd2299e6SAntonio Nino Diaz 		}
330fd2299e6SAntonio Nino Diaz 
331fd2299e6SAntonio Nino Diaz 		assert(desc_type == TABLE_DESC);
332fd2299e6SAntonio Nino Diaz 		table = (uint64_t *)(uintptr_t)(desc & TABLE_ADDR_MASK);
333fd2299e6SAntonio Nino Diaz 		entries = XLAT_TABLE_ENTRIES;
334fd2299e6SAntonio Nino Diaz 	}
335fd2299e6SAntonio Nino Diaz 
336fd2299e6SAntonio Nino Diaz 	/*
337fd2299e6SAntonio Nino Diaz 	 * This shouldn't be reached, the translation table walk should end at
338fd2299e6SAntonio Nino Diaz 	 * most at level XLAT_TABLE_LEVEL_MAX and return from inside the loop.
339fd2299e6SAntonio Nino Diaz 	 */
3405b395e37SAntonio Nino Diaz 	assert(false);
341fd2299e6SAntonio Nino Diaz 
342fd2299e6SAntonio Nino Diaz 	return NULL;
343fd2299e6SAntonio Nino Diaz }
344fd2299e6SAntonio Nino Diaz 
345fd2299e6SAntonio Nino Diaz 
xlat_get_mem_attributes_internal(const xlat_ctx_t * ctx,uintptr_t base_va,uint32_t * attributes,uint64_t ** table_entry,unsigned long long * addr_pa,unsigned int * table_level)346e5d59519SAntonio Nino Diaz static int xlat_get_mem_attributes_internal(const xlat_ctx_t *ctx,
347e5d59519SAntonio Nino Diaz 		uintptr_t base_va, uint32_t *attributes, uint64_t **table_entry,
348e7b9886cSAntonio Nino Diaz 		unsigned long long *addr_pa, unsigned int *table_level)
349fd2299e6SAntonio Nino Diaz {
350fd2299e6SAntonio Nino Diaz 	uint64_t *entry;
351fd2299e6SAntonio Nino Diaz 	uint64_t desc;
352e7b9886cSAntonio Nino Diaz 	unsigned int level;
353fd2299e6SAntonio Nino Diaz 	unsigned long long virt_addr_space_size;
354fd2299e6SAntonio Nino Diaz 
355fd2299e6SAntonio Nino Diaz 	/*
356fd2299e6SAntonio Nino Diaz 	 * Sanity-check arguments.
357fd2299e6SAntonio Nino Diaz 	 */
358fd2299e6SAntonio Nino Diaz 	assert(ctx != NULL);
3595b395e37SAntonio Nino Diaz 	assert(ctx->initialized);
360e7b9886cSAntonio Nino Diaz 	assert((ctx->xlat_regime == EL1_EL0_REGIME) ||
3611a92a0e0SAntonio Nino Diaz 	       (ctx->xlat_regime == EL2_REGIME) ||
362e7b9886cSAntonio Nino Diaz 	       (ctx->xlat_regime == EL3_REGIME));
363fd2299e6SAntonio Nino Diaz 
364e7b9886cSAntonio Nino Diaz 	virt_addr_space_size = (unsigned long long)ctx->va_max_address + 1ULL;
365e7b9886cSAntonio Nino Diaz 	assert(virt_addr_space_size > 0U);
366fd2299e6SAntonio Nino Diaz 
367fd2299e6SAntonio Nino Diaz 	entry = find_xlat_table_entry(base_va,
368fd2299e6SAntonio Nino Diaz 				ctx->base_table,
369fd2299e6SAntonio Nino Diaz 				ctx->base_table_entries,
370fd2299e6SAntonio Nino Diaz 				virt_addr_space_size,
371fd2299e6SAntonio Nino Diaz 				&level);
372fd2299e6SAntonio Nino Diaz 	if (entry == NULL) {
373e7b9886cSAntonio Nino Diaz 		WARN("Address 0x%lx is not mapped.\n", base_va);
374fd2299e6SAntonio Nino Diaz 		return -EINVAL;
375fd2299e6SAntonio Nino Diaz 	}
376fd2299e6SAntonio Nino Diaz 
377fd2299e6SAntonio Nino Diaz 	if (addr_pa != NULL) {
378fd2299e6SAntonio Nino Diaz 		*addr_pa = *entry & TABLE_ADDR_MASK;
379fd2299e6SAntonio Nino Diaz 	}
380fd2299e6SAntonio Nino Diaz 
381fd2299e6SAntonio Nino Diaz 	if (table_entry != NULL) {
382fd2299e6SAntonio Nino Diaz 		*table_entry = entry;
383fd2299e6SAntonio Nino Diaz 	}
384fd2299e6SAntonio Nino Diaz 
385fd2299e6SAntonio Nino Diaz 	if (table_level != NULL) {
386fd2299e6SAntonio Nino Diaz 		*table_level = level;
387fd2299e6SAntonio Nino Diaz 	}
388fd2299e6SAntonio Nino Diaz 
389fd2299e6SAntonio Nino Diaz 	desc = *entry;
390fd2299e6SAntonio Nino Diaz 
391fd2299e6SAntonio Nino Diaz #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
392fd2299e6SAntonio Nino Diaz 	VERBOSE("Attributes: ");
393fd2299e6SAntonio Nino Diaz 	xlat_desc_print(ctx, desc);
39439b6cc66SAntonio Nino Diaz 	printf("\n");
395fd2299e6SAntonio Nino Diaz #endif /* LOG_LEVEL >= LOG_LEVEL_VERBOSE */
396fd2299e6SAntonio Nino Diaz 
397fd2299e6SAntonio Nino Diaz 	assert(attributes != NULL);
398e7b9886cSAntonio Nino Diaz 	*attributes = 0U;
399fd2299e6SAntonio Nino Diaz 
400e7b9886cSAntonio Nino Diaz 	uint64_t attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
401fd2299e6SAntonio Nino Diaz 
402fd2299e6SAntonio Nino Diaz 	if (attr_index == ATTR_IWBWA_OWBWA_NTR_INDEX) {
403fd2299e6SAntonio Nino Diaz 		*attributes |= MT_MEMORY;
404fd2299e6SAntonio Nino Diaz 	} else if (attr_index == ATTR_NON_CACHEABLE_INDEX) {
405fd2299e6SAntonio Nino Diaz 		*attributes |= MT_NON_CACHEABLE;
406fd2299e6SAntonio Nino Diaz 	} else {
407fd2299e6SAntonio Nino Diaz 		assert(attr_index == ATTR_DEVICE_INDEX);
408fd2299e6SAntonio Nino Diaz 		*attributes |= MT_DEVICE;
409fd2299e6SAntonio Nino Diaz 	}
410fd2299e6SAntonio Nino Diaz 
411e7b9886cSAntonio Nino Diaz 	uint64_t ap2_bit = (desc >> AP2_SHIFT) & 1U;
412fd2299e6SAntonio Nino Diaz 
413fd2299e6SAntonio Nino Diaz 	if (ap2_bit == AP2_RW)
414fd2299e6SAntonio Nino Diaz 		*attributes |= MT_RW;
415fd2299e6SAntonio Nino Diaz 
416fd2299e6SAntonio Nino Diaz 	if (ctx->xlat_regime == EL1_EL0_REGIME) {
417e7b9886cSAntonio Nino Diaz 		uint64_t ap1_bit = (desc >> AP1_SHIFT) & 1U;
418e7b9886cSAntonio Nino Diaz 
419fd2299e6SAntonio Nino Diaz 		if (ap1_bit == AP1_ACCESS_UNPRIVILEGED)
420fd2299e6SAntonio Nino Diaz 			*attributes |= MT_USER;
421fd2299e6SAntonio Nino Diaz 	}
422fd2299e6SAntonio Nino Diaz 
423e3c0869fSManish V Badarkhe 	uint64_t ns_bit = (desc >> NS_SHIFT) & 1ULL;
424fd2299e6SAntonio Nino Diaz 
425e3c0869fSManish V Badarkhe #if ENABLE_RME
426e3c0869fSManish V Badarkhe 	uint64_t nse_bit = (desc >> NSE_SHIFT) & 1ULL;
427e3c0869fSManish V Badarkhe 	uint32_t sec_state = (uint32_t)(ns_bit | (nse_bit << 1ULL));
428e3c0869fSManish V Badarkhe 
429e3c0869fSManish V Badarkhe /*
430e3c0869fSManish V Badarkhe  * =========================================================
431e3c0869fSManish V Badarkhe  *  NSE    NS  |  Output PA space
432e3c0869fSManish V Badarkhe  * =========================================================
433e3c0869fSManish V Badarkhe  *    0    0   |  Secure (if S-EL2 is present, else invalid)
434e3c0869fSManish V Badarkhe  *    0    1   |  Non-secure
435e3c0869fSManish V Badarkhe  *    1    0   |  Root
436e3c0869fSManish V Badarkhe  *    1    1   |  Realm
437e3c0869fSManish V Badarkhe  *==========================================================
438e3c0869fSManish V Badarkhe  */
439e3c0869fSManish V Badarkhe 	switch (sec_state) {
440e3c0869fSManish V Badarkhe 	case 0U:
441e3c0869fSManish V Badarkhe 		/*
442e3c0869fSManish V Badarkhe 		 * We expect to get Secure mapping on an RME system only if
443e3c0869fSManish V Badarkhe 		 * S-EL2 is enabled.
444e3c0869fSManish V Badarkhe 		 * Hence panic() if we hit the case without EEL2 being enabled.
445e3c0869fSManish V Badarkhe 		 */
446e3c0869fSManish V Badarkhe 		if ((read_scr_el3() & SCR_EEL2_BIT) == 0ULL) {
447e3c0869fSManish V Badarkhe 			ERROR("A secure descriptor is not supported when"
448e3c0869fSManish V Badarkhe 			      "FEAT_RME is implemented and FEAT_SEL2 is"
449e3c0869fSManish V Badarkhe 			      "not enabled\n");
450e3c0869fSManish V Badarkhe 			panic();
451e3c0869fSManish V Badarkhe 		} else {
452e3c0869fSManish V Badarkhe 			*attributes |= MT_SECURE;
453e3c0869fSManish V Badarkhe 		}
454e3c0869fSManish V Badarkhe 		break;
455e3c0869fSManish V Badarkhe 	case 1U:
456fd2299e6SAntonio Nino Diaz 		*attributes |= MT_NS;
457e3c0869fSManish V Badarkhe 		break;
458e3c0869fSManish V Badarkhe 	case 2U:
459e3c0869fSManish V Badarkhe 		*attributes |= MT_ROOT;
460e3c0869fSManish V Badarkhe 		break;
461e3c0869fSManish V Badarkhe 	case 3U:
462e3c0869fSManish V Badarkhe 		*attributes |= MT_REALM;
463e3c0869fSManish V Badarkhe 		break;
464e3c0869fSManish V Badarkhe 	default:
465e3c0869fSManish V Badarkhe 		/* unreachable code */
466e3c0869fSManish V Badarkhe 		assert(false);
467e3c0869fSManish V Badarkhe 		break;
468e3c0869fSManish V Badarkhe 	}
469e3c0869fSManish V Badarkhe #else /* !ENABLE_RME */
470e3c0869fSManish V Badarkhe 	if (ns_bit == 1ULL) {
471e3c0869fSManish V Badarkhe 		*attributes |= MT_NS;
472e3c0869fSManish V Badarkhe 	} else {
473e3c0869fSManish V Badarkhe 		*attributes |= MT_SECURE;
474e3c0869fSManish V Badarkhe 	}
475e3c0869fSManish V Badarkhe #endif /* ENABLE_RME */
476fd2299e6SAntonio Nino Diaz 
477fd2299e6SAntonio Nino Diaz 	uint64_t xn_mask = xlat_arch_regime_get_xn_desc(ctx->xlat_regime);
478fd2299e6SAntonio Nino Diaz 
479fd2299e6SAntonio Nino Diaz 	if ((desc & xn_mask) == xn_mask) {
480fd2299e6SAntonio Nino Diaz 		*attributes |= MT_EXECUTE_NEVER;
481fd2299e6SAntonio Nino Diaz 	} else {
482e7b9886cSAntonio Nino Diaz 		assert((desc & xn_mask) == 0U);
483fd2299e6SAntonio Nino Diaz 	}
484fd2299e6SAntonio Nino Diaz 
485fd2299e6SAntonio Nino Diaz 	return 0;
486fd2299e6SAntonio Nino Diaz }
487fd2299e6SAntonio Nino Diaz 
488fd2299e6SAntonio Nino Diaz 
xlat_get_mem_attributes_ctx(const xlat_ctx_t * ctx,uintptr_t base_va,uint32_t * attr,unsigned int * table_level)489e5d59519SAntonio Nino Diaz int xlat_get_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
4909bfe78c2SLevi Yun 				uint32_t *attr, unsigned int *table_level)
491fd2299e6SAntonio Nino Diaz {
492e5d59519SAntonio Nino Diaz 	return xlat_get_mem_attributes_internal(ctx, base_va, attr,
4939bfe78c2SLevi Yun 				NULL, NULL, table_level);
494fd2299e6SAntonio Nino Diaz }
495fd2299e6SAntonio Nino Diaz 
496fd2299e6SAntonio Nino Diaz 
xlat_change_mem_attributes_ctx(const xlat_ctx_t * ctx,uintptr_t base_va,size_t size,uint32_t attr)497e5d59519SAntonio Nino Diaz int xlat_change_mem_attributes_ctx(const xlat_ctx_t *ctx, uintptr_t base_va,
498e5d59519SAntonio Nino Diaz 				   size_t size, uint32_t attr)
499fd2299e6SAntonio Nino Diaz {
500fd2299e6SAntonio Nino Diaz 	/* Note: This implementation isn't optimized. */
501fd2299e6SAntonio Nino Diaz 
502fd2299e6SAntonio Nino Diaz 	assert(ctx != NULL);
5035b395e37SAntonio Nino Diaz 	assert(ctx->initialized);
504fd2299e6SAntonio Nino Diaz 
505fd2299e6SAntonio Nino Diaz 	unsigned long long virt_addr_space_size =
506e7b9886cSAntonio Nino Diaz 		(unsigned long long)ctx->va_max_address + 1U;
507e7b9886cSAntonio Nino Diaz 	assert(virt_addr_space_size > 0U);
508fd2299e6SAntonio Nino Diaz 
509fd2299e6SAntonio Nino Diaz 	if (!IS_PAGE_ALIGNED(base_va)) {
510e7b9886cSAntonio Nino Diaz 		WARN("%s: Address 0x%lx is not aligned on a page boundary.\n",
511e7b9886cSAntonio Nino Diaz 		     __func__, base_va);
512fd2299e6SAntonio Nino Diaz 		return -EINVAL;
513fd2299e6SAntonio Nino Diaz 	}
514fd2299e6SAntonio Nino Diaz 
515e7b9886cSAntonio Nino Diaz 	if (size == 0U) {
516fd2299e6SAntonio Nino Diaz 		WARN("%s: Size is 0.\n", __func__);
517fd2299e6SAntonio Nino Diaz 		return -EINVAL;
518fd2299e6SAntonio Nino Diaz 	}
519fd2299e6SAntonio Nino Diaz 
520e7b9886cSAntonio Nino Diaz 	if ((size % PAGE_SIZE) != 0U) {
521fd2299e6SAntonio Nino Diaz 		WARN("%s: Size 0x%zx is not a multiple of a page size.\n",
522fd2299e6SAntonio Nino Diaz 		     __func__, size);
523fd2299e6SAntonio Nino Diaz 		return -EINVAL;
524fd2299e6SAntonio Nino Diaz 	}
525fd2299e6SAntonio Nino Diaz 
526e7b9886cSAntonio Nino Diaz 	if (((attr & MT_EXECUTE_NEVER) == 0U) && ((attr & MT_RW) != 0U)) {
5276a086061SAntonio Nino Diaz 		WARN("%s: Mapping memory as read-write and executable not allowed.\n",
528fd2299e6SAntonio Nino Diaz 		     __func__);
529fd2299e6SAntonio Nino Diaz 		return -EINVAL;
530fd2299e6SAntonio Nino Diaz 	}
531fd2299e6SAntonio Nino Diaz 
532e7b9886cSAntonio Nino Diaz 	size_t pages_count = size / PAGE_SIZE;
533fd2299e6SAntonio Nino Diaz 
534e7b9886cSAntonio Nino Diaz 	VERBOSE("Changing memory attributes of %zu pages starting from address 0x%lx...\n",
535e7b9886cSAntonio Nino Diaz 		pages_count, base_va);
536fd2299e6SAntonio Nino Diaz 
537fd2299e6SAntonio Nino Diaz 	uintptr_t base_va_original = base_va;
538fd2299e6SAntonio Nino Diaz 
539fd2299e6SAntonio Nino Diaz 	/*
540fd2299e6SAntonio Nino Diaz 	 * Sanity checks.
541fd2299e6SAntonio Nino Diaz 	 */
542d7b5f408SJimmy Brisson 	for (unsigned int i = 0U; i < pages_count; ++i) {
543e7b9886cSAntonio Nino Diaz 		const uint64_t *entry;
544e7b9886cSAntonio Nino Diaz 		uint64_t desc, attr_index;
545e7b9886cSAntonio Nino Diaz 		unsigned int level;
546fd2299e6SAntonio Nino Diaz 
547fd2299e6SAntonio Nino Diaz 		entry = find_xlat_table_entry(base_va,
548fd2299e6SAntonio Nino Diaz 					      ctx->base_table,
549fd2299e6SAntonio Nino Diaz 					      ctx->base_table_entries,
550fd2299e6SAntonio Nino Diaz 					      virt_addr_space_size,
551fd2299e6SAntonio Nino Diaz 					      &level);
552fd2299e6SAntonio Nino Diaz 		if (entry == NULL) {
553e7b9886cSAntonio Nino Diaz 			WARN("Address 0x%lx is not mapped.\n", base_va);
554fd2299e6SAntonio Nino Diaz 			return -EINVAL;
555fd2299e6SAntonio Nino Diaz 		}
556fd2299e6SAntonio Nino Diaz 
557fd2299e6SAntonio Nino Diaz 		desc = *entry;
558fd2299e6SAntonio Nino Diaz 
559fd2299e6SAntonio Nino Diaz 		/*
560fd2299e6SAntonio Nino Diaz 		 * Check that all the required pages are mapped at page
561fd2299e6SAntonio Nino Diaz 		 * granularity.
562fd2299e6SAntonio Nino Diaz 		 */
563fd2299e6SAntonio Nino Diaz 		if (((desc & DESC_MASK) != PAGE_DESC) ||
564fd2299e6SAntonio Nino Diaz 			(level != XLAT_TABLE_LEVEL_MAX)) {
565e7b9886cSAntonio Nino Diaz 			WARN("Address 0x%lx is not mapped at the right granularity.\n",
566e7b9886cSAntonio Nino Diaz 			     base_va);
567d7b5f408SJimmy Brisson 			WARN("Granularity is 0x%lx, should be 0x%lx.\n",
568d7b5f408SJimmy Brisson 			     XLAT_BLOCK_SIZE(level), PAGE_SIZE);
569fd2299e6SAntonio Nino Diaz 			return -EINVAL;
570fd2299e6SAntonio Nino Diaz 		}
571fd2299e6SAntonio Nino Diaz 
572fd2299e6SAntonio Nino Diaz 		/*
573fd2299e6SAntonio Nino Diaz 		 * If the region type is device, it shouldn't be executable.
574fd2299e6SAntonio Nino Diaz 		 */
575e7b9886cSAntonio Nino Diaz 		attr_index = (desc >> ATTR_INDEX_SHIFT) & ATTR_INDEX_MASK;
576fd2299e6SAntonio Nino Diaz 		if (attr_index == ATTR_DEVICE_INDEX) {
577e7b9886cSAntonio Nino Diaz 			if ((attr & MT_EXECUTE_NEVER) == 0U) {
578e7b9886cSAntonio Nino Diaz 				WARN("Setting device memory as executable at address 0x%lx.",
579e7b9886cSAntonio Nino Diaz 				     base_va);
580fd2299e6SAntonio Nino Diaz 				return -EINVAL;
581fd2299e6SAntonio Nino Diaz 			}
582fd2299e6SAntonio Nino Diaz 		}
583fd2299e6SAntonio Nino Diaz 
584fd2299e6SAntonio Nino Diaz 		base_va += PAGE_SIZE;
585fd2299e6SAntonio Nino Diaz 	}
586fd2299e6SAntonio Nino Diaz 
587fd2299e6SAntonio Nino Diaz 	/* Restore original value. */
588fd2299e6SAntonio Nino Diaz 	base_va = base_va_original;
589fd2299e6SAntonio Nino Diaz 
590e7b9886cSAntonio Nino Diaz 	for (unsigned int i = 0U; i < pages_count; ++i) {
591fd2299e6SAntonio Nino Diaz 
592e7b9886cSAntonio Nino Diaz 		uint32_t old_attr = 0U, new_attr;
593e7b9886cSAntonio Nino Diaz 		uint64_t *entry = NULL;
594e7b9886cSAntonio Nino Diaz 		unsigned int level = 0U;
595e7b9886cSAntonio Nino Diaz 		unsigned long long addr_pa = 0ULL;
596fd2299e6SAntonio Nino Diaz 
597e5d59519SAntonio Nino Diaz 		(void) xlat_get_mem_attributes_internal(ctx, base_va, &old_attr,
598fd2299e6SAntonio Nino Diaz 					    &entry, &addr_pa, &level);
599fd2299e6SAntonio Nino Diaz 
600fd2299e6SAntonio Nino Diaz 		/*
601fd2299e6SAntonio Nino Diaz 		 * From attr, only MT_RO/MT_RW, MT_EXECUTE/MT_EXECUTE_NEVER and
602fd2299e6SAntonio Nino Diaz 		 * MT_USER/MT_PRIVILEGED are taken into account. Any other
603fd2299e6SAntonio Nino Diaz 		 * information is ignored.
604fd2299e6SAntonio Nino Diaz 		 */
605fd2299e6SAntonio Nino Diaz 
606fd2299e6SAntonio Nino Diaz 		/* Clean the old attributes so that they can be rebuilt. */
607fd2299e6SAntonio Nino Diaz 		new_attr = old_attr & ~(MT_RW | MT_EXECUTE_NEVER | MT_USER);
608fd2299e6SAntonio Nino Diaz 
609fd2299e6SAntonio Nino Diaz 		/*
610fd2299e6SAntonio Nino Diaz 		 * Update attributes, but filter out the ones this function
611fd2299e6SAntonio Nino Diaz 		 * isn't allowed to change.
612fd2299e6SAntonio Nino Diaz 		 */
613fd2299e6SAntonio Nino Diaz 		new_attr |= attr & (MT_RW | MT_EXECUTE_NEVER | MT_USER);
614fd2299e6SAntonio Nino Diaz 
615fd2299e6SAntonio Nino Diaz 		/*
616fd2299e6SAntonio Nino Diaz 		 * The break-before-make sequence requires writing an invalid
617fd2299e6SAntonio Nino Diaz 		 * descriptor and making sure that the system sees the change
618fd2299e6SAntonio Nino Diaz 		 * before writing the new descriptor.
619fd2299e6SAntonio Nino Diaz 		 */
620fd2299e6SAntonio Nino Diaz 		*entry = INVALID_DESC;
6210e7a0540SArtsem Artsemenka #if !HW_ASSISTED_COHERENCY
6223e318e40SAntonio Nino Diaz 		dccvac((uintptr_t)entry);
6233e318e40SAntonio Nino Diaz #endif
624fd2299e6SAntonio Nino Diaz 		/* Invalidate any cached copy of this mapping in the TLBs. */
6258d164bc6SAntonio Nino Diaz 		xlat_arch_tlbi_va(base_va, ctx->xlat_regime);
626fd2299e6SAntonio Nino Diaz 
627fd2299e6SAntonio Nino Diaz 		/* Ensure completion of the invalidation. */
628fd2299e6SAntonio Nino Diaz 		xlat_arch_tlbi_va_sync();
629fd2299e6SAntonio Nino Diaz 
630fd2299e6SAntonio Nino Diaz 		/* Write new descriptor */
631fd2299e6SAntonio Nino Diaz 		*entry = xlat_desc(ctx, new_attr, addr_pa, level);
6320e7a0540SArtsem Artsemenka #if !HW_ASSISTED_COHERENCY
6333e318e40SAntonio Nino Diaz 		dccvac((uintptr_t)entry);
6343e318e40SAntonio Nino Diaz #endif
635fd2299e6SAntonio Nino Diaz 		base_va += PAGE_SIZE;
636fd2299e6SAntonio Nino Diaz 	}
637fd2299e6SAntonio Nino Diaz 
6381b491eeaSElyes Haouas 	/* Ensure that the last descriptor written is seen by the system. */
639fd2299e6SAntonio Nino Diaz 	dsbish();
640fd2299e6SAntonio Nino Diaz 
641fd2299e6SAntonio Nino Diaz 	return 0;
642fd2299e6SAntonio Nino Diaz }
643