2 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
4 * Licensed under the terms of the GNU GPL License version 2.
6 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
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>
16 #include <asm/processor.h>
17 #include <asm/timex.h>
19 static struct cpufreq_driver longrun_driver;
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)
26 static unsigned int longrun_low_freq, longrun_high_freq;
30 * longrun_get_policy - get the current LongRun policy
31 * @policy: struct cpufreq_policy where current policy is written into
33 * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
34 * and MSR_TMTA_LONGRUN_CTRL
36 static void __init longrun_get_policy(struct cpufreq_policy *policy)
40 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
42 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
44 policy->policy = CPUFREQ_POLICY_POWERSAVE;
46 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
50 if ( longrun_high_freq <= longrun_low_freq ) {
51 /* Assume degenerate Longrun table */
52 policy->min = policy->max = longrun_high_freq;
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);
64 * longrun_set_policy - sets a new CPUFreq policy
67 * Sets a new CPUFreq policy on LongRun-capable processors. This function
68 * has to be called with cpufreq_driver locked.
70 static int longrun_set_policy(struct cpufreq_policy *policy)
78 if ( longrun_high_freq <= longrun_low_freq ) {
79 /* Assume degenerate Longrun table */
80 pctg_lo = pctg_hi = 100;
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);
90 if (pctg_lo > pctg_hi)
93 /* performance or economy mode */
94 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
96 switch (policy->policy) {
97 case CPUFREQ_POLICY_PERFORMANCE:
100 case CPUFREQ_POLICY_POWERSAVE:
103 wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
105 /* lower and upper boundary */
106 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
107 msr_lo &= 0xFFFFFF80;
108 msr_hi &= 0xFFFFFF80;
111 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
118 * longrun_verify_poliy - verifies a new CPUFreq policy
119 * @policy: the policy to verify
121 * Validates a new CPUFreq policy. This function has to be called with
122 * cpufreq_driver locked.
124 static int longrun_verify_policy(struct cpufreq_policy *policy)
130 cpufreq_verify_within_limits(policy,
131 policy->cpuinfo.min_freq,
132 policy->cpuinfo.max_freq);
134 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
135 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
141 static unsigned int longrun_get(unsigned int cpu)
143 u32 eax, ebx, ecx, edx;
148 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
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
158 * Determines the lowest and highest possible core frequencies on this CPU.
159 * This is necessary to calculate the performance percentage according to
161 * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
163 static unsigned int __init longrun_determine_freqs(unsigned int *low_freq,
164 unsigned int *high_freq)
167 u32 save_lo, save_hi;
168 u32 eax, ebx, ecx, edx;
170 struct cpuinfo_x86 *c = cpu_data;
172 if (!low_freq || !high_freq)
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.
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 */
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 */
194 if (*low_freq > *high_freq)
195 *low_freq = *high_freq;
199 /* set the upper border to the value determined during TSC init */
200 *high_freq = (cpu_khz / 1000);
201 *high_freq = *high_freq * 1000;
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;
208 /* if current perf_pctg is larger than 90%, we need to decrease the
209 * upper limit to make the calculation more accurate.
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;
220 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
222 /* read out current core MHz and current perf_pctg */
223 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
226 wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
229 /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
231 * low_freq * ( 1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
233 * high_freq * perf_pctg is stored tempoarily into "ebx".
235 ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
237 if ((ecx > 95) || (ecx == 0) || (eax < ebx))
240 edx = (eax - ebx) / (100 - ecx);
241 *low_freq = edx * 1000; /* back to kHz */
243 if (*low_freq > *high_freq)
244 *low_freq = *high_freq;
250 static int __init longrun_cpu_init(struct cpufreq_policy *policy)
254 /* capability check */
255 if (policy->cpu != 0)
258 /* detect low and high frequency */
259 result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
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);
273 static struct cpufreq_driver longrun_driver = {
274 .flags = CPUFREQ_CONST_LOOPS,
275 .verify = longrun_verify_policy,
276 .setpolicy = longrun_set_policy,
278 .init = longrun_cpu_init,
280 .owner = THIS_MODULE,
285 * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
287 * Initializes the LongRun support.
289 static int __init longrun_init(void)
291 struct cpuinfo_x86 *c = cpu_data;
293 if (c->x86_vendor != X86_VENDOR_TRANSMETA ||
294 !cpu_has(c, X86_FEATURE_LONGRUN))
297 return cpufreq_register_driver(&longrun_driver);
302 * longrun_exit - unregisters LongRun support
304 static void __exit longrun_exit(void)
306 cpufreq_unregister_driver(&longrun_driver);
310 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
311 MODULE_DESCRIPTION ("LongRun driver for Transmeta Crusoe processors.");
312 MODULE_LICENSE ("GPL");
314 module_init(longrun_init);
315 module_exit(longrun_exit);