blob: 067a61f06bb5dd075300b6d159104cac03506f9a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.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>
Mark Langsdorfa2fed572010-03-18 18:41:46 +010048#include "mperf.h"
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
51MODULE_DESCRIPTION("ACPI Processor P-States Driver");
52MODULE_LICENSE("GPL");
53
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070054enum {
55 UNDEFINED_CAPABLE = 0,
56 SYSTEM_INTEL_MSR_CAPABLE,
Matthew Garrett3dc9a632012-09-04 08:28:02 +000057 SYSTEM_AMD_MSR_CAPABLE,
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070058 SYSTEM_IO_CAPABLE,
59};
60
61#define INTEL_MSR_RANGE (0xffff)
Matthew Garrett3dc9a632012-09-04 08:28:02 +000062#define AMD_MSR_RANGE (0x7)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070063
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
Fenghua Yu50109292007-08-07 18:40:30 -040073/* acpi_perf_data is a pointer to percpu data. */
Namhyung Kim3f6c4df2010-08-13 23:00:11 +090074static struct acpi_processor_performance __percpu *acpi_perf_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static struct cpufreq_driver acpi_cpufreq_driver;
77
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040078static unsigned int acpi_pstate_strict;
79
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070080static int check_est_cpu(unsigned int cpuid)
81{
Mike Travis92cb7612007-10-19 20:35:04 +020082 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070083
Harald Welte0de51082009-06-08 18:27:54 +080084 return cpu_has(cpu, X86_FEATURE_EST);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070085}
86
Matthew Garrett3dc9a632012-09-04 08:28:02 +000087static int check_amd_hwpstate_cpu(unsigned int cpuid)
88{
89 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
90
91 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
92}
93
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070094static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070095{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070096 struct acpi_processor_performance *perf;
97 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070098
99 perf = data->acpi_data;
100
Dave Jones3a58df32009-01-17 22:36:14 -0500101 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700102 if (value == perf->states[i].status)
103 return data->freq_table[i].frequency;
104 }
105 return 0;
106}
107
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700108static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
109{
110 int i;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700111 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700112
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000113 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
114 msr &= AMD_MSR_RANGE;
115 else
116 msr &= INTEL_MSR_RANGE;
117
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700118 perf = data->acpi_data;
119
Dave Jones3a58df32009-01-17 22:36:14 -0500120 for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700121 if (msr == perf->states[data->freq_table[i].index].status)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700122 return data->freq_table[i].frequency;
123 }
124 return data->freq_table[0].frequency;
125}
126
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700127static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
128{
129 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700130 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000131 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700132 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700133 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700134 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700135 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700136 return 0;
137 }
138}
139
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700140struct msr_addr {
141 u32 reg;
142};
143
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700144struct io_addr {
145 u16 port;
146 u8 bit_width;
147};
148
149struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700150 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100151 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500152 union {
153 struct msr_addr msr;
154 struct io_addr io;
155 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700156 u32 val;
157};
158
Andrew Morton01599fc2009-04-13 10:27:49 -0700159/* Called via smp_call_function_single(), on the target CPU */
160static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700161{
Mike Travis72859082009-01-16 15:31:15 -0800162 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700163 u32 h;
164
165 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700166 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000167 case SYSTEM_AMD_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;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000192 case SYSTEM_AMD_MSR_CAPABLE:
193 wrmsr(cmd->addr.msr.reg, cmd->val, 0);
194 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700195 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800196 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
197 cmd->val,
198 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700199 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700200 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700201 break;
202 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700203}
204
Dave Jones95dd7222006-10-18 00:41:48 -0400205static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700206{
Andrew Morton4a283952009-12-21 16:19:58 -0800207 int err;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700208 cmd->val = 0;
209
Andrew Morton4a283952009-12-21 16:19:58 -0800210 err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
211 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700212}
213
214static void drv_write(struct drv_cmd *cmd)
215{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700216 int this_cpu;
217
218 this_cpu = get_cpu();
219 if (cpumask_test_cpu(this_cpu, cmd->mask))
220 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700221 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700222 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700223}
224
Mike Travis4d8bb532009-01-04 05:18:08 -0800225static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700226{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700227 struct acpi_processor_performance *perf;
228 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700229
Mike Travis4d8bb532009-01-04 05:18:08 -0800230 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700231 return 0;
232
Tejun Heof1625062009-10-29 22:34:13 +0900233 switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700234 case SYSTEM_INTEL_MSR_CAPABLE:
235 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
236 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
237 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000238 case SYSTEM_AMD_MSR_CAPABLE:
239 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
240 cmd.addr.msr.reg = MSR_AMD_PERF_STATUS;
241 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700242 case SYSTEM_IO_CAPABLE:
243 cmd.type = SYSTEM_IO_CAPABLE;
Tejun Heof1625062009-10-29 22:34:13 +0900244 perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700245 cmd.addr.io.port = perf->control_register.address;
246 cmd.addr.io.bit_width = perf->control_register.bit_width;
247 break;
248 default:
249 return 0;
250 }
251
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100252 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700253 drv_read(&cmd);
254
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200255 pr_debug("get_cur_val = %u\n", cmd.val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700256
257 return cmd.val;
258}
259
260static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
261{
Tejun Heof1625062009-10-29 22:34:13 +0900262 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700263 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400264 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700265
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200266 pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700267
268 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700269 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700270 return 0;
271 }
272
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400273 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800274 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400275 if (freq != cached_freq) {
276 /*
277 * The dreaded BIOS frequency change behind our back.
278 * Force set the frequency on next target call.
279 */
280 data->resume = 1;
281 }
282
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200283 pr_debug("cur freq = %u\n", freq);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700284
285 return freq;
286}
287
Mike Travis72859082009-01-16 15:31:15 -0800288static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700289 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700290{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700291 unsigned int cur_freq;
292 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700293
Dave Jones3a58df32009-01-17 22:36:14 -0500294 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700295 cur_freq = extract_freq(get_cur_val(mask), data);
296 if (cur_freq == freq)
297 return 1;
298 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 return 0;
301}
302
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700303static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700304 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Tejun Heof1625062009-10-29 22:34:13 +0900306 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700307 struct acpi_processor_performance *perf;
308 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700309 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800310 unsigned int next_state = 0; /* Index into freq_table */
311 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700312 unsigned int i;
313 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200315 pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700317 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400318 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700319 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500322 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700323 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700324 data->freq_table,
325 target_freq,
326 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800327 if (unlikely(result)) {
328 result = -ENODEV;
329 goto out;
330 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500331
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700332 next_perf_state = data->freq_table[next_state].index;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700333 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700334 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200335 pr_debug("Called after resume, resetting to P%d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700336 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700337 data->resume = 0;
338 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200339 pr_debug("Already at target state (P%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700340 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800341 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700342 }
343 }
344
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700345 switch (data->cpu_feature) {
346 case SYSTEM_INTEL_MSR_CAPABLE:
347 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
348 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700349 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700350 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000351 case SYSTEM_AMD_MSR_CAPABLE:
352 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
353 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
354 cmd.val = (u32) perf->states[next_perf_state].control;
355 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700356 case SYSTEM_IO_CAPABLE:
357 cmd.type = SYSTEM_IO_CAPABLE;
358 cmd.addr.io.port = perf->control_register.address;
359 cmd.addr.io.bit_width = perf->control_register.bit_width;
360 cmd.val = (u32) perf->states[next_perf_state].control;
361 break;
362 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800363 result = -ENODEV;
364 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700365 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700366
Mike Travis4d8bb532009-01-04 05:18:08 -0800367 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700368 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100369 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700370 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100371 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700372
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800373 freqs.old = perf->states[perf->state].core_frequency * 1000;
374 freqs.new = data->freq_table[next_state].frequency;
Thomas Renninger6b72e392010-04-20 13:17:35 +0200375 for_each_cpu(i, policy->cpus) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700376 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500377 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
378 }
379
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700380 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500381
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700382 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800383 if (!check_freqs(cmd.mask, freqs.new, data)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200384 pr_debug("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700385 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800386 result = -EAGAIN;
387 goto out;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500388 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500389 }
390
Thomas Renninger6b72e392010-04-20 13:17:35 +0200391 for_each_cpu(i, policy->cpus) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700392 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500393 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
394 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700395 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500396
Mike Travis4d8bb532009-01-04 05:18:08 -0800397out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700398 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700401static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Tejun Heof1625062009-10-29 22:34:13 +0900403 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200405 pr_debug("acpi_cpufreq_verify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700407 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700411acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700413 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (cpu_khz) {
416 /* search the closest match to cpu_khz */
417 unsigned int i;
418 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500419 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Dave Jones3a58df32009-01-17 22:36:14 -0500421 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400423 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500425 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700426 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
Dave Jones95dd7222006-10-18 00:41:48 -0400429 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700430 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500431 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500433 perf->state = 0;
434 return perf->states[0].core_frequency * 1000;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800438static void free_acpi_perf_data(void)
439{
440 unsigned int i;
441
442 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
443 for_each_possible_cpu(i)
444 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
445 ->shared_cpu_map);
446 free_percpu(acpi_perf_data);
447}
448
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500449/*
450 * acpi_cpufreq_early_init - initialize ACPI P-States library
451 *
452 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
453 * in order to determine correct frequency and voltage pairings. We can
454 * do _PDC and _PSD and find out the processor dependency for the
455 * actual init that will happen later...
456 */
Fenghua Yu50109292007-08-07 18:40:30 -0400457static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500458{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800459 unsigned int i;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200460 pr_debug("acpi_cpufreq_early_init\n");
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500461
Fenghua Yu50109292007-08-07 18:40:30 -0400462 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
463 if (!acpi_perf_data) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200464 pr_debug("Memory allocation error for acpi_perf_data.\n");
Fenghua Yu50109292007-08-07 18:40:30 -0400465 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500466 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800467 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700468 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800469 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
470 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800471
472 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
473 free_acpi_perf_data();
474 return -ENOMEM;
475 }
476 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500477
478 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700479 acpi_processor_preregister_performance(acpi_perf_data);
480 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500481}
482
Dave Jones95625b82006-10-21 01:37:39 -0400483#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700484/*
485 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
486 * or do it in BIOS firmware and won't inform about it to OS. If not
487 * detected, this has a side effect of making CPU run at a different speed
488 * than OS intended it to run at. Detect it and handle it cleanly.
489 */
490static int bios_with_sw_any_bug;
491
Jeff Garzik18552562007-10-03 15:15:40 -0400492static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700493{
494 bios_with_sw_any_bug = 1;
495 return 0;
496}
497
Jeff Garzik18552562007-10-03 15:15:40 -0400498static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700499 {
500 .callback = sw_any_bug_found,
501 .ident = "Supermicro Server X6DLP",
502 .matches = {
503 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
504 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
505 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
506 },
507 },
508 { }
509};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400510
511static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
512{
John Villalovos293afe42009-09-25 13:30:08 -0400513 /* Intel Xeon Processor 7100 Series Specification Update
514 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400515 * AL30: A Machine Check Exception (MCE) Occurring during an
516 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400517 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400518 if (c->x86_vendor == X86_VENDOR_INTEL) {
519 if ((c->x86 == 15) &&
520 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400521 (c->x86_mask == 8)) {
522 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
523 "Xeon(R) 7100 Errata AL30, processors may "
524 "lock up on frequency changes: disabling "
525 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400526 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400527 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400528 }
529 return 0;
530}
Dave Jones95625b82006-10-21 01:37:39 -0400531#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700532
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700533static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700535 unsigned int i;
536 unsigned int valid_states = 0;
537 unsigned int cpu = policy->cpu;
538 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700539 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200540 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700541 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400542#ifdef CONFIG_SMP
543 static int blacklisted;
544#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200546 pr_debug("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400548#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400549 if (blacklisted)
550 return blacklisted;
551 blacklisted = acpi_cpufreq_blacklist(c);
552 if (blacklisted)
553 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400554#endif
555
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700556 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700558 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Rusty Russellb36128c2009-02-20 16:29:08 +0900560 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900561 per_cpu(acfreq_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700562
Dave Jones95dd7222006-10-18 00:41:48 -0400563 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700564 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500566 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (result)
568 goto err_free;
569
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500570 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500571 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400572
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400573 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400574 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400575 * coordination is required.
576 */
577 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700578 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800579 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700580 }
Rusty Russell835481d2009-01-04 05:18:06 -0800581 cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700582
583#ifdef CONFIG_SMP
584 dmi_check_system(sw_any_bug_dmi_table);
Rusty Russell835481d2009-01-04 05:18:06 -0800585 if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700586 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800587 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700588 }
589#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500592 if (perf->state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200593 pr_debug("No P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 result = -ENODEV;
595 goto err_unreg;
596 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500597
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700598 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 result = -ENODEV;
600 goto err_unreg;
601 }
602
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700603 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700604 case ACPI_ADR_SPACE_SYSTEM_IO:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200605 pr_debug("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700606 data->cpu_feature = SYSTEM_IO_CAPABLE;
607 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700608 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200609 pr_debug("HARDWARE addr space\n");
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000610 if (check_est_cpu(cpu)) {
611 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
612 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700613 }
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000614 if (check_amd_hwpstate_cpu(cpu)) {
615 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
616 break;
617 }
618 result = -ENODEV;
619 goto err_unreg;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700620 default:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200621 pr_debug("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700622 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700623 result = -ENODEV;
624 goto err_unreg;
625 }
626
Dave Jones95dd7222006-10-18 00:41:48 -0400627 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
628 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!data->freq_table) {
630 result = -ENOMEM;
631 goto err_unreg;
632 }
633
634 /* detect transition latency */
635 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500636 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700637 if ((perf->states[i].transition_latency * 1000) >
638 policy->cpuinfo.transition_latency)
639 policy->cpuinfo.transition_latency =
640 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700643 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
644 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
645 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700646 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700647 printk_once(KERN_INFO
648 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500652 for (i = 0; i < perf->state_count; i++) {
653 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400654 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700655 continue;
656
657 data->freq_table[valid_states].index = i;
658 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700659 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700660 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800662 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800663 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400666 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200669 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
670 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
671
Mattia Dongilia507ac42006-12-15 19:52:45 +0100672 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700673 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700674 /* Current speed is unknown and not detectable by IO port */
675 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
676 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700677 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700678 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100679 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700680 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700681 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700682 break;
683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* notify BIOS that we exist */
686 acpi_processor_notify_smm(THIS_MODULE);
687
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700688 /* Check for APERF/MPERF support in hardware */
Matthew Garrett92e03c42011-07-13 17:58:32 -0400689 if (boot_cpu_has(X86_FEATURE_APERFMPERF))
Mark Langsdorfa2fed572010-03-18 18:41:46 +0100690 acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700691
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200692 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500693 for (i = 0; i < perf->state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200694 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700695 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500696 (u32) perf->states[i].core_frequency,
697 (u32) perf->states[i].power,
698 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700701
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400702 /*
703 * the first call to ->target() should result in us actually
704 * writing something to the appropriate registers.
705 */
706 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700707
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700708 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dave Jones95dd7222006-10-18 00:41:48 -0400710err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400712err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500713 acpi_processor_unregister_performance(perf, cpu);
Dave Jones95dd7222006-10-18 00:41:48 -0400714err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 kfree(data);
Tejun Heof1625062009-10-29 22:34:13 +0900716 per_cpu(acfreq_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700718 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700721static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Tejun Heof1625062009-10-29 22:34:13 +0900723 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200725 pr_debug("acpi_cpufreq_cpu_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (data) {
728 cpufreq_frequency_table_put_attr(policy->cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900729 per_cpu(acfreq_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700730 acpi_processor_unregister_performance(data->acpi_data,
731 policy->cpu);
Zhang Ruidab5fff2010-10-12 09:09:37 +0800732 kfree(data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 kfree(data);
734 }
735
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700739static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Tejun Heof1625062009-10-29 22:34:13 +0900741 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200743 pr_debug("acpi_cpufreq_resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 data->resume = 1;
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 struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 &cpufreq_freq_attr_scaling_available_freqs,
752 NULL,
753};
754
755static struct cpufreq_driver acpi_cpufreq_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100756 .verify = acpi_cpufreq_verify,
757 .target = acpi_cpufreq_target,
758 .bios_limit = acpi_processor_get_bios_limit,
759 .init = acpi_cpufreq_cpu_init,
760 .exit = acpi_cpufreq_cpu_exit,
761 .resume = acpi_cpufreq_resume,
762 .name = "acpi-cpufreq",
763 .owner = THIS_MODULE,
764 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765};
766
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700767static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Fenghua Yu50109292007-08-07 18:40:30 -0400769 int ret;
770
Yinghai Luee297532008-09-24 19:04:31 -0700771 if (acpi_disabled)
772 return 0;
773
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200774 pr_debug("acpi_cpufreq_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Fenghua Yu50109292007-08-07 18:40:30 -0400776 ret = acpi_cpufreq_early_init();
777 if (ret)
778 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500779
Akinobu Mita847aef62008-07-14 11:59:44 +0900780 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
781 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800782 free_acpi_perf_data();
Akinobu Mita847aef62008-07-14 11:59:44 +0900783
784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700787static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200789 pr_debug("acpi_cpufreq_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 cpufreq_unregister_driver(&acpi_cpufreq_driver);
792
Luming Yu50f4ddd2011-07-08 16:37:44 -0400793 free_acpi_perf_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400796module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700797MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400798 "value 0 or non-zero. non-zero -> strict ACPI checks are "
799 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801late_initcall(acpi_cpufreq_init);
802module_exit(acpi_cpufreq_exit);
803
804MODULE_ALIAS("acpi");