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 <stdint.h>
4 #include <acpi/acpi_gnvs.h>
5 #include <arch/io.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 <halt.h>
14 #include <spi-generic.h>
15 #include <smmstore.h>
16 
17 #include <soc/iosf.h>
18 #include <soc/pci_devs.h>
19 #include <soc/pm.h>
20 #include <soc/nvs.h>
21 #include <soc/device_nvs.h>
22 
24 {
25  switch (smif) {
26  case 0x32:
27  printk(BIOS_DEBUG, "OS Init\n");
28  /*
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 
42 {
43  enable_smi(EOS);
44 }
45 
46 static 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++) {
54  u16 reg16;
55  pci_devfn_t dev = PCI_DEV(bus, slot, func);
56 
58 
59  if (val == 0xffffffff || val == 0x00000000 ||
60  val == 0x0000ffff || val == 0xffff0000)
61  continue;
62 
63  /* Disable Bus Mastering for this one device */
64  reg16 = pci_read_config16(dev, PCI_COMMAND);
65  reg16 &= ~PCI_COMMAND_MASTER;
66  pci_write_config16(dev, PCI_COMMAND, reg16);
67 
68  /* If this is a bridge, then follow it. */
70  hdr &= 0x7f;
71  if (hdr == PCI_HEADER_TYPE_BRIDGE || hdr == PCI_HEADER_TYPE_CARDBUS) {
72  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 
80 static void southbridge_smi_sleep(void)
81 {
82  uint32_t reg32;
83  uint8_t slp_typ;
85 
86  /* First, disable further SMIs */
88 
89  /* Figure out SLP_TYP */
90  reg32 = inl(pmbase + PM1_CNT);
91  printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
92  slp_typ = acpi_sleep_from_pm1(reg32);
93 
94  /* Do any mainboard sleep handling */
95  mainboard_smi_sleep(slp_typ);
96 
97  /* Log S3, S4, and S5 entry */
98  if (slp_typ >= ACPI_S3)
100 
101  /* Next, do the deed. */
102  switch (slp_typ) {
103  case ACPI_S0:
104  printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
105  break;
106  case ACPI_S1:
107  printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
108  break;
109  case ACPI_S3:
110  printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
111 
112  /* Invalidate the cache before going to S3 */
113  wbinvd();
114  break;
115  case ACPI_S4:
116  printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
117  break;
118  case ACPI_S5:
119  printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
120 
121  /* Disable all GPE */
122  disable_all_gpe();
123 
124  /* Also iterates over all bridges on bus 0 */
126  break;
127  default:
128  printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
129  break;
130  }
131 
132  /*
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.
135  */
137 
138  /* Make sure to stop executing code here for S3/S4/S5 */
139  if (slp_typ >= ACPI_S3)
140  halt();
141 
142  /*
143  * In most sleep states, the code flow of this function ends at
144  * 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 */
151  }
152 }
153 
154 /*
155  * 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.
157  */
158 static 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 
189 static void southbridge_smi_gsmi(void)
190 {
191  u32 *ret, *param;
192  uint8_t sub_command;
193  em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_ELOG_GSMI);
194 
195  if (!io_smi)
196  return;
197 
198  /* Command and return value in EAX */
199  ret = (u32 *)&io_smi->rax;
200  sub_command = (uint8_t)(*ret >> 8);
201 
202  /* Parameter buffer in EBX */
203  param = (u32 *)&io_smi->rbx;
204 
205  /* drivers/elog/gsmi.c */
206  *ret = gsmi_exec(sub_command, param);
207 }
208 
210 {
211  return (u8 *)gnvs + ALIGN_UP(sizeof(struct global_nvs), sizeof(uint64_t));
212 }
213 
214 /*
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  */
220 static void soc_legacy(void)
221 {
222  struct device_nvs *dev_nvs = acpi_get_device_nvs();
223  u32 reg32;
224 
225  /* LPE Device */
226  if (dev_nvs->lpe_en) {
228  reg32 &=
231  }
232 
233  /* SCC Devices */
234 #define SCC_ACPI_MODE_DISABLE(name_) \
235  do { if (dev_nvs->scc_en[SCC_NVS_ ## name_]) { \
236  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 
243  SCC_ACPI_MODE_DISABLE(SDIO);
244 
245  /* LPSS Devices */
246 #define LPSS_ACPI_MODE_DISABLE(name_) \
247  do { if (dev_nvs->lpss_en[LPSS_NVS_ ## name_]) { \
248  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);
261  LPSS_ACPI_MODE_DISABLE(SIO_DMA2);
264  LPSS_ACPI_MODE_DISABLE(HSUART1);
265  LPSS_ACPI_MODE_DISABLE(HSUART2);
267 }
268 
269 static void southbridge_smi_store(void)
270 {
271  u8 sub_command, ret;
272  em64t100_smm_state_save_area_t *io_smi = smi_apmc_find_state_save(APM_CNT_SMMSTORE);
273  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 
288 static void southbridge_smi_apmc(void)
289 {
290  uint8_t reg8;
291 
292  reg8 = apm_get_apmc();
293  switch (reg8) {
296  break;
297  case APM_CNT_ACPI_ENABLE:
299  break;
300  case APM_CNT_ELOG_GSMI:
301  if (CONFIG(ELOG_GSMI))
303  break;
304  case APM_CNT_LEGACY:
305  soc_legacy();
306  break;
307  case APM_CNT_SMMSTORE:
308  if (CONFIG(SMMSTORE))
310  break;
311  }
312 
313  mainboard_smi_apmc(reg8);
314 }
315 
316 static void southbridge_smi_pm1(void)
317 {
318  uint16_t pm1_sts = clear_pm1_status();
319 
320  /* While OSPM is not active, poweroff immediately on a power button event */
321  if (pm1_sts & PWRBTN_STS) {
322  /* Power button pressed */
324  disable_pm1_control(-1UL);
326  }
327 }
328 
329 static void southbridge_smi_gpe0(void)
330 {
332 }
333 
334 static 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 
348 static 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 
361 typedef void (*smi_handler_t)(void);
362 
363 static const smi_handler_t southbridge_smi[32] = {
364  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 */
396 };
397 
399 {
400  int i;
401  uint32_t smi_sts;
402 
403  /*
404  * We need to clear the SMI status registers, or we won't see what's
405  * 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 {
418  "SMI_STS[%d] occurred, but no handler available.\n", i);
419  }
420  }
421 
422  /*
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  */
427 }
#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
#define LPE_PCICFGCTR1_ACPI_INT_EN
Definition: iosf.h:334
#define LPE_PCICFGCTR1_PCI_CFG_DIS
Definition: iosf.h:333
void iosf_port58_write(int reg, uint32_t val)
Definition: iosf.c:226
uint32_t iosf_port58_read(int reg)
Definition: iosf.c:221
#define LPE_PCICFGCTR1
Definition: iosf.h:332
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 ALIGN_UP(x, a)
Definition: helpers.h:17
#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
@ SD
Definition: variants.h:56
u32 gsmi_exec(u8 command, u32 *param)
Definition: gsmi.c:46
void __noreturn halt(void)
halt the system reliably
Definition: halt.c:6
@ 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_LEGACY
Definition: smm.h:25
#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
static void southbridge_smi_pm1(void)
Definition: smihandler.c:316
void * acpi_get_device_nvs(void)
Definition: smihandler.c:209
static void southbridge_smi_store(void)
Definition: smihandler.c:269
void southbridge_smi_set_eos(void)
Definition: smihandler.c:41
static em64t100_smm_state_save_area_t * smi_apmc_find_state_save(uint8_t cmd)
Definition: smihandler.c:158
static void busmaster_disable_on_bus(int bus)
Definition: smihandler.c:46
static void southbridge_smi_apmc(void)
Definition: smihandler.c:288
static void southbridge_smi_periodic(void)
Definition: smihandler.c:348
static void southbridge_smi_gpe0(void)
Definition: smihandler.c:329
#define SCC_ACPI_MODE_DISABLE(name_)
static void southbridge_smi_gsmi(void)
Definition: smihandler.c:189
#define LPSS_ACPI_MODE_DISABLE(name_)
static void southbridge_smi_tco(void)
Definition: smihandler.c:334
static void soc_legacy(void)
Definition: smihandler.c:220
static void southbridge_smi_sleep(void)
Definition: smihandler.c:80
void(* smi_handler_t)(void)
Definition: smihandler.c:361
@ I2C7
Definition: i2c.h:57
@ I2C4
Definition: i2c.h:54
@ I2C5
Definition: i2c.h:55
@ I2C3
Definition: i2c.h:53
@ I2C2
Definition: i2c.h:52
@ I2C1
Definition: i2c.h:51
@ I2C6
Definition: i2c.h:56
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
unsigned long long uint64_t
Definition: stdint.h:17
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
Definition: device.h:76
Definition: nvs.h:14
u8 smif
Definition: nvs.h:11
u8 val
Definition: sys.c:300
typedef void(X86APIP X86EMU_intrFuncs)(int num)