blob: df0a198d550322fe2fb44d0a767e4a9150c24ed2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: BSD-3-Clause
Kever Yangbcc17262017-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 Tomsich1d379092017-09-13 21:29:35 +02009 * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
Kever Yangbcc17262017-05-05 11:47:45 +080010 */
11
12#include <common.h>
13#include <atf_common.h>
Simon Glass9edefc22019-11-14 12:57:37 -070014#include <cpu_func.h>
Kever Yangbcc17262017-05-05 11:47:45 +080015#include <errno.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060016#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Kever Yangbcc17262017-05-05 11:47:45 +080018#include <spl.h>
Simon Glass90526e92020-05-10 11:39:56 -060019#include <asm/cache.h>
Kever Yangbcc17262017-05-05 11:47:45 +080020
Michal Simek5c03c992019-12-19 18:13:31 +010021__weak struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl32_entry,
Joseph Chen47419782019-10-06 20:10:22 +020022 uintptr_t bl33_entry,
23 uintptr_t fdt_addr)
Kever Yangbcc17262017-05-05 11:47:45 +080024{
Michael Walle37c218a2020-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 Chen47419782019-10-06 20:10:22 +020027 struct entry_point_info *bl32_ep_info;
Kever Yangbcc17262017-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 Schrempfa239b822019-06-27 07:03:16 +000041 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yangbcc17262017-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 Chen47419782019-10-06 20:10:22 +020045 /* Fill BL32 related information */
Kever Yangbcc17262017-05-05 11:47:45 +080046 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
Joseph Chen47419782019-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 Yangbcc17262017-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 Yangbcc17262017-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 Tomsich1d379092017-09-13 21:29:35 +020069 bl33_ep_info->pc = bl33_entry;
Kever Yangbcc17262017-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 Tomsich1d379092017-09-13 21:29:35 +020080static inline void raw_write_daif(unsigned int daif)
Kever Yangbcc17262017-05-05 11:47:45 +080081{
82 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
83}
84
Philipp Tomsich1d379092017-09-13 21:29:35 +020085typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
86
Joseph Chen47419782019-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 Yangbcc17262017-05-05 11:47:45 +080089{
90 struct bl31_params *bl31_params;
Philipp Tomsich1d379092017-09-13 21:29:35 +020091 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yangbcc17262017-05-05 11:47:45 +080092
Joseph Chen47419782019-10-06 20:10:22 +020093 bl31_params = bl2_plat_get_bl31_params(bl32_entry, bl33_entry,
94 fdt_addr);
Kever Yangbcc17262017-05-05 11:47:45 +080095
96 raw_write_daif(SPSR_EXCEPTION_MASK);
97 dcache_disable();
98
Philipp Tomsich1d379092017-09-13 21:29:35 +020099 atf_entry((void *)bl31_params, (void *)fdt_addr);
100}
101
Joseph Chen47419782019-10-06 20:10:22 +0200102static int spl_fit_images_find(void *blob, int os)
Philipp Tomsich1d379092017-09-13 21:29:35 +0200103{
Michal Simek935568e2019-12-19 15:42:13 +0100104 int parent, node, ndepth = 0;
Philipp Tomsich1d379092017-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 Chen47419782019-10-06 20:10:22 +0200124 if (genimg_get_os_id(data) == os)
Philipp Tomsich1d379092017-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;
Michal Simekcaa7fc22020-09-03 11:24:28 +0200134 int ret;
Philipp Tomsich1d379092017-09-13 21:29:35 +0200135
Michal Simekcaa7fc22020-09-03 11:24:28 +0200136 ret = fit_image_get_entry(blob, node, &val);
137 if (ret)
138 ret = fit_image_get_load(blob, node, &val);
Philipp Tomsich1d379092017-09-13 21:29:35 +0200139
140 debug("%s: entry point 0x%lx\n", __func__, val);
141 return val;
142}
143
144void spl_invoke_atf(struct spl_image_info *spl_image)
145{
Joseph Chen47419782019-10-06 20:10:22 +0200146 uintptr_t bl32_entry = 0;
Philipp Tomsich1d379092017-09-13 21:29:35 +0200147 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
148 void *blob = spl_image->fdt_addr;
Philipp Tomsichd21fb632018-01-02 21:16:43 +0100149 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich1d379092017-09-13 21:29:35 +0200150 int node;
151
152 /*
Joseph Chen47419782019-10-06 20:10:22 +0200153 * Find the OP-TEE binary (in /fit-images) load address or
154 * entry point (if different) and pass it as the BL3-2 entry
155 * point, this is optional.
156 */
157 node = spl_fit_images_find(blob, IH_OS_TEE);
158 if (node >= 0)
159 bl32_entry = spl_fit_images_get_entry(blob, node);
160
161 /*
Philipp Tomsich1d379092017-09-13 21:29:35 +0200162 * Find the U-Boot binary (in /fit-images) load addreess or
163 * entry point (if different) and pass it as the BL3-3 entry
164 * point.
165 * This will need to be extended to support Falcon mode.
166 */
167
Joseph Chen47419782019-10-06 20:10:22 +0200168 node = spl_fit_images_find(blob, IH_OS_U_BOOT);
Philipp Tomsich1d379092017-09-13 21:29:35 +0200169 if (node >= 0)
170 bl33_entry = spl_fit_images_get_entry(blob, node);
171
172 /*
Philipp Tomsichd21fb632018-01-02 21:16:43 +0100173 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
174 * parameter and always pass 0. This is a workaround for
175 * older ATF versions that have insufficiently robust (or
176 * overzealous) argument validation.
177 */
178 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
179 platform_param = 0;
180
181 /*
Philipp Tomsich1d379092017-09-13 21:29:35 +0200182 * We don't provide a BL3-2 entry yet, but this will be possible
183 * using similar logic.
184 */
Joseph Chen47419782019-10-06 20:10:22 +0200185 bl31_entry(spl_image->entry_point, bl32_entry,
186 bl33_entry, platform_param);
Kever Yangbcc17262017-05-05 11:47:45 +0800187}