blob: e7fcaa6eedbda12ca7c41f0de0755c27c264d513 [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>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
50MODULE_DESCRIPTION("ACPI Processor P-States Driver");
51MODULE_LICENSE("GPL");
52
Andre Przywaraacd31622012-09-04 08:28:03 +000053#define PFX "acpi-cpufreq: "
54
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070055enum {
56 UNDEFINED_CAPABLE = 0,
57 SYSTEM_INTEL_MSR_CAPABLE,
Matthew Garrett3dc9a632012-09-04 08:28:02 +000058 SYSTEM_AMD_MSR_CAPABLE,
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070059 SYSTEM_IO_CAPABLE,
60};
61
62#define INTEL_MSR_RANGE (0xffff)
Matthew Garrett3dc9a632012-09-04 08:28:02 +000063#define AMD_MSR_RANGE (0x7)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070064
Andre Przywara615b7302012-09-04 08:28:07 +000065#define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
66
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070067struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070068 struct acpi_processor_performance *acpi_data;
69 struct cpufreq_frequency_table *freq_table;
70 unsigned int resume;
71 unsigned int cpu_feature;
Lan Tianyuf4fd3792013-06-27 15:08:54 +080072 cpumask_var_t freqdomain_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
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;
Andre Przywara615b7302012-09-04 08:28:07 +000081static struct msr __percpu *msrs;
82
83static bool boost_state(unsigned int cpu)
84{
85 u32 lo, hi;
86 u64 msr;
87
88 switch (boot_cpu_data.x86_vendor) {
89 case X86_VENDOR_INTEL:
90 rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
91 msr = lo | ((u64)hi << 32);
92 return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
93 case X86_VENDOR_AMD:
94 rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
95 msr = lo | ((u64)hi << 32);
96 return !(msr & MSR_K7_HWCR_CPB_DIS);
97 }
98 return false;
99}
100
101static void boost_set_msrs(bool enable, const struct cpumask *cpumask)
102{
103 u32 cpu;
104 u32 msr_addr;
105 u64 msr_mask;
106
107 switch (boot_cpu_data.x86_vendor) {
108 case X86_VENDOR_INTEL:
109 msr_addr = MSR_IA32_MISC_ENABLE;
110 msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
111 break;
112 case X86_VENDOR_AMD:
113 msr_addr = MSR_K7_HWCR;
114 msr_mask = MSR_K7_HWCR_CPB_DIS;
115 break;
116 default:
117 return;
118 }
119
120 rdmsr_on_cpus(cpumask, msr_addr, msrs);
121
122 for_each_cpu(cpu, cpumask) {
123 struct msr *reg = per_cpu_ptr(msrs, cpu);
124 if (enable)
125 reg->q &= ~msr_mask;
126 else
127 reg->q |= msr_mask;
128 }
129
130 wrmsr_on_cpus(cpumask, msr_addr, msrs);
131}
132
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100133static int _store_boost(int val)
Andre Przywara615b7302012-09-04 08:28:07 +0000134{
Andre Przywara615b7302012-09-04 08:28:07 +0000135 get_online_cpus();
Andre Przywara615b7302012-09-04 08:28:07 +0000136 boost_set_msrs(val, cpu_online_mask);
Andre Przywara615b7302012-09-04 08:28:07 +0000137 put_online_cpus();
Andre Przywara615b7302012-09-04 08:28:07 +0000138 pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
139
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100140 return 0;
Andre Przywara615b7302012-09-04 08:28:07 +0000141}
142
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800143static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
144{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800145 struct acpi_cpufreq_data *data = policy->driver_data;
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800146
147 return cpufreq_show_cpus(data->freqdomain_cpus, buf);
148}
149
150cpufreq_freq_attr_ro(freqdomain_cpus);
151
Andre Przywara11269ff2012-09-04 08:28:08 +0000152#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100153static ssize_t store_boost(const char *buf, size_t count)
154{
155 int ret;
156 unsigned long val = 0;
157
158 if (!acpi_cpufreq_driver.boost_supported)
159 return -EINVAL;
160
161 ret = kstrtoul(buf, 10, &val);
162 if (ret || (val > 1))
163 return -EINVAL;
164
165 _store_boost((int) val);
166
167 return count;
168}
169
Andre Przywara11269ff2012-09-04 08:28:08 +0000170static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
171 size_t count)
172{
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100173 return store_boost(buf, count);
Andre Przywara11269ff2012-09-04 08:28:08 +0000174}
175
176static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
177{
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100178 return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
Andre Przywara11269ff2012-09-04 08:28:08 +0000179}
180
Lan Tianyu59027d32013-08-13 10:05:53 +0800181cpufreq_freq_attr_rw(cpb);
Andre Przywara11269ff2012-09-04 08:28:08 +0000182#endif
183
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700184static int check_est_cpu(unsigned int cpuid)
185{
Mike Travis92cb7612007-10-19 20:35:04 +0200186 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700187
Harald Welte0de51082009-06-08 18:27:54 +0800188 return cpu_has(cpu, X86_FEATURE_EST);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700189}
190
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000191static int check_amd_hwpstate_cpu(unsigned int cpuid)
192{
193 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
194
195 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
196}
197
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700198static unsigned extract_io(u32 value, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700199{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700200 struct acpi_processor_performance *perf;
201 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700202
203 perf = data->acpi_data;
204
Dave Jones3a58df32009-01-17 22:36:14 -0500205 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700206 if (value == perf->states[i].status)
207 return data->freq_table[i].frequency;
208 }
209 return 0;
210}
211
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700212static unsigned extract_msr(u32 msr, struct acpi_cpufreq_data *data)
213{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300214 struct cpufreq_frequency_table *pos;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700215 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700216
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000217 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
218 msr &= AMD_MSR_RANGE;
219 else
220 msr &= INTEL_MSR_RANGE;
221
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700222 perf = data->acpi_data;
223
Stratos Karafotis041526f2014-04-25 23:15:38 +0300224 cpufreq_for_each_entry(pos, data->freq_table)
225 if (msr == perf->states[pos->driver_data].status)
226 return pos->frequency;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700227 return data->freq_table[0].frequency;
228}
229
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700230static unsigned extract_freq(u32 val, struct acpi_cpufreq_data *data)
231{
232 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700233 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000234 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700235 return extract_msr(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700236 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700237 return extract_io(val, data);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700238 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700239 return 0;
240 }
241}
242
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700243struct msr_addr {
244 u32 reg;
245};
246
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700247struct io_addr {
248 u16 port;
249 u8 bit_width;
250};
251
252struct drv_cmd {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700253 unsigned int type;
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100254 const struct cpumask *mask;
Dave Jones3a58df32009-01-17 22:36:14 -0500255 union {
256 struct msr_addr msr;
257 struct io_addr io;
258 } addr;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700259 u32 val;
260};
261
Andrew Morton01599fc2009-04-13 10:27:49 -0700262/* Called via smp_call_function_single(), on the target CPU */
263static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700264{
Mike Travis72859082009-01-16 15:31:15 -0800265 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700266 u32 h;
267
268 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700269 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000270 case SYSTEM_AMD_MSR_CAPABLE:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700271 rdmsr(cmd->addr.msr.reg, cmd->val, h);
272 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700273 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800274 acpi_os_read_port((acpi_io_address)cmd->addr.io.port,
275 &cmd->val,
276 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700277 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700278 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700279 break;
280 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700281}
282
Andrew Morton01599fc2009-04-13 10:27:49 -0700283/* Called via smp_call_function_many(), on the target CPUs */
284static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700285{
Mike Travis72859082009-01-16 15:31:15 -0800286 struct drv_cmd *cmd = _cmd;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700287 u32 lo, hi;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700288
289 switch (cmd->type) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700290 case SYSTEM_INTEL_MSR_CAPABLE:
Venki Pallipadi13424f62007-05-23 15:42:13 -0700291 rdmsr(cmd->addr.msr.reg, lo, hi);
292 lo = (lo & ~INTEL_MSR_RANGE) | (cmd->val & INTEL_MSR_RANGE);
293 wrmsr(cmd->addr.msr.reg, lo, hi);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700294 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000295 case SYSTEM_AMD_MSR_CAPABLE:
296 wrmsr(cmd->addr.msr.reg, cmd->val, 0);
297 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700298 case SYSTEM_IO_CAPABLE:
Venkatesh Pallipadi4e581ff2006-12-13 10:41:16 -0800299 acpi_os_write_port((acpi_io_address)cmd->addr.io.port,
300 cmd->val,
301 (u32)cmd->addr.io.bit_width);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700302 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700303 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700304 break;
305 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700306}
307
Dave Jones95dd7222006-10-18 00:41:48 -0400308static void drv_read(struct drv_cmd *cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700309{
Andrew Morton4a283952009-12-21 16:19:58 -0800310 int err;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700311 cmd->val = 0;
312
Andrew Morton4a283952009-12-21 16:19:58 -0800313 err = smp_call_function_any(cmd->mask, do_drv_read, cmd, 1);
314 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700315}
316
317static void drv_write(struct drv_cmd *cmd)
318{
Linus Torvaldsea34f432009-04-15 08:05:13 -0700319 int this_cpu;
320
321 this_cpu = get_cpu();
322 if (cpumask_test_cpu(this_cpu, cmd->mask))
323 do_drv_write(cmd);
Andrew Morton01599fc2009-04-13 10:27:49 -0700324 smp_call_function_many(cmd->mask, do_drv_write, cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700325 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700326}
327
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800328static u32
329get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700330{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700331 struct acpi_processor_performance *perf;
332 struct drv_cmd cmd;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700333
Mike Travis4d8bb532009-01-04 05:18:08 -0800334 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700335 return 0;
336
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800337 switch (data->cpu_feature) {
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700338 case SYSTEM_INTEL_MSR_CAPABLE:
339 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
Ross Lagerwall8673b832013-05-31 20:45:17 +0100340 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700341 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000342 case SYSTEM_AMD_MSR_CAPABLE:
343 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
Ross Lagerwall8673b832013-05-31 20:45:17 +0100344 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000345 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700346 case SYSTEM_IO_CAPABLE:
347 cmd.type = SYSTEM_IO_CAPABLE;
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800348 perf = data->acpi_data;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700349 cmd.addr.io.port = perf->control_register.address;
350 cmd.addr.io.bit_width = perf->control_register.bit_width;
351 break;
352 default:
353 return 0;
354 }
355
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100356 cmd.mask = mask;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700357 drv_read(&cmd);
358
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200359 pr_debug("get_cur_val = %u\n", cmd.val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700360
361 return cmd.val;
362}
363
364static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
365{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800366 struct acpi_cpufreq_data *data;
367 struct cpufreq_policy *policy;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700368 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400369 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700370
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200371 pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700372
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800373 policy = cpufreq_cpu_get(cpu);
374 if (unlikely(!policy))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700375 return 0;
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800376
377 data = policy->driver_data;
378 cpufreq_cpu_put(policy);
379 if (unlikely(!data || !data->acpi_data || !data->freq_table))
380 return 0;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700381
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400382 cached_freq = data->freq_table[data->acpi_data->state].frequency;
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800383 freq = extract_freq(get_cur_val(cpumask_of(cpu), data), data);
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400384 if (freq != cached_freq) {
385 /*
386 * The dreaded BIOS frequency change behind our back.
387 * Force set the frequency on next target call.
388 */
389 data->resume = 1;
390 }
391
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200392 pr_debug("cur freq = %u\n", freq);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700393
394 return freq;
395}
396
Mike Travis72859082009-01-16 15:31:15 -0800397static unsigned int check_freqs(const struct cpumask *mask, unsigned int freq,
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700398 struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700399{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700400 unsigned int cur_freq;
401 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700402
Dave Jones3a58df32009-01-17 22:36:14 -0500403 for (i = 0; i < 100; i++) {
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800404 cur_freq = extract_freq(get_cur_val(mask, data), data);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700405 if (cur_freq == freq)
406 return 1;
407 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 return 0;
410}
411
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700412static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530413 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800415 struct acpi_cpufreq_data *data = policy->driver_data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700416 struct acpi_processor_performance *perf;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700417 struct drv_cmd cmd;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800418 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700419 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700421 if (unlikely(data == NULL ||
Dave Jones95dd7222006-10-18 00:41:48 -0400422 data->acpi_data == NULL || data->freq_table == NULL)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500426 perf = data->acpi_data;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530427 next_perf_state = data->freq_table[index].driver_data;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700428 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700429 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200430 pr_debug("Called after resume, resetting to P%d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700431 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700432 data->resume = 0;
433 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200434 pr_debug("Already at target state (P%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700435 next_perf_state);
Mike Travis4d8bb532009-01-04 05:18:08 -0800436 goto out;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700437 }
438 }
439
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700440 switch (data->cpu_feature) {
441 case SYSTEM_INTEL_MSR_CAPABLE:
442 cmd.type = SYSTEM_INTEL_MSR_CAPABLE;
443 cmd.addr.msr.reg = MSR_IA32_PERF_CTL;
Venki Pallipadi13424f62007-05-23 15:42:13 -0700444 cmd.val = (u32) perf->states[next_perf_state].control;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700445 break;
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000446 case SYSTEM_AMD_MSR_CAPABLE:
447 cmd.type = SYSTEM_AMD_MSR_CAPABLE;
448 cmd.addr.msr.reg = MSR_AMD_PERF_CTL;
449 cmd.val = (u32) perf->states[next_perf_state].control;
450 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700451 case SYSTEM_IO_CAPABLE:
452 cmd.type = SYSTEM_IO_CAPABLE;
453 cmd.addr.io.port = perf->control_register.address;
454 cmd.addr.io.bit_width = perf->control_register.bit_width;
455 cmd.val = (u32) perf->states[next_perf_state].control;
456 break;
457 default:
Mike Travis4d8bb532009-01-04 05:18:08 -0800458 result = -ENODEV;
459 goto out;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700460 }
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700461
Mike Travis4d8bb532009-01-04 05:18:08 -0800462 /* cpufreq holds the hotplug lock, so we are safe from here on */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700463 if (policy->shared_type != CPUFREQ_SHARED_TYPE_ANY)
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100464 cmd.mask = policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700465 else
Ingo Molnarbfa318a2009-01-15 15:46:08 +0100466 cmd.mask = cpumask_of(policy->cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700467
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700468 drv_write(&cmd);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500469
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700470 if (acpi_pstate_strict) {
Viresh Kumard4019f02013-08-14 19:38:24 +0530471 if (!check_freqs(cmd.mask, data->freq_table[index].frequency,
472 data)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200473 pr_debug("acpi_cpufreq_target failed (%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700474 policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800475 result = -EAGAIN;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500476 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500477 }
478
Viresh Kumare15d8302013-06-19 14:22:55 +0530479 if (!result)
480 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500481
Mike Travis4d8bb532009-01-04 05:18:08 -0800482out:
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700483 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700487acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700489 struct acpi_processor_performance *perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (cpu_khz) {
492 /* search the closest match to cpu_khz */
493 unsigned int i;
494 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500495 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dave Jones3a58df32009-01-17 22:36:14 -0500497 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400499 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500501 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700502 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
Dave Jones95dd7222006-10-18 00:41:48 -0400505 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700506 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500507 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500509 perf->state = 0;
510 return perf->states[0].core_frequency * 1000;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800514static void free_acpi_perf_data(void)
515{
516 unsigned int i;
517
518 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
519 for_each_possible_cpu(i)
520 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
521 ->shared_cpu_map);
522 free_percpu(acpi_perf_data);
523}
524
Andre Przywara615b7302012-09-04 08:28:07 +0000525static int boost_notify(struct notifier_block *nb, unsigned long action,
526 void *hcpu)
527{
528 unsigned cpu = (long)hcpu;
529 const struct cpumask *cpumask;
530
531 cpumask = get_cpu_mask(cpu);
532
533 /*
534 * Clear the boost-disable bit on the CPU_DOWN path so that
535 * this cpu cannot block the remaining ones from boosting. On
536 * the CPU_UP path we simply keep the boost-disable flag in
537 * sync with the current global state.
538 */
539
540 switch (action) {
541 case CPU_UP_PREPARE:
542 case CPU_UP_PREPARE_FROZEN:
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100543 boost_set_msrs(acpi_cpufreq_driver.boost_enabled, cpumask);
Andre Przywara615b7302012-09-04 08:28:07 +0000544 break;
545
546 case CPU_DOWN_PREPARE:
547 case CPU_DOWN_PREPARE_FROZEN:
548 boost_set_msrs(1, cpumask);
549 break;
550
551 default:
552 break;
553 }
554
555 return NOTIFY_OK;
556}
557
558
559static struct notifier_block boost_nb = {
560 .notifier_call = boost_notify,
561};
562
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500563/*
564 * acpi_cpufreq_early_init - initialize ACPI P-States library
565 *
566 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
567 * in order to determine correct frequency and voltage pairings. We can
568 * do _PDC and _PSD and find out the processor dependency for the
569 * actual init that will happen later...
570 */
Fenghua Yu50109292007-08-07 18:40:30 -0400571static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500572{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800573 unsigned int i;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200574 pr_debug("acpi_cpufreq_early_init\n");
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500575
Fenghua Yu50109292007-08-07 18:40:30 -0400576 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
577 if (!acpi_perf_data) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200578 pr_debug("Memory allocation error for acpi_perf_data.\n");
Fenghua Yu50109292007-08-07 18:40:30 -0400579 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500580 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800581 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700582 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800583 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
584 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800585
586 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
587 free_acpi_perf_data();
588 return -ENOMEM;
589 }
590 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500591
592 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700593 acpi_processor_preregister_performance(acpi_perf_data);
594 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500595}
596
Dave Jones95625b82006-10-21 01:37:39 -0400597#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700598/*
599 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
600 * or do it in BIOS firmware and won't inform about it to OS. If not
601 * detected, this has a side effect of making CPU run at a different speed
602 * than OS intended it to run at. Detect it and handle it cleanly.
603 */
604static int bios_with_sw_any_bug;
605
Jeff Garzik18552562007-10-03 15:15:40 -0400606static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700607{
608 bios_with_sw_any_bug = 1;
609 return 0;
610}
611
Jeff Garzik18552562007-10-03 15:15:40 -0400612static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700613 {
614 .callback = sw_any_bug_found,
615 .ident = "Supermicro Server X6DLP",
616 .matches = {
617 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
618 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
619 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
620 },
621 },
622 { }
623};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400624
625static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
626{
John Villalovos293afe42009-09-25 13:30:08 -0400627 /* Intel Xeon Processor 7100 Series Specification Update
628 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400629 * AL30: A Machine Check Exception (MCE) Occurring during an
630 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400631 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400632 if (c->x86_vendor == X86_VENDOR_INTEL) {
633 if ((c->x86 == 15) &&
634 (c->x86_model == 6) &&
John Villalovos293afe42009-09-25 13:30:08 -0400635 (c->x86_mask == 8)) {
636 printk(KERN_INFO "acpi-cpufreq: Intel(R) "
637 "Xeon(R) 7100 Errata AL30, processors may "
638 "lock up on frequency changes: disabling "
639 "acpi-cpufreq.\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400640 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400641 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400642 }
643 return 0;
644}
Dave Jones95625b82006-10-21 01:37:39 -0400645#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700646
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700647static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700649 unsigned int i;
650 unsigned int valid_states = 0;
651 unsigned int cpu = policy->cpu;
652 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700653 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200654 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700655 struct acpi_processor_performance *perf;
John Villalovos293afe42009-09-25 13:30:08 -0400656#ifdef CONFIG_SMP
657 static int blacklisted;
658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200660 pr_debug("acpi_cpufreq_cpu_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400662#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400663 if (blacklisted)
664 return blacklisted;
665 blacklisted = acpi_cpufreq_blacklist(c);
666 if (blacklisted)
667 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400668#endif
669
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530670 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700672 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800674 if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
675 result = -ENOMEM;
676 goto err_free;
677 }
678
Rusty Russellb36128c2009-02-20 16:29:08 +0900679 data->acpi_data = per_cpu_ptr(acpi_perf_data, cpu);
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800680 policy->driver_data = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700681
Dave Jones95dd7222006-10-18 00:41:48 -0400682 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700683 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500685 result = acpi_processor_register_performance(data->acpi_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (result)
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800687 goto err_free_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500689 perf = data->acpi_data;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500690 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400691
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400692 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400693 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400694 * coordination is required.
695 */
696 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700697 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800698 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700699 }
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800700 cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700701
702#ifdef CONFIG_SMP
703 dmi_check_system(sw_any_bug_dmi_table);
Fabio Baltieri2624f902013-01-31 09:44:40 +0000704 if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700705 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Bartosz Golaszewski3280c3c2015-05-26 15:11:33 +0200706 cpumask_copy(policy->cpus, topology_core_cpumask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700707 }
Andre Przywaraacd31622012-09-04 08:28:03 +0000708
709 if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
710 cpumask_clear(policy->cpus);
711 cpumask_set_cpu(cpu, policy->cpus);
Bartosz Golaszewski3280c3c2015-05-26 15:11:33 +0200712 cpumask_copy(data->freqdomain_cpus,
713 topology_sibling_cpumask(cpu));
Andre Przywaraacd31622012-09-04 08:28:03 +0000714 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
715 pr_info_once(PFX "overriding BIOS provided _PSD data\n");
716 }
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700717#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500720 if (perf->state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200721 pr_debug("No P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 result = -ENODEV;
723 goto err_unreg;
724 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500725
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700726 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 result = -ENODEV;
728 goto err_unreg;
729 }
730
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700731 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700732 case ACPI_ADR_SPACE_SYSTEM_IO:
Matthew Garrettc40a4512013-01-20 10:24:26 +0000733 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
734 boot_cpu_data.x86 == 0xf) {
735 pr_debug("AMD K8 systems must use native drivers.\n");
736 result = -ENODEV;
737 goto err_unreg;
738 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200739 pr_debug("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700740 data->cpu_feature = SYSTEM_IO_CAPABLE;
741 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700742 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200743 pr_debug("HARDWARE addr space\n");
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000744 if (check_est_cpu(cpu)) {
745 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
746 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700747 }
Matthew Garrett3dc9a632012-09-04 08:28:02 +0000748 if (check_amd_hwpstate_cpu(cpu)) {
749 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
750 break;
751 }
752 result = -ENODEV;
753 goto err_unreg;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700754 default:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200755 pr_debug("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700756 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700757 result = -ENODEV;
758 goto err_unreg;
759 }
760
Viresh Kumar71508a12014-03-28 19:11:46 +0530761 data->freq_table = kzalloc(sizeof(*data->freq_table) *
Dave Jones95dd7222006-10-18 00:41:48 -0400762 (perf->state_count+1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!data->freq_table) {
764 result = -ENOMEM;
765 goto err_unreg;
766 }
767
768 /* detect transition latency */
769 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500770 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700771 if ((perf->states[i].transition_latency * 1000) >
772 policy->cpuinfo.transition_latency)
773 policy->cpuinfo.transition_latency =
774 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700777 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
778 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
779 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700780 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perches61c8c672009-05-26 14:58:39 -0700781 printk_once(KERN_INFO
782 "P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500786 for (i = 0; i < perf->state_count; i++) {
787 if (i > 0 && perf->states[i].core_frequency >=
Zhang Rui3cdf5522007-06-13 21:24:02 -0400788 data->freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700789 continue;
790
Viresh Kumar50701582013-03-30 16:25:15 +0530791 data->freq_table[valid_states].driver_data = i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700792 data->freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700793 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700794 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Venkatesh Pallipadi3d4a7ef2006-11-13 17:47:44 -0800796 data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800797 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Viresh Kumar776b57b2013-09-16 18:56:07 +0530799 result = cpufreq_table_validate_and_show(policy, data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400800 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto err_freqfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Thomas Renningerd876dfb2009-04-17 16:22:08 +0200803 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
804 printk(KERN_WARNING FW_WARN "P-state 0 is not max freq\n");
805
Mattia Dongilia507ac42006-12-15 19:52:45 +0100806 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700807 case ACPI_ADR_SPACE_SYSTEM_IO:
Viresh Kumar1bab64d2013-10-16 23:58:10 +0200808 /*
809 * The core will not set policy->cur, because
810 * cpufreq_driver->get is NULL, so we need to set it here.
811 * However, we have to guess it, because the current speed is
812 * unknown and not detectable via IO ports.
813 */
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700814 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
815 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700816 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700817 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700818 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700819 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700820 break;
821 }
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* notify BIOS that we exist */
824 acpi_processor_notify_smm(THIS_MODULE);
825
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200826 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500827 for (i = 0; i < perf->state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200828 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700829 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500830 (u32) perf->states[i].core_frequency,
831 (u32) perf->states[i].power,
832 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400834 /*
835 * the first call to ->target() should result in us actually
836 * writing something to the appropriate registers.
837 */
838 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700839
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700840 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Jones95dd7222006-10-18 00:41:48 -0400842err_freqfree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kfree(data->freq_table);
Dave Jones95dd7222006-10-18 00:41:48 -0400844err_unreg:
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500845 acpi_processor_unregister_performance(perf, cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800846err_free_mask:
847 free_cpumask_var(data->freqdomain_cpus);
Dave Jones95dd7222006-10-18 00:41:48 -0400848err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree(data);
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800850 policy->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700852 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700855static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800857 struct acpi_cpufreq_data *data = policy->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200859 pr_debug("acpi_cpufreq_cpu_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (data) {
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800862 policy->driver_data = NULL;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700863 acpi_processor_unregister_performance(data->acpi_data,
864 policy->cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800865 free_cpumask_var(data->freqdomain_cpus);
Zhang Ruidab5fff2010-10-12 09:09:37 +0800866 kfree(data->freq_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kfree(data);
868 }
869
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700873static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800875 struct acpi_cpufreq_data *data = policy->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200877 pr_debug("acpi_cpufreq_resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 data->resume = 1;
880
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700884static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 &cpufreq_freq_attr_scaling_available_freqs,
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800886 &freqdomain_cpus,
Andre Przywara11269ff2012-09-04 08:28:08 +0000887 NULL, /* this is a placeholder for cpb, do not remove */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 NULL,
889};
890
891static struct cpufreq_driver acpi_cpufreq_driver = {
Viresh Kumardb9be212013-10-03 20:27:56 +0530892 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530893 .target_index = acpi_cpufreq_target,
Thomas Renningere2f74f32009-11-19 12:31:01 +0100894 .bios_limit = acpi_processor_get_bios_limit,
895 .init = acpi_cpufreq_cpu_init,
896 .exit = acpi_cpufreq_cpu_exit,
897 .resume = acpi_cpufreq_resume,
898 .name = "acpi-cpufreq",
Thomas Renningere2f74f32009-11-19 12:31:01 +0100899 .attr = acpi_cpufreq_attr,
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100900 .set_boost = _store_boost,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901};
902
Andre Przywara615b7302012-09-04 08:28:07 +0000903static void __init acpi_cpufreq_boost_init(void)
904{
905 if (boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA)) {
906 msrs = msrs_alloc();
907
908 if (!msrs)
909 return;
910
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100911 acpi_cpufreq_driver.boost_supported = true;
912 acpi_cpufreq_driver.boost_enabled = boost_state(0);
Srivatsa S. Bhat0197fbd2014-03-11 02:10:06 +0530913
914 cpu_notifier_register_begin();
Andre Przywara615b7302012-09-04 08:28:07 +0000915
916 /* Force all MSRs to the same value */
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100917 boost_set_msrs(acpi_cpufreq_driver.boost_enabled,
918 cpu_online_mask);
Andre Przywara615b7302012-09-04 08:28:07 +0000919
Srivatsa S. Bhat0197fbd2014-03-11 02:10:06 +0530920 __register_cpu_notifier(&boost_nb);
Andre Przywara615b7302012-09-04 08:28:07 +0000921
Srivatsa S. Bhat0197fbd2014-03-11 02:10:06 +0530922 cpu_notifier_register_done();
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100923 }
Andre Przywara615b7302012-09-04 08:28:07 +0000924}
925
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500926static void acpi_cpufreq_boost_exit(void)
Andre Przywara615b7302012-09-04 08:28:07 +0000927{
Andre Przywara615b7302012-09-04 08:28:07 +0000928 if (msrs) {
929 unregister_cpu_notifier(&boost_nb);
930
931 msrs_free(msrs);
932 msrs = NULL;
933 }
934}
935
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700936static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Fenghua Yu50109292007-08-07 18:40:30 -0400938 int ret;
939
Rafael J. Wysocki75c07582013-10-25 16:22:47 +0200940 if (acpi_disabled)
941 return -ENODEV;
942
Yinghai Lu8a61e122013-09-20 10:43:56 -0700943 /* don't keep reloading if cpufreq_driver exists */
944 if (cpufreq_get_current_driver())
Rafael J. Wysocki75c07582013-10-25 16:22:47 +0200945 return -EEXIST;
Yinghai Luee297532008-09-24 19:04:31 -0700946
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200947 pr_debug("acpi_cpufreq_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Fenghua Yu50109292007-08-07 18:40:30 -0400949 ret = acpi_cpufreq_early_init();
950 if (ret)
951 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500952
Andre Przywara11269ff2012-09-04 08:28:08 +0000953#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
954 /* this is a sysfs file with a strange name and an even stranger
955 * semantic - per CPU instantiation, but system global effect.
956 * Lets enable it only on AMD CPUs for compatibility reasons and
957 * only if configured. This is considered legacy code, which
958 * will probably be removed at some point in the future.
959 */
960 if (check_amd_hwpstate_cpu(0)) {
961 struct freq_attr **iter;
962
963 pr_debug("adding sysfs entry for cpb\n");
964
965 for (iter = acpi_cpufreq_attr; *iter != NULL; iter++)
966 ;
967
968 /* make sure there is a terminator behind it */
969 if (iter[1] == NULL)
970 *iter = &cpb;
971 }
972#endif
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100973 acpi_cpufreq_boost_init();
Andre Przywara11269ff2012-09-04 08:28:08 +0000974
Akinobu Mita847aef62008-07-14 11:59:44 +0900975 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500976 if (ret) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800977 free_acpi_perf_data();
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500978 acpi_cpufreq_boost_exit();
979 }
Akinobu Mita847aef62008-07-14 11:59:44 +0900980 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700983static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200985 pr_debug("acpi_cpufreq_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Andre Przywara615b7302012-09-04 08:28:07 +0000987 acpi_cpufreq_boost_exit();
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 cpufreq_unregister_driver(&acpi_cpufreq_driver);
990
Luming Yu50f4ddd2011-07-08 16:37:44 -0400991 free_acpi_perf_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400994module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700995MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400996 "value 0 or non-zero. non-zero -> strict ACPI checks are "
997 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999late_initcall(acpi_cpufreq_init);
1000module_exit(acpi_cpufreq_exit);
1001
Matthew Garrettefa17192013-01-22 22:33:46 +01001002static const struct x86_cpu_id acpi_cpufreq_ids[] = {
1003 X86_FEATURE_MATCH(X86_FEATURE_ACPI),
1004 X86_FEATURE_MATCH(X86_FEATURE_HW_PSTATE),
1005 {}
1006};
1007MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
1008
Rafael J. Wysockic655aff2013-06-07 13:13:31 +02001009static const struct acpi_device_id processor_device_ids[] = {
1010 {ACPI_PROCESSOR_OBJECT_HID, },
1011 {ACPI_PROCESSOR_DEVICE_HID, },
1012 {},
1013};
1014MODULE_DEVICE_TABLE(acpi, processor_device_ids);
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016MODULE_ALIAS("acpi");