xref: /rk3399_ARM-atf/plat/arm/board/tc/tc_bl31_setup.c (revision bb04d0232e8eeb593028aa730618be35d32a4f22)
16ec0c65bSUsama Arif /*
2638e4a92SBoyan Karatotev  * Copyright (c) 2020-2024, Arm Limited and Contributors. All rights reserved.
36ec0c65bSUsama Arif  *
46ec0c65bSUsama Arif  * SPDX-License-Identifier: BSD-3-Clause
56ec0c65bSUsama Arif  */
66ec0c65bSUsama Arif 
76ec0c65bSUsama Arif #include <assert.h>
86ec0c65bSUsama Arif 
96ec0c65bSUsama Arif #include <libfdt.h>
106ec0c65bSUsama Arif #include <tc_plat.h>
116ec0c65bSUsama Arif 
12a8778185SManish V Badarkhe #include <arch_helpers.h>
136ec0c65bSUsama Arif #include <common/bl_common.h>
146ec0c65bSUsama Arif #include <common/debug.h>
156ec0c65bSUsama Arif #include <drivers/arm/css/css_mhu_doorbell.h>
166ec0c65bSUsama Arif #include <drivers/arm/css/scmi.h>
1728b2d86cSMadhukar Pappireddy #include <drivers/arm/sbsa.h>
1834a87d74SUsama Arif #include <lib/fconf/fconf.h>
1934a87d74SUsama Arif #include <lib/fconf/fconf_dyn_cfg_getter.h>
206ec0c65bSUsama Arif #include <plat/arm/common/plat_arm.h>
216ec0c65bSUsama Arif #include <plat/common/platform.h>
226ec0c65bSUsama Arif 
23d2ce6aa0SManish V Badarkhe #ifdef PLATFORM_TEST_TFM_TESTSUITE
24a8778185SManish V Badarkhe #include <psa/crypto_platform.h>
25a8778185SManish V Badarkhe #include <psa/crypto_types.h>
26a8778185SManish V Badarkhe #include <psa/crypto_values.h>
27d2ce6aa0SManish V Badarkhe #endif /* PLATFORM_TEST_TFM_TESTSUITE */
28a8778185SManish V Badarkhe 
29a8778185SManish V Badarkhe #ifdef PLATFORM_TEST_TFM_TESTSUITE
30a8778185SManish V Badarkhe /*
31a8778185SManish V Badarkhe  * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
32a8778185SManish V Badarkhe  * mbedTLS config option) so we need to provide an implementation of
33a8778185SManish V Badarkhe  * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
34a8778185SManish V Badarkhe  * actually use any of external RNG and this function is only needed during
35a8778185SManish V Badarkhe  * the execution of TF-M testsuite during exporting the public part of the
36a8778185SManish V Badarkhe  * delegated attestation key.
37a8778185SManish V Badarkhe  */
38a8778185SManish V Badarkhe psa_status_t mbedtls_psa_external_get_random(
39a8778185SManish V Badarkhe 			mbedtls_psa_external_random_context_t *context,
40a8778185SManish V Badarkhe 			uint8_t *output, size_t output_size,
41a8778185SManish V Badarkhe 			size_t *output_length)
42a8778185SManish V Badarkhe {
43a8778185SManish V Badarkhe 	for (size_t i = 0U; i < output_size; i++) {
44a8778185SManish V Badarkhe 		output[i] = (uint8_t)(read_cntpct_el0() & 0xFFU);
45a8778185SManish V Badarkhe 	}
46a8778185SManish V Badarkhe 
47a8778185SManish V Badarkhe 	*output_length = output_size;
48a8778185SManish V Badarkhe 
49a8778185SManish V Badarkhe 	return PSA_SUCCESS;
50a8778185SManish V Badarkhe }
51a8778185SManish V Badarkhe #endif /* PLATFORM_TEST_TFM_TESTSUITE */
52a8778185SManish V Badarkhe 
534f65c0beSLeo Yan #if TARGET_PLATFORM <= 2
54d2b1eb80SLeo Yan static scmi_channel_plat_info_t tc_scmi_plat_info = {
556ec0c65bSUsama Arif 	.scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
566ec0c65bSUsama Arif 	.db_reg_addr = PLAT_CSS_MHU_BASE + SENDER_REG_SET(0),
576ec0c65bSUsama Arif 	.db_preserve_mask = 0xfffffffe,
586ec0c65bSUsama Arif 	.db_modify_mask = 0x1,
596ec0c65bSUsama Arif 	.ring_doorbell = &mhuv2_ring_doorbell,
606ec0c65bSUsama Arif };
614f65c0beSLeo Yan #elif TARGET_PLATFORM == 3
624f65c0beSLeo Yan static scmi_channel_plat_info_t tc_scmi_plat_info = {
634f65c0beSLeo Yan 	.scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
644f65c0beSLeo Yan 	.db_reg_addr = PLAT_CSS_MHU_BASE + MHU_V3_SENDER_REG_SET(0),
654f65c0beSLeo Yan 	.db_preserve_mask = 0xfffffffe,
664f65c0beSLeo Yan 	.db_modify_mask = 0x1,
674f65c0beSLeo Yan 	.ring_doorbell = &mhu_ring_doorbell,
684f65c0beSLeo Yan };
69adc91a34SJagdish Gediya 
70adc91a34SJagdish Gediya static void enable_ns_mcn_pmu(void)
71adc91a34SJagdish Gediya {
72adc91a34SJagdish Gediya 	/*
73adc91a34SJagdish Gediya 	 * Enable non-secure access to MCN PMU registers
74adc91a34SJagdish Gediya 	 */
75adc91a34SJagdish Gediya 	for (int i = 0; i < MCN_INSTANCES; i++) {
76adc91a34SJagdish Gediya 		uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET +
77adc91a34SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE);
78adc91a34SJagdish Gediya 		mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
79adc91a34SJagdish Gediya 	}
80adc91a34SJagdish Gediya }
81*bb04d023SJagdish Gediya 
82*bb04d023SJagdish Gediya static void set_mcn_slc_alloc_mode(void)
83*bb04d023SJagdish Gediya {
84*bb04d023SJagdish Gediya 	/*
85*bb04d023SJagdish Gediya 	 * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
86*bb04d023SJagdish Gediya 	 * 0b01 (always alloc), configure both to 0b10 (use bus signal
87*bb04d023SJagdish Gediya 	 * attribute from interface).
88*bb04d023SJagdish Gediya 	 */
89*bb04d023SJagdish Gediya 	for (int i = 0; i < MCN_INSTANCES; i++) {
90*bb04d023SJagdish Gediya 		uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR +
91*bb04d023SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
92*bb04d023SJagdish Gediya 		uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR +
93*bb04d023SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
94*bb04d023SJagdish Gediya 
95*bb04d023SJagdish Gediya 		mmio_clrsetbits_32(slccfg_ctl_ns,
96*bb04d023SJagdish Gediya 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
97*bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
98*bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
99*bb04d023SJagdish Gediya 		mmio_clrsetbits_32(slccfg_ctl_s,
100*bb04d023SJagdish Gediya 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
101*bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
102*bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
103*bb04d023SJagdish Gediya 	}
104*bb04d023SJagdish Gediya }
1054f65c0beSLeo Yan #endif
1066ec0c65bSUsama Arif 
1076ec0c65bSUsama Arif void bl31_platform_setup(void)
1086ec0c65bSUsama Arif {
1096ec0c65bSUsama Arif 	tc_bl31_common_platform_setup();
110adc91a34SJagdish Gediya #if TARGET_PLATFORM == 3
111adc91a34SJagdish Gediya 	enable_ns_mcn_pmu();
112*bb04d023SJagdish Gediya 	set_mcn_slc_alloc_mode();
113adc91a34SJagdish Gediya #endif
1146ec0c65bSUsama Arif }
1156ec0c65bSUsama Arif 
116d2b1eb80SLeo Yan scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
1176ec0c65bSUsama Arif {
1186ec0c65bSUsama Arif 
119d2b1eb80SLeo Yan 	return &tc_scmi_plat_info;
1206ec0c65bSUsama Arif 
1216ec0c65bSUsama Arif }
1226ec0c65bSUsama Arif 
1236ec0c65bSUsama Arif void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
1246ec0c65bSUsama Arif 				u_register_t arg2, u_register_t arg3)
1256ec0c65bSUsama Arif {
1266ec0c65bSUsama Arif 	arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
12734a87d74SUsama Arif 
12834a87d74SUsama Arif 	/* Fill the properties struct with the info from the config dtb */
12934a87d74SUsama Arif 	fconf_populate("FW_CONFIG", arg1);
1306ec0c65bSUsama Arif }
1316ec0c65bSUsama Arif 
1326fbe11cdSlaurenw-arm #ifdef PLATFORM_TESTS
1334eefbf1bSSandrine Bailleux static __dead2 void tc_run_platform_tests(void)
1344eefbf1bSSandrine Bailleux {
135303ef33eSSandrine Bailleux 	int tests_failed;
136303ef33eSSandrine Bailleux 
137303ef33eSSandrine Bailleux 	printf("\nStarting platform tests...\n");
138303ef33eSSandrine Bailleux 
139657b90eaSTamas Ban #ifdef PLATFORM_TEST_NV_COUNTERS
140303ef33eSSandrine Bailleux 	tests_failed = nv_counter_test();
14100b7e0bfSlaurenw-arm #elif PLATFORM_TEST_ROTPK
14200b7e0bfSlaurenw-arm 	tests_failed = rotpk_test();
143657b90eaSTamas Ban #elif PLATFORM_TEST_TFM_TESTSUITE
144303ef33eSSandrine Bailleux 	tests_failed = run_platform_tests();
1451b076113Slaurenw-arm #endif
146303ef33eSSandrine Bailleux 
147303ef33eSSandrine Bailleux 	printf("Platform tests %s.\n",
148303ef33eSSandrine Bailleux 	       (tests_failed != 0) ? "failed" : "succeeded");
149303ef33eSSandrine Bailleux 
15057cc12c8SSandrine Bailleux 	/* Suspend booting, no matter the tests outcome. */
151303ef33eSSandrine Bailleux 	printf("Suspend booting...\n");
15225dd2172SMate Toth-Pal 	plat_error_handler(-1);
1534eefbf1bSSandrine Bailleux }
1544eefbf1bSSandrine Bailleux #endif
1554eefbf1bSSandrine Bailleux 
1566ec0c65bSUsama Arif void tc_bl31_common_platform_setup(void)
1576ec0c65bSUsama Arif {
1586ec0c65bSUsama Arif 	arm_bl31_platform_setup();
15925dd2172SMate Toth-Pal 
1604eefbf1bSSandrine Bailleux #ifdef PLATFORM_TESTS
1614eefbf1bSSandrine Bailleux 	tc_run_platform_tests();
1629b266556Slaurenw-arm #endif
1636ec0c65bSUsama Arif }
1646ec0c65bSUsama Arif 
1656ec0c65bSUsama Arif const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
1666ec0c65bSUsama Arif {
1676ec0c65bSUsama Arif 	return css_scmi_override_pm_ops(ops);
1686ec0c65bSUsama Arif }
16934a87d74SUsama Arif 
17034a87d74SUsama Arif void __init bl31_plat_arch_setup(void)
17134a87d74SUsama Arif {
17234a87d74SUsama Arif 	arm_bl31_plat_arch_setup();
17334a87d74SUsama Arif 
17434a87d74SUsama Arif 	/* HW_CONFIG was also loaded by BL2 */
17534a87d74SUsama Arif 	const struct dyn_cfg_dtb_info_t *hw_config_info;
17634a87d74SUsama Arif 
17734a87d74SUsama Arif 	hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
17834a87d74SUsama Arif 	assert(hw_config_info != NULL);
17934a87d74SUsama Arif 
18034a87d74SUsama Arif 	fconf_populate("HW_CONFIG", hw_config_info->config_addr);
18134a87d74SUsama Arif }
18228b2d86cSMadhukar Pappireddy 
183fd51b215SGovindraj Raja #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
18428b2d86cSMadhukar Pappireddy void tc_bl31_plat_runtime_setup(void)
18528b2d86cSMadhukar Pappireddy {
18628b2d86cSMadhukar Pappireddy 	/* Start secure watchdog timer. */
18728b2d86cSMadhukar Pappireddy 	plat_arm_secure_wdt_start();
188c864af98SSalman Nabi 
189c864af98SSalman Nabi 	arm_bl31_plat_runtime_setup();
19028b2d86cSMadhukar Pappireddy }
19128b2d86cSMadhukar Pappireddy 
19228b2d86cSMadhukar Pappireddy void bl31_plat_runtime_setup(void)
19328b2d86cSMadhukar Pappireddy {
19428b2d86cSMadhukar Pappireddy 	tc_bl31_plat_runtime_setup();
19528b2d86cSMadhukar Pappireddy }
19628b2d86cSMadhukar Pappireddy 
19728b2d86cSMadhukar Pappireddy /*
19828b2d86cSMadhukar Pappireddy  * Platform handler for Group0 secure interrupt.
19928b2d86cSMadhukar Pappireddy  */
20028b2d86cSMadhukar Pappireddy int plat_spmd_handle_group0_interrupt(uint32_t intid)
20128b2d86cSMadhukar Pappireddy {
20228b2d86cSMadhukar Pappireddy 	/* Trusted Watchdog timer is the only source of Group0 interrupt now. */
20328b2d86cSMadhukar Pappireddy 	if (intid == SBSA_SECURE_WDOG_INTID) {
20428b2d86cSMadhukar Pappireddy 		/* Refresh the timer. */
20528b2d86cSMadhukar Pappireddy 		plat_arm_secure_wdt_refresh();
20628b2d86cSMadhukar Pappireddy 
20728b2d86cSMadhukar Pappireddy 		return 0;
20828b2d86cSMadhukar Pappireddy 	}
20928b2d86cSMadhukar Pappireddy 
21028b2d86cSMadhukar Pappireddy 	return -1;
21128b2d86cSMadhukar Pappireddy }
212fd51b215SGovindraj Raja #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/
213