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/slab.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/cpufreq.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
20 #include <asm/uaccess.h>
23 #include <linux/acpi.h>
24 #include <acpi/processor.h>
26 MODULE_AUTHOR("Venkatesh Pallipadi");
27 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
28 MODULE_LICENSE("GPL");
31 struct cpufreq_acpi_io
{
32 struct acpi_processor_performance acpi_data
;
33 struct cpufreq_frequency_table
*freq_table
;
37 static struct cpufreq_acpi_io
*acpi_io_data
[NR_CPUS
];
39 static struct cpufreq_driver acpi_cpufreq_driver
;
43 processor_set_pstate (
48 pr_debug("processor_set_pstate\n");
50 retval
= ia64_pal_set_pstate((u64
)value
);
53 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
62 processor_get_pstate (
68 pr_debug("processor_get_pstate\n");
70 retval
= ia64_pal_get_pstate(&pstate_index
,
71 PAL_GET_PSTATE_TYPE_INSTANT
);
72 *value
= (u32
) pstate_index
;
75 pr_debug("Failed to get current freq with "
76 "error 0x%lx, idx 0x%x\n", retval
, *value
);
82 /* To be used only after data->acpi_data is initialized */
85 struct cpufreq_acpi_io
*data
,
91 pr_debug("extract_clock\n");
93 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
94 if (value
== data
->acpi_data
.states
[i
].status
)
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 pr_debug("processor_get_freq\n");
113 saved_mask
= current
->cpus_allowed
;
114 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
115 if (smp_processor_id() != cpu
)
118 /* processor_get_pstate gets the instantaneous frequency */
119 ret
= processor_get_pstate(&value
);
122 set_cpus_allowed_ptr(current
, &saved_mask
);
123 printk(KERN_WARNING
"get performance failed with error %d\n",
128 clock_freq
= extract_clock(data
, value
, cpu
);
129 ret
= (clock_freq
*1000);
132 set_cpus_allowed_ptr(current
, &saved_mask
);
139 struct cpufreq_acpi_io
*data
,
145 struct cpufreq_freqs cpufreq_freqs
;
146 cpumask_t saved_mask
;
149 pr_debug("processor_set_freq\n");
151 saved_mask
= current
->cpus_allowed
;
152 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
153 if (smp_processor_id() != cpu
) {
158 if (state
== data
->acpi_data
.state
) {
159 if (unlikely(data
->resume
)) {
160 pr_debug("Called after resume, resetting to P%d\n", state
);
163 pr_debug("Already at target state (P%d)\n", state
);
169 pr_debug("Transitioning from P%d to P%d\n",
170 data
->acpi_data
.state
, state
);
172 /* cpufreq frequency struct */
173 cpufreq_freqs
.cpu
= cpu
;
174 cpufreq_freqs
.old
= data
->freq_table
[data
->acpi_data
.state
].frequency
;
175 cpufreq_freqs
.new = data
->freq_table
[state
].frequency
;
178 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_PRECHANGE
);
181 * First we write the target state's 'control' value to the
185 value
= (u32
) data
->acpi_data
.states
[state
].control
;
187 pr_debug("Transitioning to state: 0x%08x\n", value
);
189 ret
= processor_set_pstate(value
);
191 unsigned int tmp
= cpufreq_freqs
.new;
192 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
193 cpufreq_freqs
.new = cpufreq_freqs
.old
;
194 cpufreq_freqs
.old
= tmp
;
195 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_PRECHANGE
);
196 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
197 printk(KERN_WARNING
"Transition failed with error %d\n", ret
);
202 cpufreq_notify_transition(&cpufreq_freqs
, CPUFREQ_POSTCHANGE
);
204 data
->acpi_data
.state
= state
;
209 set_cpus_allowed_ptr(current
, &saved_mask
);
218 struct cpufreq_acpi_io
*data
= acpi_io_data
[cpu
];
220 pr_debug("acpi_cpufreq_get\n");
222 return processor_get_freq(data
, cpu
);
227 acpi_cpufreq_target (
228 struct cpufreq_policy
*policy
,
229 unsigned int target_freq
,
230 unsigned int relation
)
232 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
233 unsigned int next_state
= 0;
234 unsigned int result
= 0;
236 pr_debug("acpi_cpufreq_setpolicy\n");
238 result
= cpufreq_frequency_table_target(policy
,
239 data
->freq_table
, target_freq
, relation
, &next_state
);
243 result
= processor_set_freq(data
, policy
->cpu
, next_state
);
250 acpi_cpufreq_verify (
251 struct cpufreq_policy
*policy
)
253 unsigned int result
= 0;
254 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
256 pr_debug("acpi_cpufreq_verify\n");
258 result
= cpufreq_frequency_table_verify(policy
,
266 acpi_cpufreq_cpu_init (
267 struct cpufreq_policy
*policy
)
270 unsigned int cpu
= policy
->cpu
;
271 struct cpufreq_acpi_io
*data
;
272 unsigned int result
= 0;
274 pr_debug("acpi_cpufreq_cpu_init\n");
276 data
= kzalloc(sizeof(struct cpufreq_acpi_io
), GFP_KERNEL
);
280 acpi_io_data
[cpu
] = data
;
282 result
= acpi_processor_register_performance(&data
->acpi_data
, cpu
);
287 /* capability check */
288 if (data
->acpi_data
.state_count
<= 1) {
289 pr_debug("No P-States\n");
294 if ((data
->acpi_data
.control_register
.space_id
!=
295 ACPI_ADR_SPACE_FIXED_HARDWARE
) ||
296 (data
->acpi_data
.status_register
.space_id
!=
297 ACPI_ADR_SPACE_FIXED_HARDWARE
)) {
298 pr_debug("Unsupported address space [%d, %d]\n",
299 (u32
) (data
->acpi_data
.control_register
.space_id
),
300 (u32
) (data
->acpi_data
.status_register
.space_id
));
305 /* alloc freq_table */
306 data
->freq_table
= kmalloc(sizeof(struct cpufreq_frequency_table
) *
307 (data
->acpi_data
.state_count
+ 1),
309 if (!data
->freq_table
) {
314 /* detect transition latency */
315 policy
->cpuinfo
.transition_latency
= 0;
316 for (i
=0; i
<data
->acpi_data
.state_count
; i
++) {
317 if ((data
->acpi_data
.states
[i
].transition_latency
* 1000) >
318 policy
->cpuinfo
.transition_latency
) {
319 policy
->cpuinfo
.transition_latency
=
320 data
->acpi_data
.states
[i
].transition_latency
* 1000;
323 policy
->cur
= processor_get_freq(data
, policy
->cpu
);
326 for (i
= 0; i
<= data
->acpi_data
.state_count
; i
++)
328 data
->freq_table
[i
].index
= i
;
329 if (i
< data
->acpi_data
.state_count
) {
330 data
->freq_table
[i
].frequency
=
331 data
->acpi_data
.states
[i
].core_frequency
* 1000;
333 data
->freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
337 result
= cpufreq_frequency_table_cpuinfo(policy
, data
->freq_table
);
342 /* notify BIOS that we exist */
343 acpi_processor_notify_smm(THIS_MODULE
);
345 printk(KERN_INFO
"acpi-cpufreq: CPU%u - ACPI performance management "
346 "activated.\n", cpu
);
348 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++)
349 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
350 (i
== data
->acpi_data
.state
?'*':' '), i
,
351 (u32
) data
->acpi_data
.states
[i
].core_frequency
,
352 (u32
) data
->acpi_data
.states
[i
].power
,
353 (u32
) data
->acpi_data
.states
[i
].transition_latency
,
354 (u32
) data
->acpi_data
.states
[i
].bus_master_latency
,
355 (u32
) data
->acpi_data
.states
[i
].status
,
356 (u32
) data
->acpi_data
.states
[i
].control
);
358 cpufreq_frequency_table_get_attr(data
->freq_table
, policy
->cpu
);
360 /* the first call to ->target() should result in us actually
361 * writing something to the appropriate registers. */
367 kfree(data
->freq_table
);
369 acpi_processor_unregister_performance(&data
->acpi_data
, cpu
);
372 acpi_io_data
[cpu
] = NULL
;
379 acpi_cpufreq_cpu_exit (
380 struct cpufreq_policy
*policy
)
382 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
384 pr_debug("acpi_cpufreq_cpu_exit\n");
387 cpufreq_frequency_table_put_attr(policy
->cpu
);
388 acpi_io_data
[policy
->cpu
] = NULL
;
389 acpi_processor_unregister_performance(&data
->acpi_data
,
398 static struct freq_attr
* acpi_cpufreq_attr
[] = {
399 &cpufreq_freq_attr_scaling_available_freqs
,
404 static struct cpufreq_driver acpi_cpufreq_driver
= {
405 .verify
= acpi_cpufreq_verify
,
406 .target
= acpi_cpufreq_target
,
407 .get
= acpi_cpufreq_get
,
408 .init
= acpi_cpufreq_cpu_init
,
409 .exit
= acpi_cpufreq_cpu_exit
,
410 .name
= "acpi-cpufreq",
411 .owner
= THIS_MODULE
,
412 .attr
= acpi_cpufreq_attr
,
417 acpi_cpufreq_init (void)
419 pr_debug("acpi_cpufreq_init\n");
421 return cpufreq_register_driver(&acpi_cpufreq_driver
);
426 acpi_cpufreq_exit (void)
428 pr_debug("acpi_cpufreq_exit\n");
430 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
435 late_initcall(acpi_cpufreq_init
);
436 module_exit(acpi_cpufreq_exit
);