blob: 1a59dc4024d9538319a48566456235b747d8e1fb [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>
Stefan Reinauer8e073822012-04-04 00:07:22 +020026#include <arch/io.h>
27#include <console/console.h>
28#include <device/pci_ids.h>
29#include <device/pci_def.h>
30#include <string.h>
31#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070032#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010033#include <halt.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020034
35#ifdef __SMM__
Kyösti Mälkkib4a45dc2013-07-26 08:53:59 +030036#include <arch/io.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020037#else
38# include <device/device.h>
39# include <device/pci.h>
40#endif
41
42#include "me.h"
43#include "pch.h"
44
Martin Roth7a1a3ad2017-06-24 21:29:38 -060045#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020046#include <vendorcode/google/chromeos/chromeos.h>
Stefan Reinauer49058c02012-06-11 14:13:09 -070047#include <vendorcode/google/chromeos/gnvs.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020048#endif
49
50#ifndef __SMM__
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};
60static int intel_me_read_mbp(me_bios_payload *mbp_data);
61#endif
62
63/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080064static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020065
Martin Roth7a1a3ad2017-06-24 21:29:38 -060066#if IS_ENABLED(CONFIG_DEBUG_INTEL_ME)
Stefan Reinauer8e073822012-04-04 00:07:22 +020067static 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{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800106 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200107 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));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800115 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116 mei_dump(ptr, dword, offset, "WRITE");
117}
118
119#ifndef __SMM__
Elyes HAOUASdc035282018-09-18 13:28:49 +0200120static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200121{
122 u32 dword = pci_read_config32(dev, offset);
123 memcpy(ptr, &dword, sizeof(dword));
124 mei_dump(ptr, dword, offset, "PCI READ");
125}
126#endif
127
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{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800145 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200146 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
147}
148
149static inline u32 read_cb(void)
150{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800151 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200152 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
Stefan Reinauer8e073822012-04-04 00:07:22 +0200354#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) && !defined(__SMM__)
355static 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{
363 if (!vers_name->major_version) {
364 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 */
374static int mkhi_get_fwcaps(mefwcaps_sku *cap)
375{
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 */
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000390 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg)) < 0) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200391 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
392 return -1;
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000393 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200394 *cap = cap_msg.caps_sku;
395 return 0;
396}
397
398/* Get ME Firmware Capabilities */
399static void me_print_fwcaps(mbp_fw_caps *caps_section)
400{
401 mefwcaps_sku *cap = &caps_section->fw_capabilities;
402 if (!caps_section->available) {
403 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
404 if (mkhi_get_fwcaps(cap))
405 return;
406 }
407
408 print_cap("Full Network manageability", cap->full_net);
409 print_cap("Regular Network manageability", cap->std_net);
410 print_cap("Manageability", cap->manageability);
411 print_cap("Small business technology", cap->small_business);
412 print_cap("Level III manageability", cap->l3manageability);
413 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);
Edward O'Callaghan152e5172014-07-13 00:28:05 +1000417 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200418 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
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600427#if IS_ENABLED(CONFIG_CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200428/* Tell ME to issue a global reset */
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700429static int mkhi_global_reset(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200430{
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... */
Patrick Georgi546953c2014-11-29 10:38:17 +0100450 halt();
Stefan Reinauer8e073822012-04-04 00:07:22 +0200451 }
452
453 /* If the ME responded it rejected the reset request */
454 printk(BIOS_ERR, "ME: Global Reset failed\n");
455 return -1;
456}
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700457#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200458
459#ifdef __SMM__
460
Duncan Laurie708f7312012-07-10 15:15:41 -0700461/* 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
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700488void intel_me8_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200489{
490 struct me_hfs hfs;
491 u32 reg32;
492
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800493 mei_base_address = (void *)
494 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200495
496 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800497 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200498 return;
499
500 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300501 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200502 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 */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300514 reg32 = pci_read_config32(PCH_ME_DEV, PCI_COMMAND);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200515 reg32 &= ~(PCI_COMMAND_MASTER |
516 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300517 pci_write_config32(PCH_ME_DEV, PCI_COMMAND, reg32);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200518
519 /* Hide the PCI device */
520 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
521}
522
523#else /* !__SMM__ */
524
525/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200526static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200527{
528 me_bios_path path = ME_DISABLE_BIOS_PATH;
529 struct me_hfs hfs;
530 struct me_gmes gmes;
531
Stefan Reinauer8e073822012-04-04 00:07:22 +0200532 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300533 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200534 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200535
536 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
537 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
538
539 /* Check and dump status */
540 intel_me_status(&hfs, &gmes);
541
Stefan Reinauer8e073822012-04-04 00:07:22 +0200542 /* Check Current Working State */
543 switch (hfs.working_state) {
544 case ME_HFS_CWS_NORMAL:
545 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200546 break;
547 case ME_HFS_CWS_REC:
548 path = ME_RECOVERY_BIOS_PATH;
549 break;
550 default:
551 path = ME_DISABLE_BIOS_PATH;
552 break;
553 }
554
555 /* Check Current Operation Mode */
556 switch (hfs.operation_mode) {
557 case ME_HFS_MODE_NORMAL:
558 break;
559 case ME_HFS_MODE_DEBUG:
560 case ME_HFS_MODE_DIS:
561 case ME_HFS_MODE_OVER_JMPR:
562 case ME_HFS_MODE_OVER_MEI:
563 default:
564 path = ME_DISABLE_BIOS_PATH;
565 break;
566 }
567
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700568 /* Check for any error code and valid firmware and MBP */
569 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200570 path = ME_ERROR_BIOS_PATH;
571
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700572 /* Check if the MBP is ready */
573 if (!gmes.mbp_rdy) {
574 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
575 __FUNCTION__);
576 path = ME_ERROR_BIOS_PATH;
577 }
578
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600579#if IS_ENABLED(CONFIG_ELOG)
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700580 if (path != ME_NORMAL_BIOS_PATH) {
581 struct elog_event_data_me_extended data = {
582 .current_working_state = hfs.working_state,
583 .operation_state = hfs.operation_state,
584 .operation_mode = hfs.operation_mode,
585 .error_code = hfs.error_code,
586 .progress_code = gmes.progress_code,
587 .current_pmevent = gmes.current_pmevent,
588 .current_state = gmes.current_state,
589 };
590 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
591 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
592 &data, sizeof(data));
593 }
594#endif
595
Stefan Reinauer8e073822012-04-04 00:07:22 +0200596 return path;
597}
598
599/* Prepare ME for MEI messages */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200600static int intel_mei_setup(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200601{
602 struct resource *res;
603 struct mei_csr host;
604 u32 reg32;
605
606 /* Find the MMIO base for the ME interface */
607 res = find_resource(dev, PCI_BASE_ADDRESS_0);
608 if (!res || res->base == 0 || res->size == 0) {
609 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
610 return -1;
611 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800612 mei_base_address = (u32 *)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200613
614 /* Ensure Memory and Bus Master bits are set */
615 reg32 = pci_read_config32(dev, PCI_COMMAND);
616 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
617 pci_write_config32(dev, PCI_COMMAND, reg32);
618
619 /* Clean up status for next message */
620 read_host_csr(&host);
621 host.interrupt_generate = 1;
622 host.ready = 1;
623 host.reset = 0;
624 write_host_csr(&host);
625
626 return 0;
627}
628
629/* Read the Extend register hash of ME firmware */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200630static int intel_me_extend_valid(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200631{
632 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700633 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200634 int i, count = 0;
635
636 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
637 if (!status.extend_feature_present) {
638 printk(BIOS_ERR, "ME: Extend Feature not present\n");
639 return -1;
640 }
641
642 if (!status.extend_reg_valid) {
643 printk(BIOS_ERR, "ME: Extend Register not valid\n");
644 return -1;
645 }
646
647 switch (status.extend_reg_algorithm) {
648 case PCI_ME_EXT_SHA1:
649 count = 5;
650 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
651 break;
652 case PCI_ME_EXT_SHA256:
653 count = 8;
654 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
655 break;
656 default:
657 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
658 status.extend_reg_algorithm);
659 return -1;
660 }
661
Stefan Reinauer8e073822012-04-04 00:07:22 +0200662 for (i = 0; i < count; ++i) {
Stefan Reinauer49058c02012-06-11 14:13:09 -0700663 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
664 printk(BIOS_DEBUG, "%08x", extend[i]);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200665 }
666 printk(BIOS_DEBUG, "\n");
667
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600668#if IS_ENABLED(CONFIG_CHROMEOS)
Stefan Reinauer49058c02012-06-11 14:13:09 -0700669 /* Save hash in NVS for the OS to verify */
670 chromeos_set_me_hash(extend, count);
671#endif
672
Stefan Reinauer8e073822012-04-04 00:07:22 +0200673 return 0;
674}
675
676/* Hide the ME virtual PCI devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200677static void intel_me_hide(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200678{
679 dev->enabled = 0;
680 pch_enable(dev);
681}
682
683/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200684static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200685{
686 me_bios_path path = intel_me_path(dev);
687 me_bios_payload mbp_data;
688
689 /* Do initial setup and determine the BIOS path */
690 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
691
692 switch (path) {
693 case ME_S3WAKE_BIOS_PATH:
694 intel_me_hide(dev);
695 break;
696
697 case ME_NORMAL_BIOS_PATH:
698 /* Validate the extend register */
699 if (intel_me_extend_valid(dev) < 0)
700 break; /* TODO: force recovery mode */
701
702 /* Prepare MEI MMIO interface */
703 if (intel_mei_setup(dev) < 0)
704 break;
705
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200706 if (intel_me_read_mbp(&mbp_data))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200707 break;
708
Martin Roth7a1a3ad2017-06-24 21:29:38 -0600709#if IS_ENABLED(CONFIG_CHROMEOS) && 0 /* DISABLED */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200710 /*
711 * Unlock ME in recovery mode.
712 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700713 if (vboot_recovery_mode_enabled()) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200714 /* Unlock ME flash region */
715 mkhi_hmrfpo_enable();
716
717 /* Issue global reset */
718 mkhi_global_reset();
719 return;
720 }
721#endif
722
723#if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG)
724 me_print_fw_version(&mbp_data.fw_version_name);
725 me_print_fwcaps(&mbp_data.fw_caps_sku);
726#endif
Duncan Laurie708f7312012-07-10 15:15:41 -0700727
728 /*
729 * Leave the ME unlocked in this path.
730 * It will be locked via SMI command later.
731 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200732 break;
733
734 case ME_ERROR_BIOS_PATH:
735 case ME_RECOVERY_BIOS_PATH:
736 case ME_DISABLE_BIOS_PATH:
737 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200738 break;
739 }
740}
741
Elyes HAOUASdc035282018-09-18 13:28:49 +0200742static void set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200743{
744 if (!vendor || !device) {
745 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
746 pci_read_config32(dev, PCI_VENDOR_ID));
747 } else {
748 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
749 ((device & 0xffff) << 16) | (vendor & 0xffff));
750 }
751}
752
753static struct pci_operations pci_ops = {
754 .set_subsystem = set_subsystem,
755};
756
757static struct device_operations device_ops = {
758 .read_resources = pci_dev_read_resources,
759 .set_resources = pci_dev_set_resources,
760 .enable_resources = pci_dev_enable_resources,
761 .init = intel_me_init,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200762 .ops_pci = &pci_ops,
763};
764
765static const struct pci_driver intel_me __pci_driver = {
766 .ops = &device_ops,
767 .vendor = PCI_VENDOR_ID_INTEL,
768 .device = 0x1e3a,
769};
770
771/******************************************************************************
772 * */
773static u32 me_to_host_words_pending(void)
774{
775 struct mei_csr me;
776 read_me_csr(&me);
777 if (!me.ready)
778 return 0;
779 return (me.buffer_write_ptr - me.buffer_read_ptr) &
780 (me.buffer_depth - 1);
781}
782
783#if 0
784/* This function is not yet being used, keep it in for the future. */
785static u32 host_to_me_words_room(void)
786{
787 struct mei_csr csr;
788
789 read_me_csr(&csr);
790 if (!csr.ready)
791 return 0;
792
793 read_host_csr(&csr);
794 return (csr.buffer_read_ptr - csr.buffer_write_ptr - 1) &
795 (csr.buffer_depth - 1);
796}
797#endif
798/*
799 * mbp seems to be following its own flow, let's retrieve it in a dedicated
800 * function.
801 */
802static int intel_me_read_mbp(me_bios_payload *mbp_data)
803{
804 mbp_header mbp_hdr;
805 mbp_item_header mbp_item_hdr;
806 u32 me2host_pending;
807 u32 mbp_item_id;
808 struct mei_csr host;
809
810 me2host_pending = me_to_host_words_pending();
811 if (!me2host_pending) {
812 printk(BIOS_ERR, "ME: no mbp data!\n");
813 return -1;
814 }
815
816 /* we know for sure that at least the header is there */
817 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
818
819 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
820 (me2host_pending < mbp_hdr.mbp_size)) {
821 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
822 " buffer contains %d words\n",
823 mbp_hdr.num_entries, mbp_hdr.mbp_size,
824 me2host_pending);
825 return -1;
826 }
827
828 me2host_pending--;
829 memset(mbp_data, 0, sizeof(*mbp_data));
830
831 while (mbp_hdr.num_entries--) {
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200832 u32 *copy_addr;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200833 u32 copy_size, buffer_room;
834 void *p;
835
836 if (!me2host_pending) {
837 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
838 mbp_hdr.num_entries + 1);
839 return -1;
840 }
841
842 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
843
844 if (mbp_item_hdr.length > me2host_pending) {
845 printk(BIOS_ERR, "ME: insufficient mbp data %d "
846 "entries to go!\n",
847 mbp_hdr.num_entries + 1);
848 return -1;
849 }
850
851 me2host_pending -= mbp_item_hdr.length;
852
853 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
854 mbp_item_hdr.app_id;
855
856 copy_size = mbp_item_hdr.length - 1;
857
858#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
859 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
860 break; \
861 }
862
863 p = &mbp_item_hdr;
864 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
865
Elyes HAOUASf9de5a42018-05-03 17:21:02 +0200866 switch (mbp_item_id) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200867 case 0x101:
868 SET_UP_COPY(fw_version_name);
869
870 case 0x102:
871 SET_UP_COPY(icc_profile);
872
873 case 0x103:
874 SET_UP_COPY(at_state);
875
876 case 0x201:
877 mbp_data->fw_caps_sku.available = 1;
878 SET_UP_COPY(fw_caps_sku.fw_capabilities);
879
880 case 0x301:
881 SET_UP_COPY(rom_bist_data);
882
883 case 0x401:
884 SET_UP_COPY(platform_key);
885
886 case 0x501:
887 mbp_data->fw_plat_type.available = 1;
888 SET_UP_COPY(fw_plat_type.rule_data);
889
890 case 0x601:
891 SET_UP_COPY(mfsintegrity);
892
893 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000894 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200895 mbp_item_id);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200896 while (copy_size--)
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000897 read_cb();
898 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200899 }
900
901 if (buffer_room != copy_size) {
902 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
903 " for item 0x%x!!!\n",
904 buffer_room, copy_size, mbp_item_id);
905 return -1;
906 }
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200907 while (copy_size--)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200908 *copy_addr++ = read_cb();
909 }
910
911 read_host_csr(&host);
912 host.interrupt_generate = 1;
913 write_host_csr(&host);
914
915 {
916 int cntr = 0;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200917 while (host.interrupt_generate) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200918 read_host_csr(&host);
919 cntr++;
920 }
921 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
922 }
923
924 return 0;
925}
926
927#endif /* !__SMM__ */