blob: 9dcd8ece1e2d19f42409e85854d4480e19a31785 [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 coresystems GmbH
5 * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050016 */
17
18#include <types.h>
19#include <console/console.h>
20#include <arch/acpi.h>
21#include <arch/acpigen.h>
22#include <arch/cpu.h>
23#include <cpu/x86/msr.h>
24#include <cpu/intel/speedstep.h>
25#include <cpu/intel/turbo.h>
26#include <device/device.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050027#include "haswell.h"
28#include "chip.h"
29
Duncan Laurie1ad55642013-03-07 14:08:04 -080030#include <southbridge/intel/lynxpoint/pch.h>
31
Aaron Durbin76c37002012-10-30 09:03:43 -050032static int get_cores_per_package(void)
33{
34 struct cpuinfo_x86 c;
35 struct cpuid_result result;
36 int cores = 1;
37
38 get_fms(&c, cpuid_eax(1));
39 if (c.x86 != 6)
40 return 1;
41
42 result = cpuid_ext(0xb, 1);
43 cores = result.ebx & 0xff;
44
45 return cores;
46}
47
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010048static void generate_cstate_entries(acpi_cstate_t *cstates,
Aaron Durbin76c37002012-10-30 09:03:43 -050049 int c1, int c2, int c3)
50{
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010051 int cstate_count = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050052
53 /* Count number of active C-states */
54 if (c1 > 0)
55 ++cstate_count;
56 if (c2 > 0)
57 ++cstate_count;
58 if (c3 > 0)
59 ++cstate_count;
60 if (!cstate_count)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010061 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050062
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010063 acpigen_write_package(cstate_count + 1);
64 acpigen_write_byte(cstate_count);
Aaron Durbin76c37002012-10-30 09:03:43 -050065
66 /* Add an entry if the level is enabled */
67 if (c1 > 0) {
68 cstates[c1].ctype = 1;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010069 acpigen_write_CST_package_entry(&cstates[c1]);
Aaron Durbin76c37002012-10-30 09:03:43 -050070 }
71 if (c2 > 0) {
72 cstates[c2].ctype = 2;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010073 acpigen_write_CST_package_entry(&cstates[c2]);
Aaron Durbin76c37002012-10-30 09:03:43 -050074 }
75 if (c3 > 0) {
76 cstates[c3].ctype = 3;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010077 acpigen_write_CST_package_entry(&cstates[c3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050078 }
79
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010080 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -050081}
82
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010083static void generate_C_state_entries(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050084{
85 struct cpu_info *info;
86 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110087 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -050088 struct cpu_intel_haswell_config *conf = NULL;
89
90 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
91 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
92 if (!lapic)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010093 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050094 conf = lapic->chip_info;
95 if (!conf)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010096 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050097
98 /* Find CPU map of supported C-states */
99 info = cpu_info();
100 if (!info)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100101 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500102 cpu = find_cpu_driver(info->cpu);
103 if (!cpu || !cpu->cstates)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100104 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500105
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100106 acpigen_emit_byte(0x14); /* MethodOp */
107 acpigen_write_len_f(); /* PkgLength */
108 acpigen_emit_namestring("_CST");
109 acpigen_emit_byte(0x00); /* No Arguments */
Aaron Durbin76c37002012-10-30 09:03:43 -0500110
111 /* If running on AC power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100112 acpigen_emit_byte(0xa0); /* IfOp */
113 acpigen_write_len_f(); /* PkgLength */
114 acpigen_emit_namestring("PWRS");
115 acpigen_emit_byte(0xa4); /* ReturnOp */
116 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
Aaron Durbin76c37002012-10-30 09:03:43 -0500117 conf->c2_acpower, conf->c3_acpower);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100118 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500119
120 /* Else on battery power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100121 acpigen_emit_byte(0xa4); /* ReturnOp */
122 generate_cstate_entries(cpu->cstates, conf->c1_battery,
Aaron Durbin76c37002012-10-30 09:03:43 -0500123 conf->c2_battery, conf->c3_battery);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100124 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500125}
126
127static acpi_tstate_t tss_table_fine[] = {
128 { 100, 1000, 0, 0x00, 0 },
129 { 94, 940, 0, 0x1f, 0 },
130 { 88, 880, 0, 0x1e, 0 },
131 { 82, 820, 0, 0x1d, 0 },
132 { 75, 760, 0, 0x1c, 0 },
133 { 69, 700, 0, 0x1b, 0 },
134 { 63, 640, 0, 0x1a, 0 },
135 { 57, 580, 0, 0x19, 0 },
136 { 50, 520, 0, 0x18, 0 },
137 { 44, 460, 0, 0x17, 0 },
138 { 38, 400, 0, 0x16, 0 },
139 { 32, 340, 0, 0x15, 0 },
140 { 25, 280, 0, 0x14, 0 },
141 { 19, 220, 0, 0x13, 0 },
142 { 13, 160, 0, 0x12, 0 },
143};
144
145static acpi_tstate_t tss_table_coarse[] = {
146 { 100, 1000, 0, 0x00, 0 },
147 { 88, 875, 0, 0x1f, 0 },
148 { 75, 750, 0, 0x1e, 0 },
149 { 63, 625, 0, 0x1d, 0 },
150 { 50, 500, 0, 0x1c, 0 },
151 { 38, 375, 0, 0x1b, 0 },
152 { 25, 250, 0, 0x1a, 0 },
153 { 13, 125, 0, 0x19, 0 },
154};
155
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100156static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500157{
Aaron Durbin76c37002012-10-30 09:03:43 -0500158 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100159 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500160
161 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100162 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500163
164 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100165 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500166
167 /*
168 * CPUID.(EAX=6):EAX[5] indicates support
169 * for extended throttle levels.
170 */
171 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100172 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500173 ARRAY_SIZE(tss_table_fine), tss_table_fine);
174 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100175 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500176 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500177}
178
179static int calculate_power(int tdp, int p1_ratio, int ratio)
180{
181 u32 m;
182 u32 power;
183
184 /*
185 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
186 *
187 * Power = (ratio / p1_ratio) * m * tdp
188 */
189
190 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
191 m = (m * m) / 1000;
192
193 power = ((ratio * 100000 / p1_ratio) / 100);
194 power *= (m / 100) * (tdp / 1000);
195 power /= 1000;
196
197 return (int)power;
198}
199
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100200static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500201{
Aaron Durbin76c37002012-10-30 09:03:43 -0500202 int ratio_min, ratio_max, ratio_turbo, ratio_step;
203 int coord_type, power_max, power_unit, num_entries;
204 int ratio, power, clock, clock_max;
205 msr_t msr;
206
207 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
208 msr = rdmsr(MSR_MISC_PWR_MGMT);
209 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
210 coord_type = SW_ANY;
211 else
212 coord_type = HW_ALL;
213
214 /* Get bus ratio limits and calculate clock speeds */
215 msr = rdmsr(MSR_PLATFORM_INFO);
216 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
217
218 /* Determine if this CPU has configurable TDP */
219 if (cpu_config_tdp_levels()) {
220 /* Set max ratio to nominal TDP ratio */
221 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
222 ratio_max = msr.lo & 0xff;
223 } else {
224 /* Max Non-Turbo Ratio */
225 ratio_max = (msr.lo >> 8) & 0xff;
226 }
227 clock_max = ratio_max * HASWELL_BCLK;
228
229 /* Calculate CPU TDP in mW */
230 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
231 power_unit = 2 << ((msr.lo & 0xf) - 1);
232 msr = rdmsr(MSR_PKG_POWER_SKU);
233 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
234
235 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100236 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500237
238 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100239 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500240
241 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100242 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500243
244 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100245 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500246
247 /* Determine ratio points */
248 ratio_step = PSS_RATIO_STEP;
249 num_entries = (ratio_max - ratio_min) / ratio_step;
250 while (num_entries > PSS_MAX_ENTRIES-1) {
251 ratio_step <<= 1;
252 num_entries >>= 1;
253 }
254
255 /* P[T] is Turbo state if enabled */
256 if (get_turbo_state() == TURBO_ENABLED) {
257 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100258 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500259
260 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
261 ratio_turbo = msr.lo & 0xff;
262
263 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100264 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500265 clock_max + 1, /*MHz*/
266 power_max, /*mW*/
267 PSS_LATENCY_TRANSITION, /*lat1*/
268 PSS_LATENCY_BUSMASTER, /*lat2*/
269 ratio_turbo << 8, /*control*/
270 ratio_turbo << 8); /*status*/
271 } else {
272 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100273 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500274 }
275
276 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100277 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500278 clock_max, /*MHz*/
279 power_max, /*mW*/
280 PSS_LATENCY_TRANSITION, /*lat1*/
281 PSS_LATENCY_BUSMASTER, /*lat2*/
282 ratio_max << 8, /*control*/
283 ratio_max << 8); /*status*/
284
285 /* Generate the remaining entries */
286 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
287 ratio >= ratio_min; ratio -= ratio_step) {
288
289 /* Calculate power at this ratio */
290 power = calculate_power(power_max, ratio_max, ratio);
291 clock = ratio * HASWELL_BCLK;
292
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100293 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500294 clock, /*MHz*/
295 power, /*mW*/
296 PSS_LATENCY_TRANSITION, /*lat1*/
297 PSS_LATENCY_BUSMASTER, /*lat2*/
298 ratio << 8, /*control*/
299 ratio << 8); /*status*/
300 }
301
302 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100303 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500304}
305
Elyes HAOUASf925c562018-02-08 13:05:16 +0100306void generate_cpu_entries(struct device *device)
Aaron Durbin76c37002012-10-30 09:03:43 -0500307{
Duncan Laurie1ad55642013-03-07 14:08:04 -0800308 int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6;
Aaron Durbin76c37002012-10-30 09:03:43 -0500309 int totalcores = dev_count_cpu();
310 int cores_per_package = get_cores_per_package();
311 int numcpus = totalcores/cores_per_package;
312
313 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
314 numcpus, cores_per_package);
315
Martin Roth9944b282014-08-11 11:24:55 -0600316 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700317 for (coreID = 1; coreID <= cores_per_package; coreID++) {
318 if (coreID > 1) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500319 pcontrol_blk = 0;
320 plen = 0;
321 }
322
323 /* Generate processor \_PR.CPUx */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100324 acpigen_write_processor(
Aaron Durbin76c37002012-10-30 09:03:43 -0500325 (cpuID-1)*cores_per_package+coreID-1,
326 pcontrol_blk, plen);
327
328 /* Generate P-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100329 generate_P_state_entries(
Duncan Laurie25b8b7b2013-04-19 10:02:23 -0700330 coreID-1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500331
332 /* Generate C-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100333 generate_C_state_entries();
Aaron Durbin76c37002012-10-30 09:03:43 -0500334
335 /* Generate T-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100336 generate_T_state_entries(
Aaron Durbin76c37002012-10-30 09:03:43 -0500337 cpuID-1, cores_per_package);
338
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100339 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500340 }
341 }
Arthur Heymansc54d14f2018-11-28 12:09:23 +0100342
343 /* PPKG is usually used for thermal management
344 of the first and only package. */
345 acpigen_write_processor_package("PPKG", 0, cores_per_package);
346
347 /* Add a method to notify processor nodes */
348 acpigen_write_processor_cnot(cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500349}
350
351struct chip_operations cpu_intel_haswell_ops = {
352 CHIP_NAME("Intel Haswell CPU")
353};