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