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