commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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  *
5  *  Licensed under the terms of the GNU GPL License version 2.
6  *  Based upon datasheets & sample CPUs kindly provided by AMD.
7  *
8  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
9  *
10  * Errata 5: Processor 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.
12  * Errata 15: Processors with half frequency multipliers may hang upon wakeup from disconnect.
13  * - We disable half multipliers if ACPI is used on A0 stepping CPUs.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h> 
18 #include <linux/init.h>
19 #include <linux/cpufreq.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22
23 #include <asm/msr.h>
24 #include <asm/timex.h>
25 #include <asm/io.h>
26 #include <asm/system.h>
27
28 #include "powernow-k7.h"
29
30 #define DEBUG
31
32 #ifdef DEBUG
33 #define dprintk(msg...) printk(msg)
34 #else
35 #define dprintk(msg...) do { } while(0)
36 #endif
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
59 /* divide by 1000 to get VID. */
60 static int mobile_vid_table[32] = {
61     2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
62     1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
63     1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
64     1075, 1050, 1024, 1000, 975, 950, 925, 0,
65 };
66
67 /* divide by 10 to get FID. */
68 static int fid_codes[32] = {
69     110, 115, 120, 125, 50, 55, 60, 65,
70     70, 75, 80, 85, 90, 95, 100, 105, 
71     30, 190, 40, 200, 130, 135, 140, 210,
72     150, 225, 160, 165, 170, 180, -1, -1,
73 };
74
75 static struct cpufreq_frequency_table *powernow_table;
76
77 static unsigned int can_scale_bus;
78 static unsigned int can_scale_vid;
79 static unsigned int minimum_speed=-1;
80 static unsigned int maximum_speed;
81 static unsigned int number_scales;
82 static unsigned int fsb;
83 static unsigned int latency;
84 static char have_a0;
85
86
87 static int check_powernow(void)
88 {
89         struct cpuinfo_x86 *c = cpu_data;
90         unsigned int maxei, eax, ebx, ecx, edx;
91
92         if (c->x86_vendor != X86_VENDOR_AMD) {
93                 printk (KERN_INFO PFX "AMD processor not detected.\n");
94                 return 0;
95         }
96
97         if (c->x86 !=6) {
98                 printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
99                 return 0;
100         }
101
102         printk (KERN_INFO PFX "AMD K7 CPU detected.\n");
103
104         if ((c->x86_model == 6) && (c->x86_mask == 0)) {
105                 printk (KERN_INFO PFX "K7 660[A0] core detected, enabling errata workarounds\n");
106                 have_a0 = 1;
107         }
108
109         /* Get maximum capabilities */
110         maxei = cpuid_eax (0x80000000);
111         if (maxei < 0x80000007) {       /* Any powernow info ? */
112                 printk (KERN_INFO PFX "No powernow capabilities detected\n");
113                 return 0;
114         }
115
116         cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
117         printk (KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
118
119         if (edx & 1 << 1) {
120                 printk ("frequency");
121                 can_scale_bus=1;
122         }
123
124         if ((edx & (1 << 1 | 1 << 2)) == 0x6)
125                 printk (" and ");
126
127         if (edx & 1 << 2) {
128                 printk ("voltage");
129                 can_scale_vid=1;
130         }
131
132         if (!(edx & (1 << 1 | 1 << 2))) {
133                 printk ("nothing.\n");
134                 return 0;
135         }
136
137         printk (".\n");
138         return 1;
139 }
140
141
142 static int get_ranges (unsigned char *pst)
143 {
144         unsigned int j, speed;
145         u8 fid, vid;
146
147         powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
148         if (!powernow_table)
149                 return -ENOMEM;
150         memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
151
152         for (j=0 ; j < number_scales; j++) {
153                 fid = *pst++;
154
155                 powernow_table[j].frequency = fsb * fid_codes[fid] * 100;
156                 powernow_table[j].index = fid; /* lower 8 bits */
157
158                 speed = fsb * (fid_codes[fid]/10);
159                 if ((fid_codes[fid] % 10)==5) {
160                         speed += fsb/2;
161 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
162                         if (have_a0 == 1)
163                                 powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
164 #endif
165                 }
166
167                 dprintk (KERN_INFO PFX "   FID: 0x%x (%d.%dx [%dMHz])\t", fid,
168                         fid_codes[fid] / 10, fid_codes[fid] % 10, speed);
169
170                 if (speed < minimum_speed)
171                         minimum_speed = speed;
172                 if (speed > maximum_speed)
173                         maximum_speed = speed;
174
175                 vid = *pst++;
176                 powernow_table[j].index |= (vid << 8); /* upper 8 bits */
177                 dprintk ("VID: 0x%x (%d.%03dV)\n", vid, mobile_vid_table[vid]/1000,
178                         mobile_vid_table[vid]%1000);
179         }
180         dprintk ("\n");
181
182         powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
183         powernow_table[number_scales].index = 0;
184
185         return 0;
186 }
187
188
189 static void change_FID(int fid)
190 {
191         union msr_fidvidctl fidvidctl;
192
193         if (fidvidctl.bits.FID != fid) {
194                 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
195                 fidvidctl.bits.SGTC = latency;
196                 fidvidctl.bits.FID = fid;
197                 fidvidctl.bits.FIDC = 1;
198                 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
199         }
200 }
201
202
203 static void change_VID(int vid)
204 {
205         union msr_fidvidctl fidvidctl;
206
207         if (fidvidctl.bits.VID != vid) {
208                 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
209                 fidvidctl.bits.VID = vid;
210                 fidvidctl.bits.VIDC = 1;
211                 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
212         }
213 }
214
215
216 static void change_speed (unsigned int index)
217 {
218         u8 fid, vid;
219         struct cpufreq_freqs freqs;
220         union msr_fidvidstatus fidvidstatus;
221         int cfid;
222
223         /* fid are the lower 8 bits of the index we stored into
224          * the cpufreq frequency table in powernow_decode_bios,
225          * vid are the upper 8 bits.
226          */
227
228         fid = powernow_table[index].index & 0xFF;
229         vid = (powernow_table[index].index & 0xFF00) >> 8;
230
231         freqs.cpu = 0;
232
233         cfid = fidvidstatus.bits.CFID;
234         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
235         freqs.old = fsb * fid_codes[cfid] * 100;
236         freqs.new = powernow_table[index].frequency;
237
238         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
239
240         /* Now do the magic poking into the MSRs.  */
241
242         if (have_a0 == 1)       /* A0 errata 5 */
243                 local_irq_disable();
244
245         if (freqs.old > freqs.new) {
246                 /* Going down, so change FID first */
247                 change_FID(fid);
248                 change_VID(vid);
249         } else {
250                 /* Going up, so change VID first */
251                 change_VID(vid);
252                 change_FID(fid);
253         }
254         
255
256         if (have_a0 == 1)
257                 local_irq_enable();
258
259         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
260 }
261
262
263 static int powernow_decode_bios (int maxfid, int startvid)
264 {
265         struct psb_s *psb;
266         struct pst_s *pst;
267         struct cpuinfo_x86 *c = cpu_data;
268         unsigned int i, j;
269         unsigned char *p;
270         unsigned int etuple;
271         unsigned int ret;
272
273         etuple = cpuid_eax(0x80000001);
274         etuple &= 0xf00;
275         etuple |= (c->x86_model<<4)|(c->x86_mask);
276
277         for (i=0xC0000; i < 0xffff0 ; i+=16) {
278
279                 p = phys_to_virt(i);
280
281                 if (memcmp(p, "AMDK7PNOW!",  10) == 0){
282                         dprintk (KERN_INFO PFX "Found PSB header at %p\n", p);
283                         psb = (struct psb_s *) p;
284                         dprintk (KERN_INFO PFX "Table version: 0x%x\n", psb->tableversion);
285                         if (psb->tableversion != 0x12) {
286                                 printk (KERN_INFO PFX "Sorry, only v1.2 tables supported right now\n");
287                                 return -ENODEV;
288                         }
289
290                         dprintk (KERN_INFO PFX "Flags: 0x%x (", psb->flags);
291                         if ((psb->flags & 1)==0) {
292                                 dprintk ("Mobile");
293                         } else {
294                                 dprintk ("Desktop");
295                         }
296                         dprintk (" voltage regulator)\n");
297
298                         latency = psb->settlingtime;
299                         dprintk (KERN_INFO PFX "Settling Time: %d microseconds.\n", psb->settlingtime);
300                         dprintk (KERN_INFO PFX "Has %d PST tables. (Only dumping ones relevant to this CPU).\n", psb->numpst);
301
302                         p += sizeof (struct psb_s);
303
304                         pst = (struct pst_s *) p;
305
306                         for (i = 0 ; i <psb->numpst; i++) {
307                                 pst = (struct pst_s *) p;
308                                 number_scales = pst->numpstates;
309
310                                 if ((etuple == pst->cpuid) && (maxfid==pst->maxfid) && (startvid==pst->startvid))
311                                 {
312                                         dprintk (KERN_INFO PFX "PST:%d (@%p)\n", i, pst);
313                                         dprintk (KERN_INFO PFX " cpuid: 0x%x\t", pst->cpuid);
314                                         dprintk ("fsb: %d\t", pst->fsbspeed);
315                                         dprintk ("maxFID: 0x%x\t", pst->maxfid);
316                                         dprintk ("startvid: 0x%x\n", pst->startvid);
317
318                                         fsb = pst->fsbspeed;
319                                         ret = get_ranges ((char *) pst + sizeof (struct pst_s));
320                                         return ret;
321
322                                 } else {
323                                         p = (char *) pst + sizeof (struct pst_s);
324                                         for (j=0 ; j < number_scales; j++)
325                                                 p+=2;
326                                 }
327                         }
328                         return -EINVAL;
329                 }
330                 p++;
331         }
332
333         return -ENODEV;
334 }
335
336
337 static int powernow_target (struct cpufreq_policy *policy,
338                             unsigned int target_freq,
339                             unsigned int relation)
340 {
341         unsigned int newstate;
342
343         if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
344                 return -EINVAL;
345
346         change_speed(newstate);
347
348         return 0;
349 }
350
351
352 static int powernow_verify (struct cpufreq_policy *policy)
353 {
354         return cpufreq_frequency_table_verify(policy, powernow_table);
355 }
356
357
358 static int __init powernow_cpu_init (struct cpufreq_policy *policy)
359 {
360         union msr_fidvidstatus fidvidstatus;
361         int result;
362
363         if (policy->cpu != 0)
364                 return -ENODEV;
365
366         rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
367
368         result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
369         if (result)
370                 return result;
371
372         printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
373                                 minimum_speed, maximum_speed);
374
375         policy->policy = CPUFREQ_POLICY_PERFORMANCE;
376         policy->cpuinfo.transition_latency = latency;
377         policy->cur = maximum_speed;
378
379         return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
380 }
381
382 static struct cpufreq_driver powernow_driver = {
383         .verify         = powernow_verify,
384         .target         = powernow_target,
385         .init           = powernow_cpu_init,
386         .name           = "powernow-k7",
387         .owner          = THIS_MODULE,
388 };
389
390 static int __init powernow_init (void)
391 {
392         if (dmi_broken & BROKEN_CPUFREQ) {
393                 printk (KERN_INFO PFX "Disabled at boot time by DMI,\n");
394                 return -ENODEV;
395         }
396         if (check_powernow()==0)
397                 return -ENODEV;
398         return cpufreq_register_driver(&powernow_driver);
399 }
400
401
402 static void __exit powernow_exit (void)
403 {
404         cpufreq_unregister_driver(&powernow_driver);
405         if (powernow_table)
406                 kfree(powernow_table);
407 }
408
409 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
410 MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
411 MODULE_LICENSE ("GPL");
412
413 module_init(powernow_init);
414 module_exit(powernow_exit);
415