xref: /rk3399_ARM-atf/lib/xlat_tables_v2/xlat_tables_private.h (revision a56402521f80cf1b17e3936abcc6b1772aa91e66)
1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __XLAT_TABLES_PRIVATE_H__
32 #define __XLAT_TABLES_PRIVATE_H__
33 
34 #include <cassert.h>
35 #include <platform_def.h>
36 #include <utils_def.h>
37 
38 /*
39  * If the platform hasn't defined a physical and a virtual address space size
40  * default to ADDR_SPACE_SIZE.
41  */
42 #if ERROR_DEPRECATED
43 # ifdef ADDR_SPACE_SIZE
44 #  error "ADDR_SPACE_SIZE is deprecated. Use PLAT_xxx_ADDR_SPACE_SIZE instead."
45 # endif
46 #elif defined(ADDR_SPACE_SIZE)
47 # ifndef PLAT_PHY_ADDR_SPACE_SIZE
48 #  define PLAT_PHY_ADDR_SPACE_SIZE	ADDR_SPACE_SIZE
49 # endif
50 # ifndef PLAT_VIRT_ADDR_SPACE_SIZE
51 #  define PLAT_VIRT_ADDR_SPACE_SIZE	ADDR_SPACE_SIZE
52 # endif
53 #endif
54 
55 /* The virtual and physical address space sizes must be powers of two. */
56 CASSERT(IS_POWER_OF_TWO(PLAT_VIRT_ADDR_SPACE_SIZE),
57 	assert_valid_virt_addr_space_size);
58 CASSERT(IS_POWER_OF_TWO(PLAT_PHY_ADDR_SPACE_SIZE),
59 	assert_valid_phy_addr_space_size);
60 
61 /* Struct that holds all information about the translation tables. */
62 typedef struct {
63 
64 	/*
65 	 * Max allowed Virtual and Physical Addresses.
66 	 */
67 	unsigned long long pa_max_address;
68 	uintptr_t va_max_address;
69 
70 	/*
71 	 * Array of all memory regions stored in order of ascending end address
72 	 * and ascending size to simplify the code that allows overlapping
73 	 * regions. The list is terminated by the first entry with size == 0.
74 	 */
75 	mmap_region_t *mmap; /* mmap_num + 1 elements */
76 	int mmap_num;
77 
78 	/*
79 	 * Array of finer-grain translation tables.
80 	 * For example, if the initial lookup level is 1 then this array would
81 	 * contain both level-2 and level-3 entries.
82 	 */
83 	uint64_t (*tables)[XLAT_TABLE_ENTRIES];
84 	int tables_num;
85 	/*
86 	 * Keep track of how many regions are mapped in each table. The base
87 	 * table can't be unmapped so it isn't needed to keep track of it.
88 	 */
89 #if PLAT_XLAT_TABLES_DYNAMIC
90 	int *tables_mapped_regions;
91 #endif /* PLAT_XLAT_TABLES_DYNAMIC */
92 
93 	int next_table;
94 
95 	/*
96 	 * Base translation table. It doesn't need to have the same amount of
97 	 * entries as the ones used for other levels.
98 	 */
99 	uint64_t *base_table;
100 	int base_table_entries;
101 
102 	unsigned long long max_pa;
103 	uintptr_t max_va;
104 
105 	/* Level of the base translation table. */
106 	int base_level;
107 
108 	/* Set to 1 when the translation tables are initialized. */
109 	int initialized;
110 
111 	/*
112 	 * Bit mask that has to be ORed to the rest of a translation table
113 	 * descriptor in order to prohibit execution of code at the exception
114 	 * level of this translation context.
115 	 */
116 	uint64_t execute_never_mask;
117 
118 } xlat_ctx_t;
119 
120 #if PLAT_XLAT_TABLES_DYNAMIC
121 /*
122  * Shifts and masks to access fields of an mmap_attr_t
123  */
124 /* Dynamic or static */
125 #define MT_DYN_SHIFT		30 /* 31 would cause undefined behaviours */
126 
127 /*
128  * Memory mapping private attributes
129  *
130  * Private attributes not exposed in the mmap_attr_t enum.
131  */
132 typedef enum  {
133 	/*
134 	 * Regions mapped before the MMU can't be unmapped dynamically (they are
135 	 * static) and regions mapped with MMU enabled can be unmapped. This
136 	 * behaviour can't be overridden.
137 	 *
138 	 * Static regions can overlap each other, dynamic regions can't.
139 	 */
140 	MT_STATIC	= 0 << MT_DYN_SHIFT,
141 	MT_DYNAMIC	= 1 << MT_DYN_SHIFT
142 } mmap_priv_attr_t;
143 
144 /*
145  * Function used to invalidate all levels of the translation walk for a given
146  * virtual address. It must be called for every translation table entry that is
147  * modified.
148  */
149 void xlat_arch_tlbi_va(uintptr_t va);
150 
151 /*
152  * This function has to be called at the end of any code that uses the function
153  * xlat_arch_tlbi_va().
154  */
155 void xlat_arch_tlbi_va_sync(void);
156 
157 /* Add a dynamic region to the specified context. */
158 int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
159 
160 /* Remove a dynamic region from the specified context. */
161 int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
162 			size_t size);
163 
164 #endif /* PLAT_XLAT_TABLES_DYNAMIC */
165 
166 /* Print VA, PA, size and attributes of all regions in the mmap array. */
167 void print_mmap(mmap_region_t *const mmap);
168 
169 /*
170  * Print the current state of the translation tables by reading them from
171  * memory.
172  */
173 void xlat_tables_print(xlat_ctx_t *ctx);
174 
175 /*
176  * Initialize the translation tables by mapping all regions added to the
177  * specified context.
178  */
179 void init_xlation_table(xlat_ctx_t *ctx);
180 
181 /* Add a static region to the specified context. */
182 void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
183 
184 /*
185  * Architecture-specific initialization code.
186  */
187 
188 /* Returns the current Exception Level. The returned EL must be 1 or higher. */
189 int xlat_arch_current_el(void);
190 
191 /*
192  * Returns the bit mask that has to be ORed to the rest of a translation table
193  * descriptor so that execution of code is prohibited at the given Exception
194  * Level.
195  */
196 uint64_t xlat_arch_get_xn_desc(int el);
197 
198 /* Execute architecture-specific translation table initialization code. */
199 void init_xlat_tables_arch(unsigned long long max_pa);
200 
201 /* Enable MMU and configure it to use the specified translation tables. */
202 void enable_mmu_arch(unsigned int flags, uint64_t *base_table);
203 
204 /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */
205 int is_mmu_enabled(void);
206 
207 #endif /* __XLAT_TABLES_PRIVATE_H__ */
208