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 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);
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 if (current_cpu_data.x86 == 0x10)
80 return 100 * (fid + 0x10) >> did;
82 return 100 * (fid + 0x8) >> did;
85 static u32 find_khz_freq_from_fiddid(u32 fid, u32 did)
87 return 1000 * find_freq_from_fiddid(fid, did);
90 static u32 find_fid_from_pstate(u32 pstate)
93 rdmsr(MSR_PSTATE_DEF_BASE + pstate, lo, hi);
94 return lo & HW_PSTATE_FID_MASK;
97 static u32 find_did_from_pstate(u32 pstate)
100 rdmsr(MSR_PSTATE_DEF_BASE + pstate, lo, hi);
101 return (lo & HW_PSTATE_DID_MASK) >> HW_PSTATE_DID_SHIFT;
104 /* Return the vco fid for an input fid
106 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
107 * only from corresponding high fids. This returns "high" fid corresponding to
110 static u32 convert_fid_to_vco_fid(u32 fid)
112 if (fid < HI_FID_TABLE_BOTTOM)
113 return 8 + (2 * fid);
119 * Return 1 if the pending bit is set. Unless we just instructed the processor
120 * to transition to a new state, seeing this bit set is really bad news.
122 static int pending_bit_stuck(void)
126 if (cpu_family == CPU_HW_PSTATE)
129 rdmsr(MSR_FIDVID_STATUS, lo, hi);
130 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
134 * Update the global current fid / vid values from the status msr.
135 * Returns 1 on error.
137 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
142 if (cpu_family == CPU_HW_PSTATE) {
143 rdmsr(MSR_PSTATE_STATUS, lo, hi);
144 i = lo & HW_PSTATE_MASK;
145 rdmsr(MSR_PSTATE_DEF_BASE + i, lo, hi);
146 data->currfid = lo & HW_PSTATE_FID_MASK;
147 data->currdid = (lo & HW_PSTATE_DID_MASK) >> HW_PSTATE_DID_SHIFT;
152 dprintk("detected change pending stuck\n");
155 rdmsr(MSR_FIDVID_STATUS, lo, hi);
156 } while (lo & MSR_S_LO_CHANGE_PENDING);
158 data->currvid = hi & MSR_S_HI_CURRENT_VID;
159 data->currfid = lo & MSR_S_LO_CURRENT_FID;
164 /* the isochronous relief time */
165 static void count_off_irt(struct powernow_k8_data *data)
167 udelay((1 << data->irt) * 10);
171 /* the voltage stabalization time */
172 static void count_off_vst(struct powernow_k8_data *data)
174 udelay(data->vstable * VST_UNITS_20US);
178 /* need to init the control msr to a safe value (for each cpu) */
179 static void fidvid_msr_init(void)
184 rdmsr(MSR_FIDVID_STATUS, lo, hi);
185 vid = hi & MSR_S_HI_CURRENT_VID;
186 fid = lo & MSR_S_LO_CURRENT_FID;
187 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
188 hi = MSR_C_HI_STP_GNT_BENIGN;
189 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
190 wrmsr(MSR_FIDVID_CTL, lo, hi);
194 /* write the new fid value along with the other control fields to the msr */
195 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
198 u32 savevid = data->currvid;
201 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
202 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
206 lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
208 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
209 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
212 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
214 printk(KERN_ERR PFX "Hardware error - pending bit very stuck - no further pstate changes possible\n");
217 } while (query_current_values_with_pending_wait(data));
221 if (savevid != data->currvid) {
222 printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
223 savevid, data->currvid);
227 if (fid != data->currfid) {
228 printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
236 /* Write a new vid to the hardware */
237 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
240 u32 savefid = data->currfid;
243 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
244 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
248 lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
250 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
251 vid, lo, STOP_GRANT_5NS);
254 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
256 printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
259 } while (query_current_values_with_pending_wait(data));
261 if (savefid != data->currfid) {
262 printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
263 savefid, data->currfid);
267 if (vid != data->currvid) {
268 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
277 * Reduce the vid by the max of step or reqvid.
278 * Decreasing vid codes represent increasing voltages:
279 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
281 static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
283 if ((data->currvid - reqvid) > step)
284 reqvid = data->currvid - step;
286 if (write_new_vid(data, reqvid))
294 /* Change hardware pstate by single MSR write */
295 static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
297 wrmsr(MSR_PSTATE_CTRL, pstate, 0);
298 data->currfid = find_fid_from_pstate(pstate);
302 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
303 static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
305 if (core_voltage_pre_transition(data, reqvid))
308 if (core_frequency_transition(data, reqfid))
311 if (core_voltage_post_transition(data, reqvid))
314 if (query_current_values_with_pending_wait(data))
317 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
318 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
320 reqfid, reqvid, data->currfid, data->currvid);
324 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
325 smp_processor_id(), data->currfid, data->currvid);
330 /* Phase 1 - core voltage transition ... setup voltage */
331 static int core_voltage_pre_transition(struct powernow_k8_data *data, u32 reqvid)
333 u32 rvosteps = data->rvo;
334 u32 savefid = data->currfid;
337 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
339 data->currfid, data->currvid, reqvid, data->rvo);
341 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
342 maxvid = 0x1f & (maxvid >> 16);
343 dprintk("ph1 maxvid=0x%x\n", maxvid);
344 if (reqvid < maxvid) /* lower numbers are higher voltages */
347 while (data->currvid > reqvid) {
348 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
349 data->currvid, reqvid);
350 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
354 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
355 if (data->currvid == maxvid) {
358 dprintk("ph1: changing vid for rvo, req 0x%x\n",
360 if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
366 if (query_current_values_with_pending_wait(data))
369 if (savefid != data->currfid) {
370 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
374 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
375 data->currfid, data->currvid);
380 /* Phase 2 - core frequency transition */
381 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
383 u32 vcoreqfid, vcocurrfid, vcofiddiff, fid_interval, savevid = data->currvid;
385 if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
386 printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
387 reqfid, data->currfid);
391 if (data->currfid == reqfid) {
392 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
396 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
398 data->currfid, data->currvid, reqfid);
400 vcoreqfid = convert_fid_to_vco_fid(reqfid);
401 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
402 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
403 : vcoreqfid - vcocurrfid;
405 while (vcofiddiff > 2) {
406 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
408 if (reqfid > data->currfid) {
409 if (data->currfid > LO_FID_TABLE_TOP) {
410 if (write_new_fid(data, data->currfid + fid_interval)) {
415 (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
420 if (write_new_fid(data, data->currfid - fid_interval))
424 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
425 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
426 : vcoreqfid - vcocurrfid;
429 if (write_new_fid(data, reqfid))
432 if (query_current_values_with_pending_wait(data))
435 if (data->currfid != reqfid) {
437 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
438 data->currfid, reqfid);
442 if (savevid != data->currvid) {
443 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
444 savevid, data->currvid);
448 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
449 data->currfid, data->currvid);
454 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
455 static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid)
457 u32 savefid = data->currfid;
458 u32 savereqvid = reqvid;
460 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
462 data->currfid, data->currvid);
464 if (reqvid != data->currvid) {
465 if (write_new_vid(data, reqvid))
468 if (savefid != data->currfid) {
470 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
471 savefid, data->currfid);
475 if (data->currvid != reqvid) {
477 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
478 reqvid, data->currvid);
483 if (query_current_values_with_pending_wait(data))
486 if (savereqvid != data->currvid) {
487 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
491 if (savefid != data->currfid) {
492 dprintk("ph3 failed, currfid changed 0x%x\n",
497 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
498 data->currfid, data->currvid);
503 static int check_supported_cpu(unsigned int cpu)
505 cpumask_t oldmask = CPU_MASK_ALL;
506 u32 eax, ebx, ecx, edx;
509 oldmask = current->cpus_allowed;
510 set_cpus_allowed(current, cpumask_of_cpu(cpu));
512 if (smp_processor_id() != cpu) {
513 printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
517 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
520 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
521 if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
522 ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
525 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
526 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
527 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
528 printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
532 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
533 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
535 "No frequency change capabilities detected\n");
539 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
540 if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
541 printk(KERN_INFO PFX "Power state transitions not supported\n");
544 } else { /* must be a HW Pstate capable processor */
545 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
546 if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
547 cpu_family = CPU_HW_PSTATE;
555 set_cpus_allowed(current, oldmask);
559 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
564 for (j = 0; j < data->numps; j++) {
565 if (pst[j].vid > LEAST_VID) {
566 printk(KERN_ERR PFX "vid %d invalid : 0x%x\n", j, pst[j].vid);
569 if (pst[j].vid < data->rvo) { /* vid + rvo >= 0 */
570 printk(KERN_ERR BFX "0 vid exceeded with pstate %d\n", j);
573 if (pst[j].vid < maxvid + data->rvo) { /* vid + rvo >= maxvid */
574 printk(KERN_ERR BFX "maxvid exceeded with pstate %d\n", j);
577 if (pst[j].fid > MAX_FID) {
578 printk(KERN_ERR BFX "maxfid exceeded with pstate %d\n", j);
581 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
582 /* Only first fid is allowed to be in "low" range */
583 printk(KERN_ERR BFX "two low fids - %d : 0x%x\n", j, pst[j].fid);
586 if (pst[j].fid < lastfid)
587 lastfid = pst[j].fid;
590 printk(KERN_ERR BFX "lastfid invalid\n");
593 if (lastfid > LO_FID_TABLE_TOP)
594 printk(KERN_INFO BFX "first fid not from lo freq table\n");
599 static void print_basics(struct powernow_k8_data *data)
602 for (j = 0; j < data->numps; j++) {
603 if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID) {
604 if (cpu_family == CPU_HW_PSTATE) {
605 printk(KERN_INFO PFX " %d : fid 0x%x did 0x%x (%d MHz)\n",
607 (data->powernow_table[j].index & 0xff00) >> 8,
608 (data->powernow_table[j].index & 0xff0000) >> 16,
609 data->powernow_table[j].frequency/1000);
611 printk(KERN_INFO PFX " %d : fid 0x%x (%d MHz), vid 0x%x\n",
613 data->powernow_table[j].index & 0xff,
614 data->powernow_table[j].frequency/1000,
615 data->powernow_table[j].index >> 8);
620 printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
623 static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
625 struct cpufreq_frequency_table *powernow_table;
628 if (data->batps) { /* use ACPI support to get full speed on mains power */
629 printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
630 data->numps = data->batps;
633 for ( j=1; j<data->numps; j++ ) {
634 if (pst[j-1].fid >= pst[j].fid) {
635 printk(KERN_ERR PFX "PST out of sequence\n");
640 if (data->numps < 2) {
641 printk(KERN_ERR PFX "no p states to transition\n");
645 if (check_pst_table(data, pst, maxvid))
648 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
649 * (data->numps + 1)), GFP_KERNEL);
650 if (!powernow_table) {
651 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
655 for (j = 0; j < data->numps; j++) {
656 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
657 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
658 powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
660 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
661 powernow_table[data->numps].index = 0;
663 if (query_current_values_with_pending_wait(data)) {
664 kfree(powernow_table);
668 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
669 data->powernow_table = powernow_table;
670 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
673 for (j = 0; j < data->numps; j++)
674 if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
677 dprintk("currfid/vid do not match PST, ignoring\n");
681 /* Find and validate the PSB/PST table in BIOS. */
682 static int find_psb_table(struct powernow_k8_data *data)
691 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
692 /* Scan BIOS looking for the signature. */
693 /* It can not be at ffff0 - it is too big. */
695 psb = phys_to_virt(i);
696 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
699 dprintk("found PSB header at 0x%p\n", psb);
701 dprintk("table vers: 0x%x\n", psb->tableversion);
702 if (psb->tableversion != PSB_VERSION_1_4) {
703 printk(KERN_ERR BFX "PSB table is not v1.4\n");
707 dprintk("flags: 0x%x\n", psb->flags1);
709 printk(KERN_ERR BFX "unknown flags\n");
713 data->vstable = psb->vstable;
714 dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
716 dprintk("flags2: 0x%x\n", psb->flags2);
717 data->rvo = psb->flags2 & 3;
718 data->irt = ((psb->flags2) >> 2) & 3;
719 mvs = ((psb->flags2) >> 4) & 3;
720 data->vidmvs = 1 << mvs;
721 data->batps = ((psb->flags2) >> 6) & 3;
723 dprintk("ramp voltage offset: %d\n", data->rvo);
724 dprintk("isochronous relief time: %d\n", data->irt);
725 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
727 dprintk("numpst: 0x%x\n", psb->num_tables);
728 cpst = psb->num_tables;
729 if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
730 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
731 if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
736 printk(KERN_ERR BFX "numpst must be 1\n");
740 data->plllock = psb->plllocktime;
741 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
742 dprintk("maxfid: 0x%x\n", psb->maxfid);
743 dprintk("maxvid: 0x%x\n", psb->maxvid);
744 maxvid = psb->maxvid;
746 data->numps = psb->numps;
747 dprintk("numpstates: 0x%x\n", data->numps);
748 return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
751 * If you see this message, complain to BIOS manufacturer. If
752 * he tells you "we do not support Linux" or some similar
753 * nonsense, remember that Windows 2000 uses the same legacy
754 * mechanism that the old Linux PSB driver uses. Tell them it
755 * is broken with Windows 2000.
757 * The reference to the AMD documentation is chapter 9 in the
758 * BIOS and Kernel Developer's Guide, which is available on
761 printk(KERN_ERR PFX "BIOS error - no PSB or ACPI _PSS objects\n");
765 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
766 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
768 if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
771 data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
772 data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
773 data->exttype = (data->acpi_data.states[index].control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
774 data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
775 data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
776 data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
779 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
781 struct cpufreq_frequency_table *powernow_table;
784 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
785 dprintk("register performance failed: bad ACPI data\n");
789 /* verify the data contained in the ACPI structures */
790 if (data->acpi_data.state_count <= 1) {
791 dprintk("No ACPI P-States\n");
795 if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
796 (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
797 dprintk("Invalid control/status registers (%x - %x)\n",
798 data->acpi_data.control_register.space_id,
799 data->acpi_data.status_register.space_id);
803 /* fill in data->powernow_table */
804 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
805 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
806 if (!powernow_table) {
807 dprintk("powernow_table memory alloc failure\n");
811 if (cpu_family == CPU_HW_PSTATE)
812 ret_val = fill_powernow_table_pstate(data, powernow_table);
814 ret_val = fill_powernow_table_fidvid(data, powernow_table);
818 powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
819 powernow_table[data->acpi_data.state_count].index = 0;
820 data->powernow_table = powernow_table;
823 data->numps = data->acpi_data.state_count;
824 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
826 powernow_k8_acpi_pst_values(data, 0);
828 /* notify BIOS that we exist */
829 acpi_processor_notify_smm(THIS_MODULE);
834 kfree(powernow_table);
837 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
839 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
840 data->acpi_data.state_count = 0;
845 static int fill_powernow_table_pstate(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
849 for (i = 0; i < data->acpi_data.state_count; i++) {
855 index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
856 if (index > MAX_HW_PSTATE) {
857 printk(KERN_ERR PFX "invalid pstate %d - bad value %d.\n", i, index);
858 printk(KERN_ERR PFX "Please report to BIOS manufacturer\n");
860 rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
861 if (!(hi & HW_PSTATE_VALID_MASK)) {
862 dprintk("invalid pstate %d, ignoring\n", index);
863 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
867 fid = lo & HW_PSTATE_FID_MASK;
868 did = (lo & HW_PSTATE_DID_MASK) >> HW_PSTATE_DID_SHIFT;
870 dprintk(" %d : fid 0x%x, did 0x%x\n", index, fid, did);
872 powernow_table[i].index = index | (fid << HW_FID_INDEX_SHIFT) | (did << HW_DID_INDEX_SHIFT);
874 powernow_table[i].frequency = find_khz_freq_from_fiddid(fid, did);
876 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
877 printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
878 powernow_table[i].frequency,
879 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
880 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
887 static int fill_powernow_table_fidvid(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
891 for (i = 0; i < data->acpi_data.state_count; i++) {
896 fid = data->acpi_data.states[i].status & EXT_FID_MASK;
897 vid = (data->acpi_data.states[i].status >> VID_SHIFT) & EXT_VID_MASK;
899 fid = data->acpi_data.states[i].control & FID_MASK;
900 vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
903 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
905 powernow_table[i].index = fid; /* lower 8 bits */
906 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
907 powernow_table[i].frequency = find_khz_freq_from_fid(fid);
909 /* verify frequency is OK */
910 if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
911 (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
912 dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
913 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
917 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
918 if (vid == VID_OFF) {
919 dprintk("invalid vid %u, ignoring\n", vid);
920 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
924 /* verify only 1 entry from the lo frequency table */
925 if (fid < HI_FID_TABLE_BOTTOM) {
927 /* if both entries are the same, ignore this one ... */
928 if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
929 (powernow_table[i].index != powernow_table[cntlofreq].index)) {
930 printk(KERN_ERR PFX "Too many lo freq table entries\n");
934 dprintk("double low frequency table entry, ignoring it.\n");
935 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
941 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
942 printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
943 powernow_table[i].frequency,
944 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
945 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
952 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
954 if (data->acpi_data.state_count)
955 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
959 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
960 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
961 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
962 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
964 /* Take a frequency, and issue the fid/vid transition command */
965 static int transition_frequency_fidvid(struct powernow_k8_data *data, unsigned int index)
970 struct cpufreq_freqs freqs;
972 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
974 /* fid/vid correctness check for k8 */
975 /* fid are the lower 8 bits of the index we stored into
976 * the cpufreq frequency table in find_psb_table, vid
977 * are the upper 8 bits.
979 fid = data->powernow_table[index].index & 0xFF;
980 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
982 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
984 if (query_current_values_with_pending_wait(data))
987 if ((data->currvid == vid) && (data->currfid == fid)) {
988 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
993 if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
995 "ignoring illegal change in lo freq table-%x to 0x%x\n",
1000 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
1001 smp_processor_id(), fid, vid);
1002 freqs.old = find_khz_freq_from_fid(data->currfid);
1003 freqs.new = find_khz_freq_from_fid(fid);
1005 for_each_cpu_mask(i, *(data->available_cores)) {
1007 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1010 res = transition_fid_vid(data, fid, vid);
1011 freqs.new = find_khz_freq_from_fid(data->currfid);
1013 for_each_cpu_mask(i, *(data->available_cores)) {
1015 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1020 /* Take a frequency, and issue the hardware pstate transition command */
1021 static int transition_frequency_pstate(struct powernow_k8_data *data, unsigned int index)
1027 struct cpufreq_freqs freqs;
1029 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1031 /* get fid did for hardware pstate transition */
1032 pstate = index & HW_PSTATE_MASK;
1033 if (pstate > MAX_HW_PSTATE)
1035 fid = (index & HW_FID_INDEX_MASK) >> HW_FID_INDEX_SHIFT;
1036 did = (index & HW_DID_INDEX_MASK) >> HW_DID_INDEX_SHIFT;
1037 freqs.old = find_khz_freq_from_fiddid(data->currfid, data->currdid);
1038 freqs.new = find_khz_freq_from_fiddid(fid, did);
1040 for_each_cpu_mask(i, *(data->available_cores)) {
1042 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1045 res = transition_pstate(data, pstate);
1046 data->currfid = find_fid_from_pstate(pstate);
1047 data->currdid = find_did_from_pstate(pstate);
1048 freqs.new = find_khz_freq_from_fiddid(data->currfid, data->currdid);
1050 for_each_cpu_mask(i, *(data->available_cores)) {
1052 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1057 /* Driver entry point to switch to the target frequency */
1058 static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
1060 cpumask_t oldmask = CPU_MASK_ALL;
1061 struct powernow_k8_data *data = powernow_data[pol->cpu];
1064 unsigned int newstate;
1070 checkfid = data->currfid;
1071 checkvid = data->currvid;
1073 /* only run on specific CPU from here on */
1074 oldmask = current->cpus_allowed;
1075 set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
1077 if (smp_processor_id() != pol->cpu) {
1078 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1082 if (pending_bit_stuck()) {
1083 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1087 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1088 pol->cpu, targfreq, pol->min, pol->max, relation);
1090 if (query_current_values_with_pending_wait(data))
1093 if (cpu_family == CPU_HW_PSTATE)
1094 dprintk("targ: curr fid 0x%x, did 0x%x\n",
1095 data->currfid, data->currdid);
1097 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1098 data->currfid, data->currvid);
1100 if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
1101 printk(KERN_INFO PFX
1102 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1103 checkfid, data->currfid, checkvid, data->currvid);
1107 if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
1110 mutex_lock(&fidvid_mutex);
1112 powernow_k8_acpi_pst_values(data, newstate);
1114 if (cpu_family == CPU_HW_PSTATE)
1115 ret = transition_frequency_pstate(data, newstate);
1117 ret = transition_frequency_fidvid(data, newstate);
1119 printk(KERN_ERR PFX "transition frequency failed\n");
1121 mutex_unlock(&fidvid_mutex);
1124 mutex_unlock(&fidvid_mutex);
1126 if (cpu_family == CPU_HW_PSTATE)
1127 pol->cur = find_khz_freq_from_fiddid(data->currfid, data->currdid);
1129 pol->cur = find_khz_freq_from_fid(data->currfid);
1133 set_cpus_allowed(current, oldmask);
1137 /* Driver entry point to verify the policy and range of frequencies */
1138 static int powernowk8_verify(struct cpufreq_policy *pol)
1140 struct powernow_k8_data *data = powernow_data[pol->cpu];
1145 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1148 /* per CPU init entry point to the driver */
1149 static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
1151 struct powernow_k8_data *data;
1152 cpumask_t oldmask = CPU_MASK_ALL;
1155 if (!cpu_online(pol->cpu))
1158 if (!check_supported_cpu(pol->cpu))
1161 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
1163 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1167 data->cpu = pol->cpu;
1169 if (powernow_k8_cpu_init_acpi(data)) {
1171 * Use the PSB BIOS structure. This is only availabe on
1172 * an UP version, and is deprecated by AMD.
1174 if (num_online_cpus() != 1) {
1175 printk(KERN_ERR PFX "MP systems not supported by PSB BIOS structure\n");
1179 if (pol->cpu != 0) {
1180 printk(KERN_ERR PFX "No _PSS objects for CPU other than CPU0\n");
1184 rc = find_psb_table(data);
1191 /* only run on specific CPU from here on */
1192 oldmask = current->cpus_allowed;
1193 set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
1195 if (smp_processor_id() != pol->cpu) {
1196 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1200 if (pending_bit_stuck()) {
1201 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1205 if (query_current_values_with_pending_wait(data))
1208 if (cpu_family == CPU_OPTERON)
1211 /* run on any CPU again */
1212 set_cpus_allowed(current, oldmask);
1214 if (cpu_family == CPU_HW_PSTATE)
1215 pol->cpus = cpumask_of_cpu(pol->cpu);
1217 pol->cpus = per_cpu(cpu_core_map, pol->cpu);
1218 data->available_cores = &(pol->cpus);
1220 /* Take a crude guess here.
1221 * That guess was in microseconds, so multiply with 1000 */
1222 pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
1223 + (3 * (1 << data->irt) * 10)) * 1000;
1225 if (cpu_family == CPU_HW_PSTATE)
1226 pol->cur = find_khz_freq_from_fiddid(data->currfid, data->currdid);
1228 pol->cur = find_khz_freq_from_fid(data->currfid);
1229 dprintk("policy current frequency %d kHz\n", pol->cur);
1231 /* min/max the cpu is capable of */
1232 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1233 printk(KERN_ERR PFX "invalid powernow_table\n");
1234 powernow_k8_cpu_exit_acpi(data);
1235 kfree(data->powernow_table);
1240 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1242 if (cpu_family == CPU_HW_PSTATE)
1243 dprintk("cpu_init done, current fid 0x%x, did 0x%x\n",
1244 data->currfid, data->currdid);
1246 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1247 data->currfid, data->currvid);
1249 powernow_data[pol->cpu] = data;
1254 set_cpus_allowed(current, oldmask);
1255 powernow_k8_cpu_exit_acpi(data);
1261 static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
1263 struct powernow_k8_data *data = powernow_data[pol->cpu];
1268 powernow_k8_cpu_exit_acpi(data);
1270 cpufreq_frequency_table_put_attr(pol->cpu);
1272 kfree(data->powernow_table);
1278 static unsigned int powernowk8_get (unsigned int cpu)
1280 struct powernow_k8_data *data;
1281 cpumask_t oldmask = current->cpus_allowed;
1282 unsigned int khz = 0;
1284 data = powernow_data[first_cpu(per_cpu(cpu_core_map, cpu))];
1289 set_cpus_allowed(current, cpumask_of_cpu(cpu));
1290 if (smp_processor_id() != cpu) {
1291 printk(KERN_ERR PFX "limiting to CPU %d failed in powernowk8_get\n", cpu);
1292 set_cpus_allowed(current, oldmask);
1296 if (query_current_values_with_pending_wait(data))
1299 if (cpu_family == CPU_HW_PSTATE)
1300 khz = find_khz_freq_from_fiddid(data->currfid, data->currdid);
1302 khz = find_khz_freq_from_fid(data->currfid);
1306 set_cpus_allowed(current, oldmask);
1310 static struct freq_attr* powernow_k8_attr[] = {
1311 &cpufreq_freq_attr_scaling_available_freqs,
1315 static struct cpufreq_driver cpufreq_amd64_driver = {
1316 .verify = powernowk8_verify,
1317 .target = powernowk8_target,
1318 .init = powernowk8_cpu_init,
1319 .exit = __devexit_p(powernowk8_cpu_exit),
1320 .get = powernowk8_get,
1321 .name = "powernow-k8",
1322 .owner = THIS_MODULE,
1323 .attr = powernow_k8_attr,
1326 /* driver entry point for init */
1327 static int __cpuinit powernowk8_init(void)
1329 unsigned int i, supported_cpus = 0;
1331 for_each_online_cpu(i) {
1332 if (check_supported_cpu(i))
1336 if (supported_cpus == num_online_cpus()) {
1337 printk(KERN_INFO PFX "Found %d %s "
1338 "processors (%d cpu cores) (" VERSION ")\n",
1340 boot_cpu_data.x86_model_id, supported_cpus);
1341 return cpufreq_register_driver(&cpufreq_amd64_driver);
1347 /* driver entry point for term */
1348 static void __exit powernowk8_exit(void)
1352 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1355 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
1356 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1357 MODULE_LICENSE("GPL");
1359 late_initcall(powernowk8_init);
1360 module_exit(powernowk8_exit);