patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / i386 / kernel / cpu / cpufreq / longrun.c
1 /*
2  * (C) 2002 - 2003  Dominik Brodowski <linux@brodo.de>
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h> 
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/cpufreq.h>
14
15 #include <asm/msr.h>
16 #include <asm/processor.h>
17 #include <asm/timex.h>
18
19 static struct cpufreq_driver    longrun_driver;
20
21 /**
22  * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz 
23  * values into per cent values. In TMTA microcode, the following is valid:
24  * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
25  */
26 static unsigned int longrun_low_freq, longrun_high_freq;
27
28
29 /**
30  * longrun_get_policy - get the current LongRun policy
31  * @policy: struct cpufreq_policy where current policy is written into
32  *
33  * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
34  * and MSR_TMTA_LONGRUN_CTRL
35  */
36 static void __init longrun_get_policy(struct cpufreq_policy *policy)
37 {
38         u32 msr_lo, msr_hi;
39
40         rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
41         if (msr_lo & 0x01)
42                 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
43         else
44                 policy->policy = CPUFREQ_POLICY_POWERSAVE;
45         
46         rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
47         msr_lo &= 0x0000007F;
48         msr_hi &= 0x0000007F;
49         
50         if ( longrun_high_freq <= longrun_low_freq ) {
51                 /* Assume degenerate Longrun table */
52                 policy->min = policy->max = longrun_high_freq;
53         } else {
54                 policy->min = longrun_low_freq + msr_lo * 
55                         ((longrun_high_freq - longrun_low_freq) / 100);
56                 policy->max = longrun_low_freq + msr_hi * 
57                         ((longrun_high_freq - longrun_low_freq) / 100);
58         }
59         policy->cpu = 0;
60 }
61
62
63 /**
64  * longrun_set_policy - sets a new CPUFreq policy
65  * @policy: new policy
66  *
67  * Sets a new CPUFreq policy on LongRun-capable processors. This function
68  * has to be called with cpufreq_driver locked.
69  */
70 static int longrun_set_policy(struct cpufreq_policy *policy)
71 {
72         u32 msr_lo, msr_hi;
73         u32 pctg_lo, pctg_hi;
74
75         if (!policy)
76                 return -EINVAL;
77
78         if ( longrun_high_freq <= longrun_low_freq ) {
79                 /* Assume degenerate Longrun table */
80                 pctg_lo = pctg_hi = 100;
81         } else {
82                 pctg_lo = (policy->min - longrun_low_freq) / 
83                         ((longrun_high_freq - longrun_low_freq) / 100);
84                 pctg_hi = (policy->max - longrun_low_freq) / 
85                         ((longrun_high_freq - longrun_low_freq) / 100);
86         }
87
88         if (pctg_hi > 100)
89                 pctg_hi = 100;
90         if (pctg_lo > pctg_hi)
91                 pctg_lo = pctg_hi;
92
93         /* performance or economy mode */
94         rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
95         msr_lo &= 0xFFFFFFFE;
96         switch (policy->policy) {
97         case CPUFREQ_POLICY_PERFORMANCE:
98                 msr_lo |= 0x00000001;
99                 break;
100         case CPUFREQ_POLICY_POWERSAVE:
101                 break;
102         }
103         wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
104
105         /* lower and upper boundary */
106         rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
107         msr_lo &= 0xFFFFFF80;
108         msr_hi &= 0xFFFFFF80;
109         msr_lo |= pctg_lo;
110         msr_hi |= pctg_hi;
111         wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
112
113         return 0;
114 }
115
116
117 /**
118  * longrun_verify_poliy - verifies a new CPUFreq policy
119  * @policy: the policy to verify
120  *
121  * Validates a new CPUFreq policy. This function has to be called with 
122  * cpufreq_driver locked.
123  */
124 static int longrun_verify_policy(struct cpufreq_policy *policy)
125 {
126         if (!policy)
127                 return -EINVAL;
128
129         policy->cpu = 0;
130         cpufreq_verify_within_limits(policy, 
131                 policy->cpuinfo.min_freq, 
132                 policy->cpuinfo.max_freq);
133
134         if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
135             (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
136                 return -EINVAL;
137
138         return 0;
139 }
140
141 static unsigned int longrun_get(unsigned int cpu)
142 {
143         u32 eax, ebx, ecx, edx;
144
145         if (cpu)
146                 return 0;
147
148         cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
149
150         return (eax * 1000);
151 }
152
153 /**
154  * longrun_determine_freqs - determines the lowest and highest possible core frequency
155  * @low_freq: an int to put the lowest frequency into
156  * @high_freq: an int to put the highest frequency into
157  *
158  * Determines the lowest and highest possible core frequencies on this CPU.
159  * This is necessary to calculate the performance percentage according to
160  * TMTA rules:
161  * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
162  */
163 static unsigned int __init longrun_determine_freqs(unsigned int *low_freq, 
164                                                    unsigned int *high_freq)
165 {
166         u32 msr_lo, msr_hi;
167         u32 save_lo, save_hi;
168         u32 eax, ebx, ecx, edx;
169         u32 try_hi;
170         struct cpuinfo_x86 *c = cpu_data;
171
172         if (!low_freq || !high_freq)
173                 return -EINVAL;
174
175         if (cpu_has(c, X86_FEATURE_LRTI)) {
176                 /* if the LongRun Table Interface is present, the
177                  * detection is a bit easier: 
178                  * For minimum frequency, read out the maximum
179                  * level (msr_hi), write that into "currently 
180                  * selected level", and read out the frequency.
181                  * For maximum frequency, read out level zero.
182                  */
183                 /* minimum */
184                 rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
185                 wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
186                 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
187                 *low_freq = msr_lo * 1000; /* to kHz */
188
189                 /* maximum */
190                 wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
191                 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
192                 *high_freq = msr_lo * 1000; /* to kHz */
193
194                 if (*low_freq > *high_freq)
195                         *low_freq = *high_freq;
196                 return 0;
197         }
198
199         /* set the upper border to the value determined during TSC init */
200         *high_freq = (cpu_khz / 1000);
201         *high_freq = *high_freq * 1000;
202
203         /* get current borders */
204         rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
205         save_lo = msr_lo & 0x0000007F;
206         save_hi = msr_hi & 0x0000007F;
207
208         /* if current perf_pctg is larger than 90%, we need to decrease the
209          * upper limit to make the calculation more accurate.
210          */
211         cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
212         /* try decreasing in 10% steps, some processors react only
213          * on some barrier values */
214         for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -=10) {
215                 /* set to 0 to try_hi perf_pctg */
216                 msr_lo &= 0xFFFFFF80;
217                 msr_hi &= 0xFFFFFF80;
218                 msr_lo |= 0;
219                 msr_hi |= try_hi;
220                 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
221
222                 /* read out current core MHz and current perf_pctg */
223                 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
224
225                 /* restore values */
226                 wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi); 
227         }
228
229         /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
230          * eqals
231          * low_freq * ( 1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
232          *
233          * high_freq * perf_pctg is stored tempoarily into "ebx".
234          */
235         ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
236
237         if ((ecx > 95) || (ecx == 0) || (eax < ebx))
238                 return -EIO;
239
240         edx = (eax - ebx) / (100 - ecx); 
241         *low_freq = edx * 1000; /* back to kHz */
242
243         if (*low_freq > *high_freq)
244                 *low_freq = *high_freq;
245
246         return 0;
247 }
248
249
250 static int __init longrun_cpu_init(struct cpufreq_policy *policy)
251 {
252         int                     result = 0;
253
254         /* capability check */
255         if (policy->cpu != 0)
256                 return -ENODEV;
257
258         /* detect low and high frequency */
259         result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
260         if (result)
261                 return result;
262
263         /* cpuinfo and default policy values */
264         policy->cpuinfo.min_freq = longrun_low_freq;
265         policy->cpuinfo.max_freq = longrun_high_freq;
266         policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
267         longrun_get_policy(policy);
268         
269         return 0;
270 }
271
272
273 static struct cpufreq_driver longrun_driver = {
274         .flags          = CPUFREQ_CONST_LOOPS,
275         .verify         = longrun_verify_policy,
276         .setpolicy      = longrun_set_policy,
277         .get            = longrun_get,
278         .init           = longrun_cpu_init,
279         .name           = "longrun",
280         .owner          = THIS_MODULE,
281 };
282
283
284 /**
285  * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
286  *
287  * Initializes the LongRun support.
288  */
289 static int __init longrun_init(void)
290 {
291         struct cpuinfo_x86 *c = cpu_data;
292
293         if (c->x86_vendor != X86_VENDOR_TRANSMETA || 
294             !cpu_has(c, X86_FEATURE_LONGRUN))
295                 return -ENODEV;
296
297         return cpufreq_register_driver(&longrun_driver);
298 }
299
300
301 /**
302  * longrun_exit - unregisters LongRun support
303  */
304 static void __exit longrun_exit(void)
305 {
306         cpufreq_unregister_driver(&longrun_driver);
307 }
308
309
310 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
311 MODULE_DESCRIPTION ("LongRun driver for Transmeta Crusoe processors.");
312 MODULE_LICENSE ("GPL");
313
314 module_init(longrun_init);
315 module_exit(longrun_exit);