blob: c6bdc455517f856b8c736e492a6350d5b3c78da8 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -07002/*
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -07003 * This file provides the ACPI based P-state support. This
4 * module works with generic cpufreq infrastructure. Most of
5 * the code is based on i386 version
6 * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
7 *
8 * Copyright (C) 2005 Intel Corp
9 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10 */
11
Joe Perches1c5864e2016-04-05 13:28:25 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070014#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070016#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/cpufreq.h>
19#include <linux/proc_fs.h>
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070020#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070022#include <asm/pal.h>
23
24#include <linux/acpi.h>
25#include <acpi/processor.h>
26
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070027MODULE_AUTHOR("Venkatesh Pallipadi");
28MODULE_DESCRIPTION("ACPI Processor P-States Driver");
29MODULE_LICENSE("GPL");
30
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070031struct cpufreq_acpi_io {
32 struct acpi_processor_performance acpi_data;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070033 unsigned int resume;
34};
35
Thomas Gleixner38f05ed2017-04-12 22:55:03 +020036struct cpufreq_acpi_req {
37 unsigned int cpu;
38 unsigned int state;
39};
40
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070041static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
42
43static struct cpufreq_driver acpi_cpufreq_driver;
44
45
46static int
47processor_set_pstate (
48 u32 value)
49{
50 s64 retval;
51
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020052 pr_debug("processor_set_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070053
54 retval = ia64_pal_set_pstate((u64)value);
55
56 if (retval) {
Sergei Trofimovich4c56f572021-03-13 10:42:14 +000057 pr_debug("Failed to set freq to 0x%x, with error 0x%llx\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070058 value, retval);
59 return -ENODEV;
60 }
61 return (int)retval;
62}
63
64
65static int
66processor_get_pstate (
67 u32 *value)
68{
69 u64 pstate_index = 0;
70 s64 retval;
71
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020072 pr_debug("processor_get_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070073
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080074 retval = ia64_pal_get_pstate(&pstate_index,
75 PAL_GET_PSTATE_TYPE_INSTANT);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070076 *value = (u32) pstate_index;
77
78 if (retval)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020079 pr_debug("Failed to get current freq with "
Sergei Trofimovich4c56f572021-03-13 10:42:14 +000080 "error 0x%llx, idx 0x%x\n", retval, *value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070081
82 return (int)retval;
83}
84
85
86/* To be used only after data->acpi_data is initialized */
87static unsigned
88extract_clock (
89 struct cpufreq_acpi_io *data,
Thomas Gleixner38f05ed2017-04-12 22:55:03 +020090 unsigned value)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070091{
92 unsigned long i;
93
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020094 pr_debug("extract_clock\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070095
96 for (i = 0; i < data->acpi_data.state_count; i++) {
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080097 if (value == data->acpi_data.states[i].status)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070098 return data->acpi_data.states[i].core_frequency;
99 }
100 return data->acpi_data.states[i-1].core_frequency;
101}
102
103
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200104static long
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700105processor_get_freq (
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200106 void *arg)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700107{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200108 struct cpufreq_acpi_req *req = arg;
109 unsigned int cpu = req->cpu;
110 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
111 u32 value;
112 int ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700113
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200114 pr_debug("processor_get_freq\n");
Alex Williamson182fdd22007-08-13 15:49:46 -0600115 if (smp_processor_id() != cpu)
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200116 return -EAGAIN;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700117
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -0800118 /* processor_get_pstate gets the instantaneous frequency */
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700119 ret = processor_get_pstate(&value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700120 if (ret) {
Joe Perchesb49c22a2016-04-05 13:28:24 -0700121 pr_warn("get performance failed with error %d\n", ret);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200122 return ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700123 }
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200124 return 1000 * extract_clock(data, value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700125}
126
127
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200128static long
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700129processor_set_freq (
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200130 void *arg)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700131{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200132 struct cpufreq_acpi_req *req = arg;
133 unsigned int cpu = req->cpu;
134 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
135 int ret, state = req->state;
136 u32 value;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700137
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200138 pr_debug("processor_set_freq\n");
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200139 if (smp_processor_id() != cpu)
140 return -EAGAIN;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700141
142 if (state == data->acpi_data.state) {
143 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200144 pr_debug("Called after resume, resetting to P%d\n", state);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700145 data->resume = 0;
146 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200147 pr_debug("Already at target state (P%d)\n", state);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200148 return 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700149 }
150 }
151
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200152 pr_debug("Transitioning from P%d to P%d\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700153 data->acpi_data.state, state);
154
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700155 /*
156 * First we write the target state's 'control' value to the
157 * control_register.
158 */
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700159 value = (u32) data->acpi_data.states[state].control;
160
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200161 pr_debug("Transitioning to state: 0x%08x\n", value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700162
163 ret = processor_set_pstate(value);
164 if (ret) {
Joe Perchesb49c22a2016-04-05 13:28:24 -0700165 pr_warn("Transition failed with error %d\n", ret);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200166 return -ENODEV;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700167 }
168
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700169 data->acpi_data.state = state;
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200170 return 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700171}
172
173
174static unsigned int
175acpi_cpufreq_get (
176 unsigned int cpu)
177{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200178 struct cpufreq_acpi_req req;
179 long ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700180
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200181 req.cpu = cpu;
182 ret = work_on_cpu(cpu, processor_get_freq, &req);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700183
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200184 return ret > 0 ? (unsigned int) ret : 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700185}
186
187
188static int
189acpi_cpufreq_target (
190 struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530191 unsigned int index)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700192{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200193 struct cpufreq_acpi_req req;
194
195 req.cpu = policy->cpu;
196 req.state = index;
197
198 return work_on_cpu(req.cpu, processor_set_freq, &req);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700199}
200
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700201static int
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700202acpi_cpufreq_cpu_init (
203 struct cpufreq_policy *policy)
204{
205 unsigned int i;
206 unsigned int cpu = policy->cpu;
207 struct cpufreq_acpi_io *data;
208 unsigned int result = 0;
Pan Xinhui946c14f2015-07-20 14:22:46 +0800209 struct cpufreq_frequency_table *freq_table;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700210
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200211 pr_debug("acpi_cpufreq_cpu_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700212
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530213 data = kzalloc(sizeof(*data), GFP_KERNEL);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700214 if (!data)
215 return (-ENOMEM);
216
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700217 acpi_io_data[cpu] = data;
218
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700219 result = acpi_processor_register_performance(&data->acpi_data, cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700220
221 if (result)
222 goto err_free;
223
224 /* capability check */
225 if (data->acpi_data.state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200226 pr_debug("No P-States\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700227 result = -ENODEV;
228 goto err_unreg;
229 }
230
231 if ((data->acpi_data.control_register.space_id !=
232 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
233 (data->acpi_data.status_register.space_id !=
234 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200235 pr_debug("Unsupported address space [%d, %d]\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700236 (u32) (data->acpi_data.control_register.space_id),
237 (u32) (data->acpi_data.status_register.space_id));
238 result = -ENODEV;
239 goto err_unreg;
240 }
241
242 /* alloc freq_table */
Kees Cook6396bb22018-06-12 14:03:40 -0700243 freq_table = kcalloc(data->acpi_data.state_count + 1,
244 sizeof(*freq_table),
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700245 GFP_KERNEL);
Pan Xinhui946c14f2015-07-20 14:22:46 +0800246 if (!freq_table) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700247 result = -ENOMEM;
248 goto err_unreg;
249 }
250
251 /* detect transition latency */
252 policy->cpuinfo.transition_latency = 0;
253 for (i=0; i<data->acpi_data.state_count; i++) {
254 if ((data->acpi_data.states[i].transition_latency * 1000) >
255 policy->cpuinfo.transition_latency) {
256 policy->cpuinfo.transition_latency =
257 data->acpi_data.states[i].transition_latency * 1000;
258 }
259 }
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700260
261 /* table init */
262 for (i = 0; i <= data->acpi_data.state_count; i++)
263 {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700264 if (i < data->acpi_data.state_count) {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800265 freq_table[i].frequency =
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700266 data->acpi_data.states[i].core_frequency * 1000;
267 } else {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800268 freq_table[i].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700269 }
270 }
271
Viresh Kumarb6663622018-02-26 10:38:53 +0530272 policy->freq_table = freq_table;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700273
274 /* notify BIOS that we exist */
275 acpi_processor_notify_smm(THIS_MODULE);
276
Joe Perches1c5864e2016-04-05 13:28:25 -0700277 pr_info("CPU%u - ACPI performance management activated\n", cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700278
279 for (i = 0; i < data->acpi_data.state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200280 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700281 (i == data->acpi_data.state?'*':' '), i,
282 (u32) data->acpi_data.states[i].core_frequency,
283 (u32) data->acpi_data.states[i].power,
284 (u32) data->acpi_data.states[i].transition_latency,
285 (u32) data->acpi_data.states[i].bus_master_latency,
286 (u32) data->acpi_data.states[i].status,
287 (u32) data->acpi_data.states[i].control);
288
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700289 /* the first call to ->target() should result in us actually
290 * writing something to the appropriate registers. */
291 data->resume = 1;
292
293 return (result);
294
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700295 err_unreg:
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200296 acpi_processor_unregister_performance(cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700297 err_free:
298 kfree(data);
299 acpi_io_data[cpu] = NULL;
300
301 return (result);
302}
303
304
305static int
306acpi_cpufreq_cpu_exit (
307 struct cpufreq_policy *policy)
308{
309 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
310
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200311 pr_debug("acpi_cpufreq_cpu_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700312
313 if (data) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700314 acpi_io_data[policy->cpu] = NULL;
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200315 acpi_processor_unregister_performance(policy->cpu);
Pan Xinhui555f3fe2015-07-20 14:24:36 +0800316 kfree(policy->freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700317 kfree(data);
318 }
319
320 return (0);
321}
322
323
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700324static struct cpufreq_driver acpi_cpufreq_driver = {
Viresh Kumar59b24132013-10-03 20:28:07 +0530325 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530326 .target_index = acpi_cpufreq_target,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700327 .get = acpi_cpufreq_get,
328 .init = acpi_cpufreq_cpu_init,
329 .exit = acpi_cpufreq_cpu_exit,
330 .name = "acpi-cpufreq",
Viresh Kumar59b24132013-10-03 20:28:07 +0530331 .attr = cpufreq_generic_attr,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700332};
333
334
335static int __init
336acpi_cpufreq_init (void)
337{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200338 pr_debug("acpi_cpufreq_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700339
340 return cpufreq_register_driver(&acpi_cpufreq_driver);
341}
342
343
344static void __exit
345acpi_cpufreq_exit (void)
346{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200347 pr_debug("acpi_cpufreq_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700348
349 cpufreq_unregister_driver(&acpi_cpufreq_driver);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700350}
351
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700352late_initcall(acpi_cpufreq_init);
353module_exit(acpi_cpufreq_exit);