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/config.h>
13 #include <linux/kernel.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 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
28 MODULE_AUTHOR("Venkatesh Pallipadi");
29 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
30 MODULE_LICENSE("GPL");
33 struct cpufreq_acpi_io {
34 struct acpi_processor_performance acpi_data;
35 struct cpufreq_frequency_table *freq_table;
39 static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
41 static struct cpufreq_driver acpi_cpufreq_driver;
45 processor_set_pstate (
50 dprintk("processor_set_pstate\n");
52 retval = ia64_pal_set_pstate((u64)value);
55 dprintk("Failed to set freq to 0x%x, with error 0x%x\n",
64 processor_get_pstate (
70 dprintk("processor_get_pstate\n");
72 retval = ia64_pal_get_pstate(&pstate_index);
73 *value = (u32) pstate_index;
76 dprintk("Failed to get current freq with "
77 "error 0x%x, idx 0x%x\n", retval, *value);
83 /* To be used only after data->acpi_data is initialized */
86 struct cpufreq_acpi_io *data,
92 dprintk("extract_clock\n");
94 for (i = 0; i < data->acpi_data.state_count; i++) {
95 if (value >= data->acpi_data.states[i].control)
96 return data->acpi_data.states[i].core_frequency;
98 return data->acpi_data.states[i-1].core_frequency;
104 struct cpufreq_acpi_io *data,
109 cpumask_t saved_mask;
110 unsigned long clock_freq;
112 dprintk("processor_get_freq\n");
114 saved_mask = current->cpus_allowed;
115 set_cpus_allowed(current, cpumask_of_cpu(cpu));
116 if (smp_processor_id() != cpu) {
122 * processor_get_pstate gets the average frequency since the
123 * last get. So, do two PAL_get_freq()...
125 ret = processor_get_pstate(&value);
126 ret = processor_get_pstate(&value);
129 set_cpus_allowed(current, saved_mask);
130 printk(KERN_WARNING "get performance failed with error %d\n",
135 clock_freq = extract_clock(data, value, cpu);
136 ret = (clock_freq*1000);
139 set_cpus_allowed(current, saved_mask);
146 struct cpufreq_acpi_io *data,
152 struct cpufreq_freqs cpufreq_freqs;
153 cpumask_t saved_mask;
156 dprintk("processor_set_freq\n");
158 saved_mask = current->cpus_allowed;
159 set_cpus_allowed(current, cpumask_of_cpu(cpu));
160 if (smp_processor_id() != cpu) {
165 if (state == data->acpi_data.state) {
166 if (unlikely(data->resume)) {
167 dprintk("Called after resume, resetting to P%d\n", state);
170 dprintk("Already at target state (P%d)\n", state);
176 dprintk("Transitioning from P%d to P%d\n",
177 data->acpi_data.state, state);
179 /* cpufreq frequency struct */
180 cpufreq_freqs.cpu = cpu;
181 cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
182 cpufreq_freqs.new = data->freq_table[state].frequency;
185 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
188 * First we write the target state's 'control' value to the
192 value = (u32) data->acpi_data.states[state].control;
194 dprintk("Transitioning to state: 0x%08x\n", value);
196 ret = processor_set_pstate(value);
198 unsigned int tmp = cpufreq_freqs.new;
199 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
200 cpufreq_freqs.new = cpufreq_freqs.old;
201 cpufreq_freqs.old = tmp;
202 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
203 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
204 printk(KERN_WARNING "Transition failed with error %d\n", ret);
209 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
211 data->acpi_data.state = state;
216 set_cpus_allowed(current, saved_mask);
225 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
227 dprintk("acpi_cpufreq_get\n");
229 return processor_get_freq(data, cpu);
234 acpi_cpufreq_target (
235 struct cpufreq_policy *policy,
236 unsigned int target_freq,
237 unsigned int relation)
239 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
240 unsigned int next_state = 0;
241 unsigned int result = 0;
243 dprintk("acpi_cpufreq_setpolicy\n");
245 result = cpufreq_frequency_table_target(policy,
246 data->freq_table, target_freq, relation, &next_state);
250 result = processor_set_freq(data, policy->cpu, next_state);
257 acpi_cpufreq_verify (
258 struct cpufreq_policy *policy)
260 unsigned int result = 0;
261 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
263 dprintk("acpi_cpufreq_verify\n");
265 result = cpufreq_frequency_table_verify(policy,
273 acpi_cpufreq_cpu_init (
274 struct cpufreq_policy *policy)
277 unsigned int cpu = policy->cpu;
278 struct cpufreq_acpi_io *data;
279 unsigned int result = 0;
281 dprintk("acpi_cpufreq_cpu_init\n");
283 data = kmalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
287 memset(data, 0, sizeof(struct cpufreq_acpi_io));
289 acpi_io_data[cpu] = data;
291 result = acpi_processor_register_performance(&data->acpi_data, cpu);
296 /* capability check */
297 if (data->acpi_data.state_count <= 1) {
298 dprintk("No P-States\n");
303 if ((data->acpi_data.control_register.space_id !=
304 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
305 (data->acpi_data.status_register.space_id !=
306 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
307 dprintk("Unsupported address space [%d, %d]\n",
308 (u32) (data->acpi_data.control_register.space_id),
309 (u32) (data->acpi_data.status_register.space_id));
314 /* alloc freq_table */
315 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
316 (data->acpi_data.state_count + 1),
318 if (!data->freq_table) {
323 /* detect transition latency */
324 policy->cpuinfo.transition_latency = 0;
325 for (i=0; i<data->acpi_data.state_count; i++) {
326 if ((data->acpi_data.states[i].transition_latency * 1000) >
327 policy->cpuinfo.transition_latency) {
328 policy->cpuinfo.transition_latency =
329 data->acpi_data.states[i].transition_latency * 1000;
332 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
334 policy->cur = processor_get_freq(data, policy->cpu);
337 for (i = 0; i <= data->acpi_data.state_count; i++)
339 data->freq_table[i].index = i;
340 if (i < data->acpi_data.state_count) {
341 data->freq_table[i].frequency =
342 data->acpi_data.states[i].core_frequency * 1000;
344 data->freq_table[i].frequency = CPUFREQ_TABLE_END;
348 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
353 /* notify BIOS that we exist */
354 acpi_processor_notify_smm(THIS_MODULE);
356 printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management "
357 "activated.\n", cpu);
359 for (i = 0; i < data->acpi_data.state_count; i++)
360 dprintk(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
361 (i == data->acpi_data.state?'*':' '), i,
362 (u32) data->acpi_data.states[i].core_frequency,
363 (u32) data->acpi_data.states[i].power,
364 (u32) data->acpi_data.states[i].transition_latency,
365 (u32) data->acpi_data.states[i].bus_master_latency,
366 (u32) data->acpi_data.states[i].status,
367 (u32) data->acpi_data.states[i].control);
369 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
371 /* the first call to ->target() should result in us actually
372 * writing something to the appropriate registers. */
378 kfree(data->freq_table);
380 acpi_processor_unregister_performance(&data->acpi_data, cpu);
383 acpi_io_data[cpu] = NULL;
390 acpi_cpufreq_cpu_exit (
391 struct cpufreq_policy *policy)
393 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
395 dprintk("acpi_cpufreq_cpu_exit\n");
398 cpufreq_frequency_table_put_attr(policy->cpu);
399 acpi_io_data[policy->cpu] = NULL;
400 acpi_processor_unregister_performance(&data->acpi_data,
409 static struct freq_attr* acpi_cpufreq_attr[] = {
410 &cpufreq_freq_attr_scaling_available_freqs,
415 static struct cpufreq_driver acpi_cpufreq_driver = {
416 .verify = acpi_cpufreq_verify,
417 .target = acpi_cpufreq_target,
418 .get = acpi_cpufreq_get,
419 .init = acpi_cpufreq_cpu_init,
420 .exit = acpi_cpufreq_cpu_exit,
421 .name = "acpi-cpufreq",
422 .owner = THIS_MODULE,
423 .attr = acpi_cpufreq_attr,
428 acpi_cpufreq_init (void)
430 dprintk("acpi_cpufreq_init\n");
432 return cpufreq_register_driver(&acpi_cpufreq_driver);
437 acpi_cpufreq_exit (void)
439 dprintk("acpi_cpufreq_exit\n");
441 cpufreq_unregister_driver(&acpi_cpufreq_driver);
446 late_initcall(acpi_cpufreq_init);
447 module_exit(acpi_cpufreq_exit);