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