blob: 6dd8559d2251b37d5dd97179750662dbbf2b5c33 [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
Aaron Durbin76c37002012-10-30 09:03:43 -050017static int get_cores_per_package(void)
18{
19 struct cpuinfo_x86 c;
20 struct cpuid_result result;
21 int cores = 1;
22
23 get_fms(&c, cpuid_eax(1));
24 if (c.x86 != 6)
25 return 1;
26
27 result = cpuid_ext(0xb, 1);
28 cores = result.ebx & 0xff;
29
30 return cores;
31}
32
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010033static void generate_cstate_entries(acpi_cstate_t *cstates,
Aaron Durbin76c37002012-10-30 09:03:43 -050034 int c1, int c2, int c3)
35{
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010036 int cstate_count = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050037
38 /* Count number of active C-states */
39 if (c1 > 0)
40 ++cstate_count;
41 if (c2 > 0)
42 ++cstate_count;
43 if (c3 > 0)
44 ++cstate_count;
45 if (!cstate_count)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010046 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050047
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010048 acpigen_write_package(cstate_count + 1);
49 acpigen_write_byte(cstate_count);
Aaron Durbin76c37002012-10-30 09:03:43 -050050
51 /* Add an entry if the level is enabled */
52 if (c1 > 0) {
53 cstates[c1].ctype = 1;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010054 acpigen_write_CST_package_entry(&cstates[c1]);
Aaron Durbin76c37002012-10-30 09:03:43 -050055 }
56 if (c2 > 0) {
57 cstates[c2].ctype = 2;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010058 acpigen_write_CST_package_entry(&cstates[c2]);
Aaron Durbin76c37002012-10-30 09:03:43 -050059 }
60 if (c3 > 0) {
61 cstates[c3].ctype = 3;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010062 acpigen_write_CST_package_entry(&cstates[c3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050063 }
64
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010065 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -050066}
67
Aaron Durbin76c37002012-10-30 09:03:43 -050068static acpi_tstate_t tss_table_fine[] = {
69 { 100, 1000, 0, 0x00, 0 },
70 { 94, 940, 0, 0x1f, 0 },
71 { 88, 880, 0, 0x1e, 0 },
72 { 82, 820, 0, 0x1d, 0 },
73 { 75, 760, 0, 0x1c, 0 },
74 { 69, 700, 0, 0x1b, 0 },
75 { 63, 640, 0, 0x1a, 0 },
76 { 57, 580, 0, 0x19, 0 },
77 { 50, 520, 0, 0x18, 0 },
78 { 44, 460, 0, 0x17, 0 },
79 { 38, 400, 0, 0x16, 0 },
80 { 32, 340, 0, 0x15, 0 },
81 { 25, 280, 0, 0x14, 0 },
82 { 19, 220, 0, 0x13, 0 },
83 { 13, 160, 0, 0x12, 0 },
84};
85
86static acpi_tstate_t tss_table_coarse[] = {
87 { 100, 1000, 0, 0x00, 0 },
88 { 88, 875, 0, 0x1f, 0 },
89 { 75, 750, 0, 0x1e, 0 },
90 { 63, 625, 0, 0x1d, 0 },
91 { 50, 500, 0, 0x1c, 0 },
92 { 38, 375, 0, 0x1b, 0 },
93 { 25, 250, 0, 0x1a, 0 },
94 { 13, 125, 0, 0x19, 0 },
95};
96
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010097static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -050098{
Aaron Durbin76c37002012-10-30 09:03:43 -050099 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100100 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500101
102 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100103 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500104
105 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100106 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500107
108 /*
109 * CPUID.(EAX=6):EAX[5] indicates support
110 * for extended throttle levels.
111 */
112 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100113 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500114 ARRAY_SIZE(tss_table_fine), tss_table_fine);
115 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100116 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500117 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500118}
119
Angel Pons2aaf7c02020-09-24 18:03:18 +0200120static void generate_C_state_entries(void)
121{
122 struct cpu_info *info;
123 struct cpu_driver *cpu;
124 struct device *lapic;
125 struct cpu_intel_haswell_config *conf = NULL;
126
127 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
128 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
129 if (!lapic)
130 return;
131 conf = lapic->chip_info;
132 if (!conf)
133 return;
134
135 /* Find CPU map of supported C-states */
136 info = cpu_info();
137 if (!info)
138 return;
139 cpu = find_cpu_driver(info->cpu);
140 if (!cpu || !cpu->cstates)
141 return;
142
143 acpigen_emit_byte(0x14); /* MethodOp */
144 acpigen_write_len_f(); /* PkgLength */
145 acpigen_emit_namestring("_CST");
146 acpigen_emit_byte(0x00); /* No Arguments */
147
148 /* If running on AC power */
149 acpigen_emit_byte(0xa0); /* IfOp */
150 acpigen_write_len_f(); /* PkgLength */
151 acpigen_emit_namestring("PWRS");
152 acpigen_emit_byte(0xa4); /* ReturnOp */
153 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
154 conf->c2_acpower, conf->c3_acpower);
155 acpigen_pop_len();
156
157 /* Else on battery power */
158 acpigen_emit_byte(0xa4); /* ReturnOp */
159 generate_cstate_entries(cpu->cstates, conf->c1_battery,
160 conf->c2_battery, conf->c3_battery);
161 acpigen_pop_len();
162}
163
Aaron Durbin76c37002012-10-30 09:03:43 -0500164static int calculate_power(int tdp, int p1_ratio, int ratio)
165{
166 u32 m;
167 u32 power;
168
169 /*
170 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
171 *
172 * Power = (ratio / p1_ratio) * m * tdp
173 */
174
175 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
176 m = (m * m) / 1000;
177
178 power = ((ratio * 100000 / p1_ratio) / 100);
179 power *= (m / 100) * (tdp / 1000);
180 power /= 1000;
181
182 return (int)power;
183}
184
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100185static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500186{
Aaron Durbin76c37002012-10-30 09:03:43 -0500187 int ratio_min, ratio_max, ratio_turbo, ratio_step;
188 int coord_type, power_max, power_unit, num_entries;
189 int ratio, power, clock, clock_max;
190 msr_t msr;
191
192 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
193 msr = rdmsr(MSR_MISC_PWR_MGMT);
194 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
195 coord_type = SW_ANY;
196 else
197 coord_type = HW_ALL;
198
199 /* Get bus ratio limits and calculate clock speeds */
200 msr = rdmsr(MSR_PLATFORM_INFO);
201 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
202
203 /* Determine if this CPU has configurable TDP */
204 if (cpu_config_tdp_levels()) {
205 /* Set max ratio to nominal TDP ratio */
206 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
207 ratio_max = msr.lo & 0xff;
208 } else {
209 /* Max Non-Turbo Ratio */
210 ratio_max = (msr.lo >> 8) & 0xff;
211 }
212 clock_max = ratio_max * HASWELL_BCLK;
213
214 /* Calculate CPU TDP in mW */
215 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
216 power_unit = 2 << ((msr.lo & 0xf) - 1);
217 msr = rdmsr(MSR_PKG_POWER_SKU);
218 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
219
220 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100221 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500222
223 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100224 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500225
226 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100227 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500228
229 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100230 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500231
232 /* Determine ratio points */
233 ratio_step = PSS_RATIO_STEP;
234 num_entries = (ratio_max - ratio_min) / ratio_step;
235 while (num_entries > PSS_MAX_ENTRIES-1) {
236 ratio_step <<= 1;
237 num_entries >>= 1;
238 }
239
240 /* P[T] is Turbo state if enabled */
241 if (get_turbo_state() == TURBO_ENABLED) {
242 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100243 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500244
245 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
246 ratio_turbo = msr.lo & 0xff;
247
248 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100249 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500250 clock_max + 1, /*MHz*/
251 power_max, /*mW*/
252 PSS_LATENCY_TRANSITION, /*lat1*/
253 PSS_LATENCY_BUSMASTER, /*lat2*/
254 ratio_turbo << 8, /*control*/
255 ratio_turbo << 8); /*status*/
256 } else {
257 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100258 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500259 }
260
261 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100262 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500263 clock_max, /*MHz*/
264 power_max, /*mW*/
265 PSS_LATENCY_TRANSITION, /*lat1*/
266 PSS_LATENCY_BUSMASTER, /*lat2*/
267 ratio_max << 8, /*control*/
268 ratio_max << 8); /*status*/
269
270 /* Generate the remaining entries */
271 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
272 ratio >= ratio_min; ratio -= ratio_step) {
273
274 /* Calculate power at this ratio */
275 power = calculate_power(power_max, ratio_max, ratio);
276 clock = ratio * HASWELL_BCLK;
277
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100278 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500279 clock, /*MHz*/
280 power, /*mW*/
281 PSS_LATENCY_TRANSITION, /*lat1*/
282 PSS_LATENCY_BUSMASTER, /*lat2*/
283 ratio << 8, /*control*/
284 ratio << 8); /*status*/
285 }
286
287 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100288 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500289}
290
Furquan Shaikh7536a392020-04-24 21:59:21 -0700291void generate_cpu_entries(const struct device *device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500292{
Duncan Laurie1ad55642013-03-07 14:08:04 -0800293 int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6;
Aaron Durbin76c37002012-10-30 09:03:43 -0500294 int totalcores = dev_count_cpu();
295 int cores_per_package = get_cores_per_package();
296 int numcpus = totalcores/cores_per_package;
297
298 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
299 numcpus, cores_per_package);
300
Martin Roth9944b282014-08-11 11:24:55 -0600301 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700302 for (coreID = 1; coreID <= cores_per_package; coreID++) {
303 if (coreID > 1) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500304 pcontrol_blk = 0;
305 plen = 0;
306 }
307
Christian Walterbe3979c2019-12-18 15:07:59 +0100308 /* Generate processor \_SB.CPUx */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100309 acpigen_write_processor(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200310 (cpuID - 1) * cores_per_package+coreID - 1,
Aaron Durbin76c37002012-10-30 09:03:43 -0500311 pcontrol_blk, plen);
312
313 /* Generate P-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100314 generate_P_state_entries(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200315 coreID - 1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500316
317 /* Generate C-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100318 generate_C_state_entries();
Aaron Durbin76c37002012-10-30 09:03:43 -0500319
320 /* Generate T-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100321 generate_T_state_entries(
Angel Pons2aaf7c02020-09-24 18:03:18 +0200322 cpuID - 1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500323
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100324 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500325 }
326 }
Arthur Heymansc54d14f2018-11-28 12:09:23 +0100327
328 /* PPKG is usually used for thermal management
329 of the first and only package. */
330 acpigen_write_processor_package("PPKG", 0, cores_per_package);
331
332 /* Add a method to notify processor nodes */
333 acpigen_write_processor_cnot(cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500334}
335
336struct chip_operations cpu_intel_haswell_ops = {
337 CHIP_NAME("Intel Haswell CPU")
338};