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