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/cpufreq.h>
13 #include <linux/timex.h>
16 #include <asm/processor.h>
17 #include <asm/cpu_device_id.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 __cpuinit longrun_get_policy(struct cpufreq_policy *policy)
40 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
41 pr_debug("longrun flags are %x - %x\n", msr_lo, msr_hi);
43 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
45 policy->policy = CPUFREQ_POLICY_POWERSAVE;
47 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
48 pr_debug("longrun ctrl is %x - %x\n", msr_lo, msr_hi);
52 if (longrun_high_freq <= longrun_low_freq) {
53 /* Assume degenerate Longrun table */
54 policy->min = policy->max = longrun_high_freq;
56 policy->min = longrun_low_freq + msr_lo *
57 ((longrun_high_freq - longrun_low_freq) / 100);
58 policy->max = longrun_low_freq + msr_hi *
59 ((longrun_high_freq - longrun_low_freq) / 100);
66 * longrun_set_policy - sets a new CPUFreq policy
69 * Sets a new CPUFreq policy on LongRun-capable processors. This function
70 * has to be called with cpufreq_driver locked.
72 static int longrun_set_policy(struct cpufreq_policy *policy)
80 if (longrun_high_freq <= longrun_low_freq) {
81 /* Assume degenerate Longrun table */
82 pctg_lo = pctg_hi = 100;
84 pctg_lo = (policy->min - longrun_low_freq) /
85 ((longrun_high_freq - longrun_low_freq) / 100);
86 pctg_hi = (policy->max - longrun_low_freq) /
87 ((longrun_high_freq - longrun_low_freq) / 100);
92 if (pctg_lo > pctg_hi)
95 /* performance or economy mode */
96 rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
98 switch (policy->policy) {
99 case CPUFREQ_POLICY_PERFORMANCE:
100 msr_lo |= 0x00000001;
102 case CPUFREQ_POLICY_POWERSAVE:
105 wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
107 /* lower and upper boundary */
108 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
109 msr_lo &= 0xFFFFFF80;
110 msr_hi &= 0xFFFFFF80;
113 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
120 * longrun_verify_poliy - verifies a new CPUFreq policy
121 * @policy: the policy to verify
123 * Validates a new CPUFreq policy. This function has to be called with
124 * cpufreq_driver locked.
126 static int longrun_verify_policy(struct cpufreq_policy *policy)
132 cpufreq_verify_within_limits(policy,
133 policy->cpuinfo.min_freq,
134 policy->cpuinfo.max_freq);
136 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
137 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
143 static unsigned int longrun_get(unsigned int cpu)
145 u32 eax, ebx, ecx, edx;
150 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
151 pr_debug("cpuid eax is %u\n", eax);
157 * longrun_determine_freqs - determines the lowest and highest possible core frequency
158 * @low_freq: an int to put the lowest frequency into
159 * @high_freq: an int to put the highest frequency into
161 * Determines the lowest and highest possible core frequencies on this CPU.
162 * This is necessary to calculate the performance percentage according to
164 * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
166 static int __cpuinit longrun_determine_freqs(unsigned int *low_freq,
167 unsigned int *high_freq)
170 u32 save_lo, save_hi;
171 u32 eax, ebx, ecx, edx;
173 struct cpuinfo_x86 *c = &cpu_data(0);
175 if (!low_freq || !high_freq)
178 if (cpu_has(c, X86_FEATURE_LRTI)) {
179 /* if the LongRun Table Interface is present, the
180 * detection is a bit easier:
181 * For minimum frequency, read out the maximum
182 * level (msr_hi), write that into "currently
183 * selected level", and read out the frequency.
184 * For maximum frequency, read out level zero.
187 rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
188 wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
189 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
190 *low_freq = msr_lo * 1000; /* to kHz */
193 wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
194 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
195 *high_freq = msr_lo * 1000; /* to kHz */
197 pr_debug("longrun table interface told %u - %u kHz\n",
198 *low_freq, *high_freq);
200 if (*low_freq > *high_freq)
201 *low_freq = *high_freq;
205 /* set the upper border to the value determined during TSC init */
206 *high_freq = (cpu_khz / 1000);
207 *high_freq = *high_freq * 1000;
208 pr_debug("high frequency is %u kHz\n", *high_freq);
210 /* get current borders */
211 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
212 save_lo = msr_lo & 0x0000007F;
213 save_hi = msr_hi & 0x0000007F;
215 /* if current perf_pctg is larger than 90%, we need to decrease the
216 * upper limit to make the calculation more accurate.
218 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
219 /* try decreasing in 10% steps, some processors react only
220 * on some barrier values */
221 for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
222 /* set to 0 to try_hi perf_pctg */
223 msr_lo &= 0xFFFFFF80;
224 msr_hi &= 0xFFFFFF80;
226 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
228 /* read out current core MHz and current perf_pctg */
229 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
232 wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
234 pr_debug("percentage is %u %%, freq is %u MHz\n", ecx, eax);
236 /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
238 * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
240 * high_freq * perf_pctg is stored tempoarily into "ebx".
242 ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
244 if ((ecx > 95) || (ecx == 0) || (eax < ebx))
247 edx = ((eax - ebx) * 100) / (100 - ecx);
248 *low_freq = edx * 1000; /* back to kHz */
250 pr_debug("low frequency is %u kHz\n", *low_freq);
252 if (*low_freq > *high_freq)
253 *low_freq = *high_freq;
259 static int __cpuinit longrun_cpu_init(struct cpufreq_policy *policy)
263 /* capability check */
264 if (policy->cpu != 0)
267 /* detect low and high frequency */
268 result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
272 /* cpuinfo and default policy values */
273 policy->cpuinfo.min_freq = longrun_low_freq;
274 policy->cpuinfo.max_freq = longrun_high_freq;
275 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
276 longrun_get_policy(policy);
282 static struct cpufreq_driver longrun_driver = {
283 .flags = CPUFREQ_CONST_LOOPS,
284 .verify = longrun_verify_policy,
285 .setpolicy = longrun_set_policy,
287 .init = longrun_cpu_init,
289 .owner = THIS_MODULE,
292 static const struct x86_cpu_id longrun_ids[] = {
293 { X86_VENDOR_TRANSMETA, X86_FAMILY_ANY, X86_MODEL_ANY,
294 X86_FEATURE_LONGRUN },
297 MODULE_DEVICE_TABLE(x86cpu, longrun_ids);
300 * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
302 * Initializes the LongRun support.
304 static int __init longrun_init(void)
306 if (!x86_match_cpu(longrun_ids))
308 return cpufreq_register_driver(&longrun_driver);
313 * longrun_exit - unregisters LongRun support
315 static void __exit longrun_exit(void)
317 cpufreq_unregister_driver(&longrun_driver);
321 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
322 MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
323 "Efficeon processors.");
324 MODULE_LICENSE("GPL");
326 module_init(longrun_init);
327 module_exit(longrun_exit);