xref: /aosp_15_r20/external/coreboot/src/arch/arm64/bl31.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <arch/lib_helpers.h>
4 #include <arch/mmu.h>
5 #include <arch/transition.h>
6 #include <bl31.h>
7 #include <bootmem.h>
8 #include <cbfs.h>
9 #include <cbmem.h>
10 #include <console/console.h>
11 #include <program_loading.h>
12 
13 #include <arm-trusted-firmware/include/export/common/bl_common_exp.h>
14 
15 static entry_point_info_t bl32_ep_info = {
16 	.h = {
17 		.type = PARAM_EP,
18 		.version = PARAM_VERSION_1,
19 		.size = sizeof(bl32_ep_info),
20 		.attr = EP_SECURE,
21 	},
22 };
23 static entry_point_info_t bl33_ep_info = {
24 	.h = {
25 		.type = PARAM_EP,
26 		.version = PARAM_VERSION_1,
27 		.size = sizeof(bl33_ep_info),
28 		.attr = EP_NON_SECURE,
29 	},
30 };
31 
32 static bl_params_node_t bl32_params_node = {
33 	.image_id = BL32_IMAGE_ID,
34 	.ep_info = &bl32_ep_info,
35 };
36 static bl_params_node_t bl33_params_node = {
37 	.image_id = BL33_IMAGE_ID,
38 	.ep_info = &bl33_ep_info,
39 };
40 
41 static bl_params_t bl_params = {
42 	.h = {
43 		.type = PARAM_BL_PARAMS,
44 		.version = PARAM_VERSION_2,
45 		.size = sizeof(bl_params),
46 		.attr = 0,
47 	},
48 	.head = &bl33_params_node,
49 };
50 
51 static struct bl_aux_param_header *bl_aux_params;
52 
53 /* Only works when using the default soc_get_bl31_plat_params() below. */
register_bl31_aux_param(struct bl_aux_param_header * param)54 void register_bl31_aux_param(struct bl_aux_param_header *param)
55 {
56 	param->next = (uintptr_t)bl_aux_params;
57 	bl_aux_params = param;
58 }
59 
60 /* Default implementation. All newly added SoCs should use this if possible! */
soc_get_bl31_plat_params(void)61 __weak void *soc_get_bl31_plat_params(void)
62 {
63 	static struct bl_aux_param_uint64 cbtable_param = {
64 		.h = { .type = BL_AUX_PARAM_COREBOOT_TABLE, },
65 	};
66 	if (!cbtable_param.value) {
67 		cbtable_param.value = (uint64_t)cbmem_find(CBMEM_ID_CBTABLE);
68 		if (cbtable_param.value)
69 			register_bl31_aux_param(&cbtable_param.h);
70 	}
71 	return bl_aux_params;
72 }
73 
run_bl31(u64 payload_entry,u64 payload_arg0,u64 payload_spsr)74 void run_bl31(u64 payload_entry, u64 payload_arg0, u64 payload_spsr)
75 {
76 	struct prog bl31 = PROG_INIT(PROG_BL31, CONFIG_CBFS_PREFIX"/bl31");
77 	void (*bl31_entry)(bl_params_t *params, void *plat_params) = NULL;
78 
79 	if (!selfload_check(&bl31, BM_MEM_BL31))
80 		die("BL31 load failed");
81 	bl31_entry = prog_entry(&bl31);
82 
83 	if (CONFIG(ARM64_USE_SECURE_OS)) {
84 		struct prog bl32 = PROG_INIT(PROG_BL32,
85 					     CONFIG_CBFS_PREFIX"/secure_os");
86 
87 		if (cbfs_prog_stage_load(&bl32))
88 			die("BL32 load failed");
89 
90 		bl32_ep_info.pc = (uintptr_t)prog_entry(&bl32);
91 		bl32_ep_info.spsr = SPSR_EXCEPTION_MASK |
92 				get_eret_el(EL1, SPSR_USE_L);
93 		bl33_params_node.next_params_info = &bl32_params_node;
94 	}
95 
96 	bl33_ep_info.pc = payload_entry;
97 	bl33_ep_info.spsr = payload_spsr;
98 	bl33_ep_info.args.arg0 = payload_arg0;
99 
100 	void *bl31_plat_params = soc_get_bl31_plat_params();
101 
102 	/* MMU disable will flush cache, so passed params land in memory. */
103 	raw_write_daif(SPSR_EXCEPTION_MASK);
104 	mmu_disable();
105 	bl31_entry(&bl_params, bl31_plat_params);
106 	die("BL31 returned!");
107 }
108