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 */ 28*22220e69SManish V Badarkhe #include <psa/error.h> 29a8778185SManish V Badarkhe 300328f342SLeo Yan #include <drivers/arm/rse_comms.h> 310328f342SLeo Yan #include <plat/common/platform.h> 320328f342SLeo Yan 33a8778185SManish V Badarkhe #ifdef PLATFORM_TEST_TFM_TESTSUITE 34a8778185SManish V Badarkhe /* 35a8778185SManish V Badarkhe * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG 36a8778185SManish V Badarkhe * mbedTLS config option) so we need to provide an implementation of 37a8778185SManish V Badarkhe * mbedtls_psa_external_get_random(). Provide a fake one, since we do not 38a8778185SManish V Badarkhe * actually use any of external RNG and this function is only needed during 39a8778185SManish V Badarkhe * the execution of TF-M testsuite during exporting the public part of the 40a8778185SManish V Badarkhe * delegated attestation key. 41a8778185SManish V Badarkhe */ 42a8778185SManish V Badarkhe psa_status_t mbedtls_psa_external_get_random( 43a8778185SManish V Badarkhe mbedtls_psa_external_random_context_t *context, 44a8778185SManish V Badarkhe uint8_t *output, size_t output_size, 45a8778185SManish V Badarkhe size_t *output_length) 46a8778185SManish V Badarkhe { 47a8778185SManish V Badarkhe for (size_t i = 0U; i < output_size; i++) { 48a8778185SManish V Badarkhe output[i] = (uint8_t)(read_cntpct_el0() & 0xFFU); 49a8778185SManish V Badarkhe } 50a8778185SManish V Badarkhe 51a8778185SManish V Badarkhe *output_length = output_size; 52a8778185SManish V Badarkhe 53a8778185SManish V Badarkhe return PSA_SUCCESS; 54a8778185SManish V Badarkhe } 55a8778185SManish V Badarkhe #endif /* PLATFORM_TEST_TFM_TESTSUITE */ 56a8778185SManish V Badarkhe 574f65c0beSLeo Yan #if TARGET_PLATFORM <= 2 58d2b1eb80SLeo Yan static scmi_channel_plat_info_t tc_scmi_plat_info = { 596ec0c65bSUsama Arif .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE, 606ec0c65bSUsama Arif .db_reg_addr = PLAT_CSS_MHU_BASE + SENDER_REG_SET(0), 616ec0c65bSUsama Arif .db_preserve_mask = 0xfffffffe, 626ec0c65bSUsama Arif .db_modify_mask = 0x1, 636ec0c65bSUsama Arif .ring_doorbell = &mhuv2_ring_doorbell, 646ec0c65bSUsama Arif }; 65e8e1b608SJackson Cooper-Driver #elif TARGET_PLATFORM >= 3 664f65c0beSLeo Yan static scmi_channel_plat_info_t tc_scmi_plat_info = { 674f65c0beSLeo Yan .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE, 684f65c0beSLeo Yan .db_reg_addr = PLAT_CSS_MHU_BASE + MHU_V3_SENDER_REG_SET(0), 694f65c0beSLeo Yan .db_preserve_mask = 0xfffffffe, 704f65c0beSLeo Yan .db_modify_mask = 0x1, 714f65c0beSLeo Yan .ring_doorbell = &mhu_ring_doorbell, 724f65c0beSLeo Yan }; 73e8e1b608SJackson Cooper-Driver #endif 74adc91a34SJagdish Gediya 75e8e1b608SJackson Cooper-Driver #if TARGET_PLATFORM == 3 76adc91a34SJagdish Gediya static void enable_ns_mcn_pmu(void) 77adc91a34SJagdish Gediya { 78adc91a34SJagdish Gediya /* 79adc91a34SJagdish Gediya * Enable non-secure access to MCN PMU registers 80adc91a34SJagdish Gediya */ 81adc91a34SJagdish Gediya for (int i = 0; i < MCN_INSTANCES; i++) { 82adc91a34SJagdish Gediya uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET + 83adc91a34SJagdish Gediya (i * MCN_ADDRESS_SPACE_SIZE); 84adc91a34SJagdish Gediya mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT); 85adc91a34SJagdish Gediya } 86adc91a34SJagdish Gediya } 87bb04d023SJagdish Gediya 88bb04d023SJagdish Gediya static void set_mcn_slc_alloc_mode(void) 89bb04d023SJagdish Gediya { 90bb04d023SJagdish Gediya /* 91bb04d023SJagdish Gediya * SLC WRALLOCMODE and RDALLOCMODE are configured by default to 92bb04d023SJagdish Gediya * 0b01 (always alloc), configure both to 0b10 (use bus signal 93bb04d023SJagdish Gediya * attribute from interface). 94bb04d023SJagdish Gediya */ 95bb04d023SJagdish Gediya for (int i = 0; i < MCN_INSTANCES; i++) { 96bb04d023SJagdish Gediya uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR + 97bb04d023SJagdish Gediya (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET; 98bb04d023SJagdish Gediya uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR + 99bb04d023SJagdish Gediya (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET; 100bb04d023SJagdish Gediya 101bb04d023SJagdish Gediya mmio_clrsetbits_32(slccfg_ctl_ns, 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 mmio_clrsetbits_32(slccfg_ctl_s, 106bb04d023SJagdish Gediya (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK), 107bb04d023SJagdish Gediya (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) | 108bb04d023SJagdish Gediya (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT)); 109bb04d023SJagdish Gediya } 110bb04d023SJagdish Gediya } 1114f65c0beSLeo Yan #endif 1126ec0c65bSUsama Arif 1136ec0c65bSUsama Arif void bl31_platform_setup(void) 1146ec0c65bSUsama Arif { 1156ec0c65bSUsama Arif tc_bl31_common_platform_setup(); 116adc91a34SJagdish Gediya #if TARGET_PLATFORM == 3 117adc91a34SJagdish Gediya enable_ns_mcn_pmu(); 118bb04d023SJagdish Gediya set_mcn_slc_alloc_mode(); 11989c58a50SJagdish Gediya plat_arm_ni_setup(NCI_BASE_ADDR); 120adc91a34SJagdish Gediya #endif 1216ec0c65bSUsama Arif } 1226ec0c65bSUsama Arif 123d2b1eb80SLeo Yan scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused) 1246ec0c65bSUsama Arif { 1256ec0c65bSUsama Arif 126d2b1eb80SLeo Yan return &tc_scmi_plat_info; 1276ec0c65bSUsama Arif 1286ec0c65bSUsama Arif } 1296ec0c65bSUsama Arif 1306ec0c65bSUsama Arif void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, 1316ec0c65bSUsama Arif u_register_t arg2, u_register_t arg3) 1326ec0c65bSUsama Arif { 1336ec0c65bSUsama Arif arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3); 13434a87d74SUsama Arif 13534a87d74SUsama Arif /* Fill the properties struct with the info from the config dtb */ 13634a87d74SUsama Arif fconf_populate("FW_CONFIG", arg1); 1376ec0c65bSUsama Arif } 1386ec0c65bSUsama Arif 1396fbe11cdSlaurenw-arm #ifdef PLATFORM_TESTS 1404eefbf1bSSandrine Bailleux static __dead2 void tc_run_platform_tests(void) 1414eefbf1bSSandrine Bailleux { 142303ef33eSSandrine Bailleux int tests_failed; 143303ef33eSSandrine Bailleux 144303ef33eSSandrine Bailleux printf("\nStarting platform tests...\n"); 145303ef33eSSandrine Bailleux 146657b90eaSTamas Ban #ifdef PLATFORM_TEST_NV_COUNTERS 147303ef33eSSandrine Bailleux tests_failed = nv_counter_test(); 14800b7e0bfSlaurenw-arm #elif PLATFORM_TEST_ROTPK 14900b7e0bfSlaurenw-arm tests_failed = rotpk_test(); 150657b90eaSTamas Ban #elif PLATFORM_TEST_TFM_TESTSUITE 151303ef33eSSandrine Bailleux tests_failed = run_platform_tests(); 1521b076113Slaurenw-arm #endif 153303ef33eSSandrine Bailleux 154303ef33eSSandrine Bailleux printf("Platform tests %s.\n", 155303ef33eSSandrine Bailleux (tests_failed != 0) ? "failed" : "succeeded"); 156303ef33eSSandrine Bailleux 15757cc12c8SSandrine Bailleux /* Suspend booting, no matter the tests outcome. */ 158303ef33eSSandrine Bailleux printf("Suspend booting...\n"); 15925dd2172SMate Toth-Pal plat_error_handler(-1); 1604eefbf1bSSandrine Bailleux } 1614eefbf1bSSandrine Bailleux #endif 1624eefbf1bSSandrine Bailleux 1636ec0c65bSUsama Arif void tc_bl31_common_platform_setup(void) 1646ec0c65bSUsama Arif { 1656ec0c65bSUsama Arif arm_bl31_platform_setup(); 16625dd2172SMate Toth-Pal 1674eefbf1bSSandrine Bailleux #ifdef PLATFORM_TESTS 1684eefbf1bSSandrine Bailleux tc_run_platform_tests(); 1699b266556Slaurenw-arm #endif 1706ec0c65bSUsama Arif } 1716ec0c65bSUsama Arif 1726ec0c65bSUsama Arif const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) 1736ec0c65bSUsama Arif { 1746ec0c65bSUsama Arif return css_scmi_override_pm_ops(ops); 1756ec0c65bSUsama Arif } 17634a87d74SUsama Arif 17734a87d74SUsama Arif void __init bl31_plat_arch_setup(void) 17834a87d74SUsama Arif { 17934a87d74SUsama Arif arm_bl31_plat_arch_setup(); 18034a87d74SUsama Arif 18134a87d74SUsama Arif /* HW_CONFIG was also loaded by BL2 */ 18234a87d74SUsama Arif const struct dyn_cfg_dtb_info_t *hw_config_info; 18334a87d74SUsama Arif 18434a87d74SUsama Arif hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID); 18534a87d74SUsama Arif assert(hw_config_info != NULL); 18634a87d74SUsama Arif 18734a87d74SUsama Arif fconf_populate("HW_CONFIG", hw_config_info->config_addr); 18834a87d74SUsama Arif } 18928b2d86cSMadhukar Pappireddy 190fd51b215SGovindraj Raja #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0) 19128b2d86cSMadhukar Pappireddy void tc_bl31_plat_runtime_setup(void) 19228b2d86cSMadhukar Pappireddy { 19328b2d86cSMadhukar Pappireddy /* Start secure watchdog timer. */ 19428b2d86cSMadhukar Pappireddy plat_arm_secure_wdt_start(); 195c864af98SSalman Nabi 196c864af98SSalman Nabi arm_bl31_plat_runtime_setup(); 1970328f342SLeo Yan 1980328f342SLeo Yan /* Initialise RSE communication channel */ 1990328f342SLeo Yan status = rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE, PLAT_RSE_AP_RCV_MHU_BASE); 2000328f342SLeo Yan if (status != PSA_SUCCESS) { 2010328f342SLeo Yan ERROR("Failed to initialize RSE communication channel - psa_status = %d\n", status); 2020328f342SLeo Yan } 20328b2d86cSMadhukar Pappireddy } 20428b2d86cSMadhukar Pappireddy 20528b2d86cSMadhukar Pappireddy void bl31_plat_runtime_setup(void) 20628b2d86cSMadhukar Pappireddy { 20728b2d86cSMadhukar Pappireddy tc_bl31_plat_runtime_setup(); 20828b2d86cSMadhukar Pappireddy } 20928b2d86cSMadhukar Pappireddy 21028b2d86cSMadhukar Pappireddy /* 21128b2d86cSMadhukar Pappireddy * Platform handler for Group0 secure interrupt. 21228b2d86cSMadhukar Pappireddy */ 21328b2d86cSMadhukar Pappireddy int plat_spmd_handle_group0_interrupt(uint32_t intid) 21428b2d86cSMadhukar Pappireddy { 21528b2d86cSMadhukar Pappireddy /* Trusted Watchdog timer is the only source of Group0 interrupt now. */ 21628b2d86cSMadhukar Pappireddy if (intid == SBSA_SECURE_WDOG_INTID) { 21728b2d86cSMadhukar Pappireddy /* Refresh the timer. */ 21828b2d86cSMadhukar Pappireddy plat_arm_secure_wdt_refresh(); 21928b2d86cSMadhukar Pappireddy 22028b2d86cSMadhukar Pappireddy return 0; 22128b2d86cSMadhukar Pappireddy } 22228b2d86cSMadhukar Pappireddy 22328b2d86cSMadhukar Pappireddy return -1; 22428b2d86cSMadhukar Pappireddy } 225fd51b215SGovindraj Raja #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/ 226