1 /* 2 * Reference to the ARM TF Project, 3 * plat/arm/common/arm_bl2_setup.c 4 * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights 5 * reserved. 6 * Copyright (C) 2016 Rockchip Electronic Co.,Ltd 7 * Written by Kever Yang <kever.yang@rock-chips.com> 8 * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH 9 * 10 * SPDX-License-Identifier: BSD-3-Clause 11 */ 12 13 #include <common.h> 14 #include <atf_common.h> 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 */ 34 static struct bl31_params *bl2_plat_get_bl31_params(struct spl_image_info *spl_image, 35 uintptr_t bl32_entry, 36 uintptr_t bl33_entry) 37 { 38 struct entry_point_info *bl32_ep_info; 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 */ 52 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info, 53 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); 54 55 if (bl32_entry == -1) 56 goto bl33_setup; 57 58 /* Fill BL32 related information */ 59 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info; 60 bl32_ep_info = &bl31_params_mem.bl32_ep_info; 61 SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1, 62 ATF_EP_SECURE); 63 64 bl32_ep_info->pc = bl32_entry; 65 bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, 66 DISABLE_ALL_EXECPTIONS); 67 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); 71 72 bl33_setup: 73 /* Fill BL33 related information */ 74 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info; 75 bl33_ep_info = &bl31_params_mem.bl33_ep_info; 76 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1, 77 ATF_EP_NON_SECURE); 78 79 /* BL33 expects to receive the primary CPU MPID (through x0) */ 80 bl33_ep_info->args.arg0 = 0xffff & read_mpidr(); 81 bl33_ep_info->pc = bl33_entry; 82 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, 83 DISABLE_ALL_EXECPTIONS); 84 85 /* 86 * Reference: arch/arm/lib/bootm.c 87 * boot_jump_linux(bootm_headers_t *images, int flag) 88 * { 89 * ...... 90 * armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0, 91 * images->ep, ES_TO_AARCH64); 92 * } 93 */ 94 if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL) 95 bl33_ep_info->args.arg0 = (unsigned long)spl_image->fdt_addr; 96 97 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info; 98 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info, 99 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0); 100 101 return bl2_to_bl31_params; 102 } 103 104 static inline void raw_write_daif(unsigned int daif) 105 { 106 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory"); 107 } 108 109 typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params); 110 111 void bl31_entry(struct spl_image_info *spl_image, 112 uintptr_t bl31_entry, uintptr_t bl32_entry, 113 uintptr_t bl33_entry, uintptr_t fdt_addr) 114 { 115 struct bl31_params *bl31_params; 116 atf_entry_t atf_entry = (atf_entry_t)bl31_entry; 117 118 bl31_params = bl2_plat_get_bl31_params(spl_image, bl32_entry, bl33_entry); 119 120 atf_entry((void *)bl31_params, (void *)fdt_addr); 121 } 122 123 static int spl_fit_images_find(void *blob, int os) 124 { 125 int parent, node; 126 const void *data; 127 128 if (!blob) 129 return -FDT_ERR_BADMAGIC; 130 131 parent = fdt_path_offset(blob, "/fit-images"); 132 if (parent < 0) 133 return -FDT_ERR_NOTFOUND; 134 135 fdt_for_each_subnode(node, blob, parent) { 136 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL); 137 if (!data) 138 continue; 139 140 if (genimg_get_os_id(data) == os) 141 return node; 142 } 143 144 return -FDT_ERR_NOTFOUND; 145 } 146 147 uintptr_t spl_fit_images_get_entry(void *blob, int node) 148 { 149 ulong val; 150 151 val = fdt_getprop_u32(blob, node, "entry-point"); 152 if (val == FDT_ERROR) 153 val = fdt_getprop_u32(blob, node, "load-addr"); 154 155 debug("%s: entry point 0x%lx\n", __func__, val); 156 return val; 157 } 158 159 void spl_invoke_atf(struct spl_image_info *spl_image) 160 { 161 uintptr_t bl32_entry, bl33_entry; 162 void *blob = spl_image->fdt_addr; 163 uintptr_t platform_param = (uintptr_t)blob; 164 int node; 165 166 /* 167 * Find the OP-TEE binary (in /fit-images) load address or 168 * entry point (if different) and pass it as the BL3-2 entry 169 * point, this is optional. 170 * This will need to be extended to support Falcon mode. 171 */ 172 node = spl_fit_images_find(blob, IH_OS_OP_TEE); 173 if (node >= 0) 174 bl32_entry = spl_fit_images_get_entry(blob, node); 175 else 176 bl32_entry = spl_image->entry_point_bl32; /* optional */ 177 178 /* 179 * Find the U-Boot binary (in /fit-images) load addreess or 180 * entry point (if different) and pass it as the BL3-3 entry 181 * point. 182 * This will need to be extended to support Falcon mode. 183 */ 184 node = spl_fit_images_find(blob, IH_OS_U_BOOT); 185 if (node >= 0) 186 bl33_entry = spl_fit_images_get_entry(blob, node); 187 else 188 bl33_entry = spl_image->entry_point_bl33; 189 190 /* 191 * If ATF_NO_PLATFORM_PARAM is set, we override the platform 192 * parameter and always pass 0. This is a workaround for 193 * older ATF versions that have insufficiently robust (or 194 * overzealous) argument validation. 195 */ 196 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM)) 197 platform_param = 0; 198 199 /* do cleanup */ 200 spl_cleanup_before_jump(spl_image); 201 202 /* 203 * We don't provide a BL3-2 entry yet, but this will be possible 204 * using similar logic. 205 */ 206 bl31_entry(spl_image, spl_image->entry_point, bl32_entry, 207 bl33_entry, platform_param); 208 } 209