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