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