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