xref: /rk3399_ARM-atf/lib/xlat_tables_v2/xlat_tables_private.h (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
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.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 } xlat_ctx_t;
112 
113 #if PLAT_XLAT_TABLES_DYNAMIC
114 /*
115  * Shifts and masks to access fields of an mmap_attr_t
116  */
117 /* Dynamic or static */
118 #define MT_DYN_SHIFT		30 /* 31 would cause undefined behaviours */
119 
120 /*
121  * Memory mapping private attributes
122  *
123  * Private attributes not exposed in the mmap_attr_t enum.
124  */
125 typedef enum  {
126 	/*
127 	 * Regions mapped before the MMU can't be unmapped dynamically (they are
128 	 * static) and regions mapped with MMU enabled can be unmapped. This
129 	 * behaviour can't be overridden.
130 	 *
131 	 * Static regions can overlap each other, dynamic regions can't.
132 	 */
133 	MT_STATIC	= 0 << MT_DYN_SHIFT,
134 	MT_DYNAMIC	= 1 << MT_DYN_SHIFT
135 } mmap_priv_attr_t;
136 
137 /*
138  * Function used to invalidate all levels of the translation walk for a given
139  * virtual address. It must be called for every translation table entry that is
140  * modified.
141  */
142 void xlat_arch_tlbi_va(uintptr_t va);
143 
144 /*
145  * This function has to be called at the end of any code that uses the function
146  * xlat_arch_tlbi_va().
147  */
148 void xlat_arch_tlbi_va_sync(void);
149 
150 /* Add a dynamic region to the specified context. */
151 int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
152 
153 /* Remove a dynamic region from the specified context. */
154 int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va,
155 			size_t size);
156 
157 #endif /* PLAT_XLAT_TABLES_DYNAMIC */
158 
159 /* Print VA, PA, size and attributes of all regions in the mmap array. */
160 void print_mmap(mmap_region_t *const mmap);
161 
162 /*
163  * Print the current state of the translation tables by reading them from
164  * memory.
165  */
166 void xlat_tables_print(xlat_ctx_t *ctx);
167 
168 /*
169  * Initialize the translation tables by mapping all regions added to the
170  * specified context.
171  */
172 void init_xlation_table(xlat_ctx_t *ctx);
173 
174 /* Add a static region to the specified context. */
175 void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm);
176 
177 /*
178  * Architecture-specific initialization code.
179  */
180 
181 /* Execute architecture-specific translation table initialization code. */
182 void init_xlat_tables_arch(unsigned long long max_pa);
183 
184 /* Enable MMU and configure it to use the specified translation tables. */
185 void enable_mmu_arch(unsigned int flags, uint64_t *base_table);
186 
187 /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */
188 int is_mmu_enabled(void);
189 
190 #endif /* __XLAT_TABLES_PRIVATE_H__ */
191