Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / i386 / kernel / cpu / cpufreq / powernow-k7.c
1 /*
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>
5  *
6  *  Licensed under the terms of the GNU GPL License version 2.
7  *  Based upon datasheets & sample CPUs kindly provided by AMD.
8  *
9  * Errata 5: Processor may fail to execute a FID/VID change in presence of interrupt.
10  * - We cli/sti on stepping A0 CPUs around the FID/VID transition.
11  * Errata 15: Processors with half frequency multipliers may hang upon wakeup from disconnect.
12  * - We disable half multipliers if ACPI is used on A0 stepping CPUs.
13  */
14
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/cpufreq.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/dmi.h>
24
25 #include <asm/msr.h>
26 #include <asm/timex.h>
27 #include <asm/io.h>
28 #include <asm/system.h>
29
30 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
31 #include <linux/acpi.h>
32 #include <acpi/processor.h>
33 #endif
34
35 #include "powernow-k7.h"
36
37 #define PFX "powernow: "
38
39
40 struct psb_s {
41         u8 signature[10];
42         u8 tableversion;
43         u8 flags;
44         u16 settlingtime;
45         u8 reserved1;
46         u8 numpst;
47 };
48
49 struct pst_s {
50         u32 cpuid;
51         u8 fsbspeed;
52         u8 maxfid;
53         u8 startvid;
54         u8 numpstates;
55 };
56
57 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
58 union powernow_acpi_control_t {
59         struct {
60                 unsigned long fid:5,
61                 vid:5,
62                 sgtc:20,
63                 res1:2;
64         } bits;
65         unsigned long val;
66 };
67 #endif
68
69 #ifdef CONFIG_CPU_FREQ_DEBUG
70 /* divide by 1000 to get VCore voltage in V. */
71 static int mobile_vid_table[32] = {
72     2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
73     1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
74     1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
75     1075, 1050, 1025, 1000, 975, 950, 925, 0,
76 };
77 #endif
78
79 /* divide by 10 to get FID. */
80 static int fid_codes[32] = {
81     110, 115, 120, 125, 50, 55, 60, 65,
82     70, 75, 80, 85, 90, 95, 100, 105,
83     30, 190, 40, 200, 130, 135, 140, 210,
84     150, 225, 160, 165, 170, 180, -1, -1,
85 };
86
87 /* This parameter is used in order to force ACPI instead of legacy method for
88  * configuration purpose.
89  */
90
91 static int acpi_force;
92
93 static struct cpufreq_frequency_table *powernow_table;
94
95 static unsigned int can_scale_bus;
96 static unsigned int can_scale_vid;
97 static unsigned int minimum_speed=-1;
98 static unsigned int maximum_speed;
99 static unsigned int number_scales;
100 static unsigned int fsb;
101 static unsigned int latency;
102 static char have_a0;
103
104 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "powernow-k7", msg)
105
106 static int check_fsb(unsigned int fsbspeed)
107 {
108         int delta;
109         unsigned int f = fsb / 1000;
110
111         delta = (fsbspeed > f) ? fsbspeed - f : f - fsbspeed;
112         return (delta < 5);
113 }
114
115 static int check_powernow(void)
116 {
117         struct cpuinfo_x86 *c = cpu_data;
118         unsigned int maxei, eax, ebx, ecx, edx;
119
120         if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 !=6)) {
121 #ifdef MODULE
122                 printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
123 #endif
124                 return 0;
125         }
126
127         /* Get maximum capabilities */
128         maxei = cpuid_eax (0x80000000);
129         if (maxei < 0x80000007) {       /* Any powernow info ? */
130 #ifdef MODULE
131                 printk (KERN_INFO PFX "No powernow capabilities detected\n");
132 #endif
133                 return 0;
134         }
135
136         if ((c->x86_model == 6) && (c->x86_mask == 0)) {
137                 printk (KERN_INFO PFX "K7 660[A0] core detected, enabling errata workarounds\n");
138                 have_a0 = 1;
139         }
140
141         cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
142
143         /* Check we can actually do something before we say anything.*/
144         if (!(edx & (1 << 1 | 1 << 2)))
145                 return 0;
146
147         printk (KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
148
149         if (edx & 1 << 1) {
150                 printk ("frequency");
151                 can_scale_bus=1;
152         }
153
154         if ((edx & (1 << 1 | 1 << 2)) == 0x6)
155                 printk (" and ");
156
157         if (edx & 1 << 2) {
158                 printk ("voltage");
159                 can_scale_vid=1;
160         }
161
162         printk (".\n");
163         return 1;
164 }
165
166
167 static int get_ranges (unsigned char *pst)
168 {
169         unsigned int j;
170         unsigned int speed;
171         u8 fid, vid;
172
173         powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
174         if (!powernow_table)
175                 return -ENOMEM;
176         memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
177
178         for (j=0 ; j < number_scales; j++) {
179                 fid = *pst++;
180
181                 powernow_table[j].frequency = (fsb * fid_codes[fid]) / 10;
182                 powernow_table[j].index = fid; /* lower 8 bits */
183
184                 speed = powernow_table[j].frequency;
185
186                 if ((fid_codes[fid] % 10)==5) {
187 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
188                         if (have_a0 == 1)
189                                 powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
190 #endif
191                 }
192
193                 if (speed < minimum_speed)
194                         minimum_speed = speed;
195                 if (speed > maximum_speed)
196                         maximum_speed = speed;
197
198                 vid = *pst++;
199                 powernow_table[j].index |= (vid << 8); /* upper 8 bits */
200
201                 dprintk ("   FID: 0x%x (%d.%dx [%dMHz])  "
202                          "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10, 
203                          fid_codes[fid] % 10, speed/1000, vid,  
204                          mobile_vid_table[vid]/1000,
205                          mobile_vid_table[vid]%1000);
206         }
207         powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
208         powernow_table[number_scales].index = 0;
209
210         return 0;
211 }
212
213
214 static void change_FID(int fid)
215 {
216         union msr_fidvidctl fidvidctl;
217
218         rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
219         if (fidvidctl.bits.FID != fid) {
220                 fidvidctl.bits.SGTC = latency;
221                 fidvidctl.bits.FID = fid;
222                 fidvidctl.bits.VIDC = 0;
223                 fidvidctl.bits.FIDC = 1;
224                 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
225         }
226 }
227
228
229 static void change_VID(int vid)
230 {
231         union msr_fidvidctl fidvidctl;
232
233         rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
234         if (fidvidctl.bits.VID != vid) {
235                 fidvidctl.bits.SGTC = latency;
236                 fidvidctl.bits.VID = vid;
237                 fidvidctl.bits.FIDC = 0;
238                 fidvidctl.bits.VIDC = 1;
239                 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
240         }
241 }
242
243
244 static void change_speed (unsigned int index)
245 {
246         u8 fid, vid;
247         struct cpufreq_freqs freqs;
248         union msr_fidvidstatus fidvidstatus;
249         int cfid;
250
251         /* fid are the lower 8 bits of the index we stored into
252          * the cpufreq frequency table in powernow_decode_bios,
253          * vid are the upper 8 bits.
254          */
255
256         fid = powernow_table[index].index & 0xFF;
257         vid = (powernow_table[index].index & 0xFF00) >> 8;
258
259         freqs.cpu = 0;
260
261         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
262         cfid = fidvidstatus.bits.CFID;
263         freqs.old = fsb * fid_codes[cfid] / 10;
264
265         freqs.new = powernow_table[index].frequency;
266
267         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
268
269         /* Now do the magic poking into the MSRs.  */
270
271         if (have_a0 == 1)       /* A0 errata 5 */
272                 local_irq_disable();
273
274         if (freqs.old > freqs.new) {
275                 /* Going down, so change FID first */
276                 change_FID(fid);
277                 change_VID(vid);
278         } else {
279                 /* Going up, so change VID first */
280                 change_VID(vid);
281                 change_FID(fid);
282         }
283
284
285         if (have_a0 == 1)
286                 local_irq_enable();
287
288         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
289 }
290
291
292 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
293
294 static struct acpi_processor_performance *acpi_processor_perf;
295
296 static int powernow_acpi_init(void)
297 {
298         int i;
299         int retval = 0;
300         union powernow_acpi_control_t pc;
301
302         if (acpi_processor_perf != NULL && powernow_table != NULL) {
303                 retval = -EINVAL;
304                 goto err0;
305         }
306
307         acpi_processor_perf = kmalloc(sizeof(struct acpi_processor_performance),
308                                       GFP_KERNEL);
309
310         if (!acpi_processor_perf) {
311                 retval = -ENOMEM;
312                 goto err0;
313         }
314
315         memset(acpi_processor_perf, 0, sizeof(struct acpi_processor_performance));
316
317         if (acpi_processor_register_performance(acpi_processor_perf, 0)) {
318                 retval = -EIO;
319                 goto err1;
320         }
321
322         if (acpi_processor_perf->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
323                 retval = -ENODEV;
324                 goto err2;
325         }
326
327         if (acpi_processor_perf->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
328                 retval = -ENODEV;
329                 goto err2;
330         }
331
332         number_scales = acpi_processor_perf->state_count;
333
334         if (number_scales < 2) {
335                 retval = -ENODEV;
336                 goto err2;
337         }
338
339         powernow_table = kmalloc((number_scales + 1) * (sizeof(struct cpufreq_frequency_table)), GFP_KERNEL);
340         if (!powernow_table) {
341                 retval = -ENOMEM;
342                 goto err2;
343         }
344
345         memset(powernow_table, 0, ((number_scales + 1) * sizeof(struct cpufreq_frequency_table)));
346
347         pc.val = (unsigned long) acpi_processor_perf->states[0].control;
348         for (i = 0; i < number_scales; i++) {
349                 u8 fid, vid;
350                 unsigned int speed;
351
352                 pc.val = (unsigned long) acpi_processor_perf->states[i].control;
353                 dprintk ("acpi:  P%d: %d MHz %d mW %d uS control %08x SGTC %d\n",
354                          i,
355                          (u32) acpi_processor_perf->states[i].core_frequency,
356                          (u32) acpi_processor_perf->states[i].power,
357                          (u32) acpi_processor_perf->states[i].transition_latency,
358                          (u32) acpi_processor_perf->states[i].control,
359                          pc.bits.sgtc);
360
361                 vid = pc.bits.vid;
362                 fid = pc.bits.fid;
363
364                 powernow_table[i].frequency = fsb * fid_codes[fid] / 10;
365                 powernow_table[i].index = fid; /* lower 8 bits */
366                 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
367
368                 speed = powernow_table[i].frequency;
369
370                 if ((fid_codes[fid] % 10)==5) {
371                         if (have_a0 == 1)
372                                 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
373                 }
374
375                 dprintk ("   FID: 0x%x (%d.%dx [%dMHz])  "
376                          "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10, 
377                          fid_codes[fid] % 10, speed/1000, vid,  
378                          mobile_vid_table[vid]/1000,
379                          mobile_vid_table[vid]%1000);
380
381                 if (latency < pc.bits.sgtc)
382                         latency = pc.bits.sgtc;
383
384                 if (speed < minimum_speed)
385                         minimum_speed = speed;
386                 if (speed > maximum_speed)
387                         maximum_speed = speed;
388         }
389
390         powernow_table[i].frequency = CPUFREQ_TABLE_END;
391         powernow_table[i].index = 0;
392
393         /* notify BIOS that we exist */
394         acpi_processor_notify_smm(THIS_MODULE);
395
396         return 0;
397
398 err2:
399         acpi_processor_unregister_performance(acpi_processor_perf, 0);
400 err1:
401         kfree(acpi_processor_perf);
402 err0:
403         printk(KERN_WARNING PFX "ACPI perflib can not be used in this platform\n");
404         acpi_processor_perf = NULL;
405         return retval;
406 }
407 #else
408 static int powernow_acpi_init(void)
409 {
410         printk(KERN_INFO PFX "no support for ACPI processor found."
411                "  Please recompile your kernel with ACPI processor\n");
412         return -EINVAL;
413 }
414 #endif
415
416 static int powernow_decode_bios (int maxfid, int startvid)
417 {
418         struct psb_s *psb;
419         struct pst_s *pst;
420         unsigned int i, j;
421         unsigned char *p;
422         unsigned int etuple;
423         unsigned int ret;
424
425         etuple = cpuid_eax(0x80000001);
426
427         for (i=0xC0000; i < 0xffff0 ; i+=16) {
428
429                 p = phys_to_virt(i);
430
431                 if (memcmp(p, "AMDK7PNOW!",  10) == 0){
432                         dprintk ("Found PSB header at %p\n", p);
433                         psb = (struct psb_s *) p;
434                         dprintk ("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");
437                                 return -ENODEV;
438                         }
439
440                         dprintk ("Flags: 0x%x\n", psb->flags);
441                         if ((psb->flags & 1)==0) {
442                                 dprintk ("Mobile voltage regulator\n");
443                         } else {
444                                 dprintk ("Desktop voltage regulator\n");
445                         }
446
447                         latency = psb->settlingtime;
448                         if (latency < 100) {
449                                 printk (KERN_INFO PFX "BIOS set settling time to %d microseconds."
450                                                 "Should be at least 100. Correcting.\n", latency);
451                                 latency = 100;
452                         }
453                         dprintk ("Settling Time: %d microseconds.\n", psb->settlingtime);
454                         dprintk ("Has %d PST tables. (Only dumping ones relevant to this CPU).\n", psb->numpst);
455
456                         p += sizeof (struct psb_s);
457
458                         pst = (struct pst_s *) p;
459
460                         for (i = 0 ; i <psb->numpst; i++) {
461                                 pst = (struct pst_s *) p;
462                                 number_scales = pst->numpstates;
463
464                                 if ((etuple == pst->cpuid) && check_fsb(pst->fsbspeed) &&
465                                     (maxfid==pst->maxfid) && (startvid==pst->startvid))
466                                 {
467                                         dprintk ("PST:%d (@%p)\n", i, pst);
468                                         dprintk (" cpuid: 0x%x  fsb: %d  maxFID: 0x%x  startvid: 0x%x\n", 
469                                                  pst->cpuid, pst->fsbspeed, pst->maxfid, pst->startvid);
470
471                                         ret = get_ranges ((char *) pst + sizeof (struct pst_s));
472                                         return ret;
473
474                                 } else {
475                                         p = (char *) pst + sizeof (struct pst_s);
476                                         for (j=0 ; j < number_scales; j++)
477                                                 p+=2;
478                                 }
479                         }
480                         printk (KERN_INFO PFX "No PST tables match this cpuid (0x%x)\n", etuple);
481                         printk (KERN_INFO PFX "This is indicative of a broken BIOS.\n");
482
483                         return -EINVAL;
484                 }
485                 p++;
486         }
487
488         return -ENODEV;
489 }
490
491
492 static int powernow_target (struct cpufreq_policy *policy,
493                             unsigned int target_freq,
494                             unsigned int relation)
495 {
496         unsigned int newstate;
497
498         if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
499                 return -EINVAL;
500
501         change_speed(newstate);
502
503         return 0;
504 }
505
506
507 static int powernow_verify (struct cpufreq_policy *policy)
508 {
509         return cpufreq_frequency_table_verify(policy, powernow_table);
510 }
511
512 /*
513  * We use the fact that the bus frequency is somehow
514  * a multiple of 100000/3 khz, then we compute sgtc according
515  * to this multiple.
516  * That way, we match more how AMD thinks all of that work.
517  * We will then get the same kind of behaviour already tested under
518  * the "well-known" other OS.
519  */
520 static int __init fixup_sgtc(void)
521 {
522         unsigned int sgtc;
523         unsigned int m;
524
525         m = fsb / 3333;
526         if ((m % 10) >= 5)
527                 m += 5;
528
529         m /= 10;
530
531         sgtc = 100 * m * latency;
532         sgtc = sgtc / 3;
533         if (sgtc > 0xfffff) {
534                 printk(KERN_WARNING PFX "SGTC too large %d\n", sgtc);
535                 sgtc = 0xfffff;
536         }
537         return sgtc;
538 }
539
540 static unsigned int powernow_get(unsigned int cpu)
541 {
542         union msr_fidvidstatus fidvidstatus;
543         unsigned int cfid;
544
545         if (cpu)
546                 return 0;
547         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
548         cfid = fidvidstatus.bits.CFID;
549
550         return (fsb * fid_codes[cfid] / 10);
551 }
552
553
554 static int __init acer_cpufreq_pst(struct dmi_system_id *d)
555 {
556         printk(KERN_WARNING "%s laptop with broken PST tables in BIOS detected.\n", d->ident);
557         printk(KERN_WARNING "You need to downgrade to 3A21 (09/09/2002), or try a newer BIOS than 3A71 (01/20/2003)\n");
558         printk(KERN_WARNING "cpufreq scaling has been disabled as a result of this.\n");
559         return 0;
560 }
561
562 /*
563  * Some Athlon laptops have really fucked PST tables.
564  * A BIOS update is all that can save them.
565  * Mention this, and disable cpufreq.
566  */
567 static struct dmi_system_id __initdata powernow_dmi_table[] = {
568         {
569                 .callback = acer_cpufreq_pst,
570                 .ident = "Acer Aspire",
571                 .matches = {
572                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde Software"),
573                         DMI_MATCH(DMI_BIOS_VERSION, "3A71"),
574                 },
575         },
576         { }
577 };
578
579 static int __init powernow_cpu_init (struct cpufreq_policy *policy)
580 {
581         union msr_fidvidstatus fidvidstatus;
582         int result;
583
584         if (policy->cpu != 0)
585                 return -ENODEV;
586
587         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
588
589         /* A K7 with powernow technology is set to max frequency by BIOS */
590         fsb = (10 * cpu_khz) / fid_codes[fidvidstatus.bits.MFID];
591         if (!fsb) {
592                 printk(KERN_WARNING PFX "can not determine bus frequency\n");
593                 return -EINVAL;
594         }
595         dprintk("FSB: %3d.%03d MHz\n", fsb/1000, fsb%1000);
596
597         if (dmi_check_system(powernow_dmi_table) || acpi_force) {
598                 printk (KERN_INFO PFX "PSB/PST known to be broken.  Trying ACPI instead\n");
599                 result = powernow_acpi_init();
600         } else {
601                 result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
602                 if (result) {
603                         printk (KERN_INFO PFX "Trying ACPI perflib\n");
604                         maximum_speed = 0;
605                         minimum_speed = -1;
606                         latency = 0;
607                         result = powernow_acpi_init();
608                         if (result) {
609                                 printk (KERN_INFO PFX "ACPI and legacy methods failed\n");
610                                 printk (KERN_INFO PFX "See http://www.codemonkey.org.uk/projects/cpufreq/powernow-k7.shtml\n");
611                         }
612                 } else {
613                         /* SGTC use the bus clock as timer */
614                         latency = fixup_sgtc();
615                         printk(KERN_INFO PFX "SGTC: %d\n", latency);
616                 }
617         }
618
619         if (result)
620                 return result;
621
622         printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
623                                 minimum_speed/1000, maximum_speed/1000);
624
625         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
626
627         policy->cpuinfo.transition_latency = cpufreq_scale(2000000UL, fsb, latency);
628
629         policy->cur = powernow_get(0);
630
631         cpufreq_frequency_table_get_attr(powernow_table, policy->cpu);
632
633         return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
634 }
635
636 static int powernow_cpu_exit (struct cpufreq_policy *policy) {
637         cpufreq_frequency_table_put_attr(policy->cpu);
638
639 #ifdef CONFIG_X86_POWERNOW_K7_ACPI
640         if (acpi_processor_perf) {
641                 acpi_processor_unregister_performance(acpi_processor_perf, 0);
642                 kfree(acpi_processor_perf);
643         }
644 #endif
645
646         if (powernow_table)
647                 kfree(powernow_table);
648
649         return 0;
650 }
651
652 static struct freq_attr* powernow_table_attr[] = {
653         &cpufreq_freq_attr_scaling_available_freqs,
654         NULL,
655 };
656
657 static struct cpufreq_driver powernow_driver = {
658         .verify = powernow_verify,
659         .target = powernow_target,
660         .get    = powernow_get,
661         .init   = powernow_cpu_init,
662         .exit   = powernow_cpu_exit,
663         .name   = "powernow-k7",
664         .owner  = THIS_MODULE,
665         .attr   = powernow_table_attr,
666 };
667
668 static int __init powernow_init (void)
669 {
670         if (check_powernow()==0)
671                 return -ENODEV;
672         return cpufreq_register_driver(&powernow_driver);
673 }
674
675
676 static void __exit powernow_exit (void)
677 {
678         cpufreq_unregister_driver(&powernow_driver);
679 }
680
681 module_param(acpi_force,  int, 0444);
682 MODULE_PARM_DESC(acpi_force, "Force ACPI to be used.");
683
684 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
685 MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
686 MODULE_LICENSE ("GPL");
687
688 late_initcall(powernow_init);
689 module_exit(powernow_exit);
690