xref: /rk3399_ARM-atf/plat/arm/board/tc/tc_bl1_dpe.c (revision 758ccb802d4f2a5fe55ec936a21ad4ae8cbd7b4f)
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 
9 #include <common/debug.h>
10 #include <drivers/arm/css/sds.h>
11 #include <drivers/arm/rse_comms.h>
12 #include <drivers/delay_timer.h>
13 #include <drivers/generic_delay_timer.h>
14 #include <drivers/measured_boot/metadata.h>
15 #include <drivers/measured_boot/rse/dice_prot_env.h>
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 
21 #include "tc_dpe_cert.h"
22 
23 struct dpe_metadata tc_dpe_metadata[] = {
24 	{
25 		.id = FW_CONFIG_ID,
26 		.cert_id = DPE_AP_FW_CERT_ID,
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,
32 		.pk_oid = ZERO_OID },
33 	{
34 		.id = TB_FW_CONFIG_ID,
35 		.cert_id = DPE_AP_FW_CERT_ID,
36 		.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,
44 		.cert_id = DPE_AP_FW_CERT_ID,
45 		.signer_id_size = SIGNER_ID_MIN_SIZE,
46 		.sw_type = MBOOT_BL2_IMAGE_STRING,
47 		.allow_new_context_to_derive = true,
48 		.retain_parent_context = false,
49 		.create_certificate = false,
50 		.pk_oid = ZERO_OID },
51 	{
52 		.id = DPE_INVALID_ID }
53 };
54 
55 /* Effective timeout of 10000 ms */
56 #define RSE_DPE_BOOT_10US_RETRIES		1000000
57 #define TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID	0x0000000A
58 
59 /* Context handle is meant to be used by BL2. Sharing it via TB_FW_CONFIG */
60 static int new_ctx_handle;
61 
62 void plat_dpe_share_context_handle(int *ctx_handle)
63 {
64 	new_ctx_handle = *ctx_handle;
65 }
66 
67 void plat_dpe_get_context_handle(int *ctx_handle)
68 {
69 	int retry = RSE_DPE_BOOT_10US_RETRIES;
70 	int ret;
71 
72 	/* Initialize System level generic or SP804 timer */
73 	generic_delay_timer_init();
74 
75 	/* Check the initialization of the Shared Data Storage area between RSE
76 	 * and AP. Since AP_BL1 is executed first then a bit later the RSE
77 	 * runtime, which initialize this area, therefore AP needs to check it
78 	 * in a loop until it gets written by RSE Secure Runtime.
79 	 */
80 	VERBOSE("Waiting for DPE service initialization in RSE Secure Runtime\n");
81 	while (retry > 0) {
82 		ret = sds_init(SDS_RSE_AP_REGION_ID);
83 		if (ret != SDS_OK) {
84 			udelay(10);
85 			retry--;
86 		} else {
87 			break;
88 		}
89 	}
90 
91 	if (retry == 0) {
92 		ERROR("DPE init timeout\n");
93 		plat_panic_handler();
94 	} else {
95 		VERBOSE("DPE init succeeded in %dms.\n",
96 			(RSE_DPE_BOOT_10US_RETRIES - retry) / 100);
97 	}
98 
99 	/* TODO: call this in a loop to avoid reading unfinished data */
100 	ret = sds_struct_read(SDS_RSE_AP_REGION_ID,
101 			      TC2_SDS_DPE_CTX_HANDLE_STRUCT_ID,
102 			      0,
103 			      ctx_handle,
104 			      sizeof(*ctx_handle),
105 			      SDS_ACCESS_MODE_NON_CACHED);
106 	if (ret != SDS_OK) {
107 		ERROR("Unable to get DPE context handle from SDS area\n");
108 		plat_panic_handler();
109 	}
110 
111 	VERBOSE("Received DPE context handle: 0x%x\n", *ctx_handle);
112 }
113 
114 void bl1_plat_mboot_init(void)
115 {
116 	/* Initialize the communication channel between AP and RSE */
117 	(void)rse_comms_init(PLAT_RSE_AP_SND_MHU_BASE,
118 			     PLAT_RSE_AP_RCV_MHU_BASE);
119 
120 	dpe_init(tc_dpe_metadata);
121 }
122 
123 void bl1_plat_mboot_finish(void)
124 {
125 	int rc;
126 
127 	VERBOSE("Share DPE context handle with BL2: 0x%x\n", new_ctx_handle);
128 	rc = arm_set_tb_fw_info(&new_ctx_handle);
129 	if (rc != 0) {
130 		ERROR("Unable to set DPE context handle in TB_FW_CONFIG\n");
131 		/*
132 		 * It is a fatal error because on TC platform, BL2 software
133 		 * assumes that a valid DPE context_handle is passed through
134 		 * the DTB object by BL1.
135 		 */
136 		plat_panic_handler();
137 	}
138 }
139