xref: /rk3399_ARM-atf/plat/arm/board/tc/tc_bl31_setup.c (revision 6d415de83fe084c08558895837d0eb90210420a9)
1 /*
2  * Copyright (c) 2020-2024, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <libfdt.h>
10 #include <tc_plat.h>
11 
12 #include <arch_helpers.h>
13 #include <common/bl_common.h>
14 #include <common/debug.h>
15 #include <drivers/arm/css/css_mhu_doorbell.h>
16 #include <drivers/arm/css/scmi.h>
17 #include <drivers/arm/sbsa.h>
18 #include <lib/fconf/fconf.h>
19 #include <lib/fconf/fconf_dyn_cfg_getter.h>
20 #include <plat/arm/common/plat_arm.h>
21 #include <plat/common/platform.h>
22 
23 #ifdef PLATFORM_TEST_TFM_TESTSUITE
24 #include <psa/crypto_platform.h>
25 #include <psa/crypto_types.h>
26 #include <psa/crypto_values.h>
27 #endif /* PLATFORM_TEST_TFM_TESTSUITE */
28 #include <psa/error.h>
29 
30 #include <drivers/arm/rse_comms.h>
31 #include <plat/common/platform.h>
32 
33 #ifdef PLATFORM_TEST_TFM_TESTSUITE
34 /*
35  * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
36  * mbedTLS config option) so we need to provide an implementation of
37  * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
38  * actually use any of external RNG and this function is only needed during
39  * the execution of TF-M testsuite during exporting the public part of the
40  * delegated attestation key.
41  */
42 psa_status_t mbedtls_psa_external_get_random(
43 			mbedtls_psa_external_random_context_t *context,
44 			uint8_t *output, size_t output_size,
45 			size_t *output_length)
46 {
47 	for (size_t i = 0U; i < output_size; i++) {
48 		output[i] = (uint8_t)(read_cntpct_el0() & 0xFFU);
49 	}
50 
51 	*output_length = output_size;
52 
53 	return PSA_SUCCESS;
54 }
55 #endif /* PLATFORM_TEST_TFM_TESTSUITE */
56 
57 #if TARGET_PLATFORM <= 2
58 static scmi_channel_plat_info_t tc_scmi_plat_info = {
59 	.scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
60 	.db_reg_addr = PLAT_CSS_MHU_BASE + SENDER_REG_SET(0),
61 	.db_preserve_mask = 0xfffffffe,
62 	.db_modify_mask = 0x1,
63 	.ring_doorbell = &mhuv2_ring_doorbell,
64 };
65 #elif TARGET_PLATFORM >= 3
66 static scmi_channel_plat_info_t tc_scmi_plat_info = {
67 	.scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
68 	.db_reg_addr = PLAT_CSS_MHU_BASE + MHU_V3_SENDER_REG_SET(0),
69 	.db_preserve_mask = 0xfffffffe,
70 	.db_modify_mask = 0x1,
71 	.ring_doorbell = &mhu_ring_doorbell,
72 };
73 #endif
74 
75 #if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4)
76 static void enable_ns_mcn_pmu(void)
77 {
78 	/*
79 	 * Enable non-secure access to MCN PMU registers
80 	 */
81 	for (int i = 0; i < MCN_INSTANCES; i++) {
82 		uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR(i) +
83 			MCN_SCR_OFFSET;
84 		mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
85 	}
86 }
87 #endif	/* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */
88 
89 #if TARGET_PLATFORM == 3
90 static void set_mcn_slc_alloc_mode(void)
91 {
92 	/*
93 	 * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
94 	 * 0b01 (always alloc), configure both to 0b10 (use bus signal
95 	 * attribute from interface).
96 	 */
97 	for (int i = 0; i < MCN_INSTANCES; i++) {
98 		uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR(i) +
99 			MPAM_SLCCFG_CTL_OFFSET;
100 		uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR(i) +
101 			MPAM_SLCCFG_CTL_OFFSET;
102 
103 		mmio_clrsetbits_32(slccfg_ctl_ns,
104 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
105 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
106 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
107 		mmio_clrsetbits_32(slccfg_ctl_s,
108 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
109 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
110 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
111 	}
112 }
113 #endif
114 
115 void bl31_platform_setup(void)
116 {
117 	tc_bl31_common_platform_setup();
118 #if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4)
119 	enable_ns_mcn_pmu();
120 #endif	/* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */
121 #if TARGET_PLATFORM == 3
122 	set_mcn_slc_alloc_mode();
123 	plat_arm_ni_setup(NCI_BASE_ADDR);
124 #endif
125 }
126 
127 scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
128 {
129 
130 	return &tc_scmi_plat_info;
131 
132 }
133 
134 void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
135 				u_register_t arg2, u_register_t arg3)
136 {
137 	arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
138 
139 	/* Fill the properties struct with the info from the config dtb */
140 	fconf_populate("FW_CONFIG", arg1);
141 }
142 
143 #ifdef PLATFORM_TESTS
144 static __dead2 void tc_run_platform_tests(void)
145 {
146 	int tests_failed;
147 
148 	printf("\nStarting platform tests...\n");
149 
150 #ifdef PLATFORM_TEST_NV_COUNTERS
151 	tests_failed = nv_counter_test();
152 #elif PLATFORM_TEST_ROTPK
153 	tests_failed = rotpk_test();
154 #elif PLATFORM_TEST_TFM_TESTSUITE
155 	tests_failed = run_platform_tests();
156 #endif
157 
158 	printf("Platform tests %s.\n",
159 	       (tests_failed != 0) ? "failed" : "succeeded");
160 
161 	/* Suspend booting, no matter the tests outcome. */
162 	printf("Suspend booting...\n");
163 	plat_error_handler(-1);
164 }
165 #endif
166 
167 void tc_bl31_common_platform_setup(void)
168 {
169 	arm_bl31_platform_setup();
170 
171 #ifdef PLATFORM_TESTS
172 	tc_run_platform_tests();
173 #endif
174 }
175 
176 const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
177 {
178 	return css_scmi_override_pm_ops(ops);
179 }
180 
181 void __init bl31_plat_arch_setup(void)
182 {
183 	arm_bl31_plat_arch_setup();
184 
185 	/* HW_CONFIG was also loaded by BL2 */
186 	const struct dyn_cfg_dtb_info_t *hw_config_info;
187 
188 	hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
189 	assert(hw_config_info != NULL);
190 
191 	fconf_populate("HW_CONFIG", hw_config_info->config_addr);
192 }
193 
194 #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
195 void tc_bl31_plat_runtime_setup(void)
196 {
197 	psa_status_t status;
198 
199 	/* Start secure watchdog timer. */
200 	plat_arm_secure_wdt_start();
201 
202 	arm_bl31_plat_runtime_setup();
203 
204 	/* Initialise RSE communication channel */
205 	status = rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE, PLAT_RSE_AP_RCV_MHU_BASE);
206 	if (status != PSA_SUCCESS) {
207 		ERROR("Failed to initialize RSE communication channel - psa_status = %d\n", status);
208 	}
209 }
210 
211 void bl31_plat_runtime_setup(void)
212 {
213 	tc_bl31_plat_runtime_setup();
214 }
215 
216 /*
217  * Platform handler for Group0 secure interrupt.
218  */
219 int plat_spmd_handle_group0_interrupt(uint32_t intid)
220 {
221 	/* Trusted Watchdog timer is the only source of Group0 interrupt now. */
222 	if (intid == SBSA_SECURE_WDOG_INTID) {
223 		/* Refresh the timer. */
224 		plat_arm_secure_wdt_refresh();
225 
226 		return 0;
227 	}
228 
229 	return -1;
230 }
231 #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/
232