blob: ae62638f1b86d6a26130fa69f952d5324787a540 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer8e073822012-04-04 00:07:22 +02003
4/*
5 * This is a ramstage driver for the Intel Management Engine found in the
6 * 6-series chipset. It handles the required boot-time messages over the
7 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
8 * finished with POST. Additional messages are defined for debug but are
9 * not used unless the console loglevel is high enough.
10 */
11
Furquan Shaikh76cedd22020-05-02 10:24:23 -070012#include <acpi/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020013#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020014#include <device/pci_ops.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020015#include <console/console.h>
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020016#include <device/device.h>
17#include <device/pci.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020018#include <device/pci_ids.h>
19#include <device/pci_def.h>
20#include <string.h>
21#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070022#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010023#include <halt.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020024
Stefan Reinauer8e073822012-04-04 00:07:22 +020025#include "me.h"
26#include "pch.h"
27
Julius Wernercd49cce2019-03-05 16:53:33 -080028#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020029#include <vendorcode/google/chromeos/gnvs.h>
30#endif
31
Stefan Reinauer8e073822012-04-04 00:07:22 +020032/* Path that the BIOS should take based on ME state */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020033static const char *me_bios_path_values[] __unused = {
Stefan Reinauer8e073822012-04-04 00:07:22 +020034 [ME_NORMAL_BIOS_PATH] = "Normal",
35 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
36 [ME_ERROR_BIOS_PATH] = "Error",
37 [ME_RECOVERY_BIOS_PATH] = "Recovery",
38 [ME_DISABLE_BIOS_PATH] = "Disable",
39 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
40};
Stefan Reinauer8e073822012-04-04 00:07:22 +020041
42/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080043static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020044
Stefan Reinauer8e073822012-04-04 00:07:22 +020045static void mei_dump(void *ptr, int dword, int offset, const char *type)
46{
47 struct mei_csr *csr;
48
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020049 if (!CONFIG(DEBUG_INTEL_ME))
50 return;
51
Stefan Reinauer8e073822012-04-04 00:07:22 +020052 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
53
54 switch (offset) {
55 case MEI_H_CSR:
56 case MEI_ME_CSR_HA:
57 csr = ptr;
58 if (!csr) {
59 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
60 break;
61 }
62 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
63 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
64 csr->buffer_read_ptr, csr->buffer_write_ptr,
65 csr->ready, csr->reset, csr->interrupt_generate,
66 csr->interrupt_status, csr->interrupt_enable);
67 break;
68 case MEI_ME_CB_RW:
69 case MEI_H_CB_WW:
70 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
71 break;
72 default:
73 printk(BIOS_SPEW, "0x%08x\n", offset);
74 break;
75 }
76}
Stefan Reinauer8e073822012-04-04 00:07:22 +020077
78/*
79 * ME/MEI access helpers using memcpy to avoid aliasing.
80 */
81
82static inline void mei_read_dword_ptr(void *ptr, int offset)
83{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080084 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +020085 memcpy(ptr, &dword, sizeof(dword));
86 mei_dump(ptr, dword, offset, "READ");
87}
88
89static inline void mei_write_dword_ptr(void *ptr, int offset)
90{
91 u32 dword = 0;
92 memcpy(&dword, ptr, sizeof(dword));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080093 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +020094 mei_dump(ptr, dword, offset, "WRITE");
95}
96
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020097#ifndef __SIMPLE_DEVICE__
Elyes HAOUASdc035282018-09-18 13:28:49 +020098static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Stefan Reinauer8e073822012-04-04 00:07:22 +020099{
100 u32 dword = pci_read_config32(dev, offset);
101 memcpy(ptr, &dword, sizeof(dword));
102 mei_dump(ptr, dword, offset, "PCI READ");
103}
104#endif
105
106static inline void read_host_csr(struct mei_csr *csr)
107{
108 mei_read_dword_ptr(csr, MEI_H_CSR);
109}
110
111static inline void write_host_csr(struct mei_csr *csr)
112{
113 mei_write_dword_ptr(csr, MEI_H_CSR);
114}
115
116static inline void read_me_csr(struct mei_csr *csr)
117{
118 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
119}
120
121static inline void write_cb(u32 dword)
122{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800123 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200124 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
125}
126
127static inline u32 read_cb(void)
128{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800129 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200130 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
131 return dword;
132}
133
134/* Wait for ME ready bit to be asserted */
135static int mei_wait_for_me_ready(void)
136{
137 struct mei_csr me;
Martin Rothff744bf2019-10-23 21:46:03 -0600138 unsigned int try = ME_RETRY;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200139
140 while (try--) {
141 read_me_csr(&me);
142 if (me.ready)
143 return 0;
144 udelay(ME_DELAY);
145 }
146
147 printk(BIOS_ERR, "ME: failed to become ready\n");
148 return -1;
149}
150
151static void mei_reset(void)
152{
153 struct mei_csr host;
154
155 if (mei_wait_for_me_ready() < 0)
156 return;
157
158 /* Reset host and ME circular buffers for next message */
159 read_host_csr(&host);
160 host.reset = 1;
161 host.interrupt_generate = 1;
162 write_host_csr(&host);
163
164 if (mei_wait_for_me_ready() < 0)
165 return;
166
167 /* Re-init and indicate host is ready */
168 read_host_csr(&host);
169 host.interrupt_generate = 1;
170 host.ready = 1;
171 host.reset = 0;
172 write_host_csr(&host);
173}
174
175static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
176 void *req_data)
177{
178 struct mei_csr host;
Martin Rothff744bf2019-10-23 21:46:03 -0600179 unsigned int ndata, n;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200180 u32 *data;
181
182 /* Number of dwords to write, ignoring MKHI */
183 ndata = mei->length >> 2;
184
185 /* Pad non-dword aligned request message length */
186 if (mei->length & 3)
187 ndata++;
188 if (!ndata) {
189 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
190 return -1;
191 }
192 ndata++; /* Add MEI header */
193
194 /*
195 * Make sure there is still room left in the circular buffer.
196 * Reset the buffer pointers if the requested message will not fit.
197 */
198 read_host_csr(&host);
199 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
200 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
201 mei_reset();
202 read_host_csr(&host);
203 }
204
205 /*
206 * This implementation does not handle splitting large messages
207 * across multiple transactions. Ensure the requested length
208 * will fit in the available circular buffer depth.
209 */
210 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
211 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
212 ndata + 2, host.buffer_depth);
213 return -1;
214 }
215
216 /* Write MEI header */
217 mei_write_dword_ptr(mei, MEI_H_CB_WW);
218 ndata--;
219
220 /* Write MKHI header */
221 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
222 ndata--;
223
224 /* Write message data */
225 data = req_data;
226 for (n = 0; n < ndata; ++n)
227 write_cb(*data++);
228
229 /* Generate interrupt to the ME */
230 read_host_csr(&host);
231 host.interrupt_generate = 1;
232 write_host_csr(&host);
233
234 /* Make sure ME is ready after sending request data */
235 return mei_wait_for_me_ready();
236}
237
238static int mei_recv_msg(struct mei_header *mei, struct mkhi_header *mkhi,
239 void *rsp_data, int rsp_bytes)
240{
241 struct mei_header mei_rsp;
242 struct mkhi_header mkhi_rsp;
243 struct mei_csr me, host;
Martin Rothff744bf2019-10-23 21:46:03 -0600244 unsigned int ndata, n;
245 unsigned int expected;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200246 u32 *data;
247
248 /* Total number of dwords to read from circular buffer */
249 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
250 if (rsp_bytes & 3)
251 expected++;
252
253 /*
254 * The interrupt status bit does not appear to indicate that the
255 * message has actually been received. Instead we wait until the
256 * expected number of dwords are present in the circular buffer.
257 */
258 for (n = ME_RETRY; n; --n) {
259 read_me_csr(&me);
260 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
261 break;
262 udelay(ME_DELAY);
263 }
264 if (!n) {
265 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
266 "%u, available %u\n", expected,
267 me.buffer_write_ptr - me.buffer_read_ptr);
268 return -1;
269 }
270
271 /* Read and verify MEI response header from the ME */
272 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
273 if (!mei_rsp.is_complete) {
274 printk(BIOS_ERR, "ME: response is not complete\n");
275 return -1;
276 }
277
278 /* Handle non-dword responses and expect at least MKHI header */
279 ndata = mei_rsp.length >> 2;
280 if (mei_rsp.length & 3)
281 ndata++;
282 if (ndata != (expected - 1)) {
283 printk(BIOS_ERR, "ME: response is missing data\n");
284 return -1;
285 }
286
287 /* Read and verify MKHI response header from the ME */
288 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
289 if (!mkhi_rsp.is_response ||
290 mkhi->group_id != mkhi_rsp.group_id ||
291 mkhi->command != mkhi_rsp.command) {
292 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u, "
293 "command %u ?= %u, is_response %u\n", mkhi->group_id,
294 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
295 mkhi_rsp.is_response);
296 return -1;
297 }
298 ndata--; /* MKHI header has been read */
299
300 /* Make sure caller passed a buffer with enough space */
301 if (ndata != (rsp_bytes >> 2)) {
302 printk(BIOS_ERR, "ME: not enough room in response buffer: "
303 "%u != %u\n", ndata, rsp_bytes >> 2);
304 return -1;
305 }
306
307 /* Read response data from the circular buffer */
308 data = rsp_data;
309 for (n = 0; n < ndata; ++n)
310 *data++ = read_cb();
311
312 /* Tell the ME that we have consumed the response */
313 read_host_csr(&host);
314 host.interrupt_status = 1;
315 host.interrupt_generate = 1;
316 write_host_csr(&host);
317
318 return mei_wait_for_me_ready();
319}
320
321static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
322 void *req_data, void *rsp_data, int rsp_bytes)
323{
324 if (mei_send_msg(mei, mkhi, req_data) < 0)
325 return -1;
326 if (mei_recv_msg(mei, mkhi, rsp_data, rsp_bytes) < 0)
327 return -1;
328 return 0;
329}
330
331/* Send END OF POST message to the ME */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200332static int __unused mkhi_end_of_post(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200333{
334 struct mkhi_header mkhi = {
335 .group_id = MKHI_GROUP_ID_GEN,
336 .command = MKHI_END_OF_POST,
337 };
338 struct mei_header mei = {
339 .is_complete = 1,
340 .host_address = MEI_HOST_ADDRESS,
341 .client_address = MEI_ADDRESS_MKHI,
342 .length = sizeof(mkhi),
343 };
344
345 /* Send request and wait for response */
346 if (mei_sendrecv(&mei, &mkhi, NULL, NULL, 0) < 0) {
347 printk(BIOS_ERR, "ME: END OF POST message failed\n");
348 return -1;
349 }
350
351 printk(BIOS_INFO, "ME: END OF POST message successful\n");
352 return 0;
353}
354
Stefan Reinauer8e073822012-04-04 00:07:22 +0200355/* Get ME firmware version */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200356static int __unused mkhi_get_fw_version(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200357{
358 struct me_fw_version version;
359 struct mkhi_header mkhi = {
360 .group_id = MKHI_GROUP_ID_GEN,
361 .command = MKHI_GET_FW_VERSION,
362 };
363 struct mei_header mei = {
364 .is_complete = 1,
365 .host_address = MEI_HOST_ADDRESS,
366 .client_address = MEI_ADDRESS_MKHI,
367 .length = sizeof(mkhi),
368 };
369
370 /* Send request and wait for response */
371 if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) {
372 printk(BIOS_ERR, "ME: GET FW VERSION message failed\n");
373 return -1;
374 }
375
376 printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) "
377 "%u.%u.%u.%u (recovery)\n",
378 version.code_major, version.code_minor,
379 version.code_build_number, version.code_hot_fix,
380 version.recovery_major, version.recovery_minor,
381 version.recovery_build_number, version.recovery_hot_fix);
382
383 return 0;
384}
385
386static inline void print_cap(const char *name, int state)
387{
388 printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n",
389 name, state ? "en" : "dis");
390}
391
392/* Get ME Firmware Capabilities */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200393static int __unused mkhi_get_fwcaps(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200394{
395 u32 rule_id = 0;
396 struct me_fwcaps cap;
397 struct mkhi_header mkhi = {
398 .group_id = MKHI_GROUP_ID_FWCAPS,
399 .command = MKHI_FWCAPS_GET_RULE,
400 };
401 struct mei_header mei = {
402 .is_complete = 1,
403 .host_address = MEI_HOST_ADDRESS,
404 .client_address = MEI_ADDRESS_MKHI,
405 .length = sizeof(mkhi) + sizeof(rule_id),
406 };
407
408 /* Send request and wait for response */
409 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) {
410 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
411 return -1;
412 }
413
414 print_cap("Full Network manageability", cap.caps_sku.full_net);
415 print_cap("Regular Network manageability", cap.caps_sku.std_net);
416 print_cap("Manageability", cap.caps_sku.manageability);
417 print_cap("Small business technology", cap.caps_sku.small_business);
418 print_cap("Level III manageability", cap.caps_sku.l3manageability);
419 print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at);
420 print_cap("IntelR Capability Licensing Service (CLS)",
421 cap.caps_sku.intel_cls);
422 print_cap("IntelR Power Sharing Technology (MPC)",
423 cap.caps_sku.intel_mpc);
424 print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200425 print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200426 print_cap("IPV6", cap.caps_sku.ipv6);
427 print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm);
428 print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och);
429 print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan);
430 print_cap("TLS", cap.caps_sku.tls);
431 print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan);
432
433 return 0;
434}
Stefan Reinauer8e073822012-04-04 00:07:22 +0200435
Julius Wernercd49cce2019-03-05 16:53:33 -0800436#if CONFIG(CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200437/* Tell ME to issue a global reset */
438int mkhi_global_reset(void)
439{
440 struct me_global_reset reset = {
441 .request_origin = GLOBAL_RESET_BIOS_POST,
442 .reset_type = CBM_RR_GLOBAL_RESET,
443 };
444 struct mkhi_header mkhi = {
445 .group_id = MKHI_GROUP_ID_CBM,
446 .command = MKHI_GLOBAL_RESET,
447 };
448 struct mei_header mei = {
449 .is_complete = 1,
450 .length = sizeof(mkhi) + sizeof(reset),
451 .host_address = MEI_HOST_ADDRESS,
452 .client_address = MEI_ADDRESS_MKHI,
453 };
454
455 printk(BIOS_NOTICE, "ME: Requesting global reset\n");
456
457 /* Send request and wait for response */
458 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
459 /* No response means reset will happen shortly... */
Patrick Georgi546953c2014-11-29 10:38:17 +0100460 halt();
Stefan Reinauer8e073822012-04-04 00:07:22 +0200461 }
462
463 /* If the ME responded it rejected the reset request */
464 printk(BIOS_ERR, "ME: Global Reset failed\n");
465 return -1;
466}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700467#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200468
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200469#ifdef __SIMPLE_DEVICE__
470
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700471static void intel_me7_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200472{
473 struct me_hfs hfs;
474 u32 reg32;
475
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800476 mei_base_address = (u32 *)
477 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200478
479 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800480 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200481 return;
482
483 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300484 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200485 memcpy(&hfs, &reg32, sizeof(u32));
486
487 /* Abort and leave device alone if not normal mode */
488 if (hfs.fpt_bad ||
489 hfs.working_state != ME_HFS_CWS_NORMAL ||
490 hfs.operation_mode != ME_HFS_MODE_NORMAL)
491 return;
492
493 /* Try to send EOP command so ME stops accepting other commands */
494 mkhi_end_of_post();
495
496 /* Make sure IO is disabled */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300497 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200498 reg32 &= ~(PCI_COMMAND_MASTER |
499 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300500 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200501
502 /* Hide the PCI device */
503 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
504}
505
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700506void intel_me_finalize_smm(void)
507{
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300508 u32 did = pci_read_config32(PCH_ME_DEV, PCI_VENDOR_ID);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700509 switch (did) {
Duncan Laurie708f7312012-07-10 15:15:41 -0700510 case 0x1c3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700511 intel_me7_finalize_smm();
512 break;
Duncan Laurie708f7312012-07-10 15:15:41 -0700513 case 0x1e3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700514 intel_me8_finalize_smm();
515 break;
516 default:
517 printk(BIOS_ERR, "No finalize handler for ME %08x.\n", did);
518 }
519}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200520
521#else
Stefan Reinauer8e073822012-04-04 00:07:22 +0200522
523/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200524static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200525{
526 me_bios_path path = ME_DISABLE_BIOS_PATH;
527 struct me_hfs hfs;
528 struct me_gmes gmes;
529
Stefan Reinauer8e073822012-04-04 00:07:22 +0200530 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300531 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200532 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200533
534 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
535 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
536
537 /* Check and dump status */
538 intel_me_status(&hfs, &gmes);
539
Stefan Reinauer8e073822012-04-04 00:07:22 +0200540 /* Check Current Working State */
541 switch (hfs.working_state) {
542 case ME_HFS_CWS_NORMAL:
543 path = ME_NORMAL_BIOS_PATH;
544 break;
545 case ME_HFS_CWS_REC:
546 path = ME_RECOVERY_BIOS_PATH;
547 break;
548 default:
549 path = ME_DISABLE_BIOS_PATH;
550 break;
551 }
552
553 /* Check Current Operation Mode */
554 switch (hfs.operation_mode) {
555 case ME_HFS_MODE_NORMAL:
556 break;
557 case ME_HFS_MODE_DEBUG:
558 case ME_HFS_MODE_DIS:
559 case ME_HFS_MODE_OVER_JMPR:
560 case ME_HFS_MODE_OVER_MEI:
561 default:
562 path = ME_DISABLE_BIOS_PATH;
563 break;
564 }
565
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700566 /* Check for any error code and valid firmware */
567 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200568 path = ME_ERROR_BIOS_PATH;
569
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200570 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700571 struct elog_event_data_me_extended data = {
572 .current_working_state = hfs.working_state,
573 .operation_state = hfs.operation_state,
574 .operation_mode = hfs.operation_mode,
575 .error_code = hfs.error_code,
576 .progress_code = gmes.progress_code,
577 .current_pmevent = gmes.current_pmevent,
578 .current_state = gmes.current_state,
579 };
580 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
581 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
582 &data, sizeof(data));
583 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700584
Stefan Reinauer8e073822012-04-04 00:07:22 +0200585 return path;
586}
587
588/* Prepare ME for MEI messages */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200589static int intel_mei_setup(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200590{
591 struct resource *res;
592 struct mei_csr host;
593 u32 reg32;
594
595 /* Find the MMIO base for the ME interface */
596 res = find_resource(dev, PCI_BASE_ADDRESS_0);
597 if (!res || res->base == 0 || res->size == 0) {
598 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
599 return -1;
600 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800601 mei_base_address = (u32*)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200602
603 /* Ensure Memory and Bus Master bits are set */
604 reg32 = pci_read_config32(dev, PCI_COMMAND);
605 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
606 pci_write_config32(dev, PCI_COMMAND, reg32);
607
608 /* Clean up status for next message */
609 read_host_csr(&host);
610 host.interrupt_generate = 1;
611 host.ready = 1;
612 host.reset = 0;
613 write_host_csr(&host);
614
615 return 0;
616}
617
618/* Read the Extend register hash of ME firmware */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200619static int intel_me_extend_valid(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200620{
621 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700622 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200623 int i, count = 0;
624
625 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
626 if (!status.extend_feature_present) {
627 printk(BIOS_ERR, "ME: Extend Feature not present\n");
628 return -1;
629 }
630
631 if (!status.extend_reg_valid) {
632 printk(BIOS_ERR, "ME: Extend Register not valid\n");
633 return -1;
634 }
635
636 switch (status.extend_reg_algorithm) {
637 case PCI_ME_EXT_SHA1:
638 count = 5;
639 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
640 break;
641 case PCI_ME_EXT_SHA256:
642 count = 8;
643 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
644 break;
645 default:
646 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
647 status.extend_reg_algorithm);
648 return -1;
649 }
650
651 for (i = 0; i < count; ++i) {
652 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
653 printk(BIOS_DEBUG, "%08x", extend[i]);
654 }
655 printk(BIOS_DEBUG, "\n");
656
Julius Wernercd49cce2019-03-05 16:53:33 -0800657#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200658 /* Save hash in NVS for the OS to verify */
659 chromeos_set_me_hash(extend, count);
660#endif
661
662 return 0;
663}
664
665/* Hide the ME virtual PCI devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200666static void intel_me_hide(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200667{
668 dev->enabled = 0;
669 pch_enable(dev);
670}
671
672/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200673static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200674{
675 me_bios_path path = intel_me_path(dev);
676
677 /* Do initial setup and determine the BIOS path */
678 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
679
680 switch (path) {
681 case ME_S3WAKE_BIOS_PATH:
682 intel_me_hide(dev);
683 break;
684
685 case ME_NORMAL_BIOS_PATH:
686 /* Validate the extend register */
687 if (intel_me_extend_valid(dev) < 0)
688 break; /* TODO: force recovery mode */
689
690 /* Prepare MEI MMIO interface */
691 if (intel_mei_setup(dev) < 0)
692 break;
693
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200694 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
695 /* Print ME firmware version */
696 mkhi_get_fw_version();
697 /* Print ME firmware capabilities */
698 mkhi_get_fwcaps();
699 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200700
701 /*
702 * Leave the ME unlocked in this path.
703 * It will be locked via SMI command later.
704 */
705 break;
706
707 case ME_ERROR_BIOS_PATH:
708 case ME_RECOVERY_BIOS_PATH:
709 case ME_DISABLE_BIOS_PATH:
710 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200711 break;
712 }
713}
714
Stefan Reinauer8e073822012-04-04 00:07:22 +0200715static struct pci_operations pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530716 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200717};
718
719static struct device_operations device_ops = {
720 .read_resources = pci_dev_read_resources,
721 .set_resources = pci_dev_set_resources,
722 .enable_resources = pci_dev_enable_resources,
723 .init = intel_me_init,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200724 .ops_pci = &pci_ops,
725};
726
727static const struct pci_driver intel_me __pci_driver = {
728 .ops = &device_ops,
729 .vendor = PCI_VENDOR_ID_INTEL,
730 .device = 0x1c3a,
731};
732
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200733#endif /* __SIMPLE_DEVICE__ */