Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 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 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
| 34 | #include <bl_common.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 35 | #include <debug.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 36 | #include <mmio.h> |
Jon Medhurst | b1eb093 | 2014-02-26 16:27:53 +0000 | [diff] [blame] | 37 | #include <platform.h> |
| 38 | #include <xlat_tables.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 39 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | /******************************************************************************* |
| 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 | ******************************************************************************/ |
| 47 | static unsigned long platform_config[CONFIG_LIMIT]; |
| 48 | |
| 49 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 50 | * Enable the MMU assuming that the pagetables have already been created |
| 51 | *******************************************************************************/ |
| 52 | void enable_mmu() |
| 53 | { |
| 54 | unsigned long mair, tcr, ttbr, sctlr; |
| 55 | unsigned long current_el = read_current_el(); |
| 56 | |
| 57 | /* Set the attributes in the right indices of the MAIR */ |
| 58 | mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); |
| 59 | mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, |
| 60 | ATTR_IWBWA_OWBWA_NTR_INDEX); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Set TCR bits as well. Inner & outer WBWA & shareable + T0SZ = 32 |
| 64 | */ |
| 65 | tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | |
| 66 | TCR_RGN_INNER_WBA | TCR_T0SZ_4GB; |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 67 | |
| 68 | /* Set TTBR bits as well */ |
| 69 | ttbr = (unsigned long) l1_xlation_table; |
| 70 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 71 | if (GET_EL(current_el) == MODE_EL3) { |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 72 | assert((read_sctlr_el3() & SCTLR_M_BIT) == 0); |
| 73 | |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 74 | write_mair_el3(mair); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 75 | tcr |= TCR_EL3_RES1; |
Sandrine Bailleux | 295538b | 2013-11-15 14:46:44 +0000 | [diff] [blame] | 76 | /* Invalidate EL3 TLBs */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 77 | tlbialle3(); |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 78 | |
| 79 | write_tcr_el3(tcr); |
| 80 | write_ttbr0_el3(ttbr); |
| 81 | |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 82 | /* ensure all translation table writes have drained into memory, |
| 83 | * the TLB invalidation is complete, and translation register |
| 84 | * writes are committed before enabling the MMU |
| 85 | */ |
| 86 | dsb(); |
| 87 | isb(); |
| 88 | |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 89 | sctlr = read_sctlr_el3(); |
| 90 | sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT | SCTLR_I_BIT; |
| 91 | sctlr |= SCTLR_A_BIT | SCTLR_C_BIT; |
| 92 | write_sctlr_el3(sctlr); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 93 | } else { |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 94 | assert((read_sctlr_el1() & SCTLR_M_BIT) == 0); |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 95 | |
| 96 | write_mair_el1(mair); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 97 | /* Invalidate EL1 TLBs */ |
| 98 | tlbivmalle1(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 99 | |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 100 | write_tcr_el1(tcr); |
| 101 | write_ttbr0_el1(ttbr); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 102 | |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 103 | /* ensure all translation table writes have drained into memory, |
| 104 | * the TLB invalidation is complete, and translation register |
| 105 | * writes are committed before enabling the MMU |
| 106 | */ |
| 107 | dsb(); |
| 108 | isb(); |
| 109 | |
Vikram Kanigiri | 78a6e0c | 2014-03-11 17:41:00 +0000 | [diff] [blame] | 110 | sctlr = read_sctlr_el1(); |
| 111 | sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT | SCTLR_I_BIT; |
| 112 | sctlr |= SCTLR_A_BIT | SCTLR_C_BIT; |
| 113 | write_sctlr_el1(sctlr); |
| 114 | } |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 115 | /* ensure the MMU enable takes effect immediately */ |
| 116 | isb(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
Jon Medhurst | b1eb093 | 2014-02-26 16:27:53 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Table of regions to map using the MMU. |
| 123 | * This doesn't include TZRAM as the 'mem_layout' argument passed to to |
| 124 | * configure_mmu() will give the available subset of that, |
| 125 | */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 126 | const mmap_region_t fvp_mmap[] = { |
Jon Medhurst | b1eb093 | 2014-02-26 16:27:53 +0000 | [diff] [blame] | 127 | { TZROM_BASE, TZROM_SIZE, MT_MEMORY | MT_RO | MT_SECURE }, |
| 128 | { TZDRAM_BASE, TZDRAM_SIZE, MT_MEMORY | MT_RW | MT_SECURE }, |
| 129 | { FLASH0_BASE, FLASH0_SIZE, MT_MEMORY | MT_RO | MT_SECURE }, |
| 130 | { FLASH1_BASE, FLASH1_SIZE, MT_MEMORY | MT_RO | MT_SECURE }, |
| 131 | { VRAM_BASE, VRAM_SIZE, MT_MEMORY | MT_RW | MT_SECURE }, |
| 132 | { DEVICE0_BASE, DEVICE0_SIZE, MT_DEVICE | MT_RW | MT_SECURE }, |
| 133 | { NSRAM_BASE, NSRAM_SIZE, MT_MEMORY | MT_RW | MT_NS }, |
| 134 | { DEVICE1_BASE, DEVICE1_SIZE, MT_DEVICE | MT_RW | MT_SECURE }, |
| 135 | /* 2nd GB as device for now...*/ |
| 136 | { 0x40000000, 0x40000000, MT_DEVICE | MT_RW | MT_SECURE }, |
| 137 | { DRAM_BASE, DRAM_SIZE, MT_MEMORY | MT_RW | MT_NS }, |
| 138 | {0} |
| 139 | }; |
| 140 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 141 | /******************************************************************************* |
| 142 | * Setup the pagetables as per the platform memory map & initialize the mmu |
| 143 | *******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 144 | void configure_mmu(meminfo_t *mem_layout, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 145 | unsigned long ro_start, |
| 146 | unsigned long ro_limit, |
| 147 | unsigned long coh_start, |
| 148 | unsigned long coh_limit) |
| 149 | { |
Jon Medhurst | b1eb093 | 2014-02-26 16:27:53 +0000 | [diff] [blame] | 150 | mmap_add_region(mem_layout->total_base, mem_layout->total_size, |
| 151 | MT_MEMORY | MT_RW | MT_SECURE); |
| 152 | mmap_add_region(ro_start, ro_limit - ro_start, |
| 153 | MT_MEMORY | MT_RO | MT_SECURE); |
| 154 | mmap_add_region(coh_start, coh_limit - coh_start, |
| 155 | MT_DEVICE | MT_RW | MT_SECURE); |
| 156 | |
Dan Handley | 43f5679 | 2014-04-15 10:38:02 +0100 | [diff] [blame] | 157 | mmap_add(fvp_mmap); |
Jon Medhurst | b1eb093 | 2014-02-26 16:27:53 +0000 | [diff] [blame] | 158 | |
| 159 | init_xlat_tables(); |
Sandrine Bailleux | 8d69a03 | 2013-11-27 09:38:52 +0000 | [diff] [blame] | 160 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 161 | enable_mmu(); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | /* Simple routine which returns a configuration variable value */ |
| 166 | unsigned long platform_get_cfgvar(unsigned int var_id) |
| 167 | { |
| 168 | assert(var_id < CONFIG_LIMIT); |
| 169 | return platform_config[var_id]; |
| 170 | } |
| 171 | |
| 172 | /******************************************************************************* |
| 173 | * A single boot loader stack is expected to work on both the Foundation FVP |
| 174 | * models and the two flavours of the Base FVP models (AEMv8 & Cortex). The |
| 175 | * SYS_ID register provides a mechanism for detecting the differences between |
| 176 | * these platforms. This information is stored in a per-BL array to allow the |
| 177 | * code to take the correct path.Per BL platform configuration. |
| 178 | ******************************************************************************/ |
| 179 | int platform_config_setup(void) |
| 180 | { |
| 181 | unsigned int rev, hbi, bld, arch, sys_id, midr_pn; |
| 182 | |
| 183 | sys_id = mmio_read_32(VE_SYSREGS_BASE + V2M_SYS_ID); |
| 184 | rev = (sys_id >> SYS_ID_REV_SHIFT) & SYS_ID_REV_MASK; |
| 185 | hbi = (sys_id >> SYS_ID_HBI_SHIFT) & SYS_ID_HBI_MASK; |
| 186 | bld = (sys_id >> SYS_ID_BLD_SHIFT) & SYS_ID_BLD_MASK; |
| 187 | arch = (sys_id >> SYS_ID_ARCH_SHIFT) & SYS_ID_ARCH_MASK; |
| 188 | |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 189 | if ((rev != REV_FVP) || (arch != ARCH_MODEL)) |
| 190 | panic(); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * The build field in the SYS_ID tells which variant of the GIC |
| 194 | * memory is implemented by the model. |
| 195 | */ |
| 196 | switch (bld) { |
| 197 | case BLD_GIC_VE_MMAP: |
| 198 | platform_config[CONFIG_GICD_ADDR] = VE_GICD_BASE; |
| 199 | platform_config[CONFIG_GICC_ADDR] = VE_GICC_BASE; |
| 200 | platform_config[CONFIG_GICH_ADDR] = VE_GICH_BASE; |
| 201 | platform_config[CONFIG_GICV_ADDR] = VE_GICV_BASE; |
| 202 | break; |
| 203 | case BLD_GIC_A53A57_MMAP: |
| 204 | platform_config[CONFIG_GICD_ADDR] = BASE_GICD_BASE; |
| 205 | platform_config[CONFIG_GICC_ADDR] = BASE_GICC_BASE; |
| 206 | platform_config[CONFIG_GICH_ADDR] = BASE_GICH_BASE; |
| 207 | platform_config[CONFIG_GICV_ADDR] = BASE_GICV_BASE; |
| 208 | break; |
| 209 | default: |
| 210 | assert(0); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * The hbi field in the SYS_ID is 0x020 for the Base FVP & 0x010 |
| 215 | * for the Foundation FVP. |
| 216 | */ |
| 217 | switch (hbi) { |
| 218 | case HBI_FOUNDATION: |
| 219 | platform_config[CONFIG_MAX_AFF0] = 4; |
| 220 | platform_config[CONFIG_MAX_AFF1] = 1; |
| 221 | platform_config[CONFIG_CPU_SETUP] = 0; |
| 222 | platform_config[CONFIG_BASE_MMAP] = 0; |
Harry Liebel | 30affd5 | 2013-10-30 17:41:48 +0000 | [diff] [blame] | 223 | platform_config[CONFIG_HAS_CCI] = 0; |
Harry Liebel | cef9339 | 2014-04-01 19:27:38 +0100 | [diff] [blame] | 224 | platform_config[CONFIG_HAS_TZC] = 0; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 225 | break; |
| 226 | case HBI_FVP_BASE: |
| 227 | midr_pn = (read_midr() >> MIDR_PN_SHIFT) & MIDR_PN_MASK; |
| 228 | if ((midr_pn == MIDR_PN_A57) || (midr_pn == MIDR_PN_A53)) |
| 229 | platform_config[CONFIG_CPU_SETUP] = 1; |
| 230 | else |
| 231 | platform_config[CONFIG_CPU_SETUP] = 0; |
| 232 | |
| 233 | platform_config[CONFIG_MAX_AFF0] = 4; |
| 234 | platform_config[CONFIG_MAX_AFF1] = 2; |
| 235 | platform_config[CONFIG_BASE_MMAP] = 1; |
Harry Liebel | 30affd5 | 2013-10-30 17:41:48 +0000 | [diff] [blame] | 236 | platform_config[CONFIG_HAS_CCI] = 1; |
Harry Liebel | cef9339 | 2014-04-01 19:27:38 +0100 | [diff] [blame] | 237 | platform_config[CONFIG_HAS_TZC] = 1; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | assert(0); |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 246 | unsigned long plat_get_ns_image_entrypoint(void) |
| 247 | { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 248 | return NS_IMAGE_OFFSET; |
| 249 | } |
Sandrine Bailleux | 3fa9847 | 2014-03-31 11:25:18 +0100 | [diff] [blame] | 250 | |
| 251 | uint64_t plat_get_syscnt_freq(void) |
| 252 | { |
| 253 | uint64_t counter_base_frequency; |
| 254 | |
| 255 | /* Read the frequency from Frequency modes table */ |
| 256 | counter_base_frequency = mmio_read_32(SYS_CNTCTL_BASE + CNTFID_OFF); |
| 257 | |
| 258 | /* The first entry of the frequency modes table must not be 0 */ |
| 259 | assert(counter_base_frequency != 0); |
| 260 | |
| 261 | return counter_base_frequency; |
| 262 | } |