blob: 01fbbfbcb740482c1d5f3ba557c02cd7f604287b [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer3b387452009-03-06 19:52:36 +00004 * Copyright (C) 2008-2009 coresystems GmbH
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00005 *
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 Reinauerdebb11f2008-10-29 04:46:52 +000015 */
16
17
18#include <device/device.h>
19#include <device/pci.h>
20#include <console/console.h>
21#include <arch/io.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022#include <cpu/cpu.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000023#include <cpu/x86/cache.h>
24#include <cpu/x86/smm.h>
25#include <string.h>
Stefan Reinauer573f7d42009-07-21 21:50:34 +000026#include "i82801gx.h"
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000027
Stefan Reinauerb987f7b2010-05-30 13:44:32 +000028extern unsigned char _binary_smm_start;
29extern unsigned char _binary_smm_size;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000030
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 Reinauerdebb11f2008-10-29 04:46:52 +000039/* While we read PMBASE dynamically in case it changed, let's
40 * initialize it with a sane value
41 */
42static u16 pmbase = DEFAULT_PMBASE;
43
44/**
Stefan Reinauer109ab312009-08-12 16:08:05 +000045 * @brief read and clear PM1_STS
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000046 * @return PM1_STS register
47 */
48static u16 reset_pm1_status(void)
49{
50 u16 reg16;
Stefan Reinauer109ab312009-08-12 16:08:05 +000051
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000052 reg16 = inw(pmbase + PM1_STS);
53 /* set status bits are cleared by writing 1 to them */
54 outw(reg16, pmbase + PM1_STS);
Stefan Reinauer109ab312009-08-12 16:08:05 +000055
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000056 return reg16;
57}
58
59static void dump_pm1_status(u16 pm1_sts)
60{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000061 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 Reinauerdebb11f2008-10-29 04:46:52 +000071}
72
73/**
Stefan Reinauer109ab312009-08-12 16:08:05 +000074 * @brief read and clear SMI_STS
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000075 * @return SMI_STS register
76 */
77static u32 reset_smi_status(void)
78{
79 u32 reg32;
Stefan Reinauer109ab312009-08-12 16:08:05 +000080
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000081 reg32 = inl(pmbase + SMI_STS);
82 /* set status bits are cleared by writing 1 to them */
83 outl(reg32, pmbase + SMI_STS);
Stefan Reinauer109ab312009-08-12 16:08:05 +000084
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000085 return reg32;
86}
87
88static void dump_smi_status(u32 smi_sts)
89{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000090 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 Reinauerdebb11f2008-10-29 04:46:52 +0000112}
113
114
115/**
116 * @brief read and clear GPE0_STS
117 * @return GPE0_STS register
118 */
119static u32 reset_gpe0_status(void)
120{
121 u32 reg32;
Stefan Reinauer109ab312009-08-12 16:08:05 +0000122
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000123 reg32 = inl(pmbase + GPE0_STS);
124 /* set status bits are cleared by writing 1 to them */
125 outl(reg32, pmbase + GPE0_STS);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000126
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000127 return reg32;
128}
129
130static void dump_gpe0_status(u32 gpe0_sts)
131{
132 int i;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000133 printk(BIOS_DEBUG, "GPE0_STS: ");
Konstantin Aladyshev62f80832013-03-07 04:04:27 +0400134 for (i=31; i>= 16; i--) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000135 if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16));
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000136 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000137 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 Reinauerdebb11f2008-10-29 04:46:52 +0000152}
153
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000154
155/**
156 * @brief read and clear ALT_GP_SMI_STS
157 * @return ALT_GP_SMI_STS register
158 */
159static 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
170static void dump_alt_gp_smi_status(u16 alt_gp_smi_sts)
171{
172 int i;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000173 printk(BIOS_DEBUG, "ALT_GP_SMI_STS: ");
Konstantin Aladyshev62f80832013-03-07 04:04:27 +0400174 for (i=15; i>= 0; i--) {
Konstantin Aladyshev07c3fc02013-03-07 04:37:02 +0400175 if (alt_gp_smi_sts & (1 << i)) printk(BIOS_DEBUG, "GPI%d ", i);
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000176 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000177 printk(BIOS_DEBUG, "\n");
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000178}
179
180
181
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000182/**
Stefan Reinauer109ab312009-08-12 16:08:05 +0000183 * @brief read and clear TCOx_STS
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000184 * @return TCOx_STS registers
185 */
186static u32 reset_tco_status(void)
187{
188 u32 tcobase = pmbase + 0x60;
189 u32 reg32;
Stefan Reinauer109ab312009-08-12 16:08:05 +0000190
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000191 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 Reinauer109ab312009-08-12 16:08:05 +0000196
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000197 return reg32;
198}
199
200
201static void dump_tco_status(u32 tco_sts)
202{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000203 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 Reinauerdebb11f2008-10-29 04:46:52 +0000218}
219
220
221
222/**
223 * @brief Set the EOS bit
224 */
225static void smi_set_eos(void)
226{
227 u8 reg8;
Stefan Reinauer109ab312009-08-12 16:08:05 +0000228
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000229 reg8 = inb(pmbase + SMI_EN);
230 reg8 |= EOS;
231 outb(reg8, pmbase + SMI_EN);
232}
233
234extern uint8_t smm_relocation_start, smm_relocation_end;
235
Stefan Reinauerde3206a2010-02-22 06:09:43 +0000236static void smm_relocate(void)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000237{
238 u32 smi_en;
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000239 u16 pm1_en;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000240
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000241 printk(BIOS_DEBUG, "Initializing SMM handler...");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000242
243 pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000244 printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000245
246 smi_en = inl(pmbase + SMI_EN);
247 if (smi_en & APMC_EN) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000248 printk(BIOS_INFO, "SMI# handler already enabled?\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000249 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 Reinauerc02b4fc2010-03-22 11:42:32 +0000256 printk(BIOS_DEBUG, "\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000257 dump_smi_status(reset_smi_status());
258 dump_pm1_status(reset_pm1_status());
259 dump_gpe0_status(reset_gpe0_status());
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000260 dump_alt_gp_smi_status(reset_alt_gp_smi_status());
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000261 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 Reinaueraca6ec62009-10-26 17:12:21 +0000271
272 smi_en = 0; /* reset SMI enables */
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000273
274#if 0
275 smi_en |= LEGACY_USB2_EN | LEGACY_USB_EN;
276#endif
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000277 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 Reinauerdebb11f2008-10-29 04:46:52 +0000292
Stefan Reinauer7a3d0952010-01-17 13:49:07 +0000293 pm1_en = 0;
294 pm1_en |= PWRBTN_EN;
295 pm1_en |= GBL_EN;
296 outw(pm1_en, pmbase + PM1_EN);
297
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000298 /**
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 Reinauer109ab312009-08-12 16:08:05 +0000304 * Using the local apic is a bit more tricky. According to
305 * AMD Family 11 Processor BKDG no destination shorthand must be
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000306 * 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 Reinauerc02b4fc2010-03-22 11:42:32 +0000312 printk(BIOS_SPEW, " ... raise SMI#\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000313 outb(0x00, 0xb2);
314}
315
Stefan Reinauercadc5452010-12-18 23:29:37 +0000316static int smm_handler_copied = 0;
317
Sven Schnellebfe8e512011-06-14 20:55:54 +0200318static 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 Reinauerde3206a2010-02-22 06:09:43 +0000328static void smm_install(void)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000329{
Stefan Reinauercadc5452010-12-18 23:29:37 +0000330 /* 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 Reinauerdebb11f2008-10-29 04:46:52 +0000337
Sven Schnellebfe8e512011-06-14 20:55:54 +0200338 /* 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 Reinauerdebb11f2008-10-29 04:46:52 +0000350
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
356void smm_init(void)
357{
Stefan Reinauerbc0f7a62010-08-01 15:41:14 +0000358 /* Put SMM code to 0xa0000 */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000359 smm_install();
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000360
Stefan Reinauerbc0f7a62010-08-01 15:41:14 +0000361 /* Put relocation code to 0x38000 and relocate SMBASE */
362 smm_relocate();
363
364 /* We're done. Make sure SMIs can happen! */
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000365 smi_set_eos();
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000366}
367
368void 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 Reinauerc02b4fc2010-03-22 11:42:32 +0000374 printk(BIOS_DEBUG, "Locking SMM.\n");
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000375 pci_write_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), SMRAM,
376 D_LCK | G_SMRAME | C_BASE_SEG);
377}
378
Stefan Reinauer573f7d42009-07-21 21:50:34 +0000379void 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 Reinauer573f7d42009-07-21 21:50:34 +0000385 outb(0xea, 0xb2);
386}