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