blob: 12f7ab7ed22399a098359c041c049d7e3ce56b16 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22/*
23 * This is a ramstage driver for the Intel Management Engine found in the
24 * 6-series chipset. It handles the required boot-time messages over the
25 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
26 * finished with POST. Additional messages are defined for debug but are
27 * not used unless the console loglevel is high enough.
28 */
29
30#include <arch/acpi.h>
31#include <arch/hlt.h>
32#include <arch/io.h>
33#include <console/console.h>
34#include <device/pci_ids.h>
35#include <device/pci_def.h>
36#include <string.h>
37#include <delay.h>
38#include <elog.h>
39
40#ifdef __SMM__
41# include <arch/romcc_io.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050042#else
43# include <device/device.h>
44# include <device/pci.h>
45#endif
46
47#include "me.h"
48#include "pch.h"
49
50#if CONFIG_CHROMEOS
51#include <vendorcode/google/chromeos/chromeos.h>
52#include <vendorcode/google/chromeos/gnvs.h>
53#endif
54
55#ifndef __SMM__
56/* Path that the BIOS should take based on ME state */
57static const char *me_bios_path_values[] = {
58 [ME_NORMAL_BIOS_PATH] = "Normal",
59 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
60 [ME_ERROR_BIOS_PATH] = "Error",
61 [ME_RECOVERY_BIOS_PATH] = "Recovery",
62 [ME_DISABLE_BIOS_PATH] = "Disable",
63 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
64};
Aaron Durbin9aa031e2012-11-02 09:16:46 -050065static int intel_me_read_mbp(me_bios_payload *mbp_data, device_t dev);
Aaron Durbin76c37002012-10-30 09:03:43 -050066#endif
67
68/* MMIO base address for MEI interface */
69static u32 mei_base_address;
70
71#if CONFIG_DEBUG_INTEL_ME
72static void mei_dump(void *ptr, int dword, int offset, const char *type)
73{
74 struct mei_csr *csr;
75
76 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
77
78 switch (offset) {
79 case MEI_H_CSR:
80 case MEI_ME_CSR_HA:
81 csr = ptr;
82 if (!csr) {
83 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
84 break;
85 }
86 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
87 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
88 csr->buffer_read_ptr, csr->buffer_write_ptr,
89 csr->ready, csr->reset, csr->interrupt_generate,
90 csr->interrupt_status, csr->interrupt_enable);
91 break;
92 case MEI_ME_CB_RW:
93 case MEI_H_CB_WW:
94 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
95 break;
96 default:
97 printk(BIOS_SPEW, "0x%08x\n", offset);
98 break;
99 }
100}
101#else
102# define mei_dump(ptr,dword,offset,type) do {} while (0)
103#endif
104
105/*
106 * ME/MEI access helpers using memcpy to avoid aliasing.
107 */
108
109static inline void mei_read_dword_ptr(void *ptr, int offset)
110{
111 u32 dword = read32(mei_base_address + offset);
112 memcpy(ptr, &dword, sizeof(dword));
113 mei_dump(ptr, dword, offset, "READ");
114}
115
116static inline void mei_write_dword_ptr(void *ptr, int offset)
117{
118 u32 dword = 0;
119 memcpy(&dword, ptr, sizeof(dword));
120 write32(mei_base_address + offset, dword);
121 mei_dump(ptr, dword, offset, "WRITE");
122}
123
124#ifndef __SMM__
125static inline void pci_read_dword_ptr(device_t dev, void *ptr, int offset)
126{
127 u32 dword = pci_read_config32(dev, offset);
128 memcpy(ptr, &dword, sizeof(dword));
129 mei_dump(ptr, dword, offset, "PCI READ");
130}
131#endif
132
133static inline void read_host_csr(struct mei_csr *csr)
134{
135 mei_read_dword_ptr(csr, MEI_H_CSR);
136}
137
138static inline void write_host_csr(struct mei_csr *csr)
139{
140 mei_write_dword_ptr(csr, MEI_H_CSR);
141}
142
143static inline void read_me_csr(struct mei_csr *csr)
144{
145 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
146}
147
148static inline void write_cb(u32 dword)
149{
150 write32(mei_base_address + MEI_H_CB_WW, dword);
151 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
152}
153
154static inline u32 read_cb(void)
155{
156 u32 dword = read32(mei_base_address + MEI_ME_CB_RW);
157 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
158 return dword;
159}
160
161/* Wait for ME ready bit to be asserted */
162static int mei_wait_for_me_ready(void)
163{
164 struct mei_csr me;
165 unsigned try = ME_RETRY;
166
167 while (try--) {
168 read_me_csr(&me);
169 if (me.ready)
170 return 0;
171 udelay(ME_DELAY);
172 }
173
174 printk(BIOS_ERR, "ME: failed to become ready\n");
175 return -1;
176}
177
178static void mei_reset(void)
179{
180 struct mei_csr host;
181
182 if (mei_wait_for_me_ready() < 0)
183 return;
184
185 /* Reset host and ME circular buffers for next message */
186 read_host_csr(&host);
187 host.reset = 1;
188 host.interrupt_generate = 1;
189 write_host_csr(&host);
190
191 if (mei_wait_for_me_ready() < 0)
192 return;
193
194 /* Re-init and indicate host is ready */
195 read_host_csr(&host);
196 host.interrupt_generate = 1;
197 host.ready = 1;
198 host.reset = 0;
199 write_host_csr(&host);
200}
201
202static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
203 void *req_data)
204{
205 struct mei_csr host;
206 unsigned ndata, n;
207 u32 *data;
208
209 /* Number of dwords to write, ignoring MKHI */
210 ndata = mei->length >> 2;
211
212 /* Pad non-dword aligned request message length */
213 if (mei->length & 3)
214 ndata++;
215 if (!ndata) {
216 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
217 return -1;
218 }
219 ndata++; /* Add MEI header */
220
221 /*
222 * Make sure there is still room left in the circular buffer.
223 * Reset the buffer pointers if the requested message will not fit.
224 */
225 read_host_csr(&host);
226 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
227 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
228 mei_reset();
229 read_host_csr(&host);
230 }
231
232 /*
233 * This implementation does not handle splitting large messages
234 * across multiple transactions. Ensure the requested length
235 * will fit in the available circular buffer depth.
236 */
237 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
238 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
239 ndata + 2, host.buffer_depth);
240 return -1;
241 }
242
243 /* Write MEI header */
244 mei_write_dword_ptr(mei, MEI_H_CB_WW);
245 ndata--;
246
247 /* Write MKHI header */
248 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
249 ndata--;
250
251 /* Write message data */
252 data = req_data;
253 for (n = 0; n < ndata; ++n)
254 write_cb(*data++);
255
256 /* Generate interrupt to the ME */
257 read_host_csr(&host);
258 host.interrupt_generate = 1;
259 write_host_csr(&host);
260
261 /* Make sure ME is ready after sending request data */
262 return mei_wait_for_me_ready();
263}
264
265static int mei_recv_msg(struct mkhi_header *mkhi,
266 void *rsp_data, int rsp_bytes)
267{
268 struct mei_header mei_rsp;
269 struct mkhi_header mkhi_rsp;
270 struct mei_csr me, host;
271 unsigned ndata, n/*, me_data_len*/;
272 unsigned expected;
273 u32 *data;
274
275 /* Total number of dwords to read from circular buffer */
276 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
277 if (rsp_bytes & 3)
278 expected++;
279
280 /*
281 * The interrupt status bit does not appear to indicate that the
282 * message has actually been received. Instead we wait until the
283 * expected number of dwords are present in the circular buffer.
284 */
285 for (n = ME_RETRY; n; --n) {
286 read_me_csr(&me);
287 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
288 break;
289 udelay(ME_DELAY);
290 }
291 if (!n) {
292 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
293 "%u, available %u\n", expected,
294 me.buffer_write_ptr - me.buffer_read_ptr);
295 return -1;
296 }
297
298 /* Read and verify MEI response header from the ME */
299 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
300 if (!mei_rsp.is_complete) {
301 printk(BIOS_ERR, "ME: response is not complete\n");
302 return -1;
303 }
304
305 /* Handle non-dword responses and expect at least MKHI header */
306 ndata = mei_rsp.length >> 2;
307 if (mei_rsp.length & 3)
308 ndata++;
309 if (ndata != (expected - 1)) {
310 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
311 ndata, (expected - 1));
312 return -1;
313 }
314
315 /* Read and verify MKHI response header from the ME */
316 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
317 if (!mkhi_rsp.is_response ||
318 mkhi->group_id != mkhi_rsp.group_id ||
319 mkhi->command != mkhi_rsp.command) {
320 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u,"
321 "command %u ?= %u, is_response %u\n", mkhi->group_id,
322 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
323 mkhi_rsp.is_response);
324 return -1;
325 }
326 ndata--; /* MKHI header has been read */
327
328 /* Make sure caller passed a buffer with enough space */
329 if (ndata != (rsp_bytes >> 2)) {
330 printk(BIOS_ERR, "ME: not enough room in response buffer: "
331 "%u != %u\n", ndata, rsp_bytes >> 2);
332 return -1;
333 }
334
335 /* Read response data from the circular buffer */
336 data = rsp_data;
337 for (n = 0; n < ndata; ++n)
338 *data++ = read_cb();
339
340 /* Tell the ME that we have consumed the response */
341 read_host_csr(&host);
342 host.interrupt_status = 1;
343 host.interrupt_generate = 1;
344 write_host_csr(&host);
345
346 return mei_wait_for_me_ready();
347}
348
349static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
350 void *req_data, void *rsp_data, int rsp_bytes)
351{
352 if (mei_send_msg(mei, mkhi, req_data) < 0)
353 return -1;
354 if (mei_recv_msg(mkhi, rsp_data, rsp_bytes) < 0)
355 return -1;
356 return 0;
357}
358
359#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) && !defined(__SMM__)
360static inline void print_cap(const char *name, int state)
361{
362 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
363 name, state ? " en" : "dis");
364}
365
366static void me_print_fw_version(mbp_fw_version_name *vers_name)
367{
368 if (!vers_name->major_version) {
369 printk(BIOS_ERR, "ME: mbp missing version report\n");
370 return;
371 }
372
373 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
374 vers_name->major_version, vers_name->minor_version,
375 vers_name->hotfix_version, vers_name->build_version);
376}
377
378/* Get ME Firmware Capabilities */
379static int mkhi_get_fwcaps(mefwcaps_sku *cap)
380{
381 u32 rule_id = 0;
382 struct me_fwcaps cap_msg;
383 struct mkhi_header mkhi = {
384 .group_id = MKHI_GROUP_ID_FWCAPS,
385 .command = MKHI_FWCAPS_GET_RULE,
386 };
387 struct mei_header mei = {
388 .is_complete = 1,
389 .host_address = MEI_HOST_ADDRESS,
390 .client_address = MEI_ADDRESS_MKHI,
391 .length = sizeof(mkhi) + sizeof(rule_id),
392 };
393
394 /* Send request and wait for response */
395 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg))
396 < 0) {
397 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
398 return -1;
399 }
400 *cap = cap_msg.caps_sku;
401 return 0;
402}
403
404/* Get ME Firmware Capabilities */
405static void me_print_fwcaps(mbp_fw_caps *caps_section)
406{
407 mefwcaps_sku *cap = &caps_section->fw_capabilities;
408 if (!caps_section->available) {
409 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
410 if (mkhi_get_fwcaps(cap))
411 return;
412 }
413
414 print_cap("Full Network manageability", cap->full_net);
415 print_cap("Regular Network manageability", cap->std_net);
416 print_cap("Manageability", cap->manageability);
Aaron Durbin76c37002012-10-30 09:03:43 -0500417 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
418 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
419 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
420 print_cap("ICC Over Clocking", cap->icc_over_clocking);
421 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
422 print_cap("IPV6", cap->ipv6);
423 print_cap("KVM Remote Control (KVM)", cap->kvm);
424 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
425 print_cap("Virtual LAN (VLAN)", cap->vlan);
426 print_cap("TLS", cap->tls);
427 print_cap("Wireless LAN (WLAN)", cap->wlan);
428}
429#endif
430
431#if CONFIG_CHROMEOS && 0 /* DISABLED */
432/* Tell ME to issue a global reset */
433static int mkhi_global_reset(void)
434{
435 struct me_global_reset reset = {
436 .request_origin = GLOBAL_RESET_BIOS_POST,
437 .reset_type = CBM_RR_GLOBAL_RESET,
438 };
439 struct mkhi_header mkhi = {
440 .group_id = MKHI_GROUP_ID_CBM,
441 .command = MKHI_GLOBAL_RESET,
442 };
443 struct mei_header mei = {
444 .is_complete = 1,
445 .length = sizeof(mkhi) + sizeof(reset),
446 .host_address = MEI_HOST_ADDRESS,
447 .client_address = MEI_ADDRESS_MKHI,
448 };
449
450 /* Send request and wait for response */
451 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
452 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
453 /* No response means reset will happen shortly... */
454 hlt();
455 }
456
457 /* If the ME responded it rejected the reset request */
458 printk(BIOS_ERR, "ME: Global Reset failed\n");
459 return -1;
460}
461#endif
462
463#ifdef __SMM__
464
465/* Send END OF POST message to the ME */
466static int mkhi_end_of_post(void)
467{
468 struct mkhi_header mkhi = {
469 .group_id = MKHI_GROUP_ID_GEN,
470 .command = MKHI_END_OF_POST,
471 };
472 struct mei_header mei = {
473 .is_complete = 1,
474 .host_address = MEI_HOST_ADDRESS,
475 .client_address = MEI_ADDRESS_MKHI,
476 .length = sizeof(mkhi),
477 };
478
479 u32 eop_ack;
480
481 /* Send request and wait for response */
482 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
483 if (mei_sendrecv(&mei, &mkhi, NULL, &eop_ack, sizeof(eop_ack)) < 0) {
484 printk(BIOS_ERR, "ME: END OF POST message failed\n");
485 return -1;
486 }
487
488 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
489 return 0;
490}
491
492void intel_me_finalize_smm(void)
493{
494 struct me_hfs hfs;
495 u32 reg32;
496
497 mei_base_address =
Aaron Durbin89f79a02012-10-31 23:05:25 -0500498 pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf;
Aaron Durbin76c37002012-10-30 09:03:43 -0500499
500 /* S3 path will have hidden this device already */
501 if (!mei_base_address || mei_base_address == 0xfffffff0)
502 return;
503
504 /* Make sure ME is in a mode that expects EOP */
Aaron Durbin89f79a02012-10-31 23:05:25 -0500505 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Aaron Durbin76c37002012-10-30 09:03:43 -0500506 memcpy(&hfs, &reg32, sizeof(u32));
507
508 /* Abort and leave device alone if not normal mode */
509 if (hfs.fpt_bad ||
510 hfs.working_state != ME_HFS_CWS_NORMAL ||
511 hfs.operation_mode != ME_HFS_MODE_NORMAL)
512 return;
513
514 /* Try to send EOP command so ME stops accepting other commands */
515 mkhi_end_of_post();
516
517 /* Make sure IO is disabled */
Aaron Durbin89f79a02012-10-31 23:05:25 -0500518 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Aaron Durbin76c37002012-10-30 09:03:43 -0500519 reg32 &= ~(PCI_COMMAND_MASTER |
520 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Aaron Durbin89f79a02012-10-31 23:05:25 -0500521 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500522
523 /* Hide the PCI device */
524 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
525}
526
527#else /* !__SMM__ */
528
529/* Determine the path that we should take based on ME status */
530static me_bios_path intel_me_path(device_t dev)
531{
532 me_bios_path path = ME_DISABLE_BIOS_PATH;
533 struct me_hfs hfs;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500534 struct me_hfs2 hfs2;
Aaron Durbin76c37002012-10-30 09:03:43 -0500535
536#if CONFIG_HAVE_ACPI_RESUME
537 /* S3 wake skips all MKHI messages */
538 if (acpi_slp_type == 3) {
539 return ME_S3WAKE_BIOS_PATH;
540 }
541#endif
542
543 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500544 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500545
546 /* Check and dump status */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500547 intel_me_status(&hfs, &hfs2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500548
549 /* Check Current Working State */
550 switch (hfs.working_state) {
551 case ME_HFS_CWS_NORMAL:
552 path = ME_NORMAL_BIOS_PATH;
553 break;
554 case ME_HFS_CWS_REC:
555 path = ME_RECOVERY_BIOS_PATH;
556 break;
557 default:
558 path = ME_DISABLE_BIOS_PATH;
559 break;
560 }
561
562 /* Check Current Operation Mode */
563 switch (hfs.operation_mode) {
564 case ME_HFS_MODE_NORMAL:
565 break;
566 case ME_HFS_MODE_DEBUG:
567 case ME_HFS_MODE_DIS:
568 case ME_HFS_MODE_OVER_JMPR:
569 case ME_HFS_MODE_OVER_MEI:
570 default:
571 path = ME_DISABLE_BIOS_PATH;
572 break;
573 }
574
575 /* Check for any error code and valid firmware and MBP */
576 if (hfs.error_code || hfs.fpt_bad)
577 path = ME_ERROR_BIOS_PATH;
578
579 /* Check if the MBP is ready */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500580 if (!hfs2.mbp_rdy) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500581 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
582 __FUNCTION__);
583 path = ME_ERROR_BIOS_PATH;
584 }
585
586#if CONFIG_ELOG
587 if (path != ME_NORMAL_BIOS_PATH) {
588 struct elog_event_data_me_extended data = {
589 .current_working_state = hfs.working_state,
590 .operation_state = hfs.operation_state,
591 .operation_mode = hfs.operation_mode,
592 .error_code = hfs.error_code,
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500593 .progress_code = hfs2.progress_code,
594 .current_pmevent = hfs2.current_pmevent,
595 .current_state = hfs2.current_state,
Aaron Durbin76c37002012-10-30 09:03:43 -0500596 };
597 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
598 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
599 &data, sizeof(data));
600 }
601#endif
602
603 return path;
604}
605
606/* Prepare ME for MEI messages */
607static int intel_mei_setup(device_t dev)
608{
609 struct resource *res;
610 struct mei_csr host;
611 u32 reg32;
612
613 /* Find the MMIO base for the ME interface */
614 res = find_resource(dev, PCI_BASE_ADDRESS_0);
615 if (!res || res->base == 0 || res->size == 0) {
616 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
617 return -1;
618 }
619 mei_base_address = res->base;
620
621 /* Ensure Memory and Bus Master bits are set */
622 reg32 = pci_read_config32(dev, PCI_COMMAND);
623 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
624 pci_write_config32(dev, PCI_COMMAND, reg32);
625
626 /* Clean up status for next message */
627 read_host_csr(&host);
628 host.interrupt_generate = 1;
629 host.ready = 1;
630 host.reset = 0;
631 write_host_csr(&host);
632
633 return 0;
634}
635
636/* Read the Extend register hash of ME firmware */
637static int intel_me_extend_valid(device_t dev)
638{
639 struct me_heres status;
640 u32 extend[8] = {0};
641 int i, count = 0;
642
643 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
644 if (!status.extend_feature_present) {
645 printk(BIOS_ERR, "ME: Extend Feature not present\n");
646 return -1;
647 }
648
649 if (!status.extend_reg_valid) {
650 printk(BIOS_ERR, "ME: Extend Register not valid\n");
651 return -1;
652 }
653
654 switch (status.extend_reg_algorithm) {
655 case PCI_ME_EXT_SHA1:
656 count = 5;
657 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
658 break;
659 case PCI_ME_EXT_SHA256:
660 count = 8;
661 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
662 break;
663 default:
664 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
665 status.extend_reg_algorithm);
666 return -1;
667 }
668
669 for (i = 0; i < count; ++i) {
670 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
671 printk(BIOS_DEBUG, "%08x", extend[i]);
672 }
673 printk(BIOS_DEBUG, "\n");
674
675#if CONFIG_CHROMEOS
676 /* Save hash in NVS for the OS to verify */
677 chromeos_set_me_hash(extend, count);
678#endif
679
680 return 0;
681}
682
683/* Hide the ME virtual PCI devices */
684static void intel_me_hide(device_t dev)
685{
686 dev->enabled = 0;
687 pch_enable(dev);
688}
689
690/* Check whether ME is present and do basic init */
691static void intel_me_init(device_t dev)
692{
693 me_bios_path path = intel_me_path(dev);
694 me_bios_payload mbp_data;
695
696 /* Do initial setup and determine the BIOS path */
697 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
698
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500699 if (path == ME_S3WAKE_BIOS_PATH) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500700 intel_me_hide(dev);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500701 return;
702 } else if (path == ME_NORMAL_BIOS_PATH) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500703 /* Validate the extend register */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500704 /* FIXME: force recovery mode on failure. */
705 intel_me_extend_valid(dev);
706 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500707
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500708 /*
709 * According to the ME9 BWG, BIOS is required to fetch MBP data in
710 * all boot flows except S3 Resume.
711 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500712
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500713 /* Prepare MEI MMIO interface */
714 if (intel_mei_setup(dev) < 0)
715 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500716
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500717 if(intel_me_read_mbp(&mbp_data, dev))
718 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500719
720#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500721 me_print_fw_version(&mbp_data.fw_version_name);
722 me_print_fwcaps(&mbp_data.fw_caps_sku);
Aaron Durbin76c37002012-10-30 09:03:43 -0500723#endif
724
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500725 /*
726 * Leave the ME unlocked. It will be locked via SMI command later.
727 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500728}
729
730static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
731{
732 if (!vendor || !device) {
733 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
734 pci_read_config32(dev, PCI_VENDOR_ID));
735 } else {
736 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
737 ((device & 0xffff) << 16) | (vendor & 0xffff));
738 }
739}
740
741static struct pci_operations pci_ops = {
742 .set_subsystem = set_subsystem,
743};
744
745static struct device_operations device_ops = {
746 .read_resources = pci_dev_read_resources,
747 .set_resources = pci_dev_set_resources,
748 .enable_resources = pci_dev_enable_resources,
749 .init = intel_me_init,
750 .scan_bus = scan_static_bus,
751 .ops_pci = &pci_ops,
752};
753
754static const struct pci_driver intel_me __pci_driver = {
755 .ops = &device_ops,
756 .vendor = PCI_VENDOR_ID_INTEL,
757 .device = 0x1e3a,
758};
759
760/******************************************************************************
761 * */
762static u32 me_to_host_words_pending(void)
763{
764 struct mei_csr me;
765 read_me_csr(&me);
766 if (!me.ready)
767 return 0;
768 return (me.buffer_write_ptr - me.buffer_read_ptr) &
769 (me.buffer_depth - 1);
770}
771
772#if 0
773/* This function is not yet being used, keep it in for the future. */
774static u32 host_to_me_words_room(void)
775{
776 struct mei_csr csr;
777
778 read_me_csr(&csr);
779 if (!csr.ready)
780 return 0;
781
782 read_host_csr(&csr);
783 return (csr.buffer_read_ptr - csr.buffer_write_ptr - 1) &
784 (csr.buffer_depth - 1);
785}
786#endif
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500787
788/*
789 * mbp give up routine. This path is taken if hfs.mpb_rdy is 0 or the read
790 * state machine on the BIOS end doesn't match the ME's state machine.
791 */
792static void intel_me_mbp_give_up(device_t dev)
793{
794 u32 reg32;
795 struct mei_csr csr;
796
797 reg32 = PCI_ME_MBP_GIVE_UP;
798 pci_write_config32(dev, PCI_ME_H_GS3, reg32);
799 read_host_csr(&csr);
800 csr.reset = 1;
801 csr.interrupt_generate = 1;
802 write_host_csr(&csr);
803}
804
Aaron Durbin76c37002012-10-30 09:03:43 -0500805/*
806 * mbp seems to be following its own flow, let's retrieve it in a dedicated
807 * function.
808 */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500809static int intel_me_read_mbp(me_bios_payload *mbp_data, device_t dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500810{
811 mbp_header mbp_hdr;
812 mbp_item_header mbp_item_hdr;
813 u32 me2host_pending;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500814 u32 mbp_ident;
Aaron Durbin76c37002012-10-30 09:03:43 -0500815 struct mei_csr host;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500816 struct me_hfs2 hfs2;
817 int count;
818
819 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
820
821 if (!hfs2.mbp_rdy) {
822 printk(BIOS_ERR, "ME: MBP not ready\n");
823 goto mbp_failure;
824 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500825
826 me2host_pending = me_to_host_words_pending();
827 if (!me2host_pending) {
828 printk(BIOS_ERR, "ME: no mbp data!\n");
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500829 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500830 }
831
832 /* we know for sure that at least the header is there */
833 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
834
835 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
836 (me2host_pending < mbp_hdr.mbp_size)) {
837 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
838 " buffer contains %d words\n",
839 mbp_hdr.num_entries, mbp_hdr.mbp_size,
840 me2host_pending);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500841 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500842 }
843
844 me2host_pending--;
845 memset(mbp_data, 0, sizeof(*mbp_data));
846
847 while (mbp_hdr.num_entries--) {
848 u32* copy_addr;
849 u32 copy_size, buffer_room;
850 void *p;
851
852 if (!me2host_pending) {
853 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
854 mbp_hdr.num_entries + 1);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500855 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500856 }
857
858 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
859
860 if (mbp_item_hdr.length > me2host_pending) {
861 printk(BIOS_ERR, "ME: insufficient mbp data %d "
862 "entries to go!\n",
863 mbp_hdr.num_entries + 1);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500864 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500865 }
866
867 me2host_pending -= mbp_item_hdr.length;
868
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500869 mbp_ident = MBP_MAKE_IDENT(mbp_item_hdr.app_id,
870 mbp_item_hdr.item_id);
Aaron Durbin76c37002012-10-30 09:03:43 -0500871
872 copy_size = mbp_item_hdr.length - 1;
873
874#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
875 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
876 break; \
877 }
878
879 p = &mbp_item_hdr;
880 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
881
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500882 switch(mbp_ident) {
883 case MBP_IDENT(KERNEL, FW_VER):
Aaron Durbin76c37002012-10-30 09:03:43 -0500884 SET_UP_COPY(fw_version_name);
885
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500886 case MBP_IDENT(ICC, PROFILE):
Aaron Durbin76c37002012-10-30 09:03:43 -0500887 SET_UP_COPY(icc_profile);
888
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500889 case MBP_IDENT(INTEL_AT, STATE):
Aaron Durbin76c37002012-10-30 09:03:43 -0500890 SET_UP_COPY(at_state);
891
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500892 case MBP_IDENT(KERNEL, FW_CAP):
Aaron Durbin76c37002012-10-30 09:03:43 -0500893 mbp_data->fw_caps_sku.available = 1;
894 SET_UP_COPY(fw_caps_sku.fw_capabilities);
895
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500896 case MBP_IDENT(KERNEL, ROM_BIST):
Aaron Durbin76c37002012-10-30 09:03:43 -0500897 SET_UP_COPY(rom_bist_data);
898
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500899 case MBP_IDENT(KERNEL, PLAT_KEY):
Aaron Durbin76c37002012-10-30 09:03:43 -0500900 SET_UP_COPY(platform_key);
901
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500902 case MBP_IDENT(KERNEL, FW_TYPE):
Aaron Durbin76c37002012-10-30 09:03:43 -0500903 mbp_data->fw_plat_type.available = 1;
904 SET_UP_COPY(fw_plat_type.rule_data);
905
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500906 case MBP_IDENT(KERNEL, MFS_FAILURE):
Aaron Durbin76c37002012-10-30 09:03:43 -0500907 SET_UP_COPY(mfsintegrity);
908
909 default:
910 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x!!!\n",
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500911 mbp_ident);
912 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500913 }
914
915 if (buffer_room != copy_size) {
916 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
917 " for item 0x%x!!!\n",
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500918 buffer_room, copy_size, mbp_ident);
919 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500920 }
921 while(copy_size--)
922 *copy_addr++ = read_cb();
923 }
924
925 read_host_csr(&host);
926 host.interrupt_generate = 1;
927 write_host_csr(&host);
928
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500929 for (count = ME_RETRY; count > 0; --count) {
930 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
931 if (hfs2.mbp_cleared)
932 break;
933 udelay(ME_DELAY);
934 }
935
936 if (count == 0) {
937 printk(BIOS_WARNING, "ME: Timeout waiting for mbp_cleared\n");
938 intel_me_mbp_give_up(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500939 }
940
941 return 0;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500942
943mbp_failure:
944 intel_me_mbp_give_up(dev);
945 return -1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500946}
947
948#endif /* !__SMM__ */