1 
2 /*
3  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <platform_def.h>
9 
10 #include <arch_helpers.h>
11 #include <common/bl_common.h>
12 #include <lib/xlat_tables/xlat_tables_v2.h>
13 
14 #include "qemu_private.h"
15 
16 #define MAP_DEVICE0	MAP_REGION_FLAT(DEVICE0_BASE,			\
17 					DEVICE0_SIZE,			\
18 					MT_DEVICE | MT_RW | MT_SECURE)
19 
20 #ifdef DEVICE1_BASE
21 #define MAP_DEVICE1	MAP_REGION_FLAT(DEVICE1_BASE,			\
22 					DEVICE1_SIZE,			\
23 					MT_DEVICE | MT_RW | MT_SECURE)
24 #endif
25 
26 #ifdef DEVICE2_BASE
27 #define MAP_DEVICE2	MAP_REGION_FLAT(DEVICE2_BASE,			\
28 					DEVICE2_SIZE,			\
29 					MT_DEVICE | MT_RW | MT_SECURE)
30 #endif
31 
32 #define MAP_SHARED_RAM	MAP_REGION_FLAT(SHARED_RAM_BASE,		\
33 					SHARED_RAM_SIZE,		\
34 					MT_DEVICE  | MT_RW | MT_SECURE)
35 
36 #define MAP_BL32_MEM	MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE,	\
37 					MT_MEMORY | MT_RW | MT_SECURE)
38 
39 #define MAP_NS_DRAM0	MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE,	\
40 					MT_MEMORY | MT_RW | MT_NS)
41 
42 #define MAP_FLASH0	MAP_REGION_FLAT(QEMU_FLASH0_BASE, QEMU_FLASH0_SIZE, \
43 					MT_MEMORY | MT_RO | MT_SECURE)
44 
45 #define MAP_FLASH1	MAP_REGION_FLAT(QEMU_FLASH1_BASE, QEMU_FLASH1_SIZE, \
46 					MT_MEMORY | MT_RO | MT_SECURE)
47 
48 /*
49  * Table of regions for various BL stages to map using the MMU.
50  * This doesn't include TZRAM as the 'mem_layout' argument passed to
51  * arm_configure_mmu_elx() will give the available subset of that,
52  */
53 #ifdef IMAGE_BL1
54 static const mmap_region_t plat_qemu_mmap[] = {
55 	MAP_FLASH0,
56 	MAP_FLASH1,
57 	MAP_SHARED_RAM,
58 	MAP_DEVICE0,
59 #ifdef MAP_DEVICE1
60 	MAP_DEVICE1,
61 #endif
62 #ifdef MAP_DEVICE2
63 	MAP_DEVICE2,
64 #endif
65 	{0}
66 };
67 #endif
68 #ifdef IMAGE_BL2
69 static const mmap_region_t plat_qemu_mmap[] = {
70 	MAP_FLASH0,
71 	MAP_FLASH1,
72 	MAP_SHARED_RAM,
73 	MAP_DEVICE0,
74 #ifdef MAP_DEVICE1
75 	MAP_DEVICE1,
76 #endif
77 #ifdef MAP_DEVICE2
78 	MAP_DEVICE2,
79 #endif
80 	MAP_NS_DRAM0,
81 #if SPM_MM
82 	QEMU_SP_IMAGE_MMAP,
83 #else
84 	MAP_BL32_MEM,
85 #endif
86 	{0}
87 };
88 #endif
89 #ifdef IMAGE_BL31
90 static const mmap_region_t plat_qemu_mmap[] = {
91 	MAP_SHARED_RAM,
92 	MAP_DEVICE0,
93 #ifdef MAP_DEVICE1
94 	MAP_DEVICE1,
95 #endif
96 #ifdef MAP_DEVICE2
97 	MAP_DEVICE2,
98 #endif
99 #if SPM_MM
100 	MAP_NS_DRAM0,
101 	QEMU_SPM_BUF_EL3_MMAP,
102 #else
103 	MAP_BL32_MEM,
104 #endif
105 	{0}
106 };
107 #endif
108 #ifdef IMAGE_BL32
109 static const mmap_region_t plat_qemu_mmap[] = {
110 	MAP_SHARED_RAM,
111 	MAP_DEVICE0,
112 #ifdef MAP_DEVICE1
113 	MAP_DEVICE1,
114 #endif
115 #ifdef MAP_DEVICE2
116 	MAP_DEVICE2,
117 #endif
118 	{0}
119 };
120 #endif
121 
122 /*******************************************************************************
123  * Macro generating the code for the function setting up the pagetables as per
124  * the platform memory map & initialize the mmu, for the given exception level
125  ******************************************************************************/
126 
127 #define DEFINE_CONFIGURE_MMU_EL(_el)					\
128 	void qemu_configure_mmu_##_el(unsigned long total_base,	\
129 				   unsigned long total_size,		\
130 				   unsigned long code_start,		\
131 				   unsigned long code_limit,		\
132 				   unsigned long ro_start,		\
133 				   unsigned long ro_limit,		\
134 				   unsigned long coh_start,		\
135 				   unsigned long coh_limit)		\
136 	{								\
137 		mmap_add_region(total_base, total_base,			\
138 				total_size,				\
139 				MT_MEMORY | MT_RW | MT_SECURE);		\
140 		mmap_add_region(code_start, code_start,			\
141 				code_limit - code_start,		\
142 				MT_CODE | MT_SECURE);			\
143 		mmap_add_region(ro_start, ro_start,			\
144 				ro_limit - ro_start,			\
145 				MT_RO_DATA | MT_SECURE);		\
146 		mmap_add_region(coh_start, coh_start,			\
147 				coh_limit - coh_start,			\
148 				MT_DEVICE | MT_RW | MT_SECURE);		\
149 		mmap_add(plat_qemu_mmap);				\
150 		init_xlat_tables();					\
151 									\
152 		enable_mmu_##_el(0);					\
153 	}
154 
155 /* Define EL1 and EL3 variants of the function initialising the MMU */
156 #ifdef __aarch64__
157 DEFINE_CONFIGURE_MMU_EL(el1)
158 DEFINE_CONFIGURE_MMU_EL(el3)
159 #else
160 DEFINE_CONFIGURE_MMU_EL(svc_mon)
161 #endif
162 
163 
164