blob: 289e8ce3fd13d254d7f16a6353868196d404b326 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Dave Jones3a58df32009-01-17 22:36:14 -05003 * acpi-cpufreq.c - ACPI Processor P-States Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -07008 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Joe Perches1c5864e2016-04-05 13:28:25 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070016#include <linux/smp.h>
17#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/cpufreq.h>
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040019#include <linux/compiler.h>
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -070020#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/acpi.h>
Dave Jones3a58df32009-01-17 22:36:14 -050024#include <linux/io.h>
25#include <linux/delay.h>
26#include <linux/uaccess.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <acpi/processor.h>
29
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070030#include <asm/msr.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070031#include <asm/processor.h>
32#include <asm/cpufeature.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010033#include <asm/cpu_device_id.h>
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
36MODULE_DESCRIPTION("ACPI Processor P-States Driver");
37MODULE_LICENSE("GPL");
38
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070039enum {
40 UNDEFINED_CAPABLE = 0,
41 SYSTEM_INTEL_MSR_CAPABLE,
Matthew Garrett3dc9a6332012-09-04 08:28:02 +000042 SYSTEM_AMD_MSR_CAPABLE,
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070043 SYSTEM_IO_CAPABLE,
44};
45
46#define INTEL_MSR_RANGE (0xffff)
Matthew Garrett3dc9a6332012-09-04 08:28:02 +000047#define AMD_MSR_RANGE (0x7)
Pu Wencc9690c2018-09-23 17:37:38 +080048#define HYGON_MSR_RANGE (0x7)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -070049
Andre Przywara615b7302012-09-04 08:28:07 +000050#define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
51
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -070052struct acpi_cpufreq_data {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -070053 unsigned int resume;
54 unsigned int cpu_feature;
Pan Xinhui8cfcfd32015-07-10 14:36:20 +080055 unsigned int acpi_perf_cpu;
Lan Tianyuf4fd3792013-06-27 15:08:54 +080056 cpumask_var_t freqdomain_cpus;
Rafael J. Wysockied757a22016-03-02 03:05:22 +010057 void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val);
58 u32 (*cpu_freq_read)(struct acpi_pct_register *reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Fenghua Yu50109292007-08-07 18:40:30 -040061/* acpi_perf_data is a pointer to percpu data. */
Namhyung Kim3f6c4df2010-08-13 23:00:11 +090062static struct acpi_processor_performance __percpu *acpi_perf_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Rafael J. Wysocki34276162015-07-22 22:11:56 +020064static inline struct acpi_processor_performance *to_perf_data(struct acpi_cpufreq_data *data)
65{
66 return per_cpu_ptr(acpi_perf_data, data->acpi_perf_cpu);
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static struct cpufreq_driver acpi_cpufreq_driver;
70
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -040071static unsigned int acpi_pstate_strict;
Andre Przywara615b7302012-09-04 08:28:07 +000072
73static bool boost_state(unsigned int cpu)
74{
75 u32 lo, hi;
76 u64 msr;
77
78 switch (boot_cpu_data.x86_vendor) {
79 case X86_VENDOR_INTEL:
80 rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
81 msr = lo | ((u64)hi << 32);
82 return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
Pu Wencc9690c2018-09-23 17:37:38 +080083 case X86_VENDOR_HYGON:
Andre Przywara615b7302012-09-04 08:28:07 +000084 case X86_VENDOR_AMD:
85 rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
86 msr = lo | ((u64)hi << 32);
87 return !(msr & MSR_K7_HWCR_CPB_DIS);
88 }
89 return false;
90}
91
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +010092static int boost_set_msr(bool enable)
Andre Przywara615b7302012-09-04 08:28:07 +000093{
Andre Przywara615b7302012-09-04 08:28:07 +000094 u32 msr_addr;
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +010095 u64 msr_mask, val;
Andre Przywara615b7302012-09-04 08:28:07 +000096
97 switch (boot_cpu_data.x86_vendor) {
98 case X86_VENDOR_INTEL:
99 msr_addr = MSR_IA32_MISC_ENABLE;
100 msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
101 break;
Pu Wencc9690c2018-09-23 17:37:38 +0800102 case X86_VENDOR_HYGON:
Andre Przywara615b7302012-09-04 08:28:07 +0000103 case X86_VENDOR_AMD:
104 msr_addr = MSR_K7_HWCR;
105 msr_mask = MSR_K7_HWCR_CPB_DIS;
106 break;
107 default:
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100108 return -EINVAL;
Andre Przywara615b7302012-09-04 08:28:07 +0000109 }
110
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100111 rdmsrl(msr_addr, val);
Andre Przywara615b7302012-09-04 08:28:07 +0000112
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100113 if (enable)
114 val &= ~msr_mask;
115 else
116 val |= msr_mask;
Andre Przywara615b7302012-09-04 08:28:07 +0000117
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100118 wrmsrl(msr_addr, val);
119 return 0;
120}
121
122static void boost_set_msr_each(void *p_en)
123{
124 bool enable = (bool) p_en;
125
126 boost_set_msr(enable);
Andre Przywara615b7302012-09-04 08:28:07 +0000127}
128
Rafael J. Wysocki17135782015-12-27 00:25:35 +0100129static int set_boost(int val)
Andre Przywara615b7302012-09-04 08:28:07 +0000130{
Andre Przywara615b7302012-09-04 08:28:07 +0000131 get_online_cpus();
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100132 on_each_cpu(boost_set_msr_each, (void *)(long)val, 1);
Andre Przywara615b7302012-09-04 08:28:07 +0000133 put_online_cpus();
Andre Przywara615b7302012-09-04 08:28:07 +0000134 pr_debug("Core Boosting %sabled.\n", val ? "en" : "dis");
135
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100136 return 0;
Andre Przywara615b7302012-09-04 08:28:07 +0000137}
138
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800139static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
140{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800141 struct acpi_cpufreq_data *data = policy->driver_data;
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800142
Srinivas Pandruvadae2530362015-10-07 13:50:43 -0700143 if (unlikely(!data))
144 return -ENODEV;
145
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800146 return cpufreq_show_cpus(data->freqdomain_cpus, buf);
147}
148
149cpufreq_freq_attr_ro(freqdomain_cpus);
150
Andre Przywara11269ff2012-09-04 08:28:08 +0000151#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
Rafael J. Wysocki17135782015-12-27 00:25:35 +0100152static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
153 size_t count)
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100154{
155 int ret;
Rafael J. Wysocki17135782015-12-27 00:25:35 +0100156 unsigned int val = 0;
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100157
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +0100158 if (!acpi_cpufreq_driver.set_boost)
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100159 return -EINVAL;
160
Rafael J. Wysocki17135782015-12-27 00:25:35 +0100161 ret = kstrtouint(buf, 10, &val);
162 if (ret || val > 1)
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100163 return -EINVAL;
164
Rafael J. Wysocki17135782015-12-27 00:25:35 +0100165 set_boost(val);
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100166
167 return count;
168}
169
Andre Przywara11269ff2012-09-04 08:28:08 +0000170static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
171{
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100172 return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
Andre Przywara11269ff2012-09-04 08:28:08 +0000173}
174
Lan Tianyu59027d32013-08-13 10:05:53 +0800175cpufreq_freq_attr_rw(cpb);
Andre Przywara11269ff2012-09-04 08:28:08 +0000176#endif
177
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700178static int check_est_cpu(unsigned int cpuid)
179{
Mike Travis92cb7612007-10-19 20:35:04 +0200180 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700181
Harald Welte0de51082009-06-08 18:27:54 +0800182 return cpu_has(cpu, X86_FEATURE_EST);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700183}
184
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000185static int check_amd_hwpstate_cpu(unsigned int cpuid)
186{
187 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
188
189 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
190}
191
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530192static unsigned extract_io(struct cpufreq_policy *policy, u32 value)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700193{
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530194 struct acpi_cpufreq_data *data = policy->driver_data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700195 struct acpi_processor_performance *perf;
196 int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700197
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200198 perf = to_perf_data(data);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700199
Dave Jones3a58df32009-01-17 22:36:14 -0500200 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700201 if (value == perf->states[i].status)
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530202 return policy->freq_table[i].frequency;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700203 }
204 return 0;
205}
206
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530207static unsigned extract_msr(struct cpufreq_policy *policy, u32 msr)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700208{
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530209 struct acpi_cpufreq_data *data = policy->driver_data;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300210 struct cpufreq_frequency_table *pos;
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700211 struct acpi_processor_performance *perf;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700212
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000213 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
214 msr &= AMD_MSR_RANGE;
Pu Wencc9690c2018-09-23 17:37:38 +0800215 else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
216 msr &= HYGON_MSR_RANGE;
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000217 else
218 msr &= INTEL_MSR_RANGE;
219
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200220 perf = to_perf_data(data);
Venkatesh Pallipadia6f6e6e62006-10-03 12:37:42 -0700221
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530222 cpufreq_for_each_entry(pos, policy->freq_table)
Stratos Karafotis041526f2014-04-25 23:15:38 +0300223 if (msr == perf->states[pos->driver_data].status)
224 return pos->frequency;
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530225 return policy->freq_table[0].frequency;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700226}
227
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530228static unsigned extract_freq(struct cpufreq_policy *policy, u32 val)
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700229{
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530230 struct acpi_cpufreq_data *data = policy->driver_data;
231
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700232 switch (data->cpu_feature) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700233 case SYSTEM_INTEL_MSR_CAPABLE:
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000234 case SYSTEM_AMD_MSR_CAPABLE:
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530235 return extract_msr(policy, val);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700236 case SYSTEM_IO_CAPABLE:
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530237 return extract_io(policy, val);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700238 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700239 return 0;
240 }
241}
242
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800243static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100244{
245 u32 val, dummy;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700246
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100247 rdmsr(MSR_IA32_PERF_CTL, val, dummy);
248 return val;
249}
250
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800251static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100252{
253 u32 lo, hi;
254
255 rdmsr(MSR_IA32_PERF_CTL, lo, hi);
256 lo = (lo & ~INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE);
257 wrmsr(MSR_IA32_PERF_CTL, lo, hi);
258}
259
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800260static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100261{
262 u32 val, dummy;
263
264 rdmsr(MSR_AMD_PERF_CTL, val, dummy);
265 return val;
266}
267
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800268static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100269{
270 wrmsr(MSR_AMD_PERF_CTL, val, 0);
271}
272
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800273static u32 cpu_freq_read_io(struct acpi_pct_register *reg)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100274{
275 u32 val;
276
277 acpi_os_read_port(reg->address, &val, reg->bit_width);
278 return val;
279}
280
Jisheng Zhangac13b9962016-03-22 22:34:30 +0800281static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val)
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100282{
283 acpi_os_write_port(reg->address, val, reg->bit_width);
284}
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700285
286struct drv_cmd {
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100287 struct acpi_pct_register *reg;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700288 u32 val;
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100289 union {
290 void (*write)(struct acpi_pct_register *reg, u32 val);
291 u32 (*read)(struct acpi_pct_register *reg);
292 } func;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700293};
294
Andrew Morton01599fc2009-04-13 10:27:49 -0700295/* Called via smp_call_function_single(), on the target CPU */
296static void do_drv_read(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700297{
Mike Travis72859082009-01-16 15:31:15 -0800298 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700299
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100300 cmd->val = cmd->func.read(cmd->reg);
301}
302
303static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)
304{
305 struct acpi_processor_performance *perf = to_perf_data(data);
306 struct drv_cmd cmd = {
307 .reg = &perf->control_register,
308 .func.read = data->cpu_freq_read,
309 };
310 int err;
311
312 err = smp_call_function_any(mask, do_drv_read, &cmd, 1);
313 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */
314 return cmd.val;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700315}
316
Andrew Morton01599fc2009-04-13 10:27:49 -0700317/* Called via smp_call_function_many(), on the target CPUs */
318static void do_drv_write(void *_cmd)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700319{
Mike Travis72859082009-01-16 15:31:15 -0800320 struct drv_cmd *cmd = _cmd;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700321
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100322 cmd->func.write(cmd->reg, cmd->val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700323}
324
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100325static void drv_write(struct acpi_cpufreq_data *data,
326 const struct cpumask *mask, u32 val)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700327{
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100328 struct acpi_processor_performance *perf = to_perf_data(data);
329 struct drv_cmd cmd = {
330 .reg = &perf->control_register,
331 .val = val,
332 .func.write = data->cpu_freq_write,
333 };
Linus Torvaldsea34f432009-04-15 08:05:13 -0700334 int this_cpu;
335
336 this_cpu = get_cpu();
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100337 if (cpumask_test_cpu(this_cpu, mask))
338 do_drv_write(&cmd);
339
340 smp_call_function_many(mask, do_drv_write, &cmd, 1);
Linus Torvaldsea34f432009-04-15 08:05:13 -0700341 put_cpu();
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700342}
343
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100344static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700345{
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100346 u32 val;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700347
Mike Travis4d8bb532009-01-04 05:18:08 -0800348 if (unlikely(cpumask_empty(mask)))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700349 return 0;
350
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100351 val = drv_read(data, mask);
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700352
Mohan Kumareae2ef02019-04-15 14:03:58 +0300353 pr_debug("%s = %u\n", __func__, val);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700354
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100355 return val;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700356}
357
358static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
359{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800360 struct acpi_cpufreq_data *data;
361 struct cpufreq_policy *policy;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700362 unsigned int freq;
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400363 unsigned int cached_freq;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700364
Mohan Kumareae2ef02019-04-15 14:03:58 +0300365 pr_debug("%s (%d)\n", __func__, cpu);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700366
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200367 policy = cpufreq_cpu_get_raw(cpu);
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800368 if (unlikely(!policy))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700369 return 0;
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800370
371 data = policy->driver_data;
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530372 if (unlikely(!data || !policy->freq_table))
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800373 return 0;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700374
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530375 cached_freq = policy->freq_table[to_perf_data(data)->state].frequency;
376 freq = extract_freq(policy, get_cur_val(cpumask_of(cpu), data));
Venkatesh Pallipadie56a7272008-04-28 15:13:43 -0400377 if (freq != cached_freq) {
378 /*
379 * The dreaded BIOS frequency change behind our back.
380 * Force set the frequency on next target call.
381 */
382 data->resume = 1;
383 }
384
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200385 pr_debug("cur freq = %u\n", freq);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700386
387 return freq;
388}
389
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530390static unsigned int check_freqs(struct cpufreq_policy *policy,
391 const struct cpumask *mask, unsigned int freq)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700392{
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530393 struct acpi_cpufreq_data *data = policy->driver_data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700394 unsigned int cur_freq;
395 unsigned int i;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700396
Dave Jones3a58df32009-01-17 22:36:14 -0500397 for (i = 0; i < 100; i++) {
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530398 cur_freq = extract_freq(policy, get_cur_val(mask, data));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700399 if (cur_freq == freq)
400 return 1;
401 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return 0;
404}
405
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700406static int acpi_cpufreq_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530407 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800409 struct acpi_cpufreq_data *data = policy->driver_data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700410 struct acpi_processor_performance *perf;
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100411 const struct cpumask *mask;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800412 unsigned int next_perf_state = 0; /* Index into perf table */
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700413 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530415 if (unlikely(!data)) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700416 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200419 perf = to_perf_data(data);
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530420 next_perf_state = policy->freq_table[index].driver_data;
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700421 if (perf->state == next_perf_state) {
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700422 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200423 pr_debug("Called after resume, resetting to P%d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700424 next_perf_state);
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700425 data->resume = 0;
426 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200427 pr_debug("Already at target state (P%d)\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700428 next_perf_state);
Rafael J. Wysocki9a909a12016-02-26 00:03:58 +0100429 return 0;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700430 }
431 }
432
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100433 /*
434 * The core won't allow CPUs to go away until the governor has been
435 * stopped, so we can rely on the stability of policy->cpus.
436 */
437 mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ?
438 cpumask_of(policy->cpu) : policy->cpus;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700439
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100440 drv_write(data, mask, perf->states[next_perf_state].control);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500441
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700442 if (acpi_pstate_strict) {
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530443 if (!check_freqs(policy, mask,
444 policy->freq_table[index].frequency)) {
Mohan Kumareae2ef02019-04-15 14:03:58 +0300445 pr_debug("%s (%d)\n", __func__, policy->cpu);
Mike Travis4d8bb532009-01-04 05:18:08 -0800446 result = -EAGAIN;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500447 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500448 }
449
Viresh Kumare15d8302013-06-19 14:22:55 +0530450 if (!result)
451 perf->state = next_perf_state;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500452
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700453 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Colin Ian King08e9cc42018-06-01 14:05:12 +0100456static unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
457 unsigned int target_freq)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200458{
459 struct acpi_cpufreq_data *data = policy->driver_data;
460 struct acpi_processor_performance *perf;
461 struct cpufreq_frequency_table *entry;
Viresh Kumar82577362016-06-27 09:59:34 +0530462 unsigned int next_perf_state, next_freq, index;
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200463
464 /*
465 * Find the closest frequency above target_freq.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200466 */
Steve Muckle5b6667c2016-07-13 13:25:27 -0700467 if (policy->cached_target_freq == target_freq)
468 index = policy->cached_resolved_idx;
469 else
470 index = cpufreq_table_find_index_dl(policy, target_freq);
Viresh Kumar82577362016-06-27 09:59:34 +0530471
472 entry = &policy->freq_table[index];
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200473 next_freq = entry->frequency;
474 next_perf_state = entry->driver_data;
475
476 perf = to_perf_data(data);
477 if (perf->state == next_perf_state) {
478 if (unlikely(data->resume))
479 data->resume = 0;
480 else
481 return next_freq;
482 }
483
484 data->cpu_freq_write(&perf->control_register,
485 perf->states[next_perf_state].control);
486 perf->state = next_perf_state;
487 return next_freq;
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static unsigned long
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700491acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200493 struct acpi_processor_performance *perf;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500494
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200495 perf = to_perf_data(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (cpu_khz) {
497 /* search the closest match to cpu_khz */
498 unsigned int i;
499 unsigned long freq;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500500 unsigned long freqn = perf->states[0].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Dave Jones3a58df32009-01-17 22:36:14 -0500502 for (i = 0; i < (perf->state_count-1); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 freq = freqn;
Dave Jones95dd7222006-10-18 00:41:48 -0400504 freqn = perf->states[i+1].core_frequency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if ((2 * cpu_khz) > (freqn + freq)) {
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500506 perf->state = i;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700507 return freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
Dave Jones95dd7222006-10-18 00:41:48 -0400510 perf->state = perf->state_count-1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700511 return freqn;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500512 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* assume CPU is at P0... */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500514 perf->state = 0;
515 return perf->states[0].core_frequency * 1000;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800519static void free_acpi_perf_data(void)
520{
521 unsigned int i;
522
523 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
524 for_each_possible_cpu(i)
525 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
526 ->shared_cpu_map);
527 free_percpu(acpi_perf_data);
528}
529
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100530static int cpufreq_boost_online(unsigned int cpu)
Andre Przywara615b7302012-09-04 08:28:07 +0000531{
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100532 /*
533 * On the CPU_UP path we simply keep the boost-disable flag
534 * in sync with the current global state.
535 */
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100536 return boost_set_msr(acpi_cpufreq_driver.boost_enabled);
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100537}
538
539static int cpufreq_boost_down_prep(unsigned int cpu)
540{
Andre Przywara615b7302012-09-04 08:28:07 +0000541 /*
542 * Clear the boost-disable bit on the CPU_DOWN path so that
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100543 * this cpu cannot block the remaining ones from boosting.
Andre Przywara615b7302012-09-04 08:28:07 +0000544 */
Sebastian Andrzej Siewiora3605c42016-11-28 10:52:03 +0100545 return boost_set_msr(1);
Andre Przywara615b7302012-09-04 08:28:07 +0000546}
547
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500548/*
549 * acpi_cpufreq_early_init - initialize ACPI P-States library
550 *
551 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
552 * in order to determine correct frequency and voltage pairings. We can
553 * do _PDC and _PSD and find out the processor dependency for the
554 * actual init that will happen later...
555 */
Fenghua Yu50109292007-08-07 18:40:30 -0400556static int __init acpi_cpufreq_early_init(void)
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500557{
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800558 unsigned int i;
Mohan Kumareae2ef02019-04-15 14:03:58 +0300559 pr_debug("%s\n", __func__);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500560
Fenghua Yu50109292007-08-07 18:40:30 -0400561 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
562 if (!acpi_perf_data) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200563 pr_debug("Memory allocation error for acpi_perf_data.\n");
Fenghua Yu50109292007-08-07 18:40:30 -0400564 return -ENOMEM;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500565 }
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800566 for_each_possible_cpu(i) {
Yinghai Lueaa95842009-06-06 14:51:36 -0700567 if (!zalloc_cpumask_var_node(
Mike Travis80855f72008-12-31 18:08:47 -0800568 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
569 GFP_KERNEL, cpu_to_node(i))) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800570
571 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
572 free_acpi_perf_data();
573 return -ENOMEM;
574 }
575 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500576
577 /* Do initialization in ACPI core */
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700578 acpi_processor_preregister_performance(acpi_perf_data);
579 return 0;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500580}
581
Dave Jones95625b82006-10-21 01:37:39 -0400582#ifdef CONFIG_SMP
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700583/*
584 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
585 * or do it in BIOS firmware and won't inform about it to OS. If not
586 * detected, this has a side effect of making CPU run at a different speed
587 * than OS intended it to run at. Detect it and handle it cleanly.
588 */
589static int bios_with_sw_any_bug;
590
Jeff Garzik18552562007-10-03 15:15:40 -0400591static int sw_any_bug_found(const struct dmi_system_id *d)
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700592{
593 bios_with_sw_any_bug = 1;
594 return 0;
595}
596
Jeff Garzik18552562007-10-03 15:15:40 -0400597static const struct dmi_system_id sw_any_bug_dmi_table[] = {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700598 {
599 .callback = sw_any_bug_found,
600 .ident = "Supermicro Server X6DLP",
601 .matches = {
602 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
603 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
604 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
605 },
606 },
607 { }
608};
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400609
610static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
611{
John Villalovos293afe42009-09-25 13:30:08 -0400612 /* Intel Xeon Processor 7100 Series Specification Update
613 * http://www.intel.com/Assets/PDF/specupdate/314554.pdf
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400614 * AL30: A Machine Check Exception (MCE) Occurring during an
615 * Enhanced Intel SpeedStep Technology Ratio Change May Cause
John Villalovos293afe42009-09-25 13:30:08 -0400616 * Both Processor Cores to Lock Up. */
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400617 if (c->x86_vendor == X86_VENDOR_INTEL) {
618 if ((c->x86 == 15) &&
619 (c->x86_model == 6) &&
Jia Zhangb3991512018-01-01 09:52:10 +0800620 (c->x86_stepping == 8)) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700621 pr_info("Intel(R) Xeon(R) 7100 Errata AL30, processors may lock up on frequency changes: disabling acpi-cpufreq\n");
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400622 return -ENODEV;
John Villalovos293afe42009-09-25 13:30:08 -0400623 }
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400624 }
625 return 0;
626}
Dave Jones95625b82006-10-21 01:37:39 -0400627#endif
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700628
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700629static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700631 unsigned int i;
632 unsigned int valid_states = 0;
633 unsigned int cpu = policy->cpu;
634 struct acpi_cpufreq_data *data;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700635 unsigned int result = 0;
Mike Travis92cb7612007-10-19 20:35:04 +0200636 struct cpuinfo_x86 *c = &cpu_data(policy->cpu);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700637 struct acpi_processor_performance *perf;
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530638 struct cpufreq_frequency_table *freq_table;
John Villalovos293afe42009-09-25 13:30:08 -0400639#ifdef CONFIG_SMP
640 static int blacklisted;
641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Mohan Kumareae2ef02019-04-15 14:03:58 +0300643 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400645#ifdef CONFIG_SMP
John Villalovos293afe42009-09-25 13:30:08 -0400646 if (blacklisted)
647 return blacklisted;
648 blacklisted = acpi_cpufreq_blacklist(c);
649 if (blacklisted)
650 return blacklisted;
Prarit Bhargava1a8e42f2009-08-26 13:19:37 -0400651#endif
652
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530653 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!data)
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800657 if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
658 result = -ENOMEM;
659 goto err_free;
660 }
661
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200662 perf = per_cpu_ptr(acpi_perf_data, cpu);
Pan Xinhui8cfcfd32015-07-10 14:36:20 +0800663 data->acpi_perf_cpu = cpu;
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800664 policy->driver_data = data;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700665
Dave Jones95dd7222006-10-18 00:41:48 -0400666 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700667 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Rafael J. Wysocki34276162015-07-22 22:11:56 +0200669 result = acpi_processor_register_performance(perf, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (result)
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800671 goto err_free_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500673 policy->shared_type = perf->shared_type;
Dave Jones95dd7222006-10-18 00:41:48 -0400674
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400675 /*
Dave Jones95dd7222006-10-18 00:41:48 -0400676 * Will let policy->cpus know about dependency only when software
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400677 * coordination is required.
678 */
679 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700680 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
Rusty Russell835481d2009-01-04 05:18:06 -0800681 cpumask_copy(policy->cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700682 }
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800683 cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700684
685#ifdef CONFIG_SMP
686 dmi_check_system(sw_any_bug_dmi_table);
Fabio Baltieri2624f902013-01-31 09:44:40 +0000687 if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700688 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Bartosz Golaszewski3280c3c2015-05-26 15:11:33 +0200689 cpumask_copy(policy->cpus, topology_core_cpumask(cpu));
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700690 }
Andre Przywaraacd31622012-09-04 08:28:03 +0000691
692 if (check_amd_hwpstate_cpu(cpu) && !acpi_pstate_strict) {
693 cpumask_clear(policy->cpus);
694 cpumask_set_cpu(cpu, policy->cpus);
Bartosz Golaszewski3280c3c2015-05-26 15:11:33 +0200695 cpumask_copy(data->freqdomain_cpus,
696 topology_sibling_cpumask(cpu));
Andre Przywaraacd31622012-09-04 08:28:03 +0000697 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
Joe Perches1c5864e2016-04-05 13:28:25 -0700698 pr_info_once("overriding BIOS provided _PSD data\n");
Andre Przywaraacd31622012-09-04 08:28:03 +0000699 }
Venkatesh Pallipadi8adcc0c2006-09-01 14:02:24 -0700700#endif
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* capability check */
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500703 if (perf->state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200704 pr_debug("No P-States\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 result = -ENODEV;
706 goto err_unreg;
707 }
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500708
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700709 if (perf->control_register.space_id != perf->status_register.space_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 result = -ENODEV;
711 goto err_unreg;
712 }
713
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700714 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700715 case ACPI_ADR_SPACE_SYSTEM_IO:
Matthew Garrettc40a4512013-01-20 10:24:26 +0000716 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
717 boot_cpu_data.x86 == 0xf) {
718 pr_debug("AMD K8 systems must use native drivers.\n");
719 result = -ENODEV;
720 goto err_unreg;
721 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200722 pr_debug("SYSTEM IO addr space\n");
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700723 data->cpu_feature = SYSTEM_IO_CAPABLE;
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100724 data->cpu_freq_read = cpu_freq_read_io;
725 data->cpu_freq_write = cpu_freq_write_io;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700726 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700727 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200728 pr_debug("HARDWARE addr space\n");
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000729 if (check_est_cpu(cpu)) {
730 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100731 data->cpu_freq_read = cpu_freq_read_intel;
732 data->cpu_freq_write = cpu_freq_write_intel;
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000733 break;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700734 }
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000735 if (check_amd_hwpstate_cpu(cpu)) {
736 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
Rafael J. Wysockied757a22016-03-02 03:05:22 +0100737 data->cpu_freq_read = cpu_freq_read_amd;
738 data->cpu_freq_write = cpu_freq_write_amd;
Matthew Garrett3dc9a6332012-09-04 08:28:02 +0000739 break;
740 }
741 result = -ENODEV;
742 goto err_unreg;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700743 default:
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200744 pr_debug("Unknown addr space %d\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700745 (u32) (perf->control_register.space_id));
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700746 result = -ENODEV;
747 goto err_unreg;
748 }
749
Kees Cook6396bb22018-06-12 14:03:40 -0700750 freq_table = kcalloc(perf->state_count + 1, sizeof(*freq_table),
751 GFP_KERNEL);
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530752 if (!freq_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 result = -ENOMEM;
754 goto err_unreg;
755 }
756
757 /* detect transition latency */
758 policy->cpuinfo.transition_latency = 0;
Dave Jones3a58df32009-01-17 22:36:14 -0500759 for (i = 0; i < perf->state_count; i++) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700760 if ((perf->states[i].transition_latency * 1000) >
761 policy->cpuinfo.transition_latency)
762 policy->cpuinfo.transition_latency =
763 perf->states[i].transition_latency * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700766 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */
767 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
768 policy->cpuinfo.transition_latency > 20 * 1000) {
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700769 policy->cpuinfo.transition_latency = 20 * 1000;
Joe Perchesb49c22a2016-04-05 13:28:24 -0700770 pr_info_once("P-state transition latency capped at 20 uS\n");
Pallipadi, Venkatesha59d1632009-03-19 14:41:40 -0700771 }
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* table init */
Dave Jones3a58df32009-01-17 22:36:14 -0500774 for (i = 0; i < perf->state_count; i++) {
775 if (i > 0 && perf->states[i].core_frequency >=
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530776 freq_table[valid_states-1].frequency / 1000)
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700777 continue;
778
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530779 freq_table[valid_states].driver_data = i;
780 freq_table[valid_states].frequency =
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700781 perf->states[i].core_frequency * 1000;
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700782 valid_states++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530784 freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
Viresh Kumar1a186d92018-02-26 10:38:46 +0530785 policy->freq_table = freq_table;
Venkatesh Pallipadi8edc59d92006-12-19 12:58:55 -0800786 perf->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Mattia Dongilia507ac42006-12-15 19:52:45 +0100788 switch (perf->control_register.space_id) {
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700789 case ACPI_ADR_SPACE_SYSTEM_IO:
Viresh Kumar1bab64d2013-10-16 23:58:10 +0200790 /*
791 * The core will not set policy->cur, because
792 * cpufreq_driver->get is NULL, so we need to set it here.
793 * However, we have to guess it, because the current speed is
794 * unknown and not detectable via IO ports.
795 */
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700796 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
797 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700798 case ACPI_ADR_SPACE_FIXED_HARDWARE:
Venkatesh Pallipadi7650b282006-10-03 12:36:30 -0700799 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700800 break;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700801 default:
Venkatesh Pallipadidde9f7b2006-10-03 12:33:14 -0700802 break;
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* notify BIOS that we exist */
806 acpi_processor_notify_smm(THIS_MODULE);
807
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200808 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500809 for (i = 0; i < perf->state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200810 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700811 (i == perf->state ? '*' : ' '), i,
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500812 (u32) perf->states[i].core_frequency,
813 (u32) perf->states[i].power,
814 (u32) perf->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dominik Brodowski4b31e772005-05-18 13:49:00 -0400816 /*
817 * the first call to ->target() should result in us actually
818 * writing something to the appropriate registers.
819 */
820 data->resume = 1;
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700821
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200822 policy->fast_switch_possible = !acpi_pstate_strict &&
823 !(policy_is_shared(policy) && policy->shared_type != CPUFREQ_SHARED_TYPE_ANY);
824
Venkatesh Pallipadife27cb32006-10-03 12:29:15 -0700825 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dave Jones95dd7222006-10-18 00:41:48 -0400827err_unreg:
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200828 acpi_processor_unregister_performance(cpu);
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800829err_free_mask:
830 free_cpumask_var(data->freqdomain_cpus);
Dave Jones95dd7222006-10-18 00:41:48 -0400831err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 kfree(data);
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800833 policy->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700835 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700838static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800840 struct acpi_cpufreq_data *data = policy->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Mohan Kumareae2ef02019-04-15 14:03:58 +0300842 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Viresh Kumar9b55f552016-04-07 14:06:58 +0530844 policy->fast_switch_possible = false;
845 policy->driver_data = NULL;
846 acpi_processor_unregister_performance(data->acpi_perf_cpu);
847 free_cpumask_var(data->freqdomain_cpus);
Viresh Kumar8cee1ee2016-04-07 14:06:57 +0530848 kfree(policy->freq_table);
Viresh Kumar9b55f552016-04-07 14:06:58 +0530849 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Viresh Kumar1a186d92018-02-26 10:38:46 +0530854static void acpi_cpufreq_cpu_ready(struct cpufreq_policy *policy)
855{
856 struct acpi_processor_performance *perf = per_cpu_ptr(acpi_perf_data,
857 policy->cpu);
858
859 if (perf->states[0].core_frequency * 1000 != policy->cpuinfo.max_freq)
860 pr_warn(FW_WARN "P-state 0 is not max freq\n");
861}
862
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700863static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Pan Xinhuieb0b3e72015-07-07 20:43:26 +0800865 struct acpi_cpufreq_data *data = policy->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Mohan Kumareae2ef02019-04-15 14:03:58 +0300867 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 data->resume = 1;
870
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700874static struct freq_attr *acpi_cpufreq_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 &cpufreq_freq_attr_scaling_available_freqs,
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800876 &freqdomain_cpus,
Rafael J. Wysockif56c50e2015-07-22 22:12:10 +0200877#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
878 &cpb,
879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 NULL,
881};
882
883static struct cpufreq_driver acpi_cpufreq_driver = {
Viresh Kumardb9be212013-10-03 20:27:56 +0530884 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530885 .target_index = acpi_cpufreq_target,
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200886 .fast_switch = acpi_cpufreq_fast_switch,
Thomas Renningere2f74f32009-11-19 12:31:01 +0100887 .bios_limit = acpi_processor_get_bios_limit,
888 .init = acpi_cpufreq_cpu_init,
889 .exit = acpi_cpufreq_cpu_exit,
Viresh Kumar1a186d92018-02-26 10:38:46 +0530890 .ready = acpi_cpufreq_cpu_ready,
Thomas Renningere2f74f32009-11-19 12:31:01 +0100891 .resume = acpi_cpufreq_resume,
892 .name = "acpi-cpufreq",
Thomas Renningere2f74f32009-11-19 12:31:01 +0100893 .attr = acpi_cpufreq_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100896static enum cpuhp_state acpi_cpufreq_online;
897
Andre Przywara615b7302012-09-04 08:28:07 +0000898static void __init acpi_cpufreq_boost_init(void)
899{
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100900 int ret;
Andre Przywara615b7302012-09-04 08:28:07 +0000901
Erwan Velu1222d522019-02-20 11:10:17 +0100902 if (!(boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA))) {
903 pr_debug("Boost capabilities not present in the processor\n");
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100904 return;
Erwan Velu1222d522019-02-20 11:10:17 +0100905 }
Andre Przywara615b7302012-09-04 08:28:07 +0000906
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100907 acpi_cpufreq_driver.set_boost = set_boost;
908 acpi_cpufreq_driver.boost_enabled = boost_state(0);
Andre Przywara615b7302012-09-04 08:28:07 +0000909
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100910 /*
911 * This calls the online callback on all online cpu and forces all
912 * MSRs to the same value.
913 */
914 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpufreq/acpi:online",
915 cpufreq_boost_online, cpufreq_boost_down_prep);
916 if (ret < 0) {
917 pr_err("acpi_cpufreq: failed to register hotplug callbacks\n");
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100918 return;
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100919 }
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100920 acpi_cpufreq_online = ret;
Andre Przywara615b7302012-09-04 08:28:07 +0000921}
922
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500923static void acpi_cpufreq_boost_exit(void)
Andre Przywara615b7302012-09-04 08:28:07 +0000924{
Boris Ostrovsky2a8fa122016-12-15 10:00:58 -0500925 if (acpi_cpufreq_online > 0)
Sebastian Andrzej Siewior4d66ddf2016-11-28 10:51:02 +0100926 cpuhp_remove_state_nocalls(acpi_cpufreq_online);
Andre Przywara615b7302012-09-04 08:28:07 +0000927}
928
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700929static int __init acpi_cpufreq_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Fenghua Yu50109292007-08-07 18:40:30 -0400931 int ret;
932
Rafael J. Wysocki75c07582013-10-25 16:22:47 +0200933 if (acpi_disabled)
934 return -ENODEV;
935
Yinghai Lu8a61e122013-09-20 10:43:56 -0700936 /* don't keep reloading if cpufreq_driver exists */
937 if (cpufreq_get_current_driver())
Rafael J. Wysocki75c07582013-10-25 16:22:47 +0200938 return -EEXIST;
Yinghai Luee297532008-09-24 19:04:31 -0700939
Mohan Kumareae2ef02019-04-15 14:03:58 +0300940 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Fenghua Yu50109292007-08-07 18:40:30 -0400942 ret = acpi_cpufreq_early_init();
943 if (ret)
944 return ret;
Venkatesh Pallipadi09b4d1e2005-12-14 15:05:00 -0500945
Andre Przywara11269ff2012-09-04 08:28:08 +0000946#ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
947 /* this is a sysfs file with a strange name and an even stranger
948 * semantic - per CPU instantiation, but system global effect.
949 * Lets enable it only on AMD CPUs for compatibility reasons and
950 * only if configured. This is considered legacy code, which
951 * will probably be removed at some point in the future.
952 */
Rafael J. Wysockif56c50e2015-07-22 22:12:10 +0200953 if (!check_amd_hwpstate_cpu(0)) {
954 struct freq_attr **attr;
Andre Przywara11269ff2012-09-04 08:28:08 +0000955
Rafael J. Wysockif56c50e2015-07-22 22:12:10 +0200956 pr_debug("CPB unsupported, do not expose it\n");
Andre Przywara11269ff2012-09-04 08:28:08 +0000957
Rafael J. Wysockif56c50e2015-07-22 22:12:10 +0200958 for (attr = acpi_cpufreq_attr; *attr; attr++)
959 if (*attr == &cpb) {
960 *attr = NULL;
961 break;
962 }
Andre Przywara11269ff2012-09-04 08:28:08 +0000963 }
964#endif
Lukasz Majewskicfc9c8e2013-12-20 15:24:50 +0100965 acpi_cpufreq_boost_init();
Andre Przywara11269ff2012-09-04 08:28:08 +0000966
Akinobu Mita847aef62008-07-14 11:59:44 +0900967 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500968 if (ret) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800969 free_acpi_perf_data();
Konrad Rzeszutek Wilkeb8c68e2014-01-27 22:50:35 -0500970 acpi_cpufreq_boost_exit();
971 }
Akinobu Mita847aef62008-07-14 11:59:44 +0900972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700975static void __exit acpi_cpufreq_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Mohan Kumareae2ef02019-04-15 14:03:58 +0300977 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Andre Przywara615b7302012-09-04 08:28:07 +0000979 acpi_cpufreq_boost_exit();
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 cpufreq_unregister_driver(&acpi_cpufreq_driver);
982
Luming Yu50f4ddd2011-07-08 16:37:44 -0400983 free_acpi_perf_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Venkatesh Pallipadid395bf12005-08-25 15:59:00 -0400986module_param(acpi_pstate_strict, uint, 0644);
Venkatesh Pallipadi64be7ee2006-10-03 12:35:23 -0700987MODULE_PARM_DESC(acpi_pstate_strict,
Dave Jones95dd7222006-10-18 00:41:48 -0400988 "value 0 or non-zero. non-zero -> strict ACPI checks are "
989 "performed during frequency changes.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991late_initcall(acpi_cpufreq_init);
992module_exit(acpi_cpufreq_exit);
993
Matthew Garrettefa17192013-01-22 22:33:46 +0100994static const struct x86_cpu_id acpi_cpufreq_ids[] = {
Thomas Gleixnerb11d77f2020-03-24 14:51:51 +0100995 X86_MATCH_FEATURE(X86_FEATURE_ACPI, NULL),
996 X86_MATCH_FEATURE(X86_FEATURE_HW_PSTATE, NULL),
Matthew Garrettefa17192013-01-22 22:33:46 +0100997 {}
998};
999MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
1000
Rafael J. Wysockic655aff2013-06-07 13:13:31 +02001001static const struct acpi_device_id processor_device_ids[] = {
1002 {ACPI_PROCESSOR_OBJECT_HID, },
1003 {ACPI_PROCESSOR_DEVICE_HID, },
1004 {},
1005};
1006MODULE_DEVICE_TABLE(acpi, processor_device_ids);
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008MODULE_ALIAS("acpi");