blob: 9ff0673ece6d9706b8801ec182d9ddadd6384b86 [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
3#include <types.h>
4#include <console/console.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07005#include <acpi/acpi.h>
6#include <acpi/acpigen.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02007#include <arch/cpu.h>
8#include <cpu/x86/msr.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +02009#include <cpu/intel/speedstep.h>
10#include <cpu/intel/turbo.h>
11#include <device/device.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020012#include "model_206ax.h"
13#include "chip.h"
14
15static int get_cores_per_package(void)
16{
17 struct cpuinfo_x86 c;
18 struct cpuid_result result;
19 int cores = 1;
20
21 get_fms(&c, cpuid_eax(1));
22 if (c.x86 != 6)
23 return 1;
24
Stefan Reinauerbb31f3a2012-05-11 16:30:54 -070025 result = cpuid_ext(0xb, 1);
26 cores = result.ebx & 0xff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020027
28 return cores;
29}
30
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010031static void generate_cstate_entries(acpi_cstate_t *cstates,
32 int c1, int c2, int c3)
Stefan Reinauer5c554632012-04-04 00:09:50 +020033{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010034 int cstate_count = 0;
Stefan Reinauer5c554632012-04-04 00:09:50 +020035
36 /* Count number of active C-states */
37 if (c1 > 0)
38 ++cstate_count;
39 if (c2 > 0)
40 ++cstate_count;
41 if (c3 > 0)
42 ++cstate_count;
43 if (!cstate_count)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010044 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020045
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010046 acpigen_write_package(cstate_count + 1);
47 acpigen_write_byte(cstate_count);
Stefan Reinauer5c554632012-04-04 00:09:50 +020048
49 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020050 if (c1 > 0) {
51 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010052 acpigen_write_CST_package_entry(&cstates[c1]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020053 }
54 if (c2 > 0) {
55 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010056 acpigen_write_CST_package_entry(&cstates[c2]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020057 }
58 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070059 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010060 acpigen_write_CST_package_entry(&cstates[c3]);
Stefan Reinauerc31384e2012-04-27 23:13:39 +020061 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020062
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010063 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020064}
65
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010066static void generate_C_state_entries(void)
Stefan Reinauer5c554632012-04-04 00:09:50 +020067{
Sven Schnelle51676b12012-07-29 19:18:03 +020068 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020069 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110070 struct device *lapic;
Stefan Reinauer5c554632012-04-04 00:09:50 +020071 struct cpu_intel_model_206ax_config *conf = NULL;
72
73 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
74 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
75 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010076 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020077 conf = lapic->chip_info;
78 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010079 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020080
81 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +020082 info = cpu_info();
83 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010084 return;
Sven Schnelle51676b12012-07-29 19:18:03 +020085 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +020086 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010087 return;
Stefan Reinauer5c554632012-04-04 00:09:50 +020088
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +010089 acpigen_write_method("_CST", 0);
Stefan Reinauer5c554632012-04-04 00:09:50 +020090
91 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010092 acpigen_emit_byte(0xa0); /* IfOp */
93 acpigen_write_len_f(); /* PkgLength */
94 acpigen_emit_namestring("PWRS");
95 acpigen_emit_byte(0xa4); /* ReturnOp */
96 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
97 conf->c2_acpower, conf->c3_acpower);
98 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +020099
100 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100101 acpigen_emit_byte(0xa4); /* ReturnOp */
102 generate_cstate_entries(cpu->cstates, conf->c1_battery,
103 conf->c2_battery, conf->c3_battery);
104 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200105}
106
107static acpi_tstate_t tss_table_fine[] = {
108 { 100, 1000, 0, 0x00, 0 },
109 { 94, 940, 0, 0x1f, 0 },
110 { 88, 880, 0, 0x1e, 0 },
111 { 82, 820, 0, 0x1d, 0 },
112 { 75, 760, 0, 0x1c, 0 },
113 { 69, 700, 0, 0x1b, 0 },
114 { 63, 640, 0, 0x1a, 0 },
115 { 57, 580, 0, 0x19, 0 },
116 { 50, 520, 0, 0x18, 0 },
117 { 44, 460, 0, 0x17, 0 },
118 { 38, 400, 0, 0x16, 0 },
119 { 32, 340, 0, 0x15, 0 },
120 { 25, 280, 0, 0x14, 0 },
121 { 19, 220, 0, 0x13, 0 },
122 { 13, 160, 0, 0x12, 0 },
123};
124
125static acpi_tstate_t tss_table_coarse[] = {
126 { 100, 1000, 0, 0x00, 0 },
127 { 88, 875, 0, 0x1f, 0 },
128 { 75, 750, 0, 0x1e, 0 },
129 { 63, 625, 0, 0x1d, 0 },
130 { 50, 500, 0, 0x1c, 0 },
131 { 38, 375, 0, 0x1b, 0 },
132 { 25, 250, 0, 0x1a, 0 },
133 { 13, 125, 0, 0x19, 0 },
134};
135
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100136static void generate_T_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200137{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200138 /* Indicate SW_ALL coordination for T-states */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100139 acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200140
141 /* Indicate FFixedHW so OS will use MSR */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100142 acpigen_write_empty_PTC();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200143
144 /* Set a T-state limit that can be modified in NVS */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100145 acpigen_write_TPC("\\TLVL");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200146
147 /*
148 * CPUID.(EAX=6):EAX[5] indicates support
149 * for extended throttle levels.
150 */
151 if (cpuid_eax(6) & (1 << 5))
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100152 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200153 ARRAY_SIZE(tss_table_fine), tss_table_fine);
154 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100155 acpigen_write_TSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200156 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200157}
158
159static int calculate_power(int tdp, int p1_ratio, int ratio)
160{
161 u32 m;
162 u32 power;
163
164 /*
165 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
166 *
167 * Power = (ratio / p1_ratio) * m * tdp
168 */
169
170 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
171 m = (m * m) / 1000;
172
173 power = ((ratio * 100000 / p1_ratio) / 100);
174 power *= (m / 100) * (tdp / 1000);
175 power /= 1000;
176
177 return (int)power;
178}
179
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100180static void generate_P_state_entries(int core, int cores_per_package)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200181{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200182 int ratio_min, ratio_max, ratio_turbo, ratio_step;
183 int coord_type, power_max, power_unit, num_entries;
184 int ratio, power, clock, clock_max;
185 msr_t msr;
186
187 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
188 msr = rdmsr(MSR_MISC_PWR_MGMT);
189 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
190 coord_type = SW_ANY;
191 else
192 coord_type = HW_ALL;
193
194 /* Get bus ratio limits and calculate clock speeds */
195 msr = rdmsr(MSR_PLATFORM_INFO);
196 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700197
198 /* Determine if this CPU has configurable TDP */
199 if (cpu_config_tdp_levels()) {
200 /* Set max ratio to nominal TDP ratio */
201 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
202 ratio_max = msr.lo & 0xff;
203 } else {
204 /* Max Non-Turbo Ratio */
205 ratio_max = (msr.lo >> 8) & 0xff;
206 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200207 clock_max = ratio_max * SANDYBRIDGE_BCLK;
208
209 /* Calculate CPU TDP in mW */
210 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
211 power_unit = 2 << ((msr.lo & 0xf) - 1);
212 msr = rdmsr(MSR_PKG_POWER_SKU);
213 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
214
215 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100216 acpigen_write_empty_PCT();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200217
218 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100219 acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200220
221 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100222 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200223
224 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100225 acpigen_write_name("_PSS");
Stefan Reinauer5c554632012-04-04 00:09:50 +0200226
227 /* Determine ratio points */
228 ratio_step = PSS_RATIO_STEP;
229 num_entries = (ratio_max - ratio_min) / ratio_step;
230 while (num_entries > PSS_MAX_ENTRIES-1) {
231 ratio_step <<= 1;
232 num_entries >>= 1;
233 }
234
235 /* P[T] is Turbo state if enabled */
236 if (get_turbo_state() == TURBO_ENABLED) {
237 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100238 acpigen_write_package(num_entries + 2);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200239
240 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
241 ratio_turbo = msr.lo & 0xff;
242
243 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100244 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200245 clock_max + 1, /*MHz*/
246 power_max, /*mW*/
247 PSS_LATENCY_TRANSITION, /*lat1*/
248 PSS_LATENCY_BUSMASTER, /*lat2*/
249 ratio_turbo << 8, /*control*/
250 ratio_turbo << 8); /*status*/
251 } else {
252 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100253 acpigen_write_package(num_entries + 1);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200254 }
255
256 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100257 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200258 clock_max, /*MHz*/
259 power_max, /*mW*/
260 PSS_LATENCY_TRANSITION, /*lat1*/
261 PSS_LATENCY_BUSMASTER, /*lat2*/
262 ratio_max << 8, /*control*/
263 ratio_max << 8); /*status*/
264
265 /* Generate the remaining entries */
266 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
267 ratio >= ratio_min; ratio -= ratio_step) {
268
269 /* Calculate power at this ratio */
270 power = calculate_power(power_max, ratio_max, ratio);
271 clock = ratio * SANDYBRIDGE_BCLK;
272
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100273 acpigen_write_PSS_package(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200274 clock, /*MHz*/
275 power, /*mW*/
276 PSS_LATENCY_TRANSITION, /*lat1*/
277 PSS_LATENCY_BUSMASTER, /*lat2*/
278 ratio << 8, /*control*/
279 ratio << 8); /*status*/
280 }
281
282 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100283 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200284}
285
Furquan Shaikh7536a392020-04-24 21:59:21 -0700286void generate_cpu_entries(const struct device *device)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200287{
Stefan Reinauer5c554632012-04-04 00:09:50 +0200288 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
289 int totalcores = dev_count_cpu();
290 int cores_per_package = get_cores_per_package();
291 int numcpus = totalcores/cores_per_package;
292
293 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
294 numcpus, cores_per_package);
295
Martin Roth9944b282014-08-11 11:24:55 -0600296 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Lee Leahy9d62e7e2017-03-15 17:40:50 -0700297 for (coreID = 1; coreID <= cores_per_package; coreID++) {
298 if (coreID > 1) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200299 pcontrol_blk = 0;
300 plen = 0;
301 }
302
Christian Walterbe3979c2019-12-18 15:07:59 +0100303 /* Generate processor \_SB.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100304 acpigen_write_processor(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200305 (cpuID-1)*cores_per_package+coreID-1,
306 pcontrol_blk, plen);
307
308 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100309 generate_P_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200310 cpuID-1, cores_per_package);
311
312 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100313 generate_C_state_entries();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200314
315 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100316 generate_T_state_entries(
Stefan Reinauer5c554632012-04-04 00:09:50 +0200317 cpuID-1, cores_per_package);
318
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100319 acpigen_pop_len();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200320 }
321 }
Arthur Heymans04008a92018-11-28 12:13:54 +0100322
323 /* PPKG is usually used for thermal management
324 of the first and only package. */
325 acpigen_write_processor_package("PPKG", 0, cores_per_package);
326
327 /* Add a method to notify processor nodes */
328 acpigen_write_processor_cnot(cores_per_package);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200329}
330
331struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700332 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200333};