Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 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> |
Simon Glass | 412f11b | 2019-04-25 21:58:50 -0600 | [diff] [blame^] | 15 | #include <asm/lpc_common.h> |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 16 | #include <asm/msr.h> |
Simon Glass | 412f11b | 2019-04-25 21:58:50 -0600 | [diff] [blame^] | 17 | #include <asm/pci.h> |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 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 | |
| 86 | int arch_cpu_init_dm(void) |
| 87 | { |
| 88 | struct udevice *dev; |
| 89 | int ret; |
| 90 | |
| 91 | /* Start up the LPC so we have serial */ |
| 92 | ret = uclass_first_device(UCLASS_LPC, &dev); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | if (!dev) |
| 96 | return -ENODEV; |
| 97 | ret = cpu_set_flex_ratio_to_tdp_nominal(); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | void set_max_freq(void) |
| 105 | { |
| 106 | msr_t msr, perf_ctl, platform_info; |
| 107 | |
| 108 | /* Check for configurable TDP option */ |
| 109 | platform_info = msr_read(MSR_PLATFORM_INFO); |
| 110 | |
| 111 | if ((platform_info.hi >> 1) & 3) { |
| 112 | /* Set to nominal TDP ratio */ |
| 113 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 114 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 115 | } else { |
| 116 | /* Platform Info bits 15:8 give max ratio */ |
| 117 | msr = msr_read(MSR_PLATFORM_INFO); |
| 118 | perf_ctl.lo = msr.lo & 0xff00; |
| 119 | } |
| 120 | |
| 121 | perf_ctl.hi = 0; |
| 122 | msr_write(IA32_PERF_CTL, perf_ctl); |
| 123 | |
| 124 | debug("CPU: frequency set to %d MHz\n", |
| 125 | ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK); |
| 126 | } |
| 127 | |
| 128 | int arch_cpu_init(void) |
| 129 | { |
| 130 | post_code(POST_CPU_INIT); |
| 131 | |
| 132 | return x86_cpu_init_f(); |
| 133 | } |
| 134 | |
Simon Glass | ee7c36f | 2017-03-28 10:27:30 -0600 | [diff] [blame] | 135 | int checkcpu(void) |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 136 | { |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 137 | int ret; |
| 138 | |
| 139 | set_max_freq(); |
| 140 | |
| 141 | ret = cpu_common_init(); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | gd->arch.pei_boot_mode = PEI_BOOT_NONE; |
| 145 | |
Simon Glass | ee7c36f | 2017-03-28 10:27:30 -0600 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int print_cpuinfo(void) |
| 150 | { |
| 151 | char processor_name[CPU_MAX_NAME_LEN]; |
| 152 | const char *name; |
| 153 | |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 154 | /* Print processor name */ |
| 155 | name = cpu_get_name(processor_name); |
| 156 | printf("CPU: %s\n", name); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Simon Glass | 412f11b | 2019-04-25 21:58:50 -0600 | [diff] [blame^] | 161 | void board_debug_uart_init(void) |
| 162 | { |
| 163 | struct udevice *bus = NULL; |
| 164 | |
| 165 | /* com1 / com2 decode range */ |
| 166 | pci_x86_write_config(bus, PCH_DEV_LPC, LPC_IO_DEC, 1 << 4, PCI_SIZE_16); |
| 167 | |
| 168 | pci_x86_write_config(bus, PCH_DEV_LPC, LPC_EN, COMA_LPC_EN, |
| 169 | PCI_SIZE_16); |
| 170 | } |
| 171 | |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 172 | /* |
| 173 | * The core 100MHz BLCK is disabled in deeper c-states. One needs to calibrate |
| 174 | * the 100MHz BCLCK against the 24MHz BLCK to restore the clocks properly |
| 175 | * when a core is woken up |
| 176 | */ |
| 177 | static int pcode_ready(void) |
| 178 | { |
| 179 | int wait_count; |
| 180 | const int delay_step = 10; |
| 181 | |
| 182 | wait_count = 0; |
| 183 | do { |
| 184 | if (!(readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & |
| 185 | MAILBOX_RUN_BUSY)) |
| 186 | return 0; |
| 187 | wait_count += delay_step; |
| 188 | udelay(delay_step); |
| 189 | } while (wait_count < 1000); |
| 190 | |
| 191 | return -ETIMEDOUT; |
| 192 | } |
| 193 | |
| 194 | static u32 pcode_mailbox_read(u32 command) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | ret = pcode_ready(); |
| 199 | if (ret) { |
| 200 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /* Send command and start transaction */ |
| 205 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 206 | |
| 207 | ret = pcode_ready(); |
| 208 | if (ret) { |
| 209 | debug("PCODE: mailbox timeout on completion\n"); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* Read mailbox */ |
| 214 | return readl(MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 215 | } |
| 216 | |
| 217 | static int pcode_mailbox_write(u32 command, u32 data) |
| 218 | { |
| 219 | int ret; |
| 220 | |
| 221 | ret = pcode_ready(); |
| 222 | if (ret) { |
| 223 | debug("PCODE: mailbox timeout on wait ready\n"); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | writel(data, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 228 | |
| 229 | /* Send command and start transaction */ |
| 230 | writel(command | MAILBOX_RUN_BUSY, MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 231 | |
| 232 | ret = pcode_ready(); |
| 233 | if (ret) { |
| 234 | debug("PCODE: mailbox timeout on completion\n"); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* @dev is the CPU device */ |
| 242 | static void initialize_vr_config(struct udevice *dev) |
| 243 | { |
| 244 | int ramp, min_vid; |
| 245 | msr_t msr; |
| 246 | |
| 247 | debug("Initializing VR config\n"); |
| 248 | |
| 249 | /* Configure VR_CURRENT_CONFIG */ |
| 250 | msr = msr_read(MSR_VR_CURRENT_CONFIG); |
| 251 | /* |
| 252 | * Preserve bits 63 and 62. Bit 62 is PSI4 enable, but it is only valid |
| 253 | * on ULT systems |
| 254 | */ |
| 255 | msr.hi &= 0xc0000000; |
| 256 | msr.hi |= (0x01 << (52 - 32)); /* PSI3 threshold - 1A */ |
| 257 | msr.hi |= (0x05 << (42 - 32)); /* PSI2 threshold - 5A */ |
| 258 | msr.hi |= (0x14 << (32 - 32)); /* PSI1 threshold - 20A */ |
| 259 | msr.hi |= (1 << (62 - 32)); /* Enable PSI4 */ |
| 260 | /* Leave the max instantaneous current limit (12:0) to default */ |
| 261 | msr_write(MSR_VR_CURRENT_CONFIG, msr); |
| 262 | |
| 263 | /* Configure VR_MISC_CONFIG MSR */ |
| 264 | msr = msr_read(MSR_VR_MISC_CONFIG); |
| 265 | /* Set the IOUT_SLOPE scalar applied to dIout in U10.1.9 format */ |
| 266 | msr.hi &= ~(0x3ff << (40 - 32)); |
| 267 | msr.hi |= (0x200 << (40 - 32)); /* 1.0 */ |
| 268 | /* Set IOUT_OFFSET to 0 */ |
| 269 | msr.hi &= ~0xff; |
| 270 | /* Set entry ramp rate to slow */ |
| 271 | msr.hi &= ~(1 << (51 - 32)); |
| 272 | /* Enable decay mode on C-state entry */ |
| 273 | msr.hi |= (1 << (52 - 32)); |
| 274 | /* Set the slow ramp rate */ |
| 275 | msr.hi &= ~(0x3 << (53 - 32)); |
| 276 | /* Configure the C-state exit ramp rate */ |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 277 | ramp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 278 | "intel,slow-ramp", -1); |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 279 | if (ramp != -1) { |
| 280 | /* Configured slow ramp rate */ |
| 281 | msr.hi |= ((ramp & 0x3) << (53 - 32)); |
| 282 | /* Set exit ramp rate to slow */ |
| 283 | msr.hi &= ~(1 << (50 - 32)); |
| 284 | } else { |
| 285 | /* Fast ramp rate / 4 */ |
| 286 | msr.hi |= (0x01 << (53 - 32)); |
| 287 | /* Set exit ramp rate to fast */ |
| 288 | msr.hi |= (1 << (50 - 32)); |
| 289 | } |
| 290 | /* Set MIN_VID (31:24) to allow CPU to have full control */ |
| 291 | msr.lo &= ~0xff000000; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 292 | min_vid = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 293 | "intel,min-vid", 0); |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 294 | msr.lo |= (min_vid & 0xff) << 24; |
| 295 | msr_write(MSR_VR_MISC_CONFIG, msr); |
| 296 | |
| 297 | /* Configure VR_MISC_CONFIG2 MSR */ |
| 298 | msr = msr_read(MSR_VR_MISC_CONFIG2); |
| 299 | msr.lo &= ~0xffff; |
| 300 | /* |
| 301 | * Allow CPU to control minimum voltage completely (15:8) and |
| 302 | * set the fast ramp voltage in 10mV steps |
| 303 | */ |
| 304 | if (cpu_get_family_model() == BROADWELL_FAMILY_ULT) |
| 305 | msr.lo |= 0x006a; /* 1.56V */ |
| 306 | else |
| 307 | msr.lo |= 0x006f; /* 1.60V */ |
| 308 | msr_write(MSR_VR_MISC_CONFIG2, msr); |
| 309 | |
| 310 | /* Set C9/C10 VCC Min */ |
| 311 | pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_C9C10_VOLTAGE, 0x1f1f); |
| 312 | } |
| 313 | |
| 314 | static int calibrate_24mhz_bclk(void) |
| 315 | { |
| 316 | int err_code; |
| 317 | int ret; |
| 318 | |
| 319 | ret = pcode_ready(); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
| 323 | /* A non-zero value initiates the PCODE calibration */ |
| 324 | writel(~0, MCHBAR_REG(BIOS_MAILBOX_DATA)); |
| 325 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_FSM_MEASURE_INTVL, |
| 326 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 327 | |
| 328 | ret = pcode_ready(); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | |
| 332 | err_code = readl(MCHBAR_REG(BIOS_MAILBOX_INTERFACE)) & 0xff; |
| 333 | |
| 334 | debug("PCODE: 24MHz BLCK calibration response: %d\n", err_code); |
| 335 | |
| 336 | /* Read the calibrated value */ |
| 337 | writel(MAILBOX_RUN_BUSY | MAILBOX_BIOS_CMD_READ_CALIBRATION, |
| 338 | MCHBAR_REG(BIOS_MAILBOX_INTERFACE)); |
| 339 | |
| 340 | ret = pcode_ready(); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | debug("PCODE: 24MHz BLCK calibration value: 0x%08x\n", |
| 345 | readl(MCHBAR_REG(BIOS_MAILBOX_DATA))); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static void configure_pch_power_sharing(void) |
| 351 | { |
| 352 | u32 pch_power, pch_power_ext, pmsync, pmsync2; |
| 353 | int i; |
| 354 | |
| 355 | /* Read PCH Power levels from PCODE */ |
| 356 | pch_power = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER); |
| 357 | pch_power_ext = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_PCH_POWER_EXT); |
| 358 | |
| 359 | debug("PCH Power: PCODE Levels 0x%08x 0x%08x\n", pch_power, |
| 360 | pch_power_ext); |
| 361 | |
| 362 | pmsync = readl(RCB_REG(PMSYNC_CONFIG)); |
| 363 | pmsync2 = readl(RCB_REG(PMSYNC_CONFIG2)); |
| 364 | |
| 365 | /* |
| 366 | * Program PMSYNC_TPR_CONFIG PCH power limit values |
| 367 | * pmsync[0:4] = mailbox[0:5] |
| 368 | * pmsync[8:12] = mailbox[6:11] |
| 369 | * pmsync[16:20] = mailbox[12:17] |
| 370 | */ |
| 371 | for (i = 0; i < 3; i++) { |
| 372 | u32 level = pch_power & 0x3f; |
| 373 | pch_power >>= 6; |
| 374 | pmsync &= ~(0x1f << (i * 8)); |
| 375 | pmsync |= (level & 0x1f) << (i * 8); |
| 376 | } |
| 377 | writel(pmsync, RCB_REG(PMSYNC_CONFIG)); |
| 378 | |
| 379 | /* |
| 380 | * Program PMSYNC_TPR_CONFIG2 Extended PCH power limit values |
| 381 | * pmsync2[0:4] = mailbox[23:18] |
| 382 | * pmsync2[8:12] = mailbox_ext[6:11] |
| 383 | * pmsync2[16:20] = mailbox_ext[12:17] |
| 384 | * pmsync2[24:28] = mailbox_ext[18:22] |
| 385 | */ |
| 386 | pmsync2 &= ~0x1f; |
| 387 | pmsync2 |= pch_power & 0x1f; |
| 388 | |
| 389 | for (i = 1; i < 4; i++) { |
| 390 | u32 level = pch_power_ext & 0x3f; |
| 391 | pch_power_ext >>= 6; |
| 392 | pmsync2 &= ~(0x1f << (i * 8)); |
| 393 | pmsync2 |= (level & 0x1f) << (i * 8); |
| 394 | } |
| 395 | writel(pmsync2, RCB_REG(PMSYNC_CONFIG2)); |
| 396 | } |
| 397 | |
| 398 | static int bsp_init_before_ap_bringup(struct udevice *dev) |
| 399 | { |
| 400 | int ret; |
| 401 | |
| 402 | initialize_vr_config(dev); |
| 403 | ret = calibrate_24mhz_bclk(); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | configure_pch_power_sharing(); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | int cpu_config_tdp_levels(void) |
| 412 | { |
| 413 | msr_t platform_info; |
| 414 | |
| 415 | /* Bits 34:33 indicate how many levels supported */ |
| 416 | platform_info = msr_read(MSR_PLATFORM_INFO); |
| 417 | return (platform_info.hi >> 1) & 3; |
| 418 | } |
| 419 | |
| 420 | static void set_max_ratio(void) |
| 421 | { |
| 422 | msr_t msr, perf_ctl; |
| 423 | |
| 424 | perf_ctl.hi = 0; |
| 425 | |
| 426 | /* Check for configurable TDP option */ |
| 427 | if (turbo_get_state() == TURBO_ENABLED) { |
| 428 | msr = msr_read(MSR_NHM_TURBO_RATIO_LIMIT); |
| 429 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 430 | } else if (cpu_config_tdp_levels()) { |
| 431 | /* Set to nominal TDP ratio */ |
| 432 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 433 | perf_ctl.lo = (msr.lo & 0xff) << 8; |
| 434 | } else { |
| 435 | /* Platform Info bits 15:8 give max ratio */ |
| 436 | msr = msr_read(MSR_PLATFORM_INFO); |
| 437 | perf_ctl.lo = msr.lo & 0xff00; |
| 438 | } |
| 439 | msr_write(IA32_PERF_CTL, perf_ctl); |
| 440 | |
| 441 | debug("cpu: frequency set to %d\n", |
| 442 | ((perf_ctl.lo >> 8) & 0xff) * CPU_BCLK); |
| 443 | } |
| 444 | |
| 445 | int broadwell_init(struct udevice *dev) |
| 446 | { |
| 447 | struct cpu_broadwell_priv *priv = dev_get_priv(dev); |
| 448 | int num_threads; |
| 449 | int num_cores; |
| 450 | msr_t msr; |
| 451 | int ret; |
| 452 | |
| 453 | msr = msr_read(CORE_THREAD_COUNT_MSR); |
| 454 | num_threads = (msr.lo >> 0) & 0xffff; |
| 455 | num_cores = (msr.lo >> 16) & 0xffff; |
| 456 | debug("CPU has %u cores, %u threads enabled\n", num_cores, |
| 457 | num_threads); |
| 458 | |
| 459 | priv->ht_disabled = num_threads == num_cores; |
| 460 | |
| 461 | ret = bsp_init_before_ap_bringup(dev); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | |
| 465 | set_max_ratio(); |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static void configure_mca(void) |
| 471 | { |
| 472 | msr_t msr; |
| 473 | const unsigned int mcg_cap_msr = 0x179; |
| 474 | int i; |
| 475 | int num_banks; |
| 476 | |
| 477 | msr = msr_read(mcg_cap_msr); |
| 478 | num_banks = msr.lo & 0xff; |
| 479 | msr.lo = 0; |
| 480 | msr.hi = 0; |
| 481 | /* |
| 482 | * TODO(adurbin): This should only be done on a cold boot. Also, some |
| 483 | * of these banks are core vs package scope. For now every CPU clears |
| 484 | * every bank |
| 485 | */ |
| 486 | for (i = 0; i < num_banks; i++) |
| 487 | msr_write(MSR_IA32_MC0_STATUS + (i * 4), msr); |
| 488 | } |
| 489 | |
| 490 | static void enable_lapic_tpr(void) |
| 491 | { |
| 492 | msr_t msr; |
| 493 | |
| 494 | msr = msr_read(MSR_PIC_MSG_CONTROL); |
| 495 | msr.lo &= ~(1 << 10); /* Enable APIC TPR updates */ |
| 496 | msr_write(MSR_PIC_MSG_CONTROL, msr); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | static void configure_c_states(void) |
| 501 | { |
| 502 | msr_t msr; |
| 503 | |
| 504 | msr = msr_read(MSR_PMG_CST_CONFIG_CONTROL); |
| 505 | msr.lo |= (1 << 31); /* Timed MWAIT Enable */ |
| 506 | msr.lo |= (1 << 30); /* Package c-state Undemotion Enable */ |
| 507 | msr.lo |= (1 << 29); /* Package c-state Demotion Enable */ |
| 508 | msr.lo |= (1 << 28); /* C1 Auto Undemotion Enable */ |
| 509 | msr.lo |= (1 << 27); /* C3 Auto Undemotion Enable */ |
| 510 | msr.lo |= (1 << 26); /* C1 Auto Demotion Enable */ |
| 511 | msr.lo |= (1 << 25); /* C3 Auto Demotion Enable */ |
| 512 | msr.lo &= ~(1 << 10); /* Disable IO MWAIT redirection */ |
| 513 | /* The deepest package c-state defaults to factory-configured value */ |
| 514 | msr_write(MSR_PMG_CST_CONFIG_CONTROL, msr); |
| 515 | |
| 516 | msr = msr_read(MSR_MISC_PWR_MGMT); |
| 517 | msr.lo &= ~(1 << 0); /* Enable P-state HW_ALL coordination */ |
| 518 | msr_write(MSR_MISC_PWR_MGMT, msr); |
| 519 | |
| 520 | msr = msr_read(MSR_POWER_CTL); |
| 521 | msr.lo |= (1 << 18); /* Enable Energy Perf Bias MSR 0x1b0 */ |
| 522 | msr.lo |= (1 << 1); /* C1E Enable */ |
| 523 | msr.lo |= (1 << 0); /* Bi-directional PROCHOT# */ |
| 524 | msr_write(MSR_POWER_CTL, msr); |
| 525 | |
| 526 | /* C-state Interrupt Response Latency Control 0 - package C3 latency */ |
| 527 | msr.hi = 0; |
| 528 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_0_LIMIT; |
| 529 | msr_write(MSR_C_STATE_LATENCY_CONTROL_0, msr); |
| 530 | |
| 531 | /* C-state Interrupt Response Latency Control 1 */ |
| 532 | msr.hi = 0; |
| 533 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_1_LIMIT; |
| 534 | msr_write(MSR_C_STATE_LATENCY_CONTROL_1, msr); |
| 535 | |
| 536 | /* C-state Interrupt Response Latency Control 2 - package C6/C7 short */ |
| 537 | msr.hi = 0; |
| 538 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_2_LIMIT; |
| 539 | msr_write(MSR_C_STATE_LATENCY_CONTROL_2, msr); |
| 540 | |
| 541 | /* C-state Interrupt Response Latency Control 3 - package C8 */ |
| 542 | msr.hi = 0; |
| 543 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_3_LIMIT; |
| 544 | msr_write(MSR_C_STATE_LATENCY_CONTROL_3, msr); |
| 545 | |
| 546 | /* C-state Interrupt Response Latency Control 4 - package C9 */ |
| 547 | msr.hi = 0; |
| 548 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_4_LIMIT; |
| 549 | msr_write(MSR_C_STATE_LATENCY_CONTROL_4, msr); |
| 550 | |
| 551 | /* C-state Interrupt Response Latency Control 5 - package C10 */ |
| 552 | msr.hi = 0; |
| 553 | msr.lo = IRTL_VALID | IRTL_1024_NS | C_STATE_LATENCY_CONTROL_5_LIMIT; |
| 554 | msr_write(MSR_C_STATE_LATENCY_CONTROL_5, msr); |
| 555 | } |
| 556 | |
| 557 | static void configure_misc(void) |
| 558 | { |
| 559 | msr_t msr; |
| 560 | |
| 561 | msr = msr_read(MSR_IA32_MISC_ENABLE); |
| 562 | msr.lo |= (1 << 0); /* Fast String enable */ |
| 563 | msr.lo |= (1 << 3); /* TM1/TM2/EMTTM enable */ |
| 564 | msr.lo |= (1 << 16); /* Enhanced SpeedStep Enable */ |
| 565 | msr_write(MSR_IA32_MISC_ENABLE, msr); |
| 566 | |
| 567 | /* Disable thermal interrupts */ |
| 568 | msr.lo = 0; |
| 569 | msr.hi = 0; |
| 570 | msr_write(MSR_IA32_THERM_INTERRUPT, msr); |
| 571 | |
| 572 | /* Enable package critical interrupt only */ |
| 573 | msr.lo = 1 << 4; |
| 574 | msr.hi = 0; |
| 575 | msr_write(MSR_IA32_PACKAGE_THERM_INTERRUPT, msr); |
| 576 | } |
| 577 | |
| 578 | static void configure_thermal_target(struct udevice *dev) |
| 579 | { |
| 580 | int tcc_offset; |
| 581 | msr_t msr; |
| 582 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 583 | tcc_offset = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 584 | "intel,tcc-offset", 0); |
| 585 | |
| 586 | /* Set TCC activaiton offset if supported */ |
| 587 | msr = msr_read(MSR_PLATFORM_INFO); |
| 588 | if ((msr.lo & (1 << 30)) && tcc_offset) { |
| 589 | msr = msr_read(MSR_TEMPERATURE_TARGET); |
| 590 | msr.lo &= ~(0xf << 24); /* Bits 27:24 */ |
| 591 | msr.lo |= (tcc_offset & 0xf) << 24; |
| 592 | msr_write(MSR_TEMPERATURE_TARGET, msr); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static void configure_dca_cap(void) |
| 597 | { |
| 598 | struct cpuid_result cpuid_regs; |
| 599 | msr_t msr; |
| 600 | |
| 601 | /* Check feature flag in CPUID.(EAX=1):ECX[18]==1 */ |
| 602 | cpuid_regs = cpuid(1); |
| 603 | if (cpuid_regs.ecx & (1 << 18)) { |
| 604 | msr = msr_read(MSR_IA32_PLATFORM_DCA_CAP); |
| 605 | msr.lo |= 1; |
| 606 | msr_write(MSR_IA32_PLATFORM_DCA_CAP, msr); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static void set_energy_perf_bias(u8 policy) |
| 611 | { |
| 612 | msr_t msr; |
| 613 | int ecx; |
| 614 | |
| 615 | /* Determine if energy efficient policy is supported */ |
| 616 | ecx = cpuid_ecx(0x6); |
| 617 | if (!(ecx & (1 << 3))) |
| 618 | return; |
| 619 | |
| 620 | /* Energy Policy is bits 3:0 */ |
| 621 | msr = msr_read(MSR_IA32_ENERGY_PERFORMANCE_BIAS); |
| 622 | msr.lo &= ~0xf; |
| 623 | msr.lo |= policy & 0xf; |
| 624 | msr_write(MSR_IA32_ENERGY_PERFORMANCE_BIAS, msr); |
| 625 | |
| 626 | debug("cpu: energy policy set to %u\n", policy); |
| 627 | } |
| 628 | |
| 629 | /* All CPUs including BSP will run the following function */ |
| 630 | static void cpu_core_init(struct udevice *dev) |
| 631 | { |
| 632 | /* Clear out pending MCEs */ |
| 633 | configure_mca(); |
| 634 | |
| 635 | /* Enable the local cpu apics */ |
| 636 | enable_lapic_tpr(); |
| 637 | |
| 638 | /* Configure C States */ |
| 639 | configure_c_states(); |
| 640 | |
| 641 | /* Configure Enhanced SpeedStep and Thermal Sensors */ |
| 642 | configure_misc(); |
| 643 | |
| 644 | /* Thermal throttle activation offset */ |
| 645 | configure_thermal_target(dev); |
| 646 | |
| 647 | /* Enable Direct Cache Access */ |
| 648 | configure_dca_cap(); |
| 649 | |
| 650 | /* Set energy policy */ |
| 651 | set_energy_perf_bias(ENERGY_POLICY_NORMAL); |
| 652 | |
| 653 | /* Enable Turbo */ |
| 654 | turbo_enable(); |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Configure processor power limits if possible |
| 659 | * This must be done AFTER set of BIOS_RESET_CPL |
| 660 | */ |
| 661 | void cpu_set_power_limits(int power_limit_1_time) |
| 662 | { |
| 663 | msr_t msr; |
| 664 | msr_t limit; |
| 665 | unsigned power_unit; |
| 666 | unsigned tdp, min_power, max_power, max_time; |
| 667 | u8 power_limit_1_val; |
| 668 | |
| 669 | msr = msr_read(MSR_PLATFORM_INFO); |
| 670 | if (power_limit_1_time > ARRAY_SIZE(power_limit_time_sec_to_msr)) |
| 671 | power_limit_1_time = 28; |
| 672 | |
| 673 | if (!(msr.lo & PLATFORM_INFO_SET_TDP)) |
| 674 | return; |
| 675 | |
| 676 | /* Get units */ |
| 677 | msr = msr_read(MSR_PKG_POWER_SKU_UNIT); |
| 678 | power_unit = 2 << ((msr.lo & 0xf) - 1); |
| 679 | |
| 680 | /* Get power defaults for this SKU */ |
| 681 | msr = msr_read(MSR_PKG_POWER_SKU); |
| 682 | tdp = msr.lo & 0x7fff; |
| 683 | min_power = (msr.lo >> 16) & 0x7fff; |
| 684 | max_power = msr.hi & 0x7fff; |
| 685 | max_time = (msr.hi >> 16) & 0x7f; |
| 686 | |
| 687 | debug("CPU TDP: %u Watts\n", tdp / power_unit); |
| 688 | |
| 689 | if (power_limit_time_msr_to_sec[max_time] > power_limit_1_time) |
| 690 | power_limit_1_time = power_limit_time_msr_to_sec[max_time]; |
| 691 | |
| 692 | if (min_power > 0 && tdp < min_power) |
| 693 | tdp = min_power; |
| 694 | |
| 695 | if (max_power > 0 && tdp > max_power) |
| 696 | tdp = max_power; |
| 697 | |
| 698 | power_limit_1_val = power_limit_time_sec_to_msr[power_limit_1_time]; |
| 699 | |
| 700 | /* Set long term power limit to TDP */ |
| 701 | limit.lo = 0; |
| 702 | limit.lo |= tdp & PKG_POWER_LIMIT_MASK; |
| 703 | limit.lo |= PKG_POWER_LIMIT_EN; |
| 704 | limit.lo |= (power_limit_1_val & PKG_POWER_LIMIT_TIME_MASK) << |
| 705 | PKG_POWER_LIMIT_TIME_SHIFT; |
| 706 | |
| 707 | /* Set short term power limit to 1.25 * TDP */ |
| 708 | limit.hi = 0; |
| 709 | limit.hi |= ((tdp * 125) / 100) & PKG_POWER_LIMIT_MASK; |
| 710 | limit.hi |= PKG_POWER_LIMIT_EN; |
| 711 | /* Power limit 2 time is only programmable on server SKU */ |
| 712 | |
| 713 | msr_write(MSR_PKG_POWER_LIMIT, limit); |
| 714 | |
| 715 | /* Set power limit values in MCHBAR as well */ |
| 716 | writel(limit.lo, MCHBAR_REG(MCH_PKG_POWER_LIMIT_LO)); |
| 717 | writel(limit.hi, MCHBAR_REG(MCH_PKG_POWER_LIMIT_HI)); |
| 718 | |
| 719 | /* Set DDR RAPL power limit by copying from MMIO to MSR */ |
| 720 | msr.lo = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_LO)); |
| 721 | msr.hi = readl(MCHBAR_REG(MCH_DDR_POWER_LIMIT_HI)); |
| 722 | msr_write(MSR_DDR_RAPL_LIMIT, msr); |
| 723 | |
| 724 | /* Use nominal TDP values for CPUs with configurable TDP */ |
| 725 | if (cpu_config_tdp_levels()) { |
| 726 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 727 | limit.hi = 0; |
| 728 | limit.lo = msr.lo & 0xff; |
| 729 | msr_write(MSR_TURBO_ACTIVATION_RATIO, limit); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static int broadwell_get_info(struct udevice *dev, struct cpu_info *info) |
| 734 | { |
| 735 | msr_t msr; |
| 736 | |
| 737 | msr = msr_read(IA32_PERF_CTL); |
| 738 | info->cpu_freq = ((msr.lo >> 8) & 0xff) * BROADWELL_BCLK * 1000000; |
| 739 | info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU | |
| 740 | 1 << CPU_FEAT_UCODE | 1 << CPU_FEAT_DEVICE_ID; |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int broadwell_get_count(struct udevice *dev) |
| 746 | { |
| 747 | return 4; |
| 748 | } |
| 749 | |
| 750 | static int cpu_x86_broadwell_probe(struct udevice *dev) |
| 751 | { |
| 752 | if (dev->seq == 0) { |
| 753 | cpu_core_init(dev); |
| 754 | return broadwell_init(dev); |
| 755 | } |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static const struct cpu_ops cpu_x86_broadwell_ops = { |
| 761 | .get_desc = cpu_x86_get_desc, |
| 762 | .get_info = broadwell_get_info, |
| 763 | .get_count = broadwell_get_count, |
Alexander Graf | 67f66bd | 2016-08-19 01:23:27 +0200 | [diff] [blame] | 764 | .get_vendor = cpu_x86_get_vendor, |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | static const struct udevice_id cpu_x86_broadwell_ids[] = { |
| 768 | { .compatible = "intel,core-i3-gen5" }, |
| 769 | { } |
| 770 | }; |
| 771 | |
| 772 | U_BOOT_DRIVER(cpu_x86_broadwell_drv) = { |
| 773 | .name = "cpu_x86_broadwell", |
| 774 | .id = UCLASS_CPU, |
| 775 | .of_match = cpu_x86_broadwell_ids, |
| 776 | .bind = cpu_x86_bind, |
| 777 | .probe = cpu_x86_broadwell_probe, |
| 778 | .ops = &cpu_x86_broadwell_ops, |
| 779 | .priv_auto_alloc_size = sizeof(struct cpu_broadwell_priv), |
Bin Meng | 09921ac | 2018-10-14 01:07:19 -0700 | [diff] [blame] | 780 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | 71606de | 2016-03-11 22:07:18 -0700 | [diff] [blame] | 781 | }; |