blob: cfac4566ff86bca5f0ed59a0a9cfc27442a12392 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
3/*
4 * This is a ramstage driver for the Intel Management Engine found in the
5 * 6-series chipset. It handles the required boot-time messages over the
6 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
7 * finished with POST. Additional messages are defined for debug but are
8 * not used unless the console loglevel is high enough.
9 */
10
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpi.h>
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +030012#include <cf9_reset.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020013#include <device/mmio.h>
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020014#include <device/device.h>
15#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020016#include <device/pci_ops.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020017#include <console/console.h>
18#include <device/pci_ids.h>
19#include <device/pci_def.h>
20#include <string.h>
21#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070022#include <elog.h>
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +030023#include <halt.h>
24#include <option.h>
25#include <southbridge/intel/common/me.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020026
Stefan Reinauer8e073822012-04-04 00:07:22 +020027#include "me.h"
28#include "pch.h"
29
Angel Ponsc94bc8e2021-01-27 12:17:33 +010030#ifdef __SIMPLE_DEVICE__
31
Angel Pons053fe8a2020-08-10 15:32:57 +020032/* Send END OF POST message to the ME */
Angel Ponsc94bc8e2021-01-27 12:17:33 +010033static int mkhi_end_of_post(void)
Angel Pons053fe8a2020-08-10 15:32:57 +020034{
35 struct mkhi_header mkhi = {
36 .group_id = MKHI_GROUP_ID_GEN,
37 .command = MKHI_END_OF_POST,
38 };
39 struct mei_header mei = {
40 .is_complete = 1,
41 .host_address = MEI_HOST_ADDRESS,
42 .client_address = MEI_ADDRESS_MKHI,
43 .length = sizeof(mkhi),
44 };
45
46 u32 eop_ack;
47
48 /* Send request and wait for response */
49 printk(BIOS_NOTICE, "ME: %s\n", __func__);
50 if (mei_sendrecv(&mei, &mkhi, NULL, &eop_ack, sizeof(eop_ack)) < 0) {
51 printk(BIOS_ERR, "ME: END OF POST message failed\n");
52 return -1;
53 }
54
55 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
56 return 0;
57}
58
Stefan Reinauer998f3a22012-06-11 15:15:46 -070059void intel_me8_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +020060{
61 struct me_hfs hfs;
62 u32 reg32;
63
Angel Pons2e29c3b2020-08-10 15:47:28 +020064 update_mei_base_address();
Stefan Reinauer8e073822012-04-04 00:07:22 +020065
66 /* S3 path will have hidden this device already */
Angel Pons2e29c3b2020-08-10 15:47:28 +020067 if (!is_mei_base_address_valid())
Stefan Reinauer8e073822012-04-04 00:07:22 +020068 return;
69
70 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +030071 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +020072 memcpy(&hfs, &reg32, sizeof(u32));
73
74 /* Abort and leave device alone if not normal mode */
75 if (hfs.fpt_bad ||
76 hfs.working_state != ME_HFS_CWS_NORMAL ||
77 hfs.operation_mode != ME_HFS_MODE_NORMAL)
78 return;
79
80 /* Try to send EOP command so ME stops accepting other commands */
81 mkhi_end_of_post();
82
83 /* Make sure IO is disabled */
Angel Ponsc803f652020-06-07 22:09:01 +020084 pci_and_config16(PCH_ME_DEV, PCI_COMMAND,
85 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Stefan Reinauer8e073822012-04-04 00:07:22 +020086
87 /* Hide the PCI device */
88 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
89}
90
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020091#else /* !__SIMPLE_DEVICE__ */
Stefan Reinauer8e073822012-04-04 00:07:22 +020092
Angel Ponsc94bc8e2021-01-27 12:17:33 +010093static inline void print_cap(const char *name, int state)
94{
95 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
96 name, state ? " en" : "dis");
97}
98
99static void me_print_fw_version(mbp_fw_version_name *vers_name)
100{
101 if (!vers_name->major_version) {
102 printk(BIOS_ERR, "ME: mbp missing version report\n");
103 return;
104 }
105
106 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
107 vers_name->major_version, vers_name->minor_version,
108 vers_name->hotfix_version, vers_name->build_version);
109}
110
Stefan Reinauer8e073822012-04-04 00:07:22 +0200111/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200112static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113{
114 me_bios_path path = ME_DISABLE_BIOS_PATH;
115 struct me_hfs hfs;
116 struct me_gmes gmes;
117
Stefan Reinauer8e073822012-04-04 00:07:22 +0200118 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300119 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200120 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200121
122 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
123 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
124
125 /* Check and dump status */
126 intel_me_status(&hfs, &gmes);
127
Stefan Reinauer8e073822012-04-04 00:07:22 +0200128 /* Check Current Working State */
129 switch (hfs.working_state) {
130 case ME_HFS_CWS_NORMAL:
131 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200132 break;
133 case ME_HFS_CWS_REC:
134 path = ME_RECOVERY_BIOS_PATH;
135 break;
136 default:
137 path = ME_DISABLE_BIOS_PATH;
138 break;
139 }
140
141 /* Check Current Operation Mode */
142 switch (hfs.operation_mode) {
143 case ME_HFS_MODE_NORMAL:
144 break;
145 case ME_HFS_MODE_DEBUG:
146 case ME_HFS_MODE_DIS:
147 case ME_HFS_MODE_OVER_JMPR:
148 case ME_HFS_MODE_OVER_MEI:
149 default:
150 path = ME_DISABLE_BIOS_PATH;
151 break;
152 }
153
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700154 /* Check for any error code and valid firmware and MBP */
155 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200156 path = ME_ERROR_BIOS_PATH;
157
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700158 /* Check if the MBP is ready */
159 if (!gmes.mbp_rdy) {
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300160 printk(BIOS_CRIT, "%s: mbp is not ready!\n", __func__);
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700161 path = ME_ERROR_BIOS_PATH;
162 }
163
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200164 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700165 struct elog_event_data_me_extended data = {
166 .current_working_state = hfs.working_state,
167 .operation_state = hfs.operation_state,
168 .operation_mode = hfs.operation_mode,
169 .error_code = hfs.error_code,
170 .progress_code = gmes.progress_code,
171 .current_pmevent = gmes.current_pmevent,
172 .current_state = gmes.current_state,
173 };
174 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
175 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
176 &data, sizeof(data));
177 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700178
Stefan Reinauer8e073822012-04-04 00:07:22 +0200179 return path;
180}
181
Angel Pons7f32df32020-06-02 13:36:57 +0200182static int intel_me_read_mbp(me_bios_payload *mbp_data);
183
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100184/* Get ME Firmware Capabilities */
185static int mkhi_get_fwcaps(mefwcaps_sku *cap)
186{
187 u32 rule_id = 0;
188 struct me_fwcaps cap_msg;
189 struct mkhi_header mkhi = {
190 .group_id = MKHI_GROUP_ID_FWCAPS,
191 .command = MKHI_FWCAPS_GET_RULE,
192 };
193 struct mei_header mei = {
194 .is_complete = 1,
195 .host_address = MEI_HOST_ADDRESS,
196 .client_address = MEI_ADDRESS_MKHI,
197 .length = sizeof(mkhi) + sizeof(rule_id),
198 };
199
200 /* Send request and wait for response */
201 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg)) < 0) {
202 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
203 return -1;
204 }
205 *cap = cap_msg.caps_sku;
206 return 0;
207}
208
209/* Get ME Firmware Capabilities */
210static void me_print_fwcaps(mbp_fw_caps *caps_section)
211{
212 mefwcaps_sku *cap = &caps_section->fw_capabilities;
213 if (!caps_section->available) {
214 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
215 if (mkhi_get_fwcaps(cap))
216 return;
217 }
218
219 print_cap("Full Network manageability", cap->full_net);
220 print_cap("Regular Network manageability", cap->std_net);
221 print_cap("Manageability", cap->manageability);
222 print_cap("Small business technology", cap->small_business);
223 print_cap("Level III manageability", cap->l3manageability);
224 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
225 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
226 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
227 print_cap("ICC Over Clocking", cap->icc_over_clocking);
228 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
229 print_cap("IPV6", cap->ipv6);
230 print_cap("KVM Remote Control (KVM)", cap->kvm);
231 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
232 print_cap("Virtual LAN (VLAN)", cap->vlan);
233 print_cap("TLS", cap->tls);
234 print_cap("Wireless LAN (WLAN)", cap->wlan);
235}
236
Stefan Reinauer8e073822012-04-04 00:07:22 +0200237/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200238static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200239{
240 me_bios_path path = intel_me_path(dev);
241 me_bios_payload mbp_data;
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300242 u8 me_state = 0, me_state_prev = 0;
243 bool need_reset = false;
244 struct me_hfs hfs;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200245
246 /* Do initial setup and determine the BIOS path */
Angel Pons2e29c3b2020-08-10 15:47:28 +0200247 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_get_bios_path_string(path));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200248
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300249 get_option(&me_state, "me_state");
250 get_option(&me_state_prev, "me_state_prev");
251
252 printk(BIOS_DEBUG, "ME: me_state=%u, me_state_prev=%u\n", me_state, me_state_prev);
253
Stefan Reinauer8e073822012-04-04 00:07:22 +0200254 switch (path) {
255 case ME_S3WAKE_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100256#if CONFIG(HIDE_MEI_ON_ERROR)
257 case ME_ERROR_BIOS_PATH:
258#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200259 intel_me_hide(dev);
260 break;
261
262 case ME_NORMAL_BIOS_PATH:
263 /* Validate the extend register */
264 if (intel_me_extend_valid(dev) < 0)
265 break; /* TODO: force recovery mode */
266
267 /* Prepare MEI MMIO interface */
268 if (intel_mei_setup(dev) < 0)
269 break;
270
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200271 if (intel_me_read_mbp(&mbp_data))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200272 break;
273
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200274 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
275 me_print_fw_version(&mbp_data.fw_version_name);
276 me_print_fwcaps(&mbp_data.fw_caps_sku);
277 }
Duncan Laurie708f7312012-07-10 15:15:41 -0700278
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300279 /* Put ME in Software Temporary Disable Mode, if needed */
280 if (me_state == CMOS_ME_STATE_DISABLED
281 && CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_NORMAL) {
282 printk(BIOS_INFO, "ME: disabling ME\n");
283 if (enter_soft_temp_disable()) {
284 enter_soft_temp_disable_wait();
285 need_reset = true;
286 } else {
287 printk(BIOS_ERR, "ME: failed to enter Soft Temporary Disable mode\n");
288 }
289
290 break;
291 }
292
Duncan Laurie708f7312012-07-10 15:15:41 -0700293 /*
294 * Leave the ME unlocked in this path.
295 * It will be locked via SMI command later.
296 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200297 break;
298
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300299 case ME_DISABLE_BIOS_PATH:
300 /* Bring ME out of Soft Temporary Disable mode, if needed */
301 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
302 if (hfs.operation_mode == ME_HFS_MODE_DIS
303 && me_state == CMOS_ME_STATE_NORMAL
304 && (CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_DISABLED
305 || !CMOS_ME_CHANGED(me_state_prev))) {
306 printk(BIOS_INFO, "ME: re-enabling ME\n");
307
308 exit_soft_temp_disable(dev);
309 exit_soft_temp_disable_wait(dev);
310
311 /*
312 * ME starts loading firmware immediately after writing to H_GS,
313 * but Lenovo BIOS performs a reboot after bringing ME back to
314 * Normal mode. Assume that global reset is needed.
315 */
316 need_reset = true;
317 } else {
318 intel_me_hide(dev);
319 }
320 break;
321
James Yea85d4a52020-02-22 20:30:49 +1100322#if !CONFIG(HIDE_MEI_ON_ERROR)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200323 case ME_ERROR_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100324#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200325 case ME_RECOVERY_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200326 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200327 break;
328 }
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300329
330 /* To avoid boot loops if ME fails to get back from disabled mode,
331 set the 'changed' bit here. */
332 if (me_state != CMOS_ME_STATE(me_state_prev) || need_reset) {
333 u8 new_state = me_state | CMOS_ME_STATE_CHANGED;
334 set_option("me_state_prev", &new_state);
335 }
336
337 if (need_reset) {
338 set_global_reset(true);
339 full_reset();
340 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200341}
342
Stefan Reinauer8e073822012-04-04 00:07:22 +0200343static struct device_operations device_ops = {
344 .read_resources = pci_dev_read_resources,
345 .set_resources = pci_dev_set_resources,
346 .enable_resources = pci_dev_enable_resources,
347 .init = intel_me_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200348 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200349};
350
351static const struct pci_driver intel_me __pci_driver = {
352 .ops = &device_ops,
353 .vendor = PCI_VENDOR_ID_INTEL,
354 .device = 0x1e3a,
355};
356
357/******************************************************************************
358 * */
359static u32 me_to_host_words_pending(void)
360{
361 struct mei_csr me;
362 read_me_csr(&me);
363 if (!me.ready)
364 return 0;
365 return (me.buffer_write_ptr - me.buffer_read_ptr) &
366 (me.buffer_depth - 1);
367}
368
Stefan Reinauer8e073822012-04-04 00:07:22 +0200369/*
370 * mbp seems to be following its own flow, let's retrieve it in a dedicated
371 * function.
372 */
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100373static int intel_me_read_mbp(me_bios_payload *mbp_data)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200374{
375 mbp_header mbp_hdr;
376 mbp_item_header mbp_item_hdr;
377 u32 me2host_pending;
378 u32 mbp_item_id;
379 struct mei_csr host;
380
381 me2host_pending = me_to_host_words_pending();
382 if (!me2host_pending) {
383 printk(BIOS_ERR, "ME: no mbp data!\n");
384 return -1;
385 }
386
387 /* we know for sure that at least the header is there */
388 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
389
390 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
391 (me2host_pending < mbp_hdr.mbp_size)) {
392 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
393 " buffer contains %d words\n",
394 mbp_hdr.num_entries, mbp_hdr.mbp_size,
395 me2host_pending);
396 return -1;
397 }
398
399 me2host_pending--;
400 memset(mbp_data, 0, sizeof(*mbp_data));
401
402 while (mbp_hdr.num_entries--) {
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200403 u32 *copy_addr;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200404 u32 copy_size, buffer_room;
405 void *p;
406
407 if (!me2host_pending) {
408 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
409 mbp_hdr.num_entries + 1);
410 return -1;
411 }
412
413 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
414
415 if (mbp_item_hdr.length > me2host_pending) {
416 printk(BIOS_ERR, "ME: insufficient mbp data %d "
417 "entries to go!\n",
418 mbp_hdr.num_entries + 1);
419 return -1;
420 }
421
422 me2host_pending -= mbp_item_hdr.length;
423
424 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
425 mbp_item_hdr.app_id;
426
427 copy_size = mbp_item_hdr.length - 1;
428
429#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
430 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
431 break; \
432 }
433
434 p = &mbp_item_hdr;
435 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
436
Elyes HAOUASf9de5a42018-05-03 17:21:02 +0200437 switch (mbp_item_id) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200438 case 0x101:
439 SET_UP_COPY(fw_version_name);
440
441 case 0x102:
442 SET_UP_COPY(icc_profile);
443
444 case 0x103:
445 SET_UP_COPY(at_state);
446
447 case 0x201:
448 mbp_data->fw_caps_sku.available = 1;
449 SET_UP_COPY(fw_caps_sku.fw_capabilities);
450
451 case 0x301:
452 SET_UP_COPY(rom_bist_data);
453
454 case 0x401:
455 SET_UP_COPY(platform_key);
456
457 case 0x501:
458 mbp_data->fw_plat_type.available = 1;
459 SET_UP_COPY(fw_plat_type.rule_data);
460
461 case 0x601:
462 SET_UP_COPY(mfsintegrity);
463
464 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000465 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200466 mbp_item_id);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200467 while (copy_size--)
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000468 read_cb();
469 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200470 }
471
472 if (buffer_room != copy_size) {
473 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
474 " for item 0x%x!!!\n",
475 buffer_room, copy_size, mbp_item_id);
476 return -1;
477 }
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200478 while (copy_size--)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200479 *copy_addr++ = read_cb();
480 }
481
482 read_host_csr(&host);
483 host.interrupt_generate = 1;
484 write_host_csr(&host);
485
486 {
487 int cntr = 0;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200488 while (host.interrupt_generate) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200489 read_host_csr(&host);
490 cntr++;
491 }
492 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
493 }
494
495 return 0;
496}
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100497
498#endif /* !__SIMPLE_DEVICE__ */