Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 2 | |
| 3 | #include <types.h> |
| 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> |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <cpu/x86/cache.h> |
| 8 | #include <cpu/x86/smm.h> |
| 9 | #include <device/pci_def.h> |
| 10 | #include "i82801dx.h" |
| 11 | |
| 12 | #define DEBUG_SMI |
| 13 | |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 14 | /* I830M */ |
| 15 | #define SMRAM 0x90 |
| 16 | #define D_OPEN (1 << 6) |
| 17 | #define D_CLS (1 << 5) |
| 18 | #define D_LCK (1 << 4) |
| 19 | #define G_SMRANE (1 << 3) |
| 20 | #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0)) |
| 21 | |
stepan | 836ae29 | 2010-12-08 05:42:47 +0000 | [diff] [blame] | 22 | #include "nvs.h" |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 23 | |
| 24 | /* While we read PMBASE dynamically in case it changed, let's |
| 25 | * initialize it with a sane value |
| 26 | */ |
| 27 | u16 pmbase = PMBASE_ADDR; |
| 28 | u8 smm_initialized = 0; |
| 29 | |
| 30 | unsigned char *mbi = NULL; |
| 31 | u32 mbi_len; |
| 32 | u8 mbi_initialized = 0; |
| 33 | |
| 34 | /* GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located |
| 35 | * by coreboot. |
| 36 | */ |
| 37 | global_nvs_t *gnvs = (global_nvs_t *)0x0; |
| 38 | void *tcg = (void *)0x0; |
| 39 | void *smi1 = (void *)0x0; |
| 40 | |
| 41 | /** |
| 42 | * @brief read and clear PM1_STS |
| 43 | * @return PM1_STS register |
| 44 | */ |
| 45 | static u16 reset_pm1_status(void) |
| 46 | { |
| 47 | u16 reg16; |
| 48 | |
| 49 | reg16 = inw(pmbase + PM1_STS); |
| 50 | /* set status bits are cleared by writing 1 to them */ |
| 51 | outw(reg16, pmbase + PM1_STS); |
| 52 | |
| 53 | return reg16; |
| 54 | } |
| 55 | |
| 56 | static void dump_pm1_status(u16 pm1_sts) |
| 57 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 58 | printk(BIOS_SPEW, "PM1_STS: "); |
| 59 | if (pm1_sts & (1 << 15)) printk(BIOS_SPEW, "WAK "); |
| 60 | if (pm1_sts & (1 << 14)) printk(BIOS_SPEW, "PCIEXPWAK "); |
| 61 | if (pm1_sts & (1 << 11)) printk(BIOS_SPEW, "PRBTNOR "); |
| 62 | if (pm1_sts & (1 << 10)) printk(BIOS_SPEW, "RTC "); |
| 63 | if (pm1_sts & (1 << 8)) printk(BIOS_SPEW, "PWRBTN "); |
| 64 | if (pm1_sts & (1 << 5)) printk(BIOS_SPEW, "GBL "); |
| 65 | if (pm1_sts & (1 << 4)) printk(BIOS_SPEW, "BM "); |
| 66 | if (pm1_sts & (1 << 0)) printk(BIOS_SPEW, "TMROF "); |
| 67 | printk(BIOS_SPEW, "\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 68 | int reg16 = inw(pmbase + PM1_EN); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 69 | printk(BIOS_SPEW, "PM1_EN: %x\n", reg16); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief read and clear SMI_STS |
| 74 | * @return SMI_STS register |
| 75 | */ |
| 76 | static u32 reset_smi_status(void) |
| 77 | { |
| 78 | u32 reg32; |
| 79 | |
| 80 | reg32 = inl(pmbase + SMI_STS); |
| 81 | /* set status bits are cleared by writing 1 to them */ |
| 82 | outl(reg32, pmbase + SMI_STS); |
| 83 | |
| 84 | return reg32; |
| 85 | } |
| 86 | |
| 87 | static void dump_smi_status(u32 smi_sts) |
| 88 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 89 | printk(BIOS_DEBUG, "SMI_STS: "); |
| 90 | if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI "); |
| 91 | if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI "); |
| 92 | if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR "); |
| 93 | if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI "); |
| 94 | if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 "); |
| 95 | if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 "); |
| 96 | if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI "); |
| 97 | if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI "); |
| 98 | if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC "); |
| 99 | if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO "); |
| 100 | if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON "); |
| 101 | if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI "); |
| 102 | if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI "); |
| 103 | if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 "); |
| 104 | if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 "); |
| 105 | if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR "); |
| 106 | if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM "); |
| 107 | if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI "); |
| 108 | if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB "); |
| 109 | if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS "); |
| 110 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * @brief read and clear GPE0_STS |
| 116 | * @return GPE0_STS register |
| 117 | */ |
| 118 | static u32 reset_gpe0_status(void) |
| 119 | { |
| 120 | u32 reg32; |
| 121 | |
| 122 | reg32 = inl(pmbase + GPE0_STS); |
| 123 | /* set status bits are cleared by writing 1 to them */ |
| 124 | outl(reg32, pmbase + GPE0_STS); |
| 125 | |
| 126 | return reg32; |
| 127 | } |
| 128 | |
| 129 | static void dump_gpe0_status(u32 gpe0_sts) |
| 130 | { |
| 131 | int i; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 132 | printk(BIOS_DEBUG, "GPE0_STS: "); |
Konstantin Aladyshev | 62f8083 | 2013-03-07 04:04:27 +0400 | [diff] [blame] | 133 | for (i=31; i>= 16; i--) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 134 | if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16)); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 135 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 136 | if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 "); |
| 137 | if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 "); |
| 138 | if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 "); |
| 139 | if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME "); |
| 140 | if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW "); |
| 141 | if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP "); |
| 142 | if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI "); |
| 143 | if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK "); |
| 144 | if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI "); |
| 145 | if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 "); |
| 146 | if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 "); |
| 147 | if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 "); |
| 148 | if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "HOT_PLUG "); |
| 149 | if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM "); |
| 150 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * @brief read and clear TCOx_STS |
| 156 | * @return TCOx_STS registers |
| 157 | */ |
| 158 | static u32 reset_tco_status(void) |
| 159 | { |
| 160 | u32 tcobase = pmbase + 0x60; |
| 161 | u32 reg32; |
| 162 | |
| 163 | reg32 = inl(tcobase + 0x04); |
| 164 | /* set status bits are cleared by writing 1 to them */ |
| 165 | outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS |
| 166 | if (reg32 & (1 << 18)) |
| 167 | outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS |
| 168 | |
| 169 | return reg32; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static void dump_tco_status(u32 tco_sts) |
| 174 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 175 | printk(BIOS_DEBUG, "TCO_STS: "); |
| 176 | if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV "); |
| 177 | if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT "); |
| 178 | if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO "); |
| 179 | if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET "); |
| 180 | if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR "); |
| 181 | if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI "); |
| 182 | if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI "); |
| 183 | if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR "); |
| 184 | if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY "); |
| 185 | if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT "); |
| 186 | if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT "); |
| 187 | if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO "); |
| 188 | if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI "); |
| 189 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 192 | int southbridge_io_trap_handler(int smif) |
| 193 | { |
| 194 | switch (smif) { |
| 195 | case 0x32: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 196 | printk(BIOS_DEBUG, "OS Init\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 197 | /* gnvs->smif: |
| 198 | * On success, the IO Trap Handler returns 0 |
| 199 | * On failure, the IO Trap Handler returns a value != 0 |
| 200 | */ |
| 201 | gnvs->smif = 0; |
| 202 | return 1; /* IO trap handled */ |
| 203 | } |
| 204 | |
| 205 | /* Not handled */ |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @brief Set the EOS bit |
| 211 | */ |
| 212 | void southbridge_smi_set_eos(void) |
| 213 | { |
| 214 | u8 reg8; |
| 215 | |
| 216 | reg8 = inb(pmbase + SMI_EN); |
| 217 | reg8 |= EOS; |
| 218 | outb(reg8, pmbase + SMI_EN); |
| 219 | } |
| 220 | |
| 221 | static void busmaster_disable_on_bus(int bus) |
| 222 | { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 223 | int slot, func; |
| 224 | unsigned int val; |
| 225 | unsigned char hdr; |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 226 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 227 | for (slot = 0; slot < 0x20; slot++) { |
| 228 | for (func = 0; func < 8; func++) { |
Elyes HAOUAS | 2f2191a | 2020-04-28 19:59:30 +0200 | [diff] [blame^] | 229 | u16 reg16; |
Antonello Dettori | f9aac2f | 2016-09-03 10:45:33 +0200 | [diff] [blame] | 230 | pci_devfn_t dev = PCI_DEV(bus, slot, func); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 231 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 232 | val = pci_read_config32(dev, PCI_VENDOR_ID); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 233 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 234 | if (val == 0xffffffff || val == 0x00000000 || |
| 235 | val == 0x0000ffff || val == 0xffff0000) |
| 236 | continue; |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 237 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 238 | /* Disable Bus Mastering for this one device */ |
Elyes HAOUAS | 2f2191a | 2020-04-28 19:59:30 +0200 | [diff] [blame^] | 239 | reg16 = pci_read_config16(dev, PCI_COMMAND); |
| 240 | reg16 &= ~PCI_COMMAND_MASTER; |
| 241 | pci_write_config16(dev, PCI_COMMAND, reg16); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 242 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 243 | /* If this is a bridge, then follow it. */ |
| 244 | hdr = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 245 | hdr &= 0x7f; |
| 246 | if (hdr == PCI_HEADER_TYPE_BRIDGE || |
| 247 | hdr == PCI_HEADER_TYPE_CARDBUS) { |
| 248 | unsigned int buses; |
| 249 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
| 250 | busmaster_disable_on_bus((buses >> 8) & 0xff); |
| 251 | } |
| 252 | } |
| 253 | } |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 257 | static void southbridge_smi_sleep(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 258 | { |
| 259 | u8 reg8; |
| 260 | u32 reg32; |
| 261 | u8 slp_typ; |
| 262 | /* FIXME: the power state on boot should be read from |
| 263 | * CMOS or even better from GNVS. Right now it's hard |
| 264 | * coded at compile time. |
| 265 | */ |
Nico Huber | 9faae2b | 2018-11-14 00:00:35 +0100 | [diff] [blame] | 266 | u8 s5pwr = CONFIG_MAINBOARD_POWER_FAILURE_STATE; |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 267 | |
| 268 | /* First, disable further SMIs */ |
| 269 | reg8 = inb(pmbase + SMI_EN); |
| 270 | reg8 &= ~SLP_SMI_EN; |
| 271 | outb(reg8, pmbase + SMI_EN); |
| 272 | |
| 273 | /* Figure out SLP_TYP */ |
| 274 | reg32 = inl(pmbase + PM1_CNT); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 275 | printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32); |
Aaron Durbin | 671909b | 2016-07-13 23:24:36 -0500 | [diff] [blame] | 276 | slp_typ = acpi_sleep_from_pm1(reg32); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 277 | |
| 278 | /* Next, do the deed. |
| 279 | */ |
| 280 | |
| 281 | switch (slp_typ) { |
Aaron Durbin | 671909b | 2016-07-13 23:24:36 -0500 | [diff] [blame] | 282 | case ACPI_S0: printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n"); break; |
| 283 | case ACPI_S1: printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n"); break; |
| 284 | case ACPI_S3: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 285 | printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 286 | /* Invalidate the cache before going to S3 */ |
| 287 | wbinvd(); |
| 288 | break; |
Aaron Durbin | 671909b | 2016-07-13 23:24:36 -0500 | [diff] [blame] | 289 | case ACPI_S4: printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n"); break; |
| 290 | case ACPI_S5: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 291 | printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 292 | |
| 293 | outl(0, pmbase + GPE0_EN); |
| 294 | |
| 295 | /* Should we keep the power state after a power loss? |
| 296 | * In case the setting is "ON" or "OFF" we don't have |
| 297 | * to do anything. But if it's "KEEP" we have to switch |
| 298 | * to "OFF" before entering S5. |
| 299 | */ |
| 300 | if (s5pwr == MAINBOARD_POWER_KEEP) { |
| 301 | reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3); |
| 302 | reg8 |= 1; |
| 303 | pci_write_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3, reg8); |
| 304 | } |
| 305 | |
| 306 | /* also iterates over all bridges on bus 0 */ |
| 307 | busmaster_disable_on_bus(0); |
| 308 | break; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 309 | default: printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n"); break; |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* Write back to the SLP register to cause the originally intended |
| 313 | * event again. We need to set BIT13 (SLP_EN) though to make the |
| 314 | * sleep happen. |
| 315 | */ |
| 316 | outl(reg32 | SLP_EN, pmbase + PM1_CNT); |
| 317 | |
| 318 | /* In most sleep states, the code flow of this function ends at |
| 319 | * the line above. However, if we entered sleep state S1 and wake |
| 320 | * up again, we will continue to execute code in this function. |
| 321 | */ |
| 322 | reg32 = inl(pmbase + PM1_CNT); |
| 323 | if (reg32 & SCI_EN) { |
| 324 | /* The OS is not an ACPI OS, so we set the state to S0 */ |
| 325 | reg32 &= ~(SLP_EN | SLP_TYP); |
| 326 | outl(reg32, pmbase + PM1_CNT); |
| 327 | } |
| 328 | } |
| 329 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 330 | static void southbridge_smi_apmc(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 331 | { |
| 332 | u32 pmctrl; |
| 333 | u8 reg8; |
| 334 | |
| 335 | /* Emulate B2 register as the FADT / Linux expects it */ |
| 336 | |
| 337 | reg8 = inb(APM_CNT); |
| 338 | switch (reg8) { |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 339 | case APM_CNT_CST_CONTROL: |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 340 | /* Calling this function seems to cause |
| 341 | * some kind of race condition in Linux |
| 342 | * and causes a kernel oops |
| 343 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 344 | printk(BIOS_DEBUG, "C-state control\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 345 | break; |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 346 | case APM_CNT_PST_CONTROL: |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 347 | /* Calling this function seems to cause |
| 348 | * some kind of race condition in Linux |
| 349 | * and causes a kernel oops |
| 350 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 351 | printk(BIOS_DEBUG, "P-state control\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 352 | break; |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 353 | case APM_CNT_ACPI_DISABLE: |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 354 | pmctrl = inl(pmbase + PM1_CNT); |
| 355 | pmctrl &= ~SCI_EN; |
| 356 | outl(pmctrl, pmbase + PM1_CNT); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 357 | printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 358 | break; |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 359 | case APM_CNT_ACPI_ENABLE: |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 360 | pmctrl = inl(pmbase + PM1_CNT); |
| 361 | pmctrl |= SCI_EN; |
| 362 | outl(pmctrl, pmbase + PM1_CNT); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 363 | printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 364 | break; |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 365 | case APM_CNT_GNVS_UPDATE: |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 366 | if (smm_initialized) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 367 | printk(BIOS_DEBUG, "SMI#: SMM structures already initialized!\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | gnvs = *(global_nvs_t **)0x500; |
| 371 | tcg = *(void **)0x504; |
| 372 | smi1 = *(void **)0x508; |
| 373 | smm_initialized = 1; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 374 | printk(BIOS_DEBUG, "SMI#: Setting up structures to %p, %p, %p\n", gnvs, tcg, smi1); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 375 | break; |
Sven Schnelle | f4dc1a7 | 2011-06-05 11:33:41 +0200 | [diff] [blame] | 376 | case APM_CNT_MBI_UPDATE: // FIXME |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 377 | if (mbi_initialized) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 378 | printk(BIOS_DEBUG, "SMI#: mbi already registered!\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | mbi = *(void **)0x500; |
| 382 | mbi_len = *(u32 *)0x504; |
| 383 | mbi_initialized = 1; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 384 | printk(BIOS_DEBUG, "SMI#: Registered MBI at %p (%d bytes)\n", mbi, mbi_len); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 385 | break; |
| 386 | |
| 387 | default: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 388 | printk(BIOS_DEBUG, "SMI#: Unknown function APM_CNT=%02x\n", reg8); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 392 | static void southbridge_smi_pm1(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 393 | { |
| 394 | u16 pm1_sts; |
| 395 | |
| 396 | pm1_sts = reset_pm1_status(); |
| 397 | dump_pm1_status(pm1_sts); |
| 398 | |
| 399 | /* While OSPM is not active, poweroff immediately |
| 400 | * on a power button event. |
| 401 | */ |
| 402 | if (pm1_sts & PWRBTN_STS) { |
| 403 | // power button pressed |
| 404 | u32 reg32; |
| 405 | reg32 = (7 << 10) | (1 << 13); |
| 406 | outl(reg32, pmbase + PM1_CNT); |
| 407 | } |
| 408 | } |
| 409 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 410 | static void southbridge_smi_gpe0(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 411 | { |
| 412 | u32 gpe0_sts; |
| 413 | |
| 414 | gpe0_sts = reset_gpe0_status(); |
| 415 | dump_gpe0_status(gpe0_sts); |
| 416 | } |
| 417 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 418 | static void southbridge_smi_gpi(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 419 | { |
| 420 | u16 reg16; |
| 421 | reg16 = inw(pmbase + ALT_GP_SMI_STS); |
| 422 | outl(reg16, pmbase + ALT_GP_SMI_STS); |
| 423 | |
| 424 | reg16 &= inw(pmbase + ALT_GP_SMI_EN); |
| 425 | |
Kyösti Mälkki | 48b3dbc | 2014-12-29 19:36:50 +0200 | [diff] [blame] | 426 | mainboard_smi_gpi(reg16); |
| 427 | |
| 428 | if (reg16) |
| 429 | printk(BIOS_DEBUG, "GPI (mask %04x)\n",reg16); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 432 | static void southbridge_smi_mc(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 433 | { |
| 434 | u32 reg32; |
| 435 | |
| 436 | reg32 = inl(pmbase + SMI_EN); |
| 437 | |
| 438 | /* Are periodic SMIs enabled? */ |
| 439 | if ((reg32 & MCSMI_EN) == 0) |
| 440 | return; |
| 441 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 442 | printk(BIOS_DEBUG, "Microcontroller SMI.\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | |
| 446 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 447 | static void southbridge_smi_tco(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 448 | { |
| 449 | u32 tco_sts; |
| 450 | |
| 451 | tco_sts = reset_tco_status(); |
| 452 | |
| 453 | /* Any TCO event? */ |
| 454 | if (!tco_sts) |
| 455 | return; |
| 456 | |
| 457 | if (tco_sts & (1 << 8)) { // BIOSWR |
| 458 | u8 bios_cntl; |
| 459 | |
| 460 | bios_cntl = pci_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc); |
| 461 | |
| 462 | if (bios_cntl & 1) { |
| 463 | /* BWE is RW, so the SMI was caused by a |
| 464 | * write to BWE, not by a write to the BIOS |
| 465 | */ |
| 466 | |
| 467 | /* This is the place where we notice someone |
| 468 | * is trying to tinker with the BIOS. We are |
| 469 | * trying to be nice and just ignore it. A more |
| 470 | * resolute answer would be to power down the |
| 471 | * box. |
| 472 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 473 | printk(BIOS_DEBUG, "Switching back to RO\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 474 | pci_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1)); |
| 475 | } /* No else for now? */ |
| 476 | } else if (tco_sts & (1 << 3)) { /* TIMEOUT */ |
| 477 | /* Handle TCO timeout */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 478 | printk(BIOS_DEBUG, "TCO Timeout.\n"); |
Jacob Garber | 7eb8eed | 2019-04-03 09:18:32 -0600 | [diff] [blame] | 479 | } else { |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 480 | dump_tco_status(tco_sts); |
| 481 | } |
| 482 | } |
| 483 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 484 | static void southbridge_smi_periodic(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 485 | { |
| 486 | u32 reg32; |
| 487 | |
| 488 | reg32 = inl(pmbase + SMI_EN); |
| 489 | |
| 490 | /* Are periodic SMIs enabled? */ |
| 491 | if ((reg32 & PERIODIC_EN) == 0) |
| 492 | return; |
| 493 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 494 | printk(BIOS_DEBUG, "Periodic SMI.\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 497 | static void southbridge_smi_monitor(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 498 | { |
| 499 | #define IOTRAP(x) (trap_sts & (1 << x)) |
| 500 | #if 0 |
| 501 | u32 trap_sts, trap_cycle; |
| 502 | u32 data, mask = 0; |
| 503 | int i; |
| 504 | |
| 505 | trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register |
| 506 | RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR |
| 507 | |
| 508 | trap_cycle = RCBA32(0x1e10); |
| 509 | for (i=16; i<20; i++) { |
| 510 | if (trap_cycle & (1 << i)) |
| 511 | mask |= (0xff << ((i - 16) << 2)); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /* IOTRAP(3) SMI function call */ |
| 516 | if (IOTRAP(3)) { |
| 517 | if (gnvs && gnvs->smif) |
| 518 | io_trap_handler(gnvs->smif); // call function smif |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* IOTRAP(2) currently unused |
| 523 | * IOTRAP(1) currently unused */ |
| 524 | |
| 525 | /* IOTRAP(0) SMIC */ |
| 526 | if (IOTRAP(0)) { |
| 527 | if (!(trap_cycle & (1 << 24))) { // It's a write |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 528 | printk(BIOS_DEBUG, "SMI1 command\n"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 529 | data = RCBA32(0x1e18); |
| 530 | data &= mask; |
| 531 | // if (smi1) |
Elyes HAOUAS | b0f1988 | 2018-06-09 11:59:00 +0200 | [diff] [blame] | 532 | // southbridge_smi_command(data); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 533 | // return; |
| 534 | } |
| 535 | // Fall through to debug |
| 536 | } |
| 537 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 538 | printk(BIOS_DEBUG, " trapped io address = 0x%x\n", trap_cycle & 0xfffc); |
Elyes HAOUAS | 70d79a4 | 2016-08-21 18:36:06 +0200 | [diff] [blame] | 539 | for (i=0; i < 4; i++) if (IOTRAP(i)) printk(BIOS_DEBUG, " TRAP = %d\n", i); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 540 | printk(BIOS_DEBUG, " AHBE = %x\n", (trap_cycle >> 16) & 0xf); |
| 541 | printk(BIOS_DEBUG, " MASK = 0x%08x\n", mask); |
| 542 | printk(BIOS_DEBUG, " read/write: %s\n", (trap_cycle & (1 << 24)) ? "read" : "write"); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 543 | |
| 544 | if (!(trap_cycle & (1 << 24))) { |
| 545 | /* Write Cycle */ |
| 546 | data = RCBA32(0x1e18); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 547 | printk(BIOS_DEBUG, " iotrap written data = 0x%08x\n", data); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 548 | } |
| 549 | #endif |
| 550 | #undef IOTRAP |
| 551 | } |
| 552 | |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 553 | typedef void (*smi_handler_t)(void); |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 554 | |
Stefan Reinauer | 348a1ba | 2010-03-17 01:51:11 +0000 | [diff] [blame] | 555 | smi_handler_t southbridge_smi[32] = { |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 556 | NULL, // [0] reserved |
| 557 | NULL, // [1] reserved |
| 558 | NULL, // [2] BIOS_STS |
| 559 | NULL, // [3] LEGACY_USB_STS |
| 560 | southbridge_smi_sleep, // [4] SLP_SMI_STS |
| 561 | southbridge_smi_apmc, // [5] APM_STS |
| 562 | NULL, // [6] SWSMI_TMR_STS |
| 563 | NULL, // [7] reserved |
| 564 | southbridge_smi_pm1, // [8] PM1_STS |
| 565 | southbridge_smi_gpe0, // [9] GPE0_STS |
| 566 | southbridge_smi_gpi, // [10] GPI_STS |
| 567 | southbridge_smi_mc, // [11] MCSMI_STS |
| 568 | NULL, // [12] DEVMON_STS |
| 569 | southbridge_smi_tco, // [13] TCO_STS |
| 570 | southbridge_smi_periodic, // [14] PERIODIC_STS |
| 571 | NULL, // [15] SERIRQ_SMI_STS |
| 572 | NULL, // [16] SMBUS_SMI_STS |
| 573 | NULL, // [17] LEGACY_USB2_STS |
| 574 | NULL, // [18] INTEL_USB2_STS |
| 575 | NULL, // [19] reserved |
| 576 | NULL, // [20] PCI_EXP_SMI_STS |
| 577 | southbridge_smi_monitor, // [21] MONITOR_STS |
| 578 | NULL, // [22] reserved |
| 579 | NULL, // [23] reserved |
| 580 | NULL, // [24] reserved |
| 581 | NULL, // [25] EL_SMI_STS |
| 582 | NULL, // [26] SPI_STS |
| 583 | NULL, // [27] reserved |
| 584 | NULL, // [28] reserved |
| 585 | NULL, // [29] reserved |
| 586 | NULL, // [30] reserved |
| 587 | NULL // [31] reserved |
| 588 | }; |
| 589 | |
| 590 | /** |
| 591 | * @brief Interrupt handler for SMI# |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 592 | */ |
Kyösti Mälkki | 1ef039b | 2019-08-10 15:32:03 +0300 | [diff] [blame] | 593 | void southbridge_smi_handler(void) |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 594 | { |
| 595 | int i, dump = 0; |
| 596 | u32 smi_sts; |
| 597 | |
| 598 | /* Update global variable pmbase */ |
| 599 | pmbase = pci_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc; |
| 600 | |
| 601 | /* We need to clear the SMI status registers, or we won't see what's |
| 602 | * happening in the following calls. |
| 603 | */ |
| 604 | smi_sts = reset_smi_status(); |
| 605 | |
| 606 | /* Filter all non-enabled SMI events */ |
| 607 | // FIXME Double check, this clears MONITOR |
| 608 | // smi_sts &= inl(pmbase + SMI_EN); |
| 609 | |
| 610 | /* Call SMI sub handler for each of the status bits */ |
| 611 | for (i = 0; i < 31; i++) { |
| 612 | if (smi_sts & (1 << i)) { |
Kyösti Mälkki | b987282 | 2019-08-10 15:47:29 +0300 | [diff] [blame] | 613 | if (southbridge_smi[i]) { |
| 614 | southbridge_smi[i](); |
| 615 | } else { |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 616 | printk(BIOS_DEBUG, "SMI_STS[%d] occurred, but no " |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 617 | "handler available.\n", i); |
| 618 | dump = 1; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 623 | if (dump) { |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 624 | dump_smi_status(smi_sts); |
| 625 | } |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 626 | } |