1
2
3
4
5
6
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#include <linux/timex.h>
15
16#include <asm/msr.h>
17#include <asm/processor.h>
18
19#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
20 "longrun", msg)
21
22static struct cpufreq_driver longrun_driver;
23
24
25
26
27
28
29static unsigned int longrun_low_freq, longrun_high_freq;
30
31
32
33
34
35
36
37
38
39static 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
55 if (longrun_high_freq <= longrun_low_freq) {
56
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
70
71
72
73
74
75static 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
83 if (longrun_high_freq <= longrun_low_freq) {
84
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
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
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
124
125
126
127
128
129static 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
146static 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
156 return eax * 1000;
157}
158
159
160
161
162
163
164
165
166
167
168
169static 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;
176 struct cpuinfo_x86 *c = &cpu_data(0);
177
178 if (!low_freq || !high_freq)
179 return -EINVAL;
180
181 if (cpu_has(c, X86_FEATURE_LRTI)) {
182
183
184
185
186
187
188
189
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;
194
195
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;
199
200 dprintk("longrun table interface told %u - %u kHz\n",
201 *low_freq, *high_freq);
202
203 if (*low_freq > *high_freq)
204 *low_freq = *high_freq;
205 return 0;
206 }
207
208
209 *high_freq = (cpu_khz / 1000);
210 *high_freq = *high_freq * 1000;
211 dprintk("high frequency is %u kHz\n", *high_freq);
212
213
214 rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
215 save_lo = msr_lo & 0x0000007F;
216 save_hi = msr_hi & 0x0000007F;
217
218
219
220
221 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
222
223
224 for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
225
226 msr_lo &= 0xFFFFFF80;
227 msr_hi &= 0xFFFFFF80;
228 msr_hi |= try_hi;
229 wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
230
231
232 cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
233
234
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
240
241
242
243
244
245 ebx = (((cpu_khz / 1000) * ecx) / 100);
246
247 if ((ecx > 95) || (ecx == 0) || (eax < ebx))
248 return -EIO;
249
250 edx = ((eax - ebx) * 100) / (100 - ecx);
251 *low_freq = edx * 1000;
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
262static int __init longrun_cpu_init(struct cpufreq_policy *policy)
263{
264 int result = 0;
265
266
267 if (policy->cpu != 0)
268 return -ENODEV;
269
270
271 result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
272 if (result)
273 return result;
274
275
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
285static struct cpufreq_driver longrun_driver = {
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
298
299
300
301static int __init longrun_init(void)
302{
303 struct cpuinfo_x86 *c = &cpu_data(0);
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
315
316static void __exit longrun_exit(void)
317{
318 cpufreq_unregister_driver(&longrun_driver);
319}
320
321
322MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
323MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
324 "Efficeon processors.");
325MODULE_LICENSE("GPL");
326
327module_init(longrun_init);
328module_exit(longrun_exit);
329