Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 864ca6f | 2018-10-31 15:25:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, 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 | */ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 6 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 10 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 12 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <arch_helpers.h> |
| 15 | #include <common/bl_common.h> |
| 16 | #include <common/debug.h> |
| 17 | #include <common/desc_image_load.h> |
| 18 | #include <lib/optee_utils.h> |
| 19 | #include <lib/utils.h> |
| 20 | #include <plat/common/platform.h> |
| 21 | |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 22 | #include "qemu_private.h" |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 23 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 24 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 25 | /* Data structure which holds the extents of the trusted SRAM for BL2 */ |
| 26 | static meminfo_t bl2_tzram_layout __aligned(CACHE_WRITEBACK_GRANULE); |
| 27 | |
Jens Wiklander | e22b91e | 2018-09-04 14:07:19 +0200 | [diff] [blame] | 28 | void bl2_early_platform_setup2(u_register_t arg0, u_register_t arg1, |
| 29 | u_register_t arg2, u_register_t arg3) |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 30 | { |
Jens Wiklander | e22b91e | 2018-09-04 14:07:19 +0200 | [diff] [blame] | 31 | meminfo_t *mem_layout = (void *)arg1; |
| 32 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 33 | /* Initialize the console to provide early debug support */ |
Michalis Pappas | cca6cb7 | 2018-03-04 15:43:38 +0800 | [diff] [blame] | 34 | qemu_console_init(); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 35 | |
| 36 | /* Setup the BL2 memory layout */ |
| 37 | bl2_tzram_layout = *mem_layout; |
| 38 | |
| 39 | plat_qemu_io_setup(); |
| 40 | } |
| 41 | |
| 42 | static void security_setup(void) |
| 43 | { |
| 44 | /* |
| 45 | * This is where a TrustZone address space controller and other |
| 46 | * security related peripherals, would be configured. |
| 47 | */ |
| 48 | } |
| 49 | |
| 50 | static void update_dt(void) |
| 51 | { |
| 52 | int ret; |
| 53 | void *fdt = (void *)(uintptr_t)PLAT_QEMU_DT_BASE; |
| 54 | |
| 55 | ret = fdt_open_into(fdt, fdt, PLAT_QEMU_DT_MAX_SIZE); |
| 56 | if (ret < 0) { |
| 57 | ERROR("Invalid Device Tree at %p: error %d\n", fdt, ret); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (dt_add_psci_node(fdt)) { |
| 62 | ERROR("Failed to add PSCI Device Tree node\n"); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (dt_add_psci_cpu_enable_methods(fdt)) { |
| 67 | ERROR("Failed to add PSCI cpu enable methods in Device Tree\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | ret = fdt_pack(fdt); |
| 72 | if (ret < 0) |
| 73 | ERROR("Failed to pack Device Tree at %p: error %d\n", fdt, ret); |
| 74 | } |
| 75 | |
| 76 | void bl2_platform_setup(void) |
| 77 | { |
| 78 | security_setup(); |
| 79 | update_dt(); |
| 80 | |
| 81 | /* TODO Initialize timer */ |
| 82 | } |
| 83 | |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 84 | #ifdef AARCH32 |
Antonio Nino Diaz | 099b0b1 | 2018-09-26 09:29:45 +0100 | [diff] [blame] | 85 | #define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_svc_mon(__VA_ARGS__) |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 86 | #else |
| 87 | #define QEMU_CONFIGURE_BL2_MMU(...) qemu_configure_mmu_el1(__VA_ARGS__) |
| 88 | #endif |
| 89 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 90 | void bl2_plat_arch_setup(void) |
| 91 | { |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 92 | QEMU_CONFIGURE_BL2_MMU(bl2_tzram_layout.total_base, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 93 | bl2_tzram_layout.total_size, |
Michalis Pappas | ba86112 | 2018-02-28 14:36:03 +0800 | [diff] [blame] | 94 | BL_CODE_BASE, BL_CODE_END, |
| 95 | BL_RO_DATA_BASE, BL_RO_DATA_END, |
Masahiro Yamada | 0fac5af | 2016-12-28 16:11:41 +0900 | [diff] [blame] | 96 | BL_COHERENT_RAM_BASE, BL_COHERENT_RAM_END); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /******************************************************************************* |
| 100 | * Gets SPSR for BL32 entry |
| 101 | ******************************************************************************/ |
| 102 | static uint32_t qemu_get_spsr_for_bl32_entry(void) |
| 103 | { |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 104 | #ifdef AARCH64 |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 105 | /* |
| 106 | * The Secure Payload Dispatcher service is responsible for |
| 107 | * setting the SPSR prior to entry into the BL3-2 image. |
| 108 | */ |
| 109 | return 0; |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 110 | #else |
| 111 | return SPSR_MODE32(MODE32_svc, SPSR_T_ARM, SPSR_E_LITTLE, |
| 112 | DISABLE_ALL_EXCEPTIONS); |
| 113 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /******************************************************************************* |
| 117 | * Gets SPSR for BL33 entry |
| 118 | ******************************************************************************/ |
| 119 | static uint32_t qemu_get_spsr_for_bl33_entry(void) |
| 120 | { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 121 | uint32_t spsr; |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 122 | #ifdef AARCH64 |
| 123 | unsigned int mode; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 124 | |
| 125 | /* Figure out what mode we enter the non-secure world in */ |
Antonio Nino Diaz | 864ca6f | 2018-10-31 15:25:35 +0000 | [diff] [blame] | 126 | mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * TODO: Consider the possibility of specifying the SPSR in |
| 130 | * the FIP ToC and allowing the platform to have a say as |
| 131 | * well. |
| 132 | */ |
| 133 | spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 134 | #else |
| 135 | spsr = SPSR_MODE32(MODE32_svc, |
| 136 | plat_get_ns_image_entrypoint() & 0x1, |
| 137 | SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); |
| 138 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 139 | return spsr; |
| 140 | } |
| 141 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 142 | static int qemu_bl2_handle_post_image_load(unsigned int image_id) |
| 143 | { |
| 144 | int err = 0; |
| 145 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 146 | #if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 147 | bl_mem_params_node_t *pager_mem_params = NULL; |
| 148 | bl_mem_params_node_t *paged_mem_params = NULL; |
| 149 | #endif |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 150 | |
| 151 | assert(bl_mem_params); |
| 152 | |
| 153 | switch (image_id) { |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 154 | case BL32_IMAGE_ID: |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 155 | #if defined(SPD_opteed) || defined(AARCH32_SP_OPTEE) |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 156 | pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); |
| 157 | assert(pager_mem_params); |
| 158 | |
| 159 | paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); |
| 160 | assert(paged_mem_params); |
| 161 | |
| 162 | err = parse_optee_header(&bl_mem_params->ep_info, |
| 163 | &pager_mem_params->image_info, |
| 164 | &paged_mem_params->image_info); |
| 165 | if (err != 0) { |
| 166 | WARN("OPTEE header parse error.\n"); |
| 167 | } |
| 168 | |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 169 | #if defined(SPD_opteed) |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 170 | /* |
| 171 | * OP-TEE expect to receive DTB address in x2. |
| 172 | * This will be copied into x2 by dispatcher. |
| 173 | */ |
| 174 | bl_mem_params->ep_info.args.arg3 = PLAT_QEMU_DT_BASE; |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 175 | #else /* case AARCH32_SP_OPTEE */ |
| 176 | bl_mem_params->ep_info.args.arg0 = |
| 177 | bl_mem_params->ep_info.args.arg1; |
| 178 | bl_mem_params->ep_info.args.arg1 = 0; |
| 179 | bl_mem_params->ep_info.args.arg2 = PLAT_QEMU_DT_BASE; |
| 180 | bl_mem_params->ep_info.args.arg3 = 0; |
| 181 | #endif |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 182 | #endif |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 183 | bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl32_entry(); |
| 184 | break; |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 185 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 186 | case BL33_IMAGE_ID: |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 187 | #ifdef AARCH32_SP_OPTEE |
| 188 | /* AArch32 only core: OP-TEE expects NSec EP in register LR */ |
| 189 | pager_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID); |
| 190 | assert(pager_mem_params); |
| 191 | pager_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc; |
| 192 | #endif |
| 193 | |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 194 | /* BL33 expects to receive the primary CPU MPID (through r0) */ |
| 195 | bl_mem_params->ep_info.args.arg0 = 0xffff & read_mpidr(); |
| 196 | bl_mem_params->ep_info.spsr = qemu_get_spsr_for_bl33_entry(); |
| 197 | break; |
Jonathan Wright | ff957ed | 2018-03-14 15:24:00 +0000 | [diff] [blame] | 198 | default: |
| 199 | /* Do nothing in default case */ |
| 200 | break; |
Fu Wei | c2f7844 | 2017-05-27 21:21:42 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | /******************************************************************************* |
| 207 | * This function can be used by the platforms to update/use image |
| 208 | * information for given `image_id`. |
| 209 | ******************************************************************************/ |
| 210 | int bl2_plat_handle_post_image_load(unsigned int image_id) |
| 211 | { |
| 212 | return qemu_bl2_handle_post_image_load(image_id); |
| 213 | } |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 214 | |
Etienne Carriere | 911de8c | 2018-02-02 13:23:22 +0100 | [diff] [blame] | 215 | uintptr_t plat_get_ns_image_entrypoint(void) |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 216 | { |
| 217 | return NS_IMAGE_OFFSET; |
| 218 | } |