blob: d97801e27767ae9f8ef010379520e59a14f25b51 [file] [log] [blame]
Martin Rothbf6b83a2015-10-11 10:37:02 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
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.
Martin Rothbf6b83a2015-10-11 10:37:02 +020015 */
16
17
18#include <device/device.h>
19#include <device/pci.h>
20#include <console/console.h>
21#include <arch/io.h>
22#include <cpu/cpu.h>
23#include <cpu/x86/cache.h>
24#include <cpu/x86/smm.h>
25#include <string.h>
26#include <cpu/intel/smm/gen1/smi.h>
27#include "pch.h"
28
29/* While we read PMBASE dynamically in case it changed, let's
30 * initialize it with a sane value
31 */
32static u16 pmbase = DEFAULT_PMBASE;
33
34/**
35 * @brief read and clear PM1_STS
36 * @return PM1_STS register
37 */
38static u16 reset_pm1_status(void)
39{
40 u16 reg16;
41
42 reg16 = inw(pmbase + PM1_STS);
43 /* set status bits are cleared by writing 1 to them */
44 outw(reg16, pmbase + PM1_STS);
45
46 return reg16;
47}
48
49static void dump_pm1_status(u16 pm1_sts)
50{
51 printk(BIOS_DEBUG, "PM1_STS: ");
52 if (pm1_sts & (1 << 15)) printk(BIOS_DEBUG, "WAK ");
53 if (pm1_sts & (1 << 14)) printk(BIOS_DEBUG, "PCIEXPWAK ");
54 if (pm1_sts & (1 << 11)) printk(BIOS_DEBUG, "PRBTNOR ");
55 if (pm1_sts & (1 << 10)) printk(BIOS_DEBUG, "RTC ");
56 if (pm1_sts & (1 << 8)) printk(BIOS_DEBUG, "PWRBTN ");
57 if (pm1_sts & (1 << 5)) printk(BIOS_DEBUG, "GBL ");
58 if (pm1_sts & (1 << 4)) printk(BIOS_DEBUG, "BM ");
59 if (pm1_sts & (1 << 0)) printk(BIOS_DEBUG, "TMROF ");
60 printk(BIOS_DEBUG, "\n");
61}
62
63/**
64 * @brief read and clear SMI_STS
65 * @return SMI_STS register
66 */
67static u32 reset_smi_status(void)
68{
69 u32 reg32;
70
71 reg32 = inl(pmbase + SMI_STS);
72 /* set status bits are cleared by writing 1 to them */
73 outl(reg32, pmbase + SMI_STS);
74
75 return reg32;
76}
77
78static void dump_smi_status(u32 smi_sts)
79{
80 printk(BIOS_DEBUG, "SMI_STS: ");
81 if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI ");
82 if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI ");
83 if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR ");
84 if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI ");
85 if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 ");
86 if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 ");
87 if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI ");
88 if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI ");
89 if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC ");
90 if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO ");
91 if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON ");
92 if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI ");
93 if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI ");
94 if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 ");
95 if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 ");
96 if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR ");
97 if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM ");
98 if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI ");
99 if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB ");
100 if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS ");
101 printk(BIOS_DEBUG, "\n");
102}
103
104
105/**
106 * @brief read and clear GPE0_STS
107 * @return GPE0_STS register
108 */
109static u32 reset_gpe0_status(void)
110{
111 u32 reg32;
112
113 reg32 = inl(pmbase + GPE0_STS);
114 /* set status bits are cleared by writing 1 to them */
115 outl(reg32, pmbase + GPE0_STS);
116
117 return reg32;
118}
119
120static void dump_gpe0_status(u32 gpe0_sts)
121{
122 int i;
123 printk(BIOS_DEBUG, "GPE0_STS: ");
124 for (i=31; i>= 16; i--) {
125 if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16));
126 }
127 if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 ");
128 if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 ");
129 if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 ");
130 if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME ");
131 if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW ");
132 if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP ");
133 if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI ");
134 if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK ");
135 if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI ");
136 if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 ");
137 if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 ");
138 if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 ");
139 if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "HOT_PLUG ");
140 if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM ");
141 printk(BIOS_DEBUG, "\n");
142}
143
144
145/**
146 * @brief read and clear ALT_GP_SMI_STS
147 * @return ALT_GP_SMI_STS register
148 */
149static u16 reset_alt_gp_smi_status(void)
150{
151 u16 reg16;
152
153 reg16 = inl(pmbase + ALT_GP_SMI_STS);
154 /* set status bits are cleared by writing 1 to them */
155 outl(reg16, pmbase + ALT_GP_SMI_STS);
156
157 return reg16;
158}
159
160static void dump_alt_gp_smi_status(u16 alt_gp_smi_sts)
161{
162 int i;
163 printk(BIOS_DEBUG, "ALT_GP_SMI_STS: ");
164 for (i=15; i>= 0; i--) {
165 if (alt_gp_smi_sts & (1 << i)) printk(BIOS_DEBUG, "GPI%d ", i);
166 }
167 printk(BIOS_DEBUG, "\n");
168}
169
170
171
172/**
173 * @brief read and clear TCOx_STS
174 * @return TCOx_STS registers
175 */
176static u32 reset_tco_status(void)
177{
178 u32 tcobase = pmbase + 0x60;
179 u32 reg32;
180
181 reg32 = inl(tcobase + 0x04);
182 /* set status bits are cleared by writing 1 to them */
183 outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS
184 if (reg32 & (1 << 18))
185 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
186
187 return reg32;
188}
189
190
191static void dump_tco_status(u32 tco_sts)
192{
193 printk(BIOS_DEBUG, "TCO_STS: ");
194 if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV ");
195 if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT ");
196 if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO ");
197 if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET ");
198 if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR ");
199 if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI ");
200 if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI ");
201 if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR ");
202 if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY ");
203 if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT ");
204 if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT ");
205 if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO ");
206 if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI ");
207 printk(BIOS_DEBUG, "\n");
208}
209
210
211
212/**
213 * @brief Set the EOS bit
214 */
215static void smi_set_eos(void)
216{
217 u8 reg8;
218
219 reg8 = inb(pmbase + SMI_EN);
220 reg8 |= EOS;
221 outb(reg8, pmbase + SMI_EN);
222}
223
224void southbridge_smm_init(void)
225{
226 u32 smi_en;
227 u16 pm1_en;
228 u32 gpe0_en;
229
230#if CONFIG_ELOG
231 /* Log events from chipset before clearing */
232 pch_log_state();
233#endif
234
235 printk(BIOS_DEBUG, "Initializing southbridge SMI...");
236
237 pmbase = pci_read_config32(dev_find_slot(0, PCI_DEVFN(0x1f, 0)),
238 PMBASE) & 0xff80;
239
240 printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
241
242 smi_en = inl(pmbase + SMI_EN);
243 if (smi_en & APMC_EN) {
244 printk(BIOS_INFO, "SMI# handler already enabled?\n");
245 return;
246 }
247
248 printk(BIOS_DEBUG, "\n");
249 dump_smi_status(reset_smi_status());
250 dump_pm1_status(reset_pm1_status());
251 dump_gpe0_status(reset_gpe0_status());
252 dump_alt_gp_smi_status(reset_alt_gp_smi_status());
253 dump_tco_status(reset_tco_status());
254
255 /* Disable GPE0 PME_B0 */
256 gpe0_en = inl(pmbase + GPE0_EN);
257 gpe0_en &= ~PME_B0_EN;
258 outl(gpe0_en, pmbase + GPE0_EN);
259
260 pm1_en = 0;
261 pm1_en |= PWRBTN_EN;
262 pm1_en |= GBL_EN;
263 outw(pm1_en, pmbase + PM1_EN);
264
265 /* Enable SMI generation:
266 * - on TCO events
267 * - on APMC writes (io 0xb2)
268 * - on writes to SLP_EN (sleep states)
269 * - on writes to GBL_RLS (bios commands)
270 * No SMIs:
271 * - on microcontroller writes (io 0x62/0x66)
272 */
273
274 smi_en = 0; /* reset SMI enables */
275
276#if 0
277 smi_en |= LEGACY_USB2_EN | LEGACY_USB_EN;
278#endif
279 smi_en |= TCO_EN;
280 smi_en |= APMC_EN;
281#if DEBUG_PERIODIC_SMIS
282 /* Set DEBUG_PERIODIC_SMIS in pch.h to debug using
283 * periodic SMIs.
284 */
285 smi_en |= PERIODIC_EN;
286#endif
287 smi_en |= SLP_SMI_EN;
288#if 0
289 smi_en |= BIOS_EN;
290#endif
291
292 /* The following need to be on for SMIs to happen */
293 smi_en |= EOS | GBL_SMI_EN;
294
295 outl(smi_en, pmbase + SMI_EN);
296}
297
298void southbridge_trigger_smi(void)
299{
300 /**
301 * There are several methods of raising a controlled SMI# via
302 * software, among them:
303 * - Writes to io 0xb2 (APMC)
304 * - Writes to the Local Apic ICR with Delivery mode SMI.
305 *
306 * Using the local apic is a bit more tricky. According to
307 * AMD Family 11 Processor BKDG no destination shorthand must be
308 * used.
309 * The whole SMM initialization is quite a bit hardware specific, so
310 * I'm not too worried about the better of the methods at the moment
311 */
312
313 /* raise an SMI interrupt */
314 printk(BIOS_SPEW, " ... raise SMI#\n");
315 outb(0x00, 0xb2);
316}
317
318void southbridge_clear_smi_status(void)
319{
320 /* Clear SMI status */
321 reset_smi_status();
322
323 /* Clear PM1 status */
324 reset_pm1_status();
325
326 /* Set EOS bit so other SMIs can occur. */
327 smi_set_eos();
328}
329
330void smm_setup_structures(void *gnvs, void *tcg, void *smi1)
331{
332 /*
333 * Issue SMI to set the gnvs pointer in SMM.
334 * tcg and smi1 are unused.
335 *
336 * EAX = APM_CNT_GNVS_UPDATE
337 * EBX = gnvs pointer
338 * EDX = APM_CNT
339 */
340 asm volatile (
341 "outb %%al, %%dx\n\t"
342 : /* ignore result */
343 : "a" (APM_CNT_GNVS_UPDATE),
344 "b" ((u32)gnvs),
345 "d" (APM_CNT)
346 );
347}