blob: b477a4be8a698fde564465278cf7c09321f9b00b [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
Thomas Renninger919158d2007-10-31 15:41:42 +010053/* Use cpufreq debug layer for _PPC changes. */
54#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
55 "cpufreq-core", msg)
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * _PPC support is implemented as a CPUfreq policy notifier:
59 * This means each time a CPUfreq driver registered also with
60 * the ACPI core is asked to change the speed policy, the maximum
61 * value is adjusted so that it is within the platform limit.
62 *
63 * Also, when a new platform limit value is detected, the CPUfreq
64 * policy is adjusted accordingly.
65 */
66
Thomas Renninger623b78c2007-05-18 21:59:28 -050067static unsigned int ignore_ppc = 0;
68module_param(ignore_ppc, uint, 0644);
69MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
70 "limited by BIOS, this should help");
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define PPC_REGISTERED 1
73#define PPC_IN_USE 2
74
75static int acpi_processor_ppc_status = 0;
76
77static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040078 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct cpufreq_policy *policy = data;
81 struct acpi_processor *pr;
82 unsigned int ppc = 0;
83
Thomas Renninger623b78c2007-05-18 21:59:28 -050084 if (ignore_ppc)
85 return 0;
86
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040087 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (event != CPUFREQ_INCOMPATIBLE)
90 goto out;
91
92 pr = processors[policy->cpu];
93 if (!pr || !pr->performance)
94 goto out;
95
Len Brown4be44fc2005-08-05 00:44:28 -040096 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dave Jones0916bd32006-11-22 20:42:01 -050098 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 goto out;
100
101 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -0400102 pr->performance->states[ppc].
103 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Len Brown4be44fc2005-08-05 00:44:28 -0400105 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400106 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return 0;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static struct notifier_block acpi_ppc_notifier_block = {
112 .notifier_call = acpi_processor_ppc_notifier,
113};
114
Len Brown4be44fc2005-08-05 00:44:28 -0400115static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Len Brown4be44fc2005-08-05 00:44:28 -0400117 acpi_status status = 0;
118 unsigned long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
125 * _PPC indicates the maximum state currently supported by the platform
126 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
127 */
128 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
129
130 if (status != AE_NOT_FOUND)
131 acpi_processor_ppc_status |= PPC_IN_USE;
132
Len Brown4be44fc2005-08-05 00:44:28 -0400133 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400134 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400135 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137
Thomas Renninger919158d2007-10-31 15:41:42 +0100138 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
139 (int)ppc, ppc ? "" : "not");
140
Len Brown4be44fc2005-08-05 00:44:28 -0400141 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Patrick Mocheld550d982006-06-27 00:41:40 -0400143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Len Brown4be44fc2005-08-05 00:44:28 -0400146int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500148 int ret;
149
150 if (ignore_ppc)
151 return 0;
152
153 ret = acpi_processor_get_platform_limit(pr);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ret < 0)
156 return (ret);
157 else
158 return cpufreq_update_policy(pr->id);
159}
160
Len Brown4be44fc2005-08-05 00:44:28 -0400161void acpi_processor_ppc_init(void)
162{
163 if (!cpufreq_register_notifier
164 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 acpi_processor_ppc_status |= PPC_REGISTERED;
166 else
Len Brown4be44fc2005-08-05 00:44:28 -0400167 printk(KERN_DEBUG
168 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171void acpi_processor_ppc_exit(void)
172{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400174 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
175 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 acpi_processor_ppc_status &= ~PPC_REGISTERED;
178}
179
Len Brown4be44fc2005-08-05 00:44:28 -0400180static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Len Brown4be44fc2005-08-05 00:44:28 -0400182 int result = 0;
183 acpi_status status = 0;
184 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
185 union acpi_object *pct = NULL;
186 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400190 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400191 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Len Brown4be44fc2005-08-05 00:44:28 -0400195 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400197 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400198 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 result = -EFAULT;
200 goto end;
201 }
202
203 /*
204 * control_register
205 */
206
207 obj = pct->package.elements[0];
208
209 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400210 || (obj.buffer.length < sizeof(struct acpi_pct_register))
211 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400212 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 result = -EFAULT;
214 goto end;
215 }
Len Brown4be44fc2005-08-05 00:44:28 -0400216 memcpy(&pr->performance->control_register, obj.buffer.pointer,
217 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /*
220 * status_register
221 */
222
223 obj = pct->package.elements[1];
224
225 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400226 || (obj.buffer.length < sizeof(struct acpi_pct_register))
227 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400228 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 result = -EFAULT;
230 goto end;
231 }
232
Len Brown4be44fc2005-08-05 00:44:28 -0400233 memcpy(&pr->performance->status_register, obj.buffer.pointer,
234 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 end:
Len Brown02438d82006-06-30 03:19:10 -0400237 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Len Brown4be44fc2005-08-05 00:44:28 -0400242static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Len Brown4be44fc2005-08-05 00:44:28 -0400244 int result = 0;
245 acpi_status status = AE_OK;
246 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
247 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
248 struct acpi_buffer state = { 0, NULL };
249 union acpi_object *pss = NULL;
250 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400254 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400255 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200259 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400261 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 result = -EFAULT;
263 goto end;
264 }
265
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400267 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400270 pr->performance->states =
271 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
272 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!pr->performance->states) {
274 result = -ENOMEM;
275 goto end;
276 }
277
278 for (i = 0; i < pr->performance->state_count; i++) {
279
280 struct acpi_processor_px *px = &(pr->performance->states[i]);
281
282 state.length = sizeof(struct acpi_processor_px);
283 state.pointer = px;
284
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
286
287 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400288 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400290 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 result = -EFAULT;
292 kfree(pr->performance->states);
293 goto end;
294 }
295
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400297 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
298 i,
299 (u32) px->core_frequency,
300 (u32) px->power,
301 (u32) px->transition_latency,
302 (u32) px->bus_master_latency,
303 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400306 printk(KERN_ERR PREFIX
307 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 result = -EFAULT;
309 kfree(pr->performance->states);
310 goto end;
311 }
312 }
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314 end:
Len Brown02438d82006-06-30 03:19:10 -0400315 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Patrick Mocheld550d982006-06-27 00:41:40 -0400317 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Len Brown4be44fc2005-08-05 00:44:28 -0400320static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Len Brown4be44fc2005-08-05 00:44:28 -0400322 int result = 0;
323 acpi_status status = AE_OK;
324 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 status = acpi_get_handle(pr->handle, "_PCT", &handle);
331 if (ACPI_FAILURE(status)) {
332 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400333 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 result = acpi_processor_get_performance_control(pr);
338 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 result = acpi_processor_get_performance_states(pr);
342 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400343 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348int acpi_processor_notify_smm(struct module *calling_module)
349{
350 acpi_status status;
351 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* is_done is set to negative if an error occured,
361 * and to postitive if _no_ error occured, but SMM
362 * was already notified. This avoids double notification
363 * which might lead to unexpected results...
364 */
365 if (is_done > 0) {
366 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400367 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400368 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400370 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 is_done = -EIO;
374
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300375 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300376 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300377 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Len Brown4be44fc2005-08-05 00:44:28 -0400382 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300383 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300384 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300386 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
387 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400388 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400389 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300390 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300391 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
392 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 /* Success. If there's no _PPC, we need to fear nothing, so
398 * we can allow the cpufreq driver to be rmmod'ed. */
399 is_done = 1;
400
401 if (!(acpi_processor_ppc_status & PPC_IN_USE))
402 module_put(calling_module);
403
Patrick Mocheld550d982006-06-27 00:41:40 -0400404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Len Brown4be44fc2005-08-05 00:44:28 -0400407EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
410/* /proc/acpi/processor/../performance interface (DEPRECATED) */
411
412static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
413static struct file_operations acpi_processor_perf_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400414 .open = acpi_processor_perf_open_fs,
415 .read = seq_read,
416 .llseek = seq_lseek,
417 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418};
419
420static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
421{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200422 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400423 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (!pr)
427 goto end;
428
429 if (!pr->performance) {
430 seq_puts(seq, "<not supported>\n");
431 goto end;
432 }
433
434 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400435 "active state: P%d\n",
436 pr->performance->state_count, pr->performance->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 seq_puts(seq, "states:\n");
439 for (i = 0; i < pr->performance->state_count; i++)
Len Brown4be44fc2005-08-05 00:44:28 -0400440 seq_printf(seq,
441 " %cP%d: %d MHz, %d mW, %d uS\n",
442 (i == pr->performance->state ? '*' : ' '), i,
443 (u32) pr->performance->states[i].core_frequency,
444 (u32) pr->performance->states[i].power,
445 (u32) pr->performance->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
452{
453 return single_open(file, acpi_processor_perf_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400454 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457static void acpi_cpufreq_add_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Len Brown4be44fc2005-08-05 00:44:28 -0400459 struct proc_dir_entry *entry = NULL;
460 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* add file 'performance' [R/W] */
467 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Thomas Renninger632786c2007-04-19 15:49:09 +0200468 S_IFREG | S_IRUGO,
Len Brown4be44fc2005-08-05 00:44:28 -0400469 acpi_device_dir(device));
Thomas Renningera6fc6722006-06-26 23:58:43 -0400470 if (entry){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 entry->proc_fops = &acpi_processor_perf_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 entry->data = acpi_driver_data(device);
473 entry->owner = THIS_MODULE;
474 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Len Brown4be44fc2005-08-05 00:44:28 -0400480 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /* remove file 'performance' */
487 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400488 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick Mocheld550d982006-06-27 00:41:40 -0400490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493#else
Len Brown4be44fc2005-08-05 00:44:28 -0400494static void acpi_cpufreq_add_file(struct acpi_processor *pr)
495{
496 return;
497}
498static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
499{
500 return;
501}
502#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500504static int acpi_processor_get_psd(struct acpi_processor *pr)
505{
506 int result = 0;
507 acpi_status status = AE_OK;
508 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
509 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
510 struct acpi_buffer state = {0, NULL};
511 union acpi_object *psd = NULL;
512 struct acpi_psd_package *pdomain;
513
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500514 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
515 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400516 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500517 }
518
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200519 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500520 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
521 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
522 result = -EFAULT;
523 goto end;
524 }
525
526 if (psd->package.count != 1) {
527 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
528 result = -EFAULT;
529 goto end;
530 }
531
532 pdomain = &(pr->performance->domain_info);
533
534 state.length = sizeof(struct acpi_psd_package);
535 state.pointer = pdomain;
536
537 status = acpi_extract_package(&(psd->package.elements[0]),
538 &format, &state);
539 if (ACPI_FAILURE(status)) {
540 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
541 result = -EFAULT;
542 goto end;
543 }
544
545 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
546 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
547 result = -EFAULT;
548 goto end;
549 }
550
551 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
552 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
553 result = -EFAULT;
554 goto end;
555 }
556
557end:
Len Brown02438d82006-06-30 03:19:10 -0400558 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400559 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500560}
561
562int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400563 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500564{
565 int count, count_target;
566 int retval = 0;
567 unsigned int i, j;
568 cpumask_t covered_cpus;
569 struct acpi_processor *pr;
570 struct acpi_psd_package *pdomain;
571 struct acpi_processor *match_pr;
572 struct acpi_psd_package *match_pdomain;
573
Len Brown785fccc2006-06-15 22:19:31 -0400574 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500575
576 retval = 0;
577
578 /* Call _PSD for all CPUs */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400579 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500580 pr = processors[i];
581 if (!pr) {
582 /* Look only at processors in ACPI namespace */
583 continue;
584 }
585
586 if (pr->performance) {
587 retval = -EBUSY;
588 continue;
589 }
590
Fenghua Yu50109292007-08-07 18:40:30 -0400591 if (!performance || !percpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500592 retval = -EINVAL;
593 continue;
594 }
595
Fenghua Yu50109292007-08-07 18:40:30 -0400596 pr->performance = percpu_ptr(performance, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500597 cpu_set(i, pr->performance->shared_cpu_map);
598 if (acpi_processor_get_psd(pr)) {
599 retval = -EINVAL;
600 continue;
601 }
602 }
603 if (retval)
604 goto err_ret;
605
606 /*
607 * Now that we have _PSD data from all CPUs, lets setup P-state
608 * domain info.
609 */
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 /* Basic validity check for domain info */
616 pdomain = &(pr->performance->domain_info);
617 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
618 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
619 retval = -EINVAL;
620 goto err_ret;
621 }
622 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
623 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
624 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
625 retval = -EINVAL;
626 goto err_ret;
627 }
628 }
629
630 cpus_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400631 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500632 pr = processors[i];
633 if (!pr)
634 continue;
635
636 if (cpu_isset(i, covered_cpus))
637 continue;
638
639 pdomain = &(pr->performance->domain_info);
640 cpu_set(i, pr->performance->shared_cpu_map);
641 cpu_set(i, covered_cpus);
642 if (pdomain->num_processors <= 1)
643 continue;
644
645 /* Validate the Domain info */
646 count_target = pdomain->num_processors;
647 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400648 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500649 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400650 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
651 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
652 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500653 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500654
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400655 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500656 if (i == j)
657 continue;
658
659 match_pr = processors[j];
660 if (!match_pr)
661 continue;
662
663 match_pdomain = &(match_pr->performance->domain_info);
664 if (match_pdomain->domain != pdomain->domain)
665 continue;
666
667 /* Here i and j are in the same domain */
668
669 if (match_pdomain->num_processors != count_target) {
670 retval = -EINVAL;
671 goto err_ret;
672 }
673
674 if (pdomain->coord_type != match_pdomain->coord_type) {
675 retval = -EINVAL;
676 goto err_ret;
677 }
678
679 cpu_set(j, covered_cpus);
680 cpu_set(j, pr->performance->shared_cpu_map);
681 count++;
682 }
683
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400684 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500685 if (i == j)
686 continue;
687
688 match_pr = processors[j];
689 if (!match_pr)
690 continue;
691
692 match_pdomain = &(match_pr->performance->domain_info);
693 if (match_pdomain->domain != pdomain->domain)
694 continue;
695
696 match_pr->performance->shared_type =
697 pr->performance->shared_type;
698 match_pr->performance->shared_cpu_map =
699 pr->performance->shared_cpu_map;
700 }
701 }
702
703err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400704 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500705 pr = processors[i];
706 if (!pr || !pr->performance)
707 continue;
708
709 /* Assume no coordination on any error parsing domain info */
710 if (retval) {
711 cpus_clear(pr->performance->shared_cpu_map);
712 cpu_set(i, pr->performance->shared_cpu_map);
713 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
714 }
715 pr->performance = NULL; /* Will be set for real in register */
716 }
717
Len Brown785fccc2006-06-15 22:19:31 -0400718 mutex_unlock(&performance_mutex);
Len Brown9011bff42006-05-11 00:28:12 -0400719 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500720}
721EXPORT_SYMBOL(acpi_processor_preregister_performance);
722
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724int
Len Brown4be44fc2005-08-05 00:44:28 -0400725acpi_processor_register_performance(struct acpi_processor_performance
726 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 struct acpi_processor *pr;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400734 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 pr = processors[cpu];
737 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400738 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400739 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
742 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400743 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400744 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Andrew Mortona913f502006-06-10 09:54:13 -0700747 WARN_ON(!performance);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 pr->performance = performance;
750
751 if (acpi_processor_get_performance_info(pr)) {
752 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400753 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400754 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
757 acpi_cpufreq_add_file(pr);
758
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400759 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Len Brown4be44fc2005-08-05 00:44:28 -0400762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763EXPORT_SYMBOL(acpi_processor_register_performance);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765void
Len Brown4be44fc2005-08-05 00:44:28 -0400766acpi_processor_unregister_performance(struct acpi_processor_performance
767 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct acpi_processor *pr;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400772 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 pr = processors[cpu];
775 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400776 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400777 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Andrew Mortona913f502006-06-10 09:54:13 -0700780 if (pr->performance)
781 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 pr->performance = NULL;
783
784 acpi_cpufreq_remove_file(pr);
785
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400786 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Patrick Mocheld550d982006-06-27 00:41:40 -0400788 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
Len Brown4be44fc2005-08-05 00:44:28 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791EXPORT_SYMBOL(acpi_processor_unregister_performance);