blob: 073e487090d4e547fc7112275b89298c11ad9423 [file] [log] [blame]
Usama Arifbec5afd2020-04-17 16:13:39 +01001/*
Yann Gautier9b0ccd72024-10-04 18:49:35 +02002 * Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
Usama Arifbec5afd2020-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 Ariff1513622021-04-09 17:07:41 +010010#include <tc_plat.h>
Usama Arifbec5afd2020-04-17 16:13:39 +010011
Manish V Badarkheb241efb2023-10-18 14:11:45 +010012#include <arch_helpers.h>
Usama Arifbec5afd2020-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>
Arvind Ram Prakash25ab5a12025-05-14 15:29:49 -050017#include <drivers/arm/dsu.h>
Madhukar Pappireddye108df22023-03-22 15:40:40 -050018#include <drivers/arm/sbsa.h>
Usama Arifa49bd492021-08-17 17:57:10 +010019#include <lib/fconf/fconf.h>
20#include <lib/fconf/fconf_dyn_cfg_getter.h>
Usama Arifbec5afd2020-04-17 16:13:39 +010021#include <plat/arm/common/plat_arm.h>
22#include <plat/common/platform.h>
23
Manish V Badarkheb20ca822023-12-06 09:16:08 +000024#ifdef PLATFORM_TEST_TFM_TESTSUITE
Manish V Badarkheb241efb2023-10-18 14:11:45 +010025#include <psa/crypto_platform.h>
26#include <psa/crypto_types.h>
27#include <psa/crypto_values.h>
Manish V Badarkheb20ca822023-12-06 09:16:08 +000028#endif /* PLATFORM_TEST_TFM_TESTSUITE */
Manish V Badarkhe2bdcb152024-12-15 18:26:15 +000029#include <psa/error.h>
Manish V Badarkheb241efb2023-10-18 14:11:45 +010030
Leo Yanb0bfa5b2024-05-21 16:33:01 +000031#include <plat/common/platform.h>
Yann Gautier9b0ccd72024-10-04 18:49:35 +020032#include <tc_rse_comms.h>
Leo Yanb0bfa5b2024-05-21 16:33:01 +000033
Manish V Badarkheb241efb2023-10-18 14:11:45 +010034#ifdef PLATFORM_TEST_TFM_TESTSUITE
35/*
36 * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
37 * mbedTLS config option) so we need to provide an implementation of
38 * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
39 * actually use any of external RNG and this function is only needed during
40 * the execution of TF-M testsuite during exporting the public part of the
41 * delegated attestation key.
42 */
43psa_status_t mbedtls_psa_external_get_random(
44 mbedtls_psa_external_random_context_t *context,
45 uint8_t *output, size_t output_size,
46 size_t *output_length)
47{
48 for (size_t i = 0U; i < output_size; i++) {
49 output[i] = (uint8_t)(read_cntpct_el0() & 0xFFU);
50 }
51
52 *output_length = output_size;
53
54 return PSA_SUCCESS;
55}
56#endif /* PLATFORM_TEST_TFM_TESTSUITE */
57
Leo Yan8dd7d432024-05-22 15:41:37 +010058static scmi_channel_plat_info_t tc_scmi_plat_info = {
59 .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
Leo Yan7d51bf82024-05-22 15:42:46 +010060 .db_reg_addr = PLAT_CSS_MHU_BASE + MHU_V3_SENDER_REG_SET(0),
61 .db_preserve_mask = 0xfffffffe,
62 .db_modify_mask = 0x1,
63 .ring_doorbell = &mhu_ring_doorbell,
64};
Jagdish Gediya5ae7c382023-12-18 05:56:00 +000065
Boyan Karatotevb2953472024-11-06 14:55:35 +000066/* the bottom 3 AMU group 1 counters */
67#define MPMM_GEARS ((1 << 0) | (1 << 1) | (1 << 2))
68
69uint16_t plat_amu_aux_enables[PLATFORM_CORE_COUNT] = {
70 MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS,
71 MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS,
72#if PLATFORM_CORE_COUNT == 14
73 MPMM_GEARS, MPMM_GEARS, MPMM_GEARS, MPMM_GEARS,
74 MPMM_GEARS, MPMM_GEARS
75#endif
76};
77
Arvind Ram Prakash25ab5a12025-05-14 15:29:49 -050078const dsu_driver_data_t plat_dsu_data = {
79 .clusterpwrdwn_pwrdn = false,
80 .clusterpwrdwn_memret = false,
81 .clusterpwrctlr_cachepwr = CLUSTERPWRCTLR_CACHEPWR_RESET,
82 .clusterpwrctlr_funcret = CLUSTERPWRCTLR_FUNCRET_RESET
83};
84
Jagdish Gediya35173432024-06-19 08:57:47 +000085#if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4)
Jagdish Gediya5ae7c382023-12-18 05:56:00 +000086static void enable_ns_mcn_pmu(void)
87{
88 /*
89 * Enable non-secure access to MCN PMU registers
90 */
91 for (int i = 0; i < MCN_INSTANCES; i++) {
Jagdish Gediya5e386592024-06-19 08:50:45 +000092 uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR(i) +
93 MCN_SCR_OFFSET;
Jagdish Gediya5ae7c382023-12-18 05:56:00 +000094 mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
95 }
96}
Jagdish Gediya35173432024-06-19 08:57:47 +000097#endif /* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */
Jagdish Gediyaebaa6bf2024-01-11 10:49:46 +000098
Jagdish Gediya35173432024-06-19 08:57:47 +000099#if TARGET_PLATFORM == 3
Jagdish Gediyaebaa6bf2024-01-11 10:49:46 +0000100static void set_mcn_slc_alloc_mode(void)
101{
102 /*
103 * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
104 * 0b01 (always alloc), configure both to 0b10 (use bus signal
105 * attribute from interface).
106 */
107 for (int i = 0; i < MCN_INSTANCES; i++) {
Jagdish Gediya5e386592024-06-19 08:50:45 +0000108 uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR(i) +
109 MPAM_SLCCFG_CTL_OFFSET;
110 uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR(i) +
111 MPAM_SLCCFG_CTL_OFFSET;
Jagdish Gediyaebaa6bf2024-01-11 10:49:46 +0000112
113 mmio_clrsetbits_32(slccfg_ctl_ns,
114 (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
115 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
116 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
117 mmio_clrsetbits_32(slccfg_ctl_s,
118 (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
119 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
120 (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
121 }
122}
Leo Yan7d51bf82024-05-22 15:42:46 +0100123#endif
Usama Arifbec5afd2020-04-17 16:13:39 +0100124
125void bl31_platform_setup(void)
126{
Leo Yan01c72432024-05-31 13:57:36 +0100127 psa_status_t status;
128
Usama Ariff1513622021-04-09 17:07:41 +0100129 tc_bl31_common_platform_setup();
Jagdish Gediya35173432024-06-19 08:57:47 +0000130#if (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4)
Jagdish Gediya5ae7c382023-12-18 05:56:00 +0000131 enable_ns_mcn_pmu();
Jagdish Gediya35173432024-06-19 08:57:47 +0000132#endif /* (TARGET_PLATFORM == 3) || (TARGET_PLATFORM == 4) */
133#if TARGET_PLATFORM == 3
Jagdish Gediyaebaa6bf2024-01-11 10:49:46 +0000134 set_mcn_slc_alloc_mode();
Jagdish Gediya16a0f1c2024-02-02 06:01:44 +0000135 plat_arm_ni_setup(NCI_BASE_ADDR);
Jagdish Gediya5ae7c382023-12-18 05:56:00 +0000136#endif
Leo Yan01c72432024-05-31 13:57:36 +0100137
138 /* Initialise RSE communication channel */
Yann Gautier9b0ccd72024-10-04 18:49:35 +0200139 status = plat_rse_comms_init();
Leo Yan01c72432024-05-31 13:57:36 +0100140 if (status != PSA_SUCCESS) {
141 ERROR("Failed to initialize RSE communication channel - psa_status = %d\n", status);
142 }
Usama Arifbec5afd2020-04-17 16:13:39 +0100143}
144
Leo Yan8dd7d432024-05-22 15:41:37 +0100145scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
Usama Arifbec5afd2020-04-17 16:13:39 +0100146{
147
Leo Yan8dd7d432024-05-22 15:41:37 +0100148 return &tc_scmi_plat_info;
Usama Arifbec5afd2020-04-17 16:13:39 +0100149
150}
151
152void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
153 u_register_t arg2, u_register_t arg3)
154{
Jayanth Dodderi Chidanand14db0d52025-03-20 12:06:08 +0000155 /* Initialize the console to provide early debug support */
156 arm_console_boot_init();
157
158 arm_bl31_early_platform_setup(arg0, arg1, arg2, arg3);
Usama Arifa49bd492021-08-17 17:57:10 +0100159
Jayanth Dodderi Chidanandcdb732c2025-03-01 18:17:23 +0000160#if !TRANSFER_LIST
Usama Arifa49bd492021-08-17 17:57:10 +0100161 /* Fill the properties struct with the info from the config dtb */
162 fconf_populate("FW_CONFIG", arg1);
Jayanth Dodderi Chidanandcdb732c2025-03-01 18:17:23 +0000163#endif
Usama Arifbec5afd2020-04-17 16:13:39 +0100164}
165
laurenw-arm4c4181c2023-05-04 14:55:37 -0500166#ifdef PLATFORM_TESTS
Sandrine Bailleux27fba522023-05-05 15:44:26 +0200167static __dead2 void tc_run_platform_tests(void)
Usama Arifbec5afd2020-04-17 16:13:39 +0100168{
Sandrine Bailleuxf0f42fb2023-05-05 15:59:00 +0200169 int tests_failed;
170
171 printf("\nStarting platform tests...\n");
Mate Toth-Pal14ba4af2022-10-21 14:24:49 +0200172
Tamas Ban15b79da2023-04-21 09:31:48 +0200173#ifdef PLATFORM_TEST_NV_COUNTERS
Sandrine Bailleuxf0f42fb2023-05-05 15:59:00 +0200174 tests_failed = nv_counter_test();
laurenw-arm116f10c2023-06-13 16:43:39 -0500175#elif PLATFORM_TEST_ROTPK
176 tests_failed = rotpk_test();
Tamas Ban15b79da2023-04-21 09:31:48 +0200177#elif PLATFORM_TEST_TFM_TESTSUITE
Sandrine Bailleuxf0f42fb2023-05-05 15:59:00 +0200178 tests_failed = run_platform_tests();
laurenw-arm2ce1e352023-02-07 13:40:05 -0600179#endif
Sandrine Bailleuxf0f42fb2023-05-05 15:59:00 +0200180
181 printf("Platform tests %s.\n",
182 (tests_failed != 0) ? "failed" : "succeeded");
183
Sandrine Bailleuxe1da6c42023-05-05 13:59:07 +0200184 /* Suspend booting, no matter the tests outcome. */
Sandrine Bailleuxf0f42fb2023-05-05 15:59:00 +0200185 printf("Suspend booting...\n");
Mate Toth-Pal14ba4af2022-10-21 14:24:49 +0200186 plat_error_handler(-1);
Sandrine Bailleux27fba522023-05-05 15:44:26 +0200187}
188#endif
189
190void tc_bl31_common_platform_setup(void)
191{
192 arm_bl31_platform_setup();
193
194#ifdef PLATFORM_TESTS
195 tc_run_platform_tests();
laurenw-arm481ac282023-05-03 12:48:55 -0500196#endif
Usama Arifbec5afd2020-04-17 16:13:39 +0100197}
198
199const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
200{
201 return css_scmi_override_pm_ops(ops);
202}
Usama Arifa49bd492021-08-17 17:57:10 +0100203
204void __init bl31_plat_arch_setup(void)
205{
206 arm_bl31_plat_arch_setup();
207
Jayanth Dodderi Chidanandcdb732c2025-03-01 18:17:23 +0000208 /*
209 * When TRANSFER_LIST is enabled, HW_CONFIG is included in Transfer List
210 * as an entry with the tag TL_TAG_FDT. In this case, the configuration
211 * is already available, so the fconf_populate mechanism is not needed.
212 * The code block below is only required when TRANSFER_LIST is not used.
213 */
214#if !TRANSFER_LIST
Usama Arifa49bd492021-08-17 17:57:10 +0100215 /* HW_CONFIG was also loaded by BL2 */
216 const struct dyn_cfg_dtb_info_t *hw_config_info;
217
218 hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
219 assert(hw_config_info != NULL);
220
221 fconf_populate("HW_CONFIG", hw_config_info->config_addr);
Jayanth Dodderi Chidanandcdb732c2025-03-01 18:17:23 +0000222#endif
Usama Arifa49bd492021-08-17 17:57:10 +0100223}
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500224
Govindraj Raja436ea5e2023-05-10 14:50:36 -0500225#if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500226void tc_bl31_plat_runtime_setup(void)
227{
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500228 /* Start secure watchdog timer. */
229 plat_arm_secure_wdt_start();
Salman Nabi442b0752024-02-19 17:03:44 +0000230
231 arm_bl31_plat_runtime_setup();
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500232}
233
234void bl31_plat_runtime_setup(void)
235{
236 tc_bl31_plat_runtime_setup();
237}
238
239/*
240 * Platform handler for Group0 secure interrupt.
241 */
242int plat_spmd_handle_group0_interrupt(uint32_t intid)
243{
244 /* Trusted Watchdog timer is the only source of Group0 interrupt now. */
245 if (intid == SBSA_SECURE_WDOG_INTID) {
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500246 /* Refresh the timer. */
247 plat_arm_secure_wdt_refresh();
248
Madhukar Pappireddye108df22023-03-22 15:40:40 -0500249 return 0;
250 }
251
252 return -1;
253}
Govindraj Raja436ea5e2023-05-10 14:50:36 -0500254#endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/