blob: 242f8143008ab728e963fcba99dea7bc1e1e7bd7 [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
Thomas Renninger910dfae2008-09-01 14:27:04 +020041#include <asm/cpufeature.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <acpi/acpi_bus.h>
44#include <acpi/processor.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_PROCESSOR_COMPONENT 0x01000000
47#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
49#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050050ACPI_MODULE_NAME("processor_perflib");
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
Thomas Renninger919158d2007-10-31 15:41:42 +010054/* Use cpufreq debug layer for _PPC changes. */
55#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
56 "cpufreq-core", msg)
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * _PPC support is implemented as a CPUfreq policy notifier:
60 * This means each time a CPUfreq driver registered also with
61 * the ACPI core is asked to change the speed policy, the maximum
62 * value is adjusted so that it is within the platform limit.
63 *
64 * Also, when a new platform limit value is detected, the CPUfreq
65 * policy is adjusted accordingly.
66 */
67
Thomas Renningera1531ac2008-07-29 22:32:58 -070068/* ignore_ppc:
69 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
70 * ignore _PPC
71 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
72 * 1 -> ignore _PPC totally -> forced by user through boot param
73 */
Milan Broz9f497bc2008-08-12 17:48:27 +020074static int ignore_ppc = -1;
Milan Broz613e5f32008-08-16 02:11:28 +020075module_param(ignore_ppc, int, 0644);
Thomas Renninger623b78c2007-05-18 21:59:28 -050076MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
77 "limited by BIOS, this should help");
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define PPC_REGISTERED 1
80#define PPC_IN_USE 2
81
Thomas Renningera1531ac2008-07-29 22:32:58 -070082static int acpi_processor_ppc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040085 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct cpufreq_policy *policy = data;
88 struct acpi_processor *pr;
89 unsigned int ppc = 0;
90
Thomas Renningera1531ac2008-07-29 22:32:58 -070091 if (event == CPUFREQ_START && ignore_ppc <= 0) {
92 ignore_ppc = 0;
93 return 0;
94 }
95
Thomas Renninger623b78c2007-05-18 21:59:28 -050096 if (ignore_ppc)
97 return 0;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (event != CPUFREQ_INCOMPATIBLE)
Thomas Renninger9b67c5d2008-07-29 22:32:59 -0700100 return 0;
101
102 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Mike Travis706546d2008-06-09 16:22:23 -0700104 pr = per_cpu(processors, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!pr || !pr->performance)
106 goto out;
107
Len Brown4be44fc2005-08-05 00:44:28 -0400108 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Dave Jones0916bd32006-11-22 20:42:01 -0500110 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 goto out;
112
113 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -0400114 pr->performance->states[ppc].
115 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Len Brown4be44fc2005-08-05 00:44:28 -0400117 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400118 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 return 0;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static struct notifier_block acpi_ppc_notifier_block = {
124 .notifier_call = acpi_processor_ppc_notifier,
125};
126
Len Brown4be44fc2005-08-05 00:44:28 -0400127static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Len Brown4be44fc2005-08-05 00:44:28 -0400129 acpi_status status = 0;
130 unsigned long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /*
137 * _PPC indicates the maximum state currently supported by the platform
138 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
139 */
140 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
141
142 if (status != AE_NOT_FOUND)
143 acpi_processor_ppc_status |= PPC_IN_USE;
144
Len Brown4be44fc2005-08-05 00:44:28 -0400145 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400146 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400147 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
Thomas Renninger919158d2007-10-31 15:41:42 +0100150 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
151 (int)ppc, ppc ? "" : "not");
152
Len Brown4be44fc2005-08-05 00:44:28 -0400153 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Patrick Mocheld550d982006-06-27 00:41:40 -0400155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500160 int ret;
161
162 if (ignore_ppc)
163 return 0;
164
165 ret = acpi_processor_get_platform_limit(pr);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (ret < 0)
168 return (ret);
169 else
170 return cpufreq_update_policy(pr->id);
171}
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173void acpi_processor_ppc_init(void)
174{
175 if (!cpufreq_register_notifier
176 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 acpi_processor_ppc_status |= PPC_REGISTERED;
178 else
Len Brown4be44fc2005-08-05 00:44:28 -0400179 printk(KERN_DEBUG
180 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183void acpi_processor_ppc_exit(void)
184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400186 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
187 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 acpi_processor_ppc_status &= ~PPC_REGISTERED;
190}
191
Len Brown4be44fc2005-08-05 00:44:28 -0400192static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Len Brown4be44fc2005-08-05 00:44:28 -0400194 int result = 0;
195 acpi_status status = 0;
196 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
197 union acpi_object *pct = NULL;
198 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400202 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400203 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400204 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Len Brown4be44fc2005-08-05 00:44:28 -0400207 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400209 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400210 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 result = -EFAULT;
212 goto end;
213 }
214
215 /*
216 * control_register
217 */
218
219 obj = pct->package.elements[0];
220
221 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400222 || (obj.buffer.length < sizeof(struct acpi_pct_register))
223 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400224 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 result = -EFAULT;
226 goto end;
227 }
Len Brown4be44fc2005-08-05 00:44:28 -0400228 memcpy(&pr->performance->control_register, obj.buffer.pointer,
229 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /*
232 * status_register
233 */
234
235 obj = pct->package.elements[1];
236
237 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400238 || (obj.buffer.length < sizeof(struct acpi_pct_register))
239 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400240 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 result = -EFAULT;
242 goto end;
243 }
244
Len Brown4be44fc2005-08-05 00:44:28 -0400245 memcpy(&pr->performance->status_register, obj.buffer.pointer,
246 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 end:
Len Brown02438d82006-06-30 03:19:10 -0400249 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Len Brown4be44fc2005-08-05 00:44:28 -0400256 int result = 0;
257 acpi_status status = AE_OK;
258 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
259 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
260 struct acpi_buffer state = { 0, NULL };
261 union acpi_object *pss = NULL;
262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400266 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400267 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200271 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400273 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 result = -EFAULT;
275 goto end;
276 }
277
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400279 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400282 pr->performance->states =
283 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
284 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!pr->performance->states) {
286 result = -ENOMEM;
287 goto end;
288 }
289
290 for (i = 0; i < pr->performance->state_count; i++) {
291
292 struct acpi_processor_px *px = &(pr->performance->states[i]);
293
294 state.length = sizeof(struct acpi_processor_px);
295 state.pointer = px;
296
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
298
299 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400300 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400302 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 result = -EFAULT;
304 kfree(pr->performance->states);
305 goto end;
306 }
307
308 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400309 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
310 i,
311 (u32) px->core_frequency,
312 (u32) px->power,
313 (u32) px->transition_latency,
314 (u32) px->bus_master_latency,
315 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400318 printk(KERN_ERR PREFIX
319 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 result = -EFAULT;
321 kfree(pr->performance->states);
322 goto end;
323 }
324 }
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326 end:
Len Brown02438d82006-06-30 03:19:10 -0400327 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Len Brown4be44fc2005-08-05 00:44:28 -0400334 int result = 0;
335 acpi_status status = AE_OK;
336 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = acpi_get_handle(pr->handle, "_PCT", &handle);
342 if (ACPI_FAILURE(status)) {
343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400344 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 result = acpi_processor_get_performance_control(pr);
349 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200350 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 result = acpi_processor_get_performance_states(pr);
353 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200354 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Patrick Mocheld550d982006-06-27 00:41:40 -0400356 return 0;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200357
358 /*
359 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
360 * the BIOS is older than the CPU and does not know its frequencies
361 */
362 update_bios:
363 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
364 if(boot_cpu_has(X86_FEATURE_EST))
365 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
366 "frequency support\n");
367 }
368 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Len Brown4be44fc2005-08-05 00:44:28 -0400371int acpi_processor_notify_smm(struct module *calling_module)
372{
373 acpi_status status;
374 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* is_done is set to negative if an error occured,
384 * and to postitive if _no_ error occured, but SMM
385 * was already notified. This avoids double notification
386 * which might lead to unexpected results...
387 */
388 if (is_done > 0) {
389 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400391 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400393 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 is_done = -EIO;
397
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300398 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300399 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300400 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
Len Brown4be44fc2005-08-05 00:44:28 -0400405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300406 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300407 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300409 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
410 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400411 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400412 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300413 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300414 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
415 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400417 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 /* Success. If there's no _PPC, we need to fear nothing, so
421 * we can allow the cpufreq driver to be rmmod'ed. */
422 is_done = 1;
423
424 if (!(acpi_processor_ppc_status & PPC_IN_USE))
425 module_put(calling_module);
426
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Len Brown4be44fc2005-08-05 00:44:28 -0400430EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
433/* /proc/acpi/processor/../performance interface (DEPRECATED) */
434
435static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
436static struct file_operations acpi_processor_perf_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700437 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400438 .open = acpi_processor_perf_open_fs,
439 .read = seq_read,
440 .llseek = seq_lseek,
441 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
444static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
445{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200446 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400447 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (!pr)
451 goto end;
452
453 if (!pr->performance) {
454 seq_puts(seq, "<not supported>\n");
455 goto end;
456 }
457
458 seq_printf(seq, "state count: %d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400459 "active state: P%d\n",
460 pr->performance->state_count, pr->performance->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 seq_puts(seq, "states:\n");
463 for (i = 0; i < pr->performance->state_count; i++)
Len Brown4be44fc2005-08-05 00:44:28 -0400464 seq_printf(seq,
465 " %cP%d: %d MHz, %d mW, %d uS\n",
466 (i == pr->performance->state ? '*' : ' '), i,
467 (u32) pr->performance->states[i].core_frequency,
468 (u32) pr->performance->states[i].power,
469 (u32) pr->performance->states[i].transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Len Brown4be44fc2005-08-05 00:44:28 -0400471 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
476{
477 return single_open(file, acpi_processor_perf_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -0400478 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481static void acpi_cpufreq_add_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown4be44fc2005-08-05 00:44:28 -0400483 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400487 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* add file 'performance' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700490 proc_create_data(ACPI_PROCESSOR_FILE_PERFORMANCE, S_IFREG | S_IRUGO,
491 acpi_device_dir(device),
492 &acpi_processor_perf_fops, acpi_driver_data(device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Len Brown4be44fc2005-08-05 00:44:28 -0400496static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown4be44fc2005-08-05 00:44:28 -0400498 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (acpi_bus_get_device(pr->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /* remove file 'performance' */
505 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
Len Brown4be44fc2005-08-05 00:44:28 -0400506 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Patrick Mocheld550d982006-06-27 00:41:40 -0400508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511#else
Len Brown4be44fc2005-08-05 00:44:28 -0400512static void acpi_cpufreq_add_file(struct acpi_processor *pr)
513{
514 return;
515}
516static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
517{
518 return;
519}
520#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500522static int acpi_processor_get_psd(struct acpi_processor *pr)
523{
524 int result = 0;
525 acpi_status status = AE_OK;
526 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
527 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
528 struct acpi_buffer state = {0, NULL};
529 union acpi_object *psd = NULL;
530 struct acpi_psd_package *pdomain;
531
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500532 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
533 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400534 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500535 }
536
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200537 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500538 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
539 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
540 result = -EFAULT;
541 goto end;
542 }
543
544 if (psd->package.count != 1) {
545 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
546 result = -EFAULT;
547 goto end;
548 }
549
550 pdomain = &(pr->performance->domain_info);
551
552 state.length = sizeof(struct acpi_psd_package);
553 state.pointer = pdomain;
554
555 status = acpi_extract_package(&(psd->package.elements[0]),
556 &format, &state);
557 if (ACPI_FAILURE(status)) {
558 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
559 result = -EFAULT;
560 goto end;
561 }
562
563 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
564 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
565 result = -EFAULT;
566 goto end;
567 }
568
569 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
570 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
571 result = -EFAULT;
572 goto end;
573 }
574
575end:
Len Brown02438d82006-06-30 03:19:10 -0400576 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400577 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500578}
579
580int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400581 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500582{
583 int count, count_target;
584 int retval = 0;
585 unsigned int i, j;
586 cpumask_t covered_cpus;
587 struct acpi_processor *pr;
588 struct acpi_psd_package *pdomain;
589 struct acpi_processor *match_pr;
590 struct acpi_psd_package *match_pdomain;
591
Len Brown785fccc2006-06-15 22:19:31 -0400592 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500593
594 retval = 0;
595
596 /* Call _PSD for all CPUs */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400597 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700598 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500599 if (!pr) {
600 /* Look only at processors in ACPI namespace */
601 continue;
602 }
603
604 if (pr->performance) {
605 retval = -EBUSY;
606 continue;
607 }
608
Fenghua Yu50109292007-08-07 18:40:30 -0400609 if (!performance || !percpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500610 retval = -EINVAL;
611 continue;
612 }
613
Fenghua Yu50109292007-08-07 18:40:30 -0400614 pr->performance = percpu_ptr(performance, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500615 cpu_set(i, pr->performance->shared_cpu_map);
616 if (acpi_processor_get_psd(pr)) {
617 retval = -EINVAL;
618 continue;
619 }
620 }
621 if (retval)
622 goto err_ret;
623
624 /*
625 * Now that we have _PSD data from all CPUs, lets setup P-state
626 * domain info.
627 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400628 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700629 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500630 if (!pr)
631 continue;
632
633 /* Basic validity check for domain info */
634 pdomain = &(pr->performance->domain_info);
635 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
636 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
637 retval = -EINVAL;
638 goto err_ret;
639 }
640 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
641 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
642 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
643 retval = -EINVAL;
644 goto err_ret;
645 }
646 }
647
648 cpus_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400649 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700650 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500651 if (!pr)
652 continue;
653
654 if (cpu_isset(i, covered_cpus))
655 continue;
656
657 pdomain = &(pr->performance->domain_info);
658 cpu_set(i, pr->performance->shared_cpu_map);
659 cpu_set(i, covered_cpus);
660 if (pdomain->num_processors <= 1)
661 continue;
662
663 /* Validate the Domain info */
664 count_target = pdomain->num_processors;
665 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400666 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500667 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400668 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
669 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
670 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500671 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500672
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400673 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500674 if (i == j)
675 continue;
676
Mike Travis706546d2008-06-09 16:22:23 -0700677 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500678 if (!match_pr)
679 continue;
680
681 match_pdomain = &(match_pr->performance->domain_info);
682 if (match_pdomain->domain != pdomain->domain)
683 continue;
684
685 /* Here i and j are in the same domain */
686
687 if (match_pdomain->num_processors != count_target) {
688 retval = -EINVAL;
689 goto err_ret;
690 }
691
692 if (pdomain->coord_type != match_pdomain->coord_type) {
693 retval = -EINVAL;
694 goto err_ret;
695 }
696
697 cpu_set(j, covered_cpus);
698 cpu_set(j, pr->performance->shared_cpu_map);
699 count++;
700 }
701
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400702 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500703 if (i == j)
704 continue;
705
Mike Travis706546d2008-06-09 16:22:23 -0700706 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500707 if (!match_pr)
708 continue;
709
710 match_pdomain = &(match_pr->performance->domain_info);
711 if (match_pdomain->domain != pdomain->domain)
712 continue;
713
714 match_pr->performance->shared_type =
715 pr->performance->shared_type;
716 match_pr->performance->shared_cpu_map =
717 pr->performance->shared_cpu_map;
718 }
719 }
720
721err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400722 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700723 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500724 if (!pr || !pr->performance)
725 continue;
726
727 /* Assume no coordination on any error parsing domain info */
728 if (retval) {
729 cpus_clear(pr->performance->shared_cpu_map);
730 cpu_set(i, pr->performance->shared_cpu_map);
731 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
732 }
733 pr->performance = NULL; /* Will be set for real in register */
734 }
735
Len Brown785fccc2006-06-15 22:19:31 -0400736 mutex_unlock(&performance_mutex);
Len Brown9011bff42006-05-11 00:28:12 -0400737 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500738}
739EXPORT_SYMBOL(acpi_processor_preregister_performance);
740
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742int
Len Brown4be44fc2005-08-05 00:44:28 -0400743acpi_processor_register_performance(struct acpi_processor_performance
744 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct acpi_processor *pr;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400752 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Mike Travis706546d2008-06-09 16:22:23 -0700754 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400756 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
760 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400761 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400762 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
Andrew Mortona913f502006-06-10 09:54:13 -0700765 WARN_ON(!performance);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 pr->performance = performance;
768
769 if (acpi_processor_get_performance_info(pr)) {
770 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400771 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400772 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 acpi_cpufreq_add_file(pr);
776
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400777 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
Len Brown4be44fc2005-08-05 00:44:28 -0400780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781EXPORT_SYMBOL(acpi_processor_register_performance);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783void
Len Brown4be44fc2005-08-05 00:44:28 -0400784acpi_processor_unregister_performance(struct acpi_processor_performance
785 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct acpi_processor *pr;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400790 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Mike Travis706546d2008-06-09 16:22:23 -0700792 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400794 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Andrew Mortona913f502006-06-10 09:54:13 -0700798 if (pr->performance)
799 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 pr->performance = NULL;
801
802 acpi_cpufreq_remove_file(pr);
803
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400804 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Patrick Mocheld550d982006-06-27 00:41:40 -0400806 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Len Brown4be44fc2005-08-05 00:44:28 -0400808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809EXPORT_SYMBOL(acpi_processor_unregister_performance);