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