2 * This file is part of the coreboot project.
4 * Copyright (C) 2008-2009 coresystems GmbH
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
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <console/console.h>
27 #include <cpu/x86/cache.h>
28 #include <cpu/x86/smm.h>
34 extern unsigned char _binary_smm_start;
35 extern unsigned char _binary_smm_size;
39 #define D_OPEN (1 << 6)
40 #define D_CLS (1 << 5)
41 #define D_LCK (1 << 4)
42 #define G_SMRAME (1 << 3)
43 #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0))
54 #define PM2_CNT 0x20 // mobile only
58 #define EL_SMI_EN (1 << 25) // Intel Quick Resume Technology
59 #define INTEL_USB2_EN (1 << 18) // Intel-Specific USB2 SMI logic
60 #define LEGACY_USB2_EN (1 << 17) // Legacy USB2 SMI logic
61 #define PERIODIC_EN (1 << 14) // SMI on PERIODIC_STS in SMI_STS
62 #define TCO_EN (1 << 13) // Enable TCO Logic (BIOSWE et al)
63 #define MCSMI_EN (1 << 11) // Trap microcontroller range access
64 #define BIOS_RLS (1 << 7) // asserts SCI on bit set
65 #define SWSMI_TMR_EN (1 << 6) // start software smi timer on bit set
66 #define APMC_EN (1 << 5) // Writes to APM_CNT cause SMI#
67 #define SLP_SMI_EN (1 << 4) // Write to SLP_EN in PM1_CNT asserts SMI#
68 #define LEGACY_USB_EN (1 << 3) // Legacy USB circuit SMI logic
69 #define BIOS_EN (1 << 2) // Assert SMI# on setting GBL_RLS bit
70 #define EOS (1 << 1) // End of SMI (deassert SMI#)
71 #define GBL_SMI_EN (1 << 0) // SMI# generation at all?
73 #define ALT_GP_SMI_EN 0x38
74 #define ALT_GP_SMI_STS 0x3a
76 #define DEVACT_STS 0x44
80 /* While we read PMBASE dynamically in case it changed, let's
81 * initialize it with a sane value
84 static u16 pmbase = DEFAULT_PMBASE;
86 * @brief read and clear PM1_STS
87 * @return PM1_STS register
89 static u16 reset_pm1_status(void)
93 reg16 = inw(pmbase + PM1_STS);
94 /* set status bits are cleared by writing 1 to them */
95 outw(reg16, pmbase + PM1_STS);
100 static void dump_pm1_status(u16 pm1_sts)
102 printk(BIOS_DEBUG, "PM1_STS: ");
103 if (pm1_sts & (1 << 15)) printk(BIOS_DEBUG, "WAK ");
104 if (pm1_sts & (1 << 14)) printk(BIOS_DEBUG, "PCIEXPWAK ");
105 if (pm1_sts & (1 << 11)) printk(BIOS_DEBUG, "PRBTNOR ");
106 if (pm1_sts & (1 << 10)) printk(BIOS_DEBUG, "RTC ");
107 if (pm1_sts & (1 << 8)) printk(BIOS_DEBUG, "PWRBTN ");
108 if (pm1_sts & (1 << 5)) printk(BIOS_DEBUG, "GBL ");
109 if (pm1_sts & (1 << 4)) printk(BIOS_DEBUG, "BM ");
110 if (pm1_sts & (1 << 0)) printk(BIOS_DEBUG, "TMROF ");
111 printk(BIOS_DEBUG, "\n");
115 * @brief read and clear SMI_STS
116 * @return SMI_STS register
118 static u32 reset_smi_status(void)
122 reg32 = inl(pmbase + SMI_STS);
123 /* set status bits are cleared by writing 1 to them */
124 outl(reg32, pmbase + SMI_STS);
129 static void dump_smi_status(u32 smi_sts)
131 printk(BIOS_DEBUG, "SMI_STS: ");
132 if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI ");
133 if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI ");
134 if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR ");
135 if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI ");
136 if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 ");
137 if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 ");
138 if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI ");
139 if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI ");
140 if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC ");
141 if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO ");
142 if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON ");
143 if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI ");
144 if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI ");
145 if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 ");
146 if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 ");
147 if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR ");
148 if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM ");
149 if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI ");
150 if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB ");
151 if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS ");
152 printk(BIOS_DEBUG, "\n");
157 * @brief read and clear GPE0_STS
158 * @return GPE0_STS register
160 static u32 reset_gpe0_status(void)
164 reg32 = inl(pmbase + GPE0_STS);
165 /* set status bits are cleared by writing 1 to them */
166 outl(reg32, pmbase + GPE0_STS);
171 static void dump_gpe0_status(u32 gpe0_sts)
174 printk(BIOS_DEBUG, "GPE0_STS: ");
175 for (i=31; i<= 16; i--) {
176 if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16));
178 if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 ");
179 if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 ");
180 if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 ");
181 if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME ");
182 if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW ");
183 if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP ");
184 if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI ");
185 if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK ");
186 if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI ");
187 if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 ");
188 if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 ");
189 if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 ");
190 if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "HOT_PLUG ");
191 if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM ");
192 printk(BIOS_DEBUG, "\n");
196 * @brief read and clear TCOx_STS
197 * @return TCOx_STS registers
199 static u32 reset_tco_status(void)
201 u32 tcobase = pmbase + 0x60;
204 reg32 = inl(tcobase + 0x04);
205 /* set status bits are cleared by writing 1 to them */
206 outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS
207 if (reg32 & (1 << 18))
208 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
214 static void dump_tco_status(u32 tco_sts)
216 printk(BIOS_DEBUG, "TCO_STS: ");
217 if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV ");
218 if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT ");
219 if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO ");
220 if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET ");
221 if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR ");
222 if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI ");
223 if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI ");
224 if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR ");
225 if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY ");
226 if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT ");
227 if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT ");
228 if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO ");
229 if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI ");
230 printk(BIOS_DEBUG, "\n");
236 * @brief Set the EOS bit
238 static void smi_set_eos(void)
240 // FIXME: disabled until SMM actually works
244 reg8 = inb(pmbase + SMI_EN);
246 outb(reg8, pmbase + SMI_EN);
250 extern uint8_t smm_relocation_start, smm_relocation_end;
252 static void smm_relocate(void)
256 printk(BIOS_DEBUG, "Initializing SMM handler...");
258 pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc;
259 printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
261 smi_en = inl(pmbase + SMI_EN);
262 if (smi_en & APMC_EN) {
263 printk(BIOS_INFO, "SMI# handler already enabled?\n");
267 /* copy the SMM relocation code */
268 memcpy((void *)0x38000, &smm_relocation_start,
269 &smm_relocation_end - &smm_relocation_start);
271 printk(BIOS_DEBUG, "\n");
272 dump_smi_status(reset_smi_status());
273 dump_pm1_status(reset_pm1_status());
274 dump_gpe0_status(reset_gpe0_status());
275 dump_tco_status(reset_tco_status());
277 /* Enable SMI generation:
279 * - on APMC writes (io 0xb2)
280 * - on writes to SLP_EN (sleep states)
281 * - on writes to GBL_RLS (bios commands)
283 * - on microcontroller writes (io 0x62/0x66)
285 outl(smi_en | (TCO_EN | APMC_EN | SLP_SMI_EN | BIOS_EN |
286 EOS | GBL_SMI_EN), pmbase + SMI_EN);
289 * There are several methods of raising a controlled SMI# via
290 * software, among them:
291 * - Writes to io 0xb2 (APMC)
292 * - Writes to the Local Apic ICR with Delivery mode SMI.
294 * Using the local apic is a bit more tricky. According to
295 * AMD Family 11 Processor BKDG no destination shorthand must be
297 * The whole SMM initialization is quite a bit hardware specific, so
298 * I'm not too worried about the better of the methods at the moment
301 /* raise an SMI interrupt */
302 printk(BIOS_SPEW, " ... raise SMI#\n");
306 static void smm_install(void)
308 /* enable the SMM memory window */
309 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
310 D_OPEN | G_SMRAME | C_BASE_SEG);
312 /* copy the real SMM handler */
313 memcpy((void *)0xa0000, &_binary_smm_start, (size_t)&_binary_smm_size);
316 /* close the SMM memory window and enable normal SMM */
317 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
318 G_SMRAME | C_BASE_SEG);
330 /* LOCK the SMM memory window and enable normal SMM.
331 * After running this function, only a full reset can
332 * make the SMM registers writable again.
334 printk(BIOS_DEBUG, "Locking SMM.\n");
335 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
336 D_LCK | G_SMRAME | C_BASE_SEG);
339 void smm_setup_structures(void *gnvs, void *tcg, void *smi1)
341 /* The GDT or coreboot table is going to live here. But a long time
342 * after we relocated the GNVS, so this is not troublesome.
344 *(u32 *)0x500 = (u32)gnvs;
345 *(u32 *)0x504 = (u32)tcg;
346 *(u32 *)0x508 = (u32)smi1;