| 1 |
|
| 2 |
|
| 3 |
|
| 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> |
| 15 |
#include <cpu/intel/em64t101_save_state.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 |
|
| 29 |
|
| 30 |
|
| 31 |
bool cpu_has_alternative_smrr(void) |
| 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: |
| 40 |
case 0x1c: |
| 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; |
| 50 |
msr = rdmsr(IA32_FEATURE_CONTROL); |
| 51 |
|
| 52 |
if (!((msr.lo & SMRR_ENABLE) |
| 53 |
&& (msr.lo & FEATURE_CONTROL_LOCK_BIT))) { |
| 54 |
printk(BIOS_WARNING, |
| 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 |
|
| 66 |
static void fill_in_relocation_params(struct smm_relocation_params *params) |
| 67 |
{ |
| 68 |
uintptr_t tseg_base; |
| 69 |
size_t tseg_size; |
| 70 |
|
| 71 |
|
| 72 |
const u32 rmask = ~((1 << 12) - 1); |
| 73 |
|
| 74 |
smm_region(&tseg_base, &tseg_size); |
| 75 |
|
| 76 |
if (!IS_ALIGNED(tseg_base, tseg_size)) { |
| 77 |
printk(BIOS_WARNING, |
| 78 |
"TSEG base not aligned with TSEG SIZE! Not setting SMRR\n"); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 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 |
|
| 89 |
if (cpu_has_alternative_smrr()) |
| 90 |
params->smrr_base.lo &= rmask; |
| 91 |
|
| 92 |
smm_subregion(SMM_SUBREGION_CHIPSET, ¶ms->ied_base, ¶ms->ied_size); |
| 93 |
} |
| 94 |
|
| 95 |
static void setup_ied_area(struct smm_relocation_params *params) |
| 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 |
|
| 108 |
memcpy(ied_base, &ied, sizeof(ied)); |
| 109 |
|
| 110 |
|
| 111 |
memset(ied_base + (1 << 20), 0, (32 << 10)); |
| 112 |
} |
| 113 |
|
| 114 |
void smm_lock(void) |
| 115 |
{ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
printk(BIOS_DEBUG, "Locking SMM.\n"); |
| 121 |
|
| 122 |
northbridge_write_smram(D_LCK | G_SMRAME | C_BASE_SEG); |
| 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 |
|
| 130 |
fill_in_relocation_params(&smm_reloc_params); |
| 131 |
|
| 132 |
smm_subregion(SMM_SUBREGION_HANDLER, perm_smbase, perm_smsize); |
| 133 |
|
| 134 |
if (smm_reloc_params.ied_size) |
| 135 |
setup_ied_area(&smm_reloc_params); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
*smm_save_state_size = sizeof(em64t101_smm_state_save_area_t); |
| 141 |
} |
| 142 |
|
| 143 |
void smm_initialize(void) |
| 144 |
{ |
| 145 |
|
| 146 |
smm_southbridge_clear_state(); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
smm_initiate_relocation(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 164 |
|
| 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 |
|
| 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 |
|
| 188 |
mtrr_cap = rdmsr(MTRR_CAP_MSR); |
| 189 |
if (!(mtrr_cap.lo & SMRR_SUPPORTED)) |
| 190 |
return; |
| 191 |
|
| 192 |
if (cpu_has_alternative_smrr()) |
| 193 |
write_smrr_alt(relo_params); |
| 194 |
else |
| 195 |
write_smrr(relo_params); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void smm_relocate(void) |
| 206 |
{ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if (!boot_cpu()) |
| 214 |
smm_initiate_relocation(); |
| 215 |
} |