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() */
37 #include <linux/delay.h>
41 #include <linux/acpi.h>
42 #include <linux/mutex.h>
43 #include <acpi/processor.h>
45 #define PFX "powernow-k8: "
46 #define VERSION "version 2.20.00"
47 #include "powernow-k8.h"
49 /* serialize freq changes */
50 static DEFINE_MUTEX(fidvid_mutex);
52 static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
54 static int cpu_family = CPU_OPTERON;
57 DEFINE_PER_CPU(cpumask_t, cpu_core_map);
60 /* Return a frequency in MHz, given an input fid */
61 static u32 find_freq_from_fid(u32 fid)
63 return 800 + (fid * 100);
66 /* Return a frequency in KHz, given an input fid */
67 static u32 find_khz_freq_from_fid(u32 fid)
69 return 1000 * find_freq_from_fid(fid);
72 static u32 find_khz_freq_from_pstate(struct cpufreq_frequency_table *data,
75 return data[pstate].frequency;
78 /* Return the vco fid for an input fid
80 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
81 * only from corresponding high fids. This returns "high" fid corresponding to
84 static u32 convert_fid_to_vco_fid(u32 fid)
86 if (fid < HI_FID_TABLE_BOTTOM)
93 * Return 1 if the pending bit is set. Unless we just instructed the processor
94 * to transition to a new state, seeing this bit set is really bad news.
96 static int pending_bit_stuck(void)
100 if (cpu_family == CPU_HW_PSTATE)
103 rdmsr(MSR_FIDVID_STATUS, lo, hi);
104 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
108 * Update the global current fid / vid values from the status msr.
109 * Returns 1 on error.
111 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
116 if (cpu_family == CPU_HW_PSTATE) {
117 if (data->currpstate == HW_PSTATE_INVALID) {
118 /* read (initial) hw pstate if not yet set */
119 rdmsr(MSR_PSTATE_STATUS, lo, hi);
120 i = lo & HW_PSTATE_MASK;
123 * a workaround for family 11h erratum 311 might cause
124 * an "out-of-range Pstate if the core is in Pstate-0
126 if (i >= data->numps)
127 data->currpstate = HW_PSTATE_0;
129 data->currpstate = i;
135 dprintk("detected change pending stuck\n");
138 rdmsr(MSR_FIDVID_STATUS, lo, hi);
139 } while (lo & MSR_S_LO_CHANGE_PENDING);
141 data->currvid = hi & MSR_S_HI_CURRENT_VID;
142 data->currfid = lo & MSR_S_LO_CURRENT_FID;
147 /* the isochronous relief time */
148 static void count_off_irt(struct powernow_k8_data *data)
150 udelay((1 << data->irt) * 10);
154 /* the voltage stabilization time */
155 static void count_off_vst(struct powernow_k8_data *data)
157 udelay(data->vstable * VST_UNITS_20US);
161 /* need to init the control msr to a safe value (for each cpu) */
162 static void fidvid_msr_init(void)
167 rdmsr(MSR_FIDVID_STATUS, lo, hi);
168 vid = hi & MSR_S_HI_CURRENT_VID;
169 fid = lo & MSR_S_LO_CURRENT_FID;
170 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
171 hi = MSR_C_HI_STP_GNT_BENIGN;
172 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
173 wrmsr(MSR_FIDVID_CTL, lo, hi);
176 /* write the new fid value along with the other control fields to the msr */
177 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
180 u32 savevid = data->currvid;
183 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
184 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
189 lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
190 lo |= MSR_C_LO_INIT_FID_VID;
192 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
193 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
196 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
199 "Hardware error - pending bit very stuck - "
200 "no further pstate changes possible\n");
203 } while (query_current_values_with_pending_wait(data));
207 if (savevid != data->currvid) {
209 "vid change on fid trans, old 0x%x, new 0x%x\n",
210 savevid, data->currvid);
214 if (fid != data->currfid) {
216 "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
224 /* Write a new vid to the hardware */
225 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
228 u32 savefid = data->currfid;
231 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
232 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
237 lo |= (vid << MSR_C_LO_VID_SHIFT);
238 lo |= MSR_C_LO_INIT_FID_VID;
240 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
241 vid, lo, STOP_GRANT_5NS);
244 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
246 printk(KERN_ERR PFX "internal error - pending bit "
247 "very stuck - no further pstate "
248 "changes possible\n");
251 } while (query_current_values_with_pending_wait(data));
253 if (savefid != data->currfid) {
254 printk(KERN_ERR PFX "fid changed on vid trans, old "
256 savefid, data->currfid);
260 if (vid != data->currvid) {
261 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
271 * Reduce the vid by the max of step or reqvid.
272 * Decreasing vid codes represent increasing voltages:
273 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
275 static int decrease_vid_code_by_step(struct powernow_k8_data *data,
276 u32 reqvid, u32 step)
278 if ((data->currvid - reqvid) > step)
279 reqvid = data->currvid - step;
281 if (write_new_vid(data, reqvid))
289 /* Change hardware pstate by single MSR write */
290 static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
292 wrmsr(MSR_PSTATE_CTRL, pstate, 0);
293 data->currpstate = pstate;
297 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
298 static int transition_fid_vid(struct powernow_k8_data *data,
299 u32 reqfid, u32 reqvid)
301 if (core_voltage_pre_transition(data, reqvid))
304 if (core_frequency_transition(data, reqfid))
307 if (core_voltage_post_transition(data, reqvid))
310 if (query_current_values_with_pending_wait(data))
313 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
314 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
317 reqfid, reqvid, data->currfid, data->currvid);
321 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
322 smp_processor_id(), data->currfid, data->currvid);
327 /* Phase 1 - core voltage transition ... setup voltage */
328 static int core_voltage_pre_transition(struct powernow_k8_data *data,
331 u32 rvosteps = data->rvo;
332 u32 savefid = data->currfid;
335 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
336 "reqvid 0x%x, rvo 0x%x\n",
338 data->currfid, data->currvid, reqvid, data->rvo);
340 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
341 maxvid = 0x1f & (maxvid >> 16);
342 dprintk("ph1 maxvid=0x%x\n", maxvid);
343 if (reqvid < maxvid) /* lower numbers are higher voltages */
346 while (data->currvid > reqvid) {
347 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
348 data->currvid, reqvid);
349 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
353 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
354 if (data->currvid == maxvid) {
357 dprintk("ph1: changing vid for rvo, req 0x%x\n",
359 if (decrease_vid_code_by_step(data, data->currvid-1, 1))
365 if (query_current_values_with_pending_wait(data))
368 if (savefid != data->currfid) {
369 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
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;
384 u32 fid_interval, savevid = data->currvid;
386 if ((reqfid < HI_FID_TABLE_BOTTOM) &&
387 (data->currfid < HI_FID_TABLE_BOTTOM)) {
388 printk(KERN_ERR PFX "ph2: illegal lo-lo transition "
389 "0x%x 0x%x\n", reqfid, data->currfid);
393 if (data->currfid == reqfid) {
394 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
399 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
402 data->currfid, data->currvid, reqfid);
404 vcoreqfid = convert_fid_to_vco_fid(reqfid);
405 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
406 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
407 : vcoreqfid - vcocurrfid;
409 while (vcofiddiff > 2) {
410 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
412 if (reqfid > data->currfid) {
413 if (data->currfid > LO_FID_TABLE_TOP) {
414 if (write_new_fid(data,
415 data->currfid + fid_interval))
420 2 + convert_fid_to_vco_fid(data->currfid)))
424 if (write_new_fid(data, data->currfid - fid_interval))
428 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
429 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
430 : vcoreqfid - vcocurrfid;
433 if (write_new_fid(data, reqfid))
436 if (query_current_values_with_pending_wait(data))
439 if (data->currfid != reqfid) {
441 "ph2: mismatch, failed fid transition, "
442 "curr 0x%x, req 0x%x\n",
443 data->currfid, reqfid);
447 if (savevid != data->currvid) {
448 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
449 savevid, data->currvid);
453 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
454 data->currfid, data->currvid);
459 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
460 static int core_voltage_post_transition(struct powernow_k8_data *data,
463 u32 savefid = data->currfid;
464 u32 savereqvid = reqvid;
466 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
468 data->currfid, data->currvid);
470 if (reqvid != data->currvid) {
471 if (write_new_vid(data, reqvid))
474 if (savefid != data->currfid) {
476 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
477 savefid, data->currfid);
481 if (data->currvid != reqvid) {
483 "ph3: failed vid transition\n, "
484 "req 0x%x, curr 0x%x",
485 reqvid, data->currvid);
490 if (query_current_values_with_pending_wait(data))
493 if (savereqvid != data->currvid) {
494 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
498 if (savefid != data->currfid) {
499 dprintk("ph3 failed, currfid changed 0x%x\n",
504 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
505 data->currfid, data->currvid);
510 static int check_supported_cpu(unsigned int cpu)
513 u32 eax, ebx, ecx, edx;
516 oldmask = current->cpus_allowed;
517 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
519 if (smp_processor_id() != cpu) {
520 printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
524 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
527 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
528 if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
529 ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
532 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
533 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
534 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
536 "Processor cpuid %x not supported\n", eax);
540 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
541 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
543 "No frequency change capabilities detected\n");
547 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
548 if ((edx & P_STATE_TRANSITION_CAPABLE)
549 != P_STATE_TRANSITION_CAPABLE) {
551 "Power state transitions not supported\n");
554 } else { /* must be a HW Pstate capable processor */
555 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
556 if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
557 cpu_family = CPU_HW_PSTATE;
565 set_cpus_allowed_ptr(current, &oldmask);
569 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
575 for (j = 0; j < data->numps; j++) {
576 if (pst[j].vid > LEAST_VID) {
577 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
581 if (pst[j].vid < data->rvo) {
583 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
587 if (pst[j].vid < maxvid + data->rvo) {
588 /* vid + rvo >= maxvid */
589 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
593 if (pst[j].fid > MAX_FID) {
594 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
598 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
599 /* Only first fid is allowed to be in "low" range */
600 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
601 "0x%x\n", j, pst[j].fid);
604 if (pst[j].fid < lastfid)
605 lastfid = pst[j].fid;
608 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
611 if (lastfid > LO_FID_TABLE_TOP)
612 printk(KERN_INFO FW_BUG PFX
613 "first fid not from lo freq table\n");
618 static void invalidate_entry(struct powernow_k8_data *data, unsigned int entry)
620 data->powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
623 static void print_basics(struct powernow_k8_data *data)
626 for (j = 0; j < data->numps; j++) {
627 if (data->powernow_table[j].frequency !=
628 CPUFREQ_ENTRY_INVALID) {
629 if (cpu_family == CPU_HW_PSTATE) {
631 " %d : pstate %d (%d MHz)\n", j,
632 data->powernow_table[j].index,
633 data->powernow_table[j].frequency/1000);
636 " %d : fid 0x%x (%d MHz), vid 0x%x\n",
638 data->powernow_table[j].index & 0xff,
639 data->powernow_table[j].frequency/1000,
640 data->powernow_table[j].index >> 8);
645 printk(KERN_INFO PFX "Only %d pstates on battery\n",
649 static int fill_powernow_table(struct powernow_k8_data *data,
650 struct pst_s *pst, u8 maxvid)
652 struct cpufreq_frequency_table *powernow_table;
656 /* use ACPI support to get full speed on mains power */
657 printk(KERN_WARNING PFX
658 "Only %d pstates usable (use ACPI driver for full "
659 "range\n", data->batps);
660 data->numps = data->batps;
663 for (j = 1; j < data->numps; j++) {
664 if (pst[j-1].fid >= pst[j].fid) {
665 printk(KERN_ERR PFX "PST out of sequence\n");
670 if (data->numps < 2) {
671 printk(KERN_ERR PFX "no p states to transition\n");
675 if (check_pst_table(data, pst, maxvid))
678 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
679 * (data->numps + 1)), GFP_KERNEL);
680 if (!powernow_table) {
681 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
685 for (j = 0; j < data->numps; j++) {
687 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
688 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
689 freq = find_khz_freq_from_fid(pst[j].fid);
690 powernow_table[j].frequency = freq;
692 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
693 powernow_table[data->numps].index = 0;
695 if (query_current_values_with_pending_wait(data)) {
696 kfree(powernow_table);
700 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
701 data->powernow_table = powernow_table;
702 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
705 for (j = 0; j < data->numps; j++)
706 if ((pst[j].fid == data->currfid) &&
707 (pst[j].vid == data->currvid))
710 dprintk("currfid/vid do not match PST, ignoring\n");
714 /* Find and validate the PSB/PST table in BIOS. */
715 static int find_psb_table(struct powernow_k8_data *data)
724 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
725 /* Scan BIOS looking for the signature. */
726 /* It can not be at ffff0 - it is too big. */
728 psb = phys_to_virt(i);
729 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
732 dprintk("found PSB header at 0x%p\n", psb);
734 dprintk("table vers: 0x%x\n", psb->tableversion);
735 if (psb->tableversion != PSB_VERSION_1_4) {
736 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
740 dprintk("flags: 0x%x\n", psb->flags1);
742 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
746 data->vstable = psb->vstable;
747 dprintk("voltage stabilization time: %d(*20us)\n",
750 dprintk("flags2: 0x%x\n", psb->flags2);
751 data->rvo = psb->flags2 & 3;
752 data->irt = ((psb->flags2) >> 2) & 3;
753 mvs = ((psb->flags2) >> 4) & 3;
754 data->vidmvs = 1 << mvs;
755 data->batps = ((psb->flags2) >> 6) & 3;
757 dprintk("ramp voltage offset: %d\n", data->rvo);
758 dprintk("isochronous relief time: %d\n", data->irt);
759 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
761 dprintk("numpst: 0x%x\n", psb->num_tables);
762 cpst = psb->num_tables;
763 if ((psb->cpuid == 0x00000fc0) ||
764 (psb->cpuid == 0x00000fe0)) {
765 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
766 if ((thiscpuid == 0x00000fc0) ||
767 (thiscpuid == 0x00000fe0))
771 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
775 data->plllock = psb->plllocktime;
776 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
777 dprintk("maxfid: 0x%x\n", psb->maxfid);
778 dprintk("maxvid: 0x%x\n", psb->maxvid);
779 maxvid = psb->maxvid;
781 data->numps = psb->numps;
782 dprintk("numpstates: 0x%x\n", data->numps);
783 return fill_powernow_table(data,
784 (struct pst_s *)(psb+1), maxvid);
787 * If you see this message, complain to BIOS manufacturer. If
788 * he tells you "we do not support Linux" or some similar
789 * nonsense, remember that Windows 2000 uses the same legacy
790 * mechanism that the old Linux PSB driver uses. Tell them it
791 * is broken with Windows 2000.
793 * The reference to the AMD documentation is chapter 9 in the
794 * BIOS and Kernel Developer's Guide, which is available on
797 printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
801 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
804 acpi_integer control;
806 if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
809 control = data->acpi_data.states[index].control; data->irt = (control
810 >> IRT_SHIFT) & IRT_MASK; data->rvo = (control >>
811 RVO_SHIFT) & RVO_MASK; data->exttype = (control
812 >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
813 data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK; data->vidmvs = 1
814 << ((control >> MVS_SHIFT) & MVS_MASK); data->vstable =
815 (control >> VST_SHIFT) & VST_MASK; }
817 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
819 struct cpufreq_frequency_table *powernow_table;
820 int ret_val = -ENODEV;
821 acpi_integer space_id;
823 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
824 dprintk("register performance failed: bad ACPI data\n");
828 /* verify the data contained in the ACPI structures */
829 if (data->acpi_data.state_count <= 1) {
830 dprintk("No ACPI P-States\n");
834 space_id = data->acpi_data.control_register.space_id;
835 if ((space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
836 (space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
837 dprintk("Invalid control/status registers (%x - %x)\n",
838 data->acpi_data.control_register.space_id,
843 /* fill in data->powernow_table */
844 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
845 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
846 if (!powernow_table) {
847 dprintk("powernow_table memory alloc failure\n");
851 if (cpu_family == CPU_HW_PSTATE)
852 ret_val = fill_powernow_table_pstate(data, powernow_table);
854 ret_val = fill_powernow_table_fidvid(data, powernow_table);
858 powernow_table[data->acpi_data.state_count].frequency =
860 powernow_table[data->acpi_data.state_count].index = 0;
861 data->powernow_table = powernow_table;
864 data->numps = data->acpi_data.state_count;
865 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
867 powernow_k8_acpi_pst_values(data, 0);
869 /* notify BIOS that we exist */
870 acpi_processor_notify_smm(THIS_MODULE);
872 if (!alloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
874 "unable to alloc powernow_k8_data cpumask\n");
882 kfree(powernow_table);
885 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
887 /* data->acpi_data.state_count informs us at ->exit()
888 * whether ACPI was used */
889 data->acpi_data.state_count = 0;
894 static int fill_powernow_table_pstate(struct powernow_k8_data *data,
895 struct cpufreq_frequency_table *powernow_table)
899 rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
900 data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
902 for (i = 0; i < data->acpi_data.state_count; i++) {
905 index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
906 if (index > data->max_hw_pstate) {
907 printk(KERN_ERR PFX "invalid pstate %d - "
908 "bad value %d.\n", i, index);
909 printk(KERN_ERR PFX "Please report to BIOS "
911 invalidate_entry(data, i);
914 rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
915 if (!(hi & HW_PSTATE_VALID_MASK)) {
916 dprintk("invalid pstate %d, ignoring\n", index);
917 invalidate_entry(data, i);
921 powernow_table[i].index = index;
923 powernow_table[i].frequency =
924 data->acpi_data.states[i].core_frequency * 1000;
929 static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
930 struct cpufreq_frequency_table *powernow_table)
935 for (i = 0; i < data->acpi_data.state_count; i++) {
939 acpi_integer status, control;
942 status = data->acpi_data.states[i].status;
943 fid = status & EXT_FID_MASK;
944 vid = (status >> VID_SHIFT) & EXT_VID_MASK;
946 control = data->acpi_data.states[i].control;
947 fid = control & FID_MASK;
948 vid = (control >> VID_SHIFT) & VID_MASK;
951 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
953 index = fid | (vid<<8);
954 powernow_table[i].index = index;
956 freq = find_khz_freq_from_fid(fid);
957 powernow_table[i].frequency = freq;
959 /* verify frequency is OK */
960 if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
961 dprintk("invalid freq %u kHz, ignoring\n", freq);
962 invalidate_entry(data, i);
966 /* verify voltage is OK -
967 * BIOSs are using "off" to indicate invalid */
968 if (vid == VID_OFF) {
969 dprintk("invalid vid %u, ignoring\n", vid);
970 invalidate_entry(data, i);
974 /* verify only 1 entry from the lo frequency table */
975 if (fid < HI_FID_TABLE_BOTTOM) {
977 /* if both entries are the same,
978 * ignore this one ... */
979 if ((freq != powernow_table[cntlofreq].frequency) ||
980 (index != powernow_table[cntlofreq].index)) {
982 "Too many lo freq table "
987 dprintk("double low frequency table entry, "
989 invalidate_entry(data, i);
995 if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
996 printk(KERN_INFO PFX "invalid freq entries "
997 "%u kHz vs. %u kHz\n", freq,
999 (data->acpi_data.states[i].core_frequency
1001 invalidate_entry(data, i);
1008 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
1010 if (data->acpi_data.state_count)
1011 acpi_processor_unregister_performance(&data->acpi_data,
1013 free_cpumask_var(data->acpi_data.shared_cpu_map);
1016 static int get_transition_latency(struct powernow_k8_data *data)
1018 int max_latency = 0;
1020 for (i = 0; i < data->acpi_data.state_count; i++) {
1021 int cur_latency = data->acpi_data.states[i].transition_latency
1022 + data->acpi_data.states[i].bus_master_latency;
1023 if (cur_latency > max_latency)
1024 max_latency = cur_latency;
1026 /* value in usecs, needs to be in nanoseconds */
1027 return 1000 * max_latency;
1030 /* Take a frequency, and issue the fid/vid transition command */
1031 static int transition_frequency_fidvid(struct powernow_k8_data *data,
1037 struct cpufreq_freqs freqs;
1039 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1041 /* fid/vid correctness check for k8 */
1042 /* fid are the lower 8 bits of the index we stored into
1043 * the cpufreq frequency table in find_psb_table, vid
1044 * are the upper 8 bits.
1046 fid = data->powernow_table[index].index & 0xFF;
1047 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
1049 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
1051 if (query_current_values_with_pending_wait(data))
1054 if ((data->currvid == vid) && (data->currfid == fid)) {
1055 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
1060 if ((fid < HI_FID_TABLE_BOTTOM) &&
1061 (data->currfid < HI_FID_TABLE_BOTTOM)) {
1063 "ignoring illegal change in lo freq table-%x to 0x%x\n",
1064 data->currfid, fid);
1068 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
1069 smp_processor_id(), fid, vid);
1070 freqs.old = find_khz_freq_from_fid(data->currfid);
1071 freqs.new = find_khz_freq_from_fid(fid);
1073 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1075 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1078 res = transition_fid_vid(data, fid, vid);
1079 freqs.new = find_khz_freq_from_fid(data->currfid);
1081 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1083 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1088 /* Take a frequency, and issue the hardware pstate transition command */
1089 static int transition_frequency_pstate(struct powernow_k8_data *data,
1094 struct cpufreq_freqs freqs;
1096 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1098 /* get MSR index for hardware pstate transition */
1099 pstate = index & HW_PSTATE_MASK;
1100 if (pstate > data->max_hw_pstate)
1102 freqs.old = find_khz_freq_from_pstate(data->powernow_table,
1104 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1106 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1108 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1111 res = transition_pstate(data, pstate);
1112 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1114 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1116 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1121 /* Driver entry point to switch to the target frequency */
1122 static int powernowk8_target(struct cpufreq_policy *pol,
1123 unsigned targfreq, unsigned relation)
1126 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1129 unsigned int newstate;
1135 checkfid = data->currfid;
1136 checkvid = data->currvid;
1138 /* only run on specific CPU from here on */
1139 oldmask = current->cpus_allowed;
1140 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1142 if (smp_processor_id() != pol->cpu) {
1143 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1147 if (pending_bit_stuck()) {
1148 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1152 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1153 pol->cpu, targfreq, pol->min, pol->max, relation);
1155 if (query_current_values_with_pending_wait(data))
1158 if (cpu_family != CPU_HW_PSTATE) {
1159 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1160 data->currfid, data->currvid);
1162 if ((checkvid != data->currvid) ||
1163 (checkfid != data->currfid)) {
1164 printk(KERN_INFO PFX
1165 "error - out of sync, fix 0x%x 0x%x, "
1167 checkfid, data->currfid,
1168 checkvid, data->currvid);
1172 if (cpufreq_frequency_table_target(pol, data->powernow_table,
1173 targfreq, relation, &newstate))
1176 mutex_lock(&fidvid_mutex);
1178 powernow_k8_acpi_pst_values(data, newstate);
1180 if (cpu_family == CPU_HW_PSTATE)
1181 ret = transition_frequency_pstate(data, newstate);
1183 ret = transition_frequency_fidvid(data, newstate);
1185 printk(KERN_ERR PFX "transition frequency failed\n");
1187 mutex_unlock(&fidvid_mutex);
1190 mutex_unlock(&fidvid_mutex);
1192 if (cpu_family == CPU_HW_PSTATE)
1193 pol->cur = find_khz_freq_from_pstate(data->powernow_table,
1196 pol->cur = find_khz_freq_from_fid(data->currfid);
1200 set_cpus_allowed_ptr(current, &oldmask);
1204 /* Driver entry point to verify the policy and range of frequencies */
1205 static int powernowk8_verify(struct cpufreq_policy *pol)
1207 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1212 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1215 /* per CPU init entry point to the driver */
1216 static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
1218 struct powernow_k8_data *data;
1221 static int print_once;
1223 if (!cpu_online(pol->cpu))
1226 if (!check_supported_cpu(pol->cpu))
1229 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
1231 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1235 data->cpu = pol->cpu;
1236 data->currpstate = HW_PSTATE_INVALID;
1238 if (powernow_k8_cpu_init_acpi(data)) {
1240 * Use the PSB BIOS structure. This is only availabe on
1241 * an UP version, and is deprecated by AMD.
1243 if (num_online_cpus() != 1) {
1245 * Replace this one with print_once as soon as such a
1246 * thing gets introduced
1249 WARN_ONCE(1, KERN_ERR FW_BUG PFX "Your BIOS "
1250 "does not provide ACPI _PSS objects "
1251 "in a way that Linux understands. "
1252 "Please report this to the Linux ACPI"
1253 " maintainers and complain to your "
1259 if (pol->cpu != 0) {
1260 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1261 "CPU other than CPU0. Complain to your BIOS "
1265 rc = find_psb_table(data);
1269 /* Take a crude guess here.
1270 * That guess was in microseconds, so multiply with 1000 */
1271 pol->cpuinfo.transition_latency = (
1272 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
1273 ((1 << data->irt) * 30)) * 1000;
1274 } else /* ACPI _PSS objects available */
1275 pol->cpuinfo.transition_latency = get_transition_latency(data);
1277 /* only run on specific CPU from here on */
1278 oldmask = current->cpus_allowed;
1279 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1281 if (smp_processor_id() != pol->cpu) {
1282 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1283 goto err_out_unmask;
1286 if (pending_bit_stuck()) {
1287 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1288 goto err_out_unmask;
1291 if (query_current_values_with_pending_wait(data))
1292 goto err_out_unmask;
1294 if (cpu_family == CPU_OPTERON)
1297 /* run on any CPU again */
1298 set_cpus_allowed_ptr(current, &oldmask);
1300 if (cpu_family == CPU_HW_PSTATE)
1301 cpumask_copy(pol->cpus, cpumask_of(pol->cpu));
1303 cpumask_copy(pol->cpus, &per_cpu(cpu_core_map, pol->cpu));
1304 data->available_cores = pol->cpus;
1306 if (cpu_family == CPU_HW_PSTATE)
1307 pol->cur = find_khz_freq_from_pstate(data->powernow_table,
1310 pol->cur = find_khz_freq_from_fid(data->currfid);
1311 dprintk("policy current frequency %d kHz\n", pol->cur);
1313 /* min/max the cpu is capable of */
1314 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1315 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
1316 powernow_k8_cpu_exit_acpi(data);
1317 kfree(data->powernow_table);
1322 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1324 if (cpu_family == CPU_HW_PSTATE)
1325 dprintk("cpu_init done, current pstate 0x%x\n",
1328 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1329 data->currfid, data->currvid);
1331 per_cpu(powernow_data, pol->cpu) = data;
1336 set_cpus_allowed_ptr(current, &oldmask);
1337 powernow_k8_cpu_exit_acpi(data);
1344 static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
1346 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1351 powernow_k8_cpu_exit_acpi(data);
1353 cpufreq_frequency_table_put_attr(pol->cpu);
1355 kfree(data->powernow_table);
1361 static unsigned int powernowk8_get(unsigned int cpu)
1363 struct powernow_k8_data *data;
1364 cpumask_t oldmask = current->cpus_allowed;
1365 unsigned int khz = 0;
1368 first = first_cpu(per_cpu(cpu_core_map, cpu));
1369 data = per_cpu(powernow_data, first);
1374 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
1375 if (smp_processor_id() != cpu) {
1377 "limiting to CPU %d failed in powernowk8_get\n", cpu);
1378 set_cpus_allowed_ptr(current, &oldmask);
1382 if (query_current_values_with_pending_wait(data))
1385 if (cpu_family == CPU_HW_PSTATE)
1386 khz = find_khz_freq_from_pstate(data->powernow_table,
1389 khz = find_khz_freq_from_fid(data->currfid);
1393 set_cpus_allowed_ptr(current, &oldmask);
1397 static struct freq_attr *powernow_k8_attr[] = {
1398 &cpufreq_freq_attr_scaling_available_freqs,
1402 static struct cpufreq_driver cpufreq_amd64_driver = {
1403 .verify = powernowk8_verify,
1404 .target = powernowk8_target,
1405 .init = powernowk8_cpu_init,
1406 .exit = __devexit_p(powernowk8_cpu_exit),
1407 .get = powernowk8_get,
1408 .name = "powernow-k8",
1409 .owner = THIS_MODULE,
1410 .attr = powernow_k8_attr,
1413 /* driver entry point for init */
1414 static int __cpuinit powernowk8_init(void)
1416 unsigned int i, supported_cpus = 0;
1418 for_each_online_cpu(i) {
1419 if (check_supported_cpu(i))
1423 if (supported_cpus == num_online_cpus()) {
1424 printk(KERN_INFO PFX "Found %d %s "
1425 "processors (%d cpu cores) (" VERSION ")\n",
1427 boot_cpu_data.x86_model_id, supported_cpus);
1428 return cpufreq_register_driver(&cpufreq_amd64_driver);
1434 /* driver entry point for term */
1435 static void __exit powernowk8_exit(void)
1439 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1442 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
1443 "Mark Langsdorf <mark.langsdorf@amd.com>");
1444 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1445 MODULE_LICENSE("GPL");
1447 late_initcall(powernowk8_init);
1448 module_exit(powernowk8_exit);