blob: 6323d2756ee8e359e498832f8b7be5f5722f6fb8 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23#include <types.h>
24#include <console/console.h>
25#include <arch/acpi.h>
26#include <arch/acpigen.h>
27#include <arch/cpu.h>
28#include <cpu/x86/msr.h>
29#include <cpu/intel/speedstep.h>
30#include <cpu/intel/turbo.h>
31#include <device/device.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050032#include "haswell.h"
33#include "chip.h"
34
Duncan Laurie1ad55642013-03-07 14:08:04 -080035#include <southbridge/intel/lynxpoint/pch.h>
36
Aaron Durbin76c37002012-10-30 09:03:43 -050037static int get_cores_per_package(void)
38{
39 struct cpuinfo_x86 c;
40 struct cpuid_result result;
41 int cores = 1;
42
43 get_fms(&c, cpuid_eax(1));
44 if (c.x86 != 6)
45 return 1;
46
47 result = cpuid_ext(0xb, 1);
48 cores = result.ebx & 0xff;
49
50 return cores;
51}
52
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010053static void generate_cstate_entries(acpi_cstate_t *cstates,
Aaron Durbin76c37002012-10-30 09:03:43 -050054 int c1, int c2, int c3)
55{
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010056 int cstate_count = 0;
Aaron Durbin76c37002012-10-30 09:03:43 -050057
58 /* Count number of active C-states */
59 if (c1 > 0)
60 ++cstate_count;
61 if (c2 > 0)
62 ++cstate_count;
63 if (c3 > 0)
64 ++cstate_count;
65 if (!cstate_count)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010066 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050067
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010068 acpigen_write_package(cstate_count + 1);
69 acpigen_write_byte(cstate_count);
Aaron Durbin76c37002012-10-30 09:03:43 -050070
71 /* Add an entry if the level is enabled */
72 if (c1 > 0) {
73 cstates[c1].ctype = 1;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010074 acpigen_write_CST_package_entry(&cstates[c1]);
Aaron Durbin76c37002012-10-30 09:03:43 -050075 }
76 if (c2 > 0) {
77 cstates[c2].ctype = 2;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010078 acpigen_write_CST_package_entry(&cstates[c2]);
Aaron Durbin76c37002012-10-30 09:03:43 -050079 }
80 if (c3 > 0) {
81 cstates[c3].ctype = 3;
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010082 acpigen_write_CST_package_entry(&cstates[c3]);
Aaron Durbin76c37002012-10-30 09:03:43 -050083 }
84
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010085 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -050086}
87
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010088static void generate_C_state_entries(void)
Aaron Durbin76c37002012-10-30 09:03:43 -050089{
90 struct cpu_info *info;
91 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110092 struct device *lapic;
Aaron Durbin76c37002012-10-30 09:03:43 -050093 struct cpu_intel_haswell_config *conf = NULL;
94
95 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
96 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
97 if (!lapic)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +010098 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050099 conf = lapic->chip_info;
100 if (!conf)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100101 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500102
103 /* Find CPU map of supported C-states */
104 info = cpu_info();
105 if (!info)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100106 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500107 cpu = find_cpu_driver(info->cpu);
108 if (!cpu || !cpu->cstates)
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100109 return;
Aaron Durbin76c37002012-10-30 09:03:43 -0500110
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100111 acpigen_emit_byte(0x14); /* MethodOp */
112 acpigen_write_len_f(); /* PkgLength */
113 acpigen_emit_namestring("_CST");
114 acpigen_emit_byte(0x00); /* No Arguments */
Aaron Durbin76c37002012-10-30 09:03:43 -0500115
116 /* If running on AC power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100117 acpigen_emit_byte(0xa0); /* IfOp */
118 acpigen_write_len_f(); /* PkgLength */
119 acpigen_emit_namestring("PWRS");
120 acpigen_emit_byte(0xa4); /* ReturnOp */
121 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
Aaron Durbin76c37002012-10-30 09:03:43 -0500122 conf->c2_acpower, conf->c3_acpower);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100123 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500124
125 /* Else on battery power */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100126 acpigen_emit_byte(0xa4); /* ReturnOp */
127 generate_cstate_entries(cpu->cstates, conf->c1_battery,
Aaron Durbin76c37002012-10-30 09:03:43 -0500128 conf->c2_battery, conf->c3_battery);
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100129 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500130}
131
132static acpi_tstate_t tss_table_fine[] = {
133 { 100, 1000, 0, 0x00, 0 },
134 { 94, 940, 0, 0x1f, 0 },
135 { 88, 880, 0, 0x1e, 0 },
136 { 82, 820, 0, 0x1d, 0 },
137 { 75, 760, 0, 0x1c, 0 },
138 { 69, 700, 0, 0x1b, 0 },
139 { 63, 640, 0, 0x1a, 0 },
140 { 57, 580, 0, 0x19, 0 },
141 { 50, 520, 0, 0x18, 0 },
142 { 44, 460, 0, 0x17, 0 },
143 { 38, 400, 0, 0x16, 0 },
144 { 32, 340, 0, 0x15, 0 },
145 { 25, 280, 0, 0x14, 0 },
146 { 19, 220, 0, 0x13, 0 },
147 { 13, 160, 0, 0x12, 0 },
148};
149
150static acpi_tstate_t tss_table_coarse[] = {
151 { 100, 1000, 0, 0x00, 0 },
152 { 88, 875, 0, 0x1f, 0 },
153 { 75, 750, 0, 0x1e, 0 },
154 { 63, 625, 0, 0x1d, 0 },
155 { 50, 500, 0, 0x1c, 0 },
156 { 38, 375, 0, 0x1b, 0 },
157 { 25, 250, 0, 0x1a, 0 },
158 { 13, 125, 0, 0x19, 0 },
159};
160
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100161static void generate_T_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500162{
Aaron Durbin76c37002012-10-30 09:03:43 -0500163 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100164 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500165
166 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100167 acpigen_write_empty_PTC();
Aaron Durbin76c37002012-10-30 09:03:43 -0500168
169 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100170 acpigen_write_TPC("\\TLVL");
Aaron Durbin76c37002012-10-30 09:03:43 -0500171
172 /*
173 * CPUID.(EAX=6):EAX[5] indicates support
174 * for extended throttle levels.
175 */
176 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100177 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500178 ARRAY_SIZE(tss_table_fine), tss_table_fine);
179 else
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100180 acpigen_write_TSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500181 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Aaron Durbin76c37002012-10-30 09:03:43 -0500182}
183
184static int calculate_power(int tdp, int p1_ratio, int ratio)
185{
186 u32 m;
187 u32 power;
188
189 /*
190 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
191 *
192 * Power = (ratio / p1_ratio) * m * tdp
193 */
194
195 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
196 m = (m * m) / 1000;
197
198 power = ((ratio * 100000 / p1_ratio) / 100);
199 power *= (m / 100) * (tdp / 1000);
200 power /= 1000;
201
202 return (int)power;
203}
204
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100205static void generate_P_state_entries(int core, int cores_per_package)
Aaron Durbin76c37002012-10-30 09:03:43 -0500206{
Aaron Durbin76c37002012-10-30 09:03:43 -0500207 int ratio_min, ratio_max, ratio_turbo, ratio_step;
208 int coord_type, power_max, power_unit, num_entries;
209 int ratio, power, clock, clock_max;
210 msr_t msr;
211
212 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
213 msr = rdmsr(MSR_MISC_PWR_MGMT);
214 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
215 coord_type = SW_ANY;
216 else
217 coord_type = HW_ALL;
218
219 /* Get bus ratio limits and calculate clock speeds */
220 msr = rdmsr(MSR_PLATFORM_INFO);
221 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
222
223 /* Determine if this CPU has configurable TDP */
224 if (cpu_config_tdp_levels()) {
225 /* Set max ratio to nominal TDP ratio */
226 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
227 ratio_max = msr.lo & 0xff;
228 } else {
229 /* Max Non-Turbo Ratio */
230 ratio_max = (msr.lo >> 8) & 0xff;
231 }
232 clock_max = ratio_max * HASWELL_BCLK;
233
234 /* Calculate CPU TDP in mW */
235 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
236 power_unit = 2 << ((msr.lo & 0xf) - 1);
237 msr = rdmsr(MSR_PKG_POWER_SKU);
238 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
239
240 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100241 acpigen_write_empty_PCT();
Aaron Durbin76c37002012-10-30 09:03:43 -0500242
243 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100244 acpigen_write_PPC_NVS();
Aaron Durbin76c37002012-10-30 09:03:43 -0500245
246 /* Write PSD indicating configured coordination type */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100247 acpigen_write_PSD_package(core, 1, coord_type);
Aaron Durbin76c37002012-10-30 09:03:43 -0500248
249 /* Add P-state entries in _PSS table */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100250 acpigen_write_name("_PSS");
Aaron Durbin76c37002012-10-30 09:03:43 -0500251
252 /* Determine ratio points */
253 ratio_step = PSS_RATIO_STEP;
254 num_entries = (ratio_max - ratio_min) / ratio_step;
255 while (num_entries > PSS_MAX_ENTRIES-1) {
256 ratio_step <<= 1;
257 num_entries >>= 1;
258 }
259
260 /* P[T] is Turbo state if enabled */
261 if (get_turbo_state() == TURBO_ENABLED) {
262 /* _PSS package count including Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100263 acpigen_write_package(num_entries + 2);
Aaron Durbin76c37002012-10-30 09:03:43 -0500264
265 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
266 ratio_turbo = msr.lo & 0xff;
267
268 /* Add entry for Turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100269 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500270 clock_max + 1, /*MHz*/
271 power_max, /*mW*/
272 PSS_LATENCY_TRANSITION, /*lat1*/
273 PSS_LATENCY_BUSMASTER, /*lat2*/
274 ratio_turbo << 8, /*control*/
275 ratio_turbo << 8); /*status*/
276 } else {
277 /* _PSS package count without Turbo */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100278 acpigen_write_package(num_entries + 1);
Aaron Durbin76c37002012-10-30 09:03:43 -0500279 }
280
281 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100282 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500283 clock_max, /*MHz*/
284 power_max, /*mW*/
285 PSS_LATENCY_TRANSITION, /*lat1*/
286 PSS_LATENCY_BUSMASTER, /*lat2*/
287 ratio_max << 8, /*control*/
288 ratio_max << 8); /*status*/
289
290 /* Generate the remaining entries */
291 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
292 ratio >= ratio_min; ratio -= ratio_step) {
293
294 /* Calculate power at this ratio */
295 power = calculate_power(power_max, ratio_max, ratio);
296 clock = ratio * HASWELL_BCLK;
297
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100298 acpigen_write_PSS_package(
Aaron Durbin76c37002012-10-30 09:03:43 -0500299 clock, /*MHz*/
300 power, /*mW*/
301 PSS_LATENCY_TRANSITION, /*lat1*/
302 PSS_LATENCY_BUSMASTER, /*lat2*/
303 ratio << 8, /*control*/
304 ratio << 8); /*status*/
305 }
306
307 /* Fix package length */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100308 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500309}
310
311void generate_cpu_entries(void)
312{
Duncan Laurie1ad55642013-03-07 14:08:04 -0800313 int coreID, cpuID, pcontrol_blk = get_pmbase(), plen = 6;
Aaron Durbin76c37002012-10-30 09:03:43 -0500314 int totalcores = dev_count_cpu();
315 int cores_per_package = get_cores_per_package();
316 int numcpus = totalcores/cores_per_package;
317
318 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
319 numcpus, cores_per_package);
320
Martin Roth9944b282014-08-11 11:24:55 -0600321 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Aaron Durbin76c37002012-10-30 09:03:43 -0500322 for (coreID=1; coreID<=cores_per_package; coreID++) {
323 if (coreID>1) {
324 pcontrol_blk = 0;
325 plen = 0;
326 }
327
328 /* Generate processor \_PR.CPUx */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100329 acpigen_write_processor(
Aaron Durbin76c37002012-10-30 09:03:43 -0500330 (cpuID-1)*cores_per_package+coreID-1,
331 pcontrol_blk, plen);
332
333 /* Generate P-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100334 generate_P_state_entries(
Duncan Laurie25b8b7b2013-04-19 10:02:23 -0700335 coreID-1, cores_per_package);
Aaron Durbin76c37002012-10-30 09:03:43 -0500336
337 /* Generate C-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100338 generate_C_state_entries();
Aaron Durbin76c37002012-10-30 09:03:43 -0500339
340 /* Generate T-state tables */
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100341 generate_T_state_entries(
Aaron Durbin76c37002012-10-30 09:03:43 -0500342 cpuID-1, cores_per_package);
343
Vladimir Serbinenkobe0fd0a2014-11-04 21:10:59 +0100344 acpigen_pop_len();
Aaron Durbin76c37002012-10-30 09:03:43 -0500345 }
346 }
347}
348
349struct chip_operations cpu_intel_haswell_ops = {
350 CHIP_NAME("Intel Haswell CPU")
351};