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 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 VERSION "version 2.20.00"
49 #include "powernow-k8.h"
51 /* serialize freq changes */
52 static DEFINE_MUTEX(fidvid_mutex);
54 static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
56 static int cpu_family = CPU_OPTERON;
59 static inline const struct cpumask *cpu_core_mask(int cpu)
65 /* Return a frequency in MHz, given an input fid */
66 static u32 find_freq_from_fid(u32 fid)
68 return 800 + (fid * 100);
71 /* Return a frequency in KHz, given an input fid */
72 static u32 find_khz_freq_from_fid(u32 fid)
74 return 1000 * find_freq_from_fid(fid);
77 static u32 find_khz_freq_from_pstate(struct cpufreq_frequency_table *data, u32 pstate)
79 return data[pstate].frequency;
82 /* Return the vco fid for an input fid
84 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
85 * only from corresponding high fids. This returns "high" fid corresponding to
88 static u32 convert_fid_to_vco_fid(u32 fid)
90 if (fid < HI_FID_TABLE_BOTTOM)
97 * Return 1 if the pending bit is set. Unless we just instructed the processor
98 * to transition to a new state, seeing this bit set is really bad news.
100 static int pending_bit_stuck(void)
104 if (cpu_family == CPU_HW_PSTATE)
107 rdmsr(MSR_FIDVID_STATUS, lo, hi);
108 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
112 * Update the global current fid / vid values from the status msr.
113 * Returns 1 on error.
115 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
120 if (cpu_family == CPU_HW_PSTATE) {
121 if (data->currpstate == HW_PSTATE_INVALID) {
122 /* read (initial) hw pstate if not yet set */
123 rdmsr(MSR_PSTATE_STATUS, lo, hi);
124 i = lo & HW_PSTATE_MASK;
127 * a workaround for family 11h erratum 311 might cause
128 * an "out-of-range Pstate if the core is in Pstate-0
130 if (i >= data->numps)
131 data->currpstate = HW_PSTATE_0;
133 data->currpstate = i;
139 dprintk("detected change pending stuck\n");
142 rdmsr(MSR_FIDVID_STATUS, lo, hi);
143 } while (lo & MSR_S_LO_CHANGE_PENDING);
145 data->currvid = hi & MSR_S_HI_CURRENT_VID;
146 data->currfid = lo & MSR_S_LO_CURRENT_FID;
151 /* the isochronous relief time */
152 static void count_off_irt(struct powernow_k8_data *data)
154 udelay((1 << data->irt) * 10);
158 /* the voltage stabilization time */
159 static void count_off_vst(struct powernow_k8_data *data)
161 udelay(data->vstable * VST_UNITS_20US);
165 /* need to init the control msr to a safe value (for each cpu) */
166 static void fidvid_msr_init(void)
171 rdmsr(MSR_FIDVID_STATUS, lo, hi);
172 vid = hi & MSR_S_HI_CURRENT_VID;
173 fid = lo & MSR_S_LO_CURRENT_FID;
174 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
175 hi = MSR_C_HI_STP_GNT_BENIGN;
176 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
177 wrmsr(MSR_FIDVID_CTL, lo, hi);
180 /* write the new fid value along with the other control fields to the msr */
181 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
184 u32 savevid = data->currvid;
187 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
188 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
192 lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
194 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
195 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
198 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
200 printk(KERN_ERR PFX "Hardware error - pending bit very stuck - no further pstate changes possible\n");
203 } while (query_current_values_with_pending_wait(data));
207 if (savevid != data->currvid) {
208 printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
209 savevid, data->currvid);
213 if (fid != data->currfid) {
214 printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
222 /* Write a new vid to the hardware */
223 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
226 u32 savefid = data->currfid;
229 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
230 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
234 lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
236 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
237 vid, lo, STOP_GRANT_5NS);
240 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
242 printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
245 } while (query_current_values_with_pending_wait(data));
247 if (savefid != data->currfid) {
248 printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
249 savefid, data->currfid);
253 if (vid != data->currvid) {
254 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
263 * Reduce the vid by the max of step or reqvid.
264 * Decreasing vid codes represent increasing voltages:
265 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
267 static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
269 if ((data->currvid - reqvid) > step)
270 reqvid = data->currvid - step;
272 if (write_new_vid(data, reqvid))
280 /* Change hardware pstate by single MSR write */
281 static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
283 wrmsr(MSR_PSTATE_CTRL, pstate, 0);
284 data->currpstate = pstate;
288 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
289 static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
291 if (core_voltage_pre_transition(data, reqvid))
294 if (core_frequency_transition(data, reqfid))
297 if (core_voltage_post_transition(data, reqvid))
300 if (query_current_values_with_pending_wait(data))
303 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
304 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
306 reqfid, reqvid, data->currfid, data->currvid);
310 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
311 smp_processor_id(), data->currfid, data->currvid);
316 /* Phase 1 - core voltage transition ... setup voltage */
317 static int core_voltage_pre_transition(struct powernow_k8_data *data, u32 reqvid)
319 u32 rvosteps = data->rvo;
320 u32 savefid = data->currfid;
323 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
325 data->currfid, data->currvid, reqvid, data->rvo);
327 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
328 maxvid = 0x1f & (maxvid >> 16);
329 dprintk("ph1 maxvid=0x%x\n", maxvid);
330 if (reqvid < maxvid) /* lower numbers are higher voltages */
333 while (data->currvid > reqvid) {
334 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
335 data->currvid, reqvid);
336 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
340 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
341 if (data->currvid == maxvid) {
344 dprintk("ph1: changing vid for rvo, req 0x%x\n",
346 if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
352 if (query_current_values_with_pending_wait(data))
355 if (savefid != data->currfid) {
356 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
360 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
361 data->currfid, data->currvid);
366 /* Phase 2 - core frequency transition */
367 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
369 u32 vcoreqfid, vcocurrfid, vcofiddiff, fid_interval, savevid = data->currvid;
371 if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
372 printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
373 reqfid, data->currfid);
377 if (data->currfid == reqfid) {
378 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
382 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
384 data->currfid, data->currvid, reqfid);
386 vcoreqfid = convert_fid_to_vco_fid(reqfid);
387 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
388 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
389 : vcoreqfid - vcocurrfid;
391 while (vcofiddiff > 2) {
392 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
394 if (reqfid > data->currfid) {
395 if (data->currfid > LO_FID_TABLE_TOP) {
396 if (write_new_fid(data, data->currfid + fid_interval)) {
401 (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
406 if (write_new_fid(data, data->currfid - fid_interval))
410 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
411 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
412 : vcoreqfid - vcocurrfid;
415 if (write_new_fid(data, reqfid))
418 if (query_current_values_with_pending_wait(data))
421 if (data->currfid != reqfid) {
423 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
424 data->currfid, reqfid);
428 if (savevid != data->currvid) {
429 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
430 savevid, data->currvid);
434 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
435 data->currfid, data->currvid);
440 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
441 static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid)
443 u32 savefid = data->currfid;
444 u32 savereqvid = reqvid;
446 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
448 data->currfid, data->currvid);
450 if (reqvid != data->currvid) {
451 if (write_new_vid(data, reqvid))
454 if (savefid != data->currfid) {
456 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
457 savefid, data->currfid);
461 if (data->currvid != reqvid) {
463 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
464 reqvid, data->currvid);
469 if (query_current_values_with_pending_wait(data))
472 if (savereqvid != data->currvid) {
473 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
477 if (savefid != data->currfid) {
478 dprintk("ph3 failed, currfid changed 0x%x\n",
483 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
484 data->currfid, data->currvid);
489 static int check_supported_cpu(unsigned int cpu)
492 u32 eax, ebx, ecx, edx;
495 oldmask = current->cpus_allowed;
496 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
498 if (smp_processor_id() != cpu) {
499 printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
503 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
506 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
507 if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
508 ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
511 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
512 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
513 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
514 printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
518 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
519 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
521 "No frequency change capabilities detected\n");
525 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
526 if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
527 printk(KERN_INFO PFX "Power state transitions not supported\n");
530 } else { /* must be a HW Pstate capable processor */
531 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
532 if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
533 cpu_family = CPU_HW_PSTATE;
541 set_cpus_allowed_ptr(current, &oldmask);
545 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
550 for (j = 0; j < data->numps; j++) {
551 if (pst[j].vid > LEAST_VID) {
552 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
556 if (pst[j].vid < data->rvo) { /* vid + rvo >= 0 */
557 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
561 if (pst[j].vid < maxvid + data->rvo) { /* vid + rvo >= maxvid */
562 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
566 if (pst[j].fid > MAX_FID) {
567 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
571 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
572 /* Only first fid is allowed to be in "low" range */
573 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
574 "0x%x\n", j, pst[j].fid);
577 if (pst[j].fid < lastfid)
578 lastfid = pst[j].fid;
581 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
584 if (lastfid > LO_FID_TABLE_TOP)
585 printk(KERN_INFO FW_BUG PFX "first fid not from lo freq table\n");
590 static void print_basics(struct powernow_k8_data *data)
593 for (j = 0; j < data->numps; j++) {
594 if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID) {
595 if (cpu_family == CPU_HW_PSTATE) {
596 printk(KERN_INFO PFX " %d : pstate %d (%d MHz)\n",
598 data->powernow_table[j].index,
599 data->powernow_table[j].frequency/1000);
601 printk(KERN_INFO PFX " %d : fid 0x%x (%d MHz), vid 0x%x\n",
603 data->powernow_table[j].index & 0xff,
604 data->powernow_table[j].frequency/1000,
605 data->powernow_table[j].index >> 8);
610 printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
613 static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
615 struct cpufreq_frequency_table *powernow_table;
618 if (data->batps) { /* use ACPI support to get full speed on mains power */
619 printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
620 data->numps = data->batps;
623 for ( j=1; j<data->numps; j++ ) {
624 if (pst[j-1].fid >= pst[j].fid) {
625 printk(KERN_ERR PFX "PST out of sequence\n");
630 if (data->numps < 2) {
631 printk(KERN_ERR PFX "no p states to transition\n");
635 if (check_pst_table(data, pst, maxvid))
638 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
639 * (data->numps + 1)), GFP_KERNEL);
640 if (!powernow_table) {
641 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
645 for (j = 0; j < data->numps; j++) {
646 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
647 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
648 powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
650 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
651 powernow_table[data->numps].index = 0;
653 if (query_current_values_with_pending_wait(data)) {
654 kfree(powernow_table);
658 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
659 data->powernow_table = powernow_table;
660 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
663 for (j = 0; j < data->numps; j++)
664 if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
667 dprintk("currfid/vid do not match PST, ignoring\n");
671 /* Find and validate the PSB/PST table in BIOS. */
672 static int find_psb_table(struct powernow_k8_data *data)
681 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
682 /* Scan BIOS looking for the signature. */
683 /* It can not be at ffff0 - it is too big. */
685 psb = phys_to_virt(i);
686 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
689 dprintk("found PSB header at 0x%p\n", psb);
691 dprintk("table vers: 0x%x\n", psb->tableversion);
692 if (psb->tableversion != PSB_VERSION_1_4) {
693 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
697 dprintk("flags: 0x%x\n", psb->flags1);
699 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
703 data->vstable = psb->vstable;
704 dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
706 dprintk("flags2: 0x%x\n", psb->flags2);
707 data->rvo = psb->flags2 & 3;
708 data->irt = ((psb->flags2) >> 2) & 3;
709 mvs = ((psb->flags2) >> 4) & 3;
710 data->vidmvs = 1 << mvs;
711 data->batps = ((psb->flags2) >> 6) & 3;
713 dprintk("ramp voltage offset: %d\n", data->rvo);
714 dprintk("isochronous relief time: %d\n", data->irt);
715 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
717 dprintk("numpst: 0x%x\n", psb->num_tables);
718 cpst = psb->num_tables;
719 if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
720 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
721 if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
726 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
730 data->plllock = psb->plllocktime;
731 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
732 dprintk("maxfid: 0x%x\n", psb->maxfid);
733 dprintk("maxvid: 0x%x\n", psb->maxvid);
734 maxvid = psb->maxvid;
736 data->numps = psb->numps;
737 dprintk("numpstates: 0x%x\n", data->numps);
738 return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
741 * If you see this message, complain to BIOS manufacturer. If
742 * he tells you "we do not support Linux" or some similar
743 * nonsense, remember that Windows 2000 uses the same legacy
744 * mechanism that the old Linux PSB driver uses. Tell them it
745 * is broken with Windows 2000.
747 * The reference to the AMD documentation is chapter 9 in the
748 * BIOS and Kernel Developer's Guide, which is available on
751 printk(KERN_ERR PFX "BIOS error - no PSB or ACPI _PSS objects\n");
755 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
756 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
758 if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
761 data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
762 data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
763 data->exttype = (data->acpi_data.states[index].control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
764 data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
765 data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
766 data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
769 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
771 struct cpufreq_frequency_table *powernow_table;
772 int ret_val = -ENODEV;
774 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
775 dprintk("register performance failed: bad ACPI data\n");
779 /* verify the data contained in the ACPI structures */
780 if (data->acpi_data.state_count <= 1) {
781 dprintk("No ACPI P-States\n");
785 if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
786 (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
787 dprintk("Invalid control/status registers (%x - %x)\n",
788 data->acpi_data.control_register.space_id,
789 data->acpi_data.status_register.space_id);
793 /* fill in data->powernow_table */
794 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
795 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
796 if (!powernow_table) {
797 dprintk("powernow_table memory alloc failure\n");
801 if (cpu_family == CPU_HW_PSTATE)
802 ret_val = fill_powernow_table_pstate(data, powernow_table);
804 ret_val = fill_powernow_table_fidvid(data, powernow_table);
808 powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
809 powernow_table[data->acpi_data.state_count].index = 0;
810 data->powernow_table = powernow_table;
813 data->numps = data->acpi_data.state_count;
814 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
816 powernow_k8_acpi_pst_values(data, 0);
818 /* notify BIOS that we exist */
819 acpi_processor_notify_smm(THIS_MODULE);
821 if (!alloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
823 "unable to alloc powernow_k8_data cpumask\n");
831 kfree(powernow_table);
834 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
836 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
837 data->acpi_data.state_count = 0;
842 static int fill_powernow_table_pstate(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
846 rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
847 data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
849 for (i = 0; i < data->acpi_data.state_count; i++) {
852 index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
853 if (index > data->max_hw_pstate) {
854 printk(KERN_ERR PFX "invalid pstate %d - bad value %d.\n", i, index);
855 printk(KERN_ERR PFX "Please report to BIOS manufacturer\n");
856 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
859 rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
860 if (!(hi & HW_PSTATE_VALID_MASK)) {
861 dprintk("invalid pstate %d, ignoring\n", index);
862 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
866 powernow_table[i].index = index;
868 powernow_table[i].frequency = data->acpi_data.states[i].core_frequency * 1000;
873 static int fill_powernow_table_fidvid(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
877 for (i = 0; i < data->acpi_data.state_count; i++) {
882 fid = data->acpi_data.states[i].status & EXT_FID_MASK;
883 vid = (data->acpi_data.states[i].status >> VID_SHIFT) & EXT_VID_MASK;
885 fid = data->acpi_data.states[i].control & FID_MASK;
886 vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
889 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
891 powernow_table[i].index = fid; /* lower 8 bits */
892 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
893 powernow_table[i].frequency = find_khz_freq_from_fid(fid);
895 /* verify frequency is OK */
896 if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
897 (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
898 dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
899 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
903 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
904 if (vid == VID_OFF) {
905 dprintk("invalid vid %u, ignoring\n", vid);
906 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
910 /* verify only 1 entry from the lo frequency table */
911 if (fid < HI_FID_TABLE_BOTTOM) {
913 /* if both entries are the same, ignore this one ... */
914 if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
915 (powernow_table[i].index != powernow_table[cntlofreq].index)) {
916 printk(KERN_ERR PFX "Too many lo freq table entries\n");
920 dprintk("double low frequency table entry, ignoring it.\n");
921 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
927 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
928 printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
929 powernow_table[i].frequency,
930 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
931 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
938 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
940 if (data->acpi_data.state_count)
941 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
942 free_cpumask_var(data->acpi_data.shared_cpu_map);
945 static int get_transition_latency(struct powernow_k8_data *data)
949 for (i = 0; i < data->acpi_data.state_count; i++) {
950 int cur_latency = data->acpi_data.states[i].transition_latency
951 + data->acpi_data.states[i].bus_master_latency;
952 if (cur_latency > max_latency)
953 max_latency = cur_latency;
955 /* value in usecs, needs to be in nanoseconds */
956 return 1000 * max_latency;
960 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
961 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
962 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
963 static int get_transition_latency(struct powernow_k8_data *data) { return 0; }
964 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
966 /* Take a frequency, and issue the fid/vid transition command */
967 static int transition_frequency_fidvid(struct powernow_k8_data *data, unsigned int index)
972 struct cpufreq_freqs freqs;
974 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
976 /* fid/vid correctness check for k8 */
977 /* fid are the lower 8 bits of the index we stored into
978 * the cpufreq frequency table in find_psb_table, vid
979 * are the upper 8 bits.
981 fid = data->powernow_table[index].index & 0xFF;
982 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
984 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
986 if (query_current_values_with_pending_wait(data))
989 if ((data->currvid == vid) && (data->currfid == fid)) {
990 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
995 if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
997 "ignoring illegal change in lo freq table-%x to 0x%x\n",
1002 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
1003 smp_processor_id(), fid, vid);
1004 freqs.old = find_khz_freq_from_fid(data->currfid);
1005 freqs.new = find_khz_freq_from_fid(fid);
1007 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1009 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1012 res = transition_fid_vid(data, fid, vid);
1013 freqs.new = find_khz_freq_from_fid(data->currfid);
1015 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1017 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1022 /* Take a frequency, and issue the hardware pstate transition command */
1023 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 MSR index for hardware pstate transition */
1032 pstate = index & HW_PSTATE_MASK;
1033 if (pstate > data->max_hw_pstate)
1035 freqs.old = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
1036 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1038 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1040 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1043 res = transition_pstate(data, pstate);
1044 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1046 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1048 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1053 /* Driver entry point to switch to the target frequency */
1054 static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
1057 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1060 unsigned int newstate;
1066 checkfid = data->currfid;
1067 checkvid = data->currvid;
1069 /* only run on specific CPU from here on */
1070 oldmask = current->cpus_allowed;
1071 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1073 if (smp_processor_id() != pol->cpu) {
1074 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1078 if (pending_bit_stuck()) {
1079 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1083 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1084 pol->cpu, targfreq, pol->min, pol->max, relation);
1086 if (query_current_values_with_pending_wait(data))
1089 if (cpu_family != CPU_HW_PSTATE) {
1090 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1091 data->currfid, data->currvid);
1093 if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
1094 printk(KERN_INFO PFX
1095 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1096 checkfid, data->currfid, checkvid, data->currvid);
1100 if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
1103 mutex_lock(&fidvid_mutex);
1105 powernow_k8_acpi_pst_values(data, newstate);
1107 if (cpu_family == CPU_HW_PSTATE)
1108 ret = transition_frequency_pstate(data, newstate);
1110 ret = transition_frequency_fidvid(data, newstate);
1112 printk(KERN_ERR PFX "transition frequency failed\n");
1114 mutex_unlock(&fidvid_mutex);
1117 mutex_unlock(&fidvid_mutex);
1119 if (cpu_family == CPU_HW_PSTATE)
1120 pol->cur = find_khz_freq_from_pstate(data->powernow_table, newstate);
1122 pol->cur = find_khz_freq_from_fid(data->currfid);
1126 set_cpus_allowed_ptr(current, &oldmask);
1130 /* Driver entry point to verify the policy and range of frequencies */
1131 static int powernowk8_verify(struct cpufreq_policy *pol)
1133 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1138 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1141 /* per CPU init entry point to the driver */
1142 static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
1144 struct powernow_k8_data *data;
1148 if (!cpu_online(pol->cpu))
1151 if (!check_supported_cpu(pol->cpu))
1154 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
1156 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1160 data->cpu = pol->cpu;
1161 data->currpstate = HW_PSTATE_INVALID;
1163 if (powernow_k8_cpu_init_acpi(data)) {
1165 * Use the PSB BIOS structure. This is only availabe on
1166 * an UP version, and is deprecated by AMD.
1168 if (num_online_cpus() != 1) {
1169 #ifndef CONFIG_ACPI_PROCESSOR
1170 printk(KERN_ERR PFX "ACPI Processor support is required "
1171 "for SMP systems but is absent. Please load the "
1172 "ACPI Processor module before starting this "
1175 printk(KERN_ERR FW_BUG PFX "Your BIOS does not provide"
1176 " ACPI _PSS objects in a way that Linux "
1177 "understands. Please report this to the Linux "
1178 "ACPI maintainers and complain to your BIOS "
1184 if (pol->cpu != 0) {
1185 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1186 "CPU other than CPU0. Complain to your BIOS "
1191 rc = find_psb_table(data);
1196 /* Take a crude guess here.
1197 * That guess was in microseconds, so multiply with 1000 */
1198 pol->cpuinfo.transition_latency = (
1199 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
1200 ((1 << data->irt) * 30)) * 1000;
1201 } else /* ACPI _PSS objects available */
1202 pol->cpuinfo.transition_latency = get_transition_latency(data);
1204 /* only run on specific CPU from here on */
1205 oldmask = current->cpus_allowed;
1206 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1208 if (smp_processor_id() != pol->cpu) {
1209 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1213 if (pending_bit_stuck()) {
1214 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1218 if (query_current_values_with_pending_wait(data))
1221 if (cpu_family == CPU_OPTERON)
1224 /* run on any CPU again */
1225 set_cpus_allowed_ptr(current, &oldmask);
1227 if (cpu_family == CPU_HW_PSTATE)
1228 cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
1230 cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
1231 data->available_cores = pol->cpus;
1233 if (cpu_family == CPU_HW_PSTATE)
1234 pol->cur = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
1236 pol->cur = find_khz_freq_from_fid(data->currfid);
1237 dprintk("policy current frequency %d kHz\n", pol->cur);
1239 /* min/max the cpu is capable of */
1240 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1241 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
1242 powernow_k8_cpu_exit_acpi(data);
1243 kfree(data->powernow_table);
1248 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1250 if (cpu_family == CPU_HW_PSTATE)
1251 dprintk("cpu_init done, current pstate 0x%x\n", data->currpstate);
1253 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1254 data->currfid, data->currvid);
1256 per_cpu(powernow_data, pol->cpu) = data;
1261 set_cpus_allowed_ptr(current, &oldmask);
1262 powernow_k8_cpu_exit_acpi(data);
1268 static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
1270 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1275 powernow_k8_cpu_exit_acpi(data);
1277 cpufreq_frequency_table_put_attr(pol->cpu);
1279 kfree(data->powernow_table);
1285 static unsigned int powernowk8_get (unsigned int cpu)
1287 struct powernow_k8_data *data;
1288 cpumask_t oldmask = current->cpus_allowed;
1289 unsigned int khz = 0;
1292 first = cpumask_first(cpu_core_mask(cpu));
1293 data = per_cpu(powernow_data, first);
1298 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
1299 if (smp_processor_id() != cpu) {
1301 "limiting to CPU %d failed in powernowk8_get\n", cpu);
1302 set_cpus_allowed_ptr(current, &oldmask);
1306 if (query_current_values_with_pending_wait(data))
1309 if (cpu_family == CPU_HW_PSTATE)
1310 khz = find_khz_freq_from_pstate(data->powernow_table,
1313 khz = find_khz_freq_from_fid(data->currfid);
1317 set_cpus_allowed_ptr(current, &oldmask);
1321 static struct freq_attr* powernow_k8_attr[] = {
1322 &cpufreq_freq_attr_scaling_available_freqs,
1326 static struct cpufreq_driver cpufreq_amd64_driver = {
1327 .verify = powernowk8_verify,
1328 .target = powernowk8_target,
1329 .init = powernowk8_cpu_init,
1330 .exit = __devexit_p(powernowk8_cpu_exit),
1331 .get = powernowk8_get,
1332 .name = "powernow-k8",
1333 .owner = THIS_MODULE,
1334 .attr = powernow_k8_attr,
1337 /* driver entry point for init */
1338 static int __cpuinit powernowk8_init(void)
1340 unsigned int i, supported_cpus = 0;
1342 for_each_online_cpu(i) {
1343 if (check_supported_cpu(i))
1347 if (supported_cpus == num_online_cpus()) {
1348 printk(KERN_INFO PFX "Found %d %s "
1349 "processors (%d cpu cores) (" VERSION ")\n",
1351 boot_cpu_data.x86_model_id, supported_cpus);
1352 return cpufreq_register_driver(&cpufreq_amd64_driver);
1358 /* driver entry point for term */
1359 static void __exit powernowk8_exit(void)
1363 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1366 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
1367 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1368 MODULE_LICENSE("GPL");
1370 late_initcall(powernowk8_init);
1371 module_exit(powernowk8_exit);