Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 3 | |
| 4 | #include <types.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 5 | #include <arch/io.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 7 | #include <console/console.h> |
| 8 | #include <cpu/x86/cache.h> |
| 9 | #include <device/pci_def.h> |
| 10 | #include <cpu/x86/smm.h> |
Kyösti Mälkki | e31ec29 | 2019-08-10 17:27:01 +0300 | [diff] [blame] | 11 | #include <cpu/intel/em64t101_save_state.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 12 | #include <elog.h> |
Patrick Georgi | 546953c | 2014-11-29 10:38:17 +0100 | [diff] [blame] | 13 | #include <halt.h> |
Kyösti Mälkki | cbf9571 | 2020-01-05 08:05:45 +0200 | [diff] [blame] | 14 | #include <option.h> |
Tristan Corrick | 63626b1 | 2018-11-30 22:53:50 +1300 | [diff] [blame] | 15 | #include <southbridge/intel/common/finalize.h> |
Tristan Corrick | 09fc634 | 2018-11-30 22:53:01 +1300 | [diff] [blame] | 16 | #include <northbridge/intel/haswell/haswell.h> |
| 17 | #include <cpu/intel/haswell/haswell.h> |
Matt DeVillier | 8187f11 | 2018-12-24 21:46:46 -0600 | [diff] [blame] | 18 | #include <smmstore.h> |
Tristan Corrick | 63626b1 | 2018-11-30 22:53:50 +1300 | [diff] [blame] | 19 | #include "me.h" |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 20 | #include "pch.h" |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 21 | #include "nvs.h" |
| 22 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 23 | static u8 smm_initialized = 0; |
| 24 | |
| 25 | /* GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located |
| 26 | * by coreboot. |
| 27 | */ |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 28 | static global_nvs_t *gnvs; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 29 | global_nvs_t *smm_get_gnvs(void) |
| 30 | { |
| 31 | return gnvs; |
| 32 | } |
| 33 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 34 | int southbridge_io_trap_handler(int smif) |
| 35 | { |
| 36 | switch (smif) { |
| 37 | case 0x32: |
| 38 | printk(BIOS_DEBUG, "OS Init\n"); |
| 39 | /* gnvs->smif: |
| 40 | * On success, the IO Trap Handler returns 0 |
| 41 | * On failure, the IO Trap Handler returns a value != 0 |
| 42 | */ |
| 43 | gnvs->smif = 0; |
| 44 | return 1; /* IO trap handled */ |
| 45 | } |
| 46 | |
| 47 | /* Not handled */ |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @brief Set the EOS bit |
| 53 | */ |
| 54 | void southbridge_smi_set_eos(void) |
| 55 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 56 | enable_smi(EOS); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void busmaster_disable_on_bus(int bus) |
| 60 | { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 61 | int slot, func; |
| 62 | unsigned int val; |
| 63 | unsigned char hdr; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 64 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 65 | for (slot = 0; slot < 0x20; slot++) { |
| 66 | for (func = 0; func < 8; func++) { |
Elyes HAOUAS | 73ae076 | 2020-04-28 10:13:05 +0200 | [diff] [blame^] | 67 | u16 reg16; |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 68 | pci_devfn_t dev = PCI_DEV(bus, slot, func); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 69 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 70 | val = pci_read_config32(dev, PCI_VENDOR_ID); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 71 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 72 | if (val == 0xffffffff || val == 0x00000000 || |
| 73 | val == 0x0000ffff || val == 0xffff0000) |
| 74 | continue; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 75 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 76 | /* Disable Bus Mastering for this one device */ |
Elyes HAOUAS | 73ae076 | 2020-04-28 10:13:05 +0200 | [diff] [blame^] | 77 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 78 | reg16 &= ~PCI_COMMAND_MASTER; |
| 79 | pci_write_config16(dev, PCI_COMMAND, reg16); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 80 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 81 | /* If this is a bridge, then follow it. */ |
| 82 | hdr = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 83 | hdr &= 0x7f; |
| 84 | if (hdr == PCI_HEADER_TYPE_BRIDGE || |
| 85 | hdr == PCI_HEADER_TYPE_CARDBUS) { |
| 86 | unsigned int buses; |
| 87 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
| 88 | busmaster_disable_on_bus((buses >> 8) & 0xff); |
| 89 | } |
| 90 | } |
| 91 | } |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Duncan Laurie | 2d9d39a | 2013-05-29 15:27:55 -0700 | [diff] [blame] | 94 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 95 | static void southbridge_smi_sleep(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 96 | { |
| 97 | u8 reg8; |
| 98 | u32 reg32; |
| 99 | u8 slp_typ; |
Nico Huber | 9faae2b | 2018-11-14 00:00:35 +0100 | [diff] [blame] | 100 | u8 s5pwr = CONFIG_MAINBOARD_POWER_FAILURE_STATE; |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 101 | u16 pmbase = get_pmbase(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 102 | |
| 103 | // save and recover RTC port values |
| 104 | u8 tmp70, tmp72; |
| 105 | tmp70 = inb(0x70); |
| 106 | tmp72 = inb(0x72); |
| 107 | get_option(&s5pwr, "power_on_after_fail"); |
| 108 | outb(tmp70, 0x70); |
| 109 | outb(tmp72, 0x72); |
| 110 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 111 | /* First, disable further SMIs */ |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 112 | disable_smi(SLP_SMI_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 113 | |
| 114 | /* Figure out SLP_TYP */ |
| 115 | reg32 = inl(pmbase + PM1_CNT); |
| 116 | printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32); |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 117 | slp_typ = acpi_sleep_from_pm1(reg32); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 118 | |
| 119 | /* Do any mainboard sleep handling */ |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 120 | mainboard_smi_sleep(slp_typ); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 121 | |
Duncan Laurie | 2d9d39a | 2013-05-29 15:27:55 -0700 | [diff] [blame] | 122 | /* USB sleep preparations */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 123 | #if !CONFIG(FINALIZE_USB_ROUTE_XHCI) |
Duncan Laurie | 1f52908 | 2013-07-30 15:53:45 -0700 | [diff] [blame] | 124 | usb_ehci_sleep_prepare(PCH_EHCI1_DEV, slp_typ); |
| 125 | usb_ehci_sleep_prepare(PCH_EHCI2_DEV, slp_typ); |
Duncan Laurie | 911cedf | 2013-07-30 16:05:55 -0700 | [diff] [blame] | 126 | #endif |
Duncan Laurie | 1f52908 | 2013-07-30 15:53:45 -0700 | [diff] [blame] | 127 | usb_xhci_sleep_prepare(PCH_XHCI_DEV, slp_typ); |
Duncan Laurie | 2d9d39a | 2013-05-29 15:27:55 -0700 | [diff] [blame] | 128 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 129 | /* Log S3, S4, and S5 entry */ |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 130 | if (slp_typ >= ACPI_S3) |
Kyösti Mälkki | 9dd1a12 | 2019-11-06 11:04:27 +0200 | [diff] [blame] | 131 | elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 132 | |
| 133 | /* Next, do the deed. |
| 134 | */ |
| 135 | |
| 136 | switch (slp_typ) { |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 137 | case ACPI_S0: |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 138 | printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n"); |
| 139 | break; |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 140 | case ACPI_S1: |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 141 | printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n"); |
| 142 | break; |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 143 | case ACPI_S3: |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 144 | printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n"); |
| 145 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 146 | /* Invalidate the cache before going to S3 */ |
| 147 | wbinvd(); |
| 148 | break; |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 149 | case ACPI_S4: |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 150 | printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n"); |
| 151 | break; |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 152 | case ACPI_S5: |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n"); |
| 154 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 155 | /* Disable all GPE */ |
| 156 | disable_all_gpe(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 157 | |
| 158 | /* Always set the flag in case CMOS was changed on runtime. For |
| 159 | * "KEEP", switch to "OFF" - KEEP is software emulated |
| 160 | */ |
Aaron Durbin | 89f79a0 | 2012-10-31 23:05:25 -0500 | [diff] [blame] | 161 | reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 162 | if (s5pwr == MAINBOARD_POWER_ON) { |
| 163 | reg8 &= ~1; |
| 164 | } else { |
| 165 | reg8 |= 1; |
| 166 | } |
Aaron Durbin | 89f79a0 | 2012-10-31 23:05:25 -0500 | [diff] [blame] | 167 | pci_write_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3, reg8); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 168 | |
| 169 | /* also iterates over all bridges on bus 0 */ |
| 170 | busmaster_disable_on_bus(0); |
| 171 | break; |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 172 | default: |
| 173 | printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n"); |
| 174 | break; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Write back to the SLP register to cause the originally intended |
| 178 | * event again. We need to set BIT13 (SLP_EN) though to make the |
| 179 | * sleep happen. |
| 180 | */ |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 181 | enable_pm1_control(SLP_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 182 | |
| 183 | /* Make sure to stop executing code here for S3/S4/S5 */ |
Aaron Durbin | da5f509 | 2016-07-13 23:23:16 -0500 | [diff] [blame] | 184 | if (slp_typ >= ACPI_S3) |
Patrick Georgi | 546953c | 2014-11-29 10:38:17 +0100 | [diff] [blame] | 185 | halt(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 186 | |
| 187 | /* In most sleep states, the code flow of this function ends at |
| 188 | * the line above. However, if we entered sleep state S1 and wake |
| 189 | * up again, we will continue to execute code in this function. |
| 190 | */ |
| 191 | reg32 = inl(pmbase + PM1_CNT); |
| 192 | if (reg32 & SCI_EN) { |
| 193 | /* The OS is not an ACPI OS, so we set the state to S0 */ |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 194 | disable_pm1_control(SLP_EN | SLP_TYP); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Look for Synchronous IO SMI and use save state from that |
| 200 | * core in case we are not running on the same core that |
| 201 | * initiated the IO transaction. |
| 202 | */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 203 | static em64t101_smm_state_save_area_t *smi_apmc_find_state_save(u8 cmd) |
| 204 | { |
| 205 | em64t101_smm_state_save_area_t *state; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 206 | int node; |
| 207 | |
| 208 | /* Check all nodes looking for the one that issued the IO */ |
| 209 | for (node = 0; node < CONFIG_MAX_CPUS; node++) { |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 210 | state = smm_get_save_state(node); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 211 | |
Elyes HAOUAS | 581fe58 | 2018-04-26 09:57:07 +0200 | [diff] [blame] | 212 | /* Check for Synchronous IO (bit0 == 1) */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 213 | if (!(state->io_misc_info & (1 << 0))) |
| 214 | continue; |
| 215 | |
Elyes HAOUAS | 581fe58 | 2018-04-26 09:57:07 +0200 | [diff] [blame] | 216 | /* Make sure it was a write (bit4 == 0) */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 217 | if (state->io_misc_info & (1 << 4)) |
| 218 | continue; |
| 219 | |
| 220 | /* Check for APMC IO port */ |
| 221 | if (((state->io_misc_info >> 16) & 0xff) != APM_CNT) |
| 222 | continue; |
| 223 | |
| 224 | /* Check AX against the requested command */ |
| 225 | if ((state->rax & 0xff) != cmd) |
| 226 | continue; |
| 227 | |
| 228 | return state; |
| 229 | } |
| 230 | |
| 231 | return NULL; |
| 232 | } |
| 233 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 234 | static void southbridge_smi_gsmi(void) |
| 235 | { |
| 236 | u32 *ret, *param; |
| 237 | u8 sub_command; |
| 238 | em64t101_smm_state_save_area_t *io_smi = |
Patrick Georgi | d61839c | 2018-12-03 16:10:33 +0100 | [diff] [blame] | 239 | smi_apmc_find_state_save(APM_CNT_ELOG_GSMI); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 240 | |
| 241 | if (!io_smi) |
| 242 | return; |
| 243 | |
| 244 | /* Command and return value in EAX */ |
| 245 | ret = (u32*)&io_smi->rax; |
| 246 | sub_command = (u8)(*ret >> 8); |
| 247 | |
| 248 | /* Parameter buffer in EBX */ |
| 249 | param = (u32*)&io_smi->rbx; |
| 250 | |
| 251 | /* drivers/elog/gsmi.c */ |
| 252 | *ret = gsmi_exec(sub_command, param); |
| 253 | } |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 254 | |
Matt DeVillier | 8187f11 | 2018-12-24 21:46:46 -0600 | [diff] [blame] | 255 | static void southbridge_smi_store(void) |
| 256 | { |
| 257 | u8 sub_command, ret; |
| 258 | em64t101_smm_state_save_area_t *io_smi = |
| 259 | smi_apmc_find_state_save(APM_CNT_SMMSTORE); |
| 260 | uint32_t reg_ebx; |
| 261 | |
| 262 | if (!io_smi) |
| 263 | return; |
| 264 | /* Command and return value in EAX */ |
| 265 | sub_command = (io_smi->rax >> 8) & 0xff; |
| 266 | |
| 267 | /* Parameter buffer in EBX */ |
| 268 | reg_ebx = io_smi->rbx; |
| 269 | |
| 270 | /* drivers/smmstore/smi.c */ |
| 271 | ret = smmstore_exec(sub_command, (void *)reg_ebx); |
| 272 | io_smi->rax = ret; |
| 273 | } |
| 274 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 275 | static void southbridge_smi_apmc(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 276 | { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 277 | u8 reg8; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 278 | em64t101_smm_state_save_area_t *state; |
Tristan Corrick | 09fc634 | 2018-11-30 22:53:01 +1300 | [diff] [blame] | 279 | static int chipset_finalized = 0; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 280 | |
| 281 | /* Emulate B2 register as the FADT / Linux expects it */ |
| 282 | |
| 283 | reg8 = inb(APM_CNT); |
| 284 | switch (reg8) { |
Tristan Corrick | 09fc634 | 2018-11-30 22:53:01 +1300 | [diff] [blame] | 285 | case APM_CNT_FINALIZE: |
| 286 | if (chipset_finalized) { |
| 287 | printk(BIOS_DEBUG, "SMI#: Already finalized\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
Tristan Corrick | 63626b1 | 2018-11-30 22:53:50 +1300 | [diff] [blame] | 291 | intel_me_finalize_smm(); |
Tristan Corrick | 09fc634 | 2018-11-30 22:53:01 +1300 | [diff] [blame] | 292 | intel_pch_finalize_smm(); |
| 293 | intel_northbridge_haswell_finalize_smm(); |
| 294 | intel_cpu_haswell_finalize_smm(); |
| 295 | |
| 296 | chipset_finalized = 1; |
| 297 | break; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 298 | case APM_CNT_CST_CONTROL: |
| 299 | /* Calling this function seems to cause |
| 300 | * some kind of race condition in Linux |
| 301 | * and causes a kernel oops |
| 302 | */ |
| 303 | printk(BIOS_DEBUG, "C-state control\n"); |
| 304 | break; |
| 305 | case APM_CNT_PST_CONTROL: |
| 306 | /* Calling this function seems to cause |
| 307 | * some kind of race condition in Linux |
| 308 | * and causes a kernel oops |
| 309 | */ |
| 310 | printk(BIOS_DEBUG, "P-state control\n"); |
| 311 | break; |
| 312 | case APM_CNT_ACPI_DISABLE: |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 313 | disable_pm1_control(SCI_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 314 | printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n"); |
| 315 | break; |
| 316 | case APM_CNT_ACPI_ENABLE: |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 317 | enable_pm1_control(SCI_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 318 | printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n"); |
| 319 | break; |
| 320 | case APM_CNT_GNVS_UPDATE: |
| 321 | if (smm_initialized) { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 322 | printk(BIOS_DEBUG, |
| 323 | "SMI#: SMM structures already initialized!\n"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | state = smi_apmc_find_state_save(reg8); |
| 327 | if (state) { |
| 328 | /* EBX in the state save contains the GNVS pointer */ |
| 329 | gnvs = (global_nvs_t *)((u32)state->rbx); |
| 330 | smm_initialized = 1; |
| 331 | printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs); |
| 332 | } |
| 333 | break; |
Duncan Laurie | 78145a5 | 2013-08-21 13:16:21 -0700 | [diff] [blame] | 334 | case 0xca: |
Duncan Laurie | 911cedf | 2013-07-30 16:05:55 -0700 | [diff] [blame] | 335 | usb_xhci_route_all(); |
Duncan Laurie | 911cedf | 2013-07-30 16:05:55 -0700 | [diff] [blame] | 336 | break; |
Patrick Georgi | d61839c | 2018-12-03 16:10:33 +0100 | [diff] [blame] | 337 | case APM_CNT_ELOG_GSMI: |
Kyösti Mälkki | 9dd1a12 | 2019-11-06 11:04:27 +0200 | [diff] [blame] | 338 | if (CONFIG(ELOG_GSMI)) |
| 339 | southbridge_smi_gsmi(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 340 | break; |
Matt DeVillier | 8187f11 | 2018-12-24 21:46:46 -0600 | [diff] [blame] | 341 | case APM_CNT_SMMSTORE: |
| 342 | if (CONFIG(SMMSTORE)) |
| 343 | southbridge_smi_store(); |
| 344 | break; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 347 | mainboard_smi_apmc(reg8); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 348 | } |
| 349 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 350 | static void southbridge_smi_pm1(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 351 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 352 | u16 pm1_sts = clear_pm1_status(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 353 | |
| 354 | /* While OSPM is not active, poweroff immediately |
| 355 | * on a power button event. |
| 356 | */ |
| 357 | if (pm1_sts & PWRBTN_STS) { |
| 358 | // power button pressed |
Kyösti Mälkki | 9dd1a12 | 2019-11-06 11:04:27 +0200 | [diff] [blame] | 359 | elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON); |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 360 | disable_pm1_control(-1UL); |
| 361 | enable_pm1_control(SLP_EN | (SLP_TYP_S5 << 10)); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 365 | static void southbridge_smi_gpe0(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 366 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 367 | clear_gpe_status(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 370 | static void southbridge_smi_gpi(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 371 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 372 | mainboard_smi_gpi(clear_alt_smi_status()); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 373 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 374 | /* Clear again after mainboard handler */ |
| 375 | clear_alt_smi_status(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 376 | } |
| 377 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 378 | static void southbridge_smi_mc(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 379 | { |
| 380 | u32 reg32; |
| 381 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 382 | reg32 = inl(get_pmbase() + SMI_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 383 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 384 | /* Are microcontroller SMIs enabled? */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 385 | if ((reg32 & MCSMI_EN) == 0) |
| 386 | return; |
| 387 | |
| 388 | printk(BIOS_DEBUG, "Microcontroller SMI.\n"); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 393 | static void southbridge_smi_tco(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 394 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 395 | u32 tco_sts = clear_tco_status(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 396 | |
| 397 | /* Any TCO event? */ |
| 398 | if (!tco_sts) |
| 399 | return; |
| 400 | |
| 401 | if (tco_sts & (1 << 8)) { // BIOSWR |
| 402 | u8 bios_cntl; |
| 403 | |
Aaron Durbin | 89f79a0 | 2012-10-31 23:05:25 -0500 | [diff] [blame] | 404 | bios_cntl = pci_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 405 | |
| 406 | if (bios_cntl & 1) { |
| 407 | /* BWE is RW, so the SMI was caused by a |
| 408 | * write to BWE, not by a write to the BIOS |
| 409 | */ |
| 410 | |
| 411 | /* This is the place where we notice someone |
| 412 | * is trying to tinker with the BIOS. We are |
| 413 | * trying to be nice and just ignore it. A more |
| 414 | * resolute answer would be to power down the |
| 415 | * box. |
| 416 | */ |
| 417 | printk(BIOS_DEBUG, "Switching back to RO\n"); |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 418 | pci_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, |
| 419 | (bios_cntl & ~1)); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 420 | } /* No else for now? */ |
| 421 | } else if (tco_sts & (1 << 3)) { /* TIMEOUT */ |
| 422 | /* Handle TCO timeout */ |
| 423 | printk(BIOS_DEBUG, "TCO Timeout.\n"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 427 | static void southbridge_smi_periodic(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 428 | { |
| 429 | u32 reg32; |
| 430 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 431 | reg32 = inl(get_pmbase() + SMI_EN); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 432 | |
| 433 | /* Are periodic SMIs enabled? */ |
| 434 | if ((reg32 & PERIODIC_EN) == 0) |
| 435 | return; |
| 436 | |
| 437 | printk(BIOS_DEBUG, "Periodic SMI.\n"); |
| 438 | } |
| 439 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 440 | static void southbridge_smi_monitor(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 441 | { |
| 442 | #define IOTRAP(x) (trap_sts & (1 << x)) |
| 443 | u32 trap_sts, trap_cycle; |
| 444 | u32 data, mask = 0; |
| 445 | int i; |
| 446 | |
| 447 | trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register |
| 448 | RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR |
| 449 | |
| 450 | trap_cycle = RCBA32(0x1e10); |
| 451 | for (i=16; i<20; i++) { |
| 452 | if (trap_cycle & (1 << i)) |
| 453 | mask |= (0xff << ((i - 16) << 2)); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /* IOTRAP(3) SMI function call */ |
| 458 | if (IOTRAP(3)) { |
| 459 | if (gnvs && gnvs->smif) |
| 460 | io_trap_handler(gnvs->smif); // call function smif |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | /* IOTRAP(2) currently unused |
| 465 | * IOTRAP(1) currently unused */ |
| 466 | |
| 467 | /* IOTRAP(0) SMIC */ |
| 468 | if (IOTRAP(0)) { |
| 469 | if (!(trap_cycle & (1 << 24))) { // It's a write |
| 470 | printk(BIOS_DEBUG, "SMI1 command\n"); |
| 471 | data = RCBA32(0x1e18); |
| 472 | data &= mask; |
| 473 | // if (smi1) |
Elyes HAOUAS | b0f1988 | 2018-06-09 11:59:00 +0200 | [diff] [blame] | 474 | // southbridge_smi_command(data); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 475 | // return; |
| 476 | } |
| 477 | // Fall through to debug |
| 478 | } |
| 479 | |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 480 | printk(BIOS_DEBUG, " trapped io address = 0x%x\n", |
| 481 | trap_cycle & 0xfffc); |
| 482 | for (i=0; i < 4; i++) |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 483 | if (IOTRAP(i)) printk(BIOS_DEBUG, " TRAP = %d\n", i); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 484 | printk(BIOS_DEBUG, " AHBE = %x\n", (trap_cycle >> 16) & 0xf); |
| 485 | printk(BIOS_DEBUG, " MASK = 0x%08x\n", mask); |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 486 | printk(BIOS_DEBUG, " read/write: %s\n", |
| 487 | (trap_cycle & (1 << 24)) ? "read" : "write"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 488 | |
| 489 | if (!(trap_cycle & (1 << 24))) { |
| 490 | /* Write Cycle */ |
| 491 | data = RCBA32(0x1e18); |
| 492 | printk(BIOS_DEBUG, " iotrap written data = 0x%08x\n", data); |
| 493 | } |
| 494 | #undef IOTRAP |
| 495 | } |
| 496 | |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 497 | typedef void (*smi_handler_t)(void); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 498 | |
| 499 | static smi_handler_t southbridge_smi[32] = { |
| 500 | NULL, // [0] reserved |
| 501 | NULL, // [1] reserved |
| 502 | NULL, // [2] BIOS_STS |
| 503 | NULL, // [3] LEGACY_USB_STS |
| 504 | southbridge_smi_sleep, // [4] SLP_SMI_STS |
| 505 | southbridge_smi_apmc, // [5] APM_STS |
| 506 | NULL, // [6] SWSMI_TMR_STS |
| 507 | NULL, // [7] reserved |
| 508 | southbridge_smi_pm1, // [8] PM1_STS |
| 509 | southbridge_smi_gpe0, // [9] GPE0_STS |
| 510 | southbridge_smi_gpi, // [10] GPI_STS |
| 511 | southbridge_smi_mc, // [11] MCSMI_STS |
| 512 | NULL, // [12] DEVMON_STS |
| 513 | southbridge_smi_tco, // [13] TCO_STS |
| 514 | southbridge_smi_periodic, // [14] PERIODIC_STS |
| 515 | NULL, // [15] SERIRQ_SMI_STS |
| 516 | NULL, // [16] SMBUS_SMI_STS |
| 517 | NULL, // [17] LEGACY_USB2_STS |
| 518 | NULL, // [18] INTEL_USB2_STS |
| 519 | NULL, // [19] reserved |
| 520 | NULL, // [20] PCI_EXP_SMI_STS |
| 521 | southbridge_smi_monitor, // [21] MONITOR_STS |
| 522 | NULL, // [22] reserved |
| 523 | NULL, // [23] reserved |
| 524 | NULL, // [24] reserved |
| 525 | NULL, // [25] EL_SMI_STS |
| 526 | NULL, // [26] SPI_STS |
| 527 | NULL, // [27] reserved |
| 528 | NULL, // [28] reserved |
| 529 | NULL, // [29] reserved |
| 530 | NULL, // [30] reserved |
| 531 | NULL // [31] reserved |
| 532 | }; |
| 533 | |
| 534 | /** |
| 535 | * @brief Interrupt handler for SMI# |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 536 | */ |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 537 | void southbridge_smi_handler(void) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 538 | { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 539 | int i; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 540 | u32 smi_sts; |
| 541 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 542 | /* We need to clear the SMI status registers, or we won't see what's |
| 543 | * happening in the following calls. |
| 544 | */ |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 545 | smi_sts = clear_smi_status(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 546 | |
| 547 | /* Call SMI sub handler for each of the status bits */ |
| 548 | for (i = 0; i < 31; i++) { |
| 549 | if (smi_sts & (1 << i)) { |
| 550 | if (southbridge_smi[i]) { |
Aaron Durbin | 29ffa54 | 2012-12-21 21:21:48 -0600 | [diff] [blame] | 551 | southbridge_smi[i](); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 552 | } else { |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 553 | printk(BIOS_DEBUG, |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 554 | "SMI_STS[%d] occurred, but no " |
Duncan Laurie | 467f31d | 2013-03-08 17:00:37 -0800 | [diff] [blame] | 555 | "handler available.\n", i); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 559 | } |