blob: dfbda450dca8a3a74c95a11e1bc8a1efefefab83 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin76c37002012-10-30 09:03:43 -05002
3#include <types.h>
4#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpigen.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05007#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 Durbin76c37002012-10-30 09:03:43 -050012#include "haswell.h"
13#include "chip.h"
14
Duncan Laurie1ad55642013-03-07 14:08:04 -080015#include <southbridge/intel/lynxpoint/pch.h>
16
Angel Pons618b9ad2021-01-21 21:22:19 +010017#define MWAIT_RES(state, sub_state) \
18 { \
19 .addrl = (((state) << 4) | (sub_state)), \
20 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
21 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
22 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
23 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
24 }
25
26static acpi_cstate_t cstate_map[NUM_C_STATES] = {
27 [C_STATE_C0] = { },
28 [C_STATE_C1] = {
29 .latency = 0,
30 .power = 1000,
31 .resource = MWAIT_RES(0, 0),
32 },
33 [C_STATE_C1E] = {
34 .latency = 0,
35 .power = 1000,
36 .resource = MWAIT_RES(0, 1),
37 },
38 [C_STATE_C3] = {
39 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
40 .power = 900,
41 .resource = MWAIT_RES(1, 0),
42 },
43 [C_STATE_C6_SHORT_LAT] = {
44 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
45 .power = 800,
46 .resource = MWAIT_RES(2, 0),
47 },
48 [C_STATE_C6_LONG_LAT] = {
49 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
50 .power = 800,
51 .resource = MWAIT_RES(2, 1),
52 },
53 [C_STATE_C7_SHORT_LAT] = {
54 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
55 .power = 700,
56 .resource = MWAIT_RES(3, 0),
57 },
58 [C_STATE_C7_LONG_LAT] = {
59 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
60 .power = 700,
61 .resource = MWAIT_RES(3, 1),
62 },
63 [C_STATE_C7S_SHORT_LAT] = {
64 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
65 .power = 700,
66 .resource = MWAIT_RES(3, 2),
67 },
68 [C_STATE_C7S_LONG_LAT] = {
69 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
70 .power = 700,
71 .resource = MWAIT_RES(3, 3),
72 },
73 [C_STATE_C8] = {
74 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
75 .power = 600,
76 .resource = MWAIT_RES(4, 0),
77 },
78 [C_STATE_C9] = {
79 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
80 .power = 500,
81 .resource = MWAIT_RES(5, 0),
82 },
83 [C_STATE_C10] = {
84 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
85 .power = 400,
86 .resource = MWAIT_RES(6, 0),
87 },
88};
89
Angel Pons8e6f1622020-10-29 00:18:11 +010090static int cstate_set_s0ix[3] = {
91 C_STATE_C1E,
92 C_STATE_C7S_LONG_LAT,
93 C_STATE_C10,
94};
95
Angel Ponsba5761a2020-10-28 18:50:26 +010096static int cstate_set_lp[3] = {
Angel Ponsbda1c552020-10-29 00:08:24 +010097 C_STATE_C1E,
98 C_STATE_C3,
99 C_STATE_C7S_LONG_LAT,
Angel Ponsba5761a2020-10-28 18:50:26 +0100100};
101
102static int cstate_set_trad[3] = {
Angel Ponsbda1c552020-10-29 00:08:24 +0100103 C_STATE_C1,
104 C_STATE_C3,
105 C_STATE_C6_LONG_LAT,
Angel Ponsba5761a2020-10-28 18:50:26 +0100106};
107
Angel Pons11235d62021-01-04 17:56:44 +0100108static int get_logical_cores_per_package(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500109{
Angel Pons11235d62021-01-04 17:56:44 +0100110 msr_t msr = rdmsr(MSR_CORE_THREAD_COUNT);
111 return msr.lo & 0xffff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500112}
113
Aaron Durbin76c37002012-10-30 09:03:43 -0500114static acpi_tstate_t tss_table_fine[] = {
115 { 100, 1000, 0, 0x00, 0 },
116 { 94, 940, 0, 0x1f, 0 },
117 { 88, 880, 0, 0x1e, 0 },
118 { 82, 820, 0, 0x1d, 0 },
119 { 75, 760, 0, 0x1c, 0 },
120 { 69, 700, 0, 0x1b, 0 },
121 { 63, 640, 0, 0x1a, 0 },
122 { 57, 580, 0, 0x19, 0 },
123 { 50, 520, 0, 0x18, 0 },
124 { 44, 460, 0, 0x17, 0 },
125 { 38, 400, 0, 0x16, 0 },
126 { 32, 340, 0, 0x15, 0 },
127 { 25, 280, 0, 0x14, 0 },
128 { 19, 220, 0, 0x13, 0 },
129 { 13, 160, 0, 0x12, 0 },
130};
131
132static acpi_tstate_t tss_table_coarse[] = {
133 { 100, 1000, 0, 0x00, 0 },
134 { 88, 875, 0, 0x1f, 0 },
135 { 75, 750, 0, 0x1e, 0 },
136 { 63, 625, 0, 0x1d, 0 },
137 { 50, 500, 0, 0x1c, 0 },
138 { 38, 375, 0, 0x1b, 0 },
139 { 25, 250, 0, 0x1a, 0 },
140 { 13, 125, 0, 0x19, 0 },
141};
142
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100143static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500144{
Aaron Durbin76c37002012-10-30 09:03:43 -0500145 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100146 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500147
148 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100149 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500150
151 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100152 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500153
154 /*
155 * CPUID.(EAX=6):EAX[5] indicates support
156 * for extended throttle levels.
157 */
158 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100159 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500160 ARRAY_SIZE(tss_table_fine), tss_table_fine);
161 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100162 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500163 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500164}
165
Angel Pons8e6f1622020-10-29 00:18:11 +0100166static bool is_s0ix_enabled(void)
167{
168 if (!haswell_is_ult())
169 return false;
170
171 const struct device *lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
172
173 if (!lapic || !lapic->chip_info)
174 return false;
175
176 const struct cpu_intel_haswell_config *conf = lapic->chip_info;
177
178 return conf->s0ix_enable;
179}
180
Angel Pons2aaf7c02020-09-24 18:03:18 +0200181static void generate_C_state_entries(void)
182{
Angel Pons618b9ad2021-01-21 21:22:19 +0100183 acpi_cstate_t acpi_cstate_map[3] = {0};
Angel Ponsba5761a2020-10-28 18:50:26 +0100184
Angel Pons618b9ad2021-01-21 21:22:19 +0100185 int *acpi_cstates;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200186
Angel Pons8e6f1622020-10-29 00:18:11 +0100187 if (is_s0ix_enabled())
Angel Pons618b9ad2021-01-21 21:22:19 +0100188 acpi_cstates = cstate_set_s0ix;
Angel Pons8e6f1622020-10-29 00:18:11 +0100189 else if (haswell_is_ult())
Angel Pons618b9ad2021-01-21 21:22:19 +0100190 acpi_cstates = cstate_set_lp;
Angel Ponsba5761a2020-10-28 18:50:26 +0100191 else
Angel Pons618b9ad2021-01-21 21:22:19 +0100192 acpi_cstates = cstate_set_trad;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200193
Angel Pons618b9ad2021-01-21 21:22:19 +0100194 /* Count number of active C-states */
195 int count = 0;
196
197 for (int i = 0; i < ARRAY_SIZE(acpi_cstate_map); i++) {
198 if (acpi_cstates[i] > 0 && acpi_cstates[i] < ARRAY_SIZE(cstate_map)) {
199 acpi_cstate_map[count] = cstate_map[acpi_cstates[i]];
200 acpi_cstate_map[count].ctype = i + 1;
201 count++;
202 }
Angel Ponsba5761a2020-10-28 18:50:26 +0100203 }
Angel Pons618b9ad2021-01-21 21:22:19 +0100204 acpigen_write_CST_package(acpi_cstate_map, count);
Angel Pons2aaf7c02020-09-24 18:03:18 +0200205}
206
Aaron Durbin76c37002012-10-30 09:03:43 -0500207static int calculate_power(int tdp, int p1_ratio, int ratio)
208{
209 u32 m;
210 u32 power;
211
212 /*
213 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
214 *
215 * Power = (ratio / p1_ratio) * m * tdp
216 */
217
218 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
219 m = (m * m) / 1000;
220
221 power = ((ratio * 100000 / p1_ratio) / 100);
222 power *= (m / 100) * (tdp / 1000);
223 power /= 1000;
224
225 return (int)power;
226}
227
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100228static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500229{
Aaron Durbin76c37002012-10-30 09:03:43 -0500230 int ratio_min, ratio_max, ratio_turbo, ratio_step;
231 int coord_type, power_max, power_unit, num_entries;
232 int ratio, power, clock, clock_max;
233 msr_t msr;
234
235 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
236 msr = rdmsr(MSR_MISC_PWR_MGMT);
237 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
238 coord_type = SW_ANY;
239 else
240 coord_type = HW_ALL;
241
242 /* Get bus ratio limits and calculate clock speeds */
243 msr = rdmsr(MSR_PLATFORM_INFO);
244 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
245
246 /* Determine if this CPU has configurable TDP */
247 if (cpu_config_tdp_levels()) {
248 /* Set max ratio to nominal TDP ratio */
249 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
250 ratio_max = msr.lo & 0xff;
251 } else {
252 /* Max Non-Turbo Ratio */
253 ratio_max = (msr.lo >> 8) & 0xff;
254 }
Angel Ponsca965492020-10-28 19:15:36 +0100255 clock_max = ratio_max * CPU_BCLK;
Aaron Durbin76c37002012-10-30 09:03:43 -0500256
257 /* Calculate CPU TDP in mW */
258 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
259 power_unit = 2 << ((msr.lo & 0xf) - 1);
260 msr = rdmsr(MSR_PKG_POWER_SKU);
261 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
262
263 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100264 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500265
266 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100267 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500268
269 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100270 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500271
272 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100273 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500274
275 /* Determine ratio points */
276 ratio_step = PSS_RATIO_STEP;
277 num_entries = (ratio_max - ratio_min) / ratio_step;
278 while (num_entries > PSS_MAX_ENTRIES-1) {
279 ratio_step <<= 1;
280 num_entries >>= 1;
281 }
282
283 /* P[T] is Turbo state if enabled */
284 if (get_turbo_state() == TURBO_ENABLED) {
285 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100286 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500287
288 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
289 ratio_turbo = msr.lo & 0xff;
290
291 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100292 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500293 clock_max + 1, /*MHz*/
294 power_max, /*mW*/
295 PSS_LATENCY_TRANSITION, /*lat1*/
296 PSS_LATENCY_BUSMASTER, /*lat2*/
297 ratio_turbo << 8, /*control*/
298 ratio_turbo << 8); /*status*/
299 } else {
300 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100301 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500302 }
303
304 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100305 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500306 clock_max, /*MHz*/
307 power_max, /*mW*/
308 PSS_LATENCY_TRANSITION, /*lat1*/
309 PSS_LATENCY_BUSMASTER, /*lat2*/
310 ratio_max << 8, /*control*/
311 ratio_max << 8); /*status*/
312
313 /* Generate the remaining entries */
314 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
315 ratio >= ratio_min; ratio -= ratio_step) {
316
317 /* Calculate power at this ratio */
318 power = calculate_power(power_max, ratio_max, ratio);
Angel Ponsca965492020-10-28 19:15:36 +0100319 clock = ratio * CPU_BCLK;
Aaron Durbin76c37002012-10-30 09:03:43 -0500320
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100321 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500322 clock, /*MHz*/
323 power, /*mW*/
324 PSS_LATENCY_TRANSITION, /*lat1*/
325 PSS_LATENCY_BUSMASTER, /*lat2*/
326 ratio << 8, /*control*/
327 ratio << 8); /*status*/
328 }
329
330 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100331 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500332}
333
Furquan Shaikh7536a392020-04-24 21:59:21 -0700334void generate_cpu_entries(const struct device *device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500335{
Duncan Laurie1ad55642013-03-07 14:08:04 -0800336 int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6;
Aaron Durbin76c37002012-10-30 09:03:43 -0500337 int totalcores = dev_count_cpu();
Angel Pons11235d62021-01-04 17:56:44 +0100338 int cores_per_package = get_logical_cores_per_package();
Aaron Durbin76c37002012-10-30 09:03:43 -0500339 int numcpus = totalcores/cores_per_package;
340
341 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
342 numcpus, cores_per_package);
343
Martin Roth9944b282014-08-11 11:24:55 -0600344 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700345 for (coreID = 1; coreID <= cores_per_package; coreID++) {
346 if (coreID > 1) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500347 pcontrol_blk = 0;
348 plen = 0;
349 }
350
Christian Walterbe3979c2019-12-18 15:07:59 +0100351 /* Generate processor \_SB.CPUx */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100352 acpigen_write_processor(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200353 (cpuID - 1) * cores_per_package+coreID - 1,
Aaron Durbin76c37002012-10-30 09:03:43 -0500354 pcontrol_blk, plen);
355
356 /* Generate P-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100357 generate_P_state_entries(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200358 coreID - 1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500359
360 /* Generate C-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100361 generate_C_state_entries();
Aaron Durbin76c37002012-10-30 09:03:43 -0500362
363 /* Generate T-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100364 generate_T_state_entries(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200365 cpuID - 1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500366
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100367 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500368 }
369 }
Arthur Heymansc54d14f2018-11-28 12:09:23 +0100370
371 /* PPKG is usually used for thermal management
372 of the first and only package. */
373 acpigen_write_processor_package("PPKG", 0, cores_per_package);
374
375 /* Add a method to notify processor nodes */
376 acpigen_write_processor_cnot(cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500377}
378
379struct chip_operations cpu_intel_haswell_ops = {
380 CHIP_NAME("Intel Haswell CPU")
381};