[CPUFREQ][2/8] acpi: reorganize code to make MSR support addition easier
[linux-2.6/mini2440.git] / arch / i386 / kernel / cpu / cpufreq / acpi-cpufreq.c
blobebc9fe285748512b401653130b5a59d588d6055a
1 /*
2 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.4 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/smp.h>
32 #include <linux/sched.h>
33 #include <linux/cpufreq.h>
34 #include <linux/compiler.h>
35 #include <linux/sched.h> /* current */
36 #include <linux/dmi.h>
38 #include <linux/acpi.h>
39 #include <acpi/processor.h>
41 #include <asm/io.h>
42 #include <asm/processor.h>
43 #include <asm/cpufeature.h>
44 #include <asm/delay.h>
45 #include <asm/uaccess.h>
47 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
49 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
50 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
51 MODULE_LICENSE("GPL");
54 struct acpi_cpufreq_data {
55 struct acpi_processor_performance *acpi_data;
56 struct cpufreq_frequency_table *freq_table;
57 unsigned int resume;
60 static struct acpi_cpufreq_data *drv_data[NR_CPUS];
61 static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
63 static struct cpufreq_driver acpi_cpufreq_driver;
65 static unsigned int acpi_pstate_strict;
67 static unsigned extract_freq(u32 value, struct acpi_cpufreq_data *data)
69 struct acpi_processor_performance *perf;
70 int i;
72 perf = data->acpi_data;
74 for (i = 0; i < perf->state_count; i++) {
75 if (value == perf->states[i].status)
76 return data->freq_table[i].frequency;
78 return 0;
82 static void wrport(u16 port, u8 bit_width, u32 value)
84 if (bit_width <= 8) {
85 outb(value, port);
86 } else if (bit_width <= 16) {
87 outw(value, port);
88 } else if (bit_width <= 32) {
89 outl(value, port);
93 static void rdport(u16 port, u8 bit_width, u32 *ret)
95 *ret = 0;
96 if (bit_width <= 8) {
97 *ret = inb(port);
98 } else if (bit_width <= 16) {
99 *ret = inw(port);
100 } else if (bit_width <= 32) {
101 *ret = inl(port);
105 struct io_addr {
106 u16 port;
107 u8 bit_width;
110 struct drv_cmd {
111 cpumask_t mask;
112 struct io_addr addr;
113 u32 val;
116 static void do_drv_read(struct drv_cmd *cmd)
118 rdport(cmd->addr.port, cmd->addr.bit_width, &cmd->val);
119 return;
122 static void do_drv_write(struct drv_cmd *cmd)
124 wrport(cmd->addr.port, cmd->addr.bit_width, cmd->val);
125 return;
128 static inline void drv_read(struct drv_cmd *cmd)
130 cpumask_t saved_mask = current->cpus_allowed;
131 cmd->val = 0;
133 set_cpus_allowed(current, cmd->mask);
134 do_drv_read(cmd);
135 set_cpus_allowed(current, saved_mask);
139 static void drv_write(struct drv_cmd *cmd)
141 cpumask_t saved_mask = current->cpus_allowed;
142 unsigned int i;
144 for_each_cpu_mask(i, cmd->mask) {
145 set_cpus_allowed(current, cpumask_of_cpu(i));
146 do_drv_write(cmd);
149 set_cpus_allowed(current, saved_mask);
150 return;
153 static u32 get_cur_val(cpumask_t mask)
155 struct acpi_processor_performance *perf;
156 struct drv_cmd cmd;
158 if (unlikely(cpus_empty(mask)))
159 return 0;
161 perf = drv_data[first_cpu(mask)]->acpi_data;
162 cmd.addr.port = perf->control_register.address;
163 cmd.addr.bit_width = perf->control_register.bit_width;
164 cmd.mask = mask;
166 drv_read(&cmd);
168 dprintk("get_cur_val = %u\n", cmd.val);
170 return cmd.val;
173 static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
175 struct acpi_cpufreq_data *data = drv_data[cpu];
176 unsigned int freq;
178 dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
180 if (unlikely(data == NULL ||
181 data->acpi_data == NULL ||
182 data->freq_table == NULL)) {
183 return 0;
186 freq = extract_freq(get_cur_val(cpumask_of_cpu(cpu)), data);
187 dprintk("cur freq = %u\n", freq);
189 return freq;
192 static unsigned int check_freqs(cpumask_t mask, unsigned int freq,
193 struct acpi_cpufreq_data *data)
195 unsigned int cur_freq;
196 unsigned int i;
198 for (i = 0; i < 100; i++) {
199 cur_freq = extract_freq(get_cur_val(mask), data);
200 if (cur_freq == freq)
201 return 1;
202 udelay(10);
204 return 0;
207 static int acpi_cpufreq_target(struct cpufreq_policy *policy,
208 unsigned int target_freq,
209 unsigned int relation)
211 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
212 struct acpi_processor_performance *perf;
213 struct cpufreq_freqs freqs;
214 cpumask_t online_policy_cpus;
215 struct drv_cmd cmd;
216 unsigned int next_state = 0;
217 unsigned int next_perf_state = 0;
218 unsigned int i;
219 int result = 0;
221 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
223 if (unlikely(data == NULL ||
224 data->acpi_data == NULL ||
225 data->freq_table == NULL)) {
226 return -ENODEV;
229 perf = data->acpi_data;
230 result = cpufreq_frequency_table_target(policy,
231 data->freq_table,
232 target_freq,
233 relation,
234 &next_state);
235 if (unlikely(result))
236 return -ENODEV;
238 #ifdef CONFIG_HOTPLUG_CPU
239 /* cpufreq holds the hotplug lock, so we are safe from here on */
240 cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
241 #else
242 online_policy_cpus = policy->cpus;
243 #endif
245 cmd.val = get_cur_val(online_policy_cpus);
246 freqs.old = extract_freq(cmd.val, data);
247 freqs.new = data->freq_table[next_state].frequency;
248 next_perf_state = data->freq_table[next_state].index;
249 if (freqs.new == freqs.old) {
250 if (unlikely(data->resume)) {
251 dprintk("Called after resume, resetting to P%d\n", next_perf_state);
252 data->resume = 0;
253 } else {
254 dprintk("Already at target state (P%d)\n", next_perf_state);
255 return 0;
259 cmd.addr.port = perf->control_register.address;
260 cmd.addr.bit_width = perf->control_register.bit_width;
261 cmd.val = (u32) perf->states[next_perf_state].control;
263 cpus_clear(cmd.mask);
265 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
266 cmd.mask = online_policy_cpus;
267 else
268 cpu_set(policy->cpu, cmd.mask);
270 for_each_cpu_mask(i, cmd.mask) {
271 freqs.cpu = i;
272 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
275 drv_write(&cmd);
277 if (acpi_pstate_strict) {
278 if (!check_freqs(cmd.mask, freqs.new, data)) {
279 dprintk("acpi_cpufreq_target failed (%d)\n",
280 policy->cpu);
281 return -EAGAIN;
285 for_each_cpu_mask(i, cmd.mask) {
286 freqs.cpu = i;
287 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
289 perf->state = next_perf_state;
291 return result;
295 static int
296 acpi_cpufreq_verify (
297 struct cpufreq_policy *policy)
299 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
301 dprintk("acpi_cpufreq_verify\n");
303 return cpufreq_frequency_table_verify(policy, data->freq_table);
307 static unsigned long
308 acpi_cpufreq_guess_freq (
309 struct acpi_cpufreq_data *data,
310 unsigned int cpu)
312 struct acpi_processor_performance *perf = data->acpi_data;
314 if (cpu_khz) {
315 /* search the closest match to cpu_khz */
316 unsigned int i;
317 unsigned long freq;
318 unsigned long freqn = perf->states[0].core_frequency * 1000;
320 for (i = 0; i < (perf->state_count - 1); i++) {
321 freq = freqn;
322 freqn = perf->states[i+1].core_frequency * 1000;
323 if ((2 * cpu_khz) > (freqn + freq)) {
324 perf->state = i;
325 return (freq);
328 perf->state = perf->state_count - 1;
329 return (freqn);
330 } else {
331 /* assume CPU is at P0... */
332 perf->state = 0;
333 return perf->states[0].core_frequency * 1000;
339 * acpi_cpufreq_early_init - initialize ACPI P-States library
341 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
342 * in order to determine correct frequency and voltage pairings. We can
343 * do _PDC and _PSD and find out the processor dependency for the
344 * actual init that will happen later...
346 static int acpi_cpufreq_early_init(void)
348 struct acpi_processor_performance *data;
349 cpumask_t covered;
350 unsigned int i, j;
352 dprintk("acpi_cpufreq_early_init\n");
354 for_each_possible_cpu(i) {
355 data = kzalloc(sizeof(struct acpi_processor_performance),
356 GFP_KERNEL);
357 if (!data) {
358 for_each_cpu_mask(j, covered) {
359 kfree(acpi_perf_data[j]);
360 acpi_perf_data[j] = NULL;
362 return (-ENOMEM);
364 acpi_perf_data[i] = data;
365 cpu_set(i, covered);
368 /* Do initialization in ACPI core */
369 acpi_processor_preregister_performance(acpi_perf_data);
370 return 0;
374 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
375 * or do it in BIOS firmware and won't inform about it to OS. If not
376 * detected, this has a side effect of making CPU run at a different speed
377 * than OS intended it to run at. Detect it and handle it cleanly.
379 static int bios_with_sw_any_bug;
381 static int sw_any_bug_found(struct dmi_system_id *d)
383 bios_with_sw_any_bug = 1;
384 return 0;
387 static struct dmi_system_id sw_any_bug_dmi_table[] = {
389 .callback = sw_any_bug_found,
390 .ident = "Supermicro Server X6DLP",
391 .matches = {
392 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
393 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
394 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
400 static int
401 acpi_cpufreq_cpu_init (
402 struct cpufreq_policy *policy)
404 unsigned int i;
405 unsigned int valid_states = 0;
406 unsigned int cpu = policy->cpu;
407 struct acpi_cpufreq_data *data;
408 unsigned int result = 0;
409 struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
410 struct acpi_processor_performance *perf;
412 dprintk("acpi_cpufreq_cpu_init\n");
414 if (!acpi_perf_data[cpu])
415 return (-ENODEV);
417 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
418 if (!data)
419 return (-ENOMEM);
421 data->acpi_data = acpi_perf_data[cpu];
422 drv_data[cpu] = data;
424 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
425 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
428 result = acpi_processor_register_performance(data->acpi_data, cpu);
429 if (result)
430 goto err_free;
432 perf = data->acpi_data;
433 policy->shared_type = perf->shared_type;
435 * Will let policy->cpus know about dependency only when software
436 * coordination is required.
438 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
439 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
440 policy->cpus = perf->shared_cpu_map;
443 #ifdef CONFIG_SMP
444 dmi_check_system(sw_any_bug_dmi_table);
445 if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
446 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
447 policy->cpus = cpu_core_map[cpu];
449 #endif
451 /* capability check */
452 if (perf->state_count <= 1) {
453 dprintk("No P-States\n");
454 result = -ENODEV;
455 goto err_unreg;
458 if (perf->control_register.space_id != perf->status_register.space_id) {
459 result = -ENODEV;
460 goto err_unreg;
463 switch (perf->control_register.space_id) {
464 case ACPI_ADR_SPACE_SYSTEM_IO:
465 dprintk("SYSTEM IO addr space\n");
466 break;
467 default:
468 dprintk("Unknown addr space %d\n",
469 (u32) (perf->control_register.space_id));
470 result = -ENODEV;
471 goto err_unreg;
474 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
475 if (!data->freq_table) {
476 result = -ENOMEM;
477 goto err_unreg;
480 /* detect transition latency */
481 policy->cpuinfo.transition_latency = 0;
482 for (i=0; i<perf->state_count; i++) {
483 if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
484 policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
486 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
488 /* The current speed is unknown and not detectable by ACPI... */
489 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
491 /* table init */
492 for (i=0; i<perf->state_count; i++)
494 if ( i > 0 && perf->states[i].core_frequency ==
495 perf->states[i - 1].core_frequency)
496 continue;
498 data->freq_table[valid_states].index = i;
499 data->freq_table[valid_states].frequency =
500 perf->states[i].core_frequency * 1000;
501 valid_states++;
503 data->freq_table[perf->state_count].frequency = CPUFREQ_TABLE_END;
505 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
506 if (result) {
507 goto err_freqfree;
510 /* notify BIOS that we exist */
511 acpi_processor_notify_smm(THIS_MODULE);
513 dprintk("CPU%u - ACPI performance management activated.\n", cpu);
514 for (i = 0; i < perf->state_count; i++)
515 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
516 (i == perf->state?'*':' '), i,
517 (u32) perf->states[i].core_frequency,
518 (u32) perf->states[i].power,
519 (u32) perf->states[i].transition_latency);
521 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
524 * the first call to ->target() should result in us actually
525 * writing something to the appropriate registers.
527 data->resume = 1;
529 return result;
531 err_freqfree:
532 kfree(data->freq_table);
533 err_unreg:
534 acpi_processor_unregister_performance(perf, cpu);
535 err_free:
536 kfree(data);
537 drv_data[cpu] = NULL;
539 return (result);
543 static int
544 acpi_cpufreq_cpu_exit (
545 struct cpufreq_policy *policy)
547 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
550 dprintk("acpi_cpufreq_cpu_exit\n");
552 if (data) {
553 cpufreq_frequency_table_put_attr(policy->cpu);
554 drv_data[policy->cpu] = NULL;
555 acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
556 kfree(data);
559 return (0);
562 static int
563 acpi_cpufreq_resume (
564 struct cpufreq_policy *policy)
566 struct acpi_cpufreq_data *data = drv_data[policy->cpu];
569 dprintk("acpi_cpufreq_resume\n");
571 data->resume = 1;
573 return (0);
577 static struct freq_attr* acpi_cpufreq_attr[] = {
578 &cpufreq_freq_attr_scaling_available_freqs,
579 NULL,
582 static struct cpufreq_driver acpi_cpufreq_driver = {
583 .verify = acpi_cpufreq_verify,
584 .target = acpi_cpufreq_target,
585 .get = get_cur_freq_on_cpu,
586 .init = acpi_cpufreq_cpu_init,
587 .exit = acpi_cpufreq_cpu_exit,
588 .resume = acpi_cpufreq_resume,
589 .name = "acpi-cpufreq",
590 .owner = THIS_MODULE,
591 .attr = acpi_cpufreq_attr,
595 static int __init
596 acpi_cpufreq_init (void)
598 dprintk("acpi_cpufreq_init\n");
600 acpi_cpufreq_early_init();
602 return cpufreq_register_driver(&acpi_cpufreq_driver);
606 static void __exit
607 acpi_cpufreq_exit (void)
609 unsigned int i;
610 dprintk("acpi_cpufreq_exit\n");
612 cpufreq_unregister_driver(&acpi_cpufreq_driver);
614 for_each_possible_cpu(i) {
615 kfree(acpi_perf_data[i]);
616 acpi_perf_data[i] = NULL;
618 return;
621 module_param(acpi_pstate_strict, uint, 0644);
622 MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
624 late_initcall(acpi_cpufreq_init);
625 module_exit(acpi_cpufreq_exit);
627 MODULE_ALIAS("acpi");