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