blob: 1898903853fec0198a6c78671a72863771bcdd48 [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/*
3 * Copyright (c) 2016 Google, Inc
Simon Glass780ba482016-03-11 22:06:58 -07004 */
5
6#include <common.h>
Simon Glassaba3c602019-09-25 08:11:35 -06007#include <cpu.h>
Simon Glass780ba482016-03-11 22:06:58 -07008#include <dm.h>
9#include <errno.h>
10#include <asm/cpu_common.h>
11#include <asm/intel_regs.h>
12#include <asm/lapic.h>
13#include <asm/lpc_common.h>
14#include <asm/msr.h>
15#include <asm/mtrr.h>
16#include <asm/post.h>
17#include <asm/microcode.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21static int report_bist_failure(void)
22{
23 if (gd->arch.bist != 0) {
24 post_code(POST_BIST_FAILURE);
25 printf("BIST failed: %08x\n", gd->arch.bist);
26 return -EFAULT;
27 }
28
29 return 0;
30}
31
32int cpu_common_init(void)
33{
34 struct udevice *dev, *lpc;
35 int ret;
36
37 /* Halt if there was a built in self test failure */
38 ret = report_bist_failure();
39 if (ret)
40 return ret;
41
42 enable_lapic();
43
44 ret = microcode_update_intel();
Simon Glass7f99c7c2016-07-25 18:58:57 -060045 if (ret && ret != -EEXIST) {
46 debug("%s: Microcode update failure (err=%d)\n", __func__, ret);
Simon Glass780ba482016-03-11 22:06:58 -070047 return ret;
Simon Glass7f99c7c2016-07-25 18:58:57 -060048 }
Simon Glass780ba482016-03-11 22:06:58 -070049
50 /* Enable upper 128bytes of CMOS */
51 writel(1 << 2, RCB_REG(RC));
52
53 /* Early chipset init required before RAM init can work */
54 uclass_first_device(UCLASS_NORTHBRIDGE, &dev);
55
56 ret = uclass_first_device(UCLASS_LPC, &lpc);
57 if (ret)
58 return ret;
59 if (!lpc)
60 return -ENODEV;
61
62 /* Cause the SATA device to do its early init */
Simon Glass85ee1652016-05-01 11:35:52 -060063 uclass_first_device(UCLASS_AHCI, &dev);
Simon Glass780ba482016-03-11 22:06:58 -070064
65 return 0;
66}
67
68int cpu_set_flex_ratio_to_tdp_nominal(void)
69{
70 msr_t flex_ratio, msr;
71 u8 nominal_ratio;
72
73 /* Check for Flex Ratio support */
74 flex_ratio = msr_read(MSR_FLEX_RATIO);
75 if (!(flex_ratio.lo & FLEX_RATIO_EN))
76 return -EINVAL;
77
78 /* Check for >0 configurable TDPs */
79 msr = msr_read(MSR_PLATFORM_INFO);
80 if (((msr.hi >> 1) & 3) == 0)
81 return -EINVAL;
82
83 /* Use nominal TDP ratio for flex ratio */
84 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
85 nominal_ratio = msr.lo & 0xff;
86
87 /* See if flex ratio is already set to nominal TDP ratio */
88 if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio)
89 return 0;
90
91 /* Set flex ratio to nominal TDP ratio */
92 flex_ratio.lo &= ~0xff00;
93 flex_ratio.lo |= nominal_ratio << 8;
94 flex_ratio.lo |= FLEX_RATIO_LOCK;
95 msr_write(MSR_FLEX_RATIO, flex_ratio);
96
97 /* Set flex ratio in soft reset data register bits 11:6 */
98 clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6,
99 (nominal_ratio & 0x3f) << 6);
100
101 debug("CPU: Soft reset to set up flex ratio\n");
102
103 /* Set soft reset control to use register value */
104 setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1);
105
106 /* Issue warm reset, will be "CPU only" due to soft reset data */
Simon Glass8b73e9f2016-03-11 22:06:59 -0700107 outb(0x0, IO_PORT_RESET);
108 outb(SYS_RST | RST_CPU, IO_PORT_RESET);
Simon Glass780ba482016-03-11 22:06:58 -0700109 cpu_hlt();
110
111 /* Not reached */
112 return -EINVAL;
113}
Simon Glassaba3c602019-09-25 08:11:35 -0600114
115int cpu_intel_get_info(struct cpu_info *info, int bclk)
116{
117 msr_t msr;
118
Simon Glass76ae0272019-09-25 08:56:35 -0600119 msr = msr_read(MSR_IA32_PERF_CTL);
Simon Glassaba3c602019-09-25 08:11:35 -0600120 info->cpu_freq = ((msr.lo >> 8) & 0xff) * bclk * 1000000;
121 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU |
122 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID;
123
124 return 0;
125}
Simon Glass23a6ca92019-09-25 08:56:36 -0600126
127int cpu_configure_thermal_target(struct udevice *dev)
128{
129 u32 tcc_offset;
130 msr_t msr;
131 int ret;
132
133 ret = dev_read_u32(dev, "tcc-offset", &tcc_offset);
134 if (!ret)
135 return -ENOENT;
136
137 /* Set TCC activaiton offset if supported */
138 msr = msr_read(MSR_PLATFORM_INFO);
139 if (msr.lo & (1 << 30)) {
140 msr = msr_read(MSR_TEMPERATURE_TARGET);
141 msr.lo &= ~(0xf << 24); /* Bits 27:24 */
142 msr.lo |= (tcc_offset & 0xf) << 24;
143 msr_write(MSR_TEMPERATURE_TARGET, msr);
144 }
145
146 return 0;
147}
Simon Glassb12689d2019-09-25 08:56:38 -0600148
149void cpu_set_perf_control(uint clk_ratio)
150{
151 msr_t perf_ctl;
152
153 perf_ctl.lo = (clk_ratio & 0xff) << 8;
154 perf_ctl.hi = 0;
155 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
156 debug("CPU: frequency set to %d MHz\n", clk_ratio * INTEL_BCLK_MHZ);
157}
158
159bool cpu_config_tdp_levels(void)
160{
161 msr_t platform_info;
162
163 /* Bits 34:33 indicate how many levels supported */
164 platform_info = msr_read(MSR_PLATFORM_INFO);
165
166 return ((platform_info.hi >> 1) & 3) != 0;
167}