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 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 policy->min = longrun_low_freq + msr_lo *
51 ((longrun_high_freq - longrun_low_freq) / 100);
52 policy->max = longrun_low_freq + msr_hi *
53 ((longrun_high_freq - longrun_low_freq) / 100);
59 * longrun_set_policy - sets a new CPUFreq policy
60 * @policy - new policy
62 * Sets a new CPUFreq policy on LongRun-capable processors. This function
63 * has to be called with cpufreq_driver locked.
65 static int longrun_set_policy(struct cpufreq_policy *policy)
73 pctg_lo = (policy->min - longrun_low_freq) /
74 ((longrun_high_freq - longrun_low_freq) / 100);
75 pctg_hi = (policy->max - longrun_low_freq) /
76 ((longrun_high_freq - longrun_low_freq) / 100);
80 if (pctg_lo > pctg_hi)
83 /* performance or economy mode */
84 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
86 switch (policy->policy) {
87 case CPUFREQ_POLICY_PERFORMANCE:
90 case CPUFREQ_POLICY_POWERSAVE:
93 wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
95 /* lower and upper boundary */
96 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
101 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
108 * longrun_verify_poliy - verifies a new CPUFreq policy
110 * Validates a new CPUFreq policy. This function has to be called with
111 * cpufreq_driver locked.
113 static int longrun_verify_policy(struct cpufreq_policy *policy)
119 cpufreq_verify_within_limits(policy,
120 policy->cpuinfo.min_freq,
121 policy->cpuinfo.max_freq);
123 if (policy->policy == CPUFREQ_POLICY_GOVERNOR)
131 * longrun_determine_freqs - determines the lowest and highest possible core frequency
133 * Determines the lowest and highest possible core frequencies on this CPU.
134 * This is necessary to calculate the performance percentage according to
136 * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
138 static unsigned int __init longrun_determine_freqs(unsigned int *low_freq,
139 unsigned int *high_freq)
142 u32 save_lo, save_hi;
143 u32 eax, ebx, ecx, edx;
144 struct cpuinfo_x86 *c = cpu_data;
146 if (!low_freq || !high_freq)
149 if (cpu_has(c, X86_FEATURE_LRTI)) {
150 /* if the LongRun Table Interface is present, the
151 * detection is a bit easier:
152 * For minimum frequency, read out the maximum
153 * level (msr_hi), write that into "currently
154 * selected level", and read out the frequency.
155 * For maximum frequency, read out level zero.
158 rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
159 wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
160 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
161 *low_freq = msr_lo * 1000; /* to kHz */
164 wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
165 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
166 *high_freq = msr_lo * 1000; /* to kHz */
168 if (*low_freq > *high_freq)
169 *low_freq = *high_freq;
173 /* set the upper border to the value determined during TSC init */
174 *high_freq = (cpu_khz / 1000);
175 *high_freq = *high_freq * 1000;
177 /* get current borders */
178 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
179 save_lo = msr_lo & 0x0000007F;
180 save_hi = msr_hi & 0x0000007F;
182 /* if current perf_pctg is larger than 90%, we need to decrease the
183 * upper limit to make the calculation more accurate.
185 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
187 /* set to 0 to 80 perf_pctg */
188 msr_lo &= 0xFFFFFF80;
189 msr_hi &= 0xFFFFFF80;
192 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
194 /* read out current core MHz and current perf_pctg */
195 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
198 wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
201 /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
203 * low_freq * ( 1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
205 * high_freq * perf_pctg is stored tempoarily into "ebx".
207 ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
209 if ((ecx > 95) || (ecx == 0) || (eax < ebx))
212 edx = (eax - ebx) / (100 - ecx);
213 *low_freq = edx * 1000; /* back to kHz */
215 if (*low_freq > *high_freq)
216 *low_freq = *high_freq;
222 static int longrun_cpu_init(struct cpufreq_policy *policy)
226 /* capability check */
227 if (policy->cpu != 0)
230 /* detect low and high frequency */
231 result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
235 /* cpuinfo and default policy values */
236 policy->cpuinfo.min_freq = longrun_low_freq;
237 policy->cpuinfo.max_freq = longrun_high_freq;
238 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
239 longrun_get_policy(policy);
245 static struct cpufreq_driver longrun_driver = {
246 .verify = longrun_verify_policy,
247 .setpolicy = longrun_set_policy,
248 .init = longrun_cpu_init,
250 .owner = THIS_MODULE,
255 * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
257 * Initializes the LongRun support.
259 static int __init longrun_init(void)
261 struct cpuinfo_x86 *c = cpu_data;
263 if (c->x86_vendor != X86_VENDOR_TRANSMETA ||
264 !cpu_has(c, X86_FEATURE_LONGRUN))
267 return cpufreq_register_driver(&longrun_driver);
272 * longrun_exit - unregisters LongRun support
274 static void __exit longrun_exit(void)
276 cpufreq_unregister_driver(&longrun_driver);
280 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
281 MODULE_DESCRIPTION ("LongRun driver for Transmeta Crusoe processors.");
282 MODULE_LICENSE ("GPL");
284 module_init(longrun_init);
285 module_exit(longrun_exit);