blob: 752e8c6b2c7e29e903895a4abe541b6e186b7add [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>
Frederic Weisbecker12922112009-02-07 22:16:12 +010036#include <trace/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)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -070063#define CPUID_6_ECX_APERFMPERF_CAPABILITY (0x1)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070064
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070065struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070066 struct acpi_processor_performance *acpi_data;
67 struct cpufreq_frequency_table *freq_table;
68 unsigned int resume;
69 unsigned int cpu_feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
travis@sgi.comea348f32008-01-30 13:33:12 +010072static DEFINE_PER_CPU(struct acpi_cpufreq_data *, drv_data);
73
Pallipadi, Venkatesh093f13e2009-04-15 10:37:33 -070074struct acpi_msr_data {
75 u64 saved_aperf, saved_mperf;
76};
77
78static DEFINE_PER_CPU(struct acpi_msr_data, msr_data);
79
Jason Baronb5f9fd02009-02-11 13:57:25 -050080DEFINE_TRACE(power_mark);
81
Fenghua Yu50109292007-08-07 18:40:30 -040082/* acpi_perf_data is a pointer to percpu data. */
83static struct acpi_processor_performance *acpi_perf_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static struct cpufreq_driver acpi_cpufreq_driver;
86
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040087static unsigned int acpi_pstate_strict;
88
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070089static int check_est_cpu(unsigned int cpuid)
90{
Mike Travis92cb7612007-10-19 20:35:04 +020091 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070092
93 if (cpu->x86_vendor != X86_VENDOR_INTEL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070094 !cpu_has(cpu, X86_FEATURE_EST))
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070095 return 0;
96
97 return 1;
98}
99
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700100static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700101{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700102 struct acpi_processor_performance *perf;
103 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700104
105 perf = data->acpi_data;
106
Dave Jones3a58df32009-01-17 22:36:14 -0500107 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700108 if (value == perf->states[i].status)
109 return data->freq_table[i].frequency;
110 }
111 return 0;
112}
113
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700114static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
115{
116 int i;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700117 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700118
119 msr &= INTEL_MSR_RANGE;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700120 perf = data->acpi_data;
121
Dave Jones3a58df32009-01-17 22:36:14 -0500122 for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700123 if (msr == perf->states[data->freq_table[i].index].status)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700124 return data->freq_table[i].frequency;
125 }
126 return data->freq_table[0].frequency;
127}
128
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700129static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
130{
131 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700132 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700133 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700134 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700135 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700136 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700137 return 0;
138 }
139}
140
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700141struct msr_addr {
142 u32 reg;
143};
144
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700145struct io_addr {
146 u16 port;
147 u8 bit_width;
148};
149
150struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700151 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100152 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500153 union {
154 struct msr_addr msr;
155 struct io_addr io;
156 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700157 u32 val;
158};
159
Andrew Morton01599fc2009-04-13 10:27:49 -0700160/* Called via smp_call_function_single(), on the target CPU */
161static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700162{
Mike Travis72859082009-01-16 15:31:15 -0800163 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700164 u32 h;
165
166 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700167 case SYSTEM_INTEL_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700168 rdmsr(cmd->addr.msr.reg, cmd->val, h);
169 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700170 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800171 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
172 &cmd->val,
173 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700174 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700175 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700176 break;
177 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700178}
179
Andrew Morton01599fc2009-04-13 10:27:49 -0700180/* Called via smp_call_function_many(), on the target CPUs */
181static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700182{
Mike Travis72859082009-01-16 15:31:15 -0800183 struct drv_cmd *cmd = _cmd;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700184 u32 lo, hi;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700185
186 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700187 case SYSTEM_INTEL_MSR_CAPABLE:
Venki Pallipadi13424f62007-05-23 15:42:13 -0700188 rdmsr(cmd->addr.msr.reg, lo, hi);
189 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
190 wrmsr(cmd->addr.msr.reg, lo, hi);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700191 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700192 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800193 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
194 cmd->val,
195 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700196 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700197 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700198 break;
199 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700200}
201
Dave Jones95dd7222006-10-18 00:41:48 -0400202static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700203{
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700204 cmd->val = 0;
205
Andrew Morton01599fc2009-04-13 10:27:49 -0700206 smp_call_function_single(cpumask_any(cmd->mask), do_drv_read, cmd, 1);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700207}
208
209static void drv_write(struct drv_cmd *cmd)
210{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700211 int this_cpu;
212
213 this_cpu = get_cpu();
214 if (cpumask_test_cpu(this_cpu, cmd->mask))
215 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700216 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700217 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700218}
219
Mike Travis4d8bb532009-01-04 05:18:08 -0800220static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700221{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700222 struct acpi_processor_performance *perf;
223 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700224
Mike Travis4d8bb532009-01-04 05:18:08 -0800225 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700226 return 0;
227
Mike Travis4d8bb532009-01-04 05:18:08 -0800228 switch (per_cpu(drv_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700229 case SYSTEM_INTEL_MSR_CAPABLE:
230 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
231 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
232 break;
233 case SYSTEM_IO_CAPABLE:
234 cmd.type = SYSTEM_IO_CAPABLE;
Mike Travis4d8bb532009-01-04 05:18:08 -0800235 perf = per_cpu(drv_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700236 cmd.addr.io.port = perf->control_register.address;
237 cmd.addr.io.bit_width = perf->control_register.bit_width;
238 break;
239 default:
240 return 0;
241 }
242
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100243 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700244 drv_read(&cmd);
245
246 dprintk("get_cur_val = %u\n", cmd.val);
247
248 return cmd.val;
249}
250
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700251struct perf_pair {
Mike Travise39ad412009-01-04 05:18:10 -0800252 union {
253 struct {
254 u32 lo;
255 u32 hi;
256 } split;
257 u64 whole;
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700258 } aperf, mperf;
Mike Travise39ad412009-01-04 05:18:10 -0800259};
260
Andrew Morton01599fc2009-04-13 10:27:49 -0700261/* Called via smp_call_function_single(), on the target CPU */
262static void read_measured_perf_ctrs(void *_cur)
Mike Travise39ad412009-01-04 05:18:10 -0800263{
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700264 struct perf_pair *cur = _cur;
Mike Travise39ad412009-01-04 05:18:10 -0800265
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700266 rdmsr(MSR_IA32_APERF, cur->aperf.split.lo, cur->aperf.split.hi);
267 rdmsr(MSR_IA32_MPERF, cur->mperf.split.lo, cur->mperf.split.hi);
Mike Travise39ad412009-01-04 05:18:10 -0800268}
269
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700270/*
271 * Return the measured active (C0) frequency on this CPU since last call
272 * to this function.
273 * Input: cpu number
274 * Return: Average CPU frequency in terms of max frequency (zero on error)
275 *
276 * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
277 * over a period of time, while CPU is in C0 state.
278 * IA32_MPERF counts at the rate of max advertised frequency
279 * IA32_APERF counts at the rate of actual CPU frequency
280 * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
281 * no meaning should be associated with absolute values of these MSRs.
282 */
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -0700283static unsigned int get_measured_perf(struct cpufreq_policy *policy,
284 unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700285{
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700286 struct perf_pair readin, cur;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700287 unsigned int perf_percent;
288 unsigned int retval;
289
Linus Torvalds1c98aa72009-04-13 18:09:20 -0700290 if (smp_call_function_single(cpu, read_measured_perf_ctrs, &readin, 1))
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700291 return 0;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700292
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700293 cur.aperf.whole = readin.aperf.whole -
Pallipadi, Venkatesh093f13e2009-04-15 10:37:33 -0700294 per_cpu(msr_data, cpu).saved_aperf;
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700295 cur.mperf.whole = readin.mperf.whole -
Pallipadi, Venkatesh093f13e2009-04-15 10:37:33 -0700296 per_cpu(msr_data, cpu).saved_mperf;
297 per_cpu(msr_data, cpu).saved_aperf = readin.aperf.whole;
298 per_cpu(msr_data, cpu).saved_mperf = readin.mperf.whole;
Venkatesh Pallipadi18b26462009-04-06 11:26:08 -0700299
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700300#ifdef __i386__
301 /*
302 * We dont want to do 64 bit divide with 32 bit kernel
303 * Get an approximate value. Return failure in case we cannot get
304 * an approximate value.
305 */
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700306 if (unlikely(cur.aperf.split.hi || cur.mperf.split.hi)) {
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700307 int shift_count;
308 u32 h;
309
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700310 h = max_t(u32, cur.aperf.split.hi, cur.mperf.split.hi);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700311 shift_count = fls(h);
312
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700313 cur.aperf.whole >>= shift_count;
314 cur.mperf.whole >>= shift_count;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700315 }
316
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700317 if (((unsigned long)(-1) / 100) < cur.aperf.split.lo) {
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700318 int shift_count = 7;
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700319 cur.aperf.split.lo >>= shift_count;
320 cur.mperf.split.lo >>= shift_count;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700321 }
322
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700323 if (cur.aperf.split.lo && cur.mperf.split.lo)
324 perf_percent = (cur.aperf.split.lo * 100) / cur.mperf.split.lo;
Dave Jones95dd7222006-10-18 00:41:48 -0400325 else
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700326 perf_percent = 0;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700327
328#else
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700329 if (unlikely(((unsigned long)(-1) / 100) < cur.aperf.whole)) {
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700330 int shift_count = 7;
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700331 cur.aperf.whole >>= shift_count;
332 cur.mperf.whole >>= shift_count;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700333 }
334
Venkatesh Pallipadie4f69372009-04-06 11:26:07 -0700335 if (cur.aperf.whole && cur.mperf.whole)
336 perf_percent = (cur.aperf.whole * 100) / cur.mperf.whole;
Dave Jones95dd7222006-10-18 00:41:48 -0400337 else
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700338 perf_percent = 0;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700339
340#endif
341
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200342 retval = (policy->cpuinfo.max_freq * perf_percent) / 100;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700343
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700344 return retval;
345}
346
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700347static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
348{
travis@sgi.comea348f32008-01-30 13:33:12 +0100349 struct acpi_cpufreq_data *data = per_cpu(drv_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700350 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400351 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700352
353 dprintk("get_cur_freq_on_cpu (%d)\n", cpu);
354
355 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700356 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700357 return 0;
358 }
359
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400360 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800361 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400362 if (freq != cached_freq) {
363 /*
364 * The dreaded BIOS frequency change behind our back.
365 * Force set the frequency on next target call.
366 */
367 data->resume = 1;
368 }
369
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700370 dprintk("cur freq = %u\n", freq);
371
372 return freq;
373}
374
Mike Travis72859082009-01-16 15:31:15 -0800375static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700376 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700377{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700378 unsigned int cur_freq;
379 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700380
Dave Jones3a58df32009-01-17 22:36:14 -0500381 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700382 cur_freq = extract_freq(get_cur_val(mask), data);
383 if (cur_freq == freq)
384 return 1;
385 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 return 0;
388}
389
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700390static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700391 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
travis@sgi.comea348f32008-01-30 13:33:12 +0100393 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700394 struct acpi_processor_performance *perf;
395 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700396 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800397 unsigned int next_state = 0; /* Index into freq_table */
398 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700399 unsigned int i;
400 int result = 0;
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800401 struct power_trace it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700403 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700405 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400406 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700407 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500410 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700411 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700412 data->freq_table,
413 target_freq,
414 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800415 if (unlikely(result)) {
416 result = -ENODEV;
417 goto out;
418 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500419
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700420 next_perf_state = data->freq_table[next_state].index;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700421 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700422 if (unlikely(data->resume)) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700423 dprintk("Called after resume, resetting to P%d\n",
424 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700425 data->resume = 0;
426 } else {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700427 dprintk("Already at target state (P%d)\n",
428 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800429 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700430 }
431 }
432
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800433 trace_power_mark(&it, POWER_PSTATE, next_perf_state);
434
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700435 switch (data->cpu_feature) {
436 case SYSTEM_INTEL_MSR_CAPABLE:
437 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
438 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700439 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700440 break;
441 case SYSTEM_IO_CAPABLE:
442 cmd.type = SYSTEM_IO_CAPABLE;
443 cmd.addr.io.port = perf->control_register.address;
444 cmd.addr.io.bit_width = perf->control_register.bit_width;
445 cmd.val = (u32) perf->states[next_perf_state].control;
446 break;
447 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800448 result = -ENODEV;
449 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700450 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700451
Mike Travis4d8bb532009-01-04 05:18:08 -0800452 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700453 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100454 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700455 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100456 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700457
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800458 freqs.old = perf->states[perf->state].core_frequency * 1000;
459 freqs.new = data->freq_table[next_state].frequency;
Mike Travis4d8bb532009-01-04 05:18:08 -0800460 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700461 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500462 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
463 }
464
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700465 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500466
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700467 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800468 if (!check_freqs(cmd.mask, freqs.new, data)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700469 dprintk("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700470 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800471 result = -EAGAIN;
472 goto out;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500473 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500474 }
475
Mike Travis4d8bb532009-01-04 05:18:08 -0800476 for_each_cpu(i, cmd.mask) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700477 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500478 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
479 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700480 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500481
Mike Travis4d8bb532009-01-04 05:18:08 -0800482out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700483 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700486static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
travis@sgi.comea348f32008-01-30 13:33:12 +0100488 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 dprintk("acpi_cpufreq_verify\n");
491
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700492 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700496acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700498 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (cpu_khz) {
501 /* search the closest match to cpu_khz */
502 unsigned int i;
503 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500504 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Dave Jones3a58df32009-01-17 22:36:14 -0500506 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400508 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500510 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700511 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
Dave Jones95dd7222006-10-18 00:41:48 -0400514 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700515 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500518 perf->state = 0;
519 return perf->states[0].core_frequency * 1000;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800523static void free_acpi_perf_data(void)
524{
525 unsigned int i;
526
527 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
528 for_each_possible_cpu(i)
529 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
530 ->shared_cpu_map);
531 free_percpu(acpi_perf_data);
532}
533
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500534/*
535 * acpi_cpufreq_early_init - initialize ACPI P-States library
536 *
537 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
538 * in order to determine correct frequency and voltage pairings. We can
539 * do _PDC and _PSD and find out the processor dependency for the
540 * actual init that will happen later...
541 */
Fenghua Yu50109292007-08-07 18:40:30 -0400542static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500543{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800544 unsigned int i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500545 dprintk("acpi_cpufreq_early_init\n");
546
Fenghua Yu50109292007-08-07 18:40:30 -0400547 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
548 if (!acpi_perf_data) {
549 dprintk("Memory allocation error for acpi_perf_data.\n");
550 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500551 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800552 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700553 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800554 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
555 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800556
557 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
558 free_acpi_perf_data();
559 return -ENOMEM;
560 }
561 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500562
563 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700564 acpi_processor_preregister_performance(acpi_perf_data);
565 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500566}
567
Dave Jones95625b82006-10-21 01:37:39 -0400568#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700569/*
570 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
571 * or do it in BIOS firmware and won't inform about it to OS. If not
572 * detected, this has a side effect of making CPU run at a different speed
573 * than OS intended it to run at. Detect it and handle it cleanly.
574 */
575static int bios_with_sw_any_bug;
576
Jeff Garzik18552562007-10-03 15:15:40 -0400577static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700578{
579 bios_with_sw_any_bug = 1;
580 return 0;
581}
582
Jeff Garzik18552562007-10-03 15:15:40 -0400583static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700584 {
585 .callback = sw_any_bug_found,
586 .ident = "Supermicro Server X6DLP",
587 .matches = {
588 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
589 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
590 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
591 },
592 },
593 { }
594};
Dave Jones95625b82006-10-21 01:37:39 -0400595#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700596
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700597static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700599 unsigned int i;
600 unsigned int valid_states = 0;
601 unsigned int cpu = policy->cpu;
602 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700603 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200604 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700605 struct acpi_processor_performance *perf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 dprintk("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700609 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700611 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Rusty Russellb36128c2009-02-20 16:29:08 +0900613 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
travis@sgi.comea348f32008-01-30 13:33:12 +0100614 per_cpu(drv_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700615
Dave Jones95dd7222006-10-18 00:41:48 -0400616 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700617 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500619 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (result)
621 goto err_free;
622
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500623 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500624 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400625
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400626 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400627 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400628 * coordination is required.
629 */
630 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700631 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800632 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700633 }
Rusty Russell835481d2009-01-04 05:18:06 -0800634 cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700635
636#ifdef CONFIG_SMP
637 dmi_check_system(sw_any_bug_dmi_table);
Rusty Russell835481d2009-01-04 05:18:06 -0800638 if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700639 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800640 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700641 }
642#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500645 if (perf->state_count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dprintk("No P-States\n");
647 result = -ENODEV;
648 goto err_unreg;
649 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500650
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700651 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 result = -ENODEV;
653 goto err_unreg;
654 }
655
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700656 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700657 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700658 dprintk("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700659 data->cpu_feature = SYSTEM_IO_CAPABLE;
660 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700661 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700662 dprintk("HARDWARE addr space\n");
663 if (!check_est_cpu(cpu)) {
664 result = -ENODEV;
665 goto err_unreg;
666 }
667 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700668 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700669 default:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700670 dprintk("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700671 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700672 result = -ENODEV;
673 goto err_unreg;
674 }
675
Dave Jones95dd7222006-10-18 00:41:48 -0400676 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
677 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!data->freq_table) {
679 result = -ENOMEM;
680 goto err_unreg;
681 }
682
683 /* detect transition latency */
684 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500685 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700686 if ((perf->states[i].transition_latency * 1000) >
687 policy->cpuinfo.transition_latency)
688 policy->cpuinfo.transition_latency =
689 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700692 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
693 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
694 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700695 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700696 printk_once(KERN_INFO
697 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500701 for (i = 0; i < perf->state_count; i++) {
702 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400703 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700704 continue;
705
706 data->freq_table[valid_states].index = i;
707 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700708 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700709 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800711 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800712 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400715 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200718 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
719 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
720
Mattia Dongilia507ac42006-12-15 19:52:45 +0100721 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700722 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700723 /* Current speed is unknown and not detectable by IO port */
724 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
725 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700726 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700727 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100728 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700729 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700730 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700731 break;
732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* notify BIOS that we exist */
735 acpi_processor_notify_smm(THIS_MODULE);
736
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700737 /* Check for APERF/MPERF support in hardware */
738 if (c->x86_vendor == X86_VENDOR_INTEL && c->cpuid_level >= 6) {
739 unsigned int ecx;
740 ecx = cpuid_ecx(6);
Dave Jones95dd7222006-10-18 00:41:48 -0400741 if (ecx & CPUID_6_ECX_APERFMPERF_CAPABILITY)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700742 acpi_cpufreq_driver.getavg = get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700743 }
744
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700745 dprintk("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500746 for (i = 0; i < perf->state_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700748 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500749 (u32) perf->states[i].core_frequency,
750 (u32) perf->states[i].power,
751 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700754
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400755 /*
756 * the first call to ->target() should result in us actually
757 * writing something to the appropriate registers.
758 */
759 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700760
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700761 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Jones95dd7222006-10-18 00:41:48 -0400763err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400765err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500766 acpi_processor_unregister_performance(perf, cpu);
Dave Jones95dd7222006-10-18 00:41:48 -0400767err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kfree(data);
travis@sgi.comea348f32008-01-30 13:33:12 +0100769 per_cpu(drv_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700771 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700774static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
travis@sgi.comea348f32008-01-30 13:33:12 +0100776 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 dprintk("acpi_cpufreq_cpu_exit\n");
779
780 if (data) {
781 cpufreq_frequency_table_put_attr(policy->cpu);
travis@sgi.comea348f32008-01-30 13:33:12 +0100782 per_cpu(drv_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700783 acpi_processor_unregister_performance(data->acpi_data,
784 policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 kfree(data);
786 }
787
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700791static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
travis@sgi.comea348f32008-01-30 13:33:12 +0100793 struct acpi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dprintk("acpi_cpufreq_resume\n");
796
797 data->resume = 1;
798
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700802static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 &cpufreq_freq_attr_scaling_available_freqs,
804 NULL,
805};
806
807static struct cpufreq_driver acpi_cpufreq_driver = {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700808 .verify = acpi_cpufreq_verify,
809 .target = acpi_cpufreq_target,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700810 .init = acpi_cpufreq_cpu_init,
811 .exit = acpi_cpufreq_cpu_exit,
812 .resume = acpi_cpufreq_resume,
813 .name = "acpi-cpufreq",
814 .owner = THIS_MODULE,
815 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816};
817
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700818static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Fenghua Yu50109292007-08-07 18:40:30 -0400820 int ret;
821
Yinghai Luee297532008-09-24 19:04:31 -0700822 if (acpi_disabled)
823 return 0;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 dprintk("acpi_cpufreq_init\n");
826
Fenghua Yu50109292007-08-07 18:40:30 -0400827 ret = acpi_cpufreq_early_init();
828 if (ret)
829 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500830
Akinobu Mita847aef62008-07-14 11:59:44 +0900831 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
832 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800833 free_acpi_perf_data();
Akinobu Mita847aef62008-07-14 11:59:44 +0900834
835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700838static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 dprintk("acpi_cpufreq_exit\n");
841
842 cpufreq_unregister_driver(&acpi_cpufreq_driver);
843
Fenghua Yu50109292007-08-07 18:40:30 -0400844 free_percpu(acpi_perf_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400847module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700848MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400849 "value 0 or non-zero. non-zero -> strict ACPI checks are "
850 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852late_initcall(acpi_cpufreq_init);
853module_exit(acpi_cpufreq_exit);
854
855MODULE_ALIAS("acpi");