blob: a757c0a1e7b5703f7926b3cb84fb4ee1b9f0ed14 [file] [log] [blame]
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -07001/*
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -07002 * This file provides the ACPI based P-state support. This
3 * module works with generic cpufreq infrastructure. Most of
4 * the code is based on i386 version
5 * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
6 *
7 * Copyright (C) 2005 Intel Corp
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9 */
10
Joe Perches1c5864e2016-04-05 13:28:25 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070013#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070015#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/cpufreq.h>
18#include <linux/proc_fs.h>
19#include <linux/seq_file.h>
20#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
31
32struct cpufreq_acpi_io {
33 struct acpi_processor_performance acpi_data;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070034 unsigned int resume;
35};
36
Thomas Gleixner38f05ed2017-04-12 22:55:03 +020037struct cpufreq_acpi_req {
38 unsigned int cpu;
39 unsigned int state;
40};
41
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070042static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
43
44static struct cpufreq_driver acpi_cpufreq_driver;
45
46
47static int
48processor_set_pstate (
49 u32 value)
50{
51 s64 retval;
52
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020053 pr_debug("processor_set_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070054
55 retval = ia64_pal_set_pstate((u64)value);
56
57 if (retval) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020058 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070059 value, retval);
60 return -ENODEV;
61 }
62 return (int)retval;
63}
64
65
66static int
67processor_get_pstate (
68 u32 *value)
69{
70 u64 pstate_index = 0;
71 s64 retval;
72
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020073 pr_debug("processor_get_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070074
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080075 retval = ia64_pal_get_pstate(&pstate_index,
76 PAL_GET_PSTATE_TYPE_INSTANT);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070077 *value = (u32) pstate_index;
78
79 if (retval)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020080 pr_debug("Failed to get current freq with "
Denis V. Lunev60192db2008-07-17 11:11:17 -070081 "error 0x%lx, idx 0x%x\n", retval, *value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070082
83 return (int)retval;
84}
85
86
87/* To be used only after data->acpi_data is initialized */
88static unsigned
89extract_clock (
90 struct cpufreq_acpi_io *data,
Thomas Gleixner38f05ed2017-04-12 22:55:03 +020091 unsigned value)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070092{
93 unsigned long i;
94
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020095 pr_debug("extract_clock\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070096
97 for (i = 0; i < data->acpi_data.state_count; i++) {
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080098 if (value == data->acpi_data.states[i].status)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070099 return data->acpi_data.states[i].core_frequency;
100 }
101 return data->acpi_data.states[i-1].core_frequency;
102}
103
104
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200105static long
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700106processor_get_freq (
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200107 void *arg)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700108{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200109 struct cpufreq_acpi_req *req = arg;
110 unsigned int cpu = req->cpu;
111 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
112 u32 value;
113 int ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700114
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200115 pr_debug("processor_get_freq\n");
Alex Williamson182fdd22007-08-13 15:49:46 -0600116 if (smp_processor_id() != cpu)
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200117 return -EAGAIN;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700118
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -0800119 /* processor_get_pstate gets the instantaneous frequency */
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700120 ret = processor_get_pstate(&value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700121 if (ret) {
Joe Perchesb49c22a2016-04-05 13:28:24 -0700122 pr_warn("get performance failed with error %d\n", ret);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200123 return ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700124 }
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200125 return 1000 * extract_clock(data, value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700126}
127
128
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200129static long
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700130processor_set_freq (
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200131 void *arg)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700132{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200133 struct cpufreq_acpi_req *req = arg;
134 unsigned int cpu = req->cpu;
135 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
136 int ret, state = req->state;
137 u32 value;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700138
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200139 pr_debug("processor_set_freq\n");
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200140 if (smp_processor_id() != cpu)
141 return -EAGAIN;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700142
143 if (state == data->acpi_data.state) {
144 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200145 pr_debug("Called after resume, resetting to P%d\n", state);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700146 data->resume = 0;
147 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200148 pr_debug("Already at target state (P%d)\n", state);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200149 return 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700150 }
151 }
152
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200153 pr_debug("Transitioning from P%d to P%d\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700154 data->acpi_data.state, state);
155
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700156 /*
157 * First we write the target state's 'control' value to the
158 * control_register.
159 */
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700160 value = (u32) data->acpi_data.states[state].control;
161
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200162 pr_debug("Transitioning to state: 0x%08x\n", value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700163
164 ret = processor_set_pstate(value);
165 if (ret) {
Joe Perchesb49c22a2016-04-05 13:28:24 -0700166 pr_warn("Transition failed with error %d\n", ret);
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200167 return -ENODEV;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700168 }
169
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700170 data->acpi_data.state = state;
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200171 return 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700172}
173
174
175static unsigned int
176acpi_cpufreq_get (
177 unsigned int cpu)
178{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200179 struct cpufreq_acpi_req req;
180 long ret;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700181
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200182 req.cpu = cpu;
183 ret = work_on_cpu(cpu, processor_get_freq, &req);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700184
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200185 return ret > 0 ? (unsigned int) ret : 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700186}
187
188
189static int
190acpi_cpufreq_target (
191 struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530192 unsigned int index)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700193{
Thomas Gleixner38f05ed2017-04-12 22:55:03 +0200194 struct cpufreq_acpi_req req;
195
196 req.cpu = policy->cpu;
197 req.state = index;
198
199 return work_on_cpu(req.cpu, processor_set_freq, &req);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700200}
201
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700202static int
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700203acpi_cpufreq_cpu_init (
204 struct cpufreq_policy *policy)
205{
206 unsigned int i;
207 unsigned int cpu = policy->cpu;
208 struct cpufreq_acpi_io *data;
209 unsigned int result = 0;
Pan Xinhui946c14f2015-07-20 14:22:46 +0800210 struct cpufreq_frequency_table *freq_table;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700211
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200212 pr_debug("acpi_cpufreq_cpu_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700213
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530214 data = kzalloc(sizeof(*data), GFP_KERNEL);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700215 if (!data)
216 return (-ENOMEM);
217
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700218 acpi_io_data[cpu] = data;
219
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700220 result = acpi_processor_register_performance(&data->acpi_data, cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700221
222 if (result)
223 goto err_free;
224
225 /* capability check */
226 if (data->acpi_data.state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200227 pr_debug("No P-States\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700228 result = -ENODEV;
229 goto err_unreg;
230 }
231
232 if ((data->acpi_data.control_register.space_id !=
233 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
234 (data->acpi_data.status_register.space_id !=
235 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200236 pr_debug("Unsupported address space [%d, %d]\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700237 (u32) (data->acpi_data.control_register.space_id),
238 (u32) (data->acpi_data.status_register.space_id));
239 result = -ENODEV;
240 goto err_unreg;
241 }
242
243 /* alloc freq_table */
Pan Xinhui946c14f2015-07-20 14:22:46 +0800244 freq_table = kzalloc(sizeof(*freq_table) *
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700245 (data->acpi_data.state_count + 1),
246 GFP_KERNEL);
Pan Xinhui946c14f2015-07-20 14:22:46 +0800247 if (!freq_table) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700248 result = -ENOMEM;
249 goto err_unreg;
250 }
251
252 /* detect transition latency */
253 policy->cpuinfo.transition_latency = 0;
254 for (i=0; i<data->acpi_data.state_count; i++) {
255 if ((data->acpi_data.states[i].transition_latency * 1000) >
256 policy->cpuinfo.transition_latency) {
257 policy->cpuinfo.transition_latency =
258 data->acpi_data.states[i].transition_latency * 1000;
259 }
260 }
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700261
262 /* table init */
263 for (i = 0; i <= data->acpi_data.state_count; i++)
264 {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700265 if (i < data->acpi_data.state_count) {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800266 freq_table[i].frequency =
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700267 data->acpi_data.states[i].core_frequency * 1000;
268 } else {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800269 freq_table[i].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700270 }
271 }
272
Pan Xinhui946c14f2015-07-20 14:22:46 +0800273 result = cpufreq_table_validate_and_show(policy, freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700274 if (result) {
275 goto err_freqfree;
276 }
277
278 /* notify BIOS that we exist */
279 acpi_processor_notify_smm(THIS_MODULE);
280
Joe Perches1c5864e2016-04-05 13:28:25 -0700281 pr_info("CPU%u - ACPI performance management activated\n", cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700282
283 for (i = 0; i < data->acpi_data.state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700285 (i == data->acpi_data.state?'*':' '), i,
286 (u32) data->acpi_data.states[i].core_frequency,
287 (u32) data->acpi_data.states[i].power,
288 (u32) data->acpi_data.states[i].transition_latency,
289 (u32) data->acpi_data.states[i].bus_master_latency,
290 (u32) data->acpi_data.states[i].status,
291 (u32) data->acpi_data.states[i].control);
292
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700293 /* the first call to ->target() should result in us actually
294 * writing something to the appropriate registers. */
295 data->resume = 1;
296
297 return (result);
298
299 err_freqfree:
Pan Xinhui946c14f2015-07-20 14:22:46 +0800300 kfree(freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700301 err_unreg:
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200302 acpi_processor_unregister_performance(cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700303 err_free:
304 kfree(data);
305 acpi_io_data[cpu] = NULL;
306
307 return (result);
308}
309
310
311static int
312acpi_cpufreq_cpu_exit (
313 struct cpufreq_policy *policy)
314{
315 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
316
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200317 pr_debug("acpi_cpufreq_cpu_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700318
319 if (data) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700320 acpi_io_data[policy->cpu] = NULL;
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200321 acpi_processor_unregister_performance(policy->cpu);
Pan Xinhui555f3fe2015-07-20 14:24:36 +0800322 kfree(policy->freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700323 kfree(data);
324 }
325
326 return (0);
327}
328
329
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700330static struct cpufreq_driver acpi_cpufreq_driver = {
Viresh Kumar59b24132013-10-03 20:28:07 +0530331 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530332 .target_index = acpi_cpufreq_target,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700333 .get = acpi_cpufreq_get,
334 .init = acpi_cpufreq_cpu_init,
335 .exit = acpi_cpufreq_cpu_exit,
336 .name = "acpi-cpufreq",
Viresh Kumar59b24132013-10-03 20:28:07 +0530337 .attr = cpufreq_generic_attr,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700338};
339
340
341static int __init
342acpi_cpufreq_init (void)
343{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200344 pr_debug("acpi_cpufreq_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700345
346 return cpufreq_register_driver(&acpi_cpufreq_driver);
347}
348
349
350static void __exit
351acpi_cpufreq_exit (void)
352{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200353 pr_debug("acpi_cpufreq_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700354
355 cpufreq_unregister_driver(&acpi_cpufreq_driver);
356 return;
357}
358
359
360late_initcall(acpi_cpufreq_init);
361module_exit(acpi_cpufreq_exit);
362