blob: 7d6fabaa9be19d52c082b5c722a8234846eebce8 [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>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010014#include <option.h>
Tim Wawrzynczak09635f42021-06-18 10:08:47 -060015#include <security/vboot/misc.h>
16#include <security/vboot/vboot_common.h>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010017#include <soc/intel/common/reset.h>
Subrata Banik05e06cd2017-11-09 15:04:09 +053018#include <soc/iomap.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080019#include <soc/pci_devs.h>
Sridhar Siricilla8e465452019-09-23 20:59:38 +053020#include <soc/me.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080021#include <string.h>
22#include <timer.h>
Sean Rhodes69ed3ed2021-04-30 16:38:17 +010023#include <types.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080024
Subrata Banik5c08c732017-11-13 14:54:37 +053025#define MAX_HECI_MESSAGE_RETRY_COUNT 5
26
Andrey Petrov04a72c42017-03-01 15:51:57 -080027/* Wait up to 15 sec for HECI to get ready */
Subrata Banik03aef282021-09-28 18:10:24 +053028#define HECI_DELAY_READY_MS (15 * 1000)
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +010029/* Wait up to 100 usec between circular buffer polls */
Subrata Banik03aef282021-09-28 18:10:24 +053030#define HECI_DELAY_US 100
Andrey Petrov04a72c42017-03-01 15:51:57 -080031/* Wait up to 5 sec for CSE to chew something we sent */
Subrata Banik03aef282021-09-28 18:10:24 +053032#define HECI_SEND_TIMEOUT_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080033/* Wait up to 5 sec for CSE to blurp a reply */
Subrata Banik03aef282021-09-28 18:10:24 +053034#define HECI_READ_TIMEOUT_MS (5 * 1000)
Subrata Banika219edb2021-09-25 15:02:37 +053035/* Wait up to 1 ms for CSE CIP */
Subrata Banik03aef282021-09-28 18:10:24 +053036#define HECI_CIP_TIMEOUT_US 1000
Subrata Banikf5765812021-09-30 13:37:10 +053037/* Wait up to 5 seconds for CSE to boot from RO(BP1) */
38#define CSE_DELAY_BOOT_TO_RO_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080039
40#define SLOT_SIZE sizeof(uint32_t)
41
42#define MMIO_CSE_CB_WW 0x00
43#define MMIO_HOST_CSR 0x04
44#define MMIO_CSE_CB_RW 0x08
45#define MMIO_CSE_CSR 0x0c
Subrata Banika219edb2021-09-25 15:02:37 +053046#define MMIO_CSE_DEVIDLE 0x800
47#define CSE_DEV_IDLE (1 << 2)
48#define CSE_DEV_CIP (1 << 0)
Andrey Petrov04a72c42017-03-01 15:51:57 -080049
50#define CSR_IE (1 << 0)
51#define CSR_IS (1 << 1)
52#define CSR_IG (1 << 2)
53#define CSR_READY (1 << 3)
54#define CSR_RESET (1 << 4)
55#define CSR_RP_START 8
56#define CSR_RP (((1 << 8) - 1) << CSR_RP_START)
57#define CSR_WP_START 16
58#define CSR_WP (((1 << 8) - 1) << CSR_WP_START)
59#define CSR_CBD_START 24
60#define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START)
61
62#define MEI_HDR_IS_COMPLETE (1 << 31)
63#define MEI_HDR_LENGTH_START 16
64#define MEI_HDR_LENGTH_SIZE 9
65#define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \
66 << MEI_HDR_LENGTH_START)
67#define MEI_HDR_HOST_ADDR_START 8
68#define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START)
69#define MEI_HDR_CSE_ADDR_START 0
70#define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START)
71
Subrata Banik38abbda2021-09-30 13:15:50 +053072/* Get HECI BAR 0 from PCI configuration space */
Subrata Banikc6e25522021-09-30 18:14:09 +053073static uintptr_t get_cse_bar(pci_devfn_t dev)
Subrata Banik38abbda2021-09-30 13:15:50 +053074{
75 uintptr_t bar;
76
Subrata Banikc6e25522021-09-30 18:14:09 +053077 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
Subrata Banik38abbda2021-09-30 13:15:50 +053078 assert(bar != 0);
79 /*
80 * Bits 31-12 are the base address as per EDS for SPI,
81 * Don't care about 0-11 bit
82 */
83 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
84}
Andrey Petrov04a72c42017-03-01 15:51:57 -080085
86/*
87 * Initialize the device with provided temporary BAR. If BAR is 0 use a
88 * default. This is intended for pre-mem usage only where BARs haven't been
89 * assigned yet and devices are not enabled.
90 */
91void heci_init(uintptr_t tempbar)
92{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020093 pci_devfn_t dev = PCH_DEV_CSE;
Subrata Banikc6e25522021-09-30 18:14:09 +053094
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +020095 u16 pcireg;
Andrey Petrov04a72c42017-03-01 15:51:57 -080096
97 /* Assume it is already initialized, nothing else to do */
Subrata Banikc6e25522021-09-30 18:14:09 +053098 if (get_cse_bar(dev))
Andrey Petrov04a72c42017-03-01 15:51:57 -080099 return;
100
101 /* Use default pre-ram bar */
102 if (!tempbar)
103 tempbar = HECI1_BASE_ADDRESS;
104
105 /* Assign Resources to HECI1 */
106 /* Clear BIT 1-2 of Command Register */
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200107 pcireg = pci_read_config16(dev, PCI_COMMAND);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800108 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200109 pci_write_config16(dev, PCI_COMMAND, pcireg);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800110
111 /* Program Temporary BAR for HECI1 */
112 pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar);
113 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0);
114
115 /* Enable Bus Master and MMIO Space */
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200116 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Sridhar Siricillacb2fd202021-06-09 19:27:06 +0530117
118 /* Trigger HECI Reset and make Host ready for communication with CSE */
119 heci_reset();
Subrata Banik05e06cd2017-11-09 15:04:09 +0530120}
121
Subrata Banikc6e25522021-09-30 18:14:09 +0530122static uint32_t read_bar(pci_devfn_t dev, uint32_t offset)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800123{
Subrata Banikc6e25522021-09-30 18:14:09 +0530124 return read32p(get_cse_bar(dev) + offset);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800125}
126
Subrata Banikc6e25522021-09-30 18:14:09 +0530127static void write_bar(pci_devfn_t dev, uint32_t offset, uint32_t val)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800128{
Subrata Banikc6e25522021-09-30 18:14:09 +0530129 return write32p(get_cse_bar(dev) + offset, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800130}
131
132static uint32_t read_cse_csr(void)
133{
Subrata Banikc6e25522021-09-30 18:14:09 +0530134 return read_bar(PCH_DEV_CSE, MMIO_CSE_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800135}
136
137static uint32_t read_host_csr(void)
138{
Subrata Banikc6e25522021-09-30 18:14:09 +0530139 return read_bar(PCH_DEV_CSE, MMIO_HOST_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800140}
141
142static void write_host_csr(uint32_t data)
143{
Subrata Banikc6e25522021-09-30 18:14:09 +0530144 write_bar(PCH_DEV_CSE, MMIO_HOST_CSR, data);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800145}
146
147static size_t filled_slots(uint32_t data)
148{
149 uint8_t wp, rp;
150 rp = data >> CSR_RP_START;
151 wp = data >> CSR_WP_START;
152 return (uint8_t) (wp - rp);
153}
154
155static size_t cse_filled_slots(void)
156{
157 return filled_slots(read_cse_csr());
158}
159
160static size_t host_empty_slots(void)
161{
162 uint32_t csr;
163 csr = read_host_csr();
164
165 return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr);
166}
167
168static void clear_int(void)
169{
170 uint32_t csr;
171 csr = read_host_csr();
172 csr |= CSR_IS;
173 write_host_csr(csr);
174}
175
176static uint32_t read_slot(void)
177{
Subrata Banikc6e25522021-09-30 18:14:09 +0530178 return read_bar(PCH_DEV_CSE, MMIO_CSE_CB_RW);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800179}
180
181static void write_slot(uint32_t val)
182{
Subrata Banikc6e25522021-09-30 18:14:09 +0530183 write_bar(PCH_DEV_CSE, MMIO_CSE_CB_WW, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800184}
185
186static int wait_write_slots(size_t cnt)
187{
188 struct stopwatch sw;
189
Subrata Banik03aef282021-09-28 18:10:24 +0530190 stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800191 while (host_empty_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530192 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800193 if (stopwatch_expired(&sw)) {
194 printk(BIOS_ERR, "HECI: timeout, buffer not drained\n");
195 return 0;
196 }
197 }
198 return 1;
199}
200
201static int wait_read_slots(size_t cnt)
202{
203 struct stopwatch sw;
204
Subrata Banik03aef282021-09-28 18:10:24 +0530205 stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800206 while (cse_filled_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530207 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800208 if (stopwatch_expired(&sw)) {
209 printk(BIOS_ERR, "HECI: timed out reading answer!\n");
210 return 0;
211 }
212 }
213 return 1;
214}
215
216/* get number of full 4-byte slots */
217static size_t bytes_to_slots(size_t bytes)
218{
219 return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE;
220}
221
222static int cse_ready(void)
223{
224 uint32_t csr;
225 csr = read_cse_csr();
226 return csr & CSR_READY;
227}
228
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530229static bool cse_check_hfs1_com(int mode)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530230{
231 union me_hfsts1 hfs1;
232 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530233 return hfs1.fields.operation_mode == mode;
234}
235
236bool cse_is_hfs1_cws_normal(void)
237{
238 union me_hfsts1 hfs1;
239 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
240 if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL)
241 return true;
242 return false;
243}
244
245bool cse_is_hfs1_com_normal(void)
246{
247 return cse_check_hfs1_com(ME_HFS1_COM_NORMAL);
248}
249
250bool cse_is_hfs1_com_secover_mei_msg(void)
251{
252 return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG);
253}
254
255bool cse_is_hfs1_com_soft_temp_disable(void)
256{
257 return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE);
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530258}
259
Subrata Banike74ebcd2021-12-27 10:49:19 +0000260/*
261 * TGL HFSTS1.spi_protection_mode bit replaces the previous
262 * `manufacturing mode (mfg_mode)` without changing the offset and purpose
263 * of this bit.
264 *
265 * Using HFSTS1.mfg_mode to get the SPI protection status for all PCH.
266 * mfg_mode = 0 means SPI protection in on.
267 * mfg_mode = 1 means SPI is unprotected.
268 */
269bool cse_is_hfs1_spi_protected(void)
270{
271 union me_hfsts1 hfs1;
272 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
273 return !hfs1.fields.mfg_mode;
274}
275
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530276bool cse_is_hfs3_fw_sku_lite(void)
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530277{
278 union me_hfsts3 hfs3;
279 hfs3.data = me_read_config32(PCI_ME_HFSTS3);
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530280 return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_LITE;
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530281}
282
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530283/* Makes the host ready to communicate with CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530284void cse_set_host_ready(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530285{
286 uint32_t csr;
287 csr = read_host_csr();
288 csr &= ~CSR_RESET;
289 csr |= (CSR_IG | CSR_READY);
290 write_host_csr(csr);
291}
292
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530293/* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */
294uint8_t cse_wait_sec_override_mode(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530295{
296 struct stopwatch sw;
Subrata Banik03aef282021-09-28 18:10:24 +0530297 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530298 while (!cse_is_hfs1_com_secover_mei_msg()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530299 udelay(HECI_DELAY_US);
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530300 if (stopwatch_expired(&sw)) {
301 printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n");
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530302 return 0;
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530303 }
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530304 }
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530305 printk(BIOS_DEBUG, "HECI: CSE took %lu ms to enter security override mode\n",
306 stopwatch_duration_msecs(&sw));
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530307 return 1;
308}
309
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530310/*
311 * Polls for CSE's current operation mode 'Soft Temporary Disable'.
312 * The CSE enters the current operation mode when it boots from RO(BP1).
313 */
314uint8_t cse_wait_com_soft_temp_disable(void)
315{
316 struct stopwatch sw;
Subrata Banikf5765812021-09-30 13:37:10 +0530317 stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO_MS);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530318 while (!cse_is_hfs1_com_soft_temp_disable()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530319 udelay(HECI_DELAY_US);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530320 if (stopwatch_expired(&sw)) {
321 printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n");
322 return 0;
323 }
324 }
325 printk(BIOS_SPEW, "HECI: CSE took %lu ms to boot from RO\n",
326 stopwatch_duration_msecs(&sw));
327 return 1;
328}
329
Andrey Petrov04a72c42017-03-01 15:51:57 -0800330static int wait_heci_ready(void)
331{
332 struct stopwatch sw;
333
Subrata Banik03aef282021-09-28 18:10:24 +0530334 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800335 while (!cse_ready()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530336 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800337 if (stopwatch_expired(&sw))
338 return 0;
339 }
340
341 return 1;
342}
343
344static void host_gen_interrupt(void)
345{
346 uint32_t csr;
347 csr = read_host_csr();
348 csr |= CSR_IG;
349 write_host_csr(csr);
350}
351
352static size_t hdr_get_length(uint32_t hdr)
353{
354 return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START;
355}
356
357static int
358send_one_message(uint32_t hdr, const void *buff)
359{
360 size_t pend_len, pend_slots, remainder, i;
361 uint32_t tmp;
362 const uint32_t *p = buff;
363
364 /* Get space for the header */
365 if (!wait_write_slots(1))
366 return 0;
367
368 /* First, write header */
369 write_slot(hdr);
370
371 pend_len = hdr_get_length(hdr);
372 pend_slots = bytes_to_slots(pend_len);
373
374 if (!wait_write_slots(pend_slots))
375 return 0;
376
377 /* Write the body in whole slots */
378 i = 0;
379 while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) {
380 write_slot(*p++);
381 i += SLOT_SIZE;
382 }
383
384 remainder = pend_len % SLOT_SIZE;
385 /* Pad to 4 bytes not touching caller's buffer */
386 if (remainder) {
387 memcpy(&tmp, p, remainder);
388 write_slot(tmp);
389 }
390
391 host_gen_interrupt();
392
393 /* Make sure nothing bad happened during transmission */
394 if (!cse_ready())
395 return 0;
396
397 return pend_len;
398}
399
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530400/*
401 * Send message msg of size len to host from host_addr to cse_addr.
402 * Returns 1 on success and 0 otherwise.
403 * In case of error heci_reset() may be required.
404 */
405static int
Andrey Petrov04a72c42017-03-01 15:51:57 -0800406heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr)
407{
Subrata Banik5c08c732017-11-13 14:54:37 +0530408 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800409 uint32_t csr, hdr;
Subrata Banik5c08c732017-11-13 14:54:37 +0530410 size_t sent, remaining, cb_size, max_length;
411 const uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800412
413 if (!msg || !len)
414 return 0;
415
416 clear_int();
417
Subrata Banik5c08c732017-11-13 14:54:37 +0530418 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
419 p = msg;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800420
Subrata Banik5c08c732017-11-13 14:54:37 +0530421 if (!wait_heci_ready()) {
422 printk(BIOS_ERR, "HECI: not ready\n");
423 continue;
424 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800425
Subrata Banik4a722f52017-11-13 14:56:42 +0530426 csr = read_host_csr();
Subrata Banik5c08c732017-11-13 14:54:37 +0530427 cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE;
428 /*
429 * Reserve one slot for the header. Limit max message
430 * length by 9 bits that are available in the header.
431 */
432 max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1)
433 - SLOT_SIZE;
434 remaining = len;
435
436 /*
437 * Fragment the message into smaller messages not exceeding
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100438 * useful circular buffer length. Mark last message complete.
Subrata Banik5c08c732017-11-13 14:54:37 +0530439 */
440 do {
441 hdr = MIN(max_length, remaining)
442 << MEI_HDR_LENGTH_START;
443 hdr |= client_addr << MEI_HDR_CSE_ADDR_START;
444 hdr |= host_addr << MEI_HDR_HOST_ADDR_START;
445 hdr |= (MIN(max_length, remaining) == remaining) ?
Lee Leahy68ab0b52017-03-10 13:42:34 -0800446 MEI_HDR_IS_COMPLETE : 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530447 sent = send_one_message(hdr, p);
448 p += sent;
449 remaining -= sent;
450 } while (remaining > 0 && sent != 0);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800451
Subrata Banik5c08c732017-11-13 14:54:37 +0530452 if (!remaining)
453 return 1;
454 }
455 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800456}
457
458static size_t
459recv_one_message(uint32_t *hdr, void *buff, size_t maxlen)
460{
461 uint32_t reg, *p = buff;
462 size_t recv_slots, recv_len, remainder, i;
463
464 /* first get the header */
465 if (!wait_read_slots(1))
466 return 0;
467
468 *hdr = read_slot();
469 recv_len = hdr_get_length(*hdr);
470
471 if (!recv_len)
472 printk(BIOS_WARNING, "HECI: message is zero-sized\n");
473
474 recv_slots = bytes_to_slots(recv_len);
475
476 i = 0;
477 if (recv_len > maxlen) {
478 printk(BIOS_ERR, "HECI: response is too big\n");
479 return 0;
480 }
481
482 /* wait for the rest of messages to arrive */
483 wait_read_slots(recv_slots);
484
485 /* fetch whole slots first */
486 while (i < ALIGN_DOWN(recv_len, SLOT_SIZE)) {
487 *p++ = read_slot();
488 i += SLOT_SIZE;
489 }
490
Subrata Banik5c08c732017-11-13 14:54:37 +0530491 /*
492 * If ME is not ready, something went wrong and
493 * we received junk
494 */
495 if (!cse_ready())
496 return 0;
497
Andrey Petrov04a72c42017-03-01 15:51:57 -0800498 remainder = recv_len % SLOT_SIZE;
499
500 if (remainder) {
501 reg = read_slot();
502 memcpy(p, &reg, remainder);
503 }
504
505 return recv_len;
506}
507
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530508/*
509 * Receive message into buff not exceeding maxlen. Message is considered
510 * successfully received if a 'complete' indication is read from ME side
511 * and there was enough space in the buffer to fit that message. maxlen
512 * is updated with size of message that was received. Returns 0 on failure
513 * and 1 on success.
514 * In case of error heci_reset() may be required.
515 */
516static int heci_receive(void *buff, size_t *maxlen)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800517{
Subrata Banik5c08c732017-11-13 14:54:37 +0530518 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800519 size_t left, received;
520 uint32_t hdr = 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530521 uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800522
523 if (!buff || !maxlen || !*maxlen)
524 return 0;
525
Andrey Petrov04a72c42017-03-01 15:51:57 -0800526 clear_int();
527
Subrata Banik5c08c732017-11-13 14:54:37 +0530528 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
529 p = buff;
530 left = *maxlen;
531
532 if (!wait_heci_ready()) {
533 printk(BIOS_ERR, "HECI: not ready\n");
534 continue;
535 }
536
537 /*
538 * Receive multiple packets until we meet one marked
539 * complete or we run out of space in caller-provided buffer.
540 */
541 do {
542 received = recv_one_message(&hdr, p, left);
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800543 if (!received) {
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200544 printk(BIOS_ERR, "HECI: Failed to receive!\n");
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800545 return 0;
546 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530547 left -= received;
548 p += received;
549 /* If we read out everything ping to send more */
550 if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots())
551 host_gen_interrupt();
552 } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0);
553
554 if ((hdr & MEI_HDR_IS_COMPLETE) && received) {
555 *maxlen = p - (uint8_t *) buff;
556 return 1;
557 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800558 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530559 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800560}
561
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530562int heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg, size_t *rcv_sz,
563 uint8_t cse_addr)
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530564{
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530565 if (!heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, cse_addr)) {
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530566 printk(BIOS_ERR, "HECI: send Failed\n");
567 return 0;
568 }
569
570 if (rcv_msg != NULL) {
571 if (!heci_receive(rcv_msg, rcv_sz)) {
572 printk(BIOS_ERR, "HECI: receive Failed\n");
573 return 0;
574 }
575 }
576 return 1;
577}
578
Andrey Petrov04a72c42017-03-01 15:51:57 -0800579/*
580 * Attempt to reset the device. This is useful when host and ME are out
581 * of sync during transmission or ME didn't understand the message.
582 */
583int heci_reset(void)
584{
585 uint32_t csr;
586
Duncan Laurie15ca9032020-11-05 10:09:07 -0800587 /* Clear post code to prevent eventlog entry from unknown code. */
588 post_code(0);
589
Andrey Petrov04a72c42017-03-01 15:51:57 -0800590 /* Send reset request */
591 csr = read_host_csr();
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530592 csr |= (CSR_RESET | CSR_IG);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800593 write_host_csr(csr);
594
595 if (wait_heci_ready()) {
596 /* Device is back on its imaginary feet, clear reset */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530597 cse_set_host_ready();
Andrey Petrov04a72c42017-03-01 15:51:57 -0800598 return 1;
599 }
600
601 printk(BIOS_CRIT, "HECI: reset failed\n");
602
603 return 0;
604}
605
Subrata Banik3710e992021-09-30 16:59:09 +0530606bool is_cse_devfn_visible(unsigned int devfn)
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530607{
Subrata Banik3710e992021-09-30 16:59:09 +0530608 int slot = PCI_SLOT(devfn);
609 int func = PCI_FUNC(devfn);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530610
Subrata Banik3710e992021-09-30 16:59:09 +0530611 if (!is_devfn_enabled(devfn)) {
612 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is disabled\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530613 return false;
614 }
615
Subrata Banik3710e992021-09-30 16:59:09 +0530616 if (pci_read_config16(PCI_DEV(0, slot, func), PCI_VENDOR_ID) == 0xFFFF) {
617 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is hidden\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530618 return false;
619 }
620
621 return true;
622}
623
Subrata Banik3710e992021-09-30 16:59:09 +0530624bool is_cse_enabled(void)
625{
626 return is_cse_devfn_visible(PCH_DEVFN_CSE);
627}
628
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530629uint32_t me_read_config32(int offset)
630{
631 return pci_read_config32(PCH_DEV_CSE, offset);
632}
633
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530634static bool cse_is_global_reset_allowed(void)
635{
636 /*
637 * Allow sending GLOBAL_RESET command only if:
638 * - CSE's current working state is Normal and current operation mode is Normal.
639 * - (or) CSE's current working state is normal and current operation mode can
640 * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530641 * Lite.
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530642 */
643 if (!cse_is_hfs1_cws_normal())
644 return false;
645
646 if (cse_is_hfs1_com_normal())
647 return true;
648
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530649 if (cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530650 if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg())
651 return true;
652 }
653 return false;
654}
655
Sridhar Siricillad415c202019-08-31 14:54:57 +0530656/*
Subrata Banikf463dc02020-09-14 19:04:03 +0530657 * Sends GLOBAL_RESET_REQ cmd to CSE with reset type GLOBAL_RESET.
658 * Returns 0 on failure and 1 on success.
Sridhar Siricillad415c202019-08-31 14:54:57 +0530659 */
Subrata Banikf463dc02020-09-14 19:04:03 +0530660static int cse_request_reset(enum rst_req_type rst_type)
Sridhar Siricillad415c202019-08-31 14:54:57 +0530661{
662 int status;
663 struct mkhi_hdr reply;
664 struct reset_message {
665 struct mkhi_hdr hdr;
666 uint8_t req_origin;
667 uint8_t reset_type;
668 } __packed;
669 struct reset_message msg = {
670 .hdr = {
671 .group_id = MKHI_GROUP_ID_CBM,
Sridhar Siricillae202e672020-01-07 23:36:40 +0530672 .command = MKHI_CBM_GLOBAL_RESET_REQ,
Sridhar Siricillad415c202019-08-31 14:54:57 +0530673 },
674 .req_origin = GR_ORIGIN_BIOS_POST,
675 .reset_type = rst_type
676 };
677 size_t reply_size;
678
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530679 printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type);
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530680
Sridhar Siricillac2a2d2b2020-02-27 17:16:13 +0530681 if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) {
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530682 printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n");
683 return 0;
684 }
Sridhar Siricillad415c202019-08-31 14:54:57 +0530685
Subrata Banikf463dc02020-09-14 19:04:03 +0530686 if (!cse_is_global_reset_allowed() || !is_cse_enabled()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530687 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
688 return 0;
689 }
690
Sridhar Siricillad415c202019-08-31 14:54:57 +0530691 heci_reset();
692
693 reply_size = sizeof(reply);
694 memset(&reply, 0, reply_size);
695
Sridhar Siricillad415c202019-08-31 14:54:57 +0530696 if (rst_type == CSE_RESET_ONLY)
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530697 status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530698 else
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530699 status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size,
700 HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530701
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530702 printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", status ? "success" : "failure");
703 return status;
Sridhar Siricillad415c202019-08-31 14:54:57 +0530704}
705
Subrata Banikf463dc02020-09-14 19:04:03 +0530706int cse_request_global_reset(void)
707{
708 return cse_request_reset(GLOBAL_RESET);
709}
710
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530711static bool cse_is_hmrfpo_enable_allowed(void)
712{
713 /*
714 * Allow sending HMRFPO ENABLE command only if:
715 * - CSE's current working state is Normal and current operation mode is Normal
716 * - (or) cse's current working state is normal and current operation mode is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530717 * Soft Temp Disable if CSE's Firmware SKU is Lite
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530718 */
719 if (!cse_is_hfs1_cws_normal())
720 return false;
721
722 if (cse_is_hfs1_com_normal())
723 return true;
724
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530725 if (cse_is_hfs3_fw_sku_lite() && cse_is_hfs1_com_soft_temp_disable())
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530726 return true;
727
728 return false;
729}
730
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530731/* Sends HMRFPO Enable command to CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530732int cse_hmrfpo_enable(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530733{
734 struct hmrfpo_enable_msg {
735 struct mkhi_hdr hdr;
736 uint32_t nonce[2];
737 } __packed;
738
739 /* HMRFPO Enable message */
740 struct hmrfpo_enable_msg msg = {
741 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530742 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530743 .command = MKHI_HMRFPO_ENABLE,
744 },
745 .nonce = {0},
746 };
747
748 /* HMRFPO Enable response */
749 struct hmrfpo_enable_resp {
750 struct mkhi_hdr hdr;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530751 /* Base addr for factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530752 uint32_t fct_base;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530753 /* Length of factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530754 uint32_t fct_limit;
755 uint8_t status;
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530756 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530757 } __packed;
758
759 struct hmrfpo_enable_resp resp;
760 size_t resp_size = sizeof(struct hmrfpo_enable_resp);
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530761
Sridhar Siricilla49c25f22021-11-27 19:56:47 +0530762 if (cse_is_hfs1_com_secover_mei_msg()) {
763 printk(BIOS_DEBUG, "HECI: CSE is already in security override mode, "
764 "skip sending HMRFPO_ENABLE command to CSE\n");
765 return 1;
766 }
767
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530768 printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n");
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530769
770 if (!cse_is_hmrfpo_enable_allowed()) {
771 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
772 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530773 }
774
775 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530776 &resp, &resp_size, HECI_MKHI_ADDR))
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530777 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530778
779 if (resp.hdr.result) {
780 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530781 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530782 }
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530783
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530784 if (resp.status) {
785 printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status);
786 return 0;
787 }
788
789 return 1;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530790}
791
792/*
793 * Sends HMRFPO Get Status command to CSE to get the HMRFPO status.
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530794 * The status can be DISABLED/LOCKED/ENABLED
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530795 */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530796int cse_hmrfpo_get_status(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530797{
798 struct hmrfpo_get_status_msg {
799 struct mkhi_hdr hdr;
800 } __packed;
801
802 struct hmrfpo_get_status_resp {
803 struct mkhi_hdr hdr;
804 uint8_t status;
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530805 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530806 } __packed;
807
808 struct hmrfpo_get_status_msg msg = {
809 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530810 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530811 .command = MKHI_HMRFPO_GET_STATUS,
812 },
813 };
814 struct hmrfpo_get_status_resp resp;
815 size_t resp_size = sizeof(struct hmrfpo_get_status_resp);
816
817 printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n");
818
Sridhar Siricilla206905c2020-02-06 18:48:22 +0530819 if (!cse_is_hfs1_cws_normal()) {
820 printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n");
821 return -1;
822 }
823
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530824 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530825 &resp, &resp_size, HECI_MKHI_ADDR)) {
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530826 printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n");
827 return -1;
828 }
829
830 if (resp.hdr.result) {
831 printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n",
832 resp.hdr.result);
833 return -1;
834 }
835
836 return resp.status;
837}
838
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530839void print_me_fw_version(void *unused)
840{
Johnny Lin72e76672021-10-09 12:35:35 +0800841 struct me_fw_ver_resp resp = {0};
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530842
843 /* Ignore if UART debugging is disabled */
844 if (!CONFIG(CONSOLE_SERIAL))
845 return;
846
Johnny Lin72e76672021-10-09 12:35:35 +0800847 if (get_me_fw_version(&resp) == CB_SUCCESS) {
848 printk(BIOS_DEBUG, "ME: Version: %d.%d.%d.%d\n", resp.code.major,
849 resp.code.minor, resp.code.hotfix, resp.code.build);
850 return;
851 }
852 printk(BIOS_DEBUG, "ME: Version: Unavailable\n");
853}
854
855enum cb_err get_me_fw_version(struct me_fw_ver_resp *resp)
856{
857 const struct mkhi_hdr fw_ver_msg = {
858 .group_id = MKHI_GROUP_ID_GEN,
859 .command = MKHI_GEN_GET_FW_VERSION,
860 };
861
862 if (resp == NULL) {
863 printk(BIOS_ERR, "%s failed, null pointer parameter\n", __func__);
864 return CB_ERR;
865 }
866 size_t resp_size = sizeof(*resp);
867
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200868 /* Ignore if CSE is disabled */
869 if (!is_cse_enabled())
Johnny Lin72e76672021-10-09 12:35:35 +0800870 return CB_ERR;
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200871
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530872 /*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530873 * Ignore if ME Firmware SKU type is Lite since
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530874 * print_boot_partition_info() logs RO(BP1) and RW(BP2) versions.
875 */
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530876 if (cse_is_hfs3_fw_sku_lite())
Johnny Lin72e76672021-10-09 12:35:35 +0800877 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530878
879 /*
880 * Prerequisites:
881 * 1) HFSTS1 Current Working State is Normal
882 * 2) HFSTS1 Current Operation Mode is Normal
883 * 3) It's after DRAM INIT DONE message (taken care of by calling it
884 * during ramstage
885 */
886 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal())
Johnny Lin72e76672021-10-09 12:35:35 +0800887 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530888
889 heci_reset();
890
Johnny Lin72e76672021-10-09 12:35:35 +0800891 if (!heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), resp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530892 HECI_MKHI_ADDR))
Johnny Lin72e76672021-10-09 12:35:35 +0800893 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530894
Johnny Lin72e76672021-10-09 12:35:35 +0800895 if (resp->hdr.result)
896 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530897
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530898
Johnny Lin72e76672021-10-09 12:35:35 +0800899 return CB_SUCCESS;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530900}
901
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600902void cse_trigger_vboot_recovery(enum csme_failure_reason reason)
903{
904 printk(BIOS_DEBUG, "cse: CSE status registers: HFSTS1: 0x%x, HFSTS2: 0x%x "
905 "HFSTS3: 0x%x\n", me_read_config32(PCI_ME_HFSTS1),
906 me_read_config32(PCI_ME_HFSTS2), me_read_config32(PCI_ME_HFSTS3));
907
908 if (CONFIG(VBOOT)) {
909 struct vb2_context *ctx = vboot_get_context();
910 if (ctx == NULL)
911 goto failure;
912 vb2api_fail(ctx, VB2_RECOVERY_INTEL_CSE_LITE_SKU, reason);
913 vboot_save_data(ctx);
914 vboot_reboot();
915 }
916failure:
917 die("cse: Failed to trigger recovery mode(recovery subcode:%d)\n", reason);
918}
919
Subrata Banikc6e25522021-09-30 18:14:09 +0530920static bool disable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530921{
922 struct stopwatch sw;
Subrata Banikc6e25522021-09-30 18:14:09 +0530923 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530924 dev_idle_ctrl &= ~CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530925 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530926
Subrata Banik03aef282021-09-28 18:10:24 +0530927 stopwatch_init_usecs_expire(&sw, HECI_CIP_TIMEOUT_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530928 do {
Subrata Banikc6e25522021-09-30 18:14:09 +0530929 dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530930 if ((dev_idle_ctrl & CSE_DEV_CIP) == CSE_DEV_CIP)
931 return true;
Subrata Banik03aef282021-09-28 18:10:24 +0530932 udelay(HECI_DELAY_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530933 } while (!stopwatch_expired(&sw));
934
935 return false;
936}
937
Subrata Banikc6e25522021-09-30 18:14:09 +0530938static void enable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530939{
Subrata Banikc6e25522021-09-30 18:14:09 +0530940 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530941 dev_idle_ctrl |= CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530942 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530943}
944
Subrata Banikc6e25522021-09-30 18:14:09 +0530945enum cse_device_state get_cse_device_state(unsigned int devfn)
Subrata Banika219edb2021-09-25 15:02:37 +0530946{
Subrata Banikc6e25522021-09-30 18:14:09 +0530947 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
948 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530949 if ((dev_idle_ctrl & CSE_DEV_IDLE) == CSE_DEV_IDLE)
950 return DEV_IDLE;
951
952 return DEV_ACTIVE;
953}
954
Subrata Banikc6e25522021-09-30 18:14:09 +0530955static enum cse_device_state ensure_cse_active(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530956{
Subrata Banikc6e25522021-09-30 18:14:09 +0530957 if (!disable_cse_idle(dev))
Subrata Banika219edb2021-09-25 15:02:37 +0530958 return DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530959 pci_or_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Subrata Banika219edb2021-09-25 15:02:37 +0530960
961 return DEV_ACTIVE;
962}
963
Subrata Banikc6e25522021-09-30 18:14:09 +0530964static void ensure_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530965{
Subrata Banikc6e25522021-09-30 18:14:09 +0530966 enable_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +0530967
Subrata Banikc6e25522021-09-30 18:14:09 +0530968 pci_and_config32(dev, PCI_COMMAND, ~(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
Subrata Banika219edb2021-09-25 15:02:37 +0530969}
970
Subrata Banikc6e25522021-09-30 18:14:09 +0530971bool set_cse_device_state(unsigned int devfn, enum cse_device_state requested_state)
Subrata Banika219edb2021-09-25 15:02:37 +0530972{
Subrata Banikc6e25522021-09-30 18:14:09 +0530973 enum cse_device_state current_state = get_cse_device_state(devfn);
974 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
Subrata Banika219edb2021-09-25 15:02:37 +0530975
976 if (current_state == requested_state)
977 return true;
978
979 if (requested_state == DEV_ACTIVE)
Subrata Banikc6e25522021-09-30 18:14:09 +0530980 return ensure_cse_active(dev) == requested_state;
Subrata Banika219edb2021-09-25 15:02:37 +0530981 else
Subrata Banikc6e25522021-09-30 18:14:09 +0530982 ensure_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +0530983
984 return true;
985}
986
Andrey Petrov04a72c42017-03-01 15:51:57 -0800987#if ENV_RAMSTAGE
988
Sean Rhodes69ed3ed2021-04-30 16:38:17 +0100989/*
990 * Disable the Intel (CS)Management Engine via HECI based on a cmos value
991 * of `me_state`. A value of `0` will result in a (CS)ME state of `0` (working)
992 * and value of `1` will result in a (CS)ME state of `3` (disabled).
993 *
994 * It isn't advised to use this in combination with me_cleaner.
995 *
996 * It is advisable to have a second cmos option called `me_state_counter`.
997 * Whilst not essential, it avoid reboots loops if the (CS)ME fails to
998 * change states after 3 attempts. Some versions of the (CS)ME need to be
999 * reset 3 times.
1000 *
1001 * Ideal cmos values would be:
1002 *
1003 * # coreboot config options: cpu
1004 * 432 1 e 5 me_state
1005 * 440 4 h 0 me_state_counter
1006 *
1007 * #ID value text
1008 * 5 0 Enable
1009 * 5 1 Disable
1010 */
1011
1012static void me_reset_with_count(void)
1013{
1014 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter", UINT_MAX);
1015
1016 if (cmos_me_state_counter != UINT_MAX) {
1017 printk(BIOS_DEBUG, "CMOS: me_state_counter = %u\n", cmos_me_state_counter);
1018 /* Avoid boot loops by only trying a state change 3 times */
1019 if (cmos_me_state_counter < ME_DISABLE_ATTEMPTS) {
1020 cmos_me_state_counter++;
1021 set_uint_option("me_state_counter", cmos_me_state_counter);
1022 printk(BIOS_DEBUG, "ME: Reset attempt %u/%u.\n", cmos_me_state_counter,
1023 ME_DISABLE_ATTEMPTS);
1024 do_global_reset();
1025 } else {
1026 /*
1027 * If the (CS)ME fails to change states after 3 attempts, it will
1028 * likely need a cold boot, or recovering.
1029 */
1030 printk(BIOS_ERR, "Error: Failed to change ME state in %u attempts!\n",
1031 ME_DISABLE_ATTEMPTS);
1032
1033 }
1034 } else {
1035 printk(BIOS_DEBUG, "ME: Resetting");
1036 do_global_reset();
1037 }
1038}
1039
1040static void cse_set_state(struct device *dev)
1041{
1042
1043 /* (CS)ME Disable Command */
1044 struct me_disable_command {
1045 struct mkhi_hdr hdr;
1046 uint32_t rule_id;
1047 uint8_t rule_len;
1048 uint32_t rule_data;
1049 } __packed me_disable = {
1050 .hdr = {
1051 .group_id = MKHI_GROUP_ID_FWCAPS,
1052 .command = MKHI_SET_ME_DISABLE,
1053 },
1054 .rule_id = ME_DISABLE_RULE_ID,
1055 .rule_len = ME_DISABLE_RULE_LENGTH,
1056 .rule_data = ME_DISABLE_COMMAND,
1057 };
1058
1059 struct me_disable_reply {
1060 struct mkhi_hdr hdr;
1061 uint32_t rule_id;
1062 } __packed;
1063
1064 struct me_disable_reply disable_reply;
1065
1066 size_t disable_reply_size;
1067
1068 /* (CS)ME Enable Command */
1069 struct me_enable_command {
1070 struct mkhi_hdr hdr;
1071 } me_enable = {
1072 .hdr = {
1073 .group_id = MKHI_GROUP_ID_BUP_COMMON,
1074 .command = MKHI_SET_ME_ENABLE,
1075 },
1076 };
1077
1078 struct me_enable_reply {
1079 struct mkhi_hdr hdr;
1080 } __packed;
1081
1082 struct me_enable_reply enable_reply;
1083
1084 size_t enable_reply_size;
1085
1086 /* Function Start */
1087
1088 int send;
1089 int result;
1090 /*
1091 * Check if the CMOS value "me_state" exists, if it doesn't, then
1092 * don't do anything.
1093 */
1094 const unsigned int cmos_me_state = get_uint_option("me_state", UINT_MAX);
1095
1096 if (cmos_me_state == UINT_MAX)
1097 return;
1098
1099 printk(BIOS_DEBUG, "CMOS: me_state = %u\n", cmos_me_state);
1100
1101 /*
1102 * We only take action if the me_state doesn't match the CS(ME) working state
1103 */
1104
1105 const unsigned int soft_temp_disable = cse_is_hfs1_com_soft_temp_disable();
1106
1107 if (cmos_me_state && !soft_temp_disable) {
1108 /* me_state should be disabled, but it's enabled */
1109 printk(BIOS_DEBUG, "ME needs to be disabled.\n");
1110 send = heci_send_receive(&me_disable, sizeof(me_disable),
1111 &disable_reply, &disable_reply_size, HECI_MKHI_ADDR);
1112 result = disable_reply.hdr.result;
1113 } else if (!cmos_me_state && soft_temp_disable) {
1114 /* me_state should be enabled, but it's disabled */
1115 printk(BIOS_DEBUG, "ME needs to be enabled.\n");
1116 send = heci_send_receive(&me_enable, sizeof(me_enable),
1117 &enable_reply, &enable_reply_size, HECI_MKHI_ADDR);
1118 result = enable_reply.hdr.result;
1119 } else {
1120 printk(BIOS_DEBUG, "ME is %s.\n", cmos_me_state ? "disabled" : "enabled");
1121 unsigned int cmos_me_state_counter = get_uint_option("me_state_counter",
1122 UINT_MAX);
1123 /* set me_state_counter to 0 */
1124 if ((cmos_me_state_counter != UINT_MAX && cmos_me_state_counter != 0))
1125 set_uint_option("me_state_counter", 0);
1126 return;
1127 }
1128
1129 printk(BIOS_DEBUG, "HECI: ME state change send %s!\n",
1130 send ? "success" : "failure");
1131 printk(BIOS_DEBUG, "HECI: ME state change result %s!\n",
1132 result ? "success" : "failure");
1133
1134 /*
1135 * Reset if the result was successful, or if the send failed as some older
1136 * version of the Intel (CS)ME won't successfully receive the message unless reset
1137 * twice.
1138 */
1139 if (send || !result)
1140 me_reset_with_count();
1141}
1142
Andrey Petrov04a72c42017-03-01 15:51:57 -08001143static struct device_operations cse_ops = {
Subrata Banik38abbda2021-09-30 13:15:50 +05301144 .set_resources = pci_dev_set_resources,
Andrey Petrov04a72c42017-03-01 15:51:57 -08001145 .read_resources = pci_dev_read_resources,
1146 .enable_resources = pci_dev_enable_resources,
1147 .init = pci_dev_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +05301148 .ops_pci = &pci_dev_ops_pci,
Sean Rhodes69ed3ed2021-04-30 16:38:17 +01001149 .enable = cse_set_state,
Andrey Petrov04a72c42017-03-01 15:51:57 -08001150};
1151
Hannah Williams63142152017-06-12 14:03:18 -07001152static const unsigned short pci_device_ids[] = {
1153 PCI_DEVICE_ID_INTEL_APL_CSE0,
1154 PCI_DEVICE_ID_INTEL_GLK_CSE0,
Andrey Petrov0405de92017-06-05 13:25:29 -07001155 PCI_DEVICE_ID_INTEL_CNL_CSE0,
Subrata Banikd0586d22017-11-27 13:28:41 +05301156 PCI_DEVICE_ID_INTEL_SKL_CSE0,
Maxim Polyakov571d07d2019-08-22 13:11:32 +03001157 PCI_DEVICE_ID_INTEL_LWB_CSE0,
1158 PCI_DEVICE_ID_INTEL_LWB_CSE0_SUPER,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +08001159 PCI_DEVICE_ID_INTEL_CNP_H_CSE0,
Aamir Bohra9eac0392018-06-30 12:07:04 +05301160 PCI_DEVICE_ID_INTEL_ICL_CSE0,
Ronak Kanabarda7ffb482019-02-05 01:51:13 +05301161 PCI_DEVICE_ID_INTEL_CMP_CSE0,
Gaggery Tsai12a651c2019-12-05 11:23:20 -08001162 PCI_DEVICE_ID_INTEL_CMP_H_CSE0,
Ravi Sarawadi6b5bf402019-10-21 22:25:04 -07001163 PCI_DEVICE_ID_INTEL_TGL_CSE0,
Jeremy Soller191a8d72021-08-10 14:06:51 -06001164 PCI_DEVICE_ID_INTEL_TGL_H_CSE0,
Tan, Lean Sheng26136092020-01-20 19:13:56 -08001165 PCI_DEVICE_ID_INTEL_MCC_CSE0,
1166 PCI_DEVICE_ID_INTEL_MCC_CSE1,
1167 PCI_DEVICE_ID_INTEL_MCC_CSE2,
1168 PCI_DEVICE_ID_INTEL_MCC_CSE3,
Meera Ravindranath3f4af0d2020-02-12 16:01:22 +05301169 PCI_DEVICE_ID_INTEL_JSP_CSE0,
1170 PCI_DEVICE_ID_INTEL_JSP_CSE1,
1171 PCI_DEVICE_ID_INTEL_JSP_CSE2,
1172 PCI_DEVICE_ID_INTEL_JSP_CSE3,
Subrata Banikf672f7f2020-08-03 14:29:25 +05301173 PCI_DEVICE_ID_INTEL_ADP_P_CSE0,
1174 PCI_DEVICE_ID_INTEL_ADP_P_CSE1,
1175 PCI_DEVICE_ID_INTEL_ADP_P_CSE2,
1176 PCI_DEVICE_ID_INTEL_ADP_P_CSE3,
1177 PCI_DEVICE_ID_INTEL_ADP_S_CSE0,
1178 PCI_DEVICE_ID_INTEL_ADP_S_CSE1,
1179 PCI_DEVICE_ID_INTEL_ADP_S_CSE2,
1180 PCI_DEVICE_ID_INTEL_ADP_S_CSE3,
Varshit Pandyaf4d98fdd22021-01-17 18:39:29 +05301181 PCI_DEVICE_ID_INTEL_ADP_M_CSE0,
1182 PCI_DEVICE_ID_INTEL_ADP_M_CSE1,
1183 PCI_DEVICE_ID_INTEL_ADP_M_CSE2,
1184 PCI_DEVICE_ID_INTEL_ADP_M_CSE3,
Hannah Williams63142152017-06-12 14:03:18 -07001185 0,
1186};
1187
Andrey Petrov04a72c42017-03-01 15:51:57 -08001188static const struct pci_driver cse_driver __pci_driver = {
1189 .ops = &cse_ops,
1190 .vendor = PCI_VENDOR_ID_INTEL,
1191 /* SoC/chipset needs to provide PCI device ID */
Andrey Petrov0405de92017-06-05 13:25:29 -07001192 .devices = pci_device_ids
Andrey Petrov04a72c42017-03-01 15:51:57 -08001193};
1194
1195#endif