flashrom: Fix ICH9 locking register address and add important debug output.
[coreboot.git] / src / southbridge / intel / i82801gx / smihandler.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 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 "chip.h"
28
29 // Future TODO: Move to i82801gx directory
30 #include "../../../northbridge/intel/i945/ich7.h"
31
32 #define DEBUG_SMI
33
34 #define ACPI_DISABLE    0x1e
35 #define ACPI_ENABLE     0xe1
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_SMRANE      (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 #include "i82801gx_nvs.h"
81
82 /* While we read PMBASE dynamically in case it changed, let's
83  * initialize it with a sane value
84  */
85 static u16 pmbase = DEFAULT_PMBASE;
86
87 typedef enum { SMI_LOCKED, SMI_UNLOCKED } smi_semaphore;
88
89 /* SMI multiprocessing semaphore */
90 static volatile smi_semaphore smi_handler_status = SMI_UNLOCKED;
91
92 static int smi_obtain_lock(void)
93 {
94         u8 ret = SMI_LOCKED;
95
96         asm volatile (
97                 "movb %2, %%al\n"
98                 "xchgb %%al, %1\n"
99                 "movb %%al, %0\n"
100                 : "=g" (ret), "=m" (smi_handler_status)
101                 : "g" (SMI_LOCKED)
102                 : "eax"
103         );
104
105         return (ret == SMI_UNLOCKED);
106 }
107
108 static void smi_release_lock(void)
109 {
110         asm volatile (
111                 "movb %1, %%al\n"
112                 "xchgb %%al, %0\n"
113                 : "=m" (smi_handler_status)
114                 : "g" (SMI_UNLOCKED)
115                 : "eax"
116         );
117 }
118
119 #define LAPIC_ID 0xfee00020
120 static inline __attribute__((always_inline)) unsigned long nodeid(void)
121 {
122         return (*((volatile unsigned long *)(LAPIC_ID)) >> 24);
123 }
124
125
126 /**
127  * @brief read and clear PM1_STS 
128  * @return PM1_STS register
129  */
130 static u16 reset_pm1_status(void)
131 {
132         u16 reg16;
133         
134         reg16 = inw(pmbase + PM1_STS);
135         /* set status bits are cleared by writing 1 to them */
136         outw(reg16, pmbase + PM1_STS);
137         
138         return reg16;
139 }
140
141 static void dump_pm1_status(u16 pm1_sts)
142 {
143         printk_debug("PM1_STS: ");
144         if (pm1_sts & (1 << 15)) printk_debug("WAK ");
145         if (pm1_sts & (1 << 14)) printk_debug("PCIEXPWAK ");
146         if (pm1_sts & (1 << 11)) printk_debug("PRBTNOR ");
147         if (pm1_sts & (1 << 10)) printk_debug("RTC ");
148         if (pm1_sts & (1 <<  8)) printk_debug("PWRBTN ");
149         if (pm1_sts & (1 <<  5)) printk_debug("GBL ");
150         if (pm1_sts & (1 <<  4)) printk_debug("BM ");
151         if (pm1_sts & (1 <<  0)) printk_debug("TMROF ");
152         printk_debug("\n");
153 }
154
155 /**
156  * @brief read and clear SMI_STS 
157  * @return SMI_STS register
158  */
159 static u32 reset_smi_status(void)
160 {
161         u32 reg32;
162         
163         reg32 = inl(pmbase + SMI_STS);
164         /* set status bits are cleared by writing 1 to them */
165         outl(reg32, pmbase + SMI_STS);
166         
167         return reg32;
168 }
169
170 static void dump_smi_status(u32 smi_sts)
171 {
172         printk_debug("SMI_STS: ");
173         if (smi_sts & (1 << 26)) printk_debug("SPI ");
174         if (smi_sts & (1 << 25)) printk_debug("EL_SMI ");
175         if (smi_sts & (1 << 21)) printk_debug("MONITOR ");
176         if (smi_sts & (1 << 20)) printk_debug("PCI_EXP_SMI ");
177         if (smi_sts & (1 << 18)) printk_debug("INTEL_USB2 ");
178         if (smi_sts & (1 << 17)) printk_debug("LEGACY_USB2 ");
179         if (smi_sts & (1 << 16)) printk_debug("SMBUS_SMI ");
180         if (smi_sts & (1 << 15)) printk_debug("SERIRQ_SMI ");
181         if (smi_sts & (1 << 14)) printk_debug("PERIODIC ");
182         if (smi_sts & (1 << 13)) printk_debug("TCO ");
183         if (smi_sts & (1 << 12)) printk_debug("DEVMON ");
184         if (smi_sts & (1 << 11)) printk_debug("MCSMI ");
185         if (smi_sts & (1 << 10)) printk_debug("GPI ");
186         if (smi_sts & (1 <<  9)) printk_debug("GPE0 ");
187         if (smi_sts & (1 <<  8)) printk_debug("PM1 ");
188         if (smi_sts & (1 <<  6)) printk_debug("SWSMI_TMR ");
189         if (smi_sts & (1 <<  5)) printk_debug("APM ");
190         if (smi_sts & (1 <<  4)) printk_debug("SLP_SMI ");
191         if (smi_sts & (1 <<  3)) printk_debug("LEGACY_USB ");
192         if (smi_sts & (1 <<  2)) printk_debug("BIOS ");
193         printk_debug("\n");
194 }
195
196
197 /**
198  * @brief read and clear GPE0_STS
199  * @return GPE0_STS register
200  */
201 static u32 reset_gpe0_status(void)
202 {
203         u32 reg32;
204         
205         reg32 = inl(pmbase + GPE0_STS);
206         /* set status bits are cleared by writing 1 to them */
207         outl(reg32, pmbase + GPE0_STS);
208         
209         return reg32;
210 }
211
212 static void dump_gpe0_status(u32 gpe0_sts)
213 {
214         int i;
215         printk_debug("GPE0_STS: ");
216         for (i=31; i<= 16; i--) {
217                 if (gpe0_sts & (1 << i)) printk_debug("GPIO%d ", (i-16));
218         }
219         if (gpe0_sts & (1 << 14)) printk_debug("USB4 ");
220         if (gpe0_sts & (1 << 13)) printk_debug("PME_B0 ");
221         if (gpe0_sts & (1 << 12)) printk_debug("USB3 ");
222         if (gpe0_sts & (1 << 11)) printk_debug("PME ");
223         if (gpe0_sts & (1 << 10)) printk_debug("EL_SCI/BATLOW ");
224         if (gpe0_sts & (1 <<  9)) printk_debug("PCI_EXP ");
225         if (gpe0_sts & (1 <<  8)) printk_debug("RI ");
226         if (gpe0_sts & (1 <<  7)) printk_debug("SMB_WAK ");
227         if (gpe0_sts & (1 <<  6)) printk_debug("TCO_SCI ");
228         if (gpe0_sts & (1 <<  5)) printk_debug("AC97 ");
229         if (gpe0_sts & (1 <<  4)) printk_debug("USB2 ");
230         if (gpe0_sts & (1 <<  3)) printk_debug("USB1 ");
231         if (gpe0_sts & (1 <<  2)) printk_debug("HOT_PLUG ");
232         if (gpe0_sts & (1 <<  0)) printk_debug("THRM ");
233         printk_debug("\n");
234 }
235
236
237 /**
238  * @brief read and clear TCOx_STS 
239  * @return TCOx_STS registers
240  */
241 static u32 reset_tco_status(void)
242 {
243         u32 tcobase = pmbase + 0x60;
244         u32 reg32;
245         
246         reg32 = inl(tcobase + 0x04);
247         /* set status bits are cleared by writing 1 to them */
248         outl(reg32 & ~(1<<18), tcobase + 0x04); //  Don't clear BOOT_STS before SECOND_TO_STS
249         if (reg32 & (1 << 18))
250                 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
251         
252         return reg32;
253 }
254
255
256 static void dump_tco_status(u32 tco_sts)
257 {
258         printk_debug("TCO_STS: ");
259         if (tco_sts & (1 << 20)) printk_debug("SMLINK_SLV ");
260         if (tco_sts & (1 << 18)) printk_debug("BOOT ");
261         if (tco_sts & (1 << 17)) printk_debug("SECOND_TO ");
262         if (tco_sts & (1 << 16)) printk_debug("INTRD_DET ");
263         if (tco_sts & (1 << 12)) printk_debug("DMISERR ");
264         if (tco_sts & (1 << 10)) printk_debug("DMISMI ");
265         if (tco_sts & (1 <<  9)) printk_debug("DMISCI ");
266         if (tco_sts & (1 <<  8)) printk_debug("BIOSWR ");
267         if (tco_sts & (1 <<  7)) printk_debug("NEWCENTURY ");
268         if (tco_sts & (1 <<  3)) printk_debug("TIMEOUT ");
269         if (tco_sts & (1 <<  2)) printk_debug("TCO_INT ");
270         if (tco_sts & (1 <<  1)) printk_debug("SW_TCO ");
271         if (tco_sts & (1 <<  0)) printk_debug("NMI2SMI ");
272         printk_debug("\n");
273 }
274
275
276 /* ********************* smi_util ************************* */
277
278 /* Data */
279 #define UART_RBR 0x00
280 #define UART_TBR 0x00
281
282 /* Control */
283 #define UART_IER 0x01
284 #define UART_IIR 0x02
285 #define UART_FCR 0x02
286 #define UART_LCR 0x03
287 #define UART_MCR 0x04
288 #define UART_DLL 0x00
289 #define UART_DLM 0x01
290
291 /* Status */
292 #define UART_LSR 0x05
293 #define UART_MSR 0x06
294 #define UART_SCR 0x07
295
296 static int uart_can_tx_byte(void)
297 {
298         return inb(TTYS0_BASE + UART_LSR) & 0x20;
299 }
300
301 static void uart_wait_to_tx_byte(void)
302 {
303         while(!uart_can_tx_byte()) 
304         ;
305 }
306
307 static void uart_wait_until_sent(void)
308 {
309         while(!(inb(TTYS0_BASE + UART_LSR) & 0x40))
310         ; 
311 }
312
313 static void uart_tx_byte(unsigned char data)
314 {
315         uart_wait_to_tx_byte();
316         outb(data, TTYS0_BASE + UART_TBR);
317         /* Make certain the data clears the fifos */
318         uart_wait_until_sent();
319 }
320
321 void console_tx_flush(void)
322 {
323         uart_wait_to_tx_byte();
324 }
325
326 void console_tx_byte(unsigned char byte)
327 {
328         if (byte == '\n')
329                 uart_tx_byte('\r');
330         uart_tx_byte(byte);
331 }
332
333 /* We are using PCIe accesses for now
334  *  1. the chipset can do it
335  *  2. we don't need to worry about how we leave 0xcf8/0xcfc behind
336  */
337 #include "../../../northbridge/intel/i945/pcie_config.c"
338
339 /* ********************* smi_util ************************* */
340
341
342 void io_trap_handler(int smif)
343 {
344         u8 reg8;
345         global_nvs_t *gnvs = (global_nvs_t *)0xc00;
346
347         printk_debug("SMI function trap 0x%x: ", smif);
348
349
350         switch (smif) {
351         case 0x32:
352                 printk_debug("OS Init\n");
353                 break;
354         case 0xd6:
355                 printk_debug("Get Brightness\n");
356                 outb(0x17, 0x66);
357                 reg8 = inb(0x62);
358                 gnvs->brtl = reg8;
359                 break;
360         default:
361                 printk_debug("Unknown function\n");
362                 break;
363         }
364
365         /* On success, the IO Trap Handler returns 0
366          * On failure, the IO Trap Handler returns a value != 0
367          *
368          * For now, we force the return value to 0 and log all traps to
369          * see what's going on.
370          */
371         //gnvs->smif = 0;
372 }
373
374 /**
375  * @brief Set the EOS bit
376  */
377 static void smi_set_eos(void)
378 {
379         u8 reg8;
380         
381         reg8 = inb(pmbase + SMI_EN);
382         reg8 |= EOS;
383         outb(reg8, pmbase + SMI_EN);
384 }
385
386 /**
387  * @brief Interrupt handler for SMI#
388  *
389  * @param smm_revision revision of the smm state save map
390  */
391
392 void smi_handler(u32 smm_revision)
393 {
394         u8 reg8;
395         u16 pmctrl;
396         u16 pm1_sts;
397         u32 smi_sts, gpe0_sts, tco_sts;
398         unsigned int node;
399         smm_state_save_area_t state_save;
400
401         /* Are we ok to execute the handler? */
402         if (!smi_obtain_lock())
403                 return;
404
405         node=nodeid();
406
407 #ifdef DEBUG_SMI
408         console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
409 #else
410         console_loglevel = 1;
411 #endif
412
413         printk_debug("\nSMI# #%d\n", node);
414
415         switch (smm_revision) {
416         case 0x00030007:
417                 state_save.type = LEGACY;
418                 state_save.legacy_state_save = (legacy_smm_state_save_area_t *)
419                         (0xa8000 + 0x7e00 - (node * 0x400));
420                 break;
421         case 0x00030100:
422                 state_save.type = EM64T;
423                 state_save.em64t_state_save = (em64t_smm_state_save_area_t *)
424                         (0xa8000 + 0x7d00 - (node * 0x400));
425                 break;
426         default:
427                 printk_debug("smm_revision: 0x%08x\n", smm_revision);
428                 printk_debug("SMI# not supported on your CPU\n");
429                 /* Don't release lock, so no further SMI will happen,
430                  * if we don't handle it anyways.
431                  */
432                 return;
433         }
434
435         pmbase = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc;
436         printk_spew("SMI#: pmbase = 0x%04x\n", pmbase);
437         
438         /* We need to clear the SMI status registers, or we won't see what's
439          * happening in the following calls.
440          */
441         smi_sts = reset_smi_status();
442         dump_smi_status(smi_sts);
443
444         if (smi_sts & (1 << 21)) { // MONITOR
445                 global_nvs_t *gnvs = (global_nvs_t *)0xc00;
446                 int i;
447                 u32 reg32;
448
449                 reg32 = RCBA32(0x1e00); // TRSR - Trap Status Register
450 #if 0
451                 /* Comment in for some useful debug */
452                 for (i=0; i<4; i++) {
453                         if (reg32 & (1 << i)) {
454                                 printk_debug("  io trap #%d\n", i);
455                         }
456                 }
457 #endif
458                 RCBA32(0x1e00) = reg32; // TRSR
459
460                 reg32 = RCBA32(0x1e10);
461
462                 if ((reg32 & 0xfffc) != 0x808) {
463                         printk_debug("  trapped io address = 0x%x\n", reg32 & 0xfffc);
464                         printk_debug("  AHBE = %x\n", (reg32 >> 16) & 0xf);
465                         printk_debug("  read/write: %s\n", (reg32 & (1 << 24)) ? "read" :
466                                 "write");
467                 }
468
469                 if (!(reg32 & (1 << 24))) {
470                         /* Write Cycle */
471                         reg32 = RCBA32(0x1e18);
472                         printk_debug("  iotrap written data = 0x%08x\n", reg32);
473
474                 }
475
476                 if (gnvs->smif)
477                         io_trap_handler(gnvs->smif); // call function smif
478         }
479
480         if (smi_sts & (1 << 13)) { // TCO
481                 tco_sts = reset_tco_status();
482                 dump_tco_status(tco_sts);
483
484                 if (tco_sts & (1 << 8)) { // BIOSWR
485                         u8 bios_cntl;
486
487                         bios_cntl = pcie_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc);
488
489                         if (bios_cntl & 1) {
490                                 /* BWE is RW, so the SMI was caused by a
491                                  * write to BWE, not by a write to the BIOS
492                                  */
493
494                                 /* This is the place where we notice someone
495                                  * is trying to tinker with the BIOS. We are
496                                  * trying to be nice and just ignore it. A more
497                                  * resolute answer would be to power down the
498                                  * box.
499                                  */
500                                 printk_debug("Switching back to RO\n");
501                                 pcie_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1));
502                         } /* No else for now? */
503                 }
504         }
505
506         if (smi_sts & (1 << 8)) { // PM1
507                 pm1_sts = reset_pm1_status();
508                 dump_pm1_status(pm1_sts);
509         }
510
511         if (smi_sts & (1 << 9)) { // GPE0
512                 gpe0_sts = reset_gpe0_status();
513                 dump_gpe0_status(gpe0_sts);
514         }
515
516         if (smi_sts & (1 << 5)) { // APM
517                 /* Emulate B2 register as the FADT / Linux expects it */
518
519                 reg8 = inb(0xb2);
520                 switch (reg8) {
521                 case ACPI_DISABLE:
522                         pmctrl = inw(pmbase + 0x04);
523                         pmctrl |= (1 << 0);
524                         outw(pmctrl, pmbase + 0x04);
525                         printk_debug("SMI#: ACPI disabled.\n");
526                         break;
527                 case ACPI_ENABLE:
528                         pmctrl = inw(pmbase + 0x04);
529                         pmctrl &= ~(1 << 0);
530                         outw(pmctrl, pmbase + 0x04);
531                         printk_debug("SMI#: ACPI enabled.\n");
532                         break;
533                 }
534         }
535
536         if (smi_sts & (1 << 4)) { // SLP_SMI
537                 u32 reg32;
538                 reg32 = inl(pmbase + 0x04);
539                 printk_debug("SMI#: SLP = 0x%08x\n");
540                 printk_debug("SMI#: Powering off.\n");
541                 outl((6 << 10), pmbase + 0x04);
542                 outl((1 << 13) | (6 << 10), pmbase + 0x04);
543                 printk_debug("....\n");
544         }
545
546
547         smi_release_lock();
548
549         /* De-assert SMI# signal to allow another SMI */
550         smi_set_eos();
551 }