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>
15 #include <linux/timex.h>
17 #include <linux/delay.h>
22 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
23 #include <linux/acpi.h>
24 #include <acpi/processor.h>
27 #define EPS_BRAND_C7M 0
28 #define EPS_BRAND_C7 1
29 #define EPS_BRAND_EDEN 2
30 #define EPS_BRAND_C3 3
31 #define EPS_BRAND_C7D 4
35 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
38 struct cpufreq_frequency_table freq_table[];
41 static struct eps_cpu_data *eps_cpu[NR_CPUS];
43 /* Module parameters */
44 static int freq_failsafe_off;
45 static int voltage_failsafe_off;
47 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
48 static int ignore_acpi_limit;
50 static struct acpi_processor_performance *eps_acpi_cpu_perf;
52 /* Minimum necessary to get acpi_processor_get_bios_limit() working */
53 static int eps_acpi_init(void)
55 eps_acpi_cpu_perf = kzalloc(sizeof(struct acpi_processor_performance),
57 if (!eps_acpi_cpu_perf)
60 if (!zalloc_cpumask_var(&eps_acpi_cpu_perf->shared_cpu_map,
62 kfree(eps_acpi_cpu_perf);
63 eps_acpi_cpu_perf = NULL;
67 if (acpi_processor_register_performance(eps_acpi_cpu_perf, 0)) {
68 free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
69 kfree(eps_acpi_cpu_perf);
70 eps_acpi_cpu_perf = NULL;
76 static int eps_acpi_exit(struct cpufreq_policy *policy)
78 if (eps_acpi_cpu_perf) {
79 acpi_processor_unregister_performance(eps_acpi_cpu_perf, 0);
80 free_cpumask_var(eps_acpi_cpu_perf->shared_cpu_map);
81 kfree(eps_acpi_cpu_perf);
82 eps_acpi_cpu_perf = NULL;
88 static unsigned int eps_get(unsigned int cpu)
90 struct eps_cpu_data *centaur;
95 centaur = eps_cpu[cpu];
99 /* Return current frequency */
100 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
101 return centaur->fsb * ((lo >> 8) & 0xff);
104 static int eps_set_state(struct eps_cpu_data *centaur,
108 struct cpufreq_freqs freqs;
113 freqs.old = eps_get(cpu);
114 freqs.new = centaur->fsb * ((dest_state >> 8) & 0xff);
116 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
118 /* Wait while CPU is busy */
119 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
121 while (lo & ((1 << 16) | (1 << 17))) {
123 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
125 if (unlikely(i > 64)) {
130 /* Set new multiplier and voltage */
131 wrmsr(MSR_IA32_PERF_CTL, dest_state & 0xffff, 0);
132 /* Wait until transition end */
136 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
138 if (unlikely(i > 64)) {
142 } while (lo & ((1 << 16) | (1 << 17)));
144 /* Return current frequency */
146 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
147 freqs.new = centaur->fsb * ((lo >> 8) & 0xff);
151 u8 current_multiplier, current_voltage;
153 /* Print voltage and multiplier */
154 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
155 current_voltage = lo & 0xff;
156 printk(KERN_INFO "eps: Current voltage = %dmV\n",
157 current_voltage * 16 + 700);
158 current_multiplier = (lo >> 8) & 0xff;
159 printk(KERN_INFO "eps: Current multiplier = %d\n",
163 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
167 static int eps_target(struct cpufreq_policy *policy,
168 unsigned int target_freq,
169 unsigned int relation)
171 struct eps_cpu_data *centaur;
172 unsigned int newstate = 0;
173 unsigned int cpu = policy->cpu;
174 unsigned int dest_state;
177 if (unlikely(eps_cpu[cpu] == NULL))
179 centaur = eps_cpu[cpu];
181 if (unlikely(cpufreq_frequency_table_target(policy,
182 &eps_cpu[cpu]->freq_table[0],
189 /* Make frequency transition */
190 dest_state = centaur->freq_table[newstate].index & 0xffff;
191 ret = eps_set_state(centaur, cpu, dest_state);
193 printk(KERN_ERR "eps: Timeout!\n");
197 static int eps_verify(struct cpufreq_policy *policy)
199 return cpufreq_frequency_table_verify(policy,
200 &eps_cpu[policy->cpu]->freq_table[0]);
203 static int eps_cpu_init(struct cpufreq_policy *policy)
208 u8 current_multiplier, current_voltage;
209 u8 max_multiplier, max_voltage;
210 u8 min_multiplier, min_voltage;
213 struct eps_cpu_data *centaur;
214 struct cpuinfo_x86 *c = &cpu_data(0);
215 struct cpufreq_frequency_table *f_table;
216 int k, step, voltage;
219 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
223 if (policy->cpu != 0)
227 printk(KERN_INFO "eps: Detected VIA ");
229 switch (c->x86_model) {
231 rdmsr(0x1153, lo, hi);
232 brand = (((lo >> 2) ^ lo) >> 18) & 3;
233 printk(KERN_CONT "Model A ");
236 rdmsr(0x1154, lo, hi);
237 brand = (((lo >> 4) ^ (lo >> 2))) & 0x000000ff;
238 printk(KERN_CONT "Model D ");
244 printk(KERN_CONT "C7-M\n");
247 printk(KERN_CONT "C7\n");
250 printk(KERN_CONT "Eden\n");
253 printk(KERN_CONT "C7-D\n");
256 printk(KERN_CONT "C3\n");
260 /* Enable Enhanced PowerSaver */
261 rdmsrl(MSR_IA32_MISC_ENABLE, val);
262 if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
263 val |= MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP;
264 wrmsrl(MSR_IA32_MISC_ENABLE, val);
265 /* Can be locked at 0 */
266 rdmsrl(MSR_IA32_MISC_ENABLE, val);
267 if (!(val & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP)) {
268 printk(KERN_INFO "eps: Can't enable Enhanced PowerSaver\n");
273 /* Print voltage and multiplier */
274 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
275 current_voltage = lo & 0xff;
276 printk(KERN_INFO "eps: Current voltage = %dmV\n",
277 current_voltage * 16 + 700);
278 current_multiplier = (lo >> 8) & 0xff;
279 printk(KERN_INFO "eps: Current multiplier = %d\n", current_multiplier);
282 max_voltage = hi & 0xff;
283 printk(KERN_INFO "eps: Highest voltage = %dmV\n",
284 max_voltage * 16 + 700);
285 max_multiplier = (hi >> 8) & 0xff;
286 printk(KERN_INFO "eps: Highest multiplier = %d\n", max_multiplier);
287 min_voltage = (hi >> 16) & 0xff;
288 printk(KERN_INFO "eps: Lowest voltage = %dmV\n",
289 min_voltage * 16 + 700);
290 min_multiplier = (hi >> 24) & 0xff;
291 printk(KERN_INFO "eps: Lowest multiplier = %d\n", min_multiplier);
294 if (current_multiplier == 0 || max_multiplier == 0
295 || min_multiplier == 0)
297 if (current_multiplier > max_multiplier
298 || max_multiplier <= min_multiplier)
300 if (current_voltage > 0x1f || max_voltage > 0x1f)
302 if (max_voltage < min_voltage
303 || current_voltage < min_voltage
304 || current_voltage > max_voltage)
307 /* Check for systems using underclocked CPU */
308 if (!freq_failsafe_off && max_multiplier != current_multiplier) {
309 printk(KERN_INFO "eps: Your processor is running at different "
310 "frequency then its maximum. Aborting.\n");
311 printk(KERN_INFO "eps: You can use freq_failsafe_off option "
312 "to disable this check.\n");
315 if (!voltage_failsafe_off && max_voltage != current_voltage) {
316 printk(KERN_INFO "eps: Your processor is running at different "
317 "voltage then its maximum. Aborting.\n");
318 printk(KERN_INFO "eps: You can use voltage_failsafe_off "
319 "option to disable this check.\n");
324 fsb = cpu_khz / current_multiplier;
326 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
327 /* Check for ACPI processor speed limit */
328 if (!ignore_acpi_limit && !eps_acpi_init()) {
329 if (!acpi_processor_get_bios_limit(policy->cpu, &limit)) {
330 printk(KERN_INFO "eps: ACPI limit %u.%uGHz\n",
332 (limit%1000000)/10000);
333 eps_acpi_exit(policy);
334 /* Check if max_multiplier is in BIOS limits */
335 if (limit && max_multiplier * fsb > limit) {
336 printk(KERN_INFO "eps: Aborting.\n");
343 /* Calc number of p-states supported */
344 if (brand == EPS_BRAND_C7M)
345 states = max_multiplier - min_multiplier + 1;
349 /* Allocate private data and frequency table for current cpu */
350 centaur = kzalloc(sizeof(struct eps_cpu_data)
351 + (states + 1) * sizeof(struct cpufreq_frequency_table),
355 eps_cpu[0] = centaur;
357 /* Copy basic values */
359 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
360 centaur->bios_limit = limit;
363 /* Fill frequency and MSR value table */
364 f_table = ¢aur->freq_table[0];
365 if (brand != EPS_BRAND_C7M) {
366 f_table[0].frequency = fsb * min_multiplier;
367 f_table[0].index = (min_multiplier << 8) | min_voltage;
368 f_table[1].frequency = fsb * max_multiplier;
369 f_table[1].index = (max_multiplier << 8) | max_voltage;
370 f_table[2].frequency = CPUFREQ_TABLE_END;
373 step = ((max_voltage - min_voltage) * 256)
374 / (max_multiplier - min_multiplier);
375 for (i = min_multiplier; i <= max_multiplier; i++) {
376 voltage = (k * step) / 256 + min_voltage;
377 f_table[k].frequency = fsb * i;
378 f_table[k].index = (i << 8) | voltage;
381 f_table[k].frequency = CPUFREQ_TABLE_END;
384 policy->cpuinfo.transition_latency = 140000; /* 844mV -> 700mV in ns */
385 policy->cur = fsb * current_multiplier;
387 ret = cpufreq_frequency_table_cpuinfo(policy, ¢aur->freq_table[0]);
393 cpufreq_frequency_table_get_attr(¢aur->freq_table[0], policy->cpu);
397 static int eps_cpu_exit(struct cpufreq_policy *policy)
399 unsigned int cpu = policy->cpu;
402 cpufreq_frequency_table_put_attr(policy->cpu);
408 static struct freq_attr *eps_attr[] = {
409 &cpufreq_freq_attr_scaling_available_freqs,
413 static struct cpufreq_driver eps_driver = {
414 .verify = eps_verify,
415 .target = eps_target,
416 .init = eps_cpu_init,
417 .exit = eps_cpu_exit,
419 .name = "e_powersaver",
420 .owner = THIS_MODULE,
424 static int __init eps_init(void)
426 struct cpuinfo_x86 *c = &cpu_data(0);
428 /* This driver will work only on Centaur C7 processors with
429 * Enhanced SpeedStep/PowerSaver registers */
430 if (c->x86_vendor != X86_VENDOR_CENTAUR
431 || c->x86 != 6 || c->x86_model < 10)
433 if (!cpu_has(c, X86_FEATURE_EST))
436 if (cpufreq_register_driver(&eps_driver))
441 static void __exit eps_exit(void)
443 cpufreq_unregister_driver(&eps_driver);
446 /* Allow user to overclock his machine or to change frequency to higher after
447 * unloading module */
448 module_param(freq_failsafe_off, int, 0644);
449 MODULE_PARM_DESC(freq_failsafe_off, "Disable current vs max frequency check");
450 module_param(voltage_failsafe_off, int, 0644);
451 MODULE_PARM_DESC(voltage_failsafe_off, "Disable current vs max voltage check");
452 #if defined CONFIG_ACPI_PROCESSOR || defined CONFIG_ACPI_PROCESSOR_MODULE
453 module_param(ignore_acpi_limit, int, 0644);
454 MODULE_PARM_DESC(ignore_acpi_limit, "Don't check ACPI's processor speed limit");
457 MODULE_AUTHOR("Rafal Bilski <rafalbilski@interia.pl>");
458 MODULE_DESCRIPTION("Enhanced PowerSaver driver for VIA C7 CPU's.");
459 MODULE_LICENSE("GPL");
461 module_init(eps_init);
462 module_exit(eps_exit);