coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
cpu.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <console/console.h>
4 #include <cpu/cpu.h>
5 #include <cpu/x86/cr.h>
6 #include <cpu/x86/lapic.h>
7 #include <cpu/x86/mp.h>
8 #include <cpu/x86/msr.h>
9 #include <cpu/x86/mtrr.h>
10 #include <cpu/x86/smm.h>
11 #include <cpu/intel/smm_reloc.h>
13 #include <cpu/intel/turbo.h>
15 #include <device/device.h>
16 #include <device/pci.h>
17 #include <intelblocks/cpulib.h>
18 #include <lib.h>
19 #include <soc/msr.h>
20 #include <soc/cpu.h>
21 #include <soc/iomap.h>
22 #include <soc/smm.h>
23 #include <soc/soc_util.h>
24 #include <types.h>
25 
27 {
28  msr_t msr;
29 
30  msr = rdmsr(MSR_POWER_MISC);
31  return !!(msr.lo & ENABLE_IA_UNTRUSTED);
32 }
33 
34 static struct smm_relocation_attrs relo_attrs;
35 
36 static void dnv_configure_mca(void)
37 {
38  msr_t msr;
39  struct cpuid_result cpuid_regs;
40 
41  /* Check feature flag in CPUID.(EAX=1):EDX[7]==1 MCE
42  * and CPUID.(EAX=1):EDX[14]==1 MCA*/
43  cpuid_regs = cpuid(1);
44  if ((cpuid_regs.edx & (1<<7 | 1<<14)) != (1<<7 | 1<<14))
45  return;
46 
47  msr = rdmsr(IA32_MCG_CAP);
48  if (msr.lo & IA32_MCG_CAP_CTL_P_MASK) {
49  /* Enable all error logging */
50  msr.lo = msr.hi = 0xffffffff;
51  wrmsr(IA32_MCG_CTL, msr);
52  }
53 
54  /* TODO(adurbin): This should only be done on a cold boot. Also, some
55  of these banks are core vs package scope. For now every CPU clears
56  every bank. */
57  mca_configure();
58 
59  /* TODO install a fallback MC handler for each core in case OS does
60  not provide one. Is it really needed? */
61 
62  /* Enable the machine check exception */
64 }
65 
66 static void configure_thermal_core(void)
67 {
68  msr_t msr;
69 
70  /* Disable Thermal interrupts */
71  msr.lo = 0;
72  msr.hi = 0;
75 
76  msr = rdmsr(IA32_MISC_ENABLE);
77  msr.lo |= THERMAL_MONITOR_ENABLE_BIT; /* TM1/TM2/EMTTM enable */
78  wrmsr(IA32_MISC_ENABLE, msr);
79 }
80 
81 static void denverton_core_init(struct device *cpu)
82 {
83  msr_t msr;
84 
85  printk(BIOS_DEBUG, "Init Denverton-NS SoC cores.\n");
86 
87  /* Clear out pending MCEs */
89 
90  /* Configure Thermal Sensors */
92 
93  /* Enable Fast Strings */
94  msr = rdmsr(IA32_MISC_ENABLE);
96  wrmsr(IA32_MISC_ENABLE, msr);
97 
99 
100  /* Enable Turbo */
101  enable_turbo();
102 
103  /* Enable speed step. Always ON.*/
104  msr = rdmsr(IA32_MISC_ENABLE);
105  msr.lo |= SPEED_STEP_ENABLE_BIT;
106  wrmsr(IA32_MISC_ENABLE, msr);
107 
109 }
110 
111 static struct device_operations cpu_dev_ops = {
113 };
114 
115 static const struct cpu_device_id cpu_table[] = {
117  CPUID_DENVERTON_A0_A1}, /* Denverton-NS A0/A1 CPUID */
118  {X86_VENDOR_INTEL, CPUID_DENVERTON_B0}, /* Denverton-NS B0 CPUID */
119  {0, 0},
120 };
121 
122 static const struct cpu_driver driver __cpu_driver = {
123  .ops = &cpu_dev_ops,
124  .id_table = cpu_table,
125 };
126 
127 /*
128  * MP and SMM loading initialization.
129  */
130 
131 static void relocation_handler(int cpu, uintptr_t curr_smbase,
132  uintptr_t staggered_smbase)
133 {
134  msr_t smrr;
135  em64t100_smm_state_save_area_t *smm_state;
136  (void)cpu;
137 
138  /* Set up SMRR. */
139  smrr.lo = relo_attrs.smrr_base;
140  smrr.hi = 0;
141  wrmsr(IA32_SMRR_PHYS_BASE, smrr);
142  smrr.lo = relo_attrs.smrr_mask;
143  smrr.hi = 0;
144  wrmsr(IA32_SMRR_PHYS_MASK, smrr);
145  smm_state = (void *)(SMM_EM64T100_SAVE_STATE_OFFSET + curr_smbase);
146  smm_state->smbase = staggered_smbase;
147 }
148 
149 static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize,
150  size_t *smm_save_state_size)
151 {
152  uintptr_t smm_base;
153  size_t smm_size;
154  uintptr_t handler_base;
155  size_t handler_size;
156 
157  /* All range registers are aligned to 4KiB */
158  const uint32_t rmask = ~((1 << 12) - 1);
159 
160  /* Initialize global tracking state. */
161  smm_region(&smm_base, &smm_size);
162  smm_subregion(SMM_SUBREGION_HANDLER, &handler_base, &handler_size);
163 
164  relo_attrs.smbase = smm_base;
166  relo_attrs.smrr_mask = ~(smm_size - 1) & rmask;
168 
169  *perm_smbase = handler_base;
170  *perm_smsize = handler_size;
171  *smm_save_state_size = sizeof(em64t100_smm_state_save_area_t);
172 }
173 
174 static unsigned int detect_num_cpus_via_cpuid(void)
175 {
176  unsigned int ecx = 0;
177 
178  while (1) {
179  const struct cpuid_result leaf_b = cpuid_ext(0xb, ecx);
180 
181  /* Processor doesn't have hyperthreading so just determine the
182  number of cores from level type (ecx[15:8] == 2). */
183  if ((leaf_b.ecx >> 8 & 0xff) == 2)
184  return leaf_b.ebx & 0xffff;
185 
186  ecx++;
187  }
188 }
189 
190 /* Assumes that FSP has already programmed the cores disabled register */
191 static unsigned int detect_num_cpus_via_mch(void)
192 {
193  /* Get Masks for Total Existing SOC Cores and Core Disable Mask */
194  const u32 core_exists_mask = MMIO32(DEFAULT_MCHBAR + MCH_BAR_CORE_EXISTS_MASK);
195  const u32 core_disable_mask = MMIO32(DEFAULT_MCHBAR + MCH_BAR_CORE_DISABLE_MASK);
196  const u32 active_cores_mask = ~core_disable_mask & core_exists_mask;
197 
198  /* Calculate Number of Active Cores */
199  const unsigned int active_cores = popcnt(active_cores_mask);
200  const unsigned int total_cores = popcnt(core_exists_mask);
201 
202  printk(BIOS_DEBUG, "Number of Active Cores: %u of %u total.\n",
203  active_cores, total_cores);
204 
205  return active_cores;
206 }
207 
208 /* Find CPU topology */
209 int get_cpu_count(void)
210 {
211  unsigned int num_cpus = detect_num_cpus_via_mch();
212 
213  if (num_cpus == 0 || num_cpus > CONFIG_MAX_CPUS) {
214  num_cpus = detect_num_cpus_via_cpuid();
215  printk(BIOS_DEBUG, "Number of Cores (CPUID): %u.\n", num_cpus);
216  }
217  return num_cpus;
218 }
219 
220 static void set_max_turbo_freq(void)
221 {
222  msr_t msr, perf_ctl;
223 
224  perf_ctl.hi = 0;
225 
226  /* Check for configurable TDP option */
227  if (get_turbo_state() == TURBO_ENABLED) {
229  perf_ctl.lo = (msr.lo & 0xff) << 8;
230 
231  } else if (cpu_config_tdp_levels()) {
232  /* Set to nominal TDP ratio */
234  perf_ctl.lo = (msr.lo & 0xff) << 8;
235 
236  } else {
237  /* Platform Info bits 15:8 give max ratio */
238  msr = rdmsr(MSR_PLATFORM_INFO);
239  perf_ctl.lo = msr.lo & 0xff00;
240  }
241  wrmsr(IA32_PERF_CTL, perf_ctl);
242 
243  printk(BIOS_DEBUG, "cpu: frequency set to %d\n",
244  ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK);
245 }
246 
247 /*
248  * Do essential initialization tasks before APs can be fired up
249  *
250  * 1. Prevent race condition in MTRR solution. Enable MTRRs on the BSP. This
251  * creates the MTRR solution that the APs will use. Otherwise APs will try to
252  * apply the incomplete solution as the BSP is calculating it.
253  */
254 static void pre_mp_init(void)
255 {
257  x86_mtrr_check();
258 }
259 
260 static void post_mp_init(void)
261 {
262  /* Set Max Ratio */
264 
265  /*
266  * Now that all APs have been relocated as well as the BSP let SMIs
267  * start flowing.
268  */
270 }
271 
272 /*
273  * CPU initialization recipe
274  *
275  * Note that no microcode update is passed to the init function. CSE updates
276  * the microcode on all cores before releasing them from reset. That means that
277  * the BSP and all APs will come up with the same microcode revision.
278  */
279 static const struct mp_ops mp_ops = {
281  .get_cpu_count = get_cpu_count,
282  .get_smm_info = get_smm_info,
283  .pre_mp_smm_init = smm_southbridge_clear_state,
284  .relocation_handler = relocation_handler,
285  .post_mp_init = post_mp_init,
286 };
287 
288 void mp_init_cpus(struct bus *cpu_bus)
289 {
290  /* Clear for take-off */
291  /* TODO: Handle mp_init_with_smm failure? */
292  mp_init_with_smm(cpu_bus, &mp_ops);
293 }
#define X86_VENDOR_INTEL
Definition: cpu.h:138
static struct cpuid_result cpuid_ext(int op, unsigned int ecx)
Definition: cpu.h:59
#define printk(level,...)
Definition: stdlib.h:16
void set_aesni_lock(void)
Definition: common_init.c:146
#define MSR_TURBO_RATIO_LIMIT
Definition: haswell.h:53
int cpu_config_tdp_levels(void)
Definition: haswell_init.c:300
#define MSR_CONFIG_TDP_NOMINAL
Definition: haswell.h:95
#define CPU_BCLK
Definition: haswell.h:35
enum cb_err mp_init_with_smm(struct bus *cpu_bus, const struct mp_ops *mp_ops)
Definition: mp_init.c:1145
void x86_mtrr_check(void)
Definition: mtrr.c:836
void x86_setup_mtrrs_with_detect(void)
Definition: mtrr.c:823
#define CPUID_DENVERTON_A0_A1
Definition: cpu_ids.h:7
#define CPUID_DENVERTON_B0
Definition: cpu_ids.h:8
void mca_configure(void)
Definition: cpulib.c:376
#define CR4_MCE
Definition: cr.h:120
static __always_inline void write_cr4(CRx_TYPE data)
Definition: cr.h:88
static __always_inline CRx_TYPE read_cr4(void)
Definition: cr.h:76
#define SMM_EM64T100_SAVE_STATE_OFFSET
#define MSR_PLATFORM_INFO
Definition: fsb.c:16
#define FAST_STRINGS_ENABLE_BIT
Definition: msr.h:46
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
#define IA32_MISC_ENABLE
Definition: msr.h:45
#define IA32_PACKAGE_THERM_INTERRUPT
Definition: msr.h:53
#define IA32_PERF_CTL
Definition: msr.h:43
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
#define IA32_MCG_CAP
Definition: msr.h:39
#define SPEED_STEP_ENABLE_BIT
Definition: msr.h:47
#define IA32_THERM_INTERRUPT
Definition: msr.h:44
@ SMM_SUBREGION_HANDLER
Definition: smm.h:171
void global_smi_enable(void)
Set the EOS bit and enable SMI generation from southbridge.
Definition: smi_util.c:60
void smm_region(uintptr_t *start, size_t *size)
Definition: memmap.c:50
#define DEFAULT_MCHBAR
Definition: iomap.h:11
#define MMIO32(x)
Definition: soc_util.h:51
static int popcnt(u32 x)
Definition: lib.h:49
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
void enable_pm_timer_emulation(void)
void smm_southbridge_clear_state(void)
Definition: smm.c:22
void mp_init_cpus(struct bus *cpu_bus)
Definition: cpu.c:55
int get_cpu_count(void)
Definition: cpu.c:10
u32 cpuid
bool cpu_soc_is_in_untrusted_mode(void)
Definition: cpu.c:33
#define ENABLE_IA_UNTRUSTED
Definition: msr.h:9
#define MSR_POWER_MISC
Definition: msr.h:8
static void set_max_turbo_freq(void)
Definition: cpu.c:220
static void relocation_handler(int cpu, uintptr_t curr_smbase, uintptr_t staggered_smbase)
Definition: cpu.c:131
static void denverton_core_init(struct device *cpu)
Definition: cpu.c:81
static unsigned int detect_num_cpus_via_cpuid(void)
Definition: cpu.c:174
static void configure_thermal_core(void)
Definition: cpu.c:66
static struct smm_relocation_attrs relo_attrs
Definition: cpu.c:34
static unsigned int detect_num_cpus_via_mch(void)
Definition: cpu.c:191
static const struct cpu_driver driver __cpu_driver
Definition: cpu.c:122
static void dnv_configure_mca(void)
Definition: cpu.c:36
static void pre_mp_init(void)
Definition: cpu.c:254
static void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, size_t *smm_save_state_size)
Definition: cpu.c:149
static const struct cpu_device_id cpu_table[]
Definition: cpu.c:115
static struct device_operations cpu_dev_ops
Definition: cpu.c:111
static void post_mp_init(void)
Definition: cpu.c:260
#define MCH_BAR_CORE_DISABLE_MASK
Definition: cpu.h:15
#define MCH_BAR_CORE_EXISTS_MASK
Definition: cpu.h:14
#define THERMAL_MONITOR_ENABLE_BIT
Definition: msr.h:27
#define IA32_MCG_CTL
Definition: msr.h:19
#define IA32_MCG_CAP_CTL_P_MASK
Definition: msr.h:18
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
unsigned long uintptr_t
Definition: stdint.h:21
Definition: device.h:76
Definition: cpu.h:13
struct device_operations * ops
Definition: cpu.h:14
uint32_t ecx
Definition: cpu.h:32
uint32_t ebx
Definition: cpu.h:31
uint32_t edx
Definition: cpu.h:33
void(* init)(struct device *dev)
Definition: device.h:42
Definition: device.h:107
Definition: mp.h:20
void(* pre_mp_init)(void)
Definition: mp.h:27
unsigned int hi
Definition: msr.h:112
unsigned int lo
Definition: msr.h:111
uint32_t smrr_mask
Definition: cpu.c:125
uint32_t smrr_base
Definition: cpu.c:124
uint32_t smbase
Definition: cpu.c:123
int smm_subregion(int sub, uintptr_t *start, size_t *size)
Definition: tseg_region.c:22
int get_turbo_state(void)
Definition: turbo.c:75
void enable_turbo(void)
Definition: turbo.c:89
@ TURBO_ENABLED
Definition: turbo.h:18
#define IA32_SMRR_PHYS_MASK
Definition: mtrr.h:31
#define IA32_SMRR_PHYS_BASE
Definition: mtrr.h:30
#define MTRR_TYPE_WRBACK
Definition: mtrr.h:14
#define MTRR_PHYS_MASK_VALID
Definition: mtrr.h:41
typedef void(X86APIP X86EMU_intrFuncs)(int num)