blob: f32010bee4d5650a6896624bab290a4d560e916c [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
Thomas Renninger623b78c2007-05-18 21:59:28 -050063static unsigned int ignore_ppc = 0;
64module_param(ignore_ppc, uint, 0644);
65MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
66 "limited by BIOS, this should help");
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define PPC_REGISTERED 1
69#define PPC_IN_USE 2
70
71static int acpi_processor_ppc_status = 0;
72
73static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040074 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct cpufreq_policy *policy = data;
77 struct acpi_processor *pr;
78 unsigned int ppc = 0;
79
Thomas Renninger623b78c2007-05-18 21:59:28 -050080 if (ignore_ppc)
81 return 0;
82
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040083 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (event != CPUFREQ_INCOMPATIBLE)
86 goto out;
87
88 pr = processors[policy->cpu];
89 if (!pr || !pr->performance)
90 goto out;
91
Len Brown4be44fc2005-08-05 00:44:28 -040092 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Dave Jones0916bd32006-11-22 20:42:01 -050094 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto out;
96
97 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -040098 pr->performance->states[ppc].
99 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Len Brown4be44fc2005-08-05 00:44:28 -0400101 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400102 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 return 0;
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct notifier_block acpi_ppc_notifier_block = {
108 .notifier_call = acpi_processor_ppc_notifier,
109};
110
Len Brown4be44fc2005-08-05 00:44:28 -0400111static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Len Brown4be44fc2005-08-05 00:44:28 -0400113 acpi_status status = 0;
114 unsigned long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /*
121 * _PPC indicates the maximum state currently supported by the platform
122 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
123 */
124 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
125
126 if (status != AE_NOT_FOUND)
127 acpi_processor_ppc_status |= PPC_IN_USE;
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400130 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400131 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick Mocheld550d982006-06-27 00:41:40 -0400136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Len Brown4be44fc2005-08-05 00:44:28 -0400139int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500141 int ret;
142
143 if (ignore_ppc)
144 return 0;
145
146 ret = acpi_processor_get_platform_limit(pr);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (ret < 0)
149 return (ret);
150 else
151 return cpufreq_update_policy(pr->id);
152}
153
Len Brown4be44fc2005-08-05 00:44:28 -0400154void acpi_processor_ppc_init(void)
155{
156 if (!cpufreq_register_notifier
157 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 acpi_processor_ppc_status |= PPC_REGISTERED;
159 else
Len Brown4be44fc2005-08-05 00:44:28 -0400160 printk(KERN_DEBUG
161 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164void acpi_processor_ppc_exit(void)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400167 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
168 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 acpi_processor_ppc_status &= ~PPC_REGISTERED;
171}
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Len Brown4be44fc2005-08-05 00:44:28 -0400175 int result = 0;
176 acpi_status status = 0;
177 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
178 union acpi_object *pct = NULL;
179 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400183 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400184 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400185 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400190 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400191 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 result = -EFAULT;
193 goto end;
194 }
195
196 /*
197 * control_register
198 */
199
200 obj = pct->package.elements[0];
201
202 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400203 || (obj.buffer.length < sizeof(struct acpi_pct_register))
204 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400205 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 result = -EFAULT;
207 goto end;
208 }
Len Brown4be44fc2005-08-05 00:44:28 -0400209 memcpy(&pr->performance->control_register, obj.buffer.pointer,
210 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /*
213 * status_register
214 */
215
216 obj = pct->package.elements[1];
217
218 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400219 || (obj.buffer.length < sizeof(struct acpi_pct_register))
220 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400221 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 result = -EFAULT;
223 goto end;
224 }
225
Len Brown4be44fc2005-08-05 00:44:28 -0400226 memcpy(&pr->performance->status_register, obj.buffer.pointer,
227 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Len Brown4be44fc2005-08-05 00:44:28 -0400229 end:
Len Brown02438d82006-06-30 03:19:10 -0400230 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Len Brown4be44fc2005-08-05 00:44:28 -0400237 int result = 0;
238 acpi_status status = AE_OK;
239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
241 struct acpi_buffer state = { 0, NULL };
242 union acpi_object *pss = NULL;
243 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400247 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400248 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200252 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400254 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 result = -EFAULT;
256 goto end;
257 }
258
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400260 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400263 pr->performance->states =
264 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
265 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!pr->performance->states) {
267 result = -ENOMEM;
268 goto end;
269 }
270
271 for (i = 0; i < pr->performance->state_count; i++) {
272
273 struct acpi_processor_px *px = &(pr->performance->states[i]);
274
275 state.length = sizeof(struct acpi_processor_px);
276 state.pointer = px;
277
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
279
280 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400281 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400283 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 result = -EFAULT;
285 kfree(pr->performance->states);
286 goto end;
287 }
288
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400290 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
291 i,
292 (u32) px->core_frequency,
293 (u32) px->power,
294 (u32) px->transition_latency,
295 (u32) px->bus_master_latency,
296 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400299 printk(KERN_ERR PREFIX
300 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 result = -EFAULT;
302 kfree(pr->performance->states);
303 goto end;
304 }
305 }
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307 end:
Len Brown02438d82006-06-30 03:19:10 -0400308 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Len Brown4be44fc2005-08-05 00:44:28 -0400315 int result = 0;
316 acpi_status status = AE_OK;
317 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 status = acpi_get_handle(pr->handle, "_PCT", &handle);
324 if (ACPI_FAILURE(status)) {
325 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400326 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 result = acpi_processor_get_performance_control(pr);
331 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 result = acpi_processor_get_performance_states(pr);
335 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400336 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Patrick Mocheld550d982006-06-27 00:41:40 -0400338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Len Brown4be44fc2005-08-05 00:44:28 -0400341int acpi_processor_notify_smm(struct module *calling_module)
342{
343 acpi_status status;
344 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400348 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* is_done is set to negative if an error occured,
354 * and to postitive if _no_ error occured, but SMM
355 * was already notified. This avoids double notification
356 * which might lead to unexpected results...
357 */
358 if (is_done > 0) {
359 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400360 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400361 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400363 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 is_done = -EIO;
367
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300368 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300369 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300376 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300377 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300379 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
380 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400381 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400382 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300383 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300384 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
385 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 /* Success. If there's no _PPC, we need to fear nothing, so
391 * we can allow the cpufreq driver to be rmmod'ed. */
392 is_done = 1;
393
394 if (!(acpi_processor_ppc_status & PPC_IN_USE))
395 module_put(calling_module);
396
Patrick Mocheld550d982006-06-27 00:41:40 -0400397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Len Brown4be44fc2005-08-05 00:44:28 -0400400EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
403/* /proc/acpi/processor/../performance interface (DEPRECATED) */
404
405static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
406static struct file_operations acpi_processor_perf_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400407 .open = acpi_processor_perf_open_fs,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411};
412
413static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
414{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200415 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400416 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (!pr)
420 goto end;
421
422 if (!pr->performance) {
423 seq_puts(seq, "<not supported>\n");
424 goto end;
425 }
426
427 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400428 "active state: P%d\n",
429 pr->performance->state_count, pr->performance->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 seq_puts(seq, "states:\n");
432 for (i = 0; i < pr->performance->state_count; i++)
Len Brown4be44fc2005-08-05 00:44:28 -0400433 seq_printf(seq,
434 " %cP%d: %d MHz, %d mW, %d uS\n",
435 (i == pr->performance->state ? '*' : ' '), i,
436 (u32) pr->performance->states[i].core_frequency,
437 (u32) pr->performance->states[i].power,
438 (u32) pr->performance->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Len Brown4be44fc2005-08-05 00:44:28 -0400440 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
445{
446 return single_open(file, acpi_processor_perf_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400447 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450static void acpi_cpufreq_add_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Len Brown4be44fc2005-08-05 00:44:28 -0400452 struct proc_dir_entry *entry = NULL;
453 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* add file 'performance' [R/W] */
460 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Thomas Renninger632786c2007-04-19 15:49:09 +0200461 S_IFREG | S_IRUGO,
Len Brown4be44fc2005-08-05 00:44:28 -0400462 acpi_device_dir(device));
Thomas Renningera6fc6722006-06-26 23:58:43 -0400463 if (entry){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 entry->proc_fops = &acpi_processor_perf_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 entry->data = acpi_driver_data(device);
466 entry->owner = THIS_MODULE;
467 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Len Brown4be44fc2005-08-05 00:44:28 -0400471static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Len Brown4be44fc2005-08-05 00:44:28 -0400473 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400477 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* remove file 'performance' */
480 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400481 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486#else
Len Brown4be44fc2005-08-05 00:44:28 -0400487static void acpi_cpufreq_add_file(struct acpi_processor *pr)
488{
489 return;
490}
491static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
492{
493 return;
494}
495#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500497static int acpi_processor_get_psd(struct acpi_processor *pr)
498{
499 int result = 0;
500 acpi_status status = AE_OK;
501 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
502 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
503 struct acpi_buffer state = {0, NULL};
504 union acpi_object *psd = NULL;
505 struct acpi_psd_package *pdomain;
506
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500507 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
508 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400509 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500510 }
511
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200512 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500513 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
514 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
515 result = -EFAULT;
516 goto end;
517 }
518
519 if (psd->package.count != 1) {
520 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
521 result = -EFAULT;
522 goto end;
523 }
524
525 pdomain = &(pr->performance->domain_info);
526
527 state.length = sizeof(struct acpi_psd_package);
528 state.pointer = pdomain;
529
530 status = acpi_extract_package(&(psd->package.elements[0]),
531 &format, &state);
532 if (ACPI_FAILURE(status)) {
533 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
534 result = -EFAULT;
535 goto end;
536 }
537
538 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
539 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
540 result = -EFAULT;
541 goto end;
542 }
543
544 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
545 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
546 result = -EFAULT;
547 goto end;
548 }
549
550end:
Len Brown02438d82006-06-30 03:19:10 -0400551 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400552 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500553}
554
555int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400556 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500557{
558 int count, count_target;
559 int retval = 0;
560 unsigned int i, j;
561 cpumask_t covered_cpus;
562 struct acpi_processor *pr;
563 struct acpi_psd_package *pdomain;
564 struct acpi_processor *match_pr;
565 struct acpi_psd_package *match_pdomain;
566
Len Brown785fccc2006-06-15 22:19:31 -0400567 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500568
569 retval = 0;
570
571 /* Call _PSD for all CPUs */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400572 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500573 pr = processors[i];
574 if (!pr) {
575 /* Look only at processors in ACPI namespace */
576 continue;
577 }
578
579 if (pr->performance) {
580 retval = -EBUSY;
581 continue;
582 }
583
Fenghua Yu50109292007-08-07 18:40:30 -0400584 if (!performance || !percpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500585 retval = -EINVAL;
586 continue;
587 }
588
Fenghua Yu50109292007-08-07 18:40:30 -0400589 pr->performance = percpu_ptr(performance, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500590 cpu_set(i, pr->performance->shared_cpu_map);
591 if (acpi_processor_get_psd(pr)) {
592 retval = -EINVAL;
593 continue;
594 }
595 }
596 if (retval)
597 goto err_ret;
598
599 /*
600 * Now that we have _PSD data from all CPUs, lets setup P-state
601 * domain info.
602 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400603 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500604 pr = processors[i];
605 if (!pr)
606 continue;
607
608 /* Basic validity check for domain info */
609 pdomain = &(pr->performance->domain_info);
610 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
611 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
612 retval = -EINVAL;
613 goto err_ret;
614 }
615 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
616 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
617 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
618 retval = -EINVAL;
619 goto err_ret;
620 }
621 }
622
623 cpus_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400624 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500625 pr = processors[i];
626 if (!pr)
627 continue;
628
629 if (cpu_isset(i, covered_cpus))
630 continue;
631
632 pdomain = &(pr->performance->domain_info);
633 cpu_set(i, pr->performance->shared_cpu_map);
634 cpu_set(i, covered_cpus);
635 if (pdomain->num_processors <= 1)
636 continue;
637
638 /* Validate the Domain info */
639 count_target = pdomain->num_processors;
640 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400641 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500642 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400643 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
644 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
645 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500646 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500647
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400648 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500649 if (i == j)
650 continue;
651
652 match_pr = processors[j];
653 if (!match_pr)
654 continue;
655
656 match_pdomain = &(match_pr->performance->domain_info);
657 if (match_pdomain->domain != pdomain->domain)
658 continue;
659
660 /* Here i and j are in the same domain */
661
662 if (match_pdomain->num_processors != count_target) {
663 retval = -EINVAL;
664 goto err_ret;
665 }
666
667 if (pdomain->coord_type != match_pdomain->coord_type) {
668 retval = -EINVAL;
669 goto err_ret;
670 }
671
672 cpu_set(j, covered_cpus);
673 cpu_set(j, pr->performance->shared_cpu_map);
674 count++;
675 }
676
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400677 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500678 if (i == j)
679 continue;
680
681 match_pr = processors[j];
682 if (!match_pr)
683 continue;
684
685 match_pdomain = &(match_pr->performance->domain_info);
686 if (match_pdomain->domain != pdomain->domain)
687 continue;
688
689 match_pr->performance->shared_type =
690 pr->performance->shared_type;
691 match_pr->performance->shared_cpu_map =
692 pr->performance->shared_cpu_map;
693 }
694 }
695
696err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400697 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500698 pr = processors[i];
699 if (!pr || !pr->performance)
700 continue;
701
702 /* Assume no coordination on any error parsing domain info */
703 if (retval) {
704 cpus_clear(pr->performance->shared_cpu_map);
705 cpu_set(i, pr->performance->shared_cpu_map);
706 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
707 }
708 pr->performance = NULL; /* Will be set for real in register */
709 }
710
Len Brown785fccc2006-06-15 22:19:31 -0400711 mutex_unlock(&performance_mutex);
Len Brown9011bff42006-05-11 00:28:12 -0400712 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500713}
714EXPORT_SYMBOL(acpi_processor_preregister_performance);
715
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717int
Len Brown4be44fc2005-08-05 00:44:28 -0400718acpi_processor_register_performance(struct acpi_processor_performance
719 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct acpi_processor *pr;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400727 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 pr = processors[cpu];
730 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400731 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400736 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400737 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Andrew Mortona913f502006-06-10 09:54:13 -0700740 WARN_ON(!performance);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 pr->performance = performance;
743
744 if (acpi_processor_get_performance_info(pr)) {
745 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400746 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400747 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 acpi_cpufreq_add_file(pr);
751
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400752 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Len Brown4be44fc2005-08-05 00:44:28 -0400755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756EXPORT_SYMBOL(acpi_processor_register_performance);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758void
Len Brown4be44fc2005-08-05 00:44:28 -0400759acpi_processor_unregister_performance(struct acpi_processor_performance
760 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct acpi_processor *pr;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400765 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 pr = processors[cpu];
768 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400769 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Andrew Mortona913f502006-06-10 09:54:13 -0700773 if (pr->performance)
774 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 pr->performance = NULL;
776
777 acpi_cpufreq_remove_file(pr);
778
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400779 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Patrick Mocheld550d982006-06-27 00:41:40 -0400781 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
Len Brown4be44fc2005-08-05 00:44:28 -0400783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784EXPORT_SYMBOL(acpi_processor_unregister_performance);