blob: 6fd174a3714958946be4f4b54377c434694824e2 [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"
47#define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
48#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
49#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("acpi_processor")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040052static DEFINE_MUTEX(performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * _PPC support is implemented as a CPUfreq policy notifier:
56 * This means each time a CPUfreq driver registered also with
57 * the ACPI core is asked to change the speed policy, the maximum
58 * value is adjusted so that it is within the platform limit.
59 *
60 * Also, when a new platform limit value is detected, the CPUfreq
61 * policy is adjusted accordingly.
62 */
63
64#define PPC_REGISTERED 1
65#define PPC_IN_USE 2
66
67static int acpi_processor_ppc_status = 0;
68
69static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040070 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct cpufreq_policy *policy = data;
73 struct acpi_processor *pr;
74 unsigned int ppc = 0;
75
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040076 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (event != CPUFREQ_INCOMPATIBLE)
79 goto out;
80
81 pr = processors[policy->cpu];
82 if (!pr || !pr->performance)
83 goto out;
84
Len Brown4be44fc2005-08-05 00:44:28 -040085 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dave Jones0916bd32006-11-22 20:42:01 -050087 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto out;
89
90 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -040091 pr->performance->states[ppc].
92 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Len Brown4be44fc2005-08-05 00:44:28 -040094 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040095 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 return 0;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct notifier_block acpi_ppc_notifier_block = {
101 .notifier_call = acpi_processor_ppc_notifier,
102};
103
Len Brown4be44fc2005-08-05 00:44:28 -0400104static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Len Brown4be44fc2005-08-05 00:44:28 -0400106 acpi_status status = 0;
107 unsigned long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * _PPC indicates the maximum state currently supported by the platform
115 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
116 */
117 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
118
119 if (status != AE_NOT_FOUND)
120 acpi_processor_ppc_status |= PPC_IN_USE;
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400123 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
Len Brown4be44fc2005-08-05 00:44:28 -0400127 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Patrick Mocheld550d982006-06-27 00:41:40 -0400129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Len Brown4be44fc2005-08-05 00:44:28 -0400132int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 int ret = acpi_processor_get_platform_limit(pr);
135 if (ret < 0)
136 return (ret);
137 else
138 return cpufreq_update_policy(pr->id);
139}
140
Len Brown4be44fc2005-08-05 00:44:28 -0400141void acpi_processor_ppc_init(void)
142{
143 if (!cpufreq_register_notifier
144 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 acpi_processor_ppc_status |= PPC_REGISTERED;
146 else
Len Brown4be44fc2005-08-05 00:44:28 -0400147 printk(KERN_DEBUG
148 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151void acpi_processor_ppc_exit(void)
152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400154 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
155 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 acpi_processor_ppc_status &= ~PPC_REGISTERED;
158}
159
Len Brown4be44fc2005-08-05 00:44:28 -0400160static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Len Brown4be44fc2005-08-05 00:44:28 -0400162 int result = 0;
163 acpi_status status = 0;
164 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
165 union acpi_object *pct = NULL;
166 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400170 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400171 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400172 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Len Brown4be44fc2005-08-05 00:44:28 -0400175 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400177 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400178 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 result = -EFAULT;
180 goto end;
181 }
182
183 /*
184 * control_register
185 */
186
187 obj = pct->package.elements[0];
188
189 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400190 || (obj.buffer.length < sizeof(struct acpi_pct_register))
191 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400192 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 result = -EFAULT;
194 goto end;
195 }
Len Brown4be44fc2005-08-05 00:44:28 -0400196 memcpy(&pr->performance->control_register, obj.buffer.pointer,
197 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * status_register
201 */
202
203 obj = pct->package.elements[1];
204
205 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400206 || (obj.buffer.length < sizeof(struct acpi_pct_register))
207 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400208 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 result = -EFAULT;
210 goto end;
211 }
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213 memcpy(&pr->performance->status_register, obj.buffer.pointer,
214 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Len Brown4be44fc2005-08-05 00:44:28 -0400216 end:
Len Brown02438d82006-06-30 03:19:10 -0400217 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Patrick Mocheld550d982006-06-27 00:41:40 -0400219 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Len Brown4be44fc2005-08-05 00:44:28 -0400224 int result = 0;
225 acpi_status status = AE_OK;
226 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
227 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
228 struct acpi_buffer state = { 0, NULL };
229 union acpi_object *pss = NULL;
230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400234 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400235 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Len Brown4be44fc2005-08-05 00:44:28 -0400239 pss = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400241 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 result = -EFAULT;
243 goto end;
244 }
245
246 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400247 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400250 pr->performance->states =
251 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
252 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!pr->performance->states) {
254 result = -ENOMEM;
255 goto end;
256 }
257
258 for (i = 0; i < pr->performance->state_count; i++) {
259
260 struct acpi_processor_px *px = &(pr->performance->states[i]);
261
262 state.length = sizeof(struct acpi_processor_px);
263 state.pointer = px;
264
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
266
267 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400268 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400270 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 result = -EFAULT;
272 kfree(pr->performance->states);
273 goto end;
274 }
275
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
278 i,
279 (u32) px->core_frequency,
280 (u32) px->power,
281 (u32) px->transition_latency,
282 (u32) px->bus_master_latency,
283 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400286 printk(KERN_ERR PREFIX
287 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 result = -EFAULT;
289 kfree(pr->performance->states);
290 goto end;
291 }
292 }
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 end:
Len Brown02438d82006-06-30 03:19:10 -0400295 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Len Brown4be44fc2005-08-05 00:44:28 -0400302 int result = 0;
303 acpi_status status = AE_OK;
304 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 status = acpi_get_handle(pr->handle, "_PCT", &handle);
311 if (ACPI_FAILURE(status)) {
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400313 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400314 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 result = acpi_processor_get_performance_control(pr);
318 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400319 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 result = acpi_processor_get_performance_states(pr);
322 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 result = acpi_processor_get_platform_limit(pr);
326 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332int acpi_processor_notify_smm(struct module *calling_module)
333{
334 acpi_status status;
335 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* is_done is set to negative if an error occured,
345 * and to postitive if _no_ error occured, but SMM
346 * was already notified. This avoids double notification
347 * which might lead to unexpected results...
348 */
349 if (is_done > 0) {
350 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400352 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400354 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
357 is_done = -EIO;
358
359 /* Can't write pstate_cnt to smi_cmd if either value is zero */
Len Brown4be44fc2005-08-05 00:44:28 -0400360 if ((!acpi_fadt.smi_cmd) || (!acpi_fadt.pstate_cnt)) {
361 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_cnt\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
367 "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n",
368 acpi_fadt.pstate_cnt, acpi_fadt.smi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
371 * it anyway, so we need to support it... */
372 if (acpi_fadt_is_v1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400373 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
374 "Using v1.0 FADT reserved value for pstate_cnt\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Len Brown4be44fc2005-08-05 00:44:28 -0400377 status = acpi_os_write_port(acpi_fadt.smi_cmd,
378 (u32) acpi_fadt.pstate_cnt, 8);
379 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400380 ACPI_EXCEPTION((AE_INFO, status,
381 "Failed to write pstate_cnt [0x%x] to "
382 "smi_cmd [0x%x]", acpi_fadt.pstate_cnt,
383 acpi_fadt.smi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
388 /* Success. If there's no _PPC, we need to fear nothing, so
389 * we can allow the cpufreq driver to be rmmod'ed. */
390 is_done = 1;
391
392 if (!(acpi_processor_ppc_status & PPC_IN_USE))
393 module_put(calling_module);
394
Patrick Mocheld550d982006-06-27 00:41:40 -0400395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Len Brown4be44fc2005-08-05 00:44:28 -0400398EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
401/* /proc/acpi/processor/../performance interface (DEPRECATED) */
402
403static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
404static struct file_operations acpi_processor_perf_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400405 .open = acpi_processor_perf_open_fs,
406 .read = seq_read,
407 .llseek = seq_lseek,
408 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
411static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
412{
Len Brown4be44fc2005-08-05 00:44:28 -0400413 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
414 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!pr)
418 goto end;
419
420 if (!pr->performance) {
421 seq_puts(seq, "<not supported>\n");
422 goto end;
423 }
424
425 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400426 "active state: P%d\n",
427 pr->performance->state_count, pr->performance->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 seq_puts(seq, "states:\n");
430 for (i = 0; i < pr->performance->state_count; i++)
Len Brown4be44fc2005-08-05 00:44:28 -0400431 seq_printf(seq,
432 " %cP%d: %d MHz, %d mW, %d uS\n",
433 (i == pr->performance->state ? '*' : ' '), i,
434 (u32) pr->performance->states[i].core_frequency,
435 (u32) pr->performance->states[i].power,
436 (u32) pr->performance->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Len Brown4be44fc2005-08-05 00:44:28 -0400438 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
443{
444 return single_open(file, acpi_processor_perf_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400445 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400449acpi_processor_write_performance(struct file *file,
450 const char __user * buffer,
451 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Len Brown4be44fc2005-08-05 00:44:28 -0400453 int result = 0;
454 struct seq_file *m = (struct seq_file *)file->private_data;
455 struct acpi_processor *pr = (struct acpi_processor *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct acpi_processor_performance *perf;
Len Brown4be44fc2005-08-05 00:44:28 -0400457 char state_string[12] = { '\0' };
458 unsigned int new_state = 0;
459 struct cpufreq_policy policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (!pr || (count > sizeof(state_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 perf = pr->performance;
466 if (!perf)
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (copy_from_user(state_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 state_string[count] = '\0';
473 new_state = simple_strtoul(state_string, NULL, 0);
474
475 if (new_state >= perf->state_count)
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 cpufreq_get_policy(&policy, pr->id);
479
480 policy.cpu = pr->id;
481 policy.min = perf->states[new_state].core_frequency * 1000;
482 policy.max = perf->states[new_state].core_frequency * 1000;
483
484 result = cpufreq_set_policy(&policy);
485 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400486 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Len Brown4be44fc2005-08-05 00:44:28 -0400491static void acpi_cpufreq_add_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Len Brown4be44fc2005-08-05 00:44:28 -0400493 struct proc_dir_entry *entry = NULL;
494 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* add file 'performance' [R/W] */
501 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400502 S_IFREG | S_IRUGO | S_IWUSR,
503 acpi_device_dir(device));
Thomas Renningera6fc6722006-06-26 23:58:43 -0400504 if (entry){
Arjan van de Vend479e902006-01-06 16:47:00 -0500505 acpi_processor_perf_fops.write = acpi_processor_write_performance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 entry->proc_fops = &acpi_processor_perf_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 entry->data = acpi_driver_data(device);
508 entry->owner = THIS_MODULE;
509 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Len Brown4be44fc2005-08-05 00:44:28 -0400513static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Len Brown4be44fc2005-08-05 00:44:28 -0400515 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* remove file 'performance' */
522 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400523 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Patrick Mocheld550d982006-06-27 00:41:40 -0400525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528#else
Len Brown4be44fc2005-08-05 00:44:28 -0400529static void acpi_cpufreq_add_file(struct acpi_processor *pr)
530{
531 return;
532}
533static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
534{
535 return;
536}
537#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500539static int acpi_processor_get_psd(struct acpi_processor *pr)
540{
541 int result = 0;
542 acpi_status status = AE_OK;
543 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
544 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
545 struct acpi_buffer state = {0, NULL};
546 union acpi_object *psd = NULL;
547 struct acpi_psd_package *pdomain;
548
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500549 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
550 if (ACPI_FAILURE(status)) {
Len Brown9011bff2006-05-11 00:28:12 -0400551 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500552 }
553
554 psd = (union acpi_object *) buffer.pointer;
555 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
556 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
557 result = -EFAULT;
558 goto end;
559 }
560
561 if (psd->package.count != 1) {
562 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
563 result = -EFAULT;
564 goto end;
565 }
566
567 pdomain = &(pr->performance->domain_info);
568
569 state.length = sizeof(struct acpi_psd_package);
570 state.pointer = pdomain;
571
572 status = acpi_extract_package(&(psd->package.elements[0]),
573 &format, &state);
574 if (ACPI_FAILURE(status)) {
575 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
576 result = -EFAULT;
577 goto end;
578 }
579
580 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
581 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
582 result = -EFAULT;
583 goto end;
584 }
585
586 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
587 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
588 result = -EFAULT;
589 goto end;
590 }
591
592end:
Len Brown02438d82006-06-30 03:19:10 -0400593 kfree(buffer.pointer);
Len Brown9011bff2006-05-11 00:28:12 -0400594 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500595}
596
597int acpi_processor_preregister_performance(
598 struct acpi_processor_performance **performance)
599{
600 int count, count_target;
601 int retval = 0;
602 unsigned int i, j;
603 cpumask_t covered_cpus;
604 struct acpi_processor *pr;
605 struct acpi_psd_package *pdomain;
606 struct acpi_processor *match_pr;
607 struct acpi_psd_package *match_pdomain;
608
Len Brown785fccc2006-06-15 22:19:31 -0400609 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500610
611 retval = 0;
612
613 /* Call _PSD for all CPUs */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400614 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500615 pr = processors[i];
616 if (!pr) {
617 /* Look only at processors in ACPI namespace */
618 continue;
619 }
620
621 if (pr->performance) {
622 retval = -EBUSY;
623 continue;
624 }
625
626 if (!performance || !performance[i]) {
627 retval = -EINVAL;
628 continue;
629 }
630
631 pr->performance = performance[i];
632 cpu_set(i, pr->performance->shared_cpu_map);
633 if (acpi_processor_get_psd(pr)) {
634 retval = -EINVAL;
635 continue;
636 }
637 }
638 if (retval)
639 goto err_ret;
640
641 /*
642 * Now that we have _PSD data from all CPUs, lets setup P-state
643 * domain info.
644 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400645 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500646 pr = processors[i];
647 if (!pr)
648 continue;
649
650 /* Basic validity check for domain info */
651 pdomain = &(pr->performance->domain_info);
652 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
653 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
654 retval = -EINVAL;
655 goto err_ret;
656 }
657 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
658 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
659 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
660 retval = -EINVAL;
661 goto err_ret;
662 }
663 }
664
665 cpus_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400666 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500667 pr = processors[i];
668 if (!pr)
669 continue;
670
671 if (cpu_isset(i, covered_cpus))
672 continue;
673
674 pdomain = &(pr->performance->domain_info);
675 cpu_set(i, pr->performance->shared_cpu_map);
676 cpu_set(i, covered_cpus);
677 if (pdomain->num_processors <= 1)
678 continue;
679
680 /* Validate the Domain info */
681 count_target = pdomain->num_processors;
682 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400683 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500684 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400685 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
686 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
687 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500688 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500689
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400690 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500691 if (i == j)
692 continue;
693
694 match_pr = processors[j];
695 if (!match_pr)
696 continue;
697
698 match_pdomain = &(match_pr->performance->domain_info);
699 if (match_pdomain->domain != pdomain->domain)
700 continue;
701
702 /* Here i and j are in the same domain */
703
704 if (match_pdomain->num_processors != count_target) {
705 retval = -EINVAL;
706 goto err_ret;
707 }
708
709 if (pdomain->coord_type != match_pdomain->coord_type) {
710 retval = -EINVAL;
711 goto err_ret;
712 }
713
714 cpu_set(j, covered_cpus);
715 cpu_set(j, pr->performance->shared_cpu_map);
716 count++;
717 }
718
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400719 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500720 if (i == j)
721 continue;
722
723 match_pr = processors[j];
724 if (!match_pr)
725 continue;
726
727 match_pdomain = &(match_pr->performance->domain_info);
728 if (match_pdomain->domain != pdomain->domain)
729 continue;
730
731 match_pr->performance->shared_type =
732 pr->performance->shared_type;
733 match_pr->performance->shared_cpu_map =
734 pr->performance->shared_cpu_map;
735 }
736 }
737
738err_ret:
739 if (retval) {
740 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error while parsing _PSD domain information. Assuming no coordination\n"));
741 }
742
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400743 for_each_possible_cpu(i) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500744 pr = processors[i];
745 if (!pr || !pr->performance)
746 continue;
747
748 /* Assume no coordination on any error parsing domain info */
749 if (retval) {
750 cpus_clear(pr->performance->shared_cpu_map);
751 cpu_set(i, pr->performance->shared_cpu_map);
752 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
753 }
754 pr->performance = NULL; /* Will be set for real in register */
755 }
756
Len Brown785fccc2006-06-15 22:19:31 -0400757 mutex_unlock(&performance_mutex);
Len Brown9011bff2006-05-11 00:28:12 -0400758 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500759}
760EXPORT_SYMBOL(acpi_processor_preregister_performance);
761
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763int
Len Brown4be44fc2005-08-05 00:44:28 -0400764acpi_processor_register_performance(struct acpi_processor_performance
765 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct acpi_processor *pr;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400773 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 pr = processors[cpu];
776 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400777 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400778 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400782 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400783 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Andrew Mortona913f502006-06-10 09:54:13 -0700786 WARN_ON(!performance);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 pr->performance = performance;
789
790 if (acpi_processor_get_performance_info(pr)) {
791 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400792 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400793 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 acpi_cpufreq_add_file(pr);
797
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400798 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
Len Brown4be44fc2005-08-05 00:44:28 -0400801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802EXPORT_SYMBOL(acpi_processor_register_performance);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804void
Len Brown4be44fc2005-08-05 00:44:28 -0400805acpi_processor_unregister_performance(struct acpi_processor_performance
806 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct acpi_processor *pr;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400811 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 pr = processors[cpu];
814 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400815 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Andrew Mortona913f502006-06-10 09:54:13 -0700819 if (pr->performance)
820 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 pr->performance = NULL;
822
823 acpi_cpufreq_remove_file(pr);
824
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400825 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Patrick Mocheld550d982006-06-27 00:41:40 -0400827 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
Len Brown4be44fc2005-08-05 00:44:28 -0400829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830EXPORT_SYMBOL(acpi_processor_unregister_performance);