blob: 6c271968c1573d2306ea71f4037bfc90415d5bc8 [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);
Subrata Banikb27cfd62023-12-20 20:36:05 +0530300 /*
301 * As per BWG, clearing "SPI_BIOS_CONTROL_SYNC_SS"
302 * bit is a must prior setting SPI_BIOS_CONTROL_WPD" bit
303 * to avoid 3-strike error.
304 */
Angel Ponsd21b4632021-02-10 17:12:05 +0100305 fast_spi_clear_sync_smi_status();
Subrata Banikb27cfd62023-12-20 20:36:05 +0530306 fast_spi_disable_wp();
Angel Ponsd21b4632021-02-10 17:12:05 +0100307 }
308
Patrick Georgi9360fea2018-03-14 21:11:21 +0100309 /* drivers/smmstore/smi.c */
Patrick Rudolphee38cce2020-11-30 13:54:26 +0100310 ret = smmstore_exec(sub_command, (void *)(uintptr_t)reg_ebx);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100311 save_state_ops->set_reg(io_smi, RAX, ret);
Angel Ponsd21b4632021-02-10 17:12:05 +0100312
Angel Pons23222272021-03-25 13:02:22 +0100313 if (wp_enabled) {
Angel Ponsd21b4632021-02-10 17:12:05 +0100314 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100315 set_insmm_sts(false);
316 }
Patrick Georgi9360fea2018-03-14 21:11:21 +0100317}
318
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700319__weak const struct gpio_lock_config *soc_gpio_lock_config(size_t *num)
320{
321 *num = 0;
322 return NULL;
323}
324
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700325static void soc_lock_gpios(void)
326{
327 const struct gpio_lock_config *soc_gpios;
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700328 size_t soc_gpio_num;
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700329
330 /* get list of gpios from SoC */
331 soc_gpios = soc_gpio_lock_config(&soc_gpio_num);
332
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700333 /* Lock any soc requested gpios */
334 if (soc_gpio_num)
335 gpio_lock_pads(soc_gpios, soc_gpio_num);
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700336}
337
Hannah Williamsba0fc472016-05-04 18:15:49 -0700338static void finalize(void)
339{
340 static int finalize_done;
341
342 if (finalize_done) {
343 printk(BIOS_DEBUG, "SMM already finalized.\n");
344 return;
345 }
346 finalize_done = 1;
347
Julius Wernercd49cce2019-03-05 16:53:33 -0800348 if (CONFIG(SPI_FLASH_SMM))
Subrata Banik47a655c2017-12-14 18:22:13 +0530349 /* Re-init SPI driver to handle locked BAR */
350 fast_spi_init();
Subrata Banikf9eaede2018-02-19 13:57:02 +0530351
Angel Pons23222272021-03-25 13:02:22 +0100352 if (CONFIG(BOOTMEDIA_SMM_BWP)) {
Angel Ponsd21b4632021-02-10 17:12:05 +0100353 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100354 set_insmm_sts(false);
355 }
Angel Ponsd21b4632021-02-10 17:12:05 +0100356
Aseda Aboagye63356052021-06-17 12:10:33 -0700357 /*
358 * HECI is disabled in smihandler_soc_at_finalize() which also locks down the side band
359 * interface. Some boards may require this interface in mainboard_smi_finalize(),
360 * therefore, this call must precede smihandler_soc_at_finalize().
361 */
362 mainboard_smi_finalize();
363
Nick Vaccarob6f29c92021-10-12 17:26:52 -0700364 /* Lock down all GPIOs that may have been requested by the SoC and/or the mainboard. */
365 if (CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS))
366 soc_lock_gpios();
367
Subrata Banikf9eaede2018-02-19 13:57:02 +0530368 /* Specific SOC SMI handler during ramstage finalize phase */
369 smihandler_soc_at_finalize();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700370}
371
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700372void smihandler_southbridge_apmc(
373 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700374{
375 uint8_t reg8;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700376
Kyösti Mälkki9a1620f2021-01-08 13:27:33 +0200377 reg8 = apm_get_apmc();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700378 switch (reg8) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700379 case APM_CNT_ACPI_DISABLE:
Shaunak Saha83e98232017-06-22 10:38:30 -0700380 pmc_disable_pm1_control(SCI_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700381 break;
382 case APM_CNT_ACPI_ENABLE:
Shaunak Saha83e98232017-06-22 10:38:30 -0700383 pmc_enable_pm1_control(SCI_EN);
Hannah Williamsba0fc472016-05-04 18:15:49 -0700384 break;
Patrick Georgid61839c2018-12-03 16:10:33 +0100385 case APM_CNT_ELOG_GSMI:
Julius Wernercd49cce2019-03-05 16:53:33 -0800386 if (CONFIG(ELOG_GSMI))
Hannah Williamsba0fc472016-05-04 18:15:49 -0700387 southbridge_smi_gsmi(save_state_ops);
388 break;
Patrick Georgi9fca2972018-12-03 16:13:17 +0100389 case APM_CNT_SMMSTORE:
Julius Wernercd49cce2019-03-05 16:53:33 -0800390 if (CONFIG(SMMSTORE))
Patrick Georgi9360fea2018-03-14 21:11:21 +0100391 southbridge_smi_store(save_state_ops);
392 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700393 case APM_CNT_FINALIZE:
394 finalize();
395 break;
396 }
397
398 mainboard_smi_apmc(reg8);
399}
400
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700401void smihandler_southbridge_pm1(
402 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700403{
Shaunak Saha83e98232017-06-22 10:38:30 -0700404 uint16_t pm1_sts = pmc_clear_pm1_status();
Subrata Banik47a655c2017-12-14 18:22:13 +0530405 u16 pm1_en = pmc_read_pm1_enable();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700406
407 /*
408 * While OSPM is not active, poweroff immediately
409 * on a power button event.
410 */
Subrata Banik47a655c2017-12-14 18:22:13 +0530411 if ((pm1_sts & PWRBTN_STS) && (pm1_en & PWRBTN_EN)) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700412 /* power button pressed */
Kyösti Mälkki9dd1a122019-11-06 11:04:27 +0200413 elog_gsmi_add_event(ELOG_TYPE_POWER_BUTTON);
Patrick Rudolphee38cce2020-11-30 13:54:26 +0100414 pmc_disable_pm1_control(~0);
Shaunak Saha83e98232017-06-22 10:38:30 -0700415 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
Hannah Williamsba0fc472016-05-04 18:15:49 -0700416 }
417}
418
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700419void smihandler_southbridge_gpe0(
420 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700421{
Furquan Shaikhc4e652f2017-10-11 14:44:29 -0700422 pmc_clear_all_gpe_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700423}
424
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700425void smihandler_southbridge_tco(
426 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700427{
Shaunak Saha83e98232017-06-22 10:38:30 -0700428 uint32_t tco_sts = pmc_clear_tco_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700429
Angel Pons967753f2021-02-15 17:44:09 +0100430 /*
431 * SPI synchronous SMIs are TCO SMIs, but they do not have a status
432 * bit in the TCO_STS register. Furthermore, the TCO_STS bit in the
433 * SMI_STS register is continually set until the SMI handler clears
434 * the SPI synchronous SMI status bit in the SPI controller. To not
435 * risk missing any other TCO SMIs, do not clear the TCO_STS bit in
436 * this SMI handler invocation. If the TCO_STS bit remains set when
437 * returning from SMM, another SMI immediately happens which clears
438 * the TCO_STS bit and handles any pending events.
439 */
440 fast_spi_clear_sync_smi_status();
441
Angel Ponsd21b4632021-02-10 17:12:05 +0100442 /* If enabled, enforce SMM BIOS write protection */
443 if (CONFIG(BOOTMEDIA_SMM_BWP) && fast_spi_wpd_status()) {
444 /*
445 * BWE is RW, so the SMI was caused by a
446 * write to BWE, not by a write to the BIOS
447 *
448 * This is the place where we notice someone
449 * is trying to tinker with the BIOS. We are
450 * trying to be nice and just ignore it. A more
451 * resolute answer would be to power down the
452 * box.
453 */
454 printk(BIOS_DEBUG, "Switching SPI back to RO\n");
455 fast_spi_enable_wp();
Angel Pons23222272021-03-25 13:02:22 +0100456 set_insmm_sts(false);
Angel Ponsd21b4632021-02-10 17:12:05 +0100457 }
458
Hannah Williamsba0fc472016-05-04 18:15:49 -0700459 /* Any TCO event? */
460 if (!tco_sts)
461 return;
462
463 if (tco_sts & TCO_TIMEOUT) { /* TIMEOUT */
464 /* Handle TCO timeout */
465 printk(BIOS_DEBUG, "TCO Timeout.\n");
466 }
Michael Niewöhnerccde6be2020-03-03 20:48:30 +0100467
468 if (tco_sts & (TCO_INTRD_DET << 16)) { /* INTRUDER# assertion */
469 /*
470 * Handle intrusion event
471 * If we ever get here, probably the case has been opened.
472 */
473 printk(BIOS_CRIT, "Case intrusion detected.\n");
474 }
Hannah Williamsba0fc472016-05-04 18:15:49 -0700475}
476
Brandon Breitensteina86d1b82017-06-08 17:32:02 -0700477void smihandler_southbridge_periodic(
478 const struct smm_save_state_ops *save_state_ops)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700479{
480 uint32_t reg32;
481
Shaunak Saha83e98232017-06-22 10:38:30 -0700482 reg32 = pmc_get_smi_en();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700483
484 /* Are periodic SMIs enabled? */
485 if ((reg32 & PERIODIC_EN) == 0)
486 return;
487 printk(BIOS_DEBUG, "Periodic SMI.\n");
Michał Żygowskid627f7b2022-10-28 15:44:48 +0200488
489 if (CONFIG(SOC_INTEL_COMMON_OC_WDT_RELOAD_IN_PERIODIC_SMI))
490 oc_wdt_reload();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700491}
492
Brandon Breitenstein60ce6152017-08-08 15:30:41 -0700493void smihandler_southbridge_gpi(
494 const struct smm_save_state_ops *save_state_ops)
495{
496 struct gpi_status smi_sts;
497
498 gpi_clear_get_smi_status(&smi_sts);
499 mainboard_smi_gpi_handler(&smi_sts);
500
501 /* Clear again after mainboard handler */
502 gpi_clear_get_smi_status(&smi_sts);
503}
504
Brandon Breitenstein60ce6152017-08-08 15:30:41 -0700505void smihandler_southbridge_espi(
506 const struct smm_save_state_ops *save_state_ops)
507{
508 mainboard_smi_espi_handler();
509}
510
Hannah Williamsba0fc472016-05-04 18:15:49 -0700511void southbridge_smi_handler(void)
512{
513 int i;
514 uint32_t smi_sts;
515 const struct smm_save_state_ops *save_state_ops;
516
517 /*
518 * We need to clear the SMI status registers, or we won't see what's
519 * happening in the following calls.
520 */
Shaunak Saha83e98232017-06-22 10:38:30 -0700521 smi_sts = pmc_clear_smi_status();
Hannah Williamsba0fc472016-05-04 18:15:49 -0700522
Subrata Banik47a655c2017-12-14 18:22:13 +0530523 /*
Angel Pons51775072021-02-15 15:15:22 +0100524 * When the SCI_EN bit is set, PM1 and GPE0 events will trigger a SCI
525 * instead of a SMI#. However, SMI_STS bits PM1_STS and GPE0_STS can
526 * still be set. Therefore, when SCI_EN is set, ignore PM1 and GPE0
527 * events in the SMI# handler, as these events have triggered a SCI.
528 * Do not ignore any other SMI# types, since they cannot cause a SCI.
Subrata Banik47a655c2017-12-14 18:22:13 +0530529 */
530 if (pmc_read_pm1_control() & SCI_EN)
Angel Pons51775072021-02-15 15:15:22 +0100531 smi_sts &= ~(1 << PM1_STS_BIT | 1 << GPE0_STS_BIT);
Subrata Banik47a655c2017-12-14 18:22:13 +0530532
533 if (!smi_sts)
534 return;
535
Hannah Williamsba0fc472016-05-04 18:15:49 -0700536 save_state_ops = get_smm_save_state_ops();
537
538 /* Call SMI sub handler for each of the status bits */
539 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
540 if (!(smi_sts & (1 << i)))
541 continue;
542
543 if (southbridge_smi[i] != NULL) {
544 southbridge_smi[i](save_state_ops);
545 } else {
546 printk(BIOS_DEBUG,
547 "SMI_STS[%d] occurred, but no "
548 "handler available.\n", i);
549 }
550 }
551}
552
553static uint32_t em64t100_smm_save_state_get_io_misc_info(void *state)
554{
555 em64t100_smm_state_save_area_t *smm_state = state;
556 return smm_state->io_misc_info;
557}
558
559static uint64_t em64t100_smm_save_state_get_reg(void *state, enum smm_reg reg)
560{
561 uintptr_t value = 0;
562 em64t100_smm_state_save_area_t *smm_state = state;
563
Lee Leahy68ab0b52017-03-10 13:42:34 -0800564 switch (reg) {
565 case RAX:
566 value = smm_state->rax;
567 break;
568 case RBX:
569 value = smm_state->rbx;
570 break;
571 case RCX:
572 value = smm_state->rcx;
573 break;
574 case RDX:
575 value = smm_state->rdx;
576 break;
577 default:
578 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700579 }
580 return value;
581}
582
Lee Leahy006d73d2017-03-10 11:25:57 -0800583static void em64t100_smm_save_state_set_reg(void *state, enum smm_reg reg,
584 uint64_t val)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700585{
586 em64t100_smm_state_save_area_t *smm_state = state;
Lee Leahy68ab0b52017-03-10 13:42:34 -0800587 switch (reg) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700588 case RAX:
589 smm_state->rax = val;
590 break;
591 case RBX:
592 smm_state->rbx = val;
593 break;
594 case RCX:
595 smm_state->rcx = val;
596 break;
597 case RDX:
598 smm_state->rdx = val;
599 break;
600 default:
601 break;
602 }
603}
604
605static uint32_t em64t101_smm_save_state_get_io_misc_info(void *state)
606{
607 em64t101_smm_state_save_area_t *smm_state = state;
608 return smm_state->io_misc_info;
609}
610
611static uint64_t em64t101_smm_save_state_get_reg(void *state, enum smm_reg reg)
612{
613 uintptr_t value = 0;
614 em64t101_smm_state_save_area_t *smm_state = state;
615
Lee Leahy68ab0b52017-03-10 13:42:34 -0800616 switch (reg) {
617 case RAX:
618 value = smm_state->rax;
619 break;
620 case RBX:
621 value = smm_state->rbx;
622 break;
623 case RCX:
624 value = smm_state->rcx;
625 break;
626 case RDX:
627 value = smm_state->rdx;
628 break;
629 default:
630 break;
Hannah Williamsba0fc472016-05-04 18:15:49 -0700631 }
632 return value;
633}
634
Lee Leahy006d73d2017-03-10 11:25:57 -0800635static void em64t101_smm_save_state_set_reg(void *state, enum smm_reg reg,
636 uint64_t val)
Hannah Williamsba0fc472016-05-04 18:15:49 -0700637{
638 em64t101_smm_state_save_area_t *smm_state = state;
Lee Leahy68ab0b52017-03-10 13:42:34 -0800639 switch (reg) {
Hannah Williamsba0fc472016-05-04 18:15:49 -0700640 case RAX:
641 smm_state->rax = val;
642 break;
643 case RBX:
644 smm_state->rbx = val;
645 break;
646 case RCX:
647 smm_state->rcx = val;
648 break;
649 case RDX:
650 smm_state->rdx = val;
651 break;
652 default:
653 break;
654 }
655}
656
657const struct smm_save_state_ops em64t100_smm_ops = {
658 .get_io_misc_info = em64t100_smm_save_state_get_io_misc_info,
659 .get_reg = em64t100_smm_save_state_get_reg,
660 .set_reg = em64t100_smm_save_state_set_reg,
661};
662
663const struct smm_save_state_ops em64t101_smm_ops = {
664 .get_io_misc_info = em64t101_smm_save_state_get_io_misc_info,
665 .get_reg = em64t101_smm_save_state_get_reg,
666 .set_reg = em64t101_smm_save_state_set_reg,
667};