2 * (c) 2003-2006 Advanced Micro Devices, Inc.
3 * Your use of this code is subject to the terms and conditions of the
4 * GNU general public license version 2. See "COPYING" or
5 * http://www.gnu.org/licenses/gpl.html
7 * Support : mark.langsdorf@amd.com
9 * Based on the powernow-k7.c module written by Dave Jones.
10 * (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs
11 * (C) 2004 Dominik Brodowski <linux@brodo.de>
12 * (C) 2004 Pavel Machek <pavel@suse.cz>
13 * Licensed under the terms of the GNU GPL License version 2.
14 * Based upon datasheets & sample CPUs kindly provided by AMD.
16 * Valuable input gratefully received from Dave Jones, Pavel Machek,
17 * Dominik Brodowski, Jacob Shin, and others.
18 * Originally developed by Paul Devriendt.
19 * Processor information obtained from Chapter 9 (Power and Thermal Management)
20 * of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
21 * Opteron Processors" available for download from www.amd.com
23 * Tables for specific CPUs can be inferred from
24 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/30430.pdf
27 #include <linux/kernel.h>
28 #include <linux/smp.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/cpufreq.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/cpumask.h>
35 #include <linux/sched.h> /* for current / set_cpus_allowed() */
39 #include <asm/delay.h>
41 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
42 #include <linux/acpi.h>
43 #include <linux/mutex.h>
44 #include <acpi/processor.h>
47 #define PFX "powernow-k8: "
48 #define BFX PFX "BIOS error: "
49 #define VERSION "version 2.00.00"
50 #include "powernow-k8.h"
52 /* serialize freq changes */
53 static DEFINE_MUTEX(fidvid_mutex
);
55 static struct powernow_k8_data
*powernow_data
[NR_CPUS
];
57 static int cpu_family
= CPU_OPTERON
;
60 static cpumask_t cpu_core_map
[1];
63 /* Return a frequency in MHz, given an input fid */
64 static u32
find_freq_from_fid(u32 fid
)
66 return 800 + (fid
* 100);
70 /* Return a frequency in KHz, given an input fid */
71 static u32
find_khz_freq_from_fid(u32 fid
)
73 return 1000 * find_freq_from_fid(fid
);
76 /* Return a frequency in MHz, given an input fid and did */
77 static u32
find_freq_from_fiddid(u32 fid
, u32 did
)
79 return 100 * (fid
+ 0x10) >> did
;
82 static u32
find_khz_freq_from_fiddid(u32 fid
, u32 did
)
84 return 1000 * find_freq_from_fiddid(fid
, did
);
87 static u32
find_fid_from_pstate(u32 pstate
)
90 rdmsr(MSR_PSTATE_DEF_BASE
+ pstate
, lo
, hi
);
91 return lo
& HW_PSTATE_FID_MASK
;
94 static u32
find_did_from_pstate(u32 pstate
)
97 rdmsr(MSR_PSTATE_DEF_BASE
+ pstate
, lo
, hi
);
98 return (lo
& HW_PSTATE_DID_MASK
) >> HW_PSTATE_DID_SHIFT
;
101 /* Return the vco fid for an input fid
103 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
104 * only from corresponding high fids. This returns "high" fid corresponding to
107 static u32
convert_fid_to_vco_fid(u32 fid
)
109 if (fid
< HI_FID_TABLE_BOTTOM
)
110 return 8 + (2 * fid
);
116 * Return 1 if the pending bit is set. Unless we just instructed the processor
117 * to transition to a new state, seeing this bit set is really bad news.
119 static int pending_bit_stuck(void)
123 if (cpu_family
== CPU_HW_PSTATE
)
126 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
127 return lo
& MSR_S_LO_CHANGE_PENDING
? 1 : 0;
131 * Update the global current fid / vid values from the status msr.
132 * Returns 1 on error.
134 static int query_current_values_with_pending_wait(struct powernow_k8_data
*data
)
139 if (cpu_family
== CPU_HW_PSTATE
) {
140 rdmsr(MSR_PSTATE_STATUS
, lo
, hi
);
141 i
= lo
& HW_PSTATE_MASK
;
142 rdmsr(MSR_PSTATE_DEF_BASE
+ i
, lo
, hi
);
143 data
->currfid
= lo
& HW_PSTATE_FID_MASK
;
144 data
->currdid
= (lo
& HW_PSTATE_DID_MASK
) >> HW_PSTATE_DID_SHIFT
;
149 dprintk("detected change pending stuck\n");
152 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
153 } while (lo
& MSR_S_LO_CHANGE_PENDING
);
155 data
->currvid
= hi
& MSR_S_HI_CURRENT_VID
;
156 data
->currfid
= lo
& MSR_S_LO_CURRENT_FID
;
161 /* the isochronous relief time */
162 static void count_off_irt(struct powernow_k8_data
*data
)
164 udelay((1 << data
->irt
) * 10);
168 /* the voltage stabalization time */
169 static void count_off_vst(struct powernow_k8_data
*data
)
171 udelay(data
->vstable
* VST_UNITS_20US
);
175 /* need to init the control msr to a safe value (for each cpu) */
176 static void fidvid_msr_init(void)
181 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
182 vid
= hi
& MSR_S_HI_CURRENT_VID
;
183 fid
= lo
& MSR_S_LO_CURRENT_FID
;
184 lo
= fid
| (vid
<< MSR_C_LO_VID_SHIFT
);
185 hi
= MSR_C_HI_STP_GNT_BENIGN
;
186 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo
, hi
);
187 wrmsr(MSR_FIDVID_CTL
, lo
, hi
);
191 /* write the new fid value along with the other control fields to the msr */
192 static int write_new_fid(struct powernow_k8_data
*data
, u32 fid
)
195 u32 savevid
= data
->currvid
;
198 if ((fid
& INVALID_FID_MASK
) || (data
->currvid
& INVALID_VID_MASK
)) {
199 printk(KERN_ERR PFX
"internal error - overflow on fid write\n");
203 lo
= fid
| (data
->currvid
<< MSR_C_LO_VID_SHIFT
) | MSR_C_LO_INIT_FID_VID
;
205 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
206 fid
, lo
, data
->plllock
* PLL_LOCK_CONVERSION
);
209 wrmsr(MSR_FIDVID_CTL
, lo
, data
->plllock
* PLL_LOCK_CONVERSION
);
211 printk(KERN_ERR PFX
"Hardware error - pending bit very stuck - no further pstate changes possible\n");
214 } while (query_current_values_with_pending_wait(data
));
218 if (savevid
!= data
->currvid
) {
219 printk(KERN_ERR PFX
"vid change on fid trans, old 0x%x, new 0x%x\n",
220 savevid
, data
->currvid
);
224 if (fid
!= data
->currfid
) {
225 printk(KERN_ERR PFX
"fid trans failed, fid 0x%x, curr 0x%x\n", fid
,
233 /* Write a new vid to the hardware */
234 static int write_new_vid(struct powernow_k8_data
*data
, u32 vid
)
237 u32 savefid
= data
->currfid
;
240 if ((data
->currfid
& INVALID_FID_MASK
) || (vid
& INVALID_VID_MASK
)) {
241 printk(KERN_ERR PFX
"internal error - overflow on vid write\n");
245 lo
= data
->currfid
| (vid
<< MSR_C_LO_VID_SHIFT
) | MSR_C_LO_INIT_FID_VID
;
247 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
248 vid
, lo
, STOP_GRANT_5NS
);
251 wrmsr(MSR_FIDVID_CTL
, lo
, STOP_GRANT_5NS
);
253 printk(KERN_ERR PFX
"internal error - pending bit very stuck - no further pstate changes possible\n");
256 } while (query_current_values_with_pending_wait(data
));
258 if (savefid
!= data
->currfid
) {
259 printk(KERN_ERR PFX
"fid changed on vid trans, old 0x%x new 0x%x\n",
260 savefid
, data
->currfid
);
264 if (vid
!= data
->currvid
) {
265 printk(KERN_ERR PFX
"vid trans failed, vid 0x%x, curr 0x%x\n", vid
,
274 * Reduce the vid by the max of step or reqvid.
275 * Decreasing vid codes represent increasing voltages:
276 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
278 static int decrease_vid_code_by_step(struct powernow_k8_data
*data
, u32 reqvid
, u32 step
)
280 if ((data
->currvid
- reqvid
) > step
)
281 reqvid
= data
->currvid
- step
;
283 if (write_new_vid(data
, reqvid
))
291 /* Change hardware pstate by single MSR write */
292 static int transition_pstate(struct powernow_k8_data
*data
, u32 pstate
)
294 wrmsr(MSR_PSTATE_CTRL
, pstate
, 0);
295 data
->currfid
= find_fid_from_pstate(pstate
);
299 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
300 static int transition_fid_vid(struct powernow_k8_data
*data
, u32 reqfid
, u32 reqvid
)
302 if (core_voltage_pre_transition(data
, reqvid
))
305 if (core_frequency_transition(data
, reqfid
))
308 if (core_voltage_post_transition(data
, reqvid
))
311 if (query_current_values_with_pending_wait(data
))
314 if ((reqfid
!= data
->currfid
) || (reqvid
!= data
->currvid
)) {
315 printk(KERN_ERR PFX
"failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
317 reqfid
, reqvid
, data
->currfid
, data
->currvid
);
321 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
322 smp_processor_id(), data
->currfid
, data
->currvid
);
327 /* Phase 1 - core voltage transition ... setup voltage */
328 static int core_voltage_pre_transition(struct powernow_k8_data
*data
, u32 reqvid
)
330 u32 rvosteps
= data
->rvo
;
331 u32 savefid
= data
->currfid
;
334 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
336 data
->currfid
, data
->currvid
, reqvid
, data
->rvo
);
338 rdmsr(MSR_FIDVID_STATUS
, lo
, maxvid
);
339 maxvid
= 0x1f & (maxvid
>> 16);
340 dprintk("ph1 maxvid=0x%x\n", maxvid
);
341 if (reqvid
< maxvid
) /* lower numbers are higher voltages */
344 while (data
->currvid
> reqvid
) {
345 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
346 data
->currvid
, reqvid
);
347 if (decrease_vid_code_by_step(data
, reqvid
, data
->vidmvs
))
351 while ((rvosteps
> 0) && ((data
->rvo
+ data
->currvid
) > reqvid
)) {
352 if (data
->currvid
== maxvid
) {
355 dprintk("ph1: changing vid for rvo, req 0x%x\n",
357 if (decrease_vid_code_by_step(data
, data
->currvid
- 1, 1))
363 if (query_current_values_with_pending_wait(data
))
366 if (savefid
!= data
->currfid
) {
367 printk(KERN_ERR PFX
"ph1 err, currfid changed 0x%x\n", data
->currfid
);
371 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
372 data
->currfid
, data
->currvid
);
377 /* Phase 2 - core frequency transition */
378 static int core_frequency_transition(struct powernow_k8_data
*data
, u32 reqfid
)
380 u32 vcoreqfid
, vcocurrfid
, vcofiddiff
, fid_interval
, savevid
= data
->currvid
;
382 if ((reqfid
< HI_FID_TABLE_BOTTOM
) && (data
->currfid
< HI_FID_TABLE_BOTTOM
)) {
383 printk(KERN_ERR PFX
"ph2: illegal lo-lo transition 0x%x 0x%x\n",
384 reqfid
, data
->currfid
);
388 if (data
->currfid
== reqfid
) {
389 printk(KERN_ERR PFX
"ph2 null fid transition 0x%x\n", data
->currfid
);
393 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
395 data
->currfid
, data
->currvid
, reqfid
);
397 vcoreqfid
= convert_fid_to_vco_fid(reqfid
);
398 vcocurrfid
= convert_fid_to_vco_fid(data
->currfid
);
399 vcofiddiff
= vcocurrfid
> vcoreqfid
? vcocurrfid
- vcoreqfid
400 : vcoreqfid
- vcocurrfid
;
402 while (vcofiddiff
> 2) {
403 (data
->currfid
& 1) ? (fid_interval
= 1) : (fid_interval
= 2);
405 if (reqfid
> data
->currfid
) {
406 if (data
->currfid
> LO_FID_TABLE_TOP
) {
407 if (write_new_fid(data
, data
->currfid
+ fid_interval
)) {
412 (data
, 2 + convert_fid_to_vco_fid(data
->currfid
))) {
417 if (write_new_fid(data
, data
->currfid
- fid_interval
))
421 vcocurrfid
= convert_fid_to_vco_fid(data
->currfid
);
422 vcofiddiff
= vcocurrfid
> vcoreqfid
? vcocurrfid
- vcoreqfid
423 : vcoreqfid
- vcocurrfid
;
426 if (write_new_fid(data
, reqfid
))
429 if (query_current_values_with_pending_wait(data
))
432 if (data
->currfid
!= reqfid
) {
434 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
435 data
->currfid
, reqfid
);
439 if (savevid
!= data
->currvid
) {
440 printk(KERN_ERR PFX
"ph2: vid changed, save 0x%x, curr 0x%x\n",
441 savevid
, data
->currvid
);
445 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
446 data
->currfid
, data
->currvid
);
451 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
452 static int core_voltage_post_transition(struct powernow_k8_data
*data
, u32 reqvid
)
454 u32 savefid
= data
->currfid
;
455 u32 savereqvid
= reqvid
;
457 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
459 data
->currfid
, data
->currvid
);
461 if (reqvid
!= data
->currvid
) {
462 if (write_new_vid(data
, reqvid
))
465 if (savefid
!= data
->currfid
) {
467 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
468 savefid
, data
->currfid
);
472 if (data
->currvid
!= reqvid
) {
474 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
475 reqvid
, data
->currvid
);
480 if (query_current_values_with_pending_wait(data
))
483 if (savereqvid
!= data
->currvid
) {
484 dprintk("ph3 failed, currvid 0x%x\n", data
->currvid
);
488 if (savefid
!= data
->currfid
) {
489 dprintk("ph3 failed, currfid changed 0x%x\n",
494 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
495 data
->currfid
, data
->currvid
);
500 static int check_supported_cpu(unsigned int cpu
)
502 cpumask_t oldmask
= CPU_MASK_ALL
;
503 u32 eax
, ebx
, ecx
, edx
;
506 oldmask
= current
->cpus_allowed
;
507 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
509 if (smp_processor_id() != cpu
) {
510 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", cpu
);
514 if (current_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
517 eax
= cpuid_eax(CPUID_PROCESSOR_SIGNATURE
);
518 if (((eax
& CPUID_XFAM
) != CPUID_XFAM_K8
) &&
519 ((eax
& CPUID_XFAM
) < CPUID_XFAM_10H
))
522 if ((eax
& CPUID_XFAM
) == CPUID_XFAM_K8
) {
523 if (((eax
& CPUID_USE_XFAM_XMOD
) != CPUID_USE_XFAM_XMOD
) ||
524 ((eax
& CPUID_XMOD
) > CPUID_XMOD_REV_G
)) {
525 printk(KERN_INFO PFX
"Processor cpuid %x not supported\n", eax
);
529 eax
= cpuid_eax(CPUID_GET_MAX_CAPABILITIES
);
530 if (eax
< CPUID_FREQ_VOLT_CAPABILITIES
) {
532 "No frequency change capabilities detected\n");
536 cpuid(CPUID_FREQ_VOLT_CAPABILITIES
, &eax
, &ebx
, &ecx
, &edx
);
537 if ((edx
& P_STATE_TRANSITION_CAPABLE
) != P_STATE_TRANSITION_CAPABLE
) {
538 printk(KERN_INFO PFX
"Power state transitions not supported\n");
541 } else { /* must be a HW Pstate capable processor */
542 cpuid(CPUID_FREQ_VOLT_CAPABILITIES
, &eax
, &ebx
, &ecx
, &edx
);
543 if ((edx
& USE_HW_PSTATE
) == USE_HW_PSTATE
)
544 cpu_family
= CPU_HW_PSTATE
;
552 set_cpus_allowed(current
, oldmask
);
556 static int check_pst_table(struct powernow_k8_data
*data
, struct pst_s
*pst
, u8 maxvid
)
561 for (j
= 0; j
< data
->numps
; j
++) {
562 if (pst
[j
].vid
> LEAST_VID
) {
563 printk(KERN_ERR PFX
"vid %d invalid : 0x%x\n", j
, pst
[j
].vid
);
566 if (pst
[j
].vid
< data
->rvo
) { /* vid + rvo >= 0 */
567 printk(KERN_ERR BFX
"0 vid exceeded with pstate %d\n", j
);
570 if (pst
[j
].vid
< maxvid
+ data
->rvo
) { /* vid + rvo >= maxvid */
571 printk(KERN_ERR BFX
"maxvid exceeded with pstate %d\n", j
);
574 if (pst
[j
].fid
> MAX_FID
) {
575 printk(KERN_ERR BFX
"maxfid exceeded with pstate %d\n", j
);
578 if (j
&& (pst
[j
].fid
< HI_FID_TABLE_BOTTOM
)) {
579 /* Only first fid is allowed to be in "low" range */
580 printk(KERN_ERR BFX
"two low fids - %d : 0x%x\n", j
, pst
[j
].fid
);
583 if (pst
[j
].fid
< lastfid
)
584 lastfid
= pst
[j
].fid
;
587 printk(KERN_ERR BFX
"lastfid invalid\n");
590 if (lastfid
> LO_FID_TABLE_TOP
)
591 printk(KERN_INFO BFX
"first fid not from lo freq table\n");
596 static void print_basics(struct powernow_k8_data
*data
)
599 for (j
= 0; j
< data
->numps
; j
++) {
600 if (data
->powernow_table
[j
].frequency
!= CPUFREQ_ENTRY_INVALID
) {
601 if (cpu_family
== CPU_HW_PSTATE
) {
602 printk(KERN_INFO PFX
" %d : fid 0x%x gid 0x%x (%d MHz)\n", j
, (data
->powernow_table
[j
].index
& 0xff00) >> 8,
603 (data
->powernow_table
[j
].index
& 0xff0000) >> 16,
604 data
->powernow_table
[j
].frequency
/1000);
606 printk(KERN_INFO PFX
" %d : fid 0x%x (%d MHz), vid 0x%x\n", j
,
607 data
->powernow_table
[j
].index
& 0xff,
608 data
->powernow_table
[j
].frequency
/1000,
609 data
->powernow_table
[j
].index
>> 8);
614 printk(KERN_INFO PFX
"Only %d pstates on battery\n", data
->batps
);
617 static int fill_powernow_table(struct powernow_k8_data
*data
, struct pst_s
*pst
, u8 maxvid
)
619 struct cpufreq_frequency_table
*powernow_table
;
622 if (data
->batps
) { /* use ACPI support to get full speed on mains power */
623 printk(KERN_WARNING PFX
"Only %d pstates usable (use ACPI driver for full range\n", data
->batps
);
624 data
->numps
= data
->batps
;
627 for ( j
=1; j
<data
->numps
; j
++ ) {
628 if (pst
[j
-1].fid
>= pst
[j
].fid
) {
629 printk(KERN_ERR PFX
"PST out of sequence\n");
634 if (data
->numps
< 2) {
635 printk(KERN_ERR PFX
"no p states to transition\n");
639 if (check_pst_table(data
, pst
, maxvid
))
642 powernow_table
= kmalloc((sizeof(struct cpufreq_frequency_table
)
643 * (data
->numps
+ 1)), GFP_KERNEL
);
644 if (!powernow_table
) {
645 printk(KERN_ERR PFX
"powernow_table memory alloc failure\n");
649 for (j
= 0; j
< data
->numps
; j
++) {
650 powernow_table
[j
].index
= pst
[j
].fid
; /* lower 8 bits */
651 powernow_table
[j
].index
|= (pst
[j
].vid
<< 8); /* upper 8 bits */
652 powernow_table
[j
].frequency
= find_khz_freq_from_fid(pst
[j
].fid
);
654 powernow_table
[data
->numps
].frequency
= CPUFREQ_TABLE_END
;
655 powernow_table
[data
->numps
].index
= 0;
657 if (query_current_values_with_pending_wait(data
)) {
658 kfree(powernow_table
);
662 dprintk("cfid 0x%x, cvid 0x%x\n", data
->currfid
, data
->currvid
);
663 data
->powernow_table
= powernow_table
;
666 for (j
= 0; j
< data
->numps
; j
++)
667 if ((pst
[j
].fid
==data
->currfid
) && (pst
[j
].vid
==data
->currvid
))
670 dprintk("currfid/vid do not match PST, ignoring\n");
674 /* Find and validate the PSB/PST table in BIOS. */
675 static int find_psb_table(struct powernow_k8_data
*data
)
684 for (i
= 0xc0000; i
< 0xffff0; i
+= 0x10) {
685 /* Scan BIOS looking for the signature. */
686 /* It can not be at ffff0 - it is too big. */
688 psb
= phys_to_virt(i
);
689 if (memcmp(psb
, PSB_ID_STRING
, PSB_ID_STRING_LEN
) != 0)
692 dprintk("found PSB header at 0x%p\n", psb
);
694 dprintk("table vers: 0x%x\n", psb
->tableversion
);
695 if (psb
->tableversion
!= PSB_VERSION_1_4
) {
696 printk(KERN_ERR BFX
"PSB table is not v1.4\n");
700 dprintk("flags: 0x%x\n", psb
->flags1
);
702 printk(KERN_ERR BFX
"unknown flags\n");
706 data
->vstable
= psb
->vstable
;
707 dprintk("voltage stabilization time: %d(*20us)\n", data
->vstable
);
709 dprintk("flags2: 0x%x\n", psb
->flags2
);
710 data
->rvo
= psb
->flags2
& 3;
711 data
->irt
= ((psb
->flags2
) >> 2) & 3;
712 mvs
= ((psb
->flags2
) >> 4) & 3;
713 data
->vidmvs
= 1 << mvs
;
714 data
->batps
= ((psb
->flags2
) >> 6) & 3;
716 dprintk("ramp voltage offset: %d\n", data
->rvo
);
717 dprintk("isochronous relief time: %d\n", data
->irt
);
718 dprintk("maximum voltage step: %d - 0x%x\n", mvs
, data
->vidmvs
);
720 dprintk("numpst: 0x%x\n", psb
->num_tables
);
721 cpst
= psb
->num_tables
;
722 if ((psb
->cpuid
== 0x00000fc0) || (psb
->cpuid
== 0x00000fe0) ){
723 thiscpuid
= cpuid_eax(CPUID_PROCESSOR_SIGNATURE
);
724 if ((thiscpuid
== 0x00000fc0) || (thiscpuid
== 0x00000fe0) ) {
729 printk(KERN_ERR BFX
"numpst must be 1\n");
733 data
->plllock
= psb
->plllocktime
;
734 dprintk("plllocktime: 0x%x (units 1us)\n", psb
->plllocktime
);
735 dprintk("maxfid: 0x%x\n", psb
->maxfid
);
736 dprintk("maxvid: 0x%x\n", psb
->maxvid
);
737 maxvid
= psb
->maxvid
;
739 data
->numps
= psb
->numps
;
740 dprintk("numpstates: 0x%x\n", data
->numps
);
741 return fill_powernow_table(data
, (struct pst_s
*)(psb
+1), maxvid
);
744 * If you see this message, complain to BIOS manufacturer. If
745 * he tells you "we do not support Linux" or some similar
746 * nonsense, remember that Windows 2000 uses the same legacy
747 * mechanism that the old Linux PSB driver uses. Tell them it
748 * is broken with Windows 2000.
750 * The reference to the AMD documentation is chapter 9 in the
751 * BIOS and Kernel Developer's Guide, which is available on
754 printk(KERN_ERR PFX
"BIOS error - no PSB or ACPI _PSS objects\n");
758 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
759 static void powernow_k8_acpi_pst_values(struct powernow_k8_data
*data
, unsigned int index
)
761 if (!data
->acpi_data
.state_count
|| (cpu_family
== CPU_HW_PSTATE
))
764 data
->irt
= (data
->acpi_data
.states
[index
].control
>> IRT_SHIFT
) & IRT_MASK
;
765 data
->rvo
= (data
->acpi_data
.states
[index
].control
>> RVO_SHIFT
) & RVO_MASK
;
766 data
->exttype
= (data
->acpi_data
.states
[index
].control
>> EXT_TYPE_SHIFT
) & EXT_TYPE_MASK
;
767 data
->plllock
= (data
->acpi_data
.states
[index
].control
>> PLL_L_SHIFT
) & PLL_L_MASK
;
768 data
->vidmvs
= 1 << ((data
->acpi_data
.states
[index
].control
>> MVS_SHIFT
) & MVS_MASK
);
769 data
->vstable
= (data
->acpi_data
.states
[index
].control
>> VST_SHIFT
) & VST_MASK
;
772 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data
*data
)
774 struct cpufreq_frequency_table
*powernow_table
;
777 if (acpi_processor_register_performance(&data
->acpi_data
, data
->cpu
)) {
778 dprintk("register performance failed: bad ACPI data\n");
782 /* verify the data contained in the ACPI structures */
783 if (data
->acpi_data
.state_count
<= 1) {
784 dprintk("No ACPI P-States\n");
788 if ((data
->acpi_data
.control_register
.space_id
!= ACPI_ADR_SPACE_FIXED_HARDWARE
) ||
789 (data
->acpi_data
.status_register
.space_id
!= ACPI_ADR_SPACE_FIXED_HARDWARE
)) {
790 dprintk("Invalid control/status registers (%x - %x)\n",
791 data
->acpi_data
.control_register
.space_id
,
792 data
->acpi_data
.status_register
.space_id
);
796 /* fill in data->powernow_table */
797 powernow_table
= kmalloc((sizeof(struct cpufreq_frequency_table
)
798 * (data
->acpi_data
.state_count
+ 1)), GFP_KERNEL
);
799 if (!powernow_table
) {
800 dprintk("powernow_table memory alloc failure\n");
804 if (cpu_family
== CPU_HW_PSTATE
)
805 ret_val
= fill_powernow_table_pstate(data
, powernow_table
);
807 ret_val
= fill_powernow_table_fidvid(data
, powernow_table
);
811 powernow_table
[data
->acpi_data
.state_count
].frequency
= CPUFREQ_TABLE_END
;
812 powernow_table
[data
->acpi_data
.state_count
].index
= 0;
813 data
->powernow_table
= powernow_table
;
816 data
->numps
= data
->acpi_data
.state_count
;
818 powernow_k8_acpi_pst_values(data
, 0);
820 /* notify BIOS that we exist */
821 acpi_processor_notify_smm(THIS_MODULE
);
826 kfree(powernow_table
);
829 acpi_processor_unregister_performance(&data
->acpi_data
, data
->cpu
);
831 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
832 data
->acpi_data
.state_count
= 0;
837 static int fill_powernow_table_pstate(struct powernow_k8_data
*data
, struct cpufreq_frequency_table
*powernow_table
)
841 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
847 index
= data
->acpi_data
.states
[i
].control
& HW_PSTATE_MASK
;
848 if (index
> MAX_HW_PSTATE
) {
849 printk(KERN_ERR PFX
"invalid pstate %d - bad value %d.\n", i
, index
);
850 printk(KERN_ERR PFX
"Please report to BIOS manufacturer\n");
852 rdmsr(MSR_PSTATE_DEF_BASE
+ index
, lo
, hi
);
853 if (!(hi
& HW_PSTATE_VALID_MASK
)) {
854 dprintk("invalid pstate %d, ignoring\n", index
);
855 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
859 fid
= lo
& HW_PSTATE_FID_MASK
;
860 did
= (lo
& HW_PSTATE_DID_MASK
) >> HW_PSTATE_DID_SHIFT
;
862 dprintk(" %d : fid 0x%x, did 0x%x\n", index
, fid
, did
);
864 powernow_table
[i
].index
= index
| (fid
<< HW_FID_INDEX_SHIFT
) | (did
<< HW_DID_INDEX_SHIFT
);
866 powernow_table
[i
].frequency
= find_khz_freq_from_fiddid(fid
, did
);
868 if (powernow_table
[i
].frequency
!= (data
->acpi_data
.states
[i
].core_frequency
* 1000)) {
869 printk(KERN_INFO PFX
"invalid freq entries %u kHz vs. %u kHz\n",
870 powernow_table
[i
].frequency
,
871 (unsigned int) (data
->acpi_data
.states
[i
].core_frequency
* 1000));
872 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
879 static int fill_powernow_table_fidvid(struct powernow_k8_data
*data
, struct cpufreq_frequency_table
*powernow_table
)
883 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
888 fid
= data
->acpi_data
.states
[i
].status
& EXT_FID_MASK
;
889 vid
= (data
->acpi_data
.states
[i
].status
>> VID_SHIFT
) & EXT_VID_MASK
;
891 fid
= data
->acpi_data
.states
[i
].control
& FID_MASK
;
892 vid
= (data
->acpi_data
.states
[i
].control
>> VID_SHIFT
) & VID_MASK
;
895 dprintk(" %d : fid 0x%x, vid 0x%x\n", i
, fid
, vid
);
897 powernow_table
[i
].index
= fid
; /* lower 8 bits */
898 powernow_table
[i
].index
|= (vid
<< 8); /* upper 8 bits */
899 powernow_table
[i
].frequency
= find_khz_freq_from_fid(fid
);
901 /* verify frequency is OK */
902 if ((powernow_table
[i
].frequency
> (MAX_FREQ
* 1000)) ||
903 (powernow_table
[i
].frequency
< (MIN_FREQ
* 1000))) {
904 dprintk("invalid freq %u kHz, ignoring\n", powernow_table
[i
].frequency
);
905 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
909 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
910 if (vid
== VID_OFF
) {
911 dprintk("invalid vid %u, ignoring\n", vid
);
912 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
916 /* verify only 1 entry from the lo frequency table */
917 if (fid
< HI_FID_TABLE_BOTTOM
) {
919 /* if both entries are the same, ignore this one ... */
920 if ((powernow_table
[i
].frequency
!= powernow_table
[cntlofreq
].frequency
) ||
921 (powernow_table
[i
].index
!= powernow_table
[cntlofreq
].index
)) {
922 printk(KERN_ERR PFX
"Too many lo freq table entries\n");
926 dprintk("double low frequency table entry, ignoring it.\n");
927 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
933 if (powernow_table
[i
].frequency
!= (data
->acpi_data
.states
[i
].core_frequency
* 1000)) {
934 printk(KERN_INFO PFX
"invalid freq entries %u kHz vs. %u kHz\n",
935 powernow_table
[i
].frequency
,
936 (unsigned int) (data
->acpi_data
.states
[i
].core_frequency
* 1000));
937 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
944 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data
*data
)
946 if (data
->acpi_data
.state_count
)
947 acpi_processor_unregister_performance(&data
->acpi_data
, data
->cpu
);
951 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data
*data
) { return -ENODEV
; }
952 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data
*data
) { return; }
953 static void powernow_k8_acpi_pst_values(struct powernow_k8_data
*data
, unsigned int index
) { return; }
954 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
956 /* Take a frequency, and issue the fid/vid transition command */
957 static int transition_frequency_fidvid(struct powernow_k8_data
*data
, unsigned int index
)
962 struct cpufreq_freqs freqs
;
964 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index
);
966 /* fid/vid correctness check for k8 */
967 /* fid are the lower 8 bits of the index we stored into
968 * the cpufreq frequency table in find_psb_table, vid
969 * are the upper 8 bits.
971 fid
= data
->powernow_table
[index
].index
& 0xFF;
972 vid
= (data
->powernow_table
[index
].index
& 0xFF00) >> 8;
974 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid
, vid
);
976 if (query_current_values_with_pending_wait(data
))
979 if ((data
->currvid
== vid
) && (data
->currfid
== fid
)) {
980 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
985 if ((fid
< HI_FID_TABLE_BOTTOM
) && (data
->currfid
< HI_FID_TABLE_BOTTOM
)) {
987 "ignoring illegal change in lo freq table-%x to 0x%x\n",
992 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
993 smp_processor_id(), fid
, vid
);
994 freqs
.old
= find_khz_freq_from_fid(data
->currfid
);
995 freqs
.new = find_khz_freq_from_fid(fid
);
997 for_each_cpu_mask(i
, *(data
->available_cores
)) {
999 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
1002 res
= transition_fid_vid(data
, fid
, vid
);
1003 freqs
.new = find_khz_freq_from_fid(data
->currfid
);
1005 for_each_cpu_mask(i
, *(data
->available_cores
)) {
1007 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
1012 /* Take a frequency, and issue the hardware pstate transition command */
1013 static int transition_frequency_pstate(struct powernow_k8_data
*data
, unsigned int index
)
1019 struct cpufreq_freqs freqs
;
1021 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index
);
1023 /* get fid did for hardware pstate transition */
1024 pstate
= index
& HW_PSTATE_MASK
;
1025 if (pstate
> MAX_HW_PSTATE
)
1027 fid
= (index
& HW_FID_INDEX_MASK
) >> HW_FID_INDEX_SHIFT
;
1028 did
= (index
& HW_DID_INDEX_MASK
) >> HW_DID_INDEX_SHIFT
;
1029 freqs
.old
= find_khz_freq_from_fiddid(data
->currfid
, data
->currdid
);
1030 freqs
.new = find_khz_freq_from_fiddid(fid
, did
);
1032 for_each_cpu_mask(i
, *(data
->available_cores
)) {
1034 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
1037 res
= transition_pstate(data
, pstate
);
1038 data
->currfid
= find_fid_from_pstate(pstate
);
1039 data
->currdid
= find_did_from_pstate(pstate
);
1040 freqs
.new = find_khz_freq_from_fiddid(data
->currfid
, data
->currdid
);
1042 for_each_cpu_mask(i
, *(data
->available_cores
)) {
1044 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
1049 /* Driver entry point to switch to the target frequency */
1050 static int powernowk8_target(struct cpufreq_policy
*pol
, unsigned targfreq
, unsigned relation
)
1052 cpumask_t oldmask
= CPU_MASK_ALL
;
1053 struct powernow_k8_data
*data
= powernow_data
[pol
->cpu
];
1056 unsigned int newstate
;
1062 checkfid
= data
->currfid
;
1063 checkvid
= data
->currvid
;
1065 /* only run on specific CPU from here on */
1066 oldmask
= current
->cpus_allowed
;
1067 set_cpus_allowed(current
, cpumask_of_cpu(pol
->cpu
));
1069 if (smp_processor_id() != pol
->cpu
) {
1070 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", pol
->cpu
);
1074 if (pending_bit_stuck()) {
1075 printk(KERN_ERR PFX
"failing targ, change pending bit set\n");
1079 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1080 pol
->cpu
, targfreq
, pol
->min
, pol
->max
, relation
);
1082 if (query_current_values_with_pending_wait(data
))
1085 if (cpu_family
== CPU_HW_PSTATE
)
1086 dprintk("targ: curr fid 0x%x, did 0x%x\n",
1087 data
->currfid
, data
->currvid
);
1089 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1090 data
->currfid
, data
->currvid
);
1092 if ((checkvid
!= data
->currvid
) || (checkfid
!= data
->currfid
)) {
1093 printk(KERN_INFO PFX
1094 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1095 checkfid
, data
->currfid
, checkvid
, data
->currvid
);
1099 if (cpufreq_frequency_table_target(pol
, data
->powernow_table
, targfreq
, relation
, &newstate
))
1102 mutex_lock(&fidvid_mutex
);
1104 powernow_k8_acpi_pst_values(data
, newstate
);
1106 if (cpu_family
== CPU_HW_PSTATE
)
1107 ret
= transition_frequency_pstate(data
, newstate
);
1109 ret
= transition_frequency_fidvid(data
, newstate
);
1111 printk(KERN_ERR PFX
"transition frequency failed\n");
1113 mutex_unlock(&fidvid_mutex
);
1116 mutex_unlock(&fidvid_mutex
);
1118 if (cpu_family
== CPU_HW_PSTATE
)
1119 pol
->cur
= find_khz_freq_from_fiddid(data
->currfid
, data
->currdid
);
1121 pol
->cur
= find_khz_freq_from_fid(data
->currfid
);
1125 set_cpus_allowed(current
, oldmask
);
1129 /* Driver entry point to verify the policy and range of frequencies */
1130 static int powernowk8_verify(struct cpufreq_policy
*pol
)
1132 struct powernow_k8_data
*data
= powernow_data
[pol
->cpu
];
1137 return cpufreq_frequency_table_verify(pol
, data
->powernow_table
);
1140 /* per CPU init entry point to the driver */
1141 static int __cpuinit
powernowk8_cpu_init(struct cpufreq_policy
*pol
)
1143 struct powernow_k8_data
*data
;
1144 cpumask_t oldmask
= CPU_MASK_ALL
;
1147 if (!cpu_online(pol
->cpu
))
1150 if (!check_supported_cpu(pol
->cpu
))
1153 data
= kzalloc(sizeof(struct powernow_k8_data
), GFP_KERNEL
);
1155 printk(KERN_ERR PFX
"unable to alloc powernow_k8_data");
1159 data
->cpu
= pol
->cpu
;
1161 if (powernow_k8_cpu_init_acpi(data
)) {
1163 * Use the PSB BIOS structure. This is only availabe on
1164 * an UP version, and is deprecated by AMD.
1166 if (num_online_cpus() != 1) {
1167 printk(KERN_ERR PFX
"MP systems not supported by PSB BIOS structure\n");
1171 if (pol
->cpu
!= 0) {
1172 printk(KERN_ERR PFX
"No _PSS objects for CPU other than CPU0\n");
1176 rc
= find_psb_table(data
);
1183 /* only run on specific CPU from here on */
1184 oldmask
= current
->cpus_allowed
;
1185 set_cpus_allowed(current
, cpumask_of_cpu(pol
->cpu
));
1187 if (smp_processor_id() != pol
->cpu
) {
1188 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", pol
->cpu
);
1192 if (pending_bit_stuck()) {
1193 printk(KERN_ERR PFX
"failing init, change pending bit set\n");
1197 if (query_current_values_with_pending_wait(data
))
1200 if (cpu_family
== CPU_OPTERON
)
1203 /* run on any CPU again */
1204 set_cpus_allowed(current
, oldmask
);
1206 pol
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
1207 if (cpu_family
== CPU_HW_PSTATE
)
1208 pol
->cpus
= cpumask_of_cpu(pol
->cpu
);
1210 pol
->cpus
= cpu_core_map
[pol
->cpu
];
1211 data
->available_cores
= &(pol
->cpus
);
1213 /* Take a crude guess here.
1214 * That guess was in microseconds, so multiply with 1000 */
1215 pol
->cpuinfo
.transition_latency
= (((data
->rvo
+ 8) * data
->vstable
* VST_UNITS_20US
)
1216 + (3 * (1 << data
->irt
) * 10)) * 1000;
1218 if (cpu_family
== CPU_HW_PSTATE
)
1219 pol
->cur
= find_khz_freq_from_fiddid(data
->currfid
, data
->currdid
);
1221 pol
->cur
= find_khz_freq_from_fid(data
->currfid
);
1222 dprintk("policy current frequency %d kHz\n", pol
->cur
);
1224 /* min/max the cpu is capable of */
1225 if (cpufreq_frequency_table_cpuinfo(pol
, data
->powernow_table
)) {
1226 printk(KERN_ERR PFX
"invalid powernow_table\n");
1227 powernow_k8_cpu_exit_acpi(data
);
1228 kfree(data
->powernow_table
);
1233 cpufreq_frequency_table_get_attr(data
->powernow_table
, pol
->cpu
);
1235 if (cpu_family
== CPU_HW_PSTATE
)
1236 dprintk("cpu_init done, current fid 0x%x, did 0x%x\n",
1237 data
->currfid
, data
->currdid
);
1239 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1240 data
->currfid
, data
->currvid
);
1242 powernow_data
[pol
->cpu
] = data
;
1247 set_cpus_allowed(current
, oldmask
);
1248 powernow_k8_cpu_exit_acpi(data
);
1254 static int __devexit
powernowk8_cpu_exit (struct cpufreq_policy
*pol
)
1256 struct powernow_k8_data
*data
= powernow_data
[pol
->cpu
];
1261 powernow_k8_cpu_exit_acpi(data
);
1263 cpufreq_frequency_table_put_attr(pol
->cpu
);
1265 kfree(data
->powernow_table
);
1271 static unsigned int powernowk8_get (unsigned int cpu
)
1273 struct powernow_k8_data
*data
;
1274 cpumask_t oldmask
= current
->cpus_allowed
;
1275 unsigned int khz
= 0;
1277 data
= powernow_data
[first_cpu(cpu_core_map
[cpu
])];
1282 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
1283 if (smp_processor_id() != cpu
) {
1284 printk(KERN_ERR PFX
"limiting to CPU %d failed in powernowk8_get\n", cpu
);
1285 set_cpus_allowed(current
, oldmask
);
1289 if (query_current_values_with_pending_wait(data
))
1292 if (cpu_family
== CPU_HW_PSTATE
)
1293 khz
= find_khz_freq_from_fiddid(data
->currfid
, data
->currdid
);
1295 khz
= find_khz_freq_from_fid(data
->currfid
);
1299 set_cpus_allowed(current
, oldmask
);
1303 static struct freq_attr
* powernow_k8_attr
[] = {
1304 &cpufreq_freq_attr_scaling_available_freqs
,
1308 static struct cpufreq_driver cpufreq_amd64_driver
= {
1309 .verify
= powernowk8_verify
,
1310 .target
= powernowk8_target
,
1311 .init
= powernowk8_cpu_init
,
1312 .exit
= __devexit_p(powernowk8_cpu_exit
),
1313 .get
= powernowk8_get
,
1314 .name
= "powernow-k8",
1315 .owner
= THIS_MODULE
,
1316 .attr
= powernow_k8_attr
,
1319 /* driver entry point for init */
1320 static int __cpuinit
powernowk8_init(void)
1322 unsigned int i
, supported_cpus
= 0;
1324 for_each_online_cpu(i
) {
1325 if (check_supported_cpu(i
))
1329 if (supported_cpus
== num_online_cpus()) {
1330 printk(KERN_INFO PFX
"Found %d %s "
1331 "processors (" VERSION
")\n", supported_cpus
,
1332 boot_cpu_data
.x86_model_id
);
1333 return cpufreq_register_driver(&cpufreq_amd64_driver
);
1339 /* driver entry point for term */
1340 static void __exit
powernowk8_exit(void)
1344 cpufreq_unregister_driver(&cpufreq_amd64_driver
);
1347 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
1348 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1349 MODULE_LICENSE("GPL");
1351 late_initcall(powernowk8_init
);
1352 module_exit(powernowk8_exit
);