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>
14 #include <linux/timex.h>
17 #include <asm/processor.h>
19 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
22 static struct cpufreq_driver longrun_driver;
25 * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz
26 * values into per cent values. In TMTA microcode, the following is valid:
27 * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
29 static unsigned int longrun_low_freq, longrun_high_freq;
33 * longrun_get_policy - get the current LongRun policy
34 * @policy: struct cpufreq_policy where current policy is written into
36 * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
37 * and MSR_TMTA_LONGRUN_CTRL
39 static void __init longrun_get_policy(struct cpufreq_policy *policy)
43 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
44 dprintk("longrun flags are %x - %x\n", msr_lo, msr_hi);
46 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
48 policy->policy = CPUFREQ_POLICY_POWERSAVE;
50 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
51 dprintk("longrun ctrl is %x - %x\n", msr_lo, msr_hi);
55 if (longrun_high_freq <= longrun_low_freq) {
56 /* Assume degenerate Longrun table */
57 policy->min = policy->max = longrun_high_freq;
59 policy->min = longrun_low_freq + msr_lo *
60 ((longrun_high_freq - longrun_low_freq) / 100);
61 policy->max = longrun_low_freq + msr_hi *
62 ((longrun_high_freq - longrun_low_freq) / 100);
69 * longrun_set_policy - sets a new CPUFreq policy
72 * Sets a new CPUFreq policy on LongRun-capable processors. This function
73 * has to be called with cpufreq_driver locked.
75 static int longrun_set_policy(struct cpufreq_policy *policy)
83 if (longrun_high_freq <= longrun_low_freq) {
84 /* Assume degenerate Longrun table */
85 pctg_lo = pctg_hi = 100;
87 pctg_lo = (policy->min - longrun_low_freq) /
88 ((longrun_high_freq - longrun_low_freq) / 100);
89 pctg_hi = (policy->max - longrun_low_freq) /
90 ((longrun_high_freq - longrun_low_freq) / 100);
95 if (pctg_lo > pctg_hi)
98 /* performance or economy mode */
99 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
100 msr_lo &= 0xFFFFFFFE;
101 switch (policy->policy) {
102 case CPUFREQ_POLICY_PERFORMANCE:
103 msr_lo |= 0x00000001;
105 case CPUFREQ_POLICY_POWERSAVE:
108 wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
110 /* lower and upper boundary */
111 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
112 msr_lo &= 0xFFFFFF80;
113 msr_hi &= 0xFFFFFF80;
116 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
123 * longrun_verify_poliy - verifies a new CPUFreq policy
124 * @policy: the policy to verify
126 * Validates a new CPUFreq policy. This function has to be called with
127 * cpufreq_driver locked.
129 static int longrun_verify_policy(struct cpufreq_policy *policy)
135 cpufreq_verify_within_limits(policy,
136 policy->cpuinfo.min_freq,
137 policy->cpuinfo.max_freq);
139 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
140 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
146 static unsigned int longrun_get(unsigned int cpu)
148 u32 eax, ebx, ecx, edx;
153 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
154 dprintk("cpuid eax is %u\n", eax);
160 * longrun_determine_freqs - determines the lowest and highest possible core frequency
161 * @low_freq: an int to put the lowest frequency into
162 * @high_freq: an int to put the highest frequency into
164 * Determines the lowest and highest possible core frequencies on this CPU.
165 * This is necessary to calculate the performance percentage according to
167 * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
169 static unsigned int __init longrun_determine_freqs(unsigned int *low_freq,
170 unsigned int *high_freq)
173 u32 save_lo, save_hi;
174 u32 eax, ebx, ecx, edx;
176 struct cpuinfo_x86 *c = &cpu_data(0);
178 if (!low_freq || !high_freq)
181 if (cpu_has(c, X86_FEATURE_LRTI)) {
182 /* if the LongRun Table Interface is present, the
183 * detection is a bit easier:
184 * For minimum frequency, read out the maximum
185 * level (msr_hi), write that into "currently
186 * selected level", and read out the frequency.
187 * For maximum frequency, read out level zero.
190 rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
191 wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
192 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
193 *low_freq = msr_lo * 1000; /* to kHz */
196 wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
197 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
198 *high_freq = msr_lo * 1000; /* to kHz */
200 dprintk("longrun table interface told %u - %u kHz\n",
201 *low_freq, *high_freq);
203 if (*low_freq > *high_freq)
204 *low_freq = *high_freq;
208 /* set the upper border to the value determined during TSC init */
209 *high_freq = (cpu_khz / 1000);
210 *high_freq = *high_freq * 1000;
211 dprintk("high frequency is %u kHz\n", *high_freq);
213 /* get current borders */
214 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
215 save_lo = msr_lo & 0x0000007F;
216 save_hi = msr_hi & 0x0000007F;
218 /* if current perf_pctg is larger than 90%, we need to decrease the
219 * upper limit to make the calculation more accurate.
221 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
222 /* try decreasing in 10% steps, some processors react only
223 * on some barrier values */
224 for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
225 /* set to 0 to try_hi perf_pctg */
226 msr_lo &= 0xFFFFFF80;
227 msr_hi &= 0xFFFFFF80;
229 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
231 /* read out current core MHz and current perf_pctg */
232 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
235 wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
237 dprintk("percentage is %u %%, freq is %u MHz\n", ecx, eax);
239 /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
241 * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
243 * high_freq * perf_pctg is stored tempoarily into "ebx".
245 ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
247 if ((ecx > 95) || (ecx == 0) || (eax < ebx))
250 edx = ((eax - ebx) * 100) / (100 - ecx);
251 *low_freq = edx * 1000; /* back to kHz */
253 dprintk("low frequency is %u kHz\n", *low_freq);
255 if (*low_freq > *high_freq)
256 *low_freq = *high_freq;
262 static int __init longrun_cpu_init(struct cpufreq_policy *policy)
266 /* capability check */
267 if (policy->cpu != 0)
270 /* detect low and high frequency */
271 result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
275 /* cpuinfo and default policy values */
276 policy->cpuinfo.min_freq = longrun_low_freq;
277 policy->cpuinfo.max_freq = longrun_high_freq;
278 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
279 longrun_get_policy(policy);
285 static struct cpufreq_driver longrun_driver = {
286 .flags = CPUFREQ_CONST_LOOPS,
287 .verify = longrun_verify_policy,
288 .setpolicy = longrun_set_policy,
290 .init = longrun_cpu_init,
292 .owner = THIS_MODULE,
297 * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
299 * Initializes the LongRun support.
301 static int __init longrun_init(void)
303 struct cpuinfo_x86 *c = &cpu_data(0);
305 if (c->x86_vendor != X86_VENDOR_TRANSMETA ||
306 !cpu_has(c, X86_FEATURE_LONGRUN))
309 return cpufreq_register_driver(&longrun_driver);
314 * longrun_exit - unregisters LongRun support
316 static void __exit longrun_exit(void)
318 cpufreq_unregister_driver(&longrun_driver);
322 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
323 MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
324 "Efficeon processors.");
325 MODULE_LICENSE("GPL");
327 module_init(longrun_init);
328 module_exit(longrun_exit);