blob: c522d77a1129cf35ad758570d1abcc2043d88a53 [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>
Angel Pons7f32df32020-06-02 13:36:57 +020016#include <device/pci_ops.h>
17#include <console/console.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020018#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
Stefan Reinauer8e073822012-04-04 00:07:22 +020030/* Send END OF POST message to the ME */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020031static int __unused mkhi_end_of_post(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +020032{
33 struct mkhi_header mkhi = {
34 .group_id = MKHI_GROUP_ID_GEN,
35 .command = MKHI_END_OF_POST,
36 };
37 struct mei_header mei = {
38 .is_complete = 1,
39 .host_address = MEI_HOST_ADDRESS,
40 .client_address = MEI_ADDRESS_MKHI,
41 .length = sizeof(mkhi),
42 };
43
44 /* Send request and wait for response */
45 if (mei_sendrecv(&mei, &mkhi, NULL, NULL, 0) < 0) {
46 printk(BIOS_ERR, "ME: END OF POST message failed\n");
47 return -1;
48 }
49
50 printk(BIOS_INFO, "ME: END OF POST message successful\n");
51 return 0;
52}
53
Stefan Reinauer8e073822012-04-04 00:07:22 +020054/* Get ME firmware version */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020055static int __unused mkhi_get_fw_version(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +020056{
57 struct me_fw_version version;
58 struct mkhi_header mkhi = {
59 .group_id = MKHI_GROUP_ID_GEN,
60 .command = MKHI_GET_FW_VERSION,
61 };
62 struct mei_header mei = {
63 .is_complete = 1,
64 .host_address = MEI_HOST_ADDRESS,
65 .client_address = MEI_ADDRESS_MKHI,
66 .length = sizeof(mkhi),
67 };
68
69 /* Send request and wait for response */
70 if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) {
71 printk(BIOS_ERR, "ME: GET FW VERSION message failed\n");
72 return -1;
73 }
74
75 printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) "
76 "%u.%u.%u.%u (recovery)\n",
77 version.code_major, version.code_minor,
78 version.code_build_number, version.code_hot_fix,
79 version.recovery_major, version.recovery_minor,
80 version.recovery_build_number, version.recovery_hot_fix);
81
82 return 0;
83}
84
85static inline void print_cap(const char *name, int state)
86{
87 printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n",
88 name, state ? "en" : "dis");
89}
90
91/* Get ME Firmware Capabilities */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020092static int __unused mkhi_get_fwcaps(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +020093{
94 u32 rule_id = 0;
95 struct me_fwcaps cap;
96 struct mkhi_header mkhi = {
97 .group_id = MKHI_GROUP_ID_FWCAPS,
98 .command = MKHI_FWCAPS_GET_RULE,
99 };
100 struct mei_header mei = {
101 .is_complete = 1,
102 .host_address = MEI_HOST_ADDRESS,
103 .client_address = MEI_ADDRESS_MKHI,
104 .length = sizeof(mkhi) + sizeof(rule_id),
105 };
106
107 /* Send request and wait for response */
108 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) {
109 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
110 return -1;
111 }
112
113 print_cap("Full Network manageability", cap.caps_sku.full_net);
114 print_cap("Regular Network manageability", cap.caps_sku.std_net);
115 print_cap("Manageability", cap.caps_sku.manageability);
116 print_cap("Small business technology", cap.caps_sku.small_business);
117 print_cap("Level III manageability", cap.caps_sku.l3manageability);
118 print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at);
119 print_cap("IntelR Capability Licensing Service (CLS)",
120 cap.caps_sku.intel_cls);
121 print_cap("IntelR Power Sharing Technology (MPC)",
122 cap.caps_sku.intel_mpc);
123 print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200124 print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200125 print_cap("IPV6", cap.caps_sku.ipv6);
126 print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm);
127 print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och);
128 print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan);
129 print_cap("TLS", cap.caps_sku.tls);
130 print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan);
131
132 return 0;
133}
Stefan Reinauer8e073822012-04-04 00:07:22 +0200134
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200135#ifdef __SIMPLE_DEVICE__
136
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700137static void intel_me7_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200138{
139 struct me_hfs hfs;
140 u32 reg32;
141
Angel Pons2e29c3b2020-08-10 15:47:28 +0200142 update_mei_base_address();
Stefan Reinauer8e073822012-04-04 00:07:22 +0200143
144 /* S3 path will have hidden this device already */
Angel Pons2e29c3b2020-08-10 15:47:28 +0200145 if (!is_mei_base_address_valid())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200146 return;
147
148 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300149 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200150 memcpy(&hfs, &reg32, sizeof(u32));
151
152 /* Abort and leave device alone if not normal mode */
153 if (hfs.fpt_bad ||
154 hfs.working_state != ME_HFS_CWS_NORMAL ||
155 hfs.operation_mode != ME_HFS_MODE_NORMAL)
156 return;
157
158 /* Try to send EOP command so ME stops accepting other commands */
159 mkhi_end_of_post();
160
161 /* Make sure IO is disabled */
Angel Ponsc803f652020-06-07 22:09:01 +0200162 pci_and_config16(PCH_ME_DEV, PCI_COMMAND,
163 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200164
165 /* Hide the PCI device */
166 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
167}
168
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700169void intel_me_finalize_smm(void)
170{
Angel Ponsa7db40e2020-09-10 12:51:38 +0200171 u16 did = pci_read_config16(PCH_ME_DEV, PCI_DEVICE_ID);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700172 switch (did) {
Angel Ponsa7db40e2020-09-10 12:51:38 +0200173 case 0x1c3a:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700174 intel_me7_finalize_smm();
175 break;
Angel Ponsa7db40e2020-09-10 12:51:38 +0200176 case 0x1e3a:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700177 intel_me8_finalize_smm();
178 break;
179 default:
Angel Ponsa7db40e2020-09-10 12:51:38 +0200180 printk(BIOS_ERR, "No finalize handler for ME %04x.\n", did);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700181 }
182}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200183
184#else
Stefan Reinauer8e073822012-04-04 00:07:22 +0200185
186/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200187static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200188{
189 me_bios_path path = ME_DISABLE_BIOS_PATH;
190 struct me_hfs hfs;
191 struct me_gmes gmes;
192
Stefan Reinauer8e073822012-04-04 00:07:22 +0200193 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300194 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200195 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200196
197 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
198 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
199
200 /* Check and dump status */
201 intel_me_status(&hfs, &gmes);
202
Stefan Reinauer8e073822012-04-04 00:07:22 +0200203 /* Check Current Working State */
204 switch (hfs.working_state) {
205 case ME_HFS_CWS_NORMAL:
206 path = ME_NORMAL_BIOS_PATH;
207 break;
208 case ME_HFS_CWS_REC:
209 path = ME_RECOVERY_BIOS_PATH;
210 break;
211 default:
212 path = ME_DISABLE_BIOS_PATH;
213 break;
214 }
215
216 /* Check Current Operation Mode */
217 switch (hfs.operation_mode) {
218 case ME_HFS_MODE_NORMAL:
219 break;
220 case ME_HFS_MODE_DEBUG:
221 case ME_HFS_MODE_DIS:
222 case ME_HFS_MODE_OVER_JMPR:
223 case ME_HFS_MODE_OVER_MEI:
224 default:
225 path = ME_DISABLE_BIOS_PATH;
226 break;
227 }
228
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700229 /* Check for any error code and valid firmware */
230 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200231 path = ME_ERROR_BIOS_PATH;
232
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200233 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700234 struct elog_event_data_me_extended data = {
235 .current_working_state = hfs.working_state,
236 .operation_state = hfs.operation_state,
237 .operation_mode = hfs.operation_mode,
238 .error_code = hfs.error_code,
239 .progress_code = gmes.progress_code,
240 .current_pmevent = gmes.current_pmevent,
241 .current_state = gmes.current_state,
242 };
243 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
244 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
245 &data, sizeof(data));
246 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700247
Stefan Reinauer8e073822012-04-04 00:07:22 +0200248 return path;
249}
250
Stefan Reinauer8e073822012-04-04 00:07:22 +0200251/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200252static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200253{
254 me_bios_path path = intel_me_path(dev);
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300255 u8 me_state = 0, me_state_prev = 0;
256 bool need_reset = false;
257 struct me_hfs hfs;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200258
259 /* Do initial setup and determine the BIOS path */
Angel Pons2e29c3b2020-08-10 15:47:28 +0200260 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_get_bios_path_string(path));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200261
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300262 get_option(&me_state, "me_state");
263 get_option(&me_state_prev, "me_state_prev");
264
265 printk(BIOS_DEBUG, "ME: me_state=%u, me_state_prev=%u\n", me_state, me_state_prev);
266
Stefan Reinauer8e073822012-04-04 00:07:22 +0200267 switch (path) {
268 case ME_S3WAKE_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100269#if CONFIG(HIDE_MEI_ON_ERROR)
270 case ME_ERROR_BIOS_PATH:
271#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200272 intel_me_hide(dev);
273 break;
274
275 case ME_NORMAL_BIOS_PATH:
276 /* Validate the extend register */
277 if (intel_me_extend_valid(dev) < 0)
278 break; /* TODO: force recovery mode */
279
280 /* Prepare MEI MMIO interface */
281 if (intel_mei_setup(dev) < 0)
282 break;
283
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200284 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
285 /* Print ME firmware version */
286 mkhi_get_fw_version();
287 /* Print ME firmware capabilities */
288 mkhi_get_fwcaps();
289 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200290
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300291 /* Put ME in Software Temporary Disable Mode, if needed */
292 if (me_state == CMOS_ME_STATE_DISABLED
293 && CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_NORMAL) {
294 printk(BIOS_INFO, "ME: disabling ME\n");
295 if (enter_soft_temp_disable()) {
296 enter_soft_temp_disable_wait();
297 need_reset = true;
298 } else {
299 printk(BIOS_ERR, "ME: failed to enter Soft Temporary Disable mode\n");
300 }
301
302 break;
303 }
304
Stefan Reinauer8e073822012-04-04 00:07:22 +0200305 /*
306 * Leave the ME unlocked in this path.
307 * It will be locked via SMI command later.
308 */
309 break;
310
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300311 case ME_DISABLE_BIOS_PATH:
312 /* Bring ME out of Soft Temporary Disable mode, if needed */
313 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
314 if (hfs.operation_mode == ME_HFS_MODE_DIS
315 && me_state == CMOS_ME_STATE_NORMAL
316 && (CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_DISABLED
317 || !CMOS_ME_CHANGED(me_state_prev))) {
318 printk(BIOS_INFO, "ME: re-enabling ME\n");
319
320 exit_soft_temp_disable(dev);
321 exit_soft_temp_disable_wait(dev);
322
323 /*
324 * ME starts loading firmware immediately after writing to H_GS,
325 * but Lenovo BIOS performs a reboot after bringing ME back to
326 * Normal mode. Assume that global reset is needed.
327 */
328 need_reset = true;
329 } else {
330 intel_me_hide(dev);
331 }
332 break;
333
James Yea85d4a52020-02-22 20:30:49 +1100334#if !CONFIG(HIDE_MEI_ON_ERROR)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200335 case ME_ERROR_BIOS_PATH:
James Yea85d4a52020-02-22 20:30:49 +1100336#endif
Stefan Reinauer8e073822012-04-04 00:07:22 +0200337 case ME_RECOVERY_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200338 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200339 break;
340 }
Evgeny Zinoviev833e9ba2019-11-21 21:47:31 +0300341
342 /* To avoid boot loops if ME fails to get back from disabled mode,
343 set the 'changed' bit here. */
344 if (me_state != CMOS_ME_STATE(me_state_prev) || need_reset) {
345 u8 new_state = me_state | CMOS_ME_STATE_CHANGED;
346 set_option("me_state_prev", &new_state);
347 }
348
349 if (need_reset) {
350 set_global_reset(true);
351 full_reset();
352 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200353}
354
Stefan Reinauer8e073822012-04-04 00:07:22 +0200355static struct device_operations device_ops = {
356 .read_resources = pci_dev_read_resources,
357 .set_resources = pci_dev_set_resources,
358 .enable_resources = pci_dev_enable_resources,
359 .init = intel_me_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200360 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200361};
362
363static const struct pci_driver intel_me __pci_driver = {
364 .ops = &device_ops,
365 .vendor = PCI_VENDOR_ID_INTEL,
366 .device = 0x1c3a,
367};
368
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200369#endif /* __SIMPLE_DEVICE__ */