blob: b12a1e6e7358f5814a5095885b41e4f6c6235960 [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.
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>
Duncan Lauriec1c94352012-07-13 10:11:54 -070038#include <elog.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020039
40#ifdef __SMM__
Kyösti Mälkki54d6abd2013-06-19 23:05:00 +030041#include <arch/pci_mmio_cfg.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020042#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>
Stefan Reinauer49058c02012-06-11 14:13:09 -070052#include <vendorcode/google/chromeos/gnvs.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020053#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};
65static int intel_me_read_mbp(me_bios_payload *mbp_data);
66#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
Stefan Reinauer8e073822012-04-04 00:07:22 +0200359#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);
417 print_cap("Small business technology", cap->small_business);
418 print_cap("Level III manageability", cap->l3manageability);
419 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
420 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
421 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
422 print_cap("ICC Over Clocking", cap->icc_over_clocking);
423 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
424 print_cap("IPV6", cap->ipv6);
425 print_cap("KVM Remote Control (KVM)", cap->kvm);
426 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
427 print_cap("Virtual LAN (VLAN)", cap->vlan);
428 print_cap("TLS", cap->tls);
429 print_cap("Wireless LAN (WLAN)", cap->wlan);
430}
431#endif
432
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700433#if CONFIG_CHROMEOS && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200434/* Tell ME to issue a global reset */
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700435static int mkhi_global_reset(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200436{
437 struct me_global_reset reset = {
438 .request_origin = GLOBAL_RESET_BIOS_POST,
439 .reset_type = CBM_RR_GLOBAL_RESET,
440 };
441 struct mkhi_header mkhi = {
442 .group_id = MKHI_GROUP_ID_CBM,
443 .command = MKHI_GLOBAL_RESET,
444 };
445 struct mei_header mei = {
446 .is_complete = 1,
447 .length = sizeof(mkhi) + sizeof(reset),
448 .host_address = MEI_HOST_ADDRESS,
449 .client_address = MEI_ADDRESS_MKHI,
450 };
451
452 /* Send request and wait for response */
453 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
454 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
455 /* No response means reset will happen shortly... */
456 hlt();
457 }
458
459 /* If the ME responded it rejected the reset request */
460 printk(BIOS_ERR, "ME: Global Reset failed\n");
461 return -1;
462}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700463#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200464
465#ifdef __SMM__
466
Duncan Laurie708f7312012-07-10 15:15:41 -0700467/* Send END OF POST message to the ME */
468static int mkhi_end_of_post(void)
469{
470 struct mkhi_header mkhi = {
471 .group_id = MKHI_GROUP_ID_GEN,
472 .command = MKHI_END_OF_POST,
473 };
474 struct mei_header mei = {
475 .is_complete = 1,
476 .host_address = MEI_HOST_ADDRESS,
477 .client_address = MEI_ADDRESS_MKHI,
478 .length = sizeof(mkhi),
479 };
480
481 u32 eop_ack;
482
483 /* Send request and wait for response */
484 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
485 if (mei_sendrecv(&mei, &mkhi, NULL, &eop_ack, sizeof(eop_ack)) < 0) {
486 printk(BIOS_ERR, "ME: END OF POST message failed\n");
487 return -1;
488 }
489
490 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
491 return 0;
492}
493
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700494void intel_me8_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200495{
496 struct me_hfs hfs;
497 u32 reg32;
498
499 mei_base_address =
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300500 pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200501
502 /* S3 path will have hidden this device already */
503 if (!mei_base_address || mei_base_address == 0xfffffff0)
504 return;
505
506 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300507 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200508 memcpy(&hfs, &reg32, sizeof(u32));
509
510 /* Abort and leave device alone if not normal mode */
511 if (hfs.fpt_bad ||
512 hfs.working_state != ME_HFS_CWS_NORMAL ||
513 hfs.operation_mode != ME_HFS_MODE_NORMAL)
514 return;
515
516 /* Try to send EOP command so ME stops accepting other commands */
517 mkhi_end_of_post();
518
519 /* Make sure IO is disabled */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300520 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200521 reg32 &= ~(PCI_COMMAND_MASTER |
522 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300523 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200524
525 /* Hide the PCI device */
526 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
527}
528
529#else /* !__SMM__ */
530
531/* Determine the path that we should take based on ME status */
532static me_bios_path intel_me_path(device_t dev)
533{
534 me_bios_path path = ME_DISABLE_BIOS_PATH;
535 struct me_hfs hfs;
536 struct me_gmes gmes;
537
538#if CONFIG_HAVE_ACPI_RESUME
539 /* S3 wake skips all MKHI messages */
540 if (acpi_slp_type == 3) {
541 return ME_S3WAKE_BIOS_PATH;
542 }
543#endif
544
545 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
546 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
547
548 /* Check and dump status */
549 intel_me_status(&hfs, &gmes);
550
Stefan Reinauer8e073822012-04-04 00:07:22 +0200551 /* Check Current Working State */
552 switch (hfs.working_state) {
553 case ME_HFS_CWS_NORMAL:
554 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200555 break;
556 case ME_HFS_CWS_REC:
557 path = ME_RECOVERY_BIOS_PATH;
558 break;
559 default:
560 path = ME_DISABLE_BIOS_PATH;
561 break;
562 }
563
564 /* Check Current Operation Mode */
565 switch (hfs.operation_mode) {
566 case ME_HFS_MODE_NORMAL:
567 break;
568 case ME_HFS_MODE_DEBUG:
569 case ME_HFS_MODE_DIS:
570 case ME_HFS_MODE_OVER_JMPR:
571 case ME_HFS_MODE_OVER_MEI:
572 default:
573 path = ME_DISABLE_BIOS_PATH;
574 break;
575 }
576
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700577 /* Check for any error code and valid firmware and MBP */
578 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200579 path = ME_ERROR_BIOS_PATH;
580
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700581 /* Check if the MBP is ready */
582 if (!gmes.mbp_rdy) {
583 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
584 __FUNCTION__);
585 path = ME_ERROR_BIOS_PATH;
586 }
587
588#if CONFIG_ELOG
589 if (path != ME_NORMAL_BIOS_PATH) {
590 struct elog_event_data_me_extended data = {
591 .current_working_state = hfs.working_state,
592 .operation_state = hfs.operation_state,
593 .operation_mode = hfs.operation_mode,
594 .error_code = hfs.error_code,
595 .progress_code = gmes.progress_code,
596 .current_pmevent = gmes.current_pmevent,
597 .current_state = gmes.current_state,
598 };
599 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
600 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
601 &data, sizeof(data));
602 }
603#endif
604
Stefan Reinauer8e073822012-04-04 00:07:22 +0200605 return path;
606}
607
608/* Prepare ME for MEI messages */
609static int intel_mei_setup(device_t dev)
610{
611 struct resource *res;
612 struct mei_csr host;
613 u32 reg32;
614
615 /* Find the MMIO base for the ME interface */
616 res = find_resource(dev, PCI_BASE_ADDRESS_0);
617 if (!res || res->base == 0 || res->size == 0) {
618 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
619 return -1;
620 }
621 mei_base_address = res->base;
622
623 /* Ensure Memory and Bus Master bits are set */
624 reg32 = pci_read_config32(dev, PCI_COMMAND);
625 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
626 pci_write_config32(dev, PCI_COMMAND, reg32);
627
628 /* Clean up status for next message */
629 read_host_csr(&host);
630 host.interrupt_generate = 1;
631 host.ready = 1;
632 host.reset = 0;
633 write_host_csr(&host);
634
635 return 0;
636}
637
638/* Read the Extend register hash of ME firmware */
639static int intel_me_extend_valid(device_t dev)
640{
641 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700642 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200643 int i, count = 0;
644
645 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
646 if (!status.extend_feature_present) {
647 printk(BIOS_ERR, "ME: Extend Feature not present\n");
648 return -1;
649 }
650
651 if (!status.extend_reg_valid) {
652 printk(BIOS_ERR, "ME: Extend Register not valid\n");
653 return -1;
654 }
655
656 switch (status.extend_reg_algorithm) {
657 case PCI_ME_EXT_SHA1:
658 count = 5;
659 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
660 break;
661 case PCI_ME_EXT_SHA256:
662 count = 8;
663 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
664 break;
665 default:
666 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
667 status.extend_reg_algorithm);
668 return -1;
669 }
670
Stefan Reinauer8e073822012-04-04 00:07:22 +0200671 for (i = 0; i < count; ++i) {
Stefan Reinauer49058c02012-06-11 14:13:09 -0700672 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
673 printk(BIOS_DEBUG, "%08x", extend[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200674 }
675 printk(BIOS_DEBUG, "\n");
676
Stefan Reinauer49058c02012-06-11 14:13:09 -0700677#if CONFIG_CHROMEOS
678 /* Save hash in NVS for the OS to verify */
679 chromeos_set_me_hash(extend, count);
680#endif
681
Stefan Reinauer8e073822012-04-04 00:07:22 +0200682 return 0;
683}
684
685/* Hide the ME virtual PCI devices */
686static void intel_me_hide(device_t dev)
687{
688 dev->enabled = 0;
689 pch_enable(dev);
690}
691
692/* Check whether ME is present and do basic init */
693static void intel_me_init(device_t dev)
694{
695 me_bios_path path = intel_me_path(dev);
696 me_bios_payload mbp_data;
697
698 /* Do initial setup and determine the BIOS path */
699 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
700
701 switch (path) {
702 case ME_S3WAKE_BIOS_PATH:
703 intel_me_hide(dev);
704 break;
705
706 case ME_NORMAL_BIOS_PATH:
707 /* Validate the extend register */
708 if (intel_me_extend_valid(dev) < 0)
709 break; /* TODO: force recovery mode */
710
711 /* Prepare MEI MMIO interface */
712 if (intel_mei_setup(dev) < 0)
713 break;
714
715 if(intel_me_read_mbp(&mbp_data))
716 break;
717
718#if CONFIG_CHROMEOS && 0 /* DISABLED */
719 /*
720 * Unlock ME in recovery mode.
721 */
722 if (recovery_mode_enabled()) {
723 /* Unlock ME flash region */
724 mkhi_hmrfpo_enable();
725
726 /* Issue global reset */
727 mkhi_global_reset();
728 return;
729 }
730#endif
731
732#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
733 me_print_fw_version(&mbp_data.fw_version_name);
734 me_print_fwcaps(&mbp_data.fw_caps_sku);
735#endif
Duncan Laurie708f7312012-07-10 15:15:41 -0700736
737 /*
738 * Leave the ME unlocked in this path.
739 * It will be locked via SMI command later.
740 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200741 break;
742
743 case ME_ERROR_BIOS_PATH:
744 case ME_RECOVERY_BIOS_PATH:
745 case ME_DISABLE_BIOS_PATH:
746 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200747 break;
748 }
749}
750
751static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
752{
753 if (!vendor || !device) {
754 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
755 pci_read_config32(dev, PCI_VENDOR_ID));
756 } else {
757 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
758 ((device & 0xffff) << 16) | (vendor & 0xffff));
759 }
760}
761
762static struct pci_operations pci_ops = {
763 .set_subsystem = set_subsystem,
764};
765
766static struct device_operations device_ops = {
767 .read_resources = pci_dev_read_resources,
768 .set_resources = pci_dev_set_resources,
769 .enable_resources = pci_dev_enable_resources,
770 .init = intel_me_init,
771 .scan_bus = scan_static_bus,
772 .ops_pci = &pci_ops,
773};
774
775static const struct pci_driver intel_me __pci_driver = {
776 .ops = &device_ops,
777 .vendor = PCI_VENDOR_ID_INTEL,
778 .device = 0x1e3a,
779};
780
781/******************************************************************************
782 * */
783static u32 me_to_host_words_pending(void)
784{
785 struct mei_csr me;
786 read_me_csr(&me);
787 if (!me.ready)
788 return 0;
789 return (me.buffer_write_ptr - me.buffer_read_ptr) &
790 (me.buffer_depth - 1);
791}
792
793#if 0
794/* This function is not yet being used, keep it in for the future. */
795static u32 host_to_me_words_room(void)
796{
797 struct mei_csr csr;
798
799 read_me_csr(&csr);
800 if (!csr.ready)
801 return 0;
802
803 read_host_csr(&csr);
804 return (csr.buffer_read_ptr - csr.buffer_write_ptr - 1) &
805 (csr.buffer_depth - 1);
806}
807#endif
808/*
809 * mbp seems to be following its own flow, let's retrieve it in a dedicated
810 * function.
811 */
812static int intel_me_read_mbp(me_bios_payload *mbp_data)
813{
814 mbp_header mbp_hdr;
815 mbp_item_header mbp_item_hdr;
816 u32 me2host_pending;
817 u32 mbp_item_id;
818 struct mei_csr host;
819
820 me2host_pending = me_to_host_words_pending();
821 if (!me2host_pending) {
822 printk(BIOS_ERR, "ME: no mbp data!\n");
823 return -1;
824 }
825
826 /* we know for sure that at least the header is there */
827 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
828
829 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
830 (me2host_pending < mbp_hdr.mbp_size)) {
831 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
832 " buffer contains %d words\n",
833 mbp_hdr.num_entries, mbp_hdr.mbp_size,
834 me2host_pending);
835 return -1;
836 }
837
838 me2host_pending--;
839 memset(mbp_data, 0, sizeof(*mbp_data));
840
841 while (mbp_hdr.num_entries--) {
842 u32* copy_addr;
843 u32 copy_size, buffer_room;
844 void *p;
845
846 if (!me2host_pending) {
847 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
848 mbp_hdr.num_entries + 1);
849 return -1;
850 }
851
852 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
853
854 if (mbp_item_hdr.length > me2host_pending) {
855 printk(BIOS_ERR, "ME: insufficient mbp data %d "
856 "entries to go!\n",
857 mbp_hdr.num_entries + 1);
858 return -1;
859 }
860
861 me2host_pending -= mbp_item_hdr.length;
862
863 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
864 mbp_item_hdr.app_id;
865
866 copy_size = mbp_item_hdr.length - 1;
867
868#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
869 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
870 break; \
871 }
872
873 p = &mbp_item_hdr;
874 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
875
876 switch(mbp_item_id) {
877 case 0x101:
878 SET_UP_COPY(fw_version_name);
879
880 case 0x102:
881 SET_UP_COPY(icc_profile);
882
883 case 0x103:
884 SET_UP_COPY(at_state);
885
886 case 0x201:
887 mbp_data->fw_caps_sku.available = 1;
888 SET_UP_COPY(fw_caps_sku.fw_capabilities);
889
890 case 0x301:
891 SET_UP_COPY(rom_bist_data);
892
893 case 0x401:
894 SET_UP_COPY(platform_key);
895
896 case 0x501:
897 mbp_data->fw_plat_type.available = 1;
898 SET_UP_COPY(fw_plat_type.rule_data);
899
900 case 0x601:
901 SET_UP_COPY(mfsintegrity);
902
903 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000904 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200905 mbp_item_id);
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000906 while(copy_size--)
907 read_cb();
908 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200909 }
910
911 if (buffer_room != copy_size) {
912 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
913 " for item 0x%x!!!\n",
914 buffer_room, copy_size, mbp_item_id);
915 return -1;
916 }
917 while(copy_size--)
918 *copy_addr++ = read_cb();
919 }
920
921 read_host_csr(&host);
922 host.interrupt_generate = 1;
923 write_host_csr(&host);
924
925 {
926 int cntr = 0;
927 while(host.interrupt_generate) {
928 read_host_csr(&host);
929 cntr++;
930 }
931 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
932 }
933
934 return 0;
935}
936
937#endif /* !__SMM__ */