blob: dd993cedb0aa088e689469e3f98f412b1a46b139 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer800379f2010-03-01 08:34:19 +00002
Stefan Reinauer800379f2010-03-01 08:34:19 +00003#include <device/device.h>
4#include <device/pci.h>
5#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07006#include <acpi/acpi.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +00007#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +00009#include <cpu/x86/cache.h>
10#include <cpu/x86/smm.h>
Kyösti Mälkkic4fdb7b2019-08-10 15:51:59 +030011#include <cpu/x86/smi_deprecated.h>
Stefan Reinauer800379f2010-03-01 08:34:19 +000012#include <string.h>
13#include "i82801dx.h"
14
Kyösti Mälkki55b72632019-07-08 22:36:38 +030015void northbridge_write_smram(u8 smram);
16
17/* For intel/e7505. */
Stefan Reinauer800379f2010-03-01 08:34:19 +000018#define D_OPEN (1 << 6)
19#define D_CLS (1 << 5)
20#define D_LCK (1 << 4)
21#define G_SMRAME (1 << 3)
22#define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0))
23
24/* While we read PMBASE dynamically in case it changed, let's
25 * initialize it with a sane value
26 */
27static u16 pmbase = PMBASE_ADDR;
28
29/**
30 * @brief read and clear PM1_STS
31 * @return PM1_STS register
32 */
33static u16 reset_pm1_status(void)
34{
35 u16 reg16;
36
37 reg16 = inw(pmbase + PM1_STS);
38 /* set status bits are cleared by writing 1 to them */
39 outw(reg16, pmbase + PM1_STS);
40
41 return reg16;
42}
43
44static void dump_pm1_status(u16 pm1_sts)
45{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000046 printk(BIOS_DEBUG, "PM1_STS: ");
47 if (pm1_sts & (1 << 15)) printk(BIOS_DEBUG, "WAK ");
48 if (pm1_sts & (1 << 14)) printk(BIOS_DEBUG, "PCIEXPWAK ");
49 if (pm1_sts & (1 << 11)) printk(BIOS_DEBUG, "PRBTNOR ");
50 if (pm1_sts & (1 << 10)) printk(BIOS_DEBUG, "RTC ");
51 if (pm1_sts & (1 << 8)) printk(BIOS_DEBUG, "PWRBTN ");
52 if (pm1_sts & (1 << 5)) printk(BIOS_DEBUG, "GBL ");
53 if (pm1_sts & (1 << 4)) printk(BIOS_DEBUG, "BM ");
54 if (pm1_sts & (1 << 0)) printk(BIOS_DEBUG, "TMROF ");
55 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +000056}
57
58/**
59 * @brief read and clear SMI_STS
60 * @return SMI_STS register
61 */
62static u32 reset_smi_status(void)
63{
64 u32 reg32;
65
66 reg32 = inl(pmbase + SMI_STS);
67 /* set status bits are cleared by writing 1 to them */
68 outl(reg32, pmbase + SMI_STS);
69
70 return reg32;
71}
72
73static void dump_smi_status(u32 smi_sts)
74{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000075 printk(BIOS_DEBUG, "SMI_STS: ");
76 if (smi_sts & (1 << 26)) printk(BIOS_DEBUG, "SPI ");
77 if (smi_sts & (1 << 25)) printk(BIOS_DEBUG, "EL_SMI ");
78 if (smi_sts & (1 << 21)) printk(BIOS_DEBUG, "MONITOR ");
79 if (smi_sts & (1 << 20)) printk(BIOS_DEBUG, "PCI_EXP_SMI ");
80 if (smi_sts & (1 << 18)) printk(BIOS_DEBUG, "INTEL_USB2 ");
81 if (smi_sts & (1 << 17)) printk(BIOS_DEBUG, "LEGACY_USB2 ");
82 if (smi_sts & (1 << 16)) printk(BIOS_DEBUG, "SMBUS_SMI ");
83 if (smi_sts & (1 << 15)) printk(BIOS_DEBUG, "SERIRQ_SMI ");
84 if (smi_sts & (1 << 14)) printk(BIOS_DEBUG, "PERIODIC ");
85 if (smi_sts & (1 << 13)) printk(BIOS_DEBUG, "TCO ");
86 if (smi_sts & (1 << 12)) printk(BIOS_DEBUG, "DEVMON ");
87 if (smi_sts & (1 << 11)) printk(BIOS_DEBUG, "MCSMI ");
88 if (smi_sts & (1 << 10)) printk(BIOS_DEBUG, "GPI ");
89 if (smi_sts & (1 << 9)) printk(BIOS_DEBUG, "GPE0 ");
90 if (smi_sts & (1 << 8)) printk(BIOS_DEBUG, "PM1 ");
91 if (smi_sts & (1 << 6)) printk(BIOS_DEBUG, "SWSMI_TMR ");
92 if (smi_sts & (1 << 5)) printk(BIOS_DEBUG, "APM ");
93 if (smi_sts & (1 << 4)) printk(BIOS_DEBUG, "SLP_SMI ");
94 if (smi_sts & (1 << 3)) printk(BIOS_DEBUG, "LEGACY_USB ");
95 if (smi_sts & (1 << 2)) printk(BIOS_DEBUG, "BIOS ");
96 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +000097}
98
Stefan Reinauer800379f2010-03-01 08:34:19 +000099/**
100 * @brief read and clear GPE0_STS
101 * @return GPE0_STS register
102 */
103static u32 reset_gpe0_status(void)
104{
105 u32 reg32;
106
107 reg32 = inl(pmbase + GPE0_STS);
108 /* set status bits are cleared by writing 1 to them */
109 outl(reg32, pmbase + GPE0_STS);
110
111 return reg32;
112}
113
114static void dump_gpe0_status(u32 gpe0_sts)
115{
116 int i;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000117 printk(BIOS_DEBUG, "GPE0_STS: ");
Konstantin Aladyshev62f80832013-03-07 04:04:27 +0400118 for (i=31; i>= 16; i--) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000119 if (gpe0_sts & (1 << i)) printk(BIOS_DEBUG, "GPIO%d ", (i-16));
Stefan Reinauer800379f2010-03-01 08:34:19 +0000120 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000121 if (gpe0_sts & (1 << 14)) printk(BIOS_DEBUG, "USB4 ");
122 if (gpe0_sts & (1 << 13)) printk(BIOS_DEBUG, "PME_B0 ");
123 if (gpe0_sts & (1 << 12)) printk(BIOS_DEBUG, "USB3 ");
124 if (gpe0_sts & (1 << 11)) printk(BIOS_DEBUG, "PME ");
125 if (gpe0_sts & (1 << 10)) printk(BIOS_DEBUG, "EL_SCI/BATLOW ");
126 if (gpe0_sts & (1 << 9)) printk(BIOS_DEBUG, "PCI_EXP ");
127 if (gpe0_sts & (1 << 8)) printk(BIOS_DEBUG, "RI ");
128 if (gpe0_sts & (1 << 7)) printk(BIOS_DEBUG, "SMB_WAK ");
129 if (gpe0_sts & (1 << 6)) printk(BIOS_DEBUG, "TCO_SCI ");
130 if (gpe0_sts & (1 << 5)) printk(BIOS_DEBUG, "AC97 ");
131 if (gpe0_sts & (1 << 4)) printk(BIOS_DEBUG, "USB2 ");
132 if (gpe0_sts & (1 << 3)) printk(BIOS_DEBUG, "USB1 ");
133 if (gpe0_sts & (1 << 2)) printk(BIOS_DEBUG, "HOT_PLUG ");
134 if (gpe0_sts & (1 << 0)) printk(BIOS_DEBUG, "THRM ");
135 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000136}
137
Stefan Reinauer800379f2010-03-01 08:34:19 +0000138/**
139 * @brief read and clear ALT_GP_SMI_STS
140 * @return ALT_GP_SMI_STS register
141 */
142static u16 reset_alt_gp_smi_status(void)
143{
144 u16 reg16;
145
146 reg16 = inl(pmbase + ALT_GP_SMI_STS);
147 /* set status bits are cleared by writing 1 to them */
148 outl(reg16, pmbase + ALT_GP_SMI_STS);
149
150 return reg16;
151}
152
153static void dump_alt_gp_smi_status(u16 alt_gp_smi_sts)
154{
155 int i;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000156 printk(BIOS_DEBUG, "ALT_GP_SMI_STS: ");
Konstantin Aladyshev62f80832013-03-07 04:04:27 +0400157 for (i=15; i>= 0; i--) {
Konstantin Aladyshev07c3fc02013-03-07 04:37:02 +0400158 if (alt_gp_smi_sts & (1 << i)) printk(BIOS_DEBUG, "GPI%d ", i);
Stefan Reinauer800379f2010-03-01 08:34:19 +0000159 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000160 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000161}
162
Stefan Reinauer800379f2010-03-01 08:34:19 +0000163/**
164 * @brief read and clear TCOx_STS
165 * @return TCOx_STS registers
166 */
167static u32 reset_tco_status(void)
168{
169 u32 tcobase = pmbase + 0x60;
170 u32 reg32;
171
172 reg32 = inl(tcobase + 0x04);
173 /* set status bits are cleared by writing 1 to them */
174 outl(reg32 & ~(1<<18), tcobase + 0x04); // Don't clear BOOT_STS before SECOND_TO_STS
175 if (reg32 & (1 << 18))
176 outl(reg32 & (1<<18), tcobase + 0x04); // clear BOOT_STS
177
178 return reg32;
179}
180
Stefan Reinauer800379f2010-03-01 08:34:19 +0000181static void dump_tco_status(u32 tco_sts)
182{
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000183 printk(BIOS_DEBUG, "TCO_STS: ");
184 if (tco_sts & (1 << 20)) printk(BIOS_DEBUG, "SMLINK_SLV ");
185 if (tco_sts & (1 << 18)) printk(BIOS_DEBUG, "BOOT ");
186 if (tco_sts & (1 << 17)) printk(BIOS_DEBUG, "SECOND_TO ");
187 if (tco_sts & (1 << 16)) printk(BIOS_DEBUG, "INTRD_DET ");
188 if (tco_sts & (1 << 12)) printk(BIOS_DEBUG, "DMISERR ");
189 if (tco_sts & (1 << 10)) printk(BIOS_DEBUG, "DMISMI ");
190 if (tco_sts & (1 << 9)) printk(BIOS_DEBUG, "DMISCI ");
191 if (tco_sts & (1 << 8)) printk(BIOS_DEBUG, "BIOSWR ");
192 if (tco_sts & (1 << 7)) printk(BIOS_DEBUG, "NEWCENTURY ");
193 if (tco_sts & (1 << 3)) printk(BIOS_DEBUG, "TIMEOUT ");
194 if (tco_sts & (1 << 2)) printk(BIOS_DEBUG, "TCO_INT ");
195 if (tco_sts & (1 << 1)) printk(BIOS_DEBUG, "SW_TCO ");
196 if (tco_sts & (1 << 0)) printk(BIOS_DEBUG, "NMI2SMI ");
197 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000198}
199
Stefan Reinauer800379f2010-03-01 08:34:19 +0000200/**
201 * @brief Set the EOS bit
202 */
203static void smi_set_eos(void)
204{
205 u8 reg8;
206
207 reg8 = inb(pmbase + SMI_EN);
208 reg8 |= EOS;
209 outb(reg8, pmbase + SMI_EN);
210}
211
212extern uint8_t smm_relocation_start, smm_relocation_end;
Kyösti Mälkki2e501422017-04-21 08:43:09 +0300213static void *default_smm_area = NULL;
Stefan Reinauer800379f2010-03-01 08:34:19 +0000214
Kyösti Mälkkiead8a072019-08-16 08:05:52 +0300215static void aseg_smm_relocate(void)
Stefan Reinauer800379f2010-03-01 08:34:19 +0000216{
217 u32 smi_en;
218 u16 pm1_en;
219
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000220 printk(BIOS_DEBUG, "Initializing SMM handler...");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000221
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300222 pmbase = pci_read_config16(pcidev_on_root(0x1f, 0), 0x40) & 0xfffc;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000223 printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
Stefan Reinauer800379f2010-03-01 08:34:19 +0000224
225 smi_en = inl(pmbase + SMI_EN);
226 if (smi_en & APMC_EN) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000227 printk(BIOS_INFO, "SMI# handler already enabled?\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000228 return;
229 }
230
Kyösti Mälkki2e501422017-04-21 08:43:09 +0300231 default_smm_area = backup_default_smm_area();
232
Stefan Reinauer800379f2010-03-01 08:34:19 +0000233 /* copy the SMM relocation code */
234 memcpy((void *)0x38000, &smm_relocation_start,
235 &smm_relocation_end - &smm_relocation_start);
Kyösti Mälkkib6e90212016-12-04 22:17:37 +0200236 wbinvd();
Stefan Reinauer800379f2010-03-01 08:34:19 +0000237
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_DEBUG, "\n");
Stefan Reinauer800379f2010-03-01 08:34:19 +0000239 dump_smi_status(reset_smi_status());
240 dump_pm1_status(reset_pm1_status());
241 dump_gpe0_status(reset_gpe0_status());
242 dump_alt_gp_smi_status(reset_alt_gp_smi_status());
243 dump_tco_status(reset_tco_status());
244
245 /* Enable SMI generation:
246 * - on TCO events
247 * - on APMC writes (io 0xb2)
248 * - on writes to SLP_EN (sleep states)
249 * - on writes to GBL_RLS (bios commands)
250 * No SMIs:
251 * - on microcontroller writes (io 0x62/0x66)
252 */
253
254 smi_en = 0; /* reset SMI enables */
Stefan Reinauer800379f2010-03-01 08:34:19 +0000255 smi_en |= TCO_EN;
256 smi_en |= APMC_EN;
Kyösti Mälkki94464472020-06-13 13:45:42 +0300257 if (CONFIG(DEBUG_PERIODIC_SMI))
258 smi_en |= PERIODIC_EN;
Stefan Reinauer800379f2010-03-01 08:34:19 +0000259 smi_en |= SLP_SMI_EN;
260 smi_en |= BIOS_EN;
261
262 /* The following need to be on for SMIs to happen */
263 smi_en |= EOS | GBL_SMI_EN;
264
265 outl(smi_en, pmbase + SMI_EN);
266
267 pm1_en = 0;
268 pm1_en |= PWRBTN_EN;
269 pm1_en |= GBL_EN;
270 outw(pm1_en, pmbase + PM1_EN);
271
272 /**
273 * There are several methods of raising a controlled SMI# via
274 * software, among them:
275 * - Writes to io 0xb2 (APMC)
276 * - Writes to the Local Apic ICR with Delivery mode SMI.
277 *
Elyes HAOUASa5b0bc42020-02-20 20:04:29 +0100278 * Using the local APIC is a bit more tricky. According to
Stefan Reinauer800379f2010-03-01 08:34:19 +0000279 * AMD Family 11 Processor BKDG no destination shorthand must be
280 * used.
281 * The whole SMM initialization is quite a bit hardware specific, so
282 * I'm not too worried about the better of the methods at the moment
283 */
284
285 /* raise an SMI interrupt */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000286 printk(BIOS_SPEW, " ... raise SMI#\n");
Kyösti Mälkkid1518312020-06-18 13:51:31 +0300287 apm_control(APM_CNT_NOOP_SMI);
Stefan Reinauer800379f2010-03-01 08:34:19 +0000288}
289
Kyösti Mälkkiead8a072019-08-16 08:05:52 +0300290static void aseg_smm_install(void)
Stefan Reinauer800379f2010-03-01 08:34:19 +0000291{
Stefan Reinauer800379f2010-03-01 08:34:19 +0000292 /* copy the real SMM handler */
Kyösti Mälkki9d8adc02016-12-04 22:17:37 +0200293 memcpy((void *)0xa0000, _binary_smm_start,
294 _binary_smm_end - _binary_smm_start);
Stefan Reinauer800379f2010-03-01 08:34:19 +0000295 wbinvd();
Stefan Reinauer800379f2010-03-01 08:34:19 +0000296}
297
298void smm_init(void)
299{
Stefan Reinauerbc0f7a62010-08-01 15:41:14 +0000300 /* Put SMM code to 0xa0000 */
Kyösti Mälkkiead8a072019-08-16 08:05:52 +0300301 aseg_smm_install();
Stefan Reinauer800379f2010-03-01 08:34:19 +0000302
Stefan Reinauerbc0f7a62010-08-01 15:41:14 +0000303 /* Put relocation code to 0x38000 and relocate SMBASE */
Kyösti Mälkkiead8a072019-08-16 08:05:52 +0300304 aseg_smm_relocate();
Stefan Reinauerbc0f7a62010-08-01 15:41:14 +0000305
306 /* We're done. Make sure SMIs can happen! */
Stefan Reinauer800379f2010-03-01 08:34:19 +0000307 smi_set_eos();
308}
309
Kyösti Mälkki2e501422017-04-21 08:43:09 +0300310void smm_init_completion(void)
311{
312 restore_default_smm_area(default_smm_area);
313}
314
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300315void aseg_smm_lock(void)
Stefan Reinauer800379f2010-03-01 08:34:19 +0000316{
317 /* LOCK the SMM memory window and enable normal SMM.
318 * After running this function, only a full reset can
319 * make the SMM registers writable again.
320 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000321 printk(BIOS_DEBUG, "Locking SMM.\n");
Kyösti Mälkki55b72632019-07-08 22:36:38 +0300322 northbridge_write_smram(D_LCK | G_SMRAME | C_BASE_SEG);
Stefan Reinauer800379f2010-03-01 08:34:19 +0000323}