blob: b17093fd958dcb68aa7703fb4fc9e56f30d441b7 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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
Dan Handley2bd4ef22014-04-09 13:14:54 +010031#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <assert.h>
34#include <bl_common.h>
Dan Handley714a0d22014-04-09 13:13:04 +010035#include <debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010036#include <mmio.h>
Jon Medhurstb1eb0932014-02-26 16:27:53 +000037#include <platform.h>
38#include <xlat_tables.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010039
Achin Gupta4f6ad662013-10-25 09:08:21 +010040/*******************************************************************************
41 * This array holds the characteristics of the differences between the three
42 * FVP platforms (Base, A53_A57 & Foundation). It will be populated during cold
43 * boot at each boot stage by the primary before enabling the MMU (to allow cci
44 * configuration) & used thereafter. Each BL will have its own copy to allow
45 * independent operation.
46 ******************************************************************************/
47static unsigned long platform_config[CONFIG_LIMIT];
48
49/*******************************************************************************
Sandrine Bailleux74a62b32014-05-09 11:35:36 +010050 * Macro generating the code for the function enabling the MMU in the given
51 * exception level, assuming that the pagetables have already been created.
52 *
53 * _el: Exception level at which the function will run
54 * _tcr_extra: Extra bits to set in the TCR register. This mask will
55 * be OR'ed with the default TCR value.
56 * _tlbi_fct: Function to invalidate the TLBs at the current
57 * exception level
58 ******************************************************************************/
59#define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
60 void enable_mmu_el##_el(void) \
61 { \
62 uint64_t mair, tcr, ttbr; \
63 uint32_t sctlr; \
64 \
65 assert(IS_IN_EL(_el)); \
66 assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
67 \
68 /* Set attributes in the right indices of the MAIR */ \
69 mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \
70 mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
71 ATTR_IWBWA_OWBWA_NTR_INDEX); \
72 write_mair_el##_el(mair); \
73 \
74 /* Invalidate TLBs at the current exception level */ \
75 _tlbi_fct(); \
76 \
77 /* Set TCR bits as well. */ \
78 /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
79 tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
80 TCR_RGN_INNER_WBA | TCR_T0SZ_4GB; \
81 tcr |= _tcr_extra; \
82 write_tcr_el##_el(tcr); \
83 \
84 /* Set TTBR bits as well */ \
85 ttbr = (uint64_t) l1_xlation_table; \
86 write_ttbr0_el##_el(ttbr); \
87 \
88 /* Ensure all translation table writes have drained */ \
89 /* into memory, the TLB invalidation is complete, */ \
90 /* and translation register writes are committed */ \
91 /* before enabling the MMU */ \
92 dsb(); \
93 isb(); \
94 \
95 sctlr = read_sctlr_el##_el(); \
96 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT | SCTLR_I_BIT; \
97 sctlr |= SCTLR_A_BIT | SCTLR_C_BIT; \
98 write_sctlr_el##_el(sctlr); \
99 \
100 /* Ensure the MMU enable takes effect immediately */ \
101 isb(); \
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +0000102 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100104/* Define EL1 and EL3 variants of the function enabling the MMU */
105DEFINE_ENABLE_MMU_EL(1, 0, tlbivmalle1)
106DEFINE_ENABLE_MMU_EL(3, TCR_EL3_RES1, tlbialle3)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100107
Jon Medhurstb1eb0932014-02-26 16:27:53 +0000108/*
109 * Table of regions to map using the MMU.
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100110 * This doesn't include TZRAM as the 'mem_layout' argument passed to
111 * configure_mmu_elx() will give the available subset of that,
Jon Medhurstb1eb0932014-02-26 16:27:53 +0000112 */
Dan Handleye2712bc2014-04-10 15:37:22 +0100113const mmap_region_t fvp_mmap[] = {
Jon Medhurstb1eb0932014-02-26 16:27:53 +0000114 { TZROM_BASE, TZROM_SIZE, MT_MEMORY | MT_RO | MT_SECURE },
115 { TZDRAM_BASE, TZDRAM_SIZE, MT_MEMORY | MT_RW | MT_SECURE },
116 { FLASH0_BASE, FLASH0_SIZE, MT_MEMORY | MT_RO | MT_SECURE },
117 { FLASH1_BASE, FLASH1_SIZE, MT_MEMORY | MT_RO | MT_SECURE },
118 { VRAM_BASE, VRAM_SIZE, MT_MEMORY | MT_RW | MT_SECURE },
119 { DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW | MT_SECURE },
120 { NSRAM_BASE, NSRAM_SIZE, MT_MEMORY | MT_RW | MT_NS },
121 { DEVICE1_BASE, DEVICE1_SIZE, MT_DEVICE | MT_RW | MT_SECURE },
122 /* 2nd GB as device for now...*/
123 { 0x40000000, 0x40000000, MT_DEVICE | MT_RW | MT_SECURE },
124 { DRAM_BASE, DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS },
125 {0}
126};
127
Achin Gupta4f6ad662013-10-25 09:08:21 +0100128/*******************************************************************************
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100129 * Macro generating the code for the function setting up the pagetables as per
130 * the platform memory map & initialize the mmu, for the given exception level
131 ******************************************************************************/
132#define DEFINE_CONFIGURE_MMU_EL(_el) \
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100133 void configure_mmu_el##_el(unsigned long total_base, \
134 unsigned long total_size, \
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100135 unsigned long ro_start, \
136 unsigned long ro_limit, \
137 unsigned long coh_start, \
138 unsigned long coh_limit) \
139 { \
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100140 mmap_add_region(total_base, \
141 total_size, \
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100142 MT_MEMORY | MT_RW | MT_SECURE); \
143 mmap_add_region(ro_start, ro_limit - ro_start, \
144 MT_MEMORY | MT_RO | MT_SECURE); \
145 mmap_add_region(coh_start, coh_limit - coh_start, \
146 MT_DEVICE | MT_RW | MT_SECURE); \
147 mmap_add(fvp_mmap); \
148 init_xlat_tables(); \
149 \
150 enable_mmu_el##_el(); \
151 }
Sandrine Bailleux8d69a032013-11-27 09:38:52 +0000152
Sandrine Bailleux74a62b32014-05-09 11:35:36 +0100153/* Define EL1 and EL3 variants of the function initialising the MMU */
154DEFINE_CONFIGURE_MMU_EL(1)
155DEFINE_CONFIGURE_MMU_EL(3)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100156
157/* Simple routine which returns a configuration variable value */
158unsigned long platform_get_cfgvar(unsigned int var_id)
159{
160 assert(var_id < CONFIG_LIMIT);
161 return platform_config[var_id];
162}
163
164/*******************************************************************************
165 * A single boot loader stack is expected to work on both the Foundation FVP
166 * models and the two flavours of the Base FVP models (AEMv8 & Cortex). The
167 * SYS_ID register provides a mechanism for detecting the differences between
168 * these platforms. This information is stored in a per-BL array to allow the
169 * code to take the correct path.Per BL platform configuration.
170 ******************************************************************************/
171int platform_config_setup(void)
172{
173 unsigned int rev, hbi, bld, arch, sys_id, midr_pn;
174
175 sys_id = mmio_read_32(VE_SYSREGS_BASE + V2M_SYS_ID);
176 rev = (sys_id >> SYS_ID_REV_SHIFT) & SYS_ID_REV_MASK;
177 hbi = (sys_id >> SYS_ID_HBI_SHIFT) & SYS_ID_HBI_MASK;
178 bld = (sys_id >> SYS_ID_BLD_SHIFT) & SYS_ID_BLD_MASK;
179 arch = (sys_id >> SYS_ID_ARCH_SHIFT) & SYS_ID_ARCH_MASK;
180
James Morrissey40a6f642014-02-10 14:24:36 +0000181 if ((rev != REV_FVP) || (arch != ARCH_MODEL))
182 panic();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100183
184 /*
185 * The build field in the SYS_ID tells which variant of the GIC
186 * memory is implemented by the model.
187 */
188 switch (bld) {
189 case BLD_GIC_VE_MMAP:
190 platform_config[CONFIG_GICD_ADDR] = VE_GICD_BASE;
191 platform_config[CONFIG_GICC_ADDR] = VE_GICC_BASE;
192 platform_config[CONFIG_GICH_ADDR] = VE_GICH_BASE;
193 platform_config[CONFIG_GICV_ADDR] = VE_GICV_BASE;
194 break;
195 case BLD_GIC_A53A57_MMAP:
196 platform_config[CONFIG_GICD_ADDR] = BASE_GICD_BASE;
197 platform_config[CONFIG_GICC_ADDR] = BASE_GICC_BASE;
198 platform_config[CONFIG_GICH_ADDR] = BASE_GICH_BASE;
199 platform_config[CONFIG_GICV_ADDR] = BASE_GICV_BASE;
200 break;
201 default:
202 assert(0);
203 }
204
205 /*
206 * The hbi field in the SYS_ID is 0x020 for the Base FVP & 0x010
207 * for the Foundation FVP.
208 */
209 switch (hbi) {
210 case HBI_FOUNDATION:
211 platform_config[CONFIG_MAX_AFF0] = 4;
212 platform_config[CONFIG_MAX_AFF1] = 1;
213 platform_config[CONFIG_CPU_SETUP] = 0;
214 platform_config[CONFIG_BASE_MMAP] = 0;
Harry Liebel30affd52013-10-30 17:41:48 +0000215 platform_config[CONFIG_HAS_CCI] = 0;
Harry Liebelcef93392014-04-01 19:27:38 +0100216 platform_config[CONFIG_HAS_TZC] = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100217 break;
218 case HBI_FVP_BASE:
219 midr_pn = (read_midr() >> MIDR_PN_SHIFT) & MIDR_PN_MASK;
220 if ((midr_pn == MIDR_PN_A57) || (midr_pn == MIDR_PN_A53))
221 platform_config[CONFIG_CPU_SETUP] = 1;
222 else
223 platform_config[CONFIG_CPU_SETUP] = 0;
224
225 platform_config[CONFIG_MAX_AFF0] = 4;
226 platform_config[CONFIG_MAX_AFF1] = 2;
227 platform_config[CONFIG_BASE_MMAP] = 1;
Harry Liebel30affd52013-10-30 17:41:48 +0000228 platform_config[CONFIG_HAS_CCI] = 1;
Harry Liebelcef93392014-04-01 19:27:38 +0100229 platform_config[CONFIG_HAS_TZC] = 1;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100230 break;
231 default:
232 assert(0);
233 }
234
235 return 0;
236}
237
Ian Spray84687392014-01-02 16:57:12 +0000238unsigned long plat_get_ns_image_entrypoint(void)
239{
Achin Gupta4f6ad662013-10-25 09:08:21 +0100240 return NS_IMAGE_OFFSET;
241}
Sandrine Bailleux3fa98472014-03-31 11:25:18 +0100242
243uint64_t plat_get_syscnt_freq(void)
244{
245 uint64_t counter_base_frequency;
246
247 /* Read the frequency from Frequency modes table */
248 counter_base_frequency = mmio_read_32(SYS_CNTCTL_BASE + CNTFID_OFF);
249
250 /* The first entry of the frequency modes table must not be 0 */
251 assert(counter_base_frequency != 0);
252
253 return counter_base_frequency;
254}