patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
10  *
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.
15  */
16
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>
25
26 #include <asm/msr.h>
27 #include <asm/timex.h>
28 #include <asm/io.h>
29 #include <asm/system.h>
30
31 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
32 #include <linux/acpi.h>
33 #include <acpi/processor.h>
34 #endif
35
36 #include "powernow-k7.h"
37
38 #define DEBUG
39
40 #ifdef DEBUG
41 #define dprintk(msg...) printk(msg)
42 #else
43 #define dprintk(msg...) do { } while(0)
44 #endif
45
46 #define PFX "powernow: "
47
48
49 struct psb_s {
50         u8 signature[10];
51         u8 tableversion;
52         u8 flags;
53         u16 settlingtime;
54         u8 reserved1;
55         u8 numpst;
56 };
57
58 struct pst_s {
59         u32 cpuid;
60         u8 fsbspeed;
61         u8 maxfid;
62         u8 startvid;
63         u8 numpstates;
64 };
65
66 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
67 union powernow_acpi_control_t {
68         struct {
69                 unsigned long fid:5,
70                 vid:5,
71                 sgtc:20,
72                 res1:2;
73         } bits;
74         unsigned long val;
75 };
76 #endif
77
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,
84 };
85
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,
92 };
93
94 /* This parameter is used in order to force ACPI instead of legacy method for
95  * configuration purpose.
96  */
97
98 static int acpi_force;
99
100 static struct cpufreq_frequency_table *powernow_table;
101
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;
109 static char have_a0;
110
111 static int check_fsb(unsigned int fsbspeed)
112 {
113         int delta;
114         unsigned int f = fsb / 1000;
115
116         delta = (fsbspeed > f) ? fsbspeed - f : f - fsbspeed;
117         return (delta < 5);
118 }
119
120 static int check_powernow(void)
121 {
122         struct cpuinfo_x86 *c = cpu_data;
123         unsigned int maxei, eax, ebx, ecx, edx;
124
125         if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 !=6)) {
126 #ifdef MODULE
127                 printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
128 #endif
129                 return 0;
130         }
131
132         /* Get maximum capabilities */
133         maxei = cpuid_eax (0x80000000);
134         if (maxei < 0x80000007) {       /* Any powernow info ? */
135 #ifdef MODULE
136                 printk (KERN_INFO PFX "No powernow capabilities detected\n");
137 #endif
138                 return 0;
139         }
140
141         if ((c->x86_model == 6) && (c->x86_mask == 0)) {
142                 printk (KERN_INFO PFX "K7 660[A0] core detected, enabling errata workarounds\n");
143                 have_a0 = 1;
144         }
145
146         cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
147
148         /* Check we can actually do something before we say anything.*/
149         if (!(edx & (1 << 1 | 1 << 2)))
150                 return 0;
151
152         printk (KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
153
154         if (edx & 1 << 1) {
155                 printk ("frequency");
156                 can_scale_bus=1;
157         }
158
159         if ((edx & (1 << 1 | 1 << 2)) == 0x6)
160                 printk (" and ");
161
162         if (edx & 1 << 2) {
163                 printk ("voltage");
164                 can_scale_vid=1;
165         }
166
167         printk (".\n");
168         return 1;
169 }
170
171
172 static int get_ranges (unsigned char *pst)
173 {
174         unsigned int j;
175         unsigned int speed;
176         u8 fid, vid;
177
178         powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
179         if (!powernow_table)
180                 return -ENOMEM;
181         memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
182
183         for (j=0 ; j < number_scales; j++) {
184                 fid = *pst++;
185
186                 powernow_table[j].frequency = (fsb * fid_codes[fid]) / 10;
187                 powernow_table[j].index = fid; /* lower 8 bits */
188
189                 speed = powernow_table[j].frequency;
190
191                 if ((fid_codes[fid] % 10)==5) {
192 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
193                         if (have_a0 == 1)
194                                 powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
195 #endif
196                 }
197
198                 dprintk (KERN_INFO PFX "   FID: 0x%x (%d.%dx [%dMHz])\t", fid,
199                         fid_codes[fid] / 10, fid_codes[fid] % 10, speed/1000);
200
201                 if (speed < minimum_speed)
202                         minimum_speed = speed;
203                 if (speed > maximum_speed)
204                         maximum_speed = speed;
205
206                 vid = *pst++;
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);
210         }
211         powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
212         powernow_table[number_scales].index = 0;
213
214         return 0;
215 }
216
217
218 static void change_FID(int fid)
219 {
220         union msr_fidvidctl fidvidctl;
221
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);
229         }
230 }
231
232
233 static void change_VID(int vid)
234 {
235         union msr_fidvidctl fidvidctl;
236
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);
244         }
245 }
246
247
248 static void change_speed (unsigned int index)
249 {
250         u8 fid, vid;
251         struct cpufreq_freqs freqs;
252         union msr_fidvidstatus fidvidstatus;
253         int cfid;
254
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.
258          */
259
260         fid = powernow_table[index].index & 0xFF;
261         vid = (powernow_table[index].index & 0xFF00) >> 8;
262
263         freqs.cpu = 0;
264
265         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
266         cfid = fidvidstatus.bits.CFID;
267         freqs.old = fsb * fid_codes[cfid] / 10;
268
269         freqs.new = powernow_table[index].frequency;
270
271         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
272
273         /* Now do the magic poking into the MSRs.  */
274
275         if (have_a0 == 1)       /* A0 errata 5 */
276                 local_irq_disable();
277
278         if (freqs.old > freqs.new) {
279                 /* Going down, so change FID first */
280                 change_FID(fid);
281                 change_VID(vid);
282         } else {
283                 /* Going up, so change VID first */
284                 change_VID(vid);
285                 change_FID(fid);
286         }
287         
288
289         if (have_a0 == 1)
290                 local_irq_enable();
291
292         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
293 }
294
295
296 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
297
298 struct acpi_processor_performance *acpi_processor_perf;
299
300 static int powernow_acpi_init(void)
301 {
302         int i;
303         int retval = 0;
304         union powernow_acpi_control_t pc;
305
306         if (acpi_processor_perf != NULL && powernow_table != NULL) {
307                 retval = -EINVAL;
308                 goto err0;
309         }
310
311         acpi_processor_perf = kmalloc(sizeof(struct acpi_processor_performance),
312                                       GFP_KERNEL);
313
314         if (!acpi_processor_perf) {
315                 retval = -ENOMEM;
316                 goto err0;
317         }
318
319         memset(acpi_processor_perf, 0, sizeof(struct acpi_processor_performance));
320
321         if (acpi_processor_register_performance(acpi_processor_perf, 0)) {
322                 retval = -EIO;
323                 goto err1;
324         }
325
326         if (acpi_processor_perf->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
327                 retval = -ENODEV;
328                 goto err2;
329         }
330
331         if (acpi_processor_perf->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
332                 retval = -ENODEV;
333                 goto err2;
334         }
335
336         number_scales = acpi_processor_perf->state_count;
337
338         if (number_scales < 2) {
339                 retval = -ENODEV;
340                 goto err2;
341         }
342
343         powernow_table = kmalloc((number_scales + 1) * (sizeof(struct cpufreq_frequency_table)), GFP_KERNEL);
344         if (!powernow_table) {
345                 retval = -ENOMEM;
346                 goto err2;
347         }
348
349         memset(powernow_table, 0, ((number_scales + 1) * sizeof(struct cpufreq_frequency_table)));
350
351         pc.val = (unsigned long) acpi_processor_perf->states[0].control;
352         for (i = 0; i < number_scales; i++) {
353                 u8 fid, vid;
354                 unsigned int speed;
355
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",
358                          i,
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,
363                          pc.bits.sgtc);
364
365                 vid = pc.bits.vid;
366                 fid = pc.bits.fid;
367
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 */
371
372                 speed = powernow_table[i].frequency;
373
374                 if ((fid_codes[fid] % 10)==5) {
375                         if (have_a0 == 1)
376                                 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
377                 }
378
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);
383
384                 if (latency < pc.bits.sgtc)
385                         latency = pc.bits.sgtc;
386
387                 if (speed < minimum_speed)
388                         minimum_speed = speed;
389                 if (speed > maximum_speed)
390                         maximum_speed = speed;
391         }
392
393         powernow_table[i].frequency = CPUFREQ_TABLE_END;
394         powernow_table[i].index = 0;
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 (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");
437                                 return -ENODEV;
438                         }
439
440                         dprintk (KERN_INFO PFX "Flags: 0x%x (", psb->flags);
441                         if ((psb->flags & 1)==0) {
442                                 dprintk ("Mobile");
443                         } else {
444                                 dprintk ("Desktop");
445                         }
446                         dprintk (" voltage regulator)\n");
447
448                         latency = psb->settlingtime;
449                         if (latency < 100) {
450                                 printk (KERN_INFO PFX "BIOS set settling time to %d microseconds."
451                                                 "Should be at least 100. Correcting.\n", latency);
452                                 latency = 100;
453                         }
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);
456
457                         p += sizeof (struct psb_s);
458
459                         pst = (struct pst_s *) p;
460
461                         for (i = 0 ; i <psb->numpst; i++) {
462                                 pst = (struct pst_s *) p;
463                                 number_scales = pst->numpstates;
464
465                                 if ((etuple == pst->cpuid) && check_fsb(pst->fsbspeed) &&
466                                     (maxfid==pst->maxfid) && (startvid==pst->startvid))
467                                 {
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);
473
474                                         ret = get_ranges ((char *) pst + sizeof (struct pst_s));
475                                         return ret;
476
477                                 } else {
478                                         p = (char *) pst + sizeof (struct pst_s);
479                                         for (j=0 ; j < number_scales; j++)
480                                                 p+=2;
481                                 }
482                         }
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");
485
486                         return -EINVAL;
487                 }
488                 p++;
489         }
490
491         return -ENODEV;
492 }
493
494
495 static int powernow_target (struct cpufreq_policy *policy,
496                             unsigned int target_freq,
497                             unsigned int relation)
498 {
499         unsigned int newstate;
500
501         if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
502                 return -EINVAL;
503
504         change_speed(newstate);
505
506         return 0;
507 }
508
509
510 static int powernow_verify (struct cpufreq_policy *policy)
511 {
512         return cpufreq_frequency_table_verify(policy, powernow_table);
513 }
514
515 /*
516  * We use the fact that the bus frequency is somehow
517  * a multiple of 100000/3 khz, then we compute sgtc according
518  * to this multiple.
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.
522  */
523 static int __init fixup_sgtc(void)
524 {
525         unsigned int sgtc;
526         unsigned int m;
527
528         m = fsb / 3333;
529         if ((m % 10) >= 5)
530                 m += 5;
531
532         m /= 10;
533
534         sgtc = 100 * m * latency;
535         sgtc = sgtc / 3;
536         if (sgtc > 0xfffff) {
537                 printk(KERN_WARNING PFX "SGTC too large %d\n", sgtc);
538                 sgtc = 0xfffff;
539         }
540         return sgtc;
541 }
542
543 static unsigned int powernow_get(unsigned int cpu)
544 {
545         union msr_fidvidstatus fidvidstatus;
546         unsigned int cfid;
547
548         if (cpu)
549                 return 0;
550         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
551         cfid = fidvidstatus.bits.CFID;
552
553         return (fsb * fid_codes[cfid] / 10);
554 }
555
556
557 static int __init powernow_cpu_init (struct cpufreq_policy *policy)
558 {
559         union msr_fidvidstatus fidvidstatus;
560         int result;
561
562         if (policy->cpu != 0)
563                 return -ENODEV;
564
565         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
566
567         /* A K7 with powernow technology is set to max frequency by BIOS */
568         fsb = (10 * cpu_khz) / fid_codes[fidvidstatus.bits.CFID];
569         if (!fsb) {
570                 printk(KERN_WARNING PFX "can not determine bus frequency\n");
571                 return -EINVAL;
572         }
573         dprintk(KERN_INFO PFX "FSB: %3d.%03d MHz\n", fsb/1000, fsb%1000);
574
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();
578         } else {
579                 result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
580                 if (result) {
581                         printk (KERN_INFO PFX "Trying ACPI perflib\n");
582                         maximum_speed = 0;
583                         minimum_speed = -1;
584                         latency = 0;
585                         result = powernow_acpi_init();
586                         if (result) {
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");
589                         }
590                 } else {
591                         /* SGTC use the bus clock as timer */
592                         latency = fixup_sgtc();
593                         printk(KERN_INFO PFX "SGTC: %d\n", latency);
594                 }
595         }
596
597         if (result)
598                 return result;
599
600         printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
601                                 minimum_speed/1000, maximum_speed/1000);
602
603         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
604
605         policy->cpuinfo.transition_latency = 20 * latency / fsb;
606
607         policy->cur = powernow_get(0);
608
609         cpufreq_frequency_table_get_attr(powernow_table, policy->cpu);
610
611         return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
612 }
613
614 static int powernow_cpu_exit (struct cpufreq_policy *policy) {
615         cpufreq_frequency_table_put_attr(policy->cpu);
616         return 0;
617 }
618
619 static struct freq_attr* powernow_table_attr[] = {
620         &cpufreq_freq_attr_scaling_available_freqs,
621         NULL,
622 };
623
624 static struct cpufreq_driver powernow_driver = {
625         .verify         = powernow_verify,
626         .target         = powernow_target,
627         .get            = powernow_get, 
628         .init           = powernow_cpu_init,
629         .exit           = powernow_cpu_exit,
630         .name           = "powernow-k7",
631         .owner          = THIS_MODULE,
632         .attr           = powernow_table_attr,
633 };
634
635 static int __init powernow_init (void)
636 {
637         if (check_powernow()==0)
638                 return -ENODEV;
639         return cpufreq_register_driver(&powernow_driver);
640 }
641
642
643 static void __exit powernow_exit (void)
644 {
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);
649         }
650 #endif
651         cpufreq_unregister_driver(&powernow_driver);
652         if (powernow_table)
653                 kfree(powernow_table);
654 }
655
656 module_param(acpi_force,  int, 0444);
657 MODULE_PARM_DESC(acpi_force, "Force ACPI to be used");
658
659 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
660 MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
661 MODULE_LICENSE ("GPL");
662
663 late_initcall(powernow_init);
664 module_exit(powernow_exit);
665