upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / i386 / kernel / cpu / cpufreq / powernow-k8.c
1 /*
2  *   (c) 2003, 2004 Advanced Micro Devices, Inc.
3  *  Your use of this code is subject to the terms and conditions of the
4  *  GNU general public license version 2. See "COPYING" or
5  *  http://www.gnu.org/licenses/gpl.html
6  *
7  *  Support : paul.devriendt@amd.com
8  *
9  *  Based on the powernow-k7.c module written by Dave Jones.
10  *  (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs
11  *  (C) 2004 Dominik Brodowski <linux@brodo.de>
12  *  (C) 2004 Pavel Machek <pavel@suse.cz>
13  *  Licensed under the terms of the GNU GPL License version 2.
14  *  Based upon datasheets & sample CPUs kindly provided by AMD.
15  *
16  *  Valuable input gratefully received from Dave Jones, Pavel Machek,
17  *  Dominik Brodowski, and others.
18  *  Processor information obtained from Chapter 9 (Power and Thermal Management)
19  *  of the "BIOS and Kernel Developer's Guide for the AMD Athlon 64 and AMD
20  *  Opteron Processors" available for download from www.amd.com
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/smp.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/cpufreq.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30
31 #include <asm/msr.h>
32 #include <asm/io.h>
33 #include <asm/delay.h>
34
35 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
36 #include <linux/acpi.h>
37 #include <acpi/processor.h>
38 #endif
39
40 #define PFX "powernow-k8: "
41 #define BFX PFX "BIOS error: "
42 #define VERSION "version 1.00.09e"
43 #include "powernow-k8.h"
44
45 /* serialize freq changes  */
46 static DECLARE_MUTEX(fidvid_sem);
47
48 static struct powernow_k8_data *powernow_data[NR_CPUS];
49
50 /* Return a frequency in MHz, given an input fid */
51 static u32 find_freq_from_fid(u32 fid)
52 {
53         return 800 + (fid * 100);
54 }
55
56 /* Return a frequency in KHz, given an input fid */
57 static u32 find_khz_freq_from_fid(u32 fid)
58 {
59         return 1000 * find_freq_from_fid(fid);
60 }
61
62 /* Return a voltage in miliVolts, given an input vid */
63 static u32 find_millivolts_from_vid(struct powernow_k8_data *data, u32 vid)
64 {
65         return 1550-vid*25;
66 }
67
68 /* Return the vco fid for an input fid */
69 static u32 convert_fid_to_vco_fid(u32 fid)
70 {
71         if (fid < HI_FID_TABLE_BOTTOM) {
72                 return 8 + (2 * fid);
73         } else {
74                 return fid;
75         }
76 }
77
78 /*
79  * Return 1 if the pending bit is set. Unless we just instructed the processor
80  * to transition to a new state, seeing this bit set is really bad news.
81  */
82 static int pending_bit_stuck(void)
83 {
84         u32 lo, hi;
85
86         rdmsr(MSR_FIDVID_STATUS, lo, hi);
87         return lo & MSR_S_LO_CHANGE_PENDING ? 1 : 0;
88 }
89
90 /*
91  * Update the global current fid / vid values from the status msr.
92  * Returns 1 on error.
93  */
94 static int query_current_values_with_pending_wait(struct powernow_k8_data *data)
95 {
96         u32 lo, hi;
97         u32 i = 0;
98
99         lo = MSR_S_LO_CHANGE_PENDING;
100         while (lo & MSR_S_LO_CHANGE_PENDING) {
101                 if (i++ > 0x1000000) {
102                         printk(KERN_ERR PFX "detected change pending stuck\n");
103                         return 1;
104                 }
105                 rdmsr(MSR_FIDVID_STATUS, lo, hi);
106         }
107
108         data->currvid = hi & MSR_S_HI_CURRENT_VID;
109         data->currfid = lo & MSR_S_LO_CURRENT_FID;
110
111         return 0;
112 }
113
114 /* the isochronous relief time */
115 static void count_off_irt(struct powernow_k8_data *data)
116 {
117         udelay((1 << data->irt) * 10);
118         return;
119 }
120
121 /* the voltage stabalization time */
122 static void count_off_vst(struct powernow_k8_data *data)
123 {
124         udelay(data->vstable * VST_UNITS_20US);
125         return;
126 }
127
128 /* need to init the control msr to a safe value (for each cpu) */
129 static void fidvid_msr_init(void)
130 {
131         u32 lo, hi;
132         u8 fid, vid;
133
134         rdmsr(MSR_FIDVID_STATUS, lo, hi);
135         vid = hi & MSR_S_HI_CURRENT_VID;
136         fid = lo & MSR_S_LO_CURRENT_FID;
137         lo = fid | (vid << MSR_C_LO_VID_SHIFT);
138         hi = MSR_C_HI_STP_GNT_BENIGN;
139         dprintk("cpu%d, init lo 0x%x, hi 0x%x\n", smp_processor_id(), lo, hi);
140         wrmsr(MSR_FIDVID_CTL, lo, hi);
141 }
142
143
144 /* write the new fid value along with the other control fields to the msr */
145 static int write_new_fid(struct powernow_k8_data *data, u32 fid)
146 {
147         u32 lo;
148         u32 savevid = data->currvid;
149
150         if ((fid & INVALID_FID_MASK) || (data->currvid & INVALID_VID_MASK)) {
151                 printk(KERN_ERR PFX "internal error - overflow on fid write\n");
152                 return 1;
153         }
154
155         lo = fid | (data->currvid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
156
157         dprintk("writing fid 0x%x, lo 0x%x, hi 0x%x\n",
158                 fid, lo, data->plllock * PLL_LOCK_CONVERSION);
159
160         wrmsr(MSR_FIDVID_CTL, lo, data->plllock * PLL_LOCK_CONVERSION);
161
162         if (query_current_values_with_pending_wait(data))
163                 return 1;
164
165         count_off_irt(data);
166
167         if (savevid != data->currvid) {
168                 printk(KERN_ERR PFX "vid change on fid trans, old 0x%x, new 0x%x\n",
169                        savevid, data->currvid);
170                 return 1;
171         }
172
173         if (fid != data->currfid) {
174                 printk(KERN_ERR PFX "fid trans failed, fid 0x%x, curr 0x%x\n", fid,
175                         data->currfid);
176                 return 1;
177         }
178
179         return 0;
180 }
181
182 /* Write a new vid to the hardware */
183 static int write_new_vid(struct powernow_k8_data *data, u32 vid)
184 {
185         u32 lo;
186         u32 savefid = data->currfid;
187
188         if ((data->currfid & INVALID_FID_MASK) || (vid & INVALID_VID_MASK)) {
189                 printk(KERN_ERR PFX "internal error - overflow on vid write\n");
190                 return 1;
191         }
192
193         lo = data->currfid | (vid << MSR_C_LO_VID_SHIFT) | MSR_C_LO_INIT_FID_VID;
194
195         dprintk("writing vid 0x%x, lo 0x%x, hi 0x%x\n",
196                 vid, lo, STOP_GRANT_5NS);
197
198         wrmsr(MSR_FIDVID_CTL, lo, STOP_GRANT_5NS);
199
200         if (query_current_values_with_pending_wait(data))
201                 return 1;
202
203         if (savefid != data->currfid) {
204                 printk(KERN_ERR PFX "fid changed on vid trans, old 0x%x new 0x%x\n",
205                        savefid, data->currfid);
206                 return 1;
207         }
208
209         if (vid != data->currvid) {
210                 printk(KERN_ERR PFX "vid trans failed, vid 0x%x, curr 0x%x\n", vid,
211                                 data->currvid);
212                 return 1;
213         }
214
215         return 0;
216 }
217
218 /*
219  * Reduce the vid by the max of step or reqvid.
220  * Decreasing vid codes represent increasing voltages:
221  * vid of 0 is 1.550V, vid of 0x1e is 0.800V, vid of 0x1f is off.
222  */
223 static int decrease_vid_code_by_step(struct powernow_k8_data *data, u32 reqvid, u32 step)
224 {
225         if ((data->currvid - reqvid) > step)
226                 reqvid = data->currvid - step;
227
228         if (write_new_vid(data, reqvid))
229                 return 1;
230
231         count_off_vst(data);
232
233         return 0;
234 }
235
236 /* Change the fid and vid, by the 3 phases. */
237 static int transition_fid_vid(struct powernow_k8_data *data, u32 reqfid, u32 reqvid)
238 {
239         if (core_voltage_pre_transition(data, reqvid))
240                 return 1;
241
242         if (core_frequency_transition(data, reqfid))
243                 return 1;
244
245         if (core_voltage_post_transition(data, reqvid))
246                 return 1;
247
248         if (query_current_values_with_pending_wait(data))
249                 return 1;
250
251         if ((reqfid != data->currfid) || (reqvid != data->currvid)) {
252                 printk(KERN_ERR PFX "failed (cpu%d): req 0x%x 0x%x, curr 0x%x 0x%x\n",
253                                 smp_processor_id(),
254                                 reqfid, reqvid, data->currfid, data->currvid);
255                 return 1;
256         }
257
258         dprintk("transitioned (cpu%d): new fid 0x%x, vid 0x%x\n",
259                 smp_processor_id(), data->currfid, data->currvid);
260
261         return 0;
262 }
263
264 /* Phase 1 - core voltage transition ... setup voltage */
265 static int core_voltage_pre_transition(struct powernow_k8_data *data, u32 reqvid)
266 {
267         u32 rvosteps = data->rvo;
268         u32 savefid = data->currfid;
269
270         dprintk("ph1 (cpu%d): start, currfid 0x%x, currvid 0x%x, reqvid 0x%x, rvo 0x%x\n",
271                 smp_processor_id(),
272                 data->currfid, data->currvid, reqvid, data->rvo);
273
274         while (data->currvid > reqvid) {
275                 dprintk("ph1: curr 0x%x, req vid 0x%x\n",
276                         data->currvid, reqvid);
277                 if (decrease_vid_code_by_step(data, reqvid, data->vidmvs))
278                         return 1;
279         }
280
281         while (rvosteps > 0) {
282                 if (data->currvid == 0) {
283                         rvosteps = 0;
284                 } else {
285                         dprintk("ph1: changing vid for rvo, req 0x%x\n",
286                                 data->currvid - 1);
287                         if (decrease_vid_code_by_step(data, data->currvid - 1, 1))
288                                 return 1;
289                         rvosteps--;
290                 }
291         }
292
293         if (query_current_values_with_pending_wait(data))
294                 return 1;
295
296         if (savefid != data->currfid) {
297                 printk(KERN_ERR PFX "ph1 err, currfid changed 0x%x\n", data->currfid);
298                 return 1;
299         }
300
301         dprintk("ph1 complete, currfid 0x%x, currvid 0x%x\n",
302                 data->currfid, data->currvid);
303
304         return 0;
305 }
306
307 /* Phase 2 - core frequency transition */
308 static int core_frequency_transition(struct powernow_k8_data *data, u32 reqfid)
309 {
310         u32 vcoreqfid;
311         u32 vcocurrfid;
312         u32 vcofiddiff;
313         u32 savevid = data->currvid;
314
315         if ((reqfid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
316                 printk(KERN_ERR PFX "ph2: illegal lo-lo transition 0x%x 0x%x\n",
317                         reqfid, data->currfid);
318                 return 1;
319         }
320
321         if (data->currfid == reqfid) {
322                 printk(KERN_ERR PFX "ph2 null fid transition 0x%x\n", data->currfid);
323                 return 0;
324         }
325
326         dprintk("ph2 (cpu%d): starting, currfid 0x%x, currvid 0x%x, reqfid 0x%x\n",
327                 smp_processor_id(),
328                 data->currfid, data->currvid, reqfid);
329
330         vcoreqfid = convert_fid_to_vco_fid(reqfid);
331         vcocurrfid = convert_fid_to_vco_fid(data->currfid);
332         vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
333             : vcoreqfid - vcocurrfid;
334
335         while (vcofiddiff > 2) {
336                 if (reqfid > data->currfid) {
337                         if (data->currfid > LO_FID_TABLE_TOP) {
338                                 if (write_new_fid(data, data->currfid + 2)) {
339                                         return 1;
340                                 }
341                         } else {
342                                 if (write_new_fid
343                                     (data, 2 + convert_fid_to_vco_fid(data->currfid))) {
344                                         return 1;
345                                 }
346                         }
347                 } else {
348                         if (write_new_fid(data, data->currfid - 2))
349                                 return 1;
350                 }
351
352                 vcocurrfid = convert_fid_to_vco_fid(data->currfid);
353                 vcofiddiff = vcocurrfid > vcoreqfid ? vcocurrfid - vcoreqfid
354                     : vcoreqfid - vcocurrfid;
355         }
356
357         if (write_new_fid(data, reqfid))
358                 return 1;
359
360         if (query_current_values_with_pending_wait(data))
361                 return 1;
362
363         if (data->currfid != reqfid) {
364                 printk(KERN_ERR PFX
365                         "ph2: mismatch, failed fid transition, curr 0x%x, req 0x%x\n",
366                         data->currfid, reqfid);
367                 return 1;
368         }
369
370         if (savevid != data->currvid) {
371                 printk(KERN_ERR PFX "ph2: vid changed, save 0x%x, curr 0x%x\n",
372                         savevid, data->currvid);
373                 return 1;
374         }
375
376         dprintk("ph2 complete, currfid 0x%x, currvid 0x%x\n",
377                 data->currfid, data->currvid);
378
379         return 0;
380 }
381
382 /* Phase 3 - core voltage transition flow ... jump to the final vid. */
383 static int core_voltage_post_transition(struct powernow_k8_data *data, u32 reqvid)
384 {
385         u32 savefid = data->currfid;
386         u32 savereqvid = reqvid;
387
388         dprintk("ph3 (cpu%d): starting, currfid 0x%x, currvid 0x%x\n",
389                 smp_processor_id(),
390                 data->currfid, data->currvid);
391
392         if (reqvid != data->currvid) {
393                 if (write_new_vid(data, reqvid))
394                         return 1;
395
396                 if (savefid != data->currfid) {
397                         printk(KERN_ERR PFX
398                                "ph3: bad fid change, save 0x%x, curr 0x%x\n",
399                                savefid, data->currfid);
400                         return 1;
401                 }
402
403                 if (data->currvid != reqvid) {
404                         printk(KERN_ERR PFX
405                                "ph3: failed vid transition\n, req 0x%x, curr 0x%x",
406                                reqvid, data->currvid);
407                         return 1;
408                 }
409         }
410
411         if (query_current_values_with_pending_wait(data))
412                 return 1;
413
414         if (savereqvid != data->currvid) {
415                 dprintk("ph3 failed, currvid 0x%x\n", data->currvid);
416                 return 1;
417         }
418
419         if (savefid != data->currfid) {
420                 dprintk("ph3 failed, currfid changed 0x%x\n",
421                         data->currfid);
422                 return 1;
423         }
424
425         dprintk("ph3 complete, currfid 0x%x, currvid 0x%x\n",
426                 data->currfid, data->currvid);
427
428         return 0;
429 }
430
431 static int check_supported_cpu(unsigned int cpu)
432 {
433         cpumask_t oldmask = CPU_MASK_ALL;
434         u32 eax, ebx, ecx, edx;
435         unsigned int rc = 0;
436
437         oldmask = current->cpus_allowed;
438         set_cpus_allowed(current, cpumask_of_cpu(cpu));
439         schedule();
440
441         if (smp_processor_id() != cpu) {
442                 printk(KERN_ERR "limiting to cpu %u failed\n", cpu);
443                 goto out;
444         }
445
446         if (current_cpu_data.x86_vendor != X86_VENDOR_AMD)
447                 goto out;
448
449         eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
450         if (((eax & CPUID_USE_XFAM_XMOD) != CPUID_USE_XFAM_XMOD) ||
451             ((eax & CPUID_XFAM) != CPUID_XFAM_K8) ||
452             ((eax & CPUID_XMOD) > CPUID_XMOD_REV_E)) {
453                 printk(KERN_INFO PFX "Processor cpuid %x not supported\n", eax);
454                 goto out;
455         }
456
457         eax = cpuid_eax(CPUID_GET_MAX_CAPABILITIES);
458         if (eax < CPUID_FREQ_VOLT_CAPABILITIES) {
459                 printk(KERN_INFO PFX
460                        "No frequency change capabilities detected\n");
461                 goto out;
462         }
463
464         cpuid(CPUID_FREQ_VOLT_CAPABILITIES, &eax, &ebx, &ecx, &edx);
465         if ((edx & P_STATE_TRANSITION_CAPABLE) != P_STATE_TRANSITION_CAPABLE) {
466                 printk(KERN_INFO PFX "Power state transitions not supported\n");
467                 goto out;
468         }
469
470         rc = 1;
471
472 out:
473         set_cpus_allowed(current, oldmask);
474         schedule();
475         return rc;
476
477 }
478
479 static int check_pst_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
480 {
481         unsigned int j;
482         u8 lastfid = 0xff;
483
484         for (j = 0; j < data->numps; j++) {
485                 if (pst[j].vid > LEAST_VID) {
486                         printk(KERN_ERR PFX "vid %d invalid : 0x%x\n", j, pst[j].vid);
487                         return -EINVAL;
488                 }
489                 if (pst[j].vid < data->rvo) {   /* vid + rvo >= 0 */
490                         printk(KERN_ERR BFX "0 vid exceeded with pstate %d\n", j);
491                         return -ENODEV;
492                 }
493                 if (pst[j].vid < maxvid + data->rvo) {  /* vid + rvo >= maxvid */
494                         printk(KERN_ERR BFX "maxvid exceeded with pstate %d\n", j);
495                         return -ENODEV;
496                 }
497                 if ((pst[j].fid > MAX_FID)
498                     || (pst[j].fid & 1)
499                     || (j && (pst[j].fid < HI_FID_TABLE_BOTTOM))) {
500                         /* Only first fid is allowed to be in "low" range */
501                         printk(KERN_ERR PFX "fid %d invalid : 0x%x\n", j, pst[j].fid);
502                         return -EINVAL;
503                 }
504                 if (pst[j].fid < lastfid)
505                         lastfid = pst[j].fid;
506         }
507         if (lastfid & 1) {
508                 printk(KERN_ERR PFX "lastfid invalid\n");
509                 return -EINVAL;
510         }
511         if (lastfid > LO_FID_TABLE_TOP)
512                 printk(KERN_INFO PFX  "first fid not from lo freq table\n");
513
514         return 0;
515 }
516
517 static void print_basics(struct powernow_k8_data *data)
518 {
519         int j;
520         for (j = 0; j < data->numps; j++) {
521                 if (data->powernow_table[j].frequency != CPUFREQ_ENTRY_INVALID)
522                         printk(KERN_INFO PFX "   %d : fid 0x%x (%d MHz), vid 0x%x (%d mV)\n", j,
523                                 data->powernow_table[j].index & 0xff,
524                                 data->powernow_table[j].frequency/1000,
525                                 data->powernow_table[j].index >> 8,
526                                 find_millivolts_from_vid(data, data->powernow_table[j].index >> 8));
527         }
528         if (data->batps)
529                 printk(KERN_INFO PFX "Only %d pstates on battery\n", data->batps);
530 }
531
532 static int fill_powernow_table(struct powernow_k8_data *data, struct pst_s *pst, u8 maxvid)
533 {
534         struct cpufreq_frequency_table *powernow_table;
535         unsigned int j;
536
537         if (data->batps) {    /* use ACPI support to get full speed on mains power */
538                 printk(KERN_WARNING PFX "Only %d pstates usable (use ACPI driver for full range\n", data->batps);
539                 data->numps = data->batps;
540         }
541
542         for ( j=1; j<data->numps; j++ ) {
543                 if (pst[j-1].fid >= pst[j].fid) {
544                         printk(KERN_ERR PFX "PST out of sequence\n");
545                         return -EINVAL;
546                 }
547         }
548
549         if (data->numps < 2) {
550                 printk(KERN_ERR PFX "no p states to transition\n");
551                 return -ENODEV;
552         }
553
554         if (check_pst_table(data, pst, maxvid))
555                 return -EINVAL;
556
557         powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
558                 * (data->numps + 1)), GFP_KERNEL);
559         if (!powernow_table) {
560                 printk(KERN_ERR PFX "powernow_table memory alloc failure\n");
561                 return -ENOMEM;
562         }
563
564         for (j = 0; j < data->numps; j++) {
565                 powernow_table[j].index = pst[j].fid; /* lower 8 bits */
566                 powernow_table[j].index |= (pst[j].vid << 8); /* upper 8 bits */
567                 powernow_table[j].frequency = find_khz_freq_from_fid(pst[j].fid);
568         }
569         powernow_table[data->numps].frequency = CPUFREQ_TABLE_END;
570         powernow_table[data->numps].index = 0;
571
572         if (query_current_values_with_pending_wait(data)) {
573                 kfree(powernow_table);
574                 return -EIO;
575         }
576
577         dprintk("cfid 0x%x, cvid 0x%x\n", data->currfid, data->currvid);
578         data->powernow_table = powernow_table;
579         print_basics(data);
580
581         for (j = 0; j < data->numps; j++)
582                 if ((pst[j].fid==data->currfid) && (pst[j].vid==data->currvid))
583                         return 0;
584
585         dprintk("currfid/vid do not match PST, ignoring\n");
586         return 0;
587 }
588
589 /* Find and validate the PSB/PST table in BIOS. */
590 static int find_psb_table(struct powernow_k8_data *data)
591 {
592         struct psb_s *psb;
593         unsigned int i;
594         u32 mvs;
595         u8 maxvid;
596         u32 cpst = 0;
597         u32 thiscpuid;
598
599         for (i = 0xc0000; i < 0xffff0; i += 0x10) {
600                 /* Scan BIOS looking for the signature. */
601                 /* It can not be at ffff0 - it is too big. */
602
603                 psb = phys_to_virt(i);
604                 if (memcmp(psb, PSB_ID_STRING, PSB_ID_STRING_LEN) != 0)
605                         continue;
606
607                 dprintk("found PSB header at 0x%p\n", psb);
608
609                 dprintk("table vers: 0x%x\n", psb->tableversion);
610                 if (psb->tableversion != PSB_VERSION_1_4) {
611                         printk(KERN_INFO BFX "PSB table is not v1.4\n");
612                         return -ENODEV;
613                 }
614
615                 dprintk("flags: 0x%x\n", psb->flags1);
616                 if (psb->flags1) {
617                         printk(KERN_ERR BFX "unknown flags\n");
618                         return -ENODEV;
619                 }
620
621                 data->vstable = psb->voltagestabilizationtime;
622                 dprintk("voltage stabilization time: %d(*20us)\n", data->vstable);
623
624                 dprintk("flags2: 0x%x\n", psb->flags2);
625                 data->rvo = psb->flags2 & 3;
626                 data->irt = ((psb->flags2) >> 2) & 3;
627                 mvs = ((psb->flags2) >> 4) & 3;
628                 data->vidmvs = 1 << mvs;
629                 data->batps = ((psb->flags2) >> 6) & 3;
630
631                 dprintk("ramp voltage offset: %d\n", data->rvo);
632                 dprintk("isochronous relief time: %d\n", data->irt);
633                 dprintk("maximum voltage step: %d - 0x%x\n", mvs, data->vidmvs);
634
635                 dprintk("numpst: 0x%x\n", psb->numpst);
636                 cpst = psb->numpst;
637                 if ((psb->cpuid == 0x00000fc0) || (psb->cpuid == 0x00000fe0) ){
638                         thiscpuid = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
639                         if ((thiscpuid == 0x00000fc0) || (thiscpuid == 0x00000fe0) ) {
640                                 cpst = 1;
641                         }
642                 }
643                 if (cpst != 1) {
644                         printk(KERN_ERR BFX "numpst must be 1\n");
645                         return -ENODEV;
646                 }
647
648                 data->plllock = psb->plllocktime;
649                 dprintk("plllocktime: 0x%x (units 1us)\n", psb->plllocktime);
650                 dprintk("maxfid: 0x%x\n", psb->maxfid);
651                 dprintk("maxvid: 0x%x\n", psb->maxvid);
652                 maxvid = psb->maxvid;
653
654                 data->numps = psb->numpstates;
655                 dprintk("numpstates: 0x%x\n", data->numps);
656                 return fill_powernow_table(data, (struct pst_s *)(psb+1), maxvid);
657         }
658         /*
659          * If you see this message, complain to BIOS manufacturer. If
660          * he tells you "we do not support Linux" or some similar
661          * nonsense, remember that Windows 2000 uses the same legacy
662          * mechanism that the old Linux PSB driver uses. Tell them it
663          * is broken with Windows 2000.
664          *
665          * The reference to the AMD documentation is chapter 9 in the
666          * BIOS and Kernel Developer's Guide, which is available on
667          * www.amd.com
668          */
669         printk(KERN_ERR PFX "BIOS error - no PSB\n");
670         return -ENODEV;
671 }
672
673 #ifdef CONFIG_X86_POWERNOW_K8_ACPI
674 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index)
675 {
676         if (!data->acpi_data.state_count)
677                 return;
678
679         data->irt = (data->acpi_data.states[index].control >> IRT_SHIFT) & IRT_MASK;
680         data->rvo = (data->acpi_data.states[index].control >> RVO_SHIFT) & RVO_MASK;
681         data->plllock = (data->acpi_data.states[index].control >> PLL_L_SHIFT) & PLL_L_MASK;
682         data->vidmvs = 1 << ((data->acpi_data.states[index].control >> MVS_SHIFT) & MVS_MASK);
683         data->vstable = (data->acpi_data.states[index].control >> VST_SHIFT) & VST_MASK;
684 }
685
686 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data)
687 {
688         int i;
689         int cntlofreq = 0;
690         struct cpufreq_frequency_table *powernow_table;
691
692         if (acpi_processor_register_performance(&data->acpi_data, data->cpu)) {
693                 dprintk("register performance failed\n");
694                 return -EIO;
695         }
696
697         /* verify the data contained in the ACPI structures */
698         if (data->acpi_data.state_count <= 1) {
699                 dprintk("No ACPI P-States\n");
700                 goto err_out;
701         }
702
703         if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
704                 (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
705                 dprintk("Invalid control/status registers (%x - %x)\n",
706                         data->acpi_data.control_register.space_id,
707                         data->acpi_data.status_register.space_id);
708                 goto err_out;
709         }
710
711         /* fill in data->powernow_table */
712         powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table)
713                 * (data->acpi_data.state_count + 1)), GFP_KERNEL);
714         if (!powernow_table) {
715                 dprintk("powernow_table memory alloc failure\n");
716                 goto err_out;
717         }
718
719         for (i = 0; i < data->acpi_data.state_count; i++) {
720                 u32 fid = data->acpi_data.states[i].control & FID_MASK;
721                 u32 vid = (data->acpi_data.states[i].control >> VID_SHIFT) & VID_MASK;
722
723                 dprintk("   %d : fid 0x%x, vid 0x%x\n", i, fid, vid);
724
725                 powernow_table[i].index = fid; /* lower 8 bits */
726                 powernow_table[i].index |= (vid << 8); /* upper 8 bits */
727                 powernow_table[i].frequency = find_khz_freq_from_fid(fid);
728
729                 /* verify frequency is OK */
730                 if ((powernow_table[i].frequency > (MAX_FREQ * 1000)) ||
731                         (powernow_table[i].frequency < (MIN_FREQ * 1000))) {
732                         dprintk("invalid freq %u kHz, ignoring\n", powernow_table[i].frequency);
733                         powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
734                         continue;
735                 }
736
737                 /* verify voltage is OK - BIOSs are using "off" to indicate invalid */
738                 if (vid == 0x1f) {
739                         dprintk("invalid vid %u, ignoring\n", vid);
740                         powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
741                         continue;
742                 }
743
744                 if (fid < HI_FID_TABLE_BOTTOM) {
745                         if (cntlofreq) {
746                                 /* if both entries are the same, ignore this
747                                  * one... 
748                                  */
749                                 if ((powernow_table[i].frequency != powernow_table[cntlofreq].frequency) ||
750                                     (powernow_table[i].index != powernow_table[cntlofreq].index)) {
751                                         printk(KERN_ERR PFX "Too many lo freq table entries\n");
752                                         goto err_out_mem;
753                                 }
754                                 
755                                 dprintk("double low frequency table entry, ignoring it.\n");
756                                 powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
757                                 continue;
758                         } else
759                                 cntlofreq = i;
760                 }
761
762                 if (powernow_table[i].frequency != (data->acpi_data.states[i].core_frequency * 1000)) {
763                         printk(KERN_INFO PFX "invalid freq entries %u kHz vs. %u kHz\n",
764                                 powernow_table[i].frequency,
765                                 (unsigned int) (data->acpi_data.states[i].core_frequency * 1000));
766                         powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
767                         continue;
768                 }
769         }
770
771         powernow_table[data->acpi_data.state_count].frequency = CPUFREQ_TABLE_END;
772         powernow_table[data->acpi_data.state_count].index = 0;
773         data->powernow_table = powernow_table;
774
775         /* fill in data */
776         data->numps = data->acpi_data.state_count;
777         print_basics(data);
778         powernow_k8_acpi_pst_values(data, 0);
779
780         /* notify BIOS that we exist */
781         acpi_processor_notify_smm(THIS_MODULE);
782
783         return 0;
784
785 err_out_mem:
786         kfree(powernow_table);
787
788 err_out:
789         acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
790
791         /* data->acpi_data.state_count informs us at ->exit() whether ACPI was used */
792         data->acpi_data.state_count = 0;
793
794         return -ENODEV;
795 }
796
797 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data)
798 {
799         if (data->acpi_data.state_count)
800                 acpi_processor_unregister_performance(&data->acpi_data, data->cpu);
801 }
802
803 #else
804 static int powernow_k8_cpu_init_acpi(struct powernow_k8_data *data) { return -ENODEV; }
805 static void powernow_k8_cpu_exit_acpi(struct powernow_k8_data *data) { return; }
806 static void powernow_k8_acpi_pst_values(struct powernow_k8_data *data, unsigned int index) { return; }
807 #endif /* CONFIG_X86_POWERNOW_K8_ACPI */
808
809 /* Take a frequency, and issue the fid/vid transition command */
810 static int transition_frequency(struct powernow_k8_data *data, unsigned int index)
811 {
812         u32 fid;
813         u32 vid;
814         int res;
815         struct cpufreq_freqs freqs;
816
817         dprintk("cpu %d transition to index %u\n", smp_processor_id(), index);
818
819         /* fid are the lower 8 bits of the index we stored into
820          * the cpufreq frequency table in find_psb_table, vid are 
821          * the upper 8 bits.
822          */
823
824         fid = data->powernow_table[index].index & 0xFF;
825         vid = (data->powernow_table[index].index & 0xFF00) >> 8;
826
827         dprintk("table matched fid 0x%x, giving vid 0x%x\n", fid, vid);
828
829         if (query_current_values_with_pending_wait(data))
830                 return 1;
831
832         if ((data->currvid == vid) && (data->currfid == fid)) {
833                 dprintk("target matches current values (fid 0x%x, vid 0x%x)\n",
834                         fid, vid);
835                 return 0;
836         }
837
838         if ((fid < HI_FID_TABLE_BOTTOM) && (data->currfid < HI_FID_TABLE_BOTTOM)) {
839                 printk("ignoring illegal change in lo freq table-%x to 0x%x\n",
840                        data->currfid, fid);
841                 return 1;
842         }
843
844         dprintk("cpu %d, changing to fid 0x%x, vid 0x%x\n",
845                 smp_processor_id(), fid, vid);
846
847         freqs.cpu = data->cpu;
848
849         freqs.old = find_khz_freq_from_fid(data->currfid);
850         freqs.new = find_khz_freq_from_fid(fid);
851         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
852
853         down(&fidvid_sem);
854         res = transition_fid_vid(data, fid, vid);
855         up(&fidvid_sem);
856
857         freqs.new = find_khz_freq_from_fid(data->currfid);
858         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
859
860         return res;
861 }
862
863 /* Driver entry point to switch to the target frequency */
864 static int powernowk8_target(struct cpufreq_policy *pol, unsigned targfreq, unsigned relation)
865 {
866         cpumask_t oldmask = CPU_MASK_ALL;
867         struct powernow_k8_data *data = powernow_data[pol->cpu];
868         u32 checkfid = data->currfid;
869         u32 checkvid = data->currvid;
870         unsigned int newstate;
871         int ret = -EIO;
872
873         /* only run on specific CPU from here on */
874         oldmask = current->cpus_allowed;
875         set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
876         schedule();
877
878         if (smp_processor_id() != pol->cpu) {
879                 printk(KERN_ERR "limiting to cpu %u failed\n", pol->cpu);
880                 goto err_out;
881         }
882
883         if (pending_bit_stuck()) {
884                 printk(KERN_ERR PFX "failing targ, change pending bit set\n");
885                 goto err_out;
886         }
887
888         dprintk("targ: cpu %d, %d kHz, min %d, max %d, relation %d\n",
889                 pol->cpu, targfreq, pol->min, pol->max, relation);
890
891         if (query_current_values_with_pending_wait(data)) {
892                 ret = -EIO;
893                 goto err_out;
894         }
895
896         dprintk("targ: curr fid 0x%x, vid 0x%x\n",
897                 data->currfid, data->currvid);
898
899         if ((checkvid != data->currvid) || (checkfid != data->currfid)) {
900                 printk(KERN_ERR PFX
901                        "error - out of sync, fid 0x%x 0x%x, vid 0x%x 0x%x\n",
902                        checkfid, data->currfid, checkvid, data->currvid);
903         }
904
905         if (cpufreq_frequency_table_target(pol, data->powernow_table, targfreq, relation, &newstate))
906                 goto err_out;
907
908         powernow_k8_acpi_pst_values(data, newstate);
909
910         if (transition_frequency(data, newstate)) {
911                 printk(KERN_ERR PFX "transition frequency failed\n");
912                 ret = 1;
913                 goto err_out;
914         }
915
916         pol->cur = find_khz_freq_from_fid(data->currfid);
917         ret = 0;
918
919 err_out:
920         set_cpus_allowed(current, oldmask);
921         schedule();
922
923         return ret;
924 }
925
926 /* Driver entry point to verify the policy and range of frequencies */
927 static int powernowk8_verify(struct cpufreq_policy *pol)
928 {
929         struct powernow_k8_data *data = powernow_data[pol->cpu];
930
931         return cpufreq_frequency_table_verify(pol, data->powernow_table);
932 }
933
934 /* per CPU init entry point to the driver */
935 static int __init powernowk8_cpu_init(struct cpufreq_policy *pol)
936 {
937         struct powernow_k8_data *data;
938         cpumask_t oldmask = CPU_MASK_ALL;
939         int rc;
940
941         if (!check_supported_cpu(pol->cpu))
942                 return -ENODEV;
943
944         data = kmalloc(sizeof(struct powernow_k8_data), GFP_KERNEL);
945         if (!data) {
946                 printk(KERN_ERR PFX "unable to alloc powernow_k8_data");
947                 return -ENOMEM;
948         }
949         memset(data,0,sizeof(struct powernow_k8_data));
950
951         data->cpu = pol->cpu;
952
953         if (powernow_k8_cpu_init_acpi(data)) {
954                 /*
955                  * Use the PSB BIOS structure. This is only availabe on
956                  * an UP version, and is deprecated by AMD.
957                  */
958
959                 if ((num_online_cpus() != 1) || (num_possible_cpus() != 1)) {
960                         printk(KERN_INFO PFX "MP systems not supported by PSB BIOS structure\n");
961                         kfree(data);
962                         return -ENODEV;
963                 }
964                 if (pol->cpu != 0) {
965                         printk(KERN_ERR PFX "init not cpu 0\n");
966                         kfree(data);
967                         return -ENODEV;
968                 }
969                 rc = find_psb_table(data);
970                 if (rc) {
971                         kfree(data);
972                         return -ENODEV;
973                 }
974         }
975
976         /* only run on specific CPU from here on */
977         oldmask = current->cpus_allowed;
978         set_cpus_allowed(current, cpumask_of_cpu(pol->cpu));
979         schedule();
980
981         if (smp_processor_id() != pol->cpu) {
982                 printk(KERN_ERR "limiting to cpu %u failed\n", pol->cpu);
983                 goto err_out;
984         }
985
986         if (pending_bit_stuck()) {
987                 printk(KERN_ERR PFX "failing init, change pending bit set\n");
988                 goto err_out;
989         }
990
991         if (query_current_values_with_pending_wait(data))
992                 goto err_out;
993
994         fidvid_msr_init();
995
996         /* run on any CPU again */
997         set_cpus_allowed(current, oldmask);
998         schedule();
999
1000         pol->governor = CPUFREQ_DEFAULT_GOVERNOR;
1001
1002         /* Take a crude guess here. 
1003          * That guess was in microseconds, so multiply with 1000 */
1004         pol->cpuinfo.transition_latency = (((data->rvo + 8) * data->vstable * VST_UNITS_20US)
1005             + (3 * (1 << data->irt) * 10)) * 1000;
1006
1007         pol->cur = find_khz_freq_from_fid(data->currfid);
1008         dprintk("policy current frequency %d kHz\n", pol->cur);
1009
1010         /* min/max the cpu is capable of */
1011         if (cpufreq_frequency_table_cpuinfo(pol, data->powernow_table)) {
1012                 printk(KERN_ERR PFX "invalid powernow_table\n");
1013                 kfree(data->powernow_table);
1014                 kfree(data);
1015                 return -EINVAL;
1016         }
1017
1018         cpufreq_frequency_table_get_attr(data->powernow_table, pol->cpu);
1019
1020         printk("cpu_init done, current fid 0x%x, vid 0x%x\n",
1021                data->currfid, data->currvid);
1022
1023         powernow_data[pol->cpu] = data;
1024
1025         return 0;
1026
1027 err_out:
1028         set_cpus_allowed(current, oldmask);
1029         schedule();
1030
1031         kfree(data);
1032         return -ENODEV;
1033 }
1034
1035 static int __devexit powernowk8_cpu_exit (struct cpufreq_policy *pol)
1036 {
1037         struct powernow_k8_data *data = powernow_data[pol->cpu];
1038
1039         if (!data)
1040                 return -EINVAL;
1041
1042         powernow_k8_cpu_exit_acpi(data);
1043
1044         cpufreq_frequency_table_put_attr(pol->cpu);
1045
1046         kfree(data->powernow_table);
1047         kfree(data);
1048
1049         return 0;
1050 }
1051
1052 static unsigned int powernowk8_get (unsigned int cpu)
1053 {
1054         struct powernow_k8_data *data = powernow_data[cpu];
1055         cpumask_t oldmask = current->cpus_allowed;
1056         unsigned int khz = 0;
1057
1058         set_cpus_allowed(current, cpumask_of_cpu(cpu));
1059         if (smp_processor_id() != cpu) {
1060                 printk(KERN_ERR PFX "limiting to CPU %d failed in powernowk8_get\n", cpu);
1061                 set_cpus_allowed(current, oldmask);
1062                 return 0;
1063         }
1064         preempt_disable();
1065
1066         if (query_current_values_with_pending_wait(data))
1067                 goto out;
1068
1069         khz = find_khz_freq_from_fid(data->currfid);
1070
1071  out:
1072         preempt_enable_no_resched();
1073         set_cpus_allowed(current, oldmask);
1074
1075         return khz;
1076 }
1077
1078 static struct freq_attr* powernow_k8_attr[] = {
1079         &cpufreq_freq_attr_scaling_available_freqs,
1080         NULL,
1081 };
1082
1083 static struct cpufreq_driver cpufreq_amd64_driver = {
1084         .verify = powernowk8_verify,
1085         .target = powernowk8_target,
1086         .init = powernowk8_cpu_init,
1087         .exit = __devexit_p(powernowk8_cpu_exit),
1088         .get = powernowk8_get,
1089         .name = "powernow-k8",
1090         .owner = THIS_MODULE,
1091         .attr = powernow_k8_attr,
1092 };
1093
1094 /* driver entry point for init */
1095 static int __init powernowk8_init(void)
1096 {
1097         unsigned int i, supported_cpus = 0;
1098
1099         for (i=0; i<NR_CPUS; i++) {
1100                 if (!cpu_online(i))
1101                         continue;
1102                 if (check_supported_cpu(i))
1103                         supported_cpus++;
1104         }
1105
1106         if (supported_cpus == num_online_cpus()) {
1107                 printk(KERN_INFO PFX "Found %d AMD Athlon 64 / Opteron processors (" VERSION ")\n",
1108                         supported_cpus);
1109                 return cpufreq_register_driver(&cpufreq_amd64_driver);
1110         }
1111
1112         return -ENODEV;
1113 }
1114
1115 /* driver entry point for term */
1116 static void __exit powernowk8_exit(void)
1117 {
1118         dprintk("exit\n");
1119
1120         cpufreq_unregister_driver(&cpufreq_amd64_driver);
1121 }
1122
1123 MODULE_AUTHOR("Paul Devriendt <paul.devriendt@amd.com>");
1124 MODULE_DESCRIPTION("AMD Athlon 64 and Opteron processor frequency driver.");
1125 MODULE_LICENSE("GPL");
1126
1127 late_initcall(powernowk8_init);
1128 module_exit(powernowk8_exit);