blob: 0afc73bca2ec15f8ca4219fd0bcc57e1845a3ed2 [file] [log] [blame]
Usama Ariff5c58af2020-04-17 16:13:39 +01001/*
Boyan Karatotev638e4a92023-11-29 15:27:18 +00002 * Copyright (c) 2020-2024, Arm Limited and Contributors. All rights reserved.
Usama Ariff5c58af2020-04-17 16:13:39 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8
9#include <libfdt.h>
Usama Arif6ec0c652021-04-09 17:07:41 +010010#include <tc_plat.h>
Usama Ariff5c58af2020-04-17 16:13:39 +010011
Manish V Badarkhea8778182023-10-18 14:11:45 +010012#include <arch_helpers.h>
Usama Ariff5c58af2020-04-17 16:13:39 +010013#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 Pappireddy28b2d862023-03-22 15:40:40 -050017#include <drivers/arm/sbsa.h>
Usama Arif34a87d72021-08-17 17:57:10 +010018#include <lib/fconf/fconf.h>
19#include <lib/fconf/fconf_dyn_cfg_getter.h>
Usama Ariff5c58af2020-04-17 16:13:39 +010020#include <plat/arm/common/plat_arm.h>
21#include <plat/common/platform.h>
22
Manish V Badarkhed2ce6aa2023-12-06 09:16:08 +000023#ifdef PLATFORM_TEST_TFM_TESTSUITE
Manish V Badarkhea8778182023-10-18 14:11:45 +010024#include <psa/crypto_platform.h>
25#include <psa/crypto_types.h>
26#include <psa/crypto_values.h>
Manish V Badarkhed2ce6aa2023-12-06 09:16:08 +000027#endif /* PLATFORM_TEST_TFM_TESTSUITE */
Manish V Badarkhe22220e62024-12-15 18:26:15 +000028#include <psa/error.h>
Manish V Badarkhea8778182023-10-18 14:11:45 +010029
Leo Yan0328f342024-05-21 16:33:01 +000030#include <drivers/arm/rse_comms.h>
31#include <plat/common/platform.h>
Leo Yan0328f342024-05-21 16:33:01 +000032
Manish V Badarkhea8778182023-10-18 14:11:45 +010033#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 */
42psa_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 Yan4f65c0b2024-05-22 15:42:46 +010057#if TARGET_PLATFORM <= 2
Leo Yand2b1eb82024-05-22 15:41:37 +010058static 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 Ariff5c58af2020-04-17 16:13:39 +010064};
Jackson Cooper-Drivere8e1b602023-12-14 14:32:40 +000065#elif TARGET_PLATFORM >= 3
Leo Yan4f65c0b2024-05-22 15:42:46 +010066static 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-Drivere8e1b602023-12-14 14:32:40 +000073#endif
Jagdish Gediyaadc91a32023-12-18 05:56:00 +000074
Jackson Cooper-Drivere8e1b602023-12-14 14:32:40 +000075#if TARGET_PLATFORM == 3
Jagdish Gediyaadc91a32023-12-18 05:56:00 +000076static void enable_ns_mcn_pmu(void)
77{
78 /*
79 * Enable non-secure access to MCN PMU registers
80 */
81 for (int i = 0; i < MCN_INSTANCES; i++) {
82 uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET +
83 (i * MCN_ADDRESS_SPACE_SIZE);
84 mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
85 }
86}
Jagdish Gediyabb04d022024-01-11 10:49:46 +000087
88static void set_mcn_slc_alloc_mode(void)
89{
90 /*
91 * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
92 * 0b01 (always alloc), configure both to 0b10 (use bus signal
93 * attribute from interface).
94 */
95 for (int i = 0; i < MCN_INSTANCES; i++) {
96 uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR +
97 (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
98 uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR +
99 (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
100
101 mmio_clrsetbits_32(slccfg_ctl_ns,
102 (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
103 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
104 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
105 mmio_clrsetbits_32(slccfg_ctl_s,
106 (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
107 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
108 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
109 }
110}
Leo Yan4f65c0b2024-05-22 15:42:46 +0100111#endif
Usama Ariff5c58af2020-04-17 16:13:39 +0100112
113void bl31_platform_setup(void)
114{
Usama Arif6ec0c652021-04-09 17:07:41 +0100115 tc_bl31_common_platform_setup();
Jagdish Gediyaadc91a32023-12-18 05:56:00 +0000116#if TARGET_PLATFORM == 3
117 enable_ns_mcn_pmu();
Jagdish Gediyabb04d022024-01-11 10:49:46 +0000118 set_mcn_slc_alloc_mode();
Jagdish Gediya89c58a52024-02-02 06:01:44 +0000119 plat_arm_ni_setup(NCI_BASE_ADDR);
Jagdish Gediyaadc91a32023-12-18 05:56:00 +0000120#endif
Usama Ariff5c58af2020-04-17 16:13:39 +0100121}
122
Leo Yand2b1eb82024-05-22 15:41:37 +0100123scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
Usama Ariff5c58af2020-04-17 16:13:39 +0100124{
125
Leo Yand2b1eb82024-05-22 15:41:37 +0100126 return &tc_scmi_plat_info;
Usama Ariff5c58af2020-04-17 16:13:39 +0100127
128}
129
130void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
131 u_register_t arg2, u_register_t arg3)
132{
133 arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
Usama Arif34a87d72021-08-17 17:57:10 +0100134
135 /* Fill the properties struct with the info from the config dtb */
136 fconf_populate("FW_CONFIG", arg1);
Usama Ariff5c58af2020-04-17 16:13:39 +0100137}
138
laurenw-arm6fbe11c2023-05-04 14:55:37 -0500139#ifdef PLATFORM_TESTS
Sandrine Bailleux4eefbf12023-05-05 15:44:26 +0200140static __dead2 void tc_run_platform_tests(void)
141{
Sandrine Bailleux303ef332023-05-05 15:59:00 +0200142 int tests_failed;
143
144 printf("\nStarting platform tests...\n");
145
Tamas Ban657b90e2023-04-21 09:31:48 +0200146#ifdef PLATFORM_TEST_NV_COUNTERS
Sandrine Bailleux303ef332023-05-05 15:59:00 +0200147 tests_failed = nv_counter_test();
laurenw-arm00b7e0b2023-06-13 16:43:39 -0500148#elif PLATFORM_TEST_ROTPK
149 tests_failed = rotpk_test();
Tamas Ban657b90e2023-04-21 09:31:48 +0200150#elif PLATFORM_TEST_TFM_TESTSUITE
Sandrine Bailleux303ef332023-05-05 15:59:00 +0200151 tests_failed = run_platform_tests();
laurenw-arm1b076112023-02-07 13:40:05 -0600152#endif
Sandrine Bailleux303ef332023-05-05 15:59:00 +0200153
154 printf("Platform tests %s.\n",
155 (tests_failed != 0) ? "failed" : "succeeded");
156
Sandrine Bailleux57cc12c2023-05-05 13:59:07 +0200157 /* Suspend booting, no matter the tests outcome. */
Sandrine Bailleux303ef332023-05-05 15:59:00 +0200158 printf("Suspend booting...\n");
Mate Toth-Pal25dd2172022-10-21 14:24:49 +0200159 plat_error_handler(-1);
Sandrine Bailleux4eefbf12023-05-05 15:44:26 +0200160}
161#endif
162
Usama Arif6ec0c652021-04-09 17:07:41 +0100163void tc_bl31_common_platform_setup(void)
Usama Ariff5c58af2020-04-17 16:13:39 +0100164{
165 arm_bl31_platform_setup();
Mate Toth-Pal25dd2172022-10-21 14:24:49 +0200166
Sandrine Bailleux4eefbf12023-05-05 15:44:26 +0200167#ifdef PLATFORM_TESTS
168 tc_run_platform_tests();
laurenw-arm9b266552023-05-03 12:48:55 -0500169#endif
Usama Ariff5c58af2020-04-17 16:13:39 +0100170}
171
172const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
173{
174 return css_scmi_override_pm_ops(ops);
175}
Usama Arif34a87d72021-08-17 17:57:10 +0100176
177void __init bl31_plat_arch_setup(void)
178{
179 arm_bl31_plat_arch_setup();
180
181 /* HW_CONFIG was also loaded by BL2 */
182 const struct dyn_cfg_dtb_info_t *hw_config_info;
183
184 hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
185 assert(hw_config_info != NULL);
186
187 fconf_populate("HW_CONFIG", hw_config_info->config_addr);
188}
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500189
Govindraj Rajafd51b212023-05-10 14:50:36 -0500190#if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500191void tc_bl31_plat_runtime_setup(void)
192{
Icen.Zeyada8d4d1902024-12-17 12:25:55 +0000193 psa_status_t status;
194
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500195 /* Start secure watchdog timer. */
196 plat_arm_secure_wdt_start();
Salman Nabic864af92024-02-19 17:03:44 +0000197
198 arm_bl31_plat_runtime_setup();
Leo Yan0328f342024-05-21 16:33:01 +0000199
200 /* Initialise RSE communication channel */
201 status = rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE, PLAT_RSE_AP_RCV_MHU_BASE);
202 if (status != PSA_SUCCESS) {
203 ERROR("Failed to initialize RSE communication channel - psa_status = %d\n", status);
204 }
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500205}
206
207void bl31_plat_runtime_setup(void)
208{
209 tc_bl31_plat_runtime_setup();
210}
211
212/*
213 * Platform handler for Group0 secure interrupt.
214 */
215int plat_spmd_handle_group0_interrupt(uint32_t intid)
216{
217 /* Trusted Watchdog timer is the only source of Group0 interrupt now. */
218 if (intid == SBSA_SECURE_WDOG_INTID) {
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500219 /* Refresh the timer. */
220 plat_arm_secure_wdt_refresh();
221
Madhukar Pappireddy28b2d862023-03-22 15:40:40 -0500222 return 0;
223 }
224
225 return -1;
226}
Govindraj Rajafd51b212023-05-10 14:50:36 -0500227#endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/