1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <device/pci_ops.h>
7 #include <console/console.h>
8 #include <cpu/x86/cache.h>
9 #include <cpu/x86/smm.h>
10 #include <cpu/intel/em64t100_save_state.h>
11 #include <device/pci_def.h>
12 #include <intelblocks/fast_spi.h>
14 #include <spi-generic.h>
15 #include <soc/iomap.h>
16 #include <soc/soc_util.h>
20 void southbridge_smi_set_eos(void)
25 static void busmaster_disable_on_bus(int bus
)
31 for (slot
= 0; slot
< 0x20; slot
++) {
32 for (func
= 0; func
< 8; func
++) {
35 pci_devfn_t dev
= PCI_DEV(bus
, slot
, func
);
36 val
= pci_read_config32(dev
, PCI_VENDOR_ID
);
38 if (val
== 0xffffffff || val
== 0x00000000 ||
39 val
== 0x0000ffff || val
== 0xffff0000)
42 /* Disable Bus Mastering for this one device */
43 reg16
= pci_read_config16(dev
, PCI_COMMAND
);
44 reg16
&= ~PCI_COMMAND_MASTER
;
45 pci_write_config16(dev
, PCI_COMMAND
, reg16
);
47 /* If this is a bridge, then follow it. */
48 hdr
= pci_read_config8(dev
, PCI_HEADER_TYPE
);
50 if (hdr
== PCI_HEADER_TYPE_BRIDGE
||
51 hdr
== PCI_HEADER_TYPE_CARDBUS
) {
53 buses
= pci_read_config32(dev
, PCI_PRIMARY_BUS
);
54 busmaster_disable_on_bus((buses
>> 8) & 0xff);
60 static void southbridge_smi_sleep(void)
64 uint16_t pmbase
= get_pmbase();
66 /* First, disable further SMIs */
67 disable_smi(SLP_SMI_EN
);
69 /* Figure out SLP_TYP */
70 reg32
= inl((uint16_t)(pmbase
+ PM1_CNT
));
71 printk(BIOS_SPEW
, "SMI#: SLP = 0x%08x\n", reg32
);
72 slp_typ
= (reg32
>> 10) & 7;
74 /* Do any mainboard sleep handling */
75 mainboard_smi_sleep((uint8_t)(slp_typ
- 2));
82 printk(BIOS_DEBUG
, "SMI#: Entering S0 (On)\n");
85 printk(BIOS_DEBUG
, "SMI#: Entering S1 (Assert STPCLK#)\n");
88 printk(BIOS_DEBUG
, "SMI#: Entering S3 (Suspend-To-RAM)\n");
90 /* Invalidate the cache before going to S3 */
94 printk(BIOS_DEBUG
, "SMI#: Entering S4 (Suspend-To-Disk)\n");
97 printk(BIOS_DEBUG
, "SMI#: Entering S5 (Soft Power off)\n");
102 /* also iterates over all bridges on bus 0 */
103 busmaster_disable_on_bus(0);
106 printk(BIOS_DEBUG
, "SMI#: ERROR: SLP_TYP reserved\n");
110 /* Write back to the SLP register to cause the originally intended
111 * event again. We need to set BIT13 (SLP_EN) though to make the
114 enable_pm1_control(SLP_EN
);
116 /* Make sure to stop executing code here for S3/S4/S5 */
120 /* In most sleep states, the code flow of this function ends at
121 * the line above. However, if we entered sleep state S1 and wake
122 * up again, we will continue to execute code in this function.
124 reg32
= inl((uint16_t)(pmbase
+ PM1_CNT
));
125 if (reg32
& SCI_EN
) {
126 /* The OS is not an ACPI OS, so we set the state to S0 */
127 disable_pm1_control(SLP_EN
| SLP_TYP
);
132 * Look for Synchronous IO SMI and use save state from that
133 * core in case we are not running on the same core that
134 * initiated the IO transaction.
136 static em64t100_smm_state_save_area_t
*smi_apmc_find_state_save(uint8_t cmd
)
138 em64t100_smm_state_save_area_t
*state
;
141 /* Check all nodes looking for the one that issued the IO */
142 for (node
= 0; node
< CONFIG_MAX_CPUS
; node
++) {
143 state
= smm_get_save_state(node
);
145 /* Check for Synchronous IO (bit0==1) */
146 if (!(state
->io_misc_info
& (1 << 0)))
149 /* Make sure it was a write (bit4==0) */
150 if (state
->io_misc_info
& (1 << 4))
153 /* Check for APMC IO port */
154 if (((state
->io_misc_info
>> 16) & 0xff) != APM_CNT
)
157 /* Check AX against the requested command */
158 if ((state
->rax
& 0xff) != cmd
)
167 static void finalize(void)
169 static int finalize_done
;
172 printk(BIOS_DEBUG
, "SMM already finalized.\n");
177 if (CONFIG(SPI_FLASH_SMM
))
178 /* Re-init SPI driver to handle locked BAR */
182 static void southbridge_smi_store(void)
185 em64t100_smm_state_save_area_t
*io_smi
=
186 smi_apmc_find_state_save(APM_CNT_SMMSTORE
);
191 /* Command and return value in EAX */
192 sub_command
= (io_smi
->rax
>> 8) & 0xff;
194 /* Parameter buffer in EBX */
195 reg_ebx
= io_smi
->rbx
;
197 /* drivers/smmstore/smi.c */
198 ret
= smmstore_exec(sub_command
, (void *)reg_ebx
);
202 static void southbridge_smi_apmc(void)
206 reg8
= apm_get_apmc();
208 case APM_CNT_ACPI_DISABLE
:
209 disable_pm1_control(SCI_EN
);
211 case APM_CNT_ACPI_ENABLE
:
212 enable_pm1_control(SCI_EN
);
214 case APM_CNT_FINALIZE
:
217 case APM_CNT_SMMSTORE
:
218 if (CONFIG(SMMSTORE
))
219 southbridge_smi_store();
223 mainboard_smi_apmc(reg8
);
226 static void southbridge_smi_pm1(void)
228 uint16_t pm1_sts
= clear_pm1_status();
230 /* While OSPM is not active, poweroff immediately
231 * on a power button event.
233 if (pm1_sts
& PWRBTN_STS
) {
234 // power button pressed
235 disable_pm1_control(-1UL);
236 enable_pm1_control(SLP_EN
| (SLP_TYP_S5
<< SLP_TYP_SHIFT
));
240 static void southbridge_smi_gpe0(void) { clear_gpe_status(); }
242 static void southbridge_smi_tco(void)
244 uint32_t tco_sts
= clear_tco_status();
250 if (tco_sts
& TCO1_STS_TIMEOUT
) { /* TIMEOUT */
251 /* Handle TCO timeout */
252 printk(BIOS_DEBUG
, "TCO Timeout.\n");
256 static void southbridge_smi_periodic(void)
260 reg32
= inl((uint16_t)(get_pmbase() + SMI_EN
));
262 /* Are periodic SMIs enabled? */
263 if ((reg32
& PERIODIC_EN
) == 0)
266 printk(BIOS_DEBUG
, "Periodic SMI.\n");
269 typedef void (*smi_handler_t
)(void);
271 static const smi_handler_t southbridge_smi
[32] = {
272 NULL
, // [0] reserved
273 NULL
, // [1] reserved
274 NULL
, // [2] BIOS_STS
275 NULL
, // [3] LEGACY_USB_STS
276 southbridge_smi_sleep
, // [4] SLP_SMI_STS
277 southbridge_smi_apmc
, // [5] APM_STS
278 NULL
, // [6] SWSMI_TMR_STS
279 NULL
, // [7] reserved
280 southbridge_smi_pm1
, // [8] PM1_STS
281 southbridge_smi_gpe0
, // [9] GPE0_STS
282 NULL
, // [10] reserved
283 NULL
, // [11] reserved
284 NULL
, // [12] reserved
285 southbridge_smi_tco
, // [13] TCO_STS
286 southbridge_smi_periodic
, // [14] PERIODIC_STS
287 NULL
, // [15] SERIRQ_SMI_STS
288 NULL
, // [16] SMBUS_SMI_STS
289 NULL
, // [17] LEGACY_USB2_STS
290 NULL
, // [18] INTEL_USB2_STS
291 NULL
, // [19] reserved
292 NULL
, // [20] PCI_EXP_SMI_STS
293 NULL
, // [21] reserved
294 NULL
, // [22] reserved
295 NULL
, // [23] reserved
296 NULL
, // [24] reserved
297 NULL
, // [25] reserved
298 NULL
, // [26] SPI_STS
299 NULL
, // [27] reserved
302 NULL
, // [30] reserved
303 NULL
// [31] reserved
306 void southbridge_smi_handler(void)
311 /* We need to clear the SMI status registers, or we won't see what's
312 * happening in the following calls.
314 smi_sts
= clear_smi_status();
316 /* Call SMI sub handler for each of the status bits */
317 for (i
= 0; i
< ARRAY_SIZE(southbridge_smi
); i
++) {
318 if (!(smi_sts
& (1 << i
)))
321 if (southbridge_smi
[i
] != NULL
) {
322 southbridge_smi
[i
]();
324 printk(BIOS_DEBUG
, "SMI_STS[%d] occurred, but no "
325 "handler available.\n",