SMM: add defines for APM_CNT register
[coreboot.git] / src / southbridge / intel / sch / smihandler.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 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <console/console.h>
25 #include <cpu/x86/cache.h>
26 #include <cpu/x86/smm.h>
27 #include "sch.h"
28
29 #define DEBUG_SMI
30
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_SMRANE      (1 << 3)
37 #define   C_BASE_SEG    ((0 << 2) | (1 << 1) | (0 << 0))
38
39 /* ICH7 */
40 #define PM1_STS         0x00
41 #define PM1_EN          0x02
42 #define PM1_CNT         0x04
43 #define PM1_TMR         0x08
44 #define PROC_CNT        0x10
45 #define LV2             0x14
46 #define LV3             0x15
47 #define LV4             0x16
48 #define PM2_CNT         0x20 // mobile only
49 #define GPE0_STS        0x28
50 #define GPE0_EN         0x2c
51 #define SMI_EN          0x30
52 #define   EL_SMI_EN      (1 << 25) // Intel Quick Resume Technology
53 #define   INTEL_USB2_EN  (1 << 18) // Intel-Specific USB2 SMI logic
54 #define   LEGACY_USB2_EN (1 << 17) // Legacy USB2 SMI logic
55 #define   PERIODIC_EN    (1 << 14) // SMI on PERIODIC_STS in SMI_STS
56 #define   TCO_EN         (1 << 13) // Enable TCO Logic (BIOSWE et al)
57 #define   MCSMI_EN       (1 << 11) // Trap microcontroller range access
58 #define   BIOS_RLS       (1 <<  7) // asserts SCI on bit set
59 #define   SWSMI_TMR_EN   (1 <<  6) // start software smi timer on bit set
60 #define   APMC_EN        (1 <<  5) // Writes to APM_CNT cause SMI#
61 #define   SLP_SMI_EN     (1 <<  4) // Write to SLP_EN in PM1_CNT asserts SMI#
62 #define   LEGACY_USB_EN  (1 <<  3) // Legacy USB circuit SMI logic
63 #define   BIOS_EN        (1 <<  2) // Assert SMI# on setting GBL_RLS bit
64 #define   EOS            (1 <<  1) // End of SMI (deassert SMI#)
65 #define   GBL_SMI_EN     (1 <<  0) // SMI# generation at all?
66 #define SMI_STS         0x34
67 #define ALT_GP_SMI_EN   0x38
68 #define ALT_GP_SMI_STS  0x3a
69 #define GPE_CNTL        0x42
70 #define DEVACT_STS      0x44
71 #define SS_CNT          0x50
72 #define C3_RES          0x54
73
74 //#include "i82801gx_nvs.h"
75
76 /* While we read PMBASE dynamically in case it changed, let's
77  * initialize it with a sane value
78  */
79 static u16 pmbase = DEFAULT_PMBASE;
80
81 // disabled because SMM doesn't actually work yet
82 #if 0
83 /**
84  * @brief read and clear PM1_STS
85  * @return PM1_STS register
86  */
87 static u16 reset_pm1_status(void)
88 {
89         u16 reg16;
90
91         reg16 = inw(pmbase + PM1_STS);
92         /* set status bits are cleared by writing 1 to them */
93         outw(reg16, pmbase + PM1_STS);
94
95         return reg16;
96 }
97
98 static void dump_pm1_status(u16 pm1_sts)
99 {
100         printk(BIOS_DEBUG, "PM1_STS: ");
101         if (pm1_sts & (1 << 15)) printk(BIOS_DEBUG, "WAK ");
102         if (pm1_sts & (1 << 14)) printk(BIOS_DEBUG, "PCIEXPWAK ");
103         if (pm1_sts & (1 << 11)) printk(BIOS_DEBUG, "PRBTNOR ");
104         if (pm1_sts & (1 << 10)) printk(BIOS_DEBUG, "RTC ");
105         if (pm1_sts & (1 <<  8)) printk(BIOS_DEBUG, "PWRBTN ");
106         if (pm1_sts & (1 <<  5)) printk(BIOS_DEBUG, "GBL ");
107         if (pm1_sts & (1 <<  4)) printk(BIOS_DEBUG, "BM ");
108         if (pm1_sts & (1 <<  0)) printk(BIOS_DEBUG, "TMROF ");
109         printk(BIOS_DEBUG, "\n");
110 }
111
112 /**
113  * @brief read and clear SMI_STS
114  * @return SMI_STS register
115  */
116 static u32 reset_smi_status(void)
117 {
118         u32 reg32;
119
120         reg32 = inl(pmbase + SMI_STS);
121         /* set status bits are cleared by writing 1 to them */
122         outl(reg32, pmbase + SMI_STS);
123
124         return reg32;
125 }
126
127 static void dump_smi_status(u32 smi_sts)
128 {
129         printk(BIOS_DEBUG, "SMI_STS: ");
130         if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI ");
131         if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI ");
132         if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR ");
133         if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI ");
134         if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 ");
135         if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 ");
136         if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI ");
137         if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI ");
138         if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC ");
139         if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO ");
140         if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON ");
141         if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI ");
142         if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI ");
143         if (smi_sts & (1 <<  9)) printk(BIOS_DEBUG, "GPE0 ");
144         if (smi_sts & (1 <<  8)) printk(BIOS_DEBUG, "PM1 ");
145         if (smi_sts & (1 <<  6)) printk(BIOS_DEBUG, "SWSMI_TMR ");
146         if (smi_sts & (1 <<  5)) printk(BIOS_DEBUG, "APM ");
147         if (smi_sts & (1 <<  4)) printk(BIOS_DEBUG, "SLP_SMI ");
148         if (smi_sts & (1 <<  3)) printk(BIOS_DEBUG, "LEGACY_USB ");
149         if (smi_sts & (1 <<  2)) printk(BIOS_DEBUG, "BIOS ");
150         printk(BIOS_DEBUG, "\n");
151 }
152
153
154 /**
155  * @brief read and clear GPE0_STS
156  * @return GPE0_STS register
157  */
158 static u32 reset_gpe0_status(void)
159 {
160         u32 reg32;
161
162         reg32 = inl(pmbase + GPE0_STS);
163         /* set status bits are cleared by writing 1 to them */
164         outl(reg32, pmbase + GPE0_STS);
165
166         return reg32;
167 }
168
169 static void dump_gpe0_status(u32 gpe0_sts)
170 {
171         int i;
172         printk(BIOS_DEBUG, "GPE0_STS: ");
173         for (i=31; i<= 16; i--) {
174                 if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16));
175         }
176         if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 ");
177         if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 ");
178         if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 ");
179         if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME ");
180         if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW ");
181         if (gpe0_sts & (1 <<  9)) printk(BIOS_DEBUG, "PCI_EXP ");
182         if (gpe0_sts & (1 <<  8)) printk(BIOS_DEBUG, "RI ");
183         if (gpe0_sts & (1 <<  7)) printk(BIOS_DEBUG, "SMB_WAK ");
184         if (gpe0_sts & (1 <<  6)) printk(BIOS_DEBUG, "TCO_SCI ");
185         if (gpe0_sts & (1 <<  5)) printk(BIOS_DEBUG, "AC97 ");
186         if (gpe0_sts & (1 <<  4)) printk(BIOS_DEBUG, "USB2 ");
187         if (gpe0_sts & (1 <<  3)) printk(BIOS_DEBUG, "USB1 ");
188         if (gpe0_sts & (1 <<  2)) printk(BIOS_DEBUG, "HOT_PLUG ");
189         if (gpe0_sts & (1 <<  0)) printk(BIOS_DEBUG, "THRM ");
190         printk(BIOS_DEBUG, "\n");
191 }
192
193
194 /**
195  * @brief read and clear TCOx_STS
196  * @return TCOx_STS registers
197  */
198 static u32 reset_tco_status(void)
199 {
200         u32 tcobase = pmbase + 0x60;
201         u32 reg32;
202
203         reg32 = inl(tcobase + 0x04);
204         /* set status bits are cleared by writing 1 to them */
205         outl(reg32 & ~(1<<18), tcobase + 0x04); //  Don't clear BOOT_STS before SECOND_TO_STS
206         if (reg32 & (1 << 18))
207                 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
208
209         return reg32;
210 }
211
212
213 static void dump_tco_status(u32 tco_sts)
214 {
215         printk(BIOS_DEBUG, "TCO_STS: ");
216         if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV ");
217         if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT ");
218         if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO ");
219         if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET ");
220         if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR ");
221         if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI ");
222         if (tco_sts & (1 <<  9)) printk(BIOS_DEBUG, "DMISCI ");
223         if (tco_sts & (1 <<  8)) printk(BIOS_DEBUG, "BIOSWR ");
224         if (tco_sts & (1 <<  7)) printk(BIOS_DEBUG, "NEWCENTURY ");
225         if (tco_sts & (1 <<  3)) printk(BIOS_DEBUG, "TIMEOUT ");
226         if (tco_sts & (1 <<  2)) printk(BIOS_DEBUG, "TCO_INT ");
227         if (tco_sts & (1 <<  1)) printk(BIOS_DEBUG, "SW_TCO ");
228         if (tco_sts & (1 <<  0)) printk(BIOS_DEBUG, "NMI2SMI ");
229         printk(BIOS_DEBUG, "\n");
230 }
231 #endif
232
233
234 /* We are using PCIe accesses for now
235  *  1. the chipset can do it
236  *  2. we don't need to worry about how we leave 0xcf8/0xcfc behind
237  */
238 //#include "../../../northbridge/intel/i945/pcie_config.c"
239
240 int southbridge_io_trap_handler(int smif)
241 {
242         //global_nvs_t *gnvs = (global_nvs_t *)0xc00;
243
244         switch (smif) {
245         case 0x32:
246                 printk(BIOS_DEBUG, "OS Init\n");
247                 //gnvs->smif = 0;
248                 break;
249         default:
250                 /* Not handled */
251                 return 0;
252         }
253
254         /* On success, the IO Trap Handler returns 0
255          * On failure, the IO Trap Handler returns a value != 0
256          *
257          * For now, we force the return value to 0 and log all traps to
258          * see what's going on.
259          */
260         //gnvs->smif = 0;
261         return 1; /* IO trap handled */
262 }
263
264 /**
265  * @brief Set the EOS bit
266  */
267 void southbridge_smi_set_eos(void)
268 {
269         u8 reg8;
270
271         reg8 = inb(pmbase + SMI_EN);
272         reg8 |= EOS;
273         outb(reg8, pmbase + SMI_EN);
274 }
275
276 /**
277  * @brief Interrupt handler for SMI#
278  *
279  * @param smm_revision revision of the smm state save map
280  */
281
282 void southbridge_smi_handler(unsigned int node, smm_state_save_area_t *state_save)
283 {
284 // FIXME: the necessary magic isn't available yet. the code
285 // below is a partially adapted ICH7 version of the handler
286 #if 0
287         u8 reg8;
288         u16 pmctrl;
289         u16 pm1_sts;
290         u32 smi_sts, gpe0_sts, tco_sts;
291
292         pmbase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0x48) & 0xfffc;
293         printk(BIOS_SPEW, "SMI#: pmbase = 0x%04x\n", pmbase);
294
295         /* We need to clear the SMI status registers, or we won't see what's
296          * happening in the following calls.
297          */
298         smi_sts = reset_smi_status();
299         dump_smi_status(smi_sts);
300
301         if (smi_sts & (1 << 21)) { // MONITOR
302                 global_nvs_t *gnvs = (global_nvs_t *)0xc00;
303                 u32 reg32;
304
305                 reg32 = RCBA32(0x1e00);  TRSR - Trap Status Register
306 //#if 0
307                 /* Comment in for some useful debug */
308                 for (i=0; i<4; i++) {
309                         if (reg32 & (1 << i)) {
310                                 printk(BIOS_DEBUG, "  io trap #%d\n", i);
311                         }
312                 }
313 //#endif
314                 RCBA32(0x1e00) = reg32;  TRSR
315
316                 reg32 = RCBA32(0x1e10);
317
318                 if ((reg32 & 0xfffc) != 0x808) {
319                         printk(BIOS_DEBUG, "  trapped io address = 0x%x\n", reg32 & 0xfffc);
320                         printk(BIOS_DEBUG, "  AHBE = %x\n", (reg32 >> 16) & 0xf);
321                         printk(BIOS_DEBUG, "  read/write: %s\n", (reg32 & (1 << 24)) ? "read" :
322                                 "write");
323                 }
324
325                 if (!(reg32 & (1 << 24))) {
326                         /* Write Cycle */
327                         reg32 = RCBA32(0x1e18);
328                         printk(BIOS_DEBUG, "  iotrap written data = 0x%08x\n", reg32);
329
330                 }
331
332                 if (gnvs->smif)
333                         io_trap_handler(gnvs->smif); // call function smif
334         }
335
336         if (smi_sts & (1 << 13)) { // TCO
337                 tco_sts = reset_tco_status();
338                 dump_tco_status(tco_sts);
339
340                 if (tco_sts & (1 << 8)) { // BIOSWR
341                         u8 bios_cntl;
342                         bios_cntl = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc);
343                         if (bios_cntl & 1) {
344                                 /* BWE is RW, so the SMI was caused by a
345                                  * write to BWE, not by a write to the BIOS
346                                  */
347
348                                 /* This is the place where we notice someone
349                                  * is trying to tinker with the BIOS. We are
350                                  * trying to be nice and just ignore it. A more
351                                  * resolute answer would be to power down the
352                                  * box.
353                                  */
354                                 printk(BIOS_DEBUG, "Switching back to RO\n");
355                                 pcie_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1));
356                         } /* No else for now? */
357                 }
358         }
359
360         if (smi_sts & (1 << 8)) { // PM1
361                 pm1_sts = reset_pm1_status();
362                 dump_pm1_status(pm1_sts);
363         }
364
365         if (smi_sts & (1 << 9)) { // GPE0
366                 gpe0_sts = reset_gpe0_status();
367                 dump_gpe0_status(gpe0_sts);
368         }
369
370         if (smi_sts & (1 << 5)) { // APM
371                 /* Emulate B2 register as the FADT / Linux expects it */
372
373                 reg8 = inb(0xb2);
374                 switch (reg8) {
375                 case ACPI_DISABLE:
376                         pmctrl = inw(pmbase + 0x04);
377                         pmctrl |= (1 << 0);
378                         outw(pmctrl, pmbase + 0x04);
379                         printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n");
380                         break;
381                 case ACPI_ENABLE:
382                         pmctrl = inw(pmbase + 0x04);
383                         pmctrl &= ~(1 << 0);
384                         outw(pmctrl, pmbase + 0x04);
385                         printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n");
386                         break;
387                 }
388         }
389
390         if (smi_sts & (1 << 4)) { // SLP_SMI
391                 u32 reg32;
392
393                 /* First, disable further SMIs */
394                 reg8 = inb(pmbase + SMI_EN);
395                 reg8 &= ~SLP_SMI_EN;
396                 outb(reg8, pmbase + SMI_EN);
397
398                 /* Next, do the deed, we should change
399                  * power on after power loss bits here
400                  * if we're going to S5
401                  */
402
403                 /* Write back to the SLP register to cause the
404                  * originally intended event again. We need to set BIT13
405                  * (SLP_EN) though to make the sleep happen.
406                  */
407                 reg32 = inl(pmbase + 0x04);
408                 printk(BIOS_DEBUG, "SMI#: SLP = 0x%08x\n", reg32);
409                 printk(BIOS_DEBUG, "SMI#: Powering off.\n");
410                 outl(reg32 | (1 << 13), pmbase + 0x04);
411         }
412 #endif
413 }