Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[pv_ops_mirror.git] / arch / ia64 / kernel / cpufreq / acpi-cpufreq.c
blobb8498ea6206800b613f753cc75db7c84e71b9e93
1 /*
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>
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20 #include <asm/pal.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;
35 unsigned int resume;
38 static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
40 static struct cpufreq_driver acpi_cpufreq_driver;
43 static int
44 processor_set_pstate (
45 u32 value)
47 s64 retval;
49 dprintk("processor_set_pstate\n");
51 retval = ia64_pal_set_pstate((u64)value);
53 if (retval) {
54 dprintk("Failed to set freq to 0x%x, with error 0x%x\n",
55 value, retval);
56 return -ENODEV;
58 return (int)retval;
62 static int
63 processor_get_pstate (
64 u32 *value)
66 u64 pstate_index = 0;
67 s64 retval;
69 dprintk("processor_get_pstate\n");
71 retval = ia64_pal_get_pstate(&pstate_index,
72 PAL_GET_PSTATE_TYPE_INSTANT);
73 *value = (u32) pstate_index;
75 if (retval)
76 dprintk("Failed to get current freq with "
77 "error 0x%x, idx 0x%x\n", retval, *value);
79 return (int)retval;
83 /* To be used only after data->acpi_data is initialized */
84 static unsigned
85 extract_clock (
86 struct cpufreq_acpi_io *data,
87 unsigned value,
88 unsigned int cpu)
90 unsigned long i;
92 dprintk("extract_clock\n");
94 for (i = 0; i < data->acpi_data.state_count; i++) {
95 if (value == data->acpi_data.states[i].status)
96 return data->acpi_data.states[i].core_frequency;
98 return data->acpi_data.states[i-1].core_frequency;
102 static unsigned int
103 processor_get_freq (
104 struct cpufreq_acpi_io *data,
105 unsigned int cpu)
107 int ret = 0;
108 u32 value = 0;
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)
117 goto migrate_end;
119 /* processor_get_pstate gets the instantaneous frequency */
120 ret = processor_get_pstate(&value);
122 if (ret) {
123 set_cpus_allowed(current, saved_mask);
124 printk(KERN_WARNING "get performance failed with error %d\n",
125 ret);
126 ret = 0;
127 goto migrate_end;
129 clock_freq = extract_clock(data, value, cpu);
130 ret = (clock_freq*1000);
132 migrate_end:
133 set_cpus_allowed(current, saved_mask);
134 return ret;
138 static int
139 processor_set_freq (
140 struct cpufreq_acpi_io *data,
141 unsigned int cpu,
142 int state)
144 int ret = 0;
145 u32 value = 0;
146 struct cpufreq_freqs cpufreq_freqs;
147 cpumask_t saved_mask;
148 int retval;
150 dprintk("processor_set_freq\n");
152 saved_mask = current->cpus_allowed;
153 set_cpus_allowed(current, cpumask_of_cpu(cpu));
154 if (smp_processor_id() != cpu) {
155 retval = -EAGAIN;
156 goto migrate_end;
159 if (state == data->acpi_data.state) {
160 if (unlikely(data->resume)) {
161 dprintk("Called after resume, resetting to P%d\n", state);
162 data->resume = 0;
163 } else {
164 dprintk("Already at target state (P%d)\n", state);
165 retval = 0;
166 goto migrate_end;
170 dprintk("Transitioning from P%d to P%d\n",
171 data->acpi_data.state, state);
173 /* cpufreq frequency struct */
174 cpufreq_freqs.cpu = cpu;
175 cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
176 cpufreq_freqs.new = data->freq_table[state].frequency;
178 /* notify cpufreq */
179 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
182 * First we write the target state's 'control' value to the
183 * control_register.
186 value = (u32) data->acpi_data.states[state].control;
188 dprintk("Transitioning to state: 0x%08x\n", value);
190 ret = processor_set_pstate(value);
191 if (ret) {
192 unsigned int tmp = cpufreq_freqs.new;
193 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
194 cpufreq_freqs.new = cpufreq_freqs.old;
195 cpufreq_freqs.old = tmp;
196 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
197 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
198 printk(KERN_WARNING "Transition failed with error %d\n", ret);
199 retval = -ENODEV;
200 goto migrate_end;
203 cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
205 data->acpi_data.state = state;
207 retval = 0;
209 migrate_end:
210 set_cpus_allowed(current, saved_mask);
211 return (retval);
215 static unsigned int
216 acpi_cpufreq_get (
217 unsigned int cpu)
219 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
221 dprintk("acpi_cpufreq_get\n");
223 return processor_get_freq(data, cpu);
227 static int
228 acpi_cpufreq_target (
229 struct cpufreq_policy *policy,
230 unsigned int target_freq,
231 unsigned int relation)
233 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
234 unsigned int next_state = 0;
235 unsigned int result = 0;
237 dprintk("acpi_cpufreq_setpolicy\n");
239 result = cpufreq_frequency_table_target(policy,
240 data->freq_table, target_freq, relation, &next_state);
241 if (result)
242 return (result);
244 result = processor_set_freq(data, policy->cpu, next_state);
246 return (result);
250 static int
251 acpi_cpufreq_verify (
252 struct cpufreq_policy *policy)
254 unsigned int result = 0;
255 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
257 dprintk("acpi_cpufreq_verify\n");
259 result = cpufreq_frequency_table_verify(policy,
260 data->freq_table);
262 return (result);
266 static int
267 acpi_cpufreq_cpu_init (
268 struct cpufreq_policy *policy)
270 unsigned int i;
271 unsigned int cpu = policy->cpu;
272 struct cpufreq_acpi_io *data;
273 unsigned int result = 0;
275 dprintk("acpi_cpufreq_cpu_init\n");
277 data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
278 if (!data)
279 return (-ENOMEM);
281 acpi_io_data[cpu] = data;
283 result = acpi_processor_register_performance(&data->acpi_data, cpu);
285 if (result)
286 goto err_free;
288 /* capability check */
289 if (data->acpi_data.state_count <= 1) {
290 dprintk("No P-States\n");
291 result = -ENODEV;
292 goto err_unreg;
295 if ((data->acpi_data.control_register.space_id !=
296 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
297 (data->acpi_data.status_register.space_id !=
298 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
299 dprintk("Unsupported address space [%d, %d]\n",
300 (u32) (data->acpi_data.control_register.space_id),
301 (u32) (data->acpi_data.status_register.space_id));
302 result = -ENODEV;
303 goto err_unreg;
306 /* alloc freq_table */
307 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
308 (data->acpi_data.state_count + 1),
309 GFP_KERNEL);
310 if (!data->freq_table) {
311 result = -ENOMEM;
312 goto err_unreg;
315 /* detect transition latency */
316 policy->cpuinfo.transition_latency = 0;
317 for (i=0; i<data->acpi_data.state_count; i++) {
318 if ((data->acpi_data.states[i].transition_latency * 1000) >
319 policy->cpuinfo.transition_latency) {
320 policy->cpuinfo.transition_latency =
321 data->acpi_data.states[i].transition_latency * 1000;
324 policy->cur = processor_get_freq(data, policy->cpu);
326 /* table init */
327 for (i = 0; i <= data->acpi_data.state_count; i++)
329 data->freq_table[i].index = i;
330 if (i < data->acpi_data.state_count) {
331 data->freq_table[i].frequency =
332 data->acpi_data.states[i].core_frequency * 1000;
333 } else {
334 data->freq_table[i].frequency = CPUFREQ_TABLE_END;
338 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
339 if (result) {
340 goto err_freqfree;
343 /* notify BIOS that we exist */
344 acpi_processor_notify_smm(THIS_MODULE);
346 printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management "
347 "activated.\n", cpu);
349 for (i = 0; i < data->acpi_data.state_count; i++)
350 dprintk(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
351 (i == data->acpi_data.state?'*':' '), i,
352 (u32) data->acpi_data.states[i].core_frequency,
353 (u32) data->acpi_data.states[i].power,
354 (u32) data->acpi_data.states[i].transition_latency,
355 (u32) data->acpi_data.states[i].bus_master_latency,
356 (u32) data->acpi_data.states[i].status,
357 (u32) data->acpi_data.states[i].control);
359 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
361 /* the first call to ->target() should result in us actually
362 * writing something to the appropriate registers. */
363 data->resume = 1;
365 return (result);
367 err_freqfree:
368 kfree(data->freq_table);
369 err_unreg:
370 acpi_processor_unregister_performance(&data->acpi_data, cpu);
371 err_free:
372 kfree(data);
373 acpi_io_data[cpu] = NULL;
375 return (result);
379 static int
380 acpi_cpufreq_cpu_exit (
381 struct cpufreq_policy *policy)
383 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
385 dprintk("acpi_cpufreq_cpu_exit\n");
387 if (data) {
388 cpufreq_frequency_table_put_attr(policy->cpu);
389 acpi_io_data[policy->cpu] = NULL;
390 acpi_processor_unregister_performance(&data->acpi_data,
391 policy->cpu);
392 kfree(data);
395 return (0);
399 static struct freq_attr* acpi_cpufreq_attr[] = {
400 &cpufreq_freq_attr_scaling_available_freqs,
401 NULL,
405 static struct cpufreq_driver acpi_cpufreq_driver = {
406 .verify = acpi_cpufreq_verify,
407 .target = acpi_cpufreq_target,
408 .get = acpi_cpufreq_get,
409 .init = acpi_cpufreq_cpu_init,
410 .exit = acpi_cpufreq_cpu_exit,
411 .name = "acpi-cpufreq",
412 .owner = THIS_MODULE,
413 .attr = acpi_cpufreq_attr,
417 static int __init
418 acpi_cpufreq_init (void)
420 dprintk("acpi_cpufreq_init\n");
422 return cpufreq_register_driver(&acpi_cpufreq_driver);
426 static void __exit
427 acpi_cpufreq_exit (void)
429 dprintk("acpi_cpufreq_exit\n");
431 cpufreq_unregister_driver(&acpi_cpufreq_driver);
432 return;
436 late_initcall(acpi_cpufreq_init);
437 module_exit(acpi_cpufreq_exit);