xref: /rk3399_ARM-atf/plat/arm/board/tc/tc_bl31_setup.c (revision e8e1b60820dcba1f2be151d296a8e81de9bed8ba)
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 };
61*e8e1b608SJackson Cooper-Driver #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 };
69*e8e1b608SJackson Cooper-Driver #endif
70adc91a34SJagdish Gediya 
71*e8e1b608SJackson Cooper-Driver #if TARGET_PLATFORM == 3
72adc91a34SJagdish Gediya static void enable_ns_mcn_pmu(void)
73adc91a34SJagdish Gediya {
74adc91a34SJagdish Gediya 	/*
75adc91a34SJagdish Gediya 	 * Enable non-secure access to MCN PMU registers
76adc91a34SJagdish Gediya 	 */
77adc91a34SJagdish Gediya 	for (int i = 0; i < MCN_INSTANCES; i++) {
78adc91a34SJagdish Gediya 		uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET +
79adc91a34SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE);
80adc91a34SJagdish Gediya 		mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
81adc91a34SJagdish Gediya 	}
82adc91a34SJagdish Gediya }
83bb04d023SJagdish Gediya 
84bb04d023SJagdish Gediya static void set_mcn_slc_alloc_mode(void)
85bb04d023SJagdish Gediya {
86bb04d023SJagdish Gediya 	/*
87bb04d023SJagdish Gediya 	 * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
88bb04d023SJagdish Gediya 	 * 0b01 (always alloc), configure both to 0b10 (use bus signal
89bb04d023SJagdish Gediya 	 * attribute from interface).
90bb04d023SJagdish Gediya 	 */
91bb04d023SJagdish Gediya 	for (int i = 0; i < MCN_INSTANCES; i++) {
92bb04d023SJagdish Gediya 		uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR +
93bb04d023SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
94bb04d023SJagdish Gediya 		uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR +
95bb04d023SJagdish Gediya 			(i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
96bb04d023SJagdish Gediya 
97bb04d023SJagdish Gediya 		mmio_clrsetbits_32(slccfg_ctl_ns,
98bb04d023SJagdish Gediya 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
99bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
100bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
101bb04d023SJagdish Gediya 		mmio_clrsetbits_32(slccfg_ctl_s,
102bb04d023SJagdish Gediya 				   (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
103bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
104bb04d023SJagdish Gediya 				   (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
105bb04d023SJagdish Gediya 	}
106bb04d023SJagdish Gediya }
1074f65c0beSLeo Yan #endif
1086ec0c65bSUsama Arif 
1096ec0c65bSUsama Arif void bl31_platform_setup(void)
1106ec0c65bSUsama Arif {
1116ec0c65bSUsama Arif 	tc_bl31_common_platform_setup();
112adc91a34SJagdish Gediya #if TARGET_PLATFORM == 3
113adc91a34SJagdish Gediya 	enable_ns_mcn_pmu();
114bb04d023SJagdish Gediya 	set_mcn_slc_alloc_mode();
11589c58a50SJagdish Gediya 	plat_arm_ni_setup(NCI_BASE_ADDR);
116adc91a34SJagdish Gediya #endif
1176ec0c65bSUsama Arif }
1186ec0c65bSUsama Arif 
119d2b1eb80SLeo Yan scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
1206ec0c65bSUsama Arif {
1216ec0c65bSUsama Arif 
122d2b1eb80SLeo Yan 	return &tc_scmi_plat_info;
1236ec0c65bSUsama Arif 
1246ec0c65bSUsama Arif }
1256ec0c65bSUsama Arif 
1266ec0c65bSUsama Arif void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
1276ec0c65bSUsama Arif 				u_register_t arg2, u_register_t arg3)
1286ec0c65bSUsama Arif {
1296ec0c65bSUsama Arif 	arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
13034a87d74SUsama Arif 
13134a87d74SUsama Arif 	/* Fill the properties struct with the info from the config dtb */
13234a87d74SUsama Arif 	fconf_populate("FW_CONFIG", arg1);
1336ec0c65bSUsama Arif }
1346ec0c65bSUsama Arif 
1356fbe11cdSlaurenw-arm #ifdef PLATFORM_TESTS
1364eefbf1bSSandrine Bailleux static __dead2 void tc_run_platform_tests(void)
1374eefbf1bSSandrine Bailleux {
138303ef33eSSandrine Bailleux 	int tests_failed;
139303ef33eSSandrine Bailleux 
140303ef33eSSandrine Bailleux 	printf("\nStarting platform tests...\n");
141303ef33eSSandrine Bailleux 
142657b90eaSTamas Ban #ifdef PLATFORM_TEST_NV_COUNTERS
143303ef33eSSandrine Bailleux 	tests_failed = nv_counter_test();
14400b7e0bfSlaurenw-arm #elif PLATFORM_TEST_ROTPK
14500b7e0bfSlaurenw-arm 	tests_failed = rotpk_test();
146657b90eaSTamas Ban #elif PLATFORM_TEST_TFM_TESTSUITE
147303ef33eSSandrine Bailleux 	tests_failed = run_platform_tests();
1481b076113Slaurenw-arm #endif
149303ef33eSSandrine Bailleux 
150303ef33eSSandrine Bailleux 	printf("Platform tests %s.\n",
151303ef33eSSandrine Bailleux 	       (tests_failed != 0) ? "failed" : "succeeded");
152303ef33eSSandrine Bailleux 
15357cc12c8SSandrine Bailleux 	/* Suspend booting, no matter the tests outcome. */
154303ef33eSSandrine Bailleux 	printf("Suspend booting...\n");
15525dd2172SMate Toth-Pal 	plat_error_handler(-1);
1564eefbf1bSSandrine Bailleux }
1574eefbf1bSSandrine Bailleux #endif
1584eefbf1bSSandrine Bailleux 
1596ec0c65bSUsama Arif void tc_bl31_common_platform_setup(void)
1606ec0c65bSUsama Arif {
1616ec0c65bSUsama Arif 	arm_bl31_platform_setup();
16225dd2172SMate Toth-Pal 
1634eefbf1bSSandrine Bailleux #ifdef PLATFORM_TESTS
1644eefbf1bSSandrine Bailleux 	tc_run_platform_tests();
1659b266556Slaurenw-arm #endif
1666ec0c65bSUsama Arif }
1676ec0c65bSUsama Arif 
1686ec0c65bSUsama Arif const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
1696ec0c65bSUsama Arif {
1706ec0c65bSUsama Arif 	return css_scmi_override_pm_ops(ops);
1716ec0c65bSUsama Arif }
17234a87d74SUsama Arif 
17334a87d74SUsama Arif void __init bl31_plat_arch_setup(void)
17434a87d74SUsama Arif {
17534a87d74SUsama Arif 	arm_bl31_plat_arch_setup();
17634a87d74SUsama Arif 
17734a87d74SUsama Arif 	/* HW_CONFIG was also loaded by BL2 */
17834a87d74SUsama Arif 	const struct dyn_cfg_dtb_info_t *hw_config_info;
17934a87d74SUsama Arif 
18034a87d74SUsama Arif 	hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
18134a87d74SUsama Arif 	assert(hw_config_info != NULL);
18234a87d74SUsama Arif 
18334a87d74SUsama Arif 	fconf_populate("HW_CONFIG", hw_config_info->config_addr);
18434a87d74SUsama Arif }
18528b2d86cSMadhukar Pappireddy 
186fd51b215SGovindraj Raja #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
18728b2d86cSMadhukar Pappireddy void tc_bl31_plat_runtime_setup(void)
18828b2d86cSMadhukar Pappireddy {
18928b2d86cSMadhukar Pappireddy 	/* Start secure watchdog timer. */
19028b2d86cSMadhukar Pappireddy 	plat_arm_secure_wdt_start();
191c864af98SSalman Nabi 
192c864af98SSalman Nabi 	arm_bl31_plat_runtime_setup();
19328b2d86cSMadhukar Pappireddy }
19428b2d86cSMadhukar Pappireddy 
19528b2d86cSMadhukar Pappireddy void bl31_plat_runtime_setup(void)
19628b2d86cSMadhukar Pappireddy {
19728b2d86cSMadhukar Pappireddy 	tc_bl31_plat_runtime_setup();
19828b2d86cSMadhukar Pappireddy }
19928b2d86cSMadhukar Pappireddy 
20028b2d86cSMadhukar Pappireddy /*
20128b2d86cSMadhukar Pappireddy  * Platform handler for Group0 secure interrupt.
20228b2d86cSMadhukar Pappireddy  */
20328b2d86cSMadhukar Pappireddy int plat_spmd_handle_group0_interrupt(uint32_t intid)
20428b2d86cSMadhukar Pappireddy {
20528b2d86cSMadhukar Pappireddy 	/* Trusted Watchdog timer is the only source of Group0 interrupt now. */
20628b2d86cSMadhukar Pappireddy 	if (intid == SBSA_SECURE_WDOG_INTID) {
20728b2d86cSMadhukar Pappireddy 		/* Refresh the timer. */
20828b2d86cSMadhukar Pappireddy 		plat_arm_secure_wdt_refresh();
20928b2d86cSMadhukar Pappireddy 
21028b2d86cSMadhukar Pappireddy 		return 0;
21128b2d86cSMadhukar Pappireddy 	}
21228b2d86cSMadhukar Pappireddy 
21328b2d86cSMadhukar Pappireddy 	return -1;
21428b2d86cSMadhukar Pappireddy }
215fd51b215SGovindraj Raja #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/
216