Debian Codegraph @

src/soc/intel/denverton_ns/smihandler.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <stdint.h>
4 #include <arch/hlt.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>
10 #include <cpu/intel/em64t100_save_state.h>
11 #include <device/pci_def.h>
12 #include <intelblocks/fast_spi.h>
13 #include <smmstore.h>
14 #include <spi-generic.h>
15 #include <soc/iomap.h>
16 #include <soc/soc_util.h>
17 #include <soc/pm.h>
18 #include <soc/nvs.h>
19
20 void southbridge_smi_set_eos(void)
21 {
22 enable_smi(EOS);
23 }
24
25 static void busmaster_disable_on_bus(int bus)
26 {
27 int slot, func;
28 unsigned int val;
29 unsigned char hdr;
30
31 for (slot = 0; slot < 0x20; slot++) {
32 for (func = 0; func < 8; func++) {
33 u16 reg16;
34
35 pci_devfn_t dev = PCI_DEV(bus, slot, func);
36 val = pci_read_config32(dev, PCI_VENDOR_ID);
37
38 if (val == 0xffffffff || val == 0x00000000 ||
39 val == 0x0000ffff || val == 0xffff0000)
40 continue;
41
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);
46
47 /* If this is a bridge, then follow it. */
48 hdr = pci_read_config8(dev, PCI_HEADER_TYPE);
49 hdr &= 0x7f;
50 if (hdr == PCI_HEADER_TYPE_BRIDGE ||
51 hdr == PCI_HEADER_TYPE_CARDBUS) {
52 unsigned int buses;
53 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
54 busmaster_disable_on_bus((buses >> 8) & 0xff);
55 }
56 }
57 }
58 }
59
60 static void southbridge_smi_sleep(void)
61 {
62 uint32_t reg32;
63 uint8_t slp_typ;
64 uint16_t pmbase = get_pmbase();
65
66 /* First, disable further SMIs */
67 disable_smi(SLP_SMI_EN);
68
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;
73
74 /* Do any mainboard sleep handling */
75 mainboard_smi_sleep((uint8_t)(slp_typ - 2));
76
77 /* Next, do the deed.
78 */
79
80 switch (slp_typ) {
81 case SLP_TYP_S0:
82 printk(BIOS_DEBUG, "SMI#: Entering S0 (On)\n");
83 break;
84 case SLP_TYP_S1:
85 printk(BIOS_DEBUG, "SMI#: Entering S1 (Assert STPCLK#)\n");
86 break;
87 case SLP_TYP_S3:
88 printk(BIOS_DEBUG, "SMI#: Entering S3 (Suspend-To-RAM)\n");
89
90 /* Invalidate the cache before going to S3 */
91 wbinvd();
92 break;
93 case SLP_TYP_S4:
94 printk(BIOS_DEBUG, "SMI#: Entering S4 (Suspend-To-Disk)\n");
95 break;
96 case SLP_TYP_S5:
97 printk(BIOS_DEBUG, "SMI#: Entering S5 (Soft Power off)\n");
98
99 /* Disable all GPE */
100 disable_all_gpe();
101
102 /* also iterates over all bridges on bus 0 */
103 busmaster_disable_on_bus(0);
104 break;
105 default:
106 printk(BIOS_DEBUG, "SMI#: ERROR: SLP_TYP reserved\n");
107 break;
108 }
109
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
112 * sleep happen.
113 */
114 enable_pm1_control(SLP_EN);
115
116 /* Make sure to stop executing code here for S3/S4/S5 */
117 if (slp_typ > 1)
118 hlt();
119
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.
123 */
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);
128 }
129 }
130
131 /*
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.
135 */
136 static em64t100_smm_state_save_area_t *smi_apmc_find_state_save(uint8_t cmd)
137 {
138 em64t100_smm_state_save_area_t *state;
139 int node;
140
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);
144 if (!state)
145 continue;
146
147 /* Check for Synchronous IO (bit0==1) */
148 if (!(state->io_misc_info & (1 << 0)))
149 continue;
150
151 /* Make sure it was a write (bit4==0) */
152 if (state->io_misc_info & (1 << 4))
153 continue;
154
155 /* Check for APMC IO port */
156 if (((state->io_misc_info >> 16) & 0xff) != APM_CNT)
157 continue;
158
159 /* Check AX against the requested command */
160 if ((state->rax & 0xff) != cmd)
161 continue;
162
163 return state;
164 }
165
166 return NULL;
167 }
168
169 static void finalize(void)
170 {
171 static int finalize_done;
172
173 if (finalize_done) {
174 printk(BIOS_DEBUG, "SMM already finalized.\n");
175 return;
176 }
177 finalize_done = 1;
178
179 if (CONFIG(SPI_FLASH_SMM))
180 /* Re-init SPI driver to handle locked BAR */
181 fast_spi_init();
182 }
183
184 static void southbridge_smi_store(void)
185 {
186 u8 sub_command, ret;
187 em64t100_smm_state_save_area_t *io_smi =
188 smi_apmc_find_state_save(APM_CNT_SMMSTORE);
189 uint32_t reg_ebx;
190
191 if (!io_smi)
192 return;
193 /* Command and return value in EAX */
194 sub_command = (io_smi->rax >> 8) & 0xff;
195
196 /* Parameter buffer in EBX */
197 reg_ebx = io_smi->rbx;
198
199 /* drivers/smmstore/smi.c */
200 ret = smmstore_exec(sub_command, (void *)reg_ebx);
201 io_smi->rax = ret;
202 }
203
204 static void southbridge_smi_apmc(void)
205 {
206 uint8_t reg8;
207
208 reg8 = apm_get_apmc();
209 switch (reg8) {
210 case APM_CNT_ACPI_DISABLE:
211 disable_pm1_control(SCI_EN);
212 break;
213 case APM_CNT_ACPI_ENABLE:
214 enable_pm1_control(SCI_EN);
215 break;
216 case APM_CNT_FINALIZE:
217 finalize();
218 break;
219 case APM_CNT_SMMSTORE:
220 if (CONFIG(SMMSTORE))
221 southbridge_smi_store();
222 break;
223 }
224
225 mainboard_smi_apmc(reg8);
226 }
227
228 static void southbridge_smi_pm1(void)
229 {
230 uint16_t pm1_sts = clear_pm1_status();
231
232 /* While OSPM is not active, poweroff immediately
233 * on a power button event.
234 */
235 if (pm1_sts & PWRBTN_STS) {
236 // power button pressed
237 disable_pm1_control(-1UL);
238 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
239 }
240 }
241
242 static void southbridge_smi_gpe0(void) { clear_gpe_status(); }
243
244 static void southbridge_smi_tco(void)
245 {
246 uint32_t tco_sts = clear_tco_status();
247
248 /* Any TCO event? */
249 if (!tco_sts)
250 return;
251
252 if (tco_sts & TCO1_STS_TIMEOUT) { /* TIMEOUT */
253 /* Handle TCO timeout */
254 printk(BIOS_DEBUG, "TCO Timeout.\n");
255 }
256 }
257
258 static void southbridge_smi_periodic(void)
259 {
260 uint32_t reg32;
261
262 reg32 = inl((uint16_t)(get_pmbase() + SMI_EN));
263
264 /* Are periodic SMIs enabled? */
265 if ((reg32 & PERIODIC_EN) == 0)
266 return;
267
268 printk(BIOS_DEBUG, "Periodic SMI.\n");
269 }
270
271 typedef void (*smi_handler_t)(void);
272
273 static const smi_handler_t southbridge_smi[32] = {
274 NULL, // [0] reserved
275 NULL, // [1] reserved
276 NULL, // [2] BIOS_STS
277 NULL, // [3] LEGACY_USB_STS
278 southbridge_smi_sleep, // [4] SLP_SMI_STS
279 southbridge_smi_apmc, // [5] APM_STS
280 NULL, // [6] SWSMI_TMR_STS
281 NULL, // [7] reserved
282 southbridge_smi_pm1, // [8] PM1_STS
283 southbridge_smi_gpe0, // [9] GPE0_STS
284 NULL, // [10] reserved
285 NULL, // [11] reserved
286 NULL, // [12] reserved
287 southbridge_smi_tco, // [13] TCO_STS
288 southbridge_smi_periodic, // [14] PERIODIC_STS
289 NULL, // [15] SERIRQ_SMI_STS
290 NULL, // [16] SMBUS_SMI_STS
291 NULL, // [17] LEGACY_USB2_STS
292 NULL, // [18] INTEL_USB2_STS
293 NULL, // [19] reserved
294 NULL, // [20] PCI_EXP_SMI_STS
295 NULL, // [21] reserved
296 NULL, // [22] reserved
297 NULL, // [23] reserved
298 NULL, // [24] reserved
299 NULL, // [25] reserved
300 NULL, // [26] SPI_STS
301 NULL, // [27] reserved
302 NULL, // [28] PUNIT
303 NULL, // [29] GUNIT
304 NULL, // [30] reserved
305 NULL // [31] reserved
306 };
307
308 void southbridge_smi_handler(void)
309 {
310 int i;
311 uint32_t smi_sts;
312
313 /* We need to clear the SMI status registers, or we won't see what's
314 * happening in the following calls.
315 */
316 smi_sts = clear_smi_status();
317
318 /* Call SMI sub handler for each of the status bits */
319 for (i = 0; i < ARRAY_SIZE(southbridge_smi); i++) {
320 if (!(smi_sts & (1 << i)))
321 continue;
322
323 if (southbridge_smi[i] != NULL) {
324 southbridge_smi[i]();
325 } else {
326 printk(BIOS_DEBUG, "SMI_STS[%d] occurred, but no "
327 "handler available.\n",
328 i);
329 }
330 }
331 }