blob: c31bb5eaac9cc1589f28fc81feeab83796e52c7d [file] [log] [blame]
Stefan Reinauer5c554632012-04-04 00:09:50 +02001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer5c554632012-04-04 00:09:50 +02004 * 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.
Stefan Reinauer5c554632012-04-04 00:09:50 +020013 */
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>
Stefan Reinauer5c554632012-04-04 00:09:50 +020021#include <cpu/intel/speedstep.h>
22#include <cpu/intel/turbo.h>
23#include <device/device.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020024#include "model_206ax.h"
25#include "chip.h"
26
27static int get_cores_per_package(void)
28{
29 struct cpuinfo_x86 c;
30 struct cpuid_result result;
31 int cores = 1;
32
33 get_fms(&c, cpuid_eax(1));
34 if (c.x86 != 6)
35 return 1;
36
Stefan Reinauerbb31f3a2012-05-11 16:30:54 -070037 result = cpuid_ext(0xb, 1);
38 cores = result.ebx & 0xff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020039
40 return cores;
41}
42
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010043static void generate_cstate_entries(acpi_cstate_t *cstates,
44 int c1, int c2, int c3)
Stefan Reinauer5c554632012-04-04 00:09:50 +020045{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010046 int cstate_count = 0;
Stefan Reinauer5c554632012-04-04 00:09:50 +020047
48 /* Count number of active C-states */
49 if (c1 > 0)
50 ++cstate_count;
51 if (c2 > 0)
52 ++cstate_count;
53 if (c3 > 0)
54 ++cstate_count;
55 if (!cstate_count)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010056 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020057
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010058 acpigen_write_package(cstate_count + 1);
59 acpigen_write_byte(cstate_count);
Stefan Reinauer5c554632012-04-04 00:09:50 +020060
61 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020062 if (c1 > 0) {
63 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010064 acpigen_write_CST_package_entry(&cstates[c1]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020065 }
66 if (c2 > 0) {
67 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010068 acpigen_write_CST_package_entry(&cstates[c2]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020069 }
70 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070071 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010072 acpigen_write_CST_package_entry(&cstates[c3]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020073 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020074
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010075 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020076}
77
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010078static void generate_C_state_entries(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020079{
Sven Schnelle51676b12012-07-29 19:18:03 +020080 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020081 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110082 struct device *lapic;
Stefan Reinauer5c554632012-04-04 00:09:50 +020083 struct cpu_intel_model_206ax_config *conf = NULL;
84
85 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
86 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
87 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010088 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020089 conf = lapic->chip_info;
90 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010091 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020092
93 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +020094 info = cpu_info();
95 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010096 return;
Sven Schnelle51676b12012-07-29 19:18:03 +020097 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +020098 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010099 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +0200100
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100101 acpigen_write_method("_CST", 0);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200102
103 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100104 acpigen_emit_byte(0xa0); /* IfOp */
105 acpigen_write_len_f(); /* PkgLength */
106 acpigen_emit_namestring("PWRS");
107 acpigen_emit_byte(0xa4); /* ReturnOp */
108 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
109 conf->c2_acpower, conf->c3_acpower);
110 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200111
112 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100113 acpigen_emit_byte(0xa4); /* ReturnOp */
114 generate_cstate_entries(cpu->cstates, conf->c1_battery,
115 conf->c2_battery, conf->c3_battery);
116 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200117}
118
119static acpi_tstate_t tss_table_fine[] = {
120 { 100, 1000, 0, 0x00, 0 },
121 { 94, 940, 0, 0x1f, 0 },
122 { 88, 880, 0, 0x1e, 0 },
123 { 82, 820, 0, 0x1d, 0 },
124 { 75, 760, 0, 0x1c, 0 },
125 { 69, 700, 0, 0x1b, 0 },
126 { 63, 640, 0, 0x1a, 0 },
127 { 57, 580, 0, 0x19, 0 },
128 { 50, 520, 0, 0x18, 0 },
129 { 44, 460, 0, 0x17, 0 },
130 { 38, 400, 0, 0x16, 0 },
131 { 32, 340, 0, 0x15, 0 },
132 { 25, 280, 0, 0x14, 0 },
133 { 19, 220, 0, 0x13, 0 },
134 { 13, 160, 0, 0x12, 0 },
135};
136
137static acpi_tstate_t tss_table_coarse[] = {
138 { 100, 1000, 0, 0x00, 0 },
139 { 88, 875, 0, 0x1f, 0 },
140 { 75, 750, 0, 0x1e, 0 },
141 { 63, 625, 0, 0x1d, 0 },
142 { 50, 500, 0, 0x1c, 0 },
143 { 38, 375, 0, 0x1b, 0 },
144 { 25, 250, 0, 0x1a, 0 },
145 { 13, 125, 0, 0x19, 0 },
146};
147
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100148static void generate_T_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200149{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200150 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100151 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200152
153 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100154 acpigen_write_empty_PTC();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200155
156 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100157 acpigen_write_TPC("\\TLVL");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200158
159 /*
160 * CPUID.(EAX=6):EAX[5] indicates support
161 * for extended throttle levels.
162 */
163 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100164 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200165 ARRAY_SIZE(tss_table_fine), tss_table_fine);
166 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100167 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200168 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200169}
170
171static int calculate_power(int tdp, int p1_ratio, int ratio)
172{
173 u32 m;
174 u32 power;
175
176 /*
177 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
178 *
179 * Power = (ratio / p1_ratio) * m * tdp
180 */
181
182 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
183 m = (m * m) / 1000;
184
185 power = ((ratio * 100000 / p1_ratio) / 100);
186 power *= (m / 100) * (tdp / 1000);
187 power /= 1000;
188
189 return (int)power;
190}
191
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100192static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200193{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200194 int ratio_min, ratio_max, ratio_turbo, ratio_step;
195 int coord_type, power_max, power_unit, num_entries;
196 int ratio, power, clock, clock_max;
197 msr_t msr;
198
199 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
200 msr = rdmsr(MSR_MISC_PWR_MGMT);
201 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
202 coord_type = SW_ANY;
203 else
204 coord_type = HW_ALL;
205
206 /* Get bus ratio limits and calculate clock speeds */
207 msr = rdmsr(MSR_PLATFORM_INFO);
208 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700209
210 /* Determine if this CPU has configurable TDP */
211 if (cpu_config_tdp_levels()) {
212 /* Set max ratio to nominal TDP ratio */
213 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
214 ratio_max = msr.lo & 0xff;
215 } else {
216 /* Max Non-Turbo Ratio */
217 ratio_max = (msr.lo >> 8) & 0xff;
218 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200219 clock_max = ratio_max * SANDYBRIDGE_BCLK;
220
221 /* Calculate CPU TDP in mW */
222 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
223 power_unit = 2 << ((msr.lo & 0xf) - 1);
224 msr = rdmsr(MSR_PKG_POWER_SKU);
225 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
226
227 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100228 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200229
230 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100231 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200232
233 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100234 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200235
236 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100237 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200238
239 /* Determine ratio points */
240 ratio_step = PSS_RATIO_STEP;
241 num_entries = (ratio_max - ratio_min) / ratio_step;
242 while (num_entries > PSS_MAX_ENTRIES-1) {
243 ratio_step <<= 1;
244 num_entries >>= 1;
245 }
246
247 /* P[T] is Turbo state if enabled */
248 if (get_turbo_state() == TURBO_ENABLED) {
249 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100250 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200251
252 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
253 ratio_turbo = msr.lo & 0xff;
254
255 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100256 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200257 clock_max + 1, /*MHz*/
258 power_max, /*mW*/
259 PSS_LATENCY_TRANSITION, /*lat1*/
260 PSS_LATENCY_BUSMASTER, /*lat2*/
261 ratio_turbo << 8, /*control*/
262 ratio_turbo << 8); /*status*/
263 } else {
264 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100265 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200266 }
267
268 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100269 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200270 clock_max, /*MHz*/
271 power_max, /*mW*/
272 PSS_LATENCY_TRANSITION, /*lat1*/
273 PSS_LATENCY_BUSMASTER, /*lat2*/
274 ratio_max << 8, /*control*/
275 ratio_max << 8); /*status*/
276
277 /* Generate the remaining entries */
278 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
279 ratio >= ratio_min; ratio -= ratio_step) {
280
281 /* Calculate power at this ratio */
282 power = calculate_power(power_max, ratio_max, ratio);
283 clock = ratio * SANDYBRIDGE_BCLK;
284
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100285 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200286 clock, /*MHz*/
287 power, /*mW*/
288 PSS_LATENCY_TRANSITION, /*lat1*/
289 PSS_LATENCY_BUSMASTER, /*lat2*/
290 ratio << 8, /*control*/
291 ratio << 8); /*status*/
292 }
293
294 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100295 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200296}
297
Elyes HAOUASf925c562018-02-08 13:05:16 +0100298void generate_cpu_entries(struct device *device)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200299{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200300 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
301 int totalcores = dev_count_cpu();
302 int cores_per_package = get_cores_per_package();
303 int numcpus = totalcores/cores_per_package;
304
305 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
306 numcpus, cores_per_package);
307
Martin Roth9944b282014-08-11 11:24:55 -0600308 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700309 for (coreID = 1; coreID <= cores_per_package; coreID++) {
310 if (coreID > 1) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200311 pcontrol_blk = 0;
312 plen = 0;
313 }
314
Christian Walterbe3979c2019-12-18 15:07:59 +0100315 /* Generate processor \_SB.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100316 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200317 (cpuID-1)*cores_per_package+coreID-1,
318 pcontrol_blk, plen);
319
320 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100321 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200322 cpuID-1, cores_per_package);
323
324 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100325 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200326
327 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100328 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200329 cpuID-1, cores_per_package);
330
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100331 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200332 }
333 }
Arthur Heymans04008a92018-11-28 12:13:54 +0100334
335 /* PPKG is usually used for thermal management
336 of the first and only package. */
337 acpigen_write_processor_package("PPKG", 0, cores_per_package);
338
339 /* Add a method to notify processor nodes */
340 acpigen_write_processor_cnot(cores_per_package);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200341}
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};