blob: 39aa0f63c65b75b687abd77125a3556f12a14440 [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
10#include <common.h>
Simon Glassaba3c602019-09-25 08:11:35 -060011#include <cpu.h>
Simon Glass780ba482016-03-11 22:06:58 -070012#include <dm.h>
13#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassac779e32020-09-22 12:45:08 -060015#include <acpi/acpigen.h>
Simon Glassa0163682019-09-25 08:56:40 -060016#include <asm/cpu.h>
Simon Glass780ba482016-03-11 22:06:58 -070017#include <asm/cpu_common.h>
18#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
63 ret = uclass_first_device(UCLASS_LPC, &lpc);
64 if (ret)
65 return ret;
66 if (!lpc)
67 return -ENODEV;
68
69 /* Cause the SATA device to do its early init */
Simon Glass85ee1652016-05-01 11:35:52 -060070 uclass_first_device(UCLASS_AHCI, &dev);
Simon Glass780ba482016-03-11 22:06:58 -070071
72 return 0;
73}
74
75int cpu_set_flex_ratio_to_tdp_nominal(void)
76{
77 msr_t flex_ratio, msr;
78 u8 nominal_ratio;
79
80 /* Check for Flex Ratio support */
81 flex_ratio = msr_read(MSR_FLEX_RATIO);
82 if (!(flex_ratio.lo & FLEX_RATIO_EN))
83 return -EINVAL;
84
85 /* Check for >0 configurable TDPs */
86 msr = msr_read(MSR_PLATFORM_INFO);
87 if (((msr.hi >> 1) & 3) == 0)
88 return -EINVAL;
89
90 /* Use nominal TDP ratio for flex ratio */
91 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
92 nominal_ratio = msr.lo & 0xff;
93
94 /* See if flex ratio is already set to nominal TDP ratio */
95 if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio)
96 return 0;
97
98 /* Set flex ratio to nominal TDP ratio */
99 flex_ratio.lo &= ~0xff00;
100 flex_ratio.lo |= nominal_ratio << 8;
101 flex_ratio.lo |= FLEX_RATIO_LOCK;
102 msr_write(MSR_FLEX_RATIO, flex_ratio);
103
104 /* Set flex ratio in soft reset data register bits 11:6 */
105 clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6,
106 (nominal_ratio & 0x3f) << 6);
107
108 debug("CPU: Soft reset to set up flex ratio\n");
109
110 /* Set soft reset control to use register value */
111 setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1);
112
113 /* Issue warm reset, will be "CPU only" due to soft reset data */
Simon Glass8b73e9f2016-03-11 22:06:59 -0700114 outb(0x0, IO_PORT_RESET);
115 outb(SYS_RST | RST_CPU, IO_PORT_RESET);
Simon Glass780ba482016-03-11 22:06:58 -0700116 cpu_hlt();
117
118 /* Not reached */
119 return -EINVAL;
120}
Simon Glassaba3c602019-09-25 08:11:35 -0600121
122int cpu_intel_get_info(struct cpu_info *info, int bclk)
123{
124 msr_t msr;
125
Simon Glass76ae0272019-09-25 08:56:35 -0600126 msr = msr_read(MSR_IA32_PERF_CTL);
Simon Glassaba3c602019-09-25 08:11:35 -0600127 info->cpu_freq = ((msr.lo >> 8) & 0xff) * bclk * 1000000;
128 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
129 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
Simon Glass2d8b3c62020-09-22 12:45:26 -0600130 info->address_width = cpu_phys_address_size();
Simon Glassaba3c602019-09-25 08:11:35 -0600131
132 return 0;
133}
Simon Glass23a6ca92019-09-25 08:56:36 -0600134
135int cpu_configure_thermal_target(struct udevice *dev)
136{
137 u32 tcc_offset;
138 msr_t msr;
139 int ret;
140
141 ret = dev_read_u32(dev, "tcc-offset", &tcc_offset);
142 if (!ret)
143 return -ENOENT;
144
145 /* Set TCC activaiton offset if supported */
146 msr = msr_read(MSR_PLATFORM_INFO);
147 if (msr.lo & (1 << 30)) {
148 msr = msr_read(MSR_TEMPERATURE_TARGET);
149 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
150 msr.lo |= (tcc_offset & 0xf) << 24;
151 msr_write(MSR_TEMPERATURE_TARGET, msr);
152 }
153
154 return 0;
155}
Simon Glassb12689d2019-09-25 08:56:38 -0600156
157void cpu_set_perf_control(uint clk_ratio)
158{
159 msr_t perf_ctl;
160
161 perf_ctl.lo = (clk_ratio & 0xff) << 8;
162 perf_ctl.hi = 0;
163 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
164 debug("CPU: frequency set to %d MHz\n", clk_ratio * INTEL_BCLK_MHZ);
165}
166
167bool cpu_config_tdp_levels(void)
168{
169 msr_t platform_info;
170
171 /* Bits 34:33 indicate how many levels supported */
172 platform_info = msr_read(MSR_PLATFORM_INFO);
173
174 return ((platform_info.hi >> 1) & 3) != 0;
175}
Simon Glassa0163682019-09-25 08:56:40 -0600176
177void cpu_set_p_state_to_turbo_ratio(void)
178{
179 msr_t msr;
180
181 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
182 cpu_set_perf_control(msr.lo);
183}
184
185enum burst_mode_t cpu_get_burst_mode_state(void)
186{
187 enum burst_mode_t state;
188 int burst_en, burst_cap;
189 msr_t msr;
190 uint eax;
191
192 eax = cpuid_eax(0x6);
193 burst_cap = eax & 0x2;
194 msr = msr_read(MSR_IA32_MISC_ENABLE);
195 burst_en = !(msr.hi & BURST_MODE_DISABLE);
196
197 if (!burst_cap && burst_en)
198 state = BURST_MODE_UNAVAILABLE;
199 else if (burst_cap && !burst_en)
200 state = BURST_MODE_DISABLED;
201 else if (burst_cap && burst_en)
202 state = BURST_MODE_ENABLED;
203 else
204 state = BURST_MODE_UNKNOWN;
205
206 return state;
207}
208
209void cpu_set_burst_mode(bool burst_mode)
210{
211 msr_t msr;
212
213 msr = msr_read(MSR_IA32_MISC_ENABLE);
214 if (burst_mode)
215 msr.hi &= ~BURST_MODE_DISABLE;
216 else
217 msr.hi |= BURST_MODE_DISABLE;
218 msr_write(MSR_IA32_MISC_ENABLE, msr);
219}
220
221void cpu_set_eist(bool eist_status)
222{
223 msr_t msr;
224
225 msr = msr_read(MSR_IA32_MISC_ENABLE);
226 if (eist_status)
227 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
228 else
229 msr.lo &= ~MISC_ENABLE_ENHANCED_SPEEDSTEP;
230 msr_write(MSR_IA32_MISC_ENABLE, msr);
231}
Simon Glassac779e32020-09-22 12:45:08 -0600232
233int cpu_get_coord_type(void)
234{
235 return HW_ALL;
236}
237
238int cpu_get_min_ratio(void)
239{
240 msr_t msr;
241
242 /* Get bus ratio limits and calculate clock speeds */
243 msr = msr_read(MSR_PLATFORM_INFO);
244
245 return (msr.hi >> 8) & 0xff; /* Max Efficiency Ratio */
246}
247
248int cpu_get_max_ratio(void)
249{
250 u32 ratio_max;
251 msr_t msr;
252
253 if (cpu_config_tdp_levels()) {
254 /* Set max ratio to nominal TDP ratio */
255 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
256 ratio_max = msr.lo & 0xff;
257 } else {
258 msr = msr_read(MSR_PLATFORM_INFO);
259 /* Max Non-Turbo Ratio */
260 ratio_max = (msr.lo >> 8) & 0xff;
261 }
262
263 return ratio_max;
264}
265
266int cpu_get_bus_clock_khz(void)
267{
268 /*
269 * CPU bus clock is set by default here to 100MHz. This function returns
270 * the bus clock in KHz.
271 */
272 return INTEL_BCLK_MHZ * 1000;
273}
274
275int cpu_get_power_max(void)
276{
277 int power_unit;
278 msr_t msr;
279
280 msr = msr_read(MSR_PKG_POWER_SKU_UNIT);
281 power_unit = 2 << ((msr.lo & 0xf) - 1);
282 msr = msr_read(MSR_PKG_POWER_SKU);
283
284 return (msr.lo & 0x7fff) * 1000 / power_unit;
285}
286
287int cpu_get_max_turbo_ratio(void)
288{
289 msr_t msr;
290
291 msr = msr_read(MSR_TURBO_RATIO_LIMIT);
292
293 return msr.lo & 0xff;
294}
Simon Glassd73344b2020-09-22 12:45:14 -0600295
296int cpu_get_cores_per_package(void)
297{
298 struct cpuid_result result;
299 int cores = 1;
300
301 if (gd->arch.x86_vendor != X86_VENDOR_INTEL)
302 return 1;
303
304 result = cpuid_ext(0xb, 1);
305 cores = result.ebx & 0xff;
306
307 return cores;
308}