blob: 1a8ceb719ca07407b42f1d5b6e6c3dff2db6cace [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>
29#include <cpu/intel/acpi.h>
30#include <cpu/intel/speedstep.h>
31#include <cpu/intel/turbo.h>
32#include <device/device.h>
33#include <device/pci.h>
34#include "model_206ax.h"
35#include "chip.h"
36
37static int get_cores_per_package(void)
38{
39 struct cpuinfo_x86 c;
40 struct cpuid_result result;
41 int cores = 1;
42
43 get_fms(&c, cpuid_eax(1));
44 if (c.x86 != 6)
45 return 1;
46
47 switch (c.x86_model) {
48 case CONFIG_CPU_MODEL_INDEX:
49 result = cpuid_ext(0xb, 1);
50 cores = result.ebx & 0xff;
51 break;
52 default:
53 cores = (cpuid_ebx(1) >> 16) & 0xff;
54 break;
55 }
56
57 return cores;
58}
59
60static int generate_cstate_entries(acpi_cstate_t *cstates,
61 int c1, int c2, int c3)
62{
63 int length, cstate_count = 0;
64
65 /* Count number of active C-states */
66 if (c1 > 0)
67 ++cstate_count;
68 if (c2 > 0)
69 ++cstate_count;
70 if (c3 > 0)
71 ++cstate_count;
72 if (!cstate_count)
73 return 0;
74
75 length = acpigen_write_package(cstate_count + 1);
76 length += acpigen_write_byte(cstate_count);
77
78 /* Add an entry if the level is enabled */
Stefan Reinauerc31384e2012-04-27 23:13:39 +020079 if (c1 > 0) {
80 cstates[c1].ctype = 1;
81 length += acpigen_write_CST_package_entry(&cstates[c1]);
82 }
83 if (c2 > 0) {
84 cstates[c2].ctype = 2;
85 length += acpigen_write_CST_package_entry(&cstates[c2]);
86 }
87 if (c3 > 0) {
88 cstates[c2].ctype = 2;
89 length += acpigen_write_CST_package_entry(&cstates[c3]);
90 }
Stefan Reinauer5c554632012-04-04 00:09:50 +020091
92 acpigen_patch_len(length - 1);
93 return length;
94}
95
96static int generate_C_state_entries(void)
97{
98 struct cpu_info *info;
99 struct cpu_driver *cpu;
100 int len, lenif;
101 device_t lapic;
102 struct cpu_intel_model_206ax_config *conf = NULL;
103
104 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
105 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
106 if (!lapic)
107 return 0;
108 conf = lapic->chip_info;
109 if (!conf)
110 return 0;
111
112 /* Find CPU map of supported C-states */
113 info = cpu_info();
114 if (!info)
115 return 0;
116 cpu = find_cpu_driver(info->cpu);
117 if (!cpu || !cpu->cstates)
118 return 0;
119
120 len = acpigen_emit_byte(0x14); /* MethodOp */
121 len += acpigen_write_len_f(); /* PkgLength */
122 len += acpigen_emit_namestring("_CST");
123 len += acpigen_emit_byte(0x00); /* No Arguments */
124
125 /* If running on AC power */
126 len += acpigen_emit_byte(0xa0); /* IfOp */
127 lenif = acpigen_write_len_f(); /* PkgLength */
128 lenif += acpigen_emit_namestring("PWRS");
129 lenif += acpigen_emit_byte(0xa4); /* ReturnOp */
130 lenif += generate_cstate_entries(cpu->cstates, conf->c1_acpower,
131 conf->c2_acpower, conf->c3_acpower);
132 acpigen_patch_len(lenif - 1);
133 len += lenif;
134
135 /* Else on battery power */
136 len += acpigen_emit_byte(0xa4); /* ReturnOp */
137 len += generate_cstate_entries(cpu->cstates, conf->c1_battery,
138 conf->c2_battery, conf->c3_battery);
139 acpigen_patch_len(len - 1);
140 return len;
141}
142
143static acpi_tstate_t tss_table_fine[] = {
144 { 100, 1000, 0, 0x00, 0 },
145 { 94, 940, 0, 0x1f, 0 },
146 { 88, 880, 0, 0x1e, 0 },
147 { 82, 820, 0, 0x1d, 0 },
148 { 75, 760, 0, 0x1c, 0 },
149 { 69, 700, 0, 0x1b, 0 },
150 { 63, 640, 0, 0x1a, 0 },
151 { 57, 580, 0, 0x19, 0 },
152 { 50, 520, 0, 0x18, 0 },
153 { 44, 460, 0, 0x17, 0 },
154 { 38, 400, 0, 0x16, 0 },
155 { 32, 340, 0, 0x15, 0 },
156 { 25, 280, 0, 0x14, 0 },
157 { 19, 220, 0, 0x13, 0 },
158 { 13, 160, 0, 0x12, 0 },
159};
160
161static acpi_tstate_t tss_table_coarse[] = {
162 { 100, 1000, 0, 0x00, 0 },
163 { 88, 875, 0, 0x1f, 0 },
164 { 75, 750, 0, 0x1e, 0 },
165 { 63, 625, 0, 0x1d, 0 },
166 { 50, 500, 0, 0x1c, 0 },
167 { 38, 375, 0, 0x1b, 0 },
168 { 25, 250, 0, 0x1a, 0 },
169 { 13, 125, 0, 0x19, 0 },
170};
171
172static int generate_T_state_entries(int core, int cores_per_package)
173{
174 int len;
175
176 /* Indicate SW_ALL coordination for T-states */
177 len = acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
178
179 /* Indicate FFixedHW so OS will use MSR */
180 len += acpigen_write_empty_PTC();
181
182 /* Set a T-state limit that can be modified in NVS */
183 len += acpigen_write_TPC("\\TLVL");
184
185 /*
186 * CPUID.(EAX=6):EAX[5] indicates support
187 * for extended throttle levels.
188 */
189 if (cpuid_eax(6) & (1 << 5))
190 len += acpigen_write_TSS_package(
191 ARRAY_SIZE(tss_table_fine), tss_table_fine);
192 else
193 len += acpigen_write_TSS_package(
194 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
195
196 return len;
197}
198
199static int calculate_power(int tdp, int p1_ratio, int ratio)
200{
201 u32 m;
202 u32 power;
203
204 /*
205 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
206 *
207 * Power = (ratio / p1_ratio) * m * tdp
208 */
209
210 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
211 m = (m * m) / 1000;
212
213 power = ((ratio * 100000 / p1_ratio) / 100);
214 power *= (m / 100) * (tdp / 1000);
215 power /= 1000;
216
217 return (int)power;
218}
219
220static int generate_P_state_entries(int core, int cores_per_package)
221{
222 int len, len_pss;
223 int ratio_min, ratio_max, ratio_turbo, ratio_step;
224 int coord_type, power_max, power_unit, num_entries;
225 int ratio, power, clock, clock_max;
226 msr_t msr;
227
228 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
229 msr = rdmsr(MSR_MISC_PWR_MGMT);
230 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
231 coord_type = SW_ANY;
232 else
233 coord_type = HW_ALL;
234
235 /* Get bus ratio limits and calculate clock speeds */
236 msr = rdmsr(MSR_PLATFORM_INFO);
237 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
238 ratio_max = (msr.lo >> 8) & 0xff; /* Max Non-Turbo Ratio */
239 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 */
251 len += acpigen_write_PPC(0);
252
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
332 for (cpuID=1; cpuID <=numcpus; cpuID++) {
333 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 = {
362 CHIP_NAME(CONFIG_CPU_MODEL_NAME)
363};