blob: 8cd605fe688b5478c0a76fb0c4e541db2d73700d [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070018 */
19
20/*
21 * This is a ramstage driver for the Intel Management Engine found in the
22 * southbridge. It handles the required boot-time messages over the
23 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
24 * finished with POST. Additional messages are defined for debug but are
25 * not used unless the console loglevel is high enough.
26 */
27
28#include <arch/acpi.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070029#include <arch/io.h>
30#include <console/console.h>
31#include <device/device.h>
32#include <device/pci.h>
33#include <device/pci_ids.h>
34#include <device/pci_def.h>
35#include <string.h>
36#include <delay.h>
37#include <elog.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070038#include <soc/me.h>
39#include <soc/lpc.h>
40#include <soc/pch.h>
41#include <soc/pci_devs.h>
42#include <soc/ramstage.h>
43#include <soc/rcba.h>
44#include <soc/intel/broadwell/chip.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070045
46#if CONFIG_CHROMEOS
47#include <vendorcode/google/chromeos/chromeos.h>
48#include <vendorcode/google/chromeos/gnvs.h>
49#endif
50
51/* 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};
Duncan Lauriec88c54c2014-04-30 16:36:13 -070060
61/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080062static u8 *mei_base_address;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070063
64#if CONFIG_DEBUG_INTEL_ME
65static void mei_dump(void *ptr, int dword, int offset, const char *type)
66{
67 struct mei_csr *csr;
68
69 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
70
71 switch (offset) {
72 case MEI_H_CSR:
73 case MEI_ME_CSR_HA:
74 csr = ptr;
75 if (!csr) {
76 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
77 break;
78 }
79 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
80 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
81 csr->buffer_read_ptr, csr->buffer_write_ptr,
82 csr->ready, csr->reset, csr->interrupt_generate,
83 csr->interrupt_status, csr->interrupt_enable);
84 break;
85 case MEI_ME_CB_RW:
86 case MEI_H_CB_WW:
87 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
88 break;
89 default:
90 printk(BIOS_SPEW, "0x%08x\n", offset);
91 break;
92 }
93}
94#else
95# define mei_dump(ptr,dword,offset,type) do {} while (0)
96#endif
97
98/*
99 * ME/MEI access helpers using memcpy to avoid aliasing.
100 */
101
102static inline void mei_read_dword_ptr(void *ptr, int offset)
103{
104 u32 dword = read32(mei_base_address + offset);
105 memcpy(ptr, &dword, sizeof(dword));
106 mei_dump(ptr, dword, offset, "READ");
107}
108
109static inline void mei_write_dword_ptr(void *ptr, int offset)
110{
111 u32 dword = 0;
112 memcpy(&dword, ptr, sizeof(dword));
113 write32(mei_base_address + offset, dword);
114 mei_dump(ptr, dword, offset, "WRITE");
115}
116
117static inline void pci_read_dword_ptr(device_t dev, void *ptr, int offset)
118{
119 u32 dword = pci_read_config32(dev, offset);
120 memcpy(ptr, &dword, sizeof(dword));
121 mei_dump(ptr, dword, offset, "PCI READ");
122}
123
124static inline void read_host_csr(struct mei_csr *csr)
125{
126 mei_read_dword_ptr(csr, MEI_H_CSR);
127}
128
129static inline void write_host_csr(struct mei_csr *csr)
130{
131 mei_write_dword_ptr(csr, MEI_H_CSR);
132}
133
134static inline void read_me_csr(struct mei_csr *csr)
135{
136 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
137}
138
139static inline void write_cb(u32 dword)
140{
141 write32(mei_base_address + MEI_H_CB_WW, dword);
142 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
143}
144
145static inline u32 read_cb(void)
146{
147 u32 dword = read32(mei_base_address + MEI_ME_CB_RW);
148 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
149 return dword;
150}
151
152/* Wait for ME ready bit to be asserted */
153static int mei_wait_for_me_ready(void)
154{
155 struct mei_csr me;
156 unsigned try = ME_RETRY;
157
158 while (try--) {
159 read_me_csr(&me);
160 if (me.ready)
161 return 0;
162 udelay(ME_DELAY);
163 }
164
165 printk(BIOS_ERR, "ME: failed to become ready\n");
166 return -1;
167}
168
169static void mei_reset(void)
170{
171 struct mei_csr host;
172
173 if (mei_wait_for_me_ready() < 0)
174 return;
175
176 /* Reset host and ME circular buffers for next message */
177 read_host_csr(&host);
178 host.reset = 1;
179 host.interrupt_generate = 1;
180 write_host_csr(&host);
181
182 if (mei_wait_for_me_ready() < 0)
183 return;
184
185 /* Re-init and indicate host is ready */
186 read_host_csr(&host);
187 host.interrupt_generate = 1;
188 host.ready = 1;
189 host.reset = 0;
190 write_host_csr(&host);
191}
192
193static int mei_send_packet(struct mei_header *mei, void *req_data)
194{
195 struct mei_csr host;
196 unsigned ndata, n;
197 u32 *data;
198
199 /* Number of dwords to write */
200 ndata = mei->length >> 2;
201
202 /* Pad non-dword aligned request message length */
203 if (mei->length & 3)
204 ndata++;
205 if (!ndata) {
206 printk(BIOS_DEBUG, "ME: request has no data\n");
207 return -1;
208 }
209 ndata++; /* Add MEI header */
210
211 /*
212 * Make sure there is still room left in the circular buffer.
213 * Reset the buffer pointers if the requested message will not fit.
214 */
215 read_host_csr(&host);
216 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
217 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
218 mei_reset();
219 read_host_csr(&host);
220 }
221
222 /* Ensure the requested length will fit in the circular buffer. */
223 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
224 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
225 ndata + 2, host.buffer_depth);
226 return -1;
227 }
228
229 /* Write MEI header */
230 mei_write_dword_ptr(mei, MEI_H_CB_WW);
231 ndata--;
232
233 /* Write message data */
234 data = req_data;
235 for (n = 0; n < ndata; ++n)
236 write_cb(*data++);
237
238 /* Generate interrupt to the ME */
239 read_host_csr(&host);
240 host.interrupt_generate = 1;
241 write_host_csr(&host);
242
243 /* Make sure ME is ready after sending request data */
244 return mei_wait_for_me_ready();
245}
246
247static int mei_send_data(u8 me_address, u8 host_address,
248 void *req_data, int req_bytes)
249{
250 struct mei_header header = {
251 .client_address = me_address,
252 .host_address = host_address,
253 };
254 struct mei_csr host;
255 int current = 0;
256 u8 *req_ptr = req_data;
257
258 while (!header.is_complete) {
259 int remain = req_bytes - current;
260 int buf_len;
261
262 read_host_csr(&host);
263 buf_len = host.buffer_depth - host.buffer_write_ptr;
264
265 if (buf_len > remain) {
266 /* Send all remaining data as final message */
267 header.length = req_bytes - current;
268 header.is_complete = 1;
269 } else {
270 /* Send as much data as the buffer can hold */
271 header.length = buf_len;
272 }
273
274 mei_send_packet(&header, req_ptr);
275
276 req_ptr += header.length;
277 current += header.length;
278 }
279
280 return 0;
281}
282
283static int mei_send_header(u8 me_address, u8 host_address,
284 void *header, int header_len, int complete)
285{
286 struct mei_header mei = {
287 .client_address = me_address,
288 .host_address = host_address,
289 .length = header_len,
290 .is_complete = complete,
291 };
292 return mei_send_packet(&mei, header);
293}
294
295static int mei_recv_msg(void *header, int header_bytes,
296 void *rsp_data, int rsp_bytes)
297{
298 struct mei_header mei_rsp;
299 struct mei_csr me, host;
300 unsigned ndata, n;
301 unsigned expected;
302 u32 *data;
303
304 /* Total number of dwords to read from circular buffer */
305 expected = (rsp_bytes + sizeof(mei_rsp) + header_bytes) >> 2;
306 if (rsp_bytes & 3)
307 expected++;
308
309 if (mei_wait_for_me_ready() < 0)
310 return -1;
311
312 /*
313 * The interrupt status bit does not appear to indicate that the
314 * message has actually been received. Instead we wait until the
315 * expected number of dwords are present in the circular buffer.
316 */
317 for (n = ME_RETRY; n; --n) {
318 read_me_csr(&me);
319 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
320 break;
321 udelay(ME_DELAY);
322 }
323 if (!n) {
324 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
325 "%u, available %u\n", expected,
326 me.buffer_write_ptr - me.buffer_read_ptr);
327 return -1;
328 }
329
330 /* Read and verify MEI response header from the ME */
331 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
332 if (!mei_rsp.is_complete) {
333 printk(BIOS_ERR, "ME: response is not complete\n");
334 return -1;
335 }
336
337 /* Handle non-dword responses and expect at least the header */
338 ndata = mei_rsp.length >> 2;
339 if (mei_rsp.length & 3)
340 ndata++;
341 if (ndata != (expected - 1)) {
342 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
343 ndata, (expected - 1));
344 return -1;
345 }
346
347 /* Read response header from the ME */
348 data = header;
349 for (n = 0; n < (header_bytes >> 2); ++n)
350 *data++ = read_cb();
351 ndata -= header_bytes >> 2;
352
353 /* Make sure caller passed a buffer with enough space */
354 if (ndata != (rsp_bytes >> 2)) {
355 printk(BIOS_ERR, "ME: not enough room in response buffer: "
356 "%u != %u\n", ndata, rsp_bytes >> 2);
357 return -1;
358 }
359
360 /* Read response data from the circular buffer */
361 data = rsp_data;
362 for (n = 0; n < ndata; ++n)
363 *data++ = read_cb();
364
365 /* Tell the ME that we have consumed the response */
366 read_host_csr(&host);
367 host.interrupt_status = 1;
368 host.interrupt_generate = 1;
369 write_host_csr(&host);
370
371 return mei_wait_for_me_ready();
372}
373
374static inline int mei_sendrecv_mkhi(struct mkhi_header *mkhi,
375 void *req_data, int req_bytes,
376 void *rsp_data, int rsp_bytes)
377{
378 struct mkhi_header mkhi_rsp;
379
380 /* Send header */
381 if (mei_send_header(MEI_ADDRESS_MKHI, MEI_HOST_ADDRESS,
382 mkhi, sizeof(*mkhi), req_bytes ? 0 : 1) < 0)
383 return -1;
384
385 /* Send data if available */
386 if (req_bytes && mei_send_data(MEI_ADDRESS_MKHI, MEI_HOST_ADDRESS,
387 req_data, req_bytes) < 0)
388 return -1;
389
390 /* Return now if no response expected */
391 if (!rsp_bytes)
392 return 0;
393
394 /* Read header and data */
395 if (mei_recv_msg(&mkhi_rsp, sizeof(mkhi_rsp),
396 rsp_data, rsp_bytes) < 0)
397 return -1;
398
399 if (!mkhi_rsp.is_response ||
400 mkhi->group_id != mkhi_rsp.group_id ||
401 mkhi->command != mkhi_rsp.command) {
402 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u,"
403 "command %u ?= %u, is_response %u\n", mkhi->group_id,
404 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
405 mkhi_rsp.is_response);
406 return -1;
407 }
408
409 return 0;
410}
411
412static inline int mei_sendrecv_icc(struct icc_header *icc,
413 void *req_data, int req_bytes,
414 void *rsp_data, int rsp_bytes)
415{
416 struct icc_header icc_rsp;
417
418 /* Send header */
419 if (mei_send_header(MEI_ADDRESS_ICC, MEI_HOST_ADDRESS,
420 icc, sizeof(*icc), req_bytes ? 0 : 1) < 0)
421 return -1;
422
423 /* Send data if available */
424 if (req_bytes && mei_send_data(MEI_ADDRESS_ICC, MEI_HOST_ADDRESS,
425 req_data, req_bytes) < 0)
426 return -1;
427
428 /* Read header and data, if needed */
429 if (rsp_bytes && mei_recv_msg(&icc_rsp, sizeof(icc_rsp),
430 rsp_data, rsp_bytes) < 0)
431 return -1;
432
433 return 0;
434}
435
436/*
437 * mbp give up routine. This path is taken if hfs.mpb_rdy is 0 or the read
438 * state machine on the BIOS end doesn't match the ME's state machine.
439 */
440static void intel_me_mbp_give_up(device_t dev)
441{
442 struct mei_csr csr;
443
444 pci_write_config32(dev, PCI_ME_H_GS2, PCI_ME_MBP_GIVE_UP);
445
446 read_host_csr(&csr);
447 csr.reset = 1;
448 csr.interrupt_generate = 1;
449 write_host_csr(&csr);
450}
451
452/*
453 * mbp clear routine. This will wait for the ME to indicate that
454 * the MBP has been read and cleared.
455 */
Duncan Lauriec99681f2014-12-10 08:11:09 -0800456static void intel_me_mbp_clear(device_t dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700457{
458 int count;
459 struct me_hfs2 hfs2;
460
461 /* Wait for the mbp_cleared indicator */
462 for (count = ME_RETRY; count > 0; --count) {
463 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
464 if (hfs2.mbp_cleared)
465 break;
466 udelay(ME_DELAY);
467 }
468
469 if (count == 0) {
470 printk(BIOS_WARNING, "ME: Timeout waiting for mbp_cleared\n");
471 intel_me_mbp_give_up(dev);
472 } else {
473 printk(BIOS_INFO, "ME: MBP cleared\n");
474 }
475}
476
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700477static void me_print_fw_version(mbp_fw_version_name *vers_name)
478{
479 if (!vers_name) {
480 printk(BIOS_ERR, "ME: mbp missing version report\n");
481 return;
482 }
483
484 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
485 vers_name->major_version, vers_name->minor_version,
486 vers_name->hotfix_version, vers_name->build_version);
487}
488
489#if CONFIG_DEBUG_INTEL_ME
Edward O'Callaghan8cc5dc12015-01-07 15:50:43 +1100490static inline void print_cap(const char *name, int state)
491{
492 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
493 name, state ? " en" : "dis");
494}
495
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700496/* Get ME Firmware Capabilities */
497static int mkhi_get_fwcaps(mbp_mefwcaps *cap)
498{
499 u32 rule_id = 0;
500 struct me_fwcaps cap_msg;
501 struct mkhi_header mkhi = {
502 .group_id = MKHI_GROUP_ID_FWCAPS,
503 .command = MKHI_FWCAPS_GET_RULE,
504 };
505
506 /* Send request and wait for response */
507 if (mei_sendrecv_mkhi(&mkhi, &rule_id, sizeof(u32),
508 &cap_msg, sizeof(cap_msg)) < 0) {
509 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
510 return -1;
511 }
512 *cap = cap_msg.caps_sku;
513 return 0;
514}
515
516/* Get ME Firmware Capabilities */
517static void me_print_fwcaps(mbp_mefwcaps *cap)
518{
519 mbp_mefwcaps local_caps;
520 if (!cap) {
521 cap = &local_caps;
522 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
523 if (mkhi_get_fwcaps(cap))
524 return;
525 }
526
527 print_cap("Full Network manageability", cap->full_net);
528 print_cap("Regular Network manageability", cap->std_net);
529 print_cap("Manageability", cap->manageability);
530 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
531 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
532 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
533 print_cap("ICC Over Clocking", cap->icc_over_clocking);
Edward O'Callaghan8cc5dc12015-01-07 15:50:43 +1100534 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700535 print_cap("IPV6", cap->ipv6);
536 print_cap("KVM Remote Control (KVM)", cap->kvm);
537 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
538 print_cap("Virtual LAN (VLAN)", cap->vlan);
539 print_cap("TLS", cap->tls);
540 print_cap("Wireless LAN (WLAN)", cap->wlan);
541}
542#endif
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700543
544/* Send END OF POST message to the ME */
545static int mkhi_end_of_post(void)
546{
547 struct mkhi_header mkhi = {
548 .group_id = MKHI_GROUP_ID_GEN,
549 .command = MKHI_END_OF_POST,
550 };
551 u32 eop_ack;
552
553 /* Send request and wait for response */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700554 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, &eop_ack, sizeof(eop_ack)) < 0) {
555 printk(BIOS_ERR, "ME: END OF POST message failed\n");
556 return -1;
557 }
558
559 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
560 return 0;
561}
562
Duncan Lauriec99681f2014-12-10 08:11:09 -0800563/* Send END OF POST message to the ME */
564static int mkhi_end_of_post_noack(void)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700565{
Duncan Lauriec99681f2014-12-10 08:11:09 -0800566 struct mkhi_header mkhi = {
567 .group_id = MKHI_GROUP_ID_GEN,
568 .command = MKHI_END_OF_POST_NOACK,
569 };
570
571 /* Send request, do not wait for response */
572 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, NULL, 0) < 0) {
573 printk(BIOS_ERR, "ME: END OF POST NOACK message failed\n");
574 return -1;
575 }
576
577 printk(BIOS_INFO, "ME: END OF POST NOACK message successful\n");
578 return 0;
579}
580
581/* Send HMRFPO LOCK message to the ME */
582static int mkhi_hmrfpo_lock(void)
583{
584 struct mkhi_header mkhi = {
585 .group_id = MKHI_GROUP_ID_HMRFPO,
586 .command = MKHI_HMRFPO_LOCK,
587 };
588 u32 ack;
589
590 /* Send request and wait for response */
591 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, &ack, sizeof(ack)) < 0) {
592 printk(BIOS_ERR, "ME: HMRFPO LOCK message failed\n");
593 return -1;
594 }
595
596 printk(BIOS_INFO, "ME: HMRPFO LOCK message successful (%d)\n", ack);
597 return 0;
598}
599
600/* Send HMRFPO LOCK message to the ME, do not wait for response */
601static int mkhi_hmrfpo_lock_noack(void)
602{
603 struct mkhi_header mkhi = {
604 .group_id = MKHI_GROUP_ID_HMRFPO,
605 .command = MKHI_HMRFPO_LOCK_NOACK,
606 };
607
608 /* Send request, do not wait for response */
609 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, NULL, 0) < 0) {
610 printk(BIOS_ERR, "ME: HMRFPO LOCK NOACK message failed\n");
611 return -1;
612 }
613
614 printk(BIOS_INFO, "ME: HMRPFO LOCK NOACK message successful\n");
615 return 0;
616}
617
618static void intel_me_finalize(device_t dev)
619{
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700620 u32 reg32;
621
622 /* S3 path will have hidden this device already */
Duncan Lauriec99681f2014-12-10 08:11:09 -0800623 if (!mei_base_address || mei_base_address == (u8*) 0xfffffff0)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700624 return;
625
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700626 /* Make sure IO is disabled */
627 reg32 = pci_read_config32(dev, PCI_COMMAND);
628 reg32 &= ~(PCI_COMMAND_MASTER |
629 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
630 pci_write_config32(dev, PCI_COMMAND, reg32);
631
632 /* Hide the PCI device */
633 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800634 RCBA32(FD2);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700635}
636
637static int me_icc_set_clock_enables(u32 mask)
638{
639 struct icc_clock_enables_msg clk = {
640 .clock_enables = 0, /* Turn off specified clocks */
641 .clock_mask = mask,
642 .no_response = 1, /* Do not expect response */
643 };
644 struct icc_header icc = {
645 .api_version = ICC_API_VERSION_LYNXPOINT,
646 .icc_command = ICC_SET_CLOCK_ENABLES,
647 .length = sizeof(clk),
648 };
649
650 /* Send request and wait for response */
651 if (mei_sendrecv_icc(&icc, &clk, sizeof(clk), NULL, 0) < 0) {
652 printk(BIOS_ERR, "ME: ICC SET CLOCK ENABLES message failed\n");
653 return -1;
654 } else {
655 printk(BIOS_INFO, "ME: ICC SET CLOCK ENABLES 0x%08x\n", mask);
656 }
657
658 return 0;
659}
660
661/* Determine the path that we should take based on ME status */
662static me_bios_path intel_me_path(device_t dev)
663{
664 me_bios_path path = ME_DISABLE_BIOS_PATH;
665 struct me_hfs hfs;
666 struct me_hfs2 hfs2;
667
668 /* Check and dump status */
669 intel_me_status();
670
671 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
672 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
673
674 /* Check Current Working State */
675 switch (hfs.working_state) {
676 case ME_HFS_CWS_NORMAL:
677 path = ME_NORMAL_BIOS_PATH;
678 break;
679 case ME_HFS_CWS_REC:
680 path = ME_RECOVERY_BIOS_PATH;
681 break;
682 default:
683 path = ME_DISABLE_BIOS_PATH;
684 break;
685 }
686
687 /* Check Current Operation Mode */
688 switch (hfs.operation_mode) {
689 case ME_HFS_MODE_NORMAL:
690 break;
691 case ME_HFS_MODE_DEBUG:
692 case ME_HFS_MODE_DIS:
693 case ME_HFS_MODE_OVER_JMPR:
694 case ME_HFS_MODE_OVER_MEI:
695 default:
696 path = ME_DISABLE_BIOS_PATH;
697 break;
698 }
699
700 /* Check for any error code and valid firmware and MBP */
701 if (hfs.error_code || hfs.fpt_bad)
702 path = ME_ERROR_BIOS_PATH;
703
704 /* Check if the MBP is ready */
705 if (!hfs2.mbp_rdy) {
706 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
707 __FUNCTION__);
708 path = ME_ERROR_BIOS_PATH;
709 }
710
711#if CONFIG_ELOG
712 if (path != ME_NORMAL_BIOS_PATH) {
713 struct elog_event_data_me_extended data = {
714 .current_working_state = hfs.working_state,
715 .operation_state = hfs.operation_state,
716 .operation_mode = hfs.operation_mode,
717 .error_code = hfs.error_code,
718 .progress_code = hfs2.progress_code,
719 .current_pmevent = hfs2.current_pmevent,
720 .current_state = hfs2.current_state,
721 };
722 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
723 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
724 &data, sizeof(data));
725 }
726#endif
727
728 return path;
729}
730
731/* Prepare ME for MEI messages */
732static int intel_mei_setup(device_t dev)
733{
734 struct resource *res;
735 struct mei_csr host;
736 u32 reg32;
737
738 /* Find the MMIO base for the ME interface */
739 res = find_resource(dev, PCI_BASE_ADDRESS_0);
740 if (!res || res->base == 0 || res->size == 0) {
741 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
742 return -1;
743 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800744 mei_base_address = res2mmio(res, 0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700745
746 /* Ensure Memory and Bus Master bits are set */
747 reg32 = pci_read_config32(dev, PCI_COMMAND);
748 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
749 pci_write_config32(dev, PCI_COMMAND, reg32);
750
751 /* Clean up status for next message */
752 read_host_csr(&host);
753 host.interrupt_generate = 1;
754 host.ready = 1;
755 host.reset = 0;
756 write_host_csr(&host);
757
758 return 0;
759}
760
761/* Read the Extend register hash of ME firmware */
762static int intel_me_extend_valid(device_t dev)
763{
764 struct me_heres status;
765 u32 extend[8] = {0};
766 int i, count = 0;
767
768 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
769 if (!status.extend_feature_present) {
770 printk(BIOS_ERR, "ME: Extend Feature not present\n");
771 return -1;
772 }
773
774 if (!status.extend_reg_valid) {
775 printk(BIOS_ERR, "ME: Extend Register not valid\n");
776 return -1;
777 }
778
779 switch (status.extend_reg_algorithm) {
780 case PCI_ME_EXT_SHA1:
781 count = 5;
782 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
783 break;
784 case PCI_ME_EXT_SHA256:
785 count = 8;
786 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
787 break;
788 default:
789 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
790 status.extend_reg_algorithm);
791 return -1;
792 }
793
794 for (i = 0; i < count; ++i) {
795 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
796 printk(BIOS_DEBUG, "%08x", extend[i]);
797 }
798 printk(BIOS_DEBUG, "\n");
799
800#if CONFIG_CHROMEOS
801 /* Save hash in NVS for the OS to verify */
802 chromeos_set_me_hash(extend, count);
803#endif
804
805 return 0;
806}
807
Duncan Lauriec99681f2014-12-10 08:11:09 -0800808static void intel_me_print_mbp(me_bios_payload *mbp_data)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700809{
Duncan Lauriec99681f2014-12-10 08:11:09 -0800810 me_print_fw_version(mbp_data->fw_version_name);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700811
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700812#if CONFIG_DEBUG_INTEL_ME
Duncan Lauriec99681f2014-12-10 08:11:09 -0800813 me_print_fwcaps(mbp_data->fw_capabilities);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700814#endif
815
Duncan Lauriec99681f2014-12-10 08:11:09 -0800816 if (mbp_data->plat_time) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700817 printk(BIOS_DEBUG, "ME: Wake Event to ME Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800818 mbp_data->plat_time->wake_event_mrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700819 printk(BIOS_DEBUG, "ME: ME Reset to Platform Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800820 mbp_data->plat_time->mrst_pltrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700821 printk(BIOS_DEBUG, "ME: Platform Reset to CPU Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800822 mbp_data->plat_time->pltrst_cpurst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700823 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700824}
825
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700826static u32 me_to_host_words_pending(void)
827{
828 struct mei_csr me;
829 read_me_csr(&me);
830 if (!me.ready)
831 return 0;
832 return (me.buffer_write_ptr - me.buffer_read_ptr) &
833 (me.buffer_depth - 1);
834}
835
836struct mbp_payload {
837 mbp_header header;
838 u32 data[0];
839};
840
841/*
Duncan Lauriec99681f2014-12-10 08:11:09 -0800842 * Read and print ME MBP data
843 *
844 * Return -1 to indicate a problem (give up)
845 * Return 0 to indicate success (send LOCK+EOP)
846 * Return 1 to indicate success (send LOCK+EOP with NOACK)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700847 */
848static int intel_me_read_mbp(me_bios_payload *mbp_data, device_t dev)
849{
850 mbp_header mbp_hdr;
851 u32 me2host_pending;
852 struct mei_csr host;
853 struct me_hfs2 hfs2;
854 struct mbp_payload *mbp;
855 int i;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800856 int ret = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700857
858 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
859
860 if (!hfs2.mbp_rdy) {
861 printk(BIOS_ERR, "ME: MBP not ready\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800862 intel_me_mbp_give_up(dev);
863 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700864 }
865
866 me2host_pending = me_to_host_words_pending();
867 if (!me2host_pending) {
868 printk(BIOS_ERR, "ME: no mbp data!\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800869 intel_me_mbp_give_up(dev);
870 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700871 }
872
873 /* we know for sure that at least the header is there */
874 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
875
876 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
877 (me2host_pending < mbp_hdr.mbp_size)) {
878 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
879 " buffer contains %d words\n",
880 mbp_hdr.num_entries, mbp_hdr.mbp_size,
881 me2host_pending);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800882 intel_me_mbp_give_up(dev);
883 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700884 }
885 mbp = malloc(mbp_hdr.mbp_size * sizeof(u32));
Duncan Lauriec99681f2014-12-10 08:11:09 -0800886 if (!mbp) {
887 intel_me_mbp_give_up(dev);
888 return -1;
889 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700890
891 mbp->header = mbp_hdr;
892 me2host_pending--;
893
894 i = 0;
895 while (i != me2host_pending) {
896 mei_read_dword_ptr(&mbp->data[i], MEI_ME_CB_RW);
897 i++;
898 }
899
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700900 read_host_csr(&host);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800901
902 /* Check that read and write pointers are equal. */
903 if (host.buffer_read_ptr != host.buffer_write_ptr) {
904 printk(BIOS_INFO, "ME: MBP Read/Write pointer mismatch\n");
905 printk(BIOS_INFO, "ME: MBP Waiting for MBP cleared flag\n");
906
907 /* Tell ME that the host has finished reading the MBP. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700908 host.interrupt_generate = 1;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800909 host.reset = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700910 write_host_csr(&host);
911
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700912 /* Wait for the mbp_cleared indicator. */
913 intel_me_mbp_clear(dev);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800914 } else {
915 /* Indicate NOACK messages should be used. */
916 ret = 1;
917 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700918
919 /* Dump out the MBP contents. */
Duncan Lauriec99681f2014-12-10 08:11:09 -0800920#if CONFIG_DEBUG_INTEL_ME
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700921 printk(BIOS_INFO, "ME MBP: Header: items: %d, size dw: %d\n",
922 mbp->header.num_entries, mbp->header.mbp_size);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700923 for (i = 0; i < mbp->header.mbp_size - 1; i++) {
924 printk(BIOS_INFO, "ME MBP: %04x: 0x%08x\n", i, mbp->data[i]);
925 }
926#endif
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700927
928#define ASSIGN_FIELD_PTR(field_,val_) \
929 { \
930 mbp_data->field_ = (typeof(mbp_data->field_))(void *)val_; \
931 break; \
932 }
933
934 /* Setup the pointers in the me_bios_payload structure. */
935 for (i = 0; i < mbp->header.mbp_size - 1;) {
936 mbp_item_header *item = (void *)&mbp->data[i];
937
938 switch(MBP_MAKE_IDENT(item->app_id, item->item_id)) {
939 case MBP_IDENT(KERNEL, FW_VER):
940 ASSIGN_FIELD_PTR(fw_version_name, &mbp->data[i+1]);
941
942 case MBP_IDENT(ICC, PROFILE):
943 ASSIGN_FIELD_PTR(icc_profile, &mbp->data[i+1]);
944
945 case MBP_IDENT(INTEL_AT, STATE):
946 ASSIGN_FIELD_PTR(at_state, &mbp->data[i+1]);
947
948 case MBP_IDENT(KERNEL, FW_CAP):
949 ASSIGN_FIELD_PTR(fw_capabilities, &mbp->data[i+1]);
950
951 case MBP_IDENT(KERNEL, ROM_BIST):
952 ASSIGN_FIELD_PTR(rom_bist_data, &mbp->data[i+1]);
953
954 case MBP_IDENT(KERNEL, PLAT_KEY):
955 ASSIGN_FIELD_PTR(platform_key, &mbp->data[i+1]);
956
957 case MBP_IDENT(KERNEL, FW_TYPE):
958 ASSIGN_FIELD_PTR(fw_plat_type, &mbp->data[i+1]);
959
960 case MBP_IDENT(KERNEL, MFS_FAILURE):
961 ASSIGN_FIELD_PTR(mfsintegrity, &mbp->data[i+1]);
962
963 case MBP_IDENT(KERNEL, PLAT_TIME):
964 ASSIGN_FIELD_PTR(plat_time, &mbp->data[i+1]);
965
966 case MBP_IDENT(NFC, SUPPORT_DATA):
967 ASSIGN_FIELD_PTR(nfc_data, &mbp->data[i+1]);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700968 }
969 i += item->length;
970 }
971 #undef ASSIGN_FIELD_PTR
972
Duncan Lauriec99681f2014-12-10 08:11:09 -0800973 return ret;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700974}
Duncan Lauriec99681f2014-12-10 08:11:09 -0800975
976/* Check whether ME is present and do basic init */
977static void intel_me_init(device_t dev)
978{
979 config_t *config = dev->chip_info;
980 me_bios_path path = intel_me_path(dev);
981 me_bios_payload mbp_data;
982 int mbp_ret;
983 struct me_hfs hfs;
984 struct mei_csr csr;
985
986 /* Do initial setup and determine the BIOS path */
987 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
988
989 if (path == ME_NORMAL_BIOS_PATH) {
990 /* Validate the extend register */
991 intel_me_extend_valid(dev);
992}
993
994 memset(&mbp_data, 0, sizeof(mbp_data));
995
996 /*
997 * According to the ME9 BWG, BIOS is required to fetch MBP data in
998 * all boot flows except S3 Resume.
999 */
1000
1001 /* Prepare MEI MMIO interface */
1002 if (intel_mei_setup(dev) < 0)
1003 return;
1004
1005 /* Read ME MBP data */
1006 mbp_ret = intel_me_read_mbp(&mbp_data, dev);
1007 if (mbp_ret < 0)
1008 return;
1009 intel_me_print_mbp(&mbp_data);
1010
1011 /* Set clock enables according to devicetree */
1012 if (config && config->icc_clock_disable)
1013 me_icc_set_clock_enables(config->icc_clock_disable);
1014
1015 /* Make sure ME is in a mode that expects EOP */
1016 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
1017
1018 /* Abort and leave device alone if not normal mode */
1019 if (hfs.fpt_bad ||
1020 hfs.working_state != ME_HFS_CWS_NORMAL ||
1021 hfs.operation_mode != ME_HFS_MODE_NORMAL)
1022 return;
1023
1024 if (mbp_ret) {
1025 /*
1026 * MBP Cleared wait is skipped,
1027 * Do not expect ACK and reset when complete.
1028 */
1029
1030 /* Send HMRFPO Lock command, no response */
1031 mkhi_hmrfpo_lock_noack();
1032
1033 /* Send END OF POST command, no response */
1034 mkhi_end_of_post_noack();
1035
1036 /* Assert reset and interrupt */
1037 read_host_csr(&csr);
1038 csr.interrupt_generate = 1;
1039 csr.reset = 1;
1040 write_host_csr(&csr);
1041 } else {
1042 /*
1043 * MBP Cleared wait was not skipped
1044 */
1045
1046 /* Send HMRFPO LOCK command */
1047 mkhi_hmrfpo_lock();
1048
1049 /* Send EOP command so ME stops accepting other commands */
1050 mkhi_end_of_post();
1051 }
1052}
1053
1054static void intel_me_enable(device_t dev)
1055{
Duncan Lauriec99681f2014-12-10 08:11:09 -08001056 /* Avoid talking to the device in S3 path */
Kyösti Mälkki1ec23c92015-05-29 06:18:18 +03001057 if (acpi_is_wakeup_s3()) {
Duncan Lauriec99681f2014-12-10 08:11:09 -08001058 dev->enabled = 0;
1059 pch_disable_devfn(dev);
1060 }
Duncan Lauriec99681f2014-12-10 08:11:09 -08001061}
1062
1063static struct device_operations device_ops = {
1064 .read_resources = &pci_dev_read_resources,
1065 .set_resources = &pci_dev_set_resources,
1066 .enable_resources = &pci_dev_enable_resources,
1067 .enable = &intel_me_enable,
1068 .init = &intel_me_init,
1069 .final = &intel_me_finalize,
1070 .ops_pci = &broadwell_pci_ops,
1071};
1072
1073static const unsigned short pci_device_ids[] = {
1074 0x9c3a, /* Low Power */
1075 0x9cba, /* WildcatPoint */
1076 0
1077};
1078
1079static const struct pci_driver intel_me __pci_driver = {
1080 .ops = &device_ops,
1081 .vendor = PCI_VENDOR_ID_INTEL,
1082 .devices = pci_device_ids,
1083};