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> |
| 11 | #include <asm/cpu.h> |
| 12 | #include <asm/cpu_x86.h> |
| 13 | #include <asm/cpu_common.h> |
| 14 | #include <asm/intel_regs.h> |
| 15 | #include <asm/msr.h> |
| 16 | #include <asm/post.h> |
| 17 | #include <asm/turbo.h> |
| 18 | #include <asm/arch/cpu.h> |
| 19 | #include <asm/arch/pch.h> |
| 20 | #include <asm/arch/rcb.h> |
| 21 | |
| 22 | struct cpu_broadwell_priv { |
| 23 | bool ht_disabled; |
| 24 | }; |
| 25 | |
| 26 | /* Convert time in seconds to POWER_LIMIT_1_TIME MSR value */ |
| 27 | static const u8 power_limit_time_sec_to_msr[] = { |
| 28 | [0] = 0x00, |
| 29 | [1] = 0x0a, |
| 30 | [2] = 0x0b, |
| 31 | [3] = 0x4b, |
| 32 | [4] = 0x0c, |
| 33 | [5] = 0x2c, |
| 34 | [6] = 0x4c, |
| 35 | [7] = 0x6c, |
| 36 | [8] = 0x0d, |
| 37 | [10] = 0x2d, |
| 38 | [12] = 0x4d, |
| 39 | [14] = 0x6d, |
| 40 | [16] = 0x0e, |
| 41 | [20] = 0x2e, |
| 42 | [24] = 0x4e, |
| 43 | [28] = 0x6e, |
| 44 | [32] = 0x0f, |
| 45 | [40] = 0x2f, |
| 46 | [48] = 0x4f, |
| 47 | [56] = 0x6f, |
| 48 | [64] = 0x10, |
| 49 | [80] = 0x30, |
| 50 | [96] = 0x50, |
| 51 | [112] = 0x70, |
| 52 | [128] = 0x11, |
| 53 | }; |
| 54 | |
| 55 | /* Convert POWER_LIMIT_1_TIME MSR value to seconds */ |
| 56 | static const u8 power_limit_time_msr_to_sec[] = { |
| 57 | [0x00] = 0, |
| 58 | [0x0a] = 1, |
| 59 | [0x0b] = 2, |
| 60 | [0x4b] = 3, |
| 61 | [0x0c] = 4, |
| 62 | [0x2c] = 5, |
| 63 | [0x4c] = 6, |
| 64 | [0x6c] = 7, |
| 65 | [0x0d] = 8, |
| 66 | [0x2d] = 10, |
| 67 | [0x4d] = 12, |
| 68 | [0x6d] = 14, |
| 69 | [0x0e] = 16, |
| 70 | [0x2e] = 20, |
| 71 | [0x4e] = 24, |
| 72 | [0x6e] = 28, |
| 73 | [0x0f] = 32, |
| 74 | [0x2f] = 40, |
| 75 | [0x4f] = 48, |
| 76 | [0x6f] = 56, |
| 77 | [0x10] = 64, |
| 78 | [0x30] = 80, |
| 79 | [0x50] = 96, |
| 80 | [0x70] = 112, |
| 81 | [0x11] = 128, |
| 82 | }; |
| 83 | |
| 84 | /* |
| 85 | * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate |
| 86 | * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly |
| 87 | * when a core is woken up |
| 88 | */ |
| 89 | static int pcode_ready(void) |
| 90 | { |
| 91 | int wait_count; |
| 92 | const int delay_step = 10; |
| 93 | |
| 94 | wait_count = 0; |
| 95 | do { |
| 96 | if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & |
| 97 | MAILBOX_RUN_BUSY)) |
| 98 | return 0; |
| 99 | wait_count += delay_step; |
| 100 | udelay(delay_step); |
| 101 | } while (wait_count < 1000); |
| 102 | |
| 103 | return -ETIMEDOUT; |
| 104 | } |
| 105 | |
| 106 | static u32 pcode_mailbox_read(u32 command) |
| 107 | { |
| 108 | int ret; |
| 109 | |
| 110 | ret = pcode_ready(); |
| 111 | if (ret) { |
| 112 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* Send command and start transaction */ |
| 117 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 118 | |
| 119 | ret = pcode_ready(); |
| 120 | if (ret) { |
| 121 | debug("PCODE: mailbox timeout on completion\n"); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /* Read mailbox */ |
| 126 | return readl(MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 127 | } |
| 128 | |
| 129 | static int pcode_mailbox_write(u32 command, u32 data) |
| 130 | { |
| 131 | int ret; |
| 132 | |
| 133 | ret = pcode_ready(); |
| 134 | if (ret) { |
| 135 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 140 | |
| 141 | /* Send command and start transaction */ |
| 142 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 143 | |
| 144 | ret = pcode_ready(); |
| 145 | if (ret) { |
| 146 | debug("PCODE: mailbox timeout on completion\n"); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* @dev is the CPU device */ |
| 154 | static void initialize_vr_config(struct udevice *dev) |
| 155 | { |
| 156 | int ramp, min_vid; |
| 157 | msr_t msr; |
| 158 | |
| 159 | debug("Initializing VR config\n"); |
| 160 | |
| 161 | /* Configure VR_CURRENT_CONFIG */ |
| 162 | msr = msr_read(MSR_VR_CURRENT_CONFIG); |
| 163 | /* |
| 164 | * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid |
| 165 | * on ULT systems |
| 166 | */ |
| 167 | msr.hi &= 0xc0000000; |
| 168 | msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */ |
| 169 | msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */ |
| 170 | msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */ |
| 171 | msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */ |
| 172 | /* Leave the max instantaneous current limit (12:0) to default */ |
| 173 | msr_write(MSR_VR_CURRENT_CONFIG, msr); |
| 174 | |
| 175 | /* Configure VR_MISC_CONFIG MSR */ |
| 176 | msr = msr_read(MSR_VR_MISC_CONFIG); |
| 177 | /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */ |
| 178 | msr.hi &= ~(0x3ff << (40 - 32)); |
| 179 | msr.hi |= (0x200 << (40 - 32)); /* 1.0 */ |
| 180 | /* Set IOUT_OFFSET to 0 */ |
| 181 | msr.hi &= ~0xff; |
| 182 | /* Set entry ramp rate to slow */ |
| 183 | msr.hi &= ~(1 << (51 - 32)); |
| 184 | /* Enable decay mode on C-state entry */ |
| 185 | msr.hi |= (1 << (52 - 32)); |
| 186 | /* Set the slow ramp rate */ |
| 187 | msr.hi &= ~(0x3 << (53 - 32)); |
| 188 | /* Configure the C-state exit ramp rate */ |
| 189 | ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 190 | "intel,slow-ramp", -1); |
| 191 | if (ramp != -1) { |
| 192 | /* Configured slow ramp rate */ |
| 193 | msr.hi |= ((ramp & 0x3) << (53 - 32)); |
| 194 | /* Set exit ramp rate to slow */ |
| 195 | msr.hi &= ~(1 << (50 - 32)); |
| 196 | } else { |
| 197 | /* Fast ramp rate / 4 */ |
| 198 | msr.hi |= (0x01 << (53 - 32)); |
| 199 | /* Set exit ramp rate to fast */ |
| 200 | msr.hi |= (1 << (50 - 32)); |
| 201 | } |
| 202 | /* Set MIN_VID (31:24) to allow CPU to have full control */ |
| 203 | msr.lo &= ~0xff000000; |
| 204 | min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 205 | "intel,min-vid", 0); |
| 206 | msr.lo |= (min_vid & 0xff) << 24; |
| 207 | msr_write(MSR_VR_MISC_CONFIG, msr); |
| 208 | |
| 209 | /* Configure VR_MISC_CONFIG2 MSR */ |
| 210 | msr = msr_read(MSR_VR_MISC_CONFIG2); |
| 211 | msr.lo &= ~0xffff; |
| 212 | /* |
| 213 | * Allow CPU to control minimum voltage completely (15:8) and |
| 214 | * set the fast ramp voltage in 10mV steps |
| 215 | */ |
| 216 | if (cpu_get_family_model() == BROADWELL_FAMILY_ULT) |
| 217 | msr.lo |= 0x006a; /* 1.56V */ |
| 218 | else |
| 219 | msr.lo |= 0x006f; /* 1.60V */ |
| 220 | msr_write(MSR_VR_MISC_CONFIG2, msr); |
| 221 | |
| 222 | /* Set C9/C10 VCC Min */ |
| 223 | pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f); |
| 224 | } |
| 225 | |
| 226 | static int calibrate_24mhz_bclk(void) |
| 227 | { |
| 228 | int err_code; |
| 229 | int ret; |
| 230 | |
| 231 | ret = pcode_ready(); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | /* A non-zero value initiates the PCODE calibration */ |
| 236 | writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 237 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL, |
| 238 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 239 | |
| 240 | ret = pcode_ready(); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
| 244 | err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff; |
| 245 | |
| 246 | debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code); |
| 247 | |
| 248 | /* Read the calibrated value */ |
| 249 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION, |
| 250 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 251 | |
| 252 | ret = pcode_ready(); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n", |
| 257 | readl(MCHBAR_REG(BIOS_MAILBOX_DATA))); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static void configure_pch_power_sharing(void) |
| 263 | { |
| 264 | u32 pch_power, pch_power_ext, pmsync, pmsync2; |
| 265 | int i; |
| 266 | |
| 267 | /* Read PCH Power levels from PCODE */ |
| 268 | pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER); |
| 269 | pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT); |
| 270 | |
| 271 | debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power, |
| 272 | pch_power_ext); |
| 273 | |
| 274 | pmsync = readl(RCB_REG(PMSYNC_CONFIG)); |
| 275 | pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2)); |
| 276 | |
| 277 | /* |
| 278 | * Program PMSYNC_TPR_CONFIG PCH power limit values |
| 279 | * pmsync[0:4] = mailbox[0:5] |
| 280 | * pmsync[8:12] = mailbox[6:11] |
| 281 | * pmsync[16:20] = mailbox[12:17] |
| 282 | */ |
| 283 | for (i = 0; i < 3; i++) { |
| 284 | u32 level = pch_power & 0x3f; |
| 285 | |
| 286 | pch_power >>= 6; |
| 287 | pmsync &= ~(0x1f << (i * 8)); |
| 288 | pmsync |= (level & 0x1f) << (i * 8); |
| 289 | } |
| 290 | writel(pmsync, RCB_REG(PMSYNC_CONFIG)); |
| 291 | |
| 292 | /* |
| 293 | * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values |
| 294 | * pmsync2[0:4] = mailbox[23:18] |
| 295 | * pmsync2[8:12] = mailbox_ext[6:11] |
| 296 | * pmsync2[16:20] = mailbox_ext[12:17] |
| 297 | * pmsync2[24:28] = mailbox_ext[18:22] |
| 298 | */ |
| 299 | pmsync2 &= ~0x1f; |
| 300 | pmsync2 |= pch_power & 0x1f; |
| 301 | |
| 302 | for (i = 1; i < 4; i++) { |
| 303 | u32 level = pch_power_ext & 0x3f; |
| 304 | |
| 305 | pch_power_ext >>= 6; |
| 306 | pmsync2 &= ~(0x1f << (i * 8)); |
| 307 | pmsync2 |= (level & 0x1f) << (i * 8); |
| 308 | } |
| 309 | writel(pmsync2, RCB_REG(PMSYNC_CONFIG2)); |
| 310 | } |
| 311 | |
| 312 | static int bsp_init_before_ap_bringup(struct udevice *dev) |
| 313 | { |
| 314 | int ret; |
| 315 | |
| 316 | initialize_vr_config(dev); |
| 317 | ret = calibrate_24mhz_bclk(); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | configure_pch_power_sharing(); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int cpu_config_tdp_levels(void) |
| 326 | { |
| 327 | msr_t platform_info; |
| 328 | |
| 329 | /* Bits 34:33 indicate how many levels supported */ |
| 330 | platform_info = msr_read(MSR_PLATFORM_INFO); |
| 331 | return (platform_info.hi >> 1) & 3; |
| 332 | } |
| 333 | |
| 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) { |
| 342 | msr = msr_read(MSR_NHM_TURBO_RATIO_LIMIT); |
| 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 | } |
| 353 | msr_write(IA32_PERF_CTL, perf_ctl); |
| 354 | |
| 355 | debug("cpu: frequency set to %d\n", |
| 356 | ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK); |
| 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); |
| 475 | msr.lo |= (1 << 0); /* Fast String enable */ |
| 476 | msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */ |
| 477 | msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */ |
| 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 | |
| 491 | static void configure_thermal_target(struct udevice *dev) |
| 492 | { |
| 493 | int tcc_offset; |
| 494 | msr_t msr; |
| 495 | |
| 496 | tcc_offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 497 | "intel,tcc-offset", 0); |
| 498 | |
| 499 | /* Set TCC activaiton offset if supported */ |
| 500 | msr = msr_read(MSR_PLATFORM_INFO); |
| 501 | if ((msr.lo & (1 << 30)) && tcc_offset) { |
| 502 | msr = msr_read(MSR_TEMPERATURE_TARGET); |
| 503 | msr.lo &= ~(0xf << 24); /* Bits 27:24 */ |
| 504 | msr.lo |= (tcc_offset & 0xf) << 24; |
| 505 | msr_write(MSR_TEMPERATURE_TARGET, msr); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static void configure_dca_cap(void) |
| 510 | { |
| 511 | struct cpuid_result cpuid_regs; |
| 512 | msr_t msr; |
| 513 | |
| 514 | /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */ |
| 515 | cpuid_regs = cpuid(1); |
| 516 | if (cpuid_regs.ecx & (1 << 18)) { |
| 517 | msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP); |
| 518 | msr.lo |= 1; |
| 519 | msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static void set_energy_perf_bias(u8 policy) |
| 524 | { |
| 525 | msr_t msr; |
| 526 | int ecx; |
| 527 | |
| 528 | /* Determine if energy efficient policy is supported */ |
| 529 | ecx = cpuid_ecx(0x6); |
| 530 | if (!(ecx & (1 << 3))) |
| 531 | return; |
| 532 | |
| 533 | /* Energy Policy is bits 3:0 */ |
| 534 | msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS); |
| 535 | msr.lo &= ~0xf; |
| 536 | msr.lo |= policy & 0xf; |
| 537 | msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr); |
| 538 | |
| 539 | debug("cpu: energy policy set to %u\n", policy); |
| 540 | } |
| 541 | |
| 542 | /* All CPUs including BSP will run the following function */ |
| 543 | static void cpu_core_init(struct udevice *dev) |
| 544 | { |
| 545 | /* Clear out pending MCEs */ |
| 546 | configure_mca(); |
| 547 | |
| 548 | /* Enable the local cpu apics */ |
| 549 | enable_lapic_tpr(); |
| 550 | |
| 551 | /* Configure C States */ |
| 552 | configure_c_states(); |
| 553 | |
| 554 | /* Configure Enhanced SpeedStep and Thermal Sensors */ |
| 555 | configure_misc(); |
| 556 | |
| 557 | /* Thermal throttle activation offset */ |
| 558 | configure_thermal_target(dev); |
| 559 | |
| 560 | /* Enable Direct Cache Access */ |
| 561 | configure_dca_cap(); |
| 562 | |
| 563 | /* Set energy policy */ |
| 564 | set_energy_perf_bias(ENERGY_POLICY_NORMAL); |
| 565 | |
| 566 | /* Enable Turbo */ |
| 567 | turbo_enable(); |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Configure processor power limits if possible |
| 572 | * This must be done AFTER set of BIOS_RESET_CPL |
| 573 | */ |
| 574 | void cpu_set_power_limits(int power_limit_1_time) |
| 575 | { |
| 576 | msr_t msr; |
| 577 | msr_t limit; |
| 578 | uint power_unit; |
| 579 | uint tdp, min_power, max_power, max_time; |
| 580 | u8 power_limit_1_val; |
| 581 | |
| 582 | msr = msr_read(MSR_PLATFORM_INFO); |
| 583 | if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr)) |
| 584 | power_limit_1_time = 28; |
| 585 | |
| 586 | if (!(msr.lo & PLATFORM_INFO_SET_TDP)) |
| 587 | return; |
| 588 | |
| 589 | /* Get units */ |
| 590 | msr = msr_read(MSR_PKG_POWER_SKU_UNIT); |
| 591 | power_unit = 2 << ((msr.lo & 0xf) - 1); |
| 592 | |
| 593 | /* Get power defaults for this SKU */ |
| 594 | msr = msr_read(MSR_PKG_POWER_SKU); |
| 595 | tdp = msr.lo & 0x7fff; |
| 596 | min_power = (msr.lo >> 16) & 0x7fff; |
| 597 | max_power = msr.hi & 0x7fff; |
| 598 | max_time = (msr.hi >> 16) & 0x7f; |
| 599 | |
| 600 | debug("CPU TDP: %u Watts\n", tdp / power_unit); |
| 601 | |
| 602 | if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time) |
| 603 | power_limit_1_time = power_limit_time_msr_to_sec[max_time]; |
| 604 | |
| 605 | if (min_power > 0 && tdp < min_power) |
| 606 | tdp = min_power; |
| 607 | |
| 608 | if (max_power > 0 && tdp > max_power) |
| 609 | tdp = max_power; |
| 610 | |
| 611 | power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time]; |
| 612 | |
| 613 | /* Set long term power limit to TDP */ |
| 614 | limit.lo = 0; |
| 615 | limit.lo |= tdp & PKG_POWER_LIMIT_MASK; |
| 616 | limit.lo |= PKG_POWER_LIMIT_EN; |
| 617 | limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) << |
| 618 | PKG_POWER_LIMIT_TIME_SHIFT; |
| 619 | |
| 620 | /* Set short term power limit to 1.25 * TDP */ |
| 621 | limit.hi = 0; |
| 622 | limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK; |
| 623 | limit.hi |= PKG_POWER_LIMIT_EN; |
| 624 | /* Power limit 2 time is only programmable on server SKU */ |
| 625 | |
| 626 | msr_write(MSR_PKG_POWER_LIMIT, limit); |
| 627 | |
| 628 | /* Set power limit values in MCHBAR as well */ |
| 629 | writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO)); |
| 630 | writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI)); |
| 631 | |
| 632 | /* Set DDR RAPL power limit by copying from MMIO to MSR */ |
| 633 | msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO)); |
| 634 | msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI)); |
| 635 | msr_write(MSR_DDR_RAPL_LIMIT, msr); |
| 636 | |
| 637 | /* Use nominal TDP values for CPUs with configurable TDP */ |
| 638 | if (cpu_config_tdp_levels()) { |
| 639 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 640 | limit.hi = 0; |
| 641 | limit.lo = msr.lo & 0xff; |
| 642 | msr_write(MSR_TURBO_ACTIVATION_RATIO, limit); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | static int broadwell_get_info(struct udevice *dev, struct cpu_info *info) |
| 647 | { |
| 648 | msr_t msr; |
| 649 | |
| 650 | msr = msr_read(IA32_PERF_CTL); |
| 651 | info->cpu_freq = ((msr.lo >> 8) & 0xff) * BROADWELL_BCLK * 1000000; |
| 652 | info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU | |
| 653 | 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static int broadwell_get_count(struct udevice *dev) |
| 659 | { |
| 660 | return 4; |
| 661 | } |
| 662 | |
| 663 | static int cpu_x86_broadwell_probe(struct udevice *dev) |
| 664 | { |
| 665 | if (dev->seq == 0) { |
| 666 | cpu_core_init(dev); |
| 667 | return broadwell_init(dev); |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static const struct cpu_ops cpu_x86_broadwell_ops = { |
| 674 | .get_desc = cpu_x86_get_desc, |
| 675 | .get_info = broadwell_get_info, |
| 676 | .get_count = broadwell_get_count, |
| 677 | .get_vendor = cpu_x86_get_vendor, |
| 678 | }; |
| 679 | |
| 680 | static const struct udevice_id cpu_x86_broadwell_ids[] = { |
| 681 | { .compatible = "intel,core-i3-gen5" }, |
| 682 | { } |
| 683 | }; |
| 684 | |
| 685 | U_BOOT_DRIVER(cpu_x86_broadwell_drv) = { |
| 686 | .name = "cpu_x86_broadwell", |
| 687 | .id = UCLASS_CPU, |
| 688 | .of_match = cpu_x86_broadwell_ids, |
| 689 | .bind = cpu_x86_bind, |
| 690 | .probe = cpu_x86_broadwell_probe, |
| 691 | .ops = &cpu_x86_broadwell_ops, |
| 692 | .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv), |
| 693 | .flags = DM_FLAG_PRE_RELOC, |
| 694 | }; |