Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 2 | |
| 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 Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 11 | #include <acpi/acpi.h> |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 12 | #include <cf9_reset.h> |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 13 | #include <device/mmio.h> |
Kyösti Mälkki | 21d6a27 | 2019-11-05 18:50:38 +0200 | [diff] [blame] | 14 | #include <device/device.h> |
| 15 | #include <device/pci.h> |
Angel Pons | 7f32df3 | 2020-06-02 13:36:57 +0200 | [diff] [blame] | 16 | #include <device/pci_ops.h> |
| 17 | #include <console/console.h> |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 18 | #include <device/pci_ids.h> |
| 19 | #include <device/pci_def.h> |
| 20 | #include <string.h> |
| 21 | #include <delay.h> |
Duncan Laurie | c1c9435 | 2012-07-13 10:11:54 -0700 | [diff] [blame] | 22 | #include <elog.h> |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 23 | #include <halt.h> |
| 24 | #include <option.h> |
| 25 | #include <southbridge/intel/common/me.h> |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 26 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 27 | #include "me.h" |
| 28 | #include "pch.h" |
| 29 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 30 | /* Determine the path that we should take based on ME status */ |
Elyes HAOUAS | dc03528 | 2018-09-18 13:28:49 +0200 | [diff] [blame] | 31 | static me_bios_path intel_me_path(struct device *dev) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 32 | { |
| 33 | me_bios_path path = ME_DISABLE_BIOS_PATH; |
| 34 | struct me_hfs hfs; |
| 35 | struct me_gmes gmes; |
| 36 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 37 | /* S3 wake skips all MKHI messages */ |
Kyösti Mälkki | c3ed886 | 2014-06-19 19:50:51 +0300 | [diff] [blame] | 38 | if (acpi_is_wakeup_s3()) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 39 | return ME_S3WAKE_BIOS_PATH; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 40 | |
| 41 | pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS); |
| 42 | pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES); |
| 43 | |
| 44 | /* Check and dump status */ |
| 45 | intel_me_status(&hfs, &gmes); |
| 46 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 47 | /* Check Current Working State */ |
| 48 | switch (hfs.working_state) { |
| 49 | case ME_HFS_CWS_NORMAL: |
| 50 | path = ME_NORMAL_BIOS_PATH; |
| 51 | break; |
| 52 | case ME_HFS_CWS_REC: |
| 53 | path = ME_RECOVERY_BIOS_PATH; |
| 54 | break; |
| 55 | default: |
| 56 | path = ME_DISABLE_BIOS_PATH; |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | /* Check Current Operation Mode */ |
| 61 | switch (hfs.operation_mode) { |
| 62 | case ME_HFS_MODE_NORMAL: |
| 63 | break; |
| 64 | case ME_HFS_MODE_DEBUG: |
| 65 | case ME_HFS_MODE_DIS: |
| 66 | case ME_HFS_MODE_OVER_JMPR: |
| 67 | case ME_HFS_MODE_OVER_MEI: |
| 68 | default: |
| 69 | path = ME_DISABLE_BIOS_PATH; |
| 70 | break; |
| 71 | } |
| 72 | |
Duncan Laurie | 5c88c6f | 2012-09-01 14:00:23 -0700 | [diff] [blame] | 73 | /* Check for any error code and valid firmware */ |
| 74 | if (hfs.error_code || hfs.fpt_bad) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 75 | path = ME_ERROR_BIOS_PATH; |
| 76 | |
Kyösti Mälkki | be5317f | 2019-11-06 12:07:21 +0200 | [diff] [blame] | 77 | if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) { |
Duncan Laurie | 5c88c6f | 2012-09-01 14:00:23 -0700 | [diff] [blame] | 78 | struct elog_event_data_me_extended data = { |
| 79 | .current_working_state = hfs.working_state, |
| 80 | .operation_state = hfs.operation_state, |
| 81 | .operation_mode = hfs.operation_mode, |
| 82 | .error_code = hfs.error_code, |
| 83 | .progress_code = gmes.progress_code, |
| 84 | .current_pmevent = gmes.current_pmevent, |
| 85 | .current_state = gmes.current_state, |
| 86 | }; |
| 87 | elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path); |
| 88 | elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT, |
| 89 | &data, sizeof(data)); |
| 90 | } |
Duncan Laurie | 5c88c6f | 2012-09-01 14:00:23 -0700 | [diff] [blame] | 91 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 92 | return path; |
| 93 | } |
| 94 | |
Angel Pons | c94bc8e | 2021-01-27 12:17:33 +0100 | [diff] [blame] | 95 | /* Get ME firmware version */ |
| 96 | static int mkhi_get_fw_version(void) |
| 97 | { |
| 98 | struct me_fw_version version; |
| 99 | struct mkhi_header mkhi = { |
| 100 | .group_id = MKHI_GROUP_ID_GEN, |
| 101 | .command = MKHI_GET_FW_VERSION, |
| 102 | }; |
| 103 | struct mei_header mei = { |
| 104 | .is_complete = 1, |
| 105 | .host_address = MEI_HOST_ADDRESS, |
| 106 | .client_address = MEI_ADDRESS_MKHI, |
| 107 | .length = sizeof(mkhi), |
| 108 | }; |
| 109 | |
| 110 | /* Send request and wait for response */ |
| 111 | if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) { |
| 112 | printk(BIOS_ERR, "ME: GET FW VERSION message failed\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) " |
| 117 | "%u.%u.%u.%u (recovery)\n", |
| 118 | version.code_major, version.code_minor, |
| 119 | version.code_build_number, version.code_hot_fix, |
| 120 | version.recovery_major, version.recovery_minor, |
| 121 | version.recovery_build_number, version.recovery_hot_fix); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static inline void print_cap(const char *name, int state) |
| 127 | { |
| 128 | printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n", |
| 129 | name, state ? "en" : "dis"); |
| 130 | } |
| 131 | |
| 132 | /* Get ME Firmware Capabilities */ |
| 133 | static int mkhi_get_fwcaps(void) |
| 134 | { |
| 135 | u32 rule_id = 0; |
| 136 | struct me_fwcaps cap; |
| 137 | struct mkhi_header mkhi = { |
| 138 | .group_id = MKHI_GROUP_ID_FWCAPS, |
| 139 | .command = MKHI_FWCAPS_GET_RULE, |
| 140 | }; |
| 141 | struct mei_header mei = { |
| 142 | .is_complete = 1, |
| 143 | .host_address = MEI_HOST_ADDRESS, |
| 144 | .client_address = MEI_ADDRESS_MKHI, |
| 145 | .length = sizeof(mkhi) + sizeof(rule_id), |
| 146 | }; |
| 147 | |
| 148 | /* Send request and wait for response */ |
| 149 | if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) { |
| 150 | printk(BIOS_ERR, "ME: GET FWCAPS message failed\n"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | print_cap("Full Network manageability", cap.caps_sku.full_net); |
| 155 | print_cap("Regular Network manageability", cap.caps_sku.std_net); |
| 156 | print_cap("Manageability", cap.caps_sku.manageability); |
| 157 | print_cap("Small business technology", cap.caps_sku.small_business); |
| 158 | print_cap("Level III manageability", cap.caps_sku.l3manageability); |
| 159 | print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at); |
| 160 | print_cap("IntelR Capability Licensing Service (CLS)", |
| 161 | cap.caps_sku.intel_cls); |
| 162 | print_cap("IntelR Power Sharing Technology (MPC)", |
| 163 | cap.caps_sku.intel_mpc); |
| 164 | print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking); |
| 165 | print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp); |
| 166 | print_cap("IPV6", cap.caps_sku.ipv6); |
| 167 | print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm); |
| 168 | print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och); |
| 169 | print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan); |
| 170 | print_cap("TLS", cap.caps_sku.tls); |
| 171 | print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 177 | /* Check whether ME is present and do basic init */ |
Elyes HAOUAS | dc03528 | 2018-09-18 13:28:49 +0200 | [diff] [blame] | 178 | static void intel_me_init(struct device *dev) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 179 | { |
| 180 | me_bios_path path = intel_me_path(dev); |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 181 | u8 me_state = 0, me_state_prev = 0; |
| 182 | bool need_reset = false; |
| 183 | struct me_hfs hfs; |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 184 | |
| 185 | /* Do initial setup and determine the BIOS path */ |
Angel Pons | 2e29c3b | 2020-08-10 15:47:28 +0200 | [diff] [blame] | 186 | printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_get_bios_path_string(path)); |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 187 | |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 188 | get_option(&me_state, "me_state"); |
| 189 | get_option(&me_state_prev, "me_state_prev"); |
| 190 | |
| 191 | printk(BIOS_DEBUG, "ME: me_state=%u, me_state_prev=%u\n", me_state, me_state_prev); |
| 192 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 193 | switch (path) { |
| 194 | case ME_S3WAKE_BIOS_PATH: |
James Ye | a85d4a5 | 2020-02-22 20:30:49 +1100 | [diff] [blame] | 195 | #if CONFIG(HIDE_MEI_ON_ERROR) |
| 196 | case ME_ERROR_BIOS_PATH: |
| 197 | #endif |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 198 | intel_me_hide(dev); |
| 199 | break; |
| 200 | |
| 201 | case ME_NORMAL_BIOS_PATH: |
| 202 | /* Validate the extend register */ |
| 203 | if (intel_me_extend_valid(dev) < 0) |
| 204 | break; /* TODO: force recovery mode */ |
| 205 | |
| 206 | /* Prepare MEI MMIO interface */ |
| 207 | if (intel_mei_setup(dev) < 0) |
| 208 | break; |
| 209 | |
Kyösti Mälkki | c86fc8e | 2019-11-06 06:32:27 +0200 | [diff] [blame] | 210 | if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) { |
| 211 | /* Print ME firmware version */ |
| 212 | mkhi_get_fw_version(); |
| 213 | /* Print ME firmware capabilities */ |
| 214 | mkhi_get_fwcaps(); |
| 215 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 216 | |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 217 | /* Put ME in Software Temporary Disable Mode, if needed */ |
| 218 | if (me_state == CMOS_ME_STATE_DISABLED |
| 219 | && CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_NORMAL) { |
| 220 | printk(BIOS_INFO, "ME: disabling ME\n"); |
| 221 | if (enter_soft_temp_disable()) { |
| 222 | enter_soft_temp_disable_wait(); |
| 223 | need_reset = true; |
| 224 | } else { |
| 225 | printk(BIOS_ERR, "ME: failed to enter Soft Temporary Disable mode\n"); |
| 226 | } |
| 227 | |
| 228 | break; |
| 229 | } |
| 230 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 231 | /* |
| 232 | * Leave the ME unlocked in this path. |
| 233 | * It will be locked via SMI command later. |
| 234 | */ |
| 235 | break; |
| 236 | |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 237 | case ME_DISABLE_BIOS_PATH: |
| 238 | /* Bring ME out of Soft Temporary Disable mode, if needed */ |
| 239 | pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS); |
| 240 | if (hfs.operation_mode == ME_HFS_MODE_DIS |
| 241 | && me_state == CMOS_ME_STATE_NORMAL |
| 242 | && (CMOS_ME_STATE(me_state_prev) == CMOS_ME_STATE_DISABLED |
| 243 | || !CMOS_ME_CHANGED(me_state_prev))) { |
| 244 | printk(BIOS_INFO, "ME: re-enabling ME\n"); |
| 245 | |
| 246 | exit_soft_temp_disable(dev); |
| 247 | exit_soft_temp_disable_wait(dev); |
| 248 | |
| 249 | /* |
| 250 | * ME starts loading firmware immediately after writing to H_GS, |
| 251 | * but Lenovo BIOS performs a reboot after bringing ME back to |
| 252 | * Normal mode. Assume that global reset is needed. |
| 253 | */ |
| 254 | need_reset = true; |
| 255 | } else { |
| 256 | intel_me_hide(dev); |
| 257 | } |
| 258 | break; |
| 259 | |
James Ye | a85d4a5 | 2020-02-22 20:30:49 +1100 | [diff] [blame] | 260 | #if !CONFIG(HIDE_MEI_ON_ERROR) |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 261 | case ME_ERROR_BIOS_PATH: |
James Ye | a85d4a5 | 2020-02-22 20:30:49 +1100 | [diff] [blame] | 262 | #endif |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 263 | case ME_RECOVERY_BIOS_PATH: |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 264 | case ME_FIRMWARE_UPDATE_BIOS_PATH: |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 265 | break; |
| 266 | } |
Evgeny Zinoviev | 833e9ba | 2019-11-21 21:47:31 +0300 | [diff] [blame] | 267 | |
| 268 | /* To avoid boot loops if ME fails to get back from disabled mode, |
| 269 | set the 'changed' bit here. */ |
| 270 | if (me_state != CMOS_ME_STATE(me_state_prev) || need_reset) { |
| 271 | u8 new_state = me_state | CMOS_ME_STATE_CHANGED; |
| 272 | set_option("me_state_prev", &new_state); |
| 273 | } |
| 274 | |
| 275 | if (need_reset) { |
| 276 | set_global_reset(true); |
| 277 | full_reset(); |
| 278 | } |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 281 | static struct device_operations device_ops = { |
| 282 | .read_resources = pci_dev_read_resources, |
| 283 | .set_resources = pci_dev_set_resources, |
| 284 | .enable_resources = pci_dev_enable_resources, |
| 285 | .init = intel_me_init, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 286 | .ops_pci = &pci_dev_ops_pci, |
Stefan Reinauer | 8e07382 | 2012-04-04 00:07:22 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | static const struct pci_driver intel_me __pci_driver = { |
| 290 | .ops = &device_ops, |
| 291 | .vendor = PCI_VENDOR_ID_INTEL, |
| 292 | .device = 0x1c3a, |
| 293 | }; |