Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 2 | |
Subrata Banik | 05e06cd | 2017-11-09 15:04:09 +0530 | [diff] [blame] | 3 | #include <assert.h> |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
| 5 | #include <console/console.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 6 | #include <device/mmio.h> |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 7 | #include <delay.h> |
| 8 | #include <device/pci.h> |
| 9 | #include <device/pci_ids.h> |
| 10 | #include <device/pci_ops.h> |
| 11 | #include <intelblocks/cse.h> |
Tim Wawrzynczak | 09635f4 | 2021-06-18 10:08:47 -0600 | [diff] [blame] | 12 | #include <security/vboot/misc.h> |
| 13 | #include <security/vboot/vboot_common.h> |
Subrata Banik | 05e06cd | 2017-11-09 15:04:09 +0530 | [diff] [blame] | 14 | #include <soc/iomap.h> |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 15 | #include <soc/pci_devs.h> |
Sridhar Siricilla | 8e46545 | 2019-09-23 20:59:38 +0530 | [diff] [blame] | 16 | #include <soc/me.h> |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <timer.h> |
| 19 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 20 | #define MAX_HECI_MESSAGE_RETRY_COUNT 5 |
| 21 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 22 | /* Wait up to 15 sec for HECI to get ready */ |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 23 | #define HECI_DELAY_READY_MS (15 * 1000) |
Jonathan Neuschäfer | 5268b76 | 2018-02-12 12:24:25 +0100 | [diff] [blame] | 24 | /* Wait up to 100 usec between circular buffer polls */ |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 25 | #define HECI_DELAY_US 100 |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 26 | /* Wait up to 5 sec for CSE to chew something we sent */ |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 27 | #define HECI_SEND_TIMEOUT_MS (5 * 1000) |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 28 | /* Wait up to 5 sec for CSE to blurp a reply */ |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 29 | #define HECI_READ_TIMEOUT_MS (5 * 1000) |
Subrata Banik | a219edb | 2021-09-25 15:02:37 +0530 | [diff] [blame] | 30 | /* Wait up to 1 ms for CSE CIP */ |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 31 | #define HECI_CIP_TIMEOUT_US 1000 |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 32 | |
| 33 | #define SLOT_SIZE sizeof(uint32_t) |
| 34 | |
| 35 | #define MMIO_CSE_CB_WW 0x00 |
| 36 | #define MMIO_HOST_CSR 0x04 |
| 37 | #define MMIO_CSE_CB_RW 0x08 |
| 38 | #define MMIO_CSE_CSR 0x0c |
Subrata Banik | a219edb | 2021-09-25 15:02:37 +0530 | [diff] [blame] | 39 | #define MMIO_CSE_DEVIDLE 0x800 |
| 40 | #define CSE_DEV_IDLE (1 << 2) |
| 41 | #define CSE_DEV_CIP (1 << 0) |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 42 | |
| 43 | #define CSR_IE (1 << 0) |
| 44 | #define CSR_IS (1 << 1) |
| 45 | #define CSR_IG (1 << 2) |
| 46 | #define CSR_READY (1 << 3) |
| 47 | #define CSR_RESET (1 << 4) |
| 48 | #define CSR_RP_START 8 |
| 49 | #define CSR_RP (((1 << 8) - 1) << CSR_RP_START) |
| 50 | #define CSR_WP_START 16 |
| 51 | #define CSR_WP (((1 << 8) - 1) << CSR_WP_START) |
| 52 | #define CSR_CBD_START 24 |
| 53 | #define CSR_CBD (((1 << 8) - 1) << CSR_CBD_START) |
| 54 | |
| 55 | #define MEI_HDR_IS_COMPLETE (1 << 31) |
| 56 | #define MEI_HDR_LENGTH_START 16 |
| 57 | #define MEI_HDR_LENGTH_SIZE 9 |
| 58 | #define MEI_HDR_LENGTH (((1 << MEI_HDR_LENGTH_SIZE) - 1) \ |
| 59 | << MEI_HDR_LENGTH_START) |
| 60 | #define MEI_HDR_HOST_ADDR_START 8 |
| 61 | #define MEI_HDR_HOST_ADDR (((1 << 8) - 1) << MEI_HDR_HOST_ADDR_START) |
| 62 | #define MEI_HDR_CSE_ADDR_START 0 |
| 63 | #define MEI_HDR_CSE_ADDR (((1 << 8) - 1) << MEI_HDR_CSE_ADDR_START) |
| 64 | |
Sridhar Siricilla | 09ea371 | 2019-11-12 23:35:50 +0530 | [diff] [blame] | 65 | /* Wait up to 5 seconds for CSE to boot from RO(BP1) */ |
| 66 | #define CSE_DELAY_BOOT_TO_RO (5 * 1000) |
| 67 | |
Arthur Heymans | 3d6ccd0 | 2019-05-27 17:25:23 +0200 | [diff] [blame] | 68 | static struct cse_device { |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 69 | uintptr_t sec_bar; |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 70 | } cse; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Initialize the device with provided temporary BAR. If BAR is 0 use a |
| 74 | * default. This is intended for pre-mem usage only where BARs haven't been |
| 75 | * assigned yet and devices are not enabled. |
| 76 | */ |
| 77 | void heci_init(uintptr_t tempbar) |
| 78 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 79 | #if defined(__SIMPLE_DEVICE__) |
| 80 | pci_devfn_t dev = PCH_DEV_CSE; |
| 81 | #else |
| 82 | struct device *dev = PCH_DEV_CSE; |
| 83 | #endif |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 84 | u16 pcireg; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 85 | |
| 86 | /* Assume it is already initialized, nothing else to do */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 87 | if (cse.sec_bar) |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 88 | return; |
| 89 | |
| 90 | /* Use default pre-ram bar */ |
| 91 | if (!tempbar) |
| 92 | tempbar = HECI1_BASE_ADDRESS; |
| 93 | |
| 94 | /* Assign Resources to HECI1 */ |
| 95 | /* Clear BIT 1-2 of Command Register */ |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 96 | pcireg = pci_read_config16(dev, PCI_COMMAND); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 97 | pcireg &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 98 | pci_write_config16(dev, PCI_COMMAND, pcireg); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 99 | |
| 100 | /* Program Temporary BAR for HECI1 */ |
| 101 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, tempbar); |
| 102 | pci_write_config32(dev, PCI_BASE_ADDRESS_1, 0x0); |
| 103 | |
| 104 | /* Enable Bus Master and MMIO Space */ |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 105 | pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 106 | |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 107 | cse.sec_bar = tempbar; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Subrata Banik | 05e06cd | 2017-11-09 15:04:09 +0530 | [diff] [blame] | 110 | /* Get HECI BAR 0 from PCI configuration space */ |
| 111 | static uint32_t get_cse_bar(void) |
| 112 | { |
| 113 | uintptr_t bar; |
| 114 | |
| 115 | bar = pci_read_config32(PCH_DEV_CSE, PCI_BASE_ADDRESS_0); |
| 116 | assert(bar != 0); |
| 117 | /* |
| 118 | * Bits 31-12 are the base address as per EDS for SPI, |
| 119 | * Don't care about 0-11 bit |
| 120 | */ |
| 121 | return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK; |
| 122 | } |
| 123 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 124 | static uint32_t read_bar(uint32_t offset) |
| 125 | { |
Patrick Georgi | 08c8cf9 | 2019-12-02 11:43:20 +0100 | [diff] [blame] | 126 | /* Load and cache BAR */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 127 | if (!cse.sec_bar) |
| 128 | cse.sec_bar = get_cse_bar(); |
| 129 | return read32((void *)(cse.sec_bar + offset)); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void write_bar(uint32_t offset, uint32_t val) |
| 133 | { |
Patrick Georgi | 08c8cf9 | 2019-12-02 11:43:20 +0100 | [diff] [blame] | 134 | /* Load and cache BAR */ |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 135 | if (!cse.sec_bar) |
| 136 | cse.sec_bar = get_cse_bar(); |
| 137 | return write32((void *)(cse.sec_bar + offset), val); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static uint32_t read_cse_csr(void) |
| 141 | { |
| 142 | return read_bar(MMIO_CSE_CSR); |
| 143 | } |
| 144 | |
| 145 | static uint32_t read_host_csr(void) |
| 146 | { |
| 147 | return read_bar(MMIO_HOST_CSR); |
| 148 | } |
| 149 | |
| 150 | static void write_host_csr(uint32_t data) |
| 151 | { |
| 152 | write_bar(MMIO_HOST_CSR, data); |
| 153 | } |
| 154 | |
| 155 | static size_t filled_slots(uint32_t data) |
| 156 | { |
| 157 | uint8_t wp, rp; |
| 158 | rp = data >> CSR_RP_START; |
| 159 | wp = data >> CSR_WP_START; |
| 160 | return (uint8_t) (wp - rp); |
| 161 | } |
| 162 | |
| 163 | static size_t cse_filled_slots(void) |
| 164 | { |
| 165 | return filled_slots(read_cse_csr()); |
| 166 | } |
| 167 | |
| 168 | static size_t host_empty_slots(void) |
| 169 | { |
| 170 | uint32_t csr; |
| 171 | csr = read_host_csr(); |
| 172 | |
| 173 | return ((csr & CSR_CBD) >> CSR_CBD_START) - filled_slots(csr); |
| 174 | } |
| 175 | |
| 176 | static void clear_int(void) |
| 177 | { |
| 178 | uint32_t csr; |
| 179 | csr = read_host_csr(); |
| 180 | csr |= CSR_IS; |
| 181 | write_host_csr(csr); |
| 182 | } |
| 183 | |
| 184 | static uint32_t read_slot(void) |
| 185 | { |
| 186 | return read_bar(MMIO_CSE_CB_RW); |
| 187 | } |
| 188 | |
| 189 | static void write_slot(uint32_t val) |
| 190 | { |
| 191 | write_bar(MMIO_CSE_CB_WW, val); |
| 192 | } |
| 193 | |
| 194 | static int wait_write_slots(size_t cnt) |
| 195 | { |
| 196 | struct stopwatch sw; |
| 197 | |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 198 | stopwatch_init_msecs_expire(&sw, HECI_SEND_TIMEOUT_MS); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 199 | while (host_empty_slots() < cnt) { |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 200 | udelay(HECI_DELAY_US); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 201 | if (stopwatch_expired(&sw)) { |
| 202 | printk(BIOS_ERR, "HECI: timeout, buffer not drained\n"); |
| 203 | return 0; |
| 204 | } |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static int wait_read_slots(size_t cnt) |
| 210 | { |
| 211 | struct stopwatch sw; |
| 212 | |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 213 | stopwatch_init_msecs_expire(&sw, HECI_READ_TIMEOUT_MS); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 214 | while (cse_filled_slots() < cnt) { |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 215 | udelay(HECI_DELAY_US); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 216 | if (stopwatch_expired(&sw)) { |
| 217 | printk(BIOS_ERR, "HECI: timed out reading answer!\n"); |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | /* get number of full 4-byte slots */ |
| 225 | static size_t bytes_to_slots(size_t bytes) |
| 226 | { |
| 227 | return ALIGN_UP(bytes, SLOT_SIZE) / SLOT_SIZE; |
| 228 | } |
| 229 | |
| 230 | static int cse_ready(void) |
| 231 | { |
| 232 | uint32_t csr; |
| 233 | csr = read_cse_csr(); |
| 234 | return csr & CSR_READY; |
| 235 | } |
| 236 | |
Sridhar Siricilla | 8e46545 | 2019-09-23 20:59:38 +0530 | [diff] [blame] | 237 | static bool cse_check_hfs1_com(int mode) |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 238 | { |
| 239 | union me_hfsts1 hfs1; |
| 240 | hfs1.data = me_read_config32(PCI_ME_HFSTS1); |
Sridhar Siricilla | 8e46545 | 2019-09-23 20:59:38 +0530 | [diff] [blame] | 241 | return hfs1.fields.operation_mode == mode; |
| 242 | } |
| 243 | |
| 244 | bool cse_is_hfs1_cws_normal(void) |
| 245 | { |
| 246 | union me_hfsts1 hfs1; |
| 247 | hfs1.data = me_read_config32(PCI_ME_HFSTS1); |
| 248 | if (hfs1.fields.working_state == ME_HFS1_CWS_NORMAL) |
| 249 | return true; |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | bool cse_is_hfs1_com_normal(void) |
| 254 | { |
| 255 | return cse_check_hfs1_com(ME_HFS1_COM_NORMAL); |
| 256 | } |
| 257 | |
| 258 | bool cse_is_hfs1_com_secover_mei_msg(void) |
| 259 | { |
| 260 | return cse_check_hfs1_com(ME_HFS1_COM_SECOVER_MEI_MSG); |
| 261 | } |
| 262 | |
| 263 | bool cse_is_hfs1_com_soft_temp_disable(void) |
| 264 | { |
| 265 | return cse_check_hfs1_com(ME_HFS1_COM_SOFT_TEMP_DISABLE); |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 266 | } |
| 267 | |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 268 | bool cse_is_hfs3_fw_sku_lite(void) |
Sridhar Siricilla | 3465d27 | 2020-02-06 15:31:04 +0530 | [diff] [blame] | 269 | { |
| 270 | union me_hfsts3 hfs3; |
| 271 | hfs3.data = me_read_config32(PCI_ME_HFSTS3); |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 272 | return hfs3.fields.fw_sku == ME_HFS3_FW_SKU_LITE; |
Sridhar Siricilla | 3465d27 | 2020-02-06 15:31:04 +0530 | [diff] [blame] | 273 | } |
| 274 | |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 275 | /* Makes the host ready to communicate with CSE */ |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 276 | void cse_set_host_ready(void) |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 277 | { |
| 278 | uint32_t csr; |
| 279 | csr = read_host_csr(); |
| 280 | csr &= ~CSR_RESET; |
| 281 | csr |= (CSR_IG | CSR_READY); |
| 282 | write_host_csr(csr); |
| 283 | } |
| 284 | |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 285 | /* Polls for ME mode ME_HFS1_COM_SECOVER_MEI_MSG for 15 seconds */ |
| 286 | uint8_t cse_wait_sec_override_mode(void) |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 287 | { |
| 288 | struct stopwatch sw; |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 289 | stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS); |
Sridhar Siricilla | 8e46545 | 2019-09-23 20:59:38 +0530 | [diff] [blame] | 290 | while (!cse_is_hfs1_com_secover_mei_msg()) { |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 291 | udelay(HECI_DELAY_US); |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 292 | if (stopwatch_expired(&sw)) { |
| 293 | printk(BIOS_ERR, "HECI: Timed out waiting for SEC_OVERRIDE mode!\n"); |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 294 | return 0; |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 295 | } |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 296 | } |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 297 | printk(BIOS_DEBUG, "HECI: CSE took %lu ms to enter security override mode\n", |
| 298 | stopwatch_duration_msecs(&sw)); |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
Sridhar Siricilla | 09ea371 | 2019-11-12 23:35:50 +0530 | [diff] [blame] | 302 | /* |
| 303 | * Polls for CSE's current operation mode 'Soft Temporary Disable'. |
| 304 | * The CSE enters the current operation mode when it boots from RO(BP1). |
| 305 | */ |
| 306 | uint8_t cse_wait_com_soft_temp_disable(void) |
| 307 | { |
| 308 | struct stopwatch sw; |
| 309 | stopwatch_init_msecs_expire(&sw, CSE_DELAY_BOOT_TO_RO); |
| 310 | while (!cse_is_hfs1_com_soft_temp_disable()) { |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 311 | udelay(HECI_DELAY_US); |
Sridhar Siricilla | 09ea371 | 2019-11-12 23:35:50 +0530 | [diff] [blame] | 312 | if (stopwatch_expired(&sw)) { |
| 313 | printk(BIOS_ERR, "HECI: Timed out waiting for CSE to boot from RO!\n"); |
| 314 | return 0; |
| 315 | } |
| 316 | } |
| 317 | printk(BIOS_SPEW, "HECI: CSE took %lu ms to boot from RO\n", |
| 318 | stopwatch_duration_msecs(&sw)); |
| 319 | return 1; |
| 320 | } |
| 321 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 322 | static int wait_heci_ready(void) |
| 323 | { |
| 324 | struct stopwatch sw; |
| 325 | |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 326 | stopwatch_init_msecs_expire(&sw, HECI_DELAY_READY_MS); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 327 | while (!cse_ready()) { |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 328 | udelay(HECI_DELAY_US); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 329 | if (stopwatch_expired(&sw)) |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | static void host_gen_interrupt(void) |
| 337 | { |
| 338 | uint32_t csr; |
| 339 | csr = read_host_csr(); |
| 340 | csr |= CSR_IG; |
| 341 | write_host_csr(csr); |
| 342 | } |
| 343 | |
| 344 | static size_t hdr_get_length(uint32_t hdr) |
| 345 | { |
| 346 | return (hdr & MEI_HDR_LENGTH) >> MEI_HDR_LENGTH_START; |
| 347 | } |
| 348 | |
| 349 | static int |
| 350 | send_one_message(uint32_t hdr, const void *buff) |
| 351 | { |
| 352 | size_t pend_len, pend_slots, remainder, i; |
| 353 | uint32_t tmp; |
| 354 | const uint32_t *p = buff; |
| 355 | |
| 356 | /* Get space for the header */ |
| 357 | if (!wait_write_slots(1)) |
| 358 | return 0; |
| 359 | |
| 360 | /* First, write header */ |
| 361 | write_slot(hdr); |
| 362 | |
| 363 | pend_len = hdr_get_length(hdr); |
| 364 | pend_slots = bytes_to_slots(pend_len); |
| 365 | |
| 366 | if (!wait_write_slots(pend_slots)) |
| 367 | return 0; |
| 368 | |
| 369 | /* Write the body in whole slots */ |
| 370 | i = 0; |
| 371 | while (i < ALIGN_DOWN(pend_len, SLOT_SIZE)) { |
| 372 | write_slot(*p++); |
| 373 | i += SLOT_SIZE; |
| 374 | } |
| 375 | |
| 376 | remainder = pend_len % SLOT_SIZE; |
| 377 | /* Pad to 4 bytes not touching caller's buffer */ |
| 378 | if (remainder) { |
| 379 | memcpy(&tmp, p, remainder); |
| 380 | write_slot(tmp); |
| 381 | } |
| 382 | |
| 383 | host_gen_interrupt(); |
| 384 | |
| 385 | /* Make sure nothing bad happened during transmission */ |
| 386 | if (!cse_ready()) |
| 387 | return 0; |
| 388 | |
| 389 | return pend_len; |
| 390 | } |
| 391 | |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 392 | /* |
| 393 | * Send message msg of size len to host from host_addr to cse_addr. |
| 394 | * Returns 1 on success and 0 otherwise. |
| 395 | * In case of error heci_reset() may be required. |
| 396 | */ |
| 397 | static int |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 398 | heci_send(const void *msg, size_t len, uint8_t host_addr, uint8_t client_addr) |
| 399 | { |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 400 | uint8_t retry; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 401 | uint32_t csr, hdr; |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 402 | size_t sent, remaining, cb_size, max_length; |
| 403 | const uint8_t *p; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 404 | |
| 405 | if (!msg || !len) |
| 406 | return 0; |
| 407 | |
| 408 | clear_int(); |
| 409 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 410 | for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) { |
| 411 | p = msg; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 412 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 413 | if (!wait_heci_ready()) { |
| 414 | printk(BIOS_ERR, "HECI: not ready\n"); |
| 415 | continue; |
| 416 | } |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 417 | |
Subrata Banik | 4a722f5 | 2017-11-13 14:56:42 +0530 | [diff] [blame] | 418 | csr = read_host_csr(); |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 419 | cb_size = ((csr & CSR_CBD) >> CSR_CBD_START) * SLOT_SIZE; |
| 420 | /* |
| 421 | * Reserve one slot for the header. Limit max message |
| 422 | * length by 9 bits that are available in the header. |
| 423 | */ |
| 424 | max_length = MIN(cb_size, (1 << MEI_HDR_LENGTH_SIZE) - 1) |
| 425 | - SLOT_SIZE; |
| 426 | remaining = len; |
| 427 | |
| 428 | /* |
| 429 | * Fragment the message into smaller messages not exceeding |
Jonathan Neuschäfer | 5268b76 | 2018-02-12 12:24:25 +0100 | [diff] [blame] | 430 | * useful circular buffer length. Mark last message complete. |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 431 | */ |
| 432 | do { |
| 433 | hdr = MIN(max_length, remaining) |
| 434 | << MEI_HDR_LENGTH_START; |
| 435 | hdr |= client_addr << MEI_HDR_CSE_ADDR_START; |
| 436 | hdr |= host_addr << MEI_HDR_HOST_ADDR_START; |
| 437 | hdr |= (MIN(max_length, remaining) == remaining) ? |
Lee Leahy | 68ab0b5 | 2017-03-10 13:42:34 -0800 | [diff] [blame] | 438 | MEI_HDR_IS_COMPLETE : 0; |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 439 | sent = send_one_message(hdr, p); |
| 440 | p += sent; |
| 441 | remaining -= sent; |
| 442 | } while (remaining > 0 && sent != 0); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 443 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 444 | if (!remaining) |
| 445 | return 1; |
| 446 | } |
| 447 | return 0; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static size_t |
| 451 | recv_one_message(uint32_t *hdr, void *buff, size_t maxlen) |
| 452 | { |
| 453 | uint32_t reg, *p = buff; |
| 454 | size_t recv_slots, recv_len, remainder, i; |
| 455 | |
| 456 | /* first get the header */ |
| 457 | if (!wait_read_slots(1)) |
| 458 | return 0; |
| 459 | |
| 460 | *hdr = read_slot(); |
| 461 | recv_len = hdr_get_length(*hdr); |
| 462 | |
| 463 | if (!recv_len) |
| 464 | printk(BIOS_WARNING, "HECI: message is zero-sized\n"); |
| 465 | |
| 466 | recv_slots = bytes_to_slots(recv_len); |
| 467 | |
| 468 | i = 0; |
| 469 | if (recv_len > maxlen) { |
| 470 | printk(BIOS_ERR, "HECI: response is too big\n"); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* wait for the rest of messages to arrive */ |
| 475 | wait_read_slots(recv_slots); |
| 476 | |
| 477 | /* fetch whole slots first */ |
| 478 | while (i < ALIGN_DOWN(recv_len, SLOT_SIZE)) { |
| 479 | *p++ = read_slot(); |
| 480 | i += SLOT_SIZE; |
| 481 | } |
| 482 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 483 | /* |
| 484 | * If ME is not ready, something went wrong and |
| 485 | * we received junk |
| 486 | */ |
| 487 | if (!cse_ready()) |
| 488 | return 0; |
| 489 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 490 | remainder = recv_len % SLOT_SIZE; |
| 491 | |
| 492 | if (remainder) { |
| 493 | reg = read_slot(); |
| 494 | memcpy(p, ®, remainder); |
| 495 | } |
| 496 | |
| 497 | return recv_len; |
| 498 | } |
| 499 | |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 500 | /* |
| 501 | * Receive message into buff not exceeding maxlen. Message is considered |
| 502 | * successfully received if a 'complete' indication is read from ME side |
| 503 | * and there was enough space in the buffer to fit that message. maxlen |
| 504 | * is updated with size of message that was received. Returns 0 on failure |
| 505 | * and 1 on success. |
| 506 | * In case of error heci_reset() may be required. |
| 507 | */ |
| 508 | static int heci_receive(void *buff, size_t *maxlen) |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 509 | { |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 510 | uint8_t retry; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 511 | size_t left, received; |
| 512 | uint32_t hdr = 0; |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 513 | uint8_t *p; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 514 | |
| 515 | if (!buff || !maxlen || !*maxlen) |
| 516 | return 0; |
| 517 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 518 | clear_int(); |
| 519 | |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 520 | for (retry = 0; retry < MAX_HECI_MESSAGE_RETRY_COUNT; retry++) { |
| 521 | p = buff; |
| 522 | left = *maxlen; |
| 523 | |
| 524 | if (!wait_heci_ready()) { |
| 525 | printk(BIOS_ERR, "HECI: not ready\n"); |
| 526 | continue; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Receive multiple packets until we meet one marked |
| 531 | * complete or we run out of space in caller-provided buffer. |
| 532 | */ |
| 533 | do { |
| 534 | received = recv_one_message(&hdr, p, left); |
Lijian Zhao | c50296d | 2017-12-15 19:10:18 -0800 | [diff] [blame] | 535 | if (!received) { |
Elyes HAOUAS | 3d45000 | 2018-08-09 18:55:58 +0200 | [diff] [blame] | 536 | printk(BIOS_ERR, "HECI: Failed to receive!\n"); |
Lijian Zhao | c50296d | 2017-12-15 19:10:18 -0800 | [diff] [blame] | 537 | return 0; |
| 538 | } |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 539 | left -= received; |
| 540 | p += received; |
| 541 | /* If we read out everything ping to send more */ |
| 542 | if (!(hdr & MEI_HDR_IS_COMPLETE) && !cse_filled_slots()) |
| 543 | host_gen_interrupt(); |
| 544 | } while (received && !(hdr & MEI_HDR_IS_COMPLETE) && left > 0); |
| 545 | |
| 546 | if ((hdr & MEI_HDR_IS_COMPLETE) && received) { |
| 547 | *maxlen = p - (uint8_t *) buff; |
| 548 | return 1; |
| 549 | } |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 550 | } |
Subrata Banik | 5c08c73 | 2017-11-13 14:54:37 +0530 | [diff] [blame] | 551 | return 0; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 554 | int heci_send_receive(const void *snd_msg, size_t snd_sz, void *rcv_msg, size_t *rcv_sz, |
| 555 | uint8_t cse_addr) |
Sridhar Siricilla | a5208f5 | 2019-08-30 17:10:24 +0530 | [diff] [blame] | 556 | { |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 557 | if (!heci_send(snd_msg, snd_sz, BIOS_HOST_ADDR, cse_addr)) { |
Sridhar Siricilla | a5208f5 | 2019-08-30 17:10:24 +0530 | [diff] [blame] | 558 | printk(BIOS_ERR, "HECI: send Failed\n"); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | if (rcv_msg != NULL) { |
| 563 | if (!heci_receive(rcv_msg, rcv_sz)) { |
| 564 | printk(BIOS_ERR, "HECI: receive Failed\n"); |
| 565 | return 0; |
| 566 | } |
| 567 | } |
| 568 | return 1; |
| 569 | } |
| 570 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 571 | /* |
| 572 | * Attempt to reset the device. This is useful when host and ME are out |
| 573 | * of sync during transmission or ME didn't understand the message. |
| 574 | */ |
| 575 | int heci_reset(void) |
| 576 | { |
| 577 | uint32_t csr; |
| 578 | |
Duncan Laurie | 15ca903 | 2020-11-05 10:09:07 -0800 | [diff] [blame] | 579 | /* Clear post code to prevent eventlog entry from unknown code. */ |
| 580 | post_code(0); |
| 581 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 582 | /* Send reset request */ |
| 583 | csr = read_host_csr(); |
Sridhar Siricilla | b9d075b | 2019-08-31 11:38:33 +0530 | [diff] [blame] | 584 | csr |= (CSR_RESET | CSR_IG); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 585 | write_host_csr(csr); |
| 586 | |
| 587 | if (wait_heci_ready()) { |
| 588 | /* Device is back on its imaginary feet, clear reset */ |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 589 | cse_set_host_ready(); |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | |
| 593 | printk(BIOS_CRIT, "HECI: reset failed\n"); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Sridhar Siricilla | 2cc6691 | 2019-08-31 11:20:34 +0530 | [diff] [blame] | 598 | bool is_cse_enabled(void) |
| 599 | { |
| 600 | const struct device *cse_dev = pcidev_path_on_root(PCH_DEVFN_CSE); |
| 601 | |
| 602 | if (!cse_dev || !cse_dev->enabled) { |
| 603 | printk(BIOS_WARNING, "HECI: No CSE device\n"); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | if (pci_read_config16(PCH_DEV_CSE, PCI_VENDOR_ID) == 0xFFFF) { |
| 608 | printk(BIOS_WARNING, "HECI: CSE device is hidden\n"); |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | uint32_t me_read_config32(int offset) |
| 616 | { |
| 617 | return pci_read_config32(PCH_DEV_CSE, offset); |
| 618 | } |
| 619 | |
Sridhar Siricilla | 59c7cb7d | 2020-02-07 11:59:30 +0530 | [diff] [blame] | 620 | static bool cse_is_global_reset_allowed(void) |
| 621 | { |
| 622 | /* |
| 623 | * Allow sending GLOBAL_RESET command only if: |
| 624 | * - CSE's current working state is Normal and current operation mode is Normal. |
| 625 | * - (or) CSE's current working state is normal and current operation mode can |
| 626 | * be Soft Temp Disable or Security Override Mode if CSE's Firmware SKU is |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 627 | * Lite. |
Sridhar Siricilla | 59c7cb7d | 2020-02-07 11:59:30 +0530 | [diff] [blame] | 628 | */ |
| 629 | if (!cse_is_hfs1_cws_normal()) |
| 630 | return false; |
| 631 | |
| 632 | if (cse_is_hfs1_com_normal()) |
| 633 | return true; |
| 634 | |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 635 | if (cse_is_hfs3_fw_sku_lite()) { |
Sridhar Siricilla | 59c7cb7d | 2020-02-07 11:59:30 +0530 | [diff] [blame] | 636 | if (cse_is_hfs1_com_soft_temp_disable() || cse_is_hfs1_com_secover_mei_msg()) |
| 637 | return true; |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 642 | /* |
Subrata Banik | f463dc0 | 2020-09-14 19:04:03 +0530 | [diff] [blame] | 643 | * Sends GLOBAL_RESET_REQ cmd to CSE with reset type GLOBAL_RESET. |
| 644 | * Returns 0 on failure and 1 on success. |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 645 | */ |
Subrata Banik | f463dc0 | 2020-09-14 19:04:03 +0530 | [diff] [blame] | 646 | static int cse_request_reset(enum rst_req_type rst_type) |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 647 | { |
| 648 | int status; |
| 649 | struct mkhi_hdr reply; |
| 650 | struct reset_message { |
| 651 | struct mkhi_hdr hdr; |
| 652 | uint8_t req_origin; |
| 653 | uint8_t reset_type; |
| 654 | } __packed; |
| 655 | struct reset_message msg = { |
| 656 | .hdr = { |
| 657 | .group_id = MKHI_GROUP_ID_CBM, |
Sridhar Siricilla | e202e67 | 2020-01-07 23:36:40 +0530 | [diff] [blame] | 658 | .command = MKHI_CBM_GLOBAL_RESET_REQ, |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 659 | }, |
| 660 | .req_origin = GR_ORIGIN_BIOS_POST, |
| 661 | .reset_type = rst_type |
| 662 | }; |
| 663 | size_t reply_size; |
| 664 | |
Sridhar Siricilla | f2eb687 | 2019-12-05 19:54:16 +0530 | [diff] [blame] | 665 | printk(BIOS_DEBUG, "HECI: Global Reset(Type:%d) Command\n", rst_type); |
Sridhar Siricilla | 59c7cb7d | 2020-02-07 11:59:30 +0530 | [diff] [blame] | 666 | |
Sridhar Siricilla | c2a2d2b | 2020-02-27 17:16:13 +0530 | [diff] [blame] | 667 | if (!(rst_type == GLOBAL_RESET || rst_type == CSE_RESET_ONLY)) { |
Sridhar Siricilla | f2eb687 | 2019-12-05 19:54:16 +0530 | [diff] [blame] | 668 | printk(BIOS_ERR, "HECI: Unsupported reset type is requested\n"); |
| 669 | return 0; |
| 670 | } |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 671 | |
Subrata Banik | f463dc0 | 2020-09-14 19:04:03 +0530 | [diff] [blame] | 672 | if (!cse_is_global_reset_allowed() || !is_cse_enabled()) { |
Sridhar Siricilla | 59c7cb7d | 2020-02-07 11:59:30 +0530 | [diff] [blame] | 673 | printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n"); |
| 674 | return 0; |
| 675 | } |
| 676 | |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 677 | heci_reset(); |
| 678 | |
| 679 | reply_size = sizeof(reply); |
| 680 | memset(&reply, 0, reply_size); |
| 681 | |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 682 | if (rst_type == CSE_RESET_ONLY) |
Sridhar Siricilla | f2eb687 | 2019-12-05 19:54:16 +0530 | [diff] [blame] | 683 | status = heci_send(&msg, sizeof(msg), BIOS_HOST_ADDR, HECI_MKHI_ADDR); |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 684 | else |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 685 | status = heci_send_receive(&msg, sizeof(msg), &reply, &reply_size, |
| 686 | HECI_MKHI_ADDR); |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 687 | |
Sridhar Siricilla | f2eb687 | 2019-12-05 19:54:16 +0530 | [diff] [blame] | 688 | printk(BIOS_DEBUG, "HECI: Global Reset %s!\n", status ? "success" : "failure"); |
| 689 | return status; |
Sridhar Siricilla | d415c20 | 2019-08-31 14:54:57 +0530 | [diff] [blame] | 690 | } |
| 691 | |
Subrata Banik | f463dc0 | 2020-09-14 19:04:03 +0530 | [diff] [blame] | 692 | int cse_request_global_reset(void) |
| 693 | { |
| 694 | return cse_request_reset(GLOBAL_RESET); |
| 695 | } |
| 696 | |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 697 | static bool cse_is_hmrfpo_enable_allowed(void) |
| 698 | { |
| 699 | /* |
| 700 | * Allow sending HMRFPO ENABLE command only if: |
| 701 | * - CSE's current working state is Normal and current operation mode is Normal |
| 702 | * - (or) cse's current working state is normal and current operation mode is |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 703 | * Soft Temp Disable if CSE's Firmware SKU is Lite |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 704 | */ |
| 705 | if (!cse_is_hfs1_cws_normal()) |
| 706 | return false; |
| 707 | |
| 708 | if (cse_is_hfs1_com_normal()) |
| 709 | return true; |
| 710 | |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 711 | if (cse_is_hfs3_fw_sku_lite() && cse_is_hfs1_com_soft_temp_disable()) |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 712 | return true; |
| 713 | |
| 714 | return false; |
| 715 | } |
| 716 | |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 717 | /* Sends HMRFPO Enable command to CSE */ |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 718 | int cse_hmrfpo_enable(void) |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 719 | { |
| 720 | struct hmrfpo_enable_msg { |
| 721 | struct mkhi_hdr hdr; |
| 722 | uint32_t nonce[2]; |
| 723 | } __packed; |
| 724 | |
| 725 | /* HMRFPO Enable message */ |
| 726 | struct hmrfpo_enable_msg msg = { |
| 727 | .hdr = { |
Sridhar Siricilla | e202e67 | 2020-01-07 23:36:40 +0530 | [diff] [blame] | 728 | .group_id = MKHI_GROUP_ID_HMRFPO, |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 729 | .command = MKHI_HMRFPO_ENABLE, |
| 730 | }, |
| 731 | .nonce = {0}, |
| 732 | }; |
| 733 | |
| 734 | /* HMRFPO Enable response */ |
| 735 | struct hmrfpo_enable_resp { |
| 736 | struct mkhi_hdr hdr; |
Sridhar Siricilla | e202e67 | 2020-01-07 23:36:40 +0530 | [diff] [blame] | 737 | /* Base addr for factory data area, not relevant for client SKUs */ |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 738 | uint32_t fct_base; |
Sridhar Siricilla | e202e67 | 2020-01-07 23:36:40 +0530 | [diff] [blame] | 739 | /* Length of factory data area, not relevant for client SKUs */ |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 740 | uint32_t fct_limit; |
| 741 | uint8_t status; |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 742 | uint8_t reserved[3]; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 743 | } __packed; |
| 744 | |
| 745 | struct hmrfpo_enable_resp resp; |
| 746 | size_t resp_size = sizeof(struct hmrfpo_enable_resp); |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 747 | |
| 748 | printk(BIOS_DEBUG, "HECI: Send HMRFPO Enable Command\n"); |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 749 | |
| 750 | if (!cse_is_hmrfpo_enable_allowed()) { |
| 751 | printk(BIOS_ERR, "HECI: CSE does not meet required prerequisites\n"); |
| 752 | return 0; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | if (!heci_send_receive(&msg, sizeof(struct hmrfpo_enable_msg), |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 756 | &resp, &resp_size, HECI_MKHI_ADDR)) |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 757 | return 0; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 758 | |
| 759 | if (resp.hdr.result) { |
| 760 | printk(BIOS_ERR, "HECI: Resp Failed:%d\n", resp.hdr.result); |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 761 | return 0; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 762 | } |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 763 | |
Sridhar Siricilla | d16187e | 2019-11-27 16:02:47 +0530 | [diff] [blame] | 764 | if (resp.status) { |
| 765 | printk(BIOS_ERR, "HECI: HMRFPO_Enable Failed (resp status: %d)\n", resp.status); |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | return 1; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Sends HMRFPO Get Status command to CSE to get the HMRFPO status. |
Sridhar Siricilla | 63be918 | 2020-01-19 12:38:56 +0530 | [diff] [blame] | 774 | * The status can be DISABLED/LOCKED/ENABLED |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 775 | */ |
Sridhar Siricilla | ff072e6 | 2019-11-27 14:55:16 +0530 | [diff] [blame] | 776 | int cse_hmrfpo_get_status(void) |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 777 | { |
| 778 | struct hmrfpo_get_status_msg { |
| 779 | struct mkhi_hdr hdr; |
| 780 | } __packed; |
| 781 | |
| 782 | struct hmrfpo_get_status_resp { |
| 783 | struct mkhi_hdr hdr; |
| 784 | uint8_t status; |
Sridhar Siricilla | 63be918 | 2020-01-19 12:38:56 +0530 | [diff] [blame] | 785 | uint8_t reserved[3]; |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 786 | } __packed; |
| 787 | |
| 788 | struct hmrfpo_get_status_msg msg = { |
| 789 | .hdr = { |
Sridhar Siricilla | e202e67 | 2020-01-07 23:36:40 +0530 | [diff] [blame] | 790 | .group_id = MKHI_GROUP_ID_HMRFPO, |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 791 | .command = MKHI_HMRFPO_GET_STATUS, |
| 792 | }, |
| 793 | }; |
| 794 | struct hmrfpo_get_status_resp resp; |
| 795 | size_t resp_size = sizeof(struct hmrfpo_get_status_resp); |
| 796 | |
| 797 | printk(BIOS_INFO, "HECI: Sending Get HMRFPO Status Command\n"); |
| 798 | |
Sridhar Siricilla | 206905c | 2020-02-06 18:48:22 +0530 | [diff] [blame] | 799 | if (!cse_is_hfs1_cws_normal()) { |
| 800 | printk(BIOS_ERR, "HECI: CSE's current working state is not Normal\n"); |
| 801 | return -1; |
| 802 | } |
| 803 | |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 804 | if (!heci_send_receive(&msg, sizeof(struct hmrfpo_get_status_msg), |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 805 | &resp, &resp_size, HECI_MKHI_ADDR)) { |
Sridhar Siricilla | e30a0e6 | 2019-08-31 16:12:21 +0530 | [diff] [blame] | 806 | printk(BIOS_ERR, "HECI: HMRFPO send/receive fail\n"); |
| 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | if (resp.hdr.result) { |
| 811 | printk(BIOS_ERR, "HECI: HMRFPO Resp Failed:%d\n", |
| 812 | resp.hdr.result); |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | return resp.status; |
| 817 | } |
| 818 | |
Sridhar Siricilla | 24a974a | 2020-02-19 14:41:36 +0530 | [diff] [blame] | 819 | void print_me_fw_version(void *unused) |
| 820 | { |
| 821 | struct version { |
| 822 | uint16_t minor; |
| 823 | uint16_t major; |
| 824 | uint16_t build; |
| 825 | uint16_t hotfix; |
| 826 | } __packed; |
| 827 | |
| 828 | struct fw_ver_resp { |
| 829 | struct mkhi_hdr hdr; |
| 830 | struct version code; |
| 831 | struct version rec; |
| 832 | struct version fitc; |
| 833 | } __packed; |
| 834 | |
| 835 | const struct mkhi_hdr fw_ver_msg = { |
| 836 | .group_id = MKHI_GROUP_ID_GEN, |
| 837 | .command = MKHI_GEN_GET_FW_VERSION, |
| 838 | }; |
| 839 | |
| 840 | struct fw_ver_resp resp; |
| 841 | size_t resp_size = sizeof(resp); |
| 842 | |
| 843 | /* Ignore if UART debugging is disabled */ |
| 844 | if (!CONFIG(CONSOLE_SERIAL)) |
| 845 | return; |
| 846 | |
Wim Vervoorn | 8602fb7 | 2020-03-30 12:17:54 +0200 | [diff] [blame] | 847 | /* Ignore if CSE is disabled */ |
| 848 | if (!is_cse_enabled()) |
| 849 | return; |
| 850 | |
Sridhar Siricilla | 24a974a | 2020-02-19 14:41:36 +0530 | [diff] [blame] | 851 | /* |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 852 | * Ignore if ME Firmware SKU type is Lite since |
Sridhar Siricilla | 24a974a | 2020-02-19 14:41:36 +0530 | [diff] [blame] | 853 | * print_boot_partition_info() logs RO(BP1) and RW(BP2) versions. |
| 854 | */ |
Sridhar Siricilla | 99dbca3 | 2020-05-12 21:05:04 +0530 | [diff] [blame] | 855 | if (cse_is_hfs3_fw_sku_lite()) |
Sridhar Siricilla | 24a974a | 2020-02-19 14:41:36 +0530 | [diff] [blame] | 856 | return; |
| 857 | |
| 858 | /* |
| 859 | * Prerequisites: |
| 860 | * 1) HFSTS1 Current Working State is Normal |
| 861 | * 2) HFSTS1 Current Operation Mode is Normal |
| 862 | * 3) It's after DRAM INIT DONE message (taken care of by calling it |
| 863 | * during ramstage |
| 864 | */ |
| 865 | if (!cse_is_hfs1_cws_normal() || !cse_is_hfs1_com_normal()) |
| 866 | goto fail; |
| 867 | |
| 868 | heci_reset(); |
| 869 | |
Rizwan Qureshi | 957857d | 2021-08-30 16:43:57 +0530 | [diff] [blame] | 870 | if (!heci_send_receive(&fw_ver_msg, sizeof(fw_ver_msg), &resp, &resp_size, |
| 871 | HECI_MKHI_ADDR)) |
Sridhar Siricilla | 24a974a | 2020-02-19 14:41:36 +0530 | [diff] [blame] | 872 | goto fail; |
| 873 | |
| 874 | if (resp.hdr.result) |
| 875 | goto fail; |
| 876 | |
| 877 | printk(BIOS_DEBUG, "ME: Version: %d.%d.%d.%d\n", resp.code.major, |
| 878 | resp.code.minor, resp.code.hotfix, resp.code.build); |
| 879 | return; |
| 880 | |
| 881 | fail: |
| 882 | printk(BIOS_DEBUG, "ME: Version: Unavailable\n"); |
| 883 | } |
| 884 | |
Tim Wawrzynczak | 09635f4 | 2021-06-18 10:08:47 -0600 | [diff] [blame] | 885 | void cse_trigger_vboot_recovery(enum csme_failure_reason reason) |
| 886 | { |
| 887 | printk(BIOS_DEBUG, "cse: CSE status registers: HFSTS1: 0x%x, HFSTS2: 0x%x " |
| 888 | "HFSTS3: 0x%x\n", me_read_config32(PCI_ME_HFSTS1), |
| 889 | me_read_config32(PCI_ME_HFSTS2), me_read_config32(PCI_ME_HFSTS3)); |
| 890 | |
| 891 | if (CONFIG(VBOOT)) { |
| 892 | struct vb2_context *ctx = vboot_get_context(); |
| 893 | if (ctx == NULL) |
| 894 | goto failure; |
| 895 | vb2api_fail(ctx, VB2_RECOVERY_INTEL_CSE_LITE_SKU, reason); |
| 896 | vboot_save_data(ctx); |
| 897 | vboot_reboot(); |
| 898 | } |
| 899 | failure: |
| 900 | die("cse: Failed to trigger recovery mode(recovery subcode:%d)\n", reason); |
| 901 | } |
| 902 | |
Subrata Banik | a219edb | 2021-09-25 15:02:37 +0530 | [diff] [blame] | 903 | static bool disable_cse_idle(void) |
| 904 | { |
| 905 | struct stopwatch sw; |
| 906 | uint32_t dev_idle_ctrl = read_bar(MMIO_CSE_DEVIDLE); |
| 907 | dev_idle_ctrl &= ~CSE_DEV_IDLE; |
| 908 | write_bar(MMIO_CSE_DEVIDLE, dev_idle_ctrl); |
| 909 | |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 910 | stopwatch_init_usecs_expire(&sw, HECI_CIP_TIMEOUT_US); |
Subrata Banik | a219edb | 2021-09-25 15:02:37 +0530 | [diff] [blame] | 911 | do { |
| 912 | dev_idle_ctrl = read_bar(MMIO_CSE_DEVIDLE); |
| 913 | if ((dev_idle_ctrl & CSE_DEV_CIP) == CSE_DEV_CIP) |
| 914 | return true; |
Subrata Banik | 03aef28 | 2021-09-28 18:10:24 +0530 | [diff] [blame^] | 915 | udelay(HECI_DELAY_US); |
Subrata Banik | a219edb | 2021-09-25 15:02:37 +0530 | [diff] [blame] | 916 | } while (!stopwatch_expired(&sw)); |
| 917 | |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | static void enable_cse_idle(void) |
| 922 | { |
| 923 | uint32_t dev_idle_ctrl = read_bar(MMIO_CSE_DEVIDLE); |
| 924 | dev_idle_ctrl |= CSE_DEV_IDLE; |
| 925 | write_bar(MMIO_CSE_DEVIDLE, dev_idle_ctrl); |
| 926 | } |
| 927 | |
| 928 | enum cse_device_state get_cse_device_state(void) |
| 929 | { |
| 930 | uint32_t dev_idle_ctrl = read_bar(MMIO_CSE_DEVIDLE); |
| 931 | if ((dev_idle_ctrl & CSE_DEV_IDLE) == CSE_DEV_IDLE) |
| 932 | return DEV_IDLE; |
| 933 | |
| 934 | return DEV_ACTIVE; |
| 935 | } |
| 936 | |
| 937 | static enum cse_device_state ensure_cse_active(void) |
| 938 | { |
| 939 | if (!disable_cse_idle()) |
| 940 | return DEV_IDLE; |
| 941 | pci_or_config32(PCH_DEV_CSE, PCI_COMMAND, PCI_COMMAND_MEMORY | |
| 942 | PCI_COMMAND_MASTER); |
| 943 | |
| 944 | return DEV_ACTIVE; |
| 945 | } |
| 946 | |
| 947 | static void ensure_cse_idle(void) |
| 948 | { |
| 949 | enable_cse_idle(); |
| 950 | |
| 951 | pci_and_config32(PCH_DEV_CSE, PCI_COMMAND, ~(PCI_COMMAND_MEMORY | |
| 952 | PCI_COMMAND_MASTER)); |
| 953 | } |
| 954 | |
| 955 | bool set_cse_device_state(enum cse_device_state requested_state) |
| 956 | { |
| 957 | enum cse_device_state current_state = get_cse_device_state(); |
| 958 | |
| 959 | if (current_state == requested_state) |
| 960 | return true; |
| 961 | |
| 962 | if (requested_state == DEV_ACTIVE) |
| 963 | return ensure_cse_active() == requested_state; |
| 964 | else |
| 965 | ensure_cse_idle(); |
| 966 | |
| 967 | return true; |
| 968 | } |
| 969 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 970 | #if ENV_RAMSTAGE |
| 971 | |
| 972 | static void update_sec_bar(struct device *dev) |
| 973 | { |
Patrick Georgi | c9b1359 | 2019-11-29 11:47:47 +0100 | [diff] [blame] | 974 | cse.sec_bar = find_resource(dev, PCI_BASE_ADDRESS_0)->base; |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void cse_set_resources(struct device *dev) |
| 978 | { |
Subrata Banik | 2ee54db | 2017-03-05 12:37:00 +0530 | [diff] [blame] | 979 | if (dev->path.pci.devfn == PCH_DEVFN_CSE) |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 980 | update_sec_bar(dev); |
| 981 | |
| 982 | pci_dev_set_resources(dev); |
| 983 | } |
| 984 | |
| 985 | static struct device_operations cse_ops = { |
| 986 | .set_resources = cse_set_resources, |
| 987 | .read_resources = pci_dev_read_resources, |
| 988 | .enable_resources = pci_dev_enable_resources, |
| 989 | .init = pci_dev_init, |
Subrata Banik | 6bbc91a | 2017-12-07 14:55:51 +0530 | [diff] [blame] | 990 | .ops_pci = &pci_dev_ops_pci, |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 991 | }; |
| 992 | |
Hannah Williams | 6314215 | 2017-06-12 14:03:18 -0700 | [diff] [blame] | 993 | static const unsigned short pci_device_ids[] = { |
| 994 | PCI_DEVICE_ID_INTEL_APL_CSE0, |
| 995 | PCI_DEVICE_ID_INTEL_GLK_CSE0, |
Andrey Petrov | 0405de9 | 2017-06-05 13:25:29 -0700 | [diff] [blame] | 996 | PCI_DEVICE_ID_INTEL_CNL_CSE0, |
Subrata Banik | d0586d2 | 2017-11-27 13:28:41 +0530 | [diff] [blame] | 997 | PCI_DEVICE_ID_INTEL_SKL_CSE0, |
Maxim Polyakov | 571d07d | 2019-08-22 13:11:32 +0300 | [diff] [blame] | 998 | PCI_DEVICE_ID_INTEL_LWB_CSE0, |
| 999 | PCI_DEVICE_ID_INTEL_LWB_CSE0_SUPER, |
praveen hodagatta pranesh | e26c4a4 | 2018-09-20 03:49:45 +0800 | [diff] [blame] | 1000 | PCI_DEVICE_ID_INTEL_CNP_H_CSE0, |
Aamir Bohra | 9eac039 | 2018-06-30 12:07:04 +0530 | [diff] [blame] | 1001 | PCI_DEVICE_ID_INTEL_ICL_CSE0, |
Ronak Kanabar | da7ffb48 | 2019-02-05 01:51:13 +0530 | [diff] [blame] | 1002 | PCI_DEVICE_ID_INTEL_CMP_CSE0, |
Gaggery Tsai | 12a651c | 2019-12-05 11:23:20 -0800 | [diff] [blame] | 1003 | PCI_DEVICE_ID_INTEL_CMP_H_CSE0, |
Ravi Sarawadi | 6b5bf40 | 2019-10-21 22:25:04 -0700 | [diff] [blame] | 1004 | PCI_DEVICE_ID_INTEL_TGL_CSE0, |
Jeremy Soller | 191a8d7 | 2021-08-10 14:06:51 -0600 | [diff] [blame] | 1005 | PCI_DEVICE_ID_INTEL_TGL_H_CSE0, |
Tan, Lean Sheng | 2613609 | 2020-01-20 19:13:56 -0800 | [diff] [blame] | 1006 | PCI_DEVICE_ID_INTEL_MCC_CSE0, |
| 1007 | PCI_DEVICE_ID_INTEL_MCC_CSE1, |
| 1008 | PCI_DEVICE_ID_INTEL_MCC_CSE2, |
| 1009 | PCI_DEVICE_ID_INTEL_MCC_CSE3, |
Meera Ravindranath | 3f4af0d | 2020-02-12 16:01:22 +0530 | [diff] [blame] | 1010 | PCI_DEVICE_ID_INTEL_JSP_CSE0, |
| 1011 | PCI_DEVICE_ID_INTEL_JSP_CSE1, |
| 1012 | PCI_DEVICE_ID_INTEL_JSP_CSE2, |
| 1013 | PCI_DEVICE_ID_INTEL_JSP_CSE3, |
Subrata Banik | f672f7f | 2020-08-03 14:29:25 +0530 | [diff] [blame] | 1014 | PCI_DEVICE_ID_INTEL_ADP_P_CSE0, |
| 1015 | PCI_DEVICE_ID_INTEL_ADP_P_CSE1, |
| 1016 | PCI_DEVICE_ID_INTEL_ADP_P_CSE2, |
| 1017 | PCI_DEVICE_ID_INTEL_ADP_P_CSE3, |
| 1018 | PCI_DEVICE_ID_INTEL_ADP_S_CSE0, |
| 1019 | PCI_DEVICE_ID_INTEL_ADP_S_CSE1, |
| 1020 | PCI_DEVICE_ID_INTEL_ADP_S_CSE2, |
| 1021 | PCI_DEVICE_ID_INTEL_ADP_S_CSE3, |
Varshit Pandya | f4d98fdd2 | 2021-01-17 18:39:29 +0530 | [diff] [blame] | 1022 | PCI_DEVICE_ID_INTEL_ADP_M_CSE0, |
| 1023 | PCI_DEVICE_ID_INTEL_ADP_M_CSE1, |
| 1024 | PCI_DEVICE_ID_INTEL_ADP_M_CSE2, |
| 1025 | PCI_DEVICE_ID_INTEL_ADP_M_CSE3, |
Hannah Williams | 6314215 | 2017-06-12 14:03:18 -0700 | [diff] [blame] | 1026 | 0, |
| 1027 | }; |
| 1028 | |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 1029 | static const struct pci_driver cse_driver __pci_driver = { |
| 1030 | .ops = &cse_ops, |
| 1031 | .vendor = PCI_VENDOR_ID_INTEL, |
| 1032 | /* SoC/chipset needs to provide PCI device ID */ |
Andrey Petrov | 0405de9 | 2017-06-05 13:25:29 -0700 | [diff] [blame] | 1033 | .devices = pci_device_ids |
Andrey Petrov | 04a72c4 | 2017-03-01 15:51:57 -0800 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | #endif |