blob: 58c24784db441c5fd14fcea5ddde0d3b9bdef20a [file] [log] [blame]
Stefan Reinauer8e073822012-04-04 00:07:22 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Stefan Reinauer8e073822012-04-04 00:07:22 +020015 */
16
17/*
18 * This is a ramstage driver for the Intel Management Engine found in the
19 * 6-series chipset. It handles the required boot-time messages over the
20 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
21 * finished with POST. Additional messages are defined for debug but are
22 * not used unless the console loglevel is high enough.
23 */
24
25#include <arch/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020026#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020027#include <device/pci_ops.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020028#include <console/console.h>
29#include <device/pci_ids.h>
30#include <device/pci_def.h>
31#include <string.h>
32#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070033#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010034#include <halt.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020035
Elyes HAOUASead574e2018-11-11 20:52:30 +010036#ifndef __SMM__
37#include <device/device.h>
38#include <device/pci.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020039#endif
40
41#include "me.h"
42#include "pch.h"
43
Martin Roth7a1a3ad2017-06-24 21:29:38 -060044#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020045#include <vendorcode/google/chromeos/gnvs.h>
46#endif
47
48#ifndef __SMM__
49/* Path that the BIOS should take based on ME state */
50static const char *me_bios_path_values[] = {
51 [ME_NORMAL_BIOS_PATH] = "Normal",
52 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
53 [ME_ERROR_BIOS_PATH] = "Error",
54 [ME_RECOVERY_BIOS_PATH] = "Recovery",
55 [ME_DISABLE_BIOS_PATH] = "Disable",
56 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
57};
58#endif
59
60/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080061static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020062
Martin Roth7a1a3ad2017-06-24 21:29:38 -060063#if IS_ENABLED(CONFIG_DEBUG_INTEL_ME)
Stefan Reinauer8e073822012-04-04 00:07:22 +020064static void mei_dump(void *ptr, int dword, int offset, const char *type)
65{
66 struct mei_csr *csr;
67
68 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
69
70 switch (offset) {
71 case MEI_H_CSR:
72 case MEI_ME_CSR_HA:
73 csr = ptr;
74 if (!csr) {
75 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
76 break;
77 }
78 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
79 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
80 csr->buffer_read_ptr, csr->buffer_write_ptr,
81 csr->ready, csr->reset, csr->interrupt_generate,
82 csr->interrupt_status, csr->interrupt_enable);
83 break;
84 case MEI_ME_CB_RW:
85 case MEI_H_CB_WW:
86 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
87 break;
88 default:
89 printk(BIOS_SPEW, "0x%08x\n", offset);
90 break;
91 }
92}
93#else
94# define mei_dump(ptr,dword,offset,type) do {} while (0)
95#endif
96
97/*
98 * ME/MEI access helpers using memcpy to avoid aliasing.
99 */
100
101static inline void mei_read_dword_ptr(void *ptr, int offset)
102{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800103 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200104 memcpy(ptr, &dword, sizeof(dword));
105 mei_dump(ptr, dword, offset, "READ");
106}
107
108static inline void mei_write_dword_ptr(void *ptr, int offset)
109{
110 u32 dword = 0;
111 memcpy(&dword, ptr, sizeof(dword));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800112 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113 mei_dump(ptr, dword, offset, "WRITE");
114}
115
116#ifndef __SMM__
Elyes HAOUASdc035282018-09-18 13:28:49 +0200117static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118{
119 u32 dword = pci_read_config32(dev, offset);
120 memcpy(ptr, &dword, sizeof(dword));
121 mei_dump(ptr, dword, offset, "PCI READ");
122}
123#endif
124
125static inline void read_host_csr(struct mei_csr *csr)
126{
127 mei_read_dword_ptr(csr, MEI_H_CSR);
128}
129
130static inline void write_host_csr(struct mei_csr *csr)
131{
132 mei_write_dword_ptr(csr, MEI_H_CSR);
133}
134
135static inline void read_me_csr(struct mei_csr *csr)
136{
137 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
138}
139
140static inline void write_cb(u32 dword)
141{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800142 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200143 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
144}
145
146static inline u32 read_cb(void)
147{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800148 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200149 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
150 return dword;
151}
152
153/* Wait for ME ready bit to be asserted */
154static int mei_wait_for_me_ready(void)
155{
156 struct mei_csr me;
157 unsigned try = ME_RETRY;
158
159 while (try--) {
160 read_me_csr(&me);
161 if (me.ready)
162 return 0;
163 udelay(ME_DELAY);
164 }
165
166 printk(BIOS_ERR, "ME: failed to become ready\n");
167 return -1;
168}
169
170static void mei_reset(void)
171{
172 struct mei_csr host;
173
174 if (mei_wait_for_me_ready() < 0)
175 return;
176
177 /* Reset host and ME circular buffers for next message */
178 read_host_csr(&host);
179 host.reset = 1;
180 host.interrupt_generate = 1;
181 write_host_csr(&host);
182
183 if (mei_wait_for_me_ready() < 0)
184 return;
185
186 /* Re-init and indicate host is ready */
187 read_host_csr(&host);
188 host.interrupt_generate = 1;
189 host.ready = 1;
190 host.reset = 0;
191 write_host_csr(&host);
192}
193
194static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
195 void *req_data)
196{
197 struct mei_csr host;
198 unsigned ndata, n;
199 u32 *data;
200
201 /* Number of dwords to write, ignoring MKHI */
202 ndata = mei->length >> 2;
203
204 /* Pad non-dword aligned request message length */
205 if (mei->length & 3)
206 ndata++;
207 if (!ndata) {
208 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
209 return -1;
210 }
211 ndata++; /* Add MEI header */
212
213 /*
214 * Make sure there is still room left in the circular buffer.
215 * Reset the buffer pointers if the requested message will not fit.
216 */
217 read_host_csr(&host);
218 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
219 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
220 mei_reset();
221 read_host_csr(&host);
222 }
223
224 /*
225 * This implementation does not handle splitting large messages
226 * across multiple transactions. Ensure the requested length
227 * will fit in the available circular buffer depth.
228 */
229 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
230 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
231 ndata + 2, host.buffer_depth);
232 return -1;
233 }
234
235 /* Write MEI header */
236 mei_write_dword_ptr(mei, MEI_H_CB_WW);
237 ndata--;
238
239 /* Write MKHI header */
240 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
241 ndata--;
242
243 /* Write message data */
244 data = req_data;
245 for (n = 0; n < ndata; ++n)
246 write_cb(*data++);
247
248 /* Generate interrupt to the ME */
249 read_host_csr(&host);
250 host.interrupt_generate = 1;
251 write_host_csr(&host);
252
253 /* Make sure ME is ready after sending request data */
254 return mei_wait_for_me_ready();
255}
256
257static int mei_recv_msg(struct mei_header *mei, struct mkhi_header *mkhi,
258 void *rsp_data, int rsp_bytes)
259{
260 struct mei_header mei_rsp;
261 struct mkhi_header mkhi_rsp;
262 struct mei_csr me, host;
263 unsigned ndata, n;
264 unsigned expected;
265 u32 *data;
266
267 /* Total number of dwords to read from circular buffer */
268 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
269 if (rsp_bytes & 3)
270 expected++;
271
272 /*
273 * The interrupt status bit does not appear to indicate that the
274 * message has actually been received. Instead we wait until the
275 * expected number of dwords are present in the circular buffer.
276 */
277 for (n = ME_RETRY; n; --n) {
278 read_me_csr(&me);
279 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
280 break;
281 udelay(ME_DELAY);
282 }
283 if (!n) {
284 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
285 "%u, available %u\n", expected,
286 me.buffer_write_ptr - me.buffer_read_ptr);
287 return -1;
288 }
289
290 /* Read and verify MEI response header from the ME */
291 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
292 if (!mei_rsp.is_complete) {
293 printk(BIOS_ERR, "ME: response is not complete\n");
294 return -1;
295 }
296
297 /* Handle non-dword responses and expect at least MKHI header */
298 ndata = mei_rsp.length >> 2;
299 if (mei_rsp.length & 3)
300 ndata++;
301 if (ndata != (expected - 1)) {
302 printk(BIOS_ERR, "ME: response is missing data\n");
303 return -1;
304 }
305
306 /* Read and verify MKHI response header from the ME */
307 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
308 if (!mkhi_rsp.is_response ||
309 mkhi->group_id != mkhi_rsp.group_id ||
310 mkhi->command != mkhi_rsp.command) {
311 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u, "
312 "command %u ?= %u, is_response %u\n", mkhi->group_id,
313 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
314 mkhi_rsp.is_response);
315 return -1;
316 }
317 ndata--; /* MKHI header has been read */
318
319 /* Make sure caller passed a buffer with enough space */
320 if (ndata != (rsp_bytes >> 2)) {
321 printk(BIOS_ERR, "ME: not enough room in response buffer: "
322 "%u != %u\n", ndata, rsp_bytes >> 2);
323 return -1;
324 }
325
326 /* Read response data from the circular buffer */
327 data = rsp_data;
328 for (n = 0; n < ndata; ++n)
329 *data++ = read_cb();
330
331 /* Tell the ME that we have consumed the response */
332 read_host_csr(&host);
333 host.interrupt_status = 1;
334 host.interrupt_generate = 1;
335 write_host_csr(&host);
336
337 return mei_wait_for_me_ready();
338}
339
340static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
341 void *req_data, void *rsp_data, int rsp_bytes)
342{
343 if (mei_send_msg(mei, mkhi, req_data) < 0)
344 return -1;
345 if (mei_recv_msg(mei, mkhi, rsp_data, rsp_bytes) < 0)
346 return -1;
347 return 0;
348}
349
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700350#ifdef __SMM__
Stefan Reinauer8e073822012-04-04 00:07:22 +0200351/* Send END OF POST message to the ME */
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700352static int mkhi_end_of_post(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200353{
354 struct mkhi_header mkhi = {
355 .group_id = MKHI_GROUP_ID_GEN,
356 .command = MKHI_END_OF_POST,
357 };
358 struct mei_header mei = {
359 .is_complete = 1,
360 .host_address = MEI_HOST_ADDRESS,
361 .client_address = MEI_ADDRESS_MKHI,
362 .length = sizeof(mkhi),
363 };
364
365 /* Send request and wait for response */
366 if (mei_sendrecv(&mei, &mkhi, NULL, NULL, 0) < 0) {
367 printk(BIOS_ERR, "ME: END OF POST message failed\n");
368 return -1;
369 }
370
371 printk(BIOS_INFO, "ME: END OF POST message successful\n");
372 return 0;
373}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700374#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200375
376#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) && !defined(__SMM__)
377/* Get ME firmware version */
378static int mkhi_get_fw_version(void)
379{
380 struct me_fw_version version;
381 struct mkhi_header mkhi = {
382 .group_id = MKHI_GROUP_ID_GEN,
383 .command = MKHI_GET_FW_VERSION,
384 };
385 struct mei_header mei = {
386 .is_complete = 1,
387 .host_address = MEI_HOST_ADDRESS,
388 .client_address = MEI_ADDRESS_MKHI,
389 .length = sizeof(mkhi),
390 };
391
392 /* Send request and wait for response */
393 if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) {
394 printk(BIOS_ERR, "ME: GET FW VERSION message failed\n");
395 return -1;
396 }
397
398 printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) "
399 "%u.%u.%u.%u (recovery)\n",
400 version.code_major, version.code_minor,
401 version.code_build_number, version.code_hot_fix,
402 version.recovery_major, version.recovery_minor,
403 version.recovery_build_number, version.recovery_hot_fix);
404
405 return 0;
406}
407
408static inline void print_cap(const char *name, int state)
409{
410 printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n",
411 name, state ? "en" : "dis");
412}
413
414/* Get ME Firmware Capabilities */
415static int mkhi_get_fwcaps(void)
416{
417 u32 rule_id = 0;
418 struct me_fwcaps cap;
419 struct mkhi_header mkhi = {
420 .group_id = MKHI_GROUP_ID_FWCAPS,
421 .command = MKHI_FWCAPS_GET_RULE,
422 };
423 struct mei_header mei = {
424 .is_complete = 1,
425 .host_address = MEI_HOST_ADDRESS,
426 .client_address = MEI_ADDRESS_MKHI,
427 .length = sizeof(mkhi) + sizeof(rule_id),
428 };
429
430 /* Send request and wait for response */
431 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) {
432 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
433 return -1;
434 }
435
436 print_cap("Full Network manageability", cap.caps_sku.full_net);
437 print_cap("Regular Network manageability", cap.caps_sku.std_net);
438 print_cap("Manageability", cap.caps_sku.manageability);
439 print_cap("Small business technology", cap.caps_sku.small_business);
440 print_cap("Level III manageability", cap.caps_sku.l3manageability);
441 print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at);
442 print_cap("IntelR Capability Licensing Service (CLS)",
443 cap.caps_sku.intel_cls);
444 print_cap("IntelR Power Sharing Technology (MPC)",
445 cap.caps_sku.intel_mpc);
446 print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200447 print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200448 print_cap("IPV6", cap.caps_sku.ipv6);
449 print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm);
450 print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och);
451 print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan);
452 print_cap("TLS", cap.caps_sku.tls);
453 print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan);
454
455 return 0;
456}
457#endif
458
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600459#if IS_ENABLED(CONFIG_CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200460/* Tell ME to issue a global reset */
461int mkhi_global_reset(void)
462{
463 struct me_global_reset reset = {
464 .request_origin = GLOBAL_RESET_BIOS_POST,
465 .reset_type = CBM_RR_GLOBAL_RESET,
466 };
467 struct mkhi_header mkhi = {
468 .group_id = MKHI_GROUP_ID_CBM,
469 .command = MKHI_GLOBAL_RESET,
470 };
471 struct mei_header mei = {
472 .is_complete = 1,
473 .length = sizeof(mkhi) + sizeof(reset),
474 .host_address = MEI_HOST_ADDRESS,
475 .client_address = MEI_ADDRESS_MKHI,
476 };
477
478 printk(BIOS_NOTICE, "ME: Requesting global reset\n");
479
480 /* Send request and wait for response */
481 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
482 /* No response means reset will happen shortly... */
Patrick Georgi546953c2014-11-29 10:38:17 +0100483 halt();
Stefan Reinauer8e073822012-04-04 00:07:22 +0200484 }
485
486 /* If the ME responded it rejected the reset request */
487 printk(BIOS_ERR, "ME: Global Reset failed\n");
488 return -1;
489}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700490#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200491
492#ifdef __SMM__
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700493static void intel_me7_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200494{
495 struct me_hfs hfs;
496 u32 reg32;
497
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800498 mei_base_address = (u32 *)
499 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200500
501 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800502 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200503 return;
504
505 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300506 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200507 memcpy(&hfs, &reg32, sizeof(u32));
508
509 /* Abort and leave device alone if not normal mode */
510 if (hfs.fpt_bad ||
511 hfs.working_state != ME_HFS_CWS_NORMAL ||
512 hfs.operation_mode != ME_HFS_MODE_NORMAL)
513 return;
514
515 /* Try to send EOP command so ME stops accepting other commands */
516 mkhi_end_of_post();
517
518 /* Make sure IO is disabled */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300519 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200520 reg32 &= ~(PCI_COMMAND_MASTER |
521 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300522 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200523
524 /* Hide the PCI device */
525 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
526}
527
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700528void intel_me_finalize_smm(void)
529{
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300530 u32 did = pci_read_config32(PCH_ME_DEV, PCI_VENDOR_ID);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700531 switch (did) {
Duncan Laurie708f7312012-07-10 15:15:41 -0700532 case 0x1c3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700533 intel_me7_finalize_smm();
534 break;
Duncan Laurie708f7312012-07-10 15:15:41 -0700535 case 0x1e3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700536 intel_me8_finalize_smm();
537 break;
538 default:
539 printk(BIOS_ERR, "No finalize handler for ME %08x.\n", did);
540 }
541}
Stefan Reinauer8e073822012-04-04 00:07:22 +0200542#else /* !__SMM__ */
543
544/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200545static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200546{
547 me_bios_path path = ME_DISABLE_BIOS_PATH;
548 struct me_hfs hfs;
549 struct me_gmes gmes;
550
Stefan Reinauer8e073822012-04-04 00:07:22 +0200551 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300552 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200553 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200554
555 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
556 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
557
558 /* Check and dump status */
559 intel_me_status(&hfs, &gmes);
560
Stefan Reinauer8e073822012-04-04 00:07:22 +0200561 /* Check Current Working State */
562 switch (hfs.working_state) {
563 case ME_HFS_CWS_NORMAL:
564 path = ME_NORMAL_BIOS_PATH;
565 break;
566 case ME_HFS_CWS_REC:
567 path = ME_RECOVERY_BIOS_PATH;
568 break;
569 default:
570 path = ME_DISABLE_BIOS_PATH;
571 break;
572 }
573
574 /* Check Current Operation Mode */
575 switch (hfs.operation_mode) {
576 case ME_HFS_MODE_NORMAL:
577 break;
578 case ME_HFS_MODE_DEBUG:
579 case ME_HFS_MODE_DIS:
580 case ME_HFS_MODE_OVER_JMPR:
581 case ME_HFS_MODE_OVER_MEI:
582 default:
583 path = ME_DISABLE_BIOS_PATH;
584 break;
585 }
586
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700587 /* Check for any error code and valid firmware */
588 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200589 path = ME_ERROR_BIOS_PATH;
590
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600591#if IS_ENABLED(CONFIG_ELOG)
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700592 if (path != ME_NORMAL_BIOS_PATH) {
593 struct elog_event_data_me_extended data = {
594 .current_working_state = hfs.working_state,
595 .operation_state = hfs.operation_state,
596 .operation_mode = hfs.operation_mode,
597 .error_code = hfs.error_code,
598 .progress_code = gmes.progress_code,
599 .current_pmevent = gmes.current_pmevent,
600 .current_state = gmes.current_state,
601 };
602 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
603 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
604 &data, sizeof(data));
605 }
606#endif
607
Stefan Reinauer8e073822012-04-04 00:07:22 +0200608 return path;
609}
610
611/* Prepare ME for MEI messages */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200612static int intel_mei_setup(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200613{
614 struct resource *res;
615 struct mei_csr host;
616 u32 reg32;
617
618 /* Find the MMIO base for the ME interface */
619 res = find_resource(dev, PCI_BASE_ADDRESS_0);
620 if (!res || res->base == 0 || res->size == 0) {
621 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
622 return -1;
623 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800624 mei_base_address = (u32*)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200625
626 /* Ensure Memory and Bus Master bits are set */
627 reg32 = pci_read_config32(dev, PCI_COMMAND);
628 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
629 pci_write_config32(dev, PCI_COMMAND, reg32);
630
631 /* Clean up status for next message */
632 read_host_csr(&host);
633 host.interrupt_generate = 1;
634 host.ready = 1;
635 host.reset = 0;
636 write_host_csr(&host);
637
638 return 0;
639}
640
641/* Read the Extend register hash of ME firmware */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200642static int intel_me_extend_valid(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200643{
644 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700645 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200646 int i, count = 0;
647
648 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
649 if (!status.extend_feature_present) {
650 printk(BIOS_ERR, "ME: Extend Feature not present\n");
651 return -1;
652 }
653
654 if (!status.extend_reg_valid) {
655 printk(BIOS_ERR, "ME: Extend Register not valid\n");
656 return -1;
657 }
658
659 switch (status.extend_reg_algorithm) {
660 case PCI_ME_EXT_SHA1:
661 count = 5;
662 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
663 break;
664 case PCI_ME_EXT_SHA256:
665 count = 8;
666 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
667 break;
668 default:
669 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
670 status.extend_reg_algorithm);
671 return -1;
672 }
673
674 for (i = 0; i < count; ++i) {
675 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
676 printk(BIOS_DEBUG, "%08x", extend[i]);
677 }
678 printk(BIOS_DEBUG, "\n");
679
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600680#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200681 /* Save hash in NVS for the OS to verify */
682 chromeos_set_me_hash(extend, count);
683#endif
684
685 return 0;
686}
687
688/* Hide the ME virtual PCI devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200689static void intel_me_hide(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200690{
691 dev->enabled = 0;
692 pch_enable(dev);
693}
694
695/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200696static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200697{
698 me_bios_path path = intel_me_path(dev);
699
700 /* Do initial setup and determine the BIOS path */
701 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
702
703 switch (path) {
704 case ME_S3WAKE_BIOS_PATH:
705 intel_me_hide(dev);
706 break;
707
708 case ME_NORMAL_BIOS_PATH:
709 /* Validate the extend register */
710 if (intel_me_extend_valid(dev) < 0)
711 break; /* TODO: force recovery mode */
712
713 /* Prepare MEI MMIO interface */
714 if (intel_mei_setup(dev) < 0)
715 break;
716
717#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
718 /* Print ME firmware version */
719 mkhi_get_fw_version();
720 /* Print ME firmware capabilities */
721 mkhi_get_fwcaps();
722#endif
723
724 /*
725 * Leave the ME unlocked in this path.
726 * It will be locked via SMI command later.
727 */
728 break;
729
730 case ME_ERROR_BIOS_PATH:
731 case ME_RECOVERY_BIOS_PATH:
732 case ME_DISABLE_BIOS_PATH:
733 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200734 break;
735 }
736}
737
Elyes HAOUASdc035282018-09-18 13:28:49 +0200738static void set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200739{
740 if (!vendor || !device) {
741 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
742 pci_read_config32(dev, PCI_VENDOR_ID));
743 } else {
744 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
745 ((device & 0xffff) << 16) | (vendor & 0xffff));
746 }
747}
748
749static struct pci_operations pci_ops = {
750 .set_subsystem = set_subsystem,
751};
752
753static struct device_operations device_ops = {
754 .read_resources = pci_dev_read_resources,
755 .set_resources = pci_dev_set_resources,
756 .enable_resources = pci_dev_enable_resources,
757 .init = intel_me_init,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200758 .ops_pci = &pci_ops,
759};
760
761static const struct pci_driver intel_me __pci_driver = {
762 .ops = &device_ops,
763 .vendor = PCI_VENDOR_ID_INTEL,
764 .device = 0x1c3a,
765};
766
767#endif /* !__SMM__ */