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