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