2 * AMD K7 Powernow driver.
3 * (C) 2003 Dave Jones <davej@codemonkey.org.uk> 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.
9 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
11 * Errata 5: Processor may fail to execute a FID/VID change in presence of interrupt.
12 * - We cli/sti on stepping A0 CPUs around the FID/VID transition.
13 * Errata 15: Processors 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/config.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/init.h>
22 #include <linux/cpufreq.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
27 #include <asm/timex.h>
29 #include <asm/system.h>
31 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
32 #include <linux/acpi.h>
33 #include <acpi/processor.h>
36 #include "powernow-k7.h"
41 #define dprintk(msg...) printk(msg)
43 #define dprintk(msg...) do { } while(0)
46 #define PFX "powernow: "
66 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
67 union powernow_acpi_control_t {
78 /* divide by 1000 to get VID. */
79 static int mobile_vid_table[32] = {
80 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
81 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
82 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
83 1075, 1050, 1024, 1000, 975, 950, 925, 0,
86 /* divide by 10 to get FID. */
87 static int fid_codes[32] = {
88 110, 115, 120, 125, 50, 55, 60, 65,
89 70, 75, 80, 85, 90, 95, 100, 105,
90 30, 190, 40, 200, 130, 135, 140, 210,
91 150, 225, 160, 165, 170, 180, -1, -1,
94 /* This parameter is used in order to force ACPI instead of legacy method for
95 * configuration purpose.
98 static int acpi_force;
100 static struct cpufreq_frequency_table *powernow_table;
102 static unsigned int can_scale_bus;
103 static unsigned int can_scale_vid;
104 static unsigned int minimum_speed=-1;
105 static unsigned int maximum_speed;
106 static unsigned int number_scales;
107 static unsigned int fsb;
108 static unsigned int latency;
111 static int check_fsb(unsigned int fsbspeed)
114 unsigned int f = fsb / 1000;
116 delta = (fsbspeed > f) ? fsbspeed - f : f - fsbspeed;
120 static int check_powernow(void)
122 struct cpuinfo_x86 *c = cpu_data;
123 unsigned int maxei, eax, ebx, ecx, edx;
125 if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 !=6)) {
127 printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
132 /* Get maximum capabilities */
133 maxei = cpuid_eax (0x80000000);
134 if (maxei < 0x80000007) { /* Any powernow info ? */
136 printk (KERN_INFO PFX "No powernow capabilities detected\n");
141 if ((c->x86_model == 6) && (c->x86_mask == 0)) {
142 printk (KERN_INFO PFX "K7 660[A0] core detected, 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: ");
155 printk ("frequency");
159 if ((edx & (1 << 1 | 1 << 2)) == 0x6)
172 static int get_ranges (unsigned char *pst)
178 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
181 memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
183 for (j=0 ; j < number_scales; j++) {
186 powernow_table[j].frequency = (fsb * fid_codes[fid]) / 10;
187 powernow_table[j].index = fid; /* lower 8 bits */
189 speed = powernow_table[j].frequency;
191 if ((fid_codes[fid] % 10)==5) {
192 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
194 powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
198 dprintk (KERN_INFO PFX " FID: 0x%x (%d.%dx [%dMHz])\t", fid,
199 fid_codes[fid] / 10, fid_codes[fid] % 10, speed/1000);
201 if (speed < minimum_speed)
202 minimum_speed = speed;
203 if (speed > maximum_speed)
204 maximum_speed = speed;
207 powernow_table[j].index |= (vid << 8); /* upper 8 bits */
208 dprintk ("VID: 0x%x (%d.%03dV)\n", vid, mobile_vid_table[vid]/1000,
209 mobile_vid_table[vid]%1000);
211 powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
212 powernow_table[number_scales].index = 0;
218 static void change_FID(int fid)
220 union msr_fidvidctl fidvidctl;
222 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
223 if (fidvidctl.bits.FID != fid) {
224 fidvidctl.bits.SGTC = latency;
225 fidvidctl.bits.FID = fid;
226 fidvidctl.bits.VIDC = 0;
227 fidvidctl.bits.FIDC = 1;
228 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
233 static void change_VID(int vid)
235 union msr_fidvidctl fidvidctl;
237 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
238 if (fidvidctl.bits.VID != vid) {
239 fidvidctl.bits.SGTC = latency;
240 fidvidctl.bits.VID = vid;
241 fidvidctl.bits.FIDC = 0;
242 fidvidctl.bits.VIDC = 1;
243 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
248 static void change_speed (unsigned int index)
251 struct cpufreq_freqs freqs;
252 union msr_fidvidstatus fidvidstatus;
255 /* fid are the lower 8 bits of the index we stored into
256 * the cpufreq frequency table in powernow_decode_bios,
257 * vid are the upper 8 bits.
260 fid = powernow_table[index].index & 0xFF;
261 vid = (powernow_table[index].index & 0xFF00) >> 8;
265 rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
266 cfid = fidvidstatus.bits.CFID;
267 freqs.old = fsb * fid_codes[cfid] / 10;
269 freqs.new = powernow_table[index].frequency;
271 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
273 /* Now do the magic poking into the MSRs. */
275 if (have_a0 == 1) /* A0 errata 5 */
278 if (freqs.old > freqs.new) {
279 /* Going down, so change FID first */
283 /* Going up, so change VID first */
292 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
296 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
298 struct acpi_processor_performance *acpi_processor_perf;
300 static int powernow_acpi_init(void)
304 union powernow_acpi_control_t pc;
306 if (acpi_processor_perf != NULL && powernow_table != NULL) {
311 acpi_processor_perf = kmalloc(sizeof(struct acpi_processor_performance),
314 if (!acpi_processor_perf) {
319 memset(acpi_processor_perf, 0, sizeof(struct acpi_processor_performance));
321 if (acpi_processor_register_performance(acpi_processor_perf, 0)) {
326 if (acpi_processor_perf->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
331 if (acpi_processor_perf->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
336 number_scales = acpi_processor_perf->state_count;
338 if (number_scales < 2) {
343 powernow_table = kmalloc((number_scales + 1) * (sizeof(struct cpufreq_frequency_table)), GFP_KERNEL);
344 if (!powernow_table) {
349 memset(powernow_table, 0, ((number_scales + 1) * sizeof(struct cpufreq_frequency_table)));
351 pc.val = (unsigned long) acpi_processor_perf->states[0].control;
352 for (i = 0; i < number_scales; i++) {
356 pc.val = (unsigned long) acpi_processor_perf->states[i].control;
357 dprintk (KERN_INFO PFX "acpi: P%d: %d MHz %d mW %d uS control %08x SGTC %d\n",
359 (u32) acpi_processor_perf->states[i].core_frequency,
360 (u32) acpi_processor_perf->states[i].power,
361 (u32) acpi_processor_perf->states[i].transition_latency,
362 (u32) acpi_processor_perf->states[i].control,
368 powernow_table[i].frequency = fsb * fid_codes[fid] / 10;
369 powernow_table[i].index = fid; /* lower 8 bits */
370 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
372 speed = powernow_table[i].frequency;
374 if ((fid_codes[fid] % 10)==5) {
376 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
379 dprintk (KERN_INFO PFX " FID: 0x%x (%d.%dx [%dMHz])\t", fid,
380 fid_codes[fid] / 10, fid_codes[fid] % 10, speed/1000);
381 dprintk ("VID: 0x%x (%d.%03dV)\n", vid, mobile_vid_table[vid]/1000,
382 mobile_vid_table[vid]%1000);
384 if (latency < pc.bits.sgtc)
385 latency = pc.bits.sgtc;
387 if (speed < minimum_speed)
388 minimum_speed = speed;
389 if (speed > maximum_speed)
390 maximum_speed = speed;
393 powernow_table[i].frequency = CPUFREQ_TABLE_END;
394 powernow_table[i].index = 0;
399 acpi_processor_unregister_performance(acpi_processor_perf, 0);
401 kfree(acpi_processor_perf);
403 printk(KERN_WARNING PFX "ACPI perflib can not be used in this platform\n");
404 acpi_processor_perf = NULL;
408 static int powernow_acpi_init(void)
410 printk(KERN_INFO PFX "no support for ACPI processor found."
411 " Please recompile your kernel with ACPI processor\n");
416 static int powernow_decode_bios (int maxfid, int startvid)
425 etuple = cpuid_eax(0x80000001);
427 for (i=0xC0000; i < 0xffff0 ; i+=16) {
431 if (memcmp(p, "AMDK7PNOW!", 10) == 0){
432 dprintk (KERN_INFO PFX "Found PSB header at %p\n", p);
433 psb = (struct psb_s *) p;
434 dprintk (KERN_INFO PFX "Table version: 0x%x\n", psb->tableversion);
435 if (psb->tableversion != 0x12) {
436 printk (KERN_INFO PFX "Sorry, only v1.2 tables supported right now\n");
440 dprintk (KERN_INFO PFX "Flags: 0x%x (", psb->flags);
441 if ((psb->flags & 1)==0) {
446 dprintk (" voltage regulator)\n");
448 latency = psb->settlingtime;
450 printk (KERN_INFO PFX "BIOS set settling time to %d microseconds."
451 "Should be at least 100. Correcting.\n", latency);
454 dprintk (KERN_INFO PFX "Settling Time: %d microseconds.\n", psb->settlingtime);
455 dprintk (KERN_INFO PFX "Has %d PST tables. (Only dumping ones relevant to this CPU).\n", psb->numpst);
457 p += sizeof (struct psb_s);
459 pst = (struct pst_s *) p;
461 for (i = 0 ; i <psb->numpst; i++) {
462 pst = (struct pst_s *) p;
463 number_scales = pst->numpstates;
465 if ((etuple == pst->cpuid) && check_fsb(pst->fsbspeed) &&
466 (maxfid==pst->maxfid) && (startvid==pst->startvid))
468 dprintk (KERN_INFO PFX "PST:%d (@%p)\n", i, pst);
469 dprintk (KERN_INFO PFX " cpuid: 0x%x\t", pst->cpuid);
470 dprintk ("fsb: %d\t", pst->fsbspeed);
471 dprintk ("maxFID: 0x%x\t", pst->maxfid);
472 dprintk ("startvid: 0x%x\n", pst->startvid);
474 ret = get_ranges ((char *) pst + sizeof (struct pst_s));
478 p = (char *) pst + sizeof (struct pst_s);
479 for (j=0 ; j < number_scales; j++)
483 printk (KERN_INFO PFX "No PST tables match this cpuid (0x%x)\n", etuple);
484 printk (KERN_INFO PFX "This is indicative of a broken BIOS.\n");
495 static int powernow_target (struct cpufreq_policy *policy,
496 unsigned int target_freq,
497 unsigned int relation)
499 unsigned int newstate;
501 if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
504 change_speed(newstate);
510 static int powernow_verify (struct cpufreq_policy *policy)
512 return cpufreq_frequency_table_verify(policy, powernow_table);
516 * We use the fact that the bus frequency is somehow
517 * a multiple of 100000/3 khz, then we compute sgtc according
519 * That way, we match more how AMD thinks all of that work.
520 * We will then get the same kind of behaviour already tested under
521 * the "well-known" other OS.
523 static int __init fixup_sgtc(void)
534 sgtc = 100 * m * latency;
536 if (sgtc > 0xfffff) {
537 printk(KERN_WARNING PFX "SGTC too large %d\n", sgtc);
543 static unsigned int powernow_get(unsigned int cpu)
545 union msr_fidvidstatus fidvidstatus;
550 rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
551 cfid = fidvidstatus.bits.CFID;
553 return (fsb * fid_codes[cfid] / 10);
557 static int __init powernow_cpu_init (struct cpufreq_policy *policy)
559 union msr_fidvidstatus fidvidstatus;
562 if (policy->cpu != 0)
565 rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
567 /* A K7 with powernow technology is set to max frequency by BIOS */
568 fsb = (10 * cpu_khz) / fid_codes[fidvidstatus.bits.CFID];
570 printk(KERN_WARNING PFX "can not determine bus frequency\n");
573 dprintk(KERN_INFO PFX "FSB: %3d.%03d MHz\n", fsb/1000, fsb%1000);
575 if ((dmi_broken & BROKEN_CPUFREQ) || acpi_force) {
576 printk (KERN_INFO PFX "PSB/PST known to be broken. Trying ACPI instead\n");
577 result = powernow_acpi_init();
579 result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
581 printk (KERN_INFO PFX "Trying ACPI perflib\n");
585 result = powernow_acpi_init();
587 printk (KERN_INFO PFX "ACPI and legacy methods failed\n");
588 printk (KERN_INFO PFX "See http://www.codemonkey.org.uk/projects/cpufreq/powernow-k7.shtml\n");
591 /* SGTC use the bus clock as timer */
592 latency = fixup_sgtc();
593 printk(KERN_INFO PFX "SGTC: %d\n", latency);
600 printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
601 minimum_speed/1000, maximum_speed/1000);
603 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
605 policy->cpuinfo.transition_latency = 20 * latency / fsb;
607 policy->cur = powernow_get(0);
609 cpufreq_frequency_table_get_attr(powernow_table, policy->cpu);
611 return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
614 static int powernow_cpu_exit (struct cpufreq_policy *policy) {
615 cpufreq_frequency_table_put_attr(policy->cpu);
619 static struct freq_attr* powernow_table_attr[] = {
620 &cpufreq_freq_attr_scaling_available_freqs,
624 static struct cpufreq_driver powernow_driver = {
625 .verify = powernow_verify,
626 .target = powernow_target,
628 .init = powernow_cpu_init,
629 .exit = powernow_cpu_exit,
630 .name = "powernow-k7",
631 .owner = THIS_MODULE,
632 .attr = powernow_table_attr,
635 static int __init powernow_init (void)
637 if (check_powernow()==0)
639 return cpufreq_register_driver(&powernow_driver);
643 static void __exit powernow_exit (void)
645 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
646 if (acpi_processor_perf) {
647 acpi_processor_unregister_performance(acpi_processor_perf, 0);
648 kfree(acpi_processor_perf);
651 cpufreq_unregister_driver(&powernow_driver);
653 kfree(powernow_table);
656 module_param(acpi_force, int, 0444);
657 MODULE_PARM_DESC(acpi_force, "Force ACPI to be used");
659 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
660 MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
661 MODULE_LICENSE ("GPL");
663 late_initcall(powernow_init);
664 module_exit(powernow_exit);