coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
smmrelocate.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /* SMM relocation for i945-ivybridge. */
4 
5 #include <assert.h>
6 #include <types.h>
7 #include <string.h>
8 #include <device/device.h>
9 #include <device/pci.h>
10 #include <commonlib/helpers.h>
11 #include <cpu/x86/mp.h>
12 #include <cpu/x86/msr.h>
13 #include <cpu/x86/mtrr.h>
14 #include <cpu/x86/smm.h>
16 #include <cpu/intel/smm_reloc.h>
17 #include <console/console.h>
18 #include <smp/node.h>
19 
20 #define SMRR_SUPPORTED (1 << 11)
21 
22 #define D_OPEN (1 << 6)
23 #define D_CLS (1 << 5)
24 #define D_LCK (1 << 4)
25 #define G_SMRAME (1 << 3)
26 #define C_BASE_SEG ((0 << 2) | (1 << 1) | (0 << 0))
27 
28 /* On model_6fx, model_1067x and model_106cx SMRR functions slightly
29  differently. The MSR are at different location from the rest
30  and need to be explicitly enabled in IA32_FEATURE_CONTROL MSR. */
32 {
33  struct cpuinfo_x86 c;
34  get_fms(&c, cpuid_eax(1));
35  if (c.x86 != 6)
36  return false;
37  switch (c.x86_model) {
38  case 0xf:
39  case 0x17: /* core2 */
40  case 0x1c: /* Bonnell */
41  return true;
42  default:
43  return false;
44  }
45 }
46 
47 static void write_smrr_alt(struct smm_relocation_params *relo_params)
48 {
49  msr_t msr;
51  /* SMRR enabled and feature locked */
52  if (!((msr.lo & SMRR_ENABLE)
53  && (msr.lo & FEATURE_CONTROL_LOCK_BIT))) {
55  "SMRR not enabled, skip writing SMRR...\n");
56  return;
57  }
58 
59  printk(BIOS_DEBUG, "Writing SMRR. base = 0x%08x, mask=0x%08x\n",
60  relo_params->smrr_base.lo, relo_params->smrr_mask.lo);
61 
62  wrmsr(CORE2_SMRR_PHYS_BASE, relo_params->smrr_base);
63  wrmsr(CORE2_SMRR_PHYS_MASK, relo_params->smrr_mask);
64 }
65 
67 {
68  uintptr_t tseg_base;
69  size_t tseg_size;
70 
71  /* All range registers are aligned to 4KiB */
72  const u32 rmask = ~((1 << 12) - 1);
73 
74  smm_region(&tseg_base, &tseg_size);
75 
76  if (!IS_ALIGNED(tseg_base, tseg_size)) {
78  "TSEG base not aligned with TSEG SIZE! Not setting SMRR\n");
79  return;
80  }
81 
82  /* SMRR has 32-bits of valid address aligned to 4KiB. */
83  params->smrr_base.lo = (tseg_base & rmask) | MTRR_TYPE_WRBACK;
84  params->smrr_base.hi = 0;
85  params->smrr_mask.lo = (~(tseg_size - 1) & rmask) | MTRR_PHYS_MASK_VALID;
86  params->smrr_mask.hi = 0;
87 
88  /* On model_6fx and model_1067x bits [0:11] on smrr_base are reserved */
90  params->smrr_base.lo &= rmask;
91 
92  smm_subregion(SMM_SUBREGION_CHIPSET, &params->ied_base, &params->ied_size);
93 }
94 
96 {
97  char *ied_base;
98 
99  struct ied_header ied = {
100  .signature = "INTEL RSVD",
101  .size = params->ied_size,
102  .reserved = {0},
103  };
104 
105  ied_base = (void *)params->ied_base;
106 
107  /* Place IED header at IEDBASE. */
108  memcpy(ied_base, &ied, sizeof(ied));
109 
110  /* Zero out 32KiB at IEDBASE + 1MiB */
111  memset(ied_base + (1 << 20), 0, (32 << 10));
112 }
113 
114 void smm_lock(void)
115 {
116  /* LOCK the SMM memory window and enable normal SMM.
117  * After running this function, only a full reset can
118  * make the SMM registers writable again.
119  */
120  printk(BIOS_DEBUG, "Locking SMM.\n");
121 
123 }
124 
125 void smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
126  size_t *smm_save_state_size)
127 {
128  printk(BIOS_DEBUG, "Setting up SMI for CPU\n");
129 
131 
132  smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
133 
136 
137  /* This may not be correct for older CPU's supported by this code,
138  but given that em64t101_smm_state_save_area_t is larger than the
139  save_state of these CPU's it works. */
140  *smm_save_state_size = sizeof(em64t101_smm_state_save_area_t);
141 }
142 
143 void smm_initialize(void)
144 {
145  /* Clear the SMM state in the southbridge. */
147 
148  /*
149  * Run the relocation handler for on the BSP to check and set up
150  * parallel SMM relocation.
151  */
153 }
154 
155 /* The relocation work is actually performed in SMM context, but the code
156  * resides in the ramstage module. This occurs by trampolining from the default
157  * SMRAM entry point to here. */
158 void smm_relocation_handler(int cpu, uintptr_t curr_smbase,
159  uintptr_t staggered_smbase)
160 {
161  msr_t mtrr_cap;
162  struct smm_relocation_params *relo_params = &smm_reloc_params;
163  /* The em64t101 save state is sufficiently compatible with older
164  save states with regards of smbase, smm_revision. */
165  em64t101_smm_state_save_area_t *save_state;
166  u32 smbase = staggered_smbase;
167  u32 iedbase = relo_params->ied_base;
168 
169  printk(BIOS_DEBUG, "In relocation handler: cpu %d\n", cpu);
170 
171  /* Make appropriate changes to the save state map. */
172  if (relo_params->ied_size)
173  printk(BIOS_DEBUG, "New SMBASE=0x%08x IEDBASE=0x%08x\n",
174  smbase, iedbase);
175  else
176  printk(BIOS_DEBUG, "New SMBASE=0x%08x\n",
177  smbase);
178 
179  save_state = (void *)(curr_smbase + SMM_DEFAULT_SIZE -
180  sizeof(*save_state));
181  save_state->smbase = smbase;
182 
183  printk(BIOS_SPEW, "SMM revision: 0x%08x\n", save_state->smm_revision);
184  if (save_state->smm_revision == 0x00030101)
185  save_state->iedbase = iedbase;
186 
187  /* Write EMRR and SMRR MSRs based on indicated support. */
188  mtrr_cap = rdmsr(MTRR_CAP_MSR);
189  if (!(mtrr_cap.lo & SMRR_SUPPORTED))
190  return;
191 
193  write_smrr_alt(relo_params);
194  else
195  write_smrr(relo_params);
196 }
197 
198 /*
199  * The default SMM entry can happen in parallel or serially. If the
200  * default SMM entry is done in parallel the BSP has already setup
201  * the saving state to each CPU's MSRs. At least one save state size
202  * is required for the initial SMM entry for the BSP to determine if
203  * parallel SMM relocation is even feasible.
204  */
205 void smm_relocate(void)
206 {
207  /*
208  * If smm_save_state_in_msrs is non-zero then parallel SMM relocation
209  * shall take place. Run the relocation handler a second time on the
210  * BSP to do the final move. For APs, a relocation handler always
211  * needs to be run.
212  */
213  if (!boot_cpu())
215 }
static unsigned int cpuid_eax(unsigned int op)
Definition: cpu.h:79
static void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
Definition: cpu.h:278
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
void * memset(void *dstpp, int c, size_t len)
Definition: memset.c:12
static struct sdram_info params
Definition: sdram_configs.c:83
#define IS_ALIGNED(x, a)
Definition: helpers.h:19
int boot_cpu(void)
Definition: psp.c:5
#define printk(level,...)
Definition: stdlib.h:16
void smm_relocate(void)
Definition: smmrelocate.c:247
void smm_relocation_handler(int cpu, uintptr_t curr_smbase, uintptr_t staggered_smbase)
Definition: smmrelocate.c:90
void smm_initialize(void)
Definition: smmrelocate.c:227
void smm_lock(void)
Definition: smmrelocate.c:261
void smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, size_t *smm_save_state_size)
Definition: smmrelocate.c:213
static void write_smrr_alt(struct smm_relocation_params *relo_params)
Definition: smmrelocate.c:47
#define G_SMRAME
Definition: smmrelocate.c:25
static void setup_ied_area(struct smm_relocation_params *params)
Definition: smmrelocate.c:95
bool cpu_has_alternative_smrr(void)
Definition: smmrelocate.c:31
#define SMRR_SUPPORTED
Definition: smmrelocate.c:20
#define C_BASE_SEG
Definition: smmrelocate.c:26
#define D_LCK
Definition: smmrelocate.c:24
static void fill_in_relocation_params(struct smm_relocation_params *params)
Definition: smmrelocate.c:66
void smm_initiate_relocation(void)
Definition: mp_init.c:664
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
#define IA32_FEATURE_CONTROL
Definition: msr.h:20
#define SMRR_ENABLE
Definition: msr.h:23
#define FEATURE_CONTROL_LOCK_BIT
Definition: msr.h:21
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
@ SMM_SUBREGION_HANDLER
Definition: smm.h:171
@ SMM_SUBREGION_CHIPSET
Definition: smm.h:175
void smm_region(uintptr_t *start, size_t *size)
Definition: memmap.c:50
#define SMM_DEFAULT_SIZE
Definition: smm.h:11
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_SPEW
BIOS_SPEW - Excessively verbose output.
Definition: loglevel.h:142
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
static const struct smm_save_state_ops * save_state
Definition: save_state.c:13
struct smm_relocation_params smm_reloc_params
Definition: smm_reloc.c:5
void smm_southbridge_clear_state(void)
Definition: smm.c:22
static void write_smrr(struct smm_relocation_params *relo_params)
Definition: smm_reloc.h:59
void northbridge_write_smram(u8 smram)
Definition: memmap.c:27
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
char signature[10]
Definition: smm_reloc.h:31
unsigned int lo
Definition: msr.h:111
uintptr_t ied_base
Definition: smm_reloc.h:12
#define c(value, pmcreg, dst_bits)
int smm_subregion(int sub, uintptr_t *start, size_t *size)
Definition: tseg_region.c:22
#define CORE2_SMRR_PHYS_MASK
Definition: mtrr.h:37
#define CORE2_SMRR_PHYS_BASE
Definition: mtrr.h:36
#define MTRR_CAP_MSR
Definition: mtrr.h:17
#define MTRR_TYPE_WRBACK
Definition: mtrr.h:14
#define MTRR_PHYS_MASK_VALID
Definition: mtrr.h:41