[CPUFREQ][1/8] acpi-cpufreq: software coordination and handle all CPUs in the group
[pandora-kernel.git] / arch / i386 / kernel / cpu / cpufreq / acpi-cpufreq.c
1 /*
2  * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.3 $)
3  *
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  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/cpufreq.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/compiler.h>
34 #include <linux/sched.h>        /* current */
35 #include <linux/dmi.h>
36 #include <asm/io.h>
37 #include <asm/delay.h>
38 #include <asm/uaccess.h>
39
40 #include <linux/acpi.h>
41 #include <acpi/processor.h>
42
43 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
44
45 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
46 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
47 MODULE_LICENSE("GPL");
48
49
50 struct cpufreq_acpi_io {
51         struct acpi_processor_performance       *acpi_data;
52         struct cpufreq_frequency_table          *freq_table;
53         unsigned int                            resume;
54 };
55
56 static struct cpufreq_acpi_io   *acpi_io_data[NR_CPUS];
57 static struct acpi_processor_performance        *acpi_perf_data[NR_CPUS];
58
59 static struct cpufreq_driver acpi_cpufreq_driver;
60
61 static unsigned int acpi_pstate_strict;
62
63 static int
64 acpi_processor_write_port(
65         u16     port,
66         u8      bit_width,
67         u32     value)
68 {
69         if (bit_width <= 8) {
70                 outb(value, port);
71         } else if (bit_width <= 16) {
72                 outw(value, port);
73         } else if (bit_width <= 32) {
74                 outl(value, port);
75         } else {
76                 return -ENODEV;
77         }
78         return 0;
79 }
80
81 static int
82 acpi_processor_read_port(
83         u16     port,
84         u8      bit_width,
85         u32     *ret)
86 {
87         *ret = 0;
88         if (bit_width <= 8) {
89                 *ret = inb(port);
90         } else if (bit_width <= 16) {
91                 *ret = inw(port);
92         } else if (bit_width <= 32) {
93                 *ret = inl(port);
94         } else {
95                 return -ENODEV;
96         }
97         return 0;
98 }
99
100 static int
101 acpi_processor_set_performance (
102         struct cpufreq_acpi_io  *data,
103         unsigned int            cpu,
104         int                     state)
105 {
106         u16                     port = 0;
107         u8                      bit_width = 0;
108         int                     i = 0;
109         int                     ret = 0;
110         u32                     value = 0;
111         int                     retval;
112         struct acpi_processor_performance       *perf;
113
114         dprintk("acpi_processor_set_performance\n");
115
116         retval = 0;
117         perf = data->acpi_data; 
118         if (state == perf->state) {
119                 if (unlikely(data->resume)) {
120                         dprintk("Called after resume, resetting to P%d\n", state);
121                         data->resume = 0;
122                 } else {
123                         dprintk("Already at target state (P%d)\n", state);
124                         return (retval);
125                 }
126         }
127
128         dprintk("Transitioning from P%d to P%d\n", perf->state, state);
129
130         /*
131          * First we write the target state's 'control' value to the
132          * control_register.
133          */
134
135         port = perf->control_register.address;
136         bit_width = perf->control_register.bit_width;
137         value = (u32) perf->states[state].control;
138
139         dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
140
141         ret = acpi_processor_write_port(port, bit_width, value);
142         if (ret) {
143                 dprintk("Invalid port width 0x%04x\n", bit_width);
144                 return (ret);
145         }
146
147         /*
148          * Assume the write went through when acpi_pstate_strict is not used.
149          * As read status_register is an expensive operation and there 
150          * are no specific error cases where an IO port write will fail.
151          */
152         if (acpi_pstate_strict) {
153                 /* Then we read the 'status_register' and compare the value 
154                  * with the target state's 'status' to make sure the 
155                  * transition was successful.
156                  * Note that we'll poll for up to 1ms (100 cycles of 10us) 
157                  * before giving up.
158                  */
159
160                 port = perf->status_register.address;
161                 bit_width = perf->status_register.bit_width;
162
163                 dprintk("Looking for 0x%08x from port 0x%04x\n",
164                         (u32) perf->states[state].status, port);
165
166                 for (i = 0; i < 100; i++) {
167                         ret = acpi_processor_read_port(port, bit_width, &value);
168                         if (ret) {      
169                                 dprintk("Invalid port width 0x%04x\n", bit_width);
170                                 return (ret);
171                         }
172                         if (value == (u32) perf->states[state].status)
173                                 break;
174                         udelay(10);
175                 }
176         } else {
177                 value = (u32) perf->states[state].status;
178         }
179
180         if (unlikely(value != (u32) perf->states[state].status)) {
181                 printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
182                 retval = -ENODEV;
183                 return (retval);
184         }
185
186         dprintk("Transition successful after %d microseconds\n", i * 10);
187
188         perf->state = state;
189         return (retval);
190 }
191
192
193 static int
194 acpi_cpufreq_target (
195         struct cpufreq_policy   *policy,
196         unsigned int target_freq,
197         unsigned int relation)
198 {
199         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
200         struct cpufreq_acpi_io *cpudata;
201         struct acpi_processor_performance *perf;
202         struct cpufreq_freqs freqs;
203         cpumask_t online_policy_cpus;
204         cpumask_t saved_mask;
205         cpumask_t set_mask;
206         cpumask_t covered_cpus;
207         unsigned int cur_state = 0;
208         unsigned int next_state = 0;
209         unsigned int result = 0;
210         unsigned int j;
211         unsigned int tmp;
212
213         dprintk("acpi_cpufreq_setpolicy\n");
214
215         result = cpufreq_frequency_table_target(policy,
216                         data->freq_table,
217                         target_freq,
218                         relation,
219                         &next_state);
220         if (unlikely(result))
221                 return (result);
222
223         perf = data->acpi_data;
224         cur_state = perf->state;
225         freqs.old = data->freq_table[cur_state].frequency;
226         freqs.new = data->freq_table[next_state].frequency;
227
228 #ifdef CONFIG_HOTPLUG_CPU
229         /* cpufreq holds the hotplug lock, so we are safe from here on */
230         cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
231 #else
232         online_policy_cpus = policy->cpus;
233 #endif
234
235         for_each_cpu_mask(j, online_policy_cpus) {
236                 freqs.cpu = j;
237                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
238         }
239
240         /*
241          * We need to call driver->target() on all or any CPU in
242          * policy->cpus, depending on policy->shared_type.
243          */
244         saved_mask = current->cpus_allowed;
245         cpus_clear(covered_cpus);
246         for_each_cpu_mask(j, online_policy_cpus) {
247                 /*
248                  * Support for SMP systems.
249                  * Make sure we are running on CPU that wants to change freq
250                  */
251                 cpus_clear(set_mask);
252                 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
253                         cpus_or(set_mask, set_mask, online_policy_cpus);
254                 else
255                         cpu_set(j, set_mask);
256
257                 set_cpus_allowed(current, set_mask);
258                 if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
259                         dprintk("couldn't limit to CPUs in this domain\n");
260                         result = -EAGAIN;
261                         break;
262                 }
263
264                 cpudata = acpi_io_data[j];
265                 result = acpi_processor_set_performance(cpudata, j, next_state);
266                 if (result) {
267                         result = -EAGAIN;
268                         break;
269                 }
270
271                 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
272                         break;
273  
274                 cpu_set(j, covered_cpus);
275         }
276
277         for_each_cpu_mask(j, online_policy_cpus) {
278                 freqs.cpu = j;
279                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
280         }
281
282         if (unlikely(result)) {
283                 /*
284                  * We have failed halfway through the frequency change.
285                  * We have sent callbacks to online_policy_cpus and
286                  * acpi_processor_set_performance() has been called on 
287                  * coverd_cpus. Best effort undo..
288                  */
289
290                 if (!cpus_empty(covered_cpus)) {
291                         for_each_cpu_mask(j, covered_cpus) {
292                                 cpus_clear(set_mask);
293                                 cpu_set(j, set_mask);
294                                 set_cpus_allowed(current, set_mask);
295                                 cpudata = acpi_io_data[j];
296                                 acpi_processor_set_performance(cpudata,
297                                                 j, 
298                                                 cur_state);
299                         }
300                 }
301
302                 tmp = freqs.new;
303                 freqs.new = freqs.old;
304                 freqs.old = tmp;
305                 for_each_cpu_mask(j, online_policy_cpus) {
306                         freqs.cpu = j;
307                         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
308                         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
309                 }
310         }
311
312         set_cpus_allowed(current, saved_mask);
313         return (result);
314 }
315
316
317 static int
318 acpi_cpufreq_verify (
319         struct cpufreq_policy   *policy)
320 {
321         unsigned int result = 0;
322         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
323
324         dprintk("acpi_cpufreq_verify\n");
325
326         result = cpufreq_frequency_table_verify(policy, 
327                         data->freq_table);
328
329         return (result);
330 }
331
332
333 static unsigned long
334 acpi_cpufreq_guess_freq (
335         struct cpufreq_acpi_io  *data,
336         unsigned int            cpu)
337 {
338         struct acpi_processor_performance       *perf = data->acpi_data;
339
340         if (cpu_khz) {
341                 /* search the closest match to cpu_khz */
342                 unsigned int i;
343                 unsigned long freq;
344                 unsigned long freqn = perf->states[0].core_frequency * 1000;
345
346                 for (i = 0; i < (perf->state_count - 1); i++) {
347                         freq = freqn;
348                         freqn = perf->states[i+1].core_frequency * 1000;
349                         if ((2 * cpu_khz) > (freqn + freq)) {
350                                 perf->state = i;
351                                 return (freq);
352                         }
353                 }
354                 perf->state = perf->state_count - 1;
355                 return (freqn);
356         } else {
357                 /* assume CPU is at P0... */
358                 perf->state = 0;
359                 return perf->states[0].core_frequency * 1000;
360         }
361 }
362
363
364 /*
365  * acpi_cpufreq_early_init - initialize ACPI P-States library
366  *
367  * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
368  * in order to determine correct frequency and voltage pairings. We can
369  * do _PDC and _PSD and find out the processor dependency for the
370  * actual init that will happen later...
371  */
372 static int acpi_cpufreq_early_init_acpi(void)
373 {
374         struct acpi_processor_performance       *data;
375         unsigned int                            i, j;
376
377         dprintk("acpi_cpufreq_early_init\n");
378
379         for_each_possible_cpu(i) {
380                 data = kzalloc(sizeof(struct acpi_processor_performance), 
381                         GFP_KERNEL);
382                 if (!data) {
383                         for_each_possible_cpu(j) {
384                                 kfree(acpi_perf_data[j]);
385                                 acpi_perf_data[j] = NULL;
386                         }
387                         return (-ENOMEM);
388                 }
389                 acpi_perf_data[i] = data;
390         }
391
392         /* Do initialization in ACPI core */
393         return acpi_processor_preregister_performance(acpi_perf_data);
394 }
395
396 /*
397  * Some BIOSes do SW_ANY coordination internally, either set it up in hw
398  * or do it in BIOS firmware and won't inform about it to OS. If not
399  * detected, this has a side effect of making CPU run at a different speed
400  * than OS intended it to run at. Detect it and handle it cleanly.
401  */
402 static int bios_with_sw_any_bug;
403
404 static int sw_any_bug_found(struct dmi_system_id *d)
405 {
406         bios_with_sw_any_bug = 1;
407         return 0;
408 }
409
410 static struct dmi_system_id sw_any_bug_dmi_table[] = {
411         {
412                 .callback = sw_any_bug_found,
413                 .ident = "Supermicro Server X6DLP",
414                 .matches = {
415                         DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
416                         DMI_MATCH(DMI_BIOS_VERSION, "080010"),
417                         DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
418                 },
419         },
420         { }
421 };
422
423 static int
424 acpi_cpufreq_cpu_init (
425         struct cpufreq_policy   *policy)
426 {
427         unsigned int            i;
428         unsigned int            cpu = policy->cpu;
429         struct cpufreq_acpi_io  *data;
430         unsigned int            result = 0;
431         struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
432         struct acpi_processor_performance       *perf;
433
434         dprintk("acpi_cpufreq_cpu_init\n");
435
436         if (!acpi_perf_data[cpu])
437                 return (-ENODEV);
438
439         data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
440         if (!data)
441                 return (-ENOMEM);
442
443         data->acpi_data = acpi_perf_data[cpu];
444         acpi_io_data[cpu] = data;
445
446         result = acpi_processor_register_performance(data->acpi_data, cpu);
447
448         if (result)
449                 goto err_free;
450
451         perf = data->acpi_data;
452         policy->shared_type = perf->shared_type;
453         /*
454          * Will let policy->cpus know about dependency only when software 
455          * coordination is required.
456          */
457         if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
458             policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
459                 policy->cpus = perf->shared_cpu_map;
460         }
461
462 #ifdef CONFIG_SMP
463         dmi_check_system(sw_any_bug_dmi_table);
464         if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
465                 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
466                 policy->cpus = cpu_core_map[cpu];
467         }
468 #endif
469
470         if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
471                 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
472         }
473
474         /* capability check */
475         if (perf->state_count <= 1) {
476                 dprintk("No P-States\n");
477                 result = -ENODEV;
478                 goto err_unreg;
479         }
480
481         if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
482             (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
483                 dprintk("Unsupported address space [%d, %d]\n",
484                         (u32) (perf->control_register.space_id),
485                         (u32) (perf->status_register.space_id));
486                 result = -ENODEV;
487                 goto err_unreg;
488         }
489
490         /* alloc freq_table */
491         data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
492         if (!data->freq_table) {
493                 result = -ENOMEM;
494                 goto err_unreg;
495         }
496
497         /* detect transition latency */
498         policy->cpuinfo.transition_latency = 0;
499         for (i=0; i<perf->state_count; i++) {
500                 if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
501                         policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
502         }
503         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
504
505         /* The current speed is unknown and not detectable by ACPI...  */
506         policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
507
508         /* table init */
509         for (i=0; i<=perf->state_count; i++)
510         {
511                 data->freq_table[i].index = i;
512                 if (i<perf->state_count)
513                         data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
514                 else
515                         data->freq_table[i].frequency = CPUFREQ_TABLE_END;
516         }
517
518         result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
519         if (result) {
520                 goto err_freqfree;
521         }
522
523         /* notify BIOS that we exist */
524         acpi_processor_notify_smm(THIS_MODULE);
525
526         printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
527                cpu);
528         for (i = 0; i < perf->state_count; i++)
529                 dprintk("     %cP%d: %d MHz, %d mW, %d uS\n",
530                         (i == perf->state?'*':' '), i,
531                         (u32) perf->states[i].core_frequency,
532                         (u32) perf->states[i].power,
533                         (u32) perf->states[i].transition_latency);
534
535         cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
536         
537         /*
538          * the first call to ->target() should result in us actually
539          * writing something to the appropriate registers.
540          */
541         data->resume = 1;
542         
543         return (result);
544
545  err_freqfree:
546         kfree(data->freq_table);
547  err_unreg:
548         acpi_processor_unregister_performance(perf, cpu);
549  err_free:
550         kfree(data);
551         acpi_io_data[cpu] = NULL;
552
553         return (result);
554 }
555
556
557 static int
558 acpi_cpufreq_cpu_exit (
559         struct cpufreq_policy   *policy)
560 {
561         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
562
563
564         dprintk("acpi_cpufreq_cpu_exit\n");
565
566         if (data) {
567                 cpufreq_frequency_table_put_attr(policy->cpu);
568                 acpi_io_data[policy->cpu] = NULL;
569                 acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
570                 kfree(data);
571         }
572
573         return (0);
574 }
575
576 static int
577 acpi_cpufreq_resume (
578         struct cpufreq_policy   *policy)
579 {
580         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
581
582
583         dprintk("acpi_cpufreq_resume\n");
584
585         data->resume = 1;
586
587         return (0);
588 }
589
590
591 static struct freq_attr* acpi_cpufreq_attr[] = {
592         &cpufreq_freq_attr_scaling_available_freqs,
593         NULL,
594 };
595
596 static struct cpufreq_driver acpi_cpufreq_driver = {
597         .verify = acpi_cpufreq_verify,
598         .target = acpi_cpufreq_target,
599         .init   = acpi_cpufreq_cpu_init,
600         .exit   = acpi_cpufreq_cpu_exit,
601         .resume = acpi_cpufreq_resume,
602         .name   = "acpi-cpufreq",
603         .owner  = THIS_MODULE,
604         .attr   = acpi_cpufreq_attr,
605 };
606
607
608 static int __init
609 acpi_cpufreq_init (void)
610 {
611         dprintk("acpi_cpufreq_init\n");
612
613         acpi_cpufreq_early_init_acpi();
614
615         return cpufreq_register_driver(&acpi_cpufreq_driver);
616 }
617
618
619 static void __exit
620 acpi_cpufreq_exit (void)
621 {
622         unsigned int    i;
623         dprintk("acpi_cpufreq_exit\n");
624
625         cpufreq_unregister_driver(&acpi_cpufreq_driver);
626
627         for_each_possible_cpu(i) {
628                 kfree(acpi_perf_data[i]);
629                 acpi_perf_data[i] = NULL;
630         }
631         return;
632 }
633
634 module_param(acpi_pstate_strict, uint, 0644);
635 MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
636
637 late_initcall(acpi_cpufreq_init);
638 module_exit(acpi_cpufreq_exit);
639
640 MODULE_ALIAS("acpi");