1 /*
2  * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 
10 #include <arch_helpers.h>
11 #include <common/debug.h>
12 #include <drivers/arm/css/css_mhu.h>
13 #include <drivers/arm/css/css_scp.h>
14 #include <drivers/arm/css/css_scpi.h>
15 #include <plat/common/platform.h>
16 #include <platform_def.h>
17 
18 /* ID of the MHU slot used for the BOM protocol */
19 #define BOM_MHU_SLOT_ID		0
20 
21 /* Boot commands sent from AP -> SCP */
22 #define BOOT_CMD_INFO	0x00
23 #define BOOT_CMD_DATA	0x01
24 
25 /* BOM command header */
26 typedef struct {
27 	uint32_t id : 8;
28 	uint32_t reserved : 24;
29 } bom_cmd_t;
30 
31 typedef struct {
32 	uint32_t image_size;
33 	uint32_t checksum;
34 } cmd_info_payload_t;
35 
36 /*
37  * Unlike the SCPI protocol, the boot protocol uses the same memory region
38  * for both AP -> SCP and SCP -> AP transfers; define the address of this...
39  */
40 #define BOM_SHARED_MEM		PLAT_CSS_SCP_COM_SHARED_MEM_BASE
41 #define BOM_CMD_HEADER		((bom_cmd_t *) BOM_SHARED_MEM)
42 #define BOM_CMD_PAYLOAD		((void *) (BOM_SHARED_MEM + sizeof(bom_cmd_t)))
43 
44 typedef struct {
45 	/* Offset from the base address of the Trusted RAM */
46 	uint32_t offset;
47 	uint32_t block_size;
48 } cmd_data_payload_t;
49 
50 /*
51  * All CSS platforms load SCP_BL2/SCP_BL2U just below BL2 (this is where BL31
52  * usually resides except when ARM_BL31_IN_DRAM is
53  * set). Ensure that SCP_BL2/SCP_BL2U do not overflow into shared RAM and
54  * the fw_config.
55  */
56 CASSERT(SCP_BL2_LIMIT <= BL2_BASE, assert_scp_bl2_overwrite_bl2);
57 CASSERT(SCP_BL2U_LIMIT <= BL2_BASE, assert_scp_bl2u_overwrite_bl2);
58 
59 CASSERT(SCP_BL2_BASE >= ARM_FW_CONFIG_LIMIT, assert_scp_bl2_overflow);
60 CASSERT(SCP_BL2U_BASE >= ARM_FW_CONFIG_LIMIT, assert_scp_bl2u_overflow);
61 
scp_boot_message_start(void)62 static void scp_boot_message_start(void)
63 {
64 	mhu_secure_message_start(BOM_MHU_SLOT_ID);
65 }
66 
scp_boot_message_send(size_t payload_size)67 static void scp_boot_message_send(size_t payload_size)
68 {
69 	/* Ensure that any write to the BOM payload area is seen by SCP before
70 	 * we write to the MHU register. If these 2 writes were reordered by
71 	 * the CPU then SCP would read stale payload data */
72 	dmbst();
73 
74 	/* Send command to SCP */
75 	mhu_secure_message_send(BOM_MHU_SLOT_ID);
76 }
77 
scp_boot_message_wait(size_t size)78 static uint32_t scp_boot_message_wait(size_t size)
79 {
80 	uint32_t mhu_status;
81 
82 	mhu_status = mhu_secure_message_wait();
83 
84 	/* Expect an SCP Boot Protocol message, reject any other protocol */
85 	if (mhu_status != (1 << BOM_MHU_SLOT_ID)) {
86 		ERROR("MHU: Unexpected protocol (MHU status: 0x%x)\n",
87 			mhu_status);
88 		panic();
89 	}
90 
91 	/* Ensure that any read to the BOM payload area is done after reading
92 	 * the MHU register. If these 2 reads were reordered then the CPU would
93 	 * read invalid payload data */
94 	dmbld();
95 
96 	return *(uint32_t *) BOM_SHARED_MEM;
97 }
98 
scp_boot_message_end(void)99 static void scp_boot_message_end(void)
100 {
101 	mhu_secure_message_end(BOM_MHU_SLOT_ID);
102 }
103 
css_scp_boot_image_xfer(void * image,unsigned int image_size)104 int css_scp_boot_image_xfer(void *image, unsigned int image_size)
105 {
106 	uint32_t response;
107 	uint32_t checksum;
108 	cmd_info_payload_t *cmd_info_payload;
109 	cmd_data_payload_t *cmd_data_payload;
110 
111 	assert((uintptr_t) image == SCP_BL2_BASE);
112 
113 	if ((image_size == 0) || (image_size % 4 != 0)) {
114 		ERROR("Invalid size for the SCP_BL2 image. Must be a multiple of "
115 			"4 bytes and not zero (current size = 0x%x)\n",
116 			image_size);
117 		return -1;
118 	}
119 
120 	/* Extract the checksum from the image */
121 	checksum = *(uint32_t *) image;
122 	image = (char *) image + sizeof(checksum);
123 	image_size -= sizeof(checksum);
124 
125 	mhu_secure_init();
126 
127 	VERBOSE("Send info about the SCP_BL2 image to be transferred to SCP\n");
128 
129 	/*
130 	 * Send information about the SCP firmware image about to be transferred
131 	 * to SCP
132 	 */
133 	scp_boot_message_start();
134 
135 	BOM_CMD_HEADER->id = BOOT_CMD_INFO;
136 	cmd_info_payload = BOM_CMD_PAYLOAD;
137 	cmd_info_payload->image_size = image_size;
138 	cmd_info_payload->checksum = checksum;
139 
140 	scp_boot_message_send(sizeof(*cmd_info_payload));
141 #if CSS_DETECT_PRE_1_7_0_SCP
142 	{
143 		const uint32_t deprecated_scp_nack_cmd = 0x404;
144 		uint32_t mhu_status;
145 
146 		VERBOSE("Detecting SCP version incompatibility\n");
147 
148 		mhu_status = mhu_secure_message_wait();
149 		if (mhu_status == deprecated_scp_nack_cmd) {
150 			ERROR("Detected an incompatible version of the SCP firmware.\n");
151 			ERROR("Only versions from v1.7.0 onwards are supported.\n");
152 			ERROR("Please update the SCP firmware.\n");
153 			return -1;
154 		}
155 
156 		VERBOSE("SCP version looks OK\n");
157 	}
158 #endif /* CSS_DETECT_PRE_1_7_0_SCP */
159 	response = scp_boot_message_wait(sizeof(response));
160 	scp_boot_message_end();
161 
162 	if (response != 0) {
163 		ERROR("SCP BOOT_CMD_INFO returned error %u\n", response);
164 		return -1;
165 	}
166 
167 	VERBOSE("Transferring SCP_BL2 image to SCP\n");
168 
169 	/* Transfer SCP_BL2 image to SCP */
170 	scp_boot_message_start();
171 
172 	BOM_CMD_HEADER->id = BOOT_CMD_DATA;
173 	cmd_data_payload = BOM_CMD_PAYLOAD;
174 	cmd_data_payload->offset = (uintptr_t) image - ARM_TRUSTED_SRAM_BASE;
175 	cmd_data_payload->block_size = image_size;
176 
177 	scp_boot_message_send(sizeof(*cmd_data_payload));
178 	response = scp_boot_message_wait(sizeof(response));
179 	scp_boot_message_end();
180 
181 	if (response != 0) {
182 		ERROR("SCP BOOT_CMD_DATA returned error %u\n", response);
183 		return -1;
184 	}
185 
186 	return 0;
187 }
188 
css_scp_boot_ready(void)189 int css_scp_boot_ready(void)
190 {
191 	VERBOSE("Waiting for SCP to signal it is ready to go on\n");
192 
193 	/* Wait for SCP to signal it's ready */
194 	return scpi_wait_ready();
195 }
196