Martin Roth | bf6b83a | 2015-10-11 10:37:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Martin Roth | bf6b83a | 2015-10-11 10:37:02 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <types.h> |
| 18 | #include <arch/io.h> |
| 19 | #include <console/console.h> |
| 20 | #include <cpu/x86/cache.h> |
| 21 | #include <device/pci_def.h> |
| 22 | #include <cpu/x86/smm.h> |
| 23 | #include <elog.h> |
| 24 | #include <halt.h> |
| 25 | #include <pc80/mc146818rtc.h> |
| 26 | #include "pch.h" |
| 27 | |
| 28 | #include "nvs.h" |
| 29 | |
| 30 | /* We are using PCIe accesses for now |
| 31 | * 1. the chipset can do it |
| 32 | * 2. we don't need to worry about how we leave 0xcf8/0xcfc behind |
| 33 | */ |
| 34 | #include <northbridge/intel/fsp_sandybridge/northbridge.h> |
| 35 | |
| 36 | /* While we read PMBASE dynamically in case it changed, let's |
| 37 | * initialize it with a sane value |
| 38 | */ |
| 39 | static u16 pmbase = DEFAULT_PMBASE; |
| 40 | u16 smm_get_pmbase(void) |
| 41 | { |
| 42 | return pmbase; |
| 43 | } |
| 44 | |
| 45 | static u8 smm_initialized = 0; |
| 46 | |
| 47 | /* GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located |
| 48 | * by coreboot. |
| 49 | */ |
| 50 | static global_nvs_t *gnvs; |
| 51 | global_nvs_t *smm_get_gnvs(void) |
| 52 | { |
| 53 | return gnvs; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @brief read and clear PM1_STS |
| 58 | * @return PM1_STS register |
| 59 | */ |
| 60 | static u16 reset_pm1_status(void) |
| 61 | { |
| 62 | u16 reg16; |
| 63 | |
| 64 | reg16 = inw(pmbase + PM1_STS); |
| 65 | /* set status bits are cleared by writing 1 to them */ |
| 66 | outw(reg16, pmbase + PM1_STS); |
| 67 | |
| 68 | return reg16; |
| 69 | } |
| 70 | |
| 71 | static void dump_pm1_status(u16 pm1_sts) |
| 72 | { |
| 73 | printk(BIOS_SPEW, "PM1_STS: "); |
| 74 | if (pm1_sts & (1 << 15)) printk(BIOS_SPEW, "WAK "); |
| 75 | if (pm1_sts & (1 << 14)) printk(BIOS_SPEW, "PCIEXPWAK "); |
| 76 | if (pm1_sts & (1 << 11)) printk(BIOS_SPEW, "PRBTNOR "); |
| 77 | if (pm1_sts & (1 << 10)) printk(BIOS_SPEW, "RTC "); |
| 78 | if (pm1_sts & (1 << 8)) printk(BIOS_SPEW, "PWRBTN "); |
| 79 | if (pm1_sts & (1 << 5)) printk(BIOS_SPEW, "GBL "); |
| 80 | if (pm1_sts & (1 << 4)) printk(BIOS_SPEW, "BM "); |
| 81 | if (pm1_sts & (1 << 0)) printk(BIOS_SPEW, "TMROF "); |
| 82 | printk(BIOS_SPEW, "\n"); |
| 83 | int reg16 = inw(pmbase + PM1_EN); |
| 84 | printk(BIOS_SPEW, "PM1_EN: %x\n", reg16); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @brief read and clear SMI_STS |
| 89 | * @return SMI_STS register |
| 90 | */ |
| 91 | static u32 reset_smi_status(void) |
| 92 | { |
| 93 | u32 reg32; |
| 94 | |
| 95 | reg32 = inl(pmbase + SMI_STS); |
| 96 | /* set status bits are cleared by writing 1 to them */ |
| 97 | outl(reg32, pmbase + SMI_STS); |
| 98 | |
| 99 | return reg32; |
| 100 | } |
| 101 | |
| 102 | static void dump_smi_status(u32 smi_sts) |
| 103 | { |
| 104 | printk(BIOS_DEBUG, "SMI_STS: "); |
| 105 | if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI "); |
| 106 | if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR "); |
| 107 | if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI "); |
| 108 | if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 "); |
| 109 | if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 "); |
| 110 | if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI "); |
| 111 | if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI "); |
| 112 | if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC "); |
| 113 | if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO "); |
| 114 | if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON "); |
| 115 | if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI "); |
| 116 | if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI "); |
| 117 | if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 "); |
| 118 | if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 "); |
| 119 | if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR "); |
| 120 | if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM "); |
| 121 | if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI "); |
| 122 | if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB "); |
| 123 | if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS "); |
| 124 | printk(BIOS_DEBUG, "\n"); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * @brief read and clear GPE0_STS |
| 130 | * @return GPE0_STS register |
| 131 | */ |
| 132 | static u32 reset_gpe0_status(void) |
| 133 | { |
| 134 | u32 reg32; |
| 135 | |
| 136 | reg32 = inl(pmbase + GPE0_STS); |
| 137 | /* set status bits are cleared by writing 1 to them */ |
| 138 | outl(reg32, pmbase + GPE0_STS); |
| 139 | |
| 140 | return reg32; |
| 141 | } |
| 142 | |
| 143 | static void dump_gpe0_status(u32 gpe0_sts) |
| 144 | { |
| 145 | int i; |
| 146 | printk(BIOS_DEBUG, "GPE0_STS: "); |
| 147 | for (i=31; i >= 16; i--) { |
| 148 | if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16)); |
| 149 | } |
| 150 | if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 "); |
| 151 | if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 "); |
| 152 | if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 "); |
| 153 | if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME "); |
| 154 | if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "BATLOW "); |
| 155 | if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP "); |
| 156 | if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI "); |
| 157 | if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK "); |
| 158 | if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI "); |
| 159 | if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 "); |
| 160 | if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 "); |
| 161 | if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 "); |
| 162 | if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "SWGPE "); |
| 163 | if (gpe0_sts & (1 << 1)) printk(BIOS_DEBUG, "HOTPLUG "); |
| 164 | if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM "); |
| 165 | printk(BIOS_DEBUG, "\n"); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * @brief read and clear TCOx_STS |
| 171 | * @return TCOx_STS registers |
| 172 | */ |
| 173 | static u32 reset_tco_status(void) |
| 174 | { |
| 175 | u32 tcobase = pmbase + 0x60; |
| 176 | u32 reg32; |
| 177 | |
| 178 | reg32 = inl(tcobase + 0x04); |
| 179 | /* set status bits are cleared by writing 1 to them */ |
| 180 | outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS |
| 181 | if (reg32 & (1 << 18)) |
| 182 | outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS |
| 183 | |
| 184 | return reg32; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static void dump_tco_status(u32 tco_sts) |
| 189 | { |
| 190 | printk(BIOS_DEBUG, "TCO_STS: "); |
| 191 | if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV "); |
| 192 | if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT "); |
| 193 | if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO "); |
| 194 | if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET "); |
| 195 | if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR "); |
| 196 | if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI "); |
| 197 | if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI "); |
| 198 | if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR "); |
| 199 | if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY "); |
| 200 | if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT "); |
| 201 | if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT "); |
| 202 | if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO "); |
| 203 | if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI "); |
| 204 | printk(BIOS_DEBUG, "\n"); |
| 205 | } |
| 206 | |
| 207 | int southbridge_io_trap_handler(int smif) |
| 208 | { |
| 209 | switch (smif) { |
| 210 | case 0x32: |
| 211 | printk(BIOS_DEBUG, "OS Init\n"); |
| 212 | /* gnvs->smif: |
| 213 | * On success, the IO Trap Handler returns 0 |
| 214 | * On failure, the IO Trap Handler returns a value != 0 |
| 215 | */ |
| 216 | gnvs->smif = 0; |
| 217 | return 1; /* IO trap handled */ |
| 218 | } |
| 219 | |
| 220 | /* Not handled */ |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @brief Set the EOS bit |
| 226 | */ |
| 227 | void southbridge_smi_set_eos(void) |
| 228 | { |
| 229 | u8 reg8; |
| 230 | |
| 231 | reg8 = inb(pmbase + SMI_EN); |
| 232 | reg8 |= EOS; |
| 233 | outb(reg8, pmbase + SMI_EN); |
| 234 | } |
| 235 | |
| 236 | static void busmaster_disable_on_bus(int bus) |
| 237 | { |
| 238 | int slot, func; |
| 239 | unsigned int val; |
| 240 | unsigned char hdr; |
| 241 | |
| 242 | for (slot = 0; slot < 0x20; slot++) { |
| 243 | for (func = 0; func < 8; func++) { |
| 244 | u32 reg32; |
| 245 | device_t dev = PCI_DEV(bus, slot, func); |
| 246 | |
| 247 | val = pci_read_config32(dev, PCI_VENDOR_ID); |
| 248 | |
| 249 | if (val == 0xffffffff || val == 0x00000000 || |
| 250 | val == 0x0000ffff || val == 0xffff0000) |
| 251 | continue; |
| 252 | |
| 253 | /* Disable Bus Mastering for this one device */ |
| 254 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 255 | reg32 &= ~PCI_COMMAND_MASTER; |
| 256 | pci_write_config32(dev, PCI_COMMAND, reg32); |
| 257 | |
| 258 | /* If this is a bridge, then follow it. */ |
| 259 | hdr = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 260 | hdr &= 0x7f; |
| 261 | if (hdr == PCI_HEADER_TYPE_BRIDGE || |
| 262 | hdr == PCI_HEADER_TYPE_CARDBUS) { |
| 263 | unsigned int buses; |
| 264 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
| 265 | busmaster_disable_on_bus((buses >> 8) & 0xff); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Drive GPIO 60 low to gate memory reset in S3. |
| 273 | * |
| 274 | * Intel reference designs all use GPIO 60 but it is |
| 275 | * not a requirement and boards could use a different pin. |
| 276 | */ |
| 277 | static void southbridge_gate_memory_reset(void) |
| 278 | { |
| 279 | u32 reg32; |
| 280 | u16 gpiobase; |
| 281 | |
| 282 | gpiobase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), GPIOBASE) & 0xfffc; |
| 283 | if (!gpiobase) |
| 284 | return; |
| 285 | |
| 286 | /* Make sure it is set as GPIO */ |
| 287 | reg32 = inl(gpiobase + GPIO_USE_SEL2); |
| 288 | if (!(reg32 & (1 << 28))) { |
| 289 | reg32 |= (1 << 28); |
| 290 | outl(reg32, gpiobase + GPIO_USE_SEL2); |
| 291 | } |
| 292 | |
| 293 | /* Make sure it is set as output */ |
| 294 | reg32 = inl(gpiobase + GP_IO_SEL2); |
| 295 | if (reg32 & (1 << 28)) { |
| 296 | reg32 &= ~(1 << 28); |
| 297 | outl(reg32, gpiobase + GP_IO_SEL2); |
| 298 | } |
| 299 | |
| 300 | /* Drive the output low */ |
| 301 | reg32 = inl(gpiobase + GP_LVL2); |
| 302 | reg32 &= ~(1 << 28); |
| 303 | outl(reg32, gpiobase + GP_LVL2); |
| 304 | } |
| 305 | |
| 306 | static void southbridge_smi_sleep(void) |
| 307 | { |
| 308 | u8 reg8; |
| 309 | u32 reg32; |
| 310 | u8 slp_typ; |
| 311 | u8 s5pwr = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL; |
| 312 | |
| 313 | // save and recover RTC port values |
| 314 | u8 tmp70, tmp72; |
| 315 | tmp70 = inb(0x70); |
| 316 | tmp72 = inb(0x72); |
| 317 | get_option(&s5pwr, "power_on_after_fail"); |
| 318 | outb(tmp70, 0x70); |
| 319 | outb(tmp72, 0x72); |
| 320 | |
| 321 | /* First, disable further SMIs */ |
| 322 | reg8 = inb(pmbase + SMI_EN); |
| 323 | reg8 &= ~SLP_SMI_EN; |
| 324 | outb(reg8, pmbase + SMI_EN); |
| 325 | |
| 326 | /* Figure out SLP_TYP */ |
| 327 | reg32 = inl(pmbase + PM1_CNT); |
| 328 | printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32); |
| 329 | slp_typ = (reg32 >> 10) & 7; |
| 330 | |
| 331 | /* Do any mainboard sleep handling */ |
| 332 | mainboard_smi_sleep(slp_typ-2); |
| 333 | |
| 334 | #if CONFIG_ELOG_GSMI |
| 335 | /* Log S3, S4, and S5 entry */ |
| 336 | if (slp_typ >= 5) |
| 337 | elog_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ-2); |
| 338 | #endif |
| 339 | |
| 340 | /* Next, do the deed. |
| 341 | */ |
| 342 | |
| 343 | switch (slp_typ) { |
| 344 | case 0: printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n"); break; |
| 345 | case 1: printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n"); break; |
| 346 | case 5: |
| 347 | printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n"); |
| 348 | |
| 349 | /* Gate memory reset */ |
| 350 | southbridge_gate_memory_reset(); |
| 351 | |
| 352 | /* Invalidate the cache before going to S3 */ |
| 353 | wbinvd(); |
| 354 | break; |
| 355 | case 6: printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n"); break; |
| 356 | case 7: |
| 357 | printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n"); |
| 358 | |
| 359 | outl(0, pmbase + GPE0_EN); |
| 360 | |
| 361 | /* Always set the flag in case CMOS was changed on runtime. For |
| 362 | * "KEEP", switch to "OFF" - KEEP is software emulated |
| 363 | */ |
| 364 | reg8 = pcie_read_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3); |
| 365 | if (s5pwr == MAINBOARD_POWER_ON) { |
| 366 | reg8 &= ~1; |
| 367 | } else { |
| 368 | reg8 |= 1; |
| 369 | } |
| 370 | pcie_write_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3, reg8); |
| 371 | |
| 372 | /* also iterates over all bridges on bus 0 */ |
| 373 | busmaster_disable_on_bus(0); |
| 374 | break; |
| 375 | default: printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n"); break; |
| 376 | } |
| 377 | |
| 378 | /* Write back to the SLP register to cause the originally intended |
| 379 | * event again. We need to set BIT13 (SLP_EN) though to make the |
| 380 | * sleep happen. |
| 381 | */ |
| 382 | outl(reg32 | SLP_EN, pmbase + PM1_CNT); |
| 383 | |
| 384 | /* Make sure to stop executing code here for S3/S4/S5 */ |
| 385 | if (slp_typ > 1) |
| 386 | halt(); |
| 387 | |
| 388 | /* In most sleep states, the code flow of this function ends at |
| 389 | * the line above. However, if we entered sleep state S1 and wake |
| 390 | * up again, we will continue to execute code in this function. |
| 391 | */ |
| 392 | reg32 = inl(pmbase + PM1_CNT); |
| 393 | if (reg32 & SCI_EN) { |
| 394 | /* The OS is not an ACPI OS, so we set the state to S0 */ |
| 395 | reg32 &= ~(SLP_EN | SLP_TYP); |
| 396 | outl(reg32, pmbase + PM1_CNT); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Look for Synchronous IO SMI and use save state from that |
| 402 | * core in case we are not running on the same core that |
| 403 | * initiated the IO transaction. |
| 404 | */ |
| 405 | static em64t101_smm_state_save_area_t *smi_apmc_find_state_save(u8 cmd) |
| 406 | { |
| 407 | em64t101_smm_state_save_area_t *state; |
| 408 | int node; |
| 409 | |
| 410 | /* Check all nodes looking for the one that issued the IO */ |
| 411 | for (node = 0; node < CONFIG_MAX_CPUS; node++) { |
| 412 | state = smm_get_save_state(node); |
| 413 | |
| 414 | /* Check for Synchronous IO (bit0==1) */ |
| 415 | if (!(state->io_misc_info & (1 << 0))) |
| 416 | continue; |
| 417 | |
| 418 | /* Make sure it was a write (bit4==0) */ |
| 419 | if (state->io_misc_info & (1 << 4)) |
| 420 | continue; |
| 421 | |
| 422 | /* Check for APMC IO port */ |
| 423 | if (((state->io_misc_info >> 16) & 0xff) != APM_CNT) |
| 424 | continue; |
| 425 | |
| 426 | /* Check AX against the requested command */ |
| 427 | if ((state->rax & 0xff) != cmd) |
| 428 | continue; |
| 429 | |
| 430 | return state; |
| 431 | } |
| 432 | |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | #if CONFIG_ELOG_GSMI |
| 437 | static void southbridge_smi_gsmi(void) |
| 438 | { |
| 439 | u32 *ret, *param; |
| 440 | u8 sub_command; |
| 441 | em64t101_smm_state_save_area_t *io_smi = |
| 442 | smi_apmc_find_state_save(ELOG_GSMI_APM_CNT); |
| 443 | |
| 444 | if (!io_smi) |
| 445 | return; |
| 446 | |
| 447 | /* Command and return value in EAX */ |
| 448 | ret = (u32*)&io_smi->rax; |
| 449 | sub_command = (u8)(*ret >> 8); |
| 450 | |
| 451 | /* Parameter buffer in EBX */ |
| 452 | param = (u32*)&io_smi->rbx; |
| 453 | |
| 454 | /* drivers/elog/gsmi.c */ |
| 455 | *ret = gsmi_exec(sub_command, param); |
| 456 | } |
| 457 | #endif |
| 458 | |
| 459 | static void southbridge_smi_apmc(void) |
| 460 | { |
| 461 | u32 pmctrl; |
| 462 | u8 reg8; |
| 463 | em64t101_smm_state_save_area_t *state; |
| 464 | |
| 465 | /* Emulate B2 register as the FADT / Linux expects it */ |
| 466 | |
| 467 | reg8 = inb(APM_CNT); |
| 468 | switch (reg8) { |
| 469 | case APM_CNT_CST_CONTROL: |
| 470 | /* Calling this function seems to cause |
| 471 | * some kind of race condition in Linux |
| 472 | * and causes a kernel oops |
| 473 | */ |
| 474 | printk(BIOS_DEBUG, "C-state control\n"); |
| 475 | break; |
| 476 | case APM_CNT_PST_CONTROL: |
| 477 | /* Calling this function seems to cause |
| 478 | * some kind of race condition in Linux |
| 479 | * and causes a kernel oops |
| 480 | */ |
| 481 | printk(BIOS_DEBUG, "P-state control\n"); |
| 482 | break; |
| 483 | case APM_CNT_ACPI_DISABLE: |
| 484 | pmctrl = inl(pmbase + PM1_CNT); |
| 485 | pmctrl &= ~SCI_EN; |
| 486 | outl(pmctrl, pmbase + PM1_CNT); |
| 487 | printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n"); |
| 488 | break; |
| 489 | case APM_CNT_ACPI_ENABLE: |
| 490 | pmctrl = inl(pmbase + PM1_CNT); |
| 491 | pmctrl |= SCI_EN; |
| 492 | outl(pmctrl, pmbase + PM1_CNT); |
| 493 | printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n"); |
| 494 | break; |
| 495 | case APM_CNT_GNVS_UPDATE: |
| 496 | if (smm_initialized) { |
| 497 | printk(BIOS_DEBUG, "SMI#: SMM structures already initialized!\n"); |
| 498 | return; |
| 499 | } |
| 500 | state = smi_apmc_find_state_save(reg8); |
| 501 | if (state) { |
| 502 | /* EBX in the state save contains the GNVS pointer */ |
| 503 | gnvs = (global_nvs_t *)((u32)state->rbx); |
| 504 | smm_initialized = 1; |
| 505 | printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs); |
| 506 | } |
| 507 | break; |
| 508 | #if CONFIG_ELOG_GSMI |
| 509 | case ELOG_GSMI_APM_CNT: |
| 510 | southbridge_smi_gsmi(); |
| 511 | break; |
| 512 | #endif |
| 513 | } |
| 514 | |
| 515 | mainboard_smi_apmc(reg8); |
| 516 | } |
| 517 | |
| 518 | static void southbridge_smi_pm1(void) |
| 519 | { |
| 520 | u16 pm1_sts; |
| 521 | |
| 522 | pm1_sts = reset_pm1_status(); |
| 523 | dump_pm1_status(pm1_sts); |
| 524 | |
| 525 | /* While OSPM is not active, poweroff immediately |
| 526 | * on a power button event. |
| 527 | */ |
| 528 | if (pm1_sts & PWRBTN_STS) { |
| 529 | // power button pressed |
| 530 | u32 reg32; |
| 531 | reg32 = (7 << 10) | (1 << 13); |
| 532 | #if CONFIG_ELOG_GSMI |
| 533 | elog_add_event(ELOG_TYPE_POWER_BUTTON); |
| 534 | #endif |
| 535 | outl(reg32, pmbase + PM1_CNT); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static void southbridge_smi_gpe0(void) |
| 540 | { |
| 541 | u32 gpe0_sts; |
| 542 | |
| 543 | gpe0_sts = reset_gpe0_status(); |
| 544 | dump_gpe0_status(gpe0_sts); |
| 545 | } |
| 546 | |
| 547 | static void southbridge_smi_gpi(void) |
| 548 | { |
| 549 | u16 reg16; |
| 550 | reg16 = inw(pmbase + ALT_GP_SMI_STS); |
| 551 | outw(reg16, pmbase + ALT_GP_SMI_STS); |
| 552 | |
| 553 | reg16 &= inw(pmbase + ALT_GP_SMI_EN); |
| 554 | |
| 555 | mainboard_smi_gpi(reg16); |
| 556 | |
| 557 | if (reg16) |
| 558 | printk(BIOS_DEBUG, "GPI (mask %04x)\n",reg16); |
| 559 | |
| 560 | outw(reg16, pmbase + ALT_GP_SMI_STS); |
| 561 | } |
| 562 | |
| 563 | static void southbridge_smi_mc(void) |
| 564 | { |
| 565 | u32 reg32; |
| 566 | |
| 567 | reg32 = inl(pmbase + SMI_EN); |
| 568 | |
| 569 | /* Are periodic SMIs enabled? */ |
| 570 | if ((reg32 & MCSMI_EN) == 0) |
| 571 | return; |
| 572 | |
| 573 | printk(BIOS_DEBUG, "Microcontroller SMI.\n"); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | |
| 578 | static void southbridge_smi_tco(void) |
| 579 | { |
| 580 | u32 tco_sts; |
| 581 | |
| 582 | tco_sts = reset_tco_status(); |
| 583 | |
| 584 | /* Any TCO event? */ |
| 585 | if (!tco_sts) |
| 586 | return; |
| 587 | |
| 588 | if (tco_sts & (1 << 8)) { // BIOSWR |
| 589 | u8 bios_cntl; |
| 590 | |
| 591 | bios_cntl = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc); |
| 592 | |
| 593 | if (bios_cntl & 1) { |
| 594 | /* BWE is RW, so the SMI was caused by a |
| 595 | * write to BWE, not by a write to the BIOS |
| 596 | */ |
| 597 | |
| 598 | /* This is the place where we notice someone |
| 599 | * is trying to tinker with the BIOS. We are |
| 600 | * trying to be nice and just ignore it. A more |
| 601 | * resolute answer would be to power down the |
| 602 | * box. |
| 603 | */ |
| 604 | printk(BIOS_DEBUG, "Switching back to RO\n"); |
| 605 | pcie_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1)); |
| 606 | } /* No else for now? */ |
| 607 | } else if (tco_sts & (1 << 3)) { /* TIMEOUT */ |
| 608 | /* Handle TCO timeout */ |
| 609 | printk(BIOS_DEBUG, "TCO Timeout.\n"); |
| 610 | } else if (!tco_sts) { |
| 611 | dump_tco_status(tco_sts); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static void southbridge_smi_periodic(void) |
| 616 | { |
| 617 | u32 reg32; |
| 618 | |
| 619 | reg32 = inl(pmbase + SMI_EN); |
| 620 | |
| 621 | /* Are periodic SMIs enabled? */ |
| 622 | if ((reg32 & PERIODIC_EN) == 0) |
| 623 | return; |
| 624 | |
| 625 | printk(BIOS_DEBUG, "Periodic SMI.\n"); |
| 626 | } |
| 627 | |
| 628 | static void southbridge_smi_monitor(void) |
| 629 | { |
| 630 | #define IOTRAP(x) (trap_sts & (1 << x)) |
| 631 | u32 trap_sts, trap_cycle; |
| 632 | u32 data, mask = 0; |
| 633 | int i; |
| 634 | |
| 635 | trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register |
| 636 | RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR |
| 637 | |
| 638 | trap_cycle = RCBA32(0x1e10); |
| 639 | for (i=16; i<20; i++) { |
| 640 | if (trap_cycle & (1 << i)) |
| 641 | mask |= (0xff << ((i - 16) << 2)); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | /* IOTRAP(3) SMI function call */ |
| 646 | if (IOTRAP(3)) { |
| 647 | if (gnvs && gnvs->smif) |
| 648 | io_trap_handler(gnvs->smif); // call function smif |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | /* IOTRAP(2) currently unused |
| 653 | * IOTRAP(1) currently unused */ |
| 654 | |
| 655 | /* IOTRAP(0) SMIC */ |
| 656 | if (IOTRAP(0)) { |
| 657 | if (!(trap_cycle & (1 << 24))) { // It's a write |
| 658 | printk(BIOS_DEBUG, "SMI1 command\n"); |
| 659 | data = RCBA32(0x1e18); |
| 660 | data &= mask; |
| 661 | // if (smi1) |
| 662 | // southbridge_smi_command(data); |
| 663 | // return; |
| 664 | } |
| 665 | // Fall through to debug |
| 666 | } |
| 667 | |
| 668 | printk(BIOS_DEBUG, " trapped io address = 0x%x\n", trap_cycle & 0xfffc); |
| 669 | for (i=0; i < 4; i++) if(IOTRAP(i)) printk(BIOS_DEBUG, " TRAPĀ = %d\n", i); |
| 670 | printk(BIOS_DEBUG, " AHBE = %x\n", (trap_cycle >> 16) & 0xf); |
| 671 | printk(BIOS_DEBUG, " MASK = 0x%08x\n", mask); |
| 672 | printk(BIOS_DEBUG, " read/write: %s\n", (trap_cycle & (1 << 24)) ? "read" : "write"); |
| 673 | |
| 674 | if (!(trap_cycle & (1 << 24))) { |
| 675 | /* Write Cycle */ |
| 676 | data = RCBA32(0x1e18); |
| 677 | printk(BIOS_DEBUG, " iotrap written data = 0x%08x\n", data); |
| 678 | } |
| 679 | #undef IOTRAP |
| 680 | } |
| 681 | |
| 682 | typedef void (*smi_handler_t)(void); |
| 683 | |
| 684 | static smi_handler_t southbridge_smi[32] = { |
| 685 | NULL, // [0] reserved |
| 686 | NULL, // [1] reserved |
| 687 | NULL, // [2] BIOS_STS |
| 688 | NULL, // [3] LEGACY_USB_STS |
| 689 | southbridge_smi_sleep, // [4] SLP_SMI_STS |
| 690 | southbridge_smi_apmc, // [5] APM_STS |
| 691 | NULL, // [6] SWSMI_TMR_STS |
| 692 | NULL, // [7] reserved |
| 693 | southbridge_smi_pm1, // [8] PM1_STS |
| 694 | southbridge_smi_gpe0, // [9] GPE0_STS |
| 695 | southbridge_smi_gpi, // [10] GPI_STS |
| 696 | southbridge_smi_mc, // [11] MCSMI_STS |
| 697 | NULL, // [12] DEVMON_STS |
| 698 | southbridge_smi_tco, // [13] TCO_STS |
| 699 | southbridge_smi_periodic, // [14] PERIODIC_STS |
| 700 | NULL, // [15] SERIRQ_SMI_STS |
| 701 | NULL, // [16] SMBUS_SMI_STS |
| 702 | NULL, // [17] LEGACY_USB2_STS |
| 703 | NULL, // [18] INTEL_USB2_STS |
| 704 | NULL, // [19] reserved |
| 705 | NULL, // [20] PCI_EXP_SMI_STS |
| 706 | southbridge_smi_monitor, // [21] MONITOR_STS |
| 707 | NULL, // [22] reserved |
| 708 | NULL, // [23] reserved |
| 709 | NULL, // [24] reserved |
| 710 | NULL, // [25] EL_SMI_STS |
| 711 | NULL, // [26] SPI_STS |
| 712 | NULL, // [27] reserved |
| 713 | NULL, // [28] reserved |
| 714 | NULL, // [29] reserved |
| 715 | NULL, // [30] reserved |
| 716 | NULL // [31] reserved |
| 717 | }; |
| 718 | |
| 719 | /** |
| 720 | * @brief Interrupt handler for SMI# |
| 721 | * |
| 722 | * @param node |
| 723 | * @param state_save |
| 724 | */ |
| 725 | |
| 726 | void southbridge_smi_handler(void) |
| 727 | { |
| 728 | int i, dump = 0; |
| 729 | u32 smi_sts; |
| 730 | |
| 731 | /* Update global variable pmbase */ |
| 732 | pmbase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc; |
| 733 | |
| 734 | /* We need to clear the SMI status registers, or we won't see what's |
| 735 | * happening in the following calls. |
| 736 | */ |
| 737 | smi_sts = reset_smi_status(); |
| 738 | |
| 739 | /* Call SMI sub handler for each of the status bits */ |
| 740 | for (i = 0; i < 31; i++) { |
| 741 | if (smi_sts & (1 << i)) { |
| 742 | if (southbridge_smi[i]) { |
| 743 | southbridge_smi[i](); |
| 744 | } else { |
| 745 | printk(BIOS_DEBUG, "SMI_STS[%d] occured, but no " |
| 746 | "handler available.\n", i); |
| 747 | dump = 1; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if(dump) { |
| 753 | dump_smi_status(smi_sts); |
| 754 | } |
| 755 | |
| 756 | } |