blob: 30b19e93f1824834d4fa88f8f517624459457652 [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
155static int generate_T_state_entries(int core, int cores_per_package)
156{
157 int len;
158
159 /* Indicate SW_ALL coordination for T-states */
160 len = acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
161
162 /* Indicate FFixedHW so OS will use MSR */
163 len += acpigen_write_empty_PTC();
164
165 /* Set a T-state limit that can be modified in NVS */
166 len += acpigen_write_TPC("\\TLVL");
167
168 /*
169 * CPUID.(EAX=6):EAX[5] indicates support
170 * for extended throttle levels.
171 */
172 if (cpuid_eax(6) & (1 << 5))
173 len += acpigen_write_TSS_package(
174 ARRAY_SIZE(tss_table_fine), tss_table_fine);
175 else
176 len += acpigen_write_TSS_package(
177 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
178
179 return len;
180}
181
182static int calculate_power(int tdp, int p1_ratio, int ratio)
183{
184 u32 m;
185 u32 power;
186
187 /*
188 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
189 *
190 * Power = (ratio / p1_ratio) * m * tdp
191 */
192
193 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
194 m = (m * m) / 1000;
195
196 power = ((ratio * 100000 / p1_ratio) / 100);
197 power *= (m / 100) * (tdp / 1000);
198 power /= 1000;
199
200 return (int)power;
201}
202
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100203static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200204{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200205 int ratio_min, ratio_max, ratio_turbo, ratio_step;
206 int coord_type, power_max, power_unit, num_entries;
207 int ratio, power, clock, clock_max;
208 msr_t msr;
209
210 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
211 msr = rdmsr(MSR_MISC_PWR_MGMT);
212 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
213 coord_type = SW_ANY;
214 else
215 coord_type = HW_ALL;
216
217 /* Get bus ratio limits and calculate clock speeds */
218 msr = rdmsr(MSR_PLATFORM_INFO);
219 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700220
221 /* Determine if this CPU has configurable TDP */
222 if (cpu_config_tdp_levels()) {
223 /* Set max ratio to nominal TDP ratio */
224 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
225 ratio_max = msr.lo & 0xff;
226 } else {
227 /* Max Non-Turbo Ratio */
228 ratio_max = (msr.lo >> 8) & 0xff;
229 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200230 clock_max = ratio_max * SANDYBRIDGE_BCLK;
231
232 /* Calculate CPU TDP in mW */
233 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
234 power_unit = 2 << ((msr.lo & 0xf) - 1);
235 msr = rdmsr(MSR_PKG_POWER_SKU);
236 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
237
238 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100239 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200240
241 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100242 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200243
244 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100245 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200246
247 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100248 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200249
250 /* Determine ratio points */
251 ratio_step = PSS_RATIO_STEP;
252 num_entries = (ratio_max - ratio_min) / ratio_step;
253 while (num_entries > PSS_MAX_ENTRIES-1) {
254 ratio_step <<= 1;
255 num_entries >>= 1;
256 }
257
258 /* P[T] is Turbo state if enabled */
259 if (get_turbo_state() == TURBO_ENABLED) {
260 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100261 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200262
263 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
264 ratio_turbo = msr.lo & 0xff;
265
266 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100267 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200268 clock_max + 1, /*MHz*/
269 power_max, /*mW*/
270 PSS_LATENCY_TRANSITION, /*lat1*/
271 PSS_LATENCY_BUSMASTER, /*lat2*/
272 ratio_turbo << 8, /*control*/
273 ratio_turbo << 8); /*status*/
274 } else {
275 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100276 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200277 }
278
279 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100280 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200281 clock_max, /*MHz*/
282 power_max, /*mW*/
283 PSS_LATENCY_TRANSITION, /*lat1*/
284 PSS_LATENCY_BUSMASTER, /*lat2*/
285 ratio_max << 8, /*control*/
286 ratio_max << 8); /*status*/
287
288 /* Generate the remaining entries */
289 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
290 ratio >= ratio_min; ratio -= ratio_step) {
291
292 /* Calculate power at this ratio */
293 power = calculate_power(power_max, ratio_max, ratio);
294 clock = ratio * SANDYBRIDGE_BCLK;
295
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100296 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200297 clock, /*MHz*/
298 power, /*mW*/
299 PSS_LATENCY_TRANSITION, /*lat1*/
300 PSS_LATENCY_BUSMASTER, /*lat2*/
301 ratio << 8, /*control*/
302 ratio << 8); /*status*/
303 }
304
305 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100306 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200307}
308
309void generate_cpu_entries(void)
310{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200311 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
312 int totalcores = dev_count_cpu();
313 int cores_per_package = get_cores_per_package();
314 int numcpus = totalcores/cores_per_package;
315
316 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
317 numcpus, cores_per_package);
318
Martin Roth9944b282014-08-11 11:24:55 -0600319 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200320 for (coreID=1; coreID<=cores_per_package; coreID++) {
321 if (coreID>1) {
322 pcontrol_blk = 0;
323 plen = 0;
324 }
325
326 /* Generate processor \_PR.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100327 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200328 (cpuID-1)*cores_per_package+coreID-1,
329 pcontrol_blk, plen);
330
331 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100332 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200333 cpuID-1, cores_per_package);
334
335 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100336 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200337
338 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100339 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200340 cpuID-1, cores_per_package);
341
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100342 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200343 }
344 }
345}
346
347struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700348 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200349};