Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * |
| 5 | * Based on code from coreboot src/soc/intel/broadwell/cpu.c |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <cpu.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 12 | #include <log.h> |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 13 | #include <asm/cpu.h> |
| 14 | #include <asm/cpu_x86.h> |
| 15 | #include <asm/cpu_common.h> |
| 16 | #include <asm/intel_regs.h> |
| 17 | #include <asm/msr.h> |
| 18 | #include <asm/post.h> |
| 19 | #include <asm/turbo.h> |
| 20 | #include <asm/arch/cpu.h> |
| 21 | #include <asm/arch/pch.h> |
| 22 | #include <asm/arch/rcb.h> |
| 23 | |
| 24 | struct cpu_broadwell_priv { |
| 25 | bool ht_disabled; |
| 26 | }; |
| 27 | |
| 28 | /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */ |
| 29 | static const u8 power_limit_time_sec_to_msr[] = { |
| 30 | [0] = 0x00, |
| 31 | [1] = 0x0a, |
| 32 | [2] = 0x0b, |
| 33 | [3] = 0x4b, |
| 34 | [4] = 0x0c, |
| 35 | [5] = 0x2c, |
| 36 | [6] = 0x4c, |
| 37 | [7] = 0x6c, |
| 38 | [8] = 0x0d, |
| 39 | [10] = 0x2d, |
| 40 | [12] = 0x4d, |
| 41 | [14] = 0x6d, |
| 42 | [16] = 0x0e, |
| 43 | [20] = 0x2e, |
| 44 | [24] = 0x4e, |
| 45 | [28] = 0x6e, |
| 46 | [32] = 0x0f, |
| 47 | [40] = 0x2f, |
| 48 | [48] = 0x4f, |
| 49 | [56] = 0x6f, |
| 50 | [64] = 0x10, |
| 51 | [80] = 0x30, |
| 52 | [96] = 0x50, |
| 53 | [112] = 0x70, |
| 54 | [128] = 0x11, |
| 55 | }; |
| 56 | |
| 57 | /* Convert POWER_LIMIT_1_TIME MSR value to seconds */ |
| 58 | static const u8 power_limit_time_msr_to_sec[] = { |
| 59 | [0x00] = 0, |
| 60 | [0x0a] = 1, |
| 61 | [0x0b] = 2, |
| 62 | [0x4b] = 3, |
| 63 | [0x0c] = 4, |
| 64 | [0x2c] = 5, |
| 65 | [0x4c] = 6, |
| 66 | [0x6c] = 7, |
| 67 | [0x0d] = 8, |
| 68 | [0x2d] = 10, |
| 69 | [0x4d] = 12, |
| 70 | [0x6d] = 14, |
| 71 | [0x0e] = 16, |
| 72 | [0x2e] = 20, |
| 73 | [0x4e] = 24, |
| 74 | [0x6e] = 28, |
| 75 | [0x0f] = 32, |
| 76 | [0x2f] = 40, |
| 77 | [0x4f] = 48, |
| 78 | [0x6f] = 56, |
| 79 | [0x10] = 64, |
| 80 | [0x30] = 80, |
| 81 | [0x50] = 96, |
| 82 | [0x70] = 112, |
| 83 | [0x11] = 128, |
| 84 | }; |
| 85 | |
Simon Glass | 42bf3b9 | 2019-09-25 08:11:40 -0600 | [diff] [blame] | 86 | #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) |
| 87 | int arch_cpu_init(void) |
| 88 | { |
| 89 | return 0; |
| 90 | } |
| 91 | #endif |
| 92 | |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 93 | /* |
| 94 | * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate |
| 95 | * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly |
| 96 | * when a core is woken up |
| 97 | */ |
| 98 | static int pcode_ready(void) |
| 99 | { |
| 100 | int wait_count; |
| 101 | const int delay_step = 10; |
| 102 | |
| 103 | wait_count = 0; |
| 104 | do { |
| 105 | if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & |
| 106 | MAILBOX_RUN_BUSY)) |
| 107 | return 0; |
| 108 | wait_count += delay_step; |
| 109 | udelay(delay_step); |
| 110 | } while (wait_count < 1000); |
| 111 | |
| 112 | return -ETIMEDOUT; |
| 113 | } |
| 114 | |
| 115 | static u32 pcode_mailbox_read(u32 command) |
| 116 | { |
| 117 | int ret; |
| 118 | |
| 119 | ret = pcode_ready(); |
| 120 | if (ret) { |
| 121 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /* Send command and start transaction */ |
| 126 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 127 | |
| 128 | ret = pcode_ready(); |
| 129 | if (ret) { |
| 130 | debug("PCODE: mailbox timeout on completion\n"); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /* Read mailbox */ |
| 135 | return readl(MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 136 | } |
| 137 | |
| 138 | static int pcode_mailbox_write(u32 command, u32 data) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | ret = pcode_ready(); |
| 143 | if (ret) { |
| 144 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 149 | |
| 150 | /* Send command and start transaction */ |
| 151 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 152 | |
| 153 | ret = pcode_ready(); |
| 154 | if (ret) { |
| 155 | debug("PCODE: mailbox timeout on completion\n"); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* @dev is the CPU device */ |
| 163 | static void initialize_vr_config(struct udevice *dev) |
| 164 | { |
| 165 | int ramp, min_vid; |
| 166 | msr_t msr; |
| 167 | |
| 168 | debug("Initializing VR config\n"); |
| 169 | |
| 170 | /* Configure VR_CURRENT_CONFIG */ |
| 171 | msr = msr_read(MSR_VR_CURRENT_CONFIG); |
| 172 | /* |
| 173 | * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid |
| 174 | * on ULT systems |
| 175 | */ |
| 176 | msr.hi &= 0xc0000000; |
| 177 | msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */ |
| 178 | msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */ |
| 179 | msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */ |
| 180 | msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */ |
| 181 | /* Leave the max instantaneous current limit (12:0) to default */ |
| 182 | msr_write(MSR_VR_CURRENT_CONFIG, msr); |
| 183 | |
| 184 | /* Configure VR_MISC_CONFIG MSR */ |
| 185 | msr = msr_read(MSR_VR_MISC_CONFIG); |
| 186 | /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */ |
| 187 | msr.hi &= ~(0x3ff << (40 - 32)); |
| 188 | msr.hi |= (0x200 << (40 - 32)); /* 1.0 */ |
| 189 | /* Set IOUT_OFFSET to 0 */ |
| 190 | msr.hi &= ~0xff; |
| 191 | /* Set entry ramp rate to slow */ |
| 192 | msr.hi &= ~(1 << (51 - 32)); |
| 193 | /* Enable decay mode on C-state entry */ |
| 194 | msr.hi |= (1 << (52 - 32)); |
| 195 | /* Set the slow ramp rate */ |
| 196 | msr.hi &= ~(0x3 << (53 - 32)); |
| 197 | /* Configure the C-state exit ramp rate */ |
| 198 | ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 199 | "intel,slow-ramp", -1); |
| 200 | if (ramp != -1) { |
| 201 | /* Configured slow ramp rate */ |
| 202 | msr.hi |= ((ramp & 0x3) << (53 - 32)); |
| 203 | /* Set exit ramp rate to slow */ |
| 204 | msr.hi &= ~(1 << (50 - 32)); |
| 205 | } else { |
| 206 | /* Fast ramp rate / 4 */ |
| 207 | msr.hi |= (0x01 << (53 - 32)); |
| 208 | /* Set exit ramp rate to fast */ |
| 209 | msr.hi |= (1 << (50 - 32)); |
| 210 | } |
| 211 | /* Set MIN_VID (31:24) to allow CPU to have full control */ |
| 212 | msr.lo &= ~0xff000000; |
| 213 | min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 214 | "intel,min-vid", 0); |
| 215 | msr.lo |= (min_vid & 0xff) << 24; |
| 216 | msr_write(MSR_VR_MISC_CONFIG, msr); |
| 217 | |
| 218 | /* Configure VR_MISC_CONFIG2 MSR */ |
| 219 | msr = msr_read(MSR_VR_MISC_CONFIG2); |
| 220 | msr.lo &= ~0xffff; |
| 221 | /* |
| 222 | * Allow CPU to control minimum voltage completely (15:8) and |
| 223 | * set the fast ramp voltage in 10mV steps |
| 224 | */ |
| 225 | if (cpu_get_family_model() == BROADWELL_FAMILY_ULT) |
| 226 | msr.lo |= 0x006a; /* 1.56V */ |
| 227 | else |
| 228 | msr.lo |= 0x006f; /* 1.60V */ |
| 229 | msr_write(MSR_VR_MISC_CONFIG2, msr); |
| 230 | |
| 231 | /* Set C9/C10 VCC Min */ |
| 232 | pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f); |
| 233 | } |
| 234 | |
| 235 | static int calibrate_24mhz_bclk(void) |
| 236 | { |
| 237 | int err_code; |
| 238 | int ret; |
| 239 | |
| 240 | ret = pcode_ready(); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
| 244 | /* A non-zero value initiates the PCODE calibration */ |
| 245 | writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 246 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL, |
| 247 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 248 | |
| 249 | ret = pcode_ready(); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff; |
| 254 | |
| 255 | debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code); |
| 256 | |
| 257 | /* Read the calibrated value */ |
| 258 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION, |
| 259 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 260 | |
| 261 | ret = pcode_ready(); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n", |
| 266 | readl(MCHBAR_REG(BIOS_MAILBOX_DATA))); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static void configure_pch_power_sharing(void) |
| 272 | { |
| 273 | u32 pch_power, pch_power_ext, pmsync, pmsync2; |
| 274 | int i; |
| 275 | |
| 276 | /* Read PCH Power levels from PCODE */ |
| 277 | pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER); |
| 278 | pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT); |
| 279 | |
| 280 | debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power, |
| 281 | pch_power_ext); |
| 282 | |
| 283 | pmsync = readl(RCB_REG(PMSYNC_CONFIG)); |
| 284 | pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2)); |
| 285 | |
| 286 | /* |
| 287 | * Program PMSYNC_TPR_CONFIG PCH power limit values |
| 288 | * pmsync[0:4] = mailbox[0:5] |
| 289 | * pmsync[8:12] = mailbox[6:11] |
| 290 | * pmsync[16:20] = mailbox[12:17] |
| 291 | */ |
| 292 | for (i = 0; i < 3; i++) { |
| 293 | u32 level = pch_power & 0x3f; |
| 294 | |
| 295 | pch_power >>= 6; |
| 296 | pmsync &= ~(0x1f << (i * 8)); |
| 297 | pmsync |= (level & 0x1f) << (i * 8); |
| 298 | } |
| 299 | writel(pmsync, RCB_REG(PMSYNC_CONFIG)); |
| 300 | |
| 301 | /* |
| 302 | * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values |
| 303 | * pmsync2[0:4] = mailbox[23:18] |
| 304 | * pmsync2[8:12] = mailbox_ext[6:11] |
| 305 | * pmsync2[16:20] = mailbox_ext[12:17] |
| 306 | * pmsync2[24:28] = mailbox_ext[18:22] |
| 307 | */ |
| 308 | pmsync2 &= ~0x1f; |
| 309 | pmsync2 |= pch_power & 0x1f; |
| 310 | |
| 311 | for (i = 1; i < 4; i++) { |
| 312 | u32 level = pch_power_ext & 0x3f; |
| 313 | |
| 314 | pch_power_ext >>= 6; |
| 315 | pmsync2 &= ~(0x1f << (i * 8)); |
| 316 | pmsync2 |= (level & 0x1f) << (i * 8); |
| 317 | } |
| 318 | writel(pmsync2, RCB_REG(PMSYNC_CONFIG2)); |
| 319 | } |
| 320 | |
| 321 | static int bsp_init_before_ap_bringup(struct udevice *dev) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | initialize_vr_config(dev); |
| 326 | ret = calibrate_24mhz_bclk(); |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | configure_pch_power_sharing(); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 334 | static void set_max_ratio(void) |
| 335 | { |
| 336 | msr_t msr, perf_ctl; |
| 337 | |
| 338 | perf_ctl.hi = 0; |
| 339 | |
| 340 | /* Check for configurable TDP option */ |
| 341 | if (turbo_get_state() == TURBO_ENABLED) { |
Simon Glass | 7ab72de | 2019-09-25 08:11:47 -0600 | [diff] [blame] | 342 | msr = msr_read(MSR_TURBO_RATIO_LIMIT); |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 343 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 344 | } else if (cpu_config_tdp_levels()) { |
| 345 | /* Set to nominal TDP ratio */ |
| 346 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 347 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 348 | } else { |
| 349 | /* Platform Info bits 15:8 give max ratio */ |
| 350 | msr = msr_read(MSR_PLATFORM_INFO); |
| 351 | perf_ctl.lo = msr.lo & 0xff00; |
| 352 | } |
Simon Glass | 76ae027 | 2019-09-25 08:56:35 -0600 | [diff] [blame] | 353 | msr_write(MSR_IA32_PERF_CTL, perf_ctl); |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 354 | |
| 355 | debug("cpu: frequency set to %d\n", |
Simon Glass | 4347d83 | 2019-09-25 08:56:37 -0600 | [diff] [blame] | 356 | ((perf_ctl.lo >> 8) & 0xff) * INTEL_BCLK_MHZ); |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | int broadwell_init(struct udevice *dev) |
| 360 | { |
| 361 | struct cpu_broadwell_priv *priv = dev_get_priv(dev); |
| 362 | int num_threads; |
| 363 | int num_cores; |
| 364 | msr_t msr; |
| 365 | int ret; |
| 366 | |
| 367 | msr = msr_read(CORE_THREAD_COUNT_MSR); |
| 368 | num_threads = (msr.lo >> 0) & 0xffff; |
| 369 | num_cores = (msr.lo >> 16) & 0xffff; |
| 370 | debug("CPU has %u cores, %u threads enabled\n", num_cores, |
| 371 | num_threads); |
| 372 | |
| 373 | priv->ht_disabled = num_threads == num_cores; |
| 374 | |
| 375 | ret = bsp_init_before_ap_bringup(dev); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | |
| 379 | set_max_ratio(); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static void configure_mca(void) |
| 385 | { |
| 386 | msr_t msr; |
| 387 | const unsigned int mcg_cap_msr = 0x179; |
| 388 | int i; |
| 389 | int num_banks; |
| 390 | |
| 391 | msr = msr_read(mcg_cap_msr); |
| 392 | num_banks = msr.lo & 0xff; |
| 393 | msr.lo = 0; |
| 394 | msr.hi = 0; |
| 395 | /* |
| 396 | * TODO(adurbin): This should only be done on a cold boot. Also, some |
| 397 | * of these banks are core vs package scope. For now every CPU clears |
| 398 | * every bank |
| 399 | */ |
| 400 | for (i = 0; i < num_banks; i++) |
| 401 | msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr); |
| 402 | } |
| 403 | |
| 404 | static void enable_lapic_tpr(void) |
| 405 | { |
| 406 | msr_t msr; |
| 407 | |
| 408 | msr = msr_read(MSR_PIC_MSG_CONTROL); |
| 409 | msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */ |
| 410 | msr_write(MSR_PIC_MSG_CONTROL, msr); |
| 411 | } |
| 412 | |
| 413 | static void configure_c_states(void) |
| 414 | { |
| 415 | msr_t msr; |
| 416 | |
| 417 | msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL); |
| 418 | msr.lo |= (1 << 31); /* Timed MWAIT Enable */ |
| 419 | msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */ |
| 420 | msr.lo |= (1 << 29); /* Package c-state Demotion Enable */ |
| 421 | msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */ |
| 422 | msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */ |
| 423 | msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */ |
| 424 | msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */ |
| 425 | msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */ |
| 426 | /* The deepest package c-state defaults to factory-configured value */ |
| 427 | msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr); |
| 428 | |
| 429 | msr = msr_read(MSR_MISC_PWR_MGMT); |
| 430 | msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */ |
| 431 | msr_write(MSR_MISC_PWR_MGMT, msr); |
| 432 | |
| 433 | msr = msr_read(MSR_POWER_CTL); |
| 434 | msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */ |
| 435 | msr.lo |= (1 << 1); /* C1E Enable */ |
| 436 | msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */ |
| 437 | msr_write(MSR_POWER_CTL, msr); |
| 438 | |
| 439 | /* C-state Interrupt Response Latency Control 0 - package C3 latency */ |
| 440 | msr.hi = 0; |
| 441 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT; |
| 442 | msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr); |
| 443 | |
| 444 | /* C-state Interrupt Response Latency Control 1 */ |
| 445 | msr.hi = 0; |
| 446 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT; |
| 447 | msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr); |
| 448 | |
| 449 | /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */ |
| 450 | msr.hi = 0; |
| 451 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT; |
| 452 | msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr); |
| 453 | |
| 454 | /* C-state Interrupt Response Latency Control 3 - package C8 */ |
| 455 | msr.hi = 0; |
| 456 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT; |
| 457 | msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr); |
| 458 | |
| 459 | /* C-state Interrupt Response Latency Control 4 - package C9 */ |
| 460 | msr.hi = 0; |
| 461 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT; |
| 462 | msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr); |
| 463 | |
| 464 | /* C-state Interrupt Response Latency Control 5 - package C10 */ |
| 465 | msr.hi = 0; |
| 466 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT; |
| 467 | msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr); |
| 468 | } |
| 469 | |
| 470 | static void configure_misc(void) |
| 471 | { |
| 472 | msr_t msr; |
| 473 | |
| 474 | msr = msr_read(MSR_IA32_MISC_ENABLE); |
Simon Glass | 05e85b9 | 2019-09-25 08:56:39 -0600 | [diff] [blame] | 475 | msr.lo |= MISC_ENABLE_FAST_STRING; |
| 476 | msr.lo |= MISC_ENABLE_TM1; |
| 477 | msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP; |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 478 | msr_write(MSR_IA32_MISC_ENABLE, msr); |
| 479 | |
| 480 | /* Disable thermal interrupts */ |
| 481 | msr.lo = 0; |
| 482 | msr.hi = 0; |
| 483 | msr_write(MSR_IA32_THERM_INTERRUPT, msr); |
| 484 | |
| 485 | /* Enable package critical interrupt only */ |
| 486 | msr.lo = 1 << 4; |
| 487 | msr.hi = 0; |
| 488 | msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr); |
| 489 | } |
| 490 | |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 491 | static void configure_dca_cap(void) |
| 492 | { |
| 493 | struct cpuid_result cpuid_regs; |
| 494 | msr_t msr; |
| 495 | |
| 496 | /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */ |
| 497 | cpuid_regs = cpuid(1); |
| 498 | if (cpuid_regs.ecx & (1 << 18)) { |
| 499 | msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP); |
| 500 | msr.lo |= 1; |
| 501 | msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | static void set_energy_perf_bias(u8 policy) |
| 506 | { |
| 507 | msr_t msr; |
| 508 | int ecx; |
| 509 | |
| 510 | /* Determine if energy efficient policy is supported */ |
| 511 | ecx = cpuid_ecx(0x6); |
| 512 | if (!(ecx & (1 << 3))) |
| 513 | return; |
| 514 | |
| 515 | /* Energy Policy is bits 3:0 */ |
| 516 | msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS); |
| 517 | msr.lo &= ~0xf; |
| 518 | msr.lo |= policy & 0xf; |
| 519 | msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr); |
| 520 | |
| 521 | debug("cpu: energy policy set to %u\n", policy); |
| 522 | } |
| 523 | |
| 524 | /* All CPUs including BSP will run the following function */ |
| 525 | static void cpu_core_init(struct udevice *dev) |
| 526 | { |
| 527 | /* Clear out pending MCEs */ |
| 528 | configure_mca(); |
| 529 | |
| 530 | /* Enable the local cpu apics */ |
| 531 | enable_lapic_tpr(); |
| 532 | |
| 533 | /* Configure C States */ |
| 534 | configure_c_states(); |
| 535 | |
| 536 | /* Configure Enhanced SpeedStep and Thermal Sensors */ |
| 537 | configure_misc(); |
| 538 | |
| 539 | /* Thermal throttle activation offset */ |
Simon Glass | 23a6ca9 | 2019-09-25 08:56:36 -0600 | [diff] [blame] | 540 | cpu_configure_thermal_target(dev); |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 541 | |
| 542 | /* Enable Direct Cache Access */ |
| 543 | configure_dca_cap(); |
| 544 | |
| 545 | /* Set energy policy */ |
| 546 | set_energy_perf_bias(ENERGY_POLICY_NORMAL); |
| 547 | |
| 548 | /* Enable Turbo */ |
| 549 | turbo_enable(); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Configure processor power limits if possible |
| 554 | * This must be done AFTER set of BIOS_RESET_CPL |
| 555 | */ |
| 556 | void cpu_set_power_limits(int power_limit_1_time) |
| 557 | { |
| 558 | msr_t msr; |
| 559 | msr_t limit; |
| 560 | uint power_unit; |
| 561 | uint tdp, min_power, max_power, max_time; |
| 562 | u8 power_limit_1_val; |
| 563 | |
| 564 | msr = msr_read(MSR_PLATFORM_INFO); |
| 565 | if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr)) |
| 566 | power_limit_1_time = 28; |
| 567 | |
| 568 | if (!(msr.lo & PLATFORM_INFO_SET_TDP)) |
| 569 | return; |
| 570 | |
| 571 | /* Get units */ |
| 572 | msr = msr_read(MSR_PKG_POWER_SKU_UNIT); |
| 573 | power_unit = 2 << ((msr.lo & 0xf) - 1); |
| 574 | |
| 575 | /* Get power defaults for this SKU */ |
| 576 | msr = msr_read(MSR_PKG_POWER_SKU); |
| 577 | tdp = msr.lo & 0x7fff; |
| 578 | min_power = (msr.lo >> 16) & 0x7fff; |
| 579 | max_power = msr.hi & 0x7fff; |
| 580 | max_time = (msr.hi >> 16) & 0x7f; |
| 581 | |
| 582 | debug("CPU TDP: %u Watts\n", tdp / power_unit); |
| 583 | |
| 584 | if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time) |
| 585 | power_limit_1_time = power_limit_time_msr_to_sec[max_time]; |
| 586 | |
| 587 | if (min_power > 0 && tdp < min_power) |
| 588 | tdp = min_power; |
| 589 | |
| 590 | if (max_power > 0 && tdp > max_power) |
| 591 | tdp = max_power; |
| 592 | |
| 593 | power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time]; |
| 594 | |
| 595 | /* Set long term power limit to TDP */ |
| 596 | limit.lo = 0; |
| 597 | limit.lo |= tdp & PKG_POWER_LIMIT_MASK; |
| 598 | limit.lo |= PKG_POWER_LIMIT_EN; |
| 599 | limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) << |
| 600 | PKG_POWER_LIMIT_TIME_SHIFT; |
| 601 | |
| 602 | /* Set short term power limit to 1.25 * TDP */ |
| 603 | limit.hi = 0; |
| 604 | limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK; |
| 605 | limit.hi |= PKG_POWER_LIMIT_EN; |
| 606 | /* Power limit 2 time is only programmable on server SKU */ |
| 607 | |
| 608 | msr_write(MSR_PKG_POWER_LIMIT, limit); |
| 609 | |
| 610 | /* Set power limit values in MCHBAR as well */ |
| 611 | writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO)); |
| 612 | writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI)); |
| 613 | |
| 614 | /* Set DDR RAPL power limit by copying from MMIO to MSR */ |
| 615 | msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO)); |
| 616 | msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI)); |
| 617 | msr_write(MSR_DDR_RAPL_LIMIT, msr); |
| 618 | |
| 619 | /* Use nominal TDP values for CPUs with configurable TDP */ |
| 620 | if (cpu_config_tdp_levels()) { |
| 621 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 622 | limit.hi = 0; |
| 623 | limit.lo = msr.lo & 0xff; |
| 624 | msr_write(MSR_TURBO_ACTIVATION_RATIO, limit); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static int broadwell_get_info(struct udevice *dev, struct cpu_info *info) |
| 629 | { |
Simon Glass | 4347d83 | 2019-09-25 08:56:37 -0600 | [diff] [blame] | 630 | return cpu_intel_get_info(info, INTEL_BCLK_MHZ); |
Simon Glass | 409cb19 | 2019-04-25 21:58:51 -0600 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static int broadwell_get_count(struct udevice *dev) |
| 634 | { |
| 635 | return 4; |
| 636 | } |
| 637 | |
| 638 | static int cpu_x86_broadwell_probe(struct udevice *dev) |
| 639 | { |
| 640 | if (dev->seq == 0) { |
| 641 | cpu_core_init(dev); |
| 642 | return broadwell_init(dev); |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static const struct cpu_ops cpu_x86_broadwell_ops = { |
| 649 | .get_desc = cpu_x86_get_desc, |
| 650 | .get_info = broadwell_get_info, |
| 651 | .get_count = broadwell_get_count, |
| 652 | .get_vendor = cpu_x86_get_vendor, |
| 653 | }; |
| 654 | |
| 655 | static const struct udevice_id cpu_x86_broadwell_ids[] = { |
| 656 | { .compatible = "intel,core-i3-gen5" }, |
| 657 | { } |
| 658 | }; |
| 659 | |
| 660 | U_BOOT_DRIVER(cpu_x86_broadwell_drv) = { |
| 661 | .name = "cpu_x86_broadwell", |
| 662 | .id = UCLASS_CPU, |
| 663 | .of_match = cpu_x86_broadwell_ids, |
| 664 | .bind = cpu_x86_bind, |
| 665 | .probe = cpu_x86_broadwell_probe, |
| 666 | .ops = &cpu_x86_broadwell_ops, |
| 667 | .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv), |
| 668 | .flags = DM_FLAG_PRE_RELOC, |
| 669 | }; |