blob: 4715f9d3719faced434616c5bd651563cf6be2f9 [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>
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 */
Philipp Tomsich1d379092017-09-13 21:29:35 +020033static struct bl31_params *bl2_plat_get_bl31_params(uintptr_t bl33_entry)
Kever Yangbcc17262017-05-05 11:47:45 +080034{
35 struct entry_point_info *bl33_ep_info;
36
37 /*
38 * Initialise the memory for all the arguments that needs to
39 * be passed to BL31
40 */
41 memset(&bl31_params_mem, 0, sizeof(struct bl2_to_bl31_params_mem));
42
43 /* Assign memory for TF related information */
44 bl2_to_bl31_params = &bl31_params_mem.bl31_params;
45 SET_PARAM_HEAD(bl2_to_bl31_params, ATF_PARAM_BL31, ATF_VERSION_1, 0);
46
47 /* Fill BL31 related information */
Frieder Schrempfa239b822019-06-27 07:03:16 +000048 bl2_to_bl31_params->bl31_image_info = &bl31_params_mem.bl31_image_info;
Kever Yangbcc17262017-05-05 11:47:45 +080049 SET_PARAM_HEAD(bl2_to_bl31_params->bl31_image_info,
50 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
51
52 /* Fill BL32 related information if it exists */
Kever Yangbcc17262017-05-05 11:47:45 +080053 bl2_to_bl31_params->bl32_ep_info = &bl31_params_mem.bl32_ep_info;
54 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_ep_info, ATF_PARAM_EP,
55 ATF_VERSION_1, 0);
56 bl2_to_bl31_params->bl32_image_info = &bl31_params_mem.bl32_image_info;
57 SET_PARAM_HEAD(bl2_to_bl31_params->bl32_image_info,
58 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
Kever Yang0289e292018-01-17 11:05:38 +080059#ifndef BL32_BASE
60 bl2_to_bl31_params->bl32_ep_info->pc = 0;
Kever Yangbcc17262017-05-05 11:47:45 +080061#endif /* BL32_BASE */
62
63 /* Fill BL33 related information */
64 bl2_to_bl31_params->bl33_ep_info = &bl31_params_mem.bl33_ep_info;
65 bl33_ep_info = &bl31_params_mem.bl33_ep_info;
66 SET_PARAM_HEAD(bl33_ep_info, ATF_PARAM_EP, ATF_VERSION_1,
67 ATF_EP_NON_SECURE);
68
69 /* BL33 expects to receive the primary CPU MPID (through x0) */
70 bl33_ep_info->args.arg0 = 0xffff & read_mpidr();
Philipp Tomsich1d379092017-09-13 21:29:35 +020071 bl33_ep_info->pc = bl33_entry;
Kever Yangbcc17262017-05-05 11:47:45 +080072 bl33_ep_info->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
73 DISABLE_ALL_EXECPTIONS);
74
75 bl2_to_bl31_params->bl33_image_info = &bl31_params_mem.bl33_image_info;
76 SET_PARAM_HEAD(bl2_to_bl31_params->bl33_image_info,
77 ATF_PARAM_IMAGE_BINARY, ATF_VERSION_1, 0);
78
79 return bl2_to_bl31_params;
80}
81
Philipp Tomsich1d379092017-09-13 21:29:35 +020082static inline void raw_write_daif(unsigned int daif)
Kever Yangbcc17262017-05-05 11:47:45 +080083{
84 __asm__ __volatile__("msr DAIF, %0\n\t" : : "r" (daif) : "memory");
85}
86
Philipp Tomsich1d379092017-09-13 21:29:35 +020087typedef void (*atf_entry_t)(struct bl31_params *params, void *plat_params);
88
89static void bl31_entry(uintptr_t bl31_entry, uintptr_t bl33_entry,
90 uintptr_t fdt_addr)
Kever Yangbcc17262017-05-05 11:47:45 +080091{
92 struct bl31_params *bl31_params;
Philipp Tomsich1d379092017-09-13 21:29:35 +020093 atf_entry_t atf_entry = (atf_entry_t)bl31_entry;
Kever Yangbcc17262017-05-05 11:47:45 +080094
Philipp Tomsich1d379092017-09-13 21:29:35 +020095 bl31_params = bl2_plat_get_bl31_params(bl33_entry);
Kever Yangbcc17262017-05-05 11:47:45 +080096
97 raw_write_daif(SPSR_EXCEPTION_MASK);
98 dcache_disable();
99
Philipp Tomsich1d379092017-09-13 21:29:35 +0200100 atf_entry((void *)bl31_params, (void *)fdt_addr);
101}
102
103static int spl_fit_images_find_uboot(void *blob)
104{
105 int parent, node, ndepth;
106 const void *data;
107
108 if (!blob)
109 return -FDT_ERR_BADMAGIC;
110
111 parent = fdt_path_offset(blob, "/fit-images");
112 if (parent < 0)
113 return -FDT_ERR_NOTFOUND;
114
115 for (node = fdt_next_node(blob, parent, &ndepth);
116 (node >= 0) && (ndepth > 0);
117 node = fdt_next_node(blob, node, &ndepth)) {
118 if (ndepth != 1)
119 continue;
120
121 data = fdt_getprop(blob, node, FIT_OS_PROP, NULL);
122 if (!data)
123 continue;
124
125 if (genimg_get_os_id(data) == IH_OS_U_BOOT)
126 return node;
127 };
128
129 return -FDT_ERR_NOTFOUND;
130}
131
132uintptr_t spl_fit_images_get_entry(void *blob, int node)
133{
134 ulong val;
135
136 val = fdt_getprop_u32(blob, node, "entry-point");
137 if (val == FDT_ERROR)
138 val = fdt_getprop_u32(blob, node, "load-addr");
139
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{
146 uintptr_t bl33_entry = CONFIG_SYS_TEXT_BASE;
147 void *blob = spl_image->fdt_addr;
Philipp Tomsichd21fb632018-01-02 21:16:43 +0100148 uintptr_t platform_param = (uintptr_t)blob;
Philipp Tomsich1d379092017-09-13 21:29:35 +0200149 int node;
150
151 /*
152 * Find the U-Boot binary (in /fit-images) load addreess or
153 * entry point (if different) and pass it as the BL3-3 entry
154 * point.
155 * This will need to be extended to support Falcon mode.
156 */
157
158 node = spl_fit_images_find_uboot(blob);
159 if (node >= 0)
160 bl33_entry = spl_fit_images_get_entry(blob, node);
161
162 /*
Philipp Tomsichd21fb632018-01-02 21:16:43 +0100163 * If ATF_NO_PLATFORM_PARAM is set, we override the platform
164 * parameter and always pass 0. This is a workaround for
165 * older ATF versions that have insufficiently robust (or
166 * overzealous) argument validation.
167 */
168 if (CONFIG_IS_ENABLED(ATF_NO_PLATFORM_PARAM))
169 platform_param = 0;
170
171 /*
Philipp Tomsich1d379092017-09-13 21:29:35 +0200172 * We don't provide a BL3-2 entry yet, but this will be possible
173 * using similar logic.
174 */
Philipp Tomsichd21fb632018-01-02 21:16:43 +0100175 bl31_entry(spl_image->entry_point, bl33_entry, platform_param);
Kever Yangbcc17262017-05-05 11:47:45 +0800176}