blob: 8a5b443ac8ab5d7463e9918e10b374f8c24e6462 [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.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070015 */
16
17#include <delay.h>
18#include <types.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070019#include <arch/io.h>
20#include <console/console.h>
21#include <cpu/x86/cache.h>
22#include <device/pci_def.h>
23#include <cpu/x86/smm.h>
24#include <spi-generic.h>
25#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010026#include <halt.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070027#include <pc80/mc146818rtc.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070028#include <soc/lpc.h>
29#include <soc/nvs.h>
30#include <soc/pci_devs.h>
31#include <soc/pm.h>
32#include <soc/rcba.h>
33#include <soc/smm.h>
34#include <soc/xhci.h>
Duncan Laurief3e0a132015-01-14 17:33:00 -080035#include <drivers/intel/gma/i915_reg.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070036
37static u8 smm_initialized = 0;
38
39/*
40 * GNVS needs to be updated by an 0xEA PM Trap (B2) after it has been located
41 * by coreboot.
42 */
43static global_nvs_t *gnvs;
44global_nvs_t *smm_get_gnvs(void)
45{
46 return gnvs;
47}
48
49int southbridge_io_trap_handler(int smif)
50{
51 switch (smif) {
52 case 0x32:
53 printk(BIOS_DEBUG, "OS Init\n");
54 /* gnvs->smif:
55 * On success, the IO Trap Handler returns 0
56 * On failure, the IO Trap Handler returns a value != 0
57 */
58 gnvs->smif = 0;
59 return 1; /* IO trap handled */
60 }
61
62 /* Not handled */
63 return 0;
64}
65
66/**
67 * @brief Set the EOS bit
68 */
69void southbridge_smi_set_eos(void)
70{
71 enable_smi(EOS);
72}
73
74static void busmaster_disable_on_bus(int bus)
75{
Lee Leahy26b7cd02017-03-16 18:47:55 -070076 int slot, func;
77 unsigned int val;
78 unsigned char hdr;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070079
Lee Leahy26b7cd02017-03-16 18:47:55 -070080 for (slot = 0; slot < 0x20; slot++) {
81 for (func = 0; func < 8; func++) {
82 u32 reg32;
83 device_t dev = PCI_DEV(bus, slot, func);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070084
Lee Leahy26b7cd02017-03-16 18:47:55 -070085 val = pci_read_config32(dev, PCI_VENDOR_ID);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070086
Lee Leahy26b7cd02017-03-16 18:47:55 -070087 if (val == 0xffffffff || val == 0x00000000 ||
88 val == 0x0000ffff || val == 0xffff0000)
89 continue;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070090
Lee Leahy26b7cd02017-03-16 18:47:55 -070091 /* Disable Bus Mastering for this one device */
92 reg32 = pci_read_config32(dev, PCI_COMMAND);
93 reg32 &= ~PCI_COMMAND_MASTER;
94 pci_write_config32(dev, PCI_COMMAND, reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -070095
Lee Leahy26b7cd02017-03-16 18:47:55 -070096 /* If this is a bridge, then follow it. */
97 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
98 hdr &= 0x7f;
99 if (hdr == PCI_HEADER_TYPE_BRIDGE ||
100 hdr == PCI_HEADER_TYPE_CARDBUS) {
101 unsigned int buses;
102 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
103 busmaster_disable_on_bus((buses >> 8) & 0xff);
104 }
105 }
106 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700107}
108
Duncan Laurief3e0a132015-01-14 17:33:00 -0800109/*
110 * Turn off the backlight if it is on, and wait for the specified
111 * backlight off delay. This will allow panel power timings to meet
112 * spec and prevent brief garbage on the screen when turned off
113 * during firmware with power button triggered SMI.
114 */
115static void backlight_off(void)
116{
117 void *reg_base;
118 uint32_t pp_ctrl;
119 uint32_t bl_off_delay;
120
Lee Leahy6ef51922017-03-17 10:56:08 -0700121 reg_base = (void *)((uintptr_t)pci_read_config32(SA_DEV_IGD,
122 PCI_BASE_ADDRESS_0) & ~0xf);
Duncan Laurief3e0a132015-01-14 17:33:00 -0800123
124 /* Check if backlight is enabled */
125 pp_ctrl = read32(reg_base + PCH_PP_CONTROL);
126 if (!(pp_ctrl & EDP_BLC_ENABLE))
127 return;
128
129 /* Enable writes to this register */
130 pp_ctrl &= ~PANEL_UNLOCK_MASK;
131 pp_ctrl |= PANEL_UNLOCK_REGS;
132
133 /* Turn off backlight */
134 pp_ctrl &= ~EDP_BLC_ENABLE;
135
136 write32(reg_base + PCH_PP_CONTROL, pp_ctrl);
137 read32(reg_base + PCH_PP_CONTROL);
138
139 /* Read backlight off delay in 100us units */
140 bl_off_delay = read32(reg_base + PCH_PP_OFF_DELAYS);
141 bl_off_delay &= PANEL_LIGHT_OFF_DELAY_MASK;
142 bl_off_delay *= 100;
143
144 /* Wait for backlight to turn off */
145 udelay(bl_off_delay);
146
147 printk(BIOS_INFO, "Backlight turned off\n");
148}
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700149
150static void southbridge_smi_sleep(void)
151{
152 u8 reg8;
153 u32 reg32;
154 u8 slp_typ;
155 u8 s5pwr = CONFIG_MAINBOARD_POWER_ON_AFTER_POWER_FAIL;
156
157 /* save and recover RTC port values */
158 u8 tmp70, tmp72;
159 tmp70 = inb(0x70);
160 tmp72 = inb(0x72);
161 get_option(&s5pwr, "power_on_after_fail");
162 outb(tmp70, 0x70);
163 outb(tmp72, 0x72);
164
165 /* First, disable further SMIs */
166 disable_smi(SLP_SMI_EN);
167
168 /* Figure out SLP_TYP */
169 reg32 = inl(ACPI_BASE_ADDRESS + PM1_CNT);
170 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500171 slp_typ = acpi_sleep_from_pm1(reg32);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700172
173 /* Do any mainboard sleep handling */
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500174 mainboard_smi_sleep(slp_typ);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700175
176 /* USB sleep preparations */
177 usb_xhci_sleep_prepare(PCH_DEV_XHCI, slp_typ);
178
179#if CONFIG_ELOG_GSMI
180 /* Log S3, S4, and S5 entry */
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500181 if (slp_typ >= ACPI_S3)
182 elog_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700183#endif
184
Duncan Lauried775dda2014-10-25 01:49:32 -0700185 /* Clear pending GPE events */
186 clear_gpe_status();
187
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700188 /* Next, do the deed.
189 */
190
191 switch (slp_typ) {
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500192 case ACPI_S0:
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700193 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
194 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500195 case ACPI_S1:
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700196 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
197 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500198 case ACPI_S3:
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700199 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
200
201 /* Invalidate the cache before going to S3 */
202 wbinvd();
203 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500204 case ACPI_S4:
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700205 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
206 break;
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500207 case ACPI_S5:
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700208 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
209
Duncan Laurief3e0a132015-01-14 17:33:00 -0800210 /* Turn off backlight if needed */
211 backlight_off();
212
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700213 /* Disable all GPE */
214 disable_all_gpe();
215
216 /* Always set the flag in case CMOS was changed on runtime. For
217 * "KEEP", switch to "OFF" - KEEP is software emulated
218 */
219 reg8 = pci_read_config8(PCH_DEV_LPC, GEN_PMCON_3);
220 if (s5pwr == MAINBOARD_POWER_ON)
221 reg8 &= ~1;
222 else
223 reg8 |= 1;
224 pci_write_config8(PCH_DEV_LPC, GEN_PMCON_3, reg8);
225
226 /* also iterates over all bridges on bus 0 */
227 busmaster_disable_on_bus(0);
228 break;
229 default:
230 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
231 break;
232 }
233
234 /*
235 * Write back to the SLP register to cause the originally intended
236 * event again. We need to set BIT13 (SLP_EN) though to make the
237 * sleep happen.
238 */
239 enable_pm1_control(SLP_EN);
240
241 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbin9e6d1432016-07-13 23:21:41 -0500242 if (slp_typ >= ACPI_S3)
Patrick Georgi546953c2014-11-29 10:38:17 +0100243 halt();
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700244
245 /*
246 * In most sleep states, the code flow of this function ends at
247 * the line above. However, if we entered sleep state S1 and wake
248 * up again, we will continue to execute code in this function.
249 */
250 reg32 = inl(ACPI_BASE_ADDRESS + PM1_CNT);
251 if (reg32 & SCI_EN) {
252 /* The OS is not an ACPI OS, so we set the state to S0 */
253 disable_pm1_control(SLP_EN | SLP_TYP);
254 }
255}
256
257/*
258 * Look for Synchronous IO SMI and use save state from that
259 * core in case we are not running on the same core that
260 * initiated the IO transaction.
261 */
262static em64t101_smm_state_save_area_t *smi_apmc_find_state_save(u8 cmd)
263{
264 em64t101_smm_state_save_area_t *state;
265 int node;
266
267 /* Check all nodes looking for the one that issued the IO */
268 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
269 state = smm_get_save_state(node);
270
271 /* Check for Synchronous IO (bit0==1) */
272 if (!(state->io_misc_info & (1 << 0)))
273 continue;
274
275 /* Make sure it was a write (bit4==0) */
276 if (state->io_misc_info & (1 << 4))
277 continue;
278
279 /* Check for APMC IO port */
280 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
281 continue;
282
283 /* Check AX against the requested command */
284 if ((state->rax & 0xff) != cmd)
285 continue;
286
287 return state;
288 }
289
290 return NULL;
291}
292
293#if CONFIG_ELOG_GSMI
294static void southbridge_smi_gsmi(void)
295{
296 u32 *ret, *param;
297 u8 sub_command;
298 em64t101_smm_state_save_area_t *io_smi =
299 smi_apmc_find_state_save(ELOG_GSMI_APM_CNT);
300
301 if (!io_smi)
302 return;
303
304 /* Command and return value in EAX */
Lee Leahy26b7cd02017-03-16 18:47:55 -0700305 ret = (u32 *)&io_smi->rax;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700306 sub_command = (u8)(*ret >> 8);
307
308 /* Parameter buffer in EBX */
Lee Leahy26b7cd02017-03-16 18:47:55 -0700309 param = (u32 *)&io_smi->rbx;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700310
311 /* drivers/elog/gsmi.c */
312 *ret = gsmi_exec(sub_command, param);
313}
314#endif
315
316static void finalize(void)
317{
318 static int finalize_done;
319
320 if (finalize_done) {
321 printk(BIOS_DEBUG, "SMM already finalized.\n");
322 return;
323 }
324 finalize_done = 1;
325
326#if CONFIG_SPI_FLASH_SMM
327 /* Re-init SPI driver to handle locked BAR */
328 spi_init();
329#endif
330}
331
332static void southbridge_smi_apmc(void)
333{
334 u8 reg8;
335 em64t101_smm_state_save_area_t *state;
336
337 /* Emulate B2 register as the FADT / Linux expects it */
338
339 reg8 = inb(APM_CNT);
340 switch (reg8) {
341 case APM_CNT_CST_CONTROL:
342 printk(BIOS_DEBUG, "C-state control\n");
343 break;
344 case APM_CNT_PST_CONTROL:
345 printk(BIOS_DEBUG, "P-state control\n");
346 break;
347 case APM_CNT_ACPI_DISABLE:
348 disable_pm1_control(SCI_EN);
349 printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n");
350 break;
351 case APM_CNT_ACPI_ENABLE:
352 enable_pm1_control(SCI_EN);
353 printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n");
354 break;
355 case APM_CNT_FINALIZE:
356 finalize();
357 break;
358 case APM_CNT_GNVS_UPDATE:
359 if (smm_initialized) {
360 printk(BIOS_DEBUG,
361 "SMI#: SMM structures already initialized!\n");
362 return;
363 }
364 state = smi_apmc_find_state_save(reg8);
365 if (state) {
366 /* EBX in the state save contains the GNVS pointer */
367 gnvs = (global_nvs_t *)((u32)state->rbx);
368 smm_initialized = 1;
369 printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs);
370 }
371 break;
372#if CONFIG_ELOG_GSMI
373 case ELOG_GSMI_APM_CNT:
374 southbridge_smi_gsmi();
375 break;
376#endif
377 }
378
379 mainboard_smi_apmc(reg8);
380}
381
382static void southbridge_smi_pm1(void)
383{
384 u16 pm1_sts = clear_pm1_status();
385
386 /* While OSPM is not active, poweroff immediately
387 * on a power button event.
388 */
389 if (pm1_sts & PWRBTN_STS) {
390 /* power button pressed */
391#if CONFIG_ELOG_GSMI
392 elog_add_event(ELOG_TYPE_POWER_BUTTON);
393#endif
394 disable_pm1_control(-1UL);
395 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << 10));
396 }
397}
398
399static void southbridge_smi_gpe0(void)
400{
401 clear_gpe_status();
402}
403
404static void southbridge_smi_gpi(void)
405{
406 mainboard_smi_gpi(clear_alt_smi_status());
407
408 /* Clear again after mainboard handler */
409 clear_alt_smi_status();
410}
411
412static void southbridge_smi_mc(void)
413{
414 u32 reg32 = inl(ACPI_BASE_ADDRESS + SMI_EN);
415
416 /* Are microcontroller SMIs enabled? */
417 if ((reg32 & MCSMI_EN) == 0)
418 return;
419
420 printk(BIOS_DEBUG, "Microcontroller SMI.\n");
421}
422
423static void southbridge_smi_tco(void)
424{
425 u32 tco_sts = clear_tco_status();
426
427 /* Any TCO event? */
428 if (!tco_sts)
429 return;
430
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700431 // BIOSWR
432 if (tco_sts & (1 << 8)) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700433 u8 bios_cntl = pci_read_config16(PCH_DEV_LPC, BIOS_CNTL);
434
435 if (bios_cntl & 1) {
436 /*
437 * BWE is RW, so the SMI was caused by a
438 * write to BWE, not by a write to the BIOS
439 *
440 * This is the place where we notice someone
441 * is trying to tinker with the BIOS. We are
442 * trying to be nice and just ignore it. A more
443 * resolute answer would be to power down the
444 * box.
445 */
446 printk(BIOS_DEBUG, "Switching back to RO\n");
447 pci_write_config32(PCH_DEV_LPC, BIOS_CNTL,
448 (bios_cntl & ~1));
449 } /* No else for now? */
450 } else if (tco_sts & (1 << 3)) { /* TIMEOUT */
451 /* Handle TCO timeout */
452 printk(BIOS_DEBUG, "TCO Timeout.\n");
453 }
454}
455
456static void southbridge_smi_periodic(void)
457{
458 u32 reg32 = inl(ACPI_BASE_ADDRESS + SMI_EN);
459
460 /* Are periodic SMIs enabled? */
461 if ((reg32 & PERIODIC_EN) == 0)
462 return;
463
464 printk(BIOS_DEBUG, "Periodic SMI.\n");
465}
466
467static void southbridge_smi_monitor(void)
468{
469#define IOTRAP(x) (trap_sts & (1 << x))
470 u32 trap_sts, trap_cycle;
471 u32 data, mask = 0;
472 int i;
473
474 trap_sts = RCBA32(0x1e00); // TRSR - Trap Status Register
475 RCBA32(0x1e00) = trap_sts; // Clear trap(s) in TRSR
476
477 trap_cycle = RCBA32(0x1e10);
Lee Leahy26b7cd02017-03-16 18:47:55 -0700478 for (i = 16; i < 20; i++) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700479 if (trap_cycle & (1 << i))
480 mask |= (0xff << ((i - 16) << 2));
481 }
482
483
484 /* IOTRAP(3) SMI function call */
485 if (IOTRAP(3)) {
486 if (gnvs && gnvs->smif)
487 io_trap_handler(gnvs->smif); // call function smif
488 return;
489 }
490
491 /* IOTRAP(2) currently unused
492 * IOTRAP(1) currently unused */
493
494 /* IOTRAP(0) SMIC */
495 if (IOTRAP(0)) {
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700496 // It's a write
497 if (!(trap_cycle & (1 << 24))) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700498 printk(BIOS_DEBUG, "SMI1 command\n");
499 data = RCBA32(0x1e18);
500 data &= mask;
501 // if (smi1)
Lee Leahy26b7cd02017-03-16 18:47:55 -0700502 // southbridge_smi_command(data);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700503 // return;
504 }
505 // Fall through to debug
506 }
507
508 printk(BIOS_DEBUG, " trapped io address = 0x%x\n",
509 trap_cycle & 0xfffc);
Lee Leahy26b7cd02017-03-16 18:47:55 -0700510 for (i = 0; i < 4; i++)
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700511 if (IOTRAP(i))
512 printk(BIOS_DEBUG, " TRAP = %d\n", i);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700513 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}