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