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