coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
model_206ax_init.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <cpu/cpu.h>
6 #include <cpu/x86/mtrr.h>
7 #include <cpu/x86/msr.h>
8 #include <cpu/x86/mp.h>
9 #include <cpu/intel/microcode.h>
10 #include <cpu/intel/speedstep.h>
11 #include <cpu/intel/turbo.h>
12 #include <cpu/x86/cache.h>
13 #include <cpu/x86/name.h>
14 #include "model_206ax.h"
15 #include "chip.h"
16 #include <cpu/intel/smm_reloc.h>
18 #include <smbios.h>
19 #include <types.h>
20 
21 /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */
22 static const u8 power_limit_time_sec_to_msr[] = {
23  [0] = 0x00,
24  [1] = 0x0a,
25  [2] = 0x0b,
26  [3] = 0x4b,
27  [4] = 0x0c,
28  [5] = 0x2c,
29  [6] = 0x4c,
30  [7] = 0x6c,
31  [8] = 0x0d,
32  [10] = 0x2d,
33  [12] = 0x4d,
34  [14] = 0x6d,
35  [16] = 0x0e,
36  [20] = 0x2e,
37  [24] = 0x4e,
38  [28] = 0x6e,
39  [32] = 0x0f,
40  [40] = 0x2f,
41  [48] = 0x4f,
42  [56] = 0x6f,
43  [64] = 0x10,
44  [80] = 0x30,
45  [96] = 0x50,
46  [112] = 0x70,
47  [128] = 0x11,
48 };
49 
50 /* Convert POWER_LIMIT_1_TIME MSR value to seconds */
51 static const u8 power_limit_time_msr_to_sec[] = {
52  [0x00] = 0,
53  [0x0a] = 1,
54  [0x0b] = 2,
55  [0x4b] = 3,
56  [0x0c] = 4,
57  [0x2c] = 5,
58  [0x4c] = 6,
59  [0x6c] = 7,
60  [0x0d] = 8,
61  [0x2d] = 10,
62  [0x4d] = 12,
63  [0x6d] = 14,
64  [0x0e] = 16,
65  [0x2e] = 20,
66  [0x4e] = 24,
67  [0x6e] = 28,
68  [0x0f] = 32,
69  [0x2f] = 40,
70  [0x4f] = 48,
71  [0x6f] = 56,
72  [0x10] = 64,
73  [0x30] = 80,
74  [0x50] = 96,
75  [0x70] = 112,
76  [0x11] = 128,
77 };
78 
80 {
81  msr_t platform_info;
82 
83  /* Minimum CPU revision */
85  return 0;
86 
87  /* Bits 34:33 indicate how many levels supported */
88  platform_info = rdmsr(MSR_PLATFORM_INFO);
89  return (platform_info.hi >> 1) & 3;
90 }
91 
92 /*
93  * Configure processor power limits if possible
94  * This must be done AFTER set of BIOS_RESET_CPL
95  */
96 void set_power_limits(u8 power_limit_1_time)
97 {
99  msr_t limit;
100  unsigned int power_unit;
101  unsigned int tdp, min_power, max_power, max_time;
102  u8 power_limit_1_val;
103 
104  if (power_limit_1_time >= ARRAY_SIZE(power_limit_time_sec_to_msr))
105  return;
106 
107  if (!(msr.lo & PLATFORM_INFO_SET_TDP))
108  return;
109 
110  /* Get units */
112  power_unit = 2 << ((msr.lo & 0xf) - 1);
113 
114  /* Get power defaults for this SKU */
115  msr = rdmsr(MSR_PKG_POWER_SKU);
116  tdp = msr.lo & 0x7fff;
117  min_power = (msr.lo >> 16) & 0x7fff;
118  max_power = msr.hi & 0x7fff;
119  max_time = (msr.hi >> 16) & 0x7f;
120 
121  printk(BIOS_DEBUG, "CPU TDP: %u Watts\n", tdp / power_unit);
122 
123  if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time)
124  power_limit_1_time = power_limit_time_msr_to_sec[max_time];
125 
126  if (min_power > 0 && tdp < min_power)
127  tdp = min_power;
128 
129  if (max_power > 0 && tdp > max_power)
130  tdp = max_power;
131 
132  power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time];
133 
134  /* Set long term power limit to TDP */
135  limit.lo = 0;
136  limit.lo |= tdp & PKG_POWER_LIMIT_MASK;
137  limit.lo |= PKG_POWER_LIMIT_EN;
138  limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) <<
140 
141  /* Set short term power limit to 1.25 * TDP */
142  limit.hi = 0;
143  limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK;
144  limit.hi |= PKG_POWER_LIMIT_EN;
145  /* Power limit 2 time is only programmable on SNB EP/EX */
146 
147  wrmsr(MSR_PKG_POWER_LIMIT, limit);
148 
149  /* Use nominal TDP values for CPUs with configurable TDP */
150  if (cpu_config_tdp_levels()) {
152  limit.hi = 0;
153  limit.lo = msr.lo & 0xff;
155  }
156 }
157 
158 static void configure_c_states(void)
159 {
160  msr_t msr;
161 
163  msr.lo |= (1 << 28); // C1 Auto Undemotion Enable
164  msr.lo |= (1 << 27); // C3 Auto Undemotion Enable
165  msr.lo |= (1 << 26); // C1 Auto Demotion Enable
166  msr.lo |= (1 << 25); // C3 Auto Demotion Enable
167  msr.lo &= ~(1 << 10); // Disable IO MWAIT redirection
168  msr.lo |= 7; // No package C-state limit
169 
170  msr.lo |= (1 << 15); // Lock C-State MSR
172 
173  msr = rdmsr(MSR_MISC_PWR_MGMT);
174  msr.lo &= ~(1 << 0); // Enable P-state HW_ALL coordination
175  wrmsr(MSR_MISC_PWR_MGMT, msr);
176 
177  msr = rdmsr(MSR_POWER_CTL);
178  msr.lo |= (1 << 18); // Enable Energy Perf Bias MSR 0x1b0
179  msr.lo |= (1 << 1); // C1E Enable
180  msr.lo |= (1 << 0); // Bi-directional PROCHOT#
181  wrmsr(MSR_POWER_CTL, msr);
182 
183  /* C3 Interrupt Response Time Limit */
184  msr.hi = 0;
185  msr.lo = IRTL_VALID | IRTL_1024_NS | 0x50;
186  wrmsr(MSR_PKGC3_IRTL, msr);
187 
188  /* C6 Interrupt Response Time Limit */
189  msr.hi = 0;
190  msr.lo = IRTL_VALID | IRTL_1024_NS | 0x68;
191  wrmsr(MSR_PKGC6_IRTL, msr);
192 
193  /* C7 Interrupt Response Time Limit */
194  msr.hi = 0;
195  msr.lo = IRTL_VALID | IRTL_1024_NS | 0x6D;
196  wrmsr(MSR_PKGC7_IRTL, msr);
197 
198  /* Primary Plane Current Limit */
200  msr.lo &= ~0x1fff;
201  msr.lo |= PP0_CURRENT_LIMIT;
203 
204  /* Secondary Plane Current Limit */
206  msr.lo &= ~0x1fff;
207  if (cpuid_eax(1) >= 0x30600)
208  msr.lo |= PP1_CURRENT_LIMIT_IVB;
209  else
210  msr.lo |= PP1_CURRENT_LIMIT_SNB;
212 }
213 
214 static void configure_thermal_target(void)
215 {
216  struct cpu_intel_model_206ax_config *conf;
217  struct device *lapic;
218  msr_t msr;
219 
220  /* Find pointer to CPU configuration */
222  if (!lapic || !lapic->chip_info)
223  return;
224  conf = lapic->chip_info;
225 
226  /* Set TCC activation offset if supported */
227  msr = rdmsr(MSR_PLATFORM_INFO);
228  if ((msr.lo & (1 << 30)) && conf->tcc_offset) {
230  msr.lo &= ~(0xf << 24); /* Bits 27:24 */
231  msr.lo |= (conf->tcc_offset & 0xf) << 24;
233  }
234 }
235 
236 static void configure_misc(void)
237 {
238  msr_t msr;
239 
240  msr = rdmsr(IA32_MISC_ENABLE);
241  msr.lo |= (1 << 0); /* Fast String enable */
242  msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */
243  msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */
244  wrmsr(IA32_MISC_ENABLE, msr);
245 
246  /* Disable Thermal interrupts */
247  msr.lo = 0;
248  msr.hi = 0;
250 
251  /* Enable package critical interrupt only */
252  msr.lo = 1 << 4;
253  msr.hi = 0;
255 }
256 
257 static void set_max_ratio(void)
258 {
259  msr_t msr, perf_ctl;
260 
261  perf_ctl.hi = 0;
262 
263  /* Check for configurable TDP option */
264  if (cpu_config_tdp_levels()) {
265  /* Set to nominal TDP ratio */
267  perf_ctl.lo = (msr.lo & 0xff) << 8;
268  } else {
269  /* Platform Info bits 15:8 give max ratio */
270  msr = rdmsr(MSR_PLATFORM_INFO);
271  perf_ctl.lo = msr.lo & 0xff00;
272  }
273  wrmsr(IA32_PERF_CTL, perf_ctl);
274 
275  printk(BIOS_DEBUG, "model_x06ax: frequency set to %d\n",
276  ((perf_ctl.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK);
277 }
278 
280 {
281  msr_t msr;
283  return (msr.lo & 0xff) * SANDYBRIDGE_BCLK;
284 }
285 
287 {
288  msr_t msr;
289  msr = rdmsr(MSR_PLATFORM_INFO);
290  return ((msr.lo >> 8) & 0xff) * SANDYBRIDGE_BCLK;
291 }
292 
294 {
295  return SANDYBRIDGE_BCLK;
296 }
297 
298 static void model_206ax_report(void)
299 {
300  static const char *const mode[] = {"NOT ", ""};
301  char processor_name[49];
302  int vt, txt, aes;
303  uint32_t cpu_id, cpu_feature_flag;
304 
305  /* Print processor name */
307  printk(BIOS_INFO, "CPU: %s.\n", processor_name);
308 
309  /* Print platform ID */
310  printk(BIOS_INFO, "CPU: platform id %x\n", get_platform_id());
311 
312  /* CPUID and features */
313  cpu_id = cpu_get_cpuid();
314  printk(BIOS_INFO, "CPU: cpuid(1) 0x%x\n", cpu_id);
315 
316  cpu_feature_flag = cpu_get_feature_flags_ecx();
317  aes = (cpu_feature_flag & CPUID_AES) ? 1 : 0;
318  txt = (cpu_feature_flag & CPUID_SMX) ? 1 : 0;
319  vt = (cpu_feature_flag & CPUID_VMX) ? 1 : 0;
320  printk(BIOS_INFO, "CPU: AES %ssupported\n", mode[aes]);
321  printk(BIOS_INFO, "CPU: TXT %ssupported\n", mode[txt]);
322  printk(BIOS_INFO, "CPU: VT %ssupported\n", mode[vt]);
323 }
324 
325 static void model_206ax_init(struct device *cpu)
326 {
327 
328  /* Clear out pending MCEs */
329  /* This should only be done on a cold boot */
331 
332  /* Print infos */
334 
335  /* Setup Page Attribute Tables (PAT) */
336  // TODO set up PAT
337 
339 
340  /* Set virtualization based on Kconfig option */
342 
343  /* Configure C States */
345 
346  /* Configure Enhanced SpeedStep and Thermal Sensors */
347  configure_misc();
348 
349  /* Thermal throttle activation offset */
351 
352  set_aesni_lock();
353 
354  /* Enable Direct Cache Access */
356 
357  /* Set energy policy */
359 
360  /* Set Max Ratio */
361  set_max_ratio();
362 
363  /* Enable Turbo */
364  enable_turbo();
365 }
366 
367 /* MP initialization support. */
368 static void pre_mp_init(void)
369 {
370  /* Setup MTRRs based on physical address size. */
372  x86_mtrr_check();
373 }
374 
375 static int get_cpu_count(void)
376 {
377  msr_t msr;
378  unsigned int num_threads;
379  unsigned int num_cores;
380 
382  num_threads = (msr.lo >> 0) & 0xffff;
383  num_cores = (msr.lo >> 16) & 0xffff;
384  printk(BIOS_DEBUG, "CPU has %u cores, %u threads enabled.\n",
385  num_cores, num_threads);
386 
387  return num_threads;
388 }
389 
390 static void get_microcode_info(const void **microcode, int *parallel)
391 {
393  *parallel = !intel_ht_supported();
394 }
395 
396 static void per_cpu_smm_trigger(void)
397 {
398  /* Relocate the SMM handler. */
399  smm_relocate();
400 
401  /* After SMM relocation a 2nd microcode load is required. */
402  const void *microcode_patch = intel_microcode_find();
404 }
405 
406 static void post_mp_init(void)
407 {
408  /* Now that all APs have been relocated as well as the BSP let SMIs
409  * start flowing. */
411 
412  /* Lock down the SMRAM space. */
413  smm_lock();
414 }
415 
416 static const struct mp_ops mp_ops = {
418  .get_cpu_count = get_cpu_count,
419  .get_smm_info = smm_info,
420  .get_microcode_info = get_microcode_info,
421  .pre_mp_smm_init = smm_initialize,
422  .per_cpu_smm_trigger = per_cpu_smm_trigger,
423  .relocation_handler = smm_relocation_handler,
424  .post_mp_init = post_mp_init,
425 };
426 
427 void mp_init_cpus(struct bus *cpu_bus)
428 {
429  /* TODO: Handle mp_init_with_smm failure? */
430  mp_init_with_smm(cpu_bus, &mp_ops);
431 }
432 
433 static struct device_operations cpu_dev_ops = {
435 };
436 
437 static const struct cpu_device_id cpu_table[] = {
438  { X86_VENDOR_INTEL, 0x206a0 }, /* Intel Sandybridge */
439  { X86_VENDOR_INTEL, 0x206a6 }, /* Intel Sandybridge D1 */
440  { X86_VENDOR_INTEL, 0x206a7 }, /* Intel Sandybridge D2/J1 */
441  { X86_VENDOR_INTEL, 0x306a0 }, /* Intel IvyBridge */
442  { X86_VENDOR_INTEL, 0x306a2 }, /* Intel IvyBridge */
443  { X86_VENDOR_INTEL, 0x306a4 }, /* Intel IvyBridge */
444  { X86_VENDOR_INTEL, 0x306a5 }, /* Intel IvyBridge */
445  { X86_VENDOR_INTEL, 0x306a6 }, /* Intel IvyBridge */
446  { X86_VENDOR_INTEL, 0x306a8 }, /* Intel IvyBridge */
447  { X86_VENDOR_INTEL, 0x306a9 }, /* Intel IvyBridge */
448  { 0, 0 },
449 };
450 
451 static const struct cpu_driver driver __cpu_driver = {
452  .ops = &cpu_dev_ops,
453  .id_table = cpu_table,
454 };
#define X86_VENDOR_INTEL
Definition: cpu.h:138
static unsigned int cpuid_eax(unsigned int op)
Definition: cpu.h:79
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
void configure_dca_cap(void)
Definition: common_init.c:172
void set_aesni_lock(void)
Definition: common_init.c:146
void set_energy_perf_bias(u8 policy)
Definition: common_init.c:178
void enable_lapic_tpr(void)
Definition: common_init.c:167
void set_vmx_and_lock(void)
Definition: common_init.c:15
bool intel_ht_supported(void)
Definition: hyperthreading.c:7
#define SPEEDSTEP_APIC_MAGIC
Definition: chip.h:4
#define MSR_MISC_PWR_MGMT
Definition: haswell.h:51
#define MSR_PKG_POWER_SKU
Definition: haswell.h:89
#define MSR_TEMPERATURE_TARGET
Definition: haswell.h:50
#define MSR_POWER_CTL
Definition: haswell.h:56
#define MSR_TURBO_RATIO_LIMIT
Definition: haswell.h:53
#define MSR_TURBO_ACTIVATION_RATIO
Definition: haswell.h:99
#define IRTL_VALID
Definition: haswell.h:69
#define MSR_PKG_POWER_LIMIT
Definition: haswell.h:79
#define PKG_POWER_LIMIT_TIME_SHIFT
Definition: haswell.h:83
#define PKG_POWER_LIMIT_TIME_MASK
Definition: haswell.h:84
#define PKG_POWER_LIMIT_MASK
Definition: haswell.h:80
#define PLATFORM_INFO_SET_TDP
Definition: haswell.h:39
#define MSR_CORE_THREAD_COUNT
Definition: haswell.h:37
#define MSR_PKG_POWER_SKU_UNIT
Definition: haswell.h:88
#define MSR_CONFIG_TDP_NOMINAL
Definition: haswell.h:95
#define PKG_POWER_LIMIT_EN
Definition: haswell.h:81
#define IRTL_1024_NS
Definition: haswell.h:72
#define MSR_PKG_CST_CONFIG_CONTROL
Definition: haswell.h:41
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
int get_platform_id(void)
Definition: common.c:6
static char processor_name[49]
Definition: mp_init.c:37
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
uint32_t cpu_get_feature_flags_ecx(void)
Definition: cpu_common.c:72
uint32_t cpu_get_cpuid(void)
Definition: cpu_common.c:63
struct device * dev_find_lapic(unsigned int apic_id)
Given a Local APIC ID, find the device structure.
Definition: device_util.c:17
#define MSR_PLATFORM_INFO
Definition: fsb.c:16
static const void * microcode_patch
Definition: haswell_init.c:567
static __always_inline msr_t rdmsr(unsigned int index)
Definition: msr.h:146
#define IA32_MISC_ENABLE
Definition: msr.h:45
static void mca_clear_status(void)
Definition: msr.h:176
#define IA32_PACKAGE_THERM_INTERRUPT
Definition: msr.h:53
#define IA32_PERF_CTL
Definition: msr.h:43
#define CPUID_AES
Definition: msr.h:28
#define CPUID_VMX
Definition: msr.h:24
#define ENERGY_POLICY_NORMAL
Definition: msr.h:50
static __always_inline void wrmsr(unsigned int index, msr_t msr)
Definition: msr.h:157
#define CPUID_SMX
Definition: msr.h:25
#define IA32_THERM_INTERRUPT
Definition: msr.h:44
void global_smi_enable(void)
Set the EOS bit and enable SMI generation from southbridge.
Definition: smi_util.c:60
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
void intel_microcode_load_unlocked(const void *microcode_patch)
Definition: microcode.c:71
const void * intel_microcode_find(void)
Definition: microcode.c:223
#define MSR_PKGC7_IRTL
Definition: model_2065x.h:29
#define MSR_PKGC3_IRTL
Definition: model_2065x.h:27
#define MSR_PKGC6_IRTL
Definition: model_2065x.h:28
#define PP1_CURRENT_LIMIT_SNB
Definition: model_206ax.h:78
#define PP1_CURRENT_LIMIT_IVB
Definition: model_206ax.h:79
#define IVB_CONFIG_TDP_MIN_CPUID
Definition: model_206ax.h:85
#define MSR_PP1_CURRENT_CONFIG
Definition: model_206ax.h:77
#define PP0_CURRENT_LIMIT
Definition: model_206ax.h:76
#define SANDYBRIDGE_BCLK
Definition: model_206ax.h:38
#define MSR_PP0_CURRENT_CONFIG
Definition: model_206ax.h:75
static void get_microcode_info(const void **microcode, int *parallel)
static const u8 power_limit_time_sec_to_msr[]
void mp_init_cpus(struct bus *cpu_bus)
static void model_206ax_report(void)
static const struct cpu_driver driver __cpu_driver
static void configure_c_states(void)
static void set_max_ratio(void)
static void configure_misc(void)
void set_power_limits(u8 power_limit_1_time)
static void pre_mp_init(void)
unsigned int smbios_cpu_get_current_speed_mhz(void)
unsigned int smbios_cpu_get_max_speed_mhz(void)
static void per_cpu_smm_trigger(void)
static void configure_thermal_target(void)
unsigned int smbios_processor_external_clock(void)
int cpu_config_tdp_levels(void)
static const struct cpu_device_id cpu_table[]
static int get_cpu_count(void)
static const u8 power_limit_time_msr_to_sec[]
static struct device_operations cpu_dev_ops
static void model_206ax_init(struct device *cpu)
static void post_mp_init(void)
void fill_processor_name(char *processor_name)
Definition: name.c:8
unsigned int cpu_id
Definition: chip.h:47
unsigned int uint32_t
Definition: stdint.h:14
uint8_t u8
Definition: stdint.h:45
Definition: device.h:76
Definition: cpu.h:13
struct device_operations * ops
Definition: cpu.h:14
void(* init)(struct device *dev)
Definition: device.h:42
Definition: device.h:107
DEVTREE_CONST void * chip_info
Definition: device.h:164
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
void enable_turbo(void)
Definition: turbo.c:89