blob: 63af6a62072a1c97825f13612bb4c60bba9ccbcd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Kever Yang6e79a912017-05-05 11:47:45 +08002/*
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 Tomsich4ab63e02017-09-13 21:29:35 +02009 * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
Kever Yang6e79a912017-05-05 11:47:45 +080010 */
11
12#include <common.h>
13#include <atf_common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070014#include <cpu_func.h>
Kever Yang6e79a912017-05-05 11:47:45 +080015#include <errno.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060016#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Kever Yang6e79a912017-05-05 11:47:45 +080018#include <spl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Kever Yang6e79a912017-05-05 11:47:45 +080020
Michael Walle2def7d92020-11-18 17:45:56 +010021struct bl31_params *bl2_plat_get_bl31_params_default(uintptr_t bl32_entry,
22 uintptr_t bl33_entry,
23 uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080024{
Michael Walle203fede2020-11-18 17:45:55 +010025 static struct bl2_to_bl31_params_mem bl31_params_mem;
26 struct bl31_params *bl2_to_bl31_params;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020027 struct entry_point_info *bl32_ep_info;
Kever Yang6e79a912017-05-05 11:47:45 +080028 struct entry_point_info *bl33_ep_info;
29
30 /*
31 * Initialise the memory for all the arguments that needs to
32 * be passed to BL31
33 */
34 memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
35
36 /* Assign memory for TF related information */
37 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
38 SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
39
40 /* Fill BL31 related information */
Frieder Schrempfb76d8c22019-06-27 07:03:16 +000041 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yang6e79a912017-05-05 11:47:45 +080042 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
43 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
44
Joseph Chen8dc7abe2019-10-06 20:10:22 +020045 /* Fill BL32 related information */
Kever Yang6e79a912017-05-05 11:47:45 +080046 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020047 bl32_ep_info = &bl31_params_mem.bl32_ep_info;
48 SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
49 ATF_EP_SECURE);
50
51 /* secure payload is optional, so set pc to 0 if absent */
52 bl32_ep_info->args.arg3 = fdt_addr;
53 bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
54 bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
55 DISABLE_ALL_EXECPTIONS);
56
Kever Yang6e79a912017-05-05 11:47:45 +080057 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
58 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
59 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
Kever Yang6e79a912017-05-05 11:47:45 +080060
61 /* Fill BL33 related information */
62 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
63 bl33_ep_info = &bl31_params_mem.bl33_ep_info;
64 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
65 ATF_EP_NON_SECURE);
66
67 /* BL33 expects to receive the primary CPU MPID (through x0) */
68 bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020069 bl33_ep_info->pc = bl33_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080070 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
71 DISABLE_ALL_EXECPTIONS);
72
73 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
74 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
75 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
76
77 return bl2_to_bl31_params;
78}
79
Michael Walle2def7d92020-11-18 17:45:56 +010080__weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
81 uintptr_t bl33_entry,
82 uintptr_t fdt_addr)
83{
84 return bl2_plat_get_bl31_params_default(bl32_entry, bl33_entry,
85 fdt_addr);
86}
87
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020088static inline void raw_write_daif(unsigned int daif)
Kever Yang6e79a912017-05-05 11:47:45 +080089{
90 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
91}
92
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020093typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
94
Joseph Chen8dc7abe2019-10-06 20:10:22 +020095static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
96 uintptr_t bl33_entry, uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080097{
98 struct bl31_params *bl31_params;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020099 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yang6e79a912017-05-05 11:47:45 +0800100
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200101 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
102 fdt_addr);
Kever Yang6e79a912017-05-05 11:47:45 +0800103
104 raw_write_daif(SPSR_EXCEPTION_MASK);
105 dcache_disable();
106
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200107 atf_entry((void *)bl31_params, (void *)fdt_addr);
108}
109
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200110static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200111{
Michal Simekdb878c02019-12-19 15:42:13 +0100112 int parent, node, ndepth = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200113 const void *data;
114
115 if (!blob)
116 return -FDT_ERR_BADMAGIC;
117
118 parent = fdt_path_offset(blob, "/fit-images");
119 if (parent < 0)
120 return -FDT_ERR_NOTFOUND;
121
122 for (node = fdt_next_node(blob, parent, &ndepth);
123 (node >= 0) && (ndepth > 0);
124 node = fdt_next_node(blob, node, &ndepth)) {
125 if (ndepth != 1)
126 continue;
127
128 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
129 if (!data)
130 continue;
131
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200132 if (genimg_get_os_id(data) == os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200133 return node;
134 };
135
136 return -FDT_ERR_NOTFOUND;
137}
138
139uintptr_t spl_fit_images_get_entry(void *blob, int node)
140{
141 ulong val;
Michal Simek9e64a552020-09-03 11:24:28 +0200142 int ret;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200143
Michal Simek9e64a552020-09-03 11:24:28 +0200144 ret = fit_image_get_entry(blob, node, &val);
145 if (ret)
146 ret = fit_image_get_load(blob, node, &val);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200147
148 debug("%s: entry point 0x%lx\n", __func__, val);
149 return val;
150}
151
152void spl_invoke_atf(struct spl_image_info *spl_image)
153{
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200154 uintptr_t bl32_entry = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200155 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
156 void *blob = spl_image->fdt_addr;
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100157 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200158 int node;
159
160 /*
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200161 * Find the OP-TEE binary (in /fit-images) load address or
162 * entry point (if different) and pass it as the BL3-2 entry
163 * point, this is optional.
164 */
165 node = spl_fit_images_find(blob, IH_OS_TEE);
166 if (node >= 0)
167 bl32_entry = spl_fit_images_get_entry(blob, node);
168
169 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200170 * Find the U-Boot binary (in /fit-images) load addreess or
171 * entry point (if different) and pass it as the BL3-3 entry
172 * point.
173 * This will need to be extended to support Falcon mode.
174 */
175
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200176 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200177 if (node >= 0)
178 bl33_entry = spl_fit_images_get_entry(blob, node);
179
180 /*
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100181 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
182 * parameter and always pass 0. This is a workaround for
183 * older ATF versions that have insufficiently robust (or
184 * overzealous) argument validation.
185 */
186 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
187 platform_param = 0;
188
189 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200190 * We don't provide a BL3-2 entry yet, but this will be possible
191 * using similar logic.
192 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200193 bl31_entry(spl_image->entry_point, bl32_entry,
194 bl33_entry, platform_param);
Kever Yang6e79a912017-05-05 11:47:45 +0800195}