blob: 3c5fcd1796e8a168b0c1cfdfd3a5d9e60d2ce682 [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 +010030static inline void print_cap(const char *name, int state)
31{
32 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
33 name, state ? " en" : "dis");
34}
35
36static void me_print_fw_version(mbp_fw_version_name *vers_name)
37{
38 if (!vers_name->major_version) {
39 printk(BIOS_ERR, "ME: mbp missing version report\n");
40 return;
41 }
42
43 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
44 vers_name->major_version, vers_name->minor_version,
45 vers_name->hotfix_version, vers_name->build_version);
46}
47
Stefan Reinauer8e073822012-04-04 00:07:22 +020048/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +020049static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +020050{
51 me_bios_path path = ME_DISABLE_BIOS_PATH;
Angel Pons3f7bb7d2021-01-27 13:03:20 +010052 union me_hfs hfs;
53 union me_gmes gmes;
Stefan Reinauer8e073822012-04-04 00:07:22 +020054
Stefan Reinauer8e073822012-04-04 00:07:22 +020055 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +030056 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +020057 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +020058
Angel Pons3f7bb7d2021-01-27 13:03:20 +010059 hfs.raw = pci_read_config32(dev, PCI_ME_HFS);
60 gmes.raw = pci_read_config32(dev, PCI_ME_GMES);
Stefan Reinauer8e073822012-04-04 00:07:22 +020061
62 /* Check and dump status */
63 intel_me_status(&hfs, &gmes);
64
Stefan Reinauer8e073822012-04-04 00:07:22 +020065 /* Check Current Working State */
66 switch (hfs.working_state) {
67 case ME_HFS_CWS_NORMAL:
68 path = ME_NORMAL_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +020069 break;
70 case ME_HFS_CWS_REC:
71 path = ME_RECOVERY_BIOS_PATH;
72 break;
73 default:
74 path = ME_DISABLE_BIOS_PATH;
75 break;
76 }
77
78 /* Check Current Operation Mode */
79 switch (hfs.operation_mode) {
80 case ME_HFS_MODE_NORMAL:
81 break;
82 case ME_HFS_MODE_DEBUG:
83 case ME_HFS_MODE_DIS:
84 case ME_HFS_MODE_OVER_JMPR:
85 case ME_HFS_MODE_OVER_MEI:
86 default:
87 path = ME_DISABLE_BIOS_PATH;
88 break;
89 }
90
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070091 /* Check for any error code and valid firmware and MBP */
92 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +020093 path = ME_ERROR_BIOS_PATH;
94
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070095 /* Check if the MBP is ready */
96 if (!gmes.mbp_rdy) {
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +030097 printk(BIOS_CRIT, "%s: mbp is not ready!\n", __func__);
Duncan Laurie5c88c6f2012-09-01 14:00:23 -070098 path = ME_ERROR_BIOS_PATH;
99 }
100
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200101 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700102 struct elog_event_data_me_extended data = {
103 .current_working_state = hfs.working_state,
104 .operation_state = hfs.operation_state,
105 .operation_mode = hfs.operation_mode,
106 .error_code = hfs.error_code,
107 .progress_code = gmes.progress_code,
108 .current_pmevent = gmes.current_pmevent,
109 .current_state = gmes.current_state,
110 };
111 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
112 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
113 &data, sizeof(data));
114 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700115
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116 return path;
117}
118
Angel Pons7f32df32020-06-02 13:36:57 +0200119static int intel_me_read_mbp(me_bios_payload *mbp_data);
120
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100121/* Get ME Firmware Capabilities */
122static int mkhi_get_fwcaps(mefwcaps_sku *cap)
123{
124 u32 rule_id = 0;
125 struct me_fwcaps cap_msg;
126 struct mkhi_header mkhi = {
127 .group_id = MKHI_GROUP_ID_FWCAPS,
128 .command = MKHI_FWCAPS_GET_RULE,
129 };
130 struct mei_header mei = {
131 .is_complete = 1,
132 .host_address = MEI_HOST_ADDRESS,
133 .client_address = MEI_ADDRESS_MKHI,
134 .length = sizeof(mkhi) + sizeof(rule_id),
135 };
136
137 /* Send request and wait for response */
138 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap_msg, sizeof(cap_msg)) < 0) {
139 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
140 return -1;
141 }
142 *cap = cap_msg.caps_sku;
143 return 0;
144}
145
146/* Get ME Firmware Capabilities */
147static void me_print_fwcaps(mbp_fw_caps *caps_section)
148{
149 mefwcaps_sku *cap = &caps_section->fw_capabilities;
150 if (!caps_section->available) {
151 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
152 if (mkhi_get_fwcaps(cap))
153 return;
154 }
155
156 print_cap("Full Network manageability", cap->full_net);
157 print_cap("Regular Network manageability", cap->std_net);
158 print_cap("Manageability", cap->manageability);
159 print_cap("Small business technology", cap->small_business);
160 print_cap("Level III manageability", cap->l3manageability);
161 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
162 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
163 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
164 print_cap("ICC Over Clocking", cap->icc_over_clocking);
165 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
166 print_cap("IPV6", cap->ipv6);
167 print_cap("KVM Remote Control (KVM)", cap->kvm);
168 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
169 print_cap("Virtual LAN (VLAN)", cap->vlan);
170 print_cap("TLS", cap->tls);
171 print_cap("Wireless LAN (WLAN)", cap->wlan);
172}
173
Stefan Reinauer8e073822012-04-04 00:07:22 +0200174/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200175static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200176{
177 me_bios_path path = intel_me_path(dev);
178 me_bios_payload mbp_data;
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300179 bool need_reset = false;
Angel Pons3f7bb7d2021-01-27 13:03:20 +0100180 union me_hfs hfs;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200181
182 /* Do initial setup and determine the BIOS path */
Angel Pons2e29c3b2020-08-10 15:47:28 +0200183 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_get_bios_path_string(path));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200184
Angel Pons5daa4122021-04-19 13:21:41 +0200185 u8 me_state = get_int_option("me_state", 0);
186 u8 me_state_prev = get_int_option("me_state_prev", 0);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300187
188 printk(BIOS_DEBUG, "ME: me_state=%u, me_state_prev=%u\n", me_state, me_state_prev);
189
Stefan Reinauer8e073822012-04-04 00:07:22 +0200190 switch (path) {
191 case ME_S3WAKE_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100192#if CONFIG(HIDE_MEI_ON_ERROR)
193 case ME_ERROR_BIOS_PATH:
194#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200195 intel_me_hide(dev);
196 break;
197
198 case ME_NORMAL_BIOS_PATH:
199 /* Validate the extend register */
200 if (intel_me_extend_valid(dev) < 0)
201 break; /* TODO: force recovery mode */
202
203 /* Prepare MEI MMIO interface */
204 if (intel_mei_setup(dev) < 0)
205 break;
206
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200207 if (intel_me_read_mbp(&mbp_data))
Stefan Reinauer8e073822012-04-04 00:07:22 +0200208 break;
209
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200210 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
211 me_print_fw_version(&mbp_data.fw_version_name);
212 me_print_fwcaps(&mbp_data.fw_caps_sku);
213 }
Duncan Laurie708f7312012-07-10 15:15:41 -0700214
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300215 /* Put ME in Software Temporary Disable Mode, if needed */
216 if (me_state == CMOS_ME_STATE_DISABLED
217 && CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_NORMAL) {
218 printk(BIOS_INFO, "ME: disabling ME\n");
219 if (enter_soft_temp_disable()) {
220 enter_soft_temp_disable_wait();
221 need_reset = true;
222 } else {
223 printk(BIOS_ERR, "ME: failed to enter Soft Temporary Disable mode\n");
224 }
225
226 break;
227 }
228
Duncan Laurie708f7312012-07-10 15:15:41 -0700229 /*
230 * Leave the ME unlocked in this path.
231 * It will be locked via SMI command later.
232 */
Stefan Reinauer8e073822012-04-04 00:07:22 +0200233 break;
234
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300235 case ME_DISABLE_BIOS_PATH:
236 /* Bring ME out of Soft Temporary Disable mode, if needed */
Angel Pons3f7bb7d2021-01-27 13:03:20 +0100237 hfs.raw = pci_read_config32(dev, PCI_ME_HFS);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300238 if (hfs.operation_mode == ME_HFS_MODE_DIS
239 && me_state == CMOS_ME_STATE_NORMAL
240 && (CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_DISABLED
241 || !CMOS_ME_CHANGED(me_state_prev))) {
242 printk(BIOS_INFO, "ME: re-enabling ME\n");
243
244 exit_soft_temp_disable(dev);
245 exit_soft_temp_disable_wait(dev);
246
247 /*
248 * ME starts loading firmware immediately after writing to H_GS,
249 * but Lenovo BIOS performs a reboot after bringing ME back to
250 * Normal mode. Assume that global reset is needed.
251 */
252 need_reset = true;
253 } else {
254 intel_me_hide(dev);
255 }
256 break;
257
James Yea85d4a52020-02-22 20:30:49 +1100258#if !CONFIG(HIDE_MEI_ON_ERROR)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200259 case ME_ERROR_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100260#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200261 case ME_RECOVERY_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200262 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200263 break;
264 }
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300265
266 /* To avoid boot loops if ME fails to get back from disabled mode,
267 set the 'changed' bit here. */
268 if (me_state != CMOS_ME_STATE(me_state_prev) || need_reset) {
269 u8 new_state = me_state | CMOS_ME_STATE_CHANGED;
Angel Pons5daa4122021-04-19 13:21:41 +0200270 set_int_option("me_state_prev", new_state);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300271 }
272
273 if (need_reset) {
274 set_global_reset(true);
275 full_reset();
276 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200277}
278
Stefan Reinauer8e073822012-04-04 00:07:22 +0200279static struct device_operations device_ops = {
280 .read_resources = pci_dev_read_resources,
281 .set_resources = pci_dev_set_resources,
282 .enable_resources = pci_dev_enable_resources,
283 .init = intel_me_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200284 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200285};
286
287static const struct pci_driver intel_me __pci_driver = {
288 .ops = &device_ops,
289 .vendor = PCI_VENDOR_ID_INTEL,
290 .device = 0x1e3a,
291};
292
293/******************************************************************************
294 * */
295static u32 me_to_host_words_pending(void)
296{
297 struct mei_csr me;
298 read_me_csr(&me);
299 if (!me.ready)
300 return 0;
301 return (me.buffer_write_ptr - me.buffer_read_ptr) &
302 (me.buffer_depth - 1);
303}
304
Stefan Reinauer8e073822012-04-04 00:07:22 +0200305/*
306 * mbp seems to be following its own flow, let's retrieve it in a dedicated
307 * function.
308 */
Angel Ponsc94bc8e2021-01-27 12:17:33 +0100309static int intel_me_read_mbp(me_bios_payload *mbp_data)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200310{
311 mbp_header mbp_hdr;
312 mbp_item_header mbp_item_hdr;
313 u32 me2host_pending;
314 u32 mbp_item_id;
315 struct mei_csr host;
316
317 me2host_pending = me_to_host_words_pending();
318 if (!me2host_pending) {
319 printk(BIOS_ERR, "ME: no mbp data!\n");
320 return -1;
321 }
322
323 /* we know for sure that at least the header is there */
324 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
325
326 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
327 (me2host_pending < mbp_hdr.mbp_size)) {
328 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
329 " buffer contains %d words\n",
330 mbp_hdr.num_entries, mbp_hdr.mbp_size,
331 me2host_pending);
332 return -1;
333 }
334
335 me2host_pending--;
336 memset(mbp_data, 0, sizeof(*mbp_data));
337
338 while (mbp_hdr.num_entries--) {
Elyes HAOUAS448d9fb2018-05-22 12:51:27 +0200339 u32 *copy_addr;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200340 u32 copy_size, buffer_room;
341 void *p;
342
343 if (!me2host_pending) {
344 printk(BIOS_ERR, "ME: no mbp data %d entries to go!\n",
345 mbp_hdr.num_entries + 1);
346 return -1;
347 }
348
349 mei_read_dword_ptr(&mbp_item_hdr, MEI_ME_CB_RW);
350
351 if (mbp_item_hdr.length > me2host_pending) {
352 printk(BIOS_ERR, "ME: insufficient mbp data %d "
353 "entries to go!\n",
354 mbp_hdr.num_entries + 1);
355 return -1;
356 }
357
358 me2host_pending -= mbp_item_hdr.length;
359
360 mbp_item_id = (((u32)mbp_item_hdr.item_id) << 8) +
361 mbp_item_hdr.app_id;
362
363 copy_size = mbp_item_hdr.length - 1;
364
365#define SET_UP_COPY(field) { copy_addr = (u32 *)&mbp_data->field; \
366 buffer_room = sizeof(mbp_data->field) / sizeof(u32); \
367 break; \
368 }
369
370 p = &mbp_item_hdr;
371 printk(BIOS_INFO, "ME: MBP item header %8.8x\n", *((u32*)p));
372
Elyes HAOUASf9de5a42018-05-03 17:21:02 +0200373 switch (mbp_item_id) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200374 case 0x101:
375 SET_UP_COPY(fw_version_name);
376
377 case 0x102:
378 SET_UP_COPY(icc_profile);
379
380 case 0x103:
381 SET_UP_COPY(at_state);
382
383 case 0x201:
384 mbp_data->fw_caps_sku.available = 1;
385 SET_UP_COPY(fw_caps_sku.fw_capabilities);
386
387 case 0x301:
388 SET_UP_COPY(rom_bist_data);
389
390 case 0x401:
391 SET_UP_COPY(platform_key);
392
393 case 0x501:
394 mbp_data->fw_plat_type.available = 1;
395 SET_UP_COPY(fw_plat_type.rule_data);
396
397 case 0x601:
398 SET_UP_COPY(mfsintegrity);
399
400 default:
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000401 printk(BIOS_ERR, "ME: unknown mbp item id 0x%x! Skipping\n",
Stefan Reinauer8e073822012-04-04 00:07:22 +0200402 mbp_item_id);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200403 while (copy_size--)
Vladimir Serbinenkoafc8d982014-06-11 18:52:55 +0000404 read_cb();
405 continue;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200406 }
407
408 if (buffer_room != copy_size) {
409 printk(BIOS_ERR, "ME: buffer room %d != %d copy size"
410 " for item 0x%x!!!\n",
411 buffer_room, copy_size, mbp_item_id);
412 return -1;
413 }
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200414 while (copy_size--)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200415 *copy_addr++ = read_cb();
416 }
417
418 read_host_csr(&host);
419 host.interrupt_generate = 1;
420 write_host_csr(&host);
421
422 {
423 int cntr = 0;
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200424 while (host.interrupt_generate) {
Stefan Reinauer8e073822012-04-04 00:07:22 +0200425 read_host_csr(&host);
426 cntr++;
427 }
428 printk(BIOS_SPEW, "ME: mbp read OK after %d cycles\n", cntr);
429 }
430
431 return 0;
432}