blob: 01eb8447c68d53992f319141ca753ccf47a3842f [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>
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -070034#include <device/device.h>
35#include <device/pci.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050036#include <device/pci_ids.h>
37#include <device/pci_def.h>
38#include <string.h>
39#include <delay.h>
40#include <elog.h>
41
Aaron Durbin76c37002012-10-30 09:03:43 -050042#include "me.h"
43#include "pch.h"
44
45#if CONFIG_CHROMEOS
46#include <vendorcode/google/chromeos/chromeos.h>
47#include <vendorcode/google/chromeos/gnvs.h>
48#endif
49
Duncan Laurieaf980622013-07-18 23:02:18 -070050#ifndef __SMM__
Aaron Durbin76c37002012-10-30 09:03:43 -050051/* Path that the BIOS should take based on ME state */
52static const char *me_bios_path_values[] = {
53 [ME_NORMAL_BIOS_PATH] = "Normal",
54 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
55 [ME_ERROR_BIOS_PATH] = "Error",
56 [ME_RECOVERY_BIOS_PATH] = "Recovery",
57 [ME_DISABLE_BIOS_PATH] = "Disable",
58 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
59};
Aaron Durbin9aa031e2012-11-02 09:16:46 -050060static int intel_me_read_mbp(me_bios_payload *mbp_data, device_t dev);
Duncan Laurieaf980622013-07-18 23:02:18 -070061#endif
Aaron Durbin76c37002012-10-30 09:03:43 -050062
63/* MMIO base address for MEI interface */
64static u32 mei_base_address;
65
66#if CONFIG_DEBUG_INTEL_ME
67static void mei_dump(void *ptr, int dword, int offset, const char *type)
68{
69 struct mei_csr *csr;
70
71 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
72
73 switch (offset) {
74 case MEI_H_CSR:
75 case MEI_ME_CSR_HA:
76 csr = ptr;
77 if (!csr) {
78 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
79 break;
80 }
81 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
82 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
83 csr->buffer_read_ptr, csr->buffer_write_ptr,
84 csr->ready, csr->reset, csr->interrupt_generate,
85 csr->interrupt_status, csr->interrupt_enable);
86 break;
87 case MEI_ME_CB_RW:
88 case MEI_H_CB_WW:
89 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
90 break;
91 default:
92 printk(BIOS_SPEW, "0x%08x\n", offset);
93 break;
94 }
95}
96#else
97# define mei_dump(ptr,dword,offset,type) do {} while (0)
98#endif
99
100/*
101 * ME/MEI access helpers using memcpy to avoid aliasing.
102 */
103
104static inline void mei_read_dword_ptr(void *ptr, int offset)
105{
106 u32 dword = read32(mei_base_address + offset);
107 memcpy(ptr, &dword, sizeof(dword));
108 mei_dump(ptr, dword, offset, "READ");
109}
110
111static inline void mei_write_dword_ptr(void *ptr, int offset)
112{
113 u32 dword = 0;
114 memcpy(&dword, ptr, sizeof(dword));
115 write32(mei_base_address + offset, dword);
116 mei_dump(ptr, dword, offset, "WRITE");
117}
118
Duncan Laurieaf980622013-07-18 23:02:18 -0700119#ifndef __SMM__
Aaron Durbin76c37002012-10-30 09:03:43 -0500120static inline void pci_read_dword_ptr(device_t dev, void *ptr, int offset)
121{
122 u32 dword = pci_read_config32(dev, offset);
123 memcpy(ptr, &dword, sizeof(dword));
124 mei_dump(ptr, dword, offset, "PCI READ");
125}
Duncan Laurieaf980622013-07-18 23:02:18 -0700126#endif
Aaron Durbin76c37002012-10-30 09:03:43 -0500127
128static inline void read_host_csr(struct mei_csr *csr)
129{
130 mei_read_dword_ptr(csr, MEI_H_CSR);
131}
132
133static inline void write_host_csr(struct mei_csr *csr)
134{
135 mei_write_dword_ptr(csr, MEI_H_CSR);
136}
137
138static inline void read_me_csr(struct mei_csr *csr)
139{
140 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
141}
142
143static inline void write_cb(u32 dword)
144{
145 write32(mei_base_address + MEI_H_CB_WW, dword);
146 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
147}
148
149static inline u32 read_cb(void)
150{
151 u32 dword = read32(mei_base_address + MEI_ME_CB_RW);
152 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
153 return dword;
154}
155
156/* Wait for ME ready bit to be asserted */
157static int mei_wait_for_me_ready(void)
158{
159 struct mei_csr me;
160 unsigned try = ME_RETRY;
161
162 while (try--) {
163 read_me_csr(&me);
164 if (me.ready)
165 return 0;
166 udelay(ME_DELAY);
167 }
168
169 printk(BIOS_ERR, "ME: failed to become ready\n");
170 return -1;
171}
172
173static void mei_reset(void)
174{
175 struct mei_csr host;
176
177 if (mei_wait_for_me_ready() < 0)
178 return;
179
180 /* Reset host and ME circular buffers for next message */
181 read_host_csr(&host);
182 host.reset = 1;
183 host.interrupt_generate = 1;
184 write_host_csr(&host);
185
186 if (mei_wait_for_me_ready() < 0)
187 return;
188
189 /* Re-init and indicate host is ready */
190 read_host_csr(&host);
191 host.interrupt_generate = 1;
192 host.ready = 1;
193 host.reset = 0;
194 write_host_csr(&host);
195}
196
197static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
198 void *req_data)
199{
200 struct mei_csr host;
201 unsigned ndata, n;
202 u32 *data;
203
204 /* Number of dwords to write, ignoring MKHI */
205 ndata = mei->length >> 2;
206
207 /* Pad non-dword aligned request message length */
208 if (mei->length & 3)
209 ndata++;
210 if (!ndata) {
211 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
212 return -1;
213 }
214 ndata++; /* Add MEI header */
215
216 /*
217 * Make sure there is still room left in the circular buffer.
218 * Reset the buffer pointers if the requested message will not fit.
219 */
220 read_host_csr(&host);
221 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
222 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
223 mei_reset();
224 read_host_csr(&host);
225 }
226
227 /*
228 * This implementation does not handle splitting large messages
229 * across multiple transactions. Ensure the requested length
230 * will fit in the available circular buffer depth.
231 */
232 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
233 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
234 ndata + 2, host.buffer_depth);
235 return -1;
236 }
237
238 /* Write MEI header */
239 mei_write_dword_ptr(mei, MEI_H_CB_WW);
240 ndata--;
241
242 /* Write MKHI header */
243 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
244 ndata--;
245
246 /* Write message data */
247 data = req_data;
248 for (n = 0; n < ndata; ++n)
249 write_cb(*data++);
250
251 /* Generate interrupt to the ME */
252 read_host_csr(&host);
253 host.interrupt_generate = 1;
254 write_host_csr(&host);
255
256 /* Make sure ME is ready after sending request data */
257 return mei_wait_for_me_ready();
258}
259
260static int mei_recv_msg(struct mkhi_header *mkhi,
261 void *rsp_data, int rsp_bytes)
262{
263 struct mei_header mei_rsp;
264 struct mkhi_header mkhi_rsp;
265 struct mei_csr me, host;
266 unsigned ndata, n/*, me_data_len*/;
267 unsigned expected;
268 u32 *data;
269
270 /* Total number of dwords to read from circular buffer */
271 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
272 if (rsp_bytes & 3)
273 expected++;
274
275 /*
276 * The interrupt status bit does not appear to indicate that the
277 * message has actually been received. Instead we wait until the
278 * expected number of dwords are present in the circular buffer.
279 */
280 for (n = ME_RETRY; n; --n) {
281 read_me_csr(&me);
282 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
283 break;
284 udelay(ME_DELAY);
285 }
286 if (!n) {
287 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
288 "%u, available %u\n", expected,
289 me.buffer_write_ptr - me.buffer_read_ptr);
290 return -1;
291 }
292
293 /* Read and verify MEI response header from the ME */
294 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
295 if (!mei_rsp.is_complete) {
296 printk(BIOS_ERR, "ME: response is not complete\n");
297 return -1;
298 }
299
300 /* Handle non-dword responses and expect at least MKHI header */
301 ndata = mei_rsp.length >> 2;
302 if (mei_rsp.length & 3)
303 ndata++;
304 if (ndata != (expected - 1)) {
305 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
306 ndata, (expected - 1));
307 return -1;
308 }
309
310 /* Read and verify MKHI response header from the ME */
311 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
312 if (!mkhi_rsp.is_response ||
313 mkhi->group_id != mkhi_rsp.group_id ||
314 mkhi->command != mkhi_rsp.command) {
315 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u,"
316 "command %u ?= %u, is_response %u\n", mkhi->group_id,
317 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
318 mkhi_rsp.is_response);
319 return -1;
320 }
321 ndata--; /* MKHI header has been read */
322
323 /* Make sure caller passed a buffer with enough space */
324 if (ndata != (rsp_bytes >> 2)) {
325 printk(BIOS_ERR, "ME: not enough room in response buffer: "
326 "%u != %u\n", ndata, rsp_bytes >> 2);
327 return -1;
328 }
329
330 /* Read response data from the circular buffer */
331 data = rsp_data;
332 for (n = 0; n < ndata; ++n)
333 *data++ = read_cb();
334
335 /* Tell the ME that we have consumed the response */
336 read_host_csr(&host);
337 host.interrupt_status = 1;
338 host.interrupt_generate = 1;
339 write_host_csr(&host);
340
341 return mei_wait_for_me_ready();
342}
343
344static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
345 void *req_data, void *rsp_data, int rsp_bytes)
346{
347 if (mei_send_msg(mei, mkhi, req_data) < 0)
348 return -1;
349 if (mei_recv_msg(mkhi, rsp_data, rsp_bytes) < 0)
350 return -1;
351 return 0;
352}
353
Duncan Laurieaf980622013-07-18 23:02:18 -0700354#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) && !defined(__SMM__)
Aaron Durbin76c37002012-10-30 09:03:43 -0500355static inline void print_cap(const char *name, int state)
356{
357 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
358 name, state ? " en" : "dis");
359}
360
361static void me_print_fw_version(mbp_fw_version_name *vers_name)
362{
Aaron Durbinbe985242012-12-12 12:40:33 -0600363 if (!vers_name) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500364 printk(BIOS_ERR, "ME: mbp missing version report\n");
365 return;
366 }
367
368 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
369 vers_name->major_version, vers_name->minor_version,
370 vers_name->hotfix_version, vers_name->build_version);
371}
372
373/* Get ME Firmware Capabilities */
Aaron Durbinbe985242012-12-12 12:40:33 -0600374static int mkhi_get_fwcaps(mbp_mefwcaps *cap)
Aaron Durbin76c37002012-10-30 09:03:43 -0500375{
376 u32 rule_id = 0;
377 struct me_fwcaps cap_msg;
378 struct mkhi_header mkhi = {
379 .group_id = MKHI_GROUP_ID_FWCAPS,
380 .command = MKHI_FWCAPS_GET_RULE,
381 };
382 struct mei_header mei = {
383 .is_complete = 1,
384 .host_address = MEI_HOST_ADDRESS,
385 .client_address = MEI_ADDRESS_MKHI,
386 .length = sizeof(mkhi) + sizeof(rule_id),
387 };
388
389 /* Send request and wait for response */
390 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg))
391 < 0) {
392 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
393 return -1;
394 }
395 *cap = cap_msg.caps_sku;
396 return 0;
397}
398
399/* Get ME Firmware Capabilities */
Aaron Durbinbe985242012-12-12 12:40:33 -0600400static void me_print_fwcaps(mbp_mefwcaps *cap)
Aaron Durbin76c37002012-10-30 09:03:43 -0500401{
Aaron Durbinbe985242012-12-12 12:40:33 -0600402 mbp_mefwcaps local_caps;
403 if (!cap) {
404 cap = &local_caps;
Aaron Durbin76c37002012-10-30 09:03:43 -0500405 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
406 if (mkhi_get_fwcaps(cap))
407 return;
408 }
409
410 print_cap("Full Network manageability", cap->full_net);
411 print_cap("Regular Network manageability", cap->std_net);
412 print_cap("Manageability", cap->manageability);
Aaron Durbin76c37002012-10-30 09:03:43 -0500413 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
414 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
415 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
416 print_cap("ICC Over Clocking", cap->icc_over_clocking);
417 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
418 print_cap("IPV6", cap->ipv6);
419 print_cap("KVM Remote Control (KVM)", cap->kvm);
420 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
421 print_cap("Virtual LAN (VLAN)", cap->vlan);
422 print_cap("TLS", cap->tls);
423 print_cap("Wireless LAN (WLAN)", cap->wlan);
424}
425#endif
426
427#if CONFIG_CHROMEOS && 0 /* DISABLED */
428/* Tell ME to issue a global reset */
429static int mkhi_global_reset(void)
430{
431 struct me_global_reset reset = {
432 .request_origin = GLOBAL_RESET_BIOS_POST,
433 .reset_type = CBM_RR_GLOBAL_RESET,
434 };
435 struct mkhi_header mkhi = {
436 .group_id = MKHI_GROUP_ID_CBM,
437 .command = MKHI_GLOBAL_RESET,
438 };
439 struct mei_header mei = {
440 .is_complete = 1,
441 .length = sizeof(mkhi) + sizeof(reset),
442 .host_address = MEI_HOST_ADDRESS,
443 .client_address = MEI_ADDRESS_MKHI,
444 };
445
446 /* Send request and wait for response */
447 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
448 if (mei_sendrecv(&mei, &mkhi, &reset, NULL, 0) < 0) {
449 /* No response means reset will happen shortly... */
450 hlt();
451 }
452
453 /* If the ME responded it rejected the reset request */
454 printk(BIOS_ERR, "ME: Global Reset failed\n");
455 return -1;
456}
457#endif
458
Duncan Laurieaf980622013-07-18 23:02:18 -0700459#ifdef __SMM__
460
Aaron Durbin76c37002012-10-30 09:03:43 -0500461/* Send END OF POST message to the ME */
462static int mkhi_end_of_post(void)
463{
464 struct mkhi_header mkhi = {
465 .group_id = MKHI_GROUP_ID_GEN,
466 .command = MKHI_END_OF_POST,
467 };
468 struct mei_header mei = {
469 .is_complete = 1,
470 .host_address = MEI_HOST_ADDRESS,
471 .client_address = MEI_ADDRESS_MKHI,
472 .length = sizeof(mkhi),
473 };
474
475 u32 eop_ack;
476
477 /* Send request and wait for response */
478 printk(BIOS_NOTICE, "ME: %s\n", __FUNCTION__);
479 if (mei_sendrecv(&mei, &mkhi, NULL, &eop_ack, sizeof(eop_ack)) < 0) {
480 printk(BIOS_ERR, "ME: END OF POST message failed\n");
481 return -1;
482 }
483
484 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
485 return 0;
486}
487
Duncan Laurieaf980622013-07-18 23:02:18 -0700488void intel_me_finalize_smm(void)
489{
490 struct me_hfs hfs;
491 u32 reg32;
492
493 mei_base_address =
494 pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf;
495
496 /* S3 path will have hidden this device already */
497 if (!mei_base_address || mei_base_address == 0xfffffff0)
498 return;
499
500 /* Make sure ME is in a mode that expects EOP */
501 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
502 memcpy(&hfs, &reg32, sizeof(u32));
503
504 /* Abort and leave device alone if not normal mode */
505 if (hfs.fpt_bad ||
506 hfs.working_state != ME_HFS_CWS_NORMAL ||
507 hfs.operation_mode != ME_HFS_MODE_NORMAL)
508 return;
509
510 /* Try to send EOP command so ME stops accepting other commands */
511 mkhi_end_of_post();
512
513 /* Make sure IO is disabled */
514 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
515 reg32 &= ~(PCI_COMMAND_MASTER |
516 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
517 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
518
519 /* Hide the PCI device */
520 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
521}
522
523#else /* !__SMM__ */
524
Aaron Durbin76c37002012-10-30 09:03:43 -0500525/* Determine the path that we should take based on ME status */
526static me_bios_path intel_me_path(device_t dev)
527{
528 me_bios_path path = ME_DISABLE_BIOS_PATH;
529 struct me_hfs hfs;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500530 struct me_hfs2 hfs2;
Aaron Durbin76c37002012-10-30 09:03:43 -0500531
532#if CONFIG_HAVE_ACPI_RESUME
533 /* S3 wake skips all MKHI messages */
534 if (acpi_slp_type == 3) {
535 return ME_S3WAKE_BIOS_PATH;
536 }
537#endif
538
539 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500540 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500541
542 /* Check and dump status */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500543 intel_me_status(&hfs, &hfs2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500544
545 /* Check Current Working State */
546 switch (hfs.working_state) {
547 case ME_HFS_CWS_NORMAL:
548 path = ME_NORMAL_BIOS_PATH;
549 break;
550 case ME_HFS_CWS_REC:
551 path = ME_RECOVERY_BIOS_PATH;
552 break;
553 default:
554 path = ME_DISABLE_BIOS_PATH;
555 break;
556 }
557
558 /* Check Current Operation Mode */
559 switch (hfs.operation_mode) {
560 case ME_HFS_MODE_NORMAL:
561 break;
562 case ME_HFS_MODE_DEBUG:
563 case ME_HFS_MODE_DIS:
564 case ME_HFS_MODE_OVER_JMPR:
565 case ME_HFS_MODE_OVER_MEI:
566 default:
567 path = ME_DISABLE_BIOS_PATH;
568 break;
569 }
570
571 /* Check for any error code and valid firmware and MBP */
572 if (hfs.error_code || hfs.fpt_bad)
573 path = ME_ERROR_BIOS_PATH;
574
575 /* Check if the MBP is ready */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500576 if (!hfs2.mbp_rdy) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500577 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
578 __FUNCTION__);
579 path = ME_ERROR_BIOS_PATH;
580 }
581
582#if CONFIG_ELOG
583 if (path != ME_NORMAL_BIOS_PATH) {
584 struct elog_event_data_me_extended data = {
585 .current_working_state = hfs.working_state,
586 .operation_state = hfs.operation_state,
587 .operation_mode = hfs.operation_mode,
588 .error_code = hfs.error_code,
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500589 .progress_code = hfs2.progress_code,
590 .current_pmevent = hfs2.current_pmevent,
591 .current_state = hfs2.current_state,
Aaron Durbin76c37002012-10-30 09:03:43 -0500592 };
593 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
594 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
595 &data, sizeof(data));
596 }
597#endif
598
599 return path;
600}
601
602/* Prepare ME for MEI messages */
603static int intel_mei_setup(device_t dev)
604{
605 struct resource *res;
606 struct mei_csr host;
607 u32 reg32;
608
609 /* Find the MMIO base for the ME interface */
610 res = find_resource(dev, PCI_BASE_ADDRESS_0);
611 if (!res || res->base == 0 || res->size == 0) {
612 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
613 return -1;
614 }
615 mei_base_address = res->base;
616
617 /* Ensure Memory and Bus Master bits are set */
618 reg32 = pci_read_config32(dev, PCI_COMMAND);
619 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
620 pci_write_config32(dev, PCI_COMMAND, reg32);
621
622 /* Clean up status for next message */
623 read_host_csr(&host);
624 host.interrupt_generate = 1;
625 host.ready = 1;
626 host.reset = 0;
627 write_host_csr(&host);
628
629 return 0;
630}
631
632/* Read the Extend register hash of ME firmware */
633static int intel_me_extend_valid(device_t dev)
634{
635 struct me_heres status;
636 u32 extend[8] = {0};
637 int i, count = 0;
638
639 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
640 if (!status.extend_feature_present) {
641 printk(BIOS_ERR, "ME: Extend Feature not present\n");
642 return -1;
643 }
644
645 if (!status.extend_reg_valid) {
646 printk(BIOS_ERR, "ME: Extend Register not valid\n");
647 return -1;
648 }
649
650 switch (status.extend_reg_algorithm) {
651 case PCI_ME_EXT_SHA1:
652 count = 5;
653 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
654 break;
655 case PCI_ME_EXT_SHA256:
656 count = 8;
657 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
658 break;
659 default:
660 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
661 status.extend_reg_algorithm);
662 return -1;
663 }
664
665 for (i = 0; i < count; ++i) {
666 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
667 printk(BIOS_DEBUG, "%08x", extend[i]);
668 }
669 printk(BIOS_DEBUG, "\n");
670
671#if CONFIG_CHROMEOS
672 /* Save hash in NVS for the OS to verify */
673 chromeos_set_me_hash(extend, count);
674#endif
675
676 return 0;
677}
678
679/* Hide the ME virtual PCI devices */
680static void intel_me_hide(device_t dev)
681{
682 dev->enabled = 0;
683 pch_enable(dev);
684}
685
686/* Check whether ME is present and do basic init */
687static void intel_me_init(device_t dev)
688{
689 me_bios_path path = intel_me_path(dev);
690 me_bios_payload mbp_data;
691
692 /* Do initial setup and determine the BIOS path */
693 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
694
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500695 if (path == ME_S3WAKE_BIOS_PATH) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500696 intel_me_hide(dev);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500697 return;
698 } else if (path == ME_NORMAL_BIOS_PATH) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500699 /* Validate the extend register */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500700 /* FIXME: force recovery mode on failure. */
701 intel_me_extend_valid(dev);
702 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500703
Aaron Durbinbe985242012-12-12 12:40:33 -0600704 memset(&mbp_data, 0, sizeof(mbp_data));
705
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500706 /*
707 * According to the ME9 BWG, BIOS is required to fetch MBP data in
708 * all boot flows except S3 Resume.
709 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500710
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500711 /* Prepare MEI MMIO interface */
712 if (intel_mei_setup(dev) < 0)
713 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500714
Duncan Laurie144f7b22013-05-01 11:27:58 -0700715 if (intel_me_read_mbp(&mbp_data, dev))
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500716 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500717
718#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
Aaron Durbinbe985242012-12-12 12:40:33 -0600719 me_print_fw_version(mbp_data.fw_version_name);
720 me_print_fwcaps(mbp_data.fw_capabilities);
Duncan Laurie144f7b22013-05-01 11:27:58 -0700721
722 if (mbp_data.plat_time) {
723 printk(BIOS_DEBUG, "ME: Wake Event to ME Reset: %u ms\n",
724 mbp_data.plat_time->wake_event_mrst_time_ms);
725 printk(BIOS_DEBUG, "ME: ME Reset to Platform Reset: %u ms\n",
726 mbp_data.plat_time->mrst_pltrst_time_ms);
727 printk(BIOS_DEBUG, "ME: Platform Reset to CPU Reset: %u ms\n",
728 mbp_data.plat_time->pltrst_cpurst_time_ms);
729 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500730#endif
731
Duncan Laurieaf980622013-07-18 23:02:18 -0700732 /*
733 * Leave the ME unlocked. It will be locked via SMI command later.
734 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500735}
736
737static void set_subsystem(device_t dev, unsigned vendor, unsigned device)
738{
739 if (!vendor || !device) {
740 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
741 pci_read_config32(dev, PCI_VENDOR_ID));
742 } else {
743 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
744 ((device & 0xffff) << 16) | (vendor & 0xffff));
745 }
746}
747
748static struct pci_operations pci_ops = {
749 .set_subsystem = set_subsystem,
750};
751
752static struct device_operations device_ops = {
753 .read_resources = pci_dev_read_resources,
754 .set_resources = pci_dev_set_resources,
755 .enable_resources = pci_dev_enable_resources,
756 .init = intel_me_init,
757 .scan_bus = scan_static_bus,
758 .ops_pci = &pci_ops,
759};
760
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800761static const unsigned short pci_device_ids[] = {
762 0x8c3a, /* Mobile */
763 0x9c3a, /* Low Power */
764 0
765};
766
Aaron Durbin76c37002012-10-30 09:03:43 -0500767static const struct pci_driver intel_me __pci_driver = {
768 .ops = &device_ops,
769 .vendor = PCI_VENDOR_ID_INTEL,
Duncan Laurie26e7dd72012-12-19 09:12:31 -0800770 .devices= pci_device_ids,
Aaron Durbin76c37002012-10-30 09:03:43 -0500771};
772
773/******************************************************************************
774 * */
775static u32 me_to_host_words_pending(void)
776{
777 struct mei_csr me;
778 read_me_csr(&me);
779 if (!me.ready)
780 return 0;
781 return (me.buffer_write_ptr - me.buffer_read_ptr) &
782 (me.buffer_depth - 1);
783}
784
785#if 0
786/* This function is not yet being used, keep it in for the future. */
787static u32 host_to_me_words_room(void)
788{
789 struct mei_csr csr;
790
791 read_me_csr(&csr);
792 if (!csr.ready)
793 return 0;
794
795 read_host_csr(&csr);
796 return (csr.buffer_read_ptr - csr.buffer_write_ptr - 1) &
797 (csr.buffer_depth - 1);
798}
799#endif
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500800
801/*
802 * mbp give up routine. This path is taken if hfs.mpb_rdy is 0 or the read
803 * state machine on the BIOS end doesn't match the ME's state machine.
804 */
805static void intel_me_mbp_give_up(device_t dev)
806{
807 u32 reg32;
808 struct mei_csr csr;
809
810 reg32 = PCI_ME_MBP_GIVE_UP;
Aaron Durbinb37d1fb2013-02-25 10:51:52 -0600811 pci_write_config32(dev, PCI_ME_H_GS2, reg32);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500812 read_host_csr(&csr);
813 csr.reset = 1;
814 csr.interrupt_generate = 1;
815 write_host_csr(&csr);
816}
817
Aaron Durbinbe985242012-12-12 12:40:33 -0600818struct mbp_payload {
819 mbp_header header;
820 u32 data[0];
821};
822
Aaron Durbin76c37002012-10-30 09:03:43 -0500823/*
824 * mbp seems to be following its own flow, let's retrieve it in a dedicated
825 * function.
826 */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500827static int intel_me_read_mbp(me_bios_payload *mbp_data, device_t dev)
Aaron Durbin76c37002012-10-30 09:03:43 -0500828{
829 mbp_header mbp_hdr;
Aaron Durbin76c37002012-10-30 09:03:43 -0500830 u32 me2host_pending;
Aaron Durbin76c37002012-10-30 09:03:43 -0500831 struct mei_csr host;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500832 struct me_hfs2 hfs2;
833 int count;
Aaron Durbinbe985242012-12-12 12:40:33 -0600834 struct mbp_payload *mbp;
835 int i;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500836
837 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
838
839 if (!hfs2.mbp_rdy) {
840 printk(BIOS_ERR, "ME: MBP not ready\n");
841 goto mbp_failure;
842 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500843
844 me2host_pending = me_to_host_words_pending();
845 if (!me2host_pending) {
846 printk(BIOS_ERR, "ME: no mbp data!\n");
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500847 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500848 }
849
850 /* we know for sure that at least the header is there */
851 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
852
853 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
854 (me2host_pending < mbp_hdr.mbp_size)) {
855 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
856 " buffer contains %d words\n",
857 mbp_hdr.num_entries, mbp_hdr.mbp_size,
858 me2host_pending);
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500859 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500860 }
Aaron Durbinbe985242012-12-12 12:40:33 -0600861 mbp = malloc(mbp_hdr.mbp_size * sizeof(u32));
862 if (!mbp)
863 goto mbp_failure;
Aaron Durbin76c37002012-10-30 09:03:43 -0500864
Aaron Durbinbe985242012-12-12 12:40:33 -0600865 mbp->header = mbp_hdr;
Aaron Durbin76c37002012-10-30 09:03:43 -0500866 me2host_pending--;
Aaron Durbin76c37002012-10-30 09:03:43 -0500867
Aaron Durbinbe985242012-12-12 12:40:33 -0600868 i = 0;
869 while (i != me2host_pending) {
870 mei_read_dword_ptr(&mbp->data[i], MEI_ME_CB_RW);
871 i++;
Aaron Durbin76c37002012-10-30 09:03:43 -0500872 }
873
Aaron Durbinbe985242012-12-12 12:40:33 -0600874 /* Signal to the ME that the host has finished reading the MBP. */
Aaron Durbin76c37002012-10-30 09:03:43 -0500875 read_host_csr(&host);
876 host.interrupt_generate = 1;
877 write_host_csr(&host);
878
Aaron Durbinbe985242012-12-12 12:40:33 -0600879 /* Wait for the mbp_cleared indicator. */
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500880 for (count = ME_RETRY; count > 0; --count) {
881 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
882 if (hfs2.mbp_cleared)
883 break;
884 udelay(ME_DELAY);
885 }
886
887 if (count == 0) {
888 printk(BIOS_WARNING, "ME: Timeout waiting for mbp_cleared\n");
889 intel_me_mbp_give_up(dev);
Aaron Durbin76c37002012-10-30 09:03:43 -0500890 }
891
Aaron Durbinbe985242012-12-12 12:40:33 -0600892 /* Dump out the MBP contents. */
893#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
894 printk(BIOS_INFO, "ME MBP: Header: items: %d, size dw: %d\n",
895 mbp->header.num_entries, mbp->header.mbp_size);
896 for (i = 0; i < mbp->header.mbp_size - 1; i++) {
897 printk(BIOS_INFO, "ME MBP: %04x: 0x%08x\n", i, mbp->data[i]);
898 }
899#endif
900
901 #define ASSIGN_FIELD_PTR(field_,val_) \
902 { \
903 mbp_data->field_ = (typeof(mbp_data->field_))(void *)val_; \
904 break; \
905 }
906 /* Setup the pointers in the me_bios_payload structure. */
907 for (i = 0; i < mbp->header.mbp_size - 1;) {
908 mbp_item_header *item = (void *)&mbp->data[i];
909
910 switch(MBP_MAKE_IDENT(item->app_id, item->item_id)) {
911 case MBP_IDENT(KERNEL, FW_VER):
912 ASSIGN_FIELD_PTR(fw_version_name, &mbp->data[i+1]);
913
914 case MBP_IDENT(ICC, PROFILE):
915 ASSIGN_FIELD_PTR(icc_profile, &mbp->data[i+1]);
916
917 case MBP_IDENT(INTEL_AT, STATE):
918 ASSIGN_FIELD_PTR(at_state, &mbp->data[i+1]);
919
920 case MBP_IDENT(KERNEL, FW_CAP):
921 ASSIGN_FIELD_PTR(fw_capabilities, &mbp->data[i+1]);
922
923 case MBP_IDENT(KERNEL, ROM_BIST):
924 ASSIGN_FIELD_PTR(rom_bist_data, &mbp->data[i+1]);
925
926 case MBP_IDENT(KERNEL, PLAT_KEY):
927 ASSIGN_FIELD_PTR(platform_key, &mbp->data[i+1]);
928
929 case MBP_IDENT(KERNEL, FW_TYPE):
930 ASSIGN_FIELD_PTR(fw_plat_type, &mbp->data[i+1]);
931
932 case MBP_IDENT(KERNEL, MFS_FAILURE):
933 ASSIGN_FIELD_PTR(mfsintegrity, &mbp->data[i+1]);
934
Duncan Laurie144f7b22013-05-01 11:27:58 -0700935 case MBP_IDENT(KERNEL, PLAT_TIME):
936 ASSIGN_FIELD_PTR(plat_time, &mbp->data[i+1]);
937
938 case MBP_IDENT(NFC, SUPPORT_DATA):
939 ASSIGN_FIELD_PTR(nfc_data, &mbp->data[i+1]);
940
Aaron Durbinbe985242012-12-12 12:40:33 -0600941 default:
942 printk(BIOS_ERR, "ME MBP: unknown item 0x%x @ dw offset 0x%x\n",
943 mbp->data[i], i);
944 break;
945 }
946 i += item->length;
947 }
948 #undef ASSIGN_FIELD_PTR
949
Aaron Durbin76c37002012-10-30 09:03:43 -0500950 return 0;
Aaron Durbin9aa031e2012-11-02 09:16:46 -0500951
952mbp_failure:
953 intel_me_mbp_give_up(dev);
954 return -1;
Aaron Durbin76c37002012-10-30 09:03:43 -0500955}
Duncan Laurieaf980622013-07-18 23:02:18 -0700956
957#endif /* !__SMM__ */