blob: e28a31a40829b8451a69c61597fdbfa615af9173 [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
37static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
38
39static struct cpufreq_driver acpi_cpufreq_driver;
40
41
42static int
43processor_set_pstate (
44 u32 value)
45{
46 s64 retval;
47
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020048 pr_debug("processor_set_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070049
50 retval = ia64_pal_set_pstate((u64)value);
51
52 if (retval) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020053 pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070054 value, retval);
55 return -ENODEV;
56 }
57 return (int)retval;
58}
59
60
61static int
62processor_get_pstate (
63 u32 *value)
64{
65 u64 pstate_index = 0;
66 s64 retval;
67
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020068 pr_debug("processor_get_pstate\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070069
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080070 retval = ia64_pal_get_pstate(&pstate_index,
71 PAL_GET_PSTATE_TYPE_INSTANT);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070072 *value = (u32) pstate_index;
73
74 if (retval)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020075 pr_debug("Failed to get current freq with "
Denis V. Lunev60192db2008-07-17 11:11:17 -070076 "error 0x%lx, idx 0x%x\n", retval, *value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070077
78 return (int)retval;
79}
80
81
82/* To be used only after data->acpi_data is initialized */
83static unsigned
84extract_clock (
85 struct cpufreq_acpi_io *data,
86 unsigned value,
87 unsigned int cpu)
88{
89 unsigned long i;
90
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020091 pr_debug("extract_clock\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070092
93 for (i = 0; i < data->acpi_data.state_count; i++) {
Venkatesh Pallipadi17e77b12006-12-01 15:28:14 -080094 if (value == data->acpi_data.states[i].status)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -070095 return data->acpi_data.states[i].core_frequency;
96 }
97 return data->acpi_data.states[i-1].core_frequency;
98}
99
100
101static unsigned int
102processor_get_freq (
103 struct cpufreq_acpi_io *data,
104 unsigned int cpu)
105{
106 int ret = 0;
107 u32 value = 0;
108 cpumask_t saved_mask;
109 unsigned long clock_freq;
110
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200111 pr_debug("processor_get_freq\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700112
113 saved_mask = current->cpus_allowed;
Julia Lawall552dce32010-03-26 23:02:23 +0100114 set_cpus_allowed_ptr(current, cpumask_of(cpu));
Alex Williamson182fdd22007-08-13 15:49:46 -0600115 if (smp_processor_id() != cpu)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700116 goto migrate_end;
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);
120
121 if (ret) {
Julia Lawall552dce32010-03-26 23:02:23 +0100122 set_cpus_allowed_ptr(current, &saved_mask);
Joe Perchesb49c22a2016-04-05 13:28:24 -0700123 pr_warn("get performance failed with error %d\n", ret);
Alex Williamson182fdd22007-08-13 15:49:46 -0600124 ret = 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700125 goto migrate_end;
126 }
127 clock_freq = extract_clock(data, value, cpu);
128 ret = (clock_freq*1000);
129
130migrate_end:
Julia Lawall552dce32010-03-26 23:02:23 +0100131 set_cpus_allowed_ptr(current, &saved_mask);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700132 return ret;
133}
134
135
136static int
137processor_set_freq (
138 struct cpufreq_acpi_io *data,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530139 struct cpufreq_policy *policy,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700140 int state)
141{
142 int ret = 0;
143 u32 value = 0;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700144 cpumask_t saved_mask;
145 int retval;
146
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200147 pr_debug("processor_set_freq\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700148
149 saved_mask = current->cpus_allowed;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530150 set_cpus_allowed_ptr(current, cpumask_of(policy->cpu));
151 if (smp_processor_id() != policy->cpu) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700152 retval = -EAGAIN;
153 goto migrate_end;
154 }
155
156 if (state == data->acpi_data.state) {
157 if (unlikely(data->resume)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200158 pr_debug("Called after resume, resetting to P%d\n", state);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700159 data->resume = 0;
160 } else {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200161 pr_debug("Already at target state (P%d)\n", state);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700162 retval = 0;
163 goto migrate_end;
164 }
165 }
166
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200167 pr_debug("Transitioning from P%d to P%d\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700168 data->acpi_data.state, state);
169
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700170 /*
171 * First we write the target state's 'control' value to the
172 * control_register.
173 */
174
175 value = (u32) data->acpi_data.states[state].control;
176
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200177 pr_debug("Transitioning to state: 0x%08x\n", value);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700178
179 ret = processor_set_pstate(value);
180 if (ret) {
Joe Perchesb49c22a2016-04-05 13:28:24 -0700181 pr_warn("Transition failed with error %d\n", ret);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700182 retval = -ENODEV;
183 goto migrate_end;
184 }
185
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700186 data->acpi_data.state = state;
187
188 retval = 0;
189
190migrate_end:
Julia Lawall552dce32010-03-26 23:02:23 +0100191 set_cpus_allowed_ptr(current, &saved_mask);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700192 return (retval);
193}
194
195
196static unsigned int
197acpi_cpufreq_get (
198 unsigned int cpu)
199{
200 struct cpufreq_acpi_io *data = acpi_io_data[cpu];
201
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200202 pr_debug("acpi_cpufreq_get\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700203
204 return processor_get_freq(data, cpu);
205}
206
207
208static int
209acpi_cpufreq_target (
210 struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530211 unsigned int index)
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700212{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530213 return processor_set_freq(acpi_io_data[policy->cpu], policy, index);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700214}
215
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700216static int
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700217acpi_cpufreq_cpu_init (
218 struct cpufreq_policy *policy)
219{
220 unsigned int i;
221 unsigned int cpu = policy->cpu;
222 struct cpufreq_acpi_io *data;
223 unsigned int result = 0;
Pan Xinhui946c14f2015-07-20 14:22:46 +0800224 struct cpufreq_frequency_table *freq_table;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700225
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200226 pr_debug("acpi_cpufreq_cpu_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700227
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530228 data = kzalloc(sizeof(*data), GFP_KERNEL);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700229 if (!data)
230 return (-ENOMEM);
231
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700232 acpi_io_data[cpu] = data;
233
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700234 result = acpi_processor_register_performance(&data->acpi_data, cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700235
236 if (result)
237 goto err_free;
238
239 /* capability check */
240 if (data->acpi_data.state_count <= 1) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200241 pr_debug("No P-States\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700242 result = -ENODEV;
243 goto err_unreg;
244 }
245
246 if ((data->acpi_data.control_register.space_id !=
247 ACPI_ADR_SPACE_FIXED_HARDWARE) ||
248 (data->acpi_data.status_register.space_id !=
249 ACPI_ADR_SPACE_FIXED_HARDWARE)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200250 pr_debug("Unsupported address space [%d, %d]\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700251 (u32) (data->acpi_data.control_register.space_id),
252 (u32) (data->acpi_data.status_register.space_id));
253 result = -ENODEV;
254 goto err_unreg;
255 }
256
257 /* alloc freq_table */
Pan Xinhui946c14f2015-07-20 14:22:46 +0800258 freq_table = kzalloc(sizeof(*freq_table) *
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700259 (data->acpi_data.state_count + 1),
260 GFP_KERNEL);
Pan Xinhui946c14f2015-07-20 14:22:46 +0800261 if (!freq_table) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700262 result = -ENOMEM;
263 goto err_unreg;
264 }
265
266 /* detect transition latency */
267 policy->cpuinfo.transition_latency = 0;
268 for (i=0; i<data->acpi_data.state_count; i++) {
269 if ((data->acpi_data.states[i].transition_latency * 1000) >
270 policy->cpuinfo.transition_latency) {
271 policy->cpuinfo.transition_latency =
272 data->acpi_data.states[i].transition_latency * 1000;
273 }
274 }
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700275
276 /* table init */
277 for (i = 0; i <= data->acpi_data.state_count; i++)
278 {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700279 if (i < data->acpi_data.state_count) {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800280 freq_table[i].frequency =
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700281 data->acpi_data.states[i].core_frequency * 1000;
282 } else {
Pan Xinhui946c14f2015-07-20 14:22:46 +0800283 freq_table[i].frequency = CPUFREQ_TABLE_END;
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700284 }
285 }
286
Pan Xinhui946c14f2015-07-20 14:22:46 +0800287 result = cpufreq_table_validate_and_show(policy, freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700288 if (result) {
289 goto err_freqfree;
290 }
291
292 /* notify BIOS that we exist */
293 acpi_processor_notify_smm(THIS_MODULE);
294
Joe Perches1c5864e2016-04-05 13:28:25 -0700295 pr_info("CPU%u - ACPI performance management activated\n", cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700296
297 for (i = 0; i < data->acpi_data.state_count; i++)
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200298 pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700299 (i == data->acpi_data.state?'*':' '), i,
300 (u32) data->acpi_data.states[i].core_frequency,
301 (u32) data->acpi_data.states[i].power,
302 (u32) data->acpi_data.states[i].transition_latency,
303 (u32) data->acpi_data.states[i].bus_master_latency,
304 (u32) data->acpi_data.states[i].status,
305 (u32) data->acpi_data.states[i].control);
306
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700307 /* the first call to ->target() should result in us actually
308 * writing something to the appropriate registers. */
309 data->resume = 1;
310
311 return (result);
312
313 err_freqfree:
Pan Xinhui946c14f2015-07-20 14:22:46 +0800314 kfree(freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700315 err_unreg:
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200316 acpi_processor_unregister_performance(cpu);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700317 err_free:
318 kfree(data);
319 acpi_io_data[cpu] = NULL;
320
321 return (result);
322}
323
324
325static int
326acpi_cpufreq_cpu_exit (
327 struct cpufreq_policy *policy)
328{
329 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
330
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200331 pr_debug("acpi_cpufreq_cpu_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700332
333 if (data) {
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700334 acpi_io_data[policy->cpu] = NULL;
Rafael J. Wysockib2f8dc42015-07-22 22:11:16 +0200335 acpi_processor_unregister_performance(policy->cpu);
Pan Xinhui555f3fe2015-07-20 14:24:36 +0800336 kfree(policy->freq_table);
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700337 kfree(data);
338 }
339
340 return (0);
341}
342
343
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700344static struct cpufreq_driver acpi_cpufreq_driver = {
Viresh Kumar59b24132013-10-03 20:28:07 +0530345 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530346 .target_index = acpi_cpufreq_target,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700347 .get = acpi_cpufreq_get,
348 .init = acpi_cpufreq_cpu_init,
349 .exit = acpi_cpufreq_cpu_exit,
350 .name = "acpi-cpufreq",
Viresh Kumar59b24132013-10-03 20:28:07 +0530351 .attr = cpufreq_generic_attr,
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700352};
353
354
355static int __init
356acpi_cpufreq_init (void)
357{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200358 pr_debug("acpi_cpufreq_init\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700359
360 return cpufreq_register_driver(&acpi_cpufreq_driver);
361}
362
363
364static void __exit
365acpi_cpufreq_exit (void)
366{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200367 pr_debug("acpi_cpufreq_exit\n");
Venkatesh Pallipadi4db86992005-07-29 16:15:00 -0700368
369 cpufreq_unregister_driver(&acpi_cpufreq_driver);
370 return;
371}
372
373
374late_initcall(acpi_cpufreq_init);
375module_exit(acpi_cpufreq_exit);
376