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