Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / x86 / cpu / intel_common / cpu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Google Inc.
4  * Copyright (c) 2016 Google, Inc
5  * Copyright (C) 2015-2018 Intel Corporation.
6  * Copyright (C) 2018 Siemens AG
7  * Some code taken from coreboot cpulib.c
8  */
9
10 #include <common.h>
11 #include <cpu.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <log.h>
15 #include <asm/cpu.h>
16 #include <asm/cpu_common.h>
17 #include <asm/intel_regs.h>
18 #include <asm/lapic.h>
19 #include <asm/lpc_common.h>
20 #include <asm/msr.h>
21 #include <asm/mtrr.h>
22 #include <asm/post.h>
23 #include <asm/microcode.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 static int report_bist_failure(void)
28 {
29         if (gd->arch.bist != 0) {
30                 post_code(POST_BIST_FAILURE);
31                 printf("BIST failed: %08x\n", gd->arch.bist);
32                 return -EFAULT;
33         }
34
35         return 0;
36 }
37
38 int cpu_common_init(void)
39 {
40         struct udevice *dev, *lpc;
41         int ret;
42
43         /* Halt if there was a built in self test failure */
44         ret = report_bist_failure();
45         if (ret)
46                 return ret;
47
48         enable_lapic();
49
50         ret = microcode_update_intel();
51         if (ret && ret != -EEXIST) {
52                 debug("%s: Microcode update failure (err=%d)\n", __func__, ret);
53                 return ret;
54         }
55
56         /* Enable upper 128bytes of CMOS */
57         writel(1 << 2, RCB_REG(RC));
58
59         /* Early chipset init required before RAM init can work */
60         uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
61
62         ret = uclass_first_device(UCLASS_LPC, &lpc);
63         if (ret)
64                 return ret;
65         if (!lpc)
66                 return -ENODEV;
67
68         /* Cause the SATA device to do its early init */
69         uclass_first_device(UCLASS_AHCI, &dev);
70
71         return 0;
72 }
73
74 int cpu_set_flex_ratio_to_tdp_nominal(void)
75 {
76         msr_t flex_ratio, msr;
77         u8 nominal_ratio;
78
79         /* Check for Flex Ratio support */
80         flex_ratio = msr_read(MSR_FLEX_RATIO);
81         if (!(flex_ratio.lo & FLEX_RATIO_EN))
82                 return -EINVAL;
83
84         /* Check for >0 configurable TDPs */
85         msr = msr_read(MSR_PLATFORM_INFO);
86         if (((msr.hi >> 1) & 3) == 0)
87                 return -EINVAL;
88
89         /* Use nominal TDP ratio for flex ratio */
90         msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
91         nominal_ratio = msr.lo & 0xff;
92
93         /* See if flex ratio is already set to nominal TDP ratio */
94         if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio)
95                 return 0;
96
97         /* Set flex ratio to nominal TDP ratio */
98         flex_ratio.lo &= ~0xff00;
99         flex_ratio.lo |= nominal_ratio << 8;
100         flex_ratio.lo |= FLEX_RATIO_LOCK;
101         msr_write(MSR_FLEX_RATIO, flex_ratio);
102
103         /* Set flex ratio in soft reset data register bits 11:6 */
104         clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6,
105                         (nominal_ratio & 0x3f) << 6);
106
107         debug("CPU: Soft reset to set up flex ratio\n");
108
109         /* Set soft reset control to use register value */
110         setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1);
111
112         /* Issue warm reset, will be "CPU only" due to soft reset data */
113         outb(0x0, IO_PORT_RESET);
114         outb(SYS_RST | RST_CPU, IO_PORT_RESET);
115         cpu_hlt();
116
117         /* Not reached */
118         return -EINVAL;
119 }
120
121 int cpu_intel_get_info(struct cpu_info *info, int bclk)
122 {
123         msr_t msr;
124
125         msr = msr_read(MSR_IA32_PERF_CTL);
126         info->cpu_freq = ((msr.lo >> 8) & 0xff) * bclk * 1000000;
127         info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
128                 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
129
130         return 0;
131 }
132
133 int 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 }
154
155 void 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
165 bool 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 }
174
175 void 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
183 enum 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
207 void 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
219 void 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 }