blob: 39264020b88a1b2e7d461a8d0a3f2f909a31dbb4 [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
Andre Przywara615b7302012-09-04 08:28:07 +000066#define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
67
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070068struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070069 struct acpi_processor_performance *acpi_data;
70 struct cpufreq_frequency_table *freq_table;
71 unsigned int resume;
72 unsigned int cpu_feature;
Lan Tianyuf4fd3792013-06-27 15:08:54 +080073 cpumask_var_t freqdomain_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Tejun Heof1625062009-10-29 22:34:13 +090076static DEFINE_PER_CPU(struct acpi_cpufreq_data *, acfreq_data);
travis@sgi.comea348f32008-01-30 13:33:12 +010077
Fenghua Yu50109292007-08-07 18:40:30 -040078/* acpi_perf_data is a pointer to percpu data. */
Namhyung Kim3f6c4df2010-08-13 23:00:11 +090079static struct acpi_processor_performance __percpu *acpi_perf_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct cpufreq_driver acpi_cpufreq_driver;
82
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040083static unsigned int acpi_pstate_strict;
Andre Przywara615b7302012-09-04 08:28:07 +000084static bool boost_enabled, boost_supported;
85static struct msr __percpu *msrs;
86
87static bool boost_state(unsigned int cpu)
88{
89 u32 lo, hi;
90 u64 msr;
91
92 switch (boot_cpu_data.x86_vendor) {
93 case X86_VENDOR_INTEL:
94 rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
95 msr = lo | ((u64)hi << 32);
96 return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
97 case X86_VENDOR_AMD:
98 rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
99 msr = lo | ((u64)hi << 32);
100 return !(msr & MSR_K7_HWCR_CPB_DIS);
101 }
102 return false;
103}
104
105static void boost_set_msrs(bool enable, const struct cpumask *cpumask)
106{
107 u32 cpu;
108 u32 msr_addr;
109 u64 msr_mask;
110
111 switch (boot_cpu_data.x86_vendor) {
112 case X86_VENDOR_INTEL:
113 msr_addr = MSR_IA32_MISC_ENABLE;
114 msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
115 break;
116 case X86_VENDOR_AMD:
117 msr_addr = MSR_K7_HWCR;
118 msr_mask = MSR_K7_HWCR_CPB_DIS;
119 break;
120 default:
121 return;
122 }
123
124 rdmsr_on_cpus(cpumask, msr_addr, msrs);
125
126 for_each_cpu(cpu, cpumask) {
127 struct msr *reg = per_cpu_ptr(msrs, cpu);
128 if (enable)
129 reg->q &= ~msr_mask;
130 else
131 reg->q |= msr_mask;
132 }
133
134 wrmsr_on_cpus(cpumask, msr_addr, msrs);
135}
136
Andre Przywara11269ff2012-09-04 08:28:08 +0000137static ssize_t _store_boost(const char *buf, size_t count)
Andre Przywara615b7302012-09-04 08:28:07 +0000138{
139 int ret;
140 unsigned long val = 0;
141
142 if (!boost_supported)
143 return -EINVAL;
144
145 ret = kstrtoul(buf, 10, &val);
146 if (ret || (val > 1))
147 return -EINVAL;
148
149 if ((val && boost_enabled) || (!val && !boost_enabled))
150 return count;
151
152 get_online_cpus();
153
154 boost_set_msrs(val, cpu_online_mask);
155
156 put_online_cpus();
157
158 boost_enabled = val;
159 pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
160
161 return count;
162}
163
Andre Przywara11269ff2012-09-04 08:28:08 +0000164static ssize_t store_global_boost(struct kobject *kobj, struct attribute *attr,
165 const char *buf, size_t count)
166{
167 return _store_boost(buf, count);
168}
169
Andre Przywara615b7302012-09-04 08:28:07 +0000170static ssize_t show_global_boost(struct kobject *kobj,
171 struct attribute *attr, char *buf)
172{
173 return sprintf(buf, "%u\n", boost_enabled);
174}
175
176static struct global_attr global_boost = __ATTR(boost, 0644,
177 show_global_boost,
178 store_global_boost);
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400179
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800180static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
181{
182 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
183
184 return cpufreq_show_cpus(data->freqdomain_cpus, buf);
185}
186
187cpufreq_freq_attr_ro(freqdomain_cpus);
188
Andre Przywara11269ff2012-09-04 08:28:08 +0000189#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
190static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
191 size_t count)
192{
193 return _store_boost(buf, count);
194}
195
196static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
197{
198 return sprintf(buf, "%u\n", boost_enabled);
199}
200
201static struct freq_attr cpb = __ATTR(cpb, 0644, show_cpb, store_cpb);
202#endif
203
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700204static int check_est_cpu(unsigned int cpuid)
205{
Mike Travis92cb7612007-10-19 20:35:04 +0200206 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700207
Harald Welte0de51082009-06-08 18:27:54 +0800208 return cpu_has(cpu, X86_FEATURE_EST);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700209}
210
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000211static int check_amd_hwpstate_cpu(unsigned int cpuid)
212{
213 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
214
215 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
216}
217
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700218static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700219{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700220 struct acpi_processor_performance *perf;
221 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700222
223 perf = data->acpi_data;
224
Dave Jones3a58df32009-01-17 22:36:14 -0500225 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700226 if (value == perf->states[i].status)
227 return data->freq_table[i].frequency;
228 }
229 return 0;
230}
231
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700232static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
233{
234 int i;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700235 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700236
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000237 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
238 msr &= AMD_MSR_RANGE;
239 else
240 msr &= INTEL_MSR_RANGE;
241
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700242 perf = data->acpi_data;
243
Dave Jones3a58df32009-01-17 22:36:14 -0500244 for (i = 0; data->freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
Viresh Kumar50701582013-03-30 16:25:15 +0530245 if (msr == perf->states[data->freq_table[i].driver_data].status)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700246 return data->freq_table[i].frequency;
247 }
248 return data->freq_table[0].frequency;
249}
250
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700251static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
252{
253 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700254 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000255 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700256 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700257 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700258 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700259 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700260 return 0;
261 }
262}
263
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700264struct msr_addr {
265 u32 reg;
266};
267
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700268struct io_addr {
269 u16 port;
270 u8 bit_width;
271};
272
273struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700274 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100275 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500276 union {
277 struct msr_addr msr;
278 struct io_addr io;
279 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700280 u32 val;
281};
282
Andrew Morton01599fc2009-04-13 10:27:49 -0700283/* Called via smp_call_function_single(), on the target CPU */
284static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700285{
Mike Travis72859082009-01-16 15:31:15 -0800286 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700287 u32 h;
288
289 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700290 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000291 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700292 rdmsr(cmd->addr.msr.reg, cmd->val, h);
293 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700294 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800295 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
296 &cmd->val,
297 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700298 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700299 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700300 break;
301 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700302}
303
Andrew Morton01599fc2009-04-13 10:27:49 -0700304/* Called via smp_call_function_many(), on the target CPUs */
305static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700306{
Mike Travis72859082009-01-16 15:31:15 -0800307 struct drv_cmd *cmd = _cmd;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700308 u32 lo, hi;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700309
310 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700311 case SYSTEM_INTEL_MSR_CAPABLE:
Venki Pallipadi13424f62007-05-23 15:42:13 -0700312 rdmsr(cmd->addr.msr.reg, lo, hi);
313 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
314 wrmsr(cmd->addr.msr.reg, lo, hi);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700315 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000316 case SYSTEM_AMD_MSR_CAPABLE:
317 wrmsr(cmd->addr.msr.reg, cmd->val, 0);
318 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700319 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800320 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
321 cmd->val,
322 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700323 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700324 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700325 break;
326 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700327}
328
Dave Jones95dd7222006-10-18 00:41:48 -0400329static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700330{
Andrew Morton4a283952009-12-21 16:19:58 -0800331 int err;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700332 cmd->val = 0;
333
Andrew Morton4a283952009-12-21 16:19:58 -0800334 err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
335 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700336}
337
338static void drv_write(struct drv_cmd *cmd)
339{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700340 int this_cpu;
341
342 this_cpu = get_cpu();
343 if (cpumask_test_cpu(this_cpu, cmd->mask))
344 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700345 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700346 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700347}
348
Mike Travis4d8bb532009-01-04 05:18:08 -0800349static u32 get_cur_val(const struct cpumask *mask)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700350{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700351 struct acpi_processor_performance *perf;
352 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700353
Mike Travis4d8bb532009-01-04 05:18:08 -0800354 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700355 return 0;
356
Tejun Heof1625062009-10-29 22:34:13 +0900357 switch (per_cpu(acfreq_data, cpumask_first(mask))->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700358 case SYSTEM_INTEL_MSR_CAPABLE:
359 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
Ross Lagerwall8673b832013-05-31 20:45:17 +0100360 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700361 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000362 case SYSTEM_AMD_MSR_CAPABLE:
363 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
Ross Lagerwall8673b832013-05-31 20:45:17 +0100364 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000365 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700366 case SYSTEM_IO_CAPABLE:
367 cmd.type = SYSTEM_IO_CAPABLE;
Tejun Heof1625062009-10-29 22:34:13 +0900368 perf = per_cpu(acfreq_data, cpumask_first(mask))->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700369 cmd.addr.io.port = perf->control_register.address;
370 cmd.addr.io.bit_width = perf->control_register.bit_width;
371 break;
372 default:
373 return 0;
374 }
375
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100376 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700377 drv_read(&cmd);
378
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200379 pr_debug("get_cur_val = %u\n", cmd.val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700380
381 return cmd.val;
382}
383
384static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
385{
Tejun Heof1625062009-10-29 22:34:13 +0900386 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700387 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400388 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700389
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200390 pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700391
392 if (unlikely(data == NULL ||
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700393 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700394 return 0;
395 }
396
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400397 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Mike Travise39ad412009-01-04 05:18:10 -0800398 freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400399 if (freq != cached_freq) {
400 /*
401 * The dreaded BIOS frequency change behind our back.
402 * Force set the frequency on next target call.
403 */
404 data->resume = 1;
405 }
406
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200407 pr_debug("cur freq = %u\n", freq);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700408
409 return freq;
410}
411
Mike Travis72859082009-01-16 15:31:15 -0800412static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700413 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700414{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700415 unsigned int cur_freq;
416 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700417
Dave Jones3a58df32009-01-17 22:36:14 -0500418 for (i = 0; i < 100; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700419 cur_freq = extract_freq(get_cur_val(mask), data);
420 if (cur_freq == freq)
421 return 1;
422 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 return 0;
425}
426
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700427static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700428 unsigned int target_freq, unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Tejun Heof1625062009-10-29 22:34:13 +0900430 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700431 struct acpi_processor_performance *perf;
432 struct cpufreq_freqs freqs;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700433 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800434 unsigned int next_state = 0; /* Index into freq_table */
435 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700436 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200438 pr_debug("acpi_cpufreq_target %d (%d)\n", target_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700440 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400441 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700442 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500445 perf = data->acpi_data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700446 result = cpufreq_frequency_table_target(policy,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700447 data->freq_table,
448 target_freq,
449 relation, &next_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800450 if (unlikely(result)) {
451 result = -ENODEV;
452 goto out;
453 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500454
Viresh Kumar50701582013-03-30 16:25:15 +0530455 next_perf_state = data->freq_table[next_state].driver_data;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700456 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700457 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200458 pr_debug("Called after resume, resetting to P%d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700459 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700460 data->resume = 0;
461 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200462 pr_debug("Already at target state (P%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700463 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800464 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700465 }
466 }
467
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700468 switch (data->cpu_feature) {
469 case SYSTEM_INTEL_MSR_CAPABLE:
470 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
471 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700472 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700473 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000474 case SYSTEM_AMD_MSR_CAPABLE:
475 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
476 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
477 cmd.val = (u32) perf->states[next_perf_state].control;
478 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700479 case SYSTEM_IO_CAPABLE:
480 cmd.type = SYSTEM_IO_CAPABLE;
481 cmd.addr.io.port = perf->control_register.address;
482 cmd.addr.io.bit_width = perf->control_register.bit_width;
483 cmd.val = (u32) perf->states[next_perf_state].control;
484 break;
485 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800486 result = -ENODEV;
487 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700488 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700489
Mike Travis4d8bb532009-01-04 05:18:08 -0800490 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700491 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100492 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700493 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100494 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700495
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800496 freqs.old = perf->states[perf->state].core_frequency * 1000;
497 freqs.new = data->freq_table[next_state].frequency;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530498 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500499
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700500 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500501
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700502 if (acpi_pstate_strict) {
Mike Travis4d8bb532009-01-04 05:18:08 -0800503 if (!check_freqs(cmd.mask, freqs.new, data)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200504 pr_debug("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700505 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800506 result = -EAGAIN;
Viresh Kumare15d8302013-06-19 14:22:55 +0530507 freqs.new = freqs.old;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500508 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500509 }
510
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530511 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Viresh Kumare15d8302013-06-19 14:22:55 +0530512
513 if (!result)
514 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500515
Mike Travis4d8bb532009-01-04 05:18:08 -0800516out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700517 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700520static int acpi_cpufreq_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Tejun Heof1625062009-10-29 22:34:13 +0900522 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200524 pr_debug("acpi_cpufreq_verify\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700526 return cpufreq_frequency_table_verify(policy, data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700530acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700532 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (cpu_khz) {
535 /* search the closest match to cpu_khz */
536 unsigned int i;
537 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500538 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Jones3a58df32009-01-17 22:36:14 -0500540 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400542 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500544 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700545 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 }
Dave Jones95dd7222006-10-18 00:41:48 -0400548 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700549 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500552 perf->state = 0;
553 return perf->states[0].core_frequency * 1000;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800557static void free_acpi_perf_data(void)
558{
559 unsigned int i;
560
561 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
562 for_each_possible_cpu(i)
563 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
564 ->shared_cpu_map);
565 free_percpu(acpi_perf_data);
566}
567
Andre Przywara615b7302012-09-04 08:28:07 +0000568static int boost_notify(struct notifier_block *nb, unsigned long action,
569 void *hcpu)
570{
571 unsigned cpu = (long)hcpu;
572 const struct cpumask *cpumask;
573
574 cpumask = get_cpu_mask(cpu);
575
576 /*
577 * Clear the boost-disable bit on the CPU_DOWN path so that
578 * this cpu cannot block the remaining ones from boosting. On
579 * the CPU_UP path we simply keep the boost-disable flag in
580 * sync with the current global state.
581 */
582
583 switch (action) {
584 case CPU_UP_PREPARE:
585 case CPU_UP_PREPARE_FROZEN:
586 boost_set_msrs(boost_enabled, cpumask);
587 break;
588
589 case CPU_DOWN_PREPARE:
590 case CPU_DOWN_PREPARE_FROZEN:
591 boost_set_msrs(1, cpumask);
592 break;
593
594 default:
595 break;
596 }
597
598 return NOTIFY_OK;
599}
600
601
602static struct notifier_block boost_nb = {
603 .notifier_call = boost_notify,
604};
605
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500606/*
607 * acpi_cpufreq_early_init - initialize ACPI P-States library
608 *
609 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
610 * in order to determine correct frequency and voltage pairings. We can
611 * do _PDC and _PSD and find out the processor dependency for the
612 * actual init that will happen later...
613 */
Fenghua Yu50109292007-08-07 18:40:30 -0400614static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500615{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800616 unsigned int i;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200617 pr_debug("acpi_cpufreq_early_init\n");
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500618
Fenghua Yu50109292007-08-07 18:40:30 -0400619 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
620 if (!acpi_perf_data) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200621 pr_debug("Memory allocation error for acpi_perf_data.\n");
Fenghua Yu50109292007-08-07 18:40:30 -0400622 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500623 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800624 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700625 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800626 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
627 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800628
629 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
630 free_acpi_perf_data();
631 return -ENOMEM;
632 }
633 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500634
635 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700636 acpi_processor_preregister_performance(acpi_perf_data);
637 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500638}
639
Dave Jones95625b82006-10-21 01:37:39 -0400640#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700641/*
642 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
643 * or do it in BIOS firmware and won't inform about it to OS. If not
644 * detected, this has a side effect of making CPU run at a different speed
645 * than OS intended it to run at. Detect it and handle it cleanly.
646 */
647static int bios_with_sw_any_bug;
648
Jeff Garzik18552562007-10-03 15:15:40 -0400649static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700650{
651 bios_with_sw_any_bug = 1;
652 return 0;
653}
654
Jeff Garzik18552562007-10-03 15:15:40 -0400655static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700656 {
657 .callback = sw_any_bug_found,
658 .ident = "Supermicro Server X6DLP",
659 .matches = {
660 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
661 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
662 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
663 },
664 },
665 { }
666};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400667
668static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
669{
John Villalovos293afe42009-09-25 13:30:08 -0400670 /* Intel Xeon Processor 7100 Series Specification Update
671 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400672 * AL30: A Machine Check Exception (MCE) Occurring during an
673 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400674 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400675 if (c->x86_vendor == X86_VENDOR_INTEL) {
676 if ((c->x86 == 15) &&
677 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400678 (c->x86_mask == 8)) {
679 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
680 "Xeon(R) 7100 Errata AL30, processors may "
681 "lock up on frequency changes: disabling "
682 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400683 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400684 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400685 }
686 return 0;
687}
Dave Jones95625b82006-10-21 01:37:39 -0400688#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700689
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700690static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700692 unsigned int i;
693 unsigned int valid_states = 0;
694 unsigned int cpu = policy->cpu;
695 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700696 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200697 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700698 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400699#ifdef CONFIG_SMP
700 static int blacklisted;
701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200703 pr_debug("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400705#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400706 if (blacklisted)
707 return blacklisted;
708 blacklisted = acpi_cpufreq_blacklist(c);
709 if (blacklisted)
710 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400711#endif
712
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700713 data = kzalloc(sizeof(struct acpi_cpufreq_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700715 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800717 if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
718 result = -ENOMEM;
719 goto err_free;
720 }
721
Rusty Russellb36128c2009-02-20 16:29:08 +0900722 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900723 per_cpu(acfreq_data, cpu) = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700724
Dave Jones95dd7222006-10-18 00:41:48 -0400725 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700726 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500728 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (result)
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800730 goto err_free_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500732 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500733 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400734
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400735 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400736 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400737 * coordination is required.
738 */
739 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700740 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800741 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700742 }
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800743 cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700744
745#ifdef CONFIG_SMP
746 dmi_check_system(sw_any_bug_dmi_table);
Fabio Baltieri2624f902013-01-31 09:44:40 +0000747 if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700748 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Rusty Russell835481d2009-01-04 05:18:06 -0800749 cpumask_copy(policy->cpus, cpu_core_mask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700750 }
Andre Przywaraacd31622012-09-04 08:28:03 +0000751
752 if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
753 cpumask_clear(policy->cpus);
754 cpumask_set_cpu(cpu, policy->cpus);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800755 cpumask_copy(data->freqdomain_cpus, cpu_sibling_mask(cpu));
Andre Przywaraacd31622012-09-04 08:28:03 +0000756 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
757 pr_info_once(PFX "overriding BIOS provided _PSD data\n");
758 }
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700759#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500762 if (perf->state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200763 pr_debug("No P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 result = -ENODEV;
765 goto err_unreg;
766 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500767
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700768 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 result = -ENODEV;
770 goto err_unreg;
771 }
772
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700773 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700774 case ACPI_ADR_SPACE_SYSTEM_IO:
Matthew Garrettc40a4512013-01-20 10:24:26 +0000775 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
776 boot_cpu_data.x86 == 0xf) {
777 pr_debug("AMD K8 systems must use native drivers.\n");
778 result = -ENODEV;
779 goto err_unreg;
780 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200781 pr_debug("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700782 data->cpu_feature = SYSTEM_IO_CAPABLE;
783 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700784 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200785 pr_debug("HARDWARE addr space\n");
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000786 if (check_est_cpu(cpu)) {
787 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
788 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700789 }
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000790 if (check_amd_hwpstate_cpu(cpu)) {
791 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
792 break;
793 }
794 result = -ENODEV;
795 goto err_unreg;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700796 default:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200797 pr_debug("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700798 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700799 result = -ENODEV;
800 goto err_unreg;
801 }
802
Dave Jones95dd7222006-10-18 00:41:48 -0400803 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
804 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!data->freq_table) {
806 result = -ENOMEM;
807 goto err_unreg;
808 }
809
810 /* detect transition latency */
811 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500812 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700813 if ((perf->states[i].transition_latency * 1000) >
814 policy->cpuinfo.transition_latency)
815 policy->cpuinfo.transition_latency =
816 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700819 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
820 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
821 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700822 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700823 printk_once(KERN_INFO
824 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700825 }
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500828 for (i = 0; i < perf->state_count; i++) {
829 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400830 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700831 continue;
832
Viresh Kumar50701582013-03-30 16:25:15 +0530833 data->freq_table[valid_states].driver_data = i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700834 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700835 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700836 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800838 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800839 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400842 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200845 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
846 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
847
Mattia Dongilia507ac42006-12-15 19:52:45 +0100848 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700849 case ACPI_ADR_SPACE_SYSTEM_IO:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700850 /* Current speed is unknown and not detectable by IO port */
851 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
852 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700853 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700854 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Mattia Dongilia507ac42006-12-15 19:52:45 +0100855 policy->cur = get_cur_freq_on_cpu(cpu);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700856 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700857 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700858 break;
859 }
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* notify BIOS that we exist */
862 acpi_processor_notify_smm(THIS_MODULE);
863
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700864 /* Check for APERF/MPERF support in hardware */
Matthew Garrett92e03c42011-07-13 17:58:32 -0400865 if (boot_cpu_has(X86_FEATURE_APERFMPERF))
Mark Langsdorfa2fed572010-03-18 18:41:46 +0100866 acpi_cpufreq_driver.getavg = cpufreq_get_measured_perf;
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700867
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200868 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500869 for (i = 0; i < perf->state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200870 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700871 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500872 (u32) perf->states[i].core_frequency,
873 (u32) perf->states[i].power,
874 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700877
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400878 /*
879 * the first call to ->target() should result in us actually
880 * writing something to the appropriate registers.
881 */
882 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700883
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700884 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Dave Jones95dd7222006-10-18 00:41:48 -0400886err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400888err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500889 acpi_processor_unregister_performance(perf, cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800890err_free_mask:
891 free_cpumask_var(data->freqdomain_cpus);
Dave Jones95dd7222006-10-18 00:41:48 -0400892err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 kfree(data);
Tejun Heof1625062009-10-29 22:34:13 +0900894 per_cpu(acfreq_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700896 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700899static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Tejun Heof1625062009-10-29 22:34:13 +0900901 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200903 pr_debug("acpi_cpufreq_cpu_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (data) {
906 cpufreq_frequency_table_put_attr(policy->cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900907 per_cpu(acfreq_data, policy->cpu) = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700908 acpi_processor_unregister_performance(data->acpi_data,
909 policy->cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800910 free_cpumask_var(data->freqdomain_cpus);
Zhang Ruidab5fff2010-10-12 09:09:37 +0800911 kfree(data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 kfree(data);
913 }
914
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700918static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Tejun Heof1625062009-10-29 22:34:13 +0900920 struct acpi_cpufreq_data *data = per_cpu(acfreq_data, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200922 pr_debug("acpi_cpufreq_resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 data->resume = 1;
925
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700929static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 &cpufreq_freq_attr_scaling_available_freqs,
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800931 &freqdomain_cpus,
Andre Przywara11269ff2012-09-04 08:28:08 +0000932 NULL, /* this is a placeholder for cpb, do not remove */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 NULL,
934};
935
936static struct cpufreq_driver acpi_cpufreq_driver = {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100937 .verify = acpi_cpufreq_verify,
938 .target = acpi_cpufreq_target,
939 .bios_limit = acpi_processor_get_bios_limit,
940 .init = acpi_cpufreq_cpu_init,
941 .exit = acpi_cpufreq_cpu_exit,
942 .resume = acpi_cpufreq_resume,
943 .name = "acpi-cpufreq",
944 .owner = THIS_MODULE,
945 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946};
947
Andre Przywara615b7302012-09-04 08:28:07 +0000948static void __init acpi_cpufreq_boost_init(void)
949{
950 if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
951 msrs = msrs_alloc();
952
953 if (!msrs)
954 return;
955
956 boost_supported = true;
957 boost_enabled = boost_state(0);
958
959 get_online_cpus();
960
961 /* Force all MSRs to the same value */
962 boost_set_msrs(boost_enabled, cpu_online_mask);
963
964 register_cpu_notifier(&boost_nb);
965
966 put_online_cpus();
967 } else
968 global_boost.attr.mode = 0444;
969
970 /* We create the boost file in any case, though for systems without
971 * hardware support it will be read-only and hardwired to return 0.
972 */
Viresh Kumar2361be22013-05-17 16:09:09 +0530973 if (cpufreq_sysfs_create_file(&(global_boost.attr)))
Andre Przywara615b7302012-09-04 08:28:07 +0000974 pr_warn(PFX "could not register global boost sysfs file\n");
975 else
976 pr_debug("registered global boost sysfs file\n");
977}
978
979static void __exit acpi_cpufreq_boost_exit(void)
980{
Viresh Kumar2361be22013-05-17 16:09:09 +0530981 cpufreq_sysfs_remove_file(&(global_boost.attr));
Andre Przywara615b7302012-09-04 08:28:07 +0000982
983 if (msrs) {
984 unregister_cpu_notifier(&boost_nb);
985
986 msrs_free(msrs);
987 msrs = NULL;
988 }
989}
990
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700991static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Fenghua Yu50109292007-08-07 18:40:30 -0400993 int ret;
994
Yinghai Luee297532008-09-24 19:04:31 -0700995 if (acpi_disabled)
996 return 0;
997
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200998 pr_debug("acpi_cpufreq_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Fenghua Yu50109292007-08-07 18:40:30 -04001000 ret = acpi_cpufreq_early_init();
1001 if (ret)
1002 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -05001003
Andre Przywara11269ff2012-09-04 08:28:08 +00001004#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
1005 /* this is a sysfs file with a strange name and an even stranger
1006 * semantic - per CPU instantiation, but system global effect.
1007 * Lets enable it only on AMD CPUs for compatibility reasons and
1008 * only if configured. This is considered legacy code, which
1009 * will probably be removed at some point in the future.
1010 */
1011 if (check_amd_hwpstate_cpu(0)) {
1012 struct freq_attr **iter;
1013
1014 pr_debug("adding sysfs entry for cpb\n");
1015
1016 for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
1017 ;
1018
1019 /* make sure there is a terminator behind it */
1020 if (iter[1] == NULL)
1021 *iter = &cpb;
1022 }
1023#endif
1024
Akinobu Mita847aef62008-07-14 11:59:44 +09001025 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
1026 if (ret)
Rusty Russell2fdf66b2008-12-31 18:08:47 -08001027 free_acpi_perf_data();
Andre Przywara615b7302012-09-04 08:28:07 +00001028 else
1029 acpi_cpufreq_boost_init();
Akinobu Mita847aef62008-07-14 11:59:44 +09001030
1031 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -07001034static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001036 pr_debug("acpi_cpufreq_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Andre Przywara615b7302012-09-04 08:28:07 +00001038 acpi_cpufreq_boost_exit();
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 cpufreq_unregister_driver(&acpi_cpufreq_driver);
1041
Luming Yu50f4ddd2011-07-08 16:37:44 -04001042 free_acpi_perf_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -04001045module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -07001046MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -04001047 "value 0 or non-zero. non-zero -> strict ACPI checks are "
1048 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050late_initcall(acpi_cpufreq_init);
1051module_exit(acpi_cpufreq_exit);
1052
Matthew Garrettefa17192013-01-22 22:33:46 +01001053static const struct x86_cpu_id acpi_cpufreq_ids[] = {
1054 X86_FEATURE_MATCH(X86_FEATURE_ACPI),
1055 X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
1056 {}
1057};
1058MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
1059
Rafael J. Wysockic655aff2013-06-07 13:13:31 +02001060static const struct acpi_device_id processor_device_ids[] = {
1061 {ACPI_PROCESSOR_OBJECT_HID, },
1062 {ACPI_PROCESSOR_DEVICE_HID, },
1063 {},
1064};
1065MODULE_DEVICE_TABLE(acpi, processor_device_ids);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067MODULE_ALIAS("acpi");