blob: e7f4191304298eb7c37f9eef5c28e608763e728a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass780ba482016-03-11 22:06:58 -07002/*
Simon Glassa0163682019-09-25 08:56:40 -06003 * Copyright (C) 2014 Google Inc.
Simon Glass780ba482016-03-11 22:06:58 -07004 * Copyright (c) 2016 Google, Inc
Simon Glassa0163682019-09-25 08:56:40 -06005 * Copyright (C) 2015-2018 Intel Corporation.
6 * Copyright (C) 2018 Siemens AG
7 * Some code taken from coreboot cpulib.c
Simon Glass780ba482016-03-11 22:06:58 -07008 */
9
Simon Glassaba3c602019-09-25 08:11:35 -060010#include <cpu.h>
Simon Glass780ba482016-03-11 22:06:58 -070011#include <dm.h>
12#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassac779e32020-09-22 12:45:08 -060014#include <acpi/acpigen.h>
Simon Glassa0163682019-09-25 08:56:40 -060015#include <asm/cpu.h>
Simon Glass780ba482016-03-11 22:06:58 -070016#include <asm/cpu_common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass780ba482016-03-11 22:06:58 -070018#include <asm/intel_regs.h>
19#include <asm/lapic.h>
20#include <asm/lpc_common.h>
21#include <asm/msr.h>
22#include <asm/mtrr.h>
23#include <asm/post.h>
24#include <asm/microcode.h>
25
26DECLARE_GLOBAL_DATA_PTR;
27
28static int report_bist_failure(void)
29{
30 if (gd->arch.bist != 0) {
31 post_code(POST_BIST_FAILURE);
32 printf("BIST failed: %08x\n", gd->arch.bist);
33 return -EFAULT;
34 }
35
36 return 0;
37}
38
39int cpu_common_init(void)
40{
41 struct udevice *dev, *lpc;
42 int ret;
43
44 /* Halt if there was a built in self test failure */
45 ret = report_bist_failure();
46 if (ret)
47 return ret;
48
49 enable_lapic();
50
51 ret = microcode_update_intel();
Simon Glass7f99c7c2016-07-25 18:58:57 -060052 if (ret && ret != -EEXIST) {
53 debug("%s: Microcode update failure (err=%d)\n", __func__, ret);
Simon Glass780ba482016-03-11 22:06:58 -070054 return ret;
Simon Glass7f99c7c2016-07-25 18:58:57 -060055 }
Simon Glass780ba482016-03-11 22:06:58 -070056
57 /* Enable upper 128bytes of CMOS */
58 writel(1 << 2, RCB_REG(RC));
59
60 /* Early chipset init required before RAM init can work */
61 uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
62
Michal Suchanekac12a2f2022-10-12 21:57:59 +020063 ret = uclass_first_device_err(UCLASS_LPC, &lpc);
Simon Glass780ba482016-03-11 22:06:58 -070064 if (ret)
65 return ret;
Simon Glass780ba482016-03-11 22:06:58 -070066
67 /* Cause the SATA device to do its early init */
Simon Glass85ee1652016-05-01 11:35:52 -060068 uclass_first_device(UCLASS_AHCI, &dev);
Simon Glass780ba482016-03-11 22:06:58 -070069
70 return 0;
71}
72
73int cpu_set_flex_ratio_to_tdp_nominal(void)
74{
75 msr_t flex_ratio, msr;
76 u8 nominal_ratio;
77
78 /* Check for Flex Ratio support */
79 flex_ratio = msr_read(MSR_FLEX_RATIO);
80 if (!(flex_ratio.lo & FLEX_RATIO_EN))
81 return -EINVAL;
82
83 /* Check for >0 configurable TDPs */
84 msr = msr_read(MSR_PLATFORM_INFO);
85 if (((msr.hi >> 1) & 3) == 0)
86 return -EINVAL;
87
88 /* Use nominal TDP ratio for flex ratio */
89 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
90 nominal_ratio = msr.lo & 0xff;
91
92 /* See if flex ratio is already set to nominal TDP ratio */
93 if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio)
94 return 0;
95
96 /* Set flex ratio to nominal TDP ratio */
97 flex_ratio.lo &= ~0xff00;
98 flex_ratio.lo |= nominal_ratio << 8;
99 flex_ratio.lo |= FLEX_RATIO_LOCK;
100 msr_write(MSR_FLEX_RATIO, flex_ratio);
101
102 /* Set flex ratio in soft reset data register bits 11:6 */
103 clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6,
104 (nominal_ratio & 0x3f) << 6);
105
106 debug("CPU: Soft reset to set up flex ratio\n");
107
108 /* Set soft reset control to use register value */
109 setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1);
110
111 /* Issue warm reset, will be "CPU only" due to soft reset data */
Simon Glass8b73e9f2016-03-11 22:06:59 -0700112 outb(0x0, IO_PORT_RESET);
113 outb(SYS_RST | RST_CPU, IO_PORT_RESET);
Simon Glass780ba482016-03-11 22:06:58 -0700114 cpu_hlt();
115
116 /* Not reached */
117 return -EINVAL;
118}
Simon Glassaba3c602019-09-25 08:11:35 -0600119
120int cpu_intel_get_info(struct cpu_info *info, int bclk)
121{
122 msr_t msr;
123
Simon Glass76ae0272019-09-25 08:56:35 -0600124 msr = msr_read(MSR_IA32_PERF_CTL);
Simon Glassaba3c602019-09-25 08:11:35 -0600125 info->cpu_freq = ((msr.lo >> 8) & 0xff) * bclk * 1000000;
126 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
127 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
Simon Glass2d8b3c62020-09-22 12:45:26 -0600128 info->address_width = cpu_phys_address_size();
Simon Glassaba3c602019-09-25 08:11:35 -0600129
130 return 0;
131}
Simon Glass23a6ca92019-09-25 08:56:36 -0600132
133int cpu_configure_thermal_target(struct udevice *dev)
134{
135 u32 tcc_offset;
136 msr_t msr;
137 int ret;
138
139 ret = dev_read_u32(dev, "tcc-offset", &tcc_offset);
140 if (!ret)
141 return -ENOENT;
142
143 /* Set TCC activaiton offset if supported */
144 msr = msr_read(MSR_PLATFORM_INFO);
145 if (msr.lo & (1 << 30)) {
146 msr = msr_read(MSR_TEMPERATURE_TARGET);
147 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
148 msr.lo |= (tcc_offset & 0xf) << 24;
149 msr_write(MSR_TEMPERATURE_TARGET, msr);
150 }
151
152 return 0;
153}
Simon Glassb12689d2019-09-25 08:56:38 -0600154
155void cpu_set_perf_control(uint clk_ratio)
156{
157 msr_t perf_ctl;
158
159 perf_ctl.lo = (clk_ratio & 0xff) << 8;
160 perf_ctl.hi = 0;
161 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
162 debug("CPU: frequency set to %d MHz\n", clk_ratio * INTEL_BCLK_MHZ);
163}
164
165bool cpu_config_tdp_levels(void)
166{
167 msr_t platform_info;
168
169 /* Bits 34:33 indicate how many levels supported */
170 platform_info = msr_read(MSR_PLATFORM_INFO);
171
172 return ((platform_info.hi >> 1) & 3) != 0;
173}
Simon Glassa0163682019-09-25 08:56:40 -0600174
175void cpu_set_p_state_to_turbo_ratio(void)
176{
177 msr_t msr;
178
179 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
180 cpu_set_perf_control(msr.lo);
181}
182
183enum burst_mode_t cpu_get_burst_mode_state(void)
184{
185 enum burst_mode_t state;
186 int burst_en, burst_cap;
187 msr_t msr;
188 uint eax;
189
190 eax = cpuid_eax(0x6);
191 burst_cap = eax & 0x2;
192 msr = msr_read(MSR_IA32_MISC_ENABLE);
193 burst_en = !(msr.hi & BURST_MODE_DISABLE);
194
195 if (!burst_cap && burst_en)
196 state = BURST_MODE_UNAVAILABLE;
197 else if (burst_cap && !burst_en)
198 state = BURST_MODE_DISABLED;
199 else if (burst_cap && burst_en)
200 state = BURST_MODE_ENABLED;
201 else
202 state = BURST_MODE_UNKNOWN;
203
204 return state;
205}
206
207void cpu_set_burst_mode(bool burst_mode)
208{
209 msr_t msr;
210
211 msr = msr_read(MSR_IA32_MISC_ENABLE);
212 if (burst_mode)
213 msr.hi &= ~BURST_MODE_DISABLE;
214 else
215 msr.hi |= BURST_MODE_DISABLE;
216 msr_write(MSR_IA32_MISC_ENABLE, msr);
217}
218
219void cpu_set_eist(bool eist_status)
220{
221 msr_t msr;
222
223 msr = msr_read(MSR_IA32_MISC_ENABLE);
224 if (eist_status)
225 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
226 else
227 msr.lo &= ~MISC_ENABLE_ENHANCED_SPEEDSTEP;
228 msr_write(MSR_IA32_MISC_ENABLE, msr);
229}
Simon Glassac779e32020-09-22 12:45:08 -0600230
231int cpu_get_coord_type(void)
232{
233 return HW_ALL;
234}
235
236int cpu_get_min_ratio(void)
237{
238 msr_t msr;
239
240 /* Get bus ratio limits and calculate clock speeds */
241 msr = msr_read(MSR_PLATFORM_INFO);
242
243 return (msr.hi >> 8) & 0xff; /* Max Efficiency Ratio */
244}
245
246int cpu_get_max_ratio(void)
247{
248 u32 ratio_max;
249 msr_t msr;
250
251 if (cpu_config_tdp_levels()) {
252 /* Set max ratio to nominal TDP ratio */
253 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
254 ratio_max = msr.lo & 0xff;
255 } else {
256 msr = msr_read(MSR_PLATFORM_INFO);
257 /* Max Non-Turbo Ratio */
258 ratio_max = (msr.lo >> 8) & 0xff;
259 }
260
261 return ratio_max;
262}
263
264int cpu_get_bus_clock_khz(void)
265{
266 /*
267 * CPU bus clock is set by default here to 100MHz. This function returns
268 * the bus clock in KHz.
269 */
270 return INTEL_BCLK_MHZ * 1000;
271}
272
273int cpu_get_power_max(void)
274{
275 int power_unit;
276 msr_t msr;
277
278 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
279 power_unit = 2 << ((msr.lo & 0xf) - 1);
280 msr = msr_read(MSR_PKG_POWER_SKU);
281
282 return (msr.lo & 0x7fff) * 1000 / power_unit;
283}
284
285int cpu_get_max_turbo_ratio(void)
286{
287 msr_t msr;
288
289 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
290
291 return msr.lo & 0xff;
292}
Simon Glassd73344b2020-09-22 12:45:14 -0600293
294int cpu_get_cores_per_package(void)
295{
296 struct cpuid_result result;
297 int cores = 1;
298
299 if (gd->arch.x86_vendor != X86_VENDOR_INTEL)
300 return 1;
301
302 result = cpuid_ext(0xb, 1);
303 cores = result.ebx & 0xff;
304
305 return cores;
306}
Simon Glassf07f4b92020-11-04 09:57:15 -0700307
308void cpu_mca_configure(void)
309{
310 msr_t msr;
311 int i;
312 int num_banks;
313
314 msr = msr_read(MSR_IA32_MCG_CAP);
315 num_banks = msr.lo & 0xff;
316 msr.lo = 0;
317 msr.hi = 0;
318 for (i = 0; i < num_banks; i++) {
319 /* Clear the machine check status */
320 msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr);
321 /* Initialise machine checks */
322 msr_write(MSR_IA32_MC0_CTL + i * 4,
323 (msr_t) {.lo = 0xffffffff, .hi = 0xffffffff});
324 }
325}