coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
smi.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/device.h>
4 #include <device/pci.h>
5 #include <console/console.h>
6 #include <arch/io.h>
7 #include <device/pci_ops.h>
8 #include <acpi/acpi.h>
9 #include <cpu/x86/cache.h>
10 #include <cpu/x86/smm.h>
11 #include <cpu/x86/smi_deprecated.h>
12 #include <string.h>
14 #include "i82801ix.h"
15 
16 /* I945/GM45 */
17 #define SMRAM 0x9d
18 #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  */
28 
30 static void *default_smm_area = NULL;
31 
32 static void aseg_smm_relocate(void)
33 {
34  u32 smi_en;
35  u16 pm1_en;
36 
37  printk(BIOS_DEBUG, "Initializing SMM handler...");
38 
40  0xfffc;
41  printk(BIOS_SPEW, " ... pmbase = 0x%04x\n", pmbase);
42 
43  smi_en = inl(pmbase + SMI_EN);
44  if (smi_en & GBL_SMI_EN) {
45  printk(BIOS_INFO, "SMI# handler already enabled?\n");
46  return;
47  }
48 
50 
51  /* copy the SMM relocation code */
52  memcpy((void *)0x38000, &smm_relocation_start,
54  wbinvd();
55 
56  printk(BIOS_DEBUG, "\n");
62 
63  /* Enable SMI generation:
64  * - on TCO events
65  * - on APMC writes (io 0xb2)
66  * - on writes to GBL_RLS (bios commands)
67  * No SMIs:
68  * - on microcontroller writes (io 0x62/0x66)
69  */
70 
71  smi_en = 0; /* reset SMI enables */
72  smi_en |= TCO_EN;
73  smi_en |= APMC_EN;
74  if (CONFIG(DEBUG_PERIODIC_SMI))
75  smi_en |= PERIODIC_EN;
76  smi_en |= BIOS_EN;
77 
78  /* The following need to be on for SMIs to happen */
79  smi_en |= EOS | GBL_SMI_EN;
80 
81  outl(smi_en, pmbase + SMI_EN);
82 
83  pm1_en = 0;
84  pm1_en |= PWRBTN_EN;
85  pm1_en |= GBL_EN;
86  outw(pm1_en, pmbase + PM1_EN);
87 
88  /**
89  * There are several methods of raising a controlled SMI# via
90  * software, among them:
91  * - Writes to io 0xb2 (APMC)
92  * - Writes to the Local Apic ICR with Delivery mode SMI.
93  *
94  * Using the local APIC is a bit more tricky. According to
95  * AMD Family 11 Processor BKDG no destination shorthand must be
96  * used.
97  * The whole SMM initialization is quite a bit hardware specific, so
98  * I'm not too worried about the better of the methods at the moment
99  */
100 
101  /* raise an SMI interrupt */
102  printk(BIOS_SPEW, " ... raise SMI#\n");
104 }
105 
106 static int smm_handler_copied = 0;
107 
108 static void aseg_smm_install(void)
109 {
110  /* The first CPU running this gets to copy the SMM handler. But not all
111  * of them.
112  */
113  if (smm_handler_copied)
114  return;
115  smm_handler_copied = 1;
116 
117  /* if we're resuming from S3, the SMM code is already in place,
118  * so don't copy it again to keep the current SMM state */
119 
120  if (!acpi_is_wakeup_s3()) {
121  /* enable the SMM memory window */
124 
125  /* copy the real SMM handler */
126  memcpy((void *)0xa0000, _binary_smm_start,
128  wbinvd();
129  }
130 
131  /* close the SMM memory window and enable normal SMM */
133  G_SMRAME | C_BASE_SEG);
134 }
135 
136 void smm_init(void)
137 {
138  /* Put SMM code to 0xa0000 */
140 
141  /* Put relocation code to 0x38000 and relocate SMBASE */
143 
144  /* We're done. Make sure SMIs can happen! */
145  smi_set_eos();
146 }
147 
149 {
151 }
152 
153 void aseg_smm_lock(void)
154 {
155  /* LOCK the SMM memory window and enable normal SMM.
156  * After running this function, only a full reset can
157  * make the SMM registers writable again.
158  */
159  printk(BIOS_DEBUG, "Locking SMM.\n");
162 }
#define PM1_EN
Definition: pm.h:21
#define GBL_SMI_EN
Definition: pm.h:49
#define APMC_EN
Definition: pm.h:44
#define BIOS_EN
Definition: pm.h:47
#define SMI_EN
Definition: pm.h:32
#define EOS
Definition: pm.h:48
#define PERIODIC_EN
Definition: pm.h:39
static int acpi_is_wakeup_s3(void)
Definition: acpi.h:9
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
void restore_default_smm_area(void *smm_save_area)
void * backup_default_smm_area(void)
#define TCO_EN
Definition: pm.h:211
#define PWRBTN_EN
Definition: southbridge.h:36
#define GBL_EN
Definition: southbridge.h:37
#define printk(level,...)
Definition: stdlib.h:16
u32 inl(u16 port)
void outl(u32 val, u16 port)
void outw(u16 val, u16 port)
DEVTREE_CONST struct device * pcidev_on_root(uint8_t dev, uint8_t fn)
Definition: device_const.c:260
@ CONFIG
Definition: dsi_common.h:201
static void wbinvd(void)
Definition: cache.h:15
unsigned char _binary_smm_start[]
#define APM_CNT_NOOP_SMI
Definition: smm.h:20
unsigned char _binary_smm_end[]
static __always_inline u16 pci_read_config16(const struct device *dev, u16 reg)
Definition: pci_ops.h:52
static __always_inline void pci_write_config8(const struct device *dev, u16 reg, u8 val)
Definition: pci_ops.h:64
#define DEFAULT_PMBASE
Definition: iomap.h:14
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
#define D31F0_PMBASE
Definition: pmutil.h:8
int apm_control(u8 cmd)
Definition: smi_trigger.c:31
static u16 reset_alt_gp_smi_status(void)
read and clear ALT_GP_SMI_STS
Definition: smi.c:142
static void dump_gpe0_status(u32 gpe0_sts)
Definition: smi.c:114
uint8_t smm_relocation_end
Definition: smi.c:212
static u16 reset_pm1_status(void)
read and clear PM1_STS
Definition: smi.c:33
static u32 reset_smi_status(void)
read and clear SMI_STS
Definition: smi.c:62
static void dump_alt_gp_smi_status(u16 alt_gp_smi_sts)
Definition: smi.c:153
void aseg_smm_lock(void)
Definition: smi.c:315
static void dump_tco_status(u32 tco_sts)
Definition: smi.c:181
static u32 reset_tco_status(void)
read and clear TCOx_STS
Definition: smi.c:167
static void smi_set_eos(void)
Set the EOS bit.
Definition: smi.c:203
void smm_init(void)
Definition: smi.c:298
static u32 reset_gpe0_status(void)
read and clear GPE0_STS
Definition: smi.c:103
static void dump_smi_status(u32 smi_sts)
Definition: smi.c:73
void smm_init_completion(void)
Definition: smi.c:310
static void dump_pm1_status(u16 pm1_sts)
Definition: smi.c:44
uint8_t smm_relocation_start
#define G_SMRAME
Definition: smi.c:21
static void * default_smm_area
Definition: smi.c:30
static int smm_handler_copied
Definition: smi.c:106
#define SMRAM
Definition: smi.c:17
static u16 pmbase
Definition: smi.c:27
static void aseg_smm_relocate(void)
Definition: smi.c:32
#define C_BASE_SEG
Definition: smi.c:22
static void aseg_smm_install(void)
Definition: smi.c:108
#define D_LCK
Definition: smi.c:20
#define D_OPEN
Definition: smi.c:18
#define NULL
Definition: stddef.h:19
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
unsigned char uint8_t
Definition: stdint.h:8