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