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 DEFINE_PER_CPU(cpumask_t, cpu_core_map);
62 /* Return a frequency in MHz, given an input fid */
63 static u32 find_freq_from_fid(u32 fid)
65 return 800 + (fid * 100);
68 /* Return a frequency in KHz, given an input fid */
69 static u32 find_khz_freq_from_fid(u32 fid)
71 return 1000 * find_freq_from_fid(fid);
74 static u32 find_khz_freq_from_pstate(struct cpufreq_frequency_table *data, u32 pstate)
76 return data[pstate].frequency;
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 if (cpu_family == CPU_HW_PSTATE)
104 rdmsr(MSR_FIDVID_STATUS, lo, hi);
105 return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
109 * Update the global current fid / vid values from the status msr.
110 * Returns 1 on error.
112 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
117 if (cpu_family == CPU_HW_PSTATE) {
118 if (data->currpstate == HW_PSTATE_INVALID) {
119 /* read (initial) hw pstate if not yet set */
120 rdmsr(MSR_PSTATE_STATUS, lo, hi);
121 i = lo & HW_PSTATE_MASK;
124 * a workaround for family 11h erratum 311 might cause
125 * an "out-of-range Pstate if the core is in Pstate-0
127 if (i >= data->numps)
128 data->currpstate = HW_PSTATE_0;
130 data->currpstate = i;
136 dprintk("detected change pending stuck\n");
139 rdmsr(MSR_FIDVID_STATUS, lo, hi);
140 } while (lo & MSR_S_LO_CHANGE_PENDING);
142 data->currvid = hi & MSR_S_HI_CURRENT_VID;
143 data->currfid = lo & MSR_S_LO_CURRENT_FID;
148 /* the isochronous relief time */
149 static void count_off_irt(struct powernow_k8_data *data)
151 udelay((1 << data->irt) * 10);
155 /* the voltage stabilization time */
156 static void count_off_vst(struct powernow_k8_data *data)
158 udelay(data->vstable * VST_UNITS_20US);
162 /* need to init the control msr to a safe value (for each cpu) */
163 static void fidvid_msr_init(void)
168 rdmsr(MSR_FIDVID_STATUS, lo, hi);
169 vid = hi & MSR_S_HI_CURRENT_VID;
170 fid = lo & MSR_S_LO_CURRENT_FID;
171 lo = fid | (vid << MSR_C_LO_VID_SHIFT);
172 hi = MSR_C_HI_STP_GNT_BENIGN;
173 dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
174 wrmsr(MSR_FIDVID_CTL, lo, hi);
177 /* write the new fid value along with the other control fields to the msr */
178 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
181 u32 savevid = data->currvid;
184 if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
185 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
189 lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
191 dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
192 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
195 wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
197 printk(KERN_ERR PFX "Hardware error - pending bit very stuck - no further pstate changes possible\n");
200 } while (query_current_values_with_pending_wait(data));
204 if (savevid != data->currvid) {
205 printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
206 savevid, data->currvid);
210 if (fid != data->currfid) {
211 printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
219 /* Write a new vid to the hardware */
220 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
223 u32 savefid = data->currfid;
226 if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
227 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
231 lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
233 dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
234 vid, lo, STOP_GRANT_5NS);
237 wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
239 printk(KERN_ERR PFX "internal error - pending bit very stuck - no further pstate changes possible\n");
242 } while (query_current_values_with_pending_wait(data));
244 if (savefid != data->currfid) {
245 printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
246 savefid, data->currfid);
250 if (vid != data->currvid) {
251 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
260 * Reduce the vid by the max of step or reqvid.
261 * Decreasing vid codes represent increasing voltages:
262 * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of VID_OFF is off.
264 static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
266 if ((data->currvid - reqvid) > step)
267 reqvid = data->currvid - step;
269 if (write_new_vid(data, reqvid))
277 /* Change hardware pstate by single MSR write */
278 static int transition_pstate(struct powernow_k8_data *data, u32 pstate)
280 wrmsr(MSR_PSTATE_CTRL, pstate, 0);
281 data->currpstate = pstate;
285 /* Change Opteron/Athlon64 fid and vid, by the 3 phases. */
286 static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
288 if (core_voltage_pre_transition(data, reqvid))
291 if (core_frequency_transition(data, reqfid))
294 if (core_voltage_post_transition(data, reqvid))
297 if (query_current_values_with_pending_wait(data))
300 if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
301 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
303 reqfid, reqvid, data->currfid, data->currvid);
307 dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
308 smp_processor_id(), data->currfid, data->currvid);
313 /* Phase 1 - core voltage transition ... setup voltage */
314 static int core_voltage_pre_transition(struct powernow_k8_data *data, u32 reqvid)
316 u32 rvosteps = data->rvo;
317 u32 savefid = data->currfid;
320 dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
322 data->currfid, data->currvid, reqvid, data->rvo);
324 rdmsr(MSR_FIDVID_STATUS, lo, maxvid);
325 maxvid = 0x1f & (maxvid >> 16);
326 dprintk("ph1 maxvid=0x%x\n", maxvid);
327 if (reqvid < maxvid) /* lower numbers are higher voltages */
330 while (data->currvid > reqvid) {
331 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
332 data->currvid, reqvid);
333 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
337 while ((rvosteps > 0) && ((data->rvo + data->currvid) > reqvid)) {
338 if (data->currvid == maxvid) {
341 dprintk("ph1: changing vid for rvo, req 0x%x\n",
343 if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
349 if (query_current_values_with_pending_wait(data))
352 if (savefid != data->currfid) {
353 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
357 dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
358 data->currfid, data->currvid);
363 /* Phase 2 - core frequency transition */
364 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
366 u32 vcoreqfid, vcocurrfid, vcofiddiff, fid_interval, savevid = data->currvid;
368 if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
369 printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
370 reqfid, data->currfid);
374 if (data->currfid == reqfid) {
375 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
379 dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
381 data->currfid, data->currvid, reqfid);
383 vcoreqfid = convert_fid_to_vco_fid(reqfid);
384 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
385 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
386 : vcoreqfid - vcocurrfid;
388 while (vcofiddiff > 2) {
389 (data->currfid & 1) ? (fid_interval = 1) : (fid_interval = 2);
391 if (reqfid > data->currfid) {
392 if (data->currfid > LO_FID_TABLE_TOP) {
393 if (write_new_fid(data, data->currfid + fid_interval)) {
398 (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
403 if (write_new_fid(data, data->currfid - fid_interval))
407 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
408 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
409 : vcoreqfid - vcocurrfid;
412 if (write_new_fid(data, reqfid))
415 if (query_current_values_with_pending_wait(data))
418 if (data->currfid != reqfid) {
420 "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
421 data->currfid, reqfid);
425 if (savevid != data->currvid) {
426 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
427 savevid, data->currvid);
431 dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
432 data->currfid, data->currvid);
437 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
438 static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid)
440 u32 savefid = data->currfid;
441 u32 savereqvid = reqvid;
443 dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
445 data->currfid, data->currvid);
447 if (reqvid != data->currvid) {
448 if (write_new_vid(data, reqvid))
451 if (savefid != data->currfid) {
453 "ph3: bad fid change, save 0x%x, curr 0x%x\n",
454 savefid, data->currfid);
458 if (data->currvid != reqvid) {
460 "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
461 reqvid, data->currvid);
466 if (query_current_values_with_pending_wait(data))
469 if (savereqvid != data->currvid) {
470 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
474 if (savefid != data->currfid) {
475 dprintk("ph3 failed, currfid changed 0x%x\n",
480 dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
481 data->currfid, data->currvid);
486 static int check_supported_cpu(unsigned int cpu)
489 u32 eax, ebx, ecx, edx;
492 oldmask = current->cpus_allowed;
493 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
495 if (smp_processor_id() != cpu) {
496 printk(KERN_ERR PFX "limiting to cpu %u failed\n", cpu);
500 if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
503 eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
504 if (((eax & CPUID_XFAM) != CPUID_XFAM_K8) &&
505 ((eax & CPUID_XFAM) < CPUID_XFAM_10H))
508 if ((eax & CPUID_XFAM) == CPUID_XFAM_K8) {
509 if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
510 ((eax & CPUID_XMOD) > CPUID_XMOD_REV_MASK)) {
511 printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
515 eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
516 if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
518 "No frequency change capabilities detected\n");
522 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
523 if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
524 printk(KERN_INFO PFX "Power state transitions not supported\n");
527 } else { /* must be a HW Pstate capable processor */
528 cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
529 if ((edx & USE_HW_PSTATE) == USE_HW_PSTATE)
530 cpu_family = CPU_HW_PSTATE;
538 set_cpus_allowed_ptr(current, &oldmask);
542 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
547 for (j = 0; j < data->numps; j++) {
548 if (pst[j].vid > LEAST_VID) {
549 printk(KERN_ERR FW_BUG PFX "vid %d invalid : 0x%x\n",
553 if (pst[j].vid < data->rvo) { /* vid + rvo >= 0 */
554 printk(KERN_ERR FW_BUG PFX "0 vid exceeded with pstate"
558 if (pst[j].vid < maxvid + data->rvo) { /* vid + rvo >= maxvid */
559 printk(KERN_ERR FW_BUG PFX "maxvid exceeded with pstate"
563 if (pst[j].fid > MAX_FID) {
564 printk(KERN_ERR FW_BUG PFX "maxfid exceeded with pstate"
568 if (j && (pst[j].fid < HI_FID_TABLE_BOTTOM)) {
569 /* Only first fid is allowed to be in "low" range */
570 printk(KERN_ERR FW_BUG PFX "two low fids - %d : "
571 "0x%x\n", j, pst[j].fid);
574 if (pst[j].fid < lastfid)
575 lastfid = pst[j].fid;
578 printk(KERN_ERR FW_BUG PFX "lastfid invalid\n");
581 if (lastfid > LO_FID_TABLE_TOP)
582 printk(KERN_INFO FW_BUG PFX "first fid not from lo freq table\n");
587 static void print_basics(struct powernow_k8_data *data)
590 for (j = 0; j < data->numps; j++) {
591 if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID) {
592 if (cpu_family == CPU_HW_PSTATE) {
593 printk(KERN_INFO PFX " %d : pstate %d (%d MHz)\n",
595 data->powernow_table[j].index,
596 data->powernow_table[j].frequency/1000);
598 printk(KERN_INFO PFX " %d : fid 0x%x (%d MHz), vid 0x%x\n",
600 data->powernow_table[j].index & 0xff,
601 data->powernow_table[j].frequency/1000,
602 data->powernow_table[j].index >> 8);
607 printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
610 static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
612 struct cpufreq_frequency_table *powernow_table;
615 if (data->batps) { /* use ACPI support to get full speed on mains power */
616 printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
617 data->numps = data->batps;
620 for ( j=1; j<data->numps; j++ ) {
621 if (pst[j-1].fid >= pst[j].fid) {
622 printk(KERN_ERR PFX "PST out of sequence\n");
627 if (data->numps < 2) {
628 printk(KERN_ERR PFX "no p states to transition\n");
632 if (check_pst_table(data, pst, maxvid))
635 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
636 * (data->numps + 1)), GFP_KERNEL);
637 if (!powernow_table) {
638 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
642 for (j = 0; j < data->numps; j++) {
643 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
644 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
645 powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
647 powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
648 powernow_table[data->numps].index = 0;
650 if (query_current_values_with_pending_wait(data)) {
651 kfree(powernow_table);
655 dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
656 data->powernow_table = powernow_table;
657 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
660 for (j = 0; j < data->numps; j++)
661 if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
664 dprintk("currfid/vid do not match PST, ignoring\n");
668 /* Find and validate the PSB/PST table in BIOS. */
669 static int find_psb_table(struct powernow_k8_data *data)
678 for (i = 0xc0000; i < 0xffff0; i += 0x10) {
679 /* Scan BIOS looking for the signature. */
680 /* It can not be at ffff0 - it is too big. */
682 psb = phys_to_virt(i);
683 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
686 dprintk("found PSB header at 0x%p\n", psb);
688 dprintk("table vers: 0x%x\n", psb->tableversion);
689 if (psb->tableversion != PSB_VERSION_1_4) {
690 printk(KERN_ERR FW_BUG PFX "PSB table is not v1.4\n");
694 dprintk("flags: 0x%x\n", psb->flags1);
696 printk(KERN_ERR FW_BUG PFX "unknown flags\n");
700 data->vstable = psb->vstable;
701 dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
703 dprintk("flags2: 0x%x\n", psb->flags2);
704 data->rvo = psb->flags2 & 3;
705 data->irt = ((psb->flags2) >> 2) & 3;
706 mvs = ((psb->flags2) >> 4) & 3;
707 data->vidmvs = 1 << mvs;
708 data->batps = ((psb->flags2) >> 6) & 3;
710 dprintk("ramp voltage offset: %d\n", data->rvo);
711 dprintk("isochronous relief time: %d\n", data->irt);
712 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
714 dprintk("numpst: 0x%x\n", psb->num_tables);
715 cpst = psb->num_tables;
716 if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
717 thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
718 if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
723 printk(KERN_ERR FW_BUG PFX "numpst must be 1\n");
727 data->plllock = psb->plllocktime;
728 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
729 dprintk("maxfid: 0x%x\n", psb->maxfid);
730 dprintk("maxvid: 0x%x\n", psb->maxvid);
731 maxvid = psb->maxvid;
733 data->numps = psb->numps;
734 dprintk("numpstates: 0x%x\n", data->numps);
735 return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
738 * If you see this message, complain to BIOS manufacturer. If
739 * he tells you "we do not support Linux" or some similar
740 * nonsense, remember that Windows 2000 uses the same legacy
741 * mechanism that the old Linux PSB driver uses. Tell them it
742 * is broken with Windows 2000.
744 * The reference to the AMD documentation is chapter 9 in the
745 * BIOS and Kernel Developer's Guide, which is available on
748 printk(KERN_ERR PFX "BIOS error - no PSB or ACPI _PSS objects\n");
752 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
753 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
755 if (!data->acpi_data.state_count || (cpu_family == CPU_HW_PSTATE))
758 data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
759 data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
760 data->exttype = (data->acpi_data.states[index].control >> EXT_TYPE_SHIFT) & EXT_TYPE_MASK;
761 data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
762 data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
763 data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
766 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
768 struct cpufreq_frequency_table *powernow_table;
769 int ret_val = -ENODEV;
771 if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
772 dprintk("register performance failed: bad ACPI data\n");
776 /* verify the data contained in the ACPI structures */
777 if (data->acpi_data.state_count <= 1) {
778 dprintk("No ACPI P-States\n");
782 if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
783 (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
784 dprintk("Invalid control/status registers (%x - %x)\n",
785 data->acpi_data.control_register.space_id,
786 data->acpi_data.status_register.space_id);
790 /* fill in data->powernow_table */
791 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
792 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
793 if (!powernow_table) {
794 dprintk("powernow_table memory alloc failure\n");
798 if (cpu_family == CPU_HW_PSTATE)
799 ret_val = fill_powernow_table_pstate(data, powernow_table);
801 ret_val = fill_powernow_table_fidvid(data, powernow_table);
805 powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
806 powernow_table[data->acpi_data.state_count].index = 0;
807 data->powernow_table = powernow_table;
810 data->numps = data->acpi_data.state_count;
811 if (first_cpu(per_cpu(cpu_core_map, data->cpu)) == data->cpu)
813 powernow_k8_acpi_pst_values(data, 0);
815 /* notify BIOS that we exist */
816 acpi_processor_notify_smm(THIS_MODULE);
818 if (!alloc_cpumask_var(&data->acpi_data.shared_cpu_map, GFP_KERNEL)) {
820 "unable to alloc powernow_k8_data cpumask\n");
828 kfree(powernow_table);
831 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
833 /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
834 data->acpi_data.state_count = 0;
839 static int fill_powernow_table_pstate(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
843 rdmsr(MSR_PSTATE_CUR_LIMIT, hi, lo);
844 data->max_hw_pstate = (hi & HW_PSTATE_MAX_MASK) >> HW_PSTATE_MAX_SHIFT;
846 for (i = 0; i < data->acpi_data.state_count; i++) {
849 index = data->acpi_data.states[i].control & HW_PSTATE_MASK;
850 if (index > data->max_hw_pstate) {
851 printk(KERN_ERR PFX "invalid pstate %d - bad value %d.\n", i, index);
852 printk(KERN_ERR PFX "Please report to BIOS manufacturer\n");
853 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
856 rdmsr(MSR_PSTATE_DEF_BASE + index, lo, hi);
857 if (!(hi & HW_PSTATE_VALID_MASK)) {
858 dprintk("invalid pstate %d, ignoring\n", index);
859 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
863 powernow_table[i].index = index;
865 powernow_table[i].frequency = data->acpi_data.states[i].core_frequency * 1000;
870 static int fill_powernow_table_fidvid(struct powernow_k8_data *data, struct cpufreq_frequency_table *powernow_table)
874 for (i = 0; i < data->acpi_data.state_count; i++) {
879 fid = data->acpi_data.states[i].status & EXT_FID_MASK;
880 vid = (data->acpi_data.states[i].status >> VID_SHIFT) & EXT_VID_MASK;
882 fid = data->acpi_data.states[i].control & FID_MASK;
883 vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
886 dprintk(" %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
888 powernow_table[i].index = fid; /* lower 8 bits */
889 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
890 powernow_table[i].frequency = find_khz_freq_from_fid(fid);
892 /* verify frequency is OK */
893 if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
894 (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
895 dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
896 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
900 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
901 if (vid == VID_OFF) {
902 dprintk("invalid vid %u, ignoring\n", vid);
903 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
907 /* verify only 1 entry from the lo frequency table */
908 if (fid < HI_FID_TABLE_BOTTOM) {
910 /* if both entries are the same, ignore this one ... */
911 if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
912 (powernow_table[i].index != powernow_table[cntlofreq].index)) {
913 printk(KERN_ERR PFX "Too many lo freq table entries\n");
917 dprintk("double low frequency table entry, ignoring it.\n");
918 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
924 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
925 printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
926 powernow_table[i].frequency,
927 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
928 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
935 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
937 if (data->acpi_data.state_count)
938 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
939 free_cpumask_var(data->acpi_data.shared_cpu_map);
943 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
944 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
945 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
946 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
948 /* Take a frequency, and issue the fid/vid transition command */
949 static int transition_frequency_fidvid(struct powernow_k8_data *data, unsigned int index)
954 struct cpufreq_freqs freqs;
956 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
958 /* fid/vid correctness check for k8 */
959 /* fid are the lower 8 bits of the index we stored into
960 * the cpufreq frequency table in find_psb_table, vid
961 * are the upper 8 bits.
963 fid = data->powernow_table[index].index & 0xFF;
964 vid = (data->powernow_table[index].index & 0xFF00) >> 8;
966 dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
968 if (query_current_values_with_pending_wait(data))
971 if ((data->currvid == vid) && (data->currfid == fid)) {
972 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
977 if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
979 "ignoring illegal change in lo freq table-%x to 0x%x\n",
984 dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
985 smp_processor_id(), fid, vid);
986 freqs.old = find_khz_freq_from_fid(data->currfid);
987 freqs.new = find_khz_freq_from_fid(fid);
989 for_each_cpu_mask_nr(i, *(data->available_cores)) {
991 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
994 res = transition_fid_vid(data, fid, vid);
995 freqs.new = find_khz_freq_from_fid(data->currfid);
997 for_each_cpu_mask_nr(i, *(data->available_cores)) {
999 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1004 /* Take a frequency, and issue the hardware pstate transition command */
1005 static int transition_frequency_pstate(struct powernow_k8_data *data, unsigned int index)
1009 struct cpufreq_freqs freqs;
1011 dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
1013 /* get MSR index for hardware pstate transition */
1014 pstate = index & HW_PSTATE_MASK;
1015 if (pstate > data->max_hw_pstate)
1017 freqs.old = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
1018 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1020 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1022 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1025 res = transition_pstate(data, pstate);
1026 freqs.new = find_khz_freq_from_pstate(data->powernow_table, pstate);
1028 for_each_cpu_mask_nr(i, *(data->available_cores)) {
1030 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1035 /* Driver entry point to switch to the target frequency */
1036 static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
1039 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1042 unsigned int newstate;
1048 checkfid = data->currfid;
1049 checkvid = data->currvid;
1051 /* only run on specific CPU from here on */
1052 oldmask = current->cpus_allowed;
1053 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1055 if (smp_processor_id() != pol->cpu) {
1056 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1060 if (pending_bit_stuck()) {
1061 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
1065 dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
1066 pol->cpu, targfreq, pol->min, pol->max, relation);
1068 if (query_current_values_with_pending_wait(data))
1071 if (cpu_family != CPU_HW_PSTATE) {
1072 dprintk("targ: curr fid 0x%x, vid 0x%x\n",
1073 data->currfid, data->currvid);
1075 if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
1076 printk(KERN_INFO PFX
1077 "error - out of sync, fix 0x%x 0x%x, vid 0x%x 0x%x\n",
1078 checkfid, data->currfid, checkvid, data->currvid);
1082 if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
1085 mutex_lock(&fidvid_mutex);
1087 powernow_k8_acpi_pst_values(data, newstate);
1089 if (cpu_family == CPU_HW_PSTATE)
1090 ret = transition_frequency_pstate(data, newstate);
1092 ret = transition_frequency_fidvid(data, newstate);
1094 printk(KERN_ERR PFX "transition frequency failed\n");
1096 mutex_unlock(&fidvid_mutex);
1099 mutex_unlock(&fidvid_mutex);
1101 if (cpu_family == CPU_HW_PSTATE)
1102 pol->cur = find_khz_freq_from_pstate(data->powernow_table, newstate);
1104 pol->cur = find_khz_freq_from_fid(data->currfid);
1108 set_cpus_allowed_ptr(current, &oldmask);
1112 /* Driver entry point to verify the policy and range of frequencies */
1113 static int powernowk8_verify(struct cpufreq_policy *pol)
1115 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1120 return cpufreq_frequency_table_verify(pol, data->powernow_table);
1123 /* per CPU init entry point to the driver */
1124 static int __cpuinit powernowk8_cpu_init(struct cpufreq_policy *pol)
1126 struct powernow_k8_data *data;
1130 if (!cpu_online(pol->cpu))
1133 if (!check_supported_cpu(pol->cpu))
1136 data = kzalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
1138 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
1142 data->cpu = pol->cpu;
1143 data->currpstate = HW_PSTATE_INVALID;
1145 rc = powernow_k8_cpu_init_acpi(data);
1148 * Use the PSB BIOS structure. This is only availabe on
1149 * an UP version, and is deprecated by AMD.
1151 if (num_online_cpus() != 1) {
1152 #ifndef CONFIG_ACPI_PROCESSOR
1153 printk(KERN_ERR PFX "ACPI Processor support is required "
1154 "for SMP systems but is absent. Please load the "
1155 "ACPI Processor module before starting this "
1158 printk(KERN_ERR FW_BUG PFX "Your BIOS does not provide"
1159 " ACPI _PSS objects in a way that Linux "
1160 "understands. Please report this to the Linux "
1161 "ACPI maintainers and complain to your BIOS "
1166 if (pol->cpu != 0) {
1167 printk(KERN_ERR FW_BUG PFX "No ACPI _PSS objects for "
1168 "CPU other than CPU0. Complain to your BIOS "
1172 rc = find_psb_table(data);
1178 /* only run on specific CPU from here on */
1179 oldmask = current->cpus_allowed;
1180 set_cpus_allowed_ptr(current, &cpumask_of_cpu(pol->cpu));
1182 if (smp_processor_id() != pol->cpu) {
1183 printk(KERN_ERR PFX "limiting to cpu %u failed\n", pol->cpu);
1187 if (pending_bit_stuck()) {
1188 printk(KERN_ERR PFX "failing init, change pending bit set\n");
1192 if (query_current_values_with_pending_wait(data))
1195 if (cpu_family == CPU_OPTERON)
1198 /* run on any CPU again */
1199 set_cpus_allowed_ptr(current, &oldmask);
1201 if (cpu_family == CPU_HW_PSTATE)
1202 pol->cpus = cpumask_of_cpu(pol->cpu);
1204 pol->cpus = per_cpu(cpu_core_map, pol->cpu);
1205 data->available_cores = &(pol->cpus);
1207 /* Take a crude guess here.
1208 * That guess was in microseconds, so multiply with 1000 */
1209 pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
1210 + (3 * (1 << data->irt) * 10)) * 1000;
1212 if (cpu_family == CPU_HW_PSTATE)
1213 pol->cur = find_khz_freq_from_pstate(data->powernow_table, data->currpstate);
1215 pol->cur = find_khz_freq_from_fid(data->currfid);
1216 dprintk("policy current frequency %d kHz\n", pol->cur);
1218 /* min/max the cpu is capable of */
1219 if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1220 printk(KERN_ERR FW_BUG PFX "invalid powernow_table\n");
1221 powernow_k8_cpu_exit_acpi(data);
1222 kfree(data->powernow_table);
1227 cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1229 if (cpu_family == CPU_HW_PSTATE)
1230 dprintk("cpu_init done, current pstate 0x%x\n", data->currpstate);
1232 dprintk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1233 data->currfid, data->currvid);
1235 per_cpu(powernow_data, pol->cpu) = data;
1240 set_cpus_allowed_ptr(current, &oldmask);
1241 powernow_k8_cpu_exit_acpi(data);
1247 static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
1249 struct powernow_k8_data *data = per_cpu(powernow_data, pol->cpu);
1254 powernow_k8_cpu_exit_acpi(data);
1256 cpufreq_frequency_table_put_attr(pol->cpu);
1258 kfree(data->powernow_table);
1264 static unsigned int powernowk8_get (unsigned int cpu)
1266 struct powernow_k8_data *data;
1267 cpumask_t oldmask = current->cpus_allowed;
1268 unsigned int khz = 0;
1271 first = first_cpu(per_cpu(cpu_core_map, cpu));
1272 data = per_cpu(powernow_data, first);
1277 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
1278 if (smp_processor_id() != cpu) {
1280 "limiting to CPU %d failed in powernowk8_get\n", cpu);
1281 set_cpus_allowed_ptr(current, &oldmask);
1285 if (query_current_values_with_pending_wait(data))
1288 if (cpu_family == CPU_HW_PSTATE)
1289 khz = find_khz_freq_from_pstate(data->powernow_table,
1292 khz = find_khz_freq_from_fid(data->currfid);
1296 set_cpus_allowed_ptr(current, &oldmask);
1300 static struct freq_attr* powernow_k8_attr[] = {
1301 &cpufreq_freq_attr_scaling_available_freqs,
1305 static struct cpufreq_driver cpufreq_amd64_driver = {
1306 .verify = powernowk8_verify,
1307 .target = powernowk8_target,
1308 .init = powernowk8_cpu_init,
1309 .exit = __devexit_p(powernowk8_cpu_exit),
1310 .get = powernowk8_get,
1311 .name = "powernow-k8",
1312 .owner = THIS_MODULE,
1313 .attr = powernow_k8_attr,
1316 /* driver entry point for init */
1317 static int __cpuinit powernowk8_init(void)
1319 unsigned int i, supported_cpus = 0;
1321 for_each_online_cpu(i) {
1322 if (check_supported_cpu(i))
1326 if (supported_cpus == num_online_cpus()) {
1327 printk(KERN_INFO PFX "Found %d %s "
1328 "processors (%d cpu cores) (" VERSION ")\n",
1330 boot_cpu_data.x86_model_id, supported_cpus);
1331 return cpufreq_register_driver(&cpufreq_amd64_driver);
1337 /* driver entry point for term */
1338 static void __exit powernowk8_exit(void)
1342 cpufreq_unregister_driver(&cpufreq_amd64_driver);
1345 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com> and Mark Langsdorf <mark.langsdorf@amd.com>");
1346 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1347 MODULE_LICENSE("GPL");
1349 late_initcall(powernowk8_init);
1350 module_exit(powernowk8_exit);