07a7df47a52b3fbf83323f1262f6fc85c576d407
[coreboot.git] / src / southbridge / intel / sch / smi.c
1 /*
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.
15  *
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,
19  * MA 02110-1301 USA
20  */
21
22
23 #include <device/device.h>
24 #include <device/pci.h>
25 #include <console/console.h>
26 #include <arch/io.h>
27 #include <cpu/x86/cache.h>
28 #include <cpu/x86/smm.h>
29 #include <cpu/cpu.h>
30 #include <string.h>
31 #include "chip.h"
32 #include "sch.h"
33
34 extern unsigned char _binary_smm_start;
35 extern unsigned char _binary_smm_size;
36
37 /* I945 */
38 #define SMRAM           0x9d
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))
44
45 /* ICH7 */
46 #define PM1_STS         0x00
47 #define PM1_EN          0x02
48 #define PM1_CNT         0x04
49 #define PM1_TMR         0x08
50 #define PROC_CNT        0x10
51 #define LV2             0x14
52 #define LV3             0x15
53 #define LV4             0x16
54 #define PM2_CNT         0x20 // mobile only
55 #define GPE0_STS        0x28
56 #define GPE0_EN         0x2c
57 #define SMI_EN          0x30
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?
72 #define SMI_STS         0x34
73 #define ALT_GP_SMI_EN   0x38
74 #define ALT_GP_SMI_STS  0x3a
75 #define GPE_CNTL        0x42
76 #define DEVACT_STS      0x44
77 #define SS_CNT          0x50
78 #define C3_RES          0x54
79
80 /* While we read PMBASE dynamically in case it changed, let's
81  * initialize it with a sane value
82  */
83
84 static u16 pmbase = DEFAULT_PMBASE;
85 /**
86  * @brief read and clear PM1_STS
87  * @return PM1_STS register
88  */
89 static u16 reset_pm1_status(void)
90 {
91         u16 reg16;
92
93         reg16 = inw(pmbase + PM1_STS);
94         /* set status bits are cleared by writing 1 to them */
95         outw(reg16, pmbase + PM1_STS);
96
97         return reg16;
98 }
99
100 static void dump_pm1_status(u16 pm1_sts)
101 {
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");
112 }
113
114 /**
115  * @brief read and clear SMI_STS
116  * @return SMI_STS register
117  */
118 static u32 reset_smi_status(void)
119 {
120         u32 reg32;
121
122         reg32 = inl(pmbase + SMI_STS);
123         /* set status bits are cleared by writing 1 to them */
124         outl(reg32, pmbase + SMI_STS);
125
126         return reg32;
127 }
128
129 static void dump_smi_status(u32 smi_sts)
130 {
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");
153 }
154
155
156 /**
157  * @brief read and clear GPE0_STS
158  * @return GPE0_STS register
159  */
160 static u32 reset_gpe0_status(void)
161 {
162         u32 reg32;
163
164         reg32 = inl(pmbase + GPE0_STS);
165         /* set status bits are cleared by writing 1 to them */
166         outl(reg32, pmbase + GPE0_STS);
167
168         return reg32;
169 }
170
171 static void dump_gpe0_status(u32 gpe0_sts)
172 {
173         int i;
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));
177         }
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");
193 }
194
195 /**
196  * @brief read and clear TCOx_STS
197  * @return TCOx_STS registers
198  */
199 static u32 reset_tco_status(void)
200 {
201         u32 tcobase = pmbase + 0x60;
202         u32 reg32;
203
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
209
210         return reg32;
211 }
212
213
214 static void dump_tco_status(u32 tco_sts)
215 {
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");
231 }
232
233
234
235 /**
236  * @brief Set the EOS bit
237  */
238 static void smi_set_eos(void)
239 {
240 // FIXME: disabled until SMM actually works
241 #if 0
242         u8 reg8;
243
244         reg8 = inb(pmbase + SMI_EN);
245         reg8 |= EOS;
246         outb(reg8, pmbase + SMI_EN);
247 #endif
248 }
249
250 extern uint8_t smm_relocation_start, smm_relocation_end;
251
252 static void smm_relocate(void)
253 {
254         u32 smi_en;
255
256         printk(BIOS_DEBUG, "Initializing SMM handler...");
257
258         pmbase = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x1f, 0)), 0x40) & 0xfffc;
259         printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
260
261         smi_en = inl(pmbase + SMI_EN);
262         if (smi_en & APMC_EN) {
263                 printk(BIOS_INFO, "SMI# handler already enabled?\n");
264                 return;
265         }
266
267         /* copy the SMM relocation code */
268         memcpy((void *)0x38000, &smm_relocation_start,
269                         &smm_relocation_end - &smm_relocation_start);
270
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());
276
277         /* Enable SMI generation:
278          *  - on TCO events
279          *  - on APMC writes (io 0xb2)
280          *  - on writes to SLP_EN (sleep states)
281          *  - on writes to GBL_RLS (bios commands)
282          * No SMIs:
283          *  - on microcontroller writes (io 0x62/0x66)
284          */
285         outl(smi_en | (TCO_EN | APMC_EN | SLP_SMI_EN | BIOS_EN |
286                                 EOS | GBL_SMI_EN), pmbase + SMI_EN);
287
288         /**
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.
293          *
294          * Using the local apic is a bit more tricky. According to
295          * AMD Family 11 Processor BKDG no destination shorthand must be
296          * used.
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
299          */
300
301         /* raise an SMI interrupt */
302         printk(BIOS_SPEW, "  ... raise SMI#\n");
303         outb(0x00, 0xb2);
304 }
305
306 static void smm_install(void)
307 {
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);
311
312         /* copy the real SMM handler */
313         memcpy((void *)0xa0000, &_binary_smm_start, (size_t)&_binary_smm_size);
314         wbinvd();
315
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);
319 }
320
321 void smm_init(void)
322 {
323         smm_relocate();
324         smm_install();
325         smi_set_eos();
326 }
327
328 void smm_lock(void)
329 {
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.
333          */
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);
337 }
338
339 void smm_setup_structures(void *gnvs, void *tcg, void *smi1)
340 {
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.
343          */
344         *(u32 *)0x500 = (u32)gnvs;
345         *(u32 *)0x504 = (u32)tcg;
346         *(u32 *)0x508 = (u32)smi1;
347         outb(0xea, 0xb2);
348 }