blob: 63c93739e7c7ab56aadf85a2c96c97c1504a1d60 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2014 Google Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of 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, MA 02110-1301 USA
19 */
20
21#include <delay.h>
22#include <types.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070023#include <arch/io.h>
24#include <console/console.h>
25#include <cpu/x86/cache.h>
26#include <device/pci_def.h>
27#include <cpu/x86/smm.h>
28#include <spi-generic.h>
29#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010030#include <halt.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070031#include <pc80/mc146818rtc.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070032#include <soc/lpc.h>
33#include <soc/nvs.h>
34#include <soc/pci_devs.h>
35#include <soc/pm.h>
36#include <soc/rcba.h>
37#include <soc/smm.h>
38#include <soc/xhci.h>
Duncan Laurief3e0a132015-01-14 17:33:00 -080039#include <drivers/intel/gma/i915_reg.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070040
41static u8 smm_initialized = 0;
42
43/*
44 * GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located
45 * by coreboot.
46 */
47static global_nvs_t *gnvs;
48global_nvs_t *smm_get_gnvs(void)
49{
50 return gnvs;
51}
52
53int southbridge_io_trap_handler(int smif)
54{
55 switch (smif) {
56 case 0x32:
57 printk(BIOS_DEBUG, "OS Init\n");
58 /* gnvs->smif:
59 * On success, the IO Trap Handler returns 0
60 * On failure, the IO Trap Handler returns a value != 0
61 */
62 gnvs->smif = 0;
63 return 1; /* IO trap handled */
64 }
65
66 /* Not handled */
67 return 0;
68}
69
70/**
71 * @brief Set the EOS bit
72 */
73void southbridge_smi_set_eos(void)
74{
75 enable_smi(EOS);
76}
77
78static void busmaster_disable_on_bus(int bus)
79{
80 int slot, func;
81 unsigned int val;
82 unsigned char hdr;
83
84 for (slot = 0; slot < 0x20; slot++) {
85 for (func = 0; func < 8; func++) {
86 u32 reg32;
87 device_t dev = PCI_DEV(bus, slot, func);
88
89 val = pci_read_config32(dev, PCI_VENDOR_ID);
90
91 if (val == 0xffffffff || val == 0x00000000 ||
92 val == 0x0000ffff || val == 0xffff0000)
93 continue;
94
95 /* Disable Bus Mastering for this one device */
96 reg32 = pci_read_config32(dev, PCI_COMMAND);
97 reg32 &= ~PCI_COMMAND_MASTER;
98 pci_write_config32(dev, PCI_COMMAND, reg32);
99
100 /* If this is a bridge, then follow it. */
101 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
102 hdr &= 0x7f;
103 if (hdr == PCI_HEADER_TYPE_BRIDGE ||
104 hdr == PCI_HEADER_TYPE_CARDBUS) {
105 unsigned int buses;
106 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
107 busmaster_disable_on_bus((buses >> 8) & 0xff);
108 }
109 }
110 }
111}
112
Duncan Laurief3e0a132015-01-14 17:33:00 -0800113/*
114 * Turn off the backlight if it is on, and wait for the specified
115 * backlight off delay. This will allow panel power timings to meet
116 * spec and prevent brief garbage on the screen when turned off
117 * during firmware with power button triggered SMI.
118 */
119static void backlight_off(void)
120{
121 void *reg_base;
122 uint32_t pp_ctrl;
123 uint32_t bl_off_delay;
124
125 reg_base = (void *)((uintptr_t)pci_read_config32(SA_DEV_IGD, PCI_BASE_ADDRESS_0) & ~0xf);
126
127 /* Check if backlight is enabled */
128 pp_ctrl = read32(reg_base + PCH_PP_CONTROL);
129 if (!(pp_ctrl & EDP_BLC_ENABLE))
130 return;
131
132 /* Enable writes to this register */
133 pp_ctrl &= ~PANEL_UNLOCK_MASK;
134 pp_ctrl |= PANEL_UNLOCK_REGS;
135
136 /* Turn off backlight */
137 pp_ctrl &= ~EDP_BLC_ENABLE;
138
139 write32(reg_base + PCH_PP_CONTROL, pp_ctrl);
140 read32(reg_base + PCH_PP_CONTROL);
141
142 /* Read backlight off delay in 100us units */
143 bl_off_delay = read32(reg_base + PCH_PP_OFF_DELAYS);
144 bl_off_delay &= PANEL_LIGHT_OFF_DELAY_MASK;
145 bl_off_delay *= 100;
146
147 /* Wait for backlight to turn off */
148 udelay(bl_off_delay);
149
150 printk(BIOS_INFO, "Backlight turned off\n");
151}
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700152
153static void southbridge_smi_sleep(void)
154{
155 u8 reg8;
156 u32 reg32;
157 u8 slp_typ;
158 u8 s5pwr = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
159
160 /* save and recover RTC port values */
161 u8 tmp70, tmp72;
162 tmp70 = inb(0x70);
163 tmp72 = inb(0x72);
164 get_option(&s5pwr, "power_on_after_fail");
165 outb(tmp70, 0x70);
166 outb(tmp72, 0x72);
167
168 /* First, disable further SMIs */
169 disable_smi(SLP_SMI_EN);
170
171 /* Figure out SLP_TYP */
172 reg32 = inl(ACPI_BASE_ADDRESS + PM1_CNT);
173 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
174 slp_typ = (reg32 >> 10) & 7;
175
176 /* Do any mainboard sleep handling */
177 mainboard_smi_sleep(slp_typ-2);
178
179 /* USB sleep preparations */
180 usb_xhci_sleep_prepare(PCH_DEV_XHCI, slp_typ);
181
182#if CONFIG_ELOG_GSMI
183 /* Log S3, S4, and S5 entry */
184 if (slp_typ >= 5)
185 elog_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ-2);
186#endif
187
Duncan Lauried775dda2014-10-25 01:49:32 -0700188 /* Clear pending GPE events */
189 clear_gpe_status();
190
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700191 /* Next, do the deed.
192 */
193
194 switch (slp_typ) {
195 case SLP_TYP_S0:
196 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
197 break;
198 case SLP_TYP_S1:
199 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
200 break;
201 case SLP_TYP_S3:
202 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
203
204 /* Invalidate the cache before going to S3 */
205 wbinvd();
206 break;
207 case SLP_TYP_S4:
208 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
209 break;
210 case SLP_TYP_S5:
211 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
212
Duncan Laurief3e0a132015-01-14 17:33:00 -0800213 /* Turn off backlight if needed */
214 backlight_off();
215
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700216 /* Disable all GPE */
217 disable_all_gpe();
218
219 /* Always set the flag in case CMOS was changed on runtime. For
220 * "KEEP", switch to "OFF" - KEEP is software emulated
221 */
222 reg8 = pci_read_config8(PCH_DEV_LPC, GEN_PMCON_3);
223 if (s5pwr == MAINBOARD_POWER_ON)
224 reg8 &= ~1;
225 else
226 reg8 |= 1;
227 pci_write_config8(PCH_DEV_LPC, GEN_PMCON_3, reg8);
228
229 /* also iterates over all bridges on bus 0 */
230 busmaster_disable_on_bus(0);
231 break;
232 default:
233 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
234 break;
235 }
236
237 /*
238 * Write back to the SLP register to cause the originally intended
239 * event again. We need to set BIT13 (SLP_EN) though to make the
240 * sleep happen.
241 */
242 enable_pm1_control(SLP_EN);
243
244 /* Make sure to stop executing code here for S3/S4/S5 */
245 if (slp_typ > 1)
Patrick Georgi546953c2014-11-29 10:38:17 +0100246 halt();
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700247
248 /*
249 * In most sleep states, the code flow of this function ends at
250 * the line above. However, if we entered sleep state S1 and wake
251 * up again, we will continue to execute code in this function.
252 */
253 reg32 = inl(ACPI_BASE_ADDRESS + PM1_CNT);
254 if (reg32 & SCI_EN) {
255 /* The OS is not an ACPI OS, so we set the state to S0 */
256 disable_pm1_control(SLP_EN | SLP_TYP);
257 }
258}
259
260/*
261 * Look for Synchronous IO SMI and use save state from that
262 * core in case we are not running on the same core that
263 * initiated the IO transaction.
264 */
265static em64t101_smm_state_save_area_t *smi_apmc_find_state_save(u8 cmd)
266{
267 em64t101_smm_state_save_area_t *state;
268 int node;
269
270 /* Check all nodes looking for the one that issued the IO */
271 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
272 state = smm_get_save_state(node);
273
274 /* Check for Synchronous IO (bit0==1) */
275 if (!(state->io_misc_info & (1 << 0)))
276 continue;
277
278 /* Make sure it was a write (bit4==0) */
279 if (state->io_misc_info & (1 << 4))
280 continue;
281
282 /* Check for APMC IO port */
283 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
284 continue;
285
286 /* Check AX against the requested command */
287 if ((state->rax & 0xff) != cmd)
288 continue;
289
290 return state;
291 }
292
293 return NULL;
294}
295
296#if CONFIG_ELOG_GSMI
297static void southbridge_smi_gsmi(void)
298{
299 u32 *ret, *param;
300 u8 sub_command;
301 em64t101_smm_state_save_area_t *io_smi =
302 smi_apmc_find_state_save(ELOG_GSMI_APM_CNT);
303
304 if (!io_smi)
305 return;
306
307 /* Command and return value in EAX */
308 ret = (u32*)&io_smi->rax;
309 sub_command = (u8)(*ret >> 8);
310
311 /* Parameter buffer in EBX */
312 param = (u32*)&io_smi->rbx;
313
314 /* drivers/elog/gsmi.c */
315 *ret = gsmi_exec(sub_command, param);
316}
317#endif
318
319static void finalize(void)
320{
321 static int finalize_done;
322
323 if (finalize_done) {
324 printk(BIOS_DEBUG, "SMM already finalized.\n");
325 return;
326 }
327 finalize_done = 1;
328
329#if CONFIG_SPI_FLASH_SMM
330 /* Re-init SPI driver to handle locked BAR */
331 spi_init();
332#endif
333}
334
335static void southbridge_smi_apmc(void)
336{
337 u8 reg8;
338 em64t101_smm_state_save_area_t *state;
339
340 /* Emulate B2 register as the FADT / Linux expects it */
341
342 reg8 = inb(APM_CNT);
343 switch (reg8) {
344 case APM_CNT_CST_CONTROL:
345 printk(BIOS_DEBUG, "C-state control\n");
346 break;
347 case APM_CNT_PST_CONTROL:
348 printk(BIOS_DEBUG, "P-state control\n");
349 break;
350 case APM_CNT_ACPI_DISABLE:
351 disable_pm1_control(SCI_EN);
352 printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n");
353 break;
354 case APM_CNT_ACPI_ENABLE:
355 enable_pm1_control(SCI_EN);
356 printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n");
357 break;
358 case APM_CNT_FINALIZE:
359 finalize();
360 break;
361 case APM_CNT_GNVS_UPDATE:
362 if (smm_initialized) {
363 printk(BIOS_DEBUG,
364 "SMI#: SMM structures already initialized!\n");
365 return;
366 }
367 state = smi_apmc_find_state_save(reg8);
368 if (state) {
369 /* EBX in the state save contains the GNVS pointer */
370 gnvs = (global_nvs_t *)((u32)state->rbx);
371 smm_initialized = 1;
372 printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs);
373 }
374 break;
375#if CONFIG_ELOG_GSMI
376 case ELOG_GSMI_APM_CNT:
377 southbridge_smi_gsmi();
378 break;
379#endif
380 }
381
382 mainboard_smi_apmc(reg8);
383}
384
385static void southbridge_smi_pm1(void)
386{
387 u16 pm1_sts = clear_pm1_status();
388
389 /* While OSPM is not active, poweroff immediately
390 * on a power button event.
391 */
392 if (pm1_sts & PWRBTN_STS) {
393 /* power button pressed */
394#if CONFIG_ELOG_GSMI
395 elog_add_event(ELOG_TYPE_POWER_BUTTON);
396#endif
397 disable_pm1_control(-1UL);
398 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << 10));
399 }
400}
401
402static void southbridge_smi_gpe0(void)
403{
404 clear_gpe_status();
405}
406
407static void southbridge_smi_gpi(void)
408{
409 mainboard_smi_gpi(clear_alt_smi_status());
410
411 /* Clear again after mainboard handler */
412 clear_alt_smi_status();
413}
414
415static void southbridge_smi_mc(void)
416{
417 u32 reg32 = inl(ACPI_BASE_ADDRESS + SMI_EN);
418
419 /* Are microcontroller SMIs enabled? */
420 if ((reg32 & MCSMI_EN) == 0)
421 return;
422
423 printk(BIOS_DEBUG, "Microcontroller SMI.\n");
424}
425
426static void southbridge_smi_tco(void)
427{
428 u32 tco_sts = clear_tco_status();
429
430 /* Any TCO event? */
431 if (!tco_sts)
432 return;
433
434 if (tco_sts & (1 << 8)) { // BIOSWR
435 u8 bios_cntl = pci_read_config16(PCH_DEV_LPC, BIOS_CNTL);
436
437 if (bios_cntl & 1) {
438 /*
439 * BWE is RW, so the SMI was caused by a
440 * write to BWE, not by a write to the BIOS
441 *
442 * This is the place where we notice someone
443 * is trying to tinker with the BIOS. We are
444 * trying to be nice and just ignore it. A more
445 * resolute answer would be to power down the
446 * box.
447 */
448 printk(BIOS_DEBUG, "Switching back to RO\n");
449 pci_write_config32(PCH_DEV_LPC, BIOS_CNTL,
450 (bios_cntl & ~1));
451 } /* No else for now? */
452 } else if (tco_sts & (1 << 3)) { /* TIMEOUT */
453 /* Handle TCO timeout */
454 printk(BIOS_DEBUG, "TCO Timeout.\n");
455 }
456}
457
458static void southbridge_smi_periodic(void)
459{
460 u32 reg32 = inl(ACPI_BASE_ADDRESS + SMI_EN);
461
462 /* Are periodic SMIs enabled? */
463 if ((reg32 & PERIODIC_EN) == 0)
464 return;
465
466 printk(BIOS_DEBUG, "Periodic SMI.\n");
467}
468
469static void southbridge_smi_monitor(void)
470{
471#define IOTRAP(x) (trap_sts & (1 << x))
472 u32 trap_sts, trap_cycle;
473 u32 data, mask = 0;
474 int i;
475
476 trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register
477 RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR
478
479 trap_cycle = RCBA32(0x1e10);
480 for (i=16; i<20; i++) {
481 if (trap_cycle & (1 << i))
482 mask |= (0xff << ((i - 16) << 2));
483 }
484
485
486 /* IOTRAP(3) SMI function call */
487 if (IOTRAP(3)) {
488 if (gnvs && gnvs->smif)
489 io_trap_handler(gnvs->smif); // call function smif
490 return;
491 }
492
493 /* IOTRAP(2) currently unused
494 * IOTRAP(1) currently unused */
495
496 /* IOTRAP(0) SMIC */
497 if (IOTRAP(0)) {
498 if (!(trap_cycle & (1 << 24))) { // It's a write
499 printk(BIOS_DEBUG, "SMI1 command\n");
500 data = RCBA32(0x1e18);
501 data &= mask;
502 // if (smi1)
503 // southbridge_smi_command(data);
504 // return;
505 }
506 // Fall through to debug
507 }
508
509 printk(BIOS_DEBUG, " trapped io address = 0x%x\n",
510 trap_cycle & 0xfffc);
511 for (i=0; i < 4; i++)
512 if(IOTRAP(i)) printk(BIOS_DEBUG, " TRAP = %d\n", i);
513 printk(BIOS_DEBUG, " AHBE = %x\n", (trap_cycle >> 16) & 0xf);
514 printk(BIOS_DEBUG, " MASK = 0x%08x\n", mask);
515 printk(BIOS_DEBUG, " read/write: %s\n",
516 (trap_cycle & (1 << 24)) ? "read" : "write");
517
518 if (!(trap_cycle & (1 << 24))) {
519 /* Write Cycle */
520 data = RCBA32(0x1e18);
521 printk(BIOS_DEBUG, " iotrap written data = 0x%08x\n", data);
522 }
523#undef IOTRAP
524}
525
526typedef void (*smi_handler_t)(void);
527
528static smi_handler_t southbridge_smi[32] = {
529 NULL, // [0] reserved
530 NULL, // [1] reserved
531 NULL, // [2] BIOS_STS
532 NULL, // [3] LEGACY_USB_STS
533 southbridge_smi_sleep, // [4] SLP_SMI_STS
534 southbridge_smi_apmc, // [5] APM_STS
535 NULL, // [6] SWSMI_TMR_STS
536 NULL, // [7] reserved
537 southbridge_smi_pm1, // [8] PM1_STS
538 southbridge_smi_gpe0, // [9] GPE0_STS
539 southbridge_smi_gpi, // [10] GPI_STS
540 southbridge_smi_mc, // [11] MCSMI_STS
541 NULL, // [12] DEVMON_STS
542 southbridge_smi_tco, // [13] TCO_STS
543 southbridge_smi_periodic, // [14] PERIODIC_STS
544 NULL, // [15] SERIRQ_SMI_STS
545 NULL, // [16] SMBUS_SMI_STS
546 NULL, // [17] LEGACY_USB2_STS
547 NULL, // [18] INTEL_USB2_STS
548 NULL, // [19] reserved
549 NULL, // [20] PCI_EXP_SMI_STS
550 southbridge_smi_monitor, // [21] MONITOR_STS
551 NULL, // [22] reserved
552 NULL, // [23] reserved
553 NULL, // [24] reserved
554 NULL, // [25] EL_SMI_STS
555 NULL, // [26] SPI_STS
556 NULL, // [27] reserved
557 NULL, // [28] reserved
558 NULL, // [29] reserved
559 NULL, // [30] reserved
560 NULL // [31] reserved
561};
562
563/**
564 * @brief Interrupt handler for SMI#
565 *
566 * @param smm_revision revision of the smm state save map
567 */
568
569void southbridge_smi_handler(void)
570{
571 int i;
572 u32 smi_sts;
573
574 /* We need to clear the SMI status registers, or we won't see what's
575 * happening in the following calls.
576 */
577 smi_sts = clear_smi_status();
578
579 /* Call SMI sub handler for each of the status bits */
580 for (i = 0; i < 31; i++) {
581 if (smi_sts & (1 << i)) {
582 if (southbridge_smi[i]) {
583 southbridge_smi[i]();
584 } else {
585 printk(BIOS_DEBUG,
Martin Rothde7ed6f2014-12-07 14:58:18 -0700586 "SMI_STS[%d] occurred, but no "
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700587 "handler available.\n", i);
588 }
589 }
590 }
591}