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