blob: fe2add772a73ddd885ecdcd999608830a1dbbdc0 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbin76c37002012-10-30 09:03:43 -05004 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; version 2 of
7 * the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050013 */
14
15#include <types.h>
16#include <console/console.h>
17#include <arch/acpi.h>
18#include <arch/acpigen.h>
19#include <arch/cpu.h>
20#include <cpu/x86/msr.h>
21#include <cpu/intel/speedstep.h>
22#include <cpu/intel/turbo.h>
23#include <device/device.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include "haswell.h"
25#include "chip.h"
26
Duncan Laurie1ad55642013-03-07 14:08:04 -080027#include <southbridge/intel/lynxpoint/pch.h>
28
Aaron Durbin76c37002012-10-30 09:03:43 -050029static int get_cores_per_package(void)
30{
31 struct cpuinfo_x86 c;
32 struct cpuid_result result;
33 int cores = 1;
34
35 get_fms(&c, cpuid_eax(1));
36 if (c.x86 != 6)
37 return 1;
38
39 result = cpuid_ext(0xb, 1);
40 cores = result.ebx & 0xff;
41
42 return cores;
43}
44
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010045static void generate_cstate_entries(acpi_cstate_t *cstates,
Aaron Durbin76c37002012-10-30 09:03:43 -050046 int c1, int c2, int c3)
47{
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010048 int cstate_count = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050049
50 /* Count number of active C-states */
51 if (c1 > 0)
52 ++cstate_count;
53 if (c2 > 0)
54 ++cstate_count;
55 if (c3 > 0)
56 ++cstate_count;
57 if (!cstate_count)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010058 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050059
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010060 acpigen_write_package(cstate_count + 1);
61 acpigen_write_byte(cstate_count);
Aaron Durbin76c37002012-10-30 09:03:43 -050062
63 /* Add an entry if the level is enabled */
64 if (c1 > 0) {
65 cstates[c1].ctype = 1;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010066 acpigen_write_CST_package_entry(&cstates[c1]);
Aaron Durbin76c37002012-10-30 09:03:43 -050067 }
68 if (c2 > 0) {
69 cstates[c2].ctype = 2;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010070 acpigen_write_CST_package_entry(&cstates[c2]);
Aaron Durbin76c37002012-10-30 09:03:43 -050071 }
72 if (c3 > 0) {
73 cstates[c3].ctype = 3;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010074 acpigen_write_CST_package_entry(&cstates[c3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050075 }
76
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010077 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -050078}
79
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010080static void generate_C_state_entries(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050081{
82 struct cpu_info *info;
83 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110084 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -050085 struct cpu_intel_haswell_config *conf = NULL;
86
87 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
88 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
89 if (!lapic)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010090 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050091 conf = lapic->chip_info;
92 if (!conf)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010093 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050094
95 /* Find CPU map of supported C-states */
96 info = cpu_info();
97 if (!info)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010098 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050099 cpu = find_cpu_driver(info->cpu);
100 if (!cpu || !cpu->cstates)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100101 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500102
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100103 acpigen_emit_byte(0x14); /* MethodOp */
104 acpigen_write_len_f(); /* PkgLength */
105 acpigen_emit_namestring("_CST");
106 acpigen_emit_byte(0x00); /* No Arguments */
Aaron Durbin76c37002012-10-30 09:03:43 -0500107
108 /* If running on AC power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100109 acpigen_emit_byte(0xa0); /* IfOp */
110 acpigen_write_len_f(); /* PkgLength */
111 acpigen_emit_namestring("PWRS");
112 acpigen_emit_byte(0xa4); /* ReturnOp */
113 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
Aaron Durbin76c37002012-10-30 09:03:43 -0500114 conf->c2_acpower, conf->c3_acpower);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100115 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500116
117 /* Else on battery power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100118 acpigen_emit_byte(0xa4); /* ReturnOp */
119 generate_cstate_entries(cpu->cstates, conf->c1_battery,
Aaron Durbin76c37002012-10-30 09:03:43 -0500120 conf->c2_battery, conf->c3_battery);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100121 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500122}
123
124static acpi_tstate_t tss_table_fine[] = {
125 { 100, 1000, 0, 0x00, 0 },
126 { 94, 940, 0, 0x1f, 0 },
127 { 88, 880, 0, 0x1e, 0 },
128 { 82, 820, 0, 0x1d, 0 },
129 { 75, 760, 0, 0x1c, 0 },
130 { 69, 700, 0, 0x1b, 0 },
131 { 63, 640, 0, 0x1a, 0 },
132 { 57, 580, 0, 0x19, 0 },
133 { 50, 520, 0, 0x18, 0 },
134 { 44, 460, 0, 0x17, 0 },
135 { 38, 400, 0, 0x16, 0 },
136 { 32, 340, 0, 0x15, 0 },
137 { 25, 280, 0, 0x14, 0 },
138 { 19, 220, 0, 0x13, 0 },
139 { 13, 160, 0, 0x12, 0 },
140};
141
142static acpi_tstate_t tss_table_coarse[] = {
143 { 100, 1000, 0, 0x00, 0 },
144 { 88, 875, 0, 0x1f, 0 },
145 { 75, 750, 0, 0x1e, 0 },
146 { 63, 625, 0, 0x1d, 0 },
147 { 50, 500, 0, 0x1c, 0 },
148 { 38, 375, 0, 0x1b, 0 },
149 { 25, 250, 0, 0x1a, 0 },
150 { 13, 125, 0, 0x19, 0 },
151};
152
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100153static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500154{
Aaron Durbin76c37002012-10-30 09:03:43 -0500155 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100156 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500157
158 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100159 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500160
161 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100162 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500163
164 /*
165 * CPUID.(EAX=6):EAX[5] indicates support
166 * for extended throttle levels.
167 */
168 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100169 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500170 ARRAY_SIZE(tss_table_fine), tss_table_fine);
171 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100172 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500173 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500174}
175
176static int calculate_power(int tdp, int p1_ratio, int ratio)
177{
178 u32 m;
179 u32 power;
180
181 /*
182 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
183 *
184 * Power = (ratio / p1_ratio) * m * tdp
185 */
186
187 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
188 m = (m * m) / 1000;
189
190 power = ((ratio * 100000 / p1_ratio) / 100);
191 power *= (m / 100) * (tdp / 1000);
192 power /= 1000;
193
194 return (int)power;
195}
196
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100197static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500198{
Aaron Durbin76c37002012-10-30 09:03:43 -0500199 int ratio_min, ratio_max, ratio_turbo, ratio_step;
200 int coord_type, power_max, power_unit, num_entries;
201 int ratio, power, clock, clock_max;
202 msr_t msr;
203
204 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
205 msr = rdmsr(MSR_MISC_PWR_MGMT);
206 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
207 coord_type = SW_ANY;
208 else
209 coord_type = HW_ALL;
210
211 /* Get bus ratio limits and calculate clock speeds */
212 msr = rdmsr(MSR_PLATFORM_INFO);
213 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
214
215 /* Determine if this CPU has configurable TDP */
216 if (cpu_config_tdp_levels()) {
217 /* Set max ratio to nominal TDP ratio */
218 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
219 ratio_max = msr.lo & 0xff;
220 } else {
221 /* Max Non-Turbo Ratio */
222 ratio_max = (msr.lo >> 8) & 0xff;
223 }
224 clock_max = ratio_max * HASWELL_BCLK;
225
226 /* Calculate CPU TDP in mW */
227 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
228 power_unit = 2 << ((msr.lo & 0xf) - 1);
229 msr = rdmsr(MSR_PKG_POWER_SKU);
230 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
231
232 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100233 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500234
235 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100236 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500237
238 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100239 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500240
241 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100242 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500243
244 /* Determine ratio points */
245 ratio_step = PSS_RATIO_STEP;
246 num_entries = (ratio_max - ratio_min) / ratio_step;
247 while (num_entries > PSS_MAX_ENTRIES-1) {
248 ratio_step <<= 1;
249 num_entries >>= 1;
250 }
251
252 /* P[T] is Turbo state if enabled */
253 if (get_turbo_state() == TURBO_ENABLED) {
254 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100255 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500256
257 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
258 ratio_turbo = msr.lo & 0xff;
259
260 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100261 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500262 clock_max + 1, /*MHz*/
263 power_max, /*mW*/
264 PSS_LATENCY_TRANSITION, /*lat1*/
265 PSS_LATENCY_BUSMASTER, /*lat2*/
266 ratio_turbo << 8, /*control*/
267 ratio_turbo << 8); /*status*/
268 } else {
269 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100270 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500271 }
272
273 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100274 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500275 clock_max, /*MHz*/
276 power_max, /*mW*/
277 PSS_LATENCY_TRANSITION, /*lat1*/
278 PSS_LATENCY_BUSMASTER, /*lat2*/
279 ratio_max << 8, /*control*/
280 ratio_max << 8); /*status*/
281
282 /* Generate the remaining entries */
283 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
284 ratio >= ratio_min; ratio -= ratio_step) {
285
286 /* Calculate power at this ratio */
287 power = calculate_power(power_max, ratio_max, ratio);
288 clock = ratio * HASWELL_BCLK;
289
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100290 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500291 clock, /*MHz*/
292 power, /*mW*/
293 PSS_LATENCY_TRANSITION, /*lat1*/
294 PSS_LATENCY_BUSMASTER, /*lat2*/
295 ratio << 8, /*control*/
296 ratio << 8); /*status*/
297 }
298
299 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100300 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500301}
302
Elyes HAOUASf925c562018-02-08 13:05:16 +0100303void generate_cpu_entries(struct device *device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500304{
Duncan Laurie1ad55642013-03-07 14:08:04 -0800305 int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6;
Aaron Durbin76c37002012-10-30 09:03:43 -0500306 int totalcores = dev_count_cpu();
307 int cores_per_package = get_cores_per_package();
308 int numcpus = totalcores/cores_per_package;
309
310 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
311 numcpus, cores_per_package);
312
Martin Roth9944b282014-08-11 11:24:55 -0600313 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700314 for (coreID = 1; coreID <= cores_per_package; coreID++) {
315 if (coreID > 1) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500316 pcontrol_blk = 0;
317 plen = 0;
318 }
319
Christian Walterbe3979c2019-12-18 15:07:59 +0100320 /* Generate processor \_SB.CPUx */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100321 acpigen_write_processor(
Aaron Durbin76c37002012-10-30 09:03:43 -0500322 (cpuID-1)*cores_per_package+coreID-1,
323 pcontrol_blk, plen);
324
325 /* Generate P-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100326 generate_P_state_entries(
Duncan Laurie25b8b7b2013-04-19 10:02:23 -0700327 coreID-1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500328
329 /* Generate C-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100330 generate_C_state_entries();
Aaron Durbin76c37002012-10-30 09:03:43 -0500331
332 /* Generate T-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100333 generate_T_state_entries(
Aaron Durbin76c37002012-10-30 09:03:43 -0500334 cpuID-1, cores_per_package);
335
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100336 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500337 }
338 }
Arthur Heymansc54d14f2018-11-28 12:09:23 +0100339
340 /* PPKG is usually used for thermal management
341 of the first and only package. */
342 acpigen_write_processor_package("PPKG", 0, cores_per_package);
343
344 /* Add a method to notify processor nodes */
345 acpigen_write_processor_cnot(cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500346}
347
348struct chip_operations cpu_intel_haswell_ops = {
349 CHIP_NAME("Intel Haswell CPU")
350};