blob: 702367b2a23c9f6f709f5c41a336c8f3e230a7e4 [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>
16#include <spl.h>
17
18static struct bl2_to_bl31_params_mem bl31_params_mem;
19static struct bl31_params *bl2_to_bl31_params;
20
Michal Simekd1598e22019-12-19 18:13:31 +010021__weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
Joseph Chen8dc7abe2019-10-06 20:10:22 +020022 uintptr_t bl33_entry,
23 uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080024{
Joseph Chen8dc7abe2019-10-06 20:10:22 +020025 struct entry_point_info *bl32_ep_info;
Kever Yang6e79a912017-05-05 11:47:45 +080026 struct entry_point_info *bl33_ep_info;
27
28 /*
29 * Initialise the memory for all the arguments that needs to
30 * be passed to BL31
31 */
32 memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
33
34 /* Assign memory for TF related information */
35 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
36 SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
37
38 /* Fill BL31 related information */
Frieder Schrempfb76d8c22019-06-27 07:03:16 +000039 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yang6e79a912017-05-05 11:47:45 +080040 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
41 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
42
Joseph Chen8dc7abe2019-10-06 20:10:22 +020043 /* Fill BL32 related information */
Kever Yang6e79a912017-05-05 11:47:45 +080044 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
Joseph Chen8dc7abe2019-10-06 20:10:22 +020045 bl32_ep_info = &bl31_params_mem.bl32_ep_info;
46 SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
47 ATF_EP_SECURE);
48
49 /* secure payload is optional, so set pc to 0 if absent */
50 bl32_ep_info->args.arg3 = fdt_addr;
51 bl32_ep_info->pc = bl32_entry ? bl32_entry : 0;
52 bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
53 DISABLE_ALL_EXECPTIONS);
54
Kever Yang6e79a912017-05-05 11:47:45 +080055 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
56 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
57 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
Kever Yang6e79a912017-05-05 11:47:45 +080058
59 /* Fill BL33 related information */
60 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
61 bl33_ep_info = &bl31_params_mem.bl33_ep_info;
62 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
63 ATF_EP_NON_SECURE);
64
65 /* BL33 expects to receive the primary CPU MPID (through x0) */
66 bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020067 bl33_ep_info->pc = bl33_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080068 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
69 DISABLE_ALL_EXECPTIONS);
70
71 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
72 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
73 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
74
75 return bl2_to_bl31_params;
76}
77
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020078static inline void raw_write_daif(unsigned int daif)
Kever Yang6e79a912017-05-05 11:47:45 +080079{
80 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
81}
82
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020083typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
84
Joseph Chen8dc7abe2019-10-06 20:10:22 +020085static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl32_entry,
86 uintptr_t bl33_entry, uintptr_t fdt_addr)
Kever Yang6e79a912017-05-05 11:47:45 +080087{
88 struct bl31_params *bl31_params;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020089 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yang6e79a912017-05-05 11:47:45 +080090
Joseph Chen8dc7abe2019-10-06 20:10:22 +020091 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
92 fdt_addr);
Kever Yang6e79a912017-05-05 11:47:45 +080093
94 raw_write_daif(SPSR_EXCEPTION_MASK);
95 dcache_disable();
96
Philipp Tomsich4ab63e02017-09-13 21:29:35 +020097 atf_entry((void *)bl31_params, (void *)fdt_addr);
98}
99
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200100static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200101{
Michal Simekdb878c02019-12-19 15:42:13 +0100102 int parent, node, ndepth = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200103 const void *data;
104
105 if (!blob)
106 return -FDT_ERR_BADMAGIC;
107
108 parent = fdt_path_offset(blob, "/fit-images");
109 if (parent < 0)
110 return -FDT_ERR_NOTFOUND;
111
112 for (node = fdt_next_node(blob, parent, &ndepth);
113 (node >= 0) && (ndepth > 0);
114 node = fdt_next_node(blob, node, &ndepth)) {
115 if (ndepth != 1)
116 continue;
117
118 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
119 if (!data)
120 continue;
121
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200122 if (genimg_get_os_id(data) == os)
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200123 return node;
124 };
125
126 return -FDT_ERR_NOTFOUND;
127}
128
129uintptr_t spl_fit_images_get_entry(void *blob, int node)
130{
131 ulong val;
132
133 val = fdt_getprop_u32(blob, node, "entry-point");
134 if (val == FDT_ERROR)
135 val = fdt_getprop_u32(blob, node, "load-addr");
136
137 debug("%s: entry point 0x%lx\n", __func__, val);
138 return val;
139}
140
141void spl_invoke_atf(struct spl_image_info *spl_image)
142{
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200143 uintptr_t bl32_entry = 0;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200144 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
145 void *blob = spl_image->fdt_addr;
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100146 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200147 int node;
148
149 /*
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200150 * Find the OP-TEE binary (in /fit-images) load address or
151 * entry point (if different) and pass it as the BL3-2 entry
152 * point, this is optional.
153 */
154 node = spl_fit_images_find(blob, IH_OS_TEE);
155 if (node >= 0)
156 bl32_entry = spl_fit_images_get_entry(blob, node);
157
158 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200159 * Find the U-Boot binary (in /fit-images) load addreess or
160 * entry point (if different) and pass it as the BL3-3 entry
161 * point.
162 * This will need to be extended to support Falcon mode.
163 */
164
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200165 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200166 if (node >= 0)
167 bl33_entry = spl_fit_images_get_entry(blob, node);
168
169 /*
Philipp Tomsichc4078af2018-01-02 21:16:43 +0100170 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
171 * parameter and always pass 0. This is a workaround for
172 * older ATF versions that have insufficiently robust (or
173 * overzealous) argument validation.
174 */
175 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
176 platform_param = 0;
177
178 /*
Philipp Tomsich4ab63e02017-09-13 21:29:35 +0200179 * We don't provide a BL3-2 entry yet, but this will be possible
180 * using similar logic.
181 */
Joseph Chen8dc7abe2019-10-06 20:10:22 +0200182 bl31_entry(spl_image->entry_point, bl32_entry,
183 bl33_entry, platform_param);
Kever Yang6e79a912017-05-05 11:47:45 +0800184}