2 * AMD K7 Powernow driver.
3 * (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs.
5 * Licensed under the terms of the GNU GPL License version 2.
6 * Based upon datasheets & sample CPUs kindly provided by AMD.
8 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
10 * Errata 5: Processor may fail to execute a FID/VID change in presence of interrupt.
11 * - We cli/sti on stepping A0 CPUs around the FID/VID transition.
12 * Errata 15: Processors with half frequency multipliers may hang upon wakeup from disconnect.
13 * - We disable half multipliers if ACPI is used on A0 stepping CPUs.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/cpufreq.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
24 #include <asm/timex.h>
26 #include <asm/system.h>
28 #include "powernow-k7.h"
33 #define dprintk(msg...) printk(msg)
35 #define dprintk(msg...) do { } while(0)
38 #define PFX "powernow: "
59 /* divide by 1000 to get VID. */
60 static int mobile_vid_table[32] = {
61 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
62 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
63 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
64 1075, 1050, 1024, 1000, 975, 950, 925, 0,
67 /* divide by 10 to get FID. */
68 static int fid_codes[32] = {
69 110, 115, 120, 125, 50, 55, 60, 65,
70 70, 75, 80, 85, 90, 95, 100, 105,
71 30, 190, 40, 200, 130, 135, 140, 210,
72 150, 225, 160, 165, 170, 180, -1, -1,
75 static struct cpufreq_frequency_table *powernow_table;
77 static unsigned int can_scale_bus;
78 static unsigned int can_scale_vid;
79 static unsigned int minimum_speed=-1;
80 static unsigned int maximum_speed;
81 static unsigned int number_scales;
82 static unsigned int fsb;
83 static unsigned int latency;
87 static int check_powernow(void)
89 struct cpuinfo_x86 *c = cpu_data;
90 unsigned int maxei, eax, ebx, ecx, edx;
92 if (c->x86_vendor != X86_VENDOR_AMD) {
93 printk (KERN_INFO PFX "AMD processor not detected.\n");
98 printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
102 printk (KERN_INFO PFX "AMD K7 CPU detected.\n");
104 if ((c->x86_model == 6) && (c->x86_mask == 0)) {
105 printk (KERN_INFO PFX "K7 660[A0] core detected, enabling errata workarounds\n");
109 /* Get maximum capabilities */
110 maxei = cpuid_eax (0x80000000);
111 if (maxei < 0x80000007) { /* Any powernow info ? */
112 printk (KERN_INFO PFX "No powernow capabilities detected\n");
116 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
117 printk (KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
120 printk ("frequency");
124 if ((edx & (1 << 1 | 1 << 2)) == 0x6)
132 if (!(edx & (1 << 1 | 1 << 2))) {
133 printk ("nothing.\n");
142 static int get_ranges (unsigned char *pst)
144 unsigned int j, speed;
147 powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
150 memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
152 for (j=0 ; j < number_scales; j++) {
155 powernow_table[j].frequency = fsb * fid_codes[fid] * 100;
156 powernow_table[j].index = fid; /* lower 8 bits */
158 speed = fsb * (fid_codes[fid]/10);
159 if ((fid_codes[fid] % 10)==5) {
161 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
163 powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
167 dprintk (KERN_INFO PFX " FID: 0x%x (%d.%dx [%dMHz])\t", fid,
168 fid_codes[fid] / 10, fid_codes[fid] % 10, speed);
170 if (speed < minimum_speed)
171 minimum_speed = speed;
172 if (speed > maximum_speed)
173 maximum_speed = speed;
176 powernow_table[j].index |= (vid << 8); /* upper 8 bits */
177 dprintk ("VID: 0x%x (%d.%03dV)\n", vid, mobile_vid_table[vid]/1000,
178 mobile_vid_table[vid]%1000);
182 powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
183 powernow_table[number_scales].index = 0;
189 static void change_FID(int fid)
191 union msr_fidvidctl fidvidctl;
193 if (fidvidctl.bits.FID != fid) {
194 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
195 fidvidctl.bits.SGTC = latency;
196 fidvidctl.bits.FID = fid;
197 fidvidctl.bits.FIDC = 1;
198 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
203 static void change_VID(int vid)
205 union msr_fidvidctl fidvidctl;
207 if (fidvidctl.bits.VID != vid) {
208 rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
209 fidvidctl.bits.VID = vid;
210 fidvidctl.bits.VIDC = 1;
211 wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
216 static void change_speed (unsigned int index)
219 struct cpufreq_freqs freqs;
220 union msr_fidvidstatus fidvidstatus;
223 /* fid are the lower 8 bits of the index we stored into
224 * the cpufreq frequency table in powernow_decode_bios,
225 * vid are the upper 8 bits.
228 fid = powernow_table[index].index & 0xFF;
229 vid = (powernow_table[index].index & 0xFF00) >> 8;
233 cfid = fidvidstatus.bits.CFID;
234 rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
235 freqs.old = fsb * fid_codes[cfid] * 100;
236 freqs.new = powernow_table[index].frequency;
238 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
240 /* Now do the magic poking into the MSRs. */
242 if (have_a0 == 1) /* A0 errata 5 */
245 if (freqs.old > freqs.new) {
246 /* Going down, so change FID first */
250 /* Going up, so change VID first */
259 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
263 static int powernow_decode_bios (int maxfid, int startvid)
267 struct cpuinfo_x86 *c = cpu_data;
273 etuple = cpuid_eax(0x80000001);
275 etuple |= (c->x86_model<<4)|(c->x86_mask);
277 for (i=0xC0000; i < 0xffff0 ; i+=16) {
281 if (memcmp(p, "AMDK7PNOW!", 10) == 0){
282 dprintk (KERN_INFO PFX "Found PSB header at %p\n", p);
283 psb = (struct psb_s *) p;
284 dprintk (KERN_INFO PFX "Table version: 0x%x\n", psb->tableversion);
285 if (psb->tableversion != 0x12) {
286 printk (KERN_INFO PFX "Sorry, only v1.2 tables supported right now\n");
290 dprintk (KERN_INFO PFX "Flags: 0x%x (", psb->flags);
291 if ((psb->flags & 1)==0) {
296 dprintk (" voltage regulator)\n");
298 latency = psb->settlingtime;
299 dprintk (KERN_INFO PFX "Settling Time: %d microseconds.\n", psb->settlingtime);
300 dprintk (KERN_INFO PFX "Has %d PST tables. (Only dumping ones relevant to this CPU).\n", psb->numpst);
302 p += sizeof (struct psb_s);
304 pst = (struct pst_s *) p;
306 for (i = 0 ; i <psb->numpst; i++) {
307 pst = (struct pst_s *) p;
308 number_scales = pst->numpstates;
310 if ((etuple == pst->cpuid) && (maxfid==pst->maxfid) && (startvid==pst->startvid))
312 dprintk (KERN_INFO PFX "PST:%d (@%p)\n", i, pst);
313 dprintk (KERN_INFO PFX " cpuid: 0x%x\t", pst->cpuid);
314 dprintk ("fsb: %d\t", pst->fsbspeed);
315 dprintk ("maxFID: 0x%x\t", pst->maxfid);
316 dprintk ("startvid: 0x%x\n", pst->startvid);
319 ret = get_ranges ((char *) pst + sizeof (struct pst_s));
323 p = (char *) pst + sizeof (struct pst_s);
324 for (j=0 ; j < number_scales; j++)
337 static int powernow_target (struct cpufreq_policy *policy,
338 unsigned int target_freq,
339 unsigned int relation)
341 unsigned int newstate;
343 if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
346 change_speed(newstate);
352 static int powernow_verify (struct cpufreq_policy *policy)
354 return cpufreq_frequency_table_verify(policy, powernow_table);
358 static int __init powernow_cpu_init (struct cpufreq_policy *policy)
360 union msr_fidvidstatus fidvidstatus;
363 if (policy->cpu != 0)
366 rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
368 result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
372 printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
373 minimum_speed, maximum_speed);
375 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
376 policy->cpuinfo.transition_latency = latency;
377 policy->cur = maximum_speed;
379 return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
382 static struct cpufreq_driver powernow_driver = {
383 .verify = powernow_verify,
384 .target = powernow_target,
385 .init = powernow_cpu_init,
386 .name = "powernow-k7",
387 .owner = THIS_MODULE,
390 static int __init powernow_init (void)
392 if (dmi_broken & BROKEN_CPUFREQ) {
393 printk (KERN_INFO PFX "Disabled at boot time by DMI,\n");
396 if (check_powernow()==0)
398 return cpufreq_register_driver(&powernow_driver);
402 static void __exit powernow_exit (void)
404 cpufreq_unregister_driver(&powernow_driver);
406 kfree(powernow_table);
409 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
410 MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
411 MODULE_LICENSE ("GPL");
413 module_init(powernow_init);
414 module_exit(powernow_exit);