blob: d2e7c77c1ea4901a8e3e6b2a70c99b5c991132f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Jones3a58df32009-01-17 22:36:14 -05002 * acpi-cpufreq.c - ACPI Processor P-States Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -07007 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070031#include <linux/smp.h>
32#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/cpufreq.h>
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040034#include <linux/compiler.h>
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -070035#include <linux/dmi.h>
Arjan van de Ven61613522009-09-17 16:11:28 +020036#include <trace/events/power.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/acpi.h>
Dave Jones3a58df32009-01-17 22:36:14 -050039#include <linux/io.h>
40#include <linux/delay.h>
41#include <linux/uaccess.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <acpi/processor.h>
44
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070045#include <asm/msr.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070046#include <asm/processor.h>
47#include <asm/cpufeature.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070048
Dave Jones3a58df32009-01-17 22:36:14 -050049#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
50 "acpi-cpufreq", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
53MODULE_DESCRIPTION("ACPI Processor P-States Driver");
54MODULE_LICENSE("GPL");
55
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070056enum {
57 UNDEFINED_CAPABLE = 0,
58 SYSTEM_INTEL_MSR_CAPABLE,
59 SYSTEM_IO_CAPABLE,
60};
61
62#define INTEL_MSR_RANGE (0xffff)
63
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070064struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070065 struct acpi_processor_performance *acpi_data;
66 struct cpufreq_frequency_table *freq_table;
67 unsigned int resume;
68 unsigned int cpu_feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
travis@sgi.comea348f32008-01-30 13:33:12 +010071static DEFINE_PER_CPU(struct acpi_cpufreq_data *, drv_data);
72
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +020073static DEFINE_PER_CPU(struct aperfmperf, old_perf);
Pallipadi, Venkatesh093f13e2009-04-15 10:37:33 -070074
Fenghua Yu50109292007-08-07 18:40:30 -040075/* acpi_perf_data is a pointer to percpu data. */
76static struct acpi_processor_performance *acpi_perf_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static struct cpufreq_driver acpi_cpufreq_driver;
79
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040080static unsigned int acpi_pstate_strict;
81
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070082static int check_est_cpu(unsigned int cpuid)
83{
Mike Travis92cb7612007-10-19 20:35:04 +020084 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070085
Harald Welte0de51082009-06-08 18:27:54 +080086 return cpu_has(cpu, X86_FEATURE_EST);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070087}
88
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070089static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070090{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070091 struct acpi_processor_performance *perf;
92 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070093
94 perf = data->acpi_data;
95
Dave Jones3a58df32009-01-17 22:36:14 -050096 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070097 if (value == perf->states[i].status)
98 return data->freq_table[i].frequency;
99 }
100 return 0;
101}
102
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700103static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
104{
105 int i;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700106 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700107
108 msr &= INTEL_MSR_RANGE;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700109 perf = data->acpi_data;
110
Dave Jones3a58df32009-01-17 22:36:14 -0500111 for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700112 if (msr == perf->states[data->freq_table[i].index].status)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700113 return data->freq_table[i].frequency;
114 }
115 return data->freq_table[0].frequency;
116}
117
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700118static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
119{
120 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700121 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700122 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700123 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700124 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700125 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700126 return 0;
127 }
128}
129
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700130struct msr_addr {
131 u32 reg;
132};
133
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700134struct io_addr {
135 u16 port;
136 u8 bit_width;
137};
138
139struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700140 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100141 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500142 union {
143 struct msr_addr msr;
144 struct io_addr io;
145 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700146 u32 val;
147};
148
Andrew Morton01599fc2009-04-13 10:27:49 -0700149/* Called via smp_call_function_single(), on the target CPU */
150static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700151{
Mike Travis72859082009-01-16 15:31:15 -0800152 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700153 u32 h;
154
155 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700156 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700157 rdmsr(cmd->addr.msr.reg, cmd->val, h);
158 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700159 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800160 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
161 &cmd->val,
162 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700163 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700164 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700165 break;
166 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700167}
168
Andrew Morton01599fc2009-04-13 10:27:49 -0700169/* Called via smp_call_function_many(), on the target CPUs */
170static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700171{
Mike Travis72859082009-01-16 15:31:15 -0800172 struct drv_cmd *cmd = _cmd;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700173 u32 lo, hi;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700174
175 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700176 case SYSTEM_INTEL_MSR_CAPABLE:
Venki Pallipadi13424f62007-05-23 15:42:13 -0700177 rdmsr(cmd->addr.msr.reg, lo, hi);
178 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
179 wrmsr(cmd->addr.msr.reg, lo, hi);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700180 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700181 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800182 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
183 cmd->val,
184 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700185 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700186 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700187 break;
188 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700189}
190
Dave Jones95dd7222006-10-18 00:41:48 -0400191static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700192{
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700193 cmd->val = 0;
194
Andrew Morton01599fc2009-04-13 10:27:49 -0700195 smp_call_function_single(cpumask_any(cmd->mask), do_drv_read, cmd, 1);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700196}
197
198static void drv_write(struct drv_cmd *cmd)
199{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700200 int this_cpu;
201
202 this_cpu = get_cpu();
203 if (cpumask_test_cpu(this_cpu, cmd->mask))
204 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700205 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700206 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700207}
208
Mike Travis4d8bb532009-01-04 05:18:08 -0800209static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700210{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700211 struct acpi_processor_performance *perf;
212 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700213
Mike Travis4d8bb532009-01-04 05:18:08 -0800214 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700215 return 0;
216
Mike Travis4d8bb532009-01-04 05:18:08 -0800217 switch (per_cpu(drv_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700218 case SYSTEM_INTEL_MSR_CAPABLE:
219 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
220 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
221 break;
222 case SYSTEM_IO_CAPABLE:
223 cmd.type = SYSTEM_IO_CAPABLE;
Mike Travis4d8bb532009-01-04 05:18:08 -0800224 perf = per_cpu(drv_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700225 cmd.addr.io.port = perf->control_register.address;
226 cmd.addr.io.bit_width = perf->control_register.bit_width;
227 break;
228 default:
229 return 0;
230 }
231
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100232 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700233 drv_read(&cmd);
234
235 dprintk("get_cur_val = %u\n", cmd.val);
236
237 return cmd.val;
238}
239
Andrew Morton01599fc2009-04-13 10:27:49 -0700240/* Called via smp_call_function_single(), on the target CPU */
241static void read_measured_perf_ctrs(void *_cur)
Mike Travise39ad412009-01-04 05:18:10 -0800242{
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200243 struct aperfmperf *am = _cur;
Mike Travise39ad412009-01-04 05:18:10 -0800244
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200245 get_aperfmperf(am);
Mike Travise39ad412009-01-04 05:18:10 -0800246}
247
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700248/*
249 * Return the measured active (C0) frequency on this CPU since last call
250 * to this function.
251 * Input: cpu number
252 * Return: Average CPU frequency in terms of max frequency (zero on error)
253 *
254 * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
255 * over a period of time, while CPU is in C0 state.
256 * IA32_MPERF counts at the rate of max advertised frequency
257 * IA32_APERF counts at the rate of actual CPU frequency
258 * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
259 * no meaning should be associated with absolute values of these MSRs.
260 */
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -0700261static unsigned int get_measured_perf(struct cpufreq_policy *policy,
262 unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700263{
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200264 struct aperfmperf perf;
265 unsigned long ratio;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700266 unsigned int retval;
267
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200268 if (smp_call_function_single(cpu, read_measured_perf_ctrs, &perf, 1))
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700269 return 0;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700270
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200271 ratio = calc_aperfmperf_ratio(&per_cpu(old_perf, cpu), &perf);
272 per_cpu(old_perf, cpu) = perf;
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700273
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200274 retval = (policy->cpuinfo.max_freq * ratio) >> APERFMPERF_SHIFT;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700275
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700276 return retval;
277}
278
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700279static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
280{
travis@sgi.comea348f32008-01-30 13:33:12 +0100281 struct acpi_cpufreq_data *data = per_cpu(drv_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700282 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400283 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700284
285 dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
286
287 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700288 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700289 return 0;
290 }
291
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400292 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800293 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400294 if (freq != cached_freq) {
295 /*
296 * The dreaded BIOS frequency change behind our back.
297 * Force set the frequency on next target call.
298 */
299 data->resume = 1;
300 }
301
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700302 dprintk("cur freq = %u\n", freq);
303
304 return freq;
305}
306
Mike Travis72859082009-01-16 15:31:15 -0800307static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700308 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700309{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700310 unsigned int cur_freq;
311 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700312
Dave Jones3a58df32009-01-17 22:36:14 -0500313 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700314 cur_freq = extract_freq(get_cur_val(mask), data);
315 if (cur_freq == freq)
316 return 1;
317 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 return 0;
320}
321
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700322static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700323 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
travis@sgi.comea348f32008-01-30 13:33:12 +0100325 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700326 struct acpi_processor_performance *perf;
327 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700328 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800329 unsigned int next_state = 0; /* Index into freq_table */
330 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700331 unsigned int i;
332 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700334 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700336 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400337 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700338 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500341 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700342 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700343 data->freq_table,
344 target_freq,
345 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800346 if (unlikely(result)) {
347 result = -ENODEV;
348 goto out;
349 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500350
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700351 next_perf_state = data->freq_table[next_state].index;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700352 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700353 if (unlikely(data->resume)) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700354 dprintk("Called after resume, resetting to P%d\n",
355 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700356 data->resume = 0;
357 } else {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700358 dprintk("Already at target state (P%d)\n",
359 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800360 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700361 }
362 }
363
Arjan van de Ven61613522009-09-17 16:11:28 +0200364 trace_power_frequency(POWER_PSTATE, data->freq_table[next_state].frequency);
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800365
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700366 switch (data->cpu_feature) {
367 case SYSTEM_INTEL_MSR_CAPABLE:
368 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
369 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700370 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700371 break;
372 case SYSTEM_IO_CAPABLE:
373 cmd.type = SYSTEM_IO_CAPABLE;
374 cmd.addr.io.port = perf->control_register.address;
375 cmd.addr.io.bit_width = perf->control_register.bit_width;
376 cmd.val = (u32) perf->states[next_perf_state].control;
377 break;
378 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800379 result = -ENODEV;
380 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700381 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700382
Mike Travis4d8bb532009-01-04 05:18:08 -0800383 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700384 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100385 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700386 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100387 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700388
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800389 freqs.old = perf->states[perf->state].core_frequency * 1000;
390 freqs.new = data->freq_table[next_state].frequency;
Mike Travis4d8bb532009-01-04 05:18:08 -0800391 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700392 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500393 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
394 }
395
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700396 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500397
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700398 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800399 if (!check_freqs(cmd.mask, freqs.new, data)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700400 dprintk("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700401 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800402 result = -EAGAIN;
403 goto out;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500404 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500405 }
406
Mike Travis4d8bb532009-01-04 05:18:08 -0800407 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700408 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500409 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
410 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700411 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500412
Mike Travis4d8bb532009-01-04 05:18:08 -0800413out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700414 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700417static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
travis@sgi.comea348f32008-01-30 13:33:12 +0100419 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 dprintk("acpi_cpufreq_verify\n");
422
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700423 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700427acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700429 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (cpu_khz) {
432 /* search the closest match to cpu_khz */
433 unsigned int i;
434 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500435 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Dave Jones3a58df32009-01-17 22:36:14 -0500437 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400439 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500441 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700442 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 }
Dave Jones95dd7222006-10-18 00:41:48 -0400445 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700446 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500447 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500449 perf->state = 0;
450 return perf->states[0].core_frequency * 1000;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800454static void free_acpi_perf_data(void)
455{
456 unsigned int i;
457
458 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
459 for_each_possible_cpu(i)
460 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
461 ->shared_cpu_map);
462 free_percpu(acpi_perf_data);
463}
464
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500465/*
466 * acpi_cpufreq_early_init - initialize ACPI P-States library
467 *
468 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
469 * in order to determine correct frequency and voltage pairings. We can
470 * do _PDC and _PSD and find out the processor dependency for the
471 * actual init that will happen later...
472 */
Fenghua Yu50109292007-08-07 18:40:30 -0400473static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500474{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800475 unsigned int i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500476 dprintk("acpi_cpufreq_early_init\n");
477
Fenghua Yu50109292007-08-07 18:40:30 -0400478 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
479 if (!acpi_perf_data) {
480 dprintk("Memory allocation error for acpi_perf_data.\n");
481 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500482 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800483 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700484 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800485 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
486 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800487
488 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
489 free_acpi_perf_data();
490 return -ENOMEM;
491 }
492 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500493
494 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700495 acpi_processor_preregister_performance(acpi_perf_data);
496 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500497}
498
Dave Jones95625b82006-10-21 01:37:39 -0400499#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700500/*
501 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
502 * or do it in BIOS firmware and won't inform about it to OS. If not
503 * detected, this has a side effect of making CPU run at a different speed
504 * than OS intended it to run at. Detect it and handle it cleanly.
505 */
506static int bios_with_sw_any_bug;
507
Jeff Garzik18552562007-10-03 15:15:40 -0400508static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700509{
510 bios_with_sw_any_bug = 1;
511 return 0;
512}
513
Jeff Garzik18552562007-10-03 15:15:40 -0400514static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700515 {
516 .callback = sw_any_bug_found,
517 .ident = "Supermicro Server X6DLP",
518 .matches = {
519 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
520 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
521 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
522 },
523 },
524 { }
525};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400526
527static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
528{
John Villalovos293afe42009-09-25 13:30:08 -0400529 /* Intel Xeon Processor 7100 Series Specification Update
530 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400531 * AL30: A Machine Check Exception (MCE) Occurring during an
532 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400533 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400534 if (c->x86_vendor == X86_VENDOR_INTEL) {
535 if ((c->x86 == 15) &&
536 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400537 (c->x86_mask == 8)) {
538 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
539 "Xeon(R) 7100 Errata AL30, processors may "
540 "lock up on frequency changes: disabling "
541 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400542 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400543 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400544 }
545 return 0;
546}
Dave Jones95625b82006-10-21 01:37:39 -0400547#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700548
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700549static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700551 unsigned int i;
552 unsigned int valid_states = 0;
553 unsigned int cpu = policy->cpu;
554 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700555 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200556 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700557 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400558#ifdef CONFIG_SMP
559 static int blacklisted;
560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 dprintk("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400564#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400565 if (blacklisted)
566 return blacklisted;
567 blacklisted = acpi_cpufreq_blacklist(c);
568 if (blacklisted)
569 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400570#endif
571
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700572 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700574 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Rusty Russellb36128c2009-02-20 16:29:08 +0900576 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
travis@sgi.comea348f32008-01-30 13:33:12 +0100577 per_cpu(drv_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700578
Dave Jones95dd7222006-10-18 00:41:48 -0400579 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700580 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500582 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (result)
584 goto err_free;
585
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500586 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500587 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400588
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400589 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400590 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400591 * coordination is required.
592 */
593 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700594 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800595 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700596 }
Rusty Russell835481d2009-01-04 05:18:06 -0800597 cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700598
599#ifdef CONFIG_SMP
600 dmi_check_system(sw_any_bug_dmi_table);
Rusty Russell835481d2009-01-04 05:18:06 -0800601 if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700602 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800603 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700604 }
605#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500608 if (perf->state_count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dprintk("No P-States\n");
610 result = -ENODEV;
611 goto err_unreg;
612 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500613
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700614 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 result = -ENODEV;
616 goto err_unreg;
617 }
618
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700619 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700620 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700621 dprintk("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700622 data->cpu_feature = SYSTEM_IO_CAPABLE;
623 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700624 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700625 dprintk("HARDWARE addr space\n");
626 if (!check_est_cpu(cpu)) {
627 result = -ENODEV;
628 goto err_unreg;
629 }
630 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700631 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700632 default:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700633 dprintk("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700634 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700635 result = -ENODEV;
636 goto err_unreg;
637 }
638
Dave Jones95dd7222006-10-18 00:41:48 -0400639 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
640 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!data->freq_table) {
642 result = -ENOMEM;
643 goto err_unreg;
644 }
645
646 /* detect transition latency */
647 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500648 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700649 if ((perf->states[i].transition_latency * 1000) >
650 policy->cpuinfo.transition_latency)
651 policy->cpuinfo.transition_latency =
652 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700655 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
656 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
657 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700658 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700659 printk_once(KERN_INFO
660 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500664 for (i = 0; i < perf->state_count; i++) {
665 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400666 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700667 continue;
668
669 data->freq_table[valid_states].index = i;
670 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700671 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700672 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800674 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800675 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400678 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200681 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
682 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
683
Mattia Dongilia507ac42006-12-15 19:52:45 +0100684 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700685 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700686 /* Current speed is unknown and not detectable by IO port */
687 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
688 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700689 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700690 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100691 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700692 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700693 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700694 break;
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* notify BIOS that we exist */
698 acpi_processor_notify_smm(THIS_MODULE);
699
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700700 /* Check for APERF/MPERF support in hardware */
Peter Zijlstraa8303aa2009-09-02 10:56:56 +0200701 if (cpu_has(c, X86_FEATURE_APERFMPERF))
702 acpi_cpufreq_driver.getavg = get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700703
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700704 dprintk("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500705 for (i = 0; i < perf->state_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700707 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500708 (u32) perf->states[i].core_frequency,
709 (u32) perf->states[i].power,
710 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700713
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400714 /*
715 * the first call to ->target() should result in us actually
716 * writing something to the appropriate registers.
717 */
718 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700719
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700720 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Jones95dd7222006-10-18 00:41:48 -0400722err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400724err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500725 acpi_processor_unregister_performance(perf, cpu);
Dave Jones95dd7222006-10-18 00:41:48 -0400726err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 kfree(data);
travis@sgi.comea348f32008-01-30 13:33:12 +0100728 per_cpu(drv_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700730 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700733static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
travis@sgi.comea348f32008-01-30 13:33:12 +0100735 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dprintk("acpi_cpufreq_cpu_exit\n");
738
739 if (data) {
740 cpufreq_frequency_table_put_attr(policy->cpu);
travis@sgi.comea348f32008-01-30 13:33:12 +0100741 per_cpu(drv_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700742 acpi_processor_unregister_performance(data->acpi_data,
743 policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 kfree(data);
745 }
746
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700750static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
travis@sgi.comea348f32008-01-30 13:33:12 +0100752 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dprintk("acpi_cpufreq_resume\n");
755
756 data->resume = 1;
757
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700761static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 &cpufreq_freq_attr_scaling_available_freqs,
763 NULL,
764};
765
766static struct cpufreq_driver acpi_cpufreq_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100767 .verify = acpi_cpufreq_verify,
768 .target = acpi_cpufreq_target,
769 .bios_limit = acpi_processor_get_bios_limit,
770 .init = acpi_cpufreq_cpu_init,
771 .exit = acpi_cpufreq_cpu_exit,
772 .resume = acpi_cpufreq_resume,
773 .name = "acpi-cpufreq",
774 .owner = THIS_MODULE,
775 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
777
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700778static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Fenghua Yu50109292007-08-07 18:40:30 -0400780 int ret;
781
Yinghai Luee297532008-09-24 19:04:31 -0700782 if (acpi_disabled)
783 return 0;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dprintk("acpi_cpufreq_init\n");
786
Fenghua Yu50109292007-08-07 18:40:30 -0400787 ret = acpi_cpufreq_early_init();
788 if (ret)
789 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500790
Akinobu Mita847aef62008-07-14 11:59:44 +0900791 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
792 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800793 free_acpi_perf_data();
Akinobu Mita847aef62008-07-14 11:59:44 +0900794
795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700798static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 dprintk("acpi_cpufreq_exit\n");
801
802 cpufreq_unregister_driver(&acpi_cpufreq_driver);
803
Fenghua Yu50109292007-08-07 18:40:30 -0400804 free_percpu(acpi_perf_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400807module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700808MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400809 "value 0 or non-zero. non-zero -> strict ACPI checks are "
810 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812late_initcall(acpi_cpufreq_init);
813module_exit(acpi_cpufreq_exit);
814
815MODULE_ALIAS("acpi");