blob: a073dc33375d27ce57d135244b73233fee8bdc1a [file] [log] [blame]
Tamas Bana4260892023-06-07 13:35:04 +02001/*
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 Ban0fbe8622023-06-12 11:33:47 +02009#include <common/debug.h>
Tamas Banae33fa92023-06-07 14:18:46 +020010#include <drivers/arm/css/sds.h>
Tamas Bandc15bf42024-02-22 11:35:28 +010011#include <drivers/arm/rse_comms.h>
Tamas Banae33fa92023-06-07 14:18:46 +020012#include <drivers/delay_timer.h>
13#include <drivers/generic_delay_timer.h>
Tamas Bana4260892023-06-07 13:35:04 +020014#include <drivers/measured_boot/metadata.h>
Tamas Bandc15bf42024-02-22 11:35:28 +010015#include <drivers/measured_boot/rse/dice_prot_env.h>
Tamas Bana4260892023-06-07 13:35:04 +020016#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 Bana5d3ca82024-01-30 12:56:38 +010021#include "tc_dpe_cert.h"
22
Tamas Bana4260892023-06-07 13:35:04 +020023struct dpe_metadata tc_dpe_metadata[] = {
24 {
25 .id = FW_CONFIG_ID,
Tamas Bana5d3ca82024-01-30 12:56:38 +010026 .cert_id = DPE_AP_FW_CERT_ID,
Tamas Bana4260892023-06-07 13:35:04 +020027 .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,
32 .pk_oid = ZERO_OID },
33 {
34 .id = TB_FW_CONFIG_ID,
Tamas Bana5d3ca82024-01-30 12:56:38 +010035 .cert_id = DPE_AP_FW_CERT_ID,
Tamas Bana4260892023-06-07 13:35:04 +020036 .signer_id_size = SIGNER_ID_MIN_SIZE,
37 .sw_type = MBOOT_TB_FW_CONFIG_STRING,
38 .allow_new_context_to_derive = false,
39 .retain_parent_context = true,
40 .create_certificate = false,
41 .pk_oid = ZERO_OID },
42 {
43 .id = BL2_IMAGE_ID,
Tamas Bana5d3ca82024-01-30 12:56:38 +010044 .cert_id = DPE_AP_FW_CERT_ID,
Tamas Bana4260892023-06-07 13:35:04 +020045 .signer_id_size = SIGNER_ID_MIN_SIZE,
46 .sw_type = MBOOT_BL2_IMAGE_STRING,
47 .allow_new_context_to_derive = true,
Tamas Ban14d82442024-06-03 16:51:49 +020048 .retain_parent_context = true, /* To handle restart */
Tamas Bana4260892023-06-07 13:35:04 +020049 .create_certificate = false,
50 .pk_oid = ZERO_OID },
51 {
52 .id = DPE_INVALID_ID }
53};
54
Tamas Banae33fa92023-06-07 14:18:46 +020055/* Effective timeout of 10000 ms */
Tamas Bandc15bf42024-02-22 11:35:28 +010056#define RSE_DPE_BOOT_10US_RETRIES 1000000
Tamas Banae33fa92023-06-07 14:18:46 +020057#define TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID 0x0000000A
58
Tamas Ban0fbe8622023-06-12 11:33:47 +020059/* Context handle is meant to be used by BL2. Sharing it via TB_FW_CONFIG */
60static int new_ctx_handle;
Tamas Ban14d82442024-06-03 16:51:49 +020061/* Save a valid parent context handle to be able to send commands to DPE service
62 * in case of an AP cold restart.
63 */
64static int new_parent_ctx_handle;
Tamas Ban0fbe8622023-06-12 11:33:47 +020065
Tamas Ban14d82442024-06-03 16:51:49 +020066void plat_dpe_share_context_handle(int *ctx_handle, int *parent_ctx_handle)
Tamas Ban0fbe8622023-06-12 11:33:47 +020067{
68 new_ctx_handle = *ctx_handle;
Tamas Ban14d82442024-06-03 16:51:49 +020069 new_parent_ctx_handle = *parent_ctx_handle;
Tamas Ban0fbe8622023-06-12 11:33:47 +020070}
Tamas Bana4260892023-06-07 13:35:04 +020071
Tamas Banae33fa92023-06-07 14:18:46 +020072void plat_dpe_get_context_handle(int *ctx_handle)
73{
Tamas Bandc15bf42024-02-22 11:35:28 +010074 int retry = RSE_DPE_BOOT_10US_RETRIES;
Tamas Banae33fa92023-06-07 14:18:46 +020075 int ret;
76
77 /* Initialize System level generic or SP804 timer */
78 generic_delay_timer_init();
79
Tamas Bandc15bf42024-02-22 11:35:28 +010080 /* Check the initialization of the Shared Data Storage area between RSE
81 * and AP. Since AP_BL1 is executed first then a bit later the RSE
Tamas Banae33fa92023-06-07 14:18:46 +020082 * runtime, which initialize this area, therefore AP needs to check it
Tamas Bandc15bf42024-02-22 11:35:28 +010083 * in a loop until it gets written by RSE Secure Runtime.
Tamas Banae33fa92023-06-07 14:18:46 +020084 */
Tamas Bandc15bf42024-02-22 11:35:28 +010085 VERBOSE("Waiting for DPE service initialization in RSE Secure Runtime\n");
Tamas Banae33fa92023-06-07 14:18:46 +020086 while (retry > 0) {
Tamas Bandc15bf42024-02-22 11:35:28 +010087 ret = sds_init(SDS_RSE_AP_REGION_ID);
Tamas Banae33fa92023-06-07 14:18:46 +020088 if (ret != SDS_OK) {
89 udelay(10);
90 retry--;
91 } else {
92 break;
93 }
94 }
95
96 if (retry == 0) {
97 ERROR("DPE init timeout\n");
98 plat_panic_handler();
99 } else {
100 VERBOSE("DPE init succeeded in %dms.\n",
Tamas Bandc15bf42024-02-22 11:35:28 +0100101 (RSE_DPE_BOOT_10US_RETRIES - retry) / 100);
Tamas Banae33fa92023-06-07 14:18:46 +0200102 }
103
104 /* TODO: call this in a loop to avoid reading unfinished data */
Tamas Bandc15bf42024-02-22 11:35:28 +0100105 ret = sds_struct_read(SDS_RSE_AP_REGION_ID,
Tamas Banae33fa92023-06-07 14:18:46 +0200106 TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID,
107 0,
108 ctx_handle,
109 sizeof(*ctx_handle),
110 SDS_ACCESS_MODE_NON_CACHED);
111 if (ret != SDS_OK) {
112 ERROR("Unable to get DPE context handle from SDS area\n");
113 plat_panic_handler();
114 }
115
116 VERBOSE("Received DPE context handle: 0x%x\n", *ctx_handle);
117}
118
Tamas Bana4260892023-06-07 13:35:04 +0200119void bl1_plat_mboot_init(void)
120{
Tamas Bandc15bf42024-02-22 11:35:28 +0100121 /* Initialize the communication channel between AP and RSE */
122 (void)rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE,
123 PLAT_RSE_AP_RCV_MHU_BASE);
Tamas Bana4260892023-06-07 13:35:04 +0200124
125 dpe_init(tc_dpe_metadata);
126}
127
128void bl1_plat_mboot_finish(void)
129{
Tamas Ban0fbe8622023-06-12 11:33:47 +0200130 int rc;
131
132 VERBOSE("Share DPE context handle with BL2: 0x%x\n", new_ctx_handle);
133 rc = arm_set_tb_fw_info(&new_ctx_handle);
134 if (rc != 0) {
135 ERROR("Unable to set DPE context handle in TB_FW_CONFIG\n");
136 /*
137 * It is a fatal error because on TC platform, BL2 software
138 * assumes that a valid DPE context_handle is passed through
139 * the DTB object by BL1.
140 */
141 plat_panic_handler();
142 }
Tamas Ban14d82442024-06-03 16:51:49 +0200143
144 VERBOSE("Save parent context handle: 0x%x\n", new_parent_ctx_handle);
145 rc = sds_struct_write(SDS_RSE_AP_REGION_ID,
146 TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID,
147 0,
148 &new_parent_ctx_handle,
149 sizeof(new_parent_ctx_handle),
150 SDS_ACCESS_MODE_NON_CACHED);
151 if (rc != SDS_OK) {
152 ERROR("Unable to save DPE parent context handle to SDS area\n");
153 plat_panic_handler();
154 }
Tamas Bana4260892023-06-07 13:35:04 +0200155}