tc_bl1_dpe.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2024, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <common/debug.h>
  8. #include <drivers/arm/css/sds.h>
  9. #include <drivers/arm/rse_comms.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/generic_delay_timer.h>
  12. #include <drivers/measured_boot/metadata.h>
  13. #include <drivers/measured_boot/rse/dice_prot_env.h>
  14. #include <plat/arm/common/plat_arm.h>
  15. #include <plat/common/platform.h>
  16. #include <platform_def.h>
  17. #include <tools_share/zero_oid.h>
  18. #include "tc_dpe.h"
  19. struct dpe_metadata tc_dpe_metadata[] = {
  20. {
  21. .id = FW_CONFIG_ID,
  22. .cert_id = DPE_AP_FW_CERT_ID,
  23. .signer_id_size = SIGNER_ID_MIN_SIZE,
  24. .sw_type = MBOOT_FW_CONFIG_STRING,
  25. .allow_new_context_to_derive = false,
  26. .retain_parent_context = true,
  27. .create_certificate = false,
  28. .target_locality = LOCALITY_NONE, /* won't derive don't care */
  29. .pk_oid = ZERO_OID },
  30. {
  31. .id = TB_FW_CONFIG_ID,
  32. .cert_id = DPE_AP_FW_CERT_ID,
  33. .signer_id_size = SIGNER_ID_MIN_SIZE,
  34. .sw_type = MBOOT_TB_FW_CONFIG_STRING,
  35. .allow_new_context_to_derive = false,
  36. .retain_parent_context = true,
  37. .create_certificate = false,
  38. .target_locality = LOCALITY_NONE, /* won't derive don't care */
  39. .pk_oid = ZERO_OID },
  40. {
  41. .id = BL2_IMAGE_ID,
  42. .cert_id = DPE_AP_FW_CERT_ID,
  43. .signer_id_size = SIGNER_ID_MIN_SIZE,
  44. .sw_type = MBOOT_BL2_IMAGE_STRING,
  45. .allow_new_context_to_derive = true,
  46. .retain_parent_context = true, /* To handle restart */
  47. .target_locality = LOCALITY_AP_S,
  48. .create_certificate = false,
  49. .pk_oid = ZERO_OID },
  50. {
  51. .id = DPE_INVALID_ID }
  52. };
  53. /* Effective timeout of 10000 ms */
  54. #define RSE_DPE_BOOT_10US_RETRIES 1000000
  55. #define TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID 0x0000000A
  56. /* Context handle is meant to be used by BL2. Sharing it via TB_FW_CONFIG */
  57. static int new_ctx_handle;
  58. /* Save a valid parent context handle to be able to send commands to DPE service
  59. * in case of an AP cold restart.
  60. */
  61. static int new_parent_ctx_handle;
  62. void plat_dpe_share_context_handle(int *ctx_handle, int *parent_ctx_handle)
  63. {
  64. new_ctx_handle = *ctx_handle;
  65. new_parent_ctx_handle = *parent_ctx_handle;
  66. }
  67. void plat_dpe_get_context_handle(int *ctx_handle)
  68. {
  69. int retry = RSE_DPE_BOOT_10US_RETRIES;
  70. int ret;
  71. /* Initialize System level generic or SP804 timer */
  72. generic_delay_timer_init();
  73. /* Check the initialization of the Shared Data Storage area between RSE
  74. * and AP. Since AP_BL1 is executed first then a bit later the RSE
  75. * runtime, which initialize this area, therefore AP needs to check it
  76. * in a loop until it gets written by RSE Secure Runtime.
  77. */
  78. VERBOSE("Waiting for DPE service initialization in RSE Secure Runtime\n");
  79. while (retry > 0) {
  80. ret = sds_init(SDS_RSE_AP_REGION_ID);
  81. if (ret != SDS_OK) {
  82. udelay(10);
  83. retry--;
  84. } else {
  85. break;
  86. }
  87. }
  88. if (retry == 0) {
  89. ERROR("DPE init timeout\n");
  90. plat_panic_handler();
  91. } else {
  92. VERBOSE("DPE init succeeded in %dms.\n",
  93. (RSE_DPE_BOOT_10US_RETRIES - retry) / 100);
  94. }
  95. /* TODO: call this in a loop to avoid reading unfinished data */
  96. ret = sds_struct_read(SDS_RSE_AP_REGION_ID,
  97. TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID,
  98. 0,
  99. ctx_handle,
  100. sizeof(*ctx_handle),
  101. SDS_ACCESS_MODE_NON_CACHED);
  102. if (ret != SDS_OK) {
  103. ERROR("Unable to get DPE context handle from SDS area\n");
  104. plat_panic_handler();
  105. }
  106. VERBOSE("Received DPE context handle: 0x%x\n", *ctx_handle);
  107. }
  108. void bl1_plat_mboot_init(void)
  109. {
  110. /* Initialize the communication channel between AP and RSE */
  111. (void)rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE,
  112. PLAT_RSE_AP_RCV_MHU_BASE);
  113. dpe_init(tc_dpe_metadata);
  114. }
  115. void bl1_plat_mboot_finish(void)
  116. {
  117. int rc;
  118. VERBOSE("Share DPE context handle with BL2: 0x%x\n", new_ctx_handle);
  119. rc = arm_set_tb_fw_info(&new_ctx_handle);
  120. if (rc != 0) {
  121. ERROR("Unable to set DPE context handle in TB_FW_CONFIG\n");
  122. /*
  123. * It is a fatal error because on TC platform, BL2 software
  124. * assumes that a valid DPE context_handle is passed through
  125. * the DTB object by BL1.
  126. */
  127. plat_panic_handler();
  128. }
  129. VERBOSE("Save parent context handle: 0x%x\n", new_parent_ctx_handle);
  130. rc = sds_struct_write(SDS_RSE_AP_REGION_ID,
  131. TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID,
  132. 0,
  133. &new_parent_ctx_handle,
  134. sizeof(new_parent_ctx_handle),
  135. SDS_ACCESS_MODE_NON_CACHED);
  136. if (rc != SDS_OK) {
  137. ERROR("Unable to save DPE parent context handle to SDS area\n");
  138. plat_panic_handler();
  139. }
  140. }