blob: f2cd6a93733cc6b3f985bc766ebef5e16dd5ba73 [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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23#include <types.h>
24#include <console/console.h>
25#include <arch/acpi.h>
26#include <arch/acpigen.h>
27#include <arch/cpu.h>
28#include <cpu/x86/msr.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020029#include <cpu/intel/speedstep.h>
30#include <cpu/intel/turbo.h>
31#include <device/device.h>
Stefan Reinauer5c554632012-04-04 00:09:50 +020032#include "model_206ax.h"
33#include "chip.h"
34
35static int get_cores_per_package(void)
36{
37 struct cpuinfo_x86 c;
38 struct cpuid_result result;
39 int cores = 1;
40
41 get_fms(&c, cpuid_eax(1));
42 if (c.x86 != 6)
43 return 1;
44
Stefan Reinauerbb31f3a2012-05-11 16:30:54 -070045 result = cpuid_ext(0xb, 1);
46 cores = result.ebx & 0xff;
Stefan Reinauer5c554632012-04-04 00:09:50 +020047
48 return cores;
49}
50
51static int generate_cstate_entries(acpi_cstate_t *cstates,
52 int c1, int c2, int c3)
53{
54 int length, cstate_count = 0;
55
56 /* Count number of active C-states */
57 if (c1 > 0)
58 ++cstate_count;
59 if (c2 > 0)
60 ++cstate_count;
61 if (c3 > 0)
62 ++cstate_count;
63 if (!cstate_count)
64 return 0;
65
66 length = acpigen_write_package(cstate_count + 1);
67 length += acpigen_write_byte(cstate_count);
68
69 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020070 if (c1 > 0) {
71 cstates[c1].ctype = 1;
72 length += acpigen_write_CST_package_entry(&cstates[c1]);
73 }
74 if (c2 > 0) {
75 cstates[c2].ctype = 2;
76 length += acpigen_write_CST_package_entry(&cstates[c2]);
77 }
78 if (c3 > 0) {
Duncan Laurieb38e0c32012-06-20 14:38:53 -070079 cstates[c3].ctype = 3;
Stefan Reinauerc31384e2012-04-27 23:13:39 +020080 length += acpigen_write_CST_package_entry(&cstates[c3]);
81 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020082
83 acpigen_patch_len(length - 1);
84 return length;
85}
86
87static int generate_C_state_entries(void)
88{
Sven Schnelle51676b12012-07-29 19:18:03 +020089 struct cpu_info *info;
Stefan Reinauer5c554632012-04-04 00:09:50 +020090 struct cpu_driver *cpu;
91 int len, lenif;
92 device_t lapic;
93 struct cpu_intel_model_206ax_config *conf = NULL;
94
95 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
96 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
97 if (!lapic)
98 return 0;
99 conf = lapic->chip_info;
100 if (!conf)
101 return 0;
102
103 /* Find CPU map of supported C-states */
Sven Schnelle51676b12012-07-29 19:18:03 +0200104 info = cpu_info();
105 if (!info)
Stefan Reinauer5c554632012-04-04 00:09:50 +0200106 return 0;
Sven Schnelle51676b12012-07-29 19:18:03 +0200107 cpu = find_cpu_driver(info->cpu);
Stefan Reinauer5c554632012-04-04 00:09:50 +0200108 if (!cpu || !cpu->cstates)
109 return 0;
110
111 len = acpigen_emit_byte(0x14); /* MethodOp */
112 len += acpigen_write_len_f(); /* PkgLength */
113 len += acpigen_emit_namestring("_CST");
114 len += acpigen_emit_byte(0x00); /* No Arguments */
115
116 /* If running on AC power */
117 len += acpigen_emit_byte(0xa0); /* IfOp */
118 lenif = acpigen_write_len_f(); /* PkgLength */
119 lenif += acpigen_emit_namestring("PWRS");
120 lenif += acpigen_emit_byte(0xa4); /* ReturnOp */
121 lenif += generate_cstate_entries(cpu->cstates, conf->c1_acpower,
122 conf->c2_acpower, conf->c3_acpower);
123 acpigen_patch_len(lenif - 1);
124 len += lenif;
125
126 /* Else on battery power */
127 len += acpigen_emit_byte(0xa4); /* ReturnOp */
128 len += generate_cstate_entries(cpu->cstates, conf->c1_battery,
129 conf->c2_battery, conf->c3_battery);
130 acpigen_patch_len(len - 1);
131 return len;
132}
133
134static acpi_tstate_t tss_table_fine[] = {
135 { 100, 1000, 0, 0x00, 0 },
136 { 94, 940, 0, 0x1f, 0 },
137 { 88, 880, 0, 0x1e, 0 },
138 { 82, 820, 0, 0x1d, 0 },
139 { 75, 760, 0, 0x1c, 0 },
140 { 69, 700, 0, 0x1b, 0 },
141 { 63, 640, 0, 0x1a, 0 },
142 { 57, 580, 0, 0x19, 0 },
143 { 50, 520, 0, 0x18, 0 },
144 { 44, 460, 0, 0x17, 0 },
145 { 38, 400, 0, 0x16, 0 },
146 { 32, 340, 0, 0x15, 0 },
147 { 25, 280, 0, 0x14, 0 },
148 { 19, 220, 0, 0x13, 0 },
149 { 13, 160, 0, 0x12, 0 },
150};
151
152static acpi_tstate_t tss_table_coarse[] = {
153 { 100, 1000, 0, 0x00, 0 },
154 { 88, 875, 0, 0x1f, 0 },
155 { 75, 750, 0, 0x1e, 0 },
156 { 63, 625, 0, 0x1d, 0 },
157 { 50, 500, 0, 0x1c, 0 },
158 { 38, 375, 0, 0x1b, 0 },
159 { 25, 250, 0, 0x1a, 0 },
160 { 13, 125, 0, 0x19, 0 },
161};
162
163static int generate_T_state_entries(int core, int cores_per_package)
164{
165 int len;
166
167 /* Indicate SW_ALL coordination for T-states */
168 len = acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
169
170 /* Indicate FFixedHW so OS will use MSR */
171 len += acpigen_write_empty_PTC();
172
173 /* Set a T-state limit that can be modified in NVS */
174 len += acpigen_write_TPC("\\TLVL");
175
176 /*
177 * CPUID.(EAX=6):EAX[5] indicates support
178 * for extended throttle levels.
179 */
180 if (cpuid_eax(6) & (1 << 5))
181 len += acpigen_write_TSS_package(
182 ARRAY_SIZE(tss_table_fine), tss_table_fine);
183 else
184 len += acpigen_write_TSS_package(
185 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
186
187 return len;
188}
189
190static int calculate_power(int tdp, int p1_ratio, int ratio)
191{
192 u32 m;
193 u32 power;
194
195 /*
196 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
197 *
198 * Power = (ratio / p1_ratio) * m * tdp
199 */
200
201 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
202 m = (m * m) / 1000;
203
204 power = ((ratio * 100000 / p1_ratio) / 100);
205 power *= (m / 100) * (tdp / 1000);
206 power /= 1000;
207
208 return (int)power;
209}
210
211static int generate_P_state_entries(int core, int cores_per_package)
212{
213 int len, len_pss;
214 int ratio_min, ratio_max, ratio_turbo, ratio_step;
215 int coord_type, power_max, power_unit, num_entries;
216 int ratio, power, clock, clock_max;
217 msr_t msr;
218
219 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
220 msr = rdmsr(MSR_MISC_PWR_MGMT);
221 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
222 coord_type = SW_ANY;
223 else
224 coord_type = HW_ALL;
225
226 /* Get bus ratio limits and calculate clock speeds */
227 msr = rdmsr(MSR_PLATFORM_INFO);
228 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
Duncan Laurie77dbbac2012-06-25 09:51:59 -0700229
230 /* Determine if this CPU has configurable TDP */
231 if (cpu_config_tdp_levels()) {
232 /* Set max ratio to nominal TDP ratio */
233 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
234 ratio_max = msr.lo & 0xff;
235 } else {
236 /* Max Non-Turbo Ratio */
237 ratio_max = (msr.lo >> 8) & 0xff;
238 }
Stefan Reinauer5c554632012-04-04 00:09:50 +0200239 clock_max = ratio_max * SANDYBRIDGE_BCLK;
240
241 /* Calculate CPU TDP in mW */
242 msr = rdmsr(MSR_PKG_POWER_SKU_UNIT);
243 power_unit = 2 << ((msr.lo & 0xf) - 1);
244 msr = rdmsr(MSR_PKG_POWER_SKU);
245 power_max = ((msr.lo & 0x7fff) / power_unit) * 1000;
246
247 /* Write _PCT indicating use of FFixedHW */
248 len = acpigen_write_empty_PCT();
249
250 /* Write _PPC with no limit on supported P-state */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700251 len += acpigen_write_PPC_NVS();
Stefan Reinauer5c554632012-04-04 00:09:50 +0200252
253 /* Write PSD indicating configured coordination type */
254 len += acpigen_write_PSD_package(core, cores_per_package, coord_type);
255
256 /* Add P-state entries in _PSS table */
257 len += acpigen_write_name("_PSS");
258
259 /* Determine ratio points */
260 ratio_step = PSS_RATIO_STEP;
261 num_entries = (ratio_max - ratio_min) / ratio_step;
262 while (num_entries > PSS_MAX_ENTRIES-1) {
263 ratio_step <<= 1;
264 num_entries >>= 1;
265 }
266
267 /* P[T] is Turbo state if enabled */
268 if (get_turbo_state() == TURBO_ENABLED) {
269 /* _PSS package count including Turbo */
270 len_pss = acpigen_write_package(num_entries + 2);
271
272 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
273 ratio_turbo = msr.lo & 0xff;
274
275 /* Add entry for Turbo ratio */
276 len_pss += acpigen_write_PSS_package(
277 clock_max + 1, /*MHz*/
278 power_max, /*mW*/
279 PSS_LATENCY_TRANSITION, /*lat1*/
280 PSS_LATENCY_BUSMASTER, /*lat2*/
281 ratio_turbo << 8, /*control*/
282 ratio_turbo << 8); /*status*/
283 } else {
284 /* _PSS package count without Turbo */
285 len_pss = acpigen_write_package(num_entries + 1);
286 }
287
288 /* First regular entry is max non-turbo ratio */
289 len_pss += acpigen_write_PSS_package(
290 clock_max, /*MHz*/
291 power_max, /*mW*/
292 PSS_LATENCY_TRANSITION, /*lat1*/
293 PSS_LATENCY_BUSMASTER, /*lat2*/
294 ratio_max << 8, /*control*/
295 ratio_max << 8); /*status*/
296
297 /* Generate the remaining entries */
298 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
299 ratio >= ratio_min; ratio -= ratio_step) {
300
301 /* Calculate power at this ratio */
302 power = calculate_power(power_max, ratio_max, ratio);
303 clock = ratio * SANDYBRIDGE_BCLK;
304
305 len_pss += acpigen_write_PSS_package(
306 clock, /*MHz*/
307 power, /*mW*/
308 PSS_LATENCY_TRANSITION, /*lat1*/
309 PSS_LATENCY_BUSMASTER, /*lat2*/
310 ratio << 8, /*control*/
311 ratio << 8); /*status*/
312 }
313
314 /* Fix package length */
315 len_pss--;
316 acpigen_patch_len(len_pss);
317
318 return len + len_pss;
319}
320
321void generate_cpu_entries(void)
322{
323 int len_pr;
324 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
325 int totalcores = dev_count_cpu();
326 int cores_per_package = get_cores_per_package();
327 int numcpus = totalcores/cores_per_package;
328
329 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
330 numcpus, cores_per_package);
331
Martin Roth9944b282014-08-11 11:24:55 -0600332 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Stefan Reinauer5c554632012-04-04 00:09:50 +0200333 for (coreID=1; coreID<=cores_per_package; coreID++) {
334 if (coreID>1) {
335 pcontrol_blk = 0;
336 plen = 0;
337 }
338
339 /* Generate processor \_PR.CPUx */
340 len_pr = acpigen_write_processor(
341 (cpuID-1)*cores_per_package+coreID-1,
342 pcontrol_blk, plen);
343
344 /* Generate P-state tables */
345 len_pr += generate_P_state_entries(
346 cpuID-1, cores_per_package);
347
348 /* Generate C-state tables */
349 len_pr += generate_C_state_entries();
350
351 /* Generate T-state tables */
352 len_pr += generate_T_state_entries(
353 cpuID-1, cores_per_package);
354
355 len_pr--;
356 acpigen_patch_len(len_pr);
357 }
358 }
359}
360
361struct chip_operations cpu_intel_model_206ax_ops = {
Stefan Reinauer0b7b7b62012-07-10 17:13:04 -0700362 CHIP_NAME("Intel SandyBridge/IvyBridge CPU")
Stefan Reinauer5c554632012-04-04 00:09:50 +0200363};