blob: 47c958a18817501653ba9633cd287b5e35868341 [file] [log] [blame]
Stefan Reinauer5c554632012-04-04 00:09:50 +02001/*
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
Patrick Georgib890a122015-03-26 15:17:45 +010019 * Foundation, Inc.
Stefan Reinauer5c554632012-04-04 00:09:50 +020020 */
21
22#include <types.h>
23#include <console/console.h>
24#include <arch/acpi.h>
25#include <arch/acpigen.h>
26#include <arch/cpu.h>
27#include <cpu/x86/msr.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020028#include <cpu/intel/speedstep.h>
29#include <cpu/intel/turbo.h>
30#include <device/device.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020031#include "model_206ax.h"
32#include "chip.h"
33
34static int get_cores_per_package(void)
35{
36 struct cpuinfo_x86 c;
37 struct cpuid_result result;
38 int cores = 1;
39
40 get_fms(&c, cpuid_eax(1));
41 if (c.x86 != 6)
42 return 1;
43
Stefan Reinauerbb31f3a2012-05-11 16:30:54 -070044 result = cpuid_ext(0xb, 1);
45 cores = result.ebx & 0xff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020046
47 return cores;
48}
49
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010050static void generate_cstate_entries(acpi_cstate_t *cstates,
51 int c1, int c2, int c3)
Stefan Reinauer5c554632012-04-04 00:09:50 +020052{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010053 int cstate_count = 0;
Stefan Reinauer5c554632012-04-04 00:09:50 +020054
55 /* Count number of active C-states */
56 if (c1 > 0)
57 ++cstate_count;
58 if (c2 > 0)
59 ++cstate_count;
60 if (c3 > 0)
61 ++cstate_count;
62 if (!cstate_count)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010063 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020064
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010065 acpigen_write_package(cstate_count + 1);
66 acpigen_write_byte(cstate_count);
Stefan Reinauer5c554632012-04-04 00:09:50 +020067
68 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020069 if (c1 > 0) {
70 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010071 acpigen_write_CST_package_entry(&cstates[c1]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020072 }
73 if (c2 > 0) {
74 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010075 acpigen_write_CST_package_entry(&cstates[c2]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020076 }
77 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070078 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010079 acpigen_write_CST_package_entry(&cstates[c3]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020080 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020081
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010082 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020083}
84
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010085static void generate_C_state_entries(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020086{
Sven Schnelle51676b12012-07-29 19:18:03 +020087 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020088 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110089 struct device *lapic;
Stefan Reinauer5c554632012-04-04 00:09:50 +020090 struct cpu_intel_model_206ax_config *conf = NULL;
91
92 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
93 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
94 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010095 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020096 conf = lapic->chip_info;
97 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010098 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020099
100 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +0200101 info = cpu_info();
102 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100103 return;
Sven Schnelle51676b12012-07-29 19:18:03 +0200104 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200105 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100106 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200107
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100108 acpigen_write_method("_CST", 0);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200109
110 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100111 acpigen_emit_byte(0xa0); /* IfOp */
112 acpigen_write_len_f(); /* PkgLength */
113 acpigen_emit_namestring("PWRS");
114 acpigen_emit_byte(0xa4); /* ReturnOp */
115 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
116 conf->c2_acpower, conf->c3_acpower);
117 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200118
119 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100120 acpigen_emit_byte(0xa4); /* ReturnOp */
121 generate_cstate_entries(cpu->cstates, conf->c1_battery,
122 conf->c2_battery, conf->c3_battery);
123 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200124}
125
126static acpi_tstate_t tss_table_fine[] = {
127 { 100, 1000, 0, 0x00, 0 },
128 { 94, 940, 0, 0x1f, 0 },
129 { 88, 880, 0, 0x1e, 0 },
130 { 82, 820, 0, 0x1d, 0 },
131 { 75, 760, 0, 0x1c, 0 },
132 { 69, 700, 0, 0x1b, 0 },
133 { 63, 640, 0, 0x1a, 0 },
134 { 57, 580, 0, 0x19, 0 },
135 { 50, 520, 0, 0x18, 0 },
136 { 44, 460, 0, 0x17, 0 },
137 { 38, 400, 0, 0x16, 0 },
138 { 32, 340, 0, 0x15, 0 },
139 { 25, 280, 0, 0x14, 0 },
140 { 19, 220, 0, 0x13, 0 },
141 { 13, 160, 0, 0x12, 0 },
142};
143
144static acpi_tstate_t tss_table_coarse[] = {
145 { 100, 1000, 0, 0x00, 0 },
146 { 88, 875, 0, 0x1f, 0 },
147 { 75, 750, 0, 0x1e, 0 },
148 { 63, 625, 0, 0x1d, 0 },
149 { 50, 500, 0, 0x1c, 0 },
150 { 38, 375, 0, 0x1b, 0 },
151 { 25, 250, 0, 0x1a, 0 },
152 { 13, 125, 0, 0x19, 0 },
153};
154
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100155static void generate_T_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200156{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200157 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100158 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200159
160 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100161 acpigen_write_empty_PTC();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200162
163 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100164 acpigen_write_TPC("\\TLVL");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200165
166 /*
167 * CPUID.(EAX=6):EAX[5] indicates support
168 * for extended throttle levels.
169 */
170 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100171 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200172 ARRAY_SIZE(tss_table_fine), tss_table_fine);
173 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200175 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200176}
177
178static int calculate_power(int tdp, int p1_ratio, int ratio)
179{
180 u32 m;
181 u32 power;
182
183 /*
184 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
185 *
186 * Power = (ratio / p1_ratio) * m * tdp
187 */
188
189 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
190 m = (m * m) / 1000;
191
192 power = ((ratio * 100000 / p1_ratio) / 100);
193 power *= (m / 100) * (tdp / 1000);
194 power /= 1000;
195
196 return (int)power;
197}
198
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100199static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200200{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200201 int ratio_min, ratio_max, ratio_turbo, ratio_step;
202 int coord_type, power_max, power_unit, num_entries;
203 int ratio, power, clock, clock_max;
204 msr_t msr;
205
206 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
207 msr = rdmsr(MSR_MISC_PWR_MGMT);
208 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
209 coord_type = SW_ANY;
210 else
211 coord_type = HW_ALL;
212
213 /* Get bus ratio limits and calculate clock speeds */
214 msr = rdmsr(MSR_PLATFORM_INFO);
215 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700216
217 /* Determine if this CPU has configurable TDP */
218 if (cpu_config_tdp_levels()) {
219 /* Set max ratio to nominal TDP ratio */
220 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
221 ratio_max = msr.lo & 0xff;
222 } else {
223 /* Max Non-Turbo Ratio */
224 ratio_max = (msr.lo >> 8) & 0xff;
225 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200226 clock_max = ratio_max * SANDYBRIDGE_BCLK;
227
228 /* Calculate CPU TDP in mW */
229 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
230 power_unit = 2 << ((msr.lo & 0xf) - 1);
231 msr = rdmsr(MSR_PKG_POWER_SKU);
232 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
233
234 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100235 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200236
237 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100238 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200239
240 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100241 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200242
243 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100244 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200245
246 /* Determine ratio points */
247 ratio_step = PSS_RATIO_STEP;
248 num_entries = (ratio_max - ratio_min) / ratio_step;
249 while (num_entries > PSS_MAX_ENTRIES-1) {
250 ratio_step <<= 1;
251 num_entries >>= 1;
252 }
253
254 /* P[T] is Turbo state if enabled */
255 if (get_turbo_state() == TURBO_ENABLED) {
256 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100257 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200258
259 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
260 ratio_turbo = msr.lo & 0xff;
261
262 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100263 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200264 clock_max + 1, /*MHz*/
265 power_max, /*mW*/
266 PSS_LATENCY_TRANSITION, /*lat1*/
267 PSS_LATENCY_BUSMASTER, /*lat2*/
268 ratio_turbo << 8, /*control*/
269 ratio_turbo << 8); /*status*/
270 } else {
271 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100272 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200273 }
274
275 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100276 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200277 clock_max, /*MHz*/
278 power_max, /*mW*/
279 PSS_LATENCY_TRANSITION, /*lat1*/
280 PSS_LATENCY_BUSMASTER, /*lat2*/
281 ratio_max << 8, /*control*/
282 ratio_max << 8); /*status*/
283
284 /* Generate the remaining entries */
285 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
286 ratio >= ratio_min; ratio -= ratio_step) {
287
288 /* Calculate power at this ratio */
289 power = calculate_power(power_max, ratio_max, ratio);
290 clock = ratio * SANDYBRIDGE_BCLK;
291
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100292 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200293 clock, /*MHz*/
294 power, /*mW*/
295 PSS_LATENCY_TRANSITION, /*lat1*/
296 PSS_LATENCY_BUSMASTER, /*lat2*/
297 ratio << 8, /*control*/
298 ratio << 8); /*status*/
299 }
300
301 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100302 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200303}
304
305void generate_cpu_entries(void)
306{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200307 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
308 int totalcores = dev_count_cpu();
309 int cores_per_package = get_cores_per_package();
310 int numcpus = totalcores/cores_per_package;
311
312 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
313 numcpus, cores_per_package);
314
Martin Roth9944b282014-08-11 11:24:55 -0600315 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200316 for (coreID=1; coreID<=cores_per_package; coreID++) {
317 if (coreID>1) {
318 pcontrol_blk = 0;
319 plen = 0;
320 }
321
322 /* Generate processor \_PR.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100323 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200324 (cpuID-1)*cores_per_package+coreID-1,
325 pcontrol_blk, plen);
326
327 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100328 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200329 cpuID-1, cores_per_package);
330
331 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100332 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200333
334 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100335 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200336 cpuID-1, cores_per_package);
337
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100338 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200339 }
340 }
341}
342
343struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700344 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200345};