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