blob: 20e15902b10b17f488b8ddc426c2eed20192ea89 [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin7837be62013-10-21 22:32:00 -05002
3#include <stdint.h>
Kyösti Mälkki4abc7312021-01-12 17:46:30 +02004#include <acpi/acpi_gnvs.h>
Aaron Durbin7837be62013-10-21 22:32:00 -05005#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Aaron Durbin7837be62013-10-21 22:32:00 -05007#include <console/console.h>
8#include <cpu/x86/cache.h>
9#include <cpu/x86/smm.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +030010#include <cpu/intel/em64t100_save_state.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050011#include <device/pci_def.h>
12#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010013#include <halt.h>
Aaron Durbin20885712014-02-09 16:04:06 -060014#include <spi-generic.h>
Matt DeVillierbd6bdc52018-12-25 21:54:52 -060015#include <smmstore.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050016
Marc Jones9afc5c02014-09-24 10:53:48 -060017#include <soc/iosf.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070018#include <soc/pci_devs.h>
Angel Ponsb5320b22020-07-07 18:27:30 +020019#include <soc/pm.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070020#include <soc/nvs.h>
Kyösti Mälkki4abc7312021-01-12 17:46:30 +020021#include <soc/device_nvs.h>
22
Aaron Durbin7837be62013-10-21 22:32:00 -050023int southbridge_io_trap_handler(int smif)
24{
25 switch (smif) {
26 case 0x32:
27 printk(BIOS_DEBUG, "OS Init\n");
Angel Pons26b49cc2020-07-07 17:17:51 +020028 /*
29 * gnvs->smif:
Aaron Durbin7837be62013-10-21 22:32:00 -050030 * 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
41void southbridge_smi_set_eos(void)
42{
43 enable_smi(EOS);
44}
45
Aaron Durbin7837be62013-10-21 22:32:00 -050046static void busmaster_disable_on_bus(int bus)
47{
48 int slot, func;
49 unsigned int val;
50 unsigned char hdr;
51
52 for (slot = 0; slot < 0x20; slot++) {
53 for (func = 0; func < 8; func++) {
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +020054 u16 reg16;
Elyes HAOUASc8a649c2018-06-10 23:36:44 +020055 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Aaron Durbin7837be62013-10-21 22:32:00 -050056
57 val = pci_read_config32(dev, PCI_VENDOR_ID);
58
59 if (val == 0xffffffff || val == 0x00000000 ||
60 val == 0x0000ffff || val == 0xffff0000)
61 continue;
62
63 /* Disable Bus Mastering for this one device */
Elyes HAOUAS6468d87d2020-04-29 10:22:42 +020064 reg16 = pci_read_config16(dev, PCI_COMMAND);
65 reg16 &= ~PCI_COMMAND_MASTER;
66 pci_write_config16(dev, PCI_COMMAND, reg16);
Aaron Durbin7837be62013-10-21 22:32:00 -050067
68 /* If this is a bridge, then follow it. */
69 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
70 hdr &= 0x7f;
Angel Pons5bcd35d2020-07-07 18:15:47 +020071 if (hdr == PCI_HEADER_TYPE_BRIDGE || hdr == PCI_HEADER_TYPE_CARDBUS) {
Aaron Durbin7837be62013-10-21 22:32:00 -050072 unsigned int buses;
73 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
74 busmaster_disable_on_bus((buses >> 8) & 0xff);
75 }
76 }
77 }
78}
79
80static void southbridge_smi_sleep(void)
81{
82 uint32_t reg32;
83 uint8_t slp_typ;
84 uint16_t pmbase = get_pmbase();
85
86 /* First, disable further SMIs */
87 disable_smi(SLP_SMI_EN);
88
89 /* Figure out SLP_TYP */
90 reg32 = inl(pmbase + PM1_CNT);
91 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050092 slp_typ = acpi_sleep_from_pm1(reg32);
Aaron Durbin7837be62013-10-21 22:32:00 -050093
94 /* Do any mainboard sleep handling */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050095 mainboard_smi_sleep(slp_typ);
Aaron Durbin7837be62013-10-21 22:32:00 -050096
Aaron Durbin7837be62013-10-21 22:32:00 -050097 /* Log S3, S4, and S5 entry */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050098 if (slp_typ >= ACPI_S3)
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +020099 elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
Aaron Durbin7837be62013-10-21 22:32:00 -0500100
Angel Pons26b49cc2020-07-07 17:17:51 +0200101 /* Next, do the deed. */
Aaron Durbin7837be62013-10-21 22:32:00 -0500102 switch (slp_typ) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500103 case ACPI_S0:
Aaron Durbin7837be62013-10-21 22:32:00 -0500104 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
105 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500106 case ACPI_S1:
Aaron Durbin7837be62013-10-21 22:32:00 -0500107 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
108 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500109 case ACPI_S3:
Aaron Durbin7837be62013-10-21 22:32:00 -0500110 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
111
112 /* Invalidate the cache before going to S3 */
113 wbinvd();
114 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500115 case ACPI_S4:
Aaron Durbin7837be62013-10-21 22:32:00 -0500116 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
117 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500118 case ACPI_S5:
Aaron Durbin7837be62013-10-21 22:32:00 -0500119 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
120
121 /* Disable all GPE */
122 disable_all_gpe();
123
Angel Pons26b49cc2020-07-07 17:17:51 +0200124 /* Also iterates over all bridges on bus 0 */
Aaron Durbin7837be62013-10-21 22:32:00 -0500125 busmaster_disable_on_bus(0);
126 break;
127 default:
128 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
129 break;
130 }
131
Angel Pons26b49cc2020-07-07 17:17:51 +0200132 /*
133 * Write back to the SLP register to cause the originally intended event again.
134 * We need to set BIT13 (SLP_EN) though to make the sleep happen.
Aaron Durbin7837be62013-10-21 22:32:00 -0500135 */
136 enable_pm1_control(SLP_EN);
137
138 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500139 if (slp_typ >= ACPI_S3)
Patrick Georgi546953c2014-11-29 10:38:17 +0100140 halt();
Aaron Durbin7837be62013-10-21 22:32:00 -0500141
Angel Pons26b49cc2020-07-07 17:17:51 +0200142 /*
143 * In most sleep states, the code flow of this function ends at
Aaron Durbin7837be62013-10-21 22:32:00 -0500144 * the line above. However, if we entered sleep state S1 and wake
145 * up again, we will continue to execute code in this function.
146 */
147 reg32 = inl(pmbase + PM1_CNT);
148 if (reg32 & SCI_EN) {
149 /* The OS is not an ACPI OS, so we set the state to S0 */
150 disable_pm1_control(SLP_EN | SLP_TYP);
151 }
152}
153
154/*
Angel Pons26b49cc2020-07-07 17:17:51 +0200155 * Look for Synchronous IO SMI and use save state from that core in case
156 * we are not running on the same core that initiated the IO transaction.
Aaron Durbin7837be62013-10-21 22:32:00 -0500157 */
158static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
159{
160 em64t100_smm_state_save_area_t *state;
161 int node;
162
163 /* Check all nodes looking for the one that issued the IO */
164 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
165 state = smm_get_save_state(node);
166
167 /* Check for Synchronous IO (bit0==1) */
168 if (!(state->io_misc_info & (1 << 0)))
169 continue;
170
171 /* Make sure it was a write (bit4==0) */
172 if (state->io_misc_info & (1 << 4))
173 continue;
174
175 /* Check for APMC IO port */
176 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
177 continue;
178
179 /* Check AX against the requested command */
180 if ((state->rax & 0xff) != cmd)
181 continue;
182
183 return state;
184 }
185
186 return NULL;
187}
188
Aaron Durbin7837be62013-10-21 22:32:00 -0500189static void southbridge_smi_gsmi(void)
190{
191 u32 *ret, *param;
192 uint8_t sub_command;
Angel Pons5bcd35d2020-07-07 18:15:47 +0200193 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
Aaron Durbin7837be62013-10-21 22:32:00 -0500194
195 if (!io_smi)
196 return;
197
198 /* Command and return value in EAX */
Angel Pons5bcd35d2020-07-07 18:15:47 +0200199 ret = (u32 *)&io_smi->rax;
Aaron Durbin7837be62013-10-21 22:32:00 -0500200 sub_command = (uint8_t)(*ret >> 8);
201
202 /* Parameter buffer in EBX */
Angel Pons5bcd35d2020-07-07 18:15:47 +0200203 param = (u32 *)&io_smi->rbx;
Aaron Durbin7837be62013-10-21 22:32:00 -0500204
205 /* drivers/elog/gsmi.c */
206 *ret = gsmi_exec(sub_command, param);
207}
Aaron Durbin20885712014-02-09 16:04:06 -0600208
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200209void *acpi_get_device_nvs(void)
210{
Kyösti Mälkki4bd91872021-03-16 19:02:26 +0200211 return (u8 *)gnvs + ALIGN_UP(sizeof(struct global_nvs), sizeof(uint64_t));
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200212}
213
Marc Jones9afc5c02014-09-24 10:53:48 -0600214/*
215 * soc_legacy: A payload (Depthcharge) has indicated that the
216 * legacy payload (SeaBIOS) is being loaded. Switch devices that are
217 * in ACPI mode to PCI mode so that non-ACPI drivers may work.
218 *
219 */
220static void soc_legacy(void)
221{
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200222 struct device_nvs *dev_nvs = acpi_get_device_nvs();
Marc Jones9afc5c02014-09-24 10:53:48 -0600223 u32 reg32;
224
225 /* LPE Device */
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200226 if (dev_nvs->lpe_en) {
Marc Jones9afc5c02014-09-24 10:53:48 -0600227 reg32 = iosf_port58_read(LPE_PCICFGCTR1);
228 reg32 &=
229 ~(LPE_PCICFGCTR1_PCI_CFG_DIS | LPE_PCICFGCTR1_ACPI_INT_EN);
230 iosf_port58_write(LPE_PCICFGCTR1, reg32);
231 }
232
233 /* SCC Devices */
234#define SCC_ACPI_MODE_DISABLE(name_) \
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200235 do { if (dev_nvs->scc_en[SCC_NVS_ ## name_]) { \
Marc Jones9afc5c02014-09-24 10:53:48 -0600236 reg32 = iosf_scc_read(SCC_ ## name_ ## _CTL); \
237 reg32 &= ~(SCC_CTL_PCI_CFG_DIS | SCC_CTL_ACPI_INT_EN); \
238 iosf_scc_write(SCC_ ## name_ ## _CTL, reg32); \
239 } } while (0)
240
241 SCC_ACPI_MODE_DISABLE(MMC);
242 SCC_ACPI_MODE_DISABLE(SD);
243 SCC_ACPI_MODE_DISABLE(SDIO);
244
245 /* LPSS Devices */
246#define LPSS_ACPI_MODE_DISABLE(name_) \
Kyösti Mälkki4abc7312021-01-12 17:46:30 +0200247 do { if (dev_nvs->lpss_en[LPSS_NVS_ ## name_]) { \
Marc Jones9afc5c02014-09-24 10:53:48 -0600248 reg32 = iosf_lpss_read(LPSS_ ## name_ ## _CTL); \
249 reg32 &= ~LPSS_CTL_PCI_CFG_DIS | ~LPSS_CTL_ACPI_INT_EN; \
250 iosf_lpss_write(LPSS_ ## name_ ## _CTL, reg32); \
251 } } while (0)
252
253 LPSS_ACPI_MODE_DISABLE(SIO_DMA1);
254 LPSS_ACPI_MODE_DISABLE(I2C1);
255 LPSS_ACPI_MODE_DISABLE(I2C2);
256 LPSS_ACPI_MODE_DISABLE(I2C3);
257 LPSS_ACPI_MODE_DISABLE(I2C4);
258 LPSS_ACPI_MODE_DISABLE(I2C5);
259 LPSS_ACPI_MODE_DISABLE(I2C6);
260 LPSS_ACPI_MODE_DISABLE(I2C7);
261 LPSS_ACPI_MODE_DISABLE(SIO_DMA2);
262 LPSS_ACPI_MODE_DISABLE(PWM1);
263 LPSS_ACPI_MODE_DISABLE(PWM2);
264 LPSS_ACPI_MODE_DISABLE(HSUART1);
265 LPSS_ACPI_MODE_DISABLE(HSUART2);
266 LPSS_ACPI_MODE_DISABLE(SPI);
267}
268
Matt DeVillierbd6bdc52018-12-25 21:54:52 -0600269static void southbridge_smi_store(void)
270{
271 u8 sub_command, ret;
Angel Pons5bcd35d2020-07-07 18:15:47 +0200272 em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_SMMSTORE);
Matt DeVillierbd6bdc52018-12-25 21:54:52 -0600273 uint32_t reg_ebx;
274
275 if (!io_smi)
276 return;
277 /* Command and return value in EAX */
278 sub_command = (io_smi->rax >> 8) & 0xff;
279
280 /* Parameter buffer in EBX */
281 reg_ebx = io_smi->rbx;
282
283 /* drivers/smmstore/smi.c */
284 ret = smmstore_exec(sub_command, (void *)reg_ebx);
285 io_smi->rax = ret;
286}
287
Aaron Durbin7837be62013-10-21 22:32:00 -0500288static void southbridge_smi_apmc(void)
289{
290 uint8_t reg8;
Aaron Durbin7837be62013-10-21 22:32:00 -0500291
Kyösti Mälkki9a1620f2021-01-08 13:27:33 +0200292 reg8 = apm_get_apmc();
Aaron Durbin7837be62013-10-21 22:32:00 -0500293 switch (reg8) {
Aaron Durbin7837be62013-10-21 22:32:00 -0500294 case APM_CNT_ACPI_DISABLE:
295 disable_pm1_control(SCI_EN);
Aaron Durbin7837be62013-10-21 22:32:00 -0500296 break;
297 case APM_CNT_ACPI_ENABLE:
298 enable_pm1_control(SCI_EN);
Aaron Durbin7837be62013-10-21 22:32:00 -0500299 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100300 case APM_CNT_ELOG_GSMI:
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200301 if (CONFIG(ELOG_GSMI))
302 southbridge_smi_gsmi();
Aaron Durbin7837be62013-10-21 22:32:00 -0500303 break;
Marc Jones9afc5c02014-09-24 10:53:48 -0600304 case APM_CNT_LEGACY:
305 soc_legacy();
306 break;
Matt DeVillierbd6bdc52018-12-25 21:54:52 -0600307 case APM_CNT_SMMSTORE:
308 if (CONFIG(SMMSTORE))
309 southbridge_smi_store();
310 break;
Aaron Durbin7837be62013-10-21 22:32:00 -0500311 }
312
313 mainboard_smi_apmc(reg8);
314}
315
316static void southbridge_smi_pm1(void)
317{
318 uint16_t pm1_sts = clear_pm1_status();
319
Angel Pons26b49cc2020-07-07 17:17:51 +0200320 /* While OSPM is not active, poweroff immediately on a power button event */
Aaron Durbin7837be62013-10-21 22:32:00 -0500321 if (pm1_sts & PWRBTN_STS) {
Angel Pons26b49cc2020-07-07 17:17:51 +0200322 /* Power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200323 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Aaron Durbin7837be62013-10-21 22:32:00 -0500324 disable_pm1_control(-1UL);
Aaron Durbin9f83e872013-11-11 14:45:27 -0600325 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
Aaron Durbin7837be62013-10-21 22:32:00 -0500326 }
327}
328
329static void southbridge_smi_gpe0(void)
330{
331 clear_gpe_status();
332}
333
334static void southbridge_smi_tco(void)
335{
336 uint32_t tco_sts = clear_tco_status();
337
338 /* Any TCO event? */
339 if (!tco_sts)
340 return;
341
342 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
343 /* Handle TCO timeout */
344 printk(BIOS_DEBUG, "TCO Timeout.\n");
345 }
346}
347
348static void southbridge_smi_periodic(void)
349{
350 uint32_t reg32;
351
352 reg32 = inl(get_pmbase() + SMI_EN);
353
354 /* Are periodic SMIs enabled? */
355 if ((reg32 & PERIODIC_EN) == 0)
356 return;
357
358 printk(BIOS_DEBUG, "Periodic SMI.\n");
359}
360
361typedef void (*smi_handler_t)(void);
362
363static const smi_handler_t southbridge_smi[32] = {
Angel Pons26b49cc2020-07-07 17:17:51 +0200364 NULL, /* [0] reserved */
365 NULL, /* [1] reserved */
366 NULL, /* [2] BIOS_STS */
367 NULL, /* [3] LEGACY_USB_STS */
368 southbridge_smi_sleep, /* [4] SLP_SMI_STS */
369 southbridge_smi_apmc, /* [5] APM_STS */
370 NULL, /* [6] SWSMI_TMR_STS */
371 NULL, /* [7] reserved */
372 southbridge_smi_pm1, /* [8] PM1_STS */
373 southbridge_smi_gpe0, /* [9] GPE0_STS */
374 NULL, /* [10] reserved */
375 NULL, /* [11] reserved */
376 NULL, /* [12] reserved */
377 southbridge_smi_tco, /* [13] TCO_STS */
378 southbridge_smi_periodic, /* [14] PERIODIC_STS */
379 NULL, /* [15] SERIRQ_SMI_STS */
380 NULL, /* [16] SMBUS_SMI_STS */
381 NULL, /* [17] LEGACY_USB2_STS */
382 NULL, /* [18] INTEL_USB2_STS */
383 NULL, /* [19] reserved */
384 NULL, /* [20] PCI_EXP_SMI_STS */
385 NULL, /* [21] reserved */
386 NULL, /* [22] reserved */
387 NULL, /* [23] reserved */
388 NULL, /* [24] reserved */
389 NULL, /* [25] reserved */
390 NULL, /* [26] SPI_STS */
391 NULL, /* [27] reserved */
392 NULL, /* [28] PUNIT */
393 NULL, /* [29] GUNIT */
394 NULL, /* [30] reserved */
395 NULL /* [31] reserved */
Aaron Durbin7837be62013-10-21 22:32:00 -0500396};
397
398void southbridge_smi_handler(void)
399{
400 int i;
401 uint32_t smi_sts;
402
Angel Pons26b49cc2020-07-07 17:17:51 +0200403 /*
404 * We need to clear the SMI status registers, or we won't see what's
Aaron Durbin7837be62013-10-21 22:32:00 -0500405 * happening in the following calls.
406 */
407 smi_sts = clear_smi_status();
408
409 /* Call SMI sub handler for each of the status bits */
410 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
411 if (!(smi_sts & (1 << i)))
412 continue;
413
414 if (southbridge_smi[i] != NULL) {
415 southbridge_smi[i]();
416 } else {
417 printk(BIOS_DEBUG,
Angel Pons5bcd35d2020-07-07 18:15:47 +0200418 "SMI_STS[%d] occurred, but no handler available.\n", i);
Aaron Durbin7837be62013-10-21 22:32:00 -0500419 }
420 }
Aaron Durbin9f83e872013-11-11 14:45:27 -0600421
Angel Pons26b49cc2020-07-07 17:17:51 +0200422 /*
423 * The GPIO SMI events do not have a status bit in SMI_STS. Therefore,
424 * these events need to be cleared and checked unconditionally.
425 */
Aaron Durbin9f83e872013-11-11 14:45:27 -0600426 mainboard_smi_gpi(clear_alt_status());
Aaron Durbin7837be62013-10-21 22:32:00 -0500427}