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