Debian Codegraph @

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

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #define __SIMPLE_DEVICE__
4
5 #include <assert.h>
6 #include <commonlib/helpers.h>
7 #include <console/console.h>
8 #include <device/mmio.h>
9 #include <delay.h>
10 #include <device/pci.h>
11 #include <device/pci_ids.h>
12 #include <device/pci_ops.h>
13 #include <intelblocks/cse.h>
14 #include <intelblocks/fast_spi.h>
15 #include <intelblocks/me.h>
16 #include <intelblocks/pmclib.h>
17 #include <intelblocks/post_codes.h>
18 #include <option.h>
19 #include <security/vboot/misc.h>
20 #include <security/vboot/vboot_common.h>
21 #include <soc/intel/common/reset.h>
22 #include <soc/iomap.h>
23 #include <soc/pci_devs.h>
24 #include <string.h>
25 #include <timer.h>
26 #include <types.h>
27
28 #define HECI_BASE_SIZE (4 * KiB)
29
30 #define MAX_HECI_MESSAGE_RETRY_COUNT 5
31
32 /* Wait up to 15 sec for HECI to get ready */
33 #define HECI_DELAY_READY_MS (15 * 1000)
34 /* Wait up to 100 usec between circular buffer polls */
35 #define HECI_DELAY_US 100
36 /* Wait up to 5 sec for CSE to chew something we sent */
37 #define HECI_SEND_TIMEOUT_MS (5 * 1000)
38 /* Wait up to 5 sec for CSE to blurp a reply */
39 #define HECI_READ_TIMEOUT_MS (5 * 1000)
40 /* Wait up to 1 ms for CSE CIP */
41 #define HECI_CIP_TIMEOUT_US 1000
42 /* Wait up to 5 seconds for CSE to boot from RO(BP1) */
43 #define CSE_DELAY_BOOT_TO_RO_MS (5 * 1000)
44 /* Wait up to 5 sec for CSE FW init to complete */
45 #define CSE_FW_INIT_TIMEOUT_MS (5 * 1000)
46
47 #define SLOT_SIZE sizeof(uint32_t)
48
49 #define MMIO_CSE_CB_WW 0x00
50 #define MMIO_HOST_CSR 0x04
51 #define MMIO_CSE_CB_RW 0x08
52 #define MMIO_CSE_CSR 0x0c
53 #define MMIO_CSE_DEVIDLE 0x800
54 #define CSE_DEV_IDLE (1 << 2)
55 #define CSE_DEV_CIP (1 << 0)
56
57 #define CSR_IE (1 << 0)
58 #define CSR_IS (1 << 1)
59 #define CSR_IG (1 << 2)
60 #define CSR_READY (1 << 3)
61 #define CSR_RESET (1 << 4)
62 #define CSR_RP_START 8
63 #define CSR_RP (((1 << 8) - 1) << CSR_RP_START)
64 #define CSR_WP_START 16
65 #define CSR_WP (((1 << 8) - 1) << CSR_WP_START)
66 #define CSR_CBD_START 24
67 #define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START)
68
69 #define MEI_HDR_IS_COMPLETE (1 << 31)
70 #define MEI_HDR_LENGTH_START 16
71 #define MEI_HDR_LENGTH_SIZE 9
72 #define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \
73 << MEI_HDR_LENGTH_START)
74 #define MEI_HDR_HOST_ADDR_START 8
75 #define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START)
76 #define MEI_HDR_CSE_ADDR_START 0
77 #define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START)
78
79 /* Get HECI BAR 0 from PCI configuration space */
80 static uintptr_t get_cse_bar(pci_devfn_t dev)
81 {
82 uintptr_t bar;
83
84 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
85 assert(bar != 0);
86 /*
87 * Bits 31-12 are the base address as per EDS for SPI,
88 * Don't care about 0-11 bit
89 */
90 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
91 }
92
93 static void heci_assign_resource(pci_devfn_t dev, uintptr_t tempbar)
94 {
95 u16 pcireg;
96
97 /* Assign Resources */
98 /* Clear BIT 1-2 of Command Register */
99 pcireg = pci_read_config16(dev, PCI_COMMAND);
100 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
101 pci_write_config16(dev, PCI_COMMAND, pcireg);
102
103 /* Program Temporary BAR for HECI device */
104 pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar);
105 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0);
106
107 /* Enable Bus Master and MMIO Space */
108 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
109 }
110
111 /*
112 * Initialize the CSE device with provided temporary BAR. If BAR is 0 use a
113 * default. This is intended for pre-mem usage only where BARs haven't been
114 * assigned yet and devices are not enabled.
115 */
116 void cse_init(uintptr_t tempbar)
117 {
118 pci_devfn_t dev = PCH_DEV_CSE;
119
120 /* Check if device enabled */
121 if (!is_cse_enabled())
122 return;
123
124 /* Assume it is already initialized, nothing else to do */
125 if (get_cse_bar(dev))
126 return;
127
128 /* Use default pre-ram bar */
129 if (!tempbar)
130 tempbar = HECI1_BASE_ADDRESS;
131
132 /* Assign HECI resource and enable the resource */
133 heci_assign_resource(dev, tempbar);
134
135 /* Trigger HECI Reset and make Host ready for communication with CSE */
136 heci_reset();
137 }
138
139 static uint32_t read_bar(pci_devfn_t dev, uint32_t offset)
140 {
141 return read32p(get_cse_bar(dev) + offset);
142 }
143
144 static void write_bar(pci_devfn_t dev, uint32_t offset, uint32_t val)
145 {
146 return write32p(get_cse_bar(dev) + offset, val);
147 }
148
149 static uint32_t read_cse_csr(void)
150 {
151 return read_bar(PCH_DEV_CSE, MMIO_CSE_CSR);
152 }
153
154 static uint32_t read_host_csr(void)
155 {
156 return read_bar(PCH_DEV_CSE, MMIO_HOST_CSR);
157 }
158
159 static void write_host_csr(uint32_t data)
160 {
161 write_bar(PCH_DEV_CSE, MMIO_HOST_CSR, data);
162 }
163
164 static size_t filled_slots(uint32_t data)
165 {
166 uint8_t wp, rp;
167 rp = data >> CSR_RP_START;
168 wp = data >> CSR_WP_START;
169 return (uint8_t)(wp - rp);
170 }
171
172 static size_t cse_filled_slots(void)
173 {
174 return filled_slots(read_cse_csr());
175 }
176
177 static size_t host_empty_slots(void)
178 {
179 uint32_t csr;
180 csr = read_host_csr();
181
182 return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr);
183 }
184
185 static void clear_int(void)
186 {
187 uint32_t csr;
188 csr = read_host_csr();
189 csr |= CSR_IS;
190 write_host_csr(csr);
191 }
192
193 static uint32_t read_slot(void)
194 {
195 return read_bar(PCH_DEV_CSE, MMIO_CSE_CB_RW);
196 }
197
198 static void write_slot(uint32_t val)
199 {
200 write_bar(PCH_DEV_CSE, MMIO_CSE_CB_WW, val);
201 }
202
203 static int wait_write_slots(size_t cnt)
204 {
205 struct stopwatch sw;
206
207 stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT_MS);
208 while (host_empty_slots() < cnt) {
209 udelay(HECI_DELAY_US);
210 if (stopwatch_expired(&sw)) {
211 printk(BIOS_ERR, "HECI: timeout, buffer not drained\n");
212 return 0;
213 }
214 }
215 return 1;
216 }
217
218 static int wait_read_slots(size_t cnt)
219 {
220 struct stopwatch sw;
221
222 stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT_MS);
223 while (cse_filled_slots() < cnt) {
224 udelay(HECI_DELAY_US);
225 if (stopwatch_expired(&sw)) {
226 printk(BIOS_ERR, "HECI: timed out reading answer!\n");
227 return 0;
228 }
229 }
230 return 1;
231 }
232
233 /* get number of full 4-byte slots */
234 static size_t bytes_to_slots(size_t bytes)
235 {
236 return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE;
237 }
238
239 static int cse_ready(void)
240 {
241 uint32_t csr;
242 csr = read_cse_csr();
243 return csr & CSR_READY;
244 }
245
246 static bool cse_check_hfs1_com(int mode)
247 {
248 union me_hfsts1 hfs1;
249 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
250 return hfs1.fields.operation_mode == mode;
251 }
252
253 static bool cse_is_hfs1_fw_init_complete(void)
254 {
255 union me_hfsts1 hfs1;
256 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
257 if (hfs1.fields.fw_init_complete)
258 return true;
259 return false;
260 }
261
262 bool cse_is_hfs1_cws_normal(void)
263 {
264 union me_hfsts1 hfs1;
265 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
266 if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL)
267 return true;
268 return false;
269 }
270
271 bool cse_is_hfs1_cws_m3_no_uma(void)
272 {
273 union me_hfsts1 hfs1;
274 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
275 return hfs1.fields.working_state == ME_HFS1_CWS_M3_NO_UMA;
276 }
277
278 bool cse_is_hfs1_com_normal(void)
279 {
280 return cse_check_hfs1_com(ME_HFS1_COM_NORMAL);
281 }
282
283 bool cse_is_hfs1_com_secover_mei_msg(void)
284 {
285 return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG);
286 }
287
288 bool cse_is_hfs1_com_soft_temp_disable(void)
289 {
290 return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE);
291 }
292
293 /*
294 * Starting from TGL platform, HFSTS1.spi_protection_mode replaces mfg_mode to indicate
295 * SPI protection status as well as end-of-manufacturing(EOM) status where EOM flow is
296 * triggered in single staged operation (either through first boot with required MFIT
297 * configuratin or FPT /CLOSEMANUF).
298 * In staged manufacturing flow, spi_protection_mode alone doesn't indicate the EOM status.
299 *
300 * HFSTS1.spi_protection_mode description:
301 * mfg_mode = 0 means SPI protection is on.
302 * mfg_mode = 1 means SPI is unprotected.
303 */
304 bool cse_is_hfs1_spi_protected(void)
305 {
306 union me_hfsts1 hfs1;
307 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
308 return !hfs1.fields.mfg_mode;
309 }
310
311 #define ME_HFSTS2_CUR_PM_EVENT_SHIFT 24
312 #define ME_HFSTS2_CUR_PM_EVENT_MASK (0xf << ME_HFSTS2_CUR_PM_EVENT_SHIFT)
313
314 static uint8_t cse_get_hfs2_current_pm_event(void)
315 {
316 uint32_t data = me_read_config32(PCI_ME_HFSTS2);
317 return (uint8_t)((data & ME_HFSTS2_CUR_PM_EVENT_MASK) >>
318 ME_HFSTS2_CUR_PM_EVENT_SHIFT);
319 }
320
321 bool cse_check_host_cold_reset(void)
322 {
323 uint8_t event = cse_get_hfs2_current_pm_event();
324
325 switch (event) {
326 case PWR_CYCLE_RESET_CMOFF:
327 return true;
328 default:
329 return false;
330 }
331 }
332
333 bool cse_is_hfs3_fw_sku_lite(void)
334 {
335 union me_hfsts3 hfs3;
336 hfs3.data = me_read_config32(PCI_ME_HFSTS3);
337 return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_LITE;
338 }
339
340 /* Makes the host ready to communicate with CSE */
341 void cse_set_host_ready(void)
342 {
343 uint32_t csr;
344 csr = read_host_csr();
345 csr &= ~CSR_RESET;
346 csr |= (CSR_IG | CSR_READY);
347 write_host_csr(csr);
348 }
349
350 /* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */
351 uint8_t cse_wait_sec_override_mode(void)
352 {
353 struct stopwatch sw;
354 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
355 while (!cse_is_hfs1_com_secover_mei_msg()) {
356 udelay(HECI_DELAY_US);
357 if (stopwatch_expired(&sw)) {
358 printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n");
359 return 0;
360 }
361 }
362 printk(BIOS_DEBUG, "HECI: CSE took %lld ms to enter security override mode\n",
363 stopwatch_duration_msecs(&sw));
364 return 1;
365 }
366
367 /*
368 * Polls for CSE's current operation mode 'Soft Temporary Disable'.
369 * The CSE enters the current operation mode when it boots from RO(BP1).
370 */
371 uint8_t cse_wait_com_soft_temp_disable(void)
372 {
373 struct stopwatch sw;
374 stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO_MS);
375 while (!cse_is_hfs1_com_soft_temp_disable()) {
376 udelay(HECI_DELAY_US);
377 if (stopwatch_expired(&sw)) {
378 printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n");
379 return 0;
380 }
381 }
382 printk(BIOS_SPEW, "HECI: CSE took %lld ms to boot from RO\n",
383 stopwatch_duration_msecs(&sw));
384 return 1;
385 }
386
387 static int wait_heci_ready(void)
388 {
389 struct stopwatch sw;
390
391 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
392 while (!cse_ready()) {
393 udelay(HECI_DELAY_US);
394 if (stopwatch_expired(&sw))
395 return 0;
396 }
397
398 return 1;
399 }
400
401 static void host_gen_interrupt(void)
402 {
403 uint32_t csr;
404 csr = read_host_csr();
405 csr |= CSR_IG;
406 write_host_csr(csr);
407 }
408
409 static size_t hdr_get_length(uint32_t hdr)
410 {
411 return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START;
412 }
413
414 static int
415 send_one_message(uint32_t hdr, const void *buff)
416 {
417 size_t pend_len, pend_slots, remainder, i;
418 uint32_t tmp;
419 const uint32_t *p = buff;
420
421 /* Get space for the header */
422 if (!wait_write_slots(1))
423 return 0;
424
425 /* First, write header */
426 write_slot(hdr);
427
428 pend_len = hdr_get_length(hdr);
429 pend_slots = bytes_to_slots(pend_len);
430
431 if (!wait_write_slots(pend_slots))
432 return 0;
433
434 /* Write the body in whole slots */
435 i = 0;
436 while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) {
437 write_slot(*p++);
438 i += SLOT_SIZE;
439 }
440
441 remainder = pend_len % SLOT_SIZE;
442 /* Pad to 4 bytes not touching caller's buffer */
443 if (remainder) {
444 memcpy(&tmp, p, remainder);
445 write_slot(tmp);
446 }
447
448 host_gen_interrupt();
449
450 /* Make sure nothing bad happened during transmission */
451 if (!cse_ready())
452 return 0;
453
454 return pend_len;
455 }
456
457 enum cse_tx_rx_status
458 heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr)
459 {
460 uint8_t retry;
461 uint32_t csr, hdr;
462 size_t sent, remaining, cb_size, max_length;
463 const uint8_t *p;
464
465 if (!msg || !len)
466 return CSE_TX_ERR_INPUT;
467
468 clear_int();
469
470 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
471 p = msg;
472
473 if (!wait_heci_ready()) {
474 printk(BIOS_ERR, "HECI: not ready\n");
475 continue;
476 }
477
478 csr = read_host_csr();
479 cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE;
480 /*
481 * Reserve one slot for the header. Limit max message
482 * length by 9 bits that are available in the header.
483 */
484 max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1)
485 - SLOT_SIZE;
486 remaining = len;
487
488 /*
489 * Fragment the message into smaller messages not exceeding
490 * useful circular buffer length. Mark last message complete.
491 */
492 do {
493 hdr = MIN(max_length, remaining)
494 << MEI_HDR_LENGTH_START;
495 hdr |= client_addr << MEI_HDR_CSE_ADDR_START;
496 hdr |= host_addr << MEI_HDR_HOST_ADDR_START;
497 hdr |= (MIN(max_length, remaining) == remaining) ?
498 MEI_HDR_IS_COMPLETE : 0;
499 sent = send_one_message(hdr, p);
500 p += sent;
501 remaining -= sent;
502 } while (remaining > 0 && sent != 0);
503
504 if (!remaining)
505 return CSE_TX_RX_SUCCESS;
506 }
507
508 printk(BIOS_DEBUG, "HECI: Trigger HECI reset\n");
509 heci_reset();
510 return CSE_TX_ERR_CSE_NOT_READY;
511 }
512
513 static enum cse_tx_rx_status
514 recv_one_message(uint32_t *hdr, void *buff, size_t maxlen, size_t *recv_len)
515 {
516 uint32_t reg, *p = buff;
517 size_t recv_slots, remainder, i;
518
519 /* first get the header */
520 if (!wait_read_slots(1))
521 return CSE_RX_ERR_TIMEOUT;
522
523 *hdr = read_slot();
524 *recv_len = hdr_get_length(*hdr);
525
526 if (!*recv_len)
527 printk(BIOS_WARNING, "HECI: message is zero-sized\n");
528
529 recv_slots = bytes_to_slots(*recv_len);
530
531 i = 0;
532 if (*recv_len > maxlen) {
533 printk(BIOS_ERR, "HECI: response is too big\n");
534 return CSE_RX_ERR_RESP_LEN_MISMATCH;
535 }
536
537 /* wait for the rest of messages to arrive */
538 wait_read_slots(recv_slots);
539
540 /* fetch whole slots first */
541 while (i < ALIGN_DOWN(*recv_len, SLOT_SIZE)) {
542 *p++ = read_slot();
543 i += SLOT_SIZE;
544 }
545
546 /*
547 * If ME is not ready, something went wrong and
548 * we received junk
549 */
550 if (!cse_ready())
551 return CSE_RX_ERR_CSE_NOT_READY;
552
553 remainder = *recv_len % SLOT_SIZE;
554
555 if (remainder) {
556 reg = read_slot();
557 memcpy(p, &reg, remainder);
558 }
559 return CSE_TX_RX_SUCCESS;
560 }
561
562 enum cse_tx_rx_status heci_receive(void *buff, size_t *maxlen)
563 {
564 uint8_t retry;
565 size_t left, received;
566 uint32_t hdr = 0;
567 uint8_t *p;
568 enum cse_tx_rx_status ret = CSE_RX_ERR_TIMEOUT;
569
570 if (!buff || !maxlen || !*maxlen)
571 return CSE_RX_ERR_INPUT;
572
573 clear_int();
574
575 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
576 p = buff;
577 left = *maxlen;
578
579 if (!wait_heci_ready()) {
580 printk(BIOS_ERR, "HECI: not ready\n");
581 continue;
582 }
583
584 /*
585 * Receive multiple packets until we meet one marked
586 * complete or we run out of space in caller-provided buffer.
587 */
588 do {
589 ret = recv_one_message(&hdr, p, left, &received);
590 if (ret) {
591 printk(BIOS_ERR, "HECI: Failed to receive!\n");
592 goto CSE_RX_ERR_HANDLE;
593 }
594 left -= received;
595 p += received;
596 /* If we read out everything ping to send more */
597 if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots())
598 host_gen_interrupt();
599 } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0);
600
601 if ((hdr & MEI_HDR_IS_COMPLETE) && received) {
602 *maxlen = p - (uint8_t *)buff;
603 if (CONFIG(SOC_INTEL_CSE_SERVER_SKU))
604 clear_int();
605 return CSE_TX_RX_SUCCESS;
606 }
607 }
608
609 CSE_RX_ERR_HANDLE:
610 printk(BIOS_DEBUG, "HECI: Trigger HECI Reset\n");
611 heci_reset();
612 return CSE_RX_ERR_CSE_NOT_READY;
613 }
614
615 enum cse_tx_rx_status heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg,
616 size_t *rcv_sz, uint8_t cse_addr)
617 {
618 enum cse_tx_rx_status ret;
619
620 ret = heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, cse_addr);
621 if (ret) {
622 printk(BIOS_ERR, "HECI: send Failed\n");
623 return ret;
624 }
625
626 if (rcv_msg != NULL) {
627 ret = heci_receive(rcv_msg, rcv_sz);
628 if (ret) {
629 printk(BIOS_ERR, "HECI: receive Failed\n");
630 return ret;
631 }
632 }
633 return ret;
634 }
635
636 /*
637 * Attempt to reset the device. This is useful when host and ME are out
638 * of sync during transmission or ME didn't understand the message.
639 */
640 int heci_reset(void)
641 {
642 uint32_t csr;
643
644 /* Clear post code to prevent eventlog entry from unknown code. */
645 post_code(POSTCODE_CODE_ZERO);
646
647 /* Send reset request */
648 csr = read_host_csr();
649 csr |= (CSR_RESET | CSR_IG);
650 write_host_csr(csr);
651
652 if (wait_heci_ready()) {
653 /* Device is back on its imaginary feet, clear reset */
654 cse_set_host_ready();
655 return 1;
656 }
657
658 printk(BIOS_CRIT, "HECI: reset failed\n");
659
660 return 0;
661 }
662
663 bool is_cse_devfn_visible(unsigned int devfn)
664 {
665 int slot = PCI_SLOT(devfn);
666 int func = PCI_FUNC(devfn);
667
668 if (!is_devfn_enabled(devfn)) {
669 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is disabled\n", slot, func);
670 return false;
671 }
672
673 if (pci_read_config16(PCI_DEV(0, slot, func), PCI_VENDOR_ID) == 0xFFFF) {
674 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is hidden\n", slot, func);
675 return false;
676 }
677
678 return true;
679 }
680
681 bool is_cse_enabled(void)
682 {
683 return is_cse_devfn_visible(PCH_DEVFN_CSE);
684 }
685
686 uint32_t me_read_config32(int offset)
687 {
688 return pci_read_config32(PCH_DEV_CSE, offset);
689 }
690
691 static bool cse_is_global_reset_allowed(void)
692 {
693 /*
694 * Allow sending GLOBAL_RESET command only if:
695 * - CSE's current working state is Normal and current operation mode is Normal.
696 * - (or) CSE's current working state is normal and current operation mode can
697 * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is
698 * Lite.
699 */
700 if (!cse_is_hfs1_cws_normal())
701 return false;
702
703 if (cse_is_hfs1_com_normal())
704 return true;
705
706 if (cse_is_hfs3_fw_sku_lite()) {
707 if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg())
708 return true;
709 }
710 return false;
711 }
712
713 /*
714 * Sends GLOBAL_RESET_REQ cmd to CSE with reset type GLOBAL_RESET.
715 * Returns 0 on failure and 1 on success.
716 */
717 static int cse_request_reset(enum rst_req_type rst_type)
718 {
719 int status;
720 struct mkhi_hdr reply;
721 struct reset_message {
722 struct mkhi_hdr hdr;
723 uint8_t req_origin;
724 uint8_t reset_type;
725 } __packed;
726 struct reset_message msg = {
727 .hdr = {
728 .group_id = MKHI_GROUP_ID_CBM,
729 .command = MKHI_CBM_GLOBAL_RESET_REQ,
730 },
731 .req_origin = GR_ORIGIN_BIOS_POST,
732 .reset_type = rst_type
733 };
734 size_t reply_size;
735
736 printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type);
737
738 if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) {
739 printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n");
740 return 0;
741 }
742
743 if (!cse_is_global_reset_allowed() || !is_cse_enabled()) {
744 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
745 return 0;
746 }
747
748 heci_reset();
749
750 reply_size = sizeof(reply);
751 memset(&reply, 0, reply_size);
752
753 if (rst_type == CSE_RESET_ONLY)
754 status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
755 else
756 status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size,
757 HECI_MKHI_ADDR);
758
759 printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", !status ? "success" : "failure");
760 return status;
761 }
762
763 int cse_request_global_reset(void)
764 {
765 return cse_request_reset(GLOBAL_RESET);
766 }
767
768 static bool cse_is_hmrfpo_enable_allowed(void)
769 {
770 /*
771 * Allow sending HMRFPO ENABLE command only if:
772 * - CSE's current working state is Normal and current operation mode is Normal
773 * - (or) cse's current working state is normal and current operation mode is
774 * Soft Temp Disable if CSE's Firmware SKU is Lite
775 */
776 if (!cse_is_hfs1_cws_normal())
777 return false;
778
779 if (cse_is_hfs1_com_normal())
780 return true;
781
782 if (cse_is_hfs3_fw_sku_lite() && cse_is_hfs1_com_soft_temp_disable())
783 return true;
784
785 return false;
786 }
787
788 /* Sends HMRFPO Enable command to CSE */
789 enum cb_err cse_hmrfpo_enable(void)
790 {
791 struct hmrfpo_enable_msg {
792 struct mkhi_hdr hdr;
793 uint32_t nonce[2];
794 } __packed;
795
796 /* HMRFPO Enable message */
797 struct hmrfpo_enable_msg msg = {
798 .hdr = {
799 .group_id = MKHI_GROUP_ID_HMRFPO,
800 .command = MKHI_HMRFPO_ENABLE,
801 },
802 .nonce = {0},
803 };
804
805 /* HMRFPO Enable response */
806 struct hmrfpo_enable_resp {
807 struct mkhi_hdr hdr;
808 /* Base addr for factory data area, not relevant for client SKUs */
809 uint32_t fct_base;
810 /* Length of factory data area, not relevant for client SKUs */
811 uint32_t fct_limit;
812 uint8_t status;
813 uint8_t reserved[3];
814 } __packed;
815
816 struct hmrfpo_enable_resp resp;
817 size_t resp_size = sizeof(struct hmrfpo_enable_resp);
818
819 if (cse_is_hfs1_com_secover_mei_msg()) {
820 printk(BIOS_DEBUG, "HECI: CSE is already in security override mode, "
821 "skip sending HMRFPO_ENABLE command to CSE\n");
822 return CB_SUCCESS;
823 }
824
825 printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n");
826
827 if (!cse_is_hmrfpo_enable_allowed()) {
828 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
829 return CB_ERR;
830 }
831
832 if (heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg),
833 &resp, &resp_size, HECI_MKHI_ADDR))
834 return CB_ERR;
835
836 if (resp.hdr.result) {
837 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
838 return CB_ERR;
839 }
840
841 if (resp.status) {
842 printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status);
843 return CB_ERR;
844 }
845
846 return CB_SUCCESS;
847 }
848
849 /*
850 * Sends HMRFPO Get Status command to CSE to get the HMRFPO status.
851 * The status can be DISABLED/LOCKED/ENABLED
852 */
853 int cse_hmrfpo_get_status(void)
854 {
855 struct hmrfpo_get_status_msg {
856 struct mkhi_hdr hdr;
857 } __packed;
858
859 struct hmrfpo_get_status_resp {
860 struct mkhi_hdr hdr;
861 uint8_t status;
862 uint8_t reserved[3];
863 } __packed;
864
865 struct hmrfpo_get_status_msg msg = {
866 .hdr = {
867 .group_id = MKHI_GROUP_ID_HMRFPO,
868 .command = MKHI_HMRFPO_GET_STATUS,
869 },
870 };
871 struct hmrfpo_get_status_resp resp;
872 size_t resp_size = sizeof(struct hmrfpo_get_status_resp);
873
874 printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n");
875
876 if (!cse_is_hfs1_cws_normal()) {
877 printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n");
878 return -1;
879 }
880
881 if (heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg),
882 &resp, &resp_size, HECI_MKHI_ADDR)) {
883 printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n");
884 return -1;
885 }
886
887 if (resp.hdr.result) {
888 printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n",
889 resp.hdr.result);
890 return -1;
891 }
892
893 return resp.status;
894 }
895
896 /* Queries and gets ME firmware version */
897 static enum cb_err get_me_fw_version(struct me_fw_ver_resp *resp)
898 {
899 const struct mkhi_hdr fw_ver_msg = {
900 .group_id = MKHI_GROUP_ID_GEN,
901 .command = MKHI_GEN_GET_FW_VERSION,
902 };
903
904 if (resp == NULL) {
905 printk(BIOS_ERR, "%s failed, null pointer parameter\n", __func__);
906 return CB_ERR;
907 }
908 size_t resp_size = sizeof(*resp);
909
910 /* Ignore if CSE is disabled */
911 if (!is_cse_enabled())
912 return CB_ERR;
913
914 /*
915 * Prerequisites:
916 * 1) HFSTS1 Current Working State is Normal
917 * 2) HFSTS1 Current Operation Mode is Normal
918 * 3) It's after DRAM INIT DONE message (taken care of by calling it
919 * during ramstage
920 */
921 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal())
922 return CB_ERR;
923
924 heci_reset();
925
926 if (heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), resp, &resp_size,
927 HECI_MKHI_ADDR))
928 return CB_ERR;
929
930 if (resp->hdr.result)
931 return CB_ERR;
932
933
934 return CB_SUCCESS;
935 }
936
937 void print_me_fw_version(void *unused)
938 {
939 struct me_fw_ver_resp resp = {0};
940
941 /* Ignore if UART debugging is disabled */
942 if (!CONFIG(CONSOLE_SERIAL))
943 return;
944
945 /*
946 * Skip if ME firmware is Lite SKU, as RO/RW versions are
947 * already logged by `cse_print_boot_partition_info()`
948 */
949 if (cse_is_hfs3_fw_sku_lite())
950 return;
951
952 if (get_me_fw_version(&resp) == CB_SUCCESS) {
953 printk(BIOS_DEBUG, "ME: Version: %d.%d.%d.%d\n", resp.code.major,
954 resp.code.minor, resp.code.hotfix, resp.code.build);
955 return;
956 }
957 printk(BIOS_DEBUG, "ME: Version: Unavailable\n");
958 }
959
960 void cse_trigger_vboot_recovery(enum csme_failure_reason reason)
961 {
962 printk(BIOS_DEBUG, "cse: CSE status registers: HFSTS1: 0x%x, HFSTS2: 0x%x "
963 "HFSTS3: 0x%x\n", me_read_config32(PCI_ME_HFSTS1),
964 me_read_config32(PCI_ME_HFSTS2), me_read_config32(PCI_ME_HFSTS3));
965
966 if (CONFIG(VBOOT))
967 vboot_fail_and_reboot(vboot_get_context(), VB2_RECOVERY_INTEL_CSE_LITE_SKU,
968 reason);
969
970 die("cse: Failed to trigger recovery mode(recovery subcode:%d)\n", reason);
971 }
972
973 static bool disable_cse_idle(pci_devfn_t dev)
974 {
975 struct stopwatch sw;
976 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
977 dev_idle_ctrl &= ~CSE_DEV_IDLE;
978 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
979
980 stopwatch_init_usecs_expire(&sw, HECI_CIP_TIMEOUT_US);
981 do {
982 dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
983 if ((dev_idle_ctrl & CSE_DEV_CIP) == CSE_DEV_CIP)
984 return true;
985 udelay(HECI_DELAY_US);
986 } while (!stopwatch_expired(&sw));
987
988 return false;
989 }
990
991 static void enable_cse_idle(pci_devfn_t dev)
992 {
993 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
994 dev_idle_ctrl |= CSE_DEV_IDLE;
995 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
996 }
997
998 enum cse_device_state get_cse_device_state(unsigned int devfn)
999 {
1000 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
1001 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
1002 if ((dev_idle_ctrl & CSE_DEV_IDLE) == CSE_DEV_IDLE)
1003 return DEV_IDLE;
1004
1005 return DEV_ACTIVE;
1006 }
1007
1008 static enum cse_device_state ensure_cse_active(pci_devfn_t dev)
1009 {
1010 if (!disable_cse_idle(dev))
1011 return DEV_IDLE;
1012 pci_or_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1013
1014 return DEV_ACTIVE;
1015 }
1016
1017 static void ensure_cse_idle(pci_devfn_t dev)
1018 {
1019 enable_cse_idle(dev);
1020
1021 pci_and_config32(dev, PCI_COMMAND, ~(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
1022 }
1023
1024 bool set_cse_device_state(unsigned int devfn, enum cse_device_state requested_state)
1025 {
1026 enum cse_device_state current_state = get_cse_device_state(devfn);
1027 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
1028
1029 if (current_state == requested_state)
1030 return true;
1031
1032 if (requested_state == DEV_ACTIVE)
1033 return ensure_cse_active(dev) == requested_state;
1034 else
1035 ensure_cse_idle(dev);
1036
1037 return true;
1038 }
1039
1040 void cse_set_to_d0i3(void)
1041 {
1042 if (!is_cse_devfn_visible(PCH_DEVFN_CSE))
1043 return;
1044
1045 set_cse_device_state(PCH_DEVFN_CSE, DEV_IDLE);
1046 }
1047
1048 /* Function to set D0I3 for all HECI devices */
1049 void heci_set_to_d0i3(void)
1050 {
1051 for (int i = 0; i < CONFIG_MAX_HECI_DEVICES; i++) {
1052 unsigned int devfn = PCI_DEVFN(PCH_DEV_SLOT_CSE, i);
1053 if (!is_cse_devfn_visible(devfn))
1054 continue;
1055
1056 set_cse_device_state(devfn, DEV_IDLE);
1057 }
1058 }
1059
1060 /* Initialize the HECI devices. */
1061 void heci_init(void)
1062 {
1063 for (int i = 0; i < CONFIG_MAX_HECI_DEVICES; i++) {
1064 unsigned int devfn = PCI_DEVFN(PCH_DEV_SLOT_CSE, i);
1065 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
1066
1067 if (!is_cse_devfn_visible(devfn))
1068 continue;
1069
1070 /* Assume it is already initialized, nothing else to do */
1071 if (get_cse_bar(dev))
1072 return;
1073
1074 heci_assign_resource(dev, HECI1_BASE_ADDRESS + (i * HECI_BASE_SIZE));
1075
1076 ensure_cse_active(dev);
1077 }
1078 /* Trigger HECI Reset and make Host ready for communication with CSE */
1079 heci_reset();
1080 }
1081
1082 void cse_control_global_reset_lock(void)
1083 {
1084 /*
1085 * As per ME BWG recommendation the BIOS should not lock down CF9GR bit during
1086 * manufacturing and re-manufacturing environment if HFSTS1 [4] is set. Note:
1087 * this recommendation is not applicable for CSE-Lite SKUs where BIOS should set
1088 * CF9LOCK bit irrespectively.
1089 *
1090 * Other than that, make sure payload/OS can't trigger global reset.
1091 *
1092 * BIOS must also ensure that CF9GR is cleared and locked (Bit31 of ETR3)
1093 * prior to transferring control to the OS.
1094 */
1095 if (CONFIG(SOC_INTEL_CSE_LITE_SKU) || cse_is_hfs1_spi_protected())
1096 pmc_global_reset_disable_and_lock();
1097 else
1098 pmc_global_reset_enable(false);
1099 }
1100
1101 enum cb_err cse_get_fw_feature_state(uint32_t *feature_state)
1102 {
1103 struct fw_feature_state_msg {
1104 struct mkhi_hdr hdr;
1105 uint32_t rule_id;
1106 } __packed;
1107
1108 /* Get Firmware Feature State message */
1109 struct fw_feature_state_msg msg = {
1110 .hdr = {
1111 .group_id = MKHI_GROUP_ID_FWCAPS,
1112 .command = MKHI_FWCAPS_GET_FW_FEATURE_STATE,
1113 },
1114 .rule_id = ME_FEATURE_STATE_RULE_ID
1115 };
1116
1117 /* Get Firmware Feature State response */
1118 struct fw_feature_state_resp {
1119 struct mkhi_hdr hdr;
1120 uint32_t rule_id;
1121 uint8_t rule_len;
1122 uint32_t fw_runtime_status;
1123 } __packed;
1124
1125 struct fw_feature_state_resp resp;
1126 size_t resp_size = sizeof(struct fw_feature_state_resp);
1127
1128 /* Ignore if CSE is disabled or input buffer is invalid */
1129 if (!is_cse_enabled() || !feature_state)
1130 return CB_ERR;
1131
1132 /*
1133 * Prerequisites:
1134 * 1) HFSTS1 Current Working State is Normal
1135 * 2) HFSTS1 Current Operation Mode is Normal
1136 * 3) It's after DRAM INIT DONE message (taken care of by calling it
1137 * during ramstage)
1138 */
1139 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal() || !ENV_RAMSTAGE)
1140 return CB_ERR;
1141
1142 printk(BIOS_DEBUG, "HECI: Send GET FW FEATURE STATE Command\n");
1143
1144 if (heci_send_receive(&msg, sizeof(struct fw_feature_state_msg),
1145 &resp, &resp_size, HECI_MKHI_ADDR))
1146 return CB_ERR;
1147
1148 if (resp.hdr.result) {
1149 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
1150 return CB_ERR;
1151 }
1152
1153 if (resp.rule_len != sizeof(resp.fw_runtime_status)) {
1154 printk(BIOS_ERR, "HECI: GET FW FEATURE STATE has invalid rule data length\n");
1155 return CB_ERR;
1156 }
1157
1158 *feature_state = resp.fw_runtime_status;
1159
1160 return CB_SUCCESS;
1161 }
1162
1163 void cse_enable_ptt(bool state)
1164 {
1165 struct fw_feature_shipment_override_msg {
1166 struct mkhi_hdr hdr;
1167 uint32_t enable_mask;
1168 uint32_t disable_mask;
1169 } __packed;
1170
1171 /* FW Feature Shipment Time State Override message */
1172 struct fw_feature_shipment_override_msg msg = {
1173 .hdr = {
1174 .group_id = MKHI_GROUP_ID_GEN,
1175 .command = MKHI_GEN_FW_FEATURE_SHIPMENT_OVER,
1176 },
1177 .enable_mask = 0,
1178 .disable_mask = 0
1179 };
1180
1181 /* FW Feature Shipment Time State Override response */
1182 struct fw_feature_shipment_override_resp {
1183 struct mkhi_hdr hdr;
1184 uint32_t data;
1185 } __packed;
1186
1187 struct fw_feature_shipment_override_resp resp;
1188 size_t resp_size = sizeof(struct fw_feature_shipment_override_resp);
1189 uint32_t feature_status;
1190
1191 /* Ignore if CSE is disabled */
1192 if (!is_cse_enabled())
1193 return;
1194
1195 printk(BIOS_DEBUG, "Requested to change PTT state to %sabled\n", state ? "en" : "dis");
1196
1197 /*
1198 * Prerequisites:
1199 * 1) HFSTS1 Current Working State is Normal
1200 * 2) HFSTS1 Current Operation Mode is Normal
1201 * 3) It's after DRAM INIT DONE message (taken care of by calling it
1202 * during ramstage
1203 * 4) HFSTS1 FW Init Complete is set
1204 * 5) Before EOP issued to CSE
1205 */
1206 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal() || !ENV_RAMSTAGE) {
1207 printk(BIOS_ERR, "HECI: Could not set PTT state because ME is not ready\n");
1208 return;
1209 }
1210
1211 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
1212 printk(BIOS_ERR, "HECI: Cannot determine current feature status\n");
1213 return;
1214 }
1215
1216 if (!!(feature_status & ME_FW_FEATURE_PTT) == state) {
1217 printk(BIOS_DEBUG, "HECI: PTT is already in the requested state\n");
1218 return;
1219 }
1220
1221 int elapsed = wait_ms(CSE_FW_INIT_TIMEOUT_MS, cse_is_hfs1_fw_init_complete());
1222 if (!elapsed) {
1223 printk(BIOS_ERR, "HECI: Could not set PTT state because ME is not ready\n");
1224 return;
1225 }
1226
1227 printk(BIOS_DEBUG, "HECI: CSE took %d ms to become ready\n", elapsed);
1228
1229 printk(BIOS_DEBUG, "HECI: Send FW FEATURE SHIPMENT TIME STATE OVERRIDE Command\n");
1230
1231 if (state)
1232 msg.enable_mask |= ME_FW_FEATURE_PTT;
1233 else
1234 msg.disable_mask |= ME_FW_FEATURE_PTT;
1235
1236 if (heci_send_receive(&msg, sizeof(struct fw_feature_shipment_override_msg),
1237 &resp, &resp_size, HECI_MKHI_ADDR))
1238 return;
1239
1240 if (resp.hdr.result) {
1241 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
1242 return;
1243 }
1244
1245 /* Global reset is required after acceptance of the command */
1246 if (resp.data == 0) {
1247 printk(BIOS_DEBUG, "HECI: FW FEATURE SHIPMENT TIME STATE OVERRIDE success\n");
1248 do_global_reset();
1249 } else {
1250 printk(BIOS_ERR, "HECI: FW FEATURE SHIPMENT TIME STATE OVERRIDE error (%x)\n",
1251 resp.data);
1252 }
1253 }
1254
1255 #if ENV_RAMSTAGE
1256
1257 /*
1258 * Disable the Intel (CS)Management Engine via HECI based on a cmos value
1259 * of `me_state`. A value of `0` will result in a (CS)ME state of `0` (working)
1260 * and value of `1` will result in a (CS)ME state of `3` (disabled).
1261 *
1262 * It isn't advised to use this in combination with me_cleaner.
1263 *
1264 * It is advisable to have a second cmos option called `me_state_counter`.
1265 * Whilst not essential, it avoid reboots loops if the (CS)ME fails to
1266 * change states after 3 attempts. Some versions of the (CS)ME need to be
1267 * reset 3 times.
1268 *
1269 * Ideal cmos values would be:
1270 *
1271 * # coreboot config options: cpu
1272 * 432 1 e 5 me_state
1273 * 440 4 h 0 me_state_counter
1274 *
1275 * #ID value text
1276 * 5 0 Enable
1277 * 5 1 Disable
1278 */
1279
1280 static void me_reset_with_count(void)
1281 {
1282 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter", UINT_MAX);
1283
1284 if (cmos_me_state_counter != UINT_MAX) {
1285 printk(BIOS_DEBUG, "CMOS: me_state_counter = %u\n", cmos_me_state_counter);
1286 /* Avoid boot loops by only trying a state change 3 times */
1287 if (cmos_me_state_counter < ME_DISABLE_ATTEMPTS) {
1288 cmos_me_state_counter++;
1289 set_uint_option("me_state_counter", cmos_me_state_counter);
1290 printk(BIOS_DEBUG, "ME: Reset attempt %u/%u.\n", cmos_me_state_counter,
1291 ME_DISABLE_ATTEMPTS);
1292 do_global_reset();
1293 } else {
1294 /*
1295 * If the (CS)ME fails to change states after 3 attempts, it will
1296 * likely need a cold boot, or recovering.
1297 */
1298 printk(BIOS_ERR, "Failed to change ME state in %u attempts!\n",
1299 ME_DISABLE_ATTEMPTS);
1300 }
1301 } else {
1302 printk(BIOS_DEBUG, "ME: Resetting");
1303 do_global_reset();
1304 }
1305 }
1306
1307 static void cse_set_state(struct device *dev)
1308 {
1309 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1310 return;
1311
1312 /* (CS)ME Disable Command */
1313 struct me_disable_command {
1314 struct mkhi_hdr hdr;
1315 uint32_t rule_id;
1316 uint8_t rule_len;
1317 uint32_t rule_data;
1318 } __packed me_disable = {
1319 .hdr = {
1320 .group_id = MKHI_GROUP_ID_FWCAPS,
1321 .command = MKHI_SET_ME_DISABLE,
1322 },
1323 .rule_id = ME_DISABLE_RULE_ID,
1324 .rule_len = ME_DISABLE_RULE_LENGTH,
1325 .rule_data = ME_DISABLE_COMMAND,
1326 };
1327
1328 struct me_disable_reply {
1329 struct mkhi_hdr hdr;
1330 uint32_t rule_id;
1331 } __packed;
1332
1333 struct me_disable_reply disable_reply;
1334
1335 size_t disable_reply_size;
1336
1337 /* (CS)ME Enable Command */
1338 struct me_enable_command {
1339 struct mkhi_hdr hdr;
1340 } me_enable = {
1341 .hdr = {
1342 .group_id = MKHI_GROUP_ID_BUP_COMMON,
1343 .command = MKHI_SET_ME_ENABLE,
1344 },
1345 };
1346
1347 struct me_enable_reply {
1348 struct mkhi_hdr hdr;
1349 } __packed;
1350
1351 struct me_enable_reply enable_reply;
1352
1353 size_t enable_reply_size;
1354
1355 /* Function Start */
1356
1357 int send;
1358 int result;
1359
1360 if (fast_spi_flash_descriptor_override()) {
1361 printk(BIOS_WARNING, "HECI: not setting ME state because "
1362 "flash descriptor override is enabled\n");
1363 return;
1364 }
1365
1366 /*
1367 * Check if the CMOS value "me_state" exists, if it doesn't, then
1368 * don't do anything.
1369 */
1370 const unsigned int cmos_me_state = get_uint_option("me_state", UINT_MAX);
1371
1372 if (cmos_me_state == UINT_MAX)
1373 return;
1374
1375 printk(BIOS_DEBUG, "CMOS: me_state = %u\n", cmos_me_state);
1376
1377 /*
1378 * We only take action if the me_state doesn't match the CS(ME) working state
1379 */
1380
1381 const unsigned int soft_temp_disable = cse_is_hfs1_com_soft_temp_disable();
1382
1383 if (cmos_me_state && !soft_temp_disable) {
1384 /* me_state should be disabled, but it's enabled */
1385 printk(BIOS_DEBUG, "ME needs to be disabled.\n");
1386 send = heci_send_receive(&me_disable, sizeof(me_disable),
1387 &disable_reply, &disable_reply_size, HECI_MKHI_ADDR);
1388 result = disable_reply.hdr.result;
1389 } else if (!cmos_me_state && soft_temp_disable) {
1390 /* me_state should be enabled, but it's disabled */
1391 printk(BIOS_DEBUG, "ME needs to be enabled.\n");
1392 send = heci_send_receive(&me_enable, sizeof(me_enable),
1393 &enable_reply, &enable_reply_size, HECI_MKHI_ADDR);
1394 result = enable_reply.hdr.result;
1395 } else {
1396 printk(BIOS_DEBUG, "ME is %s.\n", cmos_me_state ? "disabled" : "enabled");
1397 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter",
1398 UINT_MAX);
1399 /* set me_state_counter to 0 */
1400 if ((cmos_me_state_counter != UINT_MAX && cmos_me_state_counter != 0))
1401 set_uint_option("me_state_counter", 0);
1402 return;
1403 }
1404
1405 printk(BIOS_DEBUG, "HECI: ME state change send %s!\n",
1406 !send ? "success" : "failure");
1407 printk(BIOS_DEBUG, "HECI: ME state change result %s!\n",
1408 result ? "success" : "failure");
1409
1410 /*
1411 * Reset if the result was successful, or if the send failed as some older
1412 * version of the Intel (CS)ME won't successfully receive the message unless reset
1413 * twice.
1414 */
1415 if (send || !result)
1416 me_reset_with_count();
1417 }
1418
1419 /*
1420 * `cse_final_ready_to_boot` function is native implementation of equivalent events
1421 * performed by FSP NotifyPhase(Ready To Boot) API invocations.
1422 *
1423 * Operations are:
1424 * 1. Perform global reset lock.
1425 * 2. Put HECI1 to D0i3 and disable the HECI1 if the user selects
1426 * DISABLE_HECI1_AT_PRE_BOOT config or CSE HFSTS1 Operation Mode is
1427 * `Software Temporary Disable`.
1428 */
1429 static void cse_final_ready_to_boot(void)
1430 {
1431 cse_control_global_reset_lock();
1432
1433 if (CONFIG(DISABLE_HECI1_AT_PRE_BOOT) || cse_is_hfs1_com_soft_temp_disable()) {
1434 cse_set_to_d0i3();
1435 heci1_disable();
1436 }
1437 }
1438
1439 /*
1440 * `cse_final_end_of_firmware` function is native implementation of equivalent events
1441 * performed by FSP NotifyPhase(End of Firmware) API invocations.
1442 *
1443 * Operations are:
1444 * 1. Set D0I3 for all HECI devices.
1445 */
1446 static void cse_final_end_of_firmware(void)
1447 {
1448 heci_set_to_d0i3();
1449 }
1450
1451 /*
1452 * This function to perform essential post EOP cse related operations
1453 * upon SoC selecting `SOC_INTEL_CSE_SEND_EOP_LATE` config
1454 */
1455 void cse_late_finalize(void)
1456 {
1457 if (!CONFIG(SOC_INTEL_CSE_SEND_EOP_LATE) &&
1458 !CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
1459 return;
1460
1461 if (!CONFIG(USE_FSP_NOTIFY_PHASE_READY_TO_BOOT))
1462 cse_final_ready_to_boot();
1463
1464 if (!CONFIG(USE_FSP_NOTIFY_PHASE_END_OF_FIRMWARE))
1465 cse_final_end_of_firmware();
1466 }
1467
1468 static void intel_cse_get_rw_version(void)
1469 {
1470 if (CONFIG(SOC_INTEL_CSE_LITE_SYNC_BY_PAYLOAD))
1471 return;
1472
1473 struct cse_specific_info *info = cbmem_find(CBMEM_ID_CSE_INFO);
1474 if (info == NULL)
1475 return;
1476
1477 printk(BIOS_DEBUG, "CSE RW Firmware Version: %d.%d.%d.%d\n",
1478 info->cse_fwp_version.cur_cse_fw_version.major,
1479 info->cse_fwp_version.cur_cse_fw_version.minor,
1480 info->cse_fwp_version.cur_cse_fw_version.hotfix,
1481 info->cse_fwp_version.cur_cse_fw_version.build);
1482 }
1483
1484 /*
1485 * `cse_final` function is native implementation of equivalent events performed by
1486 * each FSP NotifyPhase() API invocations. It also displays CSE firmware version
1487 * if stored in CBMEM region.
1488 */
1489 static void cse_final(struct device *dev)
1490 {
1491 if (CONFIG(SOC_INTEL_STORE_CSE_FW_VERSION))
1492 intel_cse_get_rw_version();
1493 /*
1494 * SoC user can have three options for sending EOP:
1495 * 1. Choose to send EOP late
1496 * 2. Choose to send EOP cmd asynchronously
1497 * 3. Choose to send EOP cmd from payload i.e. skip here
1498 *
1499 * In case of sending EOP in asynchronous mode, the EOP command
1500 * has most likely not been completed yet. The finalization steps
1501 * will be run once the EOP command has successfully been completed.
1502 */
1503 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_LATE) ||
1504 CONFIG(SOC_INTEL_CSE_SEND_EOP_BY_PAYLOAD) ||
1505 CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
1506 return;
1507
1508 /* 1. Send EOP to CSE if not done.*/
1509 if (CONFIG(SOC_INTEL_CSE_SET_EOP))
1510 cse_send_end_of_post();
1511
1512 if (!CONFIG(USE_FSP_NOTIFY_PHASE_READY_TO_BOOT))
1513 cse_final_ready_to_boot();
1514
1515 if (!CONFIG(USE_FSP_NOTIFY_PHASE_END_OF_FIRMWARE))
1516 cse_final_end_of_firmware();
1517 }
1518
1519 struct device_operations cse_ops = {
1520 .set_resources = pci_dev_set_resources,
1521 .read_resources = pci_dev_read_resources,
1522 .enable_resources = pci_dev_enable_resources,
1523 .init = pci_dev_init,
1524 .ops_pci = &pci_dev_ops_pci,
1525 .enable = cse_set_state,
1526 .final = cse_final,
1527 };
1528
1529 static const unsigned short pci_device_ids[] = {
1530 PCI_DID_INTEL_WCL_CSE0,
1531 PCI_DID_INTEL_PTL_H_CSE0,
1532 PCI_DID_INTEL_PTL_U_H_CSE0,
1533 PCI_DID_INTEL_LNL_CSE0,
1534 PCI_DID_INTEL_MTL_CSE0,
1535 PCI_DID_INTEL_ARL_CSE0,
1536 PCI_DID_INTEL_ARL_S_CSE0,
1537 PCI_DID_INTEL_ARL_S_CSE1,
1538 PCI_DID_INTEL_ARL_S_CSE2,
1539 PCI_DID_INTEL_ARP_S_CSE0,
1540 PCI_DID_INTEL_ARP_S_CSE1,
1541 PCI_DID_INTEL_ARP_S_CSE2,
1542 PCI_DID_INTEL_ARP_S_CSE3,
1543 PCI_DID_INTEL_APL_CSE0,
1544 PCI_DID_INTEL_GLK_CSE0,
1545 PCI_DID_INTEL_CNL_CSE0,
1546 PCI_DID_INTEL_LWB_CSE0,
1547 PCI_DID_INTEL_LWB_CSE0_SUPER,
1548 PCI_DID_INTEL_CNP_H_CSE0,
1549 PCI_DID_INTEL_CMP_CSE0,
1550 PCI_DID_INTEL_CMP_H_CSE0,
1551 PCI_DID_INTEL_TGL_CSE0,
1552 PCI_DID_INTEL_TGL_H_CSE0,
1553 PCI_DID_INTEL_MCC_CSE0,
1554 PCI_DID_INTEL_MCC_CSE1,
1555 PCI_DID_INTEL_MCC_CSE2,
1556 PCI_DID_INTEL_MCC_CSE3,
1557 PCI_DID_INTEL_JSP_CSE0,
1558 PCI_DID_INTEL_JSP_CSE1,
1559 PCI_DID_INTEL_JSP_CSE2,
1560 PCI_DID_INTEL_JSP_CSE3,
1561 PCI_DID_INTEL_ADP_P_CSE0,
1562 PCI_DID_INTEL_ADP_P_CSE1,
1563 PCI_DID_INTEL_ADP_P_CSE2,
1564 PCI_DID_INTEL_ADP_P_CSE3,
1565 PCI_DID_INTEL_ADP_S_CSE0,
1566 PCI_DID_INTEL_ADP_S_CSE1,
1567 PCI_DID_INTEL_ADP_S_CSE2,
1568 PCI_DID_INTEL_ADP_S_CSE3,
1569 PCI_DID_INTEL_ADP_M_CSE0,
1570 PCI_DID_INTEL_ADP_M_CSE1,
1571 PCI_DID_INTEL_ADP_M_CSE2,
1572 PCI_DID_INTEL_ADP_M_CSE3,
1573 PCI_DID_INTEL_RPP_S_CSE0,
1574 PCI_DID_INTEL_RPP_S_CSE1,
1575 PCI_DID_INTEL_RPP_S_CSE2,
1576 PCI_DID_INTEL_RPP_S_CSE3,
1577 0,
1578 };
1579
1580 static const struct pci_driver cse_driver __pci_driver = {
1581 .ops = &cse_ops,
1582 .vendor = PCI_VID_INTEL,
1583 /* SoC/chipset needs to provide PCI device ID */
1584 .devices = pci_device_ids
1585 };
1586
1587 #endif