Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 18 | #include <arch/io.h> |
| 19 | #include <console/console.h> |
| 20 | #include <cpu/x86/cache.h> |
| 21 | #include <cpu/x86/smm.h> |
| 22 | #include <device/pci_def.h> |
| 23 | #include <elog.h> |
Patrick Georgi | 546953c | 2014-11-29 10:38:17 +0100 | [diff] [blame] | 24 | #include <halt.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 25 | |
Ben Gardner | fa6014a | 2015-12-08 21:20:25 -0600 | [diff] [blame] | 26 | #include <soc/pci_devs.h> |
| 27 | #include <soc/pmc.h> |
| 28 | #include <soc/nvs.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 29 | |
| 30 | /* GNVS needs to be set by coreboot initiating a software SMI. */ |
| 31 | static global_nvs_t *gnvs; |
| 32 | static int smm_initialized; |
| 33 | |
| 34 | int southbridge_io_trap_handler(int smif) |
| 35 | { |
| 36 | switch (smif) { |
| 37 | case 0x32: |
| 38 | printk(BIOS_DEBUG, "OS Init\n"); |
| 39 | /* gnvs->smif: |
| 40 | * On success, the IO Trap Handler returns 0 |
| 41 | * On failure, the IO Trap Handler returns a value != 0 |
| 42 | */ |
| 43 | gnvs->smif = 0; |
| 44 | return 1; /* IO trap handled */ |
| 45 | } |
| 46 | |
| 47 | /* Not handled */ |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | void southbridge_smi_set_eos(void) |
| 52 | { |
| 53 | enable_smi(EOS); |
| 54 | } |
| 55 | |
| 56 | global_nvs_t *smm_get_gnvs(void) |
| 57 | { |
| 58 | return gnvs; |
| 59 | } |
| 60 | |
| 61 | static void busmaster_disable_on_bus(int bus) |
| 62 | { |
| 63 | int slot, func; |
| 64 | unsigned int val; |
| 65 | unsigned char hdr; |
| 66 | |
| 67 | for (slot = 0; slot < 0x20; slot++) { |
| 68 | for (func = 0; func < 8; func++) { |
| 69 | u32 reg32; |
| 70 | device_t dev = PCI_DEV(bus, slot, func); |
| 71 | |
| 72 | val = pci_read_config32(dev, PCI_VENDOR_ID); |
| 73 | |
| 74 | if (val == 0xffffffff || val == 0x00000000 || |
| 75 | val == 0x0000ffff || val == 0xffff0000) |
| 76 | continue; |
| 77 | |
| 78 | /* Disable Bus Mastering for this one device */ |
| 79 | reg32 = pci_read_config32(dev, PCI_COMMAND); |
| 80 | reg32 &= ~PCI_COMMAND_MASTER; |
| 81 | pci_write_config32(dev, PCI_COMMAND, reg32); |
| 82 | |
| 83 | /* If this is a bridge, then follow it. */ |
| 84 | hdr = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 85 | hdr &= 0x7f; |
| 86 | if (hdr == PCI_HEADER_TYPE_BRIDGE || |
| 87 | hdr == PCI_HEADER_TYPE_CARDBUS) { |
| 88 | unsigned int buses; |
| 89 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
| 90 | busmaster_disable_on_bus((buses >> 8) & 0xff); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void southbridge_smi_sleep(void) |
| 97 | { |
| 98 | uint32_t reg32; |
| 99 | uint8_t slp_typ; |
| 100 | uint16_t pmbase = get_pmbase(); |
| 101 | |
| 102 | /* First, disable further SMIs */ |
| 103 | disable_smi(SLP_SMI_EN); |
| 104 | |
| 105 | /* Figure out SLP_TYP */ |
| 106 | reg32 = inl(pmbase + PM1_CNT); |
| 107 | printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32); |
| 108 | slp_typ = (reg32 >> 10) & 7; |
| 109 | |
| 110 | /* Do any mainboard sleep handling */ |
| 111 | mainboard_smi_sleep(slp_typ-2); |
| 112 | |
| 113 | #if IS_ENABLED(CONFIG_ELOG_GSMI) |
| 114 | /* Log S3, S4, and S5 entry */ |
| 115 | if (slp_typ >= 5) |
| 116 | elog_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ-2); |
| 117 | #endif |
| 118 | |
| 119 | /* Next, do the deed. |
| 120 | */ |
| 121 | |
| 122 | switch (slp_typ) { |
| 123 | case SLP_TYP_S0: |
| 124 | printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n"); |
| 125 | break; |
| 126 | case SLP_TYP_S1: |
| 127 | printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n"); |
| 128 | break; |
| 129 | case SLP_TYP_S3: |
| 130 | printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n"); |
| 131 | |
| 132 | /* Invalidate the cache before going to S3 */ |
| 133 | wbinvd(); |
| 134 | break; |
| 135 | case SLP_TYP_S4: |
| 136 | printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n"); |
| 137 | break; |
| 138 | case SLP_TYP_S5: |
| 139 | printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n"); |
| 140 | |
| 141 | /* Disable all GPE */ |
| 142 | disable_all_gpe(); |
| 143 | |
| 144 | /* also iterates over all bridges on bus 0 */ |
| 145 | busmaster_disable_on_bus(0); |
| 146 | break; |
| 147 | default: |
| 148 | printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n"); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | /* Write back to the SLP register to cause the originally intended |
| 153 | * event again. We need to set BIT13 (SLP_EN) though to make the |
| 154 | * sleep happen. |
| 155 | */ |
| 156 | enable_pm1_control(SLP_EN); |
| 157 | |
| 158 | /* Make sure to stop executing code here for S3/S4/S5 */ |
| 159 | if (slp_typ > 1) |
Patrick Georgi | 546953c | 2014-11-29 10:38:17 +0100 | [diff] [blame] | 160 | halt(); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 161 | |
| 162 | /* In most sleep states, the code flow of this function ends at |
| 163 | * the line above. However, if we entered sleep state S1 and wake |
| 164 | * up again, we will continue to execute code in this function. |
| 165 | */ |
| 166 | reg32 = inl(pmbase + PM1_CNT); |
| 167 | if (reg32 & SCI_EN) { |
| 168 | /* The OS is not an ACPI OS, so we set the state to S0 */ |
| 169 | disable_pm1_control(SLP_EN | SLP_TYP); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Look for Synchronous IO SMI and use save state from that |
| 175 | * core in case we are not running on the same core that |
| 176 | * initiated the IO transaction. |
| 177 | */ |
| 178 | static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd) |
| 179 | { |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 180 | em64t100_smm_state_save_area_t *state; |
| 181 | int node; |
| 182 | |
| 183 | /* Check all nodes looking for the one that issued the IO */ |
| 184 | for (node = 0; node < CONFIG_MAX_CPUS; node++) { |
| 185 | state = smm_get_save_state(node); |
| 186 | |
| 187 | /* Check for Synchronous IO (bit0==1) */ |
| 188 | if (!(state->io_misc_info & (1 << 0))) |
| 189 | continue; |
| 190 | |
| 191 | /* Make sure it was a write (bit4==0) */ |
| 192 | if (state->io_misc_info & (1 << 4)) |
| 193 | continue; |
| 194 | |
| 195 | /* Check for APMC IO port */ |
| 196 | if (((state->io_misc_info >> 16) & 0xff) != APM_CNT) |
| 197 | continue; |
| 198 | |
| 199 | /* Check AX against the requested command */ |
| 200 | if ((state->rax & 0xff) != cmd) |
| 201 | continue; |
| 202 | |
| 203 | return state; |
| 204 | } |
| 205 | |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | #if IS_ENABLED(CONFIG_ELOG_GSMI) |
| 210 | static void southbridge_smi_gsmi(void) |
| 211 | { |
| 212 | u32 *ret, *param; |
| 213 | uint8_t sub_command; |
| 214 | em64t100_smm_state_save_area_t *io_smi = |
| 215 | smi_apmc_find_state_save(ELOG_GSMI_APM_CNT); |
| 216 | |
| 217 | if (!io_smi) |
| 218 | return; |
| 219 | |
| 220 | /* Command and return value in EAX */ |
| 221 | ret = (u32*)&io_smi->rax; |
| 222 | sub_command = (uint8_t)(*ret >> 8); |
| 223 | |
| 224 | /* Parameter buffer in EBX */ |
| 225 | param = (u32*)&io_smi->rbx; |
| 226 | |
| 227 | /* drivers/elog/gsmi.c */ |
| 228 | *ret = gsmi_exec(sub_command, param); |
| 229 | } |
| 230 | #endif |
| 231 | static void southbridge_smi_apmc(void) |
| 232 | { |
| 233 | uint8_t reg8; |
| 234 | em64t100_smm_state_save_area_t *state; |
| 235 | |
| 236 | /* Emulate B2 register as the FADT / Linux expects it */ |
| 237 | |
| 238 | reg8 = inb(APM_CNT); |
| 239 | switch (reg8) { |
| 240 | case APM_CNT_CST_CONTROL: |
| 241 | /* Calling this function seems to cause |
| 242 | * some kind of race condition in Linux |
| 243 | * and causes a kernel oops |
| 244 | */ |
| 245 | printk(BIOS_DEBUG, "C-state control\n"); |
| 246 | break; |
| 247 | case APM_CNT_PST_CONTROL: |
| 248 | /* Calling this function seems to cause |
| 249 | * some kind of race condition in Linux |
| 250 | * and causes a kernel oops |
| 251 | */ |
| 252 | printk(BIOS_DEBUG, "P-state control\n"); |
| 253 | break; |
| 254 | case APM_CNT_ACPI_DISABLE: |
| 255 | disable_pm1_control(SCI_EN); |
| 256 | printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n"); |
| 257 | break; |
| 258 | case APM_CNT_ACPI_ENABLE: |
| 259 | enable_pm1_control(SCI_EN); |
| 260 | printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n"); |
| 261 | break; |
| 262 | case APM_CNT_GNVS_UPDATE: |
| 263 | if (smm_initialized) { |
| 264 | printk(BIOS_DEBUG, |
| 265 | "SMI#: SMM structures already initialized!\n"); |
| 266 | return; |
| 267 | } |
| 268 | state = smi_apmc_find_state_save(reg8); |
| 269 | if (state) { |
| 270 | /* EBX in the state save contains the GNVS pointer */ |
| 271 | gnvs = (global_nvs_t *)((uint32_t)state->rbx); |
| 272 | smm_initialized = 1; |
| 273 | printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs); |
| 274 | } |
| 275 | break; |
| 276 | #if IS_ENABLED(CONFIG_ELOG_GSMI) |
| 277 | case ELOG_GSMI_APM_CNT: |
| 278 | southbridge_smi_gsmi(); |
| 279 | break; |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | mainboard_smi_apmc(reg8); |
| 284 | } |
| 285 | |
| 286 | static void southbridge_smi_pm1(void) |
| 287 | { |
| 288 | uint16_t pm1_sts = clear_pm1_status(); |
| 289 | |
| 290 | /* While OSPM is not active, poweroff immediately |
| 291 | * on a power button event. |
| 292 | */ |
| 293 | if (pm1_sts & PWRBTN_STS) { |
| 294 | // power button pressed |
| 295 | #if IS_ENABLED(CONFIG_ELOG_GSMI) |
| 296 | elog_add_event(ELOG_TYPE_POWER_BUTTON); |
| 297 | #endif |
| 298 | disable_pm1_control(-1UL); |
| 299 | enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static void southbridge_smi_gpe0(void) |
| 304 | { |
| 305 | clear_gpe_status(); |
| 306 | } |
| 307 | |
| 308 | static void southbridge_smi_tco(void) |
| 309 | { |
| 310 | uint32_t tco_sts = clear_tco_status(); |
| 311 | |
| 312 | /* Any TCO event? */ |
| 313 | if (!tco_sts) |
| 314 | return; |
| 315 | |
| 316 | if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */ |
| 317 | /* Handle TCO timeout */ |
| 318 | printk(BIOS_DEBUG, "TCO Timeout.\n"); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | static void southbridge_smi_periodic(void) |
| 323 | { |
| 324 | uint32_t reg32; |
| 325 | |
| 326 | reg32 = inl(get_pmbase() + SMI_EN); |
| 327 | |
| 328 | /* Are periodic SMIs enabled? */ |
| 329 | if ((reg32 & PERIODIC_EN) == 0) |
| 330 | return; |
| 331 | |
| 332 | printk(BIOS_DEBUG, "Periodic SMI.\n"); |
| 333 | } |
| 334 | |
| 335 | typedef void (*smi_handler_t)(void); |
| 336 | |
| 337 | static const smi_handler_t southbridge_smi[32] = { |
| 338 | NULL, // [0] reserved |
| 339 | NULL, // [1] reserved |
| 340 | NULL, // [2] BIOS_STS |
| 341 | NULL, // [3] LEGACY_USB_STS |
| 342 | southbridge_smi_sleep, // [4] SLP_SMI_STS |
| 343 | southbridge_smi_apmc, // [5] APM_STS |
| 344 | NULL, // [6] SWSMI_TMR_STS |
| 345 | NULL, // [7] reserved |
| 346 | southbridge_smi_pm1, // [8] PM1_STS |
| 347 | southbridge_smi_gpe0, // [9] GPE0_STS |
| 348 | NULL, // [10] reserved |
| 349 | NULL, // [11] reserved |
| 350 | NULL, // [12] reserved |
| 351 | southbridge_smi_tco, // [13] TCO_STS |
| 352 | southbridge_smi_periodic, // [14] PERIODIC_STS |
| 353 | NULL, // [15] SERIRQ_SMI_STS |
| 354 | NULL, // [16] SMBUS_SMI_STS |
| 355 | NULL, // [17] LEGACY_USB2_STS |
| 356 | NULL, // [18] INTEL_USB2_STS |
| 357 | NULL, // [19] reserved |
| 358 | NULL, // [20] PCI_EXP_SMI_STS |
| 359 | NULL, // [21] reserved |
| 360 | NULL, // [22] reserved |
| 361 | NULL, // [23] reserved |
| 362 | NULL, // [24] reserved |
| 363 | NULL, // [25] reserved |
| 364 | NULL, // [26] SPI_STS |
| 365 | NULL, // [27] reserved |
| 366 | NULL, // [28] PUNIT |
| 367 | NULL, // [29] GUNIT |
| 368 | NULL, // [30] reserved |
| 369 | NULL // [31] reserved |
| 370 | }; |
| 371 | |
| 372 | void southbridge_smi_handler(void) |
| 373 | { |
| 374 | int i; |
| 375 | uint32_t smi_sts; |
| 376 | |
| 377 | /* We need to clear the SMI status registers, or we won't see what's |
| 378 | * happening in the following calls. |
| 379 | */ |
| 380 | smi_sts = clear_smi_status(); |
| 381 | |
| 382 | /* Call SMI sub handler for each of the status bits */ |
| 383 | for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) { |
| 384 | if (!(smi_sts & (1 << i))) |
| 385 | continue; |
| 386 | |
| 387 | if (southbridge_smi[i] != NULL) { |
| 388 | southbridge_smi[i](); |
| 389 | } else { |
| 390 | printk(BIOS_DEBUG, |
Martin Roth | 7c96629 | 2014-12-07 14:59:46 -0700 | [diff] [blame] | 391 | "SMI_STS[%d] occurred, but no " |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 392 | "handler available.\n", i); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /* The GPIO SMI events do not have a status bit in SMI_STS. Therefore, |
| 397 | * these events need to be cleared and checked unconditionally. */ |
| 398 | mainboard_smi_gpi(clear_alt_status()); |
| 399 | } |