blob: 01e366d2b6fb14475e0b6074b886fd8b55877348 [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
Miao Xie16be87e2008-10-24 17:22:04 +080034#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +020035#include <asm/cpufeature.h>
Miao Xie16be87e2008-10-24 17:22:04 +080036#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <acpi/acpi_bus.h>
Bjorn Helgaas89595b82008-11-07 16:57:45 -070039#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <acpi/processor.h>
41
Len Browna192a952009-07-28 16:45:54 -040042#define PREFIX "ACPI: "
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
46#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050047ACPI_MODULE_NAME("processor_perflib");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040049static DEFINE_MUTEX(performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Thomas Renninger919158d2007-10-31 15:41:42 +010051/* Use cpufreq debug layer for _PPC changes. */
52#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
53 "cpufreq-core", msg)
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * _PPC support is implemented as a CPUfreq policy notifier:
57 * This means each time a CPUfreq driver registered also with
58 * the ACPI core is asked to change the speed policy, the maximum
59 * value is adjusted so that it is within the platform limit.
60 *
61 * Also, when a new platform limit value is detected, the CPUfreq
62 * policy is adjusted accordingly.
63 */
64
Thomas Renningera1531ac2008-07-29 22:32:58 -070065/* ignore_ppc:
66 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
67 * ignore _PPC
68 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
69 * 1 -> ignore _PPC totally -> forced by user through boot param
70 */
Milan Broz9f497bc2008-08-12 17:48:27 +020071static int ignore_ppc = -1;
Milan Broz613e5f32008-08-16 02:11:28 +020072module_param(ignore_ppc, int, 0644);
Thomas Renninger623b78c2007-05-18 21:59:28 -050073MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
74 "limited by BIOS, this should help");
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define PPC_REGISTERED 1
77#define PPC_IN_USE 2
78
Thomas Renningera1531ac2008-07-29 22:32:58 -070079static int acpi_processor_ppc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040082 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct cpufreq_policy *policy = data;
85 struct acpi_processor *pr;
86 unsigned int ppc = 0;
87
Thomas Renningera1531ac2008-07-29 22:32:58 -070088 if (event == CPUFREQ_START && ignore_ppc <= 0) {
89 ignore_ppc = 0;
90 return 0;
91 }
92
Thomas Renninger623b78c2007-05-18 21:59:28 -050093 if (ignore_ppc)
94 return 0;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (event != CPUFREQ_INCOMPATIBLE)
Thomas Renninger9b67c5d2008-07-29 22:32:59 -070097 return 0;
98
99 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Mike Travis706546d2008-06-09 16:22:23 -0700101 pr = per_cpu(processors, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!pr || !pr->performance)
103 goto out;
104
Len Brown4be44fc2005-08-05 00:44:28 -0400105 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dave Jones0916bd32006-11-22 20:42:01 -0500107 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto out;
109
110 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -0400111 pr->performance->states[ppc].
112 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Len Brown4be44fc2005-08-05 00:44:28 -0400114 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400115 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 return 0;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static struct notifier_block acpi_ppc_notifier_block = {
121 .notifier_call = acpi_processor_ppc_notifier,
122};
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Len Brown4be44fc2005-08-05 00:44:28 -0400126 acpi_status status = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400127 unsigned long long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400131 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /*
134 * _PPC indicates the maximum state currently supported by the platform
135 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
136 */
137 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
138
139 if (status != AE_NOT_FOUND)
140 acpi_processor_ppc_status |= PPC_IN_USE;
141
Len Brown4be44fc2005-08-05 00:44:28 -0400142 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400143 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400144 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Thomas Renninger919158d2007-10-31 15:41:42 +0100147 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
148 (int)ppc, ppc ? "" : "not");
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Patrick Mocheld550d982006-06-27 00:41:40 -0400152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Len Brown4be44fc2005-08-05 00:44:28 -0400155int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500157 int ret;
158
159 if (ignore_ppc)
160 return 0;
161
162 ret = acpi_processor_get_platform_limit(pr);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ret < 0)
165 return (ret);
166 else
167 return cpufreq_update_policy(pr->id);
168}
169
Thomas Renningere2f74f32009-11-19 12:31:01 +0100170int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
171{
172 struct acpi_processor *pr;
173
174 pr = per_cpu(processors, cpu);
175 if (!pr || !pr->performance || !pr->performance->state_count)
176 return -ENODEV;
177 *limit = pr->performance->states[pr->performance_platform_limit].
178 core_frequency * 1000;
179 return 0;
180}
181EXPORT_SYMBOL(acpi_processor_get_bios_limit);
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183void acpi_processor_ppc_init(void)
184{
185 if (!cpufreq_register_notifier
186 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 acpi_processor_ppc_status |= PPC_REGISTERED;
188 else
Len Brown4be44fc2005-08-05 00:44:28 -0400189 printk(KERN_DEBUG
190 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Len Brown4be44fc2005-08-05 00:44:28 -0400193void acpi_processor_ppc_exit(void)
194{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400196 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
197 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 acpi_processor_ppc_status &= ~PPC_REGISTERED;
200}
201
Len Brown4be44fc2005-08-05 00:44:28 -0400202static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Len Brown4be44fc2005-08-05 00:44:28 -0400204 int result = 0;
205 acpi_status status = 0;
206 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
207 union acpi_object *pct = NULL;
208 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400212 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400213 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400219 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400220 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 result = -EFAULT;
222 goto end;
223 }
224
225 /*
226 * control_register
227 */
228
229 obj = pct->package.elements[0];
230
231 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400232 || (obj.buffer.length < sizeof(struct acpi_pct_register))
233 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400234 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 result = -EFAULT;
236 goto end;
237 }
Len Brown4be44fc2005-08-05 00:44:28 -0400238 memcpy(&pr->performance->control_register, obj.buffer.pointer,
239 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * status_register
243 */
244
245 obj = pct->package.elements[1];
246
247 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400248 || (obj.buffer.length < sizeof(struct acpi_pct_register))
249 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400250 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 result = -EFAULT;
252 goto end;
253 }
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 memcpy(&pr->performance->status_register, obj.buffer.pointer,
256 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Len Brown4be44fc2005-08-05 00:44:28 -0400258 end:
Len Brown02438d82006-06-30 03:19:10 -0400259 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Patrick Mocheld550d982006-06-27 00:41:40 -0400261 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Len Brown4be44fc2005-08-05 00:44:28 -0400264static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Len Brown4be44fc2005-08-05 00:44:28 -0400266 int result = 0;
267 acpi_status status = AE_OK;
268 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
269 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
270 struct acpi_buffer state = { 0, NULL };
271 union acpi_object *pss = NULL;
272 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400276 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400277 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200281 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400283 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 result = -EFAULT;
285 goto end;
286 }
287
288 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400289 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400292 pr->performance->states =
293 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
294 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!pr->performance->states) {
296 result = -ENOMEM;
297 goto end;
298 }
299
300 for (i = 0; i < pr->performance->state_count; i++) {
301
302 struct acpi_processor_px *px = &(pr->performance->states[i]);
303
304 state.length = sizeof(struct acpi_processor_px);
305 state.pointer = px;
306
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
308
309 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400310 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400312 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 result = -EFAULT;
314 kfree(pr->performance->states);
315 goto end;
316 }
317
318 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400319 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
320 i,
321 (u32) px->core_frequency,
322 (u32) px->power,
323 (u32) px->transition_latency,
324 (u32) px->bus_master_latency,
325 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Len Brown34d531e2009-05-26 15:11:06 -0400327 /*
328 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
329 */
330 if (!px->core_frequency ||
331 ((u32)(px->core_frequency * 1000) !=
332 (px->core_frequency * 1000))) {
333 printk(KERN_ERR FW_BUG PREFIX
334 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
335 px->core_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 result = -EFAULT;
337 kfree(pr->performance->states);
338 goto end;
339 }
340 }
341
Len Brown4be44fc2005-08-05 00:44:28 -0400342 end:
Len Brown02438d82006-06-30 03:19:10 -0400343 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 int result = 0;
351 acpi_status status = AE_OK;
352 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 status = acpi_get_handle(pr->handle, "_PCT", &handle);
358 if (ACPI_FAILURE(status)) {
359 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400360 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400361 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 result = acpi_processor_get_performance_control(pr);
365 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200366 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 result = acpi_processor_get_performance_states(pr);
369 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200370 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return 0;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200373
374 /*
375 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
376 * the BIOS is older than the CPU and does not know its frequencies
377 */
378 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800379#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200380 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
381 if(boot_cpu_has(X86_FEATURE_EST))
382 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
383 "frequency support\n");
384 }
Miao Xie16be87e2008-10-24 17:22:04 +0800385#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200386 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389int acpi_processor_notify_smm(struct module *calling_module)
390{
391 acpi_status status;
392 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400396 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* is_done is set to negative if an error occured,
402 * and to postitive if _no_ error occured, but SMM
403 * was already notified. This avoids double notification
404 * which might lead to unexpected results...
405 */
406 if (is_done > 0) {
407 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400408 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400409 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400411 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 is_done = -EIO;
415
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300416 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300417 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300418 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300424 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300425 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300427 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
428 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400429 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400430 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300431 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300432 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
433 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400435 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 /* Success. If there's no _PPC, we need to fear nothing, so
439 * we can allow the cpufreq driver to be rmmod'ed. */
440 is_done = 1;
441
442 if (!(acpi_processor_ppc_status & PPC_IN_USE))
443 module_put(calling_module);
444
Patrick Mocheld550d982006-06-27 00:41:40 -0400445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Len Brown4be44fc2005-08-05 00:44:28 -0400448EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500450static int acpi_processor_get_psd(struct acpi_processor *pr)
451{
452 int result = 0;
453 acpi_status status = AE_OK;
454 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
455 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
456 struct acpi_buffer state = {0, NULL};
457 union acpi_object *psd = NULL;
458 struct acpi_psd_package *pdomain;
459
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500460 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
461 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400462 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500463 }
464
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200465 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500466 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800467 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500468 result = -EFAULT;
469 goto end;
470 }
471
472 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800473 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500474 result = -EFAULT;
475 goto end;
476 }
477
478 pdomain = &(pr->performance->domain_info);
479
480 state.length = sizeof(struct acpi_psd_package);
481 state.pointer = pdomain;
482
483 status = acpi_extract_package(&(psd->package.elements[0]),
484 &format, &state);
485 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800486 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500487 result = -EFAULT;
488 goto end;
489 }
490
491 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800492 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500493 result = -EFAULT;
494 goto end;
495 }
496
497 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800498 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500499 result = -EFAULT;
500 goto end;
501 }
502
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100503 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
504 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
505 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
506 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
507 result = -EFAULT;
508 goto end;
509 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500510end:
Len Brown02438d82006-06-30 03:19:10 -0400511 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400512 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500513}
514
515int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400516 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500517{
518 int count, count_target;
519 int retval = 0;
520 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800521 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500522 struct acpi_processor *pr;
523 struct acpi_psd_package *pdomain;
524 struct acpi_processor *match_pr;
525 struct acpi_psd_package *match_pdomain;
526
Li Zefan79f55992009-06-15 14:58:26 +0800527 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800528 return -ENOMEM;
529
Len Brown785fccc2006-06-15 22:19:31 -0400530 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500531
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100532 /*
533 * Check if another driver has already registered, and abort before
534 * changing pr->performance if it has. Check input data as well.
535 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400536 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700537 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500538 if (!pr) {
539 /* Look only at processors in ACPI namespace */
540 continue;
541 }
542
543 if (pr->performance) {
544 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100545 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500546 }
547
Rusty Russellb36128c2009-02-20 16:29:08 +0900548 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500549 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100550 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500551 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100552 }
553
554 /* Call _PSD for all CPUs */
555 for_each_possible_cpu(i) {
556 pr = per_cpu(processors, i);
557 if (!pr)
558 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500559
Rusty Russellb36128c2009-02-20 16:29:08 +0900560 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800561 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500562 if (acpi_processor_get_psd(pr)) {
563 retval = -EINVAL;
564 continue;
565 }
566 }
567 if (retval)
568 goto err_ret;
569
570 /*
571 * Now that we have _PSD data from all CPUs, lets setup P-state
572 * domain info.
573 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400574 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700575 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500576 if (!pr)
577 continue;
578
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800579 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500580 continue;
581
582 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800583 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
584 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500585 if (pdomain->num_processors <= 1)
586 continue;
587
588 /* Validate the Domain info */
589 count_target = pdomain->num_processors;
590 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400591 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500592 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400593 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
594 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
595 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500596 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500597
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400598 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500599 if (i == j)
600 continue;
601
Mike Travis706546d2008-06-09 16:22:23 -0700602 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500603 if (!match_pr)
604 continue;
605
606 match_pdomain = &(match_pr->performance->domain_info);
607 if (match_pdomain->domain != pdomain->domain)
608 continue;
609
610 /* Here i and j are in the same domain */
611
612 if (match_pdomain->num_processors != count_target) {
613 retval = -EINVAL;
614 goto err_ret;
615 }
616
617 if (pdomain->coord_type != match_pdomain->coord_type) {
618 retval = -EINVAL;
619 goto err_ret;
620 }
621
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800622 cpumask_set_cpu(j, covered_cpus);
623 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500624 count++;
625 }
626
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400627 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500628 if (i == j)
629 continue;
630
Mike Travis706546d2008-06-09 16:22:23 -0700631 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500632 if (!match_pr)
633 continue;
634
635 match_pdomain = &(match_pr->performance->domain_info);
636 if (match_pdomain->domain != pdomain->domain)
637 continue;
638
639 match_pr->performance->shared_type =
640 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800641 cpumask_copy(match_pr->performance->shared_cpu_map,
642 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500643 }
644 }
645
646err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400647 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700648 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500649 if (!pr || !pr->performance)
650 continue;
651
652 /* Assume no coordination on any error parsing domain info */
653 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800654 cpumask_clear(pr->performance->shared_cpu_map);
655 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500656 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
657 }
658 pr->performance = NULL; /* Will be set for real in register */
659 }
660
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100661err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400662 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800663 free_cpumask_var(covered_cpus);
Len Brown9011bff42006-05-11 00:28:12 -0400664 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500665}
666EXPORT_SYMBOL(acpi_processor_preregister_performance);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668int
Len Brown4be44fc2005-08-05 00:44:28 -0400669acpi_processor_register_performance(struct acpi_processor_performance
670 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct acpi_processor *pr;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400675 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400677 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Mike Travis706546d2008-06-09 16:22:23 -0700679 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400681 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400682 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400686 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400687 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Andrew Mortona913f502006-06-10 09:54:13 -0700690 WARN_ON(!performance);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 pr->performance = performance;
693
694 if (acpi_processor_get_performance_info(pr)) {
695 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400696 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400700 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Len Brown4be44fc2005-08-05 00:44:28 -0400703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704EXPORT_SYMBOL(acpi_processor_register_performance);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706void
Len Brown4be44fc2005-08-05 00:44:28 -0400707acpi_processor_unregister_performance(struct acpi_processor_performance
708 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 struct acpi_processor *pr;
711
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400712 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Mike Travis706546d2008-06-09 16:22:23 -0700714 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400716 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Andrew Mortona913f502006-06-10 09:54:13 -0700720 if (pr->performance)
721 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 pr->performance = NULL;
723
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400724 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
Len Brown4be44fc2005-08-05 00:44:28 -0400728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729EXPORT_SYMBOL(acpi_processor_unregister_performance);