blob: c8f6a07a006b353d315840cbf508b39c4dcb1119 [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
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi.h>
4#include <acpi/acpigen.h>
Elyes Haouasad65e8c2022-10-31 14:02:13 +01005#include <console/console.h>
6#include <cpu/cpu.h>
Aaron Durbin76c37002012-10-30 09:03:43 -05007#include <cpu/intel/speedstep.h>
8#include <cpu/intel/turbo.h>
Elyes Haouasad65e8c2022-10-31 14:02:13 +01009#include <cpu/x86/msr.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050010#include <device/device.h>
Elyes Haouasad65e8c2022-10-31 14:02:13 +010011#include <types.h>
12
Aaron Durbin76c37002012-10-30 09:03:43 -050013#include "haswell.h"
14#include "chip.h"
15
Duncan Laurie1ad55642013-03-07 14:08:04 -080016#include <southbridge/intel/lynxpoint/pch.h>
17
Angel Pons618b9ad2021-01-21 21:22:19 +010018#define MWAIT_RES(state, sub_state) \
19 { \
20 .addrl = (((state) << 4) | (sub_state)), \
21 .space_id = ACPI_ADDRESS_SPACE_FIXED, \
22 .bit_width = ACPI_FFIXEDHW_VENDOR_INTEL, \
23 .bit_offset = ACPI_FFIXEDHW_CLASS_MWAIT, \
24 .access_size = ACPI_FFIXEDHW_FLAG_HW_COORD, \
25 }
26
27static acpi_cstate_t cstate_map[NUM_C_STATES] = {
28 [C_STATE_C0] = { },
29 [C_STATE_C1] = {
30 .latency = 0,
31 .power = 1000,
32 .resource = MWAIT_RES(0, 0),
33 },
34 [C_STATE_C1E] = {
35 .latency = 0,
36 .power = 1000,
37 .resource = MWAIT_RES(0, 1),
38 },
39 [C_STATE_C3] = {
40 .latency = C_STATE_LATENCY_FROM_LAT_REG(0),
41 .power = 900,
42 .resource = MWAIT_RES(1, 0),
43 },
44 [C_STATE_C6_SHORT_LAT] = {
45 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
46 .power = 800,
47 .resource = MWAIT_RES(2, 0),
48 },
49 [C_STATE_C6_LONG_LAT] = {
50 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
51 .power = 800,
52 .resource = MWAIT_RES(2, 1),
53 },
54 [C_STATE_C7_SHORT_LAT] = {
55 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
56 .power = 700,
57 .resource = MWAIT_RES(3, 0),
58 },
59 [C_STATE_C7_LONG_LAT] = {
60 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
61 .power = 700,
62 .resource = MWAIT_RES(3, 1),
63 },
64 [C_STATE_C7S_SHORT_LAT] = {
65 .latency = C_STATE_LATENCY_FROM_LAT_REG(1),
66 .power = 700,
67 .resource = MWAIT_RES(3, 2),
68 },
69 [C_STATE_C7S_LONG_LAT] = {
70 .latency = C_STATE_LATENCY_FROM_LAT_REG(2),
71 .power = 700,
72 .resource = MWAIT_RES(3, 3),
73 },
74 [C_STATE_C8] = {
75 .latency = C_STATE_LATENCY_FROM_LAT_REG(3),
76 .power = 600,
77 .resource = MWAIT_RES(4, 0),
78 },
79 [C_STATE_C9] = {
80 .latency = C_STATE_LATENCY_FROM_LAT_REG(4),
81 .power = 500,
82 .resource = MWAIT_RES(5, 0),
83 },
84 [C_STATE_C10] = {
85 .latency = C_STATE_LATENCY_FROM_LAT_REG(5),
86 .power = 400,
87 .resource = MWAIT_RES(6, 0),
88 },
89};
90
Angel Ponse49dec42021-01-21 21:54:14 +010091static const int cstate_set_s0ix[3] = {
Angel Pons8e6f1622020-10-29 00:18:11 +010092 C_STATE_C1E,
93 C_STATE_C7S_LONG_LAT,
94 C_STATE_C10,
95};
96
Angel Ponse49dec42021-01-21 21:54:14 +010097static const int cstate_set_lp[3] = {
Angel Ponsbda1c552020-10-29 00:08:24 +010098 C_STATE_C1E,
99 C_STATE_C3,
100 C_STATE_C7S_LONG_LAT,
Angel Ponsba5761a2020-10-28 18:50:26 +0100101};
102
Angel Ponse49dec42021-01-21 21:54:14 +0100103static const int cstate_set_trad[3] = {
Angel Ponsbda1c552020-10-29 00:08:24 +0100104 C_STATE_C1,
105 C_STATE_C3,
106 C_STATE_C6_LONG_LAT,
Angel Ponsba5761a2020-10-28 18:50:26 +0100107};
108
Angel Pons11235d62021-01-04 17:56:44 +0100109static int get_logical_cores_per_package(void)
Aaron Durbin76c37002012-10-30 09:03:43 -0500110{
Angel Pons11235d62021-01-04 17:56:44 +0100111 msr_t msr = rdmsr(MSR_CORE_THREAD_COUNT);
112 return msr.lo & 0xffff;
Aaron Durbin76c37002012-10-30 09:03:43 -0500113}
114
Aaron Durbin76c37002012-10-30 09:03:43 -0500115static acpi_tstate_t tss_table_fine[] = {
116 { 100, 1000, 0, 0x00, 0 },
117 { 94, 940, 0, 0x1f, 0 },
118 { 88, 880, 0, 0x1e, 0 },
119 { 82, 820, 0, 0x1d, 0 },
120 { 75, 760, 0, 0x1c, 0 },
121 { 69, 700, 0, 0x1b, 0 },
122 { 63, 640, 0, 0x1a, 0 },
123 { 57, 580, 0, 0x19, 0 },
124 { 50, 520, 0, 0x18, 0 },
125 { 44, 460, 0, 0x17, 0 },
126 { 38, 400, 0, 0x16, 0 },
127 { 32, 340, 0, 0x15, 0 },
128 { 25, 280, 0, 0x14, 0 },
129 { 19, 220, 0, 0x13, 0 },
130 { 13, 160, 0, 0x12, 0 },
131};
132
133static acpi_tstate_t tss_table_coarse[] = {
134 { 100, 1000, 0, 0x00, 0 },
135 { 88, 875, 0, 0x1f, 0 },
136 { 75, 750, 0, 0x1e, 0 },
137 { 63, 625, 0, 0x1d, 0 },
138 { 50, 500, 0, 0x1c, 0 },
139 { 38, 375, 0, 0x1b, 0 },
140 { 25, 250, 0, 0x1a, 0 },
141 { 13, 125, 0, 0x19, 0 },
142};
143
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100144static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500145{
Aaron Durbin76c37002012-10-30 09:03:43 -0500146 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100147 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500148
149 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100150 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500151
152 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100153 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500154
155 /*
156 * CPUID.(EAX=6):EAX[5] indicates support
157 * for extended throttle levels.
158 */
159 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100160 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500161 ARRAY_SIZE(tss_table_fine), tss_table_fine);
162 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100163 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500164 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500165}
166
Arthur Heymansdd96ab62021-11-15 20:11:12 +0100167static bool is_s0ix_enabled(const struct device *dev)
Angel Pons8e6f1622020-10-29 00:18:11 +0100168{
169 if (!haswell_is_ult())
170 return false;
171
Arthur Heymansdd96ab62021-11-15 20:11:12 +0100172 const struct cpu_intel_haswell_config *conf = dev->chip_info;
Angel Pons8e6f1622020-10-29 00:18:11 +0100173 return conf->s0ix_enable;
174}
175
Arthur Heymansdd96ab62021-11-15 20:11:12 +0100176static void generate_C_state_entries(const struct device *dev)
Angel Pons2aaf7c02020-09-24 18:03:18 +0200177{
Angel Pons618b9ad2021-01-21 21:22:19 +0100178 acpi_cstate_t acpi_cstate_map[3] = {0};
Angel Ponsba5761a2020-10-28 18:50:26 +0100179
Angel Ponse49dec42021-01-21 21:54:14 +0100180 const int *acpi_cstates;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200181
Arthur Heymansdd96ab62021-11-15 20:11:12 +0100182 if (is_s0ix_enabled(dev))
Angel Pons618b9ad2021-01-21 21:22:19 +0100183 acpi_cstates = cstate_set_s0ix;
Angel Pons8e6f1622020-10-29 00:18:11 +0100184 else if (haswell_is_ult())
Angel Pons618b9ad2021-01-21 21:22:19 +0100185 acpi_cstates = cstate_set_lp;
Angel Ponsba5761a2020-10-28 18:50:26 +0100186 else
Angel Pons618b9ad2021-01-21 21:22:19 +0100187 acpi_cstates = cstate_set_trad;
Angel Pons2aaf7c02020-09-24 18:03:18 +0200188
Angel Pons618b9ad2021-01-21 21:22:19 +0100189 /* Count number of active C-states */
190 int count = 0;
191
192 for (int i = 0; i < ARRAY_SIZE(acpi_cstate_map); i++) {
193 if (acpi_cstates[i] > 0 && acpi_cstates[i] < ARRAY_SIZE(cstate_map)) {
194 acpi_cstate_map[count] = cstate_map[acpi_cstates[i]];
195 acpi_cstate_map[count].ctype = i + 1;
196 count++;
197 }
Angel Ponsba5761a2020-10-28 18:50:26 +0100198 }
Angel Pons618b9ad2021-01-21 21:22:19 +0100199 acpigen_write_CST_package(acpi_cstate_map, count);
Angel Pons2aaf7c02020-09-24 18:03:18 +0200200}
201
Aaron Durbin76c37002012-10-30 09:03:43 -0500202static int calculate_power(int tdp, int p1_ratio, int ratio)
203{
204 u32 m;
205 u32 power;
206
207 /*
208 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
209 *
210 * Power = (ratio / p1_ratio) * m * tdp
211 */
212
213 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
214 m = (m * m) / 1000;
215
216 power = ((ratio * 100000 / p1_ratio) / 100);
217 power *= (m / 100) * (tdp / 1000);
218 power /= 1000;
219
220 return (int)power;
221}
222
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100223static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500224{
Aaron Durbin76c37002012-10-30 09:03:43 -0500225 int ratio_min, ratio_max, ratio_turbo, ratio_step;
226 int coord_type, power_max, power_unit, num_entries;
227 int ratio, power, clock, clock_max;
228 msr_t msr;
229
230 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
231 msr = rdmsr(MSR_MISC_PWR_MGMT);
232 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
233 coord_type = SW_ANY;
234 else
235 coord_type = HW_ALL;
236
237 /* Get bus ratio limits and calculate clock speeds */
238 msr = rdmsr(MSR_PLATFORM_INFO);
239 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
240
241 /* Determine if this CPU has configurable TDP */
242 if (cpu_config_tdp_levels()) {
243 /* Set max ratio to nominal TDP ratio */
244 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
245 ratio_max = msr.lo & 0xff;
246 } else {
247 /* Max Non-Turbo Ratio */
248 ratio_max = (msr.lo >> 8) & 0xff;
249 }
Angel Ponsca965492020-10-28 19:15:36 +0100250 clock_max = ratio_max * CPU_BCLK;
Aaron Durbin76c37002012-10-30 09:03:43 -0500251
252 /* Calculate CPU TDP in mW */
253 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
254 power_unit = 2 << ((msr.lo & 0xf) - 1);
255 msr = rdmsr(MSR_PKG_POWER_SKU);
256 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
257
258 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100259 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500260
261 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100262 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500263
264 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100265 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500266
267 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100268 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500269
270 /* Determine ratio points */
271 ratio_step = PSS_RATIO_STEP;
272 num_entries = (ratio_max - ratio_min) / ratio_step;
273 while (num_entries > PSS_MAX_ENTRIES-1) {
274 ratio_step <<= 1;
275 num_entries >>= 1;
276 }
277
278 /* P[T] is Turbo state if enabled */
279 if (get_turbo_state() == TURBO_ENABLED) {
280 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100281 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500282
283 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
284 ratio_turbo = msr.lo & 0xff;
285
286 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100287 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500288 clock_max + 1, /*MHz*/
289 power_max, /*mW*/
290 PSS_LATENCY_TRANSITION, /*lat1*/
291 PSS_LATENCY_BUSMASTER, /*lat2*/
292 ratio_turbo << 8, /*control*/
293 ratio_turbo << 8); /*status*/
294 } else {
295 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100296 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500297 }
298
299 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100300 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500301 clock_max, /*MHz*/
302 power_max, /*mW*/
303 PSS_LATENCY_TRANSITION, /*lat1*/
304 PSS_LATENCY_BUSMASTER, /*lat2*/
305 ratio_max << 8, /*control*/
306 ratio_max << 8); /*status*/
307
308 /* Generate the remaining entries */
309 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
310 ratio >= ratio_min; ratio -= ratio_step) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500311 /* Calculate power at this ratio */
312 power = calculate_power(power_max, ratio_max, ratio);
Angel Ponsca965492020-10-28 19:15:36 +0100313 clock = ratio * CPU_BCLK;
Aaron Durbin76c37002012-10-30 09:03:43 -0500314
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100315 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500316 clock, /*MHz*/
317 power, /*mW*/
318 PSS_LATENCY_TRANSITION, /*lat1*/
319 PSS_LATENCY_BUSMASTER, /*lat2*/
320 ratio << 8, /*control*/
321 ratio << 8); /*status*/
322 }
323
324 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100325 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500326}
327
Kyösti Mälkkid521b962023-04-12 21:44:49 +0300328static void generate_cpu_entry(const struct device *device, int cpu, int core, int cores_per_package)
329{
330 /* Generate Scope(\_SB) { Device(CPUx */
331 acpigen_write_processor_device(cpu * cores_per_package + core);
332
333 /* Generate P-state tables */
334 generate_P_state_entries(core, cores_per_package);
335
336 /* Generate C-state tables */
337 generate_C_state_entries(device);
338
339 /* Generate T-state tables */
340 generate_T_state_entries(cpu, cores_per_package);
341
342 acpigen_write_processor_device_end();
343}
344
Furquan Shaikh7536a392020-04-24 21:59:21 -0700345void generate_cpu_entries(const struct device *device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500346{
Aaron Durbin76c37002012-10-30 09:03:43 -0500347 int totalcores = dev_count_cpu();
Angel Pons11235d62021-01-04 17:56:44 +0100348 int cores_per_package = get_logical_cores_per_package();
Kyösti Mälkkie39a3e32023-04-12 16:39:12 +0300349 int numcpus = totalcores / cores_per_package;
Aaron Durbin76c37002012-10-30 09:03:43 -0500350
351 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
352 numcpus, cores_per_package);
353
Kyösti Mälkkid521b962023-04-12 21:44:49 +0300354 for (int cpu_id = 0; cpu_id < numcpus; cpu_id++)
355 for (int core_id = 0; core_id < cores_per_package; core_id++)
356 generate_cpu_entry(device, cpu_id, core_id, cores_per_package);
Arthur Heymansc54d14f2018-11-28 12:09:23 +0100357
358 /* PPKG is usually used for thermal management
359 of the first and only package. */
360 acpigen_write_processor_package("PPKG", 0, cores_per_package);
361
362 /* Add a method to notify processor nodes */
363 acpigen_write_processor_cnot(cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500364}
365
366struct chip_operations cpu_intel_haswell_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900367 .name = "Intel Haswell CPU",
Aaron Durbin76c37002012-10-30 09:03:43 -0500368};