]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/cpu/cpufreq/acpi-cpufreq.c
ring-buffer: do not remove reader page from list on ring buffer free
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / cpu / cpufreq / acpi-cpufreq.c
1 /*
2  * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.4 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7  *  Copyright (C) 2006       Denis Sadykov <denis.m.sadykov@intel.com>
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or (at
14  *  your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24  *
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/smp.h>
32 #include <linux/sched.h>
33 #include <linux/cpufreq.h>
34 #include <linux/compiler.h>
35 #include <linux/dmi.h>
36 #include <trace/power.h>
37
38 #include <linux/acpi.h>
39 #include <acpi/processor.h>
40
41 #include <asm/io.h>
42 #include <asm/msr.h>
43 #include <asm/processor.h>
44 #include <asm/cpufeature.h>
45 #include <asm/delay.h>
46 #include <asm/uaccess.h>
47
48 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
49
50 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
51 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
52 MODULE_LICENSE("GPL");
53
54 enum {
55         UNDEFINED_CAPABLE = 0,
56         SYSTEM_INTEL_MSR_CAPABLE,
57         SYSTEM_IO_CAPABLE,
58 };
59
60 #define INTEL_MSR_RANGE         (0xffff)
61 #define CPUID_6_ECX_APERFMPERF_CAPABILITY       (0x1)
62
63 struct acpi_cpufreq_data {
64         struct acpi_processor_performance *acpi_data;
65         struct cpufreq_frequency_table *freq_table;
66         unsigned int max_freq;
67         unsigned int resume;
68         unsigned int cpu_feature;
69 };
70
71 static DEFINE_PER_CPU(struct acpi_cpufreq_data *, drv_data);
72
73 DEFINE_TRACE(power_mark);
74
75 /* acpi_perf_data is a pointer to percpu data. */
76 static struct acpi_processor_performance *acpi_perf_data;
77
78 static struct cpufreq_driver acpi_cpufreq_driver;
79
80 static unsigned int acpi_pstate_strict;
81
82 static int check_est_cpu(unsigned int cpuid)
83 {
84         struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
85
86         if (cpu->x86_vendor != X86_VENDOR_INTEL ||
87             !cpu_has(cpu, X86_FEATURE_EST))
88                 return 0;
89
90         return 1;
91 }
92
93 static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
94 {
95         struct acpi_processor_performance *perf;
96         int i;
97
98         perf = data->acpi_data;
99
100         for (i=0; i<perf->state_count; i++) {
101                 if (value == perf->states[i].status)
102                         return data->freq_table[i].frequency;
103         }
104         return 0;
105 }
106
107 static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
108 {
109         int i;
110         struct acpi_processor_performance *perf;
111
112         msr &= INTEL_MSR_RANGE;
113         perf = data->acpi_data;
114
115         for (i=0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
116                 if (msr == perf->states[data->freq_table[i].index].status)
117                         return data->freq_table[i].frequency;
118         }
119         return data->freq_table[0].frequency;
120 }
121
122 static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
123 {
124         switch (data->cpu_feature) {
125         case SYSTEM_INTEL_MSR_CAPABLE:
126                 return extract_msr(val, data);
127         case SYSTEM_IO_CAPABLE:
128                 return extract_io(val, data);
129         default:
130                 return 0;
131         }
132 }
133
134 struct msr_addr {
135         u32 reg;
136 };
137
138 struct io_addr {
139         u16 port;
140         u8 bit_width;
141 };
142
143 typedef union {
144         struct msr_addr msr;
145         struct io_addr io;
146 } drv_addr_union;
147
148 struct drv_cmd {
149         unsigned int type;
150         const struct cpumask *mask;
151         drv_addr_union addr;
152         u32 val;
153 };
154
155 static long do_drv_read(void *_cmd)
156 {
157         struct drv_cmd *cmd = _cmd;
158         u32 h;
159
160         switch (cmd->type) {
161         case SYSTEM_INTEL_MSR_CAPABLE:
162                 rdmsr(cmd->addr.msr.reg, cmd->val, h);
163                 break;
164         case SYSTEM_IO_CAPABLE:
165                 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
166                                 &cmd->val,
167                                 (u32)cmd->addr.io.bit_width);
168                 break;
169         default:
170                 break;
171         }
172         return 0;
173 }
174
175 static long do_drv_write(void *_cmd)
176 {
177         struct drv_cmd *cmd = _cmd;
178         u32 lo, hi;
179
180         switch (cmd->type) {
181         case SYSTEM_INTEL_MSR_CAPABLE:
182                 rdmsr(cmd->addr.msr.reg, lo, hi);
183                 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
184                 wrmsr(cmd->addr.msr.reg, lo, hi);
185                 break;
186         case SYSTEM_IO_CAPABLE:
187                 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
188                                 cmd->val,
189                                 (u32)cmd->addr.io.bit_width);
190                 break;
191         default:
192                 break;
193         }
194         return 0;
195 }
196
197 static void drv_read(struct drv_cmd *cmd)
198 {
199         cmd->val = 0;
200
201         work_on_cpu(cpumask_any(cmd->mask), do_drv_read, cmd);
202 }
203
204 static void drv_write(struct drv_cmd *cmd)
205 {
206         unsigned int i;
207
208         for_each_cpu(i, cmd->mask) {
209                 work_on_cpu(i, do_drv_write, cmd);
210         }
211 }
212
213 static u32 get_cur_val(const struct cpumask *mask)
214 {
215         struct acpi_processor_performance *perf;
216         struct drv_cmd cmd;
217
218         if (unlikely(cpumask_empty(mask)))
219                 return 0;
220
221         switch (per_cpu(drv_data, cpumask_first(mask))->cpu_feature) {
222         case SYSTEM_INTEL_MSR_CAPABLE:
223                 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
224                 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
225                 break;
226         case SYSTEM_IO_CAPABLE:
227                 cmd.type = SYSTEM_IO_CAPABLE;
228                 perf = per_cpu(drv_data, cpumask_first(mask))->acpi_data;
229                 cmd.addr.io.port = perf->control_register.address;
230                 cmd.addr.io.bit_width = perf->control_register.bit_width;
231                 break;
232         default:
233                 return 0;
234         }
235
236         cmd.mask = mask;
237         drv_read(&cmd);
238
239         dprintk("get_cur_val = %u\n", cmd.val);
240
241         return cmd.val;
242 }
243
244 struct perf_cur {
245         union {
246                 struct {
247                         u32 lo;
248                         u32 hi;
249                 } split;
250                 u64 whole;
251         } aperf_cur, mperf_cur;
252 };
253
254
255 static long read_measured_perf_ctrs(void *_cur)
256 {
257         struct perf_cur *cur = _cur;
258
259         rdmsr(MSR_IA32_APERF, cur->aperf_cur.split.lo, cur->aperf_cur.split.hi);
260         rdmsr(MSR_IA32_MPERF, cur->mperf_cur.split.lo, cur->mperf_cur.split.hi);
261
262         wrmsr(MSR_IA32_APERF, 0, 0);
263         wrmsr(MSR_IA32_MPERF, 0, 0);
264
265         return 0;
266 }
267
268 /*
269  * Return the measured active (C0) frequency on this CPU since last call
270  * to this function.
271  * Input: cpu number
272  * Return: Average CPU frequency in terms of max frequency (zero on error)
273  *
274  * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
275  * over a period of time, while CPU is in C0 state.
276  * IA32_MPERF counts at the rate of max advertised frequency
277  * IA32_APERF counts at the rate of actual CPU frequency
278  * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
279  * no meaning should be associated with absolute values of these MSRs.
280  */
281 static unsigned int get_measured_perf(struct cpufreq_policy *policy,
282                                       unsigned int cpu)
283 {
284         struct perf_cur cur;
285         unsigned int perf_percent;
286         unsigned int retval;
287
288         if (!work_on_cpu(cpu, read_measured_perf_ctrs, &cur))
289                 return 0;
290
291 #ifdef __i386__
292         /*
293          * We dont want to do 64 bit divide with 32 bit kernel
294          * Get an approximate value. Return failure in case we cannot get
295          * an approximate value.
296          */
297         if (unlikely(cur.aperf_cur.split.hi || cur.mperf_cur.split.hi)) {
298                 int shift_count;
299                 u32 h;
300
301                 h = max_t(u32, cur.aperf_cur.split.hi, cur.mperf_cur.split.hi);
302                 shift_count = fls(h);
303
304                 cur.aperf_cur.whole >>= shift_count;
305                 cur.mperf_cur.whole >>= shift_count;
306         }
307
308         if (((unsigned long)(-1) / 100) < cur.aperf_cur.split.lo) {
309                 int shift_count = 7;
310                 cur.aperf_cur.split.lo >>= shift_count;
311                 cur.mperf_cur.split.lo >>= shift_count;
312         }
313
314         if (cur.aperf_cur.split.lo && cur.mperf_cur.split.lo)
315                 perf_percent = (cur.aperf_cur.split.lo * 100) /
316                                 cur.mperf_cur.split.lo;
317         else
318                 perf_percent = 0;
319
320 #else
321         if (unlikely(((unsigned long)(-1) / 100) < cur.aperf_cur.whole)) {
322                 int shift_count = 7;
323                 cur.aperf_cur.whole >>= shift_count;
324                 cur.mperf_cur.whole >>= shift_count;
325         }
326
327         if (cur.aperf_cur.whole && cur.mperf_cur.whole)
328                 perf_percent = (cur.aperf_cur.whole * 100) /
329                                 cur.mperf_cur.whole;
330         else
331                 perf_percent = 0;
332
333 #endif
334
335         retval = per_cpu(drv_data, policy->cpu)->max_freq * perf_percent / 100;
336
337         return retval;
338 }
339
340 static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
341 {
342         struct acpi_cpufreq_data *data = per_cpu(drv_data, cpu);
343         unsigned int freq;
344         unsigned int cached_freq;
345
346         dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
347
348         if (unlikely(data == NULL ||
349                      data->acpi_data == NULL || data->freq_table == NULL)) {
350                 return 0;
351         }
352
353         cached_freq = data->freq_table[data->acpi_data->state].frequency;
354         freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
355         if (freq != cached_freq) {
356                 /*
357                  * The dreaded BIOS frequency change behind our back.
358                  * Force set the frequency on next target call.
359                  */
360                 data->resume = 1;
361         }
362
363         dprintk("cur freq = %u\n", freq);
364
365         return freq;
366 }
367
368 static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
369                                 struct acpi_cpufreq_data *data)
370 {
371         unsigned int cur_freq;
372         unsigned int i;
373
374         for (i=0; i<100; i++) {
375                 cur_freq = extract_freq(get_cur_val(mask), data);
376                 if (cur_freq == freq)
377                         return 1;
378                 udelay(10);
379         }
380         return 0;
381 }
382
383 static int acpi_cpufreq_target(struct cpufreq_policy *policy,
384                                unsigned int target_freq, unsigned int relation)
385 {
386         struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
387         struct acpi_processor_performance *perf;
388         struct cpufreq_freqs freqs;
389         struct drv_cmd cmd;
390         unsigned int next_state = 0; /* Index into freq_table */
391         unsigned int next_perf_state = 0; /* Index into perf table */
392         unsigned int i;
393         int result = 0;
394         struct power_trace it;
395
396         dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
397
398         if (unlikely(data == NULL ||
399              data->acpi_data == NULL || data->freq_table == NULL)) {
400                 return -ENODEV;
401         }
402
403         perf = data->acpi_data;
404         result = cpufreq_frequency_table_target(policy,
405                                                 data->freq_table,
406                                                 target_freq,
407                                                 relation, &next_state);
408         if (unlikely(result)) {
409                 result = -ENODEV;
410                 goto out;
411         }
412
413         next_perf_state = data->freq_table[next_state].index;
414         if (perf->state == next_perf_state) {
415                 if (unlikely(data->resume)) {
416                         dprintk("Called after resume, resetting to P%d\n",
417                                 next_perf_state);
418                         data->resume = 0;
419                 } else {
420                         dprintk("Already at target state (P%d)\n",
421                                 next_perf_state);
422                         goto out;
423                 }
424         }
425
426         trace_power_mark(&it, POWER_PSTATE, next_perf_state);
427
428         switch (data->cpu_feature) {
429         case SYSTEM_INTEL_MSR_CAPABLE:
430                 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
431                 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
432                 cmd.val = (u32) perf->states[next_perf_state].control;
433                 break;
434         case SYSTEM_IO_CAPABLE:
435                 cmd.type = SYSTEM_IO_CAPABLE;
436                 cmd.addr.io.port = perf->control_register.address;
437                 cmd.addr.io.bit_width = perf->control_register.bit_width;
438                 cmd.val = (u32) perf->states[next_perf_state].control;
439                 break;
440         default:
441                 result = -ENODEV;
442                 goto out;
443         }
444
445         /* cpufreq holds the hotplug lock, so we are safe from here on */
446         if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
447                 cmd.mask = policy->cpus;
448         else
449                 cmd.mask = cpumask_of(policy->cpu);
450
451         freqs.old = perf->states[perf->state].core_frequency * 1000;
452         freqs.new = data->freq_table[next_state].frequency;
453         for_each_cpu(i, cmd.mask) {
454                 freqs.cpu = i;
455                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
456         }
457
458         drv_write(&cmd);
459
460         if (acpi_pstate_strict) {
461                 if (!check_freqs(cmd.mask, freqs.new, data)) {
462                         dprintk("acpi_cpufreq_target failed (%d)\n",
463                                 policy->cpu);
464                         result = -EAGAIN;
465                         goto out;
466                 }
467         }
468
469         for_each_cpu(i, cmd.mask) {
470                 freqs.cpu = i;
471                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
472         }
473         perf->state = next_perf_state;
474
475 out:
476         return result;
477 }
478
479 static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
480 {
481         struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
482
483         dprintk("acpi_cpufreq_verify\n");
484
485         return cpufreq_frequency_table_verify(policy, data->freq_table);
486 }
487
488 static unsigned long
489 acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
490 {
491         struct acpi_processor_performance *perf = data->acpi_data;
492
493         if (cpu_khz) {
494                 /* search the closest match to cpu_khz */
495                 unsigned int i;
496                 unsigned long freq;
497                 unsigned long freqn = perf->states[0].core_frequency * 1000;
498
499                 for (i=0; i<(perf->state_count-1); i++) {
500                         freq = freqn;
501                         freqn = perf->states[i+1].core_frequency * 1000;
502                         if ((2 * cpu_khz) > (freqn + freq)) {
503                                 perf->state = i;
504                                 return freq;
505                         }
506                 }
507                 perf->state = perf->state_count-1;
508                 return freqn;
509         } else {
510                 /* assume CPU is at P0... */
511                 perf->state = 0;
512                 return perf->states[0].core_frequency * 1000;
513         }
514 }
515
516 static void free_acpi_perf_data(void)
517 {
518         unsigned int i;
519
520         /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
521         for_each_possible_cpu(i)
522                 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
523                                  ->shared_cpu_map);
524         free_percpu(acpi_perf_data);
525 }
526
527 /*
528  * acpi_cpufreq_early_init - initialize ACPI P-States library
529  *
530  * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
531  * in order to determine correct frequency and voltage pairings. We can
532  * do _PDC and _PSD and find out the processor dependency for the
533  * actual init that will happen later...
534  */
535 static int __init acpi_cpufreq_early_init(void)
536 {
537         unsigned int i;
538         dprintk("acpi_cpufreq_early_init\n");
539
540         acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
541         if (!acpi_perf_data) {
542                 dprintk("Memory allocation error for acpi_perf_data.\n");
543                 return -ENOMEM;
544         }
545         for_each_possible_cpu(i) {
546                 if (!alloc_cpumask_var_node(
547                         &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
548                         GFP_KERNEL, cpu_to_node(i))) {
549
550                         /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
551                         free_acpi_perf_data();
552                         return -ENOMEM;
553                 }
554         }
555
556         /* Do initialization in ACPI core */
557         acpi_processor_preregister_performance(acpi_perf_data);
558         return 0;
559 }
560
561 #ifdef CONFIG_SMP
562 /*
563  * Some BIOSes do SW_ANY coordination internally, either set it up in hw
564  * or do it in BIOS firmware and won't inform about it to OS. If not
565  * detected, this has a side effect of making CPU run at a different speed
566  * than OS intended it to run at. Detect it and handle it cleanly.
567  */
568 static int bios_with_sw_any_bug;
569
570 static int sw_any_bug_found(const struct dmi_system_id *d)
571 {
572         bios_with_sw_any_bug = 1;
573         return 0;
574 }
575
576 static const struct dmi_system_id sw_any_bug_dmi_table[] = {
577         {
578                 .callback = sw_any_bug_found,
579                 .ident = "Supermicro Server X6DLP",
580                 .matches = {
581                         DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
582                         DMI_MATCH(DMI_BIOS_VERSION, "080010"),
583                         DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
584                 },
585         },
586         { }
587 };
588 #endif
589
590 static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
591 {
592         unsigned int i;
593         unsigned int valid_states = 0;
594         unsigned int cpu = policy->cpu;
595         struct acpi_cpufreq_data *data;
596         unsigned int result = 0;
597         struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
598         struct acpi_processor_performance *perf;
599
600         dprintk("acpi_cpufreq_cpu_init\n");
601
602         data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
603         if (!data)
604                 return -ENOMEM;
605
606         data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
607         per_cpu(drv_data, cpu) = data;
608
609         if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
610                 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
611
612         result = acpi_processor_register_performance(data->acpi_data, cpu);
613         if (result)
614                 goto err_free;
615
616         perf = data->acpi_data;
617         policy->shared_type = perf->shared_type;
618
619         /*
620          * Will let policy->cpus know about dependency only when software
621          * coordination is required.
622          */
623         if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
624             policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
625                 cpumask_copy(policy->cpus, perf->shared_cpu_map);
626         }
627         cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
628
629 #ifdef CONFIG_SMP
630         dmi_check_system(sw_any_bug_dmi_table);
631         if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
632                 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
633                 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
634         }
635 #endif
636
637         /* capability check */
638         if (perf->state_count <= 1) {
639                 dprintk("No P-States\n");
640                 result = -ENODEV;
641                 goto err_unreg;
642         }
643
644         if (perf->control_register.space_id != perf->status_register.space_id) {
645                 result = -ENODEV;
646                 goto err_unreg;
647         }
648
649         switch (perf->control_register.space_id) {
650         case ACPI_ADR_SPACE_SYSTEM_IO:
651                 dprintk("SYSTEM IO addr space\n");
652                 data->cpu_feature = SYSTEM_IO_CAPABLE;
653                 break;
654         case ACPI_ADR_SPACE_FIXED_HARDWARE:
655                 dprintk("HARDWARE addr space\n");
656                 if (!check_est_cpu(cpu)) {
657                         result = -ENODEV;
658                         goto err_unreg;
659                 }
660                 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
661                 break;
662         default:
663                 dprintk("Unknown addr space %d\n",
664                         (u32) (perf->control_register.space_id));
665                 result = -ENODEV;
666                 goto err_unreg;
667         }
668
669         data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
670                     (perf->state_count+1), GFP_KERNEL);
671         if (!data->freq_table) {
672                 result = -ENOMEM;
673                 goto err_unreg;
674         }
675
676         /* detect transition latency */
677         policy->cpuinfo.transition_latency = 0;
678         for (i=0; i<perf->state_count; i++) {
679                 if ((perf->states[i].transition_latency * 1000) >
680                     policy->cpuinfo.transition_latency)
681                         policy->cpuinfo.transition_latency =
682                             perf->states[i].transition_latency * 1000;
683         }
684
685         data->max_freq = perf->states[0].core_frequency * 1000;
686         /* table init */
687         for (i=0; i<perf->state_count; i++) {
688                 if (i>0 && perf->states[i].core_frequency >=
689                     data->freq_table[valid_states-1].frequency / 1000)
690                         continue;
691
692                 data->freq_table[valid_states].index = i;
693                 data->freq_table[valid_states].frequency =
694                     perf->states[i].core_frequency * 1000;
695                 valid_states++;
696         }
697         data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
698         perf->state = 0;
699
700         result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
701         if (result)
702                 goto err_freqfree;
703
704         switch (perf->control_register.space_id) {
705         case ACPI_ADR_SPACE_SYSTEM_IO:
706                 /* Current speed is unknown and not detectable by IO port */
707                 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
708                 break;
709         case ACPI_ADR_SPACE_FIXED_HARDWARE:
710                 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
711                 policy->cur = get_cur_freq_on_cpu(cpu);
712                 break;
713         default:
714                 break;
715         }
716
717         /* notify BIOS that we exist */
718         acpi_processor_notify_smm(THIS_MODULE);
719
720         /* Check for APERF/MPERF support in hardware */
721         if (c->x86_vendor == X86_VENDOR_INTEL && c->cpuid_level >= 6) {
722                 unsigned int ecx;
723                 ecx = cpuid_ecx(6);
724                 if (ecx & CPUID_6_ECX_APERFMPERF_CAPABILITY)
725                         acpi_cpufreq_driver.getavg = get_measured_perf;
726         }
727
728         dprintk("CPU%u - ACPI performance management activated.\n", cpu);
729         for (i = 0; i < perf->state_count; i++)
730                 dprintk("     %cP%d: %d MHz, %d mW, %d uS\n",
731                         (i == perf->state ? '*' : ' '), i,
732                         (u32) perf->states[i].core_frequency,
733                         (u32) perf->states[i].power,
734                         (u32) perf->states[i].transition_latency);
735
736         cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
737
738         /*
739          * the first call to ->target() should result in us actually
740          * writing something to the appropriate registers.
741          */
742         data->resume = 1;
743
744         return result;
745
746 err_freqfree:
747         kfree(data->freq_table);
748 err_unreg:
749         acpi_processor_unregister_performance(perf, cpu);
750 err_free:
751         kfree(data);
752         per_cpu(drv_data, cpu) = NULL;
753
754         return result;
755 }
756
757 static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
758 {
759         struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
760
761         dprintk("acpi_cpufreq_cpu_exit\n");
762
763         if (data) {
764                 cpufreq_frequency_table_put_attr(policy->cpu);
765                 per_cpu(drv_data, policy->cpu) = NULL;
766                 acpi_processor_unregister_performance(data->acpi_data,
767                                                       policy->cpu);
768                 kfree(data);
769         }
770
771         return 0;
772 }
773
774 static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
775 {
776         struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
777
778         dprintk("acpi_cpufreq_resume\n");
779
780         data->resume = 1;
781
782         return 0;
783 }
784
785 static struct freq_attr *acpi_cpufreq_attr[] = {
786         &cpufreq_freq_attr_scaling_available_freqs,
787         NULL,
788 };
789
790 static struct cpufreq_driver acpi_cpufreq_driver = {
791         .verify = acpi_cpufreq_verify,
792         .target = acpi_cpufreq_target,
793         .init = acpi_cpufreq_cpu_init,
794         .exit = acpi_cpufreq_cpu_exit,
795         .resume = acpi_cpufreq_resume,
796         .name = "acpi-cpufreq",
797         .owner = THIS_MODULE,
798         .attr = acpi_cpufreq_attr,
799 };
800
801 static int __init acpi_cpufreq_init(void)
802 {
803         int ret;
804
805         if (acpi_disabled)
806                 return 0;
807
808         dprintk("acpi_cpufreq_init\n");
809
810         ret = acpi_cpufreq_early_init();
811         if (ret)
812                 return ret;
813
814         ret = cpufreq_register_driver(&acpi_cpufreq_driver);
815         if (ret)
816                 free_acpi_perf_data();
817
818         return ret;
819 }
820
821 static void __exit acpi_cpufreq_exit(void)
822 {
823         dprintk("acpi_cpufreq_exit\n");
824
825         cpufreq_unregister_driver(&acpi_cpufreq_driver);
826
827         free_percpu(acpi_perf_data);
828 }
829
830 module_param(acpi_pstate_strict, uint, 0644);
831 MODULE_PARM_DESC(acpi_pstate_strict,
832         "value 0 or non-zero. non-zero -> strict ACPI checks are "
833         "performed during frequency changes.");
834
835 late_initcall(acpi_cpufreq_init);
836 module_exit(acpi_cpufreq_exit);
837
838 MODULE_ALIAS("acpi");