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>
32 // Future TODO: Move to i82801gx directory
33 #include "../../../northbridge/intel/i945/ich7.h"
35 extern unsigned char smm[];
36 extern unsigned int smm_len;
40 #define D_OPEN (1 << 6)
41 #define D_CLS (1 << 5)
42 #define D_LCK (1 << 4)
43 #define G_SMRAME (1 << 3)
44 #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0))
55 #define PM2_CNT 0x20 // mobile only
59 #define EL_SMI_EN (1 << 25) // Intel Quick Resume Technology
60 #define INTEL_USB2_EN (1 << 18) // Intel-Specific USB2 SMI logic
61 #define LEGACY_USB2_EN (1 << 17) // Legacy USB2 SMI logic
62 #define PERIODIC_EN (1 << 14) // SMI on PERIODIC_STS in SMI_STS
63 #define TCO_EN (1 << 13) // Enable TCO Logic (BIOSWE et al)
64 #define MCSMI_EN (1 << 11) // Trap microcontroller range access
65 #define BIOS_RLS (1 << 7) // asserts SCI on bit set
66 #define SWSMI_TMR_EN (1 << 6) // start software smi timer on bit set
67 #define APMC_EN (1 << 5) // Writes to APM_CNT cause SMI#
68 #define SLP_SMI_EN (1 << 4) // Write to SLP_EN in PM1_CNT asserts SMI#
69 #define LEGACY_USB_EN (1 << 3) // Legacy USB circuit SMI logic
70 #define BIOS_EN (1 << 2) // Assert SMI# on setting GBL_RLS bit
71 #define EOS (1 << 1) // End of SMI (deassert SMI#)
72 #define GBL_SMI_EN (1 << 0) // SMI# generation at all?
74 #define ALT_GP_SMI_EN 0x38
75 #define ALT_GP_SMI_STS 0x3a
77 #define DEVACT_STS 0x44
81 /* While we read PMBASE dynamically in case it changed, let's
82 * initialize it with a sane value
84 static u16 pmbase = DEFAULT_PMBASE;
87 * @brief read and clear PM1_STS
88 * @return PM1_STS register
90 static u16 reset_pm1_status(void)
94 reg16 = inw(pmbase + PM1_STS);
95 /* set status bits are cleared by writing 1 to them */
96 outw(reg16, pmbase + PM1_STS);
101 static void dump_pm1_status(u16 pm1_sts)
103 printk_debug("PM1_STS: ");
104 if (pm1_sts & (1 << 15)) printk_debug("WAK ");
105 if (pm1_sts & (1 << 14)) printk_debug("PCIEXPWAK ");
106 if (pm1_sts & (1 << 11)) printk_debug("PRBTNOR ");
107 if (pm1_sts & (1 << 10)) printk_debug("RTC ");
108 if (pm1_sts & (1 << 8)) printk_debug("PWRBTN ");
109 if (pm1_sts & (1 << 5)) printk_debug("GBL ");
110 if (pm1_sts & (1 << 4)) printk_debug("BM ");
111 if (pm1_sts & (1 << 0)) printk_debug("TMROF ");
116 * @brief read and clear SMI_STS
117 * @return SMI_STS register
119 static u32 reset_smi_status(void)
123 reg32 = inl(pmbase + SMI_STS);
124 /* set status bits are cleared by writing 1 to them */
125 outl(reg32, pmbase + SMI_STS);
130 static void dump_smi_status(u32 smi_sts)
132 printk_debug("SMI_STS: ");
133 if (smi_sts & (1 << 26)) printk_debug("SPI ");
134 if (smi_sts & (1 << 25)) printk_debug("EL_SMI ");
135 if (smi_sts & (1 << 21)) printk_debug("MONITOR ");
136 if (smi_sts & (1 << 20)) printk_debug("PCI_EXP_SMI ");
137 if (smi_sts & (1 << 18)) printk_debug("INTEL_USB2 ");
138 if (smi_sts & (1 << 17)) printk_debug("LEGACY_USB2 ");
139 if (smi_sts & (1 << 16)) printk_debug("SMBUS_SMI ");
140 if (smi_sts & (1 << 15)) printk_debug("SERIRQ_SMI ");
141 if (smi_sts & (1 << 14)) printk_debug("PERIODIC ");
142 if (smi_sts & (1 << 13)) printk_debug("TCO ");
143 if (smi_sts & (1 << 12)) printk_debug("DEVMON ");
144 if (smi_sts & (1 << 11)) printk_debug("MCSMI ");
145 if (smi_sts & (1 << 10)) printk_debug("GPI ");
146 if (smi_sts & (1 << 9)) printk_debug("GPE0 ");
147 if (smi_sts & (1 << 8)) printk_debug("PM1 ");
148 if (smi_sts & (1 << 6)) printk_debug("SWSMI_TMR ");
149 if (smi_sts & (1 << 5)) printk_debug("APM ");
150 if (smi_sts & (1 << 4)) printk_debug("SLP_SMI ");
151 if (smi_sts & (1 << 3)) printk_debug("LEGACY_USB ");
152 if (smi_sts & (1 << 2)) printk_debug("BIOS ");
158 * @brief read and clear GPE0_STS
159 * @return GPE0_STS register
161 static u32 reset_gpe0_status(void)
165 reg32 = inl(pmbase + GPE0_STS);
166 /* set status bits are cleared by writing 1 to them */
167 outl(reg32, pmbase + GPE0_STS);
172 static void dump_gpe0_status(u32 gpe0_sts)
175 printk_debug("GPE0_STS: ");
176 for (i=31; i<= 16; i--) {
177 if (gpe0_sts & (1 << i)) printk_debug("GPIO%d ", (i-16));
179 if (gpe0_sts & (1 << 14)) printk_debug("USB4 ");
180 if (gpe0_sts & (1 << 13)) printk_debug("PME_B0 ");
181 if (gpe0_sts & (1 << 12)) printk_debug("USB3 ");
182 if (gpe0_sts & (1 << 11)) printk_debug("PME ");
183 if (gpe0_sts & (1 << 10)) printk_debug("EL_SCI/BATLOW ");
184 if (gpe0_sts & (1 << 9)) printk_debug("PCI_EXP ");
185 if (gpe0_sts & (1 << 8)) printk_debug("RI ");
186 if (gpe0_sts & (1 << 7)) printk_debug("SMB_WAK ");
187 if (gpe0_sts & (1 << 6)) printk_debug("TCO_SCI ");
188 if (gpe0_sts & (1 << 5)) printk_debug("AC97 ");
189 if (gpe0_sts & (1 << 4)) printk_debug("USB2 ");
190 if (gpe0_sts & (1 << 3)) printk_debug("USB1 ");
191 if (gpe0_sts & (1 << 2)) printk_debug("HOT_PLUG ");
192 if (gpe0_sts & (1 << 0)) printk_debug("THRM ");
197 * @brief read and clear TCOx_STS
198 * @return TCOx_STS registers
200 static u32 reset_tco_status(void)
202 u32 tcobase = pmbase + 0x60;
205 reg32 = inl(tcobase + 0x04);
206 /* set status bits are cleared by writing 1 to them */
207 outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS
208 if (reg32 & (1 << 18))
209 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
215 static void dump_tco_status(u32 tco_sts)
217 printk_debug("TCO_STS: ");
218 if (tco_sts & (1 << 20)) printk_debug("SMLINK_SLV ");
219 if (tco_sts & (1 << 18)) printk_debug("BOOT ");
220 if (tco_sts & (1 << 17)) printk_debug("SECOND_TO ");
221 if (tco_sts & (1 << 16)) printk_debug("INTRD_DET ");
222 if (tco_sts & (1 << 12)) printk_debug("DMISERR ");
223 if (tco_sts & (1 << 10)) printk_debug("DMISMI ");
224 if (tco_sts & (1 << 9)) printk_debug("DMISCI ");
225 if (tco_sts & (1 << 8)) printk_debug("BIOSWR ");
226 if (tco_sts & (1 << 7)) printk_debug("NEWCENTURY ");
227 if (tco_sts & (1 << 3)) printk_debug("TIMEOUT ");
228 if (tco_sts & (1 << 2)) printk_debug("TCO_INT ");
229 if (tco_sts & (1 << 1)) printk_debug("SW_TCO ");
230 if (tco_sts & (1 << 0)) printk_debug("NMI2SMI ");
237 * @brief Set the EOS bit
239 static void smi_set_eos(void)
243 reg8 = inb(pmbase + SMI_EN);
245 outb(reg8, pmbase + SMI_EN);
248 extern uint8_t smm_relocation_start, smm_relocation_end;
250 void smm_relocate(void)
254 printk_debug("Initializing SMM handler...");
256 pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc;
257 printk_spew(" ... pmbase = 0x%04x\n", pmbase);
259 smi_en = inl(pmbase + SMI_EN);
260 if (smi_en & APMC_EN) {
261 printk_info("SMI# handler already enabled?\n");
265 /* copy the SMM relocation code */
266 memcpy((void *)0x38000, &smm_relocation_start,
267 &smm_relocation_end - &smm_relocation_start);
270 dump_smi_status(reset_smi_status());
271 dump_pm1_status(reset_pm1_status());
272 dump_gpe0_status(reset_gpe0_status());
273 dump_tco_status(reset_tco_status());
275 /* Enable SMI generation:
277 * - on APMC writes (io 0xb2)
278 * - on writes to SLP_EN (sleep states)
279 * - on writes to GBL_RLS (bios commands)
281 * - on microcontroller writes (io 0x62/0x66)
283 outl(smi_en | (TCO_EN | APMC_EN | SLP_SMI_EN | BIOS_EN |
284 EOS | GBL_SMI_EN), pmbase + SMI_EN);
287 * There are several methods of raising a controlled SMI# via
288 * software, among them:
289 * - Writes to io 0xb2 (APMC)
290 * - Writes to the Local Apic ICR with Delivery mode SMI.
292 * Using the local apic is a bit more tricky. According to
293 * AMD Family 11 Processor BKDG no destination shorthand must be
295 * The whole SMM initialization is quite a bit hardware specific, so
296 * I'm not too worried about the better of the methods at the moment
299 /* raise an SMI interrupt */
300 printk_spew(" ... raise SMI#\n");
304 void smm_install(void)
306 /* enable the SMM memory window */
307 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
308 D_OPEN | G_SMRAME | C_BASE_SEG);
310 /* copy the real SMM handler */
311 memcpy((void *)0xa0000, smm, smm_len);
314 /* close the SMM memory window and enable normal SMM */
315 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
316 G_SMRAME | C_BASE_SEG);
327 /* LOCK the SMM memory window and enable normal SMM.
328 * After running this function, only a full reset can
329 * make the SMM registers writable again.
331 printk_debug("Locking SMM.\n");
332 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
333 D_LCK | G_SMRAME | C_BASE_SEG);