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.20.00"
50 #include "powernow-k8.h"
52 /* serialize freq changes */
53 static DEFINE_MUTEX(fidvid_mutex
);
55 static DEFINE_PER_CPU(struct powernow_k8_data
*, powernow_data
);
57 static int cpu_family
= CPU_OPTERON
;
60 DEFINE_PER_CPU(cpumask_t
, cpu_core_map
);
63 /* Return a frequency in MHz, given an input fid */
64 static u32
find_freq_from_fid(u32 fid
)
66 return 800 + (fid
* 100);
69 /* Return a frequency in KHz, given an input fid */
70 static u32
find_khz_freq_from_fid(u32 fid
)
72 return 1000 * find_freq_from_fid(fid
);
75 static u32
find_khz_freq_from_pstate(struct cpufreq_frequency_table
*data
, u32 pstate
)
77 return data
[pstate
].frequency
;
80 /* Return the vco fid for an input fid
82 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
83 * only from corresponding high fids. This returns "high" fid corresponding to
86 static u32
convert_fid_to_vco_fid(u32 fid
)
88 if (fid
< HI_FID_TABLE_BOTTOM
)
95 * Return 1 if the pending bit is set. Unless we just instructed the processor
96 * to transition to a new state, seeing this bit set is really bad news.
98 static int pending_bit_stuck(void)
102 if (cpu_family
== CPU_HW_PSTATE
)
105 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
106 return lo
& MSR_S_LO_CHANGE_PENDING
? 1 : 0;
110 * Update the global current fid / vid values from the status msr.
111 * Returns 1 on error.
113 static int query_current_values_with_pending_wait(struct powernow_k8_data
*data
)
118 if (cpu_family
== CPU_HW_PSTATE
) {
119 rdmsr(MSR_PSTATE_STATUS
, lo
, hi
);
120 i
= lo
& HW_PSTATE_MASK
;
121 data
->currpstate
= i
;
126 dprintk("detected change pending stuck\n");
129 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
130 } while (lo
& MSR_S_LO_CHANGE_PENDING
);
132 data
->currvid
= hi
& MSR_S_HI_CURRENT_VID
;
133 data
->currfid
= lo
& MSR_S_LO_CURRENT_FID
;
138 /* the isochronous relief time */
139 static void count_off_irt(struct powernow_k8_data
*data
)
141 udelay((1 << data
->irt
) * 10);
145 /* the voltage stabilization time */
146 static void count_off_vst(struct powernow_k8_data
*data
)
148 udelay(data
->vstable
* VST_UNITS_20US
);
152 /* need to init the control msr to a safe value (for each cpu) */
153 static void fidvid_msr_init(void)
158 rdmsr(MSR_FIDVID_STATUS
, lo
, hi
);
159 vid
= hi
& MSR_S_HI_CURRENT_VID
;
160 fid
= lo
& MSR_S_LO_CURRENT_FID
;
161 lo
= fid
| (vid
<< MSR_C_LO_VID_SHIFT
);
162 hi
= MSR_C_HI_STP_GNT_BENIGN
;
163 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo
, hi
);
164 wrmsr(MSR_FIDVID_CTL
, lo
, hi
);
167 /* write the new fid value along with the other control fields to the msr */
168 static int write_new_fid(struct powernow_k8_data
*data
, u32 fid
)
171 u32 savevid
= data
->currvid
;
174 if ((fid
& INVALID_FID_MASK
) || (data
->currvid
& INVALID_VID_MASK
)) {
175 printk(KERN_ERR PFX
"internal error - overflow on fid write\n");
179 lo
= fid
| (data
->currvid
<< MSR_C_LO_VID_SHIFT
) | MSR_C_LO_INIT_FID_VID
;
181 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
182 fid
, lo
, data
->plllock
* PLL_LOCK_CONVERSION
);
185 wrmsr(MSR_FIDVID_CTL
, lo
, data
->plllock
* PLL_LOCK_CONVERSION
);
187 printk(KERN_ERR PFX
"Hardware error - pending bit very stuck - no further pstate changes possible\n");
190 } while (query_current_values_with_pending_wait(data
));
194 if (savevid
!= data
->currvid
) {
195 printk(KERN_ERR PFX
"vid change on fid trans, old 0x%x, new 0x%x\n",
196 savevid
, data
->currvid
);
200 if (fid
!= data
->currfid
) {
201 printk(KERN_ERR PFX
"fid trans failed, fid 0x%x, curr 0x%x\n", fid
,
209 /* Write a new vid to the hardware */
210 static int write_new_vid(struct powernow_k8_data
*data
, u32 vid
)
213 u32 savefid
= data
->currfid
;
216 if ((data
->currfid
& INVALID_FID_MASK
) || (vid
& INVALID_VID_MASK
)) {
217 printk(KERN_ERR PFX
"internal error - overflow on vid write\n");
221 lo
= data
->currfid
| (vid
<< MSR_C_LO_VID_SHIFT
) | MSR_C_LO_INIT_FID_VID
;
223 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
224 vid
, lo
, STOP_GRANT_5NS
);
227 wrmsr(MSR_FIDVID_CTL
, lo
, STOP_GRANT_5NS
);
229 printk(KERN_ERR PFX
"internal error - pending bit very stuck - no further pstate changes possible\n");
232 } while (query_current_values_with_pending_wait(data
));
234 if (savefid
!= data
->currfid
) {
235 printk(KERN_ERR PFX
"fid changed on vid trans, old 0x%x new 0x%x\n",
236 savefid
, data
->currfid
);
240 if (vid
!= data
->currvid
) {
241 printk(KERN_ERR PFX
"vid trans failed, vid 0x%x, curr 0x%x\n", vid
,
250 * Reduce the vid by the max of step or reqvid.
251 * Decreasing vid codes represent increasing voltages:
252 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
254 static int decrease_vid_code_by_step(struct powernow_k8_data
*data
, u32 reqvid
, u32 step
)
256 if ((data
->currvid
- reqvid
) > step
)
257 reqvid
= data
->currvid
- step
;
259 if (write_new_vid(data
, reqvid
))
267 /* Change hardware pstate by single MSR write */
268 static int transition_pstate(struct powernow_k8_data
*data
, u32 pstate
)
270 wrmsr(MSR_PSTATE_CTRL
, pstate
, 0);
271 data
->currpstate
= pstate
;
275 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
276 static int transition_fid_vid(struct powernow_k8_data
*data
, u32 reqfid
, u32 reqvid
)
278 if (core_voltage_pre_transition(data
, reqvid
))
281 if (core_frequency_transition(data
, reqfid
))
284 if (core_voltage_post_transition(data
, reqvid
))
287 if (query_current_values_with_pending_wait(data
))
290 if ((reqfid
!= data
->currfid
) || (reqvid
!= data
->currvid
)) {
291 printk(KERN_ERR PFX
"failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
293 reqfid
, reqvid
, data
->currfid
, data
->currvid
);
297 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
298 smp_processor_id(), data
->currfid
, data
->currvid
);
303 /* Phase 1 - core voltage transition ... setup voltage */
304 static int core_voltage_pre_transition(struct powernow_k8_data
*data
, u32 reqvid
)
306 u32 rvosteps
= data
->rvo
;
307 u32 savefid
= data
->currfid
;
310 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
312 data
->currfid
, data
->currvid
, reqvid
, data
->rvo
);
314 rdmsr(MSR_FIDVID_STATUS
, lo
, maxvid
);
315 maxvid
= 0x1f & (maxvid
>> 16);
316 dprintk("ph1 maxvid=0x%x\n", maxvid
);
317 if (reqvid
< maxvid
) /* lower numbers are higher voltages */
320 while (data
->currvid
> reqvid
) {
321 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
322 data
->currvid
, reqvid
);
323 if (decrease_vid_code_by_step(data
, reqvid
, data
->vidmvs
))
327 while ((rvosteps
> 0) && ((data
->rvo
+ data
->currvid
) > reqvid
)) {
328 if (data
->currvid
== maxvid
) {
331 dprintk("ph1: changing vid for rvo, req 0x%x\n",
333 if (decrease_vid_code_by_step(data
, data
->currvid
- 1, 1))
339 if (query_current_values_with_pending_wait(data
))
342 if (savefid
!= data
->currfid
) {
343 printk(KERN_ERR PFX
"ph1 err, currfid changed 0x%x\n", data
->currfid
);
347 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
348 data
->currfid
, data
->currvid
);
353 /* Phase 2 - core frequency transition */
354 static int core_frequency_transition(struct powernow_k8_data
*data
, u32 reqfid
)
356 u32 vcoreqfid
, vcocurrfid
, vcofiddiff
, fid_interval
, savevid
= data
->currvid
;
358 if ((reqfid
< HI_FID_TABLE_BOTTOM
) && (data
->currfid
< HI_FID_TABLE_BOTTOM
)) {
359 printk(KERN_ERR PFX
"ph2: illegal lo-lo transition 0x%x 0x%x\n",
360 reqfid
, data
->currfid
);
364 if (data
->currfid
== reqfid
) {
365 printk(KERN_ERR PFX
"ph2 null fid transition 0x%x\n", data
->currfid
);
369 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
371 data
->currfid
, data
->currvid
, reqfid
);
373 vcoreqfid
= convert_fid_to_vco_fid(reqfid
);
374 vcocurrfid
= convert_fid_to_vco_fid(data
->currfid
);
375 vcofiddiff
= vcocurrfid
> vcoreqfid
? vcocurrfid
- vcoreqfid
376 : vcoreqfid
- vcocurrfid
;
378 while (vcofiddiff
> 2) {
379 (data
->currfid
& 1) ? (fid_interval
= 1) : (fid_interval
= 2);
381 if (reqfid
> data
->currfid
) {
382 if (data
->currfid
> LO_FID_TABLE_TOP
) {
383 if (write_new_fid(data
, data
->currfid
+ fid_interval
)) {
388 (data
, 2 + convert_fid_to_vco_fid(data
->currfid
))) {
393 if (write_new_fid(data
, data
->currfid
- fid_interval
))
397 vcocurrfid
= convert_fid_to_vco_fid(data
->currfid
);
398 vcofiddiff
= vcocurrfid
> vcoreqfid
? vcocurrfid
- vcoreqfid
399 : vcoreqfid
- vcocurrfid
;
402 if (write_new_fid(data
, reqfid
))
405 if (query_current_values_with_pending_wait(data
))
408 if (data
->currfid
!= reqfid
) {
410 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
411 data
->currfid
, reqfid
);
415 if (savevid
!= data
->currvid
) {
416 printk(KERN_ERR PFX
"ph2: vid changed, save 0x%x, curr 0x%x\n",
417 savevid
, data
->currvid
);
421 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
422 data
->currfid
, data
->currvid
);
427 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
428 static int core_voltage_post_transition(struct powernow_k8_data
*data
, u32 reqvid
)
430 u32 savefid
= data
->currfid
;
431 u32 savereqvid
= reqvid
;
433 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
435 data
->currfid
, data
->currvid
);
437 if (reqvid
!= data
->currvid
) {
438 if (write_new_vid(data
, reqvid
))
441 if (savefid
!= data
->currfid
) {
443 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
444 savefid
, data
->currfid
);
448 if (data
->currvid
!= reqvid
) {
450 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
451 reqvid
, data
->currvid
);
456 if (query_current_values_with_pending_wait(data
))
459 if (savereqvid
!= data
->currvid
) {
460 dprintk("ph3 failed, currvid 0x%x\n", data
->currvid
);
464 if (savefid
!= data
->currfid
) {
465 dprintk("ph3 failed, currfid changed 0x%x\n",
470 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
471 data
->currfid
, data
->currvid
);
476 static int check_supported_cpu(unsigned int cpu
)
479 u32 eax
, ebx
, ecx
, edx
;
482 oldmask
= current
->cpus_allowed
;
483 set_cpus_allowed_ptr(current
, &cpumask_of_cpu(cpu
));
485 if (smp_processor_id() != cpu
) {
486 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", cpu
);
490 if (current_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
493 eax
= cpuid_eax(CPUID_PROCESSOR_SIGNATURE
);
494 if (((eax
& CPUID_XFAM
) != CPUID_XFAM_K8
) &&
495 ((eax
& CPUID_XFAM
) < CPUID_XFAM_10H
))
498 if ((eax
& CPUID_XFAM
) == CPUID_XFAM_K8
) {
499 if (((eax
& CPUID_USE_XFAM_XMOD
) != CPUID_USE_XFAM_XMOD
) ||
500 ((eax
& CPUID_XMOD
) > CPUID_XMOD_REV_MASK
)) {
501 printk(KERN_INFO PFX
"Processor cpuid %x not supported\n", eax
);
505 eax
= cpuid_eax(CPUID_GET_MAX_CAPABILITIES
);
506 if (eax
< CPUID_FREQ_VOLT_CAPABILITIES
) {
508 "No frequency change capabilities detected\n");
512 cpuid(CPUID_FREQ_VOLT_CAPABILITIES
, &eax
, &ebx
, &ecx
, &edx
);
513 if ((edx
& P_STATE_TRANSITION_CAPABLE
) != P_STATE_TRANSITION_CAPABLE
) {
514 printk(KERN_INFO PFX
"Power state transitions not supported\n");
517 } else { /* must be a HW Pstate capable processor */
518 cpuid(CPUID_FREQ_VOLT_CAPABILITIES
, &eax
, &ebx
, &ecx
, &edx
);
519 if ((edx
& USE_HW_PSTATE
) == USE_HW_PSTATE
)
520 cpu_family
= CPU_HW_PSTATE
;
528 set_cpus_allowed_ptr(current
, &oldmask
);
532 static int check_pst_table(struct powernow_k8_data
*data
, struct pst_s
*pst
, u8 maxvid
)
537 for (j
= 0; j
< data
->numps
; j
++) {
538 if (pst
[j
].vid
> LEAST_VID
) {
539 printk(KERN_ERR PFX
"vid %d invalid : 0x%x\n", j
, pst
[j
].vid
);
542 if (pst
[j
].vid
< data
->rvo
) { /* vid + rvo >= 0 */
543 printk(KERN_ERR BFX
"0 vid exceeded with pstate %d\n", j
);
546 if (pst
[j
].vid
< maxvid
+ data
->rvo
) { /* vid + rvo >= maxvid */
547 printk(KERN_ERR BFX
"maxvid exceeded with pstate %d\n", j
);
550 if (pst
[j
].fid
> MAX_FID
) {
551 printk(KERN_ERR BFX
"maxfid exceeded with pstate %d\n", j
);
554 if (j
&& (pst
[j
].fid
< HI_FID_TABLE_BOTTOM
)) {
555 /* Only first fid is allowed to be in "low" range */
556 printk(KERN_ERR BFX
"two low fids - %d : 0x%x\n", j
, pst
[j
].fid
);
559 if (pst
[j
].fid
< lastfid
)
560 lastfid
= pst
[j
].fid
;
563 printk(KERN_ERR BFX
"lastfid invalid\n");
566 if (lastfid
> LO_FID_TABLE_TOP
)
567 printk(KERN_INFO BFX
"first fid not from lo freq table\n");
572 static void print_basics(struct powernow_k8_data
*data
)
575 for (j
= 0; j
< data
->numps
; j
++) {
576 if (data
->powernow_table
[j
].frequency
!= CPUFREQ_ENTRY_INVALID
) {
577 if (cpu_family
== CPU_HW_PSTATE
) {
578 printk(KERN_INFO PFX
" %d : pstate %d (%d MHz)\n",
580 data
->powernow_table
[j
].index
,
581 data
->powernow_table
[j
].frequency
/1000);
583 printk(KERN_INFO PFX
" %d : fid 0x%x (%d MHz), vid 0x%x\n",
585 data
->powernow_table
[j
].index
& 0xff,
586 data
->powernow_table
[j
].frequency
/1000,
587 data
->powernow_table
[j
].index
>> 8);
592 printk(KERN_INFO PFX
"Only %d pstates on battery\n", data
->batps
);
595 static int fill_powernow_table(struct powernow_k8_data
*data
, struct pst_s
*pst
, u8 maxvid
)
597 struct cpufreq_frequency_table
*powernow_table
;
600 if (data
->batps
) { /* use ACPI support to get full speed on mains power */
601 printk(KERN_WARNING PFX
"Only %d pstates usable (use ACPI driver for full range\n", data
->batps
);
602 data
->numps
= data
->batps
;
605 for ( j
=1; j
<data
->numps
; j
++ ) {
606 if (pst
[j
-1].fid
>= pst
[j
].fid
) {
607 printk(KERN_ERR PFX
"PST out of sequence\n");
612 if (data
->numps
< 2) {
613 printk(KERN_ERR PFX
"no p states to transition\n");
617 if (check_pst_table(data
, pst
, maxvid
))
620 powernow_table
= kmalloc((sizeof(struct cpufreq_frequency_table
)
621 * (data
->numps
+ 1)), GFP_KERNEL
);
622 if (!powernow_table
) {
623 printk(KERN_ERR PFX
"powernow_table memory alloc failure\n");
627 for (j
= 0; j
< data
->numps
; j
++) {
628 powernow_table
[j
].index
= pst
[j
].fid
; /* lower 8 bits */
629 powernow_table
[j
].index
|= (pst
[j
].vid
<< 8); /* upper 8 bits */
630 powernow_table
[j
].frequency
= find_khz_freq_from_fid(pst
[j
].fid
);
632 powernow_table
[data
->numps
].frequency
= CPUFREQ_TABLE_END
;
633 powernow_table
[data
->numps
].index
= 0;
635 if (query_current_values_with_pending_wait(data
)) {
636 kfree(powernow_table
);
640 dprintk("cfid 0x%x, cvid 0x%x\n", data
->currfid
, data
->currvid
);
641 data
->powernow_table
= powernow_table
;
642 if (first_cpu(per_cpu(cpu_core_map
, data
->cpu
)) == data
->cpu
)
645 for (j
= 0; j
< data
->numps
; j
++)
646 if ((pst
[j
].fid
==data
->currfid
) && (pst
[j
].vid
==data
->currvid
))
649 dprintk("currfid/vid do not match PST, ignoring\n");
653 /* Find and validate the PSB/PST table in BIOS. */
654 static int find_psb_table(struct powernow_k8_data
*data
)
663 for (i
= 0xc0000; i
< 0xffff0; i
+= 0x10) {
664 /* Scan BIOS looking for the signature. */
665 /* It can not be at ffff0 - it is too big. */
667 psb
= phys_to_virt(i
);
668 if (memcmp(psb
, PSB_ID_STRING
, PSB_ID_STRING_LEN
) != 0)
671 dprintk("found PSB header at 0x%p\n", psb
);
673 dprintk("table vers: 0x%x\n", psb
->tableversion
);
674 if (psb
->tableversion
!= PSB_VERSION_1_4
) {
675 printk(KERN_ERR BFX
"PSB table is not v1.4\n");
679 dprintk("flags: 0x%x\n", psb
->flags1
);
681 printk(KERN_ERR BFX
"unknown flags\n");
685 data
->vstable
= psb
->vstable
;
686 dprintk("voltage stabilization time: %d(*20us)\n", data
->vstable
);
688 dprintk("flags2: 0x%x\n", psb
->flags2
);
689 data
->rvo
= psb
->flags2
& 3;
690 data
->irt
= ((psb
->flags2
) >> 2) & 3;
691 mvs
= ((psb
->flags2
) >> 4) & 3;
692 data
->vidmvs
= 1 << mvs
;
693 data
->batps
= ((psb
->flags2
) >> 6) & 3;
695 dprintk("ramp voltage offset: %d\n", data
->rvo
);
696 dprintk("isochronous relief time: %d\n", data
->irt
);
697 dprintk("maximum voltage step: %d - 0x%x\n", mvs
, data
->vidmvs
);
699 dprintk("numpst: 0x%x\n", psb
->num_tables
);
700 cpst
= psb
->num_tables
;
701 if ((psb
->cpuid
== 0x00000fc0) || (psb
->cpuid
== 0x00000fe0) ){
702 thiscpuid
= cpuid_eax(CPUID_PROCESSOR_SIGNATURE
);
703 if ((thiscpuid
== 0x00000fc0) || (thiscpuid
== 0x00000fe0) ) {
708 printk(KERN_ERR BFX
"numpst must be 1\n");
712 data
->plllock
= psb
->plllocktime
;
713 dprintk("plllocktime: 0x%x (units 1us)\n", psb
->plllocktime
);
714 dprintk("maxfid: 0x%x\n", psb
->maxfid
);
715 dprintk("maxvid: 0x%x\n", psb
->maxvid
);
716 maxvid
= psb
->maxvid
;
718 data
->numps
= psb
->numps
;
719 dprintk("numpstates: 0x%x\n", data
->numps
);
720 return fill_powernow_table(data
, (struct pst_s
*)(psb
+1), maxvid
);
723 * If you see this message, complain to BIOS manufacturer. If
724 * he tells you "we do not support Linux" or some similar
725 * nonsense, remember that Windows 2000 uses the same legacy
726 * mechanism that the old Linux PSB driver uses. Tell them it
727 * is broken with Windows 2000.
729 * The reference to the AMD documentation is chapter 9 in the
730 * BIOS and Kernel Developer's Guide, which is available on
733 printk(KERN_ERR PFX
"BIOS error - no PSB or ACPI _PSS objects\n");
737 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
738 static void powernow_k8_acpi_pst_values(struct powernow_k8_data
*data
, unsigned int index
)
740 if (!data
->acpi_data
.state_count
|| (cpu_family
== CPU_HW_PSTATE
))
743 data
->irt
= (data
->acpi_data
.states
[index
].control
>> IRT_SHIFT
) & IRT_MASK
;
744 data
->rvo
= (data
->acpi_data
.states
[index
].control
>> RVO_SHIFT
) & RVO_MASK
;
745 data
->exttype
= (data
->acpi_data
.states
[index
].control
>> EXT_TYPE_SHIFT
) & EXT_TYPE_MASK
;
746 data
->plllock
= (data
->acpi_data
.states
[index
].control
>> PLL_L_SHIFT
) & PLL_L_MASK
;
747 data
->vidmvs
= 1 << ((data
->acpi_data
.states
[index
].control
>> MVS_SHIFT
) & MVS_MASK
);
748 data
->vstable
= (data
->acpi_data
.states
[index
].control
>> VST_SHIFT
) & VST_MASK
;
751 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data
*data
)
753 struct cpufreq_frequency_table
*powernow_table
;
756 if (acpi_processor_register_performance(&data
->acpi_data
, data
->cpu
)) {
757 dprintk("register performance failed: bad ACPI data\n");
761 /* verify the data contained in the ACPI structures */
762 if (data
->acpi_data
.state_count
<= 1) {
763 dprintk("No ACPI P-States\n");
767 if ((data
->acpi_data
.control_register
.space_id
!= ACPI_ADR_SPACE_FIXED_HARDWARE
) ||
768 (data
->acpi_data
.status_register
.space_id
!= ACPI_ADR_SPACE_FIXED_HARDWARE
)) {
769 dprintk("Invalid control/status registers (%x - %x)\n",
770 data
->acpi_data
.control_register
.space_id
,
771 data
->acpi_data
.status_register
.space_id
);
775 /* fill in data->powernow_table */
776 powernow_table
= kmalloc((sizeof(struct cpufreq_frequency_table
)
777 * (data
->acpi_data
.state_count
+ 1)), GFP_KERNEL
);
778 if (!powernow_table
) {
779 dprintk("powernow_table memory alloc failure\n");
783 if (cpu_family
== CPU_HW_PSTATE
)
784 ret_val
= fill_powernow_table_pstate(data
, powernow_table
);
786 ret_val
= fill_powernow_table_fidvid(data
, powernow_table
);
790 powernow_table
[data
->acpi_data
.state_count
].frequency
= CPUFREQ_TABLE_END
;
791 powernow_table
[data
->acpi_data
.state_count
].index
= 0;
792 data
->powernow_table
= powernow_table
;
795 data
->numps
= data
->acpi_data
.state_count
;
796 if (first_cpu(per_cpu(cpu_core_map
, data
->cpu
)) == data
->cpu
)
798 powernow_k8_acpi_pst_values(data
, 0);
800 /* notify BIOS that we exist */
801 acpi_processor_notify_smm(THIS_MODULE
);
806 kfree(powernow_table
);
809 acpi_processor_unregister_performance(&data
->acpi_data
, data
->cpu
);
811 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
812 data
->acpi_data
.state_count
= 0;
817 static int fill_powernow_table_pstate(struct powernow_k8_data
*data
, struct cpufreq_frequency_table
*powernow_table
)
821 rdmsr(MSR_PSTATE_CUR_LIMIT
, hi
, lo
);
822 data
->max_hw_pstate
= (hi
& HW_PSTATE_MAX_MASK
) >> HW_PSTATE_MAX_SHIFT
;
824 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
827 index
= data
->acpi_data
.states
[i
].control
& HW_PSTATE_MASK
;
828 if (index
> data
->max_hw_pstate
) {
829 printk(KERN_ERR PFX
"invalid pstate %d - bad value %d.\n", i
, index
);
830 printk(KERN_ERR PFX
"Please report to BIOS manufacturer\n");
831 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
834 rdmsr(MSR_PSTATE_DEF_BASE
+ index
, lo
, hi
);
835 if (!(hi
& HW_PSTATE_VALID_MASK
)) {
836 dprintk("invalid pstate %d, ignoring\n", index
);
837 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
841 powernow_table
[i
].index
= index
;
843 powernow_table
[i
].frequency
= data
->acpi_data
.states
[i
].core_frequency
* 1000;
848 static int fill_powernow_table_fidvid(struct powernow_k8_data
*data
, struct cpufreq_frequency_table
*powernow_table
)
852 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
857 fid
= data
->acpi_data
.states
[i
].status
& EXT_FID_MASK
;
858 vid
= (data
->acpi_data
.states
[i
].status
>> VID_SHIFT
) & EXT_VID_MASK
;
860 fid
= data
->acpi_data
.states
[i
].control
& FID_MASK
;
861 vid
= (data
->acpi_data
.states
[i
].control
>> VID_SHIFT
) & VID_MASK
;
864 dprintk(" %d : fid 0x%x, vid 0x%x\n", i
, fid
, vid
);
866 powernow_table
[i
].index
= fid
; /* lower 8 bits */
867 powernow_table
[i
].index
|= (vid
<< 8); /* upper 8 bits */
868 powernow_table
[i
].frequency
= find_khz_freq_from_fid(fid
);
870 /* verify frequency is OK */
871 if ((powernow_table
[i
].frequency
> (MAX_FREQ
* 1000)) ||
872 (powernow_table
[i
].frequency
< (MIN_FREQ
* 1000))) {
873 dprintk("invalid freq %u kHz, ignoring\n", powernow_table
[i
].frequency
);
874 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
878 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
879 if (vid
== VID_OFF
) {
880 dprintk("invalid vid %u, ignoring\n", vid
);
881 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
885 /* verify only 1 entry from the lo frequency table */
886 if (fid
< HI_FID_TABLE_BOTTOM
) {
888 /* if both entries are the same, ignore this one ... */
889 if ((powernow_table
[i
].frequency
!= powernow_table
[cntlofreq
].frequency
) ||
890 (powernow_table
[i
].index
!= powernow_table
[cntlofreq
].index
)) {
891 printk(KERN_ERR PFX
"Too many lo freq table entries\n");
895 dprintk("double low frequency table entry, ignoring it.\n");
896 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
902 if (powernow_table
[i
].frequency
!= (data
->acpi_data
.states
[i
].core_frequency
* 1000)) {
903 printk(KERN_INFO PFX
"invalid freq entries %u kHz vs. %u kHz\n",
904 powernow_table
[i
].frequency
,
905 (unsigned int) (data
->acpi_data
.states
[i
].core_frequency
* 1000));
906 powernow_table
[i
].frequency
= CPUFREQ_ENTRY_INVALID
;
913 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data
*data
)
915 if (data
->acpi_data
.state_count
)
916 acpi_processor_unregister_performance(&data
->acpi_data
, data
->cpu
);
920 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data
*data
) { return -ENODEV
; }
921 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data
*data
) { return; }
922 static void powernow_k8_acpi_pst_values(struct powernow_k8_data
*data
, unsigned int index
) { return; }
923 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
925 /* Take a frequency, and issue the fid/vid transition command */
926 static int transition_frequency_fidvid(struct powernow_k8_data
*data
, unsigned int index
)
931 struct cpufreq_freqs freqs
;
933 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index
);
935 /* fid/vid correctness check for k8 */
936 /* fid are the lower 8 bits of the index we stored into
937 * the cpufreq frequency table in find_psb_table, vid
938 * are the upper 8 bits.
940 fid
= data
->powernow_table
[index
].index
& 0xFF;
941 vid
= (data
->powernow_table
[index
].index
& 0xFF00) >> 8;
943 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid
, vid
);
945 if (query_current_values_with_pending_wait(data
))
948 if ((data
->currvid
== vid
) && (data
->currfid
== fid
)) {
949 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
954 if ((fid
< HI_FID_TABLE_BOTTOM
) && (data
->currfid
< HI_FID_TABLE_BOTTOM
)) {
956 "ignoring illegal change in lo freq table-%x to 0x%x\n",
961 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
962 smp_processor_id(), fid
, vid
);
963 freqs
.old
= find_khz_freq_from_fid(data
->currfid
);
964 freqs
.new = find_khz_freq_from_fid(fid
);
966 for_each_cpu_mask_nr(i
, *(data
->available_cores
)) {
968 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
971 res
= transition_fid_vid(data
, fid
, vid
);
972 freqs
.new = find_khz_freq_from_fid(data
->currfid
);
974 for_each_cpu_mask_nr(i
, *(data
->available_cores
)) {
976 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
981 /* Take a frequency, and issue the hardware pstate transition command */
982 static int transition_frequency_pstate(struct powernow_k8_data
*data
, unsigned int index
)
986 struct cpufreq_freqs freqs
;
988 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index
);
990 /* get MSR index for hardware pstate transition */
991 pstate
= index
& HW_PSTATE_MASK
;
992 if (pstate
> data
->max_hw_pstate
)
994 freqs
.old
= find_khz_freq_from_pstate(data
->powernow_table
, data
->currpstate
);
995 freqs
.new = find_khz_freq_from_pstate(data
->powernow_table
, pstate
);
997 for_each_cpu_mask_nr(i
, *(data
->available_cores
)) {
999 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
1002 res
= transition_pstate(data
, pstate
);
1003 freqs
.new = find_khz_freq_from_pstate(data
->powernow_table
, pstate
);
1005 for_each_cpu_mask_nr(i
, *(data
->available_cores
)) {
1007 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
1012 /* Driver entry point to switch to the target frequency */
1013 static int powernowk8_target(struct cpufreq_policy
*pol
, unsigned targfreq
, unsigned relation
)
1016 struct powernow_k8_data
*data
= per_cpu(powernow_data
, pol
->cpu
);
1019 unsigned int newstate
;
1025 checkfid
= data
->currfid
;
1026 checkvid
= data
->currvid
;
1028 /* only run on specific CPU from here on */
1029 oldmask
= current
->cpus_allowed
;
1030 set_cpus_allowed_ptr(current
, &cpumask_of_cpu(pol
->cpu
));
1032 if (smp_processor_id() != pol
->cpu
) {
1033 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", pol
->cpu
);
1037 if (pending_bit_stuck()) {
1038 printk(KERN_ERR PFX
"failing targ, change pending bit set\n");
1042 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1043 pol
->cpu
, targfreq
, pol
->min
, pol
->max
, relation
);
1045 if (query_current_values_with_pending_wait(data
))
1048 if (cpu_family
!= CPU_HW_PSTATE
) {
1049 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1050 data
->currfid
, data
->currvid
);
1052 if ((checkvid
!= data
->currvid
) || (checkfid
!= data
->currfid
)) {
1053 printk(KERN_INFO PFX
1054 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1055 checkfid
, data
->currfid
, checkvid
, data
->currvid
);
1059 if (cpufreq_frequency_table_target(pol
, data
->powernow_table
, targfreq
, relation
, &newstate
))
1062 mutex_lock(&fidvid_mutex
);
1064 powernow_k8_acpi_pst_values(data
, newstate
);
1066 if (cpu_family
== CPU_HW_PSTATE
)
1067 ret
= transition_frequency_pstate(data
, newstate
);
1069 ret
= transition_frequency_fidvid(data
, newstate
);
1071 printk(KERN_ERR PFX
"transition frequency failed\n");
1073 mutex_unlock(&fidvid_mutex
);
1076 mutex_unlock(&fidvid_mutex
);
1078 if (cpu_family
== CPU_HW_PSTATE
)
1079 pol
->cur
= find_khz_freq_from_pstate(data
->powernow_table
, newstate
);
1081 pol
->cur
= find_khz_freq_from_fid(data
->currfid
);
1085 set_cpus_allowed_ptr(current
, &oldmask
);
1089 /* Driver entry point to verify the policy and range of frequencies */
1090 static int powernowk8_verify(struct cpufreq_policy
*pol
)
1092 struct powernow_k8_data
*data
= per_cpu(powernow_data
, pol
->cpu
);
1097 return cpufreq_frequency_table_verify(pol
, data
->powernow_table
);
1100 /* per CPU init entry point to the driver */
1101 static int __cpuinit
powernowk8_cpu_init(struct cpufreq_policy
*pol
)
1103 struct powernow_k8_data
*data
;
1107 if (!cpu_online(pol
->cpu
))
1110 if (!check_supported_cpu(pol
->cpu
))
1113 data
= kzalloc(sizeof(struct powernow_k8_data
), GFP_KERNEL
);
1115 printk(KERN_ERR PFX
"unable to alloc powernow_k8_data");
1119 data
->cpu
= pol
->cpu
;
1121 if (powernow_k8_cpu_init_acpi(data
)) {
1123 * Use the PSB BIOS structure. This is only availabe on
1124 * an UP version, and is deprecated by AMD.
1126 if (num_online_cpus() != 1) {
1127 #ifndef CONFIG_ACPI_PROCESSOR
1128 printk(KERN_ERR PFX
"ACPI Processor support is required "
1129 "for SMP systems but is absent. Please load the "
1130 "ACPI Processor module before starting this "
1133 printk(KERN_ERR PFX
"Your BIOS does not provide ACPI "
1134 "_PSS objects in a way that Linux understands. "
1135 "Please report this to the Linux ACPI maintainers"
1136 " and complain to your BIOS vendor.\n");
1141 if (pol
->cpu
!= 0) {
1142 printk(KERN_ERR PFX
"No ACPI _PSS objects for CPU other than "
1143 "CPU0. Complain to your BIOS vendor.\n");
1147 rc
= find_psb_table(data
);
1154 /* only run on specific CPU from here on */
1155 oldmask
= current
->cpus_allowed
;
1156 set_cpus_allowed_ptr(current
, &cpumask_of_cpu(pol
->cpu
));
1158 if (smp_processor_id() != pol
->cpu
) {
1159 printk(KERN_ERR PFX
"limiting to cpu %u failed\n", pol
->cpu
);
1163 if (pending_bit_stuck()) {
1164 printk(KERN_ERR PFX
"failing init, change pending bit set\n");
1168 if (query_current_values_with_pending_wait(data
))
1171 if (cpu_family
== CPU_OPTERON
)
1174 /* run on any CPU again */
1175 set_cpus_allowed_ptr(current
, &oldmask
);
1177 if (cpu_family
== CPU_HW_PSTATE
)
1178 pol
->cpus
= cpumask_of_cpu(pol
->cpu
);
1180 pol
->cpus
= per_cpu(cpu_core_map
, pol
->cpu
);
1181 data
->available_cores
= &(pol
->cpus
);
1183 /* Take a crude guess here.
1184 * That guess was in microseconds, so multiply with 1000 */
1185 pol
->cpuinfo
.transition_latency
= (((data
->rvo
+ 8) * data
->vstable
* VST_UNITS_20US
)
1186 + (3 * (1 << data
->irt
) * 10)) * 1000;
1188 if (cpu_family
== CPU_HW_PSTATE
)
1189 pol
->cur
= find_khz_freq_from_pstate(data
->powernow_table
, data
->currpstate
);
1191 pol
->cur
= find_khz_freq_from_fid(data
->currfid
);
1192 dprintk("policy current frequency %d kHz\n", pol
->cur
);
1194 /* min/max the cpu is capable of */
1195 if (cpufreq_frequency_table_cpuinfo(pol
, data
->powernow_table
)) {
1196 printk(KERN_ERR PFX
"invalid powernow_table\n");
1197 powernow_k8_cpu_exit_acpi(data
);
1198 kfree(data
->powernow_table
);
1203 cpufreq_frequency_table_get_attr(data
->powernow_table
, pol
->cpu
);
1205 if (cpu_family
== CPU_HW_PSTATE
)
1206 dprintk("cpu_init done, current pstate 0x%x\n", data
->currpstate
);
1208 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1209 data
->currfid
, data
->currvid
);
1211 per_cpu(powernow_data
, pol
->cpu
) = data
;
1216 set_cpus_allowed_ptr(current
, &oldmask
);
1217 powernow_k8_cpu_exit_acpi(data
);
1223 static int __devexit
powernowk8_cpu_exit (struct cpufreq_policy
*pol
)
1225 struct powernow_k8_data
*data
= per_cpu(powernow_data
, pol
->cpu
);
1230 powernow_k8_cpu_exit_acpi(data
);
1232 cpufreq_frequency_table_put_attr(pol
->cpu
);
1234 kfree(data
->powernow_table
);
1240 static unsigned int powernowk8_get (unsigned int cpu
)
1242 struct powernow_k8_data
*data
;
1243 cpumask_t oldmask
= current
->cpus_allowed
;
1244 unsigned int khz
= 0;
1247 first
= first_cpu(per_cpu(cpu_core_map
, cpu
));
1248 data
= per_cpu(powernow_data
, first
);
1253 set_cpus_allowed_ptr(current
, &cpumask_of_cpu(cpu
));
1254 if (smp_processor_id() != cpu
) {
1256 "limiting to CPU %d failed in powernowk8_get\n", cpu
);
1257 set_cpus_allowed_ptr(current
, &oldmask
);
1261 if (query_current_values_with_pending_wait(data
))
1264 if (cpu_family
== CPU_HW_PSTATE
)
1265 khz
= find_khz_freq_from_pstate(data
->powernow_table
,
1268 khz
= find_khz_freq_from_fid(data
->currfid
);
1272 set_cpus_allowed_ptr(current
, &oldmask
);
1276 static struct freq_attr
* powernow_k8_attr
[] = {
1277 &cpufreq_freq_attr_scaling_available_freqs
,
1281 static struct cpufreq_driver cpufreq_amd64_driver
= {
1282 .verify
= powernowk8_verify
,
1283 .target
= powernowk8_target
,
1284 .init
= powernowk8_cpu_init
,
1285 .exit
= __devexit_p(powernowk8_cpu_exit
),
1286 .get
= powernowk8_get
,
1287 .name
= "powernow-k8",
1288 .owner
= THIS_MODULE
,
1289 .attr
= powernow_k8_attr
,
1292 /* driver entry point for init */
1293 static int __cpuinit
powernowk8_init(void)
1295 unsigned int i
, supported_cpus
= 0;
1297 for_each_online_cpu(i
) {
1298 if (check_supported_cpu(i
))
1302 if (supported_cpus
== num_online_cpus()) {
1303 printk(KERN_INFO PFX
"Found %d %s "
1304 "processors (%d cpu cores) (" VERSION
")\n",
1306 boot_cpu_data
.x86_model_id
, supported_cpus
);
1307 return cpufreq_register_driver(&cpufreq_amd64_driver
);
1313 /* driver entry point for term */
1314 static void __exit
powernowk8_exit(void)
1318 cpufreq_unregister_driver(&cpufreq_amd64_driver
);
1321 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
1322 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1323 MODULE_LICENSE("GPL");
1325 late_initcall(powernowk8_init
);
1326 module_exit(powernowk8_exit
);