blob: 8daf6d2db629cc6df2ea62ad8853820cd7b03685 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Andrey Petrov04a72c42017-03-01 15:51:57 -08003
Subrata Banik05e06cd2017-11-09 15:04:09 +05304#include <assert.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -08005#include <commonlib/helpers.h>
6#include <console/console.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -08008#include <delay.h>
9#include <device/pci.h>
10#include <device/pci_ids.h>
11#include <device/pci_ops.h>
12#include <intelblocks/cse.h>
Subrata Banik05e06cd2017-11-09 15:04:09 +053013#include <soc/iomap.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080014#include <soc/pci_devs.h>
Sridhar Siricilla8e465452019-09-23 20:59:38 +053015#include <soc/me.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080016#include <string.h>
17#include <timer.h>
18
Subrata Banik5c08c732017-11-13 14:54:37 +053019#define MAX_HECI_MESSAGE_RETRY_COUNT 5
20
Andrey Petrov04a72c42017-03-01 15:51:57 -080021/* Wait up to 15 sec for HECI to get ready */
22#define HECI_DELAY_READY (15 * 1000)
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +010023/* Wait up to 100 usec between circular buffer polls */
Andrey Petrov04a72c42017-03-01 15:51:57 -080024#define HECI_DELAY 100
25/* Wait up to 5 sec for CSE to chew something we sent */
26#define HECI_SEND_TIMEOUT (5 * 1000)
27/* Wait up to 5 sec for CSE to blurp a reply */
28#define HECI_READ_TIMEOUT (5 * 1000)
29
30#define SLOT_SIZE sizeof(uint32_t)
31
32#define MMIO_CSE_CB_WW 0x00
33#define MMIO_HOST_CSR 0x04
34#define MMIO_CSE_CB_RW 0x08
35#define MMIO_CSE_CSR 0x0c
36
37#define CSR_IE (1 << 0)
38#define CSR_IS (1 << 1)
39#define CSR_IG (1 << 2)
40#define CSR_READY (1 << 3)
41#define CSR_RESET (1 << 4)
42#define CSR_RP_START 8
43#define CSR_RP (((1 << 8) - 1) << CSR_RP_START)
44#define CSR_WP_START 16
45#define CSR_WP (((1 << 8) - 1) << CSR_WP_START)
46#define CSR_CBD_START 24
47#define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START)
48
49#define MEI_HDR_IS_COMPLETE (1 << 31)
50#define MEI_HDR_LENGTH_START 16
51#define MEI_HDR_LENGTH_SIZE 9
52#define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \
53 << MEI_HDR_LENGTH_START)
54#define MEI_HDR_HOST_ADDR_START 8
55#define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START)
56#define MEI_HDR_CSE_ADDR_START 0
57#define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START)
58
Sridhar Siricilla09ea3712019-11-12 23:35:50 +053059/* Wait up to 5 seconds for CSE to boot from RO(BP1) */
60#define CSE_DELAY_BOOT_TO_RO (5 * 1000)
61
Arthur Heymans3d6ccd02019-05-27 17:25:23 +020062static struct cse_device {
Andrey Petrov04a72c42017-03-01 15:51:57 -080063 uintptr_t sec_bar;
Patrick Georgic9b13592019-11-29 11:47:47 +010064} cse;
Andrey Petrov04a72c42017-03-01 15:51:57 -080065
66/*
67 * Initialize the device with provided temporary BAR. If BAR is 0 use a
68 * default. This is intended for pre-mem usage only where BARs haven't been
69 * assigned yet and devices are not enabled.
70 */
71void heci_init(uintptr_t tempbar)
72{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020073#if defined(__SIMPLE_DEVICE__)
74 pci_devfn_t dev = PCH_DEV_CSE;
75#else
76 struct device *dev = PCH_DEV_CSE;
77#endif
Andrey Petrov04a72c42017-03-01 15:51:57 -080078 u8 pcireg;
79
80 /* Assume it is already initialized, nothing else to do */
Patrick Georgic9b13592019-11-29 11:47:47 +010081 if (cse.sec_bar)
Andrey Petrov04a72c42017-03-01 15:51:57 -080082 return;
83
84 /* Use default pre-ram bar */
85 if (!tempbar)
86 tempbar = HECI1_BASE_ADDRESS;
87
88 /* Assign Resources to HECI1 */
89 /* Clear BIT 1-2 of Command Register */
90 pcireg = pci_read_config8(dev, PCI_COMMAND);
91 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
92 pci_write_config8(dev, PCI_COMMAND, pcireg);
93
94 /* Program Temporary BAR for HECI1 */
95 pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar);
96 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0);
97
98 /* Enable Bus Master and MMIO Space */
99 pcireg = pci_read_config8(dev, PCI_COMMAND);
100 pcireg |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
101 pci_write_config8(dev, PCI_COMMAND, pcireg);
102
Patrick Georgic9b13592019-11-29 11:47:47 +0100103 cse.sec_bar = tempbar;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800104}
105
Subrata Banik05e06cd2017-11-09 15:04:09 +0530106/* Get HECI BAR 0 from PCI configuration space */
107static uint32_t get_cse_bar(void)
108{
109 uintptr_t bar;
110
111 bar = pci_read_config32(PCH_DEV_CSE, PCI_BASE_ADDRESS_0);
112 assert(bar != 0);
113 /*
114 * Bits 31-12 are the base address as per EDS for SPI,
115 * Don't care about 0-11 bit
116 */
117 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
118}
119
Andrey Petrov04a72c42017-03-01 15:51:57 -0800120static uint32_t read_bar(uint32_t offset)
121{
Patrick Georgi08c8cf92019-12-02 11:43:20 +0100122 /* Load and cache BAR */
Patrick Georgic9b13592019-11-29 11:47:47 +0100123 if (!cse.sec_bar)
124 cse.sec_bar = get_cse_bar();
125 return read32((void *)(cse.sec_bar + offset));
Andrey Petrov04a72c42017-03-01 15:51:57 -0800126}
127
128static void write_bar(uint32_t offset, uint32_t val)
129{
Patrick Georgi08c8cf92019-12-02 11:43:20 +0100130 /* Load and cache BAR */
Patrick Georgic9b13592019-11-29 11:47:47 +0100131 if (!cse.sec_bar)
132 cse.sec_bar = get_cse_bar();
133 return write32((void *)(cse.sec_bar + offset), val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800134}
135
136static uint32_t read_cse_csr(void)
137{
138 return read_bar(MMIO_CSE_CSR);
139}
140
141static uint32_t read_host_csr(void)
142{
143 return read_bar(MMIO_HOST_CSR);
144}
145
146static void write_host_csr(uint32_t data)
147{
148 write_bar(MMIO_HOST_CSR, data);
149}
150
151static size_t filled_slots(uint32_t data)
152{
153 uint8_t wp, rp;
154 rp = data >> CSR_RP_START;
155 wp = data >> CSR_WP_START;
156 return (uint8_t) (wp - rp);
157}
158
159static size_t cse_filled_slots(void)
160{
161 return filled_slots(read_cse_csr());
162}
163
164static size_t host_empty_slots(void)
165{
166 uint32_t csr;
167 csr = read_host_csr();
168
169 return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr);
170}
171
172static void clear_int(void)
173{
174 uint32_t csr;
175 csr = read_host_csr();
176 csr |= CSR_IS;
177 write_host_csr(csr);
178}
179
180static uint32_t read_slot(void)
181{
182 return read_bar(MMIO_CSE_CB_RW);
183}
184
185static void write_slot(uint32_t val)
186{
187 write_bar(MMIO_CSE_CB_WW, val);
188}
189
190static int wait_write_slots(size_t cnt)
191{
192 struct stopwatch sw;
193
194 stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT);
195 while (host_empty_slots() < cnt) {
196 udelay(HECI_DELAY);
197 if (stopwatch_expired(&sw)) {
198 printk(BIOS_ERR, "HECI: timeout, buffer not drained\n");
199 return 0;
200 }
201 }
202 return 1;
203}
204
205static int wait_read_slots(size_t cnt)
206{
207 struct stopwatch sw;
208
209 stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT);
210 while (cse_filled_slots() < cnt) {
211 udelay(HECI_DELAY);
212 if (stopwatch_expired(&sw)) {
213 printk(BIOS_ERR, "HECI: timed out reading answer!\n");
214 return 0;
215 }
216 }
217 return 1;
218}
219
220/* get number of full 4-byte slots */
221static size_t bytes_to_slots(size_t bytes)
222{
223 return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE;
224}
225
226static int cse_ready(void)
227{
228 uint32_t csr;
229 csr = read_cse_csr();
230 return csr & CSR_READY;
231}
232
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530233static bool cse_check_hfs1_com(int mode)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530234{
235 union me_hfsts1 hfs1;
236 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530237 return hfs1.fields.operation_mode == mode;
238}
239
240bool cse_is_hfs1_cws_normal(void)
241{
242 union me_hfsts1 hfs1;
243 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
244 if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL)
245 return true;
246 return false;
247}
248
249bool cse_is_hfs1_com_normal(void)
250{
251 return cse_check_hfs1_com(ME_HFS1_COM_NORMAL);
252}
253
254bool cse_is_hfs1_com_secover_mei_msg(void)
255{
256 return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG);
257}
258
259bool cse_is_hfs1_com_soft_temp_disable(void)
260{
261 return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE);
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530262}
263
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530264bool cse_is_hfs3_fw_sku_custom(void)
265{
266 union me_hfsts3 hfs3;
267 hfs3.data = me_read_config32(PCI_ME_HFSTS3);
268 return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_CUSTOM;
269}
270
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530271/* Makes the host ready to communicate with CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530272void cse_set_host_ready(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530273{
274 uint32_t csr;
275 csr = read_host_csr();
276 csr &= ~CSR_RESET;
277 csr |= (CSR_IG | CSR_READY);
278 write_host_csr(csr);
279}
280
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530281/* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */
282uint8_t cse_wait_sec_override_mode(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530283{
284 struct stopwatch sw;
285 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530286 while (!cse_is_hfs1_com_secover_mei_msg()) {
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530287 udelay(HECI_DELAY);
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530288 if (stopwatch_expired(&sw)) {
289 printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n");
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530290 return 0;
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530291 }
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530292 }
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530293 printk(BIOS_DEBUG, "HECI: CSE took %lu ms to enter security override mode\n",
294 stopwatch_duration_msecs(&sw));
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530295 return 1;
296}
297
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530298/*
299 * Polls for CSE's current operation mode 'Soft Temporary Disable'.
300 * The CSE enters the current operation mode when it boots from RO(BP1).
301 */
302uint8_t cse_wait_com_soft_temp_disable(void)
303{
304 struct stopwatch sw;
305 stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO);
306 while (!cse_is_hfs1_com_soft_temp_disable()) {
307 udelay(HECI_DELAY);
308 if (stopwatch_expired(&sw)) {
309 printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n");
310 return 0;
311 }
312 }
313 printk(BIOS_SPEW, "HECI: CSE took %lu ms to boot from RO\n",
314 stopwatch_duration_msecs(&sw));
315 return 1;
316}
317
Andrey Petrov04a72c42017-03-01 15:51:57 -0800318static int wait_heci_ready(void)
319{
320 struct stopwatch sw;
321
322 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY);
323 while (!cse_ready()) {
324 udelay(HECI_DELAY);
325 if (stopwatch_expired(&sw))
326 return 0;
327 }
328
329 return 1;
330}
331
332static void host_gen_interrupt(void)
333{
334 uint32_t csr;
335 csr = read_host_csr();
336 csr |= CSR_IG;
337 write_host_csr(csr);
338}
339
340static size_t hdr_get_length(uint32_t hdr)
341{
342 return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START;
343}
344
345static int
346send_one_message(uint32_t hdr, const void *buff)
347{
348 size_t pend_len, pend_slots, remainder, i;
349 uint32_t tmp;
350 const uint32_t *p = buff;
351
352 /* Get space for the header */
353 if (!wait_write_slots(1))
354 return 0;
355
356 /* First, write header */
357 write_slot(hdr);
358
359 pend_len = hdr_get_length(hdr);
360 pend_slots = bytes_to_slots(pend_len);
361
362 if (!wait_write_slots(pend_slots))
363 return 0;
364
365 /* Write the body in whole slots */
366 i = 0;
367 while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) {
368 write_slot(*p++);
369 i += SLOT_SIZE;
370 }
371
372 remainder = pend_len % SLOT_SIZE;
373 /* Pad to 4 bytes not touching caller's buffer */
374 if (remainder) {
375 memcpy(&tmp, p, remainder);
376 write_slot(tmp);
377 }
378
379 host_gen_interrupt();
380
381 /* Make sure nothing bad happened during transmission */
382 if (!cse_ready())
383 return 0;
384
385 return pend_len;
386}
387
388int
389heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr)
390{
Subrata Banik5c08c732017-11-13 14:54:37 +0530391 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800392 uint32_t csr, hdr;
Subrata Banik5c08c732017-11-13 14:54:37 +0530393 size_t sent, remaining, cb_size, max_length;
394 const uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800395
396 if (!msg || !len)
397 return 0;
398
399 clear_int();
400
Subrata Banik5c08c732017-11-13 14:54:37 +0530401 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
402 p = msg;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800403
Subrata Banik5c08c732017-11-13 14:54:37 +0530404 if (!wait_heci_ready()) {
405 printk(BIOS_ERR, "HECI: not ready\n");
406 continue;
407 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800408
Subrata Banik4a722f52017-11-13 14:56:42 +0530409 csr = read_host_csr();
Subrata Banik5c08c732017-11-13 14:54:37 +0530410 cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE;
411 /*
412 * Reserve one slot for the header. Limit max message
413 * length by 9 bits that are available in the header.
414 */
415 max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1)
416 - SLOT_SIZE;
417 remaining = len;
418
419 /*
420 * Fragment the message into smaller messages not exceeding
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100421 * useful circular buffer length. Mark last message complete.
Subrata Banik5c08c732017-11-13 14:54:37 +0530422 */
423 do {
424 hdr = MIN(max_length, remaining)
425 << MEI_HDR_LENGTH_START;
426 hdr |= client_addr << MEI_HDR_CSE_ADDR_START;
427 hdr |= host_addr << MEI_HDR_HOST_ADDR_START;
428 hdr |= (MIN(max_length, remaining) == remaining) ?
Lee Leahy68ab0b52017-03-10 13:42:34 -0800429 MEI_HDR_IS_COMPLETE : 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530430 sent = send_one_message(hdr, p);
431 p += sent;
432 remaining -= sent;
433 } while (remaining > 0 && sent != 0);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800434
Subrata Banik5c08c732017-11-13 14:54:37 +0530435 if (!remaining)
436 return 1;
437 }
438 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800439}
440
441static size_t
442recv_one_message(uint32_t *hdr, void *buff, size_t maxlen)
443{
444 uint32_t reg, *p = buff;
445 size_t recv_slots, recv_len, remainder, i;
446
447 /* first get the header */
448 if (!wait_read_slots(1))
449 return 0;
450
451 *hdr = read_slot();
452 recv_len = hdr_get_length(*hdr);
453
454 if (!recv_len)
455 printk(BIOS_WARNING, "HECI: message is zero-sized\n");
456
457 recv_slots = bytes_to_slots(recv_len);
458
459 i = 0;
460 if (recv_len > maxlen) {
461 printk(BIOS_ERR, "HECI: response is too big\n");
462 return 0;
463 }
464
465 /* wait for the rest of messages to arrive */
466 wait_read_slots(recv_slots);
467
468 /* fetch whole slots first */
469 while (i < ALIGN_DOWN(recv_len, SLOT_SIZE)) {
470 *p++ = read_slot();
471 i += SLOT_SIZE;
472 }
473
Subrata Banik5c08c732017-11-13 14:54:37 +0530474 /*
475 * If ME is not ready, something went wrong and
476 * we received junk
477 */
478 if (!cse_ready())
479 return 0;
480
Andrey Petrov04a72c42017-03-01 15:51:57 -0800481 remainder = recv_len % SLOT_SIZE;
482
483 if (remainder) {
484 reg = read_slot();
485 memcpy(p, &reg, remainder);
486 }
487
488 return recv_len;
489}
490
491int heci_receive(void *buff, size_t *maxlen)
492{
Subrata Banik5c08c732017-11-13 14:54:37 +0530493 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800494 size_t left, received;
495 uint32_t hdr = 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530496 uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800497
498 if (!buff || !maxlen || !*maxlen)
499 return 0;
500
Andrey Petrov04a72c42017-03-01 15:51:57 -0800501 clear_int();
502
Subrata Banik5c08c732017-11-13 14:54:37 +0530503 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
504 p = buff;
505 left = *maxlen;
506
507 if (!wait_heci_ready()) {
508 printk(BIOS_ERR, "HECI: not ready\n");
509 continue;
510 }
511
512 /*
513 * Receive multiple packets until we meet one marked
514 * complete or we run out of space in caller-provided buffer.
515 */
516 do {
517 received = recv_one_message(&hdr, p, left);
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800518 if (!received) {
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200519 printk(BIOS_ERR, "HECI: Failed to receive!\n");
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800520 return 0;
521 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530522 left -= received;
523 p += received;
524 /* If we read out everything ping to send more */
525 if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots())
526 host_gen_interrupt();
527 } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0);
528
529 if ((hdr & MEI_HDR_IS_COMPLETE) && received) {
530 *maxlen = p - (uint8_t *) buff;
531 return 1;
532 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800533 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530534 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800535}
536
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530537int heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg, size_t *rcv_sz)
538{
539 if (!heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, HECI_MKHI_ADDR)) {
540 printk(BIOS_ERR, "HECI: send Failed\n");
541 return 0;
542 }
543
544 if (rcv_msg != NULL) {
545 if (!heci_receive(rcv_msg, rcv_sz)) {
546 printk(BIOS_ERR, "HECI: receive Failed\n");
547 return 0;
548 }
549 }
550 return 1;
551}
552
Andrey Petrov04a72c42017-03-01 15:51:57 -0800553/*
554 * Attempt to reset the device. This is useful when host and ME are out
555 * of sync during transmission or ME didn't understand the message.
556 */
557int heci_reset(void)
558{
559 uint32_t csr;
560
561 /* Send reset request */
562 csr = read_host_csr();
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530563 csr |= (CSR_RESET | CSR_IG);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800564 write_host_csr(csr);
565
566 if (wait_heci_ready()) {
567 /* Device is back on its imaginary feet, clear reset */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530568 cse_set_host_ready();
Andrey Petrov04a72c42017-03-01 15:51:57 -0800569 return 1;
570 }
571
572 printk(BIOS_CRIT, "HECI: reset failed\n");
573
574 return 0;
575}
576
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530577bool is_cse_enabled(void)
578{
579 const struct device *cse_dev = pcidev_path_on_root(PCH_DEVFN_CSE);
580
581 if (!cse_dev || !cse_dev->enabled) {
582 printk(BIOS_WARNING, "HECI: No CSE device\n");
583 return false;
584 }
585
586 if (pci_read_config16(PCH_DEV_CSE, PCI_VENDOR_ID) == 0xFFFF) {
587 printk(BIOS_WARNING, "HECI: CSE device is hidden\n");
588 return false;
589 }
590
591 return true;
592}
593
594uint32_t me_read_config32(int offset)
595{
596 return pci_read_config32(PCH_DEV_CSE, offset);
597}
598
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530599static bool cse_is_global_reset_allowed(void)
600{
601 /*
602 * Allow sending GLOBAL_RESET command only if:
603 * - CSE's current working state is Normal and current operation mode is Normal.
604 * - (or) CSE's current working state is normal and current operation mode can
605 * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is
606 * Custom.
607 */
608 if (!cse_is_hfs1_cws_normal())
609 return false;
610
611 if (cse_is_hfs1_com_normal())
612 return true;
613
614 if (cse_is_hfs3_fw_sku_custom()) {
615 if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg())
616 return true;
617 }
618 return false;
619}
620
Sridhar Siricillad415c202019-08-31 14:54:57 +0530621/*
Sridhar Siricillac2a2d2b2020-02-27 17:16:13 +0530622 * Sends GLOBAL_RESET_REQ cmd to CSE.The reset type can be GLOBAL_RESET/CSE_RESET_ONLY.
Sridhar Siricillad415c202019-08-31 14:54:57 +0530623 */
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530624int cse_request_global_reset(enum rst_req_type rst_type)
Sridhar Siricillad415c202019-08-31 14:54:57 +0530625{
626 int status;
627 struct mkhi_hdr reply;
628 struct reset_message {
629 struct mkhi_hdr hdr;
630 uint8_t req_origin;
631 uint8_t reset_type;
632 } __packed;
633 struct reset_message msg = {
634 .hdr = {
635 .group_id = MKHI_GROUP_ID_CBM,
Sridhar Siricillae202e672020-01-07 23:36:40 +0530636 .command = MKHI_CBM_GLOBAL_RESET_REQ,
Sridhar Siricillad415c202019-08-31 14:54:57 +0530637 },
638 .req_origin = GR_ORIGIN_BIOS_POST,
639 .reset_type = rst_type
640 };
641 size_t reply_size;
642
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530643 printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type);
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530644
Sridhar Siricillac2a2d2b2020-02-27 17:16:13 +0530645 if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) {
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530646 printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n");
647 return 0;
648 }
Sridhar Siricillad415c202019-08-31 14:54:57 +0530649
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530650 if (!cse_is_global_reset_allowed()) {
651 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
652 return 0;
653 }
654
Sridhar Siricillad415c202019-08-31 14:54:57 +0530655 heci_reset();
656
657 reply_size = sizeof(reply);
658 memset(&reply, 0, reply_size);
659
Sridhar Siricillad415c202019-08-31 14:54:57 +0530660 if (rst_type == CSE_RESET_ONLY)
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530661 status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530662 else
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530663 status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530664
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530665 printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", status ? "success" : "failure");
666 return status;
Sridhar Siricillad415c202019-08-31 14:54:57 +0530667}
668
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530669static bool cse_is_hmrfpo_enable_allowed(void)
670{
671 /*
672 * Allow sending HMRFPO ENABLE command only if:
673 * - CSE's current working state is Normal and current operation mode is Normal
674 * - (or) cse's current working state is normal and current operation mode is
675 * Soft Temp Disable if CSE's Firmware SKU is Custom
676 */
677 if (!cse_is_hfs1_cws_normal())
678 return false;
679
680 if (cse_is_hfs1_com_normal())
681 return true;
682
683 if (cse_is_hfs3_fw_sku_custom() && cse_is_hfs1_com_soft_temp_disable())
684 return true;
685
686 return false;
687}
688
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530689/* Sends HMRFPO Enable command to CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530690int cse_hmrfpo_enable(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530691{
692 struct hmrfpo_enable_msg {
693 struct mkhi_hdr hdr;
694 uint32_t nonce[2];
695 } __packed;
696
697 /* HMRFPO Enable message */
698 struct hmrfpo_enable_msg msg = {
699 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530700 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530701 .command = MKHI_HMRFPO_ENABLE,
702 },
703 .nonce = {0},
704 };
705
706 /* HMRFPO Enable response */
707 struct hmrfpo_enable_resp {
708 struct mkhi_hdr hdr;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530709 /* Base addr for factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530710 uint32_t fct_base;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530711 /* Length of factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530712 uint32_t fct_limit;
713 uint8_t status;
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530714 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530715 } __packed;
716
717 struct hmrfpo_enable_resp resp;
718 size_t resp_size = sizeof(struct hmrfpo_enable_resp);
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530719
720 printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n");
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530721
722 if (!cse_is_hmrfpo_enable_allowed()) {
723 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
724 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530725 }
726
727 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg),
728 &resp, &resp_size))
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530729 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530730
731 if (resp.hdr.result) {
732 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530733 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530734 }
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530735
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530736 if (resp.status) {
737 printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status);
738 return 0;
739 }
740
741 return 1;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530742}
743
744/*
745 * Sends HMRFPO Get Status command to CSE to get the HMRFPO status.
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530746 * The status can be DISABLED/LOCKED/ENABLED
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530747 */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530748int cse_hmrfpo_get_status(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530749{
750 struct hmrfpo_get_status_msg {
751 struct mkhi_hdr hdr;
752 } __packed;
753
754 struct hmrfpo_get_status_resp {
755 struct mkhi_hdr hdr;
756 uint8_t status;
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530757 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530758 } __packed;
759
760 struct hmrfpo_get_status_msg msg = {
761 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530762 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530763 .command = MKHI_HMRFPO_GET_STATUS,
764 },
765 };
766 struct hmrfpo_get_status_resp resp;
767 size_t resp_size = sizeof(struct hmrfpo_get_status_resp);
768
769 printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n");
770
Sridhar Siricilla206905c2020-02-06 18:48:22 +0530771 if (!cse_is_hfs1_cws_normal()) {
772 printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n");
773 return -1;
774 }
775
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530776 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg),
777 &resp, &resp_size)) {
778 printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n");
779 return -1;
780 }
781
782 if (resp.hdr.result) {
783 printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n",
784 resp.hdr.result);
785 return -1;
786 }
787
788 return resp.status;
789}
790
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530791void print_me_fw_version(void *unused)
792{
793 struct version {
794 uint16_t minor;
795 uint16_t major;
796 uint16_t build;
797 uint16_t hotfix;
798 } __packed;
799
800 struct fw_ver_resp {
801 struct mkhi_hdr hdr;
802 struct version code;
803 struct version rec;
804 struct version fitc;
805 } __packed;
806
807 const struct mkhi_hdr fw_ver_msg = {
808 .group_id = MKHI_GROUP_ID_GEN,
809 .command = MKHI_GEN_GET_FW_VERSION,
810 };
811
812 struct fw_ver_resp resp;
813 size_t resp_size = sizeof(resp);
814
815 /* Ignore if UART debugging is disabled */
816 if (!CONFIG(CONSOLE_SERIAL))
817 return;
818
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200819 /* Ignore if CSE is disabled */
820 if (!is_cse_enabled())
821 return;
822
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530823 /*
824 * Ignore if ME Firmware SKU type is custom since
825 * print_boot_partition_info() logs RO(BP1) and RW(BP2) versions.
826 */
827 if (cse_is_hfs3_fw_sku_custom())
828 return;
829
830 /*
831 * Prerequisites:
832 * 1) HFSTS1 Current Working State is Normal
833 * 2) HFSTS1 Current Operation Mode is Normal
834 * 3) It's after DRAM INIT DONE message (taken care of by calling it
835 * during ramstage
836 */
837 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal())
838 goto fail;
839
840 heci_reset();
841
842 if (!heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), &resp, &resp_size))
843 goto fail;
844
845 if (resp.hdr.result)
846 goto fail;
847
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
852fail:
853 printk(BIOS_DEBUG, "ME: Version: Unavailable\n");
854}
855
Andrey Petrov04a72c42017-03-01 15:51:57 -0800856#if ENV_RAMSTAGE
857
858static void update_sec_bar(struct device *dev)
859{
Patrick Georgic9b13592019-11-29 11:47:47 +0100860 cse.sec_bar = find_resource(dev, PCI_BASE_ADDRESS_0)->base;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800861}
862
863static void cse_set_resources(struct device *dev)
864{
Subrata Banik2ee54db2017-03-05 12:37:00 +0530865 if (dev->path.pci.devfn == PCH_DEVFN_CSE)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800866 update_sec_bar(dev);
867
868 pci_dev_set_resources(dev);
869}
870
871static struct device_operations cse_ops = {
872 .set_resources = cse_set_resources,
873 .read_resources = pci_dev_read_resources,
874 .enable_resources = pci_dev_enable_resources,
875 .init = pci_dev_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530876 .ops_pci = &pci_dev_ops_pci,
Andrey Petrov04a72c42017-03-01 15:51:57 -0800877};
878
Hannah Williams63142152017-06-12 14:03:18 -0700879static const unsigned short pci_device_ids[] = {
880 PCI_DEVICE_ID_INTEL_APL_CSE0,
881 PCI_DEVICE_ID_INTEL_GLK_CSE0,
Andrey Petrov0405de92017-06-05 13:25:29 -0700882 PCI_DEVICE_ID_INTEL_CNL_CSE0,
Subrata Banikd0586d22017-11-27 13:28:41 +0530883 PCI_DEVICE_ID_INTEL_SKL_CSE0,
Maxim Polyakov571d07d2019-08-22 13:11:32 +0300884 PCI_DEVICE_ID_INTEL_LWB_CSE0,
885 PCI_DEVICE_ID_INTEL_LWB_CSE0_SUPER,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800886 PCI_DEVICE_ID_INTEL_CNP_H_CSE0,
Aamir Bohra9eac0392018-06-30 12:07:04 +0530887 PCI_DEVICE_ID_INTEL_ICL_CSE0,
Ronak Kanabarda7ffb482019-02-05 01:51:13 +0530888 PCI_DEVICE_ID_INTEL_CMP_CSE0,
Gaggery Tsai12a651c2019-12-05 11:23:20 -0800889 PCI_DEVICE_ID_INTEL_CMP_H_CSE0,
Ravi Sarawadi6b5bf402019-10-21 22:25:04 -0700890 PCI_DEVICE_ID_INTEL_TGL_CSE0,
Tan, Lean Sheng26136092020-01-20 19:13:56 -0800891 PCI_DEVICE_ID_INTEL_MCC_CSE0,
892 PCI_DEVICE_ID_INTEL_MCC_CSE1,
893 PCI_DEVICE_ID_INTEL_MCC_CSE2,
894 PCI_DEVICE_ID_INTEL_MCC_CSE3,
Meera Ravindranath3f4af0d2020-02-12 16:01:22 +0530895 PCI_DEVICE_ID_INTEL_JSP_CSE0,
896 PCI_DEVICE_ID_INTEL_JSP_CSE1,
897 PCI_DEVICE_ID_INTEL_JSP_CSE2,
898 PCI_DEVICE_ID_INTEL_JSP_CSE3,
Hannah Williams63142152017-06-12 14:03:18 -0700899 0,
900};
901
Andrey Petrov04a72c42017-03-01 15:51:57 -0800902static const struct pci_driver cse_driver __pci_driver = {
903 .ops = &cse_ops,
904 .vendor = PCI_VENDOR_ID_INTEL,
905 /* SoC/chipset needs to provide PCI device ID */
Andrey Petrov0405de92017-06-05 13:25:29 -0700906 .devices = pci_device_ids
Andrey Petrov04a72c42017-03-01 15:51:57 -0800907};
908
909#endif