blob: 7b3e555ec9add54ff2e4bf7a2269bc597d0da7d8 [file] [log] [blame]
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00001/*
2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +00005 */
6
7#ifndef __XLAT_TABLES_PRIVATE_H__
8#define __XLAT_TABLES_PRIVATE_H__
9
10#include <cassert.h>
11#include <platform_def.h>
Scott Brandenbf404c02017-04-10 11:45:52 -070012#include <utils_def.h>
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000013
14/*
15 * If the platform hasn't defined a physical and a virtual address space size
16 * default to ADDR_SPACE_SIZE.
17 */
18#if ERROR_DEPRECATED
19# ifdef ADDR_SPACE_SIZE
20# error "ADDR_SPACE_SIZE is deprecated. Use PLAT_xxx_ADDR_SPACE_SIZE instead."
21# endif
22#elif defined(ADDR_SPACE_SIZE)
23# ifndef PLAT_PHY_ADDR_SPACE_SIZE
24# define PLAT_PHY_ADDR_SPACE_SIZE ADDR_SPACE_SIZE
25# endif
26# ifndef PLAT_VIRT_ADDR_SPACE_SIZE
27# define PLAT_VIRT_ADDR_SPACE_SIZE ADDR_SPACE_SIZE
28# endif
29#endif
30
31/* The virtual and physical address space sizes must be powers of two. */
32CASSERT(IS_POWER_OF_TWO(PLAT_VIRT_ADDR_SPACE_SIZE),
33 assert_valid_virt_addr_space_size);
34CASSERT(IS_POWER_OF_TWO(PLAT_PHY_ADDR_SPACE_SIZE),
35 assert_valid_phy_addr_space_size);
36
37/* Struct that holds all information about the translation tables. */
38typedef struct {
39
40 /*
41 * Max allowed Virtual and Physical Addresses.
42 */
43 unsigned long long pa_max_address;
44 uintptr_t va_max_address;
45
46 /*
47 * Array of all memory regions stored in order of ascending end address
48 * and ascending size to simplify the code that allows overlapping
49 * regions. The list is terminated by the first entry with size == 0.
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +010050 * The max size of the list is stored in `mmap_num`. `mmap` points to an
51 * array of mmap_num + 1 elements, so that there is space for the final
52 * null entry.
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000053 */
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +010054 mmap_region_t *mmap;
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000055 int mmap_num;
56
57 /*
58 * Array of finer-grain translation tables.
59 * For example, if the initial lookup level is 1 then this array would
60 * contain both level-2 and level-3 entries.
61 */
62 uint64_t (*tables)[XLAT_TABLE_ENTRIES];
63 int tables_num;
Antonio Nino Diazac998032017-02-27 17:23:54 +000064 /*
65 * Keep track of how many regions are mapped in each table. The base
66 * table can't be unmapped so it isn't needed to keep track of it.
67 */
68#if PLAT_XLAT_TABLES_DYNAMIC
69 int *tables_mapped_regions;
70#endif /* PLAT_XLAT_TABLES_DYNAMIC */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000071
72 int next_table;
73
74 /*
75 * Base translation table. It doesn't need to have the same amount of
76 * entries as the ones used for other levels.
77 */
78 uint64_t *base_table;
79 int base_table_entries;
80
Antonio Nino Diaz9b11f392017-05-08 16:43:53 +010081 /*
82 * Max Physical and Virtual addresses currently in use by the
83 * translation tables. These might get updated as we map/unmap memory
84 * regions but they will never go beyond pa/va_max_address.
85 */
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +000086 unsigned long long max_pa;
87 uintptr_t max_va;
88
89 /* Level of the base translation table. */
90 int base_level;
91
92 /* Set to 1 when the translation tables are initialized. */
93 int initialized;
94
Antonio Nino Diazefabaa92017-04-27 13:30:22 +010095 /*
96 * Bit mask that has to be ORed to the rest of a translation table
97 * descriptor in order to prohibit execution of code at the exception
98 * level of this translation context.
99 */
100 uint64_t execute_never_mask;
101
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000102} xlat_ctx_t;
103
Antonio Nino Diazac998032017-02-27 17:23:54 +0000104#if PLAT_XLAT_TABLES_DYNAMIC
105/*
106 * Shifts and masks to access fields of an mmap_attr_t
107 */
108/* Dynamic or static */
109#define MT_DYN_SHIFT 30 /* 31 would cause undefined behaviours */
110
111/*
112 * Memory mapping private attributes
113 *
114 * Private attributes not exposed in the mmap_attr_t enum.
115 */
116typedef enum {
117 /*
118 * Regions mapped before the MMU can't be unmapped dynamically (they are
119 * static) and regions mapped with MMU enabled can be unmapped. This
120 * behaviour can't be overridden.
121 *
122 * Static regions can overlap each other, dynamic regions can't.
123 */
124 MT_STATIC = 0 << MT_DYN_SHIFT,
125 MT_DYNAMIC = 1 << MT_DYN_SHIFT
126} mmap_priv_attr_t;
127
128/*
129 * Function used to invalidate all levels of the translation walk for a given
130 * virtual address. It must be called for every translation table entry that is
131 * modified.
132 */
133void xlat_arch_tlbi_va(uintptr_t va);
134
135/*
136 * This function has to be called at the end of any code that uses the function
137 * xlat_arch_tlbi_va().
138 */
139void xlat_arch_tlbi_va_sync(void);
140
141/* Add a dynamic region to the specified context. */
142int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
143
144/* Remove a dynamic region from the specified context. */
145int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
146 size_t size);
147
148#endif /* PLAT_XLAT_TABLES_DYNAMIC */
149
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000150/* Print VA, PA, size and attributes of all regions in the mmap array. */
151void print_mmap(mmap_region_t *const mmap);
152
153/*
154 * Print the current state of the translation tables by reading them from
155 * memory.
156 */
157void xlat_tables_print(xlat_ctx_t *ctx);
158
159/*
160 * Initialize the translation tables by mapping all regions added to the
161 * specified context.
162 */
163void init_xlation_table(xlat_ctx_t *ctx);
164
165/* Add a static region to the specified context. */
166void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
167
168/*
169 * Architecture-specific initialization code.
170 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100171
172/* Returns the current Exception Level. The returned EL must be 1 or higher. */
173int xlat_arch_current_el(void);
174
175/*
176 * Returns the bit mask that has to be ORed to the rest of a translation table
177 * descriptor so that execution of code is prohibited at the given Exception
178 * Level.
179 */
180uint64_t xlat_arch_get_xn_desc(int el);
Antonio Nino Diaz233c7c12017-03-08 14:40:23 +0000181
182/* Execute architecture-specific translation table initialization code. */
183void init_xlat_tables_arch(unsigned long long max_pa);
184
185/* Enable MMU and configure it to use the specified translation tables. */
186void enable_mmu_arch(unsigned int flags, uint64_t *base_table);
187
188/* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */
189int is_mmu_enabled(void);
190
191#endif /* __XLAT_TABLES_PRIVATE_H__ */