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