blob: 70e717305c2957f27f401d80555c9ae60e93531f [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
Andre Przywaraacd31622012-09-04 08:28:03 +000054#define PFX "acpi-cpufreq: "
55
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070056enum {
57 UNDEFINED_CAPABLE = 0,
58 SYSTEM_INTEL_MSR_CAPABLE,
Matthew Garrett3dc9a632012-09-04 08:28:02 +000059 SYSTEM_AMD_MSR_CAPABLE,
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070060 SYSTEM_IO_CAPABLE,
61};
62
63#define INTEL_MSR_RANGE (0xffff)
Matthew Garrett3dc9a632012-09-04 08:28:02 +000064#define AMD_MSR_RANGE (0x7)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070065
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070066struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070067 struct acpi_processor_performance *acpi_data;
68 struct cpufreq_frequency_table *freq_table;
69 unsigned int resume;
70 unsigned int cpu_feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
Tejun Heof1625062009-10-29 22:34:13 +090073static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
travis@sgi.comea348f32008-01-30 13:33:12 +010074
Fenghua Yu50109292007-08-07 18:40:30 -040075/* acpi_perf_data is a pointer to percpu data. */
Namhyung Kim3f6c4df2010-08-13 23:00:11 +090076static struct acpi_processor_performance __percpu *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
Matthew Garrett3dc9a632012-09-04 08:28:02 +000089static int check_amd_hwpstate_cpu(unsigned int cpuid)
90{
91 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
92
93 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
94}
95
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070096static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070097{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070098 struct acpi_processor_performance *perf;
99 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700100
101 perf = data->acpi_data;
102
Dave Jones3a58df32009-01-17 22:36:14 -0500103 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700104 if (value == perf->states[i].status)
105 return data->freq_table[i].frequency;
106 }
107 return 0;
108}
109
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700110static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
111{
112 int i;
Venkatesh Pallipadia6f6e6e2006-10-03 12:37:42 -0700113 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700114
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000115 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
116 msr &= AMD_MSR_RANGE;
117 else
118 msr &= INTEL_MSR_RANGE;
119
Venkatesh Pallipadia6f6e6e2006-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 Pallipadia6f6e6e2006-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:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000133 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700134 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700135 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700136 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700137 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700138 return 0;
139 }
140}
141
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700142struct msr_addr {
143 u32 reg;
144};
145
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700146struct io_addr {
147 u16 port;
148 u8 bit_width;
149};
150
151struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700152 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100153 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500154 union {
155 struct msr_addr msr;
156 struct io_addr io;
157 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700158 u32 val;
159};
160
Andrew Morton01599fc2009-04-13 10:27:49 -0700161/* Called via smp_call_function_single(), on the target CPU */
162static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700163{
Mike Travis72859082009-01-16 15:31:15 -0800164 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700165 u32 h;
166
167 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700168 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000169 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700170 rdmsr(cmd->addr.msr.reg, cmd->val, h);
171 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700172 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800173 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
174 &cmd->val,
175 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700176 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700177 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700178 break;
179 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700180}
181
Andrew Morton01599fc2009-04-13 10:27:49 -0700182/* Called via smp_call_function_many(), on the target CPUs */
183static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700184{
Mike Travis72859082009-01-16 15:31:15 -0800185 struct drv_cmd *cmd = _cmd;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700186 u32 lo, hi;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700187
188 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700189 case SYSTEM_INTEL_MSR_CAPABLE:
Venki Pallipadi13424f62007-05-23 15:42:13 -0700190 rdmsr(cmd->addr.msr.reg, lo, hi);
191 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
192 wrmsr(cmd->addr.msr.reg, lo, hi);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700193 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000194 case SYSTEM_AMD_MSR_CAPABLE:
195 wrmsr(cmd->addr.msr.reg, cmd->val, 0);
196 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700197 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800198 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
199 cmd->val,
200 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700201 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700202 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700203 break;
204 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700205}
206
Dave Jones95dd7222006-10-18 00:41:48 -0400207static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700208{
Andrew Morton4a283952009-12-21 16:19:58 -0800209 int err;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700210 cmd->val = 0;
211
Andrew Morton4a283952009-12-21 16:19:58 -0800212 err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
213 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700214}
215
216static void drv_write(struct drv_cmd *cmd)
217{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700218 int this_cpu;
219
220 this_cpu = get_cpu();
221 if (cpumask_test_cpu(this_cpu, cmd->mask))
222 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700223 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700224 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700225}
226
Mike Travis4d8bb532009-01-04 05:18:08 -0800227static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700228{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700229 struct acpi_processor_performance *perf;
230 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700231
Mike Travis4d8bb532009-01-04 05:18:08 -0800232 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700233 return 0;
234
Tejun Heof1625062009-10-29 22:34:13 +0900235 switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700236 case SYSTEM_INTEL_MSR_CAPABLE:
237 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
238 cmd.addr.msr.reg = MSR_IA32_PERF_STATUS;
239 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000240 case SYSTEM_AMD_MSR_CAPABLE:
241 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
242 cmd.addr.msr.reg = MSR_AMD_PERF_STATUS;
243 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700244 case SYSTEM_IO_CAPABLE:
245 cmd.type = SYSTEM_IO_CAPABLE;
Tejun Heof1625062009-10-29 22:34:13 +0900246 perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700247 cmd.addr.io.port = perf->control_register.address;
248 cmd.addr.io.bit_width = perf->control_register.bit_width;
249 break;
250 default:
251 return 0;
252 }
253
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100254 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700255 drv_read(&cmd);
256
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200257 pr_debug("get_cur_val = %u\n", cmd.val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700258
259 return cmd.val;
260}
261
262static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
263{
Tejun Heof1625062009-10-29 22:34:13 +0900264 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700265 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400266 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700267
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200268 pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700269
270 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700271 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700272 return 0;
273 }
274
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400275 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800276 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400277 if (freq != cached_freq) {
278 /*
279 * The dreaded BIOS frequency change behind our back.
280 * Force set the frequency on next target call.
281 */
282 data->resume = 1;
283 }
284
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200285 pr_debug("cur freq = %u\n", freq);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700286
287 return freq;
288}
289
Mike Travis72859082009-01-16 15:31:15 -0800290static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700291 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700292{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700293 unsigned int cur_freq;
294 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700295
Dave Jones3a58df32009-01-17 22:36:14 -0500296 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700297 cur_freq = extract_freq(get_cur_val(mask), data);
298 if (cur_freq == freq)
299 return 1;
300 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 return 0;
303}
304
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700305static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700306 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Tejun Heof1625062009-10-29 22:34:13 +0900308 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700309 struct acpi_processor_performance *perf;
310 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700311 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800312 unsigned int next_state = 0; /* Index into freq_table */
313 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700314 unsigned int i;
315 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200317 pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700319 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400320 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700321 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500324 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700325 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700326 data->freq_table,
327 target_freq,
328 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800329 if (unlikely(result)) {
330 result = -ENODEV;
331 goto out;
332 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500333
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700334 next_perf_state = data->freq_table[next_state].index;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700335 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700336 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200337 pr_debug("Called after resume, resetting to P%d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700338 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700339 data->resume = 0;
340 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200341 pr_debug("Already at target state (P%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700342 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800343 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700344 }
345 }
346
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700347 switch (data->cpu_feature) {
348 case SYSTEM_INTEL_MSR_CAPABLE:
349 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
350 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700351 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700352 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000353 case SYSTEM_AMD_MSR_CAPABLE:
354 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
355 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
356 cmd.val = (u32) perf->states[next_perf_state].control;
357 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700358 case SYSTEM_IO_CAPABLE:
359 cmd.type = SYSTEM_IO_CAPABLE;
360 cmd.addr.io.port = perf->control_register.address;
361 cmd.addr.io.bit_width = perf->control_register.bit_width;
362 cmd.val = (u32) perf->states[next_perf_state].control;
363 break;
364 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800365 result = -ENODEV;
366 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700367 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700368
Mike Travis4d8bb532009-01-04 05:18:08 -0800369 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700370 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100371 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700372 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100373 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700374
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800375 freqs.old = perf->states[perf->state].core_frequency * 1000;
376 freqs.new = data->freq_table[next_state].frequency;
Thomas Renninger6b72e392010-04-20 13:17:35 +0200377 for_each_cpu(i, policy->cpus) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700378 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500379 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
380 }
381
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700382 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500383
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700384 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800385 if (!check_freqs(cmd.mask, freqs.new, data)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200386 pr_debug("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700387 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800388 result = -EAGAIN;
389 goto out;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500390 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500391 }
392
Thomas Renninger6b72e392010-04-20 13:17:35 +0200393 for_each_cpu(i, policy->cpus) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700394 freqs.cpu = i;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500395 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
396 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700397 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500398
Mike Travis4d8bb532009-01-04 05:18:08 -0800399out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700400 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700403static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Tejun Heof1625062009-10-29 22:34:13 +0900405 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200407 pr_debug("acpi_cpufreq_verify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700409 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700413acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700415 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (cpu_khz) {
418 /* search the closest match to cpu_khz */
419 unsigned int i;
420 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500421 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dave Jones3a58df32009-01-17 22:36:14 -0500423 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400425 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500427 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700428 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
Dave Jones95dd7222006-10-18 00:41:48 -0400431 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700432 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500433 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500435 perf->state = 0;
436 return perf->states[0].core_frequency * 1000;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800440static void free_acpi_perf_data(void)
441{
442 unsigned int i;
443
444 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
445 for_each_possible_cpu(i)
446 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
447 ->shared_cpu_map);
448 free_percpu(acpi_perf_data);
449}
450
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500451/*
452 * acpi_cpufreq_early_init - initialize ACPI P-States library
453 *
454 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
455 * in order to determine correct frequency and voltage pairings. We can
456 * do _PDC and _PSD and find out the processor dependency for the
457 * actual init that will happen later...
458 */
Fenghua Yu50109292007-08-07 18:40:30 -0400459static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500460{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800461 unsigned int i;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200462 pr_debug("acpi_cpufreq_early_init\n");
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500463
Fenghua Yu50109292007-08-07 18:40:30 -0400464 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
465 if (!acpi_perf_data) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200466 pr_debug("Memory allocation error for acpi_perf_data.\n");
Fenghua Yu50109292007-08-07 18:40:30 -0400467 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500468 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800469 for_each_possible_cpu(i) {
Yinghai Lueaa958402009-06-06 14:51:36 -0700470 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800471 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
472 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800473
474 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
475 free_acpi_perf_data();
476 return -ENOMEM;
477 }
478 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500479
480 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700481 acpi_processor_preregister_performance(acpi_perf_data);
482 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500483}
484
Dave Jones95625b82006-10-21 01:37:39 -0400485#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700486/*
487 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
488 * or do it in BIOS firmware and won't inform about it to OS. If not
489 * detected, this has a side effect of making CPU run at a different speed
490 * than OS intended it to run at. Detect it and handle it cleanly.
491 */
492static int bios_with_sw_any_bug;
493
Jeff Garzik18552562007-10-03 15:15:40 -0400494static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700495{
496 bios_with_sw_any_bug = 1;
497 return 0;
498}
499
Jeff Garzik18552562007-10-03 15:15:40 -0400500static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700501 {
502 .callback = sw_any_bug_found,
503 .ident = "Supermicro Server X6DLP",
504 .matches = {
505 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
506 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
507 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
508 },
509 },
510 { }
511};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400512
513static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
514{
John Villalovos293afe42009-09-25 13:30:08 -0400515 /* Intel Xeon Processor 7100 Series Specification Update
516 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400517 * AL30: A Machine Check Exception (MCE) Occurring during an
518 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400519 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400520 if (c->x86_vendor == X86_VENDOR_INTEL) {
521 if ((c->x86 == 15) &&
522 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400523 (c->x86_mask == 8)) {
524 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
525 "Xeon(R) 7100 Errata AL30, processors may "
526 "lock up on frequency changes: disabling "
527 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400528 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400529 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400530 }
531 return 0;
532}
Dave Jones95625b82006-10-21 01:37:39 -0400533#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700534
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700535static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700537 unsigned int i;
538 unsigned int valid_states = 0;
539 unsigned int cpu = policy->cpu;
540 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700541 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200542 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700543 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400544#ifdef CONFIG_SMP
545 static int blacklisted;
546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200548 pr_debug("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400550#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400551 if (blacklisted)
552 return blacklisted;
553 blacklisted = acpi_cpufreq_blacklist(c);
554 if (blacklisted)
555 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400556#endif
557
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700558 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700560 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Rusty Russellb36128c2009-02-20 16:29:08 +0900562 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900563 per_cpu(acfreq_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700564
Dave Jones95dd7222006-10-18 00:41:48 -0400565 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700566 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500568 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (result)
570 goto err_free;
571
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500572 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500573 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400574
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400575 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400576 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400577 * coordination is required.
578 */
579 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700580 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800581 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700582 }
Rusty Russell835481d2009-01-04 05:18:06 -0800583 cpumask_copy(policy->related_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700584
585#ifdef CONFIG_SMP
586 dmi_check_system(sw_any_bug_dmi_table);
Rusty Russell835481d2009-01-04 05:18:06 -0800587 if (bios_with_sw_any_bug && cpumask_weight(policy->cpus) == 1) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700588 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800589 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700590 }
Andre Przywaraacd31622012-09-04 08:28:03 +0000591
592 if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
593 cpumask_clear(policy->cpus);
594 cpumask_set_cpu(cpu, policy->cpus);
595 cpumask_copy(policy->related_cpus, cpu_sibling_mask(cpu));
596 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
597 pr_info_once(PFX "overriding BIOS provided _PSD data\n");
598 }
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700599#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500602 if (perf->state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200603 pr_debug("No P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 result = -ENODEV;
605 goto err_unreg;
606 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500607
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700608 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 result = -ENODEV;
610 goto err_unreg;
611 }
612
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700613 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700614 case ACPI_ADR_SPACE_SYSTEM_IO:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200615 pr_debug("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700616 data->cpu_feature = SYSTEM_IO_CAPABLE;
617 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700618 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200619 pr_debug("HARDWARE addr space\n");
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000620 if (check_est_cpu(cpu)) {
621 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
622 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700623 }
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000624 if (check_amd_hwpstate_cpu(cpu)) {
625 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
626 break;
627 }
628 result = -ENODEV;
629 goto err_unreg;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700630 default:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200631 pr_debug("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700632 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700633 result = -ENODEV;
634 goto err_unreg;
635 }
636
Dave Jones95dd7222006-10-18 00:41:48 -0400637 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
638 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!data->freq_table) {
640 result = -ENOMEM;
641 goto err_unreg;
642 }
643
644 /* detect transition latency */
645 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500646 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700647 if ((perf->states[i].transition_latency * 1000) >
648 policy->cpuinfo.transition_latency)
649 policy->cpuinfo.transition_latency =
650 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700653 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
654 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
655 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700656 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700657 printk_once(KERN_INFO
658 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500662 for (i = 0; i < perf->state_count; i++) {
663 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400664 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700665 continue;
666
667 data->freq_table[valid_states].index = i;
668 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700669 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700670 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800672 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800673 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400676 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200679 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
680 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
681
Mattia Dongilia507ac42006-12-15 19:52:45 +0100682 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700683 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700684 /* Current speed is unknown and not detectable by IO port */
685 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
686 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700687 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700688 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100689 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700690 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700691 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700692 break;
693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* notify BIOS that we exist */
696 acpi_processor_notify_smm(THIS_MODULE);
697
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700698 /* Check for APERF/MPERF support in hardware */
Matthew Garrett92e03c42011-07-13 17:58:32 -0400699 if (boot_cpu_has(X86_FEATURE_APERFMPERF))
Mark Langsdorfa2fed572010-03-18 18:41:46 +0100700 acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700701
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200702 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500703 for (i = 0; i < perf->state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200704 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700705 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500706 (u32) perf->states[i].core_frequency,
707 (u32) perf->states[i].power,
708 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700711
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400712 /*
713 * the first call to ->target() should result in us actually
714 * writing something to the appropriate registers.
715 */
716 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700717
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700718 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dave Jones95dd7222006-10-18 00:41:48 -0400720err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400722err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500723 acpi_processor_unregister_performance(perf, cpu);
Dave Jones95dd7222006-10-18 00:41:48 -0400724err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 kfree(data);
Tejun Heof1625062009-10-29 22:34:13 +0900726 per_cpu(acfreq_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700728 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700731static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Tejun Heof1625062009-10-29 22:34:13 +0900733 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200735 pr_debug("acpi_cpufreq_cpu_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (data) {
738 cpufreq_frequency_table_put_attr(policy->cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900739 per_cpu(acfreq_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700740 acpi_processor_unregister_performance(data->acpi_data,
741 policy->cpu);
Zhang Ruidab5fff2010-10-12 09:09:37 +0800742 kfree(data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 kfree(data);
744 }
745
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700749static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Tejun Heof1625062009-10-29 22:34:13 +0900751 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200753 pr_debug("acpi_cpufreq_resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 data->resume = 1;
756
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700760static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 &cpufreq_freq_attr_scaling_available_freqs,
762 NULL,
763};
764
765static struct cpufreq_driver acpi_cpufreq_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100766 .verify = acpi_cpufreq_verify,
767 .target = acpi_cpufreq_target,
768 .bios_limit = acpi_processor_get_bios_limit,
769 .init = acpi_cpufreq_cpu_init,
770 .exit = acpi_cpufreq_cpu_exit,
771 .resume = acpi_cpufreq_resume,
772 .name = "acpi-cpufreq",
773 .owner = THIS_MODULE,
774 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775};
776
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700777static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Fenghua Yu50109292007-08-07 18:40:30 -0400779 int ret;
780
Yinghai Luee297532008-09-24 19:04:31 -0700781 if (acpi_disabled)
782 return 0;
783
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200784 pr_debug("acpi_cpufreq_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Fenghua Yu50109292007-08-07 18:40:30 -0400786 ret = acpi_cpufreq_early_init();
787 if (ret)
788 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500789
Akinobu Mita847aef62008-07-14 11:59:44 +0900790 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
791 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800792 free_acpi_perf_data();
Akinobu Mita847aef62008-07-14 11:59:44 +0900793
794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700797static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200799 pr_debug("acpi_cpufreq_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 cpufreq_unregister_driver(&acpi_cpufreq_driver);
802
Luming Yu50f4ddd2011-07-08 16:37:44 -0400803 free_acpi_perf_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400806module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700807MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400808 "value 0 or non-zero. non-zero -> strict ACPI checks are "
809 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811late_initcall(acpi_cpufreq_init);
812module_exit(acpi_cpufreq_exit);
813
814MODULE_ALIAS("acpi");