Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | 9b0ccd7 | 2024-10-04 18:49:35 +0200 | [diff] [blame] | 2 | * Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved. |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | |
| 9 | #include <libfdt.h> |
Usama Arif | f151362 | 2021-04-09 17:07:41 +0100 | [diff] [blame] | 10 | #include <tc_plat.h> |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 11 | |
Manish V Badarkhe | b241efb | 2023-10-18 14:11:45 +0100 | [diff] [blame] | 12 | #include <arch_helpers.h> |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 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> |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 17 | #include <drivers/arm/sbsa.h> |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 18 | #include <lib/fconf/fconf.h> |
| 19 | #include <lib/fconf/fconf_dyn_cfg_getter.h> |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 20 | #include <plat/arm/common/plat_arm.h> |
| 21 | #include <plat/common/platform.h> |
| 22 | |
Manish V Badarkhe | b20ca82 | 2023-12-06 09:16:08 +0000 | [diff] [blame] | 23 | #ifdef PLATFORM_TEST_TFM_TESTSUITE |
Manish V Badarkhe | b241efb | 2023-10-18 14:11:45 +0100 | [diff] [blame] | 24 | #include <psa/crypto_platform.h> |
| 25 | #include <psa/crypto_types.h> |
| 26 | #include <psa/crypto_values.h> |
Manish V Badarkhe | b20ca82 | 2023-12-06 09:16:08 +0000 | [diff] [blame] | 27 | #endif /* PLATFORM_TEST_TFM_TESTSUITE */ |
Manish V Badarkhe | 2bdcb15 | 2024-12-15 18:26:15 +0000 | [diff] [blame] | 28 | #include <psa/error.h> |
Manish V Badarkhe | b241efb | 2023-10-18 14:11:45 +0100 | [diff] [blame] | 29 | |
Leo Yan | b0bfa5b | 2024-05-21 16:33:01 +0000 | [diff] [blame] | 30 | #include <plat/common/platform.h> |
Yann Gautier | 9b0ccd7 | 2024-10-04 18:49:35 +0200 | [diff] [blame] | 31 | #include <tc_rse_comms.h> |
Leo Yan | b0bfa5b | 2024-05-21 16:33:01 +0000 | [diff] [blame] | 32 | |
Manish V Badarkhe | b241efb | 2023-10-18 14:11:45 +0100 | [diff] [blame] | 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 | |
Leo Yan | 7d51bf8 | 2024-05-22 15:42:46 +0100 | [diff] [blame] | 57 | #if TARGET_PLATFORM <= 2 |
Leo Yan | 8dd7d43 | 2024-05-22 15:41:37 +0100 | [diff] [blame] | 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, |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 64 | }; |
Jackson Cooper-Driver | 3653ded | 2023-12-14 14:32:40 +0000 | [diff] [blame] | 65 | #elif TARGET_PLATFORM >= 3 |
Leo Yan | 7d51bf8 | 2024-05-22 15:42:46 +0100 | [diff] [blame] | 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 | }; |
Jackson Cooper-Driver | 3653ded | 2023-12-14 14:32:40 +0000 | [diff] [blame] | 73 | #endif |
Jagdish Gediya | 5ae7c38 | 2023-12-18 05:56:00 +0000 | [diff] [blame] | 74 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 75 | /* the bottom 3 AMU group 1 counters */ |
| 76 | #define MPMM_GEARS ((1 << 0) | (1 << 1) | (1 << 2)) |
| 77 | |
| 78 | uint16_t plat_amu_aux_enables[PLATFORM_CORE_COUNT] = { |
| 79 | MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, |
| 80 | MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, |
| 81 | #if PLATFORM_CORE_COUNT == 14 |
| 82 | MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, |
| 83 | MPMM_GEARS, MPMM_GEARS |
| 84 | #endif |
| 85 | }; |
| 86 | |
Jagdish Gediya | 3517343 | 2024-06-19 08:57:47 +0000 | [diff] [blame] | 87 | #if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) |
Jagdish Gediya | 5ae7c38 | 2023-12-18 05:56:00 +0000 | [diff] [blame] | 88 | static void enable_ns_mcn_pmu(void) |
| 89 | { |
| 90 | /* |
| 91 | * Enable non-secure access to MCN PMU registers |
| 92 | */ |
| 93 | for (int i = 0; i < MCN_INSTANCES; i++) { |
Jagdish Gediya | 5e38659 | 2024-06-19 08:50:45 +0000 | [diff] [blame] | 94 | uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR(i) + |
| 95 | MCN_SCR_OFFSET; |
Jagdish Gediya | 5ae7c38 | 2023-12-18 05:56:00 +0000 | [diff] [blame] | 96 | mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT); |
| 97 | } |
| 98 | } |
Jagdish Gediya | 3517343 | 2024-06-19 08:57:47 +0000 | [diff] [blame] | 99 | #endif /* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */ |
Jagdish Gediya | ebaa6bf | 2024-01-11 10:49:46 +0000 | [diff] [blame] | 100 | |
Jagdish Gediya | 3517343 | 2024-06-19 08:57:47 +0000 | [diff] [blame] | 101 | #if TARGET_PLATFORM == 3 |
Jagdish Gediya | ebaa6bf | 2024-01-11 10:49:46 +0000 | [diff] [blame] | 102 | static void set_mcn_slc_alloc_mode(void) |
| 103 | { |
| 104 | /* |
| 105 | * SLC WRALLOCMODE and RDALLOCMODE are configured by default to |
| 106 | * 0b01 (always alloc), configure both to 0b10 (use bus signal |
| 107 | * attribute from interface). |
| 108 | */ |
| 109 | for (int i = 0; i < MCN_INSTANCES; i++) { |
Jagdish Gediya | 5e38659 | 2024-06-19 08:50:45 +0000 | [diff] [blame] | 110 | uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR(i) + |
| 111 | MPAM_SLCCFG_CTL_OFFSET; |
| 112 | uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR(i) + |
| 113 | MPAM_SLCCFG_CTL_OFFSET; |
Jagdish Gediya | ebaa6bf | 2024-01-11 10:49:46 +0000 | [diff] [blame] | 114 | |
| 115 | mmio_clrsetbits_32(slccfg_ctl_ns, |
| 116 | (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK), |
| 117 | (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) | |
| 118 | (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT)); |
| 119 | mmio_clrsetbits_32(slccfg_ctl_s, |
| 120 | (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK), |
| 121 | (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) | |
| 122 | (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT)); |
| 123 | } |
| 124 | } |
Leo Yan | 7d51bf8 | 2024-05-22 15:42:46 +0100 | [diff] [blame] | 125 | #endif |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 126 | |
| 127 | void bl31_platform_setup(void) |
| 128 | { |
Leo Yan | 01c7243 | 2024-05-31 13:57:36 +0100 | [diff] [blame] | 129 | psa_status_t status; |
| 130 | |
Usama Arif | f151362 | 2021-04-09 17:07:41 +0100 | [diff] [blame] | 131 | tc_bl31_common_platform_setup(); |
Jagdish Gediya | 3517343 | 2024-06-19 08:57:47 +0000 | [diff] [blame] | 132 | #if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) |
Jagdish Gediya | 5ae7c38 | 2023-12-18 05:56:00 +0000 | [diff] [blame] | 133 | enable_ns_mcn_pmu(); |
Jagdish Gediya | 3517343 | 2024-06-19 08:57:47 +0000 | [diff] [blame] | 134 | #endif /* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */ |
| 135 | #if TARGET_PLATFORM == 3 |
Jagdish Gediya | ebaa6bf | 2024-01-11 10:49:46 +0000 | [diff] [blame] | 136 | set_mcn_slc_alloc_mode(); |
Jagdish Gediya | 16a0f1c | 2024-02-02 06:01:44 +0000 | [diff] [blame] | 137 | plat_arm_ni_setup(NCI_BASE_ADDR); |
Jagdish Gediya | 5ae7c38 | 2023-12-18 05:56:00 +0000 | [diff] [blame] | 138 | #endif |
Leo Yan | 01c7243 | 2024-05-31 13:57:36 +0100 | [diff] [blame] | 139 | |
| 140 | /* Initialise RSE communication channel */ |
Yann Gautier | 9b0ccd7 | 2024-10-04 18:49:35 +0200 | [diff] [blame] | 141 | status = plat_rse_comms_init(); |
Leo Yan | 01c7243 | 2024-05-31 13:57:36 +0100 | [diff] [blame] | 142 | if (status != PSA_SUCCESS) { |
| 143 | ERROR("Failed to initialize RSE communication channel - psa_status = %d\n", status); |
| 144 | } |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Leo Yan | 8dd7d43 | 2024-05-22 15:41:37 +0100 | [diff] [blame] | 147 | scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused) |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 148 | { |
| 149 | |
Leo Yan | 8dd7d43 | 2024-05-22 15:41:37 +0100 | [diff] [blame] | 150 | return &tc_scmi_plat_info; |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 151 | |
| 152 | } |
| 153 | |
| 154 | void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1, |
| 155 | u_register_t arg2, u_register_t arg3) |
| 156 | { |
Jayanth Dodderi Chidanand | 14db0d5 | 2025-03-20 12:06:08 +0000 | [diff] [blame] | 157 | /* Initialize the console to provide early debug support */ |
| 158 | arm_console_boot_init(); |
| 159 | |
| 160 | arm_bl31_early_platform_setup(arg0, arg1, arg2, arg3); |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 161 | |
Jayanth Dodderi Chidanand | cdb732c | 2025-03-01 18:17:23 +0000 | [diff] [blame^] | 162 | #if !TRANSFER_LIST |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 163 | /* Fill the properties struct with the info from the config dtb */ |
| 164 | fconf_populate("FW_CONFIG", arg1); |
Jayanth Dodderi Chidanand | cdb732c | 2025-03-01 18:17:23 +0000 | [diff] [blame^] | 165 | #endif |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 166 | } |
| 167 | |
laurenw-arm | 4c4181c | 2023-05-04 14:55:37 -0500 | [diff] [blame] | 168 | #ifdef PLATFORM_TESTS |
Sandrine Bailleux | 27fba52 | 2023-05-05 15:44:26 +0200 | [diff] [blame] | 169 | static __dead2 void tc_run_platform_tests(void) |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 170 | { |
Sandrine Bailleux | f0f42fb | 2023-05-05 15:59:00 +0200 | [diff] [blame] | 171 | int tests_failed; |
| 172 | |
| 173 | printf("\nStarting platform tests...\n"); |
Mate Toth-Pal | 14ba4af | 2022-10-21 14:24:49 +0200 | [diff] [blame] | 174 | |
Tamas Ban | 15b79da | 2023-04-21 09:31:48 +0200 | [diff] [blame] | 175 | #ifdef PLATFORM_TEST_NV_COUNTERS |
Sandrine Bailleux | f0f42fb | 2023-05-05 15:59:00 +0200 | [diff] [blame] | 176 | tests_failed = nv_counter_test(); |
laurenw-arm | 116f10c | 2023-06-13 16:43:39 -0500 | [diff] [blame] | 177 | #elif PLATFORM_TEST_ROTPK |
| 178 | tests_failed = rotpk_test(); |
Tamas Ban | 15b79da | 2023-04-21 09:31:48 +0200 | [diff] [blame] | 179 | #elif PLATFORM_TEST_TFM_TESTSUITE |
Sandrine Bailleux | f0f42fb | 2023-05-05 15:59:00 +0200 | [diff] [blame] | 180 | tests_failed = run_platform_tests(); |
laurenw-arm | 2ce1e35 | 2023-02-07 13:40:05 -0600 | [diff] [blame] | 181 | #endif |
Sandrine Bailleux | f0f42fb | 2023-05-05 15:59:00 +0200 | [diff] [blame] | 182 | |
| 183 | printf("Platform tests %s.\n", |
| 184 | (tests_failed != 0) ? "failed" : "succeeded"); |
| 185 | |
Sandrine Bailleux | e1da6c4 | 2023-05-05 13:59:07 +0200 | [diff] [blame] | 186 | /* Suspend booting, no matter the tests outcome. */ |
Sandrine Bailleux | f0f42fb | 2023-05-05 15:59:00 +0200 | [diff] [blame] | 187 | printf("Suspend booting...\n"); |
Mate Toth-Pal | 14ba4af | 2022-10-21 14:24:49 +0200 | [diff] [blame] | 188 | plat_error_handler(-1); |
Sandrine Bailleux | 27fba52 | 2023-05-05 15:44:26 +0200 | [diff] [blame] | 189 | } |
| 190 | #endif |
| 191 | |
| 192 | void tc_bl31_common_platform_setup(void) |
| 193 | { |
| 194 | arm_bl31_platform_setup(); |
| 195 | |
| 196 | #ifdef PLATFORM_TESTS |
| 197 | tc_run_platform_tests(); |
laurenw-arm | 481ac28 | 2023-05-03 12:48:55 -0500 | [diff] [blame] | 198 | #endif |
Usama Arif | bec5afd | 2020-04-17 16:13:39 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops) |
| 202 | { |
| 203 | return css_scmi_override_pm_ops(ops); |
| 204 | } |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 205 | |
| 206 | void __init bl31_plat_arch_setup(void) |
| 207 | { |
| 208 | arm_bl31_plat_arch_setup(); |
| 209 | |
Jayanth Dodderi Chidanand | cdb732c | 2025-03-01 18:17:23 +0000 | [diff] [blame^] | 210 | /* |
| 211 | * When TRANSFER_LIST is enabled, HW_CONFIG is included in Transfer List |
| 212 | * as an entry with the tag TL_TAG_FDT. In this case, the configuration |
| 213 | * is already available, so the fconf_populate mechanism is not needed. |
| 214 | * The code block below is only required when TRANSFER_LIST is not used. |
| 215 | */ |
| 216 | #if !TRANSFER_LIST |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 217 | /* HW_CONFIG was also loaded by BL2 */ |
| 218 | const struct dyn_cfg_dtb_info_t *hw_config_info; |
| 219 | |
| 220 | hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID); |
| 221 | assert(hw_config_info != NULL); |
| 222 | |
| 223 | fconf_populate("HW_CONFIG", hw_config_info->config_addr); |
Jayanth Dodderi Chidanand | cdb732c | 2025-03-01 18:17:23 +0000 | [diff] [blame^] | 224 | #endif |
Usama Arif | a49bd49 | 2021-08-17 17:57:10 +0100 | [diff] [blame] | 225 | } |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 226 | |
Govindraj Raja | 436ea5e | 2023-05-10 14:50:36 -0500 | [diff] [blame] | 227 | #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0) |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 228 | void tc_bl31_plat_runtime_setup(void) |
| 229 | { |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 230 | /* Start secure watchdog timer. */ |
| 231 | plat_arm_secure_wdt_start(); |
Salman Nabi | 442b075 | 2024-02-19 17:03:44 +0000 | [diff] [blame] | 232 | |
| 233 | arm_bl31_plat_runtime_setup(); |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void bl31_plat_runtime_setup(void) |
| 237 | { |
| 238 | tc_bl31_plat_runtime_setup(); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Platform handler for Group0 secure interrupt. |
| 243 | */ |
| 244 | int plat_spmd_handle_group0_interrupt(uint32_t intid) |
| 245 | { |
| 246 | /* Trusted Watchdog timer is the only source of Group0 interrupt now. */ |
| 247 | if (intid == SBSA_SECURE_WDOG_INTID) { |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 248 | /* Refresh the timer. */ |
| 249 | plat_arm_secure_wdt_refresh(); |
| 250 | |
Madhukar Pappireddy | e108df2 | 2023-03-22 15:40:40 -0500 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | return -1; |
| 255 | } |
Govindraj Raja | 436ea5e | 2023-05-10 14:50:36 -0500 | [diff] [blame] | 256 | #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/ |