Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Reference to the ARM TF Project, |
| 4 | * plat/arm/common/arm_bl2_setup.c |
| 5 | * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights |
| 6 | * reserved. |
| 7 | * Copyright (C) 2016 Rockchip Electronic Co.,Ltd |
| 8 | * Written by Kever Yang <kever.yang@rock-chips.com> |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 9 | * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 12 | #include <atf_common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 13 | #include <cpu_func.h> |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 15 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 17 | #include <spl.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 18 | #include <asm/cache.h> |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 19 | |
Michael Walle | 0e4d90c | 2020-11-18 17:45:57 +0100 | [diff] [blame] | 20 | /* Holds all the structures we need for bl31 parameter passing */ |
| 21 | struct bl2_to_bl31_params_mem { |
| 22 | struct bl31_params bl31_params; |
| 23 | struct atf_image_info bl31_image_info; |
| 24 | struct atf_image_info bl32_image_info; |
| 25 | struct atf_image_info bl33_image_info; |
| 26 | struct entry_point_info bl33_ep_info; |
| 27 | struct entry_point_info bl32_ep_info; |
| 28 | struct entry_point_info bl31_ep_info; |
| 29 | }; |
| 30 | |
Michael Walle | afaf6ea | 2020-11-18 17:45:58 +0100 | [diff] [blame] | 31 | struct bl2_to_bl31_params_mem_v2 { |
| 32 | struct bl_params bl_params; |
| 33 | struct bl_params_node bl31_params_node; |
| 34 | struct bl_params_node bl32_params_node; |
| 35 | struct bl_params_node bl33_params_node; |
| 36 | struct atf_image_info bl31_image_info; |
| 37 | struct atf_image_info bl32_image_info; |
| 38 | struct atf_image_info bl33_image_info; |
| 39 | struct entry_point_info bl33_ep_info; |
| 40 | struct entry_point_info bl32_ep_info; |
| 41 | struct entry_point_info bl31_ep_info; |
| 42 | }; |
| 43 | |
Michael Walle | 2def7d9 | 2020-11-18 17:45:56 +0100 | [diff] [blame] | 44 | struct bl31_params *bl2_plat_get_bl31_params_default(uintptr_t bl32_entry, |
| 45 | uintptr_t bl33_entry, |
| 46 | uintptr_t fdt_addr) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 47 | { |
Michael Walle | 203fede | 2020-11-18 17:45:55 +0100 | [diff] [blame] | 48 | static struct bl2_to_bl31_params_mem bl31_params_mem; |
| 49 | struct bl31_params *bl2_to_bl31_params; |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 50 | struct entry_point_info *bl32_ep_info; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 51 | struct entry_point_info *bl33_ep_info; |
| 52 | |
| 53 | /* |
| 54 | * Initialise the memory for all the arguments that needs to |
| 55 | * be passed to BL31 |
| 56 | */ |
| 57 | memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem)); |
| 58 | |
| 59 | /* Assign memory for TF related information */ |
| 60 | bl2_to_bl31_params = &bl31_params_mem.bl31_params; |
| 61 | SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0); |
| 62 | |
| 63 | /* Fill BL31 related information */ |
Frieder Schrempf | b76d8c2 | 2019-06-27 07:03:16 +0000 | [diff] [blame] | 64 | bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 65 | SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, |
| 66 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
| 67 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 68 | /* Fill BL32 related information */ |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 69 | bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info; |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 70 | bl32_ep_info = &bl31_params_mem.bl32_ep_info; |
| 71 | SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1, |
| 72 | ATF_EP_SECURE); |
| 73 | |
| 74 | /* secure payload is optional, so set pc to 0 if absent */ |
| 75 | bl32_ep_info->args.arg3 = fdt_addr; |
| 76 | bl32_ep_info->pc = bl32_entry ? bl32_entry : 0; |
| 77 | bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, |
| 78 | DISABLE_ALL_EXECPTIONS); |
| 79 | |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 80 | bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info; |
| 81 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, |
| 82 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 83 | |
| 84 | /* Fill BL33 related information */ |
| 85 | bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 86 | bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 87 | SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1, |
| 88 | ATF_EP_NON_SECURE); |
| 89 | |
| 90 | /* BL33 expects to receive the primary CPU MPID (through x0) */ |
| 91 | bl33_ep_info->args.arg0 = 0xffff & read_mpidr(); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 92 | bl33_ep_info->pc = bl33_entry; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 93 | bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, |
| 94 | DISABLE_ALL_EXECPTIONS); |
| 95 | |
| 96 | bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info; |
| 97 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, |
| 98 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
| 99 | |
| 100 | return bl2_to_bl31_params; |
| 101 | } |
| 102 | |
Michael Walle | 2def7d9 | 2020-11-18 17:45:56 +0100 | [diff] [blame] | 103 | __weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry, |
| 104 | uintptr_t bl33_entry, |
| 105 | uintptr_t fdt_addr) |
| 106 | { |
| 107 | return bl2_plat_get_bl31_params_default(bl32_entry, bl33_entry, |
| 108 | fdt_addr); |
| 109 | } |
| 110 | |
Michael Walle | afaf6ea | 2020-11-18 17:45:58 +0100 | [diff] [blame] | 111 | struct bl_params *bl2_plat_get_bl31_params_v2_default(uintptr_t bl32_entry, |
| 112 | uintptr_t bl33_entry, |
| 113 | uintptr_t fdt_addr) |
| 114 | { |
| 115 | static struct bl2_to_bl31_params_mem_v2 bl31_params_mem; |
| 116 | struct bl_params *bl_params; |
| 117 | struct bl_params_node *bl_params_node; |
| 118 | |
| 119 | /* |
| 120 | * Initialise the memory for all the arguments that needs to |
| 121 | * be passed to BL31 |
| 122 | */ |
| 123 | memset(&bl31_params_mem, 0, sizeof(bl31_params_mem)); |
| 124 | |
| 125 | /* Assign memory for TF related information */ |
| 126 | bl_params = &bl31_params_mem.bl_params; |
| 127 | SET_PARAM_HEAD(bl_params, ATF_PARAM_BL_PARAMS, ATF_VERSION_2, 0); |
| 128 | bl_params->head = &bl31_params_mem.bl31_params_node; |
| 129 | |
| 130 | /* Fill BL31 related information */ |
| 131 | bl_params_node = &bl31_params_mem.bl31_params_node; |
| 132 | bl_params_node->image_id = ATF_BL31_IMAGE_ID; |
| 133 | bl_params_node->image_info = &bl31_params_mem.bl31_image_info; |
| 134 | bl_params_node->ep_info = &bl31_params_mem.bl31_ep_info; |
| 135 | bl_params_node->next_params_info = &bl31_params_mem.bl32_params_node; |
| 136 | SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY, |
| 137 | ATF_VERSION_2, 0); |
| 138 | |
| 139 | /* Fill BL32 related information */ |
| 140 | bl_params_node = &bl31_params_mem.bl32_params_node; |
| 141 | bl_params_node->image_id = ATF_BL32_IMAGE_ID; |
| 142 | bl_params_node->image_info = &bl31_params_mem.bl32_image_info; |
| 143 | bl_params_node->ep_info = &bl31_params_mem.bl32_ep_info; |
| 144 | bl_params_node->next_params_info = &bl31_params_mem.bl33_params_node; |
| 145 | SET_PARAM_HEAD(bl_params_node->ep_info, ATF_PARAM_EP, |
| 146 | ATF_VERSION_2, ATF_EP_SECURE); |
| 147 | |
| 148 | /* secure payload is optional, so set pc to 0 if absent */ |
| 149 | bl_params_node->ep_info->args.arg3 = fdt_addr; |
| 150 | bl_params_node->ep_info->pc = bl32_entry ? bl32_entry : 0; |
| 151 | bl_params_node->ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, |
| 152 | DISABLE_ALL_EXECPTIONS); |
| 153 | SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY, |
| 154 | ATF_VERSION_2, 0); |
| 155 | |
| 156 | /* Fill BL33 related information */ |
| 157 | bl_params_node = &bl31_params_mem.bl33_params_node; |
| 158 | bl_params_node->image_id = ATF_BL33_IMAGE_ID; |
| 159 | bl_params_node->image_info = &bl31_params_mem.bl33_image_info; |
| 160 | bl_params_node->ep_info = &bl31_params_mem.bl33_ep_info; |
| 161 | bl_params_node->next_params_info = NULL; |
| 162 | SET_PARAM_HEAD(bl_params_node->ep_info, ATF_PARAM_EP, |
| 163 | ATF_VERSION_2, ATF_EP_NON_SECURE); |
| 164 | |
| 165 | /* BL33 expects to receive the primary CPU MPID (through x0) */ |
| 166 | bl_params_node->ep_info->args.arg0 = 0xffff & read_mpidr(); |
| 167 | bl_params_node->ep_info->pc = bl33_entry; |
| 168 | bl_params_node->ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, |
| 169 | DISABLE_ALL_EXECPTIONS); |
| 170 | SET_PARAM_HEAD(bl_params_node->image_info, ATF_PARAM_IMAGE_BINARY, |
| 171 | ATF_VERSION_2, 0); |
| 172 | |
| 173 | return bl_params; |
| 174 | } |
| 175 | |
| 176 | __weak struct bl_params *bl2_plat_get_bl31_params_v2(uintptr_t bl32_entry, |
| 177 | uintptr_t bl33_entry, |
| 178 | uintptr_t fdt_addr) |
| 179 | { |
| 180 | return bl2_plat_get_bl31_params_v2_default(bl32_entry, bl33_entry, |
| 181 | fdt_addr); |
| 182 | } |
| 183 | |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 184 | static inline void raw_write_daif(unsigned int daif) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 185 | { |
Alistair Delva | c89a2b7 | 2022-09-26 20:47:55 +0000 | [diff] [blame] | 186 | __asm__ __volatile__("msr DAIF, %x0\n\t" : : "r" (daif) : "memory"); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Chanho Park | 53a44fc | 2023-09-08 17:08:56 +0900 | [diff] [blame] | 189 | typedef void __noreturn (*atf_entry_t)(struct bl31_params *params, void *plat_params); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 190 | |
Chanho Park | 53a44fc | 2023-09-08 17:08:56 +0900 | [diff] [blame] | 191 | static void __noreturn bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry, |
| 192 | uintptr_t bl33_entry, uintptr_t fdt_addr) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 193 | { |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 194 | atf_entry_t atf_entry = (atf_entry_t)bl31_entry; |
Michael Walle | afaf6ea | 2020-11-18 17:45:58 +0100 | [diff] [blame] | 195 | void *bl31_params; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 196 | |
Michael Walle | afaf6ea | 2020-11-18 17:45:58 +0100 | [diff] [blame] | 197 | if (CONFIG_IS_ENABLED(ATF_LOAD_IMAGE_V2)) |
| 198 | bl31_params = bl2_plat_get_bl31_params_v2(bl32_entry, |
| 199 | bl33_entry, |
| 200 | fdt_addr); |
| 201 | else |
| 202 | bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry, |
| 203 | fdt_addr); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 204 | |
| 205 | raw_write_daif(SPSR_EXCEPTION_MASK); |
Simon Glass | ab2e3c5 | 2024-06-27 09:29:47 +0100 | [diff] [blame] | 206 | if (!CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) |
| 207 | dcache_disable(); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 208 | |
Michael Walle | afaf6ea | 2020-11-18 17:45:58 +0100 | [diff] [blame] | 209 | atf_entry(bl31_params, (void *)fdt_addr); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 212 | static int spl_fit_images_find(void *blob, int os) |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 213 | { |
Michal Simek | db878c0 | 2019-12-19 15:42:13 +0100 | [diff] [blame] | 214 | int parent, node, ndepth = 0; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 215 | const void *data; |
| 216 | |
| 217 | if (!blob) |
| 218 | return -FDT_ERR_BADMAGIC; |
| 219 | |
| 220 | parent = fdt_path_offset(blob, "/fit-images"); |
| 221 | if (parent < 0) |
| 222 | return -FDT_ERR_NOTFOUND; |
| 223 | |
| 224 | for (node = fdt_next_node(blob, parent, &ndepth); |
| 225 | (node >= 0) && (ndepth > 0); |
| 226 | node = fdt_next_node(blob, node, &ndepth)) { |
| 227 | if (ndepth != 1) |
| 228 | continue; |
| 229 | |
| 230 | data = fdt_getprop(blob, node, FIT_OS_PROP, NULL); |
| 231 | if (!data) |
| 232 | continue; |
| 233 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 234 | if (genimg_get_os_id(data) == os) |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 235 | return node; |
| 236 | }; |
| 237 | |
| 238 | return -FDT_ERR_NOTFOUND; |
| 239 | } |
| 240 | |
| 241 | uintptr_t spl_fit_images_get_entry(void *blob, int node) |
| 242 | { |
| 243 | ulong val; |
Michal Simek | 9e64a55 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 244 | int ret; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 245 | |
Michal Simek | 9e64a55 | 2020-09-03 11:24:28 +0200 | [diff] [blame] | 246 | ret = fit_image_get_entry(blob, node, &val); |
| 247 | if (ret) |
| 248 | ret = fit_image_get_load(blob, node, &val); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 249 | |
| 250 | debug("%s: entry point 0x%lx\n", __func__, val); |
| 251 | return val; |
| 252 | } |
| 253 | |
Chanho Park | 53a44fc | 2023-09-08 17:08:56 +0900 | [diff] [blame] | 254 | void __noreturn spl_invoke_atf(struct spl_image_info *spl_image) |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 255 | { |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 256 | uintptr_t bl32_entry = 0; |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 257 | uintptr_t bl33_entry = CONFIG_TEXT_BASE; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 258 | void *blob = spl_image->fdt_addr; |
Philipp Tomsich | c4078af | 2018-01-02 21:16:43 +0100 | [diff] [blame] | 259 | uintptr_t platform_param = (uintptr_t)blob; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 260 | int node; |
| 261 | |
| 262 | /* |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 263 | * Find the OP-TEE binary (in /fit-images) load address or |
| 264 | * entry point (if different) and pass it as the BL3-2 entry |
| 265 | * point, this is optional. |
| 266 | */ |
| 267 | node = spl_fit_images_find(blob, IH_OS_TEE); |
| 268 | if (node >= 0) |
| 269 | bl32_entry = spl_fit_images_get_entry(blob, node); |
| 270 | |
| 271 | /* |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 272 | * Find the U-Boot binary (in /fit-images) load addreess or |
| 273 | * entry point (if different) and pass it as the BL3-3 entry |
| 274 | * point. |
| 275 | * This will need to be extended to support Falcon mode. |
| 276 | */ |
| 277 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 278 | node = spl_fit_images_find(blob, IH_OS_U_BOOT); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 279 | if (node >= 0) |
| 280 | bl33_entry = spl_fit_images_get_entry(blob, node); |
| 281 | |
| 282 | /* |
Philipp Tomsich | c4078af | 2018-01-02 21:16:43 +0100 | [diff] [blame] | 283 | * If ATF_NO_PLATFORM_PARAM is set, we override the platform |
| 284 | * parameter and always pass 0. This is a workaround for |
| 285 | * older ATF versions that have insufficiently robust (or |
| 286 | * overzealous) argument validation. |
| 287 | */ |
| 288 | if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM)) |
| 289 | platform_param = 0; |
| 290 | |
| 291 | /* |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 292 | * We don't provide a BL3-2 entry yet, but this will be possible |
| 293 | * using similar logic. |
| 294 | */ |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 295 | bl31_entry(spl_image->entry_point, bl32_entry, |
| 296 | bl33_entry, platform_param); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 297 | } |