2 * AMD K7 Powernow driver.
3 * (C) 2003 Dave Jones on behalf of SuSE Labs.
4 * (C) 2003-2004 Dave Jones <davej@redhat.com>
6 * Licensed under the terms of the GNU GPL License version 2.
7 * Based upon datasheets & sample CPUs kindly provided by AMD.
10 * CPU may fail to execute a FID/VID change in presence of interrupt.
11 * - We cli/sti on stepping A0 CPUs around the FID/VID transition.
13 * CPU with half frequency multipliers may hang upon wakeup from disconnect.
14 * - We disable half multipliers if ACPI is used on A0 stepping CPUs.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/cpufreq.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/dmi.h>
25 #include <linux/timex.h>
28 #include <asm/timer.h> /* Needed for recalibrate_cpu_khz() */
30 #include <asm/system.h>
32 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
33 #include <linux/acpi.h>
34 #include <acpi/processor.h>
37 #include "powernow-k7.h"
39 #define PFX "powernow: "
59 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
60 union powernow_acpi_control_t {
71 #ifdef CONFIG_CPU_FREQ_DEBUG
72 /* divide by 1000 to get VCore voltage in V. */
73 static const int mobile_vid_table[32] = {
74 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
75 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
76 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
77 1075, 1050, 1025, 1000, 975, 950, 925, 0,
81 /* divide by 10 to get FID. */
82 static const int fid_codes[32] = {
83 110, 115, 120, 125, 50, 55, 60, 65,
84 70, 75, 80, 85, 90, 95, 100, 105,
85 30, 190, 40, 200, 130, 135, 140, 210,
86 150, 225, 160, 165, 170, 180, -1, -1,
89 /* This parameter is used in order to force ACPI instead of legacy method for
90 * configuration purpose.
93 static int acpi_force;
95 static struct cpufreq_frequency_table *powernow_table;
97 static unsigned int can_scale_bus;
98 static unsigned int can_scale_vid;
99 static unsigned int minimum_speed = -1;
100 static unsigned int maximum_speed;
101 static unsigned int number_scales;
102 static unsigned int fsb;
103 static unsigned int latency;
106 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
109 static int check_fsb(unsigned int fsbspeed)
112 unsigned int f = fsb / 1000;
114 delta = (fsbspeed > f) ? fsbspeed - f : f - fsbspeed;
118 static int check_powernow(void)
120 struct cpuinfo_x86 *c = &cpu_data(0);
121 unsigned int maxei, eax, ebx, ecx, edx;
123 if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 6)) {
125 printk(KERN_INFO PFX "This module only works with "
131 /* Get maximum capabilities */
132 maxei = cpuid_eax(0x80000000);
133 if (maxei < 0x80000007) { /* Any powernow info ? */
135 printk(KERN_INFO PFX "No powernow capabilities detected\n");
140 if ((c->x86_model == 6) && (c->x86_mask == 0)) {
141 printk(KERN_INFO PFX "K7 660[A0] core detected, "
142 "enabling errata workarounds\n");
146 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
148 /* Check we can actually do something before we say anything.*/
149 if (!(edx & (1 << 1 | 1 << 2)))
152 printk(KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
159 if ((edx & (1 << 1 | 1 << 2)) == 0x6)
171 static void invalidate_entry(unsigned int entry)
173 powernow_table[entry].frequency = CPUFREQ_ENTRY_INVALID;
176 static int get_ranges(unsigned char *pst)
182 powernow_table = kzalloc((sizeof(struct cpufreq_frequency_table) *
183 (number_scales + 1)), GFP_KERNEL);
187 for (j = 0 ; j < number_scales; j++) {
190 powernow_table[j].frequency = (fsb * fid_codes[fid]) / 10;
191 powernow_table[j].index = fid; /* lower 8 bits */
193 speed = powernow_table[j].frequency;
195 if ((fid_codes[fid] % 10) == 5) {
196 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
202 if (speed < minimum_speed)
203 minimum_speed = speed;
204 if (speed > maximum_speed)
205 maximum_speed = speed;
208 powernow_table[j].index |= (vid << 8); /* upper 8 bits */
210 dprintk(" FID: 0x%x (%d.%dx [%dMHz]) "
211 "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10,
212 fid_codes[fid] % 10, speed/1000, vid,
213 mobile_vid_table[vid]/1000,
214 mobile_vid_table[vid]%1000);
216 powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
217 powernow_table[number_scales].index = 0;
223 static void change_FID(int fid)
225 union msr_fidvidctl fidvidctl;
227 rdmsrl(MSR_K7_FID_VID_CTL, fidvidctl.val);
228 if (fidvidctl.bits.FID != fid) {
229 fidvidctl.bits.SGTC = latency;
230 fidvidctl.bits.FID = fid;
231 fidvidctl.bits.VIDC = 0;
232 fidvidctl.bits.FIDC = 1;
233 wrmsrl(MSR_K7_FID_VID_CTL, fidvidctl.val);
238 static void change_VID(int vid)
240 union msr_fidvidctl fidvidctl;
242 rdmsrl(MSR_K7_FID_VID_CTL, fidvidctl.val);
243 if (fidvidctl.bits.VID != vid) {
244 fidvidctl.bits.SGTC = latency;
245 fidvidctl.bits.VID = vid;
246 fidvidctl.bits.FIDC = 0;
247 fidvidctl.bits.VIDC = 1;
248 wrmsrl(MSR_K7_FID_VID_CTL, fidvidctl.val);
253 static void change_speed(unsigned int index)
256 struct cpufreq_freqs freqs;
257 union msr_fidvidstatus fidvidstatus;
260 /* fid are the lower 8 bits of the index we stored into
261 * the cpufreq frequency table in powernow_decode_bios,
262 * vid are the upper 8 bits.
265 fid = powernow_table[index].index & 0xFF;
266 vid = (powernow_table[index].index & 0xFF00) >> 8;
270 rdmsrl(MSR_K7_FID_VID_STATUS, fidvidstatus.val);
271 cfid = fidvidstatus.bits.CFID;
272 freqs.old = fsb * fid_codes[cfid] / 10;
274 freqs.new = powernow_table[index].frequency;
276 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
278 /* Now do the magic poking into the MSRs. */
280 if (have_a0 == 1) /* A0 errata 5 */
283 if (freqs.old > freqs.new) {
284 /* Going down, so change FID first */
288 /* Going up, so change VID first */
297 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
301 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
303 static struct acpi_processor_performance *acpi_processor_perf;
305 static int powernow_acpi_init(void)
309 union powernow_acpi_control_t pc;
311 if (acpi_processor_perf != NULL && powernow_table != NULL) {
316 acpi_processor_perf = kzalloc(sizeof(struct acpi_processor_performance),
318 if (!acpi_processor_perf) {
323 if (!alloc_cpumask_var(&acpi_processor_perf->shared_cpu_map,
329 if (acpi_processor_register_performance(acpi_processor_perf, 0)) {
334 if (acpi_processor_perf->control_register.space_id !=
335 ACPI_ADR_SPACE_FIXED_HARDWARE) {
340 if (acpi_processor_perf->status_register.space_id !=
341 ACPI_ADR_SPACE_FIXED_HARDWARE) {
346 number_scales = acpi_processor_perf->state_count;
348 if (number_scales < 2) {
353 powernow_table = kzalloc((sizeof(struct cpufreq_frequency_table) *
354 (number_scales + 1)), GFP_KERNEL);
355 if (!powernow_table) {
360 pc.val = (unsigned long) acpi_processor_perf->states[0].control;
361 for (i = 0; i < number_scales; i++) {
363 struct acpi_processor_px *state =
364 &acpi_processor_perf->states[i];
365 unsigned int speed, speed_mhz;
367 pc.val = (unsigned long) state->control;
368 dprintk("acpi: P%d: %d MHz %d mW %d uS control %08x SGTC %d\n",
370 (u32) state->core_frequency,
372 (u32) state->transition_latency,
373 (u32) state->control,
379 powernow_table[i].frequency = fsb * fid_codes[fid] / 10;
380 powernow_table[i].index = fid; /* lower 8 bits */
381 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
383 speed = powernow_table[i].frequency;
384 speed_mhz = speed / 1000;
386 /* processor_perflib will multiply the MHz value by 1000 to
387 * get a KHz value (e.g. 1266000). However, powernow-k7 works
388 * with true KHz values (e.g. 1266768). To ensure that all
389 * powernow frequencies are available, we must ensure that
390 * ACPI doesn't restrict them, so we round up the MHz value
391 * to ensure that perflib's computed KHz value is greater than
392 * or equal to powernow's KHz value.
394 if (speed % 1000 > 0)
397 if ((fid_codes[fid] % 10) == 5) {
402 dprintk(" FID: 0x%x (%d.%dx [%dMHz]) "
403 "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10,
404 fid_codes[fid] % 10, speed_mhz, vid,
405 mobile_vid_table[vid]/1000,
406 mobile_vid_table[vid]%1000);
408 if (state->core_frequency != speed_mhz) {
409 state->core_frequency = speed_mhz;
410 dprintk(" Corrected ACPI frequency to %d\n",
414 if (latency < pc.bits.sgtc)
415 latency = pc.bits.sgtc;
417 if (speed < minimum_speed)
418 minimum_speed = speed;
419 if (speed > maximum_speed)
420 maximum_speed = speed;
423 powernow_table[i].frequency = CPUFREQ_TABLE_END;
424 powernow_table[i].index = 0;
426 /* notify BIOS that we exist */
427 acpi_processor_notify_smm(THIS_MODULE);
432 acpi_processor_unregister_performance(acpi_processor_perf, 0);
434 free_cpumask_var(acpi_processor_perf->shared_cpu_map);
436 kfree(acpi_processor_perf);
438 printk(KERN_WARNING PFX "ACPI perflib can not be used on "
440 acpi_processor_perf = NULL;
444 static int powernow_acpi_init(void)
446 printk(KERN_INFO PFX "no support for ACPI processor found."
447 " Please recompile your kernel with ACPI processor\n");
452 static void print_pst_entry(struct pst_s *pst, unsigned int j)
454 dprintk("PST:%d (@%p)\n", j, pst);
455 dprintk(" cpuid: 0x%x fsb: %d maxFID: 0x%x startvid: 0x%x\n",
456 pst->cpuid, pst->fsbspeed, pst->maxfid, pst->startvid);
459 static int powernow_decode_bios(int maxfid, int startvid)
468 etuple = cpuid_eax(0x80000001);
470 for (i = 0xC0000; i < 0xffff0 ; i += 16) {
474 if (memcmp(p, "AMDK7PNOW!", 10) == 0) {
475 dprintk("Found PSB header at %p\n", p);
476 psb = (struct psb_s *) p;
477 dprintk("Table version: 0x%x\n", psb->tableversion);
478 if (psb->tableversion != 0x12) {
479 printk(KERN_INFO PFX "Sorry, only v1.2 tables"
480 " supported right now\n");
484 dprintk("Flags: 0x%x\n", psb->flags);
485 if ((psb->flags & 1) == 0)
486 dprintk("Mobile voltage regulator\n");
488 dprintk("Desktop voltage regulator\n");
490 latency = psb->settlingtime;
492 printk(KERN_INFO PFX "BIOS set settling time "
493 "to %d microseconds. "
494 "Should be at least 100. "
495 "Correcting.\n", latency);
498 dprintk("Settling Time: %d microseconds.\n",
500 dprintk("Has %d PST tables. (Only dumping ones "
501 "relevant to this CPU).\n",
504 p += sizeof(struct psb_s);
506 pst = (struct pst_s *) p;
508 for (j = 0; j < psb->numpst; j++) {
509 pst = (struct pst_s *) p;
510 number_scales = pst->numpstates;
512 if ((etuple == pst->cpuid) &&
513 check_fsb(pst->fsbspeed) &&
514 (maxfid == pst->maxfid) &&
515 (startvid == pst->startvid)) {
516 print_pst_entry(pst, j);
517 p = (char *)pst + sizeof(struct pst_s);
522 p = (char *)pst + sizeof(struct pst_s);
523 for (k = 0; k < number_scales; k++)
527 printk(KERN_INFO PFX "No PST tables match this cpuid "
529 printk(KERN_INFO PFX "This is indicative of a broken "
541 static int powernow_target(struct cpufreq_policy *policy,
542 unsigned int target_freq,
543 unsigned int relation)
545 unsigned int newstate;
547 if (cpufreq_frequency_table_target(policy, powernow_table, target_freq,
548 relation, &newstate))
551 change_speed(newstate);
557 static int powernow_verify(struct cpufreq_policy *policy)
559 return cpufreq_frequency_table_verify(policy, powernow_table);
563 * We use the fact that the bus frequency is somehow
564 * a multiple of 100000/3 khz, then we compute sgtc according
566 * That way, we match more how AMD thinks all of that work.
567 * We will then get the same kind of behaviour already tested under
568 * the "well-known" other OS.
570 static int __init fixup_sgtc(void)
581 sgtc = 100 * m * latency;
583 if (sgtc > 0xfffff) {
584 printk(KERN_WARNING PFX "SGTC too large %d\n", sgtc);
590 static unsigned int powernow_get(unsigned int cpu)
592 union msr_fidvidstatus fidvidstatus;
597 rdmsrl(MSR_K7_FID_VID_STATUS, fidvidstatus.val);
598 cfid = fidvidstatus.bits.CFID;
600 return fsb * fid_codes[cfid] / 10;
604 static int __init acer_cpufreq_pst(const struct dmi_system_id *d)
606 printk(KERN_WARNING PFX
607 "%s laptop with broken PST tables in BIOS detected.\n",
609 printk(KERN_WARNING PFX
610 "You need to downgrade to 3A21 (09/09/2002), or try a newer "
611 "BIOS than 3A71 (01/20/2003)\n");
612 printk(KERN_WARNING PFX
613 "cpufreq scaling has been disabled as a result of this.\n");
618 * Some Athlon laptops have really fucked PST tables.
619 * A BIOS update is all that can save them.
620 * Mention this, and disable cpufreq.
622 static struct dmi_system_id __initdata powernow_dmi_table[] = {
624 .callback = acer_cpufreq_pst,
625 .ident = "Acer Aspire",
627 DMI_MATCH(DMI_SYS_VENDOR, "Insyde Software"),
628 DMI_MATCH(DMI_BIOS_VERSION, "3A71"),
634 static int __init powernow_cpu_init(struct cpufreq_policy *policy)
636 union msr_fidvidstatus fidvidstatus;
639 if (policy->cpu != 0)
642 rdmsrl(MSR_K7_FID_VID_STATUS, fidvidstatus.val);
644 recalibrate_cpu_khz();
646 fsb = (10 * cpu_khz) / fid_codes[fidvidstatus.bits.CFID];
648 printk(KERN_WARNING PFX "can not determine bus frequency\n");
651 dprintk("FSB: %3dMHz\n", fsb/1000);
653 if (dmi_check_system(powernow_dmi_table) || acpi_force) {
654 printk(KERN_INFO PFX "PSB/PST known to be broken. "
655 "Trying ACPI instead\n");
656 result = powernow_acpi_init();
658 result = powernow_decode_bios(fidvidstatus.bits.MFID,
659 fidvidstatus.bits.SVID);
661 printk(KERN_INFO PFX "Trying ACPI perflib\n");
665 result = powernow_acpi_init();
668 "ACPI and legacy methods failed\n");
671 /* SGTC use the bus clock as timer */
672 latency = fixup_sgtc();
673 printk(KERN_INFO PFX "SGTC: %d\n", latency);
680 printk(KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
681 minimum_speed/1000, maximum_speed/1000);
683 policy->cpuinfo.transition_latency =
684 cpufreq_scale(2000000UL, fsb, latency);
686 policy->cur = powernow_get(0);
688 cpufreq_frequency_table_get_attr(powernow_table, policy->cpu);
690 return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
693 static int powernow_cpu_exit(struct cpufreq_policy *policy)
695 cpufreq_frequency_table_put_attr(policy->cpu);
697 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
698 if (acpi_processor_perf) {
699 acpi_processor_unregister_performance(acpi_processor_perf, 0);
700 free_cpumask_var(acpi_processor_perf->shared_cpu_map);
701 kfree(acpi_processor_perf);
705 kfree(powernow_table);
709 static struct freq_attr *powernow_table_attr[] = {
710 &cpufreq_freq_attr_scaling_available_freqs,
714 static struct cpufreq_driver powernow_driver = {
715 .verify = powernow_verify,
716 .target = powernow_target,
718 .init = powernow_cpu_init,
719 .exit = powernow_cpu_exit,
720 .name = "powernow-k7",
721 .owner = THIS_MODULE,
722 .attr = powernow_table_attr,
725 static int __init powernow_init(void)
727 if (check_powernow() == 0)
729 return cpufreq_register_driver(&powernow_driver);
733 static void __exit powernow_exit(void)
735 cpufreq_unregister_driver(&powernow_driver);
738 module_param(acpi_force, int, 0444);
739 MODULE_PARM_DESC(acpi_force, "Force ACPI to be used.");
741 MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
742 MODULE_DESCRIPTION("Powernow driver for AMD K7 processors.");
743 MODULE_LICENSE("GPL");
745 late_initcall(powernow_init);
746 module_exit(powernow_exit);