xref: /OK3568_Linux_fs/u-boot/common/spl/spl_atf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Reference to the ARM TF Project,
3  * plat/arm/common/arm_bl2_setup.c
4  * Portions copyright (c) 2013-2016, ARM Limited and Contributors. All rights
5  * reserved.
6  * Copyright (C) 2016 Rockchip Electronic Co.,Ltd
7  * Written by Kever Yang <kever.yang@rock-chips.com>
8  * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH
9  *
10  * SPDX-License-Identifier:     BSD-3-Clause
11  */
12 
13 #include <common.h>
14 #include <atf_common.h>
15 #include <errno.h>
16 #include <spl.h>
17 
18 static struct bl2_to_bl31_params_mem bl31_params_mem;
19 static struct bl31_params *bl2_to_bl31_params;
20 
21 /**
22  * bl2_plat_get_bl31_params() - prepare params for bl31.
23  *
24  * This function assigns a pointer to the memory that the platform has kept
25  * aside to pass platform specific and trusted firmware related information
26  * to BL31. This memory is allocated by allocating memory to
27  * bl2_to_bl31_params_mem structure which is a superset of all the
28  * structure whose information is passed to BL31
29  * NOTE: This function should be called only once and should be done
30  * before generating params to BL31
31  *
32  * @return bl31 params structure pointer
33  */
bl2_plat_get_bl31_params(struct spl_image_info * spl_image,uintptr_t bl32_entry,uintptr_t bl33_entry)34 static struct bl31_params *bl2_plat_get_bl31_params(struct spl_image_info *spl_image,
35 						    uintptr_t bl32_entry,
36 						    uintptr_t bl33_entry)
37 {
38 	struct entry_point_info *bl32_ep_info;
39 	struct entry_point_info *bl33_ep_info;
40 
41 	/*
42 	 * Initialise the memory for all the arguments that needs to
43 	 * be passed to BL31
44 	 */
45 	memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
46 
47 	/* Assign memory for TF related information */
48 	bl2_to_bl31_params = &bl31_params_mem.bl31_params;
49 	SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
50 
51 	/* Fill BL31 related information */
52 	SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
53 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
54 
55 	if (bl32_entry == -1)
56 		goto bl33_setup;
57 
58 	/* Fill BL32 related information */
59 	bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
60 	bl32_ep_info = &bl31_params_mem.bl32_ep_info;
61 	SET_PARAM_HEAD(bl32_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
62 		       ATF_EP_SECURE);
63 
64 	bl32_ep_info->pc = bl32_entry;
65 	bl32_ep_info->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
66 				     DISABLE_ALL_EXECPTIONS);
67 
68 	bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
69 	SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
70 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
71 
72 bl33_setup:
73 	/* Fill BL33 related information */
74 	bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
75 	bl33_ep_info = &bl31_params_mem.bl33_ep_info;
76 	SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
77 		       ATF_EP_NON_SECURE);
78 
79 	/* BL33 expects to receive the primary CPU MPID (through x0) */
80 	bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
81 	bl33_ep_info->pc = bl33_entry;
82 
83 #ifdef CONFIG_SPL_ATF_AARCH32_BL33
84 	bl33_ep_info->spsr = SPSR_32(MODE32_svc, SPSR_T_ARM, EP_EE_LITTLE,
85 				     DISABLE_ALL_EXECPTIONS_32);
86 #else
87 	bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
88 				     DISABLE_ALL_EXECPTIONS);
89 #endif
90 	/*
91 	 * Reference: arch/arm/lib/bootm.c
92 	 * boot_jump_linux(bootm_headers_t *images, int flag)
93 	 * {
94 	 * 	......
95 	 * 	armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
96 	 * 			   images->ep, ES_TO_AARCH64);
97 	 * }
98 	 */
99 	if (spl_image->next_stage == SPL_NEXT_STAGE_KERNEL)
100 		bl33_ep_info->args.arg0 = (unsigned long)spl_image->fdt_addr;
101 
102 	bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
103 	SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
104 		       ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
105 
106 	return bl2_to_bl31_params;
107 }
108 
raw_write_daif(unsigned int daif)109 static inline void raw_write_daif(unsigned int daif)
110 {
111 	__asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
112 }
113 
114 typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
115 
bl31_entry(struct spl_image_info * spl_image,uintptr_t bl31_entry,uintptr_t bl32_entry,uintptr_t bl33_entry,uintptr_t fdt_addr)116 void bl31_entry(struct spl_image_info *spl_image,
117 		uintptr_t bl31_entry, uintptr_t bl32_entry,
118 		uintptr_t bl33_entry, uintptr_t fdt_addr)
119 {
120 	struct bl31_params *bl31_params;
121 	atf_entry_t  atf_entry = (atf_entry_t)bl31_entry;
122 
123 	bl31_params = bl2_plat_get_bl31_params(spl_image, bl32_entry, bl33_entry);
124 
125 	atf_entry((void *)bl31_params, (void *)fdt_addr);
126 }
127 
spl_fit_images_find(void * blob,int os)128 static int spl_fit_images_find(void *blob, int os)
129 {
130 	int parent, node;
131 	const void *data;
132 
133 	if (!blob)
134 		return -FDT_ERR_BADMAGIC;
135 
136 	parent = fdt_path_offset(blob, "/fit-images");
137 	if (parent < 0)
138 		return -FDT_ERR_NOTFOUND;
139 
140 	fdt_for_each_subnode(node, blob, parent) {
141 		data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
142 		if (!data)
143 			continue;
144 
145 		if (genimg_get_os_id(data) == os)
146 			return node;
147 	}
148 
149 	return -FDT_ERR_NOTFOUND;
150 }
151 
spl_fit_images_get_entry(void * blob,int node)152 uintptr_t spl_fit_images_get_entry(void *blob, int node)
153 {
154 	ulong  val;
155 
156 	val = fdt_getprop_u32(blob, node, "entry-point");
157 	if (val == FDT_ERROR)
158 		val = fdt_getprop_u32(blob, node, "load-addr");
159 
160 	debug("%s: entry point 0x%lx\n", __func__, val);
161 	return val;
162 }
163 
spl_invoke_atf(struct spl_image_info * spl_image)164 void spl_invoke_atf(struct spl_image_info *spl_image)
165 {
166 	uintptr_t bl32_entry, bl33_entry;
167 	void *blob = spl_image->fdt_addr;
168 	uintptr_t platform_param = (uintptr_t)blob;
169 	int node;
170 
171 	/*
172 	 * Find the OP-TEE binary (in /fit-images) load address or
173 	 * entry point (if different) and pass it as the BL3-2 entry
174 	 * point, this is optional.
175 	 * This will need to be extended to support Falcon mode.
176 	 */
177 	node = spl_fit_images_find(blob, IH_OS_OP_TEE);
178 	if (node >= 0)
179 		bl32_entry = spl_fit_images_get_entry(blob, node);
180 	else
181 		bl32_entry = spl_image->entry_point_bl32; /* optional */
182 
183 	/*
184 	 * Find the U-Boot binary (in /fit-images) load addreess or
185 	 * entry point (if different) and pass it as the BL3-3 entry
186 	 * point.
187 	 * This will need to be extended to support Falcon mode.
188 	 */
189 	node = spl_fit_images_find(blob, IH_OS_U_BOOT);
190 	if (node >= 0)
191 		bl33_entry = spl_fit_images_get_entry(blob, node);
192 	else
193 		bl33_entry = spl_image->entry_point_bl33;
194 
195 	/*
196 	 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
197 	 * parameter and always pass 0.  This is a workaround for
198 	 * older ATF versions that have insufficiently robust (or
199 	 * overzealous) argument validation.
200 	 */
201 	if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
202 		platform_param = 0;
203 
204 	/* do cleanup */
205 	spl_cleanup_before_jump(spl_image);
206 
207 	/*
208 	 * We don't provide a BL3-2 entry yet, but this will be possible
209 	 * using similar logic.
210 	 */
211 	bl31_entry(spl_image, spl_image->entry_point, bl32_entry,
212 		   bl33_entry, platform_param);
213 }
214