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