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-or-later */
2 
3 #include <assert.h>
4 #include <string.h>
5 #include <cpu/x86/lapic.h>
6 #include <cpu/x86/mp.h>
8 #include <cpu/intel/smm_reloc.h>
9 #include <console/console.h>
10 #include <smp/node.h>
11 #include <soc/msr.h>
12 #include <soc/smmrelocate.h>
13 
15 {
16  uintptr_t tseg_base;
17  size_t tseg_size;
18 
19  smm_region(&tseg_base, &tseg_size);
20 
21  if (!IS_ALIGNED(tseg_base, tseg_size)) {
22  /*
23  * Note SMRR2 is supported which might support base/size combinations.
24  * For now it looks like FSP-M always uses aligned base/size, so let's
25  * not care about that.
26  */
28  "TSEG base not aligned with TSEG SIZE! Not setting SMRR\n");
29  return;
30  }
31 
32  /* SMRR has 32-bits of valid address aligned to 4KiB. */
33  if (!IS_ALIGNED(tseg_size, 4 * KiB)) {
35  "TSEG size not aligned to the minimum 4KiB! Not setting SMRR\n");
36  return;
37  }
38 
39  smm_subregion(SMM_SUBREGION_CHIPSET, &params->ied_base, &params->ied_size);
40 
41  params->smrr_base.lo = tseg_base | MTRR_TYPE_WRBACK;
42  params->smrr_base.hi = 0;
43  params->smrr_mask.lo = ~(tseg_size - 1) | MTRR_PHYS_MASK_VALID;
44  params->smrr_mask.hi = 0;
45 }
46 
48 {
49  char *ied_base;
50 
51  const struct ied_header ied = {
52  .signature = "INTEL RSVD",
53  .size = params->ied_size,
54  .reserved = {0},
55  };
56 
57  ied_base = (void *)params->ied_base;
58 
59  printk(BIOS_DEBUG, "IED base = 0x%08x\n", (u32)params->ied_base);
60  printk(BIOS_DEBUG, "IED size = 0x%08x\n", (u32)params->ied_size);
61 
62  /* Place IED header at IEDBASE. */
63  memcpy(ied_base, &ied, sizeof(ied));
64 
65  assert(params->ied_size > 1 * MiB + 32 * KiB);
66 
67  /* Zero out 32KiB at IEDBASE + 1MiB */
68  memset(ied_base + 1 * MiB, 0, 32 * KiB);
69 }
70 
71 void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
72  size_t *smm_save_state_size)
73 {
75 
76  smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize);
77 
80 
81  *smm_save_state_size = sizeof(em64t101_smm_state_save_area_t);
82 }
83 
84 static void update_save_state(int cpu, uintptr_t curr_smbase,
85  uintptr_t staggered_smbase,
86  struct smm_relocation_params *relo_params)
87 {
88  u32 smbase;
89  u32 iedbase;
90  em64t101_smm_state_save_area_t *save_state;
91  /*
92  * The relocated handler runs with all CPUs concurrently. Therefore
93  * stagger the entry points adjusting SMBASE downwards by save state
94  * size * CPU num.
95  */
96  smbase = staggered_smbase;
97  iedbase = relo_params->ied_base;
98 
99  printk(BIOS_DEBUG, "New SMBASE=0x%08x IEDBASE=0x%08x\n apic_id=0x%x\n",
100  smbase, iedbase, initial_lapicid());
101 
102  save_state = (void *)(curr_smbase + SMM_DEFAULT_SIZE - sizeof(*save_state));
103 
104  save_state->smbase = smbase;
105  save_state->iedbase = iedbase;
106 }
107 
108 /*
109  * The relocation work is actually performed in SMM context, but the code
110  * resides in the ramstage module. This occurs by trampolining from the default
111  * SMRAM entry point to here.
112  */
113 void smm_relocation_handler(int cpu, uintptr_t curr_smbase,
114  uintptr_t staggered_smbase)
115 {
116  msr_t mtrr_cap;
117  struct smm_relocation_params *relo_params = &smm_reloc_params;
118 
119  printk(BIOS_DEBUG, "%s : CPU %d\n", __func__, cpu);
120 
121  /* Make appropriate changes to the save state map. */
122  update_save_state(cpu, curr_smbase, staggered_smbase, relo_params);
123 
124  /* Write SMRR MSRs based on indicated support. */
125  mtrr_cap = rdmsr(MTRR_CAP_MSR);
126  if (mtrr_cap.lo & SMRR_SUPPORTED)
127  write_smrr(relo_params);
128 }
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
#define assert(statement)
Definition: assert.h:74
static struct sdram_info params
Definition: sdram_configs.c:83
#define IS_ALIGNED(x, a)
Definition: helpers.h:19
#define MiB
Definition: helpers.h:76
#define KiB
Definition: helpers.h:75
#define printk(level,...)
Definition: stdlib.h:16
void smm_relocation_handler(int cpu, uintptr_t curr_smbase, uintptr_t staggered_smbase)
Definition: smmrelocate.c:90
#define SMRR_SUPPORTED
Definition: smmrelocate.c:20
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
@ 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
static __always_inline unsigned int initial_lapicid(void)
Definition: lapic.h:126
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#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
static void write_smrr(struct smm_relocation_params *relo_params)
Definition: smm_reloc.h:59
static void setup_ied_area(struct smm_relocation_params *params)
Definition: smmrelocate.c:47
static void update_save_state(int cpu, uintptr_t curr_smbase, uintptr_t staggered_smbase, struct smm_relocation_params *relo_params)
Definition: smmrelocate.c:84
void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, size_t *smm_save_state_size)
Definition: smmrelocate.c:71
static void fill_in_relocation_params(struct smm_relocation_params *params)
Definition: smmrelocate.c:14
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
int smm_subregion(int sub, uintptr_t *start, size_t *size)
Definition: tseg_region.c:22
#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