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 | |
| 12 | #include <common.h> |
| 13 | #include <atf_common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 14 | #include <cpu_func.h> |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | #include <spl.h> |
| 17 | |
| 18 | static struct bl2_to_bl31_params_mem bl31_params_mem; |
| 19 | static struct bl31_params *bl2_to_bl31_params; |
| 20 | |
| 21 | /** |
| 22 | * bl2_plat_get_bl31_params() - prepare params for bl31. |
| 23 | * |
| 24 | * This function assigns a pointer to the memory that the platform has kept |
| 25 | * aside to pass platform specific and trusted firmware related information |
| 26 | * to BL31. This memory is allocated by allocating memory to |
| 27 | * bl2_to_bl31_params_mem structure which is a superset of all the |
| 28 | * structure whose information is passed to BL31 |
| 29 | * NOTE: This function should be called only once and should be done |
| 30 | * before generating params to BL31 |
| 31 | * |
| 32 | * @return bl31 params structure pointer |
| 33 | */ |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 34 | static struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry, |
| 35 | uintptr_t bl33_entry, |
| 36 | uintptr_t fdt_addr) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 37 | { |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 38 | struct entry_point_info *bl32_ep_info; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 39 | struct entry_point_info *bl33_ep_info; |
| 40 | |
| 41 | /* |
| 42 | * Initialise the memory for all the arguments that needs to |
| 43 | * be passed to BL31 |
| 44 | */ |
| 45 | memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem)); |
| 46 | |
| 47 | /* Assign memory for TF related information */ |
| 48 | bl2_to_bl31_params = &bl31_params_mem.bl31_params; |
| 49 | SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0); |
| 50 | |
| 51 | /* Fill BL31 related information */ |
Frieder Schrempf | b76d8c2 | 2019-06-27 07:03:16 +0000 | [diff] [blame] | 52 | 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] | 53 | SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, |
| 54 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
| 55 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 56 | /* Fill BL32 related information */ |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 57 | 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] | 58 | bl32_ep_info = &bl31_params_mem.bl32_ep_info; |
| 59 | SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1, |
| 60 | ATF_EP_SECURE); |
| 61 | |
| 62 | /* secure payload is optional, so set pc to 0 if absent */ |
| 63 | bl32_ep_info->args.arg3 = fdt_addr; |
| 64 | bl32_ep_info->pc = bl32_entry ? bl32_entry : 0; |
| 65 | bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, |
| 66 | DISABLE_ALL_EXECPTIONS); |
| 67 | |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 68 | bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info; |
| 69 | SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info, |
| 70 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 71 | |
| 72 | /* Fill BL33 related information */ |
| 73 | bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 74 | bl33_ep_info = &bl31_params_mem.bl33_ep_info; |
| 75 | SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1, |
| 76 | ATF_EP_NON_SECURE); |
| 77 | |
| 78 | /* BL33 expects to receive the primary CPU MPID (through x0) */ |
| 79 | bl33_ep_info->args.arg0 = 0xffff & read_mpidr(); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 80 | bl33_ep_info->pc = bl33_entry; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 81 | bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, |
| 82 | DISABLE_ALL_EXECPTIONS); |
| 83 | |
| 84 | bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info; |
| 85 | SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, |
| 86 | ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); |
| 87 | |
| 88 | return bl2_to_bl31_params; |
| 89 | } |
| 90 | |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 91 | static inline void raw_write_daif(unsigned int daif) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 92 | { |
| 93 | __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory"); |
| 94 | } |
| 95 | |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 96 | typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params); |
| 97 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 98 | static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry, |
| 99 | uintptr_t bl33_entry, uintptr_t fdt_addr) |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 100 | { |
| 101 | struct bl31_params *bl31_params; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 102 | atf_entry_t atf_entry = (atf_entry_t)bl31_entry; |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 103 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 104 | bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry, |
| 105 | fdt_addr); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 106 | |
| 107 | raw_write_daif(SPSR_EXCEPTION_MASK); |
| 108 | dcache_disable(); |
| 109 | |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 110 | atf_entry((void *)bl31_params, (void *)fdt_addr); |
| 111 | } |
| 112 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 113 | static int spl_fit_images_find(void *blob, int os) |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 114 | { |
| 115 | int parent, node, ndepth; |
| 116 | const void *data; |
| 117 | |
| 118 | if (!blob) |
| 119 | return -FDT_ERR_BADMAGIC; |
| 120 | |
| 121 | parent = fdt_path_offset(blob, "/fit-images"); |
| 122 | if (parent < 0) |
| 123 | return -FDT_ERR_NOTFOUND; |
| 124 | |
| 125 | for (node = fdt_next_node(blob, parent, &ndepth); |
| 126 | (node >= 0) && (ndepth > 0); |
| 127 | node = fdt_next_node(blob, node, &ndepth)) { |
| 128 | if (ndepth != 1) |
| 129 | continue; |
| 130 | |
| 131 | data = fdt_getprop(blob, node, FIT_OS_PROP, NULL); |
| 132 | if (!data) |
| 133 | continue; |
| 134 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 135 | if (genimg_get_os_id(data) == os) |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 136 | return node; |
| 137 | }; |
| 138 | |
| 139 | return -FDT_ERR_NOTFOUND; |
| 140 | } |
| 141 | |
| 142 | uintptr_t spl_fit_images_get_entry(void *blob, int node) |
| 143 | { |
| 144 | ulong val; |
| 145 | |
| 146 | val = fdt_getprop_u32(blob, node, "entry-point"); |
| 147 | if (val == FDT_ERROR) |
| 148 | val = fdt_getprop_u32(blob, node, "load-addr"); |
| 149 | |
| 150 | debug("%s: entry point 0x%lx\n", __func__, val); |
| 151 | return val; |
| 152 | } |
| 153 | |
| 154 | void spl_invoke_atf(struct spl_image_info *spl_image) |
| 155 | { |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 156 | uintptr_t bl32_entry = 0; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 157 | uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE; |
| 158 | void *blob = spl_image->fdt_addr; |
Philipp Tomsich | c4078af | 2018-01-02 21:16:43 +0100 | [diff] [blame] | 159 | uintptr_t platform_param = (uintptr_t)blob; |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 160 | int node; |
| 161 | |
| 162 | /* |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 163 | * Find the OP-TEE binary (in /fit-images) load address or |
| 164 | * entry point (if different) and pass it as the BL3-2 entry |
| 165 | * point, this is optional. |
| 166 | */ |
| 167 | node = spl_fit_images_find(blob, IH_OS_TEE); |
| 168 | if (node >= 0) |
| 169 | bl32_entry = spl_fit_images_get_entry(blob, node); |
| 170 | |
| 171 | /* |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 172 | * Find the U-Boot binary (in /fit-images) load addreess or |
| 173 | * entry point (if different) and pass it as the BL3-3 entry |
| 174 | * point. |
| 175 | * This will need to be extended to support Falcon mode. |
| 176 | */ |
| 177 | |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 178 | node = spl_fit_images_find(blob, IH_OS_U_BOOT); |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 179 | if (node >= 0) |
| 180 | bl33_entry = spl_fit_images_get_entry(blob, node); |
| 181 | |
| 182 | /* |
Philipp Tomsich | c4078af | 2018-01-02 21:16:43 +0100 | [diff] [blame] | 183 | * If ATF_NO_PLATFORM_PARAM is set, we override the platform |
| 184 | * parameter and always pass 0. This is a workaround for |
| 185 | * older ATF versions that have insufficiently robust (or |
| 186 | * overzealous) argument validation. |
| 187 | */ |
| 188 | if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM)) |
| 189 | platform_param = 0; |
| 190 | |
| 191 | /* |
Philipp Tomsich | 4ab63e0 | 2017-09-13 21:29:35 +0200 | [diff] [blame] | 192 | * We don't provide a BL3-2 entry yet, but this will be possible |
| 193 | * using similar logic. |
| 194 | */ |
Joseph Chen | 8dc7abe | 2019-10-06 20:10:22 +0200 | [diff] [blame] | 195 | bl31_entry(spl_image->entry_point, bl32_entry, |
| 196 | bl33_entry, platform_param); |
Kever Yang | 6e79a91 | 2017-05-05 11:47:45 +0800 | [diff] [blame] | 197 | } |