Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> | |
3 | * | |
4 | * Licensed under the terms of the GNU GPL License version 2. | |
5 | * | |
6 | * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous* | |
7 | */ | |
8 | ||
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> | |
48ee923a | 14 | #include <linux/timex.h> |
1da177e4 LT |
15 | |
16 | #include <asm/msr.h> | |
17 | #include <asm/processor.h> | |
1da177e4 | 18 | |
48ee923a DJ |
19 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \ |
20 | "longrun", msg) | |
1da177e4 | 21 | |
221dee28 | 22 | static struct cpufreq_driver longrun_driver; |
1da177e4 LT |
23 | |
24 | /** | |
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) | |
28 | */ | |
29 | static unsigned int longrun_low_freq, longrun_high_freq; | |
30 | ||
31 | ||
32 | /** | |
33 | * longrun_get_policy - get the current LongRun policy | |
34 | * @policy: struct cpufreq_policy where current policy is written into | |
35 | * | |
36 | * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS | |
37 | * and MSR_TMTA_LONGRUN_CTRL | |
38 | */ | |
39 | static void __init longrun_get_policy(struct cpufreq_policy *policy) | |
40 | { | |
41 | u32 msr_lo, msr_hi; | |
42 | ||
43 | rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi); | |
44 | dprintk("longrun flags are %x - %x\n", msr_lo, msr_hi); | |
45 | if (msr_lo & 0x01) | |
46 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; | |
47 | else | |
48 | policy->policy = CPUFREQ_POLICY_POWERSAVE; | |
49 | ||
50 | rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi); | |
51 | dprintk("longrun ctrl is %x - %x\n", msr_lo, msr_hi); | |
52 | msr_lo &= 0x0000007F; | |
53 | msr_hi &= 0x0000007F; | |
54 | ||
48ee923a | 55 | if (longrun_high_freq <= longrun_low_freq) { |
1da177e4 LT |
56 | /* Assume degenerate Longrun table */ |
57 | policy->min = policy->max = longrun_high_freq; | |
58 | } else { | |
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); | |
63 | } | |
64 | policy->cpu = 0; | |
65 | } | |
66 | ||
67 | ||
68 | /** | |
69 | * longrun_set_policy - sets a new CPUFreq policy | |
70 | * @policy: new policy | |
71 | * | |
72 | * Sets a new CPUFreq policy on LongRun-capable processors. This function | |
73 | * has to be called with cpufreq_driver locked. | |
74 | */ | |
75 | static int longrun_set_policy(struct cpufreq_policy *policy) | |
76 | { | |
77 | u32 msr_lo, msr_hi; | |
78 | u32 pctg_lo, pctg_hi; | |
79 | ||
80 | if (!policy) | |
81 | return -EINVAL; | |
82 | ||
48ee923a | 83 | if (longrun_high_freq <= longrun_low_freq) { |
1da177e4 LT |
84 | /* Assume degenerate Longrun table */ |
85 | pctg_lo = pctg_hi = 100; | |
86 | } else { | |
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); | |
91 | } | |
92 | ||
93 | if (pctg_hi > 100) | |
94 | pctg_hi = 100; | |
95 | if (pctg_lo > pctg_hi) | |
96 | pctg_lo = pctg_hi; | |
97 | ||
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; | |
104 | break; | |
105 | case CPUFREQ_POLICY_POWERSAVE: | |
106 | break; | |
107 | } | |
108 | wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi); | |
109 | ||
110 | /* lower and upper boundary */ | |
111 | rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi); | |
112 | msr_lo &= 0xFFFFFF80; | |
113 | msr_hi &= 0xFFFFFF80; | |
114 | msr_lo |= pctg_lo; | |
115 | msr_hi |= pctg_hi; | |
116 | wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi); | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | ||
122 | /** | |
123 | * longrun_verify_poliy - verifies a new CPUFreq policy | |
124 | * @policy: the policy to verify | |
125 | * | |
126 | * Validates a new CPUFreq policy. This function has to be called with | |
127 | * cpufreq_driver locked. | |
128 | */ | |
129 | static int longrun_verify_policy(struct cpufreq_policy *policy) | |
130 | { | |
131 | if (!policy) | |
132 | return -EINVAL; | |
133 | ||
134 | policy->cpu = 0; | |
135 | cpufreq_verify_within_limits(policy, | |
136 | policy->cpuinfo.min_freq, | |
137 | policy->cpuinfo.max_freq); | |
138 | ||
139 | if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) && | |
140 | (policy->policy != CPUFREQ_POLICY_PERFORMANCE)) | |
141 | return -EINVAL; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static unsigned int longrun_get(unsigned int cpu) | |
147 | { | |
148 | u32 eax, ebx, ecx, edx; | |
149 | ||
150 | if (cpu) | |
151 | return 0; | |
152 | ||
153 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx); | |
154 | dprintk("cpuid eax is %u\n", eax); | |
155 | ||
48ee923a | 156 | return eax * 1000; |
1da177e4 LT |
157 | } |
158 | ||
159 | /** | |
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 | |
163 | * | |
164 | * Determines the lowest and highest possible core frequencies on this CPU. | |
165 | * This is necessary to calculate the performance percentage according to | |
166 | * TMTA rules: | |
167 | * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq) | |
168 | */ | |
169 | static unsigned int __init longrun_determine_freqs(unsigned int *low_freq, | |
170 | unsigned int *high_freq) | |
171 | { | |
172 | u32 msr_lo, msr_hi; | |
173 | u32 save_lo, save_hi; | |
174 | u32 eax, ebx, ecx, edx; | |
175 | u32 try_hi; | |
92cb7612 | 176 | struct cpuinfo_x86 *c = &cpu_data(0); |
1da177e4 LT |
177 | |
178 | if (!low_freq || !high_freq) | |
179 | return -EINVAL; | |
180 | ||
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. | |
188 | */ | |
189 | /* minimum */ | |
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 */ | |
194 | ||
195 | /* maximum */ | |
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 */ | |
199 | ||
48ee923a DJ |
200 | dprintk("longrun table interface told %u - %u kHz\n", |
201 | *low_freq, *high_freq); | |
1da177e4 LT |
202 | |
203 | if (*low_freq > *high_freq) | |
204 | *low_freq = *high_freq; | |
205 | return 0; | |
206 | } | |
207 | ||
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); | |
212 | ||
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; | |
217 | ||
218 | /* if current perf_pctg is larger than 90%, we need to decrease the | |
219 | * upper limit to make the calculation more accurate. | |
220 | */ | |
221 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx); | |
222 | /* try decreasing in 10% steps, some processors react only | |
223 | * on some barrier values */ | |
48ee923a | 224 | for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) { |
1da177e4 LT |
225 | /* set to 0 to try_hi perf_pctg */ |
226 | msr_lo &= 0xFFFFFF80; | |
227 | msr_hi &= 0xFFFFFF80; | |
1da177e4 LT |
228 | msr_hi |= try_hi; |
229 | wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi); | |
230 | ||
231 | /* read out current core MHz and current perf_pctg */ | |
232 | cpuid(0x80860007, &eax, &ebx, &ecx, &edx); | |
233 | ||
234 | /* restore values */ | |
235 | wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi); | |
236 | } | |
237 | dprintk("percentage is %u %%, freq is %u MHz\n", ecx, eax); | |
238 | ||
239 | /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq) | |
240 | * eqals | |
48ee923a | 241 | * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg) |
1da177e4 LT |
242 | * |
243 | * high_freq * perf_pctg is stored tempoarily into "ebx". | |
244 | */ | |
245 | ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */ | |
246 | ||
247 | if ((ecx > 95) || (ecx == 0) || (eax < ebx)) | |
248 | return -EIO; | |
249 | ||
667ad4f7 | 250 | edx = ((eax - ebx) * 100) / (100 - ecx); |
1da177e4 LT |
251 | *low_freq = edx * 1000; /* back to kHz */ |
252 | ||
253 | dprintk("low frequency is %u kHz\n", *low_freq); | |
254 | ||
255 | if (*low_freq > *high_freq) | |
256 | *low_freq = *high_freq; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | ||
262 | static int __init longrun_cpu_init(struct cpufreq_policy *policy) | |
263 | { | |
264 | int result = 0; | |
265 | ||
266 | /* capability check */ | |
267 | if (policy->cpu != 0) | |
268 | return -ENODEV; | |
269 | ||
270 | /* detect low and high frequency */ | |
271 | result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq); | |
272 | if (result) | |
273 | return result; | |
274 | ||
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); | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
284 | ||
221dee28 | 285 | static struct cpufreq_driver longrun_driver = { |
1da177e4 LT |
286 | .flags = CPUFREQ_CONST_LOOPS, |
287 | .verify = longrun_verify_policy, | |
288 | .setpolicy = longrun_set_policy, | |
289 | .get = longrun_get, | |
290 | .init = longrun_cpu_init, | |
291 | .name = "longrun", | |
292 | .owner = THIS_MODULE, | |
293 | }; | |
294 | ||
295 | ||
296 | /** | |
297 | * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver | |
298 | * | |
299 | * Initializes the LongRun support. | |
300 | */ | |
301 | static int __init longrun_init(void) | |
302 | { | |
92cb7612 | 303 | struct cpuinfo_x86 *c = &cpu_data(0); |
1da177e4 LT |
304 | |
305 | if (c->x86_vendor != X86_VENDOR_TRANSMETA || | |
306 | !cpu_has(c, X86_FEATURE_LONGRUN)) | |
307 | return -ENODEV; | |
308 | ||
309 | return cpufreq_register_driver(&longrun_driver); | |
310 | } | |
311 | ||
312 | ||
313 | /** | |
314 | * longrun_exit - unregisters LongRun support | |
315 | */ | |
316 | static void __exit longrun_exit(void) | |
317 | { | |
318 | cpufreq_unregister_driver(&longrun_driver); | |
319 | } | |
320 | ||
321 | ||
48ee923a DJ |
322 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
323 | MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and " | |
324 | "Efficeon processors."); | |
325 | MODULE_LICENSE("GPL"); | |
1da177e4 LT |
326 | |
327 | module_init(longrun_init); | |
328 | module_exit(longrun_exit); |