2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License v2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <console/console.h>
23 #include <cpu/x86/msr.h>
24 #include <arch/acpigen.h>
25 #include <cpu/amd/model_fxx_powernow.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <cpu/x86/msr.h>
29 #include <cpu/amd/mtrr.h>
30 #include <cpu/amd/amdk8_sysconf.h>
33 static int write_pstates_for_core(u8 pstate_num, u16 *pstate_feq, u8 *pstate_vid,
34 u8 *pstate_fid, u32 *pstate_power, int coreID,
35 u32 pcontrol_blk, u8 plen, u8 onlyBSP) {
38 if ((onlyBSP) && (coreID != 0)) {
43 lenpr = acpigen_write_processor(coreID, pcontrol_blk, plen);
44 lenpr += acpigen_write_empty_PCT();
45 lenpr += acpigen_write_name("_PSS");
47 /* add later to total sum */
48 lenp = acpigen_write_package(pstate_num);
50 for (i = 0;i < pstate_num;i++) {
54 (0x3 << 30) | /* IRT */
55 (0x2 << 28) | /* RVO */
56 (0x1 << 27) | /* ExtType */
57 (0x2 << 20) | /* PLL_LOCK_TIME */
58 (0x0 << 18) | /* MVS */
59 (0x5 << 11) | /* VST */
60 (pstate_vid[i] << 6) |
63 (pstate_vid[i] << 6) |
66 lenp += acpigen_write_PSS_package(pstate_feq[i],
73 /* update the package size */
74 acpigen_patch_len(lenp - 1);
77 lenpr += acpigen_write_PPC(pstate_num);
78 /* patch the whole Processor token length */
79 acpigen_patch_len(lenpr - 2);
83 * Details about this algorithm , refert to BDKG 10.5.1
84 * Two parts are included, the another is the DSDT reconstruction process
87 static int pstates_algorithm(u32 pcontrol_blk, u8 plen, u8 onlyBSP)
90 u8 processor_brand[49];
92 struct cpuid_result cpuid1;
94 struct power_limit_encoding {
100 u8 Max_fid, Max_vid, Start_fid, Start_vid, Min_fid, Min_vid;
105 u32 Pstate_power[10];
107 u8 PstateStep, PstateStep_coef;
117 static struct power_limit_encoding TDP[20] = {
118 {0x11, 0x0, 0x8, 62},
119 {0x11, 0x1, 0x8, 89},
120 {0x11, 0x1, 0xa, 103},
121 {0x11, 0x1, 0xc, 125},
122 {0x11, 0x0, 0x2, 15},
123 {0x11, 0x0, 0x4, 35},
124 {0x11, 0x1, 0x2, 35},
125 {0x11, 0x0, 0x5, 45},
126 {0x11, 0x1, 0x7, 76},
127 {0x11, 0x1, 0x6, 65},
128 {0x11, 0x1, 0x8, 89},
130 {0x11, 0x1, 0x1, 22},
131 {0x12, 0x0, 0x6, 25},
134 {0x12, 0x0, 0x4, 15},
135 {0x12, 0x0, 0xc, 35},
136 {0x12, 0x1, 0xc, 35},
140 /* Get the Processor Brand String using cpuid(0x8000000x) command x=2,3,4 */
141 cpuid1 = cpuid(0x80000002);
142 v = (u32 *) processor_brand;
147 cpuid1 = cpuid(0x80000003);
152 cpuid1 = cpuid(0x80000004);
157 processor_brand[48] = 0;
158 printk_info("processor_brand=%s\n", processor_brand);
161 * Based on the CPU socket type,cmp_cap and pwr_lmt , get the power limit.
162 * socket_type : 0x10 SocketF; 0x11 AM2/ASB1 ; 0x12 S1G1
163 * cmp_cap : 0x0 SingleCore ; 0x1 DualCore
165 printk_info("Pstates Algorithm ...\n");
167 (pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x18, 3)), 0xE8) &
169 cpuid1 = cpuid(0x80000001);
170 pwr_lmt = ((cpuid1.ebx & 0x1C0) >> 5) | ((cpuid1.ebx & 0x4000) >> 14);
171 for (index = 0; index <= sizeof(TDP) / sizeof(TDP[0]); index++)
172 if (TDP[index].socket_type == CPU_SOCKET_TYPE &&
173 TDP[index].cmp_cap == cmp_cap &&
174 TDP[index].pwr_lmt == pwr_lmt) {
175 power_limit = TDP[index].power_limit;
180 /* See if the CPUID(0x80000007) returned EDX[2:1]==11b */
181 cpuid1 = cpuid(0x80000007);
182 if ((cpuid1.edx & 0x6) != 0x6) {
183 printk_info("No valid set of P-states\n");
187 msr = rdmsr(0xc0010042);
188 Max_fid = (msr.lo & 0x3F0000) >> 16;
189 Start_fid = (msr.lo & 0x3F00) >> 8;
190 Max_vid = (msr.hi & 0x3F0000) >> 16;
191 Start_vid = (msr.hi & 0x3F00) >> 8;
192 PstateStep = (msr.hi & 0x1000000) >> 24;
193 IntPstateSup = (msr.hi & 0x20000000) >> 29;
196 * The P1...P[Min+1] VID need PstateStep to calculate
197 * P[N] = P[N-1]VID + 2^PstateStep
198 * PstateStep_coef = 2^PstateStep
205 if (IntPstateSup == 0) {
206 printk_info("No intermediate P-states are supported\n");
210 /* Get the multipier of the fid frequency */
212 * Fid multiplier is always 100 revF and revG.
214 fid_multiplier = 100;
217 * Formula1: CPUFreq = FID * fid_multiplier + 800
218 * Formula2: CPUVolt = 1550 - VID * 25 (mv)
219 * Formula3: Power = (PwrLmt * P[N]Frequency*(P[N]Voltage^2))/(P[0]Frequency * P[0]Voltage^2))
222 /* Construct P0(P[Max]) state */
223 Max_feq = Max_fid * fid_multiplier + 800;
224 if (Max_fid == 0x2A && Max_vid != 0x0) {
226 Pstate_fid[0] = Start_fid + 0xA; /* Start Frequency + 1GHz */
227 Pstate_feq[0] = Pstate_fid[0] * fid_multiplier + 800;
229 Pstate_vid[0] = Max_vid + 0x2; /* Maximum Voltage - 50mV */
230 Pstate_volt[0] = 1550 - Pstate_vid[0] * 25;
231 Pstate_power[0] = power_limit * 1000; /* mw */
235 Pstate_fid[0] = Max_fid;
236 Pstate_feq[0] = Max_feq;
238 Pstate_vid[0] = Max_vid + 0x2;
239 Pstate_volt[0] = 1550 - Pstate_vid[0] * 25;
240 Pstate_power[0] = power_limit * 1000; /* mw */
246 /* Construct P1 state */
247 if (((Max_fid & 0x1) != 0) && ((Max_fid - 0x1) >= (Min_fid + 0x8))) { /* odd value */
248 Pstate_fid[1] = Max_fid - 0x1;
249 Pstate_feq[1] = Pstate_fid[1] * fid_multiplier + 800;
250 Cur_fid = Pstate_fid[1];
251 Cur_feq = Pstate_feq[1];
252 if (((Pstate_vid[0] & 0x1) != 0) && ((Pstate_vid[0] - 0x1) < Min_vid)) { /* odd value */
253 Pstate_vid[1] = Pstate_vid[0] + 0x1;
254 Pstate_volt[1] = 1550 - Pstate_vid[1] * 25;
256 (unsigned long long)Pstate_power[0] *
257 Pstate_feq[1] * Pstate_volt[1] * Pstate_volt[1] /
258 (Pstate_feq[0] * Pstate_volt[0] * Pstate_volt[0]);
260 if (((Pstate_vid[0] & 0x1) == 0) && ((Pstate_vid[0] - 0x1) < Min_vid)) { /* even value */
261 Pstate_vid[1] = Pstate_vid[0] + PstateStep_coef;
262 Pstate_volt[1] = 1550 - Pstate_vid[1] * 25;
264 (unsigned long long)Pstate_power[0] *
265 Pstate_feq[1] * Pstate_volt[1] * Pstate_volt[1] /
266 (Pstate_feq[0] * Pstate_volt[0] * Pstate_volt[0]);
271 if (((Max_fid & 0x1) == 0) && ((Max_fid - 0x2) >= (Min_fid + 0x8))) { /* even value */
272 Pstate_fid[1] = Max_fid - 0x2;
273 Pstate_feq[1] = Pstate_fid[1] * fid_multiplier + 800;
274 Cur_fid = Pstate_fid[1];
275 Cur_feq = Pstate_feq[1];
276 if (((Pstate_vid[0] & 0x1) != 0) && ((Pstate_vid[0] - 0x1) < Min_vid)) { /* odd value */
277 Pstate_vid[1] = Pstate_vid[0] + 0x1;
278 Pstate_volt[1] = 1550 - Pstate_vid[1] * 25;
280 (unsigned long long)Pstate_power[0] *
281 Pstate_feq[1] * Pstate_volt[1] * Pstate_volt[1] /
282 (Pstate_feq[0] * Pstate_volt[0] * Pstate_volt[0]);
284 if (((Pstate_vid[0] & 0x1) == 0) && ((Pstate_vid[0] - 0x1) < Min_vid)) { /* even value */
285 Pstate_vid[1] = Pstate_vid[0] + PstateStep_coef;
286 Pstate_volt[1] = 1550 - Pstate_vid[1] * 25;
288 (unsigned long long)Pstate_power[0] *
289 Pstate_feq[1] * Pstate_volt[1] * Pstate_volt[1] /
290 (Pstate_feq[0] * Pstate_volt[0] * Pstate_volt[0]);
296 /* Construct P2...P[Min-1] state */
297 Cur_fid = Cur_fid - 0x2;
298 Cur_feq = Cur_fid * fid_multiplier + 800;
299 while (Cur_feq >= ((Min_fid * fid_multiplier) + 800) * 2) {
300 Pstate_fid[Pstate_num] = Cur_fid;
301 Pstate_feq[Pstate_num] =
302 Pstate_fid[Pstate_num] * fid_multiplier + 800;
303 Cur_fid = Cur_fid - 0x2;
304 Cur_feq = Cur_fid * fid_multiplier + 800;
305 if (Pstate_vid[Pstate_num - 1] >= Min_vid) {
306 Pstate_vid[Pstate_num] = Pstate_vid[Pstate_num - 1];
307 Pstate_volt[Pstate_num] = Pstate_volt[Pstate_num - 1];
308 Pstate_power[Pstate_num] = Pstate_power[Pstate_num - 1];
310 Pstate_vid[Pstate_num] =
311 Pstate_vid[Pstate_num - 1] + PstateStep_coef;
312 Pstate_volt[Pstate_num] =
313 1550 - Pstate_vid[Pstate_num] * 25;
314 Pstate_power[Pstate_num] =
315 (unsigned long long)Pstate_power[0] *
316 Pstate_feq[Pstate_num] * Pstate_volt[Pstate_num] *
317 Pstate_volt[Pstate_num] / (Pstate_feq[0] *
324 /* Constuct P[Min] State */
325 if (Max_fid == 0x2A && Max_vid != 0x0) {
326 Pstate_fid[Pstate_num] = 0x2;
327 Pstate_feq[Pstate_num] =
328 Pstate_fid[Pstate_num] * fid_multiplier + 800;
329 Pstate_vid[Pstate_num] = Min_vid;
330 Pstate_volt[Pstate_num] = 1550 - Pstate_vid[Pstate_num] * 25;
331 Pstate_power[Pstate_num] =
332 (unsigned long long)Pstate_power[0] *
333 Pstate_feq[Pstate_num] * Pstate_volt[Pstate_num] *
334 Pstate_volt[Pstate_num] / (Pstate_feq[0] * Pstate_volt[0] *
338 Pstate_fid[Pstate_num] = Start_fid;
339 Pstate_feq[Pstate_num] =
340 Pstate_fid[Pstate_num] * fid_multiplier + 800;
341 Pstate_vid[Pstate_num] = Min_vid;
342 Pstate_volt[Pstate_num] = 1550 - Pstate_vid[Pstate_num] * 25;
343 Pstate_power[Pstate_num] =
344 (unsigned long long)Pstate_power[0] *
345 Pstate_feq[Pstate_num] * Pstate_volt[Pstate_num] *
346 Pstate_volt[Pstate_num] / (Pstate_feq[0] * Pstate_volt[0] *
351 /* Print Pstate freq,vid,volt,power */
353 for (index = 0; index < Pstate_num; index++) {
354 printk_info("Pstate_freq[%d] = %dMHz\t", index,
356 printk_info("Pstate_vid[%d] = %d\t", index, Pstate_vid[index]);
357 printk_info("Pstate_volt[%d] = %dmv\t", index,
359 printk_info("Pstate_power[%d] = %dmw\n", index,
360 Pstate_power[index]);
368 for (index = 0; index < (cmp_cap + 1); index++) {
369 len += write_pstates_for_core(Pstate_num, Pstate_feq, Pstate_vid,
370 Pstate_fid, Pstate_power, index,
371 pcontrol_blk, plen, onlyBSP);
377 int amd_model_fxx_generate_powernow(u32 pcontrol_blk, u8 plen, u8 onlyBSP) {
379 char pscope[] = "\\_PR_";
381 lens = acpigen_write_scope(pscope);
382 lens += pstates_algorithm(pcontrol_blk, plen, onlyBSP);
384 acpigen_patch_len(lens - 1);