Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 2 | |
| 3 | #include <types.h> |
| 4 | #include <console/console.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 5 | #include <acpi/acpi.h> |
| 6 | #include <acpi/acpigen.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 7 | #include <arch/cpu.h> |
| 8 | #include <cpu/x86/msr.h> |
| 9 | #include <cpu/intel/speedstep.h> |
| 10 | #include <cpu/intel/turbo.h> |
| 11 | #include <device/device.h> |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 12 | #include "haswell.h" |
| 13 | #include "chip.h" |
| 14 | |
Duncan Laurie | 1ad5564 | 2013-03-07 14:08:04 -0800 | [diff] [blame] | 15 | #include <southbridge/intel/lynxpoint/pch.h> |
| 16 | |
Angel Pons | 8e6f162 | 2020-10-29 00:18:11 +0100 | [diff] [blame^] | 17 | static int cstate_set_s0ix[3] = { |
| 18 | C_STATE_C1E, |
| 19 | C_STATE_C7S_LONG_LAT, |
| 20 | C_STATE_C10, |
| 21 | }; |
| 22 | |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 23 | static int cstate_set_lp[3] = { |
Angel Pons | bda1c55 | 2020-10-29 00:08:24 +0100 | [diff] [blame] | 24 | C_STATE_C1E, |
| 25 | C_STATE_C3, |
| 26 | C_STATE_C7S_LONG_LAT, |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static int cstate_set_trad[3] = { |
Angel Pons | bda1c55 | 2020-10-29 00:08:24 +0100 | [diff] [blame] | 30 | C_STATE_C1, |
| 31 | C_STATE_C3, |
| 32 | C_STATE_C6_LONG_LAT, |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 35 | static int get_cores_per_package(void) |
| 36 | { |
| 37 | struct cpuinfo_x86 c; |
| 38 | struct cpuid_result result; |
| 39 | int cores = 1; |
| 40 | |
| 41 | get_fms(&c, cpuid_eax(1)); |
| 42 | if (c.x86 != 6) |
| 43 | return 1; |
| 44 | |
| 45 | result = cpuid_ext(0xb, 1); |
| 46 | cores = result.ebx & 0xff; |
| 47 | |
| 48 | return cores; |
| 49 | } |
| 50 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 51 | static acpi_tstate_t tss_table_fine[] = { |
| 52 | { 100, 1000, 0, 0x00, 0 }, |
| 53 | { 94, 940, 0, 0x1f, 0 }, |
| 54 | { 88, 880, 0, 0x1e, 0 }, |
| 55 | { 82, 820, 0, 0x1d, 0 }, |
| 56 | { 75, 760, 0, 0x1c, 0 }, |
| 57 | { 69, 700, 0, 0x1b, 0 }, |
| 58 | { 63, 640, 0, 0x1a, 0 }, |
| 59 | { 57, 580, 0, 0x19, 0 }, |
| 60 | { 50, 520, 0, 0x18, 0 }, |
| 61 | { 44, 460, 0, 0x17, 0 }, |
| 62 | { 38, 400, 0, 0x16, 0 }, |
| 63 | { 32, 340, 0, 0x15, 0 }, |
| 64 | { 25, 280, 0, 0x14, 0 }, |
| 65 | { 19, 220, 0, 0x13, 0 }, |
| 66 | { 13, 160, 0, 0x12, 0 }, |
| 67 | }; |
| 68 | |
| 69 | static acpi_tstate_t tss_table_coarse[] = { |
| 70 | { 100, 1000, 0, 0x00, 0 }, |
| 71 | { 88, 875, 0, 0x1f, 0 }, |
| 72 | { 75, 750, 0, 0x1e, 0 }, |
| 73 | { 63, 625, 0, 0x1d, 0 }, |
| 74 | { 50, 500, 0, 0x1c, 0 }, |
| 75 | { 38, 375, 0, 0x1b, 0 }, |
| 76 | { 25, 250, 0, 0x1a, 0 }, |
| 77 | { 13, 125, 0, 0x19, 0 }, |
| 78 | }; |
| 79 | |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 80 | static void generate_T_state_entries(int core, int cores_per_package) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 81 | { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 82 | /* Indicate SW_ALL coordination for T-states */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 83 | acpigen_write_TSD_package(core, cores_per_package, SW_ALL); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 84 | |
| 85 | /* Indicate FFixedHW so OS will use MSR */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 86 | acpigen_write_empty_PTC(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 87 | |
| 88 | /* Set a T-state limit that can be modified in NVS */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 89 | acpigen_write_TPC("\\TLVL"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * CPUID.(EAX=6):EAX[5] indicates support |
| 93 | * for extended throttle levels. |
| 94 | */ |
| 95 | if (cpuid_eax(6) & (1 << 5)) |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 96 | acpigen_write_TSS_package( |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 97 | ARRAY_SIZE(tss_table_fine), tss_table_fine); |
| 98 | else |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 99 | acpigen_write_TSS_package( |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 100 | ARRAY_SIZE(tss_table_coarse), tss_table_coarse); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Angel Pons | 8e6f162 | 2020-10-29 00:18:11 +0100 | [diff] [blame^] | 103 | static bool is_s0ix_enabled(void) |
| 104 | { |
| 105 | if (!haswell_is_ult()) |
| 106 | return false; |
| 107 | |
| 108 | const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC); |
| 109 | |
| 110 | if (!lapic || !lapic->chip_info) |
| 111 | return false; |
| 112 | |
| 113 | const struct cpu_intel_haswell_config *conf = lapic->chip_info; |
| 114 | |
| 115 | return conf->s0ix_enable; |
| 116 | } |
| 117 | |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 118 | static void generate_C_state_entries(void) |
| 119 | { |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 120 | acpi_cstate_t map[3]; |
| 121 | int *set; |
| 122 | int i; |
| 123 | |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 124 | struct cpu_info *info; |
| 125 | struct cpu_driver *cpu; |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 126 | |
| 127 | /* Find CPU map of supported C-states */ |
| 128 | info = cpu_info(); |
| 129 | if (!info) |
| 130 | return; |
| 131 | cpu = find_cpu_driver(info->cpu); |
| 132 | if (!cpu || !cpu->cstates) |
| 133 | return; |
| 134 | |
Angel Pons | 8e6f162 | 2020-10-29 00:18:11 +0100 | [diff] [blame^] | 135 | if (is_s0ix_enabled()) |
| 136 | set = cstate_set_s0ix; |
| 137 | else if (haswell_is_ult()) |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 138 | set = cstate_set_lp; |
| 139 | else |
| 140 | set = cstate_set_trad; |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 141 | |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 142 | for (i = 0; i < ARRAY_SIZE(map); i++) { |
| 143 | map[i] = cpu->cstates[set[i]]; |
| 144 | map[i].ctype = i + 1; |
| 145 | } |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 146 | |
Angel Pons | ba5761a | 2020-10-28 18:50:26 +0100 | [diff] [blame] | 147 | /* Generate C-state tables */ |
| 148 | acpigen_write_CST_package(map, ARRAY_SIZE(map)); |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 151 | static int calculate_power(int tdp, int p1_ratio, int ratio) |
| 152 | { |
| 153 | u32 m; |
| 154 | u32 power; |
| 155 | |
| 156 | /* |
| 157 | * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2 |
| 158 | * |
| 159 | * Power = (ratio / p1_ratio) * m * tdp |
| 160 | */ |
| 161 | |
| 162 | m = (110000 - ((p1_ratio - ratio) * 625)) / 11; |
| 163 | m = (m * m) / 1000; |
| 164 | |
| 165 | power = ((ratio * 100000 / p1_ratio) / 100); |
| 166 | power *= (m / 100) * (tdp / 1000); |
| 167 | power /= 1000; |
| 168 | |
| 169 | return (int)power; |
| 170 | } |
| 171 | |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 172 | static void generate_P_state_entries(int core, int cores_per_package) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 173 | { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 174 | int ratio_min, ratio_max, ratio_turbo, ratio_step; |
| 175 | int coord_type, power_max, power_unit, num_entries; |
| 176 | int ratio, power, clock, clock_max; |
| 177 | msr_t msr; |
| 178 | |
| 179 | /* Determine P-state coordination type from MISC_PWR_MGMT[0] */ |
| 180 | msr = rdmsr(MSR_MISC_PWR_MGMT); |
| 181 | if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS) |
| 182 | coord_type = SW_ANY; |
| 183 | else |
| 184 | coord_type = HW_ALL; |
| 185 | |
| 186 | /* Get bus ratio limits and calculate clock speeds */ |
| 187 | msr = rdmsr(MSR_PLATFORM_INFO); |
| 188 | ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */ |
| 189 | |
| 190 | /* Determine if this CPU has configurable TDP */ |
| 191 | if (cpu_config_tdp_levels()) { |
| 192 | /* Set max ratio to nominal TDP ratio */ |
| 193 | msr = rdmsr(MSR_CONFIG_TDP_NOMINAL); |
| 194 | ratio_max = msr.lo & 0xff; |
| 195 | } else { |
| 196 | /* Max Non-Turbo Ratio */ |
| 197 | ratio_max = (msr.lo >> 8) & 0xff; |
| 198 | } |
Angel Pons | ca96549 | 2020-10-28 19:15:36 +0100 | [diff] [blame] | 199 | clock_max = ratio_max * CPU_BCLK; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 200 | |
| 201 | /* Calculate CPU TDP in mW */ |
| 202 | msr = rdmsr(MSR_PKG_POWER_SKU_UNIT); |
| 203 | power_unit = 2 << ((msr.lo & 0xf) - 1); |
| 204 | msr = rdmsr(MSR_PKG_POWER_SKU); |
| 205 | power_max = ((msr.lo & 0x7fff) / power_unit) * 1000; |
| 206 | |
| 207 | /* Write _PCT indicating use of FFixedHW */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 208 | acpigen_write_empty_PCT(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 209 | |
| 210 | /* Write _PPC with no limit on supported P-state */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 211 | acpigen_write_PPC_NVS(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 212 | |
| 213 | /* Write PSD indicating configured coordination type */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 214 | acpigen_write_PSD_package(core, 1, coord_type); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 215 | |
| 216 | /* Add P-state entries in _PSS table */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 217 | acpigen_write_name("_PSS"); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 218 | |
| 219 | /* Determine ratio points */ |
| 220 | ratio_step = PSS_RATIO_STEP; |
| 221 | num_entries = (ratio_max - ratio_min) / ratio_step; |
| 222 | while (num_entries > PSS_MAX_ENTRIES-1) { |
| 223 | ratio_step <<= 1; |
| 224 | num_entries >>= 1; |
| 225 | } |
| 226 | |
| 227 | /* P[T] is Turbo state if enabled */ |
| 228 | if (get_turbo_state() == TURBO_ENABLED) { |
| 229 | /* _PSS package count including Turbo */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 230 | acpigen_write_package(num_entries + 2); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 231 | |
| 232 | msr = rdmsr(MSR_TURBO_RATIO_LIMIT); |
| 233 | ratio_turbo = msr.lo & 0xff; |
| 234 | |
| 235 | /* Add entry for Turbo ratio */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 236 | acpigen_write_PSS_package( |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 237 | clock_max + 1, /*MHz*/ |
| 238 | power_max, /*mW*/ |
| 239 | PSS_LATENCY_TRANSITION, /*lat1*/ |
| 240 | PSS_LATENCY_BUSMASTER, /*lat2*/ |
| 241 | ratio_turbo << 8, /*control*/ |
| 242 | ratio_turbo << 8); /*status*/ |
| 243 | } else { |
| 244 | /* _PSS package count without Turbo */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 245 | acpigen_write_package(num_entries + 1); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* First regular entry is max non-turbo ratio */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 249 | acpigen_write_PSS_package( |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 250 | clock_max, /*MHz*/ |
| 251 | power_max, /*mW*/ |
| 252 | PSS_LATENCY_TRANSITION, /*lat1*/ |
| 253 | PSS_LATENCY_BUSMASTER, /*lat2*/ |
| 254 | ratio_max << 8, /*control*/ |
| 255 | ratio_max << 8); /*status*/ |
| 256 | |
| 257 | /* Generate the remaining entries */ |
| 258 | for (ratio = ratio_min + ((num_entries - 1) * ratio_step); |
| 259 | ratio >= ratio_min; ratio -= ratio_step) { |
| 260 | |
| 261 | /* Calculate power at this ratio */ |
| 262 | power = calculate_power(power_max, ratio_max, ratio); |
Angel Pons | ca96549 | 2020-10-28 19:15:36 +0100 | [diff] [blame] | 263 | clock = ratio * CPU_BCLK; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 264 | |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 265 | acpigen_write_PSS_package( |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 266 | clock, /*MHz*/ |
| 267 | power, /*mW*/ |
| 268 | PSS_LATENCY_TRANSITION, /*lat1*/ |
| 269 | PSS_LATENCY_BUSMASTER, /*lat2*/ |
| 270 | ratio << 8, /*control*/ |
| 271 | ratio << 8); /*status*/ |
| 272 | } |
| 273 | |
| 274 | /* Fix package length */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 275 | acpigen_pop_len(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 278 | void generate_cpu_entries(const struct device *device) |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 279 | { |
Duncan Laurie | 1ad5564 | 2013-03-07 14:08:04 -0800 | [diff] [blame] | 280 | int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6; |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 281 | int totalcores = dev_count_cpu(); |
| 282 | int cores_per_package = get_cores_per_package(); |
| 283 | int numcpus = totalcores/cores_per_package; |
| 284 | |
| 285 | printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n", |
| 286 | numcpus, cores_per_package); |
| 287 | |
Martin Roth | 9944b28 | 2014-08-11 11:24:55 -0600 | [diff] [blame] | 288 | for (cpuID = 1; cpuID <= numcpus; cpuID++) { |
Lee Leahy | 9d62e7e | 2017-03-15 17:40:50 -0700 | [diff] [blame] | 289 | for (coreID = 1; coreID <= cores_per_package; coreID++) { |
| 290 | if (coreID > 1) { |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 291 | pcontrol_blk = 0; |
| 292 | plen = 0; |
| 293 | } |
| 294 | |
Christian Walter | be3979c | 2019-12-18 15:07:59 +0100 | [diff] [blame] | 295 | /* Generate processor \_SB.CPUx */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 296 | acpigen_write_processor( |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 297 | (cpuID - 1) * cores_per_package+coreID - 1, |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 298 | pcontrol_blk, plen); |
| 299 | |
| 300 | /* Generate P-state tables */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 301 | generate_P_state_entries( |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 302 | coreID - 1, cores_per_package); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 303 | |
| 304 | /* Generate C-state tables */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 305 | generate_C_state_entries(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 306 | |
| 307 | /* Generate T-state tables */ |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 308 | generate_T_state_entries( |
Angel Pons | 2aaf7c0 | 2020-09-24 18:03:18 +0200 | [diff] [blame] | 309 | cpuID - 1, cores_per_package); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 310 | |
Vladimir Serbinenko | be0fd0a | 2014-11-04 21:10:59 +0100 | [diff] [blame] | 311 | acpigen_pop_len(); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 312 | } |
| 313 | } |
Arthur Heymans | c54d14f | 2018-11-28 12:09:23 +0100 | [diff] [blame] | 314 | |
| 315 | /* PPKG is usually used for thermal management |
| 316 | of the first and only package. */ |
| 317 | acpigen_write_processor_package("PPKG", 0, cores_per_package); |
| 318 | |
| 319 | /* Add a method to notify processor nodes */ |
| 320 | acpigen_write_processor_cnot(cores_per_package); |
Aaron Durbin | 76c3700 | 2012-10-30 09:03:43 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | struct chip_operations cpu_intel_haswell_ops = { |
| 324 | CHIP_NAME("Intel Haswell CPU") |
| 325 | }; |