Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[linux-2.6] / 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/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/cpufreq.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/compiler.h>
35 #include <linux/sched.h>        /* current */
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
58 static struct cpufreq_driver acpi_cpufreq_driver;
59
60 static unsigned int acpi_pstate_strict;
61
62 static int
63 acpi_processor_write_port(
64         u16     port,
65         u8      bit_width,
66         u32     value)
67 {
68         if (bit_width <= 8) {
69                 outb(value, port);
70         } else if (bit_width <= 16) {
71                 outw(value, port);
72         } else if (bit_width <= 32) {
73                 outl(value, port);
74         } else {
75                 return -ENODEV;
76         }
77         return 0;
78 }
79
80 static int
81 acpi_processor_read_port(
82         u16     port,
83         u8      bit_width,
84         u32     *ret)
85 {
86         *ret = 0;
87         if (bit_width <= 8) {
88                 *ret = inb(port);
89         } else if (bit_width <= 16) {
90                 *ret = inw(port);
91         } else if (bit_width <= 32) {
92                 *ret = inl(port);
93         } else {
94                 return -ENODEV;
95         }
96         return 0;
97 }
98
99 static int
100 acpi_processor_set_performance (
101         struct cpufreq_acpi_io  *data,
102         unsigned int            cpu,
103         int                     state)
104 {
105         u16                     port = 0;
106         u8                      bit_width = 0;
107         int                     ret = 0;
108         u32                     value = 0;
109         int                     i = 0;
110         struct cpufreq_freqs    cpufreq_freqs;
111         cpumask_t               saved_mask;
112         int                     retval;
113
114         dprintk("acpi_processor_set_performance\n");
115
116         /*
117          * TBD: Use something other than set_cpus_allowed.
118          * As set_cpus_allowed is a bit racy, 
119          * with any other set_cpus_allowed for this process.
120          */
121         saved_mask = current->cpus_allowed;
122         set_cpus_allowed(current, cpumask_of_cpu(cpu));
123         if (smp_processor_id() != cpu) {
124                 return (-EAGAIN);
125         }
126         
127         if (state == data->acpi_data.state) {
128                 if (unlikely(data->resume)) {
129                         dprintk("Called after resume, resetting to P%d\n", state);
130                         data->resume = 0;
131                 } else {
132                         dprintk("Already at target state (P%d)\n", state);
133                         retval = 0;
134                         goto migrate_end;
135                 }
136         }
137
138         dprintk("Transitioning from P%d to P%d\n",
139                 data->acpi_data.state, state);
140
141         /* cpufreq frequency struct */
142         cpufreq_freqs.cpu = cpu;
143         cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
144         cpufreq_freqs.new = data->freq_table[state].frequency;
145
146         /* notify cpufreq */
147         cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
148
149         /*
150          * First we write the target state's 'control' value to the
151          * control_register.
152          */
153
154         port = data->acpi_data.control_register.address;
155         bit_width = data->acpi_data.control_register.bit_width;
156         value = (u32) data->acpi_data.states[state].control;
157
158         dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
159
160         ret = acpi_processor_write_port(port, bit_width, value);
161         if (ret) {
162                 dprintk("Invalid port width 0x%04x\n", bit_width);
163                 retval = ret;
164                 goto migrate_end;
165         }
166
167         /*
168          * Assume the write went through when acpi_pstate_strict is not used.
169          * As read status_register is an expensive operation and there 
170          * are no specific error cases where an IO port write will fail.
171          */
172         if (acpi_pstate_strict) {
173                 /* Then we read the 'status_register' and compare the value 
174                  * with the target state's 'status' to make sure the 
175                  * transition was successful.
176                  * Note that we'll poll for up to 1ms (100 cycles of 10us) 
177                  * before giving up.
178                  */
179
180                 port = data->acpi_data.status_register.address;
181                 bit_width = data->acpi_data.status_register.bit_width;
182
183                 dprintk("Looking for 0x%08x from port 0x%04x\n",
184                         (u32) data->acpi_data.states[state].status, port);
185
186                 for (i=0; i<100; i++) {
187                         ret = acpi_processor_read_port(port, bit_width, &value);
188                         if (ret) {      
189                                 dprintk("Invalid port width 0x%04x\n", bit_width);
190                                 retval = ret;
191                                 goto migrate_end;
192                         }
193                         if (value == (u32) data->acpi_data.states[state].status)
194                                 break;
195                         udelay(10);
196                 }
197         } else {
198                 i = 0;
199                 value = (u32) data->acpi_data.states[state].status;
200         }
201
202         /* notify cpufreq */
203         cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
204
205         if (unlikely(value != (u32) data->acpi_data.states[state].status)) {
206                 unsigned int tmp = cpufreq_freqs.new;
207                 cpufreq_freqs.new = cpufreq_freqs.old;
208                 cpufreq_freqs.old = tmp;
209                 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
210                 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
211                 printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
212                 retval = -ENODEV;
213                 goto migrate_end;
214         }
215
216         dprintk("Transition successful after %d microseconds\n", i * 10);
217
218         data->acpi_data.state = state;
219
220         retval = 0;
221 migrate_end:
222         set_cpus_allowed(current, saved_mask);
223         return (retval);
224 }
225
226
227 static int
228 acpi_cpufreq_target (
229         struct cpufreq_policy   *policy,
230         unsigned int target_freq,
231         unsigned int relation)
232 {
233         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
234         unsigned int next_state = 0;
235         unsigned int result = 0;
236
237         dprintk("acpi_cpufreq_setpolicy\n");
238
239         result = cpufreq_frequency_table_target(policy,
240                         data->freq_table,
241                         target_freq,
242                         relation,
243                         &next_state);
244         if (result)
245                 return (result);
246
247         result = acpi_processor_set_performance (data, policy->cpu, next_state);
248
249         return (result);
250 }
251
252
253 static int
254 acpi_cpufreq_verify (
255         struct cpufreq_policy   *policy)
256 {
257         unsigned int result = 0;
258         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
259
260         dprintk("acpi_cpufreq_verify\n");
261
262         result = cpufreq_frequency_table_verify(policy, 
263                         data->freq_table);
264
265         return (result);
266 }
267
268
269 static unsigned long
270 acpi_cpufreq_guess_freq (
271         struct cpufreq_acpi_io  *data,
272         unsigned int            cpu)
273 {
274         if (cpu_khz) {
275                 /* search the closest match to cpu_khz */
276                 unsigned int i;
277                 unsigned long freq;
278                 unsigned long freqn = data->acpi_data.states[0].core_frequency * 1000;
279
280                 for (i=0; i < (data->acpi_data.state_count - 1); i++) {
281                         freq = freqn;
282                         freqn = data->acpi_data.states[i+1].core_frequency * 1000;
283                         if ((2 * cpu_khz) > (freqn + freq)) {
284                                 data->acpi_data.state = i;
285                                 return (freq);
286                         }
287                 }
288                 data->acpi_data.state = data->acpi_data.state_count - 1;
289                 return (freqn);
290         } else
291                 /* assume CPU is at P0... */
292                 data->acpi_data.state = 0;
293                 return data->acpi_data.states[0].core_frequency * 1000;
294         
295 }
296
297
298 /* 
299  * acpi_processor_cpu_init_pdc_est - let BIOS know about the SMP capabilities
300  * of this driver
301  * @perf: processor-specific acpi_io_data struct
302  * @cpu: CPU being initialized
303  *
304  * To avoid issues with legacy OSes, some BIOSes require to be informed of
305  * the SMP capabilities of OS P-state driver. Here we set the bits in _PDC 
306  * accordingly, for Enhanced Speedstep. Actual call to _PDC is done in
307  * driver/acpi/processor.c
308  */
309 static void 
310 acpi_processor_cpu_init_pdc_est(
311                 struct acpi_processor_performance *perf, 
312                 unsigned int cpu,
313                 struct acpi_object_list *obj_list
314                 )
315 {
316         union acpi_object *obj;
317         u32 *buf;
318         struct cpuinfo_x86 *c = cpu_data + cpu;
319         dprintk("acpi_processor_cpu_init_pdc_est\n");
320
321         if (!cpu_has(c, X86_FEATURE_EST))
322                 return;
323
324         /* Initialize pdc. It will be used later. */
325         if (!obj_list)
326                 return;
327                 
328         if (!(obj_list->count && obj_list->pointer))
329                 return;
330
331         obj = obj_list->pointer;
332         if ((obj->buffer.length == 12) && obj->buffer.pointer) {
333                 buf = (u32 *)obj->buffer.pointer;
334                 buf[0] = ACPI_PDC_REVISION_ID;
335                 buf[1] = 1;
336                 buf[2] = ACPI_PDC_EST_CAPABILITY_SMP;
337                 perf->pdc = obj_list;
338         }
339         return;
340 }
341  
342
343 /* CPU specific PDC initialization */
344 static void 
345 acpi_processor_cpu_init_pdc(
346                 struct acpi_processor_performance *perf, 
347                 unsigned int cpu,
348                 struct acpi_object_list *obj_list
349                 )
350 {
351         struct cpuinfo_x86 *c = cpu_data + cpu;
352         dprintk("acpi_processor_cpu_init_pdc\n");
353         perf->pdc = NULL;
354         if (cpu_has(c, X86_FEATURE_EST))
355                 acpi_processor_cpu_init_pdc_est(perf, cpu, obj_list);
356         return;
357 }
358
359
360 static int
361 acpi_cpufreq_cpu_init (
362         struct cpufreq_policy   *policy)
363 {
364         unsigned int            i;
365         unsigned int            cpu = policy->cpu;
366         struct cpufreq_acpi_io  *data;
367         unsigned int            result = 0;
368         struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
369
370         union acpi_object               arg0 = {ACPI_TYPE_BUFFER};
371         u32                             arg0_buf[3];
372         struct acpi_object_list         arg_list = {1, &arg0};
373
374         dprintk("acpi_cpufreq_cpu_init\n");
375         /* setup arg_list for _PDC settings */
376         arg0.buffer.length = 12;
377         arg0.buffer.pointer = (u8 *) arg0_buf;
378
379         data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
380         if (!data)
381                 return (-ENOMEM);
382
383         acpi_io_data[cpu] = data;
384
385         acpi_processor_cpu_init_pdc(&data->acpi_data, cpu, &arg_list);
386         result = acpi_processor_register_performance(&data->acpi_data, cpu);
387         data->acpi_data.pdc = NULL;
388
389         if (result)
390                 goto err_free;
391
392         if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
393                 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
394         }
395
396         /* capability check */
397         if (data->acpi_data.state_count <= 1) {
398                 dprintk("No P-States\n");
399                 result = -ENODEV;
400                 goto err_unreg;
401         }
402         if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
403             (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
404                 dprintk("Unsupported address space [%d, %d]\n",
405                         (u32) (data->acpi_data.control_register.space_id),
406                         (u32) (data->acpi_data.status_register.space_id));
407                 result = -ENODEV;
408                 goto err_unreg;
409         }
410
411         /* alloc freq_table */
412         data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (data->acpi_data.state_count + 1), GFP_KERNEL);
413         if (!data->freq_table) {
414                 result = -ENOMEM;
415                 goto err_unreg;
416         }
417
418         /* detect transition latency */
419         policy->cpuinfo.transition_latency = 0;
420         for (i=0; i<data->acpi_data.state_count; i++) {
421                 if ((data->acpi_data.states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
422                         policy->cpuinfo.transition_latency = data->acpi_data.states[i].transition_latency * 1000;
423         }
424         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
425
426         /* The current speed is unknown and not detectable by ACPI...  */
427         policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
428
429         /* table init */
430         for (i=0; i<=data->acpi_data.state_count; i++)
431         {
432                 data->freq_table[i].index = i;
433                 if (i<data->acpi_data.state_count)
434                         data->freq_table[i].frequency = data->acpi_data.states[i].core_frequency * 1000;
435                 else
436                         data->freq_table[i].frequency = CPUFREQ_TABLE_END;
437         }
438
439         result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
440         if (result) {
441                 goto err_freqfree;
442         }
443
444         /* notify BIOS that we exist */
445         acpi_processor_notify_smm(THIS_MODULE);
446
447         printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
448                cpu);
449         for (i = 0; i < data->acpi_data.state_count; i++)
450                 dprintk("     %cP%d: %d MHz, %d mW, %d uS\n",
451                         (i == data->acpi_data.state?'*':' '), i,
452                         (u32) data->acpi_data.states[i].core_frequency,
453                         (u32) data->acpi_data.states[i].power,
454                         (u32) data->acpi_data.states[i].transition_latency);
455
456         cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
457         
458         /*
459          * the first call to ->target() should result in us actually
460          * writing something to the appropriate registers.
461          */
462         data->resume = 1;
463         
464         return (result);
465
466  err_freqfree:
467         kfree(data->freq_table);
468  err_unreg:
469         acpi_processor_unregister_performance(&data->acpi_data, cpu);
470  err_free:
471         kfree(data);
472         acpi_io_data[cpu] = NULL;
473
474         return (result);
475 }
476
477
478 static int
479 acpi_cpufreq_cpu_exit (
480         struct cpufreq_policy   *policy)
481 {
482         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
483
484
485         dprintk("acpi_cpufreq_cpu_exit\n");
486
487         if (data) {
488                 cpufreq_frequency_table_put_attr(policy->cpu);
489                 acpi_io_data[policy->cpu] = NULL;
490                 acpi_processor_unregister_performance(&data->acpi_data, policy->cpu);
491                 kfree(data);
492         }
493
494         return (0);
495 }
496
497 static int
498 acpi_cpufreq_resume (
499         struct cpufreq_policy   *policy)
500 {
501         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
502
503
504         dprintk("acpi_cpufreq_resume\n");
505
506         data->resume = 1;
507
508         return (0);
509 }
510
511
512 static struct freq_attr* acpi_cpufreq_attr[] = {
513         &cpufreq_freq_attr_scaling_available_freqs,
514         NULL,
515 };
516
517 static struct cpufreq_driver acpi_cpufreq_driver = {
518         .verify         = acpi_cpufreq_verify,
519         .target         = acpi_cpufreq_target,
520         .init           = acpi_cpufreq_cpu_init,
521         .exit           = acpi_cpufreq_cpu_exit,
522         .resume         = acpi_cpufreq_resume,
523         .name           = "acpi-cpufreq",
524         .owner          = THIS_MODULE,
525         .attr           = acpi_cpufreq_attr,
526 };
527
528
529 static int __init
530 acpi_cpufreq_init (void)
531 {
532         int                     result = 0;
533
534         dprintk("acpi_cpufreq_init\n");
535
536         result = cpufreq_register_driver(&acpi_cpufreq_driver);
537         
538         return (result);
539 }
540
541
542 static void __exit
543 acpi_cpufreq_exit (void)
544 {
545         dprintk("acpi_cpufreq_exit\n");
546
547         cpufreq_unregister_driver(&acpi_cpufreq_driver);
548
549         return;
550 }
551
552 module_param(acpi_pstate_strict, uint, 0644);
553 MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
554
555 late_initcall(acpi_cpufreq_init);
556 module_exit(acpi_cpufreq_exit);
557
558 MODULE_ALIAS("acpi");