2 * (c) 2003-2012 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
8 * Andreas Herrmann <andreas.herrmann3@amd.com>
10 * Based on the powernow-k7.c module written by Dave Jones.
11 * (C) 2003 Dave Jones on behalf of SuSE Labs
12 * (C) 2004 Dominik Brodowski <linux@brodo.de>
13 * (C) 2004 Pavel Machek <pavel@ucw.cz>
14 * Licensed under the terms of the GNU GPL License version 2.
15 * Based upon datasheets & sample CPUs kindly provided by AMD.
17 * Valuable input gratefully received from Dave Jones, Pavel Machek,
18 * Dominik Brodowski, Jacob Shin, and others.
19 * Originally developed by Paul Devriendt.
21 * Processor information obtained from Chapter 9 (Power and Thermal
22 * Management) of the "BIOS and Kernel Developer's Guide (BKDG) for
23 * the AMD Athlon 64 and AMD Opteron Processors" and section "2.x
24 * Power Management" in BKDGs for newer AMD CPU families.
26 * Tables for specific CPUs can be inferred from AMD's processor
27 * power and thermal data sheets, (e.g. 30417.pdf, 30430.pdf, 43375.pdf)
30 #include <linux/kernel.h>
31 #include <linux/smp.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/cpufreq.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/cpumask.h>
38 #include <linux/sched.h> /* for current / set_cpus_allowed() */
40 #include <linux/delay.h>
43 #include <asm/cpu_device_id.h>
45 #include <linux/acpi.h>
46 #include <linux/mutex.h>
47 #include <acpi/processor.h>
49 #define PFX "powernow-k8: "
50 #define VERSION "version 2.20.00"
51 #include "powernow-k8.h"
53 /* serialize freq changes */
54 static DEFINE_MUTEX(fidvid_mutex);
56 static DEFINE_PER_CPU(struct powernow_k8_data *, powernow_data);
58 static struct cpufreq_driver cpufreq_amd64_driver;
61 static inline const struct cpumask *cpu_core_mask(int cpu)
67 /* Return a frequency in MHz, given an input fid */
68 static u32 find_freq_from_fid(u32 fid)
70 return 800 + (fid * 100);
73 /* Return a frequency in KHz, given an input fid */
74 static u32 find_khz_freq_from_fid(u32 fid)
76 return 1000 * find_freq_from_fid(fid);
79 /* Return the vco fid for an input fid
81 * Each "low" fid has corresponding "high" fid, and you can get to "low" fids
82 * only from corresponding high fids. This returns "high" fid corresponding to
85 static u32 convert_fid_to_vco_fid(u32 fid)
87 if (fid < HI_FID_TABLE_BOTTOM)
94 * Return 1 if the pending bit is set. Unless we just instructed the processor
95 * to transition to a new state, seeing this bit set is really bad news.
97 static int pending_bit_stuck(void)
101 rdmsr(MSR_FIDVID_STATUS, lo, hi);
102 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
106 * Update the global current fid / vid values from the status msr.
107 * Returns 1 on error.
109 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
116 pr_debug("detected change pending stuck\n");
119 rdmsr(MSR_FIDVID_STATUS, lo, hi);
120 } while (lo & MSR_S_LO_CHANGE_PENDING);
122 data->currvid = hi & MSR_S_HI_CURRENT_VID;
123 data->currfid = lo & MSR_S_LO_CURRENT_FID;
128 /* the isochronous relief time */
129 static void count_off_irt(struct powernow_k8_data *data)
131 udelay((1 << data->irt) * 10);
135 /* the voltage stabilization time */
136 static void count_off_vst(struct powernow_k8_data *data)
138 udelay(data->vstable * VST_UNITS_20US);
142 /* need to init the control msr to a safe value (for each cpu) */
143 static void fidvid_msr_init(void)
148 rdmsr(MSR_FIDVID_STATUS, lo, hi);
149 vid = hi & MSR_S_HI_CURRENT_VID;
150 fid = lo & MSR_S_LO_CURRENT_FID;
151 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
152 hi = MSR_C_HI_STP_GNT_BENIGN;
153 pr_debug("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
154 wrmsr(MSR_FIDVID_CTL, lo, hi);
157 /* write the new fid value along with the other control fields to the msr */
158 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
161 u32 savevid = data->currvid;
164 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
165 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
170 lo |= (data->currvid << MSR_C_LO_VID_SHIFT);
171 lo |= MSR_C_LO_INIT_FID_VID;
173 pr_debug("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
174 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
177 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
180 "Hardware error - pending bit very stuck - "
181 "no further pstate changes possible\n");
184 } while (query_current_values_with_pending_wait(data));
188 if (savevid != data->currvid) {
190 "vid change on fid trans, old 0x%x, new 0x%x\n",
191 savevid, data->currvid);
195 if (fid != data->currfid) {
197 "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
205 /* Write a new vid to the hardware */
206 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
209 u32 savefid = data->currfid;
212 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
213 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
218 lo |= (vid << MSR_C_LO_VID_SHIFT);
219 lo |= MSR_C_LO_INIT_FID_VID;
221 pr_debug("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
222 vid, lo, STOP_GRANT_5NS);
225 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
227 printk(KERN_ERR PFX "internal error - pending bit "
228 "very stuck - no further pstate "
229 "changes possible\n");
232 } while (query_current_values_with_pending_wait(data));
234 if (savefid != data->currfid) {
235 printk(KERN_ERR PFX "fid changed on vid trans, old "
237 savefid, data->currfid);
241 if (vid != data->currvid) {
242 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, "
252 * Reduce the vid by the max of step or reqvid.
253 * Decreasing vid codes represent increasing voltages:
254 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
256 static int decrease_vid_code_by_step(struct powernow_k8_data *data,
257 u32 reqvid, u32 step)
259 if ((data->currvid - reqvid) > step)
260 reqvid = data->currvid - step;
262 if (write_new_vid(data, reqvid))
270 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
271 static int transition_fid_vid(struct powernow_k8_data *data,
272 u32 reqfid, u32 reqvid)
274 if (core_voltage_pre_transition(data, reqvid, reqfid))
277 if (core_frequency_transition(data, reqfid))
280 if (core_voltage_post_transition(data, reqvid))
283 if (query_current_values_with_pending_wait(data))
286 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
287 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, "
290 reqfid, reqvid, data->currfid, data->currvid);
294 pr_debug("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
295 smp_processor_id(), data->currfid, data->currvid);
300 /* Phase 1 - core voltage transition ... setup voltage */
301 static int core_voltage_pre_transition(struct powernow_k8_data *data,
302 u32 reqvid, u32 reqfid)
304 u32 rvosteps = data->rvo;
305 u32 savefid = data->currfid;
306 u32 maxvid, lo, rvomult = 1;
308 pr_debug("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, "
309 "reqvid 0x%x, rvo 0x%x\n",
311 data->currfid, data->currvid, reqvid, data->rvo);
313 if ((savefid < LO_FID_TABLE_TOP) && (reqfid < LO_FID_TABLE_TOP))
316 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
317 maxvid = 0x1f & (maxvid >> 16);
318 pr_debug("ph1 maxvid=0x%x\n", maxvid);
319 if (reqvid < maxvid) /* lower numbers are higher voltages */
322 while (data->currvid > reqvid) {
323 pr_debug("ph1: curr 0x%x, req vid 0x%x\n",
324 data->currvid, reqvid);
325 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
329 while ((rvosteps > 0) &&
330 ((rvomult * data->rvo + data->currvid) > reqvid)) {
331 if (data->currvid == maxvid) {
334 pr_debug("ph1: changing vid for rvo, req 0x%x\n",
336 if (decrease_vid_code_by_step(data, data->currvid-1, 1))
342 if (query_current_values_with_pending_wait(data))
345 if (savefid != data->currfid) {
346 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n",
351 pr_debug("ph1 complete, currfid 0x%x, currvid 0x%x\n",
352 data->currfid, data->currvid);
357 /* Phase 2 - core frequency transition */
358 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
360 u32 vcoreqfid, vcocurrfid, vcofiddiff;
361 u32 fid_interval, savevid = data->currvid;
363 if (data->currfid == reqfid) {
364 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n",
369 pr_debug("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, "
372 data->currfid, data->currvid, reqfid);
374 vcoreqfid = convert_fid_to_vco_fid(reqfid);
375 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
376 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
377 : vcoreqfid - vcocurrfid;
379 if ((reqfid <= LO_FID_TABLE_TOP) && (data->currfid <= LO_FID_TABLE_TOP))
382 while (vcofiddiff > 2) {
383 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
385 if (reqfid > data->currfid) {
386 if (data->currfid > LO_FID_TABLE_TOP) {
387 if (write_new_fid(data,
388 data->currfid + fid_interval))
393 2 + convert_fid_to_vco_fid(data->currfid)))
397 if (write_new_fid(data, data->currfid - fid_interval))
401 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
402 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
403 : vcoreqfid - vcocurrfid;
406 if (write_new_fid(data, reqfid))
409 if (query_current_values_with_pending_wait(data))
412 if (data->currfid != reqfid) {
414 "ph2: mismatch, failed fid transition, "
415 "curr 0x%x, req 0x%x\n",
416 data->currfid, reqfid);
420 if (savevid != data->currvid) {
421 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
422 savevid, data->currvid);
426 pr_debug("ph2 complete, currfid 0x%x, currvid 0x%x\n",
427 data->currfid, data->currvid);
432 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
433 static int core_voltage_post_transition(struct powernow_k8_data *data,
436 u32 savefid = data->currfid;
437 u32 savereqvid = reqvid;
439 pr_debug("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
441 data->currfid, data->currvid);
443 if (reqvid != data->currvid) {
444 if (write_new_vid(data, reqvid))
447 if (savefid != data->currfid) {
449 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
450 savefid, data->currfid);
454 if (data->currvid != reqvid) {
456 "ph3: failed vid transition\n, "
457 "req 0x%x, curr 0x%x",
458 reqvid, data->currvid);
463 if (query_current_values_with_pending_wait(data))
466 if (savereqvid != data->currvid) {
467 pr_debug("ph3 failed, currvid 0x%x\n", data->currvid);
471 if (savefid != data->currfid) {
472 pr_debug("ph3 failed, currfid changed 0x%x\n",
477 pr_debug("ph3 complete, currfid 0x%x, currvid 0x%x\n",
478 data->currfid, data->currvid);
483 static const struct x86_cpu_id powernow_k8_ids[] = {
484 /* IO based frequency switching */
485 { X86_VENDOR_AMD, 0xf },
488 MODULE_DEVICE_TABLE(x86cpu, powernow_k8_ids);
490 static void check_supported_cpu(void *_rc)
492 u32 eax, ebx, ecx, edx;
497 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
499 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
500 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
501 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
503 "Processor cpuid %x not supported\n", eax);
507 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
508 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
510 "No frequency change capabilities detected\n");
514 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
515 if ((edx & P_STATE_TRANSITION_CAPABLE)
516 != P_STATE_TRANSITION_CAPABLE) {
518 "Power state transitions not supported\n");
525 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst,
531 for (j = 0; j < data->numps; j++) {
532 if (pst[j].vid > LEAST_VID) {
533 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
537 if (pst[j].vid < data->rvo) {
539 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
543 if (pst[j].vid < maxvid + data->rvo) {
544 /* vid + rvo >= maxvid */
545 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
549 if (pst[j].fid > MAX_FID) {
550 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
554 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
555 /* Only first fid is allowed to be in "low" range */
556 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
557 "0x%x\n", j, pst[j].fid);
560 if (pst[j].fid < lastfid)
561 lastfid = pst[j].fid;
564 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
567 if (lastfid > LO_FID_TABLE_TOP)
568 printk(KERN_INFO FW_BUG PFX
569 "first fid not from lo freq table\n");
574 static void invalidate_entry(struct cpufreq_frequency_table *powernow_table,
577 powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
580 static void print_basics(struct powernow_k8_data *data)
583 for (j = 0; j < data->numps; j++) {
584 if (data->powernow_table[j].frequency !=
585 CPUFREQ_ENTRY_INVALID) {
587 "fid 0x%x (%d MHz), vid 0x%x\n",
588 data->powernow_table[j].index & 0xff,
589 data->powernow_table[j].frequency/1000,
590 data->powernow_table[j].index >> 8);
594 printk(KERN_INFO PFX "Only %d pstates on battery\n",
598 static int fill_powernow_table(struct powernow_k8_data *data,
599 struct pst_s *pst, u8 maxvid)
601 struct cpufreq_frequency_table *powernow_table;
605 /* use ACPI support to get full speed on mains power */
606 printk(KERN_WARNING PFX
607 "Only %d pstates usable (use ACPI driver for full "
608 "range\n", data->batps);
609 data->numps = data->batps;
612 for (j = 1; j < data->numps; j++) {
613 if (pst[j-1].fid >= pst[j].fid) {
614 printk(KERN_ERR PFX "PST out of sequence\n");
619 if (data->numps < 2) {
620 printk(KERN_ERR PFX "no p states to transition\n");
624 if (check_pst_table(data, pst, maxvid))
627 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
628 * (data->numps + 1)), GFP_KERNEL);
629 if (!powernow_table) {
630 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
634 for (j = 0; j < data->numps; j++) {
636 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
637 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
638 freq = find_khz_freq_from_fid(pst[j].fid);
639 powernow_table[j].frequency = freq;
641 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
642 powernow_table[data->numps].index = 0;
644 if (query_current_values_with_pending_wait(data)) {
645 kfree(powernow_table);
649 pr_debug("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
650 data->powernow_table = powernow_table;
651 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
654 for (j = 0; j < data->numps; j++)
655 if ((pst[j].fid == data->currfid) &&
656 (pst[j].vid == data->currvid))
659 pr_debug("currfid/vid do not match PST, ignoring\n");
663 /* Find and validate the PSB/PST table in BIOS. */
664 static int find_psb_table(struct powernow_k8_data *data)
673 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
674 /* Scan BIOS looking for the signature. */
675 /* It can not be at ffff0 - it is too big. */
677 psb = phys_to_virt(i);
678 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
681 pr_debug("found PSB header at 0x%p\n", psb);
683 pr_debug("table vers: 0x%x\n", psb->tableversion);
684 if (psb->tableversion != PSB_VERSION_1_4) {
685 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
689 pr_debug("flags: 0x%x\n", psb->flags1);
691 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
695 data->vstable = psb->vstable;
696 pr_debug("voltage stabilization time: %d(*20us)\n",
699 pr_debug("flags2: 0x%x\n", psb->flags2);
700 data->rvo = psb->flags2 & 3;
701 data->irt = ((psb->flags2) >> 2) & 3;
702 mvs = ((psb->flags2) >> 4) & 3;
703 data->vidmvs = 1 << mvs;
704 data->batps = ((psb->flags2) >> 6) & 3;
706 pr_debug("ramp voltage offset: %d\n", data->rvo);
707 pr_debug("isochronous relief time: %d\n", data->irt);
708 pr_debug("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
710 pr_debug("numpst: 0x%x\n", psb->num_tables);
711 cpst = psb->num_tables;
712 if ((psb->cpuid == 0x00000fc0) ||
713 (psb->cpuid == 0x00000fe0)) {
714 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
715 if ((thiscpuid == 0x00000fc0) ||
716 (thiscpuid == 0x00000fe0))
720 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
724 data->plllock = psb->plllocktime;
725 pr_debug("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
726 pr_debug("maxfid: 0x%x\n", psb->maxfid);
727 pr_debug("maxvid: 0x%x\n", psb->maxvid);
728 maxvid = psb->maxvid;
730 data->numps = psb->numps;
731 pr_debug("numpstates: 0x%x\n", data->numps);
732 return fill_powernow_table(data,
733 (struct pst_s *)(psb+1), maxvid);
736 * If you see this message, complain to BIOS manufacturer. If
737 * he tells you "we do not support Linux" or some similar
738 * nonsense, remember that Windows 2000 uses the same legacy
739 * mechanism that the old Linux PSB driver uses. Tell them it
740 * is broken with Windows 2000.
742 * The reference to the AMD documentation is chapter 9 in the
743 * BIOS and Kernel Developer's Guide, which is available on
746 printk(KERN_ERR FW_BUG PFX "No PSB or ACPI _PSS objects\n");
747 printk(KERN_ERR PFX "Make sure that your BIOS is up to date"
748 " and Cool'N'Quiet support is enabled in BIOS setup\n");
752 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data,
757 if (!data->acpi_data.state_count)
760 control = data->acpi_data.states[index].control;
761 data->irt = (control >> IRT_SHIFT) & IRT_MASK;
762 data->rvo = (control >> RVO_SHIFT) & RVO_MASK;
763 data->exttype = (control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
764 data->plllock = (control >> PLL_L_SHIFT) & PLL_L_MASK;
765 data->vidmvs = 1 << ((control >> MVS_SHIFT) & MVS_MASK);
766 data->vstable = (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;
775 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
776 pr_debug("register performance failed: bad ACPI data\n");
780 /* verify the data contained in the ACPI structures */
781 if (data->acpi_data.state_count <= 1) {
782 pr_debug("No ACPI P-States\n");
786 control = data->acpi_data.control_register.space_id;
787 status = data->acpi_data.status_register.space_id;
789 if ((control != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
790 (status != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
791 pr_debug("Invalid control/status registers (%llx - %llx)\n",
796 /* fill in data->powernow_table */
797 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
798 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
799 if (!powernow_table) {
800 pr_debug("powernow_table memory alloc failure\n");
805 data->numps = data->acpi_data.state_count;
806 powernow_k8_acpi_pst_values(data, 0);
808 ret_val = fill_powernow_table_fidvid(data, powernow_table);
812 powernow_table[data->acpi_data.state_count].frequency =
814 powernow_table[data->acpi_data.state_count].index = 0;
815 data->powernow_table = powernow_table;
817 if (cpumask_first(cpu_core_mask(data->cpu)) == data->cpu)
820 /* notify BIOS that we exist */
821 acpi_processor_notify_smm(THIS_MODULE);
823 if (!zalloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
825 "unable to alloc powernow_k8_data cpumask\n");
833 kfree(powernow_table);
836 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
838 /* data->acpi_data.state_count informs us at ->exit()
839 * whether ACPI was used */
840 data->acpi_data.state_count = 0;
845 static int fill_powernow_table_fidvid(struct powernow_k8_data *data,
846 struct cpufreq_frequency_table *powernow_table)
850 for (i = 0; i < data->acpi_data.state_count; i++) {
857 status = data->acpi_data.states[i].status;
858 fid = status & EXT_FID_MASK;
859 vid = (status >> VID_SHIFT) & EXT_VID_MASK;
861 control = data->acpi_data.states[i].control;
862 fid = control & FID_MASK;
863 vid = (control >> VID_SHIFT) & VID_MASK;
866 pr_debug(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
868 index = fid | (vid<<8);
869 powernow_table[i].index = index;
871 freq = find_khz_freq_from_fid(fid);
872 powernow_table[i].frequency = freq;
874 /* verify frequency is OK */
875 if ((freq > (MAX_FREQ * 1000)) || (freq < (MIN_FREQ * 1000))) {
876 pr_debug("invalid freq %u kHz, ignoring\n", freq);
877 invalidate_entry(powernow_table, i);
881 /* verify voltage is OK -
882 * BIOSs are using "off" to indicate invalid */
883 if (vid == VID_OFF) {
884 pr_debug("invalid vid %u, ignoring\n", vid);
885 invalidate_entry(powernow_table, i);
889 if (freq != (data->acpi_data.states[i].core_frequency * 1000)) {
890 printk(KERN_INFO PFX "invalid freq entries "
891 "%u kHz vs. %u kHz\n", freq,
893 (data->acpi_data.states[i].core_frequency
895 invalidate_entry(powernow_table, i);
902 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
904 if (data->acpi_data.state_count)
905 acpi_processor_unregister_performance(&data->acpi_data,
907 free_cpumask_var(data->acpi_data.shared_cpu_map);
910 static int get_transition_latency(struct powernow_k8_data *data)
914 for (i = 0; i < data->acpi_data.state_count; i++) {
915 int cur_latency = data->acpi_data.states[i].transition_latency
916 + data->acpi_data.states[i].bus_master_latency;
917 if (cur_latency > max_latency)
918 max_latency = cur_latency;
920 if (max_latency == 0) {
921 pr_err(FW_WARN PFX "Invalid zero transition latency\n");
924 /* value in usecs, needs to be in nanoseconds */
925 return 1000 * max_latency;
928 /* Take a frequency, and issue the fid/vid transition command */
929 static int transition_frequency_fidvid(struct powernow_k8_data *data,
935 struct cpufreq_freqs freqs;
937 pr_debug("cpu %d transition to index %u\n", smp_processor_id(), index);
939 /* fid/vid correctness check for k8 */
940 /* fid are the lower 8 bits of the index we stored into
941 * the cpufreq frequency table in find_psb_table, vid
942 * are the upper 8 bits.
944 fid = data->powernow_table[index].index & 0xFF;
945 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
947 pr_debug("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
949 if (query_current_values_with_pending_wait(data))
952 if ((data->currvid == vid) && (data->currfid == fid)) {
953 pr_debug("target matches current values (fid 0x%x, vid 0x%x)\n",
958 pr_debug("cpu %d, changing to fid 0x%x, vid 0x%x\n",
959 smp_processor_id(), fid, vid);
960 freqs.old = find_khz_freq_from_fid(data->currfid);
961 freqs.new = find_khz_freq_from_fid(fid);
963 for_each_cpu(i, data->available_cores) {
965 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
968 res = transition_fid_vid(data, fid, vid);
972 freqs.new = find_khz_freq_from_fid(data->currfid);
974 for_each_cpu(i, data->available_cores) {
976 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
981 /* Driver entry point to switch to the target frequency */
982 static int powernowk8_target(struct cpufreq_policy *pol,
983 unsigned targfreq, unsigned relation)
985 cpumask_var_t oldmask;
986 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
989 unsigned int newstate;
995 checkfid = data->currfid;
996 checkvid = data->currvid;
998 /* only run on specific CPU from here on. */
999 /* This is poor form: use a workqueue or smp_call_function_single */
1000 if (!alloc_cpumask_var(&oldmask, GFP_KERNEL))
1003 cpumask_copy(oldmask, tsk_cpus_allowed(current));
1004 set_cpus_allowed_ptr(current, cpumask_of(pol->cpu));
1006 if (smp_processor_id() != pol->cpu) {
1007 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1011 if (pending_bit_stuck()) {
1012 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1016 pr_debug("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1017 pol->cpu, targfreq, pol->min, pol->max, relation);
1019 if (query_current_values_with_pending_wait(data))
1022 pr_debug("targ: curr fid 0x%x, vid 0x%x\n",
1023 data->currfid, data->currvid);
1025 if ((checkvid != data->currvid) ||
1026 (checkfid != data->currfid)) {
1028 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1029 checkfid, data->currfid,
1030 checkvid, data->currvid);
1033 if (cpufreq_frequency_table_target(pol, data->powernow_table,
1034 targfreq, relation, &newstate))
1037 mutex_lock(&fidvid_mutex);
1039 powernow_k8_acpi_pst_values(data, newstate);
1041 ret = transition_frequency_fidvid(data, newstate);
1044 printk(KERN_ERR PFX "transition frequency failed\n");
1046 mutex_unlock(&fidvid_mutex);
1049 mutex_unlock(&fidvid_mutex);
1051 pol->cur = find_khz_freq_from_fid(data->currfid);
1055 set_cpus_allowed_ptr(current, oldmask);
1056 free_cpumask_var(oldmask);
1060 /* Driver entry point to verify the policy and range of frequencies */
1061 static int powernowk8_verify(struct cpufreq_policy *pol)
1063 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1068 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1071 struct init_on_cpu {
1072 struct powernow_k8_data *data;
1076 static void __cpuinit powernowk8_cpu_init_on_cpu(void *_init_on_cpu)
1078 struct init_on_cpu *init_on_cpu = _init_on_cpu;
1080 if (pending_bit_stuck()) {
1081 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1082 init_on_cpu->rc = -ENODEV;
1086 if (query_current_values_with_pending_wait(init_on_cpu->data)) {
1087 init_on_cpu->rc = -ENODEV;
1093 init_on_cpu->rc = 0;
1096 static const char missing_pss_msg[] =
1098 FW_BUG PFX "No compatible ACPI _PSS objects found.\n"
1099 FW_BUG PFX "First, make sure Cool'N'Quiet is enabled in the BIOS.\n"
1100 FW_BUG PFX "If that doesn't help, try upgrading your BIOS.\n";
1102 /* per CPU init entry point to the driver */
1103 static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
1105 struct powernow_k8_data *data;
1106 struct init_on_cpu init_on_cpu;
1109 if (!cpu_online(pol->cpu))
1112 smp_call_function_single(pol->cpu, check_supported_cpu, &rc, 1);
1116 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
1118 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1122 data->cpu = pol->cpu;
1124 if (powernow_k8_cpu_init_acpi(data)) {
1126 * Use the PSB BIOS structure. This is only available on
1127 * an UP version, and is deprecated by AMD.
1129 if (num_online_cpus() != 1) {
1130 printk_once(missing_pss_msg);
1133 if (pol->cpu != 0) {
1134 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1135 "CPU other than CPU0. Complain to your BIOS "
1139 rc = find_psb_table(data);
1143 /* Take a crude guess here.
1144 * That guess was in microseconds, so multiply with 1000 */
1145 pol->cpuinfo.transition_latency = (
1146 ((data->rvo + 8) * data->vstable * VST_UNITS_20US) +
1147 ((1 << data->irt) * 30)) * 1000;
1148 } else /* ACPI _PSS objects available */
1149 pol->cpuinfo.transition_latency = get_transition_latency(data);
1151 /* only run on specific CPU from here on */
1152 init_on_cpu.data = data;
1153 smp_call_function_single(data->cpu, powernowk8_cpu_init_on_cpu,
1155 rc = init_on_cpu.rc;
1157 goto err_out_exit_acpi;
1159 cpumask_copy(pol->cpus, cpu_core_mask(pol->cpu));
1160 data->available_cores = pol->cpus;
1162 pol->cur = find_khz_freq_from_fid(data->currfid);
1163 pr_debug("policy current frequency %d kHz\n", pol->cur);
1165 /* min/max the cpu is capable of */
1166 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1167 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
1168 powernow_k8_cpu_exit_acpi(data);
1169 kfree(data->powernow_table);
1174 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1176 pr_debug("cpu_init done, current fid 0x%x, vid 0x%x\n",
1177 data->currfid, data->currvid);
1179 per_cpu(powernow_data, pol->cpu) = data;
1184 powernow_k8_cpu_exit_acpi(data);
1191 static int __devexit powernowk8_cpu_exit(struct cpufreq_policy *pol)
1193 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1198 powernow_k8_cpu_exit_acpi(data);
1200 cpufreq_frequency_table_put_attr(pol->cpu);
1202 kfree(data->powernow_table);
1204 per_cpu(powernow_data, pol->cpu) = NULL;
1209 static void query_values_on_cpu(void *_err)
1212 struct powernow_k8_data *data = __this_cpu_read(powernow_data);
1214 *err = query_current_values_with_pending_wait(data);
1217 static unsigned int powernowk8_get(unsigned int cpu)
1219 struct powernow_k8_data *data = per_cpu(powernow_data, cpu);
1220 unsigned int khz = 0;
1226 smp_call_function_single(cpu, query_values_on_cpu, &err, true);
1230 khz = find_khz_freq_from_fid(data->currfid);
1237 static struct freq_attr *powernow_k8_attr[] = {
1238 &cpufreq_freq_attr_scaling_available_freqs,
1242 static struct cpufreq_driver cpufreq_amd64_driver = {
1243 .verify = powernowk8_verify,
1244 .target = powernowk8_target,
1245 .bios_limit = acpi_processor_get_bios_limit,
1246 .init = powernowk8_cpu_init,
1247 .exit = __devexit_p(powernowk8_cpu_exit),
1248 .get = powernowk8_get,
1249 .name = "powernow-k8",
1250 .owner = THIS_MODULE,
1251 .attr = powernow_k8_attr,
1254 /* driver entry point for init */
1255 static int __cpuinit powernowk8_init(void)
1257 unsigned int i, supported_cpus = 0;
1260 if (static_cpu_has(X86_FEATURE_HW_PSTATE)) {
1261 pr_warn(PFX "this CPU is not supported anymore, using acpi-cpufreq instead.\n");
1262 request_module("acpi-cpufreq");
1266 if (!x86_match_cpu(powernow_k8_ids))
1269 for_each_online_cpu(i) {
1271 smp_call_function_single(i, check_supported_cpu, &rc, 1);
1276 if (supported_cpus != num_online_cpus())
1279 rv = cpufreq_register_driver(&cpufreq_amd64_driver);
1282 pr_info(PFX "Found %d %s (%d cpu cores) (" VERSION ")\n",
1283 num_online_nodes(), boot_cpu_data.x86_model_id,
1289 /* driver entry point for term */
1290 static void __exit powernowk8_exit(void)
1294 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1297 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and "
1298 "Mark Langsdorf <mark.langsdorf@amd.com>");
1299 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1300 MODULE_LICENSE("GPL");
1302 late_initcall(powernowk8_init);
1303 module_exit(powernowk8_exit);