2 * arch/ia64/kernel/cpufreq/acpi-cpufreq.c
3 * This file provides the ACPI based P-state support. This
4 * module works with generic cpufreq infrastructure. Most of
5 * the code is based on i386 version
6 * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
8 * Copyright (C) 2005 Intel Corp
9 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
19 #include <asm/uaccess.h>
22 #include <linux/acpi.h>
23 #include <acpi/processor.h>
25 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
27 MODULE_AUTHOR("Venkatesh Pallipadi");
28 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
29 MODULE_LICENSE("GPL");
32 struct cpufreq_acpi_io
{
33 struct acpi_processor_performance acpi_data
;
34 struct cpufreq_frequency_table
*freq_table
;
38 static struct cpufreq_acpi_io
*acpi_io_data
[NR_CPUS
];
40 static struct cpufreq_driver acpi_cpufreq_driver
;
44 processor_set_pstate (
49 dprintk("processor_set_pstate\n");
51 retval
= ia64_pal_set_pstate((u64
)value
);
54 dprintk("Failed to set freq to 0x%x, with error 0x%x\n",
63 processor_get_pstate (
69 dprintk("processor_get_pstate\n");
71 retval
= ia64_pal_get_pstate(&pstate_index
);
72 *value
= (u32
) pstate_index
;
75 dprintk("Failed to get current freq with "
76 "error 0x%x, idx 0x%x\n", retval
, *value
);
82 /* To be used only after data->acpi_data is initialized */
85 struct cpufreq_acpi_io
*data
,
91 dprintk("extract_clock\n");
93 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
94 if (value
>= data
->acpi_data
.states
[i
].control
)
95 return data
->acpi_data
.states
[i
].core_frequency
;
97 return data
->acpi_data
.states
[i
-1].core_frequency
;
103 struct cpufreq_acpi_io
*data
,
108 cpumask_t saved_mask
;
109 unsigned long clock_freq
;
111 dprintk("processor_get_freq\n");
113 saved_mask
= current
->cpus_allowed
;
114 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
115 if (smp_processor_id() != cpu
) {
121 * processor_get_pstate gets the average frequency since the
122 * last get. So, do two PAL_get_freq()...
124 ret
= processor_get_pstate(&value
);
125 ret
= processor_get_pstate(&value
);
128 set_cpus_allowed(current
, saved_mask
);
129 printk(KERN_WARNING
"get performance failed with error %d\n",
134 clock_freq
= extract_clock(data
, value
, cpu
);
135 ret
= (clock_freq
*1000);
138 set_cpus_allowed(current
, saved_mask
);
145 struct cpufreq_acpi_io
*data
,
151 struct cpufreq_freqs cpufreq_freqs
;
152 cpumask_t saved_mask
;
155 dprintk("processor_set_freq\n");
157 saved_mask
= current
->cpus_allowed
;
158 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
159 if (smp_processor_id() != cpu
) {
164 if (state
== data
->acpi_data
.state
) {
165 if (unlikely(data
->resume
)) {
166 dprintk("Called after resume, resetting to P%d\n", state
);
169 dprintk("Already at target state (P%d)\n", state
);
175 dprintk("Transitioning from P%d to P%d\n",
176 data
->acpi_data
.state
, state
);
178 /* cpufreq frequency struct */
179 cpufreq_freqs
.cpu
= cpu
;
180 cpufreq_freqs
.old
= data
->freq_table
[data
->acpi_data
.state
].frequency
;
181 cpufreq_freqs
.new = data
->freq_table
[state
].frequency
;
184 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_PRECHANGE
);
187 * First we write the target state's 'control' value to the
191 value
= (u32
) data
->acpi_data
.states
[state
].control
;
193 dprintk("Transitioning to state: 0x%08x\n", value
);
195 ret
= processor_set_pstate(value
);
197 unsigned int tmp
= cpufreq_freqs
.new;
198 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
199 cpufreq_freqs
.new = cpufreq_freqs
.old
;
200 cpufreq_freqs
.old
= tmp
;
201 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_PRECHANGE
);
202 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
203 printk(KERN_WARNING
"Transition failed with error %d\n", ret
);
208 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
210 data
->acpi_data
.state
= state
;
215 set_cpus_allowed(current
, saved_mask
);
224 struct cpufreq_acpi_io
*data
= acpi_io_data
[cpu
];
226 dprintk("acpi_cpufreq_get\n");
228 return processor_get_freq(data
, cpu
);
233 acpi_cpufreq_target (
234 struct cpufreq_policy
*policy
,
235 unsigned int target_freq
,
236 unsigned int relation
)
238 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
239 unsigned int next_state
= 0;
240 unsigned int result
= 0;
242 dprintk("acpi_cpufreq_setpolicy\n");
244 result
= cpufreq_frequency_table_target(policy
,
245 data
->freq_table
, target_freq
, relation
, &next_state
);
249 result
= processor_set_freq(data
, policy
->cpu
, next_state
);
256 acpi_cpufreq_verify (
257 struct cpufreq_policy
*policy
)
259 unsigned int result
= 0;
260 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
262 dprintk("acpi_cpufreq_verify\n");
264 result
= cpufreq_frequency_table_verify(policy
,
272 acpi_cpufreq_cpu_init (
273 struct cpufreq_policy
*policy
)
276 unsigned int cpu
= policy
->cpu
;
277 struct cpufreq_acpi_io
*data
;
278 unsigned int result
= 0;
280 dprintk("acpi_cpufreq_cpu_init\n");
282 data
= kmalloc(sizeof(struct cpufreq_acpi_io
), GFP_KERNEL
);
286 memset(data
, 0, sizeof(struct cpufreq_acpi_io
));
288 acpi_io_data
[cpu
] = data
;
290 result
= acpi_processor_register_performance(&data
->acpi_data
, cpu
);
295 /* capability check */
296 if (data
->acpi_data
.state_count
<= 1) {
297 dprintk("No P-States\n");
302 if ((data
->acpi_data
.control_register
.space_id
!=
303 ACPI_ADR_SPACE_FIXED_HARDWARE
) ||
304 (data
->acpi_data
.status_register
.space_id
!=
305 ACPI_ADR_SPACE_FIXED_HARDWARE
)) {
306 dprintk("Unsupported address space [%d, %d]\n",
307 (u32
) (data
->acpi_data
.control_register
.space_id
),
308 (u32
) (data
->acpi_data
.status_register
.space_id
));
313 /* alloc freq_table */
314 data
->freq_table
= kmalloc(sizeof(struct cpufreq_frequency_table
) *
315 (data
->acpi_data
.state_count
+ 1),
317 if (!data
->freq_table
) {
322 /* detect transition latency */
323 policy
->cpuinfo
.transition_latency
= 0;
324 for (i
=0; i
<data
->acpi_data
.state_count
; i
++) {
325 if ((data
->acpi_data
.states
[i
].transition_latency
* 1000) >
326 policy
->cpuinfo
.transition_latency
) {
327 policy
->cpuinfo
.transition_latency
=
328 data
->acpi_data
.states
[i
].transition_latency
* 1000;
331 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
333 policy
->cur
= processor_get_freq(data
, policy
->cpu
);
336 for (i
= 0; i
<= data
->acpi_data
.state_count
; i
++)
338 data
->freq_table
[i
].index
= i
;
339 if (i
< data
->acpi_data
.state_count
) {
340 data
->freq_table
[i
].frequency
=
341 data
->acpi_data
.states
[i
].core_frequency
* 1000;
343 data
->freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
347 result
= cpufreq_frequency_table_cpuinfo(policy
, data
->freq_table
);
352 /* notify BIOS that we exist */
353 acpi_processor_notify_smm(THIS_MODULE
);
355 printk(KERN_INFO
"acpi-cpufreq: CPU%u - ACPI performance management "
356 "activated.\n", cpu
);
358 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++)
359 dprintk(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
360 (i
== data
->acpi_data
.state
?'*':' '), i
,
361 (u32
) data
->acpi_data
.states
[i
].core_frequency
,
362 (u32
) data
->acpi_data
.states
[i
].power
,
363 (u32
) data
->acpi_data
.states
[i
].transition_latency
,
364 (u32
) data
->acpi_data
.states
[i
].bus_master_latency
,
365 (u32
) data
->acpi_data
.states
[i
].status
,
366 (u32
) data
->acpi_data
.states
[i
].control
);
368 cpufreq_frequency_table_get_attr(data
->freq_table
, policy
->cpu
);
370 /* the first call to ->target() should result in us actually
371 * writing something to the appropriate registers. */
377 kfree(data
->freq_table
);
379 acpi_processor_unregister_performance(&data
->acpi_data
, cpu
);
382 acpi_io_data
[cpu
] = NULL
;
389 acpi_cpufreq_cpu_exit (
390 struct cpufreq_policy
*policy
)
392 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
394 dprintk("acpi_cpufreq_cpu_exit\n");
397 cpufreq_frequency_table_put_attr(policy
->cpu
);
398 acpi_io_data
[policy
->cpu
] = NULL
;
399 acpi_processor_unregister_performance(&data
->acpi_data
,
408 static struct freq_attr
* acpi_cpufreq_attr
[] = {
409 &cpufreq_freq_attr_scaling_available_freqs
,
414 static struct cpufreq_driver acpi_cpufreq_driver
= {
415 .verify
= acpi_cpufreq_verify
,
416 .target
= acpi_cpufreq_target
,
417 .get
= acpi_cpufreq_get
,
418 .init
= acpi_cpufreq_cpu_init
,
419 .exit
= acpi_cpufreq_cpu_exit
,
420 .name
= "acpi-cpufreq",
421 .owner
= THIS_MODULE
,
422 .attr
= acpi_cpufreq_attr
,
427 acpi_cpufreq_init (void)
429 dprintk("acpi_cpufreq_init\n");
431 return cpufreq_register_driver(&acpi_cpufreq_driver
);
436 acpi_cpufreq_exit (void)
438 dprintk("acpi_cpufreq_exit\n");
440 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
445 late_initcall(acpi_cpufreq_init
);
446 module_exit(acpi_cpufreq_exit
);