blob: bfe801d587d386e2059a557c96fa8ce8918a3076 [file] [log] [blame]
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +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/speedstep.h>
30#include <cpu/intel/turbo.h>
31#include <device/device.h>
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020032#include "model_2065x.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
45 result = cpuid_ext(0xb, 1);
46 cores = result.ebx & 0xff;
47
48 return cores;
49}
50
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010051static void generate_cstate_entries(acpi_cstate_t *cstates,
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020052 int c1, int c2, int c3)
53{
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010054 int cstate_count = 0;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020055
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)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010064 return;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020065
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010066 acpigen_write_package(cstate_count + 1);
67 acpigen_write_byte(cstate_count);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020068
69 /* Add an entry if the level is enabled */
70 if (c1 > 0) {
71 cstates[c1].ctype = 1;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010072 acpigen_write_CST_package_entry(&cstates[c1]);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020073 }
74 if (c2 > 0) {
75 cstates[c2].ctype = 2;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010076 acpigen_write_CST_package_entry(&cstates[c2]);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020077 }
78 if (c3 > 0) {
79 cstates[c3].ctype = 3;
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010080 acpigen_write_CST_package_entry(&cstates[c3]);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020081 }
82
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010083 acpigen_pop_len();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020084}
85
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010086static void generate_C_state_entries(void)
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020087{
88 struct cpu_info *info;
89 struct cpu_driver *cpu;
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +110090 struct device *lapic;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020091 struct cpu_intel_model_2065x_config *conf = NULL;
92
93 /* Find the SpeedStep CPU in the device tree using magic APIC ID */
94 lapic = dev_find_lapic(SPEEDSTEP_APIC_MAGIC);
95 if (!lapic)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010096 return;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +020097 conf = lapic->chip_info;
98 if (!conf)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +010099 return;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200100
101 /* Find CPU map of supported C-states */
102 info = cpu_info();
103 if (!info)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100104 return;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200105 cpu = find_cpu_driver(info->cpu);
106 if (!cpu || !cpu->cstates)
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100107 return;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200108
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100109 acpigen_emit_byte(0x14); /* MethodOp */
110 acpigen_write_len_f(); /* PkgLength */
111 acpigen_emit_namestring("_CST");
112 acpigen_emit_byte(0x00); /* No Arguments */
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200113
114 /* If running on AC power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100115 acpigen_emit_byte(0xa0); /* IfOp */
116 acpigen_write_len_f(); /* PkgLength */
117 acpigen_emit_namestring("PWRS");
118 acpigen_emit_byte(0xa4); /* ReturnOp */
119 generate_cstate_entries(cpu->cstates, conf->c1_acpower,
120 conf->c2_acpower, conf->c3_acpower);
121 acpigen_pop_len();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200122
123 /* Else on battery power */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100124 acpigen_emit_byte(0xa4); /* ReturnOp */
125 generate_cstate_entries(cpu->cstates, conf->c1_battery,
126 conf->c2_battery, conf->c3_battery);
127 acpigen_pop_len();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200128}
129
130static acpi_tstate_t tss_table_fine[] = {
131 { 100, 1000, 0, 0x00, 0 },
132 { 94, 940, 0, 0x1f, 0 },
133 { 88, 880, 0, 0x1e, 0 },
134 { 82, 820, 0, 0x1d, 0 },
135 { 75, 760, 0, 0x1c, 0 },
136 { 69, 700, 0, 0x1b, 0 },
137 { 63, 640, 0, 0x1a, 0 },
138 { 57, 580, 0, 0x19, 0 },
139 { 50, 520, 0, 0x18, 0 },
140 { 44, 460, 0, 0x17, 0 },
141 { 38, 400, 0, 0x16, 0 },
142 { 32, 340, 0, 0x15, 0 },
143 { 25, 280, 0, 0x14, 0 },
144 { 19, 220, 0, 0x13, 0 },
145 { 13, 160, 0, 0x12, 0 },
146};
147
148static acpi_tstate_t tss_table_coarse[] = {
149 { 100, 1000, 0, 0x00, 0 },
150 { 88, 875, 0, 0x1f, 0 },
151 { 75, 750, 0, 0x1e, 0 },
152 { 63, 625, 0, 0x1d, 0 },
153 { 50, 500, 0, 0x1c, 0 },
154 { 38, 375, 0, 0x1b, 0 },
155 { 25, 250, 0, 0x1a, 0 },
156 { 13, 125, 0, 0x19, 0 },
157};
158
159static int generate_T_state_entries(int core, int cores_per_package)
160{
161 int len;
162
163 /* Indicate SW_ALL coordination for T-states */
164 len = acpigen_write_TSD_package(core, cores_per_package, SW_ALL);
165
166 /* Indicate FFixedHW so OS will use MSR */
167 len += acpigen_write_empty_PTC();
168
169 /* Set a T-state limit that can be modified in NVS */
170 len += acpigen_write_TPC("\\TLVL");
171
172 /*
173 * CPUID.(EAX=6):EAX[5] indicates support
174 * for extended throttle levels.
175 */
176 if (cpuid_eax(6) & (1 << 5))
177 len += acpigen_write_TSS_package(
178 ARRAY_SIZE(tss_table_fine), tss_table_fine);
179 else
180 len += acpigen_write_TSS_package(
181 ARRAY_SIZE(tss_table_coarse), tss_table_coarse);
182
183 return len;
184}
185
186static int calculate_power(int tdp, int p1_ratio, int ratio)
187{
188 u32 m;
189 u32 power;
190
191 /*
192 * M = ((1.1 - ((p1_ratio - ratio) * 0.00625)) / 1.1) ^ 2
193 *
194 * Power = (ratio / p1_ratio) * m * tdp
195 */
196
197 m = (110000 - ((p1_ratio - ratio) * 625)) / 11;
198 m = (m * m) / 1000;
199
200 power = ((ratio * 100000 / p1_ratio) / 100);
201 power *= (m / 100) * (tdp / 1000);
202 power /= 1000;
203
204 return (int)power;
205}
206
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100207static void generate_P_state_entries(int core, int cores_per_package)
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200208{
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200209 int ratio_min, ratio_max, ratio_turbo, ratio_step;
210 int coord_type, power_max, num_entries;
211 int ratio, power, clock, clock_max;
212 msr_t msr;
213
214 /* Determine P-state coordination type from MISC_PWR_MGMT[0] */
215 msr = rdmsr(MSR_MISC_PWR_MGMT);
216 if (msr.lo & MISC_PWR_MGMT_EIST_HW_DIS)
217 coord_type = SW_ANY;
218 else
219 coord_type = HW_ALL;
220
221 /* Get bus ratio limits and calculate clock speeds */
222 msr = rdmsr(MSR_PLATFORM_INFO);
223 ratio_min = (msr.hi >> (40-32)) & 0xff; /* Max Efficiency Ratio */
224
225 /* Determine if this CPU has configurable TDP */
226 if (cpu_config_tdp_levels()) {
227 /* Set max ratio to nominal TDP ratio */
228 msr = rdmsr(MSR_CONFIG_TDP_NOMINAL);
229 ratio_max = msr.lo & 0xff;
230 } else {
231 /* Max Non-Turbo Ratio */
232 ratio_max = (msr.lo >> 8) & 0xff;
233 }
Vladimir Serbinenko71f35eb2013-11-12 23:32:52 +0100234 clock_max = ratio_max * NEHALEM_BCLK + ratio_max / 3;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200235
236 /* Calculate CPU TDP in mW */
237 power_max = 25000;
238
239 /* Write _PCT indicating use of FFixedHW */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100240 acpigen_write_empty_PCT();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200241
242 /* Write _PPC with no limit on supported P-state */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100243 acpigen_write_PPC_NVS();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200244
245 /* Write PSD indicating configured coordination type */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100246 acpigen_write_PSD_package(core, cores_per_package, coord_type);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200247
248 /* Add P-state entries in _PSS table */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100249 acpigen_write_name("_PSS");
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200250
251 /* Determine ratio points */
252 ratio_step = PSS_RATIO_STEP;
253 num_entries = (ratio_max - ratio_min) / ratio_step;
254 while (num_entries > PSS_MAX_ENTRIES-1) {
255 ratio_step <<= 1;
256 num_entries >>= 1;
257 }
258
259 /* P[T] is Turbo state if enabled */
260 if (get_turbo_state() == TURBO_ENABLED) {
261 /* _PSS package count including Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100262 acpigen_write_package(num_entries + 2);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200263
264 msr = rdmsr(MSR_TURBO_RATIO_LIMIT);
265 ratio_turbo = msr.lo & 0xff;
266
267 /* Add entry for Turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100268 acpigen_write_PSS_package(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200269 clock_max + 1, /*MHz*/
270 power_max, /*mW*/
271 PSS_LATENCY_TRANSITION, /*lat1*/
272 PSS_LATENCY_BUSMASTER, /*lat2*/
273 ratio_turbo, /*control*/
274 ratio_turbo); /*status*/
275 } else {
276 /* _PSS package count without Turbo */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100277 acpigen_write_package(num_entries + 1);
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200278 }
279
280 /* First regular entry is max non-turbo ratio */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100281 acpigen_write_PSS_package(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200282 clock_max, /*MHz*/
283 power_max, /*mW*/
284 PSS_LATENCY_TRANSITION, /*lat1*/
285 PSS_LATENCY_BUSMASTER, /*lat2*/
286 ratio_max, /*control*/
287 ratio_max); /*status*/
288
289 /* Generate the remaining entries */
290 for (ratio = ratio_min + ((num_entries - 1) * ratio_step);
291 ratio >= ratio_min; ratio -= ratio_step) {
292
293 /* Calculate power at this ratio */
294 power = calculate_power(power_max, ratio_max, ratio);
Vladimir Serbinenko71f35eb2013-11-12 23:32:52 +0100295 clock = ratio * NEHALEM_BCLK + ratio / 3;
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200296
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100297 acpigen_write_PSS_package(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200298 clock, /*MHz*/
299 power, /*mW*/
300 PSS_LATENCY_TRANSITION, /*lat1*/
301 PSS_LATENCY_BUSMASTER, /*lat2*/
302 ratio, /*control*/
303 ratio); /*status*/
304 }
305
306 /* Fix package length */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100307 acpigen_pop_len();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200308}
309
310void generate_cpu_entries(void)
311{
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200312 int coreID, cpuID, pcontrol_blk = PMB0_BASE, plen = 6;
313 int totalcores = dev_count_cpu();
314 int cores_per_package = get_cores_per_package();
315 int numcpus = totalcores/cores_per_package;
316
317 printk(BIOS_DEBUG, "Found %d CPU(s) with %d core(s) each.\n",
318 numcpus, cores_per_package);
319
Martin Roth9944b282014-08-11 11:24:55 -0600320 for (cpuID = 1; cpuID <= numcpus; cpuID++) {
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200321 for (coreID=1; coreID<=cores_per_package; coreID++) {
322 if (coreID>1) {
323 pcontrol_blk = 0;
324 plen = 0;
325 }
326
327 /* Generate processor \_PR.CPUx */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100328 acpigen_write_processor(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200329 (cpuID-1)*cores_per_package+coreID-1,
330 pcontrol_blk, plen);
331
332 /* Generate P-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100333 generate_P_state_entries(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200334 cpuID-1, cores_per_package);
335
336 /* Generate C-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100337 generate_C_state_entries();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200338
339 /* Generate T-state tables */
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100340 generate_T_state_entries(
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200341 cpuID-1, cores_per_package);
342
Vladimir Serbinenko226d7842014-11-04 21:09:23 +0100343 acpigen_pop_len();
Vladimir Serbinenko22dcdd92013-06-06 22:10:45 +0200344 }
345 }
346}
347
348struct chip_operations cpu_intel_model_2065x_ops = {
349 CHIP_NAME("Intel Nehalem CPU")
350};