tc_bl31_setup.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2020-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <libfdt.h>
  8. #include <tc_plat.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <drivers/arm/css/css_mhu_doorbell.h>
  13. #include <drivers/arm/css/scmi.h>
  14. #include <drivers/arm/sbsa.h>
  15. #include <lib/fconf/fconf.h>
  16. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  17. #include <plat/arm/common/plat_arm.h>
  18. #include <plat/common/platform.h>
  19. #ifdef PLATFORM_TEST_TFM_TESTSUITE
  20. #include <psa/crypto_platform.h>
  21. #include <psa/crypto_types.h>
  22. #include <psa/crypto_values.h>
  23. #endif /* PLATFORM_TEST_TFM_TESTSUITE */
  24. #ifdef PLATFORM_TEST_TFM_TESTSUITE
  25. /*
  26. * We pretend using an external RNG (through MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
  27. * mbedTLS config option) so we need to provide an implementation of
  28. * mbedtls_psa_external_get_random(). Provide a fake one, since we do not
  29. * actually use any of external RNG and this function is only needed during
  30. * the execution of TF-M testsuite during exporting the public part of the
  31. * delegated attestation key.
  32. */
  33. psa_status_t mbedtls_psa_external_get_random(
  34. mbedtls_psa_external_random_context_t *context,
  35. uint8_t *output, size_t output_size,
  36. size_t *output_length)
  37. {
  38. for (size_t i = 0U; i < output_size; i++) {
  39. output[i] = (uint8_t)(read_cntpct_el0() & 0xFFU);
  40. }
  41. *output_length = output_size;
  42. return PSA_SUCCESS;
  43. }
  44. #endif /* PLATFORM_TEST_TFM_TESTSUITE */
  45. #if TARGET_PLATFORM <= 2
  46. static scmi_channel_plat_info_t tc_scmi_plat_info = {
  47. .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
  48. .db_reg_addr = PLAT_CSS_MHU_BASE + SENDER_REG_SET(0),
  49. .db_preserve_mask = 0xfffffffe,
  50. .db_modify_mask = 0x1,
  51. .ring_doorbell = &mhuv2_ring_doorbell,
  52. };
  53. #elif TARGET_PLATFORM >= 3
  54. static scmi_channel_plat_info_t tc_scmi_plat_info = {
  55. .scmi_mbx_mem = CSS_SCMI_PAYLOAD_BASE,
  56. .db_reg_addr = PLAT_CSS_MHU_BASE + MHU_V3_SENDER_REG_SET(0),
  57. .db_preserve_mask = 0xfffffffe,
  58. .db_modify_mask = 0x1,
  59. .ring_doorbell = &mhu_ring_doorbell,
  60. };
  61. #endif
  62. #if TARGET_PLATFORM == 3
  63. static void enable_ns_mcn_pmu(void)
  64. {
  65. /*
  66. * Enable non-secure access to MCN PMU registers
  67. */
  68. for (int i = 0; i < MCN_INSTANCES; i++) {
  69. uintptr_t mcn_scr = MCN_MICROARCH_BASE_ADDR + MCN_SCR_OFFSET +
  70. (i * MCN_ADDRESS_SPACE_SIZE);
  71. mmio_setbits_32(mcn_scr, 1 << MCN_SCR_PMU_BIT);
  72. }
  73. }
  74. static void set_mcn_slc_alloc_mode(void)
  75. {
  76. /*
  77. * SLC WRALLOCMODE and RDALLOCMODE are configured by default to
  78. * 0b01 (always alloc), configure both to 0b10 (use bus signal
  79. * attribute from interface).
  80. */
  81. for (int i = 0; i < MCN_INSTANCES; i++) {
  82. uintptr_t slccfg_ctl_ns = MCN_MPAM_NS_BASE_ADDR +
  83. (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
  84. uintptr_t slccfg_ctl_s = MCN_MPAM_S_BASE_ADDR +
  85. (i * MCN_ADDRESS_SPACE_SIZE) + MPAM_SLCCFG_CTL_OFFSET;
  86. mmio_clrsetbits_32(slccfg_ctl_ns,
  87. (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
  88. (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
  89. (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
  90. mmio_clrsetbits_32(slccfg_ctl_s,
  91. (SLC_RDALLOCMODE_MASK | SLC_WRALLOCMODE_MASK),
  92. (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_RDALLOCMODE_SHIFT) |
  93. (SLC_ALLOC_BUS_SIGNAL_ATTR << SLC_WRALLOCMODE_SHIFT));
  94. }
  95. }
  96. #endif
  97. void bl31_platform_setup(void)
  98. {
  99. tc_bl31_common_platform_setup();
  100. #if TARGET_PLATFORM == 3
  101. enable_ns_mcn_pmu();
  102. set_mcn_slc_alloc_mode();
  103. plat_arm_ni_setup(NCI_BASE_ADDR);
  104. #endif
  105. }
  106. scmi_channel_plat_info_t *plat_css_get_scmi_info(unsigned int channel_id __unused)
  107. {
  108. return &tc_scmi_plat_info;
  109. }
  110. void bl31_early_platform_setup2(u_register_t arg0, u_register_t arg1,
  111. u_register_t arg2, u_register_t arg3)
  112. {
  113. arm_bl31_early_platform_setup((void *)arg0, arg1, arg2, (void *)arg3);
  114. /* Fill the properties struct with the info from the config dtb */
  115. fconf_populate("FW_CONFIG", arg1);
  116. }
  117. #ifdef PLATFORM_TESTS
  118. static __dead2 void tc_run_platform_tests(void)
  119. {
  120. int tests_failed;
  121. printf("\nStarting platform tests...\n");
  122. #ifdef PLATFORM_TEST_NV_COUNTERS
  123. tests_failed = nv_counter_test();
  124. #elif PLATFORM_TEST_ROTPK
  125. tests_failed = rotpk_test();
  126. #elif PLATFORM_TEST_TFM_TESTSUITE
  127. tests_failed = run_platform_tests();
  128. #endif
  129. printf("Platform tests %s.\n",
  130. (tests_failed != 0) ? "failed" : "succeeded");
  131. /* Suspend booting, no matter the tests outcome. */
  132. printf("Suspend booting...\n");
  133. plat_error_handler(-1);
  134. }
  135. #endif
  136. void tc_bl31_common_platform_setup(void)
  137. {
  138. arm_bl31_platform_setup();
  139. #ifdef PLATFORM_TESTS
  140. tc_run_platform_tests();
  141. #endif
  142. }
  143. const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
  144. {
  145. return css_scmi_override_pm_ops(ops);
  146. }
  147. void __init bl31_plat_arch_setup(void)
  148. {
  149. arm_bl31_plat_arch_setup();
  150. /* HW_CONFIG was also loaded by BL2 */
  151. const struct dyn_cfg_dtb_info_t *hw_config_info;
  152. hw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, HW_CONFIG_ID);
  153. assert(hw_config_info != NULL);
  154. fconf_populate("HW_CONFIG", hw_config_info->config_addr);
  155. }
  156. #if defined(SPD_spmd) && (SPMC_AT_EL3 == 0)
  157. void tc_bl31_plat_runtime_setup(void)
  158. {
  159. /* Start secure watchdog timer. */
  160. plat_arm_secure_wdt_start();
  161. arm_bl31_plat_runtime_setup();
  162. }
  163. void bl31_plat_runtime_setup(void)
  164. {
  165. tc_bl31_plat_runtime_setup();
  166. }
  167. /*
  168. * Platform handler for Group0 secure interrupt.
  169. */
  170. int plat_spmd_handle_group0_interrupt(uint32_t intid)
  171. {
  172. /* Trusted Watchdog timer is the only source of Group0 interrupt now. */
  173. if (intid == SBSA_SECURE_WDOG_INTID) {
  174. /* Refresh the timer. */
  175. plat_arm_secure_wdt_refresh();
  176. return 0;
  177. }
  178. return -1;
  179. }
  180. #endif /*defined(SPD_spmd) && (SPMC_AT_EL3 == 0)*/