Debian Codegraph @

src/soc/intel/common/block/cse/cse_eop.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpi.h>
4 #include <bootstate.h>
5 #include <console/console.h>
6 #include <intelblocks/cse.h>
7 #include <intelblocks/fast_spi.h>
8 #include <intelblocks/pmc_ipc.h>
9 #include <security/vboot/vboot_common.h>
10 #include <soc/intel/common/reset.h>
11 #include <soc/pci_devs.h>
12 #include <timestamp.h>
13 #include <types.h>
14
15 #define CSE_MAX_RETRY_CMD 3
16
17 enum cse_cmd_result {
18 CSE_CMD_RESULT_GLOBAL_RESET_REQUESTED,
19 CSE_CMD_RESULT_SUCCESS,
20 CSE_CMD_RESULT_ERROR,
21 CSE_CMD_RESULT_DISABLED,
22 CSE_CMD_RESULT_RETRY,
23 };
24
25 static enum cse_cmd_result decode_heci_send_receive_error(enum cse_tx_rx_status ret)
26 {
27 switch (ret) {
28 case CSE_TX_ERR_CSE_NOT_READY:
29 case CSE_RX_ERR_CSE_NOT_READY:
30 case CSE_RX_ERR_RESP_LEN_MISMATCH:
31 case CSE_RX_ERR_TIMEOUT:
32 return CSE_CMD_RESULT_RETRY;
33 default:
34 return CSE_CMD_RESULT_ERROR;
35 }
36 }
37
38 static enum cse_cmd_result cse_disable_mei_bus(void)
39 {
40 struct bus_disable_message {
41 uint8_t command;
42 uint8_t reserved[3];
43 } __packed msg = {
44 .command = MEI_BUS_DISABLE_COMMAND,
45 };
46 struct bus_disable_resp {
47 uint8_t command;
48 uint8_t status;
49 uint8_t reserved[2];
50 } __packed reply = {};
51
52 size_t reply_sz = sizeof(reply);
53 enum cse_tx_rx_status ret;
54
55 printk(BIOS_DEBUG, "HECI, Sending MEI BIOS DISABLE command\n");
56 ret = heci_send_receive(&msg, sizeof(msg), &reply, &reply_sz, HECI_MEI_ADDR);
57
58 if (ret) {
59 printk(BIOS_ERR, "HECI: Failed to Disable MEI bus\n");
60 return decode_heci_send_receive_error(ret);
61 }
62
63 if (reply.status) {
64 printk(BIOS_ERR, "HECI: MEI_Bus_Disable Failed (status: %d)\n", reply.status);
65 return CSE_CMD_RESULT_ERROR;
66 }
67
68 return CSE_CMD_RESULT_SUCCESS;
69 }
70
71 static enum cse_cmd_result cse_receive_eop(void)
72 {
73 enum {
74 EOP_REQUESTED_ACTION_CONTINUE = 0,
75 EOP_REQUESTED_ACTION_GLOBAL_RESET = 1,
76 };
77 enum cse_tx_rx_status ret;
78 struct end_of_post_resp {
79 struct mkhi_hdr hdr;
80 uint32_t requested_actions;
81 } __packed resp = {};
82 size_t resp_size = sizeof(resp);
83
84 ret = heci_receive(&resp, &resp_size);
85 if (ret)
86 return decode_heci_send_receive_error(ret);
87
88 if (resp.hdr.group_id != MKHI_GROUP_ID_GEN ||
89 resp.hdr.command != MKHI_END_OF_POST) {
90 printk(BIOS_ERR, "HECI: EOP Unexpected response group or command.\n");
91 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
92 printk(BIOS_ERR, "HECI: It could be a HECI command conflict.\n");
93 return CSE_CMD_RESULT_ERROR;
94 }
95
96 if (resp.hdr.result) {
97 printk(BIOS_ERR, "HECI: EOP Resp Failed: %u\n", resp.hdr.result);
98 return CSE_CMD_RESULT_ERROR;
99 }
100
101 printk(BIOS_INFO, "CSE: EOP requested action: ");
102
103 switch (resp.requested_actions) {
104 case EOP_REQUESTED_ACTION_GLOBAL_RESET:
105 printk(BIOS_INFO, "global reset\n");
106 return CSE_CMD_RESULT_GLOBAL_RESET_REQUESTED;
107 case EOP_REQUESTED_ACTION_CONTINUE:
108 printk(BIOS_INFO, "continue boot\n");
109 return CSE_CMD_RESULT_SUCCESS;
110 default:
111 printk(BIOS_INFO, "unknown %u\n", resp.requested_actions);
112 return CSE_CMD_RESULT_ERROR;
113 }
114 }
115
116 static enum cse_cmd_result cse_send_eop(void)
117 {
118 enum cse_tx_rx_status ret;
119 struct end_of_post_msg {
120 struct mkhi_hdr hdr;
121 } __packed msg = {
122 .hdr = {
123 .group_id = MKHI_GROUP_ID_GEN,
124 .command = MKHI_END_OF_POST,
125 },
126 };
127
128 /*
129 * Prerequisites:
130 * 1) HFSTS1 CWS is Normal
131 * 2) HFSTS1 COM is Normal
132 * 3) Only sent after DID (accomplished by compiling this into ramstage)
133 */
134
135 if (cse_is_hfs1_com_soft_temp_disable()) {
136 printk(BIOS_ERR, "HECI: Prerequisites not met for sending EOP\n");
137 if (CONFIG(SOC_INTEL_CSE_LITE_SKU))
138 return CSE_CMD_RESULT_ERROR;
139 return CSE_CMD_RESULT_DISABLED;
140 }
141
142 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) {
143 printk(BIOS_ERR, "HECI: Prerequisites not met for sending EOP\n");
144 return CSE_CMD_RESULT_ERROR;
145 }
146
147 printk(BIOS_INFO, "HECI: Sending End-of-Post\n");
148
149 ret = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
150 if (ret)
151 return decode_heci_send_receive_error(ret);
152
153 return CSE_CMD_RESULT_SUCCESS;
154 }
155
156 static enum cse_cmd_result cse_send_and_receive_eop(void)
157 {
158 enum cse_cmd_result ret;
159
160 ret = cse_send_eop();
161 if (ret != CSE_CMD_RESULT_SUCCESS)
162 return ret;
163
164 return cse_receive_eop();
165 }
166
167 static enum cse_cmd_result cse_send_cmd_retries(enum cse_cmd_result (*cse_send_command)(void))
168 {
169 size_t retry;
170 enum cse_cmd_result ret;
171 for (retry = 0; retry < CSE_MAX_RETRY_CMD; retry++) {
172 ret = cse_send_command();
173 if (ret != CSE_CMD_RESULT_RETRY)
174 break;
175 }
176 return ret;
177 }
178
179 /*
180 * On EOP error, the BIOS is required to send an MEI bus disable message to the
181 * CSE, followed by disabling all MEI devices. After successfully completing
182 * this, it is safe to boot.
183 */
184 static void cse_handle_eop_error(void)
185 {
186 if (cse_send_cmd_retries(cse_disable_mei_bus))
187 die("Failed to disable MEI bus while recovering from EOP error\n"
188 "Preventing system from booting into an insecure state.\n");
189
190 if (!cse_disable_mei_devices())
191 die("Error disabling MEI devices while recovering from EOP error\n"
192 "Preventing system from booting into an insecure state.\n");
193 }
194
195 static void handle_cse_eop_result(enum cse_cmd_result result)
196 {
197 switch (result) {
198 case CSE_CMD_RESULT_GLOBAL_RESET_REQUESTED:
199 printk(BIOS_INFO, "CSE requested global reset in EOP response, resetting...\n");
200 do_global_reset();
201 break;
202 case CSE_CMD_RESULT_SUCCESS:
203 printk(BIOS_INFO, "CSE EOP successful, continuing boot\n");
204 break;
205 case CSE_CMD_RESULT_DISABLED:
206 printk(BIOS_INFO, "CSE is disabled, continuing boot\n");
207 break;
208 case CSE_CMD_RESULT_ERROR: /* fallthrough */
209 default:
210 printk(BIOS_ERR, "Failed to send EOP to CSE, %d\n", result);
211 /* For vboot, trigger recovery mode if applicable, as there is
212 likely something very broken in this case. */
213 if (CONFIG(VBOOT) && !vboot_recovery_mode_enabled())
214 cse_trigger_vboot_recovery(CSE_EOP_FAIL);
215
216 /* In non-vboot builds or recovery mode, follow the BWG in order
217 to continue to boot securely. */
218 cse_handle_eop_error();
219 break;
220 }
221 }
222
223 static void do_send_end_of_post(bool wait_for_completion)
224 {
225 static bool eop_sent = false, eop_complete = false;
226 enum cse_cmd_result ret;
227
228 if (eop_complete) {
229 printk(BIOS_WARNING, "EOP already sent\n");
230 return;
231 }
232
233 if (acpi_get_sleep_type() == ACPI_S3) {
234 printk(BIOS_INFO, "Skip sending EOP during S3 resume\n");
235 return;
236 }
237
238 /*
239 * If CSE is already hidden then accessing CSE registers would be wrong and will
240 * receive junk, hence, return as CSE is already disabled.
241 */
242 if (!is_cse_enabled()) {
243 printk(BIOS_DEBUG, "CSE is disabled, cannot send End-of-Post (EOP) message\n");
244 return;
245 }
246
247 if (fast_spi_flash_descriptor_override()) {
248 printk(BIOS_WARNING, "CSE: not sending EOP because flash descriptor override is enabled\n");
249 return;
250 }
251
252 if (!eop_sent) {
253 set_cse_device_state(PCH_DEVFN_CSE, DEV_ACTIVE);
254 timestamp_add_now(TS_ME_END_OF_POST_START);
255 ret = cse_send_cmd_retries(cse_send_eop);
256 if (ret == CSE_CMD_RESULT_SUCCESS)
257 eop_sent = true;
258 }
259
260 if (!wait_for_completion)
261 return;
262
263 set_cse_device_state(PCH_DEVFN_CSE, DEV_ACTIVE);
264 ret = cse_receive_eop();
265 if (ret != CSE_CMD_RESULT_SUCCESS) {
266 ret = cse_send_cmd_retries(cse_send_and_receive_eop);
267 handle_cse_eop_result(ret);
268 }
269 timestamp_add_now(TS_ME_END_OF_POST_END);
270
271 set_cse_device_state(PCH_DEVFN_CSE, DEV_IDLE);
272
273 eop_complete = true;
274 }
275
276 /*
277 * Don't send EOP if the following conditions are met:
278 * CSE Lite:
279 * 1. "The platform is running CSE-Lite SKU" AND
280 * 2. 'The CSE is running the RO FW" AND
281 * 3. "The board is in recovery mode"
282 *
283 * Other CSE Type:
284 * 1. "The board is in recovery mode"
285 *
286 * The above conditions summarize that the CSE is in "SOFT TEMP DISABLE" state,
287 * hence, don't send the EOP command to CSE.
288 */
289 static bool is_cse_eop_supported(void)
290 {
291 /* CSE Lite */
292 if ((CONFIG(SOC_INTEL_CSE_LITE_SKU) && vboot_recovery_mode_enabled()) &&
293 cse_is_hfs1_com_soft_temp_disable()) {
294 printk(BIOS_INFO, "HECI: coreboot in recovery mode; found CSE Lite in expected "
295 "SOFT TEMP DISABLE state, skipping EOP\n");
296 return false;
297 }
298 /* Other CSE Type */
299 if (cse_is_hfs1_com_soft_temp_disable()) {
300 printk(BIOS_INFO, "HECI: coreboot in recovery mode; found CSE in expected "
301 "SOFT TEMP DISABLE state, skipping EOP\n");
302 return false;
303 }
304
305 return true;
306 }
307
308 void cse_send_end_of_post(void)
309 {
310 if (!is_cse_eop_supported())
311 return;
312
313 return do_send_end_of_post(!CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC));
314 }
315
316 static void send_cse_eop_with_late_finalize(void *unused)
317 {
318 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_BY_PAYLOAD)) {
319 printk(BIOS_INFO, "Deferring CSE EOP to payload\n");
320 return;
321 }
322
323 if (is_cse_eop_supported())
324 do_send_end_of_post(true);
325
326 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_LATE) ||
327 CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
328 cse_late_finalize();
329 }
330
331 /*
332 * Ideally, to give coreboot maximum flexibility, sending EOP would be done as
333 * late possible. If HECI_DISABLE_USING_SMM is selected, then sending EOP must
334 * be performed before the HECI bus is disabled, so these boards use
335 * BS_PAYLOAD_LOAD, which happens before the HECI_DISABLE_USING_SMM Kconfig takes
336 * effect (EOP is sent using the HECI bus).
337 * Otherwise, EOP can be pushed a little later, and can be performed in
338 * BS_PAYLOAD_BOOT instead.
339 */
340 #if !CONFIG(HECI_DISABLE_USING_SMM)
341 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, send_cse_eop_with_late_finalize, NULL);
342 #else
343 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_ENTRY, send_cse_eop_with_late_finalize, NULL);
344 #endif