blob: 0498d0a2c994391f4121c7d645b3776b6bedfa8f [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>
14#include <errno.h>
15#include <spl.h>
16
17static struct bl2_to_bl31_params_mem bl31_params_mem;
18static struct bl31_params *bl2_to_bl31_params;
19
20/**
21 * bl2_plat_get_bl31_params() - prepare params for bl31.
22 *
23 * This function assigns a pointer to the memory that the platform has kept
24 * aside to pass platform specific and trusted firmware related information
25 * to BL31. This memory is allocated by allocating memory to
26 * bl2_to_bl31_params_mem structure which is a superset of all the
27 * structure whose information is passed to BL31
28 * NOTE: This function should be called only once and should be done
29 * before generating params to BL31
30 *
31 * @return bl31 params structure pointer
32 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +020033static struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
34 uintptr_t bl33_entry,
35 uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080036{
Joseph Chen8dc7abe2019-10-06 20:10:22 +020037 struct entry_point_info *bl32_ep_info;
Kever Yang6e79a912017-05-05 11:47:45 +080038 struct entry_point_info *bl33_ep_info;
39
40 /*
41 * Initialise the memory for all the arguments that needs to
42 * be passed to BL31
43 */
44 memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
45
46 /* Assign memory for TF related information */
47 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
48 SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
49
50 /* Fill BL31 related information */
Frieder Schrempfb76d8c22019-06-27 07:03:16 +000051 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yang6e79a912017-05-05 11:47:45 +080052 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
53 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
54
Joseph Chen8dc7abe2019-10-06 20:10:22 +020055 /* Fill BL32 related information */
Kever Yang6e79a912017-05-05 11:47:45 +080056 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020057 bl32_ep_info = &bl31_params_mem.bl32_ep_info;
58 SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
59 ATF_EP_SECURE);
60
61 /* secure payload is optional, so set pc to 0 if absent */
62 bl32_ep_info->args.arg3 = fdt_addr;
63 bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
64 bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
65 DISABLE_ALL_EXECPTIONS);
66
Kever Yang6e79a912017-05-05 11:47:45 +080067 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
68 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
69 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
Kever Yang6e79a912017-05-05 11:47:45 +080070
71 /* Fill BL33 related information */
72 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
73 bl33_ep_info = &bl31_params_mem.bl33_ep_info;
74 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
75 ATF_EP_NON_SECURE);
76
77 /* BL33 expects to receive the primary CPU MPID (through x0) */
78 bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020079 bl33_ep_info->pc = bl33_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080080 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
81 DISABLE_ALL_EXECPTIONS);
82
83 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
84 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
85 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
86
87 return bl2_to_bl31_params;
88}
89
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020090static inline void raw_write_daif(unsigned int daif)
Kever Yang6e79a912017-05-05 11:47:45 +080091{
92 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
93}
94
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020095typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
96
Joseph Chen8dc7abe2019-10-06 20:10:22 +020097static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
98 uintptr_t bl33_entry, uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080099{
100 struct bl31_params *bl31_params;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200101 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yang6e79a912017-05-05 11:47:45 +0800102
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200103 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
104 fdt_addr);
Kever Yang6e79a912017-05-05 11:47:45 +0800105
106 raw_write_daif(SPSR_EXCEPTION_MASK);
107 dcache_disable();
108
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200109 atf_entry((void *)bl31_params, (void *)fdt_addr);
110}
111
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200112static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200113{
114 int parent, node, ndepth;
115 const void *data;
116
117 if (!blob)
118 return -FDT_ERR_BADMAGIC;
119
120 parent = fdt_path_offset(blob, "/fit-images");
121 if (parent < 0)
122 return -FDT_ERR_NOTFOUND;
123
124 for (node = fdt_next_node(blob, parent, &ndepth);
125 (node >= 0) && (ndepth > 0);
126 node = fdt_next_node(blob, node, &ndepth)) {
127 if (ndepth != 1)
128 continue;
129
130 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
131 if (!data)
132 continue;
133
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200134 if (genimg_get_os_id(data) == os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200135 return node;
136 };
137
138 return -FDT_ERR_NOTFOUND;
139}
140
141uintptr_t spl_fit_images_get_entry(void *blob, int node)
142{
143 ulong val;
144
145 val = fdt_getprop_u32(blob, node, "entry-point");
146 if (val == FDT_ERROR)
147 val = fdt_getprop_u32(blob, node, "load-addr");
148
149 debug("%s: entry point 0x%lx\n", __func__, val);
150 return val;
151}
152
153void spl_invoke_atf(struct spl_image_info *spl_image)
154{
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200155 uintptr_t bl32_entry = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200156 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
157 void *blob = spl_image->fdt_addr;
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100158 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200159 int node;
160
161 /*
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200162 * Find the OP-TEE binary (in /fit-images) load address or
163 * entry point (if different) and pass it as the BL3-2 entry
164 * point, this is optional.
165 */
166 node = spl_fit_images_find(blob, IH_OS_TEE);
167 if (node >= 0)
168 bl32_entry = spl_fit_images_get_entry(blob, node);
169
170 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200171 * Find the U-Boot binary (in /fit-images) load addreess or
172 * entry point (if different) and pass it as the BL3-3 entry
173 * point.
174 * This will need to be extended to support Falcon mode.
175 */
176
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200177 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200178 if (node >= 0)
179 bl33_entry = spl_fit_images_get_entry(blob, node);
180
181 /*
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100182 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
183 * parameter and always pass 0. This is a workaround for
184 * older ATF versions that have insufficiently robust (or
185 * overzealous) argument validation.
186 */
187 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
188 platform_param = 0;
189
190 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200191 * We don't provide a BL3-2 entry yet, but this will be possible
192 * using similar logic.
193 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200194 bl31_entry(spl_image->entry_point, bl32_entry,
195 bl33_entry, platform_param);
Kever Yang6e79a912017-05-05 11:47:45 +0800196}