blob: 463b0247cbc517c40567446df9446fff389c849b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * processor_perflib.c - ACPI Processor P-States Library ($Revision: 71 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
9 *
10 *
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/cpufreq.h>
33
34#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/uaccess.h>
40#endif
41
42#include <acpi/acpi_bus.h>
43#include <acpi/processor.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_PROCESSOR_COMPONENT 0x01000000
46#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
48#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050049ACPI_MODULE_NAME("processor_perflib");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040051static DEFINE_MUTEX(performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * _PPC support is implemented as a CPUfreq policy notifier:
55 * This means each time a CPUfreq driver registered also with
56 * the ACPI core is asked to change the speed policy, the maximum
57 * value is adjusted so that it is within the platform limit.
58 *
59 * Also, when a new platform limit value is detected, the CPUfreq
60 * policy is adjusted accordingly.
61 */
62
63#define PPC_REGISTERED 1
64#define PPC_IN_USE 2
65
66static int acpi_processor_ppc_status = 0;
67
68static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040069 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct cpufreq_policy *policy = data;
72 struct acpi_processor *pr;
73 unsigned int ppc = 0;
74
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040075 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (event != CPUFREQ_INCOMPATIBLE)
78 goto out;
79
80 pr = processors[policy->cpu];
81 if (!pr || !pr->performance)
82 goto out;
83
Len Brown4be44fc2005-08-05 00:44:28 -040084 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dave Jones0916bd32006-11-22 20:42:01 -050086 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto out;
88
89 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -040090 pr->performance->states[ppc].
91 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Len Brown4be44fc2005-08-05 00:44:28 -040093 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040094 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 return 0;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static struct notifier_block acpi_ppc_notifier_block = {
100 .notifier_call = acpi_processor_ppc_notifier,
101};
102
Len Brown4be44fc2005-08-05 00:44:28 -0400103static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Len Brown4be44fc2005-08-05 00:44:28 -0400105 acpi_status status = 0;
106 unsigned long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /*
113 * _PPC indicates the maximum state currently supported by the platform
114 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
115 */
116 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
117
118 if (status != AE_NOT_FOUND)
119 acpi_processor_ppc_status |= PPC_IN_USE;
120
Len Brown4be44fc2005-08-05 00:44:28 -0400121 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400122 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400123 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
Len Brown4be44fc2005-08-05 00:44:28 -0400126 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Patrick Mocheld550d982006-06-27 00:41:40 -0400128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Len Brown4be44fc2005-08-05 00:44:28 -0400131int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int ret = acpi_processor_get_platform_limit(pr);
134 if (ret < 0)
135 return (ret);
136 else
137 return cpufreq_update_policy(pr->id);
138}
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140void acpi_processor_ppc_init(void)
141{
142 if (!cpufreq_register_notifier
143 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 acpi_processor_ppc_status |= PPC_REGISTERED;
145 else
Len Brown4be44fc2005-08-05 00:44:28 -0400146 printk(KERN_DEBUG
147 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150void acpi_processor_ppc_exit(void)
151{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400153 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
154 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 acpi_processor_ppc_status &= ~PPC_REGISTERED;
157}
158
Len Brown4be44fc2005-08-05 00:44:28 -0400159static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Len Brown4be44fc2005-08-05 00:44:28 -0400161 int result = 0;
162 acpi_status status = 0;
163 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
164 union acpi_object *pct = NULL;
165 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400169 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400170 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Len Brown4be44fc2005-08-05 00:44:28 -0400174 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400176 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400177 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 result = -EFAULT;
179 goto end;
180 }
181
182 /*
183 * control_register
184 */
185
186 obj = pct->package.elements[0];
187
188 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400189 || (obj.buffer.length < sizeof(struct acpi_pct_register))
190 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400191 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 result = -EFAULT;
193 goto end;
194 }
Len Brown4be44fc2005-08-05 00:44:28 -0400195 memcpy(&pr->performance->control_register, obj.buffer.pointer,
196 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
199 * status_register
200 */
201
202 obj = pct->package.elements[1];
203
204 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400205 || (obj.buffer.length < sizeof(struct acpi_pct_register))
206 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400207 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 result = -EFAULT;
209 goto end;
210 }
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212 memcpy(&pr->performance->status_register, obj.buffer.pointer,
213 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Len Brown4be44fc2005-08-05 00:44:28 -0400215 end:
Len Brown02438d82006-06-30 03:19:10 -0400216 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Patrick Mocheld550d982006-06-27 00:41:40 -0400218 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Len Brown4be44fc2005-08-05 00:44:28 -0400223 int result = 0;
224 acpi_status status = AE_OK;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
227 struct acpi_buffer state = { 0, NULL };
228 union acpi_object *pss = NULL;
229 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400233 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400234 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400235 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200238 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400240 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 result = -EFAULT;
242 goto end;
243 }
244
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400246 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400249 pr->performance->states =
250 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
251 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!pr->performance->states) {
253 result = -ENOMEM;
254 goto end;
255 }
256
257 for (i = 0; i < pr->performance->state_count; i++) {
258
259 struct acpi_processor_px *px = &(pr->performance->states[i]);
260
261 state.length = sizeof(struct acpi_processor_px);
262 state.pointer = px;
263
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
265
266 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400267 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400269 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 result = -EFAULT;
271 kfree(pr->performance->states);
272 goto end;
273 }
274
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400276 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
277 i,
278 (u32) px->core_frequency,
279 (u32) px->power,
280 (u32) px->transition_latency,
281 (u32) px->bus_master_latency,
282 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400285 printk(KERN_ERR PREFIX
286 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 result = -EFAULT;
288 kfree(pr->performance->states);
289 goto end;
290 }
291 }
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 end:
Len Brown02438d82006-06-30 03:19:10 -0400294 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Patrick Mocheld550d982006-06-27 00:41:40 -0400296 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Len Brown4be44fc2005-08-05 00:44:28 -0400301 int result = 0;
302 acpi_status status = AE_OK;
303 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 status = acpi_get_handle(pr->handle, "_PCT", &handle);
310 if (ACPI_FAILURE(status)) {
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400312 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 result = acpi_processor_get_performance_control(pr);
317 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 result = acpi_processor_get_performance_states(pr);
321 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327int acpi_processor_notify_smm(struct module *calling_module)
328{
329 acpi_status status;
330 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* is_done is set to negative if an error occured,
340 * and to postitive if _no_ error occured, but SMM
341 * was already notified. This avoids double notification
342 * which might lead to unexpected results...
343 */
344 if (is_done > 0) {
345 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400346 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400347 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400349 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 is_done = -EIO;
353
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300354 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300355 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
Len Brown4be44fc2005-08-05 00:44:28 -0400361 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300362 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300363 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300365 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
366 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400367 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400368 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300369 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300370 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
371 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400373 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 /* Success. If there's no _PPC, we need to fear nothing, so
377 * we can allow the cpufreq driver to be rmmod'ed. */
378 is_done = 1;
379
380 if (!(acpi_processor_ppc_status & PPC_IN_USE))
381 module_put(calling_module);
382
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Len Brown4be44fc2005-08-05 00:44:28 -0400386EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
389/* /proc/acpi/processor/../performance interface (DEPRECATED) */
390
391static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
392static struct file_operations acpi_processor_perf_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400393 .open = acpi_processor_perf_open_fs,
394 .read = seq_read,
395 .llseek = seq_lseek,
396 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
399static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
400{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200401 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400402 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!pr)
406 goto end;
407
408 if (!pr->performance) {
409 seq_puts(seq, "<not supported>\n");
410 goto end;
411 }
412
413 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400414 "active state: P%d\n",
415 pr->performance->state_count, pr->performance->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 seq_puts(seq, "states:\n");
418 for (i = 0; i < pr->performance->state_count; i++)
Len Brown4be44fc2005-08-05 00:44:28 -0400419 seq_printf(seq,
420 " %cP%d: %d MHz, %d mW, %d uS\n",
421 (i == pr->performance->state ? '*' : ' '), i,
422 (u32) pr->performance->states[i].core_frequency,
423 (u32) pr->performance->states[i].power,
424 (u32) pr->performance->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
431{
432 return single_open(file, acpi_processor_perf_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400433 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Len Brown4be44fc2005-08-05 00:44:28 -0400436static void acpi_cpufreq_add_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown4be44fc2005-08-05 00:44:28 -0400438 struct proc_dir_entry *entry = NULL;
439 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* add file 'performance' [R/W] */
446 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Thomas Renninger632786c2007-04-19 15:49:09 +0200447 S_IFREG | S_IRUGO,
Len Brown4be44fc2005-08-05 00:44:28 -0400448 acpi_device_dir(device));
Thomas Renningera6fc6722006-06-26 23:58:43 -0400449 if (entry){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 entry->proc_fops = &acpi_processor_perf_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 entry->data = acpi_driver_data(device);
452 entry->owner = THIS_MODULE;
453 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Len Brown4be44fc2005-08-05 00:44:28 -0400459 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* remove file 'performance' */
466 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400467 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Patrick Mocheld550d982006-06-27 00:41:40 -0400469 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472#else
Len Brown4be44fc2005-08-05 00:44:28 -0400473static void acpi_cpufreq_add_file(struct acpi_processor *pr)
474{
475 return;
476}
477static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
478{
479 return;
480}
481#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500483static int acpi_processor_get_psd(struct acpi_processor *pr)
484{
485 int result = 0;
486 acpi_status status = AE_OK;
487 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
488 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
489 struct acpi_buffer state = {0, NULL};
490 union acpi_object *psd = NULL;
491 struct acpi_psd_package *pdomain;
492
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500493 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
494 if (ACPI_FAILURE(status)) {
Len Brown9011bff2006-05-11 00:28:12 -0400495 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500496 }
497
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200498 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500499 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
500 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
501 result = -EFAULT;
502 goto end;
503 }
504
505 if (psd->package.count != 1) {
506 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
507 result = -EFAULT;
508 goto end;
509 }
510
511 pdomain = &(pr->performance->domain_info);
512
513 state.length = sizeof(struct acpi_psd_package);
514 state.pointer = pdomain;
515
516 status = acpi_extract_package(&(psd->package.elements[0]),
517 &format, &state);
518 if (ACPI_FAILURE(status)) {
519 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
520 result = -EFAULT;
521 goto end;
522 }
523
524 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
525 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
526 result = -EFAULT;
527 goto end;
528 }
529
530 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
531 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
532 result = -EFAULT;
533 goto end;
534 }
535
536end:
Len Brown02438d82006-06-30 03:19:10 -0400537 kfree(buffer.pointer);
Len Brown9011bff2006-05-11 00:28:12 -0400538 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500539}
540
541int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400542 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500543{
544 int count, count_target;
545 int retval = 0;
546 unsigned int i, j;
547 cpumask_t covered_cpus;
548 struct acpi_processor *pr;
549 struct acpi_psd_package *pdomain;
550 struct acpi_processor *match_pr;
551 struct acpi_psd_package *match_pdomain;
552
Len Brown785fccc2006-06-15 22:19:31 -0400553 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500554
555 retval = 0;
556
557 /* Call _PSD for all CPUs */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400558 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500559 pr = processors[i];
560 if (!pr) {
561 /* Look only at processors in ACPI namespace */
562 continue;
563 }
564
565 if (pr->performance) {
566 retval = -EBUSY;
567 continue;
568 }
569
Fenghua Yu50109292007-08-07 18:40:30 -0400570 if (!performance || !percpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500571 retval = -EINVAL;
572 continue;
573 }
574
Fenghua Yu50109292007-08-07 18:40:30 -0400575 pr->performance = percpu_ptr(performance, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500576 cpu_set(i, pr->performance->shared_cpu_map);
577 if (acpi_processor_get_psd(pr)) {
578 retval = -EINVAL;
579 continue;
580 }
581 }
582 if (retval)
583 goto err_ret;
584
585 /*
586 * Now that we have _PSD data from all CPUs, lets setup P-state
587 * domain info.
588 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400589 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500590 pr = processors[i];
591 if (!pr)
592 continue;
593
594 /* Basic validity check for domain info */
595 pdomain = &(pr->performance->domain_info);
596 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
597 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
598 retval = -EINVAL;
599 goto err_ret;
600 }
601 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
602 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
603 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
604 retval = -EINVAL;
605 goto err_ret;
606 }
607 }
608
609 cpus_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400610 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500611 pr = processors[i];
612 if (!pr)
613 continue;
614
615 if (cpu_isset(i, covered_cpus))
616 continue;
617
618 pdomain = &(pr->performance->domain_info);
619 cpu_set(i, pr->performance->shared_cpu_map);
620 cpu_set(i, covered_cpus);
621 if (pdomain->num_processors <= 1)
622 continue;
623
624 /* Validate the Domain info */
625 count_target = pdomain->num_processors;
626 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400627 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500628 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400629 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
630 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
631 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500632 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500633
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400634 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500635 if (i == j)
636 continue;
637
638 match_pr = processors[j];
639 if (!match_pr)
640 continue;
641
642 match_pdomain = &(match_pr->performance->domain_info);
643 if (match_pdomain->domain != pdomain->domain)
644 continue;
645
646 /* Here i and j are in the same domain */
647
648 if (match_pdomain->num_processors != count_target) {
649 retval = -EINVAL;
650 goto err_ret;
651 }
652
653 if (pdomain->coord_type != match_pdomain->coord_type) {
654 retval = -EINVAL;
655 goto err_ret;
656 }
657
658 cpu_set(j, covered_cpus);
659 cpu_set(j, pr->performance->shared_cpu_map);
660 count++;
661 }
662
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400663 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500664 if (i == j)
665 continue;
666
667 match_pr = processors[j];
668 if (!match_pr)
669 continue;
670
671 match_pdomain = &(match_pr->performance->domain_info);
672 if (match_pdomain->domain != pdomain->domain)
673 continue;
674
675 match_pr->performance->shared_type =
676 pr->performance->shared_type;
677 match_pr->performance->shared_cpu_map =
678 pr->performance->shared_cpu_map;
679 }
680 }
681
682err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400683 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500684 pr = processors[i];
685 if (!pr || !pr->performance)
686 continue;
687
688 /* Assume no coordination on any error parsing domain info */
689 if (retval) {
690 cpus_clear(pr->performance->shared_cpu_map);
691 cpu_set(i, pr->performance->shared_cpu_map);
692 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
693 }
694 pr->performance = NULL; /* Will be set for real in register */
695 }
696
Len Brown785fccc2006-06-15 22:19:31 -0400697 mutex_unlock(&performance_mutex);
Len Brown9011bff2006-05-11 00:28:12 -0400698 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500699}
700EXPORT_SYMBOL(acpi_processor_preregister_performance);
701
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703int
Len Brown4be44fc2005-08-05 00:44:28 -0400704acpi_processor_register_performance(struct acpi_processor_performance
705 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct acpi_processor *pr;
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400711 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400713 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 pr = processors[cpu];
716 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400717 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400718 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400722 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400723 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Andrew Mortona913f502006-06-10 09:54:13 -0700726 WARN_ON(!performance);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 pr->performance = performance;
729
730 if (acpi_processor_get_performance_info(pr)) {
731 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400732 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400733 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
736 acpi_cpufreq_add_file(pr);
737
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400738 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Len Brown4be44fc2005-08-05 00:44:28 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742EXPORT_SYMBOL(acpi_processor_register_performance);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744void
Len Brown4be44fc2005-08-05 00:44:28 -0400745acpi_processor_unregister_performance(struct acpi_processor_performance
746 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 struct acpi_processor *pr;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400751 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 pr = processors[cpu];
754 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400755 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Andrew Mortona913f502006-06-10 09:54:13 -0700759 if (pr->performance)
760 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 pr->performance = NULL;
762
763 acpi_cpufreq_remove_file(pr);
764
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400765 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
Len Brown4be44fc2005-08-05 00:44:28 -0400769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770EXPORT_SYMBOL(acpi_processor_unregister_performance);