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