Merge branch 'upstream' of git://lost.foo-projects.org/~ahkok/git/netdev-2.6 into...
[pandora-kernel.git] / arch / ia64 / kernel / cpufreq / acpi-cpufreq.c
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)
7  *
8  * Copyright (C) 2005 Intel Corp
9  *      Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10  */
11
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>
21
22 #include <linux/acpi.h>
23 #include <acpi/processor.h>
24
25 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
26
27 MODULE_AUTHOR("Venkatesh Pallipadi");
28 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
29 MODULE_LICENSE("GPL");
30
31
32 struct cpufreq_acpi_io {
33         struct acpi_processor_performance       acpi_data;
34         struct cpufreq_frequency_table          *freq_table;
35         unsigned int                            resume;
36 };
37
38 static struct cpufreq_acpi_io   *acpi_io_data[NR_CPUS];
39
40 static struct cpufreq_driver acpi_cpufreq_driver;
41
42
43 static int
44 processor_set_pstate (
45         u32     value)
46 {
47         s64 retval;
48
49         dprintk("processor_set_pstate\n");
50
51         retval = ia64_pal_set_pstate((u64)value);
52
53         if (retval) {
54                 dprintk("Failed to set freq to 0x%x, with error 0x%x\n",
55                         value, retval);
56                 return -ENODEV;
57         }
58         return (int)retval;
59 }
60
61
62 static int
63 processor_get_pstate (
64         u32     *value)
65 {
66         u64     pstate_index = 0;
67         s64     retval;
68
69         dprintk("processor_get_pstate\n");
70
71         retval = ia64_pal_get_pstate(&pstate_index);
72         *value = (u32) pstate_index;
73
74         if (retval)
75                 dprintk("Failed to get current freq with "
76                         "error 0x%x, idx 0x%x\n", retval, *value);
77
78         return (int)retval;
79 }
80
81
82 /* To be used only after data->acpi_data is initialized */
83 static unsigned
84 extract_clock (
85         struct cpufreq_acpi_io *data,
86         unsigned value,
87         unsigned int cpu)
88 {
89         unsigned long i;
90
91         dprintk("extract_clock\n");
92
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;
96         }
97         return data->acpi_data.states[i-1].core_frequency;
98 }
99
100
101 static unsigned int
102 processor_get_freq (
103         struct cpufreq_acpi_io  *data,
104         unsigned int            cpu)
105 {
106         int                     ret = 0;
107         u32                     value = 0;
108         cpumask_t               saved_mask;
109         unsigned long           clock_freq;
110
111         dprintk("processor_get_freq\n");
112
113         saved_mask = current->cpus_allowed;
114         set_cpus_allowed(current, cpumask_of_cpu(cpu));
115         if (smp_processor_id() != cpu) {
116                 ret = -EAGAIN;
117                 goto migrate_end;
118         }
119
120         /*
121          * processor_get_pstate gets the average frequency since the
122          * last get. So, do two PAL_get_freq()...
123          */
124         ret = processor_get_pstate(&value);
125         ret = processor_get_pstate(&value);
126
127         if (ret) {
128                 set_cpus_allowed(current, saved_mask);
129                 printk(KERN_WARNING "get performance failed with error %d\n",
130                        ret);
131                 ret = -EAGAIN;
132                 goto migrate_end;
133         }
134         clock_freq = extract_clock(data, value, cpu);
135         ret = (clock_freq*1000);
136
137 migrate_end:
138         set_cpus_allowed(current, saved_mask);
139         return ret;
140 }
141
142
143 static int
144 processor_set_freq (
145         struct cpufreq_acpi_io  *data,
146         unsigned int            cpu,
147         int                     state)
148 {
149         int                     ret = 0;
150         u32                     value = 0;
151         struct cpufreq_freqs    cpufreq_freqs;
152         cpumask_t               saved_mask;
153         int                     retval;
154
155         dprintk("processor_set_freq\n");
156
157         saved_mask = current->cpus_allowed;
158         set_cpus_allowed(current, cpumask_of_cpu(cpu));
159         if (smp_processor_id() != cpu) {
160                 retval = -EAGAIN;
161                 goto migrate_end;
162         }
163
164         if (state == data->acpi_data.state) {
165                 if (unlikely(data->resume)) {
166                         dprintk("Called after resume, resetting to P%d\n", state);
167                         data->resume = 0;
168                 } else {
169                         dprintk("Already at target state (P%d)\n", state);
170                         retval = 0;
171                         goto migrate_end;
172                 }
173         }
174
175         dprintk("Transitioning from P%d to P%d\n",
176                 data->acpi_data.state, state);
177
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;
182
183         /* notify cpufreq */
184         cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
185
186         /*
187          * First we write the target state's 'control' value to the
188          * control_register.
189          */
190
191         value = (u32) data->acpi_data.states[state].control;
192
193         dprintk("Transitioning to state: 0x%08x\n", value);
194
195         ret = processor_set_pstate(value);
196         if (ret) {
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);
204                 retval = -ENODEV;
205                 goto migrate_end;
206         }
207
208         cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
209
210         data->acpi_data.state = state;
211
212         retval = 0;
213
214 migrate_end:
215         set_cpus_allowed(current, saved_mask);
216         return (retval);
217 }
218
219
220 static unsigned int
221 acpi_cpufreq_get (
222         unsigned int            cpu)
223 {
224         struct cpufreq_acpi_io *data = acpi_io_data[cpu];
225
226         dprintk("acpi_cpufreq_get\n");
227
228         return processor_get_freq(data, cpu);
229 }
230
231
232 static int
233 acpi_cpufreq_target (
234         struct cpufreq_policy   *policy,
235         unsigned int target_freq,
236         unsigned int relation)
237 {
238         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
239         unsigned int next_state = 0;
240         unsigned int result = 0;
241
242         dprintk("acpi_cpufreq_setpolicy\n");
243
244         result = cpufreq_frequency_table_target(policy,
245                         data->freq_table, target_freq, relation, &next_state);
246         if (result)
247                 return (result);
248
249         result = processor_set_freq(data, policy->cpu, next_state);
250
251         return (result);
252 }
253
254
255 static int
256 acpi_cpufreq_verify (
257         struct cpufreq_policy   *policy)
258 {
259         unsigned int result = 0;
260         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
261
262         dprintk("acpi_cpufreq_verify\n");
263
264         result = cpufreq_frequency_table_verify(policy,
265                         data->freq_table);
266
267         return (result);
268 }
269
270
271 static int
272 acpi_cpufreq_cpu_init (
273         struct cpufreq_policy   *policy)
274 {
275         unsigned int            i;
276         unsigned int            cpu = policy->cpu;
277         struct cpufreq_acpi_io  *data;
278         unsigned int            result = 0;
279
280         dprintk("acpi_cpufreq_cpu_init\n");
281
282         data = kmalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
283         if (!data)
284                 return (-ENOMEM);
285
286         memset(data, 0, sizeof(struct cpufreq_acpi_io));
287
288         acpi_io_data[cpu] = data;
289
290         result = acpi_processor_register_performance(&data->acpi_data, cpu);
291
292         if (result)
293                 goto err_free;
294
295         /* capability check */
296         if (data->acpi_data.state_count <= 1) {
297                 dprintk("No P-States\n");
298                 result = -ENODEV;
299                 goto err_unreg;
300         }
301
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));
309                 result = -ENODEV;
310                 goto err_unreg;
311         }
312
313         /* alloc freq_table */
314         data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
315                                    (data->acpi_data.state_count + 1),
316                                    GFP_KERNEL);
317         if (!data->freq_table) {
318                 result = -ENOMEM;
319                 goto err_unreg;
320         }
321
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;
329                 }
330         }
331         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
332
333         policy->cur = processor_get_freq(data, policy->cpu);
334
335         /* table init */
336         for (i = 0; i <= data->acpi_data.state_count; i++)
337         {
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;
342                 } else {
343                         data->freq_table[i].frequency = CPUFREQ_TABLE_END;
344                 }
345         }
346
347         result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
348         if (result) {
349                 goto err_freqfree;
350         }
351
352         /* notify BIOS that we exist */
353         acpi_processor_notify_smm(THIS_MODULE);
354
355         printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management "
356                "activated.\n", cpu);
357
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);
367
368         cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
369
370         /* the first call to ->target() should result in us actually
371          * writing something to the appropriate registers. */
372         data->resume = 1;
373
374         return (result);
375
376  err_freqfree:
377         kfree(data->freq_table);
378  err_unreg:
379         acpi_processor_unregister_performance(&data->acpi_data, cpu);
380  err_free:
381         kfree(data);
382         acpi_io_data[cpu] = NULL;
383
384         return (result);
385 }
386
387
388 static int
389 acpi_cpufreq_cpu_exit (
390         struct cpufreq_policy   *policy)
391 {
392         struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
393
394         dprintk("acpi_cpufreq_cpu_exit\n");
395
396         if (data) {
397                 cpufreq_frequency_table_put_attr(policy->cpu);
398                 acpi_io_data[policy->cpu] = NULL;
399                 acpi_processor_unregister_performance(&data->acpi_data,
400                                                       policy->cpu);
401                 kfree(data);
402         }
403
404         return (0);
405 }
406
407
408 static struct freq_attr* acpi_cpufreq_attr[] = {
409         &cpufreq_freq_attr_scaling_available_freqs,
410         NULL,
411 };
412
413
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,
423 };
424
425
426 static int __init
427 acpi_cpufreq_init (void)
428 {
429         dprintk("acpi_cpufreq_init\n");
430
431         return cpufreq_register_driver(&acpi_cpufreq_driver);
432 }
433
434
435 static void __exit
436 acpi_cpufreq_exit (void)
437 {
438         dprintk("acpi_cpufreq_exit\n");
439
440         cpufreq_unregister_driver(&acpi_cpufreq_driver);
441         return;
442 }
443
444
445 late_initcall(acpi_cpufreq_init);
446 module_exit(acpi_cpufreq_exit);
447