coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
smihandler.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <arch/hlt.h>
4 #include <arch/io.h>
5 #include <device/mmio.h>
6 #include <device/pci_ops.h>
7 #include <console/console.h>
8 #include <cpu/x86/cache.h>
9 #include <cpu/x86/smm.h>
11 #include <device/pci_def.h>
12 #include <elog.h>
13 #include <soc/nvs.h>
14 #include <soc/pci_devs.h>
15 #include <soc/pm.h>
16 #include <spi-generic.h>
17 #include <stdint.h>
18 #include <soc/gpio.h>
19 #include <smmstore.h>
20 
22 {
23  switch (smif) {
24  case 0x32:
25  printk(BIOS_DEBUG, "OS Init\n");
26  /*
27  * gnvs->smif:
28  * On success, the IO Trap Handler returns 0
29  * On failure, the IO Trap Handler returns a value != 0
30  */
31  gnvs->smif = 0;
32  return 1; /* IO trap handled */
33  }
34 
35  /* Not handled */
36  return 0;
37 }
38 
40 {
41  enable_smi(EOS);
42 }
43 
44 static void busmaster_disable_on_bus(int bus)
45 {
46  int slot, func;
47  unsigned int val;
48  unsigned char hdr;
49 
50  for (slot = 0; slot < 0x20; slot++) {
51  for (func = 0; func < 8; func++) {
52  u16 reg16;
53  pci_devfn_t dev = PCI_DEV(bus, slot, func);
54 
56 
57  if (val == 0xffffffff || val == 0x00000000 ||
58  val == 0x0000ffff || val == 0xffff0000)
59  continue;
60 
61  /* Disable Bus Mastering for this one device */
62  reg16 = pci_read_config16(dev, PCI_COMMAND);
63  reg16 &= ~PCI_COMMAND_MASTER;
64  pci_write_config16(dev, PCI_COMMAND, reg16);
65 
66  /* If this is a bridge, then follow it. */
68  hdr &= 0x7f;
69  if (hdr == PCI_HEADER_TYPE_BRIDGE || hdr == PCI_HEADER_TYPE_CARDBUS) {
70  unsigned int buses;
71  buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
72  busmaster_disable_on_bus((buses >> 8) & 0xff);
73  }
74  }
75  }
76 }
77 
79 {
80  /* Tri-state eMMC */
91 
92  /* Tri-state HDMI */
95 
96  /* Tri-state CFIO 139 and 140 */
99 }
100 
101 static void southbridge_smi_sleep(void)
102 {
103  uint32_t reg32;
104  uint8_t slp_typ;
106 
107  /* First, disable further SMIs */
109 
110  /* Figure out SLP_TYP */
111  reg32 = inl(pmbase + PM1_CNT);
112  printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
113  slp_typ = acpi_sleep_from_pm1(reg32);
114 
115  /* Do any mainboard sleep handling */
116  mainboard_smi_sleep(slp_typ);
117 
118  /* Log S3, S4, and S5 entry */
119  if (slp_typ >= ACPI_S3)
121 
122  /* Clear pending GPE events */
124 
125  /* Next, do the deed. */
126  switch (slp_typ) {
127  case ACPI_S0:
128  printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
129  break;
130  case ACPI_S1:
131  printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
132  break;
133  case ACPI_S3:
134  printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
135 
136  /* Invalidate the cache before going to S3 */
137  wbinvd();
138  break;
139  case ACPI_S4:
140  printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
141  break;
142  case ACPI_S5:
143  printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
144 
145  /* Disable all GPE */
146  disable_all_gpe();
147 
148  /* Also iterates over all bridges on bus 0 */
150  break;
151  default:
152  printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
153  break;
154  }
155 
156  /* Clear pending wake status bit to avoid immediate wake */
157  write32((void *)(0xfed88000 + 0x0200), read32((void *)(0xfed88000 + 0x0200)));
158 
159  /* Tri-state specific GPIOS to avoid leakage during S3/S5 */
160  if ((slp_typ == ACPI_S3) || (slp_typ == ACPI_S5))
162 
163  /*
164  * Write back to the SLP register to cause the originally intended event again.
165  * We need to set BIT13 (SLP_EN) though to make the sleep happen.
166  */
168 
169  /* Make sure to stop executing code here for S3/S4/S5 */
170  if (slp_typ >= ACPI_S3)
171  hlt();
172 
173  /*
174  * In most sleep states, the code flow of this function ends at
175  * 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 */
182  }
183 }
184 
185 /*
186  * Look for Synchronous IO SMI and use save state from that core in case
187  * we are not running on the same core that initiated the IO transaction.
188  */
189 static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
190 {
191  em64t100_smm_state_save_area_t *state;
192  int node;
193 
194  /* Check all nodes looking for the one that issued the IO */
195  for (node = 0; node < CONFIG_MAX_CPUS; node++) {
196  state = smm_get_save_state(node);
197 
198  /* Check for Synchronous IO (bit0==1) */
199  if (!(state->io_misc_info & (1 << 0)))
200  continue;
201 
202  /* Make sure it was a write (bit4==0) */
203  if (state->io_misc_info & (1 << 4))
204  continue;
205 
206  /* Check for APMC IO port */
207  if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
208  continue;
209 
210  /* Check AX against the requested command */
211  if ((state->rax & 0xff) != cmd)
212  continue;
213 
214  return state;
215  }
216 
217  return NULL;
218 }
219 
220 static void southbridge_smi_gsmi(void)
221 {
222  u32 *ret, *param;
223  uint8_t sub_command;
224  em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
225 
226  if (!io_smi)
227  return;
228 
229  /* Command and return value in EAX */
230  ret = (u32 *)&io_smi->rax;
231  sub_command = (uint8_t)(*ret >> 8);
232 
233  /* Parameter buffer in EBX */
234  param = (u32 *)&io_smi->rbx;
235 
236  /* drivers/elog/gsmi.c */
237  *ret = gsmi_exec(sub_command, param);
238 }
239 
240 static void southbridge_smi_store(void)
241 {
242  u8 sub_command, ret;
243  em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_SMMSTORE);
244  uint32_t reg_ebx;
245 
246  if (!io_smi)
247  return;
248  /* Command and return value in EAX */
249  sub_command = (io_smi->rax >> 8) & 0xff;
250 
251  /* Parameter buffer in EBX */
252  reg_ebx = io_smi->rbx;
253 
254  /* drivers/smmstore/smi.c */
255  ret = smmstore_exec(sub_command, (void *)reg_ebx);
256  io_smi->rax = ret;
257 }
258 
259 static void southbridge_smi_apmc(void)
260 {
261  uint8_t reg8;
262 
263  reg8 = apm_get_apmc();
264  switch (reg8) {
267  break;
268  case APM_CNT_ACPI_ENABLE:
270  break;
271  case APM_CNT_ELOG_GSMI:
272  if (CONFIG(ELOG_GSMI))
274  break;
275  case APM_CNT_SMMSTORE:
276  if (CONFIG(SMMSTORE))
278  break;
279  }
280 
281  mainboard_smi_apmc(reg8);
282 }
283 
284 static void southbridge_smi_pm1(void)
285 {
286  uint16_t pm1_sts = clear_pm1_status();
287 
288  /* While OSPM is not active, poweroff immediately on a power button event */
289  if (pm1_sts & PWRBTN_STS) {
290  /* Power button pressed */
292  disable_pm1_control(-1UL);
294  }
295 }
296 
297 static void southbridge_smi_gpe0(void)
298 {
300 }
301 
302 static void southbridge_smi_tco(void)
303 {
304  uint32_t tco_sts = clear_tco_status();
305 
306  /* Any TCO event? */
307  if (!tco_sts)
308  return;
309 
310  if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
311  /* Handle TCO timeout */
312  printk(BIOS_DEBUG, "TCO Timeout.\n");
313  }
314 }
315 
316 static void southbridge_smi_periodic(void)
317 {
318  uint32_t reg32;
319 
320  reg32 = inl(get_pmbase() + SMI_EN);
321 
322  /* Are periodic SMIs enabled? */
323  if ((reg32 & PERIODIC_EN) == 0)
324  return;
325 
326  printk(BIOS_DEBUG, "Periodic SMI.\n");
327 }
328 
329 typedef void (*smi_handler_t)(void);
330 
331 static const smi_handler_t southbridge_smi[32] = {
332  NULL, /* [0] reserved */
333  NULL, /* [1] reserved */
334  NULL, /* [2] BIOS_STS */
335  NULL, /* [3] LEGACY_USB_STS */
336  southbridge_smi_sleep, /* [4] SLP_SMI_STS */
337  southbridge_smi_apmc, /* [5] APM_STS */
338  NULL, /* [6] SWSMI_TMR_STS */
339  NULL, /* [7] reserved */
340  southbridge_smi_pm1, /* [8] PM1_STS */
341  southbridge_smi_gpe0, /* [9] GPE0_STS */
342  NULL, /* [10] reserved */
343  NULL, /* [11] reserved */
344  NULL, /* [12] reserved */
345  southbridge_smi_tco, /* [13] TCO_STS */
346  southbridge_smi_periodic, /* [14] PERIODIC_STS */
347  NULL, /* [15] SERIRQ_SMI_STS */
348  NULL, /* [16] SMBUS_SMI_STS */
349  NULL, /* [17] LEGACY_USB2_STS */
350  NULL, /* [18] INTEL_USB2_STS */
351  NULL, /* [19] reserved */
352  NULL, /* [20] PCI_EXP_SMI_STS */
353  NULL, /* [21] reserved */
354  NULL, /* [22] reserved */
355  NULL, /* [23] reserved */
356  NULL, /* [24] reserved */
357  NULL, /* [25] reserved */
358  NULL, /* [26] SPI_STS */
359  NULL, /* [27] reserved */
360  NULL, /* [28] PUNIT */
361  NULL, /* [29] GUNIT */
362  NULL, /* [30] reserved */
363  NULL /* [31] reserved */
364 };
365 
367 {
368  int i;
369  uint32_t smi_sts;
370 
371  /*
372  * We need to clear the SMI status registers, or we won't see what's
373  * happening in the following calls.
374  */
375  smi_sts = clear_smi_status();
376 
377  /* Call SMI sub handler for each of the status bits */
378  for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
379  if (!(smi_sts & (1 << i)))
380  continue;
381 
382  if (southbridge_smi[i] != NULL) {
383  southbridge_smi[i]();
384  } else {
386  "SMI_STS[%d] occurred, but no handler available.\n", i);
387  }
388  }
389 
390  /*
391  * The GPIO SMI events do not have a status bit in SMI_STS. Therefore,
392  * these events need to be cleared and checked unconditionally.
393  */
395 }
#define SCI_EN
Definition: pm.h:30
#define SLP_SMI_EN
Definition: pm.h:45
#define PM1_CNT
Definition: pm.h:27
#define SMI_EN
Definition: pm.h:32
#define EOS
Definition: pm.h:48
#define PERIODIC_EN
Definition: pm.h:39
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
static __always_inline void hlt(void)
Definition: hlt.h:6
uint16_t clear_pm1_status(void)
Definition: pmutil.c:152
void enable_pm1_control(uint32_t mask)
Definition: pmutil.c:105
uint32_t clear_alt_status(void)
Definition: pmutil.c:312
void disable_smi(uint32_t mask)
Definition: pmutil.c:97
void enable_smi(uint32_t mask)
Definition: pmutil.c:89
uint32_t clear_gpe_status(void)
Definition: pmutil.c:265
void disable_pm1_control(uint32_t mask)
Definition: pmutil.c:113
void disable_all_gpe(void)
Definition: pmutil.c:210
uint32_t clear_tco_status(void)
Definition: pmutil.c:189
uint32_t clear_smi_status(void)
Definition: pmutil.c:84
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define PWRBTN_STS
Definition: southbridge.h:30
#define ELOG_TYPE_ACPI_ENTER
Definition: elog.h:143
#define ELOG_TYPE_POWER_BUTTON
Definition: elog.h:133
#define printk(level,...)
Definition: stdlib.h:16
void __weak southbridge_smi_handler(void)
Definition: smihandler.c:207
void __weak mainboard_smi_sleep(u8 slp_typ)
Definition: smihandler.c:210
int __weak mainboard_smi_apmc(u8 data)
Definition: smihandler.c:209
void __weak mainboard_smi_gpi(u32 gpi_sts)
Definition: smihandler.c:208
void * smm_get_save_state(int cpu)
Definition: smihandler.c:114
u32 inl(u16 port)
uint32_t smmstore_exec(uint8_t command, void *param)
Definition: smi.c:144
@ CONFIG
Definition: dsi_common.h:201
u32 gsmi_exec(u8 command, u32 *param)
Definition: gsmi.c:46
@ ACPI_S5
Definition: acpi.h:1385
@ ACPI_S1
Definition: acpi.h:1381
@ ACPI_S4
Definition: acpi.h:1384
@ ACPI_S3
Definition: acpi.h:1383
@ ACPI_S0
Definition: acpi.h:1380
static void wbinvd(void)
Definition: cache.h:15
#define APM_CNT
Definition: smm.h:19
#define APM_CNT_ELOG_GSMI
Definition: smm.h:29
#define APM_CNT_ACPI_DISABLE
Definition: smm.h:21
#define APM_CNT_ACPI_ENABLE
Definition: smm.h:22
#define APM_CNT_SMMSTORE
Definition: smm.h:28
static __always_inline u16 pci_read_config16(const struct device *dev, u16 reg)
Definition: pci_ops.h:52
static __always_inline u32 pci_read_config32(const struct device *dev, u16 reg)
Definition: pci_ops.h:58
static __always_inline u8 pci_read_config8(const struct device *dev, u16 reg)
Definition: pci_ops.h:46
static __always_inline void pci_write_config16(const struct device *dev, u16 reg, u16 val)
Definition: pci_ops.h:70
static int elog_gsmi_add_event(u8 event_type)
Definition: elog.h:45
static int elog_gsmi_add_event_byte(u8 event_type, u8 data)
Definition: elog.h:46
#define SLP_EN
Definition: pmc.h:62
#define SLP_TYP_S5
Definition: pmc.h:69
#define SLP_TYP
Definition: pmc.h:64
#define SLP_TYP_SHIFT
Definition: pmc.h:63
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
state
Definition: raminit.c:1787
#define PCI_HEADER_TYPE
Definition: pci_def.h:47
#define PCI_PRIMARY_BUS
Definition: pci_def.h:100
#define PCI_HEADER_TYPE_CARDBUS
Definition: pci_def.h:50
#define PCI_COMMAND_MASTER
Definition: pci_def.h:13
#define PCI_COMMAND
Definition: pci_def.h:10
#define PCI_HEADER_TYPE_BRIDGE
Definition: pci_def.h:49
#define PCI_VENDOR_ID
Definition: pci_def.h:8
#define PCI_DEV(SEGBUS, DEV, FN)
Definition: pci_type.h:14
u32 pci_devfn_t
Definition: pci_type.h:8
u8 apm_get_apmc(void)
Definition: smi_trigger.c:46
struct global_nvs * gnvs
int southbridge_io_trap_handler(int smif)
Definition: smihandler.c:131
const smi_handler_t southbridge_smi[SMI_STS_BITS]
Definition: smihandler.c:17
#define TCO_TIMEOUT
Definition: smbus.h:8
void southbridge_smi_set_eos(void)
Definition: smihandler.c:41
void(* smi_handler_t)(void)
Definition: smihandler.c:361
#define MMC1_RCLK_OFFSET
Definition: gpio.h:94
#define MMC1_D7_MMIO_OFFSET
Definition: gpio.h:93
#define MMC1_D5_MMIO_OFFSET
Definition: gpio.h:91
#define COMMUNITY_GPSOUTHEAST_BASE
Definition: gpio.h:23
#define MMC1_D6_MMIO_OFFSET
Definition: gpio.h:92
#define COMMUNITY_GPNORTH_BASE
Definition: gpio.h:17
#define HV_DDI2_DDC_SCL_MMIO_OFFSET
Definition: gpio.h:96
#define SDMMC1_D3_MMIO_OFFSET
Definition: gpio.h:89
#define CFIO_139_MMIO_OFFSET
Definition: gpio.h:97
#define SDMMC1_CMD_MMIO_OFFSET
Definition: gpio.h:85
#define SDMMC1_D0_MMIO_OFFSET
Definition: gpio.h:86
#define MMC1_D4_SD_WE_MMIO_OFFSET
Definition: gpio.h:90
#define SDMMC1_D2_MMIO_OFFSET
Definition: gpio.h:88
#define SDMMC1_D1_MMIO_OFFSET
Definition: gpio.h:87
#define PAD_CONTROL_REG0_TRISTATE
Definition: gpio.h:73
#define CFIO_140_MMIO_OFFSET
Definition: gpio.h:98
#define COMMUNITY_GPSOUTHWEST_BASE
Definition: gpio.h:14
#define HV_DDI2_DDC_SDA_MMIO_OFFSET
Definition: gpio.h:95
static void southbridge_smi_pm1(void)
Definition: smihandler.c:284
static void southbridge_smi_store(void)
Definition: smihandler.c:240
static em64t100_smm_state_save_area_t * smi_apmc_find_state_save(uint8_t cmd)
Definition: smihandler.c:189
static void busmaster_disable_on_bus(int bus)
Definition: smihandler.c:44
static void southbridge_smi_apmc(void)
Definition: smihandler.c:259
static void southbridge_smi_periodic(void)
Definition: smihandler.c:316
static void southbridge_smi_gpe0(void)
Definition: smihandler.c:297
static void tristate_gpios(uint32_t val)
Definition: smihandler.c:78
static void southbridge_smi_gsmi(void)
Definition: smihandler.c:220
static void southbridge_smi_tco(void)
Definition: smihandler.c:302
static void southbridge_smi_sleep(void)
Definition: smihandler.c:101
u16 get_pmbase(void)
Definition: smihandler.c:20
u16 pmbase
Definition: smihandler.c:25
#define NULL
Definition: stddef.h:19
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
Definition: device.h:76
u8 smif
Definition: nvs.h:11
u8 val
Definition: sys.c:300
typedef void(X86APIP X86EMU_intrFuncs)(int num)