blob: b83e3bb54aeaaad332861c3c041b4e4ad0dbfefc [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>
Kever Yang6e79a912017-05-05 11:47:45 +080017#include <spl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <asm/cache.h>
Kever Yang6e79a912017-05-05 11:47:45 +080019
20static struct bl2_to_bl31_params_mem bl31_params_mem;
21static struct bl31_params *bl2_to_bl31_params;
22
Michal Simekd1598e22019-12-19 18:13:31 +010023__weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
Joseph Chen8dc7abe2019-10-06 20:10:22 +020024 uintptr_t bl33_entry,
25 uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080026{
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
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020080static inline void raw_write_daif(unsigned int daif)
Kever Yang6e79a912017-05-05 11:47:45 +080081{
82 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
83}
84
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020085typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
86
Joseph Chen8dc7abe2019-10-06 20:10:22 +020087static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
88 uintptr_t bl33_entry, uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080089{
90 struct bl31_params *bl31_params;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020091 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080092
Joseph Chen8dc7abe2019-10-06 20:10:22 +020093 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
94 fdt_addr);
Kever Yang6e79a912017-05-05 11:47:45 +080095
96 raw_write_daif(SPSR_EXCEPTION_MASK);
97 dcache_disable();
98
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020099 atf_entry((void *)bl31_params, (void *)fdt_addr);
100}
101
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200102static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200103{
Michal Simekdb878c02019-12-19 15:42:13 +0100104 int parent, node, ndepth = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200105 const void *data;
106
107 if (!blob)
108 return -FDT_ERR_BADMAGIC;
109
110 parent = fdt_path_offset(blob, "/fit-images");
111 if (parent < 0)
112 return -FDT_ERR_NOTFOUND;
113
114 for (node = fdt_next_node(blob, parent, &ndepth);
115 (node >= 0) && (ndepth > 0);
116 node = fdt_next_node(blob, node, &ndepth)) {
117 if (ndepth != 1)
118 continue;
119
120 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
121 if (!data)
122 continue;
123
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200124 if (genimg_get_os_id(data) == os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200125 return node;
126 };
127
128 return -FDT_ERR_NOTFOUND;
129}
130
131uintptr_t spl_fit_images_get_entry(void *blob, int node)
132{
133 ulong val;
134
135 val = fdt_getprop_u32(blob, node, "entry-point");
136 if (val == FDT_ERROR)
137 val = fdt_getprop_u32(blob, node, "load-addr");
138
139 debug("%s: entry point 0x%lx\n", __func__, val);
140 return val;
141}
142
143void spl_invoke_atf(struct spl_image_info *spl_image)
144{
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200145 uintptr_t bl32_entry = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200146 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
147 void *blob = spl_image->fdt_addr;
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100148 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200149 int node;
150
151 /*
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200152 * Find the OP-TEE binary (in /fit-images) load address or
153 * entry point (if different) and pass it as the BL3-2 entry
154 * point, this is optional.
155 */
156 node = spl_fit_images_find(blob, IH_OS_TEE);
157 if (node >= 0)
158 bl32_entry = spl_fit_images_get_entry(blob, node);
159
160 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200161 * Find the U-Boot binary (in /fit-images) load addreess or
162 * entry point (if different) and pass it as the BL3-3 entry
163 * point.
164 * This will need to be extended to support Falcon mode.
165 */
166
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200167 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200168 if (node >= 0)
169 bl33_entry = spl_fit_images_get_entry(blob, node);
170
171 /*
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100172 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
173 * parameter and always pass 0. This is a workaround for
174 * older ATF versions that have insufficiently robust (or
175 * overzealous) argument validation.
176 */
177 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
178 platform_param = 0;
179
180 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200181 * We don't provide a BL3-2 entry yet, but this will be possible
182 * using similar logic.
183 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200184 bl31_entry(spl_image->entry_point, bl32_entry,
185 bl33_entry, platform_param);
Kever Yang6e79a912017-05-05 11:47:45 +0800186}