blob: 9a5e5c056111bf4a4422ffcd57308073e7cc19f8 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <types.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05004#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02005#include <device/pci_ops.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05006#include <console/console.h>
7#include <cpu/x86/cache.h>
8#include <device/pci_def.h>
9#include <cpu/x86/smm.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +030010#include <cpu/intel/em64t101_save_state.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050011#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010012#include <halt.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020013#include <option.h>
Tristan Corrick63626b12018-11-30 22:53:50 +130014#include <southbridge/intel/common/finalize.h>
Tristan Corrick09fc6342018-11-30 22:53:01 +130015#include <northbridge/intel/haswell/haswell.h>
16#include <cpu/intel/haswell/haswell.h>
Matt DeVillier8187f112018-12-24 21:46:46 -060017#include <smmstore.h>
Tristan Corrick63626b12018-11-30 22:53:50 +130018#include "me.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include "pch.h"
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include "nvs.h"
21
Aaron Durbin76c37002012-10-30 09:03:43 -050022static u8 smm_initialized = 0;
23
Aaron Durbin76c37002012-10-30 09:03:43 -050024int southbridge_io_trap_handler(int smif)
25{
26 switch (smif) {
27 case 0x32:
28 printk(BIOS_DEBUG, "OS Init\n");
29 /* gnvs->smif:
30 * On success, the IO Trap Handler returns 0
31 * On failure, the IO Trap Handler returns a value != 0
32 */
33 gnvs->smif = 0;
34 return 1; /* IO trap handled */
35 }
36
37 /* Not handled */
38 return 0;
39}
40
41/**
42 * @brief Set the EOS bit
43 */
44void southbridge_smi_set_eos(void)
45{
Duncan Laurie467f31d2013-03-08 17:00:37 -080046 enable_smi(EOS);
Aaron Durbin76c37002012-10-30 09:03:43 -050047}
48
49static void busmaster_disable_on_bus(int bus)
50{
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020051 int slot, func;
52 unsigned int val;
53 unsigned char hdr;
Aaron Durbin76c37002012-10-30 09:03:43 -050054
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020055 for (slot = 0; slot < 0x20; slot++) {
56 for (func = 0; func < 8; func++) {
Elyes HAOUAS68c851b2018-06-12 22:06:09 +020057 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Aaron Durbin76c37002012-10-30 09:03:43 -050058
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020059 val = pci_read_config32(dev, PCI_VENDOR_ID);
Aaron Durbin76c37002012-10-30 09:03:43 -050060
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020061 if (val == 0xffffffff || val == 0x00000000 ||
62 val == 0x0000ffff || val == 0xffff0000)
63 continue;
Aaron Durbin76c37002012-10-30 09:03:43 -050064
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020065 /* Disable Bus Mastering for this one device */
Angel Ponsbf9bc502020-06-08 00:12:43 +020066 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER);
Aaron Durbin76c37002012-10-30 09:03:43 -050067
Elyes HAOUASba28e8d2016-08-31 19:22:16 +020068 /* If this is a bridge, then follow it. */
69 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
70 hdr &= 0x7f;
71 if (hdr == PCI_HEADER_TYPE_BRIDGE ||
72 hdr == PCI_HEADER_TYPE_CARDBUS) {
73 unsigned int buses;
74 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
75 busmaster_disable_on_bus((buses >> 8) & 0xff);
76 }
77 }
78 }
Aaron Durbin76c37002012-10-30 09:03:43 -050079}
80
Aaron Durbin29ffa542012-12-21 21:21:48 -060081static void southbridge_smi_sleep(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050082{
83 u8 reg8;
84 u32 reg32;
85 u8 slp_typ;
Nico Huber9faae2b2018-11-14 00:00:35 +010086 u8 s5pwr = CONFIG_MAINBOARD_POWER_FAILURE_STATE;
Duncan Laurie467f31d2013-03-08 17:00:37 -080087 u16 pmbase = get_pmbase();
Aaron Durbin76c37002012-10-30 09:03:43 -050088
Angel Pons2aaf7c02020-09-24 18:03:18 +020089 /* save and recover RTC port values */
Aaron Durbin76c37002012-10-30 09:03:43 -050090 u8 tmp70, tmp72;
91 tmp70 = inb(0x70);
92 tmp72 = inb(0x72);
93 get_option(&s5pwr, "power_on_after_fail");
94 outb(tmp70, 0x70);
95 outb(tmp72, 0x72);
96
Aaron Durbin76c37002012-10-30 09:03:43 -050097 /* First, disable further SMIs */
Duncan Laurie467f31d2013-03-08 17:00:37 -080098 disable_smi(SLP_SMI_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -050099
100 /* Figure out SLP_TYP */
101 reg32 = inl(pmbase + PM1_CNT);
102 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbinda5f5092016-07-13 23:23:16 -0500103 slp_typ = acpi_sleep_from_pm1(reg32);
Aaron Durbin76c37002012-10-30 09:03:43 -0500104
105 /* Do any mainboard sleep handling */
Aaron Durbinda5f5092016-07-13 23:23:16 -0500106 mainboard_smi_sleep(slp_typ);
Aaron Durbin76c37002012-10-30 09:03:43 -0500107
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700108 /* USB sleep preparations */
Julius Wernercd49cce2019-03-05 16:53:33 -0800109#if !CONFIG(FINALIZE_USB_ROUTE_XHCI)
Duncan Laurie1f529082013-07-30 15:53:45 -0700110 usb_ehci_sleep_prepare(PCH_EHCI1_DEV, slp_typ);
111 usb_ehci_sleep_prepare(PCH_EHCI2_DEV, slp_typ);
Duncan Laurie911cedf2013-07-30 16:05:55 -0700112#endif
Duncan Laurie1f529082013-07-30 15:53:45 -0700113 usb_xhci_sleep_prepare(PCH_XHCI_DEV, slp_typ);
Duncan Laurie2d9d39a2013-05-29 15:27:55 -0700114
Aaron Durbin76c37002012-10-30 09:03:43 -0500115 /* Log S3, S4, and S5 entry */
Aaron Durbinda5f5092016-07-13 23:23:16 -0500116 if (slp_typ >= ACPI_S3)
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200117 elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
Aaron Durbin76c37002012-10-30 09:03:43 -0500118
119 /* Next, do the deed.
120 */
121
122 switch (slp_typ) {
Aaron Durbinda5f5092016-07-13 23:23:16 -0500123 case ACPI_S0:
Duncan Laurie467f31d2013-03-08 17:00:37 -0800124 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
125 break;
Aaron Durbinda5f5092016-07-13 23:23:16 -0500126 case ACPI_S1:
Duncan Laurie467f31d2013-03-08 17:00:37 -0800127 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
128 break;
Aaron Durbinda5f5092016-07-13 23:23:16 -0500129 case ACPI_S3:
Aaron Durbin76c37002012-10-30 09:03:43 -0500130 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
131
Aaron Durbin76c37002012-10-30 09:03:43 -0500132 /* Invalidate the cache before going to S3 */
133 wbinvd();
134 break;
Aaron Durbinda5f5092016-07-13 23:23:16 -0500135 case ACPI_S4:
Duncan Laurie467f31d2013-03-08 17:00:37 -0800136 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
137 break;
Aaron Durbinda5f5092016-07-13 23:23:16 -0500138 case ACPI_S5:
Aaron Durbin76c37002012-10-30 09:03:43 -0500139 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
140
Duncan Laurie467f31d2013-03-08 17:00:37 -0800141 /* Disable all GPE */
142 disable_all_gpe();
Aaron Durbin76c37002012-10-30 09:03:43 -0500143
144 /* Always set the flag in case CMOS was changed on runtime. For
145 * "KEEP", switch to "OFF" - KEEP is software emulated
146 */
Aaron Durbin89f79a02012-10-31 23:05:25 -0500147 reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3);
Angel Pons2aaf7c02020-09-24 18:03:18 +0200148 if (s5pwr == MAINBOARD_POWER_ON)
Aaron Durbin76c37002012-10-30 09:03:43 -0500149 reg8 &= ~1;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200150 else
Aaron Durbin76c37002012-10-30 09:03:43 -0500151 reg8 |= 1;
Aaron Durbin89f79a02012-10-31 23:05:25 -0500152 pci_write_config8(PCI_DEV(0, 0x1f, 0), GEN_PMCON_3, reg8);
Aaron Durbin76c37002012-10-30 09:03:43 -0500153
154 /* also iterates over all bridges on bus 0 */
155 busmaster_disable_on_bus(0);
156 break;
Duncan Laurie467f31d2013-03-08 17:00:37 -0800157 default:
158 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
159 break;
Aaron Durbin76c37002012-10-30 09:03:43 -0500160 }
161
Angel Pons2aaf7c02020-09-24 18:03:18 +0200162 /*
163 * Write back to the SLP register to cause the originally intended
Aaron Durbin76c37002012-10-30 09:03:43 -0500164 * event again. We need to set BIT13 (SLP_EN) though to make the
165 * sleep happen.
166 */
Duncan Laurie467f31d2013-03-08 17:00:37 -0800167 enable_pm1_control(SLP_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -0500168
169 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbinda5f5092016-07-13 23:23:16 -0500170 if (slp_typ >= ACPI_S3)
Patrick Georgi546953c2014-11-29 10:38:17 +0100171 halt();
Aaron Durbin76c37002012-10-30 09:03:43 -0500172
Angel Pons2aaf7c02020-09-24 18:03:18 +0200173 /*
174 * In most sleep states, the code flow of this function ends at
Aaron Durbin76c37002012-10-30 09:03:43 -0500175 * the line above. However, if we entered sleep state S1 and wake
176 * up again, we will continue to execute code in this function.
177 */
178 reg32 = inl(pmbase + PM1_CNT);
179 if (reg32 & SCI_EN) {
180 /* The OS is not an ACPI OS, so we set the state to S0 */
Duncan Laurie467f31d2013-03-08 17:00:37 -0800181 disable_pm1_control(SLP_EN | SLP_TYP);
Aaron Durbin76c37002012-10-30 09:03:43 -0500182 }
183}
184
185/*
186 * Look for Synchronous IO SMI and use save state from that
187 * core in case we are not running on the same core that
188 * initiated the IO transaction.
189 */
Aaron Durbin76c37002012-10-30 09:03:43 -0500190static em64t101_smm_state_save_area_t *smi_apmc_find_state_save(u8 cmd)
191{
192 em64t101_smm_state_save_area_t *state;
Aaron Durbin76c37002012-10-30 09:03:43 -0500193 int node;
194
195 /* Check all nodes looking for the one that issued the IO */
196 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
Aaron Durbin29ffa542012-12-21 21:21:48 -0600197 state = smm_get_save_state(node);
Aaron Durbin76c37002012-10-30 09:03:43 -0500198
Elyes HAOUAS581fe582018-04-26 09:57:07 +0200199 /* Check for Synchronous IO (bit0 == 1) */
Aaron Durbin76c37002012-10-30 09:03:43 -0500200 if (!(state->io_misc_info & (1 << 0)))
201 continue;
202
Elyes HAOUAS581fe582018-04-26 09:57:07 +0200203 /* Make sure it was a write (bit4 == 0) */
Aaron Durbin76c37002012-10-30 09:03:43 -0500204 if (state->io_misc_info & (1 << 4))
205 continue;
206
207 /* Check for APMC IO port */
208 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
209 continue;
210
211 /* Check AX against the requested command */
212 if ((state->rax & 0xff) != cmd)
213 continue;
214
215 return state;
216 }
217
218 return NULL;
219}
220
Aaron Durbin76c37002012-10-30 09:03:43 -0500221static void southbridge_smi_gsmi(void)
222{
223 u32 *ret, *param;
224 u8 sub_command;
225 em64t101_smm_state_save_area_t *io_smi =
Patrick Georgid61839c2018-12-03 16:10:33 +0100226 smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
Aaron Durbin76c37002012-10-30 09:03:43 -0500227
228 if (!io_smi)
229 return;
230
231 /* Command and return value in EAX */
Angel Pons2aaf7c02020-09-24 18:03:18 +0200232 ret = (u32 *)&io_smi->rax;
Aaron Durbin76c37002012-10-30 09:03:43 -0500233 sub_command = (u8)(*ret >> 8);
234
235 /* Parameter buffer in EBX */
Angel Pons2aaf7c02020-09-24 18:03:18 +0200236 param = (u32 *)&io_smi->rbx;
Aaron Durbin76c37002012-10-30 09:03:43 -0500237
238 /* drivers/elog/gsmi.c */
239 *ret = gsmi_exec(sub_command, param);
240}
Aaron Durbin76c37002012-10-30 09:03:43 -0500241
Matt DeVillier8187f112018-12-24 21:46:46 -0600242static void southbridge_smi_store(void)
243{
244 u8 sub_command, ret;
245 em64t101_smm_state_save_area_t *io_smi =
246 smi_apmc_find_state_save(APM_CNT_SMMSTORE);
247 uint32_t reg_ebx;
248
249 if (!io_smi)
250 return;
251 /* Command and return value in EAX */
252 sub_command = (io_smi->rax >> 8) & 0xff;
253
254 /* Parameter buffer in EBX */
255 reg_ebx = io_smi->rbx;
256
257 /* drivers/smmstore/smi.c */
258 ret = smmstore_exec(sub_command, (void *)reg_ebx);
259 io_smi->rax = ret;
260}
261
Aaron Durbin29ffa542012-12-21 21:21:48 -0600262static void southbridge_smi_apmc(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500263{
Aaron Durbin76c37002012-10-30 09:03:43 -0500264 u8 reg8;
Aaron Durbin76c37002012-10-30 09:03:43 -0500265 em64t101_smm_state_save_area_t *state;
Tristan Corrick09fc6342018-11-30 22:53:01 +1300266 static int chipset_finalized = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -0500267
268 /* Emulate B2 register as the FADT / Linux expects it */
269
270 reg8 = inb(APM_CNT);
271 switch (reg8) {
Tristan Corrick09fc6342018-11-30 22:53:01 +1300272 case APM_CNT_FINALIZE:
273 if (chipset_finalized) {
274 printk(BIOS_DEBUG, "SMI#: Already finalized\n");
275 return;
276 }
277
Tristan Corrick63626b12018-11-30 22:53:50 +1300278 intel_me_finalize_smm();
Tristan Corrick09fc6342018-11-30 22:53:01 +1300279 intel_pch_finalize_smm();
280 intel_northbridge_haswell_finalize_smm();
281 intel_cpu_haswell_finalize_smm();
282
283 chipset_finalized = 1;
284 break;
Aaron Durbin76c37002012-10-30 09:03:43 -0500285 case APM_CNT_CST_CONTROL:
286 /* Calling this function seems to cause
287 * some kind of race condition in Linux
288 * and causes a kernel oops
289 */
290 printk(BIOS_DEBUG, "C-state control\n");
291 break;
292 case APM_CNT_PST_CONTROL:
293 /* Calling this function seems to cause
294 * some kind of race condition in Linux
295 * and causes a kernel oops
296 */
297 printk(BIOS_DEBUG, "P-state control\n");
298 break;
299 case APM_CNT_ACPI_DISABLE:
Duncan Laurie467f31d2013-03-08 17:00:37 -0800300 disable_pm1_control(SCI_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -0500301 printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n");
302 break;
303 case APM_CNT_ACPI_ENABLE:
Duncan Laurie467f31d2013-03-08 17:00:37 -0800304 enable_pm1_control(SCI_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -0500305 printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n");
306 break;
307 case APM_CNT_GNVS_UPDATE:
308 if (smm_initialized) {
Duncan Laurie467f31d2013-03-08 17:00:37 -0800309 printk(BIOS_DEBUG,
310 "SMI#: SMM structures already initialized!\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500311 return;
312 }
313 state = smi_apmc_find_state_save(reg8);
314 if (state) {
315 /* EBX in the state save contains the GNVS pointer */
Kyösti Mälkki0c1dd9c2020-06-17 23:37:49 +0300316 gnvs = (struct global_nvs *)((u32)state->rbx);
Patrick Rudolph9f8f1152020-05-06 11:58:45 +0200317 if (smm_points_to_smram(gnvs, sizeof(*gnvs))) {
318 printk(BIOS_ERR, "SMI#: ERROR: GNVS overlaps SMM\n");
319 return;
320 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500321 smm_initialized = 1;
322 printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs);
323 }
324 break;
Kyösti Mälkkib486f292020-06-18 14:05:35 +0300325 case APM_CNT_ROUTE_ALL_XHCI:
Duncan Laurie911cedf2013-07-30 16:05:55 -0700326 usb_xhci_route_all();
Duncan Laurie911cedf2013-07-30 16:05:55 -0700327 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100328 case APM_CNT_ELOG_GSMI:
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200329 if (CONFIG(ELOG_GSMI))
330 southbridge_smi_gsmi();
Aaron Durbin76c37002012-10-30 09:03:43 -0500331 break;
Matt DeVillier8187f112018-12-24 21:46:46 -0600332 case APM_CNT_SMMSTORE:
333 if (CONFIG(SMMSTORE))
334 southbridge_smi_store();
335 break;
Aaron Durbin76c37002012-10-30 09:03:43 -0500336 }
337
Aaron Durbin29ffa542012-12-21 21:21:48 -0600338 mainboard_smi_apmc(reg8);
Aaron Durbin76c37002012-10-30 09:03:43 -0500339}
340
Aaron Durbin29ffa542012-12-21 21:21:48 -0600341static void southbridge_smi_pm1(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500342{
Duncan Laurie467f31d2013-03-08 17:00:37 -0800343 u16 pm1_sts = clear_pm1_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500344
345 /* While OSPM is not active, poweroff immediately
346 * on a power button event.
347 */
348 if (pm1_sts & PWRBTN_STS) {
Angel Pons2aaf7c02020-09-24 18:03:18 +0200349 /* power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200350 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Duncan Laurie467f31d2013-03-08 17:00:37 -0800351 disable_pm1_control(-1UL);
352 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << 10));
Aaron Durbin76c37002012-10-30 09:03:43 -0500353 }
354}
355
Aaron Durbin29ffa542012-12-21 21:21:48 -0600356static void southbridge_smi_gpe0(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500357{
Duncan Laurie467f31d2013-03-08 17:00:37 -0800358 clear_gpe_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500359}
360
Aaron Durbin29ffa542012-12-21 21:21:48 -0600361static void southbridge_smi_gpi(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500362{
Duncan Laurie467f31d2013-03-08 17:00:37 -0800363 mainboard_smi_gpi(clear_alt_smi_status());
Aaron Durbin76c37002012-10-30 09:03:43 -0500364
Duncan Laurie467f31d2013-03-08 17:00:37 -0800365 /* Clear again after mainboard handler */
366 clear_alt_smi_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500367}
368
Aaron Durbin29ffa542012-12-21 21:21:48 -0600369static void southbridge_smi_mc(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500370{
Angel Pons2aaf7c02020-09-24 18:03:18 +0200371 u32 reg32 = inl(get_pmbase() + SMI_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -0500372
Duncan Laurie467f31d2013-03-08 17:00:37 -0800373 /* Are microcontroller SMIs enabled? */
Aaron Durbin76c37002012-10-30 09:03:43 -0500374 if ((reg32 & MCSMI_EN) == 0)
375 return;
376
377 printk(BIOS_DEBUG, "Microcontroller SMI.\n");
378}
379
Aaron Durbin29ffa542012-12-21 21:21:48 -0600380static void southbridge_smi_tco(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500381{
Duncan Laurie467f31d2013-03-08 17:00:37 -0800382 u32 tco_sts = clear_tco_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500383
384 /* Any TCO event? */
385 if (!tco_sts)
386 return;
387
Angel Pons2aaf7c02020-09-24 18:03:18 +0200388 // BIOSWR
389 if (tco_sts & (1 << 8)) {
390 u8 bios_cntl = pci_read_config16(PCI_DEV(0, 0x1f, 0), 0xdc);
Aaron Durbin76c37002012-10-30 09:03:43 -0500391
392 if (bios_cntl & 1) {
Angel Pons2aaf7c02020-09-24 18:03:18 +0200393 /*
394 * BWE is RW, so the SMI was caused by a
Aaron Durbin76c37002012-10-30 09:03:43 -0500395 * write to BWE, not by a write to the BIOS
Angel Pons2aaf7c02020-09-24 18:03:18 +0200396 *
397 * This is the place where we notice someone
Aaron Durbin76c37002012-10-30 09:03:43 -0500398 * is trying to tinker with the BIOS. We are
399 * trying to be nice and just ignore it. A more
400 * resolute answer would be to power down the
401 * box.
402 */
403 printk(BIOS_DEBUG, "Switching back to RO\n");
Angel Ponsbf9bc502020-06-08 00:12:43 +0200404 pci_write_config32(PCI_DEV(0, 0x1f, 0), 0xdc, (bios_cntl & ~1));
Aaron Durbin76c37002012-10-30 09:03:43 -0500405 } /* No else for now? */
406 } else if (tco_sts & (1 << 3)) { /* TIMEOUT */
407 /* Handle TCO timeout */
408 printk(BIOS_DEBUG, "TCO Timeout.\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500409 }
410}
411
Aaron Durbin29ffa542012-12-21 21:21:48 -0600412static void southbridge_smi_periodic(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500413{
Angel Pons2aaf7c02020-09-24 18:03:18 +0200414 u32 reg32 = inl(get_pmbase() + SMI_EN);
Aaron Durbin76c37002012-10-30 09:03:43 -0500415
416 /* Are periodic SMIs enabled? */
417 if ((reg32 & PERIODIC_EN) == 0)
418 return;
419
420 printk(BIOS_DEBUG, "Periodic SMI.\n");
421}
422
Aaron Durbin29ffa542012-12-21 21:21:48 -0600423static void southbridge_smi_monitor(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500424{
425#define IOTRAP(x) (trap_sts & (1 << x))
426 u32 trap_sts, trap_cycle;
Elyes HAOUAS2a66dd22020-08-07 15:22:12 +0200427 u32 mask = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -0500428 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);
Angel Pons2aaf7c02020-09-24 18:03:18 +0200434 for (i = 16; i < 20; i++) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500435 if (trap_cycle & (1 << i))
436 mask |= (0xff << ((i - 16) << 2));
437 }
438
Aaron Durbin76c37002012-10-30 09:03:43 -0500439 /* IOTRAP(3) SMI function call */
440 if (IOTRAP(3)) {
441 if (gnvs && gnvs->smif)
442 io_trap_handler(gnvs->smif); // call function smif
443 return;
444 }
445
446 /* IOTRAP(2) currently unused
447 * IOTRAP(1) currently unused */
448
449 /* IOTRAP(0) SMIC */
450 if (IOTRAP(0)) {
Angel Pons2aaf7c02020-09-24 18:03:18 +0200451 // It's a write
452 if (!(trap_cycle & (1 << 24))) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500453 printk(BIOS_DEBUG, "SMI1 command\n");
Elyes HAOUAS2a66dd22020-08-07 15:22:12 +0200454 (void)RCBA32(0x1e18);
455 // data = RCBA32(0x1e18);
456 // data &= mask;
Aaron Durbin76c37002012-10-30 09:03:43 -0500457 // if (smi1)
Elyes HAOUASb0f19882018-06-09 11:59:00 +0200458 // southbridge_smi_command(data);
Aaron Durbin76c37002012-10-30 09:03:43 -0500459 // return;
460 }
461 // Fall through to debug
462 }
463
Duncan Laurie467f31d2013-03-08 17:00:37 -0800464 printk(BIOS_DEBUG, " trapped io address = 0x%x\n",
465 trap_cycle & 0xfffc);
Angel Pons2aaf7c02020-09-24 18:03:18 +0200466 for (i = 0; i < 4; i++)
467 if (IOTRAP(i))
468 printk(BIOS_DEBUG, " TRAP = %d\n", i);
Aaron Durbin76c37002012-10-30 09:03:43 -0500469 printk(BIOS_DEBUG, " AHBE = %x\n", (trap_cycle >> 16) & 0xf);
470 printk(BIOS_DEBUG, " MASK = 0x%08x\n", mask);
Duncan Laurie467f31d2013-03-08 17:00:37 -0800471 printk(BIOS_DEBUG, " read/write: %s\n",
472 (trap_cycle & (1 << 24)) ? "read" : "write");
Aaron Durbin76c37002012-10-30 09:03:43 -0500473
474 if (!(trap_cycle & (1 << 24))) {
475 /* Write Cycle */
Elyes HAOUAS2a66dd22020-08-07 15:22:12 +0200476 printk(BIOS_DEBUG, " iotrap written data = 0x%08x\n", RCBA32(0x1e18));
Aaron Durbin76c37002012-10-30 09:03:43 -0500477 }
478#undef IOTRAP
479}
480
Aaron Durbin29ffa542012-12-21 21:21:48 -0600481typedef void (*smi_handler_t)(void);
Aaron Durbin76c37002012-10-30 09:03:43 -0500482
483static smi_handler_t southbridge_smi[32] = {
484 NULL, // [0] reserved
485 NULL, // [1] reserved
486 NULL, // [2] BIOS_STS
487 NULL, // [3] LEGACY_USB_STS
488 southbridge_smi_sleep, // [4] SLP_SMI_STS
489 southbridge_smi_apmc, // [5] APM_STS
490 NULL, // [6] SWSMI_TMR_STS
491 NULL, // [7] reserved
492 southbridge_smi_pm1, // [8] PM1_STS
493 southbridge_smi_gpe0, // [9] GPE0_STS
494 southbridge_smi_gpi, // [10] GPI_STS
495 southbridge_smi_mc, // [11] MCSMI_STS
496 NULL, // [12] DEVMON_STS
497 southbridge_smi_tco, // [13] TCO_STS
498 southbridge_smi_periodic, // [14] PERIODIC_STS
499 NULL, // [15] SERIRQ_SMI_STS
500 NULL, // [16] SMBUS_SMI_STS
501 NULL, // [17] LEGACY_USB2_STS
502 NULL, // [18] INTEL_USB2_STS
503 NULL, // [19] reserved
504 NULL, // [20] PCI_EXP_SMI_STS
505 southbridge_smi_monitor, // [21] MONITOR_STS
506 NULL, // [22] reserved
507 NULL, // [23] reserved
508 NULL, // [24] reserved
509 NULL, // [25] EL_SMI_STS
510 NULL, // [26] SPI_STS
511 NULL, // [27] reserved
512 NULL, // [28] reserved
513 NULL, // [29] reserved
514 NULL, // [30] reserved
515 NULL // [31] reserved
516};
517
518/**
519 * @brief Interrupt handler for SMI#
Aaron Durbin76c37002012-10-30 09:03:43 -0500520 */
Aaron Durbin29ffa542012-12-21 21:21:48 -0600521void southbridge_smi_handler(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500522{
Duncan Laurie467f31d2013-03-08 17:00:37 -0800523 int i;
Aaron Durbin76c37002012-10-30 09:03:43 -0500524 u32 smi_sts;
525
Aaron Durbin76c37002012-10-30 09:03:43 -0500526 /* We need to clear the SMI status registers, or we won't see what's
527 * happening in the following calls.
528 */
Duncan Laurie467f31d2013-03-08 17:00:37 -0800529 smi_sts = clear_smi_status();
Aaron Durbin76c37002012-10-30 09:03:43 -0500530
531 /* Call SMI sub handler for each of the status bits */
532 for (i = 0; i < 31; i++) {
533 if (smi_sts & (1 << i)) {
534 if (southbridge_smi[i]) {
Aaron Durbin29ffa542012-12-21 21:21:48 -0600535 southbridge_smi[i]();
Aaron Durbin76c37002012-10-30 09:03:43 -0500536 } else {
Duncan Laurie467f31d2013-03-08 17:00:37 -0800537 printk(BIOS_DEBUG,
Martin Roth2ed0aa22016-01-05 20:58:58 -0700538 "SMI_STS[%d] occurred, but no "
Duncan Laurie467f31d2013-03-08 17:00:37 -0800539 "handler available.\n", i);
Aaron Durbin76c37002012-10-30 09:03:43 -0500540 }
541 }
542 }
Aaron Durbin76c37002012-10-30 09:03:43 -0500543}