Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 5 | */ |
| 6 | #include <arch_helpers.h> |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 7 | #include <assert.h> |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 8 | #include <bl_common.h> |
| 9 | #include <console.h> |
| 10 | #include <debug.h> |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 11 | #include <desc_image_load.h> |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 12 | #include <libfdt.h> |
| 13 | #include <platform_def.h> |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 14 | #include <string.h> |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 15 | #include <utils.h> |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 16 | #include "qemu_private.h" |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * The next 2 constants identify the extents of the code & RO data region. |
| 20 | * These addresses are used by the MMU setup code and therefore they must be |
| 21 | * page-aligned. It is the responsibility of the linker script to ensure that |
| 22 | * __RO_START__ and __RO_END__ linker symbols refer to page-aligned addresses. |
| 23 | */ |
| 24 | #define BL2_RO_BASE (unsigned long)(&__RO_START__) |
| 25 | #define BL2_RO_LIMIT (unsigned long)(&__RO_END__) |
| 26 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 27 | /* Data structure which holds the extents of the trusted SRAM for BL2 */ |
| 28 | static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); |
| 29 | |
| 30 | #if !LOAD_IMAGE_V2 |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 31 | /******************************************************************************* |
| 32 | * This structure represents the superset of information that is passed to |
| 33 | * BL3-1, e.g. while passing control to it from BL2, bl31_params |
| 34 | * and other platform specific params |
| 35 | ******************************************************************************/ |
| 36 | typedef struct bl2_to_bl31_params_mem { |
| 37 | bl31_params_t bl31_params; |
| 38 | image_info_t bl31_image_info; |
| 39 | image_info_t bl32_image_info; |
| 40 | image_info_t bl33_image_info; |
| 41 | entry_point_info_t bl33_ep_info; |
| 42 | entry_point_info_t bl32_ep_info; |
| 43 | entry_point_info_t bl31_ep_info; |
| 44 | } bl2_to_bl31_params_mem_t; |
| 45 | |
| 46 | |
| 47 | static bl2_to_bl31_params_mem_t bl31_params_mem; |
| 48 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 49 | |
| 50 | meminfo_t *bl2_plat_sec_mem_layout(void) |
| 51 | { |
| 52 | return &bl2_tzram_layout; |
| 53 | } |
| 54 | |
| 55 | /******************************************************************************* |
| 56 | * This function assigns a pointer to the memory that the platform has kept |
| 57 | * aside to pass platform specific and trusted firmware related information |
| 58 | * to BL31. This memory is allocated by allocating memory to |
| 59 | * bl2_to_bl31_params_mem_t structure which is a superset of all the |
| 60 | * structure whose information is passed to BL31 |
| 61 | * NOTE: This function should be called only once and should be done |
| 62 | * before generating params to BL31 |
| 63 | ******************************************************************************/ |
| 64 | bl31_params_t *bl2_plat_get_bl31_params(void) |
| 65 | { |
| 66 | bl31_params_t *bl2_to_bl31_params; |
| 67 | |
| 68 | /* |
| 69 | * Initialise the memory for all the arguments that needs to |
| 70 | * be passed to BL3-1 |
| 71 | */ |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 72 | zeromem(&bl31_params_mem, sizeof(bl2_to_bl31_params_mem_t)); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 73 | |
| 74 | /* Assign memory for TF related information */ |
| 75 | bl2_to_bl31_params = &bl31_params_mem.bl31_params; |
| 76 | SET_PARAM_HEAD(bl2_to_bl31_params, PARAM_BL31, VERSION_1, 0); |
| 77 | |
| 78 | /* Fill BL3-1 related information */ |
| 79 | bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info; |
| 80 | SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, PARAM_IMAGE_BINARY, |
| 81 | VERSION_1, 0); |
| 82 | |
| 83 | /* Fill BL3-2 related information */ |
| 84 | bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info; |
| 85 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, PARAM_EP, |
| 86 | VERSION_1, 0); |
| 87 | bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info; |
| 88 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, PARAM_IMAGE_BINARY, |
| 89 | VERSION_1, 0); |
| 90 | |
| 91 | /* Fill BL3-3 related information */ |
| 92 | bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 93 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_ep_info, |
| 94 | PARAM_EP, VERSION_1, 0); |
| 95 | |
| 96 | /* BL3-3 expects to receive the primary CPU MPID (through x0) */ |
| 97 | bl2_to_bl31_params->bl33_ep_info->args.arg0 = 0xffff & read_mpidr(); |
| 98 | |
| 99 | bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info; |
| 100 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, PARAM_IMAGE_BINARY, |
| 101 | VERSION_1, 0); |
| 102 | |
| 103 | return bl2_to_bl31_params; |
| 104 | } |
| 105 | |
| 106 | /* Flush the TF params and the TF plat params */ |
| 107 | void bl2_plat_flush_bl31_params(void) |
| 108 | { |
| 109 | flush_dcache_range((unsigned long)&bl31_params_mem, |
| 110 | sizeof(bl2_to_bl31_params_mem_t)); |
| 111 | } |
| 112 | |
| 113 | /******************************************************************************* |
| 114 | * This function returns a pointer to the shared memory that the platform |
| 115 | * has kept to point to entry point information of BL31 to BL2 |
| 116 | ******************************************************************************/ |
| 117 | struct entry_point_info *bl2_plat_get_bl31_ep_info(void) |
| 118 | { |
| 119 | #if DEBUG |
| 120 | bl31_params_mem.bl31_ep_info.args.arg1 = QEMU_BL31_PLAT_PARAM_VAL; |
| 121 | #endif |
| 122 | |
| 123 | return &bl31_params_mem.bl31_ep_info; |
| 124 | } |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 125 | #endif /* !LOAD_IMAGE_V2 */ |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 126 | |
| 127 | |
| 128 | |
| 129 | void bl2_early_platform_setup(meminfo_t *mem_layout) |
| 130 | { |
| 131 | /* Initialize the console to provide early debug support */ |
| 132 | console_init(PLAT_QEMU_BOOT_UART_BASE, PLAT_QEMU_BOOT_UART_CLK_IN_HZ, |
| 133 | PLAT_QEMU_CONSOLE_BAUDRATE); |
| 134 | |
| 135 | /* Setup the BL2 memory layout */ |
| 136 | bl2_tzram_layout = *mem_layout; |
| 137 | |
| 138 | plat_qemu_io_setup(); |
| 139 | } |
| 140 | |
| 141 | static void security_setup(void) |
| 142 | { |
| 143 | /* |
| 144 | * This is where a TrustZone address space controller and other |
| 145 | * security related peripherals, would be configured. |
| 146 | */ |
| 147 | } |
| 148 | |
| 149 | static void update_dt(void) |
| 150 | { |
| 151 | int ret; |
| 152 | void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE; |
| 153 | |
| 154 | ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE); |
| 155 | if (ret < 0) { |
| 156 | ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (dt_add_psci_node(fdt)) { |
| 161 | ERROR("Failed to add PSCI Device Tree node\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (dt_add_psci_cpu_enable_methods(fdt)) { |
| 166 | ERROR("Failed to add PSCI cpu enable methods in Device Tree\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | ret = fdt_pack(fdt); |
| 171 | if (ret < 0) |
| 172 | ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret); |
| 173 | } |
| 174 | |
| 175 | void bl2_platform_setup(void) |
| 176 | { |
| 177 | security_setup(); |
| 178 | update_dt(); |
| 179 | |
| 180 | /* TODO Initialize timer */ |
| 181 | } |
| 182 | |
| 183 | void bl2_plat_arch_setup(void) |
| 184 | { |
| 185 | qemu_configure_mmu_el1(bl2_tzram_layout.total_base, |
| 186 | bl2_tzram_layout.total_size, |
| 187 | BL2_RO_BASE, BL2_RO_LIMIT, |
Masahiro Yamada | 0fac5af | 2016-12-28 16:11:41 +0900 | [diff] [blame] | 188 | BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /******************************************************************************* |
| 192 | * Gets SPSR for BL32 entry |
| 193 | ******************************************************************************/ |
| 194 | static uint32_t qemu_get_spsr_for_bl32_entry(void) |
| 195 | { |
| 196 | /* |
| 197 | * The Secure Payload Dispatcher service is responsible for |
| 198 | * setting the SPSR prior to entry into the BL3-2 image. |
| 199 | */ |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /******************************************************************************* |
| 204 | * Gets SPSR for BL33 entry |
| 205 | ******************************************************************************/ |
| 206 | static uint32_t qemu_get_spsr_for_bl33_entry(void) |
| 207 | { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 208 | unsigned int mode; |
| 209 | uint32_t spsr; |
| 210 | |
| 211 | /* Figure out what mode we enter the non-secure world in */ |
Jeenu Viswambharan | 2a9b882 | 2017-02-21 14:40:44 +0000 | [diff] [blame] | 212 | mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * TODO: Consider the possibility of specifying the SPSR in |
| 216 | * the FIP ToC and allowing the platform to have a say as |
| 217 | * well. |
| 218 | */ |
| 219 | spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
| 220 | return spsr; |
| 221 | } |
| 222 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 223 | #if LOAD_IMAGE_V2 |
| 224 | static int qemu_bl2_handle_post_image_load(unsigned int image_id) |
| 225 | { |
| 226 | int err = 0; |
| 227 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
| 228 | |
| 229 | assert(bl_mem_params); |
| 230 | |
| 231 | switch (image_id) { |
| 232 | # ifdef AARCH64 |
| 233 | case BL32_IMAGE_ID: |
| 234 | bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry(); |
| 235 | break; |
| 236 | # endif |
| 237 | case BL33_IMAGE_ID: |
| 238 | /* BL33 expects to receive the primary CPU MPID (through r0) */ |
| 239 | bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr(); |
| 240 | bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry(); |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | return err; |
| 245 | } |
| 246 | |
| 247 | /******************************************************************************* |
| 248 | * This function can be used by the platforms to update/use image |
| 249 | * information for given `image_id`. |
| 250 | ******************************************************************************/ |
| 251 | int bl2_plat_handle_post_image_load(unsigned int image_id) |
| 252 | { |
| 253 | return qemu_bl2_handle_post_image_load(image_id); |
| 254 | } |
| 255 | |
| 256 | #else /* LOAD_IMAGE_V2 */ |
| 257 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 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 | } |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 323 | #endif /* !LOAD_IMAGE_V2 */ |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 324 | |
| 325 | unsigned long plat_get_ns_image_entrypoint(void) |
| 326 | { |
| 327 | return NS_IMAGE_OFFSET; |
| 328 | } |