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