blob: c722f5c1f203b59a4d200dae557f758b14c19b19 [file] [log] [blame]
Aaron Durbin7837be62013-10-21 22:32:00 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin7837be62013-10-21 22:32:00 -050014 */
15
16#include <stdint.h>
17#include <stdlib.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050018#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050020#include <console/console.h>
21#include <cpu/x86/cache.h>
22#include <cpu/x86/smm.h>
23#include <device/pci_def.h>
24#include <elog.h>
Patrick Georgi546953c2014-11-29 10:38:17 +010025#include <halt.h>
Aaron Durbin20885712014-02-09 16:04:06 -060026#include <spi-generic.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050027
Marc Jones9afc5c02014-09-24 10:53:48 -060028#include <soc/iosf.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070029#include <soc/pci_devs.h>
30#include <soc/pmc.h>
31#include <soc/nvs.h>
Aaron Durbin7837be62013-10-21 22:32:00 -050032
33/* GNVS needs to be set by coreboot initiating a software SMI. */
34static global_nvs_t *gnvs;
35static int smm_initialized;
36
37int southbridge_io_trap_handler(int smif)
38{
39 switch (smif) {
40 case 0x32:
41 printk(BIOS_DEBUG, "OS Init\n");
42 /* gnvs->smif:
43 * On success, the IO Trap Handler returns 0
44 * On failure, the IO Trap Handler returns a value != 0
45 */
46 gnvs->smif = 0;
47 return 1; /* IO trap handled */
48 }
49
50 /* Not handled */
51 return 0;
52}
53
54void southbridge_smi_set_eos(void)
55{
56 enable_smi(EOS);
57}
58
59global_nvs_t *smm_get_gnvs(void)
60{
61 return gnvs;
62}
63
64static void busmaster_disable_on_bus(int bus)
65{
66 int slot, func;
67 unsigned int val;
68 unsigned char hdr;
69
70 for (slot = 0; slot < 0x20; slot++) {
71 for (func = 0; func < 8; func++) {
72 u32 reg32;
Elyes HAOUASc8a649c2018-06-10 23:36:44 +020073 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Aaron Durbin7837be62013-10-21 22:32:00 -050074
75 val = pci_read_config32(dev, PCI_VENDOR_ID);
76
77 if (val == 0xffffffff || val == 0x00000000 ||
78 val == 0x0000ffff || val == 0xffff0000)
79 continue;
80
81 /* Disable Bus Mastering for this one device */
82 reg32 = pci_read_config32(dev, PCI_COMMAND);
83 reg32 &= ~PCI_COMMAND_MASTER;
84 pci_write_config32(dev, PCI_COMMAND, reg32);
85
86 /* If this is a bridge, then follow it. */
87 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
88 hdr &= 0x7f;
89 if (hdr == PCI_HEADER_TYPE_BRIDGE ||
90 hdr == PCI_HEADER_TYPE_CARDBUS) {
91 unsigned int buses;
92 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
93 busmaster_disable_on_bus((buses >> 8) & 0xff);
94 }
95 }
96 }
97}
98
99static void southbridge_smi_sleep(void)
100{
101 uint32_t reg32;
102 uint8_t slp_typ;
103 uint16_t pmbase = get_pmbase();
104
105 /* First, disable further SMIs */
106 disable_smi(SLP_SMI_EN);
107
108 /* Figure out SLP_TYP */
109 reg32 = inl(pmbase + PM1_CNT);
110 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500111 slp_typ = acpi_sleep_from_pm1(reg32);
Aaron Durbin7837be62013-10-21 22:32:00 -0500112
113 /* Do any mainboard sleep handling */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500114 mainboard_smi_sleep(slp_typ);
Aaron Durbin7837be62013-10-21 22:32:00 -0500115
Martin Rothe6ff1592017-06-24 21:34:29 -0600116#if IS_ENABLED(CONFIG_ELOG_GSMI)
Aaron Durbin7837be62013-10-21 22:32:00 -0500117 /* Log S3, S4, and S5 entry */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500118 if (slp_typ >= ACPI_S3)
119 elog_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
Aaron Durbin7837be62013-10-21 22:32:00 -0500120#endif
121
122 /* Next, do the deed.
123 */
124
125 switch (slp_typ) {
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500126 case ACPI_S0:
Aaron Durbin7837be62013-10-21 22:32:00 -0500127 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
128 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500129 case ACPI_S1:
Aaron Durbin7837be62013-10-21 22:32:00 -0500130 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
131 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500132 case ACPI_S3:
Aaron Durbin7837be62013-10-21 22:32:00 -0500133 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
134
135 /* Invalidate the cache before going to S3 */
136 wbinvd();
137 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500138 case ACPI_S4:
Aaron Durbin7837be62013-10-21 22:32:00 -0500139 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
140 break;
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500141 case ACPI_S5:
Aaron Durbin7837be62013-10-21 22:32:00 -0500142 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
143
144 /* Disable all GPE */
145 disable_all_gpe();
146
147 /* also iterates over all bridges on bus 0 */
148 busmaster_disable_on_bus(0);
149 break;
150 default:
151 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
152 break;
153 }
154
155 /* Write back to the SLP register to cause the originally intended
156 * event again. We need to set BIT13 (SLP_EN) though to make the
157 * sleep happen.
158 */
159 enable_pm1_control(SLP_EN);
160
161 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500162 if (slp_typ >= ACPI_S3)
Patrick Georgi546953c2014-11-29 10:38:17 +0100163 halt();
Aaron Durbin7837be62013-10-21 22:32:00 -0500164
165 /* In most sleep states, the code flow of this function ends at
166 * the line above. However, if we entered sleep state S1 and wake
167 * up again, we will continue to execute code in this function.
168 */
169 reg32 = inl(pmbase + PM1_CNT);
170 if (reg32 & SCI_EN) {
171 /* The OS is not an ACPI OS, so we set the state to S0 */
172 disable_pm1_control(SLP_EN | SLP_TYP);
173 }
174}
175
176/*
177 * Look for Synchronous IO SMI and use save state from that
178 * core in case we are not running on the same core that
179 * initiated the IO transaction.
180 */
181static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
182{
183 em64t100_smm_state_save_area_t *state;
184 int node;
185
186 /* Check all nodes looking for the one that issued the IO */
187 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
188 state = smm_get_save_state(node);
189
190 /* Check for Synchronous IO (bit0==1) */
191 if (!(state->io_misc_info & (1 << 0)))
192 continue;
193
194 /* Make sure it was a write (bit4==0) */
195 if (state->io_misc_info & (1 << 4))
196 continue;
197
198 /* Check for APMC IO port */
199 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
200 continue;
201
202 /* Check AX against the requested command */
203 if ((state->rax & 0xff) != cmd)
204 continue;
205
206 return state;
207 }
208
209 return NULL;
210}
211
Martin Rothe6ff1592017-06-24 21:34:29 -0600212#if IS_ENABLED(CONFIG_ELOG_GSMI)
Aaron Durbin7837be62013-10-21 22:32:00 -0500213static void southbridge_smi_gsmi(void)
214{
215 u32 *ret, *param;
216 uint8_t sub_command;
217 em64t100_smm_state_save_area_t *io_smi =
Patrick Georgid61839c2018-12-03 16:10:33 +0100218 smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
Aaron Durbin7837be62013-10-21 22:32:00 -0500219
220 if (!io_smi)
221 return;
222
223 /* Command and return value in EAX */
224 ret = (u32*)&io_smi->rax;
225 sub_command = (uint8_t)(*ret >> 8);
226
227 /* Parameter buffer in EBX */
228 param = (u32*)&io_smi->rbx;
229
230 /* drivers/elog/gsmi.c */
231 *ret = gsmi_exec(sub_command, param);
232}
233#endif
Aaron Durbin20885712014-02-09 16:04:06 -0600234
235static void finalize(void)
236{
237 static int finalize_done;
238
239 if (finalize_done) {
240 printk(BIOS_DEBUG, "SMM already finalized.\n");
241 return;
242 }
243 finalize_done = 1;
244
Martin Rothe6ff1592017-06-24 21:34:29 -0600245#if IS_ENABLED(CONFIG_SPI_FLASH_SMM)
Aaron Durbin20885712014-02-09 16:04:06 -0600246 /* Re-init SPI driver to handle locked BAR */
247 spi_init();
248#endif
249}
250
Marc Jones9afc5c02014-09-24 10:53:48 -0600251/*
252 * soc_legacy: A payload (Depthcharge) has indicated that the
253 * legacy payload (SeaBIOS) is being loaded. Switch devices that are
254 * in ACPI mode to PCI mode so that non-ACPI drivers may work.
255 *
256 */
257static void soc_legacy(void)
258{
259 u32 reg32;
260
261 /* LPE Device */
262 if (gnvs->dev.lpe_en) {
263 reg32 = iosf_port58_read(LPE_PCICFGCTR1);
264 reg32 &=
265 ~(LPE_PCICFGCTR1_PCI_CFG_DIS | LPE_PCICFGCTR1_ACPI_INT_EN);
266 iosf_port58_write(LPE_PCICFGCTR1, reg32);
267 }
268
269 /* SCC Devices */
270#define SCC_ACPI_MODE_DISABLE(name_) \
271 do { if (gnvs->dev.scc_en[SCC_NVS_ ## name_]) { \
272 reg32 = iosf_scc_read(SCC_ ## name_ ## _CTL); \
273 reg32 &= ~(SCC_CTL_PCI_CFG_DIS | SCC_CTL_ACPI_INT_EN); \
274 iosf_scc_write(SCC_ ## name_ ## _CTL, reg32); \
275 } } while (0)
276
277 SCC_ACPI_MODE_DISABLE(MMC);
278 SCC_ACPI_MODE_DISABLE(SD);
279 SCC_ACPI_MODE_DISABLE(SDIO);
280
281 /* LPSS Devices */
282#define LPSS_ACPI_MODE_DISABLE(name_) \
283 do { if (gnvs->dev.lpss_en[LPSS_NVS_ ## name_]) { \
284 reg32 = iosf_lpss_read(LPSS_ ## name_ ## _CTL); \
285 reg32 &= ~LPSS_CTL_PCI_CFG_DIS | ~LPSS_CTL_ACPI_INT_EN; \
286 iosf_lpss_write(LPSS_ ## name_ ## _CTL, reg32); \
287 } } while (0)
288
289 LPSS_ACPI_MODE_DISABLE(SIO_DMA1);
290 LPSS_ACPI_MODE_DISABLE(I2C1);
291 LPSS_ACPI_MODE_DISABLE(I2C2);
292 LPSS_ACPI_MODE_DISABLE(I2C3);
293 LPSS_ACPI_MODE_DISABLE(I2C4);
294 LPSS_ACPI_MODE_DISABLE(I2C5);
295 LPSS_ACPI_MODE_DISABLE(I2C6);
296 LPSS_ACPI_MODE_DISABLE(I2C7);
297 LPSS_ACPI_MODE_DISABLE(SIO_DMA2);
298 LPSS_ACPI_MODE_DISABLE(PWM1);
299 LPSS_ACPI_MODE_DISABLE(PWM2);
300 LPSS_ACPI_MODE_DISABLE(HSUART1);
301 LPSS_ACPI_MODE_DISABLE(HSUART2);
302 LPSS_ACPI_MODE_DISABLE(SPI);
303}
304
Aaron Durbin7837be62013-10-21 22:32:00 -0500305static void southbridge_smi_apmc(void)
306{
307 uint8_t reg8;
308 em64t100_smm_state_save_area_t *state;
309
310 /* Emulate B2 register as the FADT / Linux expects it */
311
312 reg8 = inb(APM_CNT);
313 switch (reg8) {
314 case APM_CNT_CST_CONTROL:
315 /* Calling this function seems to cause
316 * some kind of race condition in Linux
317 * and causes a kernel oops
318 */
319 printk(BIOS_DEBUG, "C-state control\n");
320 break;
321 case APM_CNT_PST_CONTROL:
322 /* Calling this function seems to cause
323 * some kind of race condition in Linux
324 * and causes a kernel oops
325 */
326 printk(BIOS_DEBUG, "P-state control\n");
327 break;
328 case APM_CNT_ACPI_DISABLE:
329 disable_pm1_control(SCI_EN);
330 printk(BIOS_DEBUG, "SMI#: ACPI disabled.\n");
331 break;
332 case APM_CNT_ACPI_ENABLE:
333 enable_pm1_control(SCI_EN);
334 printk(BIOS_DEBUG, "SMI#: ACPI enabled.\n");
335 break;
336 case APM_CNT_GNVS_UPDATE:
337 if (smm_initialized) {
338 printk(BIOS_DEBUG,
339 "SMI#: SMM structures already initialized!\n");
340 return;
341 }
342 state = smi_apmc_find_state_save(reg8);
343 if (state) {
344 /* EBX in the state save contains the GNVS pointer */
345 gnvs = (global_nvs_t *)((uint32_t)state->rbx);
346 smm_initialized = 1;
347 printk(BIOS_DEBUG, "SMI#: Setting GNVS to %p\n", gnvs);
348 }
349 break;
Martin Rothe6ff1592017-06-24 21:34:29 -0600350#if IS_ENABLED(CONFIG_ELOG_GSMI)
Patrick Georgid61839c2018-12-03 16:10:33 +0100351 case APM_CNT_ELOG_GSMI:
Aaron Durbin7837be62013-10-21 22:32:00 -0500352 southbridge_smi_gsmi();
353 break;
354#endif
Aaron Durbin20885712014-02-09 16:04:06 -0600355 case APM_CNT_FINALIZE:
356 finalize();
357 break;
Marc Jones9afc5c02014-09-24 10:53:48 -0600358
359 case APM_CNT_LEGACY:
360 soc_legacy();
361 break;
Aaron Durbin7837be62013-10-21 22:32:00 -0500362 }
363
364 mainboard_smi_apmc(reg8);
365}
366
367static void southbridge_smi_pm1(void)
368{
369 uint16_t pm1_sts = clear_pm1_status();
370
371 /* While OSPM is not active, poweroff immediately
372 * on a power button event.
373 */
374 if (pm1_sts & PWRBTN_STS) {
375 // power button pressed
Martin Rothe6ff1592017-06-24 21:34:29 -0600376#if IS_ENABLED(CONFIG_ELOG_GSMI)
Aaron Durbin7837be62013-10-21 22:32:00 -0500377 elog_add_event(ELOG_TYPE_POWER_BUTTON);
378#endif
379 disable_pm1_control(-1UL);
Aaron Durbin9f83e872013-11-11 14:45:27 -0600380 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
Aaron Durbin7837be62013-10-21 22:32:00 -0500381 }
382}
383
384static void southbridge_smi_gpe0(void)
385{
386 clear_gpe_status();
387}
388
389static void southbridge_smi_tco(void)
390{
391 uint32_t tco_sts = clear_tco_status();
392
393 /* Any TCO event? */
394 if (!tco_sts)
395 return;
396
397 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
398 /* Handle TCO timeout */
399 printk(BIOS_DEBUG, "TCO Timeout.\n");
400 }
401}
402
403static void southbridge_smi_periodic(void)
404{
405 uint32_t reg32;
406
407 reg32 = inl(get_pmbase() + SMI_EN);
408
409 /* Are periodic SMIs enabled? */
410 if ((reg32 & PERIODIC_EN) == 0)
411 return;
412
413 printk(BIOS_DEBUG, "Periodic SMI.\n");
414}
415
416typedef void (*smi_handler_t)(void);
417
418static const smi_handler_t southbridge_smi[32] = {
419 NULL, // [0] reserved
420 NULL, // [1] reserved
421 NULL, // [2] BIOS_STS
422 NULL, // [3] LEGACY_USB_STS
423 southbridge_smi_sleep, // [4] SLP_SMI_STS
424 southbridge_smi_apmc, // [5] APM_STS
425 NULL, // [6] SWSMI_TMR_STS
426 NULL, // [7] reserved
427 southbridge_smi_pm1, // [8] PM1_STS
428 southbridge_smi_gpe0, // [9] GPE0_STS
429 NULL, // [10] reserved
430 NULL, // [11] reserved
431 NULL, // [12] reserved
432 southbridge_smi_tco, // [13] TCO_STS
433 southbridge_smi_periodic, // [14] PERIODIC_STS
434 NULL, // [15] SERIRQ_SMI_STS
435 NULL, // [16] SMBUS_SMI_STS
436 NULL, // [17] LEGACY_USB2_STS
437 NULL, // [18] INTEL_USB2_STS
438 NULL, // [19] reserved
439 NULL, // [20] PCI_EXP_SMI_STS
440 NULL, // [21] reserved
441 NULL, // [22] reserved
442 NULL, // [23] reserved
443 NULL, // [24] reserved
444 NULL, // [25] reserved
445 NULL, // [26] SPI_STS
446 NULL, // [27] reserved
447 NULL, // [28] PUNIT
448 NULL, // [29] GUNIT
449 NULL, // [30] reserved
450 NULL // [31] reserved
451};
452
453void southbridge_smi_handler(void)
454{
455 int i;
456 uint32_t smi_sts;
457
458 /* We need to clear the SMI status registers, or we won't see what's
459 * happening in the following calls.
460 */
461 smi_sts = clear_smi_status();
462
463 /* Call SMI sub handler for each of the status bits */
464 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
465 if (!(smi_sts & (1 << i)))
466 continue;
467
468 if (southbridge_smi[i] != NULL) {
469 southbridge_smi[i]();
470 } else {
471 printk(BIOS_DEBUG,
Martin Roth99a3bba2014-12-07 14:57:26 -0700472 "SMI_STS[%d] occurred, but no "
Aaron Durbin7837be62013-10-21 22:32:00 -0500473 "handler available.\n", i);
474 }
475 }
Aaron Durbin9f83e872013-11-11 14:45:27 -0600476
477 /* The GPIO SMI events do not have a status bit in SMI_STS. Therefore,
478 * these events need to be cleared and checked unconditionally. */
479 mainboard_smi_gpi(clear_alt_status());
Aaron Durbin7837be62013-10-21 22:32:00 -0500480}