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