blob: 13ee20a728fbd32be75e74deef9dfa08aee081ee [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
16static int get_cores_per_package(void)
17{
18 struct cpuinfo_x86 c;
19 struct cpuid_result result;
20 int cores = 1;
21
22 get_fms(&c, cpuid_eax(1));
23 if (c.x86 != 6)
24 return 1;
25
Stefan Reinauerbb31f3a2012-05-11 16:30:54 -070026 result = cpuid_ext(0xb, 1);
27 cores = result.ebx & 0xff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020028
29 return cores;
30}
31
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010032static void generate_cstate_entries(acpi_cstate_t *cstates,
33 int c1, int c2, int c3)
Stefan Reinauer5c554632012-04-04 00:09:50 +020034{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010035 int cstate_count = 0;
Stefan Reinauer5c554632012-04-04 00:09:50 +020036
37 /* Count number of active C-states */
38 if (c1 > 0)
39 ++cstate_count;
40 if (c2 > 0)
41 ++cstate_count;
42 if (c3 > 0)
43 ++cstate_count;
44 if (!cstate_count)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010045 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020046
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010047 acpigen_write_package(cstate_count + 1);
48 acpigen_write_byte(cstate_count);
Stefan Reinauer5c554632012-04-04 00:09:50 +020049
50 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020051 if (c1 > 0) {
52 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010053 acpigen_write_CST_package_entry(&cstates[c1]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020054 }
55 if (c2 > 0) {
56 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010057 acpigen_write_CST_package_entry(&cstates[c2]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020058 }
59 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070060 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010061 acpigen_write_CST_package_entry(&cstates[c3]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020062 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020063
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010064 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020065}
66
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010067static void generate_C_state_entries(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020068{
Sven Schnelle51676b12012-07-29 19:18:03 +020069 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020070 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110071 struct device *lapic;
Stefan Reinauer5c554632012-04-04 00:09:50 +020072 struct cpu_intel_model_206ax_config *conf = NULL;
73
74 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
75 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
76 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010077 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020078 conf = lapic->chip_info;
79 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010080 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020081
82 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +020083 info = cpu_info();
84 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010085 return;
Sven Schnelle51676b12012-07-29 19:18:03 +020086 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +020087 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010088 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020089
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +010090 acpigen_write_method("_CST", 0);
Stefan Reinauer5c554632012-04-04 00:09:50 +020091
92 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010093 acpigen_emit_byte(0xa0); /* IfOp */
94 acpigen_write_len_f(); /* PkgLength */
95 acpigen_emit_namestring("PWRS");
96 acpigen_emit_byte(0xa4); /* ReturnOp */
97 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
98 conf->c2_acpower, conf->c3_acpower);
99 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200100
101 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100102 acpigen_emit_byte(0xa4); /* ReturnOp */
103 generate_cstate_entries(cpu->cstates, conf->c1_battery,
104 conf->c2_battery, conf->c3_battery);
105 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200106}
107
108static acpi_tstate_t tss_table_fine[] = {
109 { 100, 1000, 0, 0x00, 0 },
110 { 94, 940, 0, 0x1f, 0 },
111 { 88, 880, 0, 0x1e, 0 },
112 { 82, 820, 0, 0x1d, 0 },
113 { 75, 760, 0, 0x1c, 0 },
114 { 69, 700, 0, 0x1b, 0 },
115 { 63, 640, 0, 0x1a, 0 },
116 { 57, 580, 0, 0x19, 0 },
117 { 50, 520, 0, 0x18, 0 },
118 { 44, 460, 0, 0x17, 0 },
119 { 38, 400, 0, 0x16, 0 },
120 { 32, 340, 0, 0x15, 0 },
121 { 25, 280, 0, 0x14, 0 },
122 { 19, 220, 0, 0x13, 0 },
123 { 13, 160, 0, 0x12, 0 },
124};
125
126static acpi_tstate_t tss_table_coarse[] = {
127 { 100, 1000, 0, 0x00, 0 },
128 { 88, 875, 0, 0x1f, 0 },
129 { 75, 750, 0, 0x1e, 0 },
130 { 63, 625, 0, 0x1d, 0 },
131 { 50, 500, 0, 0x1c, 0 },
132 { 38, 375, 0, 0x1b, 0 },
133 { 25, 250, 0, 0x1a, 0 },
134 { 13, 125, 0, 0x19, 0 },
135};
136
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100137static void generate_T_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200138{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200139 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100140 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200141
142 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100143 acpigen_write_empty_PTC();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200144
145 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100146 acpigen_write_TPC("\\TLVL");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200147
148 /*
149 * CPUID.(EAX=6):EAX[5] indicates support
150 * for extended throttle levels.
151 */
152 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100153 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200154 ARRAY_SIZE(tss_table_fine), tss_table_fine);
155 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100156 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200157 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200158}
159
160static int calculate_power(int tdp, int p1_ratio, int ratio)
161{
162 u32 m;
163 u32 power;
164
165 /*
166 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
167 *
168 * Power = (ratio / p1_ratio) * m * tdp
169 */
170
171 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
172 m = (m * m) / 1000;
173
174 power = ((ratio * 100000 / p1_ratio) / 100);
175 power *= (m / 100) * (tdp / 1000);
176 power /= 1000;
177
178 return (int)power;
179}
180
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100181static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200182{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200183 int ratio_min, ratio_max, ratio_turbo, ratio_step;
184 int coord_type, power_max, power_unit, num_entries;
185 int ratio, power, clock, clock_max;
186 msr_t msr;
187
188 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
189 msr = rdmsr(MSR_MISC_PWR_MGMT);
190 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
191 coord_type = SW_ANY;
192 else
193 coord_type = HW_ALL;
194
195 /* Get bus ratio limits and calculate clock speeds */
196 msr = rdmsr(MSR_PLATFORM_INFO);
197 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700198
199 /* Determine if this CPU has configurable TDP */
200 if (cpu_config_tdp_levels()) {
201 /* Set max ratio to nominal TDP ratio */
202 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
203 ratio_max = msr.lo & 0xff;
204 } else {
205 /* Max Non-Turbo Ratio */
206 ratio_max = (msr.lo >> 8) & 0xff;
207 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200208 clock_max = ratio_max * SANDYBRIDGE_BCLK;
209
210 /* Calculate CPU TDP in mW */
211 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
212 power_unit = 2 << ((msr.lo & 0xf) - 1);
213 msr = rdmsr(MSR_PKG_POWER_SKU);
214 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
215
216 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100217 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200218
219 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100220 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200221
222 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100223 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200224
225 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100226 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200227
228 /* Determine ratio points */
229 ratio_step = PSS_RATIO_STEP;
230 num_entries = (ratio_max - ratio_min) / ratio_step;
231 while (num_entries > PSS_MAX_ENTRIES-1) {
232 ratio_step <<= 1;
233 num_entries >>= 1;
234 }
235
236 /* P[T] is Turbo state if enabled */
237 if (get_turbo_state() == TURBO_ENABLED) {
238 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100239 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200240
241 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
242 ratio_turbo = msr.lo & 0xff;
243
244 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100245 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200246 clock_max + 1, /*MHz*/
247 power_max, /*mW*/
248 PSS_LATENCY_TRANSITION, /*lat1*/
249 PSS_LATENCY_BUSMASTER, /*lat2*/
250 ratio_turbo << 8, /*control*/
251 ratio_turbo << 8); /*status*/
252 } else {
253 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100254 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200255 }
256
257 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100258 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200259 clock_max, /*MHz*/
260 power_max, /*mW*/
261 PSS_LATENCY_TRANSITION, /*lat1*/
262 PSS_LATENCY_BUSMASTER, /*lat2*/
263 ratio_max << 8, /*control*/
264 ratio_max << 8); /*status*/
265
266 /* Generate the remaining entries */
267 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
268 ratio >= ratio_min; ratio -= ratio_step) {
269
270 /* Calculate power at this ratio */
271 power = calculate_power(power_max, ratio_max, ratio);
272 clock = ratio * SANDYBRIDGE_BCLK;
273
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100274 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200275 clock, /*MHz*/
276 power, /*mW*/
277 PSS_LATENCY_TRANSITION, /*lat1*/
278 PSS_LATENCY_BUSMASTER, /*lat2*/
279 ratio << 8, /*control*/
280 ratio << 8); /*status*/
281 }
282
283 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100284 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200285}
286
Furquan Shaikh7536a392020-04-24 21:59:21 -0700287void generate_cpu_entries(const struct device *device)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200288{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200289 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
290 int totalcores = dev_count_cpu();
291 int cores_per_package = get_cores_per_package();
292 int numcpus = totalcores/cores_per_package;
293
294 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
295 numcpus, cores_per_package);
296
Martin Roth9944b282014-08-11 11:24:55 -0600297 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700298 for (coreID = 1; coreID <= cores_per_package; coreID++) {
299 if (coreID > 1) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200300 pcontrol_blk = 0;
301 plen = 0;
302 }
303
Christian Walterbe3979c2019-12-18 15:07:59 +0100304 /* Generate processor \_SB.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100305 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200306 (cpuID-1)*cores_per_package+coreID-1,
307 pcontrol_blk, plen);
308
309 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100310 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200311 cpuID-1, cores_per_package);
312
313 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100314 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200315
316 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100317 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200318 cpuID-1, cores_per_package);
319
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100320 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200321 }
322 }
Arthur Heymans04008a92018-11-28 12:13:54 +0100323
324 /* PPKG is usually used for thermal management
325 of the first and only package. */
326 acpigen_write_processor_package("PPKG", 0, cores_per_package);
327
328 /* Add a method to notify processor nodes */
329 acpigen_write_processor_cnot(cores_per_package);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200330}
331
332struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700333 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200334};