1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
5 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/cpufreq.h>
12 #include <linux/timex.h>
15 #include <asm/processor.h>
16 #include <asm/cpu_device_id.h>
18 static struct cpufreq_driver longrun_driver
;
21 * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz
22 * values into per cent values. In TMTA microcode, the following is valid:
23 * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
25 static unsigned int longrun_low_freq
, longrun_high_freq
;
29 * longrun_get_policy - get the current LongRun policy
30 * @policy: struct cpufreq_policy where current policy is written into
32 * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
33 * and MSR_TMTA_LONGRUN_CTRL
35 static void longrun_get_policy(struct cpufreq_policy
*policy
)
39 rdmsr(MSR_TMTA_LONGRUN_FLAGS
, msr_lo
, msr_hi
);
40 pr_debug("longrun flags are %x - %x\n", 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
);
47 pr_debug("longrun ctrl is %x - %x\n", msr_lo
, msr_hi
);
51 if (longrun_high_freq
<= longrun_low_freq
) {
52 /* Assume degenerate Longrun table */
53 policy
->min
= policy
->max
= longrun_high_freq
;
55 policy
->min
= longrun_low_freq
+ msr_lo
*
56 ((longrun_high_freq
- longrun_low_freq
) / 100);
57 policy
->max
= longrun_low_freq
+ msr_hi
*
58 ((longrun_high_freq
- longrun_low_freq
) / 100);
65 * longrun_set_policy - sets a new CPUFreq policy
68 * Sets a new CPUFreq policy on LongRun-capable processors. This function
69 * has to be called with cpufreq_driver locked.
71 static int longrun_set_policy(struct cpufreq_policy
*policy
)
79 if (longrun_high_freq
<= longrun_low_freq
) {
80 /* Assume degenerate Longrun table */
81 pctg_lo
= pctg_hi
= 100;
83 pctg_lo
= (policy
->min
- longrun_low_freq
) /
84 ((longrun_high_freq
- longrun_low_freq
) / 100);
85 pctg_hi
= (policy
->max
- longrun_low_freq
) /
86 ((longrun_high_freq
- longrun_low_freq
) / 100);
91 if (pctg_lo
> pctg_hi
)
94 /* performance or economy mode */
95 rdmsr(MSR_TMTA_LONGRUN_FLAGS
, msr_lo
, msr_hi
);
97 switch (policy
->policy
) {
98 case CPUFREQ_POLICY_PERFORMANCE
:
101 case CPUFREQ_POLICY_POWERSAVE
:
104 wrmsr(MSR_TMTA_LONGRUN_FLAGS
, msr_lo
, msr_hi
);
106 /* lower and upper boundary */
107 rdmsr(MSR_TMTA_LONGRUN_CTRL
, msr_lo
, msr_hi
);
108 msr_lo
&= 0xFFFFFF80;
109 msr_hi
&= 0xFFFFFF80;
112 wrmsr(MSR_TMTA_LONGRUN_CTRL
, msr_lo
, msr_hi
);
119 * longrun_verify_poliy - verifies a new CPUFreq policy
120 * @policy: the policy to verify
122 * Validates a new CPUFreq policy. This function has to be called with
123 * cpufreq_driver locked.
125 static int longrun_verify_policy(struct cpufreq_policy_data
*policy
)
131 cpufreq_verify_within_cpu_limits(policy
);
136 static unsigned int longrun_get(unsigned int cpu
)
138 u32 eax
, ebx
, ecx
, edx
;
143 cpuid(0x80860007, &eax
, &ebx
, &ecx
, &edx
);
144 pr_debug("cpuid eax is %u\n", eax
);
150 * longrun_determine_freqs - determines the lowest and highest possible core frequency
151 * @low_freq: an int to put the lowest frequency into
152 * @high_freq: an int to put the highest frequency into
154 * Determines the lowest and highest possible core frequencies on this CPU.
155 * This is necessary to calculate the performance percentage according to
157 * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
159 static int longrun_determine_freqs(unsigned int *low_freq
,
160 unsigned int *high_freq
)
163 u32 save_lo
, save_hi
;
164 u32 eax
, ebx
, ecx
, edx
;
166 struct cpuinfo_x86
*c
= &cpu_data(0);
168 if (!low_freq
|| !high_freq
)
171 if (cpu_has(c
, X86_FEATURE_LRTI
)) {
172 /* if the LongRun Table Interface is present, the
173 * detection is a bit easier:
174 * For minimum frequency, read out the maximum
175 * level (msr_hi), write that into "currently
176 * selected level", and read out the frequency.
177 * For maximum frequency, read out level zero.
180 rdmsr(MSR_TMTA_LRTI_READOUT
, msr_lo
, msr_hi
);
181 wrmsr(MSR_TMTA_LRTI_READOUT
, msr_hi
, msr_hi
);
182 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ
, msr_lo
, msr_hi
);
183 *low_freq
= msr_lo
* 1000; /* to kHz */
186 wrmsr(MSR_TMTA_LRTI_READOUT
, 0, msr_hi
);
187 rdmsr(MSR_TMTA_LRTI_VOLT_MHZ
, msr_lo
, msr_hi
);
188 *high_freq
= msr_lo
* 1000; /* to kHz */
190 pr_debug("longrun table interface told %u - %u kHz\n",
191 *low_freq
, *high_freq
);
193 if (*low_freq
> *high_freq
)
194 *low_freq
= *high_freq
;
198 /* set the upper border to the value determined during TSC init */
199 *high_freq
= (cpu_khz
/ 1000);
200 *high_freq
= *high_freq
* 1000;
201 pr_debug("high frequency is %u kHz\n", *high_freq
);
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;
219 wrmsr(MSR_TMTA_LONGRUN_CTRL
, msr_lo
, msr_hi
);
221 /* read out current core MHz and current perf_pctg */
222 cpuid(0x80860007, &eax
, &ebx
, &ecx
, &edx
);
225 wrmsr(MSR_TMTA_LONGRUN_CTRL
, save_lo
, save_hi
);
227 pr_debug("percentage is %u %%, freq is %u MHz\n", ecx
, eax
);
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) / (100 - ecx
);
241 *low_freq
= edx
* 1000; /* back to kHz */
243 pr_debug("low frequency is %u kHz\n", *low_freq
);
245 if (*low_freq
> *high_freq
)
246 *low_freq
= *high_freq
;
252 static int longrun_cpu_init(struct cpufreq_policy
*policy
)
256 /* capability check */
257 if (policy
->cpu
!= 0)
260 /* detect low and high frequency */
261 result
= longrun_determine_freqs(&longrun_low_freq
, &longrun_high_freq
);
265 /* cpuinfo and default policy values */
266 policy
->cpuinfo
.min_freq
= longrun_low_freq
;
267 policy
->cpuinfo
.max_freq
= longrun_high_freq
;
268 longrun_get_policy(policy
);
274 static struct cpufreq_driver longrun_driver
= {
275 .flags
= CPUFREQ_CONST_LOOPS
,
276 .verify
= longrun_verify_policy
,
277 .setpolicy
= longrun_set_policy
,
279 .init
= longrun_cpu_init
,
283 static const struct x86_cpu_id longrun_ids
[] = {
284 { X86_VENDOR_TRANSMETA
, X86_FAMILY_ANY
, X86_MODEL_ANY
,
285 X86_FEATURE_LONGRUN
},
288 MODULE_DEVICE_TABLE(x86cpu
, longrun_ids
);
291 * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
293 * Initializes the LongRun support.
295 static int __init
longrun_init(void)
297 if (!x86_match_cpu(longrun_ids
))
299 return cpufreq_register_driver(&longrun_driver
);
304 * longrun_exit - unregisters LongRun support
306 static void __exit
longrun_exit(void)
308 cpufreq_unregister_driver(&longrun_driver
);
312 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
313 MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
314 "Efficeon processors.");
315 MODULE_LICENSE("GPL");
317 module_init(longrun_init
);
318 module_exit(longrun_exit
);