blob: 1b1920fa7c8085934cbd846e9329cc538c25be4b [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
Tejun Heof1625062009-10-29 22:34:13 +090071static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
travis@sgi.comea348f32008-01-30 13:33:12 +010072
Tejun Heof1625062009-10-29 22:34:13 +090073static DEFINE_PER_CPU(struct aperfmperf, acfreq_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{
Andrew Morton4a283952009-12-21 16:19:58 -0800193 int err;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700194 cmd->val = 0;
195
Andrew Morton4a283952009-12-21 16:19:58 -0800196 err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
197 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700198}
199
200static void drv_write(struct drv_cmd *cmd)
201{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700202 int this_cpu;
203
204 this_cpu = get_cpu();
205 if (cpumask_test_cpu(this_cpu, cmd->mask))
206 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700207 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700208 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700209}
210
Mike Travis4d8bb532009-01-04 05:18:08 -0800211static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700212{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700213 struct acpi_processor_performance *perf;
214 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700215
Mike Travis4d8bb532009-01-04 05:18:08 -0800216 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700217 return 0;
218
Tejun Heof1625062009-10-29 22:34:13 +0900219 switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700220 case SYSTEM_INTEL_MSR_CAPABLE:
221 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
222 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
223 break;
224 case SYSTEM_IO_CAPABLE:
225 cmd.type = SYSTEM_IO_CAPABLE;
Tejun Heof1625062009-10-29 22:34:13 +0900226 perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700227 cmd.addr.io.port = perf->control_register.address;
228 cmd.addr.io.bit_width = perf->control_register.bit_width;
229 break;
230 default:
231 return 0;
232 }
233
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100234 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700235 drv_read(&cmd);
236
237 dprintk("get_cur_val = %u\n", cmd.val);
238
239 return cmd.val;
240}
241
Andrew Morton01599fc2009-04-13 10:27:49 -0700242/* Called via smp_call_function_single(), on the target CPU */
243static void read_measured_perf_ctrs(void *_cur)
Mike Travise39ad412009-01-04 05:18:10 -0800244{
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200245 struct aperfmperf *am = _cur;
Mike Travise39ad412009-01-04 05:18:10 -0800246
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200247 get_aperfmperf(am);
Mike Travise39ad412009-01-04 05:18:10 -0800248}
249
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700250/*
251 * Return the measured active (C0) frequency on this CPU since last call
252 * to this function.
253 * Input: cpu number
254 * Return: Average CPU frequency in terms of max frequency (zero on error)
255 *
256 * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
257 * over a period of time, while CPU is in C0 state.
258 * IA32_MPERF counts at the rate of max advertised frequency
259 * IA32_APERF counts at the rate of actual CPU frequency
260 * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
261 * no meaning should be associated with absolute values of these MSRs.
262 */
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -0700263static unsigned int get_measured_perf(struct cpufreq_policy *policy,
264 unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700265{
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200266 struct aperfmperf perf;
267 unsigned long ratio;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700268 unsigned int retval;
269
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200270 if (smp_call_function_single(cpu, read_measured_perf_ctrs, &perf, 1))
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700271 return 0;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700272
Tejun Heof1625062009-10-29 22:34:13 +0900273 ratio = calc_aperfmperf_ratio(&per_cpu(acfreq_old_perf, cpu), &perf);
274 per_cpu(acfreq_old_perf, cpu) = perf;
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700275
Peter Zijlstra5cbc19a2009-09-02 11:49:52 +0200276 retval = (policy->cpuinfo.max_freq * ratio) >> APERFMPERF_SHIFT;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700277
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700278 return retval;
279}
280
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700281static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
282{
Tejun Heof1625062009-10-29 22:34:13 +0900283 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700284 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400285 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700286
287 dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
288
289 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700290 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700291 return 0;
292 }
293
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400294 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800295 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400296 if (freq != cached_freq) {
297 /*
298 * The dreaded BIOS frequency change behind our back.
299 * Force set the frequency on next target call.
300 */
301 data->resume = 1;
302 }
303
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700304 dprintk("cur freq = %u\n", freq);
305
306 return freq;
307}
308
Mike Travis72859082009-01-16 15:31:15 -0800309static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700310 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700311{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700312 unsigned int cur_freq;
313 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700314
Dave Jones3a58df32009-01-17 22:36:14 -0500315 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700316 cur_freq = extract_freq(get_cur_val(mask), data);
317 if (cur_freq == freq)
318 return 1;
319 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 return 0;
322}
323
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700324static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700325 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Tejun Heof1625062009-10-29 22:34:13 +0900327 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700328 struct acpi_processor_performance *perf;
329 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700330 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800331 unsigned int next_state = 0; /* Index into freq_table */
332 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700333 unsigned int i;
334 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700336 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700338 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400339 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700340 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500343 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700344 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700345 data->freq_table,
346 target_freq,
347 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800348 if (unlikely(result)) {
349 result = -ENODEV;
350 goto out;
351 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500352
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700353 next_perf_state = data->freq_table[next_state].index;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700354 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700355 if (unlikely(data->resume)) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700356 dprintk("Called after resume, resetting to P%d\n",
357 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700358 data->resume = 0;
359 } else {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700360 dprintk("Already at target state (P%d)\n",
361 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800362 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700363 }
364 }
365
Arjan van de Ven61613522009-09-17 16:11:28 +0200366 trace_power_frequency(POWER_PSTATE, data->freq_table[next_state].frequency);
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800367
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700368 switch (data->cpu_feature) {
369 case SYSTEM_INTEL_MSR_CAPABLE:
370 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
371 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700372 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700373 break;
374 case SYSTEM_IO_CAPABLE:
375 cmd.type = SYSTEM_IO_CAPABLE;
376 cmd.addr.io.port = perf->control_register.address;
377 cmd.addr.io.bit_width = perf->control_register.bit_width;
378 cmd.val = (u32) perf->states[next_perf_state].control;
379 break;
380 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800381 result = -ENODEV;
382 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700383 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700384
Mike Travis4d8bb532009-01-04 05:18:08 -0800385 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700386 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100387 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700388 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100389 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700390
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800391 freqs.old = perf->states[perf->state].core_frequency * 1000;
392 freqs.new = data->freq_table[next_state].frequency;
Mike Travis4d8bb532009-01-04 05:18:08 -0800393 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700394 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500395 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
396 }
397
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700398 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500399
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700400 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800401 if (!check_freqs(cmd.mask, freqs.new, data)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700402 dprintk("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700403 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800404 result = -EAGAIN;
405 goto out;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500406 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500407 }
408
Mike Travis4d8bb532009-01-04 05:18:08 -0800409 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700410 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500411 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
412 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700413 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500414
Mike Travis4d8bb532009-01-04 05:18:08 -0800415out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700416 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700419static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Tejun Heof1625062009-10-29 22:34:13 +0900421 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 dprintk("acpi_cpufreq_verify\n");
424
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700425 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700429acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700431 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (cpu_khz) {
434 /* search the closest match to cpu_khz */
435 unsigned int i;
436 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500437 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dave Jones3a58df32009-01-17 22:36:14 -0500439 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400441 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500443 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700444 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
Dave Jones95dd7222006-10-18 00:41:48 -0400447 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700448 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500449 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500451 perf->state = 0;
452 return perf->states[0].core_frequency * 1000;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800456static void free_acpi_perf_data(void)
457{
458 unsigned int i;
459
460 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
461 for_each_possible_cpu(i)
462 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
463 ->shared_cpu_map);
464 free_percpu(acpi_perf_data);
465}
466
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500467/*
468 * acpi_cpufreq_early_init - initialize ACPI P-States library
469 *
470 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
471 * in order to determine correct frequency and voltage pairings. We can
472 * do _PDC and _PSD and find out the processor dependency for the
473 * actual init that will happen later...
474 */
Fenghua Yu50109292007-08-07 18:40:30 -0400475static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500476{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800477 unsigned int i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500478 dprintk("acpi_cpufreq_early_init\n");
479
Fenghua Yu50109292007-08-07 18:40:30 -0400480 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
481 if (!acpi_perf_data) {
482 dprintk("Memory allocation error for acpi_perf_data.\n");
483 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500484 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800485 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700486 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800487 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
488 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800489
490 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
491 free_acpi_perf_data();
492 return -ENOMEM;
493 }
494 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500495
496 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700497 acpi_processor_preregister_performance(acpi_perf_data);
498 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500499}
500
Dave Jones95625b82006-10-21 01:37:39 -0400501#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700502/*
503 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
504 * or do it in BIOS firmware and won't inform about it to OS. If not
505 * detected, this has a side effect of making CPU run at a different speed
506 * than OS intended it to run at. Detect it and handle it cleanly.
507 */
508static int bios_with_sw_any_bug;
509
Jeff Garzik18552562007-10-03 15:15:40 -0400510static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700511{
512 bios_with_sw_any_bug = 1;
513 return 0;
514}
515
Jeff Garzik18552562007-10-03 15:15:40 -0400516static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700517 {
518 .callback = sw_any_bug_found,
519 .ident = "Supermicro Server X6DLP",
520 .matches = {
521 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
522 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
523 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
524 },
525 },
526 { }
527};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400528
529static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
530{
John Villalovos293afe42009-09-25 13:30:08 -0400531 /* Intel Xeon Processor 7100 Series Specification Update
532 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400533 * AL30: A Machine Check Exception (MCE) Occurring during an
534 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400535 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400536 if (c->x86_vendor == X86_VENDOR_INTEL) {
537 if ((c->x86 == 15) &&
538 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400539 (c->x86_mask == 8)) {
540 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
541 "Xeon(R) 7100 Errata AL30, processors may "
542 "lock up on frequency changes: disabling "
543 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400544 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400545 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400546 }
547 return 0;
548}
Dave Jones95625b82006-10-21 01:37:39 -0400549#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700550
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700551static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700553 unsigned int i;
554 unsigned int valid_states = 0;
555 unsigned int cpu = policy->cpu;
556 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700557 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200558 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700559 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400560#ifdef CONFIG_SMP
561 static int blacklisted;
562#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 dprintk("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400566#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400567 if (blacklisted)
568 return blacklisted;
569 blacklisted = acpi_cpufreq_blacklist(c);
570 if (blacklisted)
571 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400572#endif
573
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700574 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700576 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Rusty Russellb36128c2009-02-20 16:29:08 +0900578 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900579 per_cpu(acfreq_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700580
Dave Jones95dd7222006-10-18 00:41:48 -0400581 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700582 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500584 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (result)
586 goto err_free;
587
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500588 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500589 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400590
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400591 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400592 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400593 * coordination is required.
594 */
595 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700596 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800597 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700598 }
Rusty Russell835481d2009-01-04 05:18:06 -0800599 cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700600
601#ifdef CONFIG_SMP
602 dmi_check_system(sw_any_bug_dmi_table);
Rusty Russell835481d2009-01-04 05:18:06 -0800603 if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700604 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800605 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700606 }
607#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500610 if (perf->state_count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 dprintk("No P-States\n");
612 result = -ENODEV;
613 goto err_unreg;
614 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500615
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700616 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 result = -ENODEV;
618 goto err_unreg;
619 }
620
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700621 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700622 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700623 dprintk("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700624 data->cpu_feature = SYSTEM_IO_CAPABLE;
625 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700626 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700627 dprintk("HARDWARE addr space\n");
628 if (!check_est_cpu(cpu)) {
629 result = -ENODEV;
630 goto err_unreg;
631 }
632 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700633 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700634 default:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700635 dprintk("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700636 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700637 result = -ENODEV;
638 goto err_unreg;
639 }
640
Dave Jones95dd7222006-10-18 00:41:48 -0400641 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
642 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!data->freq_table) {
644 result = -ENOMEM;
645 goto err_unreg;
646 }
647
648 /* detect transition latency */
649 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500650 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700651 if ((perf->states[i].transition_latency * 1000) >
652 policy->cpuinfo.transition_latency)
653 policy->cpuinfo.transition_latency =
654 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700657 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
658 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
659 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700660 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700661 printk_once(KERN_INFO
662 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700663 }
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500666 for (i = 0; i < perf->state_count; i++) {
667 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400668 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700669 continue;
670
671 data->freq_table[valid_states].index = i;
672 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700673 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700674 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800676 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800677 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400680 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200683 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
684 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
685
Mattia Dongilia507ac42006-12-15 19:52:45 +0100686 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700687 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700688 /* Current speed is unknown and not detectable by IO port */
689 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
690 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700691 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700692 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100693 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700694 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700695 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700696 break;
697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* notify BIOS that we exist */
700 acpi_processor_notify_smm(THIS_MODULE);
701
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700702 /* Check for APERF/MPERF support in hardware */
Peter Zijlstraa8303aa2009-09-02 10:56:56 +0200703 if (cpu_has(c, X86_FEATURE_APERFMPERF))
704 acpi_cpufreq_driver.getavg = get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700705
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700706 dprintk("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500707 for (i = 0; i < perf->state_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700709 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500710 (u32) perf->states[i].core_frequency,
711 (u32) perf->states[i].power,
712 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700715
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400716 /*
717 * the first call to ->target() should result in us actually
718 * writing something to the appropriate registers.
719 */
720 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700721
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700722 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Jones95dd7222006-10-18 00:41:48 -0400724err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400726err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500727 acpi_processor_unregister_performance(perf, cpu);
Dave Jones95dd7222006-10-18 00:41:48 -0400728err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 kfree(data);
Tejun Heof1625062009-10-29 22:34:13 +0900730 per_cpu(acfreq_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700732 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700735static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Tejun Heof1625062009-10-29 22:34:13 +0900737 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 dprintk("acpi_cpufreq_cpu_exit\n");
740
741 if (data) {
742 cpufreq_frequency_table_put_attr(policy->cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900743 per_cpu(acfreq_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700744 acpi_processor_unregister_performance(data->acpi_data,
745 policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 kfree(data);
747 }
748
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700752static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Tejun Heof1625062009-10-29 22:34:13 +0900754 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dprintk("acpi_cpufreq_resume\n");
757
758 data->resume = 1;
759
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700763static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 &cpufreq_freq_attr_scaling_available_freqs,
765 NULL,
766};
767
768static struct cpufreq_driver acpi_cpufreq_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100769 .verify = acpi_cpufreq_verify,
770 .target = acpi_cpufreq_target,
771 .bios_limit = acpi_processor_get_bios_limit,
772 .init = acpi_cpufreq_cpu_init,
773 .exit = acpi_cpufreq_cpu_exit,
774 .resume = acpi_cpufreq_resume,
775 .name = "acpi-cpufreq",
776 .owner = THIS_MODULE,
777 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778};
779
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700780static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Fenghua Yu50109292007-08-07 18:40:30 -0400782 int ret;
783
Yinghai Luee297532008-09-24 19:04:31 -0700784 if (acpi_disabled)
785 return 0;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dprintk("acpi_cpufreq_init\n");
788
Fenghua Yu50109292007-08-07 18:40:30 -0400789 ret = acpi_cpufreq_early_init();
790 if (ret)
791 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500792
Akinobu Mita847aef62008-07-14 11:59:44 +0900793 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
794 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800795 free_acpi_perf_data();
Akinobu Mita847aef62008-07-14 11:59:44 +0900796
797 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700800static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 dprintk("acpi_cpufreq_exit\n");
803
804 cpufreq_unregister_driver(&acpi_cpufreq_driver);
805
Fenghua Yu50109292007-08-07 18:40:30 -0400806 free_percpu(acpi_perf_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400809module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700810MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400811 "value 0 or non-zero. non-zero -> strict ACPI checks are "
812 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814late_initcall(acpi_cpufreq_init);
815module_exit(acpi_cpufreq_exit);
816
817MODULE_ALIAS("acpi");