2 * This file provides the ACPI based P-state support. This
3 * module works with generic cpufreq infrastructure. Most of
4 * the code is based on i386 version
5 * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
7 * Copyright (C) 2005 Intel Corp
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
11 #include <linux/kernel.h>
12 #include <linux/slab.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 MODULE_AUTHOR("Venkatesh Pallipadi");
26 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
27 MODULE_LICENSE("GPL");
30 struct cpufreq_acpi_io
{
31 struct acpi_processor_performance acpi_data
;
32 struct cpufreq_frequency_table
*freq_table
;
36 static struct cpufreq_acpi_io
*acpi_io_data
[NR_CPUS
];
38 static struct cpufreq_driver acpi_cpufreq_driver
;
42 processor_set_pstate (
47 pr_debug("processor_set_pstate\n");
49 retval
= ia64_pal_set_pstate((u64
)value
);
52 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
61 processor_get_pstate (
67 pr_debug("processor_get_pstate\n");
69 retval
= ia64_pal_get_pstate(&pstate_index
,
70 PAL_GET_PSTATE_TYPE_INSTANT
);
71 *value
= (u32
) pstate_index
;
74 pr_debug("Failed to get current freq with "
75 "error 0x%lx, idx 0x%x\n", retval
, *value
);
81 /* To be used only after data->acpi_data is initialized */
84 struct cpufreq_acpi_io
*data
,
90 pr_debug("extract_clock\n");
92 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++) {
93 if (value
== data
->acpi_data
.states
[i
].status
)
94 return data
->acpi_data
.states
[i
].core_frequency
;
96 return data
->acpi_data
.states
[i
-1].core_frequency
;
102 struct cpufreq_acpi_io
*data
,
107 cpumask_t saved_mask
;
108 unsigned long clock_freq
;
110 pr_debug("processor_get_freq\n");
112 saved_mask
= current
->cpus_allowed
;
113 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
114 if (smp_processor_id() != cpu
)
117 /* processor_get_pstate gets the instantaneous frequency */
118 ret
= processor_get_pstate(&value
);
121 set_cpus_allowed_ptr(current
, &saved_mask
);
122 printk(KERN_WARNING
"get performance failed with error %d\n",
127 clock_freq
= extract_clock(data
, value
, cpu
);
128 ret
= (clock_freq
*1000);
131 set_cpus_allowed_ptr(current
, &saved_mask
);
138 struct cpufreq_acpi_io
*data
,
139 struct cpufreq_policy
*policy
,
144 cpumask_t saved_mask
;
147 pr_debug("processor_set_freq\n");
149 saved_mask
= current
->cpus_allowed
;
150 set_cpus_allowed_ptr(current
, cpumask_of(policy
->cpu
));
151 if (smp_processor_id() != policy
->cpu
) {
156 if (state
== data
->acpi_data
.state
) {
157 if (unlikely(data
->resume
)) {
158 pr_debug("Called after resume, resetting to P%d\n", state
);
161 pr_debug("Already at target state (P%d)\n", state
);
167 pr_debug("Transitioning from P%d to P%d\n",
168 data
->acpi_data
.state
, state
);
171 * First we write the target state's 'control' value to the
175 value
= (u32
) data
->acpi_data
.states
[state
].control
;
177 pr_debug("Transitioning to state: 0x%08x\n", value
);
179 ret
= processor_set_pstate(value
);
181 printk(KERN_WARNING
"Transition failed with error %d\n", ret
);
186 data
->acpi_data
.state
= state
;
191 set_cpus_allowed_ptr(current
, &saved_mask
);
200 struct cpufreq_acpi_io
*data
= acpi_io_data
[cpu
];
202 pr_debug("acpi_cpufreq_get\n");
204 return processor_get_freq(data
, cpu
);
209 acpi_cpufreq_target (
210 struct cpufreq_policy
*policy
,
213 return processor_set_freq(acpi_io_data
[policy
->cpu
], policy
, index
);
217 acpi_cpufreq_cpu_init (
218 struct cpufreq_policy
*policy
)
221 unsigned int cpu
= policy
->cpu
;
222 struct cpufreq_acpi_io
*data
;
223 unsigned int result
= 0;
225 pr_debug("acpi_cpufreq_cpu_init\n");
227 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
231 acpi_io_data
[cpu
] = data
;
233 result
= acpi_processor_register_performance(&data
->acpi_data
, cpu
);
238 /* capability check */
239 if (data
->acpi_data
.state_count
<= 1) {
240 pr_debug("No P-States\n");
245 if ((data
->acpi_data
.control_register
.space_id
!=
246 ACPI_ADR_SPACE_FIXED_HARDWARE
) ||
247 (data
->acpi_data
.status_register
.space_id
!=
248 ACPI_ADR_SPACE_FIXED_HARDWARE
)) {
249 pr_debug("Unsupported address space [%d, %d]\n",
250 (u32
) (data
->acpi_data
.control_register
.space_id
),
251 (u32
) (data
->acpi_data
.status_register
.space_id
));
256 /* alloc freq_table */
257 data
->freq_table
= kmalloc(sizeof(*data
->freq_table
) *
258 (data
->acpi_data
.state_count
+ 1),
260 if (!data
->freq_table
) {
265 /* detect transition latency */
266 policy
->cpuinfo
.transition_latency
= 0;
267 for (i
=0; i
<data
->acpi_data
.state_count
; i
++) {
268 if ((data
->acpi_data
.states
[i
].transition_latency
* 1000) >
269 policy
->cpuinfo
.transition_latency
) {
270 policy
->cpuinfo
.transition_latency
=
271 data
->acpi_data
.states
[i
].transition_latency
* 1000;
276 for (i
= 0; i
<= data
->acpi_data
.state_count
; i
++)
278 data
->freq_table
[i
].driver_data
= i
;
279 if (i
< data
->acpi_data
.state_count
) {
280 data
->freq_table
[i
].frequency
=
281 data
->acpi_data
.states
[i
].core_frequency
* 1000;
283 data
->freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
287 result
= cpufreq_table_validate_and_show(policy
, data
->freq_table
);
292 /* notify BIOS that we exist */
293 acpi_processor_notify_smm(THIS_MODULE
);
295 printk(KERN_INFO
"acpi-cpufreq: CPU%u - ACPI performance management "
296 "activated.\n", cpu
);
298 for (i
= 0; i
< data
->acpi_data
.state_count
; i
++)
299 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
300 (i
== data
->acpi_data
.state
?'*':' '), i
,
301 (u32
) data
->acpi_data
.states
[i
].core_frequency
,
302 (u32
) data
->acpi_data
.states
[i
].power
,
303 (u32
) data
->acpi_data
.states
[i
].transition_latency
,
304 (u32
) data
->acpi_data
.states
[i
].bus_master_latency
,
305 (u32
) data
->acpi_data
.states
[i
].status
,
306 (u32
) data
->acpi_data
.states
[i
].control
);
308 /* the first call to ->target() should result in us actually
309 * writing something to the appropriate registers. */
315 kfree(data
->freq_table
);
317 acpi_processor_unregister_performance(&data
->acpi_data
, cpu
);
320 acpi_io_data
[cpu
] = NULL
;
327 acpi_cpufreq_cpu_exit (
328 struct cpufreq_policy
*policy
)
330 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
332 pr_debug("acpi_cpufreq_cpu_exit\n");
335 cpufreq_frequency_table_put_attr(policy
->cpu
);
336 acpi_io_data
[policy
->cpu
] = NULL
;
337 acpi_processor_unregister_performance(&data
->acpi_data
,
346 static struct cpufreq_driver acpi_cpufreq_driver
= {
347 .verify
= cpufreq_generic_frequency_table_verify
,
348 .target_index
= acpi_cpufreq_target
,
349 .get
= acpi_cpufreq_get
,
350 .init
= acpi_cpufreq_cpu_init
,
351 .exit
= acpi_cpufreq_cpu_exit
,
352 .name
= "acpi-cpufreq",
353 .attr
= cpufreq_generic_attr
,
358 acpi_cpufreq_init (void)
360 pr_debug("acpi_cpufreq_init\n");
362 return cpufreq_register_driver(&acpi_cpufreq_driver
);
367 acpi_cpufreq_exit (void)
369 pr_debug("acpi_cpufreq_exit\n");
371 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
376 late_initcall(acpi_cpufreq_init
);
377 module_exit(acpi_cpufreq_exit
);