blob: 6c4bb73ceea601294fe77c179caac49698d77a25 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Andrey Petrov04a72c42017-03-01 15:51:57 -08002
Subrata Banikc6e25522021-09-30 18:14:09 +05303#define __SIMPLE_DEVICE__
4
Subrata Banik05e06cd2017-11-09 15:04:09 +05305#include <assert.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -08006#include <commonlib/helpers.h>
7#include <console/console.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -08009#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>
Dinesh Gehlot7e396162023-02-20 06:26:17 +000014#include <intelblocks/me.h>
Subrata Banik80c92892022-02-01 00:26:55 +053015#include <intelblocks/pmclib.h>
Martin Roth8c974502022-11-20 17:56:44 -070016#include <intelblocks/post_codes.h>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010017#include <option.h>
Tim Wawrzynczak09635f42021-06-18 10:08:47 -060018#include <security/vboot/misc.h>
19#include <security/vboot/vboot_common.h>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010020#include <soc/intel/common/reset.h>
Subrata Banik05e06cd2017-11-09 15:04:09 +053021#include <soc/iomap.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080022#include <soc/pci_devs.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080023#include <string.h>
24#include <timer.h>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010025#include <types.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080026
Subrata Banik801dbf42022-06-01 07:56:40 +000027#define HECI_BASE_SIZE (4 * KiB)
28
Subrata Banik5c08c732017-11-13 14:54:37 +053029#define MAX_HECI_MESSAGE_RETRY_COUNT 5
30
Andrey Petrov04a72c42017-03-01 15:51:57 -080031/* Wait up to 15 sec for HECI to get ready */
Subrata Banik03aef282021-09-28 18:10:24 +053032#define HECI_DELAY_READY_MS (15 * 1000)
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +010033/* Wait up to 100 usec between circular buffer polls */
Subrata Banik03aef282021-09-28 18:10:24 +053034#define HECI_DELAY_US 100
Andrey Petrov04a72c42017-03-01 15:51:57 -080035/* Wait up to 5 sec for CSE to chew something we sent */
Subrata Banik03aef282021-09-28 18:10:24 +053036#define HECI_SEND_TIMEOUT_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080037/* Wait up to 5 sec for CSE to blurp a reply */
Subrata Banik03aef282021-09-28 18:10:24 +053038#define HECI_READ_TIMEOUT_MS (5 * 1000)
Subrata Banika219edb2021-09-25 15:02:37 +053039/* Wait up to 1 ms for CSE CIP */
Subrata Banik03aef282021-09-28 18:10:24 +053040#define HECI_CIP_TIMEOUT_US 1000
Subrata Banikf5765812021-09-30 13:37:10 +053041/* Wait up to 5 seconds for CSE to boot from RO(BP1) */
42#define CSE_DELAY_BOOT_TO_RO_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080043
44#define SLOT_SIZE sizeof(uint32_t)
45
46#define MMIO_CSE_CB_WW 0x00
47#define MMIO_HOST_CSR 0x04
48#define MMIO_CSE_CB_RW 0x08
49#define MMIO_CSE_CSR 0x0c
Subrata Banika219edb2021-09-25 15:02:37 +053050#define MMIO_CSE_DEVIDLE 0x800
51#define CSE_DEV_IDLE (1 << 2)
52#define CSE_DEV_CIP (1 << 0)
Andrey Petrov04a72c42017-03-01 15:51:57 -080053
54#define CSR_IE (1 << 0)
55#define CSR_IS (1 << 1)
56#define CSR_IG (1 << 2)
57#define CSR_READY (1 << 3)
58#define CSR_RESET (1 << 4)
59#define CSR_RP_START 8
60#define CSR_RP (((1 << 8) - 1) << CSR_RP_START)
61#define CSR_WP_START 16
62#define CSR_WP (((1 << 8) - 1) << CSR_WP_START)
63#define CSR_CBD_START 24
64#define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START)
65
66#define MEI_HDR_IS_COMPLETE (1 << 31)
67#define MEI_HDR_LENGTH_START 16
68#define MEI_HDR_LENGTH_SIZE 9
69#define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \
70 << MEI_HDR_LENGTH_START)
71#define MEI_HDR_HOST_ADDR_START 8
72#define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START)
73#define MEI_HDR_CSE_ADDR_START 0
74#define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START)
75
Subrata Banik38abbda2021-09-30 13:15:50 +053076/* Get HECI BAR 0 from PCI configuration space */
Subrata Banikc6e25522021-09-30 18:14:09 +053077static uintptr_t get_cse_bar(pci_devfn_t dev)
Subrata Banik38abbda2021-09-30 13:15:50 +053078{
79 uintptr_t bar;
80
Subrata Banikc6e25522021-09-30 18:14:09 +053081 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
Subrata Banik38abbda2021-09-30 13:15:50 +053082 assert(bar != 0);
83 /*
84 * Bits 31-12 are the base address as per EDS for SPI,
85 * Don't care about 0-11 bit
86 */
87 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
88}
Andrey Petrov04a72c42017-03-01 15:51:57 -080089
Subrata Banik801dbf42022-06-01 07:56:40 +000090static void heci_assign_resource(pci_devfn_t dev, uintptr_t tempbar)
91{
92 u16 pcireg;
93
94 /* Assign Resources */
95 /* Clear BIT 1-2 of Command Register */
96 pcireg = pci_read_config16(dev, PCI_COMMAND);
97 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
98 pci_write_config16(dev, PCI_COMMAND, pcireg);
99
100 /* Program Temporary BAR for HECI device */
101 pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar);
102 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0);
103
104 /* Enable Bus Master and MMIO Space */
105 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
106}
107
Andrey Petrov04a72c42017-03-01 15:51:57 -0800108/*
Subrata Banik0b92aa62022-06-01 06:54:44 +0000109 * Initialize the CSE device with provided temporary BAR. If BAR is 0 use a
Andrey Petrov04a72c42017-03-01 15:51:57 -0800110 * default. This is intended for pre-mem usage only where BARs haven't been
111 * assigned yet and devices are not enabled.
112 */
Subrata Banik0b92aa62022-06-01 06:54:44 +0000113void cse_init(uintptr_t tempbar)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800114{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200115 pci_devfn_t dev = PCH_DEV_CSE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530116
Matt DeVillierf711bf02022-01-25 19:48:38 -0600117 /* Check if device enabled */
118 if (!is_cse_enabled())
119 return;
120
Andrey Petrov04a72c42017-03-01 15:51:57 -0800121 /* Assume it is already initialized, nothing else to do */
Subrata Banikc6e25522021-09-30 18:14:09 +0530122 if (get_cse_bar(dev))
Andrey Petrov04a72c42017-03-01 15:51:57 -0800123 return;
124
125 /* Use default pre-ram bar */
126 if (!tempbar)
127 tempbar = HECI1_BASE_ADDRESS;
128
Subrata Banik801dbf42022-06-01 07:56:40 +0000129 /* Assign HECI resource and enable the resource */
130 heci_assign_resource(dev, tempbar);
Sridhar Siricillacb2fd202021-06-09 19:27:06 +0530131
132 /* Trigger HECI Reset and make Host ready for communication with CSE */
133 heci_reset();
Subrata Banik05e06cd2017-11-09 15:04:09 +0530134}
135
Subrata Banikc6e25522021-09-30 18:14:09 +0530136static uint32_t read_bar(pci_devfn_t dev, uint32_t offset)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800137{
Subrata Banikc6e25522021-09-30 18:14:09 +0530138 return read32p(get_cse_bar(dev) + offset);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800139}
140
Subrata Banikc6e25522021-09-30 18:14:09 +0530141static void write_bar(pci_devfn_t dev, uint32_t offset, uint32_t val)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800142{
Subrata Banikc6e25522021-09-30 18:14:09 +0530143 return write32p(get_cse_bar(dev) + offset, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800144}
145
146static uint32_t read_cse_csr(void)
147{
Subrata Banikc6e25522021-09-30 18:14:09 +0530148 return read_bar(PCH_DEV_CSE, MMIO_CSE_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800149}
150
151static uint32_t read_host_csr(void)
152{
Subrata Banikc6e25522021-09-30 18:14:09 +0530153 return read_bar(PCH_DEV_CSE, MMIO_HOST_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800154}
155
156static void write_host_csr(uint32_t data)
157{
Subrata Banikc6e25522021-09-30 18:14:09 +0530158 write_bar(PCH_DEV_CSE, MMIO_HOST_CSR, data);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800159}
160
161static size_t filled_slots(uint32_t data)
162{
163 uint8_t wp, rp;
164 rp = data >> CSR_RP_START;
165 wp = data >> CSR_WP_START;
Elyes Haouas9018dee2022-11-18 15:07:33 +0100166 return (uint8_t)(wp - rp);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800167}
168
169static size_t cse_filled_slots(void)
170{
171 return filled_slots(read_cse_csr());
172}
173
174static size_t host_empty_slots(void)
175{
176 uint32_t csr;
177 csr = read_host_csr();
178
179 return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr);
180}
181
182static void clear_int(void)
183{
184 uint32_t csr;
185 csr = read_host_csr();
186 csr |= CSR_IS;
187 write_host_csr(csr);
188}
189
190static uint32_t read_slot(void)
191{
Subrata Banikc6e25522021-09-30 18:14:09 +0530192 return read_bar(PCH_DEV_CSE, MMIO_CSE_CB_RW);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800193}
194
195static void write_slot(uint32_t val)
196{
Subrata Banikc6e25522021-09-30 18:14:09 +0530197 write_bar(PCH_DEV_CSE, MMIO_CSE_CB_WW, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800198}
199
200static int wait_write_slots(size_t cnt)
201{
202 struct stopwatch sw;
203
Subrata Banik03aef282021-09-28 18:10:24 +0530204 stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800205 while (host_empty_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530206 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800207 if (stopwatch_expired(&sw)) {
208 printk(BIOS_ERR, "HECI: timeout, buffer not drained\n");
209 return 0;
210 }
211 }
212 return 1;
213}
214
215static int wait_read_slots(size_t cnt)
216{
217 struct stopwatch sw;
218
Subrata Banik03aef282021-09-28 18:10:24 +0530219 stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800220 while (cse_filled_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530221 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800222 if (stopwatch_expired(&sw)) {
223 printk(BIOS_ERR, "HECI: timed out reading answer!\n");
224 return 0;
225 }
226 }
227 return 1;
228}
229
230/* get number of full 4-byte slots */
231static size_t bytes_to_slots(size_t bytes)
232{
233 return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE;
234}
235
236static int cse_ready(void)
237{
238 uint32_t csr;
239 csr = read_cse_csr();
240 return csr & CSR_READY;
241}
242
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530243static bool cse_check_hfs1_com(int mode)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530244{
245 union me_hfsts1 hfs1;
246 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530247 return hfs1.fields.operation_mode == mode;
248}
249
Michał Żygowskidaa17102022-10-04 10:55:38 +0200250static bool cse_is_hfs1_fw_init_complete(void)
251{
252 union me_hfsts1 hfs1;
253 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
254 if (hfs1.fields.fw_init_complete)
255 return true;
256 return false;
257}
258
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530259bool cse_is_hfs1_cws_normal(void)
260{
261 union me_hfsts1 hfs1;
262 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
263 if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL)
264 return true;
265 return false;
266}
267
268bool cse_is_hfs1_com_normal(void)
269{
270 return cse_check_hfs1_com(ME_HFS1_COM_NORMAL);
271}
272
273bool cse_is_hfs1_com_secover_mei_msg(void)
274{
275 return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG);
276}
277
278bool cse_is_hfs1_com_soft_temp_disable(void)
279{
280 return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE);
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530281}
282
Subrata Banike74ebcd2021-12-27 10:49:19 +0000283/*
Sridhar Siricilla90a43932022-09-12 10:37:17 +0530284 * Starting from TGL platform, HFSTS1.spi_protection_mode replaces mfg_mode to indicate
285 * SPI protection status as well as end-of-manufacturing(EOM) status where EOM flow is
286 * triggered in single staged operation (either through first boot with required MFIT
287 * configuratin or FPT /CLOSEMANUF).
288 * In staged manufacturing flow, spi_protection_mode alone doesn't indicate the EOM status.
Subrata Banike74ebcd2021-12-27 10:49:19 +0000289 *
Sridhar Siricilla90a43932022-09-12 10:37:17 +0530290 * HFSTS1.spi_protection_mode description:
291 * mfg_mode = 0 means SPI protection is on.
Subrata Banike74ebcd2021-12-27 10:49:19 +0000292 * mfg_mode = 1 means SPI is unprotected.
293 */
294bool cse_is_hfs1_spi_protected(void)
295{
296 union me_hfsts1 hfs1;
297 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
298 return !hfs1.fields.mfg_mode;
299}
300
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530301bool cse_is_hfs3_fw_sku_lite(void)
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530302{
303 union me_hfsts3 hfs3;
304 hfs3.data = me_read_config32(PCI_ME_HFSTS3);
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530305 return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_LITE;
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530306}
307
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530308/* Makes the host ready to communicate with CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530309void cse_set_host_ready(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530310{
311 uint32_t csr;
312 csr = read_host_csr();
313 csr &= ~CSR_RESET;
314 csr |= (CSR_IG | CSR_READY);
315 write_host_csr(csr);
316}
317
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530318/* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */
319uint8_t cse_wait_sec_override_mode(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530320{
321 struct stopwatch sw;
Subrata Banik03aef282021-09-28 18:10:24 +0530322 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530323 while (!cse_is_hfs1_com_secover_mei_msg()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530324 udelay(HECI_DELAY_US);
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530325 if (stopwatch_expired(&sw)) {
326 printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n");
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530327 return 0;
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530328 }
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530329 }
Rob Barnesd522f382022-09-12 06:31:47 -0600330 printk(BIOS_DEBUG, "HECI: CSE took %lld ms to enter security override mode\n",
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530331 stopwatch_duration_msecs(&sw));
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530332 return 1;
333}
334
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530335/*
336 * Polls for CSE's current operation mode 'Soft Temporary Disable'.
337 * The CSE enters the current operation mode when it boots from RO(BP1).
338 */
339uint8_t cse_wait_com_soft_temp_disable(void)
340{
341 struct stopwatch sw;
Subrata Banikf5765812021-09-30 13:37:10 +0530342 stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO_MS);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530343 while (!cse_is_hfs1_com_soft_temp_disable()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530344 udelay(HECI_DELAY_US);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530345 if (stopwatch_expired(&sw)) {
346 printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n");
347 return 0;
348 }
349 }
Rob Barnesd522f382022-09-12 06:31:47 -0600350 printk(BIOS_SPEW, "HECI: CSE took %lld ms to boot from RO\n",
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530351 stopwatch_duration_msecs(&sw));
352 return 1;
353}
354
Andrey Petrov04a72c42017-03-01 15:51:57 -0800355static int wait_heci_ready(void)
356{
357 struct stopwatch sw;
358
Subrata Banik03aef282021-09-28 18:10:24 +0530359 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800360 while (!cse_ready()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530361 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800362 if (stopwatch_expired(&sw))
363 return 0;
364 }
365
366 return 1;
367}
368
369static void host_gen_interrupt(void)
370{
371 uint32_t csr;
372 csr = read_host_csr();
373 csr |= CSR_IG;
374 write_host_csr(csr);
375}
376
377static size_t hdr_get_length(uint32_t hdr)
378{
379 return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START;
380}
381
382static int
383send_one_message(uint32_t hdr, const void *buff)
384{
385 size_t pend_len, pend_slots, remainder, i;
386 uint32_t tmp;
387 const uint32_t *p = buff;
388
389 /* Get space for the header */
390 if (!wait_write_slots(1))
391 return 0;
392
393 /* First, write header */
394 write_slot(hdr);
395
396 pend_len = hdr_get_length(hdr);
397 pend_slots = bytes_to_slots(pend_len);
398
399 if (!wait_write_slots(pend_slots))
400 return 0;
401
402 /* Write the body in whole slots */
403 i = 0;
404 while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) {
405 write_slot(*p++);
406 i += SLOT_SIZE;
407 }
408
409 remainder = pend_len % SLOT_SIZE;
410 /* Pad to 4 bytes not touching caller's buffer */
411 if (remainder) {
412 memcpy(&tmp, p, remainder);
413 write_slot(tmp);
414 }
415
416 host_gen_interrupt();
417
418 /* Make sure nothing bad happened during transmission */
419 if (!cse_ready())
420 return 0;
421
422 return pend_len;
423}
424
Jeremy Compostella0e1be042023-03-13 13:41:43 -0700425enum cse_tx_rx_status
Andrey Petrov04a72c42017-03-01 15:51:57 -0800426heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr)
427{
Subrata Banik5c08c732017-11-13 14:54:37 +0530428 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800429 uint32_t csr, hdr;
Subrata Banik5c08c732017-11-13 14:54:37 +0530430 size_t sent, remaining, cb_size, max_length;
431 const uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800432
433 if (!msg || !len)
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530434 return CSE_TX_ERR_INPUT;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800435
436 clear_int();
437
Subrata Banik5c08c732017-11-13 14:54:37 +0530438 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
439 p = msg;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800440
Subrata Banik5c08c732017-11-13 14:54:37 +0530441 if (!wait_heci_ready()) {
442 printk(BIOS_ERR, "HECI: not ready\n");
443 continue;
444 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800445
Subrata Banik4a722f52017-11-13 14:56:42 +0530446 csr = read_host_csr();
Subrata Banik5c08c732017-11-13 14:54:37 +0530447 cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE;
448 /*
449 * Reserve one slot for the header. Limit max message
450 * length by 9 bits that are available in the header.
451 */
452 max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1)
453 - SLOT_SIZE;
454 remaining = len;
455
456 /*
457 * Fragment the message into smaller messages not exceeding
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100458 * useful circular buffer length. Mark last message complete.
Subrata Banik5c08c732017-11-13 14:54:37 +0530459 */
460 do {
461 hdr = MIN(max_length, remaining)
462 << MEI_HDR_LENGTH_START;
463 hdr |= client_addr << MEI_HDR_CSE_ADDR_START;
464 hdr |= host_addr << MEI_HDR_HOST_ADDR_START;
465 hdr |= (MIN(max_length, remaining) == remaining) ?
Lee Leahy68ab0b52017-03-10 13:42:34 -0800466 MEI_HDR_IS_COMPLETE : 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530467 sent = send_one_message(hdr, p);
468 p += sent;
469 remaining -= sent;
470 } while (remaining > 0 && sent != 0);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800471
Subrata Banik5c08c732017-11-13 14:54:37 +0530472 if (!remaining)
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530473 return CSE_TX_RX_SUCCESS;
Subrata Banik5c08c732017-11-13 14:54:37 +0530474 }
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530475
Sridhar Siricilla1506b772022-03-05 10:02:25 +0530476 printk(BIOS_DEBUG, "HECI: Trigger HECI reset\n");
477 heci_reset();
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530478 return CSE_TX_ERR_CSE_NOT_READY;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800479}
480
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530481static enum cse_tx_rx_status
482recv_one_message(uint32_t *hdr, void *buff, size_t maxlen, size_t *recv_len)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800483{
484 uint32_t reg, *p = buff;
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530485 size_t recv_slots, remainder, i;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800486
487 /* first get the header */
488 if (!wait_read_slots(1))
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530489 return CSE_RX_ERR_TIMEOUT;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800490
491 *hdr = read_slot();
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530492 *recv_len = hdr_get_length(*hdr);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800493
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530494 if (!*recv_len)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800495 printk(BIOS_WARNING, "HECI: message is zero-sized\n");
496
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530497 recv_slots = bytes_to_slots(*recv_len);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800498
499 i = 0;
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530500 if (*recv_len > maxlen) {
Andrey Petrov04a72c42017-03-01 15:51:57 -0800501 printk(BIOS_ERR, "HECI: response is too big\n");
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530502 return CSE_RX_ERR_RESP_LEN_MISMATCH;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800503 }
504
505 /* wait for the rest of messages to arrive */
506 wait_read_slots(recv_slots);
507
508 /* fetch whole slots first */
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530509 while (i < ALIGN_DOWN(*recv_len, SLOT_SIZE)) {
Andrey Petrov04a72c42017-03-01 15:51:57 -0800510 *p++ = read_slot();
511 i += SLOT_SIZE;
512 }
513
Subrata Banik5c08c732017-11-13 14:54:37 +0530514 /*
515 * If ME is not ready, something went wrong and
516 * we received junk
517 */
518 if (!cse_ready())
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530519 return CSE_RX_ERR_CSE_NOT_READY;
Subrata Banik5c08c732017-11-13 14:54:37 +0530520
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530521 remainder = *recv_len % SLOT_SIZE;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800522
523 if (remainder) {
524 reg = read_slot();
525 memcpy(p, &reg, remainder);
526 }
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530527 return CSE_TX_RX_SUCCESS;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800528}
529
Jeremy Compostella0e1be042023-03-13 13:41:43 -0700530enum cse_tx_rx_status heci_receive(void *buff, size_t *maxlen)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800531{
Subrata Banik5c08c732017-11-13 14:54:37 +0530532 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800533 size_t left, received;
534 uint32_t hdr = 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530535 uint8_t *p;
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530536 enum cse_tx_rx_status ret = CSE_RX_ERR_TIMEOUT;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800537
538 if (!buff || !maxlen || !*maxlen)
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530539 return CSE_RX_ERR_INPUT;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800540
Andrey Petrov04a72c42017-03-01 15:51:57 -0800541 clear_int();
542
Subrata Banik5c08c732017-11-13 14:54:37 +0530543 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
544 p = buff;
545 left = *maxlen;
546
547 if (!wait_heci_ready()) {
548 printk(BIOS_ERR, "HECI: not ready\n");
549 continue;
550 }
551
552 /*
553 * Receive multiple packets until we meet one marked
554 * complete or we run out of space in caller-provided buffer.
555 */
556 do {
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530557 ret = recv_one_message(&hdr, p, left, &received);
558 if (ret) {
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200559 printk(BIOS_ERR, "HECI: Failed to receive!\n");
Sridhar Siricilla1506b772022-03-05 10:02:25 +0530560 goto CSE_RX_ERR_HANDLE;
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800561 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530562 left -= received;
563 p += received;
564 /* If we read out everything ping to send more */
565 if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots())
566 host_gen_interrupt();
567 } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0);
568
569 if ((hdr & MEI_HDR_IS_COMPLETE) && received) {
Elyes Haouas9018dee2022-11-18 15:07:33 +0100570 *maxlen = p - (uint8_t *)buff;
Johnny Lina3e68c92022-08-09 15:36:30 +0800571 if (CONFIG(SOC_INTEL_CSE_SERVER_SKU))
572 clear_int();
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530573 return CSE_TX_RX_SUCCESS;
Subrata Banik5c08c732017-11-13 14:54:37 +0530574 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800575 }
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530576
Sridhar Siricilla1506b772022-03-05 10:02:25 +0530577CSE_RX_ERR_HANDLE:
578 printk(BIOS_DEBUG, "HECI: Trigger HECI Reset\n");
579 heci_reset();
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530580 return CSE_RX_ERR_CSE_NOT_READY;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800581}
582
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530583enum cse_tx_rx_status heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg,
584 size_t *rcv_sz, uint8_t cse_addr)
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530585{
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530586 enum cse_tx_rx_status ret;
587
588 ret = heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, cse_addr);
589 if (ret) {
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530590 printk(BIOS_ERR, "HECI: send Failed\n");
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530591 return ret;
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530592 }
593
594 if (rcv_msg != NULL) {
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530595 ret = heci_receive(rcv_msg, rcv_sz);
596 if (ret) {
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530597 printk(BIOS_ERR, "HECI: receive Failed\n");
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530598 return ret;
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530599 }
600 }
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530601 return ret;
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530602}
603
Andrey Petrov04a72c42017-03-01 15:51:57 -0800604/*
605 * Attempt to reset the device. This is useful when host and ME are out
606 * of sync during transmission or ME didn't understand the message.
607 */
608int heci_reset(void)
609{
610 uint32_t csr;
611
Duncan Laurie15ca9032020-11-05 10:09:07 -0800612 /* Clear post code to prevent eventlog entry from unknown code. */
Martin Roth8c974502022-11-20 17:56:44 -0700613 post_code(POST_CODE_ZERO);
Duncan Laurie15ca9032020-11-05 10:09:07 -0800614
Andrey Petrov04a72c42017-03-01 15:51:57 -0800615 /* Send reset request */
616 csr = read_host_csr();
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530617 csr |= (CSR_RESET | CSR_IG);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800618 write_host_csr(csr);
619
620 if (wait_heci_ready()) {
621 /* Device is back on its imaginary feet, clear reset */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530622 cse_set_host_ready();
Andrey Petrov04a72c42017-03-01 15:51:57 -0800623 return 1;
624 }
625
626 printk(BIOS_CRIT, "HECI: reset failed\n");
627
628 return 0;
629}
630
Subrata Banik3710e992021-09-30 16:59:09 +0530631bool is_cse_devfn_visible(unsigned int devfn)
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530632{
Subrata Banik3710e992021-09-30 16:59:09 +0530633 int slot = PCI_SLOT(devfn);
634 int func = PCI_FUNC(devfn);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530635
Subrata Banik3710e992021-09-30 16:59:09 +0530636 if (!is_devfn_enabled(devfn)) {
637 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is disabled\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530638 return false;
639 }
640
Subrata Banik3710e992021-09-30 16:59:09 +0530641 if (pci_read_config16(PCI_DEV(0, slot, func), PCI_VENDOR_ID) == 0xFFFF) {
642 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is hidden\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530643 return false;
644 }
645
646 return true;
647}
648
Subrata Banik3710e992021-09-30 16:59:09 +0530649bool is_cse_enabled(void)
650{
651 return is_cse_devfn_visible(PCH_DEVFN_CSE);
652}
653
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530654uint32_t me_read_config32(int offset)
655{
656 return pci_read_config32(PCH_DEV_CSE, offset);
657}
658
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530659static bool cse_is_global_reset_allowed(void)
660{
661 /*
662 * Allow sending GLOBAL_RESET command only if:
663 * - CSE's current working state is Normal and current operation mode is Normal.
664 * - (or) CSE's current working state is normal and current operation mode can
665 * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530666 * Lite.
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530667 */
668 if (!cse_is_hfs1_cws_normal())
669 return false;
670
671 if (cse_is_hfs1_com_normal())
672 return true;
673
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530674 if (cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530675 if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg())
676 return true;
677 }
678 return false;
679}
680
Sridhar Siricillad415c202019-08-31 14:54:57 +0530681/*
Subrata Banikf463dc02020-09-14 19:04:03 +0530682 * Sends GLOBAL_RESET_REQ cmd to CSE with reset type GLOBAL_RESET.
683 * Returns 0 on failure and 1 on success.
Sridhar Siricillad415c202019-08-31 14:54:57 +0530684 */
Subrata Banikf463dc02020-09-14 19:04:03 +0530685static int cse_request_reset(enum rst_req_type rst_type)
Sridhar Siricillad415c202019-08-31 14:54:57 +0530686{
687 int status;
688 struct mkhi_hdr reply;
689 struct reset_message {
690 struct mkhi_hdr hdr;
691 uint8_t req_origin;
692 uint8_t reset_type;
693 } __packed;
694 struct reset_message msg = {
695 .hdr = {
696 .group_id = MKHI_GROUP_ID_CBM,
Sridhar Siricillae202e672020-01-07 23:36:40 +0530697 .command = MKHI_CBM_GLOBAL_RESET_REQ,
Sridhar Siricillad415c202019-08-31 14:54:57 +0530698 },
699 .req_origin = GR_ORIGIN_BIOS_POST,
700 .reset_type = rst_type
701 };
702 size_t reply_size;
703
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530704 printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type);
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530705
Sridhar Siricillac2a2d2b2020-02-27 17:16:13 +0530706 if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) {
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530707 printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n");
708 return 0;
709 }
Sridhar Siricillad415c202019-08-31 14:54:57 +0530710
Subrata Banikf463dc02020-09-14 19:04:03 +0530711 if (!cse_is_global_reset_allowed() || !is_cse_enabled()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530712 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
713 return 0;
714 }
715
Sridhar Siricillad415c202019-08-31 14:54:57 +0530716 heci_reset();
717
718 reply_size = sizeof(reply);
719 memset(&reply, 0, reply_size);
720
Sridhar Siricillad415c202019-08-31 14:54:57 +0530721 if (rst_type == CSE_RESET_ONLY)
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530722 status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530723 else
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530724 status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size,
725 HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530726
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530727 printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", !status ? "success" : "failure");
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530728 return status;
Sridhar Siricillad415c202019-08-31 14:54:57 +0530729}
730
Subrata Banikf463dc02020-09-14 19:04:03 +0530731int cse_request_global_reset(void)
732{
733 return cse_request_reset(GLOBAL_RESET);
734}
735
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530736static bool cse_is_hmrfpo_enable_allowed(void)
737{
738 /*
739 * Allow sending HMRFPO ENABLE command only if:
740 * - CSE's current working state is Normal and current operation mode is Normal
741 * - (or) cse's current working state is normal and current operation mode is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530742 * Soft Temp Disable if CSE's Firmware SKU is Lite
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530743 */
744 if (!cse_is_hfs1_cws_normal())
745 return false;
746
747 if (cse_is_hfs1_com_normal())
748 return true;
749
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530750 if (cse_is_hfs3_fw_sku_lite() && cse_is_hfs1_com_soft_temp_disable())
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530751 return true;
752
753 return false;
754}
755
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530756/* Sends HMRFPO Enable command to CSE */
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530757enum cb_err cse_hmrfpo_enable(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530758{
759 struct hmrfpo_enable_msg {
760 struct mkhi_hdr hdr;
761 uint32_t nonce[2];
762 } __packed;
763
764 /* HMRFPO Enable message */
765 struct hmrfpo_enable_msg msg = {
766 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530767 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530768 .command = MKHI_HMRFPO_ENABLE,
769 },
770 .nonce = {0},
771 };
772
773 /* HMRFPO Enable response */
774 struct hmrfpo_enable_resp {
775 struct mkhi_hdr hdr;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530776 /* Base addr for factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530777 uint32_t fct_base;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530778 /* Length of factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530779 uint32_t fct_limit;
780 uint8_t status;
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530781 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530782 } __packed;
783
784 struct hmrfpo_enable_resp resp;
785 size_t resp_size = sizeof(struct hmrfpo_enable_resp);
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530786
Sridhar Siricilla49c25f22021-11-27 19:56:47 +0530787 if (cse_is_hfs1_com_secover_mei_msg()) {
788 printk(BIOS_DEBUG, "HECI: CSE is already in security override mode, "
789 "skip sending HMRFPO_ENABLE command to CSE\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530790 return CB_SUCCESS;
Sridhar Siricilla49c25f22021-11-27 19:56:47 +0530791 }
792
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530793 printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n");
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530794
795 if (!cse_is_hmrfpo_enable_allowed()) {
796 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530797 return CB_ERR;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530798 }
799
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530800 if (heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530801 &resp, &resp_size, HECI_MKHI_ADDR))
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530802 return CB_ERR;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530803
804 if (resp.hdr.result) {
805 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530806 return CB_ERR;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530807 }
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530808
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530809 if (resp.status) {
810 printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status);
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530811 return CB_ERR;
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530812 }
813
Sridhar Siricillaad6d3122023-01-10 14:59:35 +0530814 return CB_SUCCESS;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530815}
816
817/*
818 * Sends HMRFPO Get Status command to CSE to get the HMRFPO status.
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530819 * The status can be DISABLED/LOCKED/ENABLED
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530820 */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530821int cse_hmrfpo_get_status(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530822{
823 struct hmrfpo_get_status_msg {
824 struct mkhi_hdr hdr;
825 } __packed;
826
827 struct hmrfpo_get_status_resp {
828 struct mkhi_hdr hdr;
829 uint8_t status;
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530830 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530831 } __packed;
832
833 struct hmrfpo_get_status_msg msg = {
834 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530835 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530836 .command = MKHI_HMRFPO_GET_STATUS,
837 },
838 };
839 struct hmrfpo_get_status_resp resp;
840 size_t resp_size = sizeof(struct hmrfpo_get_status_resp);
841
842 printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n");
843
Sridhar Siricilla206905c2020-02-06 18:48:22 +0530844 if (!cse_is_hfs1_cws_normal()) {
845 printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n");
846 return -1;
847 }
848
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530849 if (heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530850 &resp, &resp_size, HECI_MKHI_ADDR)) {
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530851 printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n");
852 return -1;
853 }
854
855 if (resp.hdr.result) {
856 printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n",
857 resp.hdr.result);
858 return -1;
859 }
860
861 return resp.status;
862}
863
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530864void print_me_fw_version(void *unused)
865{
Johnny Lin72e76672021-10-09 12:35:35 +0800866 struct me_fw_ver_resp resp = {0};
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530867
868 /* Ignore if UART debugging is disabled */
869 if (!CONFIG(CONSOLE_SERIAL))
870 return;
871
Johnny Lin72e76672021-10-09 12:35:35 +0800872 if (get_me_fw_version(&resp) == CB_SUCCESS) {
873 printk(BIOS_DEBUG, "ME: Version: %d.%d.%d.%d\n", resp.code.major,
874 resp.code.minor, resp.code.hotfix, resp.code.build);
875 return;
876 }
877 printk(BIOS_DEBUG, "ME: Version: Unavailable\n");
878}
879
880enum cb_err get_me_fw_version(struct me_fw_ver_resp *resp)
881{
882 const struct mkhi_hdr fw_ver_msg = {
883 .group_id = MKHI_GROUP_ID_GEN,
884 .command = MKHI_GEN_GET_FW_VERSION,
885 };
886
887 if (resp == NULL) {
888 printk(BIOS_ERR, "%s failed, null pointer parameter\n", __func__);
889 return CB_ERR;
890 }
891 size_t resp_size = sizeof(*resp);
892
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200893 /* Ignore if CSE is disabled */
894 if (!is_cse_enabled())
Johnny Lin72e76672021-10-09 12:35:35 +0800895 return CB_ERR;
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200896
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530897 /*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530898 * Ignore if ME Firmware SKU type is Lite since
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530899 * print_boot_partition_info() logs RO(BP1) and RW(BP2) versions.
900 */
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530901 if (cse_is_hfs3_fw_sku_lite())
Johnny Lin72e76672021-10-09 12:35:35 +0800902 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530903
904 /*
905 * Prerequisites:
906 * 1) HFSTS1 Current Working State is Normal
907 * 2) HFSTS1 Current Operation Mode is Normal
908 * 3) It's after DRAM INIT DONE message (taken care of by calling it
909 * during ramstage
910 */
911 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal())
Johnny Lin72e76672021-10-09 12:35:35 +0800912 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530913
914 heci_reset();
915
Sridhar Siricilla6836da22022-02-23 23:36:45 +0530916 if (heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), resp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530917 HECI_MKHI_ADDR))
Johnny Lin72e76672021-10-09 12:35:35 +0800918 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530919
Johnny Lin72e76672021-10-09 12:35:35 +0800920 if (resp->hdr.result)
921 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530922
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530923
Johnny Lin72e76672021-10-09 12:35:35 +0800924 return CB_SUCCESS;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530925}
926
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600927void cse_trigger_vboot_recovery(enum csme_failure_reason reason)
928{
929 printk(BIOS_DEBUG, "cse: CSE status registers: HFSTS1: 0x%x, HFSTS2: 0x%x "
930 "HFSTS3: 0x%x\n", me_read_config32(PCI_ME_HFSTS1),
931 me_read_config32(PCI_ME_HFSTS2), me_read_config32(PCI_ME_HFSTS3));
932
Jakub Czapiga605f7932022-11-04 12:18:04 +0000933 if (CONFIG(VBOOT))
934 vboot_fail_and_reboot(vboot_get_context(), VB2_RECOVERY_INTEL_CSE_LITE_SKU,
935 reason);
936
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600937 die("cse: Failed to trigger recovery mode(recovery subcode:%d)\n", reason);
938}
939
Subrata Banikc6e25522021-09-30 18:14:09 +0530940static bool disable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530941{
942 struct stopwatch sw;
Subrata Banikc6e25522021-09-30 18:14:09 +0530943 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530944 dev_idle_ctrl &= ~CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530945 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530946
Subrata Banik03aef282021-09-28 18:10:24 +0530947 stopwatch_init_usecs_expire(&sw, HECI_CIP_TIMEOUT_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530948 do {
Subrata Banikc6e25522021-09-30 18:14:09 +0530949 dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530950 if ((dev_idle_ctrl & CSE_DEV_CIP) == CSE_DEV_CIP)
951 return true;
Subrata Banik03aef282021-09-28 18:10:24 +0530952 udelay(HECI_DELAY_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530953 } while (!stopwatch_expired(&sw));
954
955 return false;
956}
957
Subrata Banikc6e25522021-09-30 18:14:09 +0530958static void enable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530959{
Subrata Banikc6e25522021-09-30 18:14:09 +0530960 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530961 dev_idle_ctrl |= CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530962 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530963}
964
Subrata Banikc6e25522021-09-30 18:14:09 +0530965enum cse_device_state get_cse_device_state(unsigned int devfn)
Subrata Banika219edb2021-09-25 15:02:37 +0530966{
Subrata Banikc6e25522021-09-30 18:14:09 +0530967 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
968 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530969 if ((dev_idle_ctrl & CSE_DEV_IDLE) == CSE_DEV_IDLE)
970 return DEV_IDLE;
971
972 return DEV_ACTIVE;
973}
974
Subrata Banikc6e25522021-09-30 18:14:09 +0530975static enum cse_device_state ensure_cse_active(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530976{
Subrata Banikc6e25522021-09-30 18:14:09 +0530977 if (!disable_cse_idle(dev))
Subrata Banika219edb2021-09-25 15:02:37 +0530978 return DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530979 pci_or_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Subrata Banika219edb2021-09-25 15:02:37 +0530980
981 return DEV_ACTIVE;
982}
983
Subrata Banikc6e25522021-09-30 18:14:09 +0530984static void ensure_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530985{
Subrata Banikc6e25522021-09-30 18:14:09 +0530986 enable_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +0530987
Subrata Banikc6e25522021-09-30 18:14:09 +0530988 pci_and_config32(dev, PCI_COMMAND, ~(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
Subrata Banika219edb2021-09-25 15:02:37 +0530989}
990
Subrata Banikc6e25522021-09-30 18:14:09 +0530991bool set_cse_device_state(unsigned int devfn, enum cse_device_state requested_state)
Subrata Banika219edb2021-09-25 15:02:37 +0530992{
Subrata Banikc6e25522021-09-30 18:14:09 +0530993 enum cse_device_state current_state = get_cse_device_state(devfn);
994 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
Subrata Banika219edb2021-09-25 15:02:37 +0530995
996 if (current_state == requested_state)
997 return true;
998
999 if (requested_state == DEV_ACTIVE)
Subrata Banikc6e25522021-09-30 18:14:09 +05301000 return ensure_cse_active(dev) == requested_state;
Subrata Banika219edb2021-09-25 15:02:37 +05301001 else
Subrata Banikc6e25522021-09-30 18:14:09 +05301002 ensure_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +05301003
1004 return true;
1005}
1006
Subrata Banik526cc3e2022-01-31 21:55:51 +05301007void cse_set_to_d0i3(void)
1008{
1009 if (!is_cse_devfn_visible(PCH_DEVFN_CSE))
1010 return;
1011
1012 set_cse_device_state(PCH_DEVFN_CSE, DEV_IDLE);
1013}
1014
1015/* Function to set D0I3 for all HECI devices */
1016void heci_set_to_d0i3(void)
1017{
1018 for (int i = 0; i < CONFIG_MAX_HECI_DEVICES; i++) {
Subrata Banik57909562022-06-02 00:25:36 +05301019 unsigned int devfn = PCI_DEVFN(PCH_DEV_SLOT_CSE, i);
Subrata Banik01bf0022022-04-06 18:59:37 +05301020 if (!is_cse_devfn_visible(devfn))
Subrata Banik526cc3e2022-01-31 21:55:51 +05301021 continue;
1022
Subrata Banik01bf0022022-04-06 18:59:37 +05301023 set_cse_device_state(devfn, DEV_IDLE);
Subrata Banik526cc3e2022-01-31 21:55:51 +05301024 }
1025}
1026
Subrata Banik801dbf42022-06-01 07:56:40 +00001027/* Initialize the HECI devices. */
1028void heci_init(void)
1029{
1030 for (int i = 0; i < CONFIG_MAX_HECI_DEVICES; i++) {
1031 unsigned int devfn = PCI_DEVFN(PCH_DEV_SLOT_CSE, i);
1032 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
1033
1034 if (!is_cse_devfn_visible(devfn))
1035 continue;
1036
1037 /* Assume it is already initialized, nothing else to do */
1038 if (get_cse_bar(dev))
1039 return;
1040
1041 heci_assign_resource(dev, HECI1_BASE_ADDRESS + (i * HECI_BASE_SIZE));
1042
1043 ensure_cse_active(dev);
1044 }
1045 /* Trigger HECI Reset and make Host ready for communication with CSE */
1046 heci_reset();
1047}
1048
Subrata Banik80c92892022-02-01 00:26:55 +05301049void cse_control_global_reset_lock(void)
1050{
1051 /*
1052 * As per ME BWG recommendation the BIOS should not lock down CF9GR bit during
1053 * manufacturing and re-manufacturing environment if HFSTS1 [4] is set. Note:
1054 * this recommendation is not applicable for CSE-Lite SKUs where BIOS should set
1055 * CF9LOCK bit irrespectively.
1056 *
1057 * Other than that, make sure payload/OS can't trigger global reset.
1058 *
1059 * BIOS must also ensure that CF9GR is cleared and locked (Bit31 of ETR3)
1060 * prior to transferring control to the OS.
1061 */
1062 if (CONFIG(SOC_INTEL_CSE_LITE_SKU) || cse_is_hfs1_spi_protected())
1063 pmc_global_reset_disable_and_lock();
1064 else
1065 pmc_global_reset_enable(false);
1066}
1067
Michał Żygowskidaa17102022-10-04 10:55:38 +02001068enum cb_err cse_get_fw_feature_state(uint32_t *feature_state)
1069{
1070 struct fw_feature_state_msg {
1071 struct mkhi_hdr hdr;
1072 uint32_t rule_id;
1073 } __packed;
1074
1075 /* Get Firmware Feature State message */
1076 struct fw_feature_state_msg msg = {
1077 .hdr = {
1078 .group_id = MKHI_GROUP_ID_FWCAPS,
1079 .command = MKHI_FWCAPS_GET_FW_FEATURE_STATE,
1080 },
1081 .rule_id = ME_FEATURE_STATE_RULE_ID
1082 };
1083
1084 /* Get Firmware Feature State response */
1085 struct fw_feature_state_resp {
1086 struct mkhi_hdr hdr;
1087 uint32_t rule_id;
1088 uint8_t rule_len;
1089 uint32_t fw_runtime_status;
1090 } __packed;
1091
1092 struct fw_feature_state_resp resp;
1093 size_t resp_size = sizeof(struct fw_feature_state_resp);
1094
1095 /* Ignore if CSE is disabled or input buffer is invalid */
1096 if (!is_cse_enabled() || !feature_state)
1097 return CB_ERR;
1098
1099 /*
1100 * Prerequisites:
1101 * 1) HFSTS1 Current Working State is Normal
1102 * 2) HFSTS1 Current Operation Mode is Normal
1103 * 3) It's after DRAM INIT DONE message (taken care of by calling it
1104 * during ramstage)
1105 */
1106 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal() || !ENV_RAMSTAGE)
1107 return CB_ERR;
1108
1109 printk(BIOS_DEBUG, "HECI: Send GET FW FEATURE STATE Command\n");
1110
1111 if (heci_send_receive(&msg, sizeof(struct fw_feature_state_msg),
1112 &resp, &resp_size, HECI_MKHI_ADDR))
1113 return CB_ERR;
1114
1115 if (resp.hdr.result) {
1116 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
1117 return CB_ERR;
1118 }
1119
1120 if (resp.rule_len != sizeof(resp.fw_runtime_status)) {
1121 printk(BIOS_ERR, "HECI: GET FW FEATURE STATE has invalid rule data length\n");
1122 return CB_ERR;
1123 }
1124
1125 *feature_state = resp.fw_runtime_status;
1126
1127 return CB_SUCCESS;
1128}
1129
1130void cse_enable_ptt(bool state)
1131{
1132 struct fw_feature_shipment_override_msg {
1133 struct mkhi_hdr hdr;
1134 uint32_t enable_mask;
1135 uint32_t disable_mask;
1136 } __packed;
1137
1138 /* FW Feature Shipment Time State Override message */
1139 struct fw_feature_shipment_override_msg msg = {
1140 .hdr = {
1141 .group_id = MKHI_GROUP_ID_GEN,
1142 .command = MKHI_GEN_FW_FEATURE_SHIPMENT_OVER,
1143 },
1144 .enable_mask = 0,
1145 .disable_mask = 0
1146 };
1147
1148 /* FW Feature Shipment Time State Override response */
1149 struct fw_feature_shipment_override_resp {
1150 struct mkhi_hdr hdr;
1151 uint32_t data;
1152 } __packed;
1153
1154 struct fw_feature_shipment_override_resp resp;
1155 size_t resp_size = sizeof(struct fw_feature_shipment_override_resp);
1156 uint32_t feature_status;
1157
1158 /* Ignore if CSE is disabled */
1159 if (!is_cse_enabled())
1160 return;
1161
1162 printk(BIOS_DEBUG, "Requested to change PTT state to %sabled\n", state ? "en" : "dis");
1163
1164 /*
1165 * Prerequisites:
1166 * 1) HFSTS1 Current Working State is Normal
1167 * 2) HFSTS1 Current Operation Mode is Normal
1168 * 3) It's after DRAM INIT DONE message (taken care of by calling it
1169 * during ramstage
1170 * 4) HFSTS1 FW Init Complete is set
1171 * 5) Before EOP issued to CSE
1172 */
1173 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal() ||
1174 !cse_is_hfs1_fw_init_complete() || !ENV_RAMSTAGE) {
1175 printk(BIOS_ERR, "HECI: Unmet prerequisites for"
1176 "FW FEATURE SHIPMENT TIME STATE OVERRIDE\n");
1177 return;
1178 }
1179
1180 if (cse_get_fw_feature_state(&feature_status) != CB_SUCCESS) {
1181 printk(BIOS_ERR, "HECI: Cannot determine current feature status\n");
1182 return;
1183 }
1184
1185 if (!!(feature_status & ME_FW_FEATURE_PTT) == state) {
1186 printk(BIOS_DEBUG, "HECI: PTT is already in the requested state\n");
1187 return;
1188 }
1189
1190 printk(BIOS_DEBUG, "HECI: Send FW FEATURE SHIPMENT TIME STATE OVERRIDE Command\n");
1191
1192 if (state)
1193 msg.enable_mask |= ME_FW_FEATURE_PTT;
1194 else
1195 msg.disable_mask |= ME_FW_FEATURE_PTT;
1196
1197 if (heci_send_receive(&msg, sizeof(struct fw_feature_shipment_override_msg),
1198 &resp, &resp_size, HECI_MKHI_ADDR))
1199 return;
1200
1201 if (resp.hdr.result) {
1202 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
1203 return;
1204 }
1205
1206 /* Global reset is required after acceptance of the command */
1207 if (resp.data == 0) {
1208 printk(BIOS_DEBUG, "HECI: FW FEATURE SHIPMENT TIME STATE OVERRIDE success\n");
1209 do_global_reset();
1210 } else {
1211 printk(BIOS_ERR, "HECI: FW FEATURE SHIPMENT TIME STATE OVERRIDE error (%x)\n",
1212 resp.data);
1213 }
1214}
1215
Andrey Petrov04a72c42017-03-01 15:51:57 -08001216#if ENV_RAMSTAGE
1217
Sean Rhodes69ed3ed2021-04-30 16:38:17 +01001218/*
1219 * Disable the Intel (CS)Management Engine via HECI based on a cmos value
1220 * of `me_state`. A value of `0` will result in a (CS)ME state of `0` (working)
1221 * and value of `1` will result in a (CS)ME state of `3` (disabled).
1222 *
1223 * It isn't advised to use this in combination with me_cleaner.
1224 *
1225 * It is advisable to have a second cmos option called `me_state_counter`.
1226 * Whilst not essential, it avoid reboots loops if the (CS)ME fails to
1227 * change states after 3 attempts. Some versions of the (CS)ME need to be
1228 * reset 3 times.
1229 *
1230 * Ideal cmos values would be:
1231 *
1232 * # coreboot config options: cpu
1233 * 432 1 e 5 me_state
1234 * 440 4 h 0 me_state_counter
1235 *
1236 * #ID value text
1237 * 5 0 Enable
1238 * 5 1 Disable
1239 */
1240
1241static void me_reset_with_count(void)
1242{
1243 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter", UINT_MAX);
1244
1245 if (cmos_me_state_counter != UINT_MAX) {
1246 printk(BIOS_DEBUG, "CMOS: me_state_counter = %u\n", cmos_me_state_counter);
1247 /* Avoid boot loops by only trying a state change 3 times */
1248 if (cmos_me_state_counter < ME_DISABLE_ATTEMPTS) {
1249 cmos_me_state_counter++;
1250 set_uint_option("me_state_counter", cmos_me_state_counter);
1251 printk(BIOS_DEBUG, "ME: Reset attempt %u/%u.\n", cmos_me_state_counter,
1252 ME_DISABLE_ATTEMPTS);
1253 do_global_reset();
1254 } else {
1255 /*
1256 * If the (CS)ME fails to change states after 3 attempts, it will
1257 * likely need a cold boot, or recovering.
1258 */
Julius Wernere9665952022-01-21 17:06:20 -08001259 printk(BIOS_ERR, "Failed to change ME state in %u attempts!\n",
Sean Rhodes69ed3ed2021-04-30 16:38:17 +01001260 ME_DISABLE_ATTEMPTS);
1261
1262 }
1263 } else {
1264 printk(BIOS_DEBUG, "ME: Resetting");
1265 do_global_reset();
1266 }
1267}
1268
1269static void cse_set_state(struct device *dev)
1270{
1271
1272 /* (CS)ME Disable Command */
1273 struct me_disable_command {
1274 struct mkhi_hdr hdr;
1275 uint32_t rule_id;
1276 uint8_t rule_len;
1277 uint32_t rule_data;
1278 } __packed me_disable = {
1279 .hdr = {
1280 .group_id = MKHI_GROUP_ID_FWCAPS,
1281 .command = MKHI_SET_ME_DISABLE,
1282 },
1283 .rule_id = ME_DISABLE_RULE_ID,
1284 .rule_len = ME_DISABLE_RULE_LENGTH,
1285 .rule_data = ME_DISABLE_COMMAND,
1286 };
1287
1288 struct me_disable_reply {
1289 struct mkhi_hdr hdr;
1290 uint32_t rule_id;
1291 } __packed;
1292
1293 struct me_disable_reply disable_reply;
1294
1295 size_t disable_reply_size;
1296
1297 /* (CS)ME Enable Command */
1298 struct me_enable_command {
1299 struct mkhi_hdr hdr;
1300 } me_enable = {
1301 .hdr = {
1302 .group_id = MKHI_GROUP_ID_BUP_COMMON,
1303 .command = MKHI_SET_ME_ENABLE,
1304 },
1305 };
1306
1307 struct me_enable_reply {
1308 struct mkhi_hdr hdr;
1309 } __packed;
1310
1311 struct me_enable_reply enable_reply;
1312
1313 size_t enable_reply_size;
1314
1315 /* Function Start */
1316
1317 int send;
1318 int result;
1319 /*
1320 * Check if the CMOS value "me_state" exists, if it doesn't, then
1321 * don't do anything.
1322 */
1323 const unsigned int cmos_me_state = get_uint_option("me_state", UINT_MAX);
1324
1325 if (cmos_me_state == UINT_MAX)
1326 return;
1327
1328 printk(BIOS_DEBUG, "CMOS: me_state = %u\n", cmos_me_state);
1329
1330 /*
1331 * We only take action if the me_state doesn't match the CS(ME) working state
1332 */
1333
1334 const unsigned int soft_temp_disable = cse_is_hfs1_com_soft_temp_disable();
1335
1336 if (cmos_me_state && !soft_temp_disable) {
1337 /* me_state should be disabled, but it's enabled */
1338 printk(BIOS_DEBUG, "ME needs to be disabled.\n");
1339 send = heci_send_receive(&me_disable, sizeof(me_disable),
1340 &disable_reply, &disable_reply_size, HECI_MKHI_ADDR);
1341 result = disable_reply.hdr.result;
1342 } else if (!cmos_me_state && soft_temp_disable) {
1343 /* me_state should be enabled, but it's disabled */
1344 printk(BIOS_DEBUG, "ME needs to be enabled.\n");
1345 send = heci_send_receive(&me_enable, sizeof(me_enable),
1346 &enable_reply, &enable_reply_size, HECI_MKHI_ADDR);
1347 result = enable_reply.hdr.result;
1348 } else {
1349 printk(BIOS_DEBUG, "ME is %s.\n", cmos_me_state ? "disabled" : "enabled");
1350 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter",
1351 UINT_MAX);
1352 /* set me_state_counter to 0 */
1353 if ((cmos_me_state_counter != UINT_MAX && cmos_me_state_counter != 0))
1354 set_uint_option("me_state_counter", 0);
1355 return;
1356 }
1357
1358 printk(BIOS_DEBUG, "HECI: ME state change send %s!\n",
Sridhar Siricilla6836da22022-02-23 23:36:45 +05301359 !send ? "success" : "failure");
Sean Rhodes69ed3ed2021-04-30 16:38:17 +01001360 printk(BIOS_DEBUG, "HECI: ME state change result %s!\n",
1361 result ? "success" : "failure");
1362
1363 /*
1364 * Reset if the result was successful, or if the send failed as some older
1365 * version of the Intel (CS)ME won't successfully receive the message unless reset
1366 * twice.
1367 */
1368 if (send || !result)
1369 me_reset_with_count();
1370}
1371
Subrata Banik90e318b2022-02-06 16:26:45 +05301372/*
1373 * `cse_final_ready_to_boot` function is native implementation of equivalent events
1374 * performed by FSP NotifyPhase(Ready To Boot) API invocations.
1375 *
1376 * Operations are:
Subrata Banik5214c402022-11-24 20:43:37 +05301377 * 1. Perform global reset lock.
1378 * 2. Put HECI1 to D0i3 and disable the HECI1 if the user selects
Subrata Banik670572f2022-04-25 15:39:55 +05301379 * DISABLE_HECI1_AT_PRE_BOOT config or CSE HFSTS1 Operation Mode is
1380 * `Software Temporary Disable`.
Subrata Banik90e318b2022-02-06 16:26:45 +05301381 */
1382static void cse_final_ready_to_boot(void)
1383{
Subrata Banik90e318b2022-02-06 16:26:45 +05301384 cse_control_global_reset_lock();
1385
Subrata Banik670572f2022-04-25 15:39:55 +05301386 if (CONFIG(DISABLE_HECI1_AT_PRE_BOOT) || cse_is_hfs1_com_soft_temp_disable()) {
Subrata Banik90e318b2022-02-06 16:26:45 +05301387 cse_set_to_d0i3();
1388 heci1_disable();
1389 }
1390}
1391
1392/*
1393 * `cse_final_end_of_firmware` function is native implementation of equivalent events
1394 * performed by FSP NotifyPhase(End of Firmware) API invocations.
1395 *
1396 * Operations are:
1397 * 1. Set D0I3 for all HECI devices.
1398 */
1399static void cse_final_end_of_firmware(void)
1400{
1401 heci_set_to_d0i3();
1402}
1403
Subrata Banik90e318b2022-02-06 16:26:45 +05301404/*
Subrata Banik17a3da82022-11-24 21:51:42 +05301405 * This function to perform essential post EOP cse related operations
1406 * upon SoC selecting `SOC_INTEL_CSE_SEND_EOP_LATE` config
1407 */
1408void cse_late_finalize(void)
1409{
Jeremy Compostellae7a12042023-03-13 13:59:08 -07001410 if (!CONFIG(SOC_INTEL_CSE_SEND_EOP_LATE) &&
1411 !CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
Subrata Banik17a3da82022-11-24 21:51:42 +05301412 return;
1413
1414 if (!CONFIG(USE_FSP_NOTIFY_PHASE_READY_TO_BOOT))
1415 cse_final_ready_to_boot();
1416
1417 if (!CONFIG(USE_FSP_NOTIFY_PHASE_END_OF_FIRMWARE))
1418 cse_final_end_of_firmware();
1419}
1420
1421/*
Subrata Banik90e318b2022-02-06 16:26:45 +05301422 * `cse_final` function is native implementation of equivalent events performed by
1423 * each FSP NotifyPhase() API invocations.
1424 */
1425static void cse_final(struct device *dev)
1426{
Subrata Banik5214c402022-11-24 20:43:37 +05301427 /* SoC user decided to send EOP late */
1428 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_LATE))
1429 return;
1430
1431 /* 1. Send EOP to CSE if not done.*/
1432 if (CONFIG(SOC_INTEL_CSE_SET_EOP))
1433 cse_send_end_of_post();
1434
Jeremy Compostellae7a12042023-03-13 13:59:08 -07001435 /*
1436 * In asynchronous mode, the EOP command has most likely not been
1437 * completed yet. Finalization steps will be run once the EOP command
1438 * has successfully been completed.
1439 */
1440 if (CONFIG(SOC_INTEL_CSE_SEND_EOP_ASYNC))
1441 return;
1442
Angel Pons28315f82022-04-19 10:03:56 +02001443 if (!CONFIG(USE_FSP_NOTIFY_PHASE_READY_TO_BOOT))
1444 cse_final_ready_to_boot();
1445
1446 if (!CONFIG(USE_FSP_NOTIFY_PHASE_END_OF_FIRMWARE))
1447 cse_final_end_of_firmware();
Subrata Banik90e318b2022-02-06 16:26:45 +05301448}
1449
Nico Huber57686192022-08-06 19:11:55 +02001450struct device_operations cse_ops = {
Subrata Banik38abbda2021-09-30 13:15:50 +05301451 .set_resources = pci_dev_set_resources,
Andrey Petrov04a72c42017-03-01 15:51:57 -08001452 .read_resources = pci_dev_read_resources,
1453 .enable_resources = pci_dev_enable_resources,
1454 .init = pci_dev_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +05301455 .ops_pci = &pci_dev_ops_pci,
Sean Rhodes69ed3ed2021-04-30 16:38:17 +01001456 .enable = cse_set_state,
Subrata Banik90e318b2022-02-06 16:26:45 +05301457 .final = cse_final,
Andrey Petrov04a72c42017-03-01 15:51:57 -08001458};
1459
Hannah Williams63142152017-06-12 14:03:18 -07001460static const unsigned short pci_device_ids[] = {
Wonkyu Kim9f401072020-11-13 15:16:32 -08001461 PCI_DID_INTEL_MTL_CSE0,
Felix Singer43b7f412022-03-07 04:34:52 +01001462 PCI_DID_INTEL_APL_CSE0,
1463 PCI_DID_INTEL_GLK_CSE0,
1464 PCI_DID_INTEL_CNL_CSE0,
Felix Singer43b7f412022-03-07 04:34:52 +01001465 PCI_DID_INTEL_LWB_CSE0,
1466 PCI_DID_INTEL_LWB_CSE0_SUPER,
1467 PCI_DID_INTEL_CNP_H_CSE0,
Felix Singer43b7f412022-03-07 04:34:52 +01001468 PCI_DID_INTEL_CMP_CSE0,
1469 PCI_DID_INTEL_CMP_H_CSE0,
1470 PCI_DID_INTEL_TGL_CSE0,
1471 PCI_DID_INTEL_TGL_H_CSE0,
1472 PCI_DID_INTEL_MCC_CSE0,
1473 PCI_DID_INTEL_MCC_CSE1,
1474 PCI_DID_INTEL_MCC_CSE2,
1475 PCI_DID_INTEL_MCC_CSE3,
1476 PCI_DID_INTEL_JSP_CSE0,
1477 PCI_DID_INTEL_JSP_CSE1,
1478 PCI_DID_INTEL_JSP_CSE2,
1479 PCI_DID_INTEL_JSP_CSE3,
1480 PCI_DID_INTEL_ADP_P_CSE0,
1481 PCI_DID_INTEL_ADP_P_CSE1,
1482 PCI_DID_INTEL_ADP_P_CSE2,
1483 PCI_DID_INTEL_ADP_P_CSE3,
1484 PCI_DID_INTEL_ADP_S_CSE0,
1485 PCI_DID_INTEL_ADP_S_CSE1,
1486 PCI_DID_INTEL_ADP_S_CSE2,
1487 PCI_DID_INTEL_ADP_S_CSE3,
1488 PCI_DID_INTEL_ADP_M_CSE0,
1489 PCI_DID_INTEL_ADP_M_CSE1,
1490 PCI_DID_INTEL_ADP_M_CSE2,
1491 PCI_DID_INTEL_ADP_M_CSE3,
Hannah Williams63142152017-06-12 14:03:18 -07001492 0,
1493};
1494
Andrey Petrov04a72c42017-03-01 15:51:57 -08001495static const struct pci_driver cse_driver __pci_driver = {
1496 .ops = &cse_ops,
Felix Singer43b7f412022-03-07 04:34:52 +01001497 .vendor = PCI_VID_INTEL,
Andrey Petrov04a72c42017-03-01 15:51:57 -08001498 /* SoC/chipset needs to provide PCI device ID */
Andrey Petrov0405de92017-06-05 13:25:29 -07001499 .devices = pci_device_ids
Andrey Petrov04a72c42017-03-01 15:51:57 -08001500};
1501
1502#endif