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