Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Stefan Reinauer | 3b38745 | 2009-03-06 19:52:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2009 coresystems GmbH |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 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. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
| 18 | #include <device/device.h> |
| 19 | #include <device/pci.h> |
| 20 | #include <console/console.h> |
| 21 | #include <arch/io.h> |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 22 | #include <cpu/cpu.h> |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 23 | #include <cpu/x86/cache.h> |
| 24 | #include <cpu/x86/smm.h> |
| 25 | #include <string.h> |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 26 | #include "i82801gx.h" |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 27 | |
Stefan Reinauer | b987f7b | 2010-05-30 13:44:32 +0000 | [diff] [blame] | 28 | extern unsigned char _binary_smm_start; |
| 29 | extern unsigned char _binary_smm_size; |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 30 | |
| 31 | /* I945 */ |
| 32 | #define SMRAM 0x9d |
| 33 | #define D_OPEN (1 << 6) |
| 34 | #define D_CLS (1 << 5) |
| 35 | #define D_LCK (1 << 4) |
| 36 | #define G_SMRAME (1 << 3) |
| 37 | #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0)) |
| 38 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 39 | /* While we read PMBASE dynamically in case it changed, let's |
| 40 | * initialize it with a sane value |
| 41 | */ |
| 42 | static u16 pmbase = DEFAULT_PMBASE; |
| 43 | |
| 44 | /** |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 45 | * @brief read and clear PM1_STS |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 46 | * @return PM1_STS register |
| 47 | */ |
| 48 | static u16 reset_pm1_status(void) |
| 49 | { |
| 50 | u16 reg16; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 51 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 52 | reg16 = inw(pmbase + PM1_STS); |
| 53 | /* set status bits are cleared by writing 1 to them */ |
| 54 | outw(reg16, pmbase + PM1_STS); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 55 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 56 | return reg16; |
| 57 | } |
| 58 | |
| 59 | static void dump_pm1_status(u16 pm1_sts) |
| 60 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 61 | printk(BIOS_DEBUG, "PM1_STS: "); |
| 62 | if (pm1_sts & (1 << 15)) printk(BIOS_DEBUG, "WAK "); |
| 63 | if (pm1_sts & (1 << 14)) printk(BIOS_DEBUG, "PCIEXPWAK "); |
| 64 | if (pm1_sts & (1 << 11)) printk(BIOS_DEBUG, "PRBTNOR "); |
| 65 | if (pm1_sts & (1 << 10)) printk(BIOS_DEBUG, "RTC "); |
| 66 | if (pm1_sts & (1 << 8)) printk(BIOS_DEBUG, "PWRBTN "); |
| 67 | if (pm1_sts & (1 << 5)) printk(BIOS_DEBUG, "GBL "); |
| 68 | if (pm1_sts & (1 << 4)) printk(BIOS_DEBUG, "BM "); |
| 69 | if (pm1_sts & (1 << 0)) printk(BIOS_DEBUG, "TMROF "); |
| 70 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 74 | * @brief read and clear SMI_STS |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 75 | * @return SMI_STS register |
| 76 | */ |
| 77 | static u32 reset_smi_status(void) |
| 78 | { |
| 79 | u32 reg32; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 80 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 81 | reg32 = inl(pmbase + SMI_STS); |
| 82 | /* set status bits are cleared by writing 1 to them */ |
| 83 | outl(reg32, pmbase + SMI_STS); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 84 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 85 | return reg32; |
| 86 | } |
| 87 | |
| 88 | static void dump_smi_status(u32 smi_sts) |
| 89 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 90 | printk(BIOS_DEBUG, "SMI_STS: "); |
| 91 | if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI "); |
| 92 | if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI "); |
| 93 | if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR "); |
| 94 | if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI "); |
| 95 | if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 "); |
| 96 | if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 "); |
| 97 | if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI "); |
| 98 | if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI "); |
| 99 | if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC "); |
| 100 | if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO "); |
| 101 | if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON "); |
| 102 | if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI "); |
| 103 | if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI "); |
| 104 | if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 "); |
| 105 | if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 "); |
| 106 | if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR "); |
| 107 | if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM "); |
| 108 | if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI "); |
| 109 | if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB "); |
| 110 | if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS "); |
| 111 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * @brief read and clear GPE0_STS |
| 117 | * @return GPE0_STS register |
| 118 | */ |
| 119 | static u32 reset_gpe0_status(void) |
| 120 | { |
| 121 | u32 reg32; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 122 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 123 | reg32 = inl(pmbase + GPE0_STS); |
| 124 | /* set status bits are cleared by writing 1 to them */ |
| 125 | outl(reg32, pmbase + GPE0_STS); |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 127 | return reg32; |
| 128 | } |
| 129 | |
| 130 | static void dump_gpe0_status(u32 gpe0_sts) |
| 131 | { |
| 132 | int i; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 133 | printk(BIOS_DEBUG, "GPE0_STS: "); |
Konstantin Aladyshev | 62f8083 | 2013-03-07 04:04:27 +0400 | [diff] [blame] | 134 | for (i=31; i>= 16; i--) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 135 | if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16)); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 136 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 137 | if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 "); |
| 138 | if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 "); |
| 139 | if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 "); |
| 140 | if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME "); |
| 141 | if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW "); |
| 142 | if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP "); |
| 143 | if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI "); |
| 144 | if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK "); |
| 145 | if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI "); |
| 146 | if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 "); |
| 147 | if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 "); |
| 148 | if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 "); |
| 149 | if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "HOT_PLUG "); |
| 150 | if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM "); |
| 151 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * @brief read and clear ALT_GP_SMI_STS |
| 157 | * @return ALT_GP_SMI_STS register |
| 158 | */ |
| 159 | static u16 reset_alt_gp_smi_status(void) |
| 160 | { |
| 161 | u16 reg16; |
| 162 | |
| 163 | reg16 = inl(pmbase + ALT_GP_SMI_STS); |
| 164 | /* set status bits are cleared by writing 1 to them */ |
| 165 | outl(reg16, pmbase + ALT_GP_SMI_STS); |
| 166 | |
| 167 | return reg16; |
| 168 | } |
| 169 | |
| 170 | static void dump_alt_gp_smi_status(u16 alt_gp_smi_sts) |
| 171 | { |
| 172 | int i; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 173 | printk(BIOS_DEBUG, "ALT_GP_SMI_STS: "); |
Konstantin Aladyshev | 62f8083 | 2013-03-07 04:04:27 +0400 | [diff] [blame] | 174 | for (i=15; i>= 0; i--) { |
Konstantin Aladyshev | 07c3fc0 | 2013-03-07 04:37:02 +0400 | [diff] [blame] | 175 | if (alt_gp_smi_sts & (1 << i)) printk(BIOS_DEBUG, "GPI%d ", i); |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 176 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 177 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 182 | /** |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 183 | * @brief read and clear TCOx_STS |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 184 | * @return TCOx_STS registers |
| 185 | */ |
| 186 | static u32 reset_tco_status(void) |
| 187 | { |
| 188 | u32 tcobase = pmbase + 0x60; |
| 189 | u32 reg32; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 190 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 191 | reg32 = inl(tcobase + 0x04); |
| 192 | /* set status bits are cleared by writing 1 to them */ |
| 193 | outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS |
| 194 | if (reg32 & (1 << 18)) |
| 195 | outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 196 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 197 | return reg32; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static void dump_tco_status(u32 tco_sts) |
| 202 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 203 | printk(BIOS_DEBUG, "TCO_STS: "); |
| 204 | if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV "); |
| 205 | if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT "); |
| 206 | if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO "); |
| 207 | if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET "); |
| 208 | if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR "); |
| 209 | if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI "); |
| 210 | if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI "); |
| 211 | if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR "); |
| 212 | if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY "); |
| 213 | if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT "); |
| 214 | if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT "); |
| 215 | if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO "); |
| 216 | if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI "); |
| 217 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * @brief Set the EOS bit |
| 224 | */ |
| 225 | static void smi_set_eos(void) |
| 226 | { |
| 227 | u8 reg8; |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 228 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 229 | reg8 = inb(pmbase + SMI_EN); |
| 230 | reg8 |= EOS; |
| 231 | outb(reg8, pmbase + SMI_EN); |
| 232 | } |
| 233 | |
| 234 | extern uint8_t smm_relocation_start, smm_relocation_end; |
| 235 | |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 236 | static void smm_relocate(void) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 237 | { |
| 238 | u32 smi_en; |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 239 | u16 pm1_en; |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 240 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 241 | printk(BIOS_DEBUG, "Initializing SMM handler..."); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 242 | |
| 243 | pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 244 | printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 245 | |
| 246 | smi_en = inl(pmbase + SMI_EN); |
| 247 | if (smi_en & APMC_EN) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 248 | printk(BIOS_INFO, "SMI# handler already enabled?\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
| 252 | /* copy the SMM relocation code */ |
| 253 | memcpy((void *)0x38000, &smm_relocation_start, |
| 254 | &smm_relocation_end - &smm_relocation_start); |
| 255 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 256 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 257 | dump_smi_status(reset_smi_status()); |
| 258 | dump_pm1_status(reset_pm1_status()); |
| 259 | dump_gpe0_status(reset_gpe0_status()); |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 260 | dump_alt_gp_smi_status(reset_alt_gp_smi_status()); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 261 | dump_tco_status(reset_tco_status()); |
| 262 | |
| 263 | /* Enable SMI generation: |
| 264 | * - on TCO events |
| 265 | * - on APMC writes (io 0xb2) |
| 266 | * - on writes to SLP_EN (sleep states) |
| 267 | * - on writes to GBL_RLS (bios commands) |
| 268 | * No SMIs: |
| 269 | * - on microcontroller writes (io 0x62/0x66) |
| 270 | */ |
Stefan Reinauer | aca6ec6 | 2009-10-26 17:12:21 +0000 | [diff] [blame] | 271 | |
| 272 | smi_en = 0; /* reset SMI enables */ |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 273 | |
| 274 | #if 0 |
| 275 | smi_en |= LEGACY_USB2_EN | LEGACY_USB_EN; |
| 276 | #endif |
Stefan Reinauer | aca6ec6 | 2009-10-26 17:12:21 +0000 | [diff] [blame] | 277 | smi_en |= TCO_EN; |
| 278 | smi_en |= APMC_EN; |
| 279 | #if DEBUG_PERIODIC_SMIS |
| 280 | /* Set DEBUG_PERIODIC_SMIS in i82801gx.h to debug using |
| 281 | * periodic SMIs. |
| 282 | */ |
| 283 | smi_en |= PERIODIC_EN; |
| 284 | #endif |
| 285 | smi_en |= SLP_SMI_EN; |
| 286 | smi_en |= BIOS_EN; |
| 287 | |
| 288 | /* The following need to be on for SMIs to happen */ |
| 289 | smi_en |= EOS | GBL_SMI_EN; |
| 290 | |
| 291 | outl(smi_en, pmbase + SMI_EN); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 292 | |
Stefan Reinauer | 7a3d095 | 2010-01-17 13:49:07 +0000 | [diff] [blame] | 293 | pm1_en = 0; |
| 294 | pm1_en |= PWRBTN_EN; |
| 295 | pm1_en |= GBL_EN; |
| 296 | outw(pm1_en, pmbase + PM1_EN); |
| 297 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 298 | /** |
| 299 | * There are several methods of raising a controlled SMI# via |
| 300 | * software, among them: |
| 301 | * - Writes to io 0xb2 (APMC) |
| 302 | * - Writes to the Local Apic ICR with Delivery mode SMI. |
| 303 | * |
Stefan Reinauer | 109ab31 | 2009-08-12 16:08:05 +0000 | [diff] [blame] | 304 | * Using the local apic is a bit more tricky. According to |
| 305 | * AMD Family 11 Processor BKDG no destination shorthand must be |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 306 | * used. |
| 307 | * The whole SMM initialization is quite a bit hardware specific, so |
| 308 | * I'm not too worried about the better of the methods at the moment |
| 309 | */ |
| 310 | |
| 311 | /* raise an SMI interrupt */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 312 | printk(BIOS_SPEW, " ... raise SMI#\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 313 | outb(0x00, 0xb2); |
| 314 | } |
| 315 | |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 316 | static int smm_handler_copied = 0; |
| 317 | |
Sven Schnelle | bfe8e51 | 2011-06-14 20:55:54 +0200 | [diff] [blame] | 318 | static int is_wakeup(void) |
| 319 | { |
| 320 | device_t dev0 = dev_find_slot(0, PCI_DEVFN(0,0)); |
| 321 | |
| 322 | if (!dev0) |
| 323 | return 0; |
| 324 | |
| 325 | return pci_read_config32(dev0, 0xdc) == SKPAD_ACPI_S3_MAGIC; |
| 326 | } |
| 327 | |
Stefan Reinauer | de3206a | 2010-02-22 06:09:43 +0000 | [diff] [blame] | 328 | static void smm_install(void) |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 329 | { |
Stefan Reinauer | cadc545 | 2010-12-18 23:29:37 +0000 | [diff] [blame] | 330 | /* The first CPU running this gets to copy the SMM handler. But not all |
| 331 | * of them. |
| 332 | */ |
| 333 | if (smm_handler_copied) |
| 334 | return; |
| 335 | smm_handler_copied = 1; |
| 336 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 337 | |
Sven Schnelle | bfe8e51 | 2011-06-14 20:55:54 +0200 | [diff] [blame] | 338 | /* if we're resuming from S3, the SMM code is already in place, |
| 339 | * so don't copy it again to keep the current SMM state */ |
| 340 | |
| 341 | if (!is_wakeup()) { |
| 342 | /* enable the SMM memory window */ |
| 343 | pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM, |
| 344 | D_OPEN | G_SMRAME | C_BASE_SEG); |
| 345 | |
| 346 | /* copy the real SMM handler */ |
| 347 | memcpy((void *)0xa0000, &_binary_smm_start, (size_t)&_binary_smm_size); |
| 348 | wbinvd(); |
| 349 | } |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 350 | |
| 351 | /* close the SMM memory window and enable normal SMM */ |
| 352 | pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM, |
| 353 | G_SMRAME | C_BASE_SEG); |
| 354 | } |
| 355 | |
| 356 | void smm_init(void) |
| 357 | { |
Stefan Reinauer | bc0f7a6 | 2010-08-01 15:41:14 +0000 | [diff] [blame] | 358 | /* Put SMM code to 0xa0000 */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 359 | smm_install(); |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 360 | |
Stefan Reinauer | bc0f7a6 | 2010-08-01 15:41:14 +0000 | [diff] [blame] | 361 | /* Put relocation code to 0x38000 and relocate SMBASE */ |
| 362 | smm_relocate(); |
| 363 | |
| 364 | /* We're done. Make sure SMIs can happen! */ |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 365 | smi_set_eos(); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void smm_lock(void) |
| 369 | { |
| 370 | /* LOCK the SMM memory window and enable normal SMM. |
| 371 | * After running this function, only a full reset can |
| 372 | * make the SMM registers writable again. |
| 373 | */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 374 | printk(BIOS_DEBUG, "Locking SMM.\n"); |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 375 | pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM, |
| 376 | D_LCK | G_SMRAME | C_BASE_SEG); |
| 377 | } |
| 378 | |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 379 | void smm_setup_structures(void *gnvs, void *tcg, void *smi1) |
| 380 | { |
| 381 | /* The GDT or coreboot table is going to live here. But a long time |
| 382 | * after we relocated the GNVS, so this is not troublesome. |
| 383 | */ |
| 384 | *(u32 *)0x500 = (u32)gnvs; |
Stefan Reinauer | 573f7d4 | 2009-07-21 21:50:34 +0000 | [diff] [blame] | 385 | outb(0xea, 0xb2); |
| 386 | } |