blob: 8702c4da238a781d171edfe88f44c945ecaf4bbe [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>
Stefan Reinauer8e073822012-04-04 00:07:22 +020019#include <string.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070020#include <elog.h>
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +030021#include <halt.h>
22#include <option.h>
23#include <southbridge/intel/common/me.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020024
Stefan Reinauer8e073822012-04-04 00:07:22 +020025#include "me.h"
26#include "pch.h"
27
Angel Ponsc94bc8e2021-01-27 12:17:33 +010028static inline void print_cap(const char *name, int state)
29{
30 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
31 name, state ? " en" : "dis");
32}
33
34static void me_print_fw_version(mbp_fw_version_name *vers_name)
35{
36 if (!vers_name->major_version) {
37 printk(BIOS_ERR, "ME: mbp missing version report\n");
38 return;
39 }
40
41 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
42 vers_name->major_version, vers_name->minor_version,
43 vers_name->hotfix_version, vers_name->build_version);
44}
45
Stefan Reinauer8e073822012-04-04 00:07:22 +020046/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +020047static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +020048{
49 me_bios_path path = ME_DISABLE_BIOS_PATH;
Angel Pons3f7bb7d2021-01-27 13:03:20 +010050 union me_hfs hfs;
51 union me_gmes gmes;
Stefan Reinauer8e073822012-04-04 00:07:22 +020052
Stefan Reinauer8e073822012-04-04 00:07:22 +020053 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +030054 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +020055 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +020056
Angel Pons3f7bb7d2021-01-27 13:03:20 +010057 hfs.raw = pci_read_config32(dev, PCI_ME_HFS);
58 gmes.raw = pci_read_config32(dev, PCI_ME_GMES);
Stefan Reinauer8e073822012-04-04 00:07:22 +020059
60 /* Check and dump status */
61 intel_me_status(&hfs, &gmes);
62
Stefan Reinauer8e073822012-04-04 00:07:22 +020063 /* Check Current Working State */
64 switch (hfs.working_state) {
65 case ME_HFS_CWS_NORMAL:
66 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +020067 break;
68 case ME_HFS_CWS_REC:
69 path = ME_RECOVERY_BIOS_PATH;
70 break;
71 default:
72 path = ME_DISABLE_BIOS_PATH;
73 break;
74 }
75
76 /* Check Current Operation Mode */
77 switch (hfs.operation_mode) {
78 case ME_HFS_MODE_NORMAL:
79 break;
80 case ME_HFS_MODE_DEBUG:
81 case ME_HFS_MODE_DIS:
82 case ME_HFS_MODE_OVER_JMPR:
83 case ME_HFS_MODE_OVER_MEI:
84 default:
85 path = ME_DISABLE_BIOS_PATH;
86 break;
87 }
88
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070089 /* Check for any error code and valid firmware and MBP */
90 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +020091 path = ME_ERROR_BIOS_PATH;
92
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070093 /* Check if the MBP is ready */
94 if (!gmes.mbp_rdy) {
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +030095 printk(BIOS_CRIT, "%s: mbp is not ready!\n", __func__);
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070096 path = ME_ERROR_BIOS_PATH;
97 }
98
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +020099 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700100 struct elog_event_data_me_extended data = {
101 .current_working_state = hfs.working_state,
102 .operation_state = hfs.operation_state,
103 .operation_mode = hfs.operation_mode,
104 .error_code = hfs.error_code,
105 .progress_code = gmes.progress_code,
106 .current_pmevent = gmes.current_pmevent,
107 .current_state = gmes.current_state,
108 };
109 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
110 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
111 &data, sizeof(data));
112 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700113
Stefan Reinauer8e073822012-04-04 00:07:22 +0200114 return path;
115}
116
Angel Pons7f32df32020-06-02 13:36:57 +0200117static int intel_me_read_mbp(me_bios_payload *mbp_data);
118
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100119/* Get ME Firmware Capabilities */
120static int mkhi_get_fwcaps(mefwcaps_sku *cap)
121{
122 u32 rule_id = 0;
123 struct me_fwcaps cap_msg;
124 struct mkhi_header mkhi = {
125 .group_id = MKHI_GROUP_ID_FWCAPS,
126 .command = MKHI_FWCAPS_GET_RULE,
127 };
128 struct mei_header mei = {
129 .is_complete = 1,
130 .host_address = MEI_HOST_ADDRESS,
131 .client_address = MEI_ADDRESS_MKHI,
132 .length = sizeof(mkhi) + sizeof(rule_id),
133 };
134
135 /* Send request and wait for response */
136 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg)) < 0) {
137 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
138 return -1;
139 }
140 *cap = cap_msg.caps_sku;
141 return 0;
142}
143
144/* Get ME Firmware Capabilities */
145static void me_print_fwcaps(mbp_fw_caps *caps_section)
146{
147 mefwcaps_sku *cap = &caps_section->fw_capabilities;
148 if (!caps_section->available) {
149 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
150 if (mkhi_get_fwcaps(cap))
151 return;
152 }
153
154 print_cap("Full Network manageability", cap->full_net);
155 print_cap("Regular Network manageability", cap->std_net);
156 print_cap("Manageability", cap->manageability);
157 print_cap("Small business technology", cap->small_business);
158 print_cap("Level III manageability", cap->l3manageability);
159 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
160 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
161 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
162 print_cap("ICC Over Clocking", cap->icc_over_clocking);
163 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
164 print_cap("IPV6", cap->ipv6);
165 print_cap("KVM Remote Control (KVM)", cap->kvm);
166 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
167 print_cap("Virtual LAN (VLAN)", cap->vlan);
168 print_cap("TLS", cap->tls);
169 print_cap("Wireless LAN (WLAN)", cap->wlan);
170}
171
Stefan Reinauer8e073822012-04-04 00:07:22 +0200172/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200173static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200174{
175 me_bios_path path = intel_me_path(dev);
176 me_bios_payload mbp_data;
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300177 bool need_reset = false;
Angel Pons3f7bb7d2021-01-27 13:03:20 +0100178 union me_hfs hfs;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200179
180 /* Do initial setup and determine the BIOS path */
Angel Pons2e29c3b2020-08-10 15:47:28 +0200181 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_get_bios_path_string(path));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200182
Angel Pons88dcb312021-04-26 17:10:28 +0200183 u8 me_state = get_uint_option("me_state", 0);
184 u8 me_state_prev = get_uint_option("me_state_prev", 0);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300185
186 printk(BIOS_DEBUG, "ME: me_state=%u, me_state_prev=%u\n", me_state, me_state_prev);
187
Stefan Reinauer8e073822012-04-04 00:07:22 +0200188 switch (path) {
189 case ME_S3WAKE_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100190#if CONFIG(HIDE_MEI_ON_ERROR)
191 case ME_ERROR_BIOS_PATH:
192#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200193 intel_me_hide(dev);
194 break;
195
196 case ME_NORMAL_BIOS_PATH:
197 /* Validate the extend register */
198 if (intel_me_extend_valid(dev) < 0)
199 break; /* TODO: force recovery mode */
200
201 /* Prepare MEI MMIO interface */
202 if (intel_mei_setup(dev) < 0)
203 break;
204
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200205 if (intel_me_read_mbp(&mbp_data))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200206 break;
207
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200208 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
209 me_print_fw_version(&mbp_data.fw_version_name);
210 me_print_fwcaps(&mbp_data.fw_caps_sku);
211 }
Duncan Laurie708f7312012-07-10 15:15:41 -0700212
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300213 /* Put ME in Software Temporary Disable Mode, if needed */
214 if (me_state == CMOS_ME_STATE_DISABLED
215 && CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_NORMAL) {
216 printk(BIOS_INFO, "ME: disabling ME\n");
217 if (enter_soft_temp_disable()) {
218 enter_soft_temp_disable_wait();
219 need_reset = true;
220 } else {
221 printk(BIOS_ERR, "ME: failed to enter Soft Temporary Disable mode\n");
222 }
223
224 break;
225 }
226
Duncan Laurie708f7312012-07-10 15:15:41 -0700227 /*
228 * Leave the ME unlocked in this path.
229 * It will be locked via SMI command later.
230 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200231 break;
232
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300233 case ME_DISABLE_BIOS_PATH:
234 /* Bring ME out of Soft Temporary Disable mode, if needed */
Angel Pons3f7bb7d2021-01-27 13:03:20 +0100235 hfs.raw = pci_read_config32(dev, PCI_ME_HFS);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300236 if (hfs.operation_mode == ME_HFS_MODE_DIS
237 && me_state == CMOS_ME_STATE_NORMAL
238 && (CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_DISABLED
239 || !CMOS_ME_CHANGED(me_state_prev))) {
240 printk(BIOS_INFO, "ME: re-enabling ME\n");
241
242 exit_soft_temp_disable(dev);
243 exit_soft_temp_disable_wait(dev);
244
245 /*
246 * ME starts loading firmware immediately after writing to H_GS,
247 * but Lenovo BIOS performs a reboot after bringing ME back to
248 * Normal mode. Assume that global reset is needed.
249 */
250 need_reset = true;
251 } else {
252 intel_me_hide(dev);
253 }
254 break;
255
James Yea85d4a52020-02-22 20:30:49 +1100256#if !CONFIG(HIDE_MEI_ON_ERROR)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200257 case ME_ERROR_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100258#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200259 case ME_RECOVERY_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200260 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200261 break;
262 }
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300263
264 /* To avoid boot loops if ME fails to get back from disabled mode,
265 set the 'changed' bit here. */
266 if (me_state != CMOS_ME_STATE(me_state_prev) || need_reset) {
267 u8 new_state = me_state | CMOS_ME_STATE_CHANGED;
Angel Pons88dcb312021-04-26 17:10:28 +0200268 set_uint_option("me_state_prev", new_state);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300269 }
270
271 if (need_reset) {
272 set_global_reset(true);
273 full_reset();
274 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200275}
276
Stefan Reinauer8e073822012-04-04 00:07:22 +0200277static struct device_operations device_ops = {
278 .read_resources = pci_dev_read_resources,
279 .set_resources = pci_dev_set_resources,
280 .enable_resources = pci_dev_enable_resources,
281 .init = intel_me_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200282 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200283};
284
285static const struct pci_driver intel_me __pci_driver = {
286 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100287 .vendor = PCI_VID_INTEL,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200288 .device = 0x1e3a,
289};
290
291/******************************************************************************
292 * */
293static u32 me_to_host_words_pending(void)
294{
295 struct mei_csr me;
296 read_me_csr(&me);
297 if (!me.ready)
298 return 0;
299 return (me.buffer_write_ptr - me.buffer_read_ptr) &
300 (me.buffer_depth - 1);
301}
302
Stefan Reinauer8e073822012-04-04 00:07:22 +0200303/*
304 * mbp seems to be following its own flow, let's retrieve it in a dedicated
305 * function.
306 */
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100307static int intel_me_read_mbp(me_bios_payload *mbp_data)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200308{
309 mbp_header mbp_hdr;
310 mbp_item_header mbp_item_hdr;
311 u32 me2host_pending;
312 u32 mbp_item_id;
313 struct mei_csr host;
314
315 me2host_pending = me_to_host_words_pending();
316 if (!me2host_pending) {
317 printk(BIOS_ERR, "ME: no mbp data!\n");
318 return -1;
319 }
320
321 /* we know for sure that at least the header is there */
322 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
323
324 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
325 (me2host_pending < mbp_hdr.mbp_size)) {
326 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
327 " buffer contains %d words\n",
328 mbp_hdr.num_entries, mbp_hdr.mbp_size,
329 me2host_pending);
330 return -1;
331 }
332
333 me2host_pending--;
334 memset(mbp_data, 0, sizeof(*mbp_data));
335
336 while (mbp_hdr.num_entries--) {
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200337 u32 *copy_addr;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200338 u32 copy_size, buffer_room;
339 void *p;
340
341 if (!me2host_pending) {
342 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
343 mbp_hdr.num_entries + 1);
344 return -1;
345 }
346
347 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
348
349 if (mbp_item_hdr.length > me2host_pending) {
350 printk(BIOS_ERR, "ME: insufficient mbp data %d "
351 "entries to go!\n",
352 mbp_hdr.num_entries + 1);
353 return -1;
354 }
355
356 me2host_pending -= mbp_item_hdr.length;
357
358 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
359 mbp_item_hdr.app_id;
360
361 copy_size = mbp_item_hdr.length - 1;
362
363#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
364 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
365 break; \
366 }
367
368 p = &mbp_item_hdr;
369 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
370
Elyes HAOUASf9de5a42018-05-03 17:21:02 +0200371 switch (mbp_item_id) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200372 case 0x101:
373 SET_UP_COPY(fw_version_name);
374
375 case 0x102:
376 SET_UP_COPY(icc_profile);
377
378 case 0x103:
379 SET_UP_COPY(at_state);
380
381 case 0x201:
382 mbp_data->fw_caps_sku.available = 1;
383 SET_UP_COPY(fw_caps_sku.fw_capabilities);
384
385 case 0x301:
386 SET_UP_COPY(rom_bist_data);
387
388 case 0x401:
389 SET_UP_COPY(platform_key);
390
391 case 0x501:
392 mbp_data->fw_plat_type.available = 1;
393 SET_UP_COPY(fw_plat_type.rule_data);
394
395 case 0x601:
396 SET_UP_COPY(mfsintegrity);
397
398 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000399 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200400 mbp_item_id);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200401 while (copy_size--)
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000402 read_cb();
403 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200404 }
405
406 if (buffer_room != copy_size) {
407 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
408 " for item 0x%x!!!\n",
409 buffer_room, copy_size, mbp_item_id);
410 return -1;
411 }
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200412 while (copy_size--)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200413 *copy_addr++ = read_cb();
414 }
415
416 read_host_csr(&host);
417 host.interrupt_generate = 1;
418 write_host_csr(&host);
419
420 {
421 int cntr = 0;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200422 while (host.interrupt_generate) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200423 read_host_csr(&host);
424 cntr++;
425 }
426 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
427 }
428
429 return 0;
430}