Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016, 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 | #include <arch_helpers.h> |
| 31 | #include <bl_common.h> |
| 32 | #include <console.h> |
| 33 | #include <debug.h> |
| 34 | #include <libfdt.h> |
| 35 | #include <platform_def.h> |
| 36 | #include "qemu_private.h" |
| 37 | #include <string.h> |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * The next 2 constants identify the extents of the code & RO data region. |
| 42 | * These addresses are used by the MMU setup code and therefore they must be |
| 43 | * page-aligned. It is the responsibility of the linker script to ensure that |
| 44 | * __RO_START__ and __RO_END__ linker symbols refer to page-aligned addresses. |
| 45 | */ |
| 46 | #define BL2_RO_BASE (unsigned long)(&__RO_START__) |
| 47 | #define BL2_RO_LIMIT (unsigned long)(&__RO_END__) |
| 48 | |
| 49 | /* |
| 50 | * The next 2 constants identify the extents of the coherent memory region. |
| 51 | * These addresses are used by the MMU setup code and therefore they must be |
| 52 | * page-aligned. It is the responsibility of the linker script to ensure that |
| 53 | * __COHERENT_RAM_START__ and __COHERENT_RAM_END__ linker symbols refer to |
| 54 | * page-aligned addresses. |
| 55 | */ |
| 56 | #define BL2_COHERENT_RAM_BASE (unsigned long)(&__COHERENT_RAM_START__) |
| 57 | #define BL2_COHERENT_RAM_LIMIT (unsigned long)(&__COHERENT_RAM_END__) |
| 58 | |
| 59 | /******************************************************************************* |
| 60 | * This structure represents the superset of information that is passed to |
| 61 | * BL3-1, e.g. while passing control to it from BL2, bl31_params |
| 62 | * and other platform specific params |
| 63 | ******************************************************************************/ |
| 64 | typedef struct bl2_to_bl31_params_mem { |
| 65 | bl31_params_t bl31_params; |
| 66 | image_info_t bl31_image_info; |
| 67 | image_info_t bl32_image_info; |
| 68 | image_info_t bl33_image_info; |
| 69 | entry_point_info_t bl33_ep_info; |
| 70 | entry_point_info_t bl32_ep_info; |
| 71 | entry_point_info_t bl31_ep_info; |
| 72 | } bl2_to_bl31_params_mem_t; |
| 73 | |
| 74 | |
| 75 | static bl2_to_bl31_params_mem_t bl31_params_mem; |
| 76 | |
| 77 | |
| 78 | |
| 79 | /* Data structure which holds the extents of the trusted SRAM for BL2 */ |
| 80 | static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); |
| 81 | |
| 82 | meminfo_t *bl2_plat_sec_mem_layout(void) |
| 83 | { |
| 84 | return &bl2_tzram_layout; |
| 85 | } |
| 86 | |
| 87 | /******************************************************************************* |
| 88 | * This function assigns a pointer to the memory that the platform has kept |
| 89 | * aside to pass platform specific and trusted firmware related information |
| 90 | * to BL31. This memory is allocated by allocating memory to |
| 91 | * bl2_to_bl31_params_mem_t structure which is a superset of all the |
| 92 | * structure whose information is passed to BL31 |
| 93 | * NOTE: This function should be called only once and should be done |
| 94 | * before generating params to BL31 |
| 95 | ******************************************************************************/ |
| 96 | bl31_params_t *bl2_plat_get_bl31_params(void) |
| 97 | { |
| 98 | bl31_params_t *bl2_to_bl31_params; |
| 99 | |
| 100 | /* |
| 101 | * Initialise the memory for all the arguments that needs to |
| 102 | * be passed to BL3-1 |
| 103 | */ |
| 104 | memset(&bl31_params_mem, 0, sizeof(bl2_to_bl31_params_mem_t)); |
| 105 | |
| 106 | /* Assign memory for TF related information */ |
| 107 | bl2_to_bl31_params = &bl31_params_mem.bl31_params; |
| 108 | SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0); |
| 109 | |
| 110 | /* Fill BL3-1 related information */ |
| 111 | bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info; |
| 112 | SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY, |
| 113 | VERSION_1, 0); |
| 114 | |
| 115 | /* Fill BL3-2 related information */ |
| 116 | bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info; |
| 117 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP, |
| 118 | VERSION_1, 0); |
| 119 | bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info; |
| 120 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY, |
| 121 | VERSION_1, 0); |
| 122 | |
| 123 | /* Fill BL3-3 related information */ |
| 124 | bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 125 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info, |
| 126 | PARAM_EP, VERSION_1, 0); |
| 127 | |
| 128 | /* BL3-3 expects to receive the primary CPU MPID (through x0) */ |
| 129 | bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr(); |
| 130 | |
| 131 | bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info; |
| 132 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY, |
| 133 | VERSION_1, 0); |
| 134 | |
| 135 | return bl2_to_bl31_params; |
| 136 | } |
| 137 | |
| 138 | /* Flush the TF params and the TF plat params */ |
| 139 | void bl2_plat_flush_bl31_params(void) |
| 140 | { |
| 141 | flush_dcache_range((unsigned long)&bl31_params_mem, |
| 142 | sizeof(bl2_to_bl31_params_mem_t)); |
| 143 | } |
| 144 | |
| 145 | /******************************************************************************* |
| 146 | * This function returns a pointer to the shared memory that the platform |
| 147 | * has kept to point to entry point information of BL31 to BL2 |
| 148 | ******************************************************************************/ |
| 149 | struct entry_point_info *bl2_plat_get_bl31_ep_info(void) |
| 150 | { |
| 151 | #if DEBUG |
| 152 | bl31_params_mem.bl31_ep_info.args.arg1 = QEMU_BL31_PLAT_PARAM_VAL; |
| 153 | #endif |
| 154 | |
| 155 | return &bl31_params_mem.bl31_ep_info; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | void bl2_early_platform_setup(meminfo_t *mem_layout) |
| 161 | { |
| 162 | /* Initialize the console to provide early debug support */ |
| 163 | console_init(PLAT_QEMU_BOOT_UART_BASE, PLAT_QEMU_BOOT_UART_CLK_IN_HZ, |
| 164 | PLAT_QEMU_CONSOLE_BAUDRATE); |
| 165 | |
| 166 | /* Setup the BL2 memory layout */ |
| 167 | bl2_tzram_layout = *mem_layout; |
| 168 | |
| 169 | plat_qemu_io_setup(); |
| 170 | } |
| 171 | |
| 172 | static void security_setup(void) |
| 173 | { |
| 174 | /* |
| 175 | * This is where a TrustZone address space controller and other |
| 176 | * security related peripherals, would be configured. |
| 177 | */ |
| 178 | } |
| 179 | |
| 180 | static void update_dt(void) |
| 181 | { |
| 182 | int ret; |
| 183 | void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE; |
| 184 | |
| 185 | ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE); |
| 186 | if (ret < 0) { |
| 187 | ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (dt_add_psci_node(fdt)) { |
| 192 | ERROR("Failed to add PSCI Device Tree node\n"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (dt_add_psci_cpu_enable_methods(fdt)) { |
| 197 | ERROR("Failed to add PSCI cpu enable methods in Device Tree\n"); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | ret = fdt_pack(fdt); |
| 202 | if (ret < 0) |
| 203 | ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret); |
| 204 | } |
| 205 | |
| 206 | void bl2_platform_setup(void) |
| 207 | { |
| 208 | security_setup(); |
| 209 | update_dt(); |
| 210 | |
| 211 | /* TODO Initialize timer */ |
| 212 | } |
| 213 | |
| 214 | void bl2_plat_arch_setup(void) |
| 215 | { |
| 216 | qemu_configure_mmu_el1(bl2_tzram_layout.total_base, |
| 217 | bl2_tzram_layout.total_size, |
| 218 | BL2_RO_BASE, BL2_RO_LIMIT, |
| 219 | BL2_COHERENT_RAM_BASE, BL2_COHERENT_RAM_LIMIT); |
| 220 | } |
| 221 | |
| 222 | /******************************************************************************* |
| 223 | * Gets SPSR for BL32 entry |
| 224 | ******************************************************************************/ |
| 225 | static uint32_t qemu_get_spsr_for_bl32_entry(void) |
| 226 | { |
| 227 | /* |
| 228 | * The Secure Payload Dispatcher service is responsible for |
| 229 | * setting the SPSR prior to entry into the BL3-2 image. |
| 230 | */ |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /******************************************************************************* |
| 235 | * Gets SPSR for BL33 entry |
| 236 | ******************************************************************************/ |
| 237 | static uint32_t qemu_get_spsr_for_bl33_entry(void) |
| 238 | { |
| 239 | unsigned long el_status; |
| 240 | unsigned int mode; |
| 241 | uint32_t spsr; |
| 242 | |
| 243 | /* Figure out what mode we enter the non-secure world in */ |
| 244 | el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT; |
| 245 | el_status &= ID_AA64PFR0_ELX_MASK; |
| 246 | |
| 247 | mode = (el_status) ? MODE_EL2 : MODE_EL1; |
| 248 | |
| 249 | /* |
| 250 | * TODO: Consider the possibility of specifying the SPSR in |
| 251 | * the FIP ToC and allowing the platform to have a say as |
| 252 | * well. |
| 253 | */ |
| 254 | spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
| 255 | return spsr; |
| 256 | } |
| 257 | |
| 258 | /******************************************************************************* |
| 259 | * Before calling this function BL3-1 is loaded in memory and its entrypoint |
| 260 | * is set by load_image. This is a placeholder for the platform to change |
| 261 | * the entrypoint of BL3-1 and set SPSR and security state. |
| 262 | * On ARM standard platforms we only set the security state of the entrypoint |
| 263 | ******************************************************************************/ |
| 264 | void bl2_plat_set_bl31_ep_info(image_info_t *bl31_image_info, |
| 265 | entry_point_info_t *bl31_ep_info) |
| 266 | { |
| 267 | SET_SECURITY_STATE(bl31_ep_info->h.attr, SECURE); |
| 268 | bl31_ep_info->spsr = SPSR_64(MODE_EL3, MODE_SP_ELX, |
| 269 | DISABLE_ALL_EXCEPTIONS); |
| 270 | } |
| 271 | |
| 272 | /******************************************************************************* |
| 273 | * Before calling this function BL3-2 is loaded in memory and its entrypoint |
| 274 | * is set by load_image. This is a placeholder for the platform to change |
| 275 | * the entrypoint of BL3-2 and set SPSR and security state. |
| 276 | * On ARM standard platforms we only set the security state of the entrypoint |
| 277 | ******************************************************************************/ |
| 278 | void bl2_plat_set_bl32_ep_info(image_info_t *bl32_image_info, |
| 279 | entry_point_info_t *bl32_ep_info) |
| 280 | { |
| 281 | SET_SECURITY_STATE(bl32_ep_info->h.attr, SECURE); |
| 282 | bl32_ep_info->spsr = qemu_get_spsr_for_bl32_entry(); |
| 283 | } |
| 284 | |
| 285 | /******************************************************************************* |
| 286 | * Before calling this function BL3-3 is loaded in memory and its entrypoint |
| 287 | * is set by load_image. This is a placeholder for the platform to change |
| 288 | * the entrypoint of BL3-3 and set SPSR and security state. |
| 289 | * On ARM standard platforms we only set the security state of the entrypoint |
| 290 | ******************************************************************************/ |
| 291 | void bl2_plat_set_bl33_ep_info(image_info_t *image, |
| 292 | entry_point_info_t *bl33_ep_info) |
| 293 | { |
| 294 | |
| 295 | SET_SECURITY_STATE(bl33_ep_info->h.attr, NON_SECURE); |
| 296 | bl33_ep_info->spsr = qemu_get_spsr_for_bl33_entry(); |
| 297 | } |
| 298 | |
| 299 | /******************************************************************************* |
| 300 | * Populate the extents of memory available for loading BL32 |
| 301 | ******************************************************************************/ |
| 302 | void bl2_plat_get_bl32_meminfo(meminfo_t *bl32_meminfo) |
| 303 | { |
| 304 | /* |
| 305 | * Populate the extents of memory available for loading BL32. |
| 306 | */ |
| 307 | bl32_meminfo->total_base = BL32_BASE; |
| 308 | bl32_meminfo->free_base = BL32_BASE; |
| 309 | bl32_meminfo->total_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE; |
| 310 | bl32_meminfo->free_size = (BL32_MEM_BASE + BL32_MEM_SIZE) - BL32_BASE; |
| 311 | } |
| 312 | |
| 313 | /******************************************************************************* |
| 314 | * Populate the extents of memory available for loading BL33 |
| 315 | ******************************************************************************/ |
| 316 | void bl2_plat_get_bl33_meminfo(meminfo_t *bl33_meminfo) |
| 317 | { |
| 318 | bl33_meminfo->total_base = NS_DRAM0_BASE; |
| 319 | bl33_meminfo->total_size = NS_DRAM0_SIZE; |
| 320 | bl33_meminfo->free_base = NS_DRAM0_BASE; |
| 321 | bl33_meminfo->free_size = NS_DRAM0_SIZE; |
| 322 | } |
| 323 | |
| 324 | unsigned long plat_get_ns_image_entrypoint(void) |
| 325 | { |
| 326 | return NS_IMAGE_OFFSET; |
| 327 | } |