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