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