blob: 680ec87f3e5c77c626acc0439432c65a293dd729 [file] [log] [blame]
Angel Pons0612b272020-04-05 15:46:56 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Hannah Williamsba0fc472016-05-04 18:15:49 -07002
3#include <arch/hlt.h>
4#include <arch/io.h>
5#include <console/console.h>
6#include <cpu/x86/cache.h>
Angel Pons23222272021-03-25 13:02:22 +01007#include <cpu/x86/msr.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -07008#include <cpu/x86/smm.h>
Kyösti Mälkkie31ec292019-08-10 17:27:01 +03009#include <cpu/intel/em64t100_save_state.h>
10#include <cpu/intel/em64t101_save_state.h>
Angel Pons23222272021-03-25 13:02:22 +010011#include <cpu/intel/msr.h>
Furquan Shaikh10c3b962018-03-01 15:21:05 -080012#include <delay.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080013#include <device/mmio.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070014#include <device/pci_def.h>
Jianjun Wang8565b94a2022-03-02 10:20:26 +080015#include <device/pci_ops.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070016#include <elog.h>
Subrata Banik47a655c2017-12-14 18:22:13 +053017#include <intelblocks/fast_spi.h>
Michał Żygowskid627f7b2022-10-28 15:44:48 +020018#include <intelblocks/oc_wdt.h>
Shaunak Saha83e98232017-06-22 10:38:30 -070019#include <intelblocks/pmclib.h>
Brandon Breitensteina86d1b82017-06-08 17:32:02 -070020#include <intelblocks/smihandler.h>
Michael Niewöhnerccde6be2020-03-03 20:48:30 +010021#include <intelblocks/tco.h>
Furquan Shaikhea4ece62017-08-05 11:12:44 -070022#include <intelblocks/uart.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010023#include <smmstore.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070024#include <soc/nvs.h>
Nico Huber3e786b52019-08-05 21:12:33 +020025#include <soc/pci_devs.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070026#include <soc/pm.h>
27#include <soc/gpio.h>
28#include <soc/iomap.h>
Subrata Banik7bc4dc52018-05-17 18:40:32 +053029#include <soc/smbus.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070030#include <spi-generic.h>
31#include <stdint.h>
Hannah Williamsba0fc472016-05-04 18:15:49 -070032
Subrata Banik47a655c2017-12-14 18:22:13 +053033/* SoC overrides. */
34
Subrata Banik00b75332020-02-20 12:09:45 +053035__weak const struct smm_save_state_ops *get_smm_save_state_ops(void)
36{
37 return &em64t101_smm_ops;
38}
39
Subrata Banikf9eaede2018-02-19 13:57:02 +053040/* Specific SOC SMI handler during ramstage finalize phase */
Aaron Durbin64031672018-04-21 14:45:32 -060041__weak void smihandler_soc_at_finalize(void)
Subrata Banikf9eaede2018-02-19 13:57:02 +053042{
Yuchen Heff4a3a62023-03-21 23:40:08 +010043 /* no-op */
Subrata Banikf9eaede2018-02-19 13:57:02 +053044}
45
Elyes HAOUAS19b88592018-09-20 08:52:27 +020046__weak int smihandler_soc_disable_busmaster(pci_devfn_t dev)
Hannah Williamsba0fc472016-05-04 18:15:49 -070047{
48 return 1;
49}
50
Subrata Banik261b8932017-12-22 13:41:35 +053051/* Mainboard overrides. */
52
Aaron Durbin64031672018-04-21 14:45:32 -060053__weak void mainboard_smi_gpi_handler(
Subrata Banik261b8932017-12-22 13:41:35 +053054 const struct gpi_status *sts)
55{
Yuchen Heff4a3a62023-03-21 23:40:08 +010056 /* no-op */
Subrata Banik261b8932017-12-22 13:41:35 +053057}
58
Aaron Durbin64031672018-04-21 14:45:32 -060059__weak void mainboard_smi_espi_handler(void)
Subrata Banik261b8932017-12-22 13:41:35 +053060{
Yuchen Heff4a3a62023-03-21 23:40:08 +010061 /* no-op */
Subrata Banik261b8932017-12-22 13:41:35 +053062}
63
64/* Common Functions */
65
Hannah Williamsba0fc472016-05-04 18:15:49 -070066static void *find_save_state(const struct smm_save_state_ops *save_state_ops,
Brandon Breitensteina86d1b82017-06-08 17:32:02 -070067 int cmd)
Hannah Williamsba0fc472016-05-04 18:15:49 -070068{
69 int node;
70 void *state = NULL;
71 uint32_t io_misc_info;
72 uint8_t reg_al;
73
74 /* Check all nodes looking for the one that issued the IO */
75 for (node = 0; node < CONFIG_MAX_CPUS; node++) {
76 state = smm_get_save_state(node);
77
78 io_misc_info = save_state_ops->get_io_misc_info(state);
79
80 /* Check for Synchronous IO (bit0==1) */
81 if (!(io_misc_info & (1 << 0)))
82 continue;
83 /* Make sure it was a write (bit4==0) */
84 if (io_misc_info & (1 << 4))
85 continue;
86 /* Check for APMC IO port */
87 if (((io_misc_info >> 16) & 0xff) != APM_CNT)
88 continue;
89 /* Check AL against the requested command */
90 reg_al = save_state_ops->get_reg(state, RAX);
91 if (reg_al != cmd)
92 continue;
93 break;
94 }
95 return state;
96}
97
Brandon Breitensteina86d1b82017-06-08 17:32:02 -070098/* Inherited from cpu/x86/smm.h resulting in a different signature */
Hannah Williamsba0fc472016-05-04 18:15:49 -070099void southbridge_smi_set_eos(void)
100{
Shaunak Saha83e98232017-06-22 10:38:30 -0700101 pmc_enable_smi(EOS);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700102}
103
Hannah Williamsba0fc472016-05-04 18:15:49 -0700104static void busmaster_disable_on_bus(int bus)
105{
106 int slot, func;
107 unsigned int val;
108 unsigned char hdr;
109
110 for (slot = 0; slot < 0x20; slot++) {
111 for (func = 0; func < 8; func++) {
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200112 u16 reg16;
Kyösti Mälkkie16c9df2018-12-29 08:04:16 +0200113
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200114 pci_devfn_t dev = PCI_DEV(bus, slot, func);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700115
Subrata Banik261b8932017-12-22 13:41:35 +0530116 if (!smihandler_soc_disable_busmaster(dev))
Hannah Williamsba0fc472016-05-04 18:15:49 -0700117 continue;
118 val = pci_read_config32(dev, PCI_VENDOR_ID);
119
120 if (val == 0xffffffff || val == 0x00000000 ||
121 val == 0x0000ffff || val == 0xffff0000)
122 continue;
123
124 /* Disable Bus Mastering for this one device */
Elyes HAOUAS2ec1c132020-04-29 09:57:05 +0200125 reg16 = pci_read_config16(dev, PCI_COMMAND);
126 reg16 &= ~PCI_COMMAND_MASTER;
127 pci_write_config16(dev, PCI_COMMAND, reg16);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700128
Aaron Durbin065b6832016-06-10 21:44:26 -0500129 /* If it's not a bridge, move on. */
Hannah Williamsba0fc472016-05-04 18:15:49 -0700130 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
131 hdr &= 0x7f;
Aaron Durbin065b6832016-06-10 21:44:26 -0500132 if (hdr != PCI_HEADER_TYPE_BRIDGE &&
133 hdr != PCI_HEADER_TYPE_CARDBUS)
134 continue;
135
136 /*
137 * If secondary bus is equal to current bus bypass
138 * the bridge because it's likely unconfigured and
139 * would cause infinite recursion.
140 */
141 int secbus = pci_read_config8(dev, PCI_SECONDARY_BUS);
142
143 if (secbus == bus)
144 continue;
145
146 busmaster_disable_on_bus(secbus);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700147 }
148 }
149}
150
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700151void smihandler_southbridge_sleep(
152 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700153{
154 uint32_t reg32;
155 uint8_t slp_typ;
156
157 /* First, disable further SMIs */
Shaunak Saha83e98232017-06-22 10:38:30 -0700158 pmc_disable_smi(SLP_SMI_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700159 /* Figure out SLP_TYP */
Barnali Sarkar9e55ff62017-06-05 20:01:14 +0530160 reg32 = inl(ACPI_BASE_ADDRESS + PM1_CNT);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700161 printk(BIOS_SPEW, "SMI#: SLP = 0x%08x\n", reg32);
Aaron Durbin56db47f2016-07-13 23:19:46 -0500162 slp_typ = acpi_sleep_from_pm1(reg32);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700163
164 /* Do any mainboard sleep handling */
Aaron Durbin56db47f2016-07-13 23:19:46 -0500165 mainboard_smi_sleep(slp_typ);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700166
167 /* Log S3, S4, and S5 entry */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200168 if (slp_typ >= ACPI_S3)
169 elog_gsmi_add_event_byte(ELOG_TYPE_ACPI_ENTER, slp_typ);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700170
171 /* Clear pending GPE events */
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700172 pmc_clear_all_gpe_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700173
174 /* Next, do the deed. */
175
176 switch (slp_typ) {
Aaron Durbin56db47f2016-07-13 23:19:46 -0500177 case ACPI_S0:
Hannah Williamsba0fc472016-05-04 18:15:49 -0700178 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
179 break;
Aaron Durbin56db47f2016-07-13 23:19:46 -0500180 case ACPI_S3:
Hannah Williamsba0fc472016-05-04 18:15:49 -0700181 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
Patrick Rudolph06c022f2020-10-08 16:36:25 +0200182 if (CONFIG(SOC_INTEL_COMMON_BLOCK_UART))
183 gnvs->uior = uart_is_controller_initialized();
Furquan Shaikhea4ece62017-08-05 11:12:44 -0700184
Hannah Williamsba0fc472016-05-04 18:15:49 -0700185 /* Invalidate the cache before going to S3 */
186 wbinvd();
187 break;
Aaron Durbin56db47f2016-07-13 23:19:46 -0500188 case ACPI_S4:
Hannah Williamsba0fc472016-05-04 18:15:49 -0700189 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
190 break;
Aaron Durbin56db47f2016-07-13 23:19:46 -0500191 case ACPI_S5:
Hannah Williamsba0fc472016-05-04 18:15:49 -0700192 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
193
194 /* Disable all GPE */
Shaunak Saha83e98232017-06-22 10:38:30 -0700195 pmc_disable_all_gpe();
Subrata Banik47a655c2017-12-14 18:22:13 +0530196 /* Set which state system will be after power reapplied */
Nico Huber3e786b52019-08-05 21:12:33 +0200197 pmc_set_power_failure_state(false);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700198 /* also iterates over all bridges on bus 0 */
199 busmaster_disable_on_bus(0);
Furquan Shaikh10c3b962018-03-01 15:21:05 -0800200
201 /*
202 * Some platforms (e.g. Chromebooks) have observed race between
203 * SLP SMI and PWRBTN SMI because of the way these SMIs are
204 * triggered on power button press. Allow adding a delay before
205 * triggering sleep enable for S5, so that power button
206 * interrupt does not result into immediate wake.
207 */
208 mdelay(CONFIG_SOC_INTEL_COMMON_BLOCK_SMM_S5_DELAY_MS);
209
210 /*
211 * Ensure any pending power button status bits are cleared as
212 * the system is entering S5 and doesn't want to be woken up
213 * right away from older power button events.
214 */
215 pmc_clear_pm1_status();
216
Hannah Williamsba0fc472016-05-04 18:15:49 -0700217 break;
218 default:
219 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
220 break;
221 }
Shaunak Sahade9fed42016-08-22 22:05:35 -0700222
Hannah Williamsba0fc472016-05-04 18:15:49 -0700223 /*
224 * Write back to the SLP register to cause the originally intended
225 * event again. We need to set BIT13 (SLP_EN) though to make the
226 * sleep happen.
227 */
Shaunak Saha83e98232017-06-22 10:38:30 -0700228 pmc_enable_pm1_control(SLP_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700229
230 /* Make sure to stop executing code here for S3/S4/S5 */
Aaron Durbin56db47f2016-07-13 23:19:46 -0500231 if (slp_typ >= ACPI_S3)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700232 hlt();
233
234 /*
235 * In most sleep states, the code flow of this function ends at
236 * the line above. However, if we entered sleep state S1 and wake
237 * up again, we will continue to execute code in this function.
238 */
Subrata Banik47a655c2017-12-14 18:22:13 +0530239 if (pmc_read_pm1_control() & SCI_EN) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700240 /* The OS is not an ACPI OS, so we set the state to S0 */
Shaunak Saha83e98232017-06-22 10:38:30 -0700241 pmc_disable_pm1_control(SLP_EN | SLP_TYP);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700242 }
243}
244
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700245static void southbridge_smi_gsmi(
246 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700247{
248 u8 sub_command, ret;
249 void *io_smi = NULL;
250 uint32_t reg_ebx;
251
Patrick Georgid61839c2018-12-03 16:10:33 +0100252 io_smi = find_save_state(save_state_ops, APM_CNT_ELOG_GSMI);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700253 if (!io_smi)
254 return;
255 /* Command and return value in EAX */
256 sub_command = (save_state_ops->get_reg(io_smi, RAX) >> 8)
257 & 0xff;
258
259 /* Parameter buffer in EBX */
260 reg_ebx = save_state_ops->get_reg(io_smi, RBX);
261
262 /* drivers/elog/gsmi.c */
Aaron Durbin2d45c282016-08-05 21:26:53 -0500263 ret = gsmi_exec(sub_command, &reg_ebx);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700264 save_state_ops->set_reg(io_smi, RAX, ret);
265}
266
Angel Pons23222272021-03-25 13:02:22 +0100267static void set_insmm_sts(const bool enable_writes)
268{
269 msr_t msr = {
270 .lo = read32p(0xfed30880),
271 .hi = 0,
272 };
273 if (enable_writes)
274 msr.lo |= 1;
275 else
276 msr.lo &= ~1;
277
278 wrmsr(MSR_SPCL_CHIPSET_USAGE, msr);
279}
280
Patrick Georgi9360fea2018-03-14 21:11:21 +0100281static void southbridge_smi_store(
282 const struct smm_save_state_ops *save_state_ops)
283{
284 u8 sub_command, ret;
285 void *io_smi;
286 uint32_t reg_ebx;
287
Patrick Georgi9fca2972018-12-03 16:13:17 +0100288 io_smi = find_save_state(save_state_ops, APM_CNT_SMMSTORE);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100289 if (!io_smi)
290 return;
291 /* Command and return value in EAX */
292 sub_command = (save_state_ops->get_reg(io_smi, RAX) >> 8) & 0xff;
293
294 /* Parameter buffer in EBX */
295 reg_ebx = save_state_ops->get_reg(io_smi, RBX);
296
Angel Ponsd21b4632021-02-10 17:12:05 +0100297 const bool wp_enabled = !fast_spi_wpd_status();
298 if (wp_enabled) {
Angel Pons23222272021-03-25 13:02:22 +0100299 set_insmm_sts(true);
Angel Ponsd21b4632021-02-10 17:12:05 +0100300 fast_spi_disable_wp();
301 /* Not clearing SPI sync SMI status here results in hangs */
302 fast_spi_clear_sync_smi_status();
303 }
304
Patrick Georgi9360fea2018-03-14 21:11:21 +0100305 /* drivers/smmstore/smi.c */
Patrick Rudolphee38cce2020-11-30 13:54:26 +0100306 ret = smmstore_exec(sub_command, (void *)(uintptr_t)reg_ebx);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100307 save_state_ops->set_reg(io_smi, RAX, ret);
Angel Ponsd21b4632021-02-10 17:12:05 +0100308
Angel Pons23222272021-03-25 13:02:22 +0100309 if (wp_enabled) {
Angel Ponsd21b4632021-02-10 17:12:05 +0100310 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100311 set_insmm_sts(false);
312 }
Patrick Georgi9360fea2018-03-14 21:11:21 +0100313}
314
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700315__weak const struct gpio_lock_config *soc_gpio_lock_config(size_t *num)
316{
317 *num = 0;
318 return NULL;
319}
320
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700321static void soc_lock_gpios(void)
322{
323 const struct gpio_lock_config *soc_gpios;
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700324 size_t soc_gpio_num;
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700325
326 /* get list of gpios from SoC */
327 soc_gpios = soc_gpio_lock_config(&soc_gpio_num);
328
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700329 /* Lock any soc requested gpios */
330 if (soc_gpio_num)
331 gpio_lock_pads(soc_gpios, soc_gpio_num);
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700332}
333
Hannah Williamsba0fc472016-05-04 18:15:49 -0700334static void finalize(void)
335{
336 static int finalize_done;
337
338 if (finalize_done) {
339 printk(BIOS_DEBUG, "SMM already finalized.\n");
340 return;
341 }
342 finalize_done = 1;
343
Julius Wernercd49cce2019-03-05 16:53:33 -0800344 if (CONFIG(SPI_FLASH_SMM))
Subrata Banik47a655c2017-12-14 18:22:13 +0530345 /* Re-init SPI driver to handle locked BAR */
346 fast_spi_init();
Subrata Banikf9eaede2018-02-19 13:57:02 +0530347
Angel Pons23222272021-03-25 13:02:22 +0100348 if (CONFIG(BOOTMEDIA_SMM_BWP)) {
Angel Ponsd21b4632021-02-10 17:12:05 +0100349 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100350 set_insmm_sts(false);
351 }
Angel Ponsd21b4632021-02-10 17:12:05 +0100352
Aseda Aboagye63356052021-06-17 12:10:33 -0700353 /*
354 * HECI is disabled in smihandler_soc_at_finalize() which also locks down the side band
355 * interface. Some boards may require this interface in mainboard_smi_finalize(),
356 * therefore, this call must precede smihandler_soc_at_finalize().
357 */
358 mainboard_smi_finalize();
359
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700360 /* Lock down all GPIOs that may have been requested by the SoC and/or the mainboard. */
361 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS))
362 soc_lock_gpios();
363
Subrata Banikf9eaede2018-02-19 13:57:02 +0530364 /* Specific SOC SMI handler during ramstage finalize phase */
365 smihandler_soc_at_finalize();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700366}
367
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700368void smihandler_southbridge_apmc(
369 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700370{
371 uint8_t reg8;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700372
Kyösti Mälkki9a1620f2021-01-08 13:27:33 +0200373 reg8 = apm_get_apmc();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700374 switch (reg8) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700375 case APM_CNT_ACPI_DISABLE:
Shaunak Saha83e98232017-06-22 10:38:30 -0700376 pmc_disable_pm1_control(SCI_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700377 break;
378 case APM_CNT_ACPI_ENABLE:
Shaunak Saha83e98232017-06-22 10:38:30 -0700379 pmc_enable_pm1_control(SCI_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700380 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100381 case APM_CNT_ELOG_GSMI:
Julius Wernercd49cce2019-03-05 16:53:33 -0800382 if (CONFIG(ELOG_GSMI))
Hannah Williamsba0fc472016-05-04 18:15:49 -0700383 southbridge_smi_gsmi(save_state_ops);
384 break;
Patrick Georgi9fca2972018-12-03 16:13:17 +0100385 case APM_CNT_SMMSTORE:
Julius Wernercd49cce2019-03-05 16:53:33 -0800386 if (CONFIG(SMMSTORE))
Patrick Georgi9360fea2018-03-14 21:11:21 +0100387 southbridge_smi_store(save_state_ops);
388 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700389 case APM_CNT_FINALIZE:
390 finalize();
391 break;
392 }
393
394 mainboard_smi_apmc(reg8);
395}
396
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700397void smihandler_southbridge_pm1(
398 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700399{
Shaunak Saha83e98232017-06-22 10:38:30 -0700400 uint16_t pm1_sts = pmc_clear_pm1_status();
Subrata Banik47a655c2017-12-14 18:22:13 +0530401 u16 pm1_en = pmc_read_pm1_enable();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700402
403 /*
404 * While OSPM is not active, poweroff immediately
405 * on a power button event.
406 */
Subrata Banik47a655c2017-12-14 18:22:13 +0530407 if ((pm1_sts & PWRBTN_STS) && (pm1_en & PWRBTN_EN)) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700408 /* power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200409 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Patrick Rudolphee38cce2020-11-30 13:54:26 +0100410 pmc_disable_pm1_control(~0);
Shaunak Saha83e98232017-06-22 10:38:30 -0700411 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
Hannah Williamsba0fc472016-05-04 18:15:49 -0700412 }
413}
414
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700415void smihandler_southbridge_gpe0(
416 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700417{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700418 pmc_clear_all_gpe_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700419}
420
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700421void smihandler_southbridge_tco(
422 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700423{
Shaunak Saha83e98232017-06-22 10:38:30 -0700424 uint32_t tco_sts = pmc_clear_tco_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700425
Angel Pons967753f2021-02-15 17:44:09 +0100426 /*
427 * SPI synchronous SMIs are TCO SMIs, but they do not have a status
428 * bit in the TCO_STS register. Furthermore, the TCO_STS bit in the
429 * SMI_STS register is continually set until the SMI handler clears
430 * the SPI synchronous SMI status bit in the SPI controller. To not
431 * risk missing any other TCO SMIs, do not clear the TCO_STS bit in
432 * this SMI handler invocation. If the TCO_STS bit remains set when
433 * returning from SMM, another SMI immediately happens which clears
434 * the TCO_STS bit and handles any pending events.
435 */
436 fast_spi_clear_sync_smi_status();
437
Angel Ponsd21b4632021-02-10 17:12:05 +0100438 /* If enabled, enforce SMM BIOS write protection */
439 if (CONFIG(BOOTMEDIA_SMM_BWP) && fast_spi_wpd_status()) {
440 /*
441 * BWE is RW, so the SMI was caused by a
442 * write to BWE, not by a write to the BIOS
443 *
444 * This is the place where we notice someone
445 * is trying to tinker with the BIOS. We are
446 * trying to be nice and just ignore it. A more
447 * resolute answer would be to power down the
448 * box.
449 */
450 printk(BIOS_DEBUG, "Switching SPI back to RO\n");
451 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100452 set_insmm_sts(false);
Angel Ponsd21b4632021-02-10 17:12:05 +0100453 }
454
Hannah Williamsba0fc472016-05-04 18:15:49 -0700455 /* Any TCO event? */
456 if (!tco_sts)
457 return;
458
459 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
460 /* Handle TCO timeout */
461 printk(BIOS_DEBUG, "TCO Timeout.\n");
462 }
Michael Niewöhnerccde6be2020-03-03 20:48:30 +0100463
464 if (tco_sts & (TCO_INTRD_DET << 16)) { /* INTRUDER# assertion */
465 /*
466 * Handle intrusion event
467 * If we ever get here, probably the case has been opened.
468 */
469 printk(BIOS_CRIT, "Case intrusion detected.\n");
470 }
Hannah Williamsba0fc472016-05-04 18:15:49 -0700471}
472
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700473void smihandler_southbridge_periodic(
474 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700475{
476 uint32_t reg32;
477
Shaunak Saha83e98232017-06-22 10:38:30 -0700478 reg32 = pmc_get_smi_en();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700479
480 /* Are periodic SMIs enabled? */
481 if ((reg32 & PERIODIC_EN) == 0)
482 return;
483 printk(BIOS_DEBUG, "Periodic SMI.\n");
Michał Żygowskid627f7b2022-10-28 15:44:48 +0200484
485 if (CONFIG(SOC_INTEL_COMMON_OC_WDT_RELOAD_IN_PERIODIC_SMI))
486 oc_wdt_reload();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700487}
488
Brandon Breitenstein60ce6152017-08-08 15:30:41 -0700489void smihandler_southbridge_gpi(
490 const struct smm_save_state_ops *save_state_ops)
491{
492 struct gpi_status smi_sts;
493
494 gpi_clear_get_smi_status(&smi_sts);
495 mainboard_smi_gpi_handler(&smi_sts);
496
497 /* Clear again after mainboard handler */
498 gpi_clear_get_smi_status(&smi_sts);
499}
500
Brandon Breitenstein60ce6152017-08-08 15:30:41 -0700501void smihandler_southbridge_espi(
502 const struct smm_save_state_ops *save_state_ops)
503{
504 mainboard_smi_espi_handler();
505}
506
Hannah Williamsba0fc472016-05-04 18:15:49 -0700507void southbridge_smi_handler(void)
508{
509 int i;
510 uint32_t smi_sts;
511 const struct smm_save_state_ops *save_state_ops;
512
513 /*
514 * We need to clear the SMI status registers, or we won't see what's
515 * happening in the following calls.
516 */
Shaunak Saha83e98232017-06-22 10:38:30 -0700517 smi_sts = pmc_clear_smi_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700518
Subrata Banik47a655c2017-12-14 18:22:13 +0530519 /*
Angel Pons51775072021-02-15 15:15:22 +0100520 * When the SCI_EN bit is set, PM1 and GPE0 events will trigger a SCI
521 * instead of a SMI#. However, SMI_STS bits PM1_STS and GPE0_STS can
522 * still be set. Therefore, when SCI_EN is set, ignore PM1 and GPE0
523 * events in the SMI# handler, as these events have triggered a SCI.
524 * Do not ignore any other SMI# types, since they cannot cause a SCI.
Subrata Banik47a655c2017-12-14 18:22:13 +0530525 */
526 if (pmc_read_pm1_control() & SCI_EN)
Angel Pons51775072021-02-15 15:15:22 +0100527 smi_sts &= ~(1 << PM1_STS_BIT | 1 << GPE0_STS_BIT);
Subrata Banik47a655c2017-12-14 18:22:13 +0530528
529 if (!smi_sts)
530 return;
531
Hannah Williamsba0fc472016-05-04 18:15:49 -0700532 save_state_ops = get_smm_save_state_ops();
533
534 /* Call SMI sub handler for each of the status bits */
535 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
536 if (!(smi_sts & (1 << i)))
537 continue;
538
539 if (southbridge_smi[i] != NULL) {
540 southbridge_smi[i](save_state_ops);
541 } else {
542 printk(BIOS_DEBUG,
543 "SMI_STS[%d] occurred, but no "
544 "handler available.\n", i);
545 }
546 }
547}
548
549static uint32_t em64t100_smm_save_state_get_io_misc_info(void *state)
550{
551 em64t100_smm_state_save_area_t *smm_state = state;
552 return smm_state->io_misc_info;
553}
554
555static uint64_t em64t100_smm_save_state_get_reg(void *state, enum smm_reg reg)
556{
557 uintptr_t value = 0;
558 em64t100_smm_state_save_area_t *smm_state = state;
559
Lee Leahy68ab0b52017-03-10 13:42:34 -0800560 switch (reg) {
561 case RAX:
562 value = smm_state->rax;
563 break;
564 case RBX:
565 value = smm_state->rbx;
566 break;
567 case RCX:
568 value = smm_state->rcx;
569 break;
570 case RDX:
571 value = smm_state->rdx;
572 break;
573 default:
574 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700575 }
576 return value;
577}
578
Lee Leahy006d73d2017-03-10 11:25:57 -0800579static void em64t100_smm_save_state_set_reg(void *state, enum smm_reg reg,
580 uint64_t val)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700581{
582 em64t100_smm_state_save_area_t *smm_state = state;
Lee Leahy68ab0b52017-03-10 13:42:34 -0800583 switch (reg) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700584 case RAX:
585 smm_state->rax = val;
586 break;
587 case RBX:
588 smm_state->rbx = val;
589 break;
590 case RCX:
591 smm_state->rcx = val;
592 break;
593 case RDX:
594 smm_state->rdx = val;
595 break;
596 default:
597 break;
598 }
599}
600
601static uint32_t em64t101_smm_save_state_get_io_misc_info(void *state)
602{
603 em64t101_smm_state_save_area_t *smm_state = state;
604 return smm_state->io_misc_info;
605}
606
607static uint64_t em64t101_smm_save_state_get_reg(void *state, enum smm_reg reg)
608{
609 uintptr_t value = 0;
610 em64t101_smm_state_save_area_t *smm_state = state;
611
Lee Leahy68ab0b52017-03-10 13:42:34 -0800612 switch (reg) {
613 case RAX:
614 value = smm_state->rax;
615 break;
616 case RBX:
617 value = smm_state->rbx;
618 break;
619 case RCX:
620 value = smm_state->rcx;
621 break;
622 case RDX:
623 value = smm_state->rdx;
624 break;
625 default:
626 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700627 }
628 return value;
629}
630
Lee Leahy006d73d2017-03-10 11:25:57 -0800631static void em64t101_smm_save_state_set_reg(void *state, enum smm_reg reg,
632 uint64_t val)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700633{
634 em64t101_smm_state_save_area_t *smm_state = state;
Lee Leahy68ab0b52017-03-10 13:42:34 -0800635 switch (reg) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700636 case RAX:
637 smm_state->rax = val;
638 break;
639 case RBX:
640 smm_state->rbx = val;
641 break;
642 case RCX:
643 smm_state->rcx = val;
644 break;
645 case RDX:
646 smm_state->rdx = val;
647 break;
648 default:
649 break;
650 }
651}
652
653const struct smm_save_state_ops em64t100_smm_ops = {
654 .get_io_misc_info = em64t100_smm_save_state_get_io_misc_info,
655 .get_reg = em64t100_smm_save_state_get_reg,
656 .set_reg = em64t100_smm_save_state_set_reg,
657};
658
659const struct smm_save_state_ops em64t101_smm_ops = {
660 .get_io_misc_info = em64t101_smm_save_state_get_io_misc_info,
661 .get_reg = em64t101_smm_save_state_get_reg,
662 .set_reg = em64t101_smm_save_state_set_reg,
663};