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 }; 69*adc91a34SJagdish Gediya 70*adc91a34SJagdish Gediya static void enable_ns_mcn_pmu(void) 71*adc91a34SJagdish Gediya { 72*adc91a34SJagdish Gediya /* 73*adc91a34SJagdish Gediya * Enable non-secure access to MCN PMU registers 74*adc91a34SJagdish Gediya */ 75*adc91a34SJagdish Gediya for (int i = 0; i < MCN_INSTANCES; i++) { 76*adc91a34SJagdish Gediya uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET + 77*adc91a34SJagdish Gediya (i * MCN_ADDRESS_SPACE_SIZE); 78*adc91a34SJagdish Gediya mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT); 79*adc91a34SJagdish Gediya } 80*adc91a34SJagdish Gediya } 814f65c0beSLeo Yan #endif 826ec0c65bSUsama Arif 836ec0c65bSUsama Arif void bl31_platform_setup(void) 846ec0c65bSUsama Arif { 856ec0c65bSUsama Arif tc_bl31_common_platform_setup(); 86*adc91a34SJagdish Gediya #if TARGET_PLATFORM == 3 87*adc91a34SJagdish Gediya enable_ns_mcn_pmu(); 88*adc91a34SJagdish Gediya #endif 896ec0c65bSUsama Arif } 906ec0c65bSUsama Arif 91d2b1eb80SLeo Yan scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused) 926ec0c65bSUsama Arif { 936ec0c65bSUsama Arif 94d2b1eb80SLeo Yan return &tc_scmi_plat_info; 956ec0c65bSUsama Arif 966ec0c65bSUsama Arif } 976ec0c65bSUsama Arif 986ec0c65bSUsama Arif void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, 996ec0c65bSUsama Arif u_register_t arg2, u_register_t arg3) 1006ec0c65bSUsama Arif { 1016ec0c65bSUsama Arif arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3); 10234a87d74SUsama Arif 10334a87d74SUsama Arif /* Fill the properties struct with the info from the config dtb */ 10434a87d74SUsama Arif fconf_populate("FW_CONFIG", arg1); 1056ec0c65bSUsama Arif } 1066ec0c65bSUsama Arif 1076fbe11cdSlaurenw-arm #ifdef PLATFORM_TESTS 1084eefbf1bSSandrine Bailleux static __dead2 void tc_run_platform_tests(void) 1094eefbf1bSSandrine Bailleux { 110303ef33eSSandrine Bailleux int tests_failed; 111303ef33eSSandrine Bailleux 112303ef33eSSandrine Bailleux printf("\nStarting platform tests...\n"); 113303ef33eSSandrine Bailleux 114657b90eaSTamas Ban #ifdef PLATFORM_TEST_NV_COUNTERS 115303ef33eSSandrine Bailleux tests_failed = nv_counter_test(); 11600b7e0bfSlaurenw-arm #elif PLATFORM_TEST_ROTPK 11700b7e0bfSlaurenw-arm tests_failed = rotpk_test(); 118657b90eaSTamas Ban #elif PLATFORM_TEST_TFM_TESTSUITE 119303ef33eSSandrine Bailleux tests_failed = run_platform_tests(); 1201b076113Slaurenw-arm #endif 121303ef33eSSandrine Bailleux 122303ef33eSSandrine Bailleux printf("Platform tests %s.\n", 123303ef33eSSandrine Bailleux (tests_failed != 0) ? "failed" : "succeeded"); 124303ef33eSSandrine Bailleux 12557cc12c8SSandrine Bailleux /* Suspend booting, no matter the tests outcome. */ 126303ef33eSSandrine Bailleux printf("Suspend booting...\n"); 12725dd2172SMate Toth-Pal plat_error_handler(-1); 1284eefbf1bSSandrine Bailleux } 1294eefbf1bSSandrine Bailleux #endif 1304eefbf1bSSandrine Bailleux 1316ec0c65bSUsama Arif void tc_bl31_common_platform_setup(void) 1326ec0c65bSUsama Arif { 1336ec0c65bSUsama Arif arm_bl31_platform_setup(); 13425dd2172SMate Toth-Pal 1354eefbf1bSSandrine Bailleux #ifdef PLATFORM_TESTS 1364eefbf1bSSandrine Bailleux tc_run_platform_tests(); 1379b266556Slaurenw-arm #endif 1386ec0c65bSUsama Arif } 1396ec0c65bSUsama Arif 1406ec0c65bSUsama Arif const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) 1416ec0c65bSUsama Arif { 1426ec0c65bSUsama Arif return css_scmi_override_pm_ops(ops); 1436ec0c65bSUsama Arif } 14434a87d74SUsama Arif 14534a87d74SUsama Arif void __init bl31_plat_arch_setup(void) 14634a87d74SUsama Arif { 14734a87d74SUsama Arif arm_bl31_plat_arch_setup(); 14834a87d74SUsama Arif 14934a87d74SUsama Arif /* HW_CONFIG was also loaded by BL2 */ 15034a87d74SUsama Arif const struct dyn_cfg_dtb_info_t *hw_config_info; 15134a87d74SUsama Arif 15234a87d74SUsama Arif hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID); 15334a87d74SUsama Arif assert(hw_config_info != NULL); 15434a87d74SUsama Arif 15534a87d74SUsama Arif fconf_populate("HW_CONFIG", hw_config_info->config_addr); 15634a87d74SUsama Arif } 15728b2d86cSMadhukar Pappireddy 158fd51b215SGovindraj Raja #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0) 15928b2d86cSMadhukar Pappireddy void tc_bl31_plat_runtime_setup(void) 16028b2d86cSMadhukar Pappireddy { 16128b2d86cSMadhukar Pappireddy /* Start secure watchdog timer. */ 16228b2d86cSMadhukar Pappireddy plat_arm_secure_wdt_start(); 163c864af98SSalman Nabi 164c864af98SSalman Nabi arm_bl31_plat_runtime_setup(); 16528b2d86cSMadhukar Pappireddy } 16628b2d86cSMadhukar Pappireddy 16728b2d86cSMadhukar Pappireddy void bl31_plat_runtime_setup(void) 16828b2d86cSMadhukar Pappireddy { 16928b2d86cSMadhukar Pappireddy tc_bl31_plat_runtime_setup(); 17028b2d86cSMadhukar Pappireddy } 17128b2d86cSMadhukar Pappireddy 17228b2d86cSMadhukar Pappireddy /* 17328b2d86cSMadhukar Pappireddy * Platform handler for Group0 secure interrupt. 17428b2d86cSMadhukar Pappireddy */ 17528b2d86cSMadhukar Pappireddy int plat_spmd_handle_group0_interrupt(uint32_t intid) 17628b2d86cSMadhukar Pappireddy { 17728b2d86cSMadhukar Pappireddy /* Trusted Watchdog timer is the only source of Group0 interrupt now. */ 17828b2d86cSMadhukar Pappireddy if (intid == SBSA_SECURE_WDOG_INTID) { 17928b2d86cSMadhukar Pappireddy /* Refresh the timer. */ 18028b2d86cSMadhukar Pappireddy plat_arm_secure_wdt_refresh(); 18128b2d86cSMadhukar Pappireddy 18228b2d86cSMadhukar Pappireddy return 0; 18328b2d86cSMadhukar Pappireddy } 18428b2d86cSMadhukar Pappireddy 18528b2d86cSMadhukar Pappireddy return -1; 18628b2d86cSMadhukar Pappireddy } 187fd51b215SGovindraj Raja #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/ 188