Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 2 | /* |
Simon Glass | a016368 | 2019-09-25 08:56:40 -0600 | [diff] [blame] | 3 | * Copyright (C) 2014 Google Inc. |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 4 | * Copyright (c) 2016 Google, Inc |
Simon Glass | a016368 | 2019-09-25 08:56:40 -0600 | [diff] [blame] | 5 | * Copyright (C) 2015-2018 Intel Corporation. |
| 6 | * Copyright (C) 2018 Siemens AG |
| 7 | * Some code taken from coreboot cpulib.c |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | aba3c60 | 2019-09-25 08:11:35 -0600 | [diff] [blame] | 11 | #include <cpu.h> |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 14 | #include <log.h> |
Simon Glass | a016368 | 2019-09-25 08:56:40 -0600 | [diff] [blame] | 15 | #include <asm/cpu.h> |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 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(); |
Simon Glass | 7f99c7c | 2016-07-25 18:58:57 -0600 | [diff] [blame] | 51 | if (ret && ret != -EEXIST) { |
| 52 | debug("%s: Microcode update failure (err=%d)\n", __func__, ret); |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 53 | return ret; |
Simon Glass | 7f99c7c | 2016-07-25 18:58:57 -0600 | [diff] [blame] | 54 | } |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 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 */ |
Simon Glass | 85ee165 | 2016-05-01 11:35:52 -0600 | [diff] [blame] | 69 | uclass_first_device(UCLASS_AHCI, &dev); |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 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 */ |
Simon Glass | 8b73e9f | 2016-03-11 22:06:59 -0700 | [diff] [blame] | 113 | outb(0x0, IO_PORT_RESET); |
| 114 | outb(SYS_RST | RST_CPU, IO_PORT_RESET); |
Simon Glass | 780ba48 | 2016-03-11 22:06:58 -0700 | [diff] [blame] | 115 | cpu_hlt(); |
| 116 | |
| 117 | /* Not reached */ |
| 118 | return -EINVAL; |
| 119 | } |
Simon Glass | aba3c60 | 2019-09-25 08:11:35 -0600 | [diff] [blame] | 120 | |
| 121 | int cpu_intel_get_info(struct cpu_info *info, int bclk) |
| 122 | { |
| 123 | msr_t msr; |
| 124 | |
Simon Glass | 76ae027 | 2019-09-25 08:56:35 -0600 | [diff] [blame] | 125 | msr = msr_read(MSR_IA32_PERF_CTL); |
Simon Glass | aba3c60 | 2019-09-25 08:11:35 -0600 | [diff] [blame] | 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 | } |
Simon Glass | 23a6ca9 | 2019-09-25 08:56:36 -0600 | [diff] [blame] | 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 | } |
Simon Glass | b12689d | 2019-09-25 08:56:38 -0600 | [diff] [blame] | 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 | } |
Simon Glass | a016368 | 2019-09-25 08:56:40 -0600 | [diff] [blame] | 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 | } |