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