blob: b24f411f00e12f1160a579cb5ad4d83a8e910379 [file] [log] [blame]
Angel Ponsf23ae0b2020-04-02 23:48:12 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer5c554632012-04-04 00:09:50 +02002
Stefan Reinauer5c554632012-04-04 00:09:50 +02003#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpi.h>
5#include <acpi/acpigen.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02006#include <arch/cpu.h>
7#include <cpu/x86/msr.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02008#include <cpu/intel/speedstep.h>
9#include <cpu/intel/turbo.h>
10#include <device/device.h>
Elyes HAOUAS7cf1f202020-07-22 07:53:53 +020011#include <stdint.h>
12
Stefan Reinauer5c554632012-04-04 00:09:50 +020013#include "model_206ax.h"
14#include "chip.h"
15
Evgeny Zinoviev920d2b72020-06-16 08:23:09 +030016static int get_logical_cores_per_package(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020017{
Evgeny Zinoviev920d2b72020-06-16 08:23:09 +030018 msr_t msr = rdmsr(MSR_CORE_THREAD_COUNT);
19 return msr.lo & 0xffff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020020}
21
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010022static void generate_cstate_entries(acpi_cstate_t *cstates,
23 int c1, int c2, int c3)
Stefan Reinauer5c554632012-04-04 00:09:50 +020024{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010025 int cstate_count = 0;
Stefan Reinauer5c554632012-04-04 00:09:50 +020026
27 /* Count number of active C-states */
28 if (c1 > 0)
29 ++cstate_count;
30 if (c2 > 0)
31 ++cstate_count;
32 if (c3 > 0)
33 ++cstate_count;
34 if (!cstate_count)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010035 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020036
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010037 acpigen_write_package(cstate_count + 1);
38 acpigen_write_byte(cstate_count);
Stefan Reinauer5c554632012-04-04 00:09:50 +020039
40 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020041 if (c1 > 0) {
42 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010043 acpigen_write_CST_package_entry(&cstates[c1]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020044 }
45 if (c2 > 0) {
46 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010047 acpigen_write_CST_package_entry(&cstates[c2]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020048 }
49 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070050 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010051 acpigen_write_CST_package_entry(&cstates[c3]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020052 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020053
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010054 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020055}
56
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010057static void generate_C_state_entries(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020058{
Sven Schnelle51676b12012-07-29 19:18:03 +020059 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020060 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110061 struct device *lapic;
Stefan Reinauer5c554632012-04-04 00:09:50 +020062 struct cpu_intel_model_206ax_config *conf = NULL;
63
64 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
65 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
66 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010067 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020068 conf = lapic->chip_info;
69 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010070 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020071
72 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +020073 info = cpu_info();
74 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010075 return;
Sven Schnelle51676b12012-07-29 19:18:03 +020076 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +020077 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010078 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020079
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +010080 acpigen_write_method("_CST", 0);
Stefan Reinauer5c554632012-04-04 00:09:50 +020081
82 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010083 acpigen_emit_byte(0xa0); /* IfOp */
84 acpigen_write_len_f(); /* PkgLength */
85 acpigen_emit_namestring("PWRS");
86 acpigen_emit_byte(0xa4); /* ReturnOp */
87 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
88 conf->c2_acpower, conf->c3_acpower);
89 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020090
91 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010092 acpigen_emit_byte(0xa4); /* ReturnOp */
93 generate_cstate_entries(cpu->cstates, conf->c1_battery,
94 conf->c2_battery, conf->c3_battery);
95 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020096}
97
98static acpi_tstate_t tss_table_fine[] = {
99 { 100, 1000, 0, 0x00, 0 },
100 { 94, 940, 0, 0x1f, 0 },
101 { 88, 880, 0, 0x1e, 0 },
102 { 82, 820, 0, 0x1d, 0 },
103 { 75, 760, 0, 0x1c, 0 },
104 { 69, 700, 0, 0x1b, 0 },
105 { 63, 640, 0, 0x1a, 0 },
106 { 57, 580, 0, 0x19, 0 },
107 { 50, 520, 0, 0x18, 0 },
108 { 44, 460, 0, 0x17, 0 },
109 { 38, 400, 0, 0x16, 0 },
110 { 32, 340, 0, 0x15, 0 },
111 { 25, 280, 0, 0x14, 0 },
112 { 19, 220, 0, 0x13, 0 },
113 { 13, 160, 0, 0x12, 0 },
114};
115
116static acpi_tstate_t tss_table_coarse[] = {
117 { 100, 1000, 0, 0x00, 0 },
118 { 88, 875, 0, 0x1f, 0 },
119 { 75, 750, 0, 0x1e, 0 },
120 { 63, 625, 0, 0x1d, 0 },
121 { 50, 500, 0, 0x1c, 0 },
122 { 38, 375, 0, 0x1b, 0 },
123 { 25, 250, 0, 0x1a, 0 },
124 { 13, 125, 0, 0x19, 0 },
125};
126
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100127static void generate_T_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200128{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200129 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100130 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200131
132 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100133 acpigen_write_empty_PTC();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200134
135 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100136 acpigen_write_TPC("\\TLVL");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200137
138 /*
139 * CPUID.(EAX=6):EAX[5] indicates support
140 * for extended throttle levels.
141 */
142 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100143 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200144 ARRAY_SIZE(tss_table_fine), tss_table_fine);
145 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100146 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200147 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200148}
149
150static int calculate_power(int tdp, int p1_ratio, int ratio)
151{
152 u32 m;
153 u32 power;
154
155 /*
156 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
157 *
158 * Power = (ratio / p1_ratio) * m * tdp
159 */
160
161 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
162 m = (m * m) / 1000;
163
164 power = ((ratio * 100000 / p1_ratio) / 100);
165 power *= (m / 100) * (tdp / 1000);
166 power /= 1000;
167
168 return (int)power;
169}
170
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100171static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200172{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200173 int ratio_min, ratio_max, ratio_turbo, ratio_step;
174 int coord_type, power_max, power_unit, num_entries;
175 int ratio, power, clock, clock_max;
176 msr_t msr;
177
178 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
179 msr = rdmsr(MSR_MISC_PWR_MGMT);
180 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
181 coord_type = SW_ANY;
182 else
183 coord_type = HW_ALL;
184
185 /* Get bus ratio limits and calculate clock speeds */
186 msr = rdmsr(MSR_PLATFORM_INFO);
187 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700188
189 /* Determine if this CPU has configurable TDP */
190 if (cpu_config_tdp_levels()) {
191 /* Set max ratio to nominal TDP ratio */
192 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
193 ratio_max = msr.lo & 0xff;
194 } else {
195 /* Max Non-Turbo Ratio */
196 ratio_max = (msr.lo >> 8) & 0xff;
197 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200198 clock_max = ratio_max * SANDYBRIDGE_BCLK;
199
200 /* Calculate CPU TDP in mW */
201 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
202 power_unit = 2 << ((msr.lo & 0xf) - 1);
203 msr = rdmsr(MSR_PKG_POWER_SKU);
204 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
205
206 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100207 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200208
209 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100210 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200211
212 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100213 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200214
215 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100216 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200217
218 /* Determine ratio points */
219 ratio_step = PSS_RATIO_STEP;
220 num_entries = (ratio_max - ratio_min) / ratio_step;
221 while (num_entries > PSS_MAX_ENTRIES-1) {
222 ratio_step <<= 1;
223 num_entries >>= 1;
224 }
225
226 /* P[T] is Turbo state if enabled */
227 if (get_turbo_state() == TURBO_ENABLED) {
228 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100229 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200230
231 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
232 ratio_turbo = msr.lo & 0xff;
233
234 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100235 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200236 clock_max + 1, /*MHz*/
237 power_max, /*mW*/
238 PSS_LATENCY_TRANSITION, /*lat1*/
239 PSS_LATENCY_BUSMASTER, /*lat2*/
240 ratio_turbo << 8, /*control*/
241 ratio_turbo << 8); /*status*/
242 } else {
243 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100244 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200245 }
246
247 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100248 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200249 clock_max, /*MHz*/
250 power_max, /*mW*/
251 PSS_LATENCY_TRANSITION, /*lat1*/
252 PSS_LATENCY_BUSMASTER, /*lat2*/
253 ratio_max << 8, /*control*/
254 ratio_max << 8); /*status*/
255
256 /* Generate the remaining entries */
257 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
258 ratio >= ratio_min; ratio -= ratio_step) {
259
260 /* Calculate power at this ratio */
261 power = calculate_power(power_max, ratio_max, ratio);
262 clock = ratio * SANDYBRIDGE_BCLK;
263
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100264 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200265 clock, /*MHz*/
266 power, /*mW*/
267 PSS_LATENCY_TRANSITION, /*lat1*/
268 PSS_LATENCY_BUSMASTER, /*lat2*/
269 ratio << 8, /*control*/
270 ratio << 8); /*status*/
271 }
272
273 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100274 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200275}
276
Furquan Shaikh7536a392020-04-24 21:59:21 -0700277void generate_cpu_entries(const struct device *device)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200278{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200279 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
280 int totalcores = dev_count_cpu();
Evgeny Zinoviev920d2b72020-06-16 08:23:09 +0300281 int cores_per_package = get_logical_cores_per_package();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200282 int numcpus = totalcores/cores_per_package;
283
284 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
285 numcpus, cores_per_package);
286
Martin Roth9944b282014-08-11 11:24:55 -0600287 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700288 for (coreID = 1; coreID <= cores_per_package; coreID++) {
289 if (coreID > 1) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200290 pcontrol_blk = 0;
291 plen = 0;
292 }
293
Christian Walterbe3979c2019-12-18 15:07:59 +0100294 /* Generate processor \_SB.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100295 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200296 (cpuID-1)*cores_per_package+coreID-1,
297 pcontrol_blk, plen);
298
299 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100300 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200301 cpuID-1, cores_per_package);
302
303 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100304 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200305
306 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100307 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200308 cpuID-1, cores_per_package);
309
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100310 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200311 }
312 }
Arthur Heymans04008a92018-11-28 12:13:54 +0100313
314 /* PPKG is usually used for thermal management
315 of the first and only package. */
316 acpigen_write_processor_package("PPKG", 0, cores_per_package);
317
318 /* Add a method to notify processor nodes */
319 acpigen_write_processor_cnot(cores_per_package);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200320}
321
322struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700323 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200324};