2 * Based on documentation provided by Dave Jones. Thanks!
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/ioport.h>
14 #include <linux/slab.h>
18 #include <asm/timex.h>
20 #include <asm/delay.h>
22 #define EPS_BRAND_C7M 0
23 #define EPS_BRAND_C7 1
24 #define EPS_BRAND_EDEN 2
25 #define EPS_BRAND_C3 3
26 #define EPS_BRAND_C7D 4
30 struct cpufreq_frequency_table freq_table
[];
33 static struct eps_cpu_data
*eps_cpu
[NR_CPUS
];
36 static unsigned int eps_get(unsigned int cpu
)
38 struct eps_cpu_data
*centaur
;
43 centaur
= eps_cpu
[cpu
];
47 /* Return current frequency */
48 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
49 return centaur
->fsb
* ((lo
>> 8) & 0xff);
52 static int eps_set_state(struct eps_cpu_data
*centaur
,
56 struct cpufreq_freqs freqs
;
58 <<<<<<< HEAD
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
59 u8 current_multiplier
, current_voltage
;
61 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
65 freqs
.old
= eps_get(cpu
);
66 freqs
.new = centaur
->fsb
* ((dest_state
>> 8) & 0xff);
68 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
70 /* Wait while CPU is busy */
71 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
73 while (lo
& ((1 << 16) | (1 << 17))) {
75 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
77 if (unlikely(i
> 64)) {
82 /* Set new multiplier and voltage */
83 wrmsr(MSR_IA32_PERF_CTL
, dest_state
& 0xffff, 0);
84 /* Wait until transition end */
88 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
90 if (unlikely(i
> 64)) {
94 } while (lo
& ((1 << 16) | (1 << 17)));
96 /* Return current frequency */
98 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
99 freqs
.new = centaur
->fsb
* ((lo
>> 8) & 0xff);
101 <<<<<<< HEAD
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
105 u8 current_multiplier
, current_voltage
;
107 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
108 /* Print voltage and multiplier */
109 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
110 current_voltage
= lo
& 0xff;
111 printk(KERN_INFO
"eps: Current voltage = %dmV\n",
112 current_voltage
* 16 + 700);
113 current_multiplier
= (lo
>> 8) & 0xff;
114 printk(KERN_INFO
"eps: Current multiplier = %d\n",
116 <<<<<<< HEAD
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
121 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kernel
/cpu
/cpufreq
/e_powersaver
.c
122 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
126 static int eps_target(struct cpufreq_policy
*policy
,
127 unsigned int target_freq
,
128 unsigned int relation
)
130 struct eps_cpu_data
*centaur
;
131 unsigned int newstate
= 0;
132 unsigned int cpu
= policy
->cpu
;
133 unsigned int dest_state
;
136 if (unlikely(eps_cpu
[cpu
] == NULL
))
138 centaur
= eps_cpu
[cpu
];
140 if (unlikely(cpufreq_frequency_table_target(policy
,
141 &eps_cpu
[cpu
]->freq_table
[0],
148 /* Make frequency transition */
149 dest_state
= centaur
->freq_table
[newstate
].index
& 0xffff;
150 ret
= eps_set_state(centaur
, cpu
, dest_state
);
152 printk(KERN_ERR
"eps: Timeout!\n");
156 static int eps_verify(struct cpufreq_policy
*policy
)
158 return cpufreq_frequency_table_verify(policy
,
159 &eps_cpu
[policy
->cpu
]->freq_table
[0]);
162 static int eps_cpu_init(struct cpufreq_policy
*policy
)
167 u8 current_multiplier
, current_voltage
;
168 u8 max_multiplier
, max_voltage
;
169 u8 min_multiplier
, min_voltage
;
172 struct eps_cpu_data
*centaur
;
173 struct cpuinfo_x86
*c
= &cpu_data(0);
174 struct cpufreq_frequency_table
*f_table
;
175 int k
, step
, voltage
;
179 if (policy
->cpu
!= 0)
183 printk(KERN_INFO
"eps: Detected VIA ");
185 switch (c
->x86_model
) {
187 rdmsr(0x1153, lo
, hi
);
188 brand
= (((lo
>> 2) ^ lo
) >> 18) & 3;
189 printk(KERN_CONT
"Model A ");
192 rdmsr(0x1154, lo
, hi
);
193 brand
= (((lo
>> 4) ^ (lo
>> 2))) & 0x000000ff;
194 printk(KERN_CONT
"Model D ");
200 printk(KERN_CONT
"C7-M\n");
203 printk(KERN_CONT
"C7\n");
206 printk(KERN_CONT
"Eden\n");
209 printk(KERN_CONT
"C7-D\n");
212 printk(KERN_CONT
"C3\n");
216 /* Enable Enhanced PowerSaver */
217 rdmsrl(MSR_IA32_MISC_ENABLE
, val
);
218 if (!(val
& 1 << 16)) {
220 wrmsrl(MSR_IA32_MISC_ENABLE
, val
);
221 /* Can be locked at 0 */
222 rdmsrl(MSR_IA32_MISC_ENABLE
, val
);
223 if (!(val
& 1 << 16)) {
224 printk(KERN_INFO
"eps: Can't enable Enhanced PowerSaver\n");
229 /* Print voltage and multiplier */
230 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
231 current_voltage
= lo
& 0xff;
232 printk(KERN_INFO
"eps: Current voltage = %dmV\n", current_voltage
* 16 + 700);
233 current_multiplier
= (lo
>> 8) & 0xff;
234 printk(KERN_INFO
"eps: Current multiplier = %d\n", current_multiplier
);
237 max_voltage
= hi
& 0xff;
238 printk(KERN_INFO
"eps: Highest voltage = %dmV\n", max_voltage
* 16 + 700);
239 max_multiplier
= (hi
>> 8) & 0xff;
240 printk(KERN_INFO
"eps: Highest multiplier = %d\n", max_multiplier
);
241 min_voltage
= (hi
>> 16) & 0xff;
242 printk(KERN_INFO
"eps: Lowest voltage = %dmV\n", min_voltage
* 16 + 700);
243 min_multiplier
= (hi
>> 24) & 0xff;
244 printk(KERN_INFO
"eps: Lowest multiplier = %d\n", min_multiplier
);
247 if (current_multiplier
== 0 || max_multiplier
== 0
248 || min_multiplier
== 0)
250 if (current_multiplier
> max_multiplier
251 || max_multiplier
<= min_multiplier
)
253 if (current_voltage
> 0x1f || max_voltage
> 0x1f)
255 if (max_voltage
< min_voltage
)
259 fsb
= cpu_khz
/ current_multiplier
;
260 /* Calc number of p-states supported */
261 if (brand
== EPS_BRAND_C7M
)
262 states
= max_multiplier
- min_multiplier
+ 1;
266 /* Allocate private data and frequency table for current cpu */
267 centaur
= kzalloc(sizeof(struct eps_cpu_data
)
268 + (states
+ 1) * sizeof(struct cpufreq_frequency_table
),
272 eps_cpu
[0] = centaur
;
274 /* Copy basic values */
277 /* Fill frequency and MSR value table */
278 f_table
= ¢aur
->freq_table
[0];
279 if (brand
!= EPS_BRAND_C7M
) {
280 f_table
[0].frequency
= fsb
* min_multiplier
;
281 f_table
[0].index
= (min_multiplier
<< 8) | min_voltage
;
282 f_table
[1].frequency
= fsb
* max_multiplier
;
283 f_table
[1].index
= (max_multiplier
<< 8) | max_voltage
;
284 f_table
[2].frequency
= CPUFREQ_TABLE_END
;
287 step
= ((max_voltage
- min_voltage
) * 256)
288 / (max_multiplier
- min_multiplier
);
289 for (i
= min_multiplier
; i
<= max_multiplier
; i
++) {
290 voltage
= (k
* step
) / 256 + min_voltage
;
291 f_table
[k
].frequency
= fsb
* i
;
292 f_table
[k
].index
= (i
<< 8) | voltage
;
295 f_table
[k
].frequency
= CPUFREQ_TABLE_END
;
298 policy
->cpuinfo
.transition_latency
= 140000; /* 844mV -> 700mV in ns */
299 policy
->cur
= fsb
* current_multiplier
;
301 ret
= cpufreq_frequency_table_cpuinfo(policy
, ¢aur
->freq_table
[0]);
307 cpufreq_frequency_table_get_attr(¢aur
->freq_table
[0], policy
->cpu
);
311 static int eps_cpu_exit(struct cpufreq_policy
*policy
)
313 unsigned int cpu
= policy
->cpu
;
314 struct eps_cpu_data
*centaur
;
317 if (eps_cpu
[cpu
] == NULL
)
319 centaur
= eps_cpu
[cpu
];
321 /* Get max frequency */
322 rdmsr(MSR_IA32_PERF_STATUS
, lo
, hi
);
323 /* Set max frequency */
324 eps_set_state(centaur
, cpu
, hi
& 0xffff);
326 cpufreq_frequency_table_put_attr(policy
->cpu
);
332 static struct freq_attr
* eps_attr
[] = {
333 &cpufreq_freq_attr_scaling_available_freqs
,
337 static struct cpufreq_driver eps_driver
= {
338 .verify
= eps_verify
,
339 .target
= eps_target
,
340 .init
= eps_cpu_init
,
341 .exit
= eps_cpu_exit
,
343 .name
= "e_powersaver",
344 .owner
= THIS_MODULE
,
348 static int __init
eps_init(void)
350 struct cpuinfo_x86
*c
= &cpu_data(0);
352 /* This driver will work only on Centaur C7 processors with
353 * Enhanced SpeedStep/PowerSaver registers */
354 if (c
->x86_vendor
!= X86_VENDOR_CENTAUR
355 || c
->x86
!= 6 || c
->x86_model
< 10)
357 if (!cpu_has(c
, X86_FEATURE_EST
))
360 if (cpufreq_register_driver(&eps_driver
))
365 static void __exit
eps_exit(void)
367 cpufreq_unregister_driver(&eps_driver
);
370 MODULE_AUTHOR("Rafa³ Bilski <rafalbilski@interia.pl>");
371 MODULE_DESCRIPTION("Enhanced PowerSaver driver for VIA C7 CPU's.");
372 MODULE_LICENSE("GPL");
374 module_init(eps_init
);
375 module_exit(eps_exit
);