blob: 54a16ce622fb3d09c51fad4dd484f4ae72511f16 [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>
Stefan Reinauer8e073822012-04-04 00:07:22 +020026#include <arch/io.h>
27#include <console/console.h>
28#include <device/pci_ids.h>
29#include <device/pci_def.h>
30#include <string.h>
31#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070032#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010033#include <halt.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020034
35#ifdef __SMM__
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +030036#include <arch/io.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020037#else
38# include <device/device.h>
39# include <device/pci.h>
40#endif
41
Arthur Heymansd2d2aef2018-01-16 14:19:37 +010042#include <southbridge/intel/common/rcba.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020043#include "me.h"
44#include "pch.h"
45
Martin Roth7a1a3ad2017-06-24 21:29:38 -060046#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020047#include <vendorcode/google/chromeos/chromeos.h>
Stefan Reinauer49058c02012-06-11 14:13:09 -070048#include <vendorcode/google/chromeos/gnvs.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020049#endif
50
51#ifndef __SMM__
52/* Path that the BIOS should take based on ME state */
53static const char *me_bios_path_values[] = {
54 [ME_NORMAL_BIOS_PATH] = "Normal",
55 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
56 [ME_ERROR_BIOS_PATH] = "Error",
57 [ME_RECOVERY_BIOS_PATH] = "Recovery",
58 [ME_DISABLE_BIOS_PATH] = "Disable",
59 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
60};
61static int intel_me_read_mbp(me_bios_payload *mbp_data);
62#endif
63
64/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080065static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020066
Martin Roth7a1a3ad2017-06-24 21:29:38 -060067#if IS_ENABLED(CONFIG_DEBUG_INTEL_ME)
Stefan Reinauer8e073822012-04-04 00:07:22 +020068static void mei_dump(void *ptr, int dword, int offset, const char *type)
69{
70 struct mei_csr *csr;
71
72 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
73
74 switch (offset) {
75 case MEI_H_CSR:
76 case MEI_ME_CSR_HA:
77 csr = ptr;
78 if (!csr) {
79 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
80 break;
81 }
82 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
83 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
84 csr->buffer_read_ptr, csr->buffer_write_ptr,
85 csr->ready, csr->reset, csr->interrupt_generate,
86 csr->interrupt_status, csr->interrupt_enable);
87 break;
88 case MEI_ME_CB_RW:
89 case MEI_H_CB_WW:
90 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
91 break;
92 default:
93 printk(BIOS_SPEW, "0x%08x\n", offset);
94 break;
95 }
96}
97#else
98# define mei_dump(ptr,dword,offset,type) do {} while (0)
99#endif
100
101/*
102 * ME/MEI access helpers using memcpy to avoid aliasing.
103 */
104
105static inline void mei_read_dword_ptr(void *ptr, int offset)
106{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800107 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200108 memcpy(ptr, &dword, sizeof(dword));
109 mei_dump(ptr, dword, offset, "READ");
110}
111
112static inline void mei_write_dword_ptr(void *ptr, int offset)
113{
114 u32 dword = 0;
115 memcpy(&dword, ptr, sizeof(dword));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800116 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200117 mei_dump(ptr, dword, offset, "WRITE");
118}
119
120#ifndef __SMM__
121static inline void pci_read_dword_ptr(device_t dev, void *ptr, int offset)
122{
123 u32 dword = pci_read_config32(dev, offset);
124 memcpy(ptr, &dword, sizeof(dword));
125 mei_dump(ptr, dword, offset, "PCI READ");
126}
127#endif
128
129static inline void read_host_csr(struct mei_csr *csr)
130{
131 mei_read_dword_ptr(csr, MEI_H_CSR);
132}
133
134static inline void write_host_csr(struct mei_csr *csr)
135{
136 mei_write_dword_ptr(csr, MEI_H_CSR);
137}
138
139static inline void read_me_csr(struct mei_csr *csr)
140{
141 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
142}
143
144static inline void write_cb(u32 dword)
145{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800146 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200147 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
148}
149
150static inline u32 read_cb(void)
151{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800152 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200153 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
154 return dword;
155}
156
157/* Wait for ME ready bit to be asserted */
158static int mei_wait_for_me_ready(void)
159{
160 struct mei_csr me;
161 unsigned try = ME_RETRY;
162
163 while (try--) {
164 read_me_csr(&me);
165 if (me.ready)
166 return 0;
167 udelay(ME_DELAY);
168 }
169
170 printk(BIOS_ERR, "ME: failed to become ready\n");
171 return -1;
172}
173
174static void mei_reset(void)
175{
176 struct mei_csr host;
177
178 if (mei_wait_for_me_ready() < 0)
179 return;
180
181 /* Reset host and ME circular buffers for next message */
182 read_host_csr(&host);
183 host.reset = 1;
184 host.interrupt_generate = 1;
185 write_host_csr(&host);
186
187 if (mei_wait_for_me_ready() < 0)
188 return;
189
190 /* Re-init and indicate host is ready */
191 read_host_csr(&host);
192 host.interrupt_generate = 1;
193 host.ready = 1;
194 host.reset = 0;
195 write_host_csr(&host);
196}
197
198static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
199 void *req_data)
200{
201 struct mei_csr host;
202 unsigned ndata, n;
203 u32 *data;
204
205 /* Number of dwords to write, ignoring MKHI */
206 ndata = mei->length >> 2;
207
208 /* Pad non-dword aligned request message length */
209 if (mei->length & 3)
210 ndata++;
211 if (!ndata) {
212 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
213 return -1;
214 }
215 ndata++; /* Add MEI header */
216
217 /*
218 * Make sure there is still room left in the circular buffer.
219 * Reset the buffer pointers if the requested message will not fit.
220 */
221 read_host_csr(&host);
222 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
223 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
224 mei_reset();
225 read_host_csr(&host);
226 }
227
228 /*
229 * This implementation does not handle splitting large messages
230 * across multiple transactions. Ensure the requested length
231 * will fit in the available circular buffer depth.
232 */
233 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
234 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
235 ndata + 2, host.buffer_depth);
236 return -1;
237 }
238
239 /* Write MEI header */
240 mei_write_dword_ptr(mei, MEI_H_CB_WW);
241 ndata--;
242
243 /* Write MKHI header */
244 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
245 ndata--;
246
247 /* Write message data */
248 data = req_data;
249 for (n = 0; n < ndata; ++n)
250 write_cb(*data++);
251
252 /* Generate interrupt to the ME */
253 read_host_csr(&host);
254 host.interrupt_generate = 1;
255 write_host_csr(&host);
256
257 /* Make sure ME is ready after sending request data */
258 return mei_wait_for_me_ready();
259}
260
261static int mei_recv_msg(struct mkhi_header *mkhi,
262 void *rsp_data, int rsp_bytes)
263{
264 struct mei_header mei_rsp;
265 struct mkhi_header mkhi_rsp;
266 struct mei_csr me, host;
267 unsigned ndata, n/*, me_data_len*/;
268 unsigned expected;
269 u32 *data;
270
271 /* Total number of dwords to read from circular buffer */
272 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
273 if (rsp_bytes & 3)
274 expected++;
275
276 /*
277 * The interrupt status bit does not appear to indicate that the
278 * message has actually been received. Instead we wait until the
279 * expected number of dwords are present in the circular buffer.
280 */
281 for (n = ME_RETRY; n; --n) {
282 read_me_csr(&me);
283 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
284 break;
285 udelay(ME_DELAY);
286 }
287 if (!n) {
288 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
289 "%u, available %u\n", expected,
290 me.buffer_write_ptr - me.buffer_read_ptr);
291 return -1;
292 }
293
294 /* Read and verify MEI response header from the ME */
295 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
296 if (!mei_rsp.is_complete) {
297 printk(BIOS_ERR, "ME: response is not complete\n");
298 return -1;
299 }
300
301 /* Handle non-dword responses and expect at least MKHI header */
302 ndata = mei_rsp.length >> 2;
303 if (mei_rsp.length & 3)
304 ndata++;
305 if (ndata != (expected - 1)) {
306 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
307 ndata, (expected - 1));
308 return -1;
309 }
310
311 /* Read and verify MKHI response header from the ME */
312 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
313 if (!mkhi_rsp.is_response ||
314 mkhi->group_id != mkhi_rsp.group_id ||
315 mkhi->command != mkhi_rsp.command) {
316 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u,"
317 "command %u ?= %u, is_response %u\n", mkhi->group_id,
318 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
319 mkhi_rsp.is_response);
320 return -1;
321 }
322 ndata--; /* MKHI header has been read */
323
324 /* Make sure caller passed a buffer with enough space */
325 if (ndata != (rsp_bytes >> 2)) {
326 printk(BIOS_ERR, "ME: not enough room in response buffer: "
327 "%u != %u\n", ndata, rsp_bytes >> 2);
328 return -1;
329 }
330
331 /* Read response data from the circular buffer */
332 data = rsp_data;
333 for (n = 0; n < ndata; ++n)
334 *data++ = read_cb();
335
336 /* Tell the ME that we have consumed the response */
337 read_host_csr(&host);
338 host.interrupt_status = 1;
339 host.interrupt_generate = 1;
340 write_host_csr(&host);
341
342 return mei_wait_for_me_ready();
343}
344
345static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
346 void *req_data, void *rsp_data, int rsp_bytes)
347{
348 if (mei_send_msg(mei, mkhi, req_data) < 0)
349 return -1;
350 if (mei_recv_msg(mkhi, rsp_data, rsp_bytes) < 0)
351 return -1;
352 return 0;
353}
354
Stefan Reinauer8e073822012-04-04 00:07:22 +0200355#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) && !defined(__SMM__)
356static inline void print_cap(const char *name, int state)
357{
358 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
359 name, state ? " en" : "dis");
360}
361
362static void me_print_fw_version(mbp_fw_version_name *vers_name)
363{
364 if (!vers_name->major_version) {
365 printk(BIOS_ERR, "ME: mbp missing version report\n");
366 return;
367 }
368
369 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
370 vers_name->major_version, vers_name->minor_version,
371 vers_name->hotfix_version, vers_name->build_version);
372}
373
374/* Get ME Firmware Capabilities */
375static int mkhi_get_fwcaps(mefwcaps_sku *cap)
376{
377 u32 rule_id = 0;
378 struct me_fwcaps cap_msg;
379 struct mkhi_header mkhi = {
380 .group_id = MKHI_GROUP_ID_FWCAPS,
381 .command = MKHI_FWCAPS_GET_RULE,
382 };
383 struct mei_header mei = {
384 .is_complete = 1,
385 .host_address = MEI_HOST_ADDRESS,
386 .client_address = MEI_ADDRESS_MKHI,
387 .length = sizeof(mkhi) + sizeof(rule_id),
388 };
389
390 /* Send request and wait for response */
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000391 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg)) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200392 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
393 return -1;
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000394 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200395 *cap = cap_msg.caps_sku;
396 return 0;
397}
398
399/* Get ME Firmware Capabilities */
400static void me_print_fwcaps(mbp_fw_caps *caps_section)
401{
402 mefwcaps_sku *cap = &caps_section->fw_capabilities;
403 if (!caps_section->available) {
404 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
405 if (mkhi_get_fwcaps(cap))
406 return;
407 }
408
409 print_cap("Full Network manageability", cap->full_net);
410 print_cap("Regular Network manageability", cap->std_net);
411 print_cap("Manageability", cap->manageability);
412 print_cap("Small business technology", cap->small_business);
413 print_cap("Level III manageability", cap->l3manageability);
414 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
415 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
416 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
417 print_cap("ICC Over Clocking", cap->icc_over_clocking);
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000418 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200419 print_cap("IPV6", cap->ipv6);
420 print_cap("KVM Remote Control (KVM)", cap->kvm);
421 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
422 print_cap("Virtual LAN (VLAN)", cap->vlan);
423 print_cap("TLS", cap->tls);
424 print_cap("Wireless LAN (WLAN)", cap->wlan);
425}
426#endif
427
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600428#if IS_ENABLED(CONFIG_CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200429/* Tell ME to issue a global reset */
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700430static int mkhi_global_reset(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200431{
432 struct me_global_reset reset = {
433 .request_origin = GLOBAL_RESET_BIOS_POST,
434 .reset_type = CBM_RR_GLOBAL_RESET,
435 };
436 struct mkhi_header mkhi = {
437 .group_id = MKHI_GROUP_ID_CBM,
438 .command = MKHI_GLOBAL_RESET,
439 };
440 struct mei_header mei = {
441 .is_complete = 1,
442 .length = sizeof(mkhi) + sizeof(reset),
443 .host_address = MEI_HOST_ADDRESS,
444 .client_address = MEI_ADDRESS_MKHI,
445 };
446
447 /* Send request and wait for response */
448 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
449 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
450 /* No response means reset will happen shortly... */
Patrick Georgi546953c2014-11-29 10:38:17 +0100451 halt();
Stefan Reinauer8e073822012-04-04 00:07:22 +0200452 }
453
454 /* If the ME responded it rejected the reset request */
455 printk(BIOS_ERR, "ME: Global Reset failed\n");
456 return -1;
457}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700458#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200459
460#ifdef __SMM__
461
Duncan Laurie708f7312012-07-10 15:15:41 -0700462/* Send END OF POST message to the ME */
463static int mkhi_end_of_post(void)
464{
465 struct mkhi_header mkhi = {
466 .group_id = MKHI_GROUP_ID_GEN,
467 .command = MKHI_END_OF_POST,
468 };
469 struct mei_header mei = {
470 .is_complete = 1,
471 .host_address = MEI_HOST_ADDRESS,
472 .client_address = MEI_ADDRESS_MKHI,
473 .length = sizeof(mkhi),
474 };
475
476 u32 eop_ack;
477
478 /* Send request and wait for response */
479 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
480 if (mei_sendrecv(&mei, &mkhi, NULL, &eop_ack, sizeof(eop_ack)) < 0) {
481 printk(BIOS_ERR, "ME: END OF POST message failed\n");
482 return -1;
483 }
484
485 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
486 return 0;
487}
488
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700489void intel_me8_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200490{
491 struct me_hfs hfs;
492 u32 reg32;
493
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800494 mei_base_address = (void *)
495 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200496
497 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800498 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200499 return;
500
501 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300502 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200503 memcpy(&hfs, &reg32, sizeof(u32));
504
505 /* Abort and leave device alone if not normal mode */
506 if (hfs.fpt_bad ||
507 hfs.working_state != ME_HFS_CWS_NORMAL ||
508 hfs.operation_mode != ME_HFS_MODE_NORMAL)
509 return;
510
511 /* Try to send EOP command so ME stops accepting other commands */
512 mkhi_end_of_post();
513
514 /* Make sure IO is disabled */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300515 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200516 reg32 &= ~(PCI_COMMAND_MASTER |
517 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300518 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200519
520 /* Hide the PCI device */
521 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
522}
523
524#else /* !__SMM__ */
525
526/* Determine the path that we should take based on ME status */
527static me_bios_path intel_me_path(device_t dev)
528{
529 me_bios_path path = ME_DISABLE_BIOS_PATH;
530 struct me_hfs hfs;
531 struct me_gmes gmes;
532
Stefan Reinauer8e073822012-04-04 00:07:22 +0200533 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300534 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200535 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200536
537 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
538 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
539
540 /* Check and dump status */
541 intel_me_status(&hfs, &gmes);
542
Stefan Reinauer8e073822012-04-04 00:07:22 +0200543 /* Check Current Working State */
544 switch (hfs.working_state) {
545 case ME_HFS_CWS_NORMAL:
546 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200547 break;
548 case ME_HFS_CWS_REC:
549 path = ME_RECOVERY_BIOS_PATH;
550 break;
551 default:
552 path = ME_DISABLE_BIOS_PATH;
553 break;
554 }
555
556 /* Check Current Operation Mode */
557 switch (hfs.operation_mode) {
558 case ME_HFS_MODE_NORMAL:
559 break;
560 case ME_HFS_MODE_DEBUG:
561 case ME_HFS_MODE_DIS:
562 case ME_HFS_MODE_OVER_JMPR:
563 case ME_HFS_MODE_OVER_MEI:
564 default:
565 path = ME_DISABLE_BIOS_PATH;
566 break;
567 }
568
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700569 /* Check for any error code and valid firmware and MBP */
570 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200571 path = ME_ERROR_BIOS_PATH;
572
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700573 /* Check if the MBP is ready */
574 if (!gmes.mbp_rdy) {
575 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
576 __FUNCTION__);
577 path = ME_ERROR_BIOS_PATH;
578 }
579
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600580#if IS_ENABLED(CONFIG_ELOG)
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700581 if (path != ME_NORMAL_BIOS_PATH) {
582 struct elog_event_data_me_extended data = {
583 .current_working_state = hfs.working_state,
584 .operation_state = hfs.operation_state,
585 .operation_mode = hfs.operation_mode,
586 .error_code = hfs.error_code,
587 .progress_code = gmes.progress_code,
588 .current_pmevent = gmes.current_pmevent,
589 .current_state = gmes.current_state,
590 };
591 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
592 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
593 &data, sizeof(data));
594 }
595#endif
596
Stefan Reinauer8e073822012-04-04 00:07:22 +0200597 return path;
598}
599
600/* Prepare ME for MEI messages */
601static int intel_mei_setup(device_t dev)
602{
603 struct resource *res;
604 struct mei_csr host;
605 u32 reg32;
606
607 /* Find the MMIO base for the ME interface */
608 res = find_resource(dev, PCI_BASE_ADDRESS_0);
609 if (!res || res->base == 0 || res->size == 0) {
610 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
611 return -1;
612 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800613 mei_base_address = (u32 *)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200614
615 /* Ensure Memory and Bus Master bits are set */
616 reg32 = pci_read_config32(dev, PCI_COMMAND);
617 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
618 pci_write_config32(dev, PCI_COMMAND, reg32);
619
620 /* Clean up status for next message */
621 read_host_csr(&host);
622 host.interrupt_generate = 1;
623 host.ready = 1;
624 host.reset = 0;
625 write_host_csr(&host);
626
627 return 0;
628}
629
630/* Read the Extend register hash of ME firmware */
631static int intel_me_extend_valid(device_t dev)
632{
633 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700634 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200635 int i, count = 0;
636
637 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
638 if (!status.extend_feature_present) {
639 printk(BIOS_ERR, "ME: Extend Feature not present\n");
640 return -1;
641 }
642
643 if (!status.extend_reg_valid) {
644 printk(BIOS_ERR, "ME: Extend Register not valid\n");
645 return -1;
646 }
647
648 switch (status.extend_reg_algorithm) {
649 case PCI_ME_EXT_SHA1:
650 count = 5;
651 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
652 break;
653 case PCI_ME_EXT_SHA256:
654 count = 8;
655 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
656 break;
657 default:
658 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
659 status.extend_reg_algorithm);
660 return -1;
661 }
662
Stefan Reinauer8e073822012-04-04 00:07:22 +0200663 for (i = 0; i < count; ++i) {
Stefan Reinauer49058c02012-06-11 14:13:09 -0700664 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
665 printk(BIOS_DEBUG, "%08x", extend[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200666 }
667 printk(BIOS_DEBUG, "\n");
668
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600669#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer49058c02012-06-11 14:13:09 -0700670 /* Save hash in NVS for the OS to verify */
671 chromeos_set_me_hash(extend, count);
672#endif
673
Stefan Reinauer8e073822012-04-04 00:07:22 +0200674 return 0;
675}
676
677/* Hide the ME virtual PCI devices */
678static void intel_me_hide(device_t dev)
679{
680 dev->enabled = 0;
681 pch_enable(dev);
682}
683
684/* Check whether ME is present and do basic init */
685static void intel_me_init(device_t dev)
686{
687 me_bios_path path = intel_me_path(dev);
688 me_bios_payload mbp_data;
689
690 /* Do initial setup and determine the BIOS path */
691 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
692
693 switch (path) {
694 case ME_S3WAKE_BIOS_PATH:
695 intel_me_hide(dev);
696 break;
697
698 case ME_NORMAL_BIOS_PATH:
699 /* Validate the extend register */
700 if (intel_me_extend_valid(dev) < 0)
701 break; /* TODO: force recovery mode */
702
703 /* Prepare MEI MMIO interface */
704 if (intel_mei_setup(dev) < 0)
705 break;
706
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200707 if (intel_me_read_mbp(&mbp_data))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200708 break;
709
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600710#if IS_ENABLED(CONFIG_CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200711 /*
712 * Unlock ME in recovery mode.
713 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700714 if (vboot_recovery_mode_enabled()) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200715 /* Unlock ME flash region */
716 mkhi_hmrfpo_enable();
717
718 /* Issue global reset */
719 mkhi_global_reset();
720 return;
721 }
722#endif
723
724#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
725 me_print_fw_version(&mbp_data.fw_version_name);
726 me_print_fwcaps(&mbp_data.fw_caps_sku);
727#endif
Duncan Laurie708f7312012-07-10 15:15:41 -0700728
729 /*
730 * Leave the ME unlocked in this path.
731 * It will be locked via SMI command later.
732 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200733 break;
734
735 case ME_ERROR_BIOS_PATH:
736 case ME_RECOVERY_BIOS_PATH:
737 case ME_DISABLE_BIOS_PATH:
738 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200739 break;
740 }
741}
742
743static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
744{
745 if (!vendor || !device) {
746 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
747 pci_read_config32(dev, PCI_VENDOR_ID));
748 } else {
749 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
750 ((device & 0xffff) << 16) | (vendor & 0xffff));
751 }
752}
753
754static struct pci_operations pci_ops = {
755 .set_subsystem = set_subsystem,
756};
757
758static struct device_operations device_ops = {
759 .read_resources = pci_dev_read_resources,
760 .set_resources = pci_dev_set_resources,
761 .enable_resources = pci_dev_enable_resources,
762 .init = intel_me_init,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200763 .ops_pci = &pci_ops,
764};
765
766static const struct pci_driver intel_me __pci_driver = {
767 .ops = &device_ops,
768 .vendor = PCI_VENDOR_ID_INTEL,
769 .device = 0x1e3a,
770};
771
772/******************************************************************************
773 * */
774static u32 me_to_host_words_pending(void)
775{
776 struct mei_csr me;
777 read_me_csr(&me);
778 if (!me.ready)
779 return 0;
780 return (me.buffer_write_ptr - me.buffer_read_ptr) &
781 (me.buffer_depth - 1);
782}
783
784#if 0
785/* This function is not yet being used, keep it in for the future. */
786static u32 host_to_me_words_room(void)
787{
788 struct mei_csr csr;
789
790 read_me_csr(&csr);
791 if (!csr.ready)
792 return 0;
793
794 read_host_csr(&csr);
795 return (csr.buffer_read_ptr - csr.buffer_write_ptr - 1) &
796 (csr.buffer_depth - 1);
797}
798#endif
799/*
800 * mbp seems to be following its own flow, let's retrieve it in a dedicated
801 * function.
802 */
803static int intel_me_read_mbp(me_bios_payload *mbp_data)
804{
805 mbp_header mbp_hdr;
806 mbp_item_header mbp_item_hdr;
807 u32 me2host_pending;
808 u32 mbp_item_id;
809 struct mei_csr host;
810
811 me2host_pending = me_to_host_words_pending();
812 if (!me2host_pending) {
813 printk(BIOS_ERR, "ME: no mbp data!\n");
814 return -1;
815 }
816
817 /* we know for sure that at least the header is there */
818 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
819
820 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
821 (me2host_pending < mbp_hdr.mbp_size)) {
822 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
823 " buffer contains %d words\n",
824 mbp_hdr.num_entries, mbp_hdr.mbp_size,
825 me2host_pending);
826 return -1;
827 }
828
829 me2host_pending--;
830 memset(mbp_data, 0, sizeof(*mbp_data));
831
832 while (mbp_hdr.num_entries--) {
833 u32* copy_addr;
834 u32 copy_size, buffer_room;
835 void *p;
836
837 if (!me2host_pending) {
838 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
839 mbp_hdr.num_entries + 1);
840 return -1;
841 }
842
843 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
844
845 if (mbp_item_hdr.length > me2host_pending) {
846 printk(BIOS_ERR, "ME: insufficient mbp data %d "
847 "entries to go!\n",
848 mbp_hdr.num_entries + 1);
849 return -1;
850 }
851
852 me2host_pending -= mbp_item_hdr.length;
853
854 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
855 mbp_item_hdr.app_id;
856
857 copy_size = mbp_item_hdr.length - 1;
858
859#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
860 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
861 break; \
862 }
863
864 p = &mbp_item_hdr;
865 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
866
Elyes HAOUASf9de5a42018-05-03 17:21:02 +0200867 switch (mbp_item_id) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200868 case 0x101:
869 SET_UP_COPY(fw_version_name);
870
871 case 0x102:
872 SET_UP_COPY(icc_profile);
873
874 case 0x103:
875 SET_UP_COPY(at_state);
876
877 case 0x201:
878 mbp_data->fw_caps_sku.available = 1;
879 SET_UP_COPY(fw_caps_sku.fw_capabilities);
880
881 case 0x301:
882 SET_UP_COPY(rom_bist_data);
883
884 case 0x401:
885 SET_UP_COPY(platform_key);
886
887 case 0x501:
888 mbp_data->fw_plat_type.available = 1;
889 SET_UP_COPY(fw_plat_type.rule_data);
890
891 case 0x601:
892 SET_UP_COPY(mfsintegrity);
893
894 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000895 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200896 mbp_item_id);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200897 while (copy_size--)
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000898 read_cb();
899 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200900 }
901
902 if (buffer_room != copy_size) {
903 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
904 " for item 0x%x!!!\n",
905 buffer_room, copy_size, mbp_item_id);
906 return -1;
907 }
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200908 while (copy_size--)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200909 *copy_addr++ = read_cb();
910 }
911
912 read_host_csr(&host);
913 host.interrupt_generate = 1;
914 write_host_csr(&host);
915
916 {
917 int cntr = 0;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200918 while (host.interrupt_generate) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200919 read_host_csr(&host);
920 cntr++;
921 }
922 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
923 }
924
925 return 0;
926}
927
928#endif /* !__SMM__ */