blob: 436553cd0f7f0b78b7a65a4d58ffaae53fa88748 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
3/*
4 * This is a ramstage driver for the Intel Management Engine found in the
5 * 6-series chipset. It handles the required boot-time messages over the
6 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
7 * finished with POST. Additional messages are defined for debug but are
8 * not used unless the console loglevel is high enough.
9 */
10
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020012#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020013#include <device/pci_ops.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020014#include <console/console.h>
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020015#include <device/device.h>
16#include <device/pci.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020017#include <device/pci_ids.h>
18#include <device/pci_def.h>
19#include <string.h>
20#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070021#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010022#include <halt.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020023
Stefan Reinauer8e073822012-04-04 00:07:22 +020024#include "me.h"
25#include "pch.h"
26
Julius Wernercd49cce2019-03-05 16:53:33 -080027#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020028#include <vendorcode/google/chromeos/gnvs.h>
29#endif
30
Stefan Reinauer8e073822012-04-04 00:07:22 +020031/* Path that the BIOS should take based on ME state */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020032static const char *me_bios_path_values[] __unused = {
Stefan Reinauer8e073822012-04-04 00:07:22 +020033 [ME_NORMAL_BIOS_PATH] = "Normal",
34 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
35 [ME_ERROR_BIOS_PATH] = "Error",
36 [ME_RECOVERY_BIOS_PATH] = "Recovery",
37 [ME_DISABLE_BIOS_PATH] = "Disable",
38 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
39};
Stefan Reinauer8e073822012-04-04 00:07:22 +020040
41/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080042static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020043
Stefan Reinauer8e073822012-04-04 00:07:22 +020044static void mei_dump(void *ptr, int dword, int offset, const char *type)
45{
46 struct mei_csr *csr;
47
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020048 if (!CONFIG(DEBUG_INTEL_ME))
49 return;
50
Stefan Reinauer8e073822012-04-04 00:07:22 +020051 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
52
53 switch (offset) {
54 case MEI_H_CSR:
55 case MEI_ME_CSR_HA:
56 csr = ptr;
57 if (!csr) {
58 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
59 break;
60 }
61 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
62 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
63 csr->buffer_read_ptr, csr->buffer_write_ptr,
64 csr->ready, csr->reset, csr->interrupt_generate,
65 csr->interrupt_status, csr->interrupt_enable);
66 break;
67 case MEI_ME_CB_RW:
68 case MEI_H_CB_WW:
69 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
70 break;
71 default:
72 printk(BIOS_SPEW, "0x%08x\n", offset);
73 break;
74 }
75}
Stefan Reinauer8e073822012-04-04 00:07:22 +020076
77/*
78 * ME/MEI access helpers using memcpy to avoid aliasing.
79 */
80
81static inline void mei_read_dword_ptr(void *ptr, int offset)
82{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080083 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +020084 memcpy(ptr, &dword, sizeof(dword));
85 mei_dump(ptr, dword, offset, "READ");
86}
87
88static inline void mei_write_dword_ptr(void *ptr, int offset)
89{
90 u32 dword = 0;
91 memcpy(&dword, ptr, sizeof(dword));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080092 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +020093 mei_dump(ptr, dword, offset, "WRITE");
94}
95
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020096#ifndef __SIMPLE_DEVICE__
Elyes HAOUASdc035282018-09-18 13:28:49 +020097static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Stefan Reinauer8e073822012-04-04 00:07:22 +020098{
99 u32 dword = pci_read_config32(dev, offset);
100 memcpy(ptr, &dword, sizeof(dword));
101 mei_dump(ptr, dword, offset, "PCI READ");
102}
103#endif
104
105static inline void read_host_csr(struct mei_csr *csr)
106{
107 mei_read_dword_ptr(csr, MEI_H_CSR);
108}
109
110static inline void write_host_csr(struct mei_csr *csr)
111{
112 mei_write_dword_ptr(csr, MEI_H_CSR);
113}
114
115static inline void read_me_csr(struct mei_csr *csr)
116{
117 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
118}
119
120static inline void write_cb(u32 dword)
121{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800122 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200123 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
124}
125
126static inline u32 read_cb(void)
127{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800128 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200129 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
130 return dword;
131}
132
133/* Wait for ME ready bit to be asserted */
134static int mei_wait_for_me_ready(void)
135{
136 struct mei_csr me;
Martin Rothff744bf2019-10-23 21:46:03 -0600137 unsigned int try = ME_RETRY;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200138
139 while (try--) {
140 read_me_csr(&me);
141 if (me.ready)
142 return 0;
143 udelay(ME_DELAY);
144 }
145
146 printk(BIOS_ERR, "ME: failed to become ready\n");
147 return -1;
148}
149
150static void mei_reset(void)
151{
152 struct mei_csr host;
153
154 if (mei_wait_for_me_ready() < 0)
155 return;
156
157 /* Reset host and ME circular buffers for next message */
158 read_host_csr(&host);
159 host.reset = 1;
160 host.interrupt_generate = 1;
161 write_host_csr(&host);
162
163 if (mei_wait_for_me_ready() < 0)
164 return;
165
166 /* Re-init and indicate host is ready */
167 read_host_csr(&host);
168 host.interrupt_generate = 1;
169 host.ready = 1;
170 host.reset = 0;
171 write_host_csr(&host);
172}
173
174static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
175 void *req_data)
176{
177 struct mei_csr host;
Martin Rothff744bf2019-10-23 21:46:03 -0600178 unsigned int ndata, n;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200179 u32 *data;
180
181 /* Number of dwords to write, ignoring MKHI */
182 ndata = mei->length >> 2;
183
184 /* Pad non-dword aligned request message length */
185 if (mei->length & 3)
186 ndata++;
187 if (!ndata) {
188 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
189 return -1;
190 }
191 ndata++; /* Add MEI header */
192
193 /*
194 * Make sure there is still room left in the circular buffer.
195 * Reset the buffer pointers if the requested message will not fit.
196 */
197 read_host_csr(&host);
198 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
199 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
200 mei_reset();
201 read_host_csr(&host);
202 }
203
204 /*
205 * This implementation does not handle splitting large messages
206 * across multiple transactions. Ensure the requested length
207 * will fit in the available circular buffer depth.
208 */
209 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
210 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
211 ndata + 2, host.buffer_depth);
212 return -1;
213 }
214
215 /* Write MEI header */
216 mei_write_dword_ptr(mei, MEI_H_CB_WW);
217 ndata--;
218
219 /* Write MKHI header */
220 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
221 ndata--;
222
223 /* Write message data */
224 data = req_data;
225 for (n = 0; n < ndata; ++n)
226 write_cb(*data++);
227
228 /* Generate interrupt to the ME */
229 read_host_csr(&host);
230 host.interrupt_generate = 1;
231 write_host_csr(&host);
232
233 /* Make sure ME is ready after sending request data */
234 return mei_wait_for_me_ready();
235}
236
237static int mei_recv_msg(struct mei_header *mei, struct mkhi_header *mkhi,
238 void *rsp_data, int rsp_bytes)
239{
240 struct mei_header mei_rsp;
241 struct mkhi_header mkhi_rsp;
242 struct mei_csr me, host;
Martin Rothff744bf2019-10-23 21:46:03 -0600243 unsigned int ndata, n;
244 unsigned int expected;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200245 u32 *data;
246
247 /* Total number of dwords to read from circular buffer */
248 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
249 if (rsp_bytes & 3)
250 expected++;
251
252 /*
253 * The interrupt status bit does not appear to indicate that the
254 * message has actually been received. Instead we wait until the
255 * expected number of dwords are present in the circular buffer.
256 */
257 for (n = ME_RETRY; n; --n) {
258 read_me_csr(&me);
259 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
260 break;
261 udelay(ME_DELAY);
262 }
263 if (!n) {
264 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
265 "%u, available %u\n", expected,
266 me.buffer_write_ptr - me.buffer_read_ptr);
267 return -1;
268 }
269
270 /* Read and verify MEI response header from the ME */
271 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
272 if (!mei_rsp.is_complete) {
273 printk(BIOS_ERR, "ME: response is not complete\n");
274 return -1;
275 }
276
277 /* Handle non-dword responses and expect at least MKHI header */
278 ndata = mei_rsp.length >> 2;
279 if (mei_rsp.length & 3)
280 ndata++;
281 if (ndata != (expected - 1)) {
282 printk(BIOS_ERR, "ME: response is missing data\n");
283 return -1;
284 }
285
286 /* Read and verify MKHI response header from the ME */
287 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
288 if (!mkhi_rsp.is_response ||
289 mkhi->group_id != mkhi_rsp.group_id ||
290 mkhi->command != mkhi_rsp.command) {
291 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u, "
292 "command %u ?= %u, is_response %u\n", mkhi->group_id,
293 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
294 mkhi_rsp.is_response);
295 return -1;
296 }
297 ndata--; /* MKHI header has been read */
298
299 /* Make sure caller passed a buffer with enough space */
300 if (ndata != (rsp_bytes >> 2)) {
301 printk(BIOS_ERR, "ME: not enough room in response buffer: "
302 "%u != %u\n", ndata, rsp_bytes >> 2);
303 return -1;
304 }
305
306 /* Read response data from the circular buffer */
307 data = rsp_data;
308 for (n = 0; n < ndata; ++n)
309 *data++ = read_cb();
310
311 /* Tell the ME that we have consumed the response */
312 read_host_csr(&host);
313 host.interrupt_status = 1;
314 host.interrupt_generate = 1;
315 write_host_csr(&host);
316
317 return mei_wait_for_me_ready();
318}
319
320static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
321 void *req_data, void *rsp_data, int rsp_bytes)
322{
323 if (mei_send_msg(mei, mkhi, req_data) < 0)
324 return -1;
325 if (mei_recv_msg(mei, mkhi, rsp_data, rsp_bytes) < 0)
326 return -1;
327 return 0;
328}
329
330/* Send END OF POST message to the ME */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200331static int __unused mkhi_end_of_post(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200332{
333 struct mkhi_header mkhi = {
334 .group_id = MKHI_GROUP_ID_GEN,
335 .command = MKHI_END_OF_POST,
336 };
337 struct mei_header mei = {
338 .is_complete = 1,
339 .host_address = MEI_HOST_ADDRESS,
340 .client_address = MEI_ADDRESS_MKHI,
341 .length = sizeof(mkhi),
342 };
343
344 /* Send request and wait for response */
345 if (mei_sendrecv(&mei, &mkhi, NULL, NULL, 0) < 0) {
346 printk(BIOS_ERR, "ME: END OF POST message failed\n");
347 return -1;
348 }
349
350 printk(BIOS_INFO, "ME: END OF POST message successful\n");
351 return 0;
352}
353
Stefan Reinauer8e073822012-04-04 00:07:22 +0200354/* Get ME firmware version */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200355static int __unused mkhi_get_fw_version(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200356{
357 struct me_fw_version version;
358 struct mkhi_header mkhi = {
359 .group_id = MKHI_GROUP_ID_GEN,
360 .command = MKHI_GET_FW_VERSION,
361 };
362 struct mei_header mei = {
363 .is_complete = 1,
364 .host_address = MEI_HOST_ADDRESS,
365 .client_address = MEI_ADDRESS_MKHI,
366 .length = sizeof(mkhi),
367 };
368
369 /* Send request and wait for response */
370 if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) {
371 printk(BIOS_ERR, "ME: GET FW VERSION message failed\n");
372 return -1;
373 }
374
375 printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) "
376 "%u.%u.%u.%u (recovery)\n",
377 version.code_major, version.code_minor,
378 version.code_build_number, version.code_hot_fix,
379 version.recovery_major, version.recovery_minor,
380 version.recovery_build_number, version.recovery_hot_fix);
381
382 return 0;
383}
384
385static inline void print_cap(const char *name, int state)
386{
387 printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n",
388 name, state ? "en" : "dis");
389}
390
391/* Get ME Firmware Capabilities */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200392static int __unused mkhi_get_fwcaps(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200393{
394 u32 rule_id = 0;
395 struct me_fwcaps cap;
396 struct mkhi_header mkhi = {
397 .group_id = MKHI_GROUP_ID_FWCAPS,
398 .command = MKHI_FWCAPS_GET_RULE,
399 };
400 struct mei_header mei = {
401 .is_complete = 1,
402 .host_address = MEI_HOST_ADDRESS,
403 .client_address = MEI_ADDRESS_MKHI,
404 .length = sizeof(mkhi) + sizeof(rule_id),
405 };
406
407 /* Send request and wait for response */
408 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) {
409 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
410 return -1;
411 }
412
413 print_cap("Full Network manageability", cap.caps_sku.full_net);
414 print_cap("Regular Network manageability", cap.caps_sku.std_net);
415 print_cap("Manageability", cap.caps_sku.manageability);
416 print_cap("Small business technology", cap.caps_sku.small_business);
417 print_cap("Level III manageability", cap.caps_sku.l3manageability);
418 print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at);
419 print_cap("IntelR Capability Licensing Service (CLS)",
420 cap.caps_sku.intel_cls);
421 print_cap("IntelR Power Sharing Technology (MPC)",
422 cap.caps_sku.intel_mpc);
423 print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200424 print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200425 print_cap("IPV6", cap.caps_sku.ipv6);
426 print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm);
427 print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och);
428 print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan);
429 print_cap("TLS", cap.caps_sku.tls);
430 print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan);
431
432 return 0;
433}
Stefan Reinauer8e073822012-04-04 00:07:22 +0200434
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200435#ifdef __SIMPLE_DEVICE__
436
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700437static void intel_me7_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200438{
439 struct me_hfs hfs;
440 u32 reg32;
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200441 u16 reg16;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200442
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800443 mei_base_address = (u32 *)
444 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200445
446 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800447 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200448 return;
449
450 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300451 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200452 memcpy(&hfs, &reg32, sizeof(u32));
453
454 /* Abort and leave device alone if not normal mode */
455 if (hfs.fpt_bad ||
456 hfs.working_state != ME_HFS_CWS_NORMAL ||
457 hfs.operation_mode != ME_HFS_MODE_NORMAL)
458 return;
459
460 /* Try to send EOP command so ME stops accepting other commands */
461 mkhi_end_of_post();
462
463 /* Make sure IO is disabled */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200464 reg16 = pci_read_config16(PCH_ME_DEV, PCI_COMMAND);
465 reg16 &= ~(PCI_COMMAND_MASTER |
Stefan Reinauer8e073822012-04-04 00:07:22 +0200466 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200467 pci_write_config16(PCH_ME_DEV, PCI_COMMAND, reg16);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200468
469 /* Hide the PCI device */
470 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
471}
472
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700473void intel_me_finalize_smm(void)
474{
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300475 u32 did = pci_read_config32(PCH_ME_DEV, PCI_VENDOR_ID);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700476 switch (did) {
Duncan Laurie708f7312012-07-10 15:15:41 -0700477 case 0x1c3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700478 intel_me7_finalize_smm();
479 break;
Duncan Laurie708f7312012-07-10 15:15:41 -0700480 case 0x1e3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700481 intel_me8_finalize_smm();
482 break;
483 default:
484 printk(BIOS_ERR, "No finalize handler for ME %08x.\n", did);
485 }
486}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200487
488#else
Stefan Reinauer8e073822012-04-04 00:07:22 +0200489
490/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200491static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200492{
493 me_bios_path path = ME_DISABLE_BIOS_PATH;
494 struct me_hfs hfs;
495 struct me_gmes gmes;
496
Stefan Reinauer8e073822012-04-04 00:07:22 +0200497 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300498 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200499 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200500
501 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
502 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
503
504 /* Check and dump status */
505 intel_me_status(&hfs, &gmes);
506
Stefan Reinauer8e073822012-04-04 00:07:22 +0200507 /* Check Current Working State */
508 switch (hfs.working_state) {
509 case ME_HFS_CWS_NORMAL:
510 path = ME_NORMAL_BIOS_PATH;
511 break;
512 case ME_HFS_CWS_REC:
513 path = ME_RECOVERY_BIOS_PATH;
514 break;
515 default:
516 path = ME_DISABLE_BIOS_PATH;
517 break;
518 }
519
520 /* Check Current Operation Mode */
521 switch (hfs.operation_mode) {
522 case ME_HFS_MODE_NORMAL:
523 break;
524 case ME_HFS_MODE_DEBUG:
525 case ME_HFS_MODE_DIS:
526 case ME_HFS_MODE_OVER_JMPR:
527 case ME_HFS_MODE_OVER_MEI:
528 default:
529 path = ME_DISABLE_BIOS_PATH;
530 break;
531 }
532
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700533 /* Check for any error code and valid firmware */
534 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200535 path = ME_ERROR_BIOS_PATH;
536
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200537 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700538 struct elog_event_data_me_extended data = {
539 .current_working_state = hfs.working_state,
540 .operation_state = hfs.operation_state,
541 .operation_mode = hfs.operation_mode,
542 .error_code = hfs.error_code,
543 .progress_code = gmes.progress_code,
544 .current_pmevent = gmes.current_pmevent,
545 .current_state = gmes.current_state,
546 };
547 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
548 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
549 &data, sizeof(data));
550 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700551
Stefan Reinauer8e073822012-04-04 00:07:22 +0200552 return path;
553}
554
555/* Prepare ME for MEI messages */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200556static int intel_mei_setup(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200557{
558 struct resource *res;
559 struct mei_csr host;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200560
561 /* Find the MMIO base for the ME interface */
562 res = find_resource(dev, PCI_BASE_ADDRESS_0);
563 if (!res || res->base == 0 || res->size == 0) {
564 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
565 return -1;
566 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800567 mei_base_address = (u32*)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200568
569 /* Ensure Memory and Bus Master bits are set */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200570 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200571
572 /* Clean up status for next message */
573 read_host_csr(&host);
574 host.interrupt_generate = 1;
575 host.ready = 1;
576 host.reset = 0;
577 write_host_csr(&host);
578
579 return 0;
580}
581
582/* Read the Extend register hash of ME firmware */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200583static int intel_me_extend_valid(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200584{
585 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700586 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200587 int i, count = 0;
588
589 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
590 if (!status.extend_feature_present) {
591 printk(BIOS_ERR, "ME: Extend Feature not present\n");
592 return -1;
593 }
594
595 if (!status.extend_reg_valid) {
596 printk(BIOS_ERR, "ME: Extend Register not valid\n");
597 return -1;
598 }
599
600 switch (status.extend_reg_algorithm) {
601 case PCI_ME_EXT_SHA1:
602 count = 5;
603 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
604 break;
605 case PCI_ME_EXT_SHA256:
606 count = 8;
607 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
608 break;
609 default:
610 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
611 status.extend_reg_algorithm);
612 return -1;
613 }
614
615 for (i = 0; i < count; ++i) {
616 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
617 printk(BIOS_DEBUG, "%08x", extend[i]);
618 }
619 printk(BIOS_DEBUG, "\n");
620
Julius Wernercd49cce2019-03-05 16:53:33 -0800621#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200622 /* Save hash in NVS for the OS to verify */
623 chromeos_set_me_hash(extend, count);
624#endif
625
626 return 0;
627}
628
629/* Hide the ME virtual PCI devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200630static void intel_me_hide(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200631{
632 dev->enabled = 0;
633 pch_enable(dev);
634}
635
636/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200637static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200638{
639 me_bios_path path = intel_me_path(dev);
640
641 /* Do initial setup and determine the BIOS path */
642 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
643
644 switch (path) {
645 case ME_S3WAKE_BIOS_PATH:
646 intel_me_hide(dev);
647 break;
648
649 case ME_NORMAL_BIOS_PATH:
650 /* Validate the extend register */
651 if (intel_me_extend_valid(dev) < 0)
652 break; /* TODO: force recovery mode */
653
654 /* Prepare MEI MMIO interface */
655 if (intel_mei_setup(dev) < 0)
656 break;
657
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200658 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
659 /* Print ME firmware version */
660 mkhi_get_fw_version();
661 /* Print ME firmware capabilities */
662 mkhi_get_fwcaps();
663 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200664
665 /*
666 * Leave the ME unlocked in this path.
667 * It will be locked via SMI command later.
668 */
669 break;
670
671 case ME_ERROR_BIOS_PATH:
672 case ME_RECOVERY_BIOS_PATH:
673 case ME_DISABLE_BIOS_PATH:
674 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200675 break;
676 }
677}
678
Stefan Reinauer8e073822012-04-04 00:07:22 +0200679static struct pci_operations pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530680 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200681};
682
683static struct device_operations device_ops = {
684 .read_resources = pci_dev_read_resources,
685 .set_resources = pci_dev_set_resources,
686 .enable_resources = pci_dev_enable_resources,
687 .init = intel_me_init,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200688 .ops_pci = &pci_ops,
689};
690
691static const struct pci_driver intel_me __pci_driver = {
692 .ops = &device_ops,
693 .vendor = PCI_VENDOR_ID_INTEL,
694 .device = 0x1c3a,
695};
696
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200697#endif /* __SIMPLE_DEVICE__ */