Debian Codegraph @

src/southbridge/intel/lynxpoint/smihandler.c

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