blob: f37ff9589ebdb2e365498fef89e89154c39ce3ed [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>
Tim Wawrzynczak09635f42021-06-18 10:08:47 -060014#include <security/vboot/misc.h>
15#include <security/vboot/vboot_common.h>
Subrata Banik05e06cd2017-11-09 15:04:09 +053016#include <soc/iomap.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080017#include <soc/pci_devs.h>
Sridhar Siricilla8e465452019-09-23 20:59:38 +053018#include <soc/me.h>
Andrey Petrov04a72c42017-03-01 15:51:57 -080019#include <string.h>
20#include <timer.h>
21
Subrata Banik5c08c732017-11-13 14:54:37 +053022#define MAX_HECI_MESSAGE_RETRY_COUNT 5
23
Andrey Petrov04a72c42017-03-01 15:51:57 -080024/* Wait up to 15 sec for HECI to get ready */
Subrata Banik03aef282021-09-28 18:10:24 +053025#define HECI_DELAY_READY_MS (15 * 1000)
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +010026/* Wait up to 100 usec between circular buffer polls */
Subrata Banik03aef282021-09-28 18:10:24 +053027#define HECI_DELAY_US 100
Andrey Petrov04a72c42017-03-01 15:51:57 -080028/* Wait up to 5 sec for CSE to chew something we sent */
Subrata Banik03aef282021-09-28 18:10:24 +053029#define HECI_SEND_TIMEOUT_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080030/* Wait up to 5 sec for CSE to blurp a reply */
Subrata Banik03aef282021-09-28 18:10:24 +053031#define HECI_READ_TIMEOUT_MS (5 * 1000)
Subrata Banika219edb2021-09-25 15:02:37 +053032/* Wait up to 1 ms for CSE CIP */
Subrata Banik03aef282021-09-28 18:10:24 +053033#define HECI_CIP_TIMEOUT_US 1000
Subrata Banikf5765812021-09-30 13:37:10 +053034/* Wait up to 5 seconds for CSE to boot from RO(BP1) */
35#define CSE_DELAY_BOOT_TO_RO_MS (5 * 1000)
Andrey Petrov04a72c42017-03-01 15:51:57 -080036
37#define SLOT_SIZE sizeof(uint32_t)
38
39#define MMIO_CSE_CB_WW 0x00
40#define MMIO_HOST_CSR 0x04
41#define MMIO_CSE_CB_RW 0x08
42#define MMIO_CSE_CSR 0x0c
Subrata Banika219edb2021-09-25 15:02:37 +053043#define MMIO_CSE_DEVIDLE 0x800
44#define CSE_DEV_IDLE (1 << 2)
45#define CSE_DEV_CIP (1 << 0)
Andrey Petrov04a72c42017-03-01 15:51:57 -080046
47#define CSR_IE (1 << 0)
48#define CSR_IS (1 << 1)
49#define CSR_IG (1 << 2)
50#define CSR_READY (1 << 3)
51#define CSR_RESET (1 << 4)
52#define CSR_RP_START 8
53#define CSR_RP (((1 << 8) - 1) << CSR_RP_START)
54#define CSR_WP_START 16
55#define CSR_WP (((1 << 8) - 1) << CSR_WP_START)
56#define CSR_CBD_START 24
57#define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START)
58
59#define MEI_HDR_IS_COMPLETE (1 << 31)
60#define MEI_HDR_LENGTH_START 16
61#define MEI_HDR_LENGTH_SIZE 9
62#define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \
63 << MEI_HDR_LENGTH_START)
64#define MEI_HDR_HOST_ADDR_START 8
65#define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START)
66#define MEI_HDR_CSE_ADDR_START 0
67#define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START)
68
Subrata Banik38abbda2021-09-30 13:15:50 +053069/* Get HECI BAR 0 from PCI configuration space */
Subrata Banikc6e25522021-09-30 18:14:09 +053070static uintptr_t get_cse_bar(pci_devfn_t dev)
Subrata Banik38abbda2021-09-30 13:15:50 +053071{
72 uintptr_t bar;
73
Subrata Banikc6e25522021-09-30 18:14:09 +053074 bar = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
Subrata Banik38abbda2021-09-30 13:15:50 +053075 assert(bar != 0);
76 /*
77 * Bits 31-12 are the base address as per EDS for SPI,
78 * Don't care about 0-11 bit
79 */
80 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
81}
Andrey Petrov04a72c42017-03-01 15:51:57 -080082
83/*
84 * Initialize the device with provided temporary BAR. If BAR is 0 use a
85 * default. This is intended for pre-mem usage only where BARs haven't been
86 * assigned yet and devices are not enabled.
87 */
88void heci_init(uintptr_t tempbar)
89{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020090 pci_devfn_t dev = PCH_DEV_CSE;
Subrata Banikc6e25522021-09-30 18:14:09 +053091
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +020092 u16 pcireg;
Andrey Petrov04a72c42017-03-01 15:51:57 -080093
94 /* Assume it is already initialized, nothing else to do */
Subrata Banikc6e25522021-09-30 18:14:09 +053095 if (get_cse_bar(dev))
Andrey Petrov04a72c42017-03-01 15:51:57 -080096 return;
97
98 /* Use default pre-ram bar */
99 if (!tempbar)
100 tempbar = HECI1_BASE_ADDRESS;
101
102 /* Assign Resources to HECI1 */
103 /* Clear BIT 1-2 of Command Register */
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200104 pcireg = pci_read_config16(dev, PCI_COMMAND);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800105 pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200106 pci_write_config16(dev, PCI_COMMAND, pcireg);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800107
108 /* Program Temporary BAR for HECI1 */
109 pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar);
110 pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0);
111
112 /* Enable Bus Master and MMIO Space */
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200113 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Sridhar Siricillacb2fd202021-06-09 19:27:06 +0530114
115 /* Trigger HECI Reset and make Host ready for communication with CSE */
116 heci_reset();
Subrata Banik05e06cd2017-11-09 15:04:09 +0530117}
118
Subrata Banikc6e25522021-09-30 18:14:09 +0530119static uint32_t read_bar(pci_devfn_t dev, uint32_t offset)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800120{
Subrata Banikc6e25522021-09-30 18:14:09 +0530121 return read32p(get_cse_bar(dev) + offset);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800122}
123
Subrata Banikc6e25522021-09-30 18:14:09 +0530124static void write_bar(pci_devfn_t dev, uint32_t offset, uint32_t val)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800125{
Subrata Banikc6e25522021-09-30 18:14:09 +0530126 return write32p(get_cse_bar(dev) + offset, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800127}
128
129static uint32_t read_cse_csr(void)
130{
Subrata Banikc6e25522021-09-30 18:14:09 +0530131 return read_bar(PCH_DEV_CSE, MMIO_CSE_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800132}
133
134static uint32_t read_host_csr(void)
135{
Subrata Banikc6e25522021-09-30 18:14:09 +0530136 return read_bar(PCH_DEV_CSE, MMIO_HOST_CSR);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800137}
138
139static void write_host_csr(uint32_t data)
140{
Subrata Banikc6e25522021-09-30 18:14:09 +0530141 write_bar(PCH_DEV_CSE, MMIO_HOST_CSR, data);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800142}
143
144static size_t filled_slots(uint32_t data)
145{
146 uint8_t wp, rp;
147 rp = data >> CSR_RP_START;
148 wp = data >> CSR_WP_START;
149 return (uint8_t) (wp - rp);
150}
151
152static size_t cse_filled_slots(void)
153{
154 return filled_slots(read_cse_csr());
155}
156
157static size_t host_empty_slots(void)
158{
159 uint32_t csr;
160 csr = read_host_csr();
161
162 return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr);
163}
164
165static void clear_int(void)
166{
167 uint32_t csr;
168 csr = read_host_csr();
169 csr |= CSR_IS;
170 write_host_csr(csr);
171}
172
173static uint32_t read_slot(void)
174{
Subrata Banikc6e25522021-09-30 18:14:09 +0530175 return read_bar(PCH_DEV_CSE, MMIO_CSE_CB_RW);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800176}
177
178static void write_slot(uint32_t val)
179{
Subrata Banikc6e25522021-09-30 18:14:09 +0530180 write_bar(PCH_DEV_CSE, MMIO_CSE_CB_WW, val);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800181}
182
183static int wait_write_slots(size_t cnt)
184{
185 struct stopwatch sw;
186
Subrata Banik03aef282021-09-28 18:10:24 +0530187 stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800188 while (host_empty_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530189 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800190 if (stopwatch_expired(&sw)) {
191 printk(BIOS_ERR, "HECI: timeout, buffer not drained\n");
192 return 0;
193 }
194 }
195 return 1;
196}
197
198static int wait_read_slots(size_t cnt)
199{
200 struct stopwatch sw;
201
Subrata Banik03aef282021-09-28 18:10:24 +0530202 stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800203 while (cse_filled_slots() < cnt) {
Subrata Banik03aef282021-09-28 18:10:24 +0530204 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800205 if (stopwatch_expired(&sw)) {
206 printk(BIOS_ERR, "HECI: timed out reading answer!\n");
207 return 0;
208 }
209 }
210 return 1;
211}
212
213/* get number of full 4-byte slots */
214static size_t bytes_to_slots(size_t bytes)
215{
216 return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE;
217}
218
219static int cse_ready(void)
220{
221 uint32_t csr;
222 csr = read_cse_csr();
223 return csr & CSR_READY;
224}
225
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530226static bool cse_check_hfs1_com(int mode)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530227{
228 union me_hfsts1 hfs1;
229 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530230 return hfs1.fields.operation_mode == mode;
231}
232
233bool cse_is_hfs1_cws_normal(void)
234{
235 union me_hfsts1 hfs1;
236 hfs1.data = me_read_config32(PCI_ME_HFSTS1);
237 if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL)
238 return true;
239 return false;
240}
241
242bool cse_is_hfs1_com_normal(void)
243{
244 return cse_check_hfs1_com(ME_HFS1_COM_NORMAL);
245}
246
247bool cse_is_hfs1_com_secover_mei_msg(void)
248{
249 return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG);
250}
251
252bool cse_is_hfs1_com_soft_temp_disable(void)
253{
254 return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE);
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530255}
256
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530257bool cse_is_hfs3_fw_sku_lite(void)
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530258{
259 union me_hfsts3 hfs3;
260 hfs3.data = me_read_config32(PCI_ME_HFSTS3);
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530261 return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_LITE;
Sridhar Siricilla3465d272020-02-06 15:31:04 +0530262}
263
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530264/* Makes the host ready to communicate with CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530265void cse_set_host_ready(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530266{
267 uint32_t csr;
268 csr = read_host_csr();
269 csr &= ~CSR_RESET;
270 csr |= (CSR_IG | CSR_READY);
271 write_host_csr(csr);
272}
273
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530274/* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */
275uint8_t cse_wait_sec_override_mode(void)
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530276{
277 struct stopwatch sw;
Subrata Banik03aef282021-09-28 18:10:24 +0530278 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Sridhar Siricilla8e465452019-09-23 20:59:38 +0530279 while (!cse_is_hfs1_com_secover_mei_msg()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530280 udelay(HECI_DELAY_US);
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530281 if (stopwatch_expired(&sw)) {
282 printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n");
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530283 return 0;
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530284 }
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530285 }
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530286 printk(BIOS_DEBUG, "HECI: CSE took %lu ms to enter security override mode\n",
287 stopwatch_duration_msecs(&sw));
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530288 return 1;
289}
290
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530291/*
292 * Polls for CSE's current operation mode 'Soft Temporary Disable'.
293 * The CSE enters the current operation mode when it boots from RO(BP1).
294 */
295uint8_t cse_wait_com_soft_temp_disable(void)
296{
297 struct stopwatch sw;
Subrata Banikf5765812021-09-30 13:37:10 +0530298 stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO_MS);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530299 while (!cse_is_hfs1_com_soft_temp_disable()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530300 udelay(HECI_DELAY_US);
Sridhar Siricilla09ea3712019-11-12 23:35:50 +0530301 if (stopwatch_expired(&sw)) {
302 printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n");
303 return 0;
304 }
305 }
306 printk(BIOS_SPEW, "HECI: CSE took %lu ms to boot from RO\n",
307 stopwatch_duration_msecs(&sw));
308 return 1;
309}
310
Andrey Petrov04a72c42017-03-01 15:51:57 -0800311static int wait_heci_ready(void)
312{
313 struct stopwatch sw;
314
Subrata Banik03aef282021-09-28 18:10:24 +0530315 stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800316 while (!cse_ready()) {
Subrata Banik03aef282021-09-28 18:10:24 +0530317 udelay(HECI_DELAY_US);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800318 if (stopwatch_expired(&sw))
319 return 0;
320 }
321
322 return 1;
323}
324
325static void host_gen_interrupt(void)
326{
327 uint32_t csr;
328 csr = read_host_csr();
329 csr |= CSR_IG;
330 write_host_csr(csr);
331}
332
333static size_t hdr_get_length(uint32_t hdr)
334{
335 return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START;
336}
337
338static int
339send_one_message(uint32_t hdr, const void *buff)
340{
341 size_t pend_len, pend_slots, remainder, i;
342 uint32_t tmp;
343 const uint32_t *p = buff;
344
345 /* Get space for the header */
346 if (!wait_write_slots(1))
347 return 0;
348
349 /* First, write header */
350 write_slot(hdr);
351
352 pend_len = hdr_get_length(hdr);
353 pend_slots = bytes_to_slots(pend_len);
354
355 if (!wait_write_slots(pend_slots))
356 return 0;
357
358 /* Write the body in whole slots */
359 i = 0;
360 while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) {
361 write_slot(*p++);
362 i += SLOT_SIZE;
363 }
364
365 remainder = pend_len % SLOT_SIZE;
366 /* Pad to 4 bytes not touching caller's buffer */
367 if (remainder) {
368 memcpy(&tmp, p, remainder);
369 write_slot(tmp);
370 }
371
372 host_gen_interrupt();
373
374 /* Make sure nothing bad happened during transmission */
375 if (!cse_ready())
376 return 0;
377
378 return pend_len;
379}
380
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530381/*
382 * Send message msg of size len to host from host_addr to cse_addr.
383 * Returns 1 on success and 0 otherwise.
384 * In case of error heci_reset() may be required.
385 */
386static int
Andrey Petrov04a72c42017-03-01 15:51:57 -0800387heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr)
388{
Subrata Banik5c08c732017-11-13 14:54:37 +0530389 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800390 uint32_t csr, hdr;
Subrata Banik5c08c732017-11-13 14:54:37 +0530391 size_t sent, remaining, cb_size, max_length;
392 const uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800393
394 if (!msg || !len)
395 return 0;
396
397 clear_int();
398
Subrata Banik5c08c732017-11-13 14:54:37 +0530399 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
400 p = msg;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800401
Subrata Banik5c08c732017-11-13 14:54:37 +0530402 if (!wait_heci_ready()) {
403 printk(BIOS_ERR, "HECI: not ready\n");
404 continue;
405 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800406
Subrata Banik4a722f52017-11-13 14:56:42 +0530407 csr = read_host_csr();
Subrata Banik5c08c732017-11-13 14:54:37 +0530408 cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE;
409 /*
410 * Reserve one slot for the header. Limit max message
411 * length by 9 bits that are available in the header.
412 */
413 max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1)
414 - SLOT_SIZE;
415 remaining = len;
416
417 /*
418 * Fragment the message into smaller messages not exceeding
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100419 * useful circular buffer length. Mark last message complete.
Subrata Banik5c08c732017-11-13 14:54:37 +0530420 */
421 do {
422 hdr = MIN(max_length, remaining)
423 << MEI_HDR_LENGTH_START;
424 hdr |= client_addr << MEI_HDR_CSE_ADDR_START;
425 hdr |= host_addr << MEI_HDR_HOST_ADDR_START;
426 hdr |= (MIN(max_length, remaining) == remaining) ?
Lee Leahy68ab0b52017-03-10 13:42:34 -0800427 MEI_HDR_IS_COMPLETE : 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530428 sent = send_one_message(hdr, p);
429 p += sent;
430 remaining -= sent;
431 } while (remaining > 0 && sent != 0);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800432
Subrata Banik5c08c732017-11-13 14:54:37 +0530433 if (!remaining)
434 return 1;
435 }
436 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800437}
438
439static size_t
440recv_one_message(uint32_t *hdr, void *buff, size_t maxlen)
441{
442 uint32_t reg, *p = buff;
443 size_t recv_slots, recv_len, remainder, i;
444
445 /* first get the header */
446 if (!wait_read_slots(1))
447 return 0;
448
449 *hdr = read_slot();
450 recv_len = hdr_get_length(*hdr);
451
452 if (!recv_len)
453 printk(BIOS_WARNING, "HECI: message is zero-sized\n");
454
455 recv_slots = bytes_to_slots(recv_len);
456
457 i = 0;
458 if (recv_len > maxlen) {
459 printk(BIOS_ERR, "HECI: response is too big\n");
460 return 0;
461 }
462
463 /* wait for the rest of messages to arrive */
464 wait_read_slots(recv_slots);
465
466 /* fetch whole slots first */
467 while (i < ALIGN_DOWN(recv_len, SLOT_SIZE)) {
468 *p++ = read_slot();
469 i += SLOT_SIZE;
470 }
471
Subrata Banik5c08c732017-11-13 14:54:37 +0530472 /*
473 * If ME is not ready, something went wrong and
474 * we received junk
475 */
476 if (!cse_ready())
477 return 0;
478
Andrey Petrov04a72c42017-03-01 15:51:57 -0800479 remainder = recv_len % SLOT_SIZE;
480
481 if (remainder) {
482 reg = read_slot();
483 memcpy(p, &reg, remainder);
484 }
485
486 return recv_len;
487}
488
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530489/*
490 * Receive message into buff not exceeding maxlen. Message is considered
491 * successfully received if a 'complete' indication is read from ME side
492 * and there was enough space in the buffer to fit that message. maxlen
493 * is updated with size of message that was received. Returns 0 on failure
494 * and 1 on success.
495 * In case of error heci_reset() may be required.
496 */
497static int heci_receive(void *buff, size_t *maxlen)
Andrey Petrov04a72c42017-03-01 15:51:57 -0800498{
Subrata Banik5c08c732017-11-13 14:54:37 +0530499 uint8_t retry;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800500 size_t left, received;
501 uint32_t hdr = 0;
Subrata Banik5c08c732017-11-13 14:54:37 +0530502 uint8_t *p;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800503
504 if (!buff || !maxlen || !*maxlen)
505 return 0;
506
Andrey Petrov04a72c42017-03-01 15:51:57 -0800507 clear_int();
508
Subrata Banik5c08c732017-11-13 14:54:37 +0530509 for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) {
510 p = buff;
511 left = *maxlen;
512
513 if (!wait_heci_ready()) {
514 printk(BIOS_ERR, "HECI: not ready\n");
515 continue;
516 }
517
518 /*
519 * Receive multiple packets until we meet one marked
520 * complete or we run out of space in caller-provided buffer.
521 */
522 do {
523 received = recv_one_message(&hdr, p, left);
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800524 if (!received) {
Elyes HAOUAS3d450002018-08-09 18:55:58 +0200525 printk(BIOS_ERR, "HECI: Failed to receive!\n");
Lijian Zhaoc50296d2017-12-15 19:10:18 -0800526 return 0;
527 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530528 left -= received;
529 p += received;
530 /* If we read out everything ping to send more */
531 if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots())
532 host_gen_interrupt();
533 } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0);
534
535 if ((hdr & MEI_HDR_IS_COMPLETE) && received) {
536 *maxlen = p - (uint8_t *) buff;
537 return 1;
538 }
Andrey Petrov04a72c42017-03-01 15:51:57 -0800539 }
Subrata Banik5c08c732017-11-13 14:54:37 +0530540 return 0;
Andrey Petrov04a72c42017-03-01 15:51:57 -0800541}
542
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530543int heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg, size_t *rcv_sz,
544 uint8_t cse_addr)
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530545{
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530546 if (!heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, cse_addr)) {
Sridhar Siricillaa5208f52019-08-30 17:10:24 +0530547 printk(BIOS_ERR, "HECI: send Failed\n");
548 return 0;
549 }
550
551 if (rcv_msg != NULL) {
552 if (!heci_receive(rcv_msg, rcv_sz)) {
553 printk(BIOS_ERR, "HECI: receive Failed\n");
554 return 0;
555 }
556 }
557 return 1;
558}
559
Andrey Petrov04a72c42017-03-01 15:51:57 -0800560/*
561 * Attempt to reset the device. This is useful when host and ME are out
562 * of sync during transmission or ME didn't understand the message.
563 */
564int heci_reset(void)
565{
566 uint32_t csr;
567
Duncan Laurie15ca9032020-11-05 10:09:07 -0800568 /* Clear post code to prevent eventlog entry from unknown code. */
569 post_code(0);
570
Andrey Petrov04a72c42017-03-01 15:51:57 -0800571 /* Send reset request */
572 csr = read_host_csr();
Sridhar Siricillab9d075b2019-08-31 11:38:33 +0530573 csr |= (CSR_RESET | CSR_IG);
Andrey Petrov04a72c42017-03-01 15:51:57 -0800574 write_host_csr(csr);
575
576 if (wait_heci_ready()) {
577 /* Device is back on its imaginary feet, clear reset */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530578 cse_set_host_ready();
Andrey Petrov04a72c42017-03-01 15:51:57 -0800579 return 1;
580 }
581
582 printk(BIOS_CRIT, "HECI: reset failed\n");
583
584 return 0;
585}
586
Subrata Banik3710e992021-09-30 16:59:09 +0530587bool is_cse_devfn_visible(unsigned int devfn)
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530588{
Subrata Banik3710e992021-09-30 16:59:09 +0530589 int slot = PCI_SLOT(devfn);
590 int func = PCI_FUNC(devfn);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530591
Subrata Banik3710e992021-09-30 16:59:09 +0530592 if (!is_devfn_enabled(devfn)) {
593 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is disabled\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530594 return false;
595 }
596
Subrata Banik3710e992021-09-30 16:59:09 +0530597 if (pci_read_config16(PCI_DEV(0, slot, func), PCI_VENDOR_ID) == 0xFFFF) {
598 printk(BIOS_WARNING, "HECI: CSE device %02x.%01x is hidden\n", slot, func);
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530599 return false;
600 }
601
602 return true;
603}
604
Subrata Banik3710e992021-09-30 16:59:09 +0530605bool is_cse_enabled(void)
606{
607 return is_cse_devfn_visible(PCH_DEVFN_CSE);
608}
609
Sridhar Siricilla2cc66912019-08-31 11:20:34 +0530610uint32_t me_read_config32(int offset)
611{
612 return pci_read_config32(PCH_DEV_CSE, offset);
613}
614
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530615static bool cse_is_global_reset_allowed(void)
616{
617 /*
618 * Allow sending GLOBAL_RESET command only if:
619 * - CSE's current working state is Normal and current operation mode is Normal.
620 * - (or) CSE's current working state is normal and current operation mode can
621 * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530622 * Lite.
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530623 */
624 if (!cse_is_hfs1_cws_normal())
625 return false;
626
627 if (cse_is_hfs1_com_normal())
628 return true;
629
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530630 if (cse_is_hfs3_fw_sku_lite()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530631 if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg())
632 return true;
633 }
634 return false;
635}
636
Sridhar Siricillad415c202019-08-31 14:54:57 +0530637/*
Subrata Banikf463dc02020-09-14 19:04:03 +0530638 * Sends GLOBAL_RESET_REQ cmd to CSE with reset type GLOBAL_RESET.
639 * Returns 0 on failure and 1 on success.
Sridhar Siricillad415c202019-08-31 14:54:57 +0530640 */
Subrata Banikf463dc02020-09-14 19:04:03 +0530641static int cse_request_reset(enum rst_req_type rst_type)
Sridhar Siricillad415c202019-08-31 14:54:57 +0530642{
643 int status;
644 struct mkhi_hdr reply;
645 struct reset_message {
646 struct mkhi_hdr hdr;
647 uint8_t req_origin;
648 uint8_t reset_type;
649 } __packed;
650 struct reset_message msg = {
651 .hdr = {
652 .group_id = MKHI_GROUP_ID_CBM,
Sridhar Siricillae202e672020-01-07 23:36:40 +0530653 .command = MKHI_CBM_GLOBAL_RESET_REQ,
Sridhar Siricillad415c202019-08-31 14:54:57 +0530654 },
655 .req_origin = GR_ORIGIN_BIOS_POST,
656 .reset_type = rst_type
657 };
658 size_t reply_size;
659
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530660 printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type);
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530661
Sridhar Siricillac2a2d2b2020-02-27 17:16:13 +0530662 if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) {
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530663 printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n");
664 return 0;
665 }
Sridhar Siricillad415c202019-08-31 14:54:57 +0530666
Subrata Banikf463dc02020-09-14 19:04:03 +0530667 if (!cse_is_global_reset_allowed() || !is_cse_enabled()) {
Sridhar Siricilla59c7cb7d2020-02-07 11:59:30 +0530668 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
669 return 0;
670 }
671
Sridhar Siricillad415c202019-08-31 14:54:57 +0530672 heci_reset();
673
674 reply_size = sizeof(reply);
675 memset(&reply, 0, reply_size);
676
Sridhar Siricillad415c202019-08-31 14:54:57 +0530677 if (rst_type == CSE_RESET_ONLY)
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530678 status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530679 else
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530680 status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size,
681 HECI_MKHI_ADDR);
Sridhar Siricillad415c202019-08-31 14:54:57 +0530682
Sridhar Siricillaf2eb6872019-12-05 19:54:16 +0530683 printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", status ? "success" : "failure");
684 return status;
Sridhar Siricillad415c202019-08-31 14:54:57 +0530685}
686
Subrata Banikf463dc02020-09-14 19:04:03 +0530687int cse_request_global_reset(void)
688{
689 return cse_request_reset(GLOBAL_RESET);
690}
691
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530692static bool cse_is_hmrfpo_enable_allowed(void)
693{
694 /*
695 * Allow sending HMRFPO ENABLE command only if:
696 * - CSE's current working state is Normal and current operation mode is Normal
697 * - (or) cse's current working state is normal and current operation mode is
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530698 * Soft Temp Disable if CSE's Firmware SKU is Lite
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530699 */
700 if (!cse_is_hfs1_cws_normal())
701 return false;
702
703 if (cse_is_hfs1_com_normal())
704 return true;
705
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530706 if (cse_is_hfs3_fw_sku_lite() && cse_is_hfs1_com_soft_temp_disable())
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530707 return true;
708
709 return false;
710}
711
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530712/* Sends HMRFPO Enable command to CSE */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530713int cse_hmrfpo_enable(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530714{
715 struct hmrfpo_enable_msg {
716 struct mkhi_hdr hdr;
717 uint32_t nonce[2];
718 } __packed;
719
720 /* HMRFPO Enable message */
721 struct hmrfpo_enable_msg msg = {
722 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530723 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530724 .command = MKHI_HMRFPO_ENABLE,
725 },
726 .nonce = {0},
727 };
728
729 /* HMRFPO Enable response */
730 struct hmrfpo_enable_resp {
731 struct mkhi_hdr hdr;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530732 /* Base addr for factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530733 uint32_t fct_base;
Sridhar Siricillae202e672020-01-07 23:36:40 +0530734 /* Length of factory data area, not relevant for client SKUs */
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530735 uint32_t fct_limit;
736 uint8_t status;
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530737 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530738 } __packed;
739
740 struct hmrfpo_enable_resp resp;
741 size_t resp_size = sizeof(struct hmrfpo_enable_resp);
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530742
743 printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n");
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530744
745 if (!cse_is_hmrfpo_enable_allowed()) {
746 printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n");
747 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530748 }
749
750 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530751 &resp, &resp_size, HECI_MKHI_ADDR))
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530752 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530753
754 if (resp.hdr.result) {
755 printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result);
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530756 return 0;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530757 }
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530758
Sridhar Siricillad16187e2019-11-27 16:02:47 +0530759 if (resp.status) {
760 printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status);
761 return 0;
762 }
763
764 return 1;
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530765}
766
767/*
768 * Sends HMRFPO Get Status command to CSE to get the HMRFPO status.
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530769 * The status can be DISABLED/LOCKED/ENABLED
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530770 */
Sridhar Siricillaff072e62019-11-27 14:55:16 +0530771int cse_hmrfpo_get_status(void)
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530772{
773 struct hmrfpo_get_status_msg {
774 struct mkhi_hdr hdr;
775 } __packed;
776
777 struct hmrfpo_get_status_resp {
778 struct mkhi_hdr hdr;
779 uint8_t status;
Sridhar Siricilla63be9182020-01-19 12:38:56 +0530780 uint8_t reserved[3];
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530781 } __packed;
782
783 struct hmrfpo_get_status_msg msg = {
784 .hdr = {
Sridhar Siricillae202e672020-01-07 23:36:40 +0530785 .group_id = MKHI_GROUP_ID_HMRFPO,
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530786 .command = MKHI_HMRFPO_GET_STATUS,
787 },
788 };
789 struct hmrfpo_get_status_resp resp;
790 size_t resp_size = sizeof(struct hmrfpo_get_status_resp);
791
792 printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n");
793
Sridhar Siricilla206905c2020-02-06 18:48:22 +0530794 if (!cse_is_hfs1_cws_normal()) {
795 printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n");
796 return -1;
797 }
798
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530799 if (!heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg),
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530800 &resp, &resp_size, HECI_MKHI_ADDR)) {
Sridhar Siricillae30a0e62019-08-31 16:12:21 +0530801 printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n");
802 return -1;
803 }
804
805 if (resp.hdr.result) {
806 printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n",
807 resp.hdr.result);
808 return -1;
809 }
810
811 return resp.status;
812}
813
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530814void print_me_fw_version(void *unused)
815{
Johnny Lin72e76672021-10-09 12:35:35 +0800816 struct me_fw_ver_resp resp = {0};
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530817
818 /* Ignore if UART debugging is disabled */
819 if (!CONFIG(CONSOLE_SERIAL))
820 return;
821
Johnny Lin72e76672021-10-09 12:35:35 +0800822 if (get_me_fw_version(&resp) == CB_SUCCESS) {
823 printk(BIOS_DEBUG, "ME: Version: %d.%d.%d.%d\n", resp.code.major,
824 resp.code.minor, resp.code.hotfix, resp.code.build);
825 return;
826 }
827 printk(BIOS_DEBUG, "ME: Version: Unavailable\n");
828}
829
830enum cb_err get_me_fw_version(struct me_fw_ver_resp *resp)
831{
832 const struct mkhi_hdr fw_ver_msg = {
833 .group_id = MKHI_GROUP_ID_GEN,
834 .command = MKHI_GEN_GET_FW_VERSION,
835 };
836
837 if (resp == NULL) {
838 printk(BIOS_ERR, "%s failed, null pointer parameter\n", __func__);
839 return CB_ERR;
840 }
841 size_t resp_size = sizeof(*resp);
842
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200843 /* Ignore if CSE is disabled */
844 if (!is_cse_enabled())
Johnny Lin72e76672021-10-09 12:35:35 +0800845 return CB_ERR;
Wim Vervoorn8602fb72020-03-30 12:17:54 +0200846
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530847 /*
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530848 * Ignore if ME Firmware SKU type is Lite since
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530849 * print_boot_partition_info() logs RO(BP1) and RW(BP2) versions.
850 */
Sridhar Siricilla99dbca32020-05-12 21:05:04 +0530851 if (cse_is_hfs3_fw_sku_lite())
Johnny Lin72e76672021-10-09 12:35:35 +0800852 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530853
854 /*
855 * Prerequisites:
856 * 1) HFSTS1 Current Working State is Normal
857 * 2) HFSTS1 Current Operation Mode is Normal
858 * 3) It's after DRAM INIT DONE message (taken care of by calling it
859 * during ramstage
860 */
861 if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal())
Johnny Lin72e76672021-10-09 12:35:35 +0800862 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530863
864 heci_reset();
865
Johnny Lin72e76672021-10-09 12:35:35 +0800866 if (!heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), resp, &resp_size,
Rizwan Qureshi957857d2021-08-30 16:43:57 +0530867 HECI_MKHI_ADDR))
Johnny Lin72e76672021-10-09 12:35:35 +0800868 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530869
Johnny Lin72e76672021-10-09 12:35:35 +0800870 if (resp->hdr.result)
871 return CB_ERR;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530872
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530873
Johnny Lin72e76672021-10-09 12:35:35 +0800874 return CB_SUCCESS;
Sridhar Siricilla24a974a2020-02-19 14:41:36 +0530875}
876
Tim Wawrzynczak09635f42021-06-18 10:08:47 -0600877void cse_trigger_vboot_recovery(enum csme_failure_reason reason)
878{
879 printk(BIOS_DEBUG, "cse: CSE status registers: HFSTS1: 0x%x, HFSTS2: 0x%x "
880 "HFSTS3: 0x%x\n", me_read_config32(PCI_ME_HFSTS1),
881 me_read_config32(PCI_ME_HFSTS2), me_read_config32(PCI_ME_HFSTS3));
882
883 if (CONFIG(VBOOT)) {
884 struct vb2_context *ctx = vboot_get_context();
885 if (ctx == NULL)
886 goto failure;
887 vb2api_fail(ctx, VB2_RECOVERY_INTEL_CSE_LITE_SKU, reason);
888 vboot_save_data(ctx);
889 vboot_reboot();
890 }
891failure:
892 die("cse: Failed to trigger recovery mode(recovery subcode:%d)\n", reason);
893}
894
Subrata Banikc6e25522021-09-30 18:14:09 +0530895static bool disable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530896{
897 struct stopwatch sw;
Subrata Banikc6e25522021-09-30 18:14:09 +0530898 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530899 dev_idle_ctrl &= ~CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530900 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530901
Subrata Banik03aef282021-09-28 18:10:24 +0530902 stopwatch_init_usecs_expire(&sw, HECI_CIP_TIMEOUT_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530903 do {
Subrata Banikc6e25522021-09-30 18:14:09 +0530904 dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530905 if ((dev_idle_ctrl & CSE_DEV_CIP) == CSE_DEV_CIP)
906 return true;
Subrata Banik03aef282021-09-28 18:10:24 +0530907 udelay(HECI_DELAY_US);
Subrata Banika219edb2021-09-25 15:02:37 +0530908 } while (!stopwatch_expired(&sw));
909
910 return false;
911}
912
Subrata Banikc6e25522021-09-30 18:14:09 +0530913static void enable_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530914{
Subrata Banikc6e25522021-09-30 18:14:09 +0530915 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530916 dev_idle_ctrl |= CSE_DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530917 write_bar(dev, MMIO_CSE_DEVIDLE, dev_idle_ctrl);
Subrata Banika219edb2021-09-25 15:02:37 +0530918}
919
Subrata Banikc6e25522021-09-30 18:14:09 +0530920enum cse_device_state get_cse_device_state(unsigned int devfn)
Subrata Banika219edb2021-09-25 15:02:37 +0530921{
Subrata Banikc6e25522021-09-30 18:14:09 +0530922 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
923 uint32_t dev_idle_ctrl = read_bar(dev, MMIO_CSE_DEVIDLE);
Subrata Banika219edb2021-09-25 15:02:37 +0530924 if ((dev_idle_ctrl & CSE_DEV_IDLE) == CSE_DEV_IDLE)
925 return DEV_IDLE;
926
927 return DEV_ACTIVE;
928}
929
Subrata Banikc6e25522021-09-30 18:14:09 +0530930static enum cse_device_state ensure_cse_active(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530931{
Subrata Banikc6e25522021-09-30 18:14:09 +0530932 if (!disable_cse_idle(dev))
Subrata Banika219edb2021-09-25 15:02:37 +0530933 return DEV_IDLE;
Subrata Banikc6e25522021-09-30 18:14:09 +0530934 pci_or_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Subrata Banika219edb2021-09-25 15:02:37 +0530935
936 return DEV_ACTIVE;
937}
938
Subrata Banikc6e25522021-09-30 18:14:09 +0530939static void ensure_cse_idle(pci_devfn_t dev)
Subrata Banika219edb2021-09-25 15:02:37 +0530940{
Subrata Banikc6e25522021-09-30 18:14:09 +0530941 enable_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +0530942
Subrata Banikc6e25522021-09-30 18:14:09 +0530943 pci_and_config32(dev, PCI_COMMAND, ~(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
Subrata Banika219edb2021-09-25 15:02:37 +0530944}
945
Subrata Banikc6e25522021-09-30 18:14:09 +0530946bool set_cse_device_state(unsigned int devfn, enum cse_device_state requested_state)
Subrata Banika219edb2021-09-25 15:02:37 +0530947{
Subrata Banikc6e25522021-09-30 18:14:09 +0530948 enum cse_device_state current_state = get_cse_device_state(devfn);
949 pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn));
Subrata Banika219edb2021-09-25 15:02:37 +0530950
951 if (current_state == requested_state)
952 return true;
953
954 if (requested_state == DEV_ACTIVE)
Subrata Banikc6e25522021-09-30 18:14:09 +0530955 return ensure_cse_active(dev) == requested_state;
Subrata Banika219edb2021-09-25 15:02:37 +0530956 else
Subrata Banikc6e25522021-09-30 18:14:09 +0530957 ensure_cse_idle(dev);
Subrata Banika219edb2021-09-25 15:02:37 +0530958
959 return true;
960}
961
Andrey Petrov04a72c42017-03-01 15:51:57 -0800962#if ENV_RAMSTAGE
963
Andrey Petrov04a72c42017-03-01 15:51:57 -0800964static struct device_operations cse_ops = {
Subrata Banik38abbda2021-09-30 13:15:50 +0530965 .set_resources = pci_dev_set_resources,
Andrey Petrov04a72c42017-03-01 15:51:57 -0800966 .read_resources = pci_dev_read_resources,
967 .enable_resources = pci_dev_enable_resources,
968 .init = pci_dev_init,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530969 .ops_pci = &pci_dev_ops_pci,
Andrey Petrov04a72c42017-03-01 15:51:57 -0800970};
971
Hannah Williams63142152017-06-12 14:03:18 -0700972static const unsigned short pci_device_ids[] = {
973 PCI_DEVICE_ID_INTEL_APL_CSE0,
974 PCI_DEVICE_ID_INTEL_GLK_CSE0,
Andrey Petrov0405de92017-06-05 13:25:29 -0700975 PCI_DEVICE_ID_INTEL_CNL_CSE0,
Subrata Banikd0586d22017-11-27 13:28:41 +0530976 PCI_DEVICE_ID_INTEL_SKL_CSE0,
Maxim Polyakov571d07d2019-08-22 13:11:32 +0300977 PCI_DEVICE_ID_INTEL_LWB_CSE0,
978 PCI_DEVICE_ID_INTEL_LWB_CSE0_SUPER,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800979 PCI_DEVICE_ID_INTEL_CNP_H_CSE0,
Aamir Bohra9eac0392018-06-30 12:07:04 +0530980 PCI_DEVICE_ID_INTEL_ICL_CSE0,
Ronak Kanabarda7ffb482019-02-05 01:51:13 +0530981 PCI_DEVICE_ID_INTEL_CMP_CSE0,
Gaggery Tsai12a651c2019-12-05 11:23:20 -0800982 PCI_DEVICE_ID_INTEL_CMP_H_CSE0,
Ravi Sarawadi6b5bf402019-10-21 22:25:04 -0700983 PCI_DEVICE_ID_INTEL_TGL_CSE0,
Jeremy Soller191a8d72021-08-10 14:06:51 -0600984 PCI_DEVICE_ID_INTEL_TGL_H_CSE0,
Tan, Lean Sheng26136092020-01-20 19:13:56 -0800985 PCI_DEVICE_ID_INTEL_MCC_CSE0,
986 PCI_DEVICE_ID_INTEL_MCC_CSE1,
987 PCI_DEVICE_ID_INTEL_MCC_CSE2,
988 PCI_DEVICE_ID_INTEL_MCC_CSE3,
Meera Ravindranath3f4af0d2020-02-12 16:01:22 +0530989 PCI_DEVICE_ID_INTEL_JSP_CSE0,
990 PCI_DEVICE_ID_INTEL_JSP_CSE1,
991 PCI_DEVICE_ID_INTEL_JSP_CSE2,
992 PCI_DEVICE_ID_INTEL_JSP_CSE3,
Subrata Banikf672f7f2020-08-03 14:29:25 +0530993 PCI_DEVICE_ID_INTEL_ADP_P_CSE0,
994 PCI_DEVICE_ID_INTEL_ADP_P_CSE1,
995 PCI_DEVICE_ID_INTEL_ADP_P_CSE2,
996 PCI_DEVICE_ID_INTEL_ADP_P_CSE3,
997 PCI_DEVICE_ID_INTEL_ADP_S_CSE0,
998 PCI_DEVICE_ID_INTEL_ADP_S_CSE1,
999 PCI_DEVICE_ID_INTEL_ADP_S_CSE2,
1000 PCI_DEVICE_ID_INTEL_ADP_S_CSE3,
Varshit Pandyaf4d98fdd22021-01-17 18:39:29 +05301001 PCI_DEVICE_ID_INTEL_ADP_M_CSE0,
1002 PCI_DEVICE_ID_INTEL_ADP_M_CSE1,
1003 PCI_DEVICE_ID_INTEL_ADP_M_CSE2,
1004 PCI_DEVICE_ID_INTEL_ADP_M_CSE3,
Hannah Williams63142152017-06-12 14:03:18 -07001005 0,
1006};
1007
Andrey Petrov04a72c42017-03-01 15:51:57 -08001008static const struct pci_driver cse_driver __pci_driver = {
1009 .ops = &cse_ops,
1010 .vendor = PCI_VENDOR_ID_INTEL,
1011 /* SoC/chipset needs to provide PCI device ID */
Andrey Petrov0405de92017-06-05 13:25:29 -07001012 .devices = pci_device_ids
Andrey Petrov04a72c42017-03-01 15:51:57 -08001013};
1014
1015#endif