blob: cafb41000f6bd6b36eaaa4b4348faef59e5c0e49 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
44#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050045ACPI_MODULE_NAME("processor_perflib");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040047static DEFINE_MUTEX(performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Thomas Renninger919158d2007-10-31 15:41:42 +010049/* Use cpufreq debug layer for _PPC changes. */
50#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
51 "cpufreq-core", msg)
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * _PPC support is implemented as a CPUfreq policy notifier:
55 * This means each time a CPUfreq driver registered also with
56 * the ACPI core is asked to change the speed policy, the maximum
57 * value is adjusted so that it is within the platform limit.
58 *
59 * Also, when a new platform limit value is detected, the CPUfreq
60 * policy is adjusted accordingly.
61 */
62
Thomas Renningera1531ac2008-07-29 22:32:58 -070063/* ignore_ppc:
64 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
65 * ignore _PPC
66 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
67 * 1 -> ignore _PPC totally -> forced by user through boot param
68 */
Milan Broz9f497bc2008-08-12 17:48:27 +020069static int ignore_ppc = -1;
Milan Broz613e5f32008-08-16 02:11:28 +020070module_param(ignore_ppc, int, 0644);
Thomas Renninger623b78c2007-05-18 21:59:28 -050071MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
72 "limited by BIOS, this should help");
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define PPC_REGISTERED 1
75#define PPC_IN_USE 2
76
Thomas Renningera1531ac2008-07-29 22:32:58 -070077static int acpi_processor_ppc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040080 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct cpufreq_policy *policy = data;
83 struct acpi_processor *pr;
84 unsigned int ppc = 0;
85
Thomas Renningera1531ac2008-07-29 22:32:58 -070086 if (event == CPUFREQ_START && ignore_ppc <= 0) {
87 ignore_ppc = 0;
88 return 0;
89 }
90
Thomas Renninger623b78c2007-05-18 21:59:28 -050091 if (ignore_ppc)
92 return 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (event != CPUFREQ_INCOMPATIBLE)
Thomas Renninger9b67c5d2008-07-29 22:32:59 -070095 return 0;
96
97 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Mike Travis706546d2008-06-09 16:22:23 -070099 pr = per_cpu(processors, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!pr || !pr->performance)
101 goto out;
102
Len Brown4be44fc2005-08-05 00:44:28 -0400103 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Jones0916bd32006-11-22 20:42:01 -0500105 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out;
107
108 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 pr->performance->states[ppc].
110 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Len Brown4be44fc2005-08-05 00:44:28 -0400112 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400113 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return 0;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static struct notifier_block acpi_ppc_notifier_block = {
119 .notifier_call = acpi_processor_ppc_notifier,
120};
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Len Brown4be44fc2005-08-05 00:44:28 -0400124 acpi_status status = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400125 unsigned long long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /*
132 * _PPC indicates the maximum state currently supported by the platform
133 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
134 */
135 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
136
137 if (status != AE_NOT_FOUND)
138 acpi_processor_ppc_status |= PPC_IN_USE;
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400141 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400142 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Thomas Renninger919158d2007-10-31 15:41:42 +0100145 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
146 (int)ppc, ppc ? "" : "not");
147
Len Brown4be44fc2005-08-05 00:44:28 -0400148 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Len Brown4be44fc2005-08-05 00:44:28 -0400153int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500155 int ret;
156
157 if (ignore_ppc)
158 return 0;
159
160 ret = acpi_processor_get_platform_limit(pr);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (ret < 0)
163 return (ret);
164 else
165 return cpufreq_update_policy(pr->id);
166}
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168void acpi_processor_ppc_init(void)
169{
170 if (!cpufreq_register_notifier
171 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 acpi_processor_ppc_status |= PPC_REGISTERED;
173 else
Len Brown4be44fc2005-08-05 00:44:28 -0400174 printk(KERN_DEBUG
175 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178void acpi_processor_ppc_exit(void)
179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400181 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
182 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 acpi_processor_ppc_status &= ~PPC_REGISTERED;
185}
186
Len Brown4be44fc2005-08-05 00:44:28 -0400187static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Len Brown4be44fc2005-08-05 00:44:28 -0400189 int result = 0;
190 acpi_status status = 0;
191 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
192 union acpi_object *pct = NULL;
193 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400197 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400198 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
Len Brown4be44fc2005-08-05 00:44:28 -0400202 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400204 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400205 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 result = -EFAULT;
207 goto end;
208 }
209
210 /*
211 * control_register
212 */
213
214 obj = pct->package.elements[0];
215
216 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400217 || (obj.buffer.length < sizeof(struct acpi_pct_register))
218 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400219 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 result = -EFAULT;
221 goto end;
222 }
Len Brown4be44fc2005-08-05 00:44:28 -0400223 memcpy(&pr->performance->control_register, obj.buffer.pointer,
224 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /*
227 * status_register
228 */
229
230 obj = pct->package.elements[1];
231
232 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400233 || (obj.buffer.length < sizeof(struct acpi_pct_register))
234 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400235 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 result = -EFAULT;
237 goto end;
238 }
239
Len Brown4be44fc2005-08-05 00:44:28 -0400240 memcpy(&pr->performance->status_register, obj.buffer.pointer,
241 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 end:
Len Brown02438d82006-06-30 03:19:10 -0400244 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Len Brown4be44fc2005-08-05 00:44:28 -0400251 int result = 0;
252 acpi_status status = AE_OK;
253 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
254 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
255 struct acpi_buffer state = { 0, NULL };
256 union acpi_object *pss = NULL;
257 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400261 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400262 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200266 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400268 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 result = -EFAULT;
270 goto end;
271 }
272
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400274 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400277 pr->performance->states =
278 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
279 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!pr->performance->states) {
281 result = -ENOMEM;
282 goto end;
283 }
284
285 for (i = 0; i < pr->performance->state_count; i++) {
286
287 struct acpi_processor_px *px = &(pr->performance->states[i]);
288
289 state.length = sizeof(struct acpi_processor_px);
290 state.pointer = px;
291
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
293
294 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400295 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400297 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 result = -EFAULT;
299 kfree(pr->performance->states);
300 goto end;
301 }
302
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400304 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
305 i,
306 (u32) px->core_frequency,
307 (u32) px->power,
308 (u32) px->transition_latency,
309 (u32) px->bus_master_latency,
310 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (!px->core_frequency) {
Len Brown64684632006-06-26 23:41:38 -0400313 printk(KERN_ERR PREFIX
314 "Invalid _PSS data: freq is zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 result = -EFAULT;
316 kfree(pr->performance->states);
317 goto end;
318 }
319 }
320
Len Brown4be44fc2005-08-05 00:44:28 -0400321 end:
Len Brown02438d82006-06-30 03:19:10 -0400322 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Len Brown4be44fc2005-08-05 00:44:28 -0400329 int result = 0;
330 acpi_status status = AE_OK;
331 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 status = acpi_get_handle(pr->handle, "_PCT", &handle);
337 if (ACPI_FAILURE(status)) {
338 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400339 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400340 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 result = acpi_processor_get_performance_control(pr);
344 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200345 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 result = acpi_processor_get_performance_states(pr);
348 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200349 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return 0;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200352
353 /*
354 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
355 * the BIOS is older than the CPU and does not know its frequencies
356 */
357 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800358#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200359 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
360 if(boot_cpu_has(X86_FEATURE_EST))
361 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
362 "frequency support\n");
363 }
Miao Xie16be87e2008-10-24 17:22:04 +0800364#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200365 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Len Brown4be44fc2005-08-05 00:44:28 -0400368int acpi_processor_notify_smm(struct module *calling_module)
369{
370 acpi_status status;
371 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400375 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* is_done is set to negative if an error occured,
381 * and to postitive if _no_ error occured, but SMM
382 * was already notified. This avoids double notification
383 * which might lead to unexpected results...
384 */
385 if (is_done > 0) {
386 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400387 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400388 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 is_done = -EIO;
394
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300395 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300396 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300397 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Len Brown4be44fc2005-08-05 00:44:28 -0400402 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300403 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300404 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300406 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
407 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400408 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400409 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300410 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300411 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
412 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 /* Success. If there's no _PPC, we need to fear nothing, so
418 * we can allow the cpufreq driver to be rmmod'ed. */
419 is_done = 1;
420
421 if (!(acpi_processor_ppc_status & PPC_IN_USE))
422 module_put(calling_module);
423
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Len Brown4be44fc2005-08-05 00:44:28 -0400427EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500429static int acpi_processor_get_psd(struct acpi_processor *pr)
430{
431 int result = 0;
432 acpi_status status = AE_OK;
433 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
434 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
435 struct acpi_buffer state = {0, NULL};
436 union acpi_object *psd = NULL;
437 struct acpi_psd_package *pdomain;
438
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500439 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
440 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400441 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500442 }
443
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200444 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500445 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800446 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500447 result = -EFAULT;
448 goto end;
449 }
450
451 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800452 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500453 result = -EFAULT;
454 goto end;
455 }
456
457 pdomain = &(pr->performance->domain_info);
458
459 state.length = sizeof(struct acpi_psd_package);
460 state.pointer = pdomain;
461
462 status = acpi_extract_package(&(psd->package.elements[0]),
463 &format, &state);
464 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800465 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500466 result = -EFAULT;
467 goto end;
468 }
469
470 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800471 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500472 result = -EFAULT;
473 goto end;
474 }
475
476 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800477 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500478 result = -EFAULT;
479 goto end;
480 }
481
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100482 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
483 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
484 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
485 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
486 result = -EFAULT;
487 goto end;
488 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500489end:
Len Brown02438d82006-06-30 03:19:10 -0400490 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400491 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500492}
493
494int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400495 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500496{
497 int count, count_target;
498 int retval = 0;
499 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800500 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500501 struct acpi_processor *pr;
502 struct acpi_psd_package *pdomain;
503 struct acpi_processor *match_pr;
504 struct acpi_psd_package *match_pdomain;
505
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800506 if (!alloc_cpumask_var(&covered_cpus, GFP_KERNEL))
507 return -ENOMEM;
508
Len Brown785fccc2006-06-15 22:19:31 -0400509 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500510
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100511 /*
512 * Check if another driver has already registered, and abort before
513 * changing pr->performance if it has. Check input data as well.
514 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400515 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700516 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500517 if (!pr) {
518 /* Look only at processors in ACPI namespace */
519 continue;
520 }
521
522 if (pr->performance) {
523 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100524 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500525 }
526
Rusty Russellb36128c2009-02-20 16:29:08 +0900527 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500528 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100529 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500530 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100531 }
532
533 /* Call _PSD for all CPUs */
534 for_each_possible_cpu(i) {
535 pr = per_cpu(processors, i);
536 if (!pr)
537 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500538
Rusty Russellb36128c2009-02-20 16:29:08 +0900539 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800540 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500541 if (acpi_processor_get_psd(pr)) {
542 retval = -EINVAL;
543 continue;
544 }
545 }
546 if (retval)
547 goto err_ret;
548
549 /*
550 * Now that we have _PSD data from all CPUs, lets setup P-state
551 * domain info.
552 */
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800553 cpumask_clear(covered_cpus);
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400554 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700555 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500556 if (!pr)
557 continue;
558
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800559 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500560 continue;
561
562 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800563 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
564 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500565 if (pdomain->num_processors <= 1)
566 continue;
567
568 /* Validate the Domain info */
569 count_target = pdomain->num_processors;
570 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400571 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500572 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400573 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
574 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
575 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500576 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500577
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400578 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500579 if (i == j)
580 continue;
581
Mike Travis706546d2008-06-09 16:22:23 -0700582 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500583 if (!match_pr)
584 continue;
585
586 match_pdomain = &(match_pr->performance->domain_info);
587 if (match_pdomain->domain != pdomain->domain)
588 continue;
589
590 /* Here i and j are in the same domain */
591
592 if (match_pdomain->num_processors != count_target) {
593 retval = -EINVAL;
594 goto err_ret;
595 }
596
597 if (pdomain->coord_type != match_pdomain->coord_type) {
598 retval = -EINVAL;
599 goto err_ret;
600 }
601
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800602 cpumask_set_cpu(j, covered_cpus);
603 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500604 count++;
605 }
606
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400607 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500608 if (i == j)
609 continue;
610
Mike Travis706546d2008-06-09 16:22:23 -0700611 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500612 if (!match_pr)
613 continue;
614
615 match_pdomain = &(match_pr->performance->domain_info);
616 if (match_pdomain->domain != pdomain->domain)
617 continue;
618
619 match_pr->performance->shared_type =
620 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800621 cpumask_copy(match_pr->performance->shared_cpu_map,
622 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500623 }
624 }
625
626err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400627 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700628 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500629 if (!pr || !pr->performance)
630 continue;
631
632 /* Assume no coordination on any error parsing domain info */
633 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800634 cpumask_clear(pr->performance->shared_cpu_map);
635 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500636 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
637 }
638 pr->performance = NULL; /* Will be set for real in register */
639 }
640
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100641err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400642 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800643 free_cpumask_var(covered_cpus);
Len Brown9011bff42006-05-11 00:28:12 -0400644 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500645}
646EXPORT_SYMBOL(acpi_processor_preregister_performance);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648int
Len Brown4be44fc2005-08-05 00:44:28 -0400649acpi_processor_register_performance(struct acpi_processor_performance
650 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct acpi_processor *pr;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400657 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Mike Travis706546d2008-06-09 16:22:23 -0700659 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400661 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400666 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Andrew Mortona913f502006-06-10 09:54:13 -0700670 WARN_ON(!performance);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 pr->performance = performance;
673
674 if (acpi_processor_get_performance_info(pr)) {
675 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400676 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400677 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400680 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Len Brown4be44fc2005-08-05 00:44:28 -0400683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684EXPORT_SYMBOL(acpi_processor_register_performance);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686void
Len Brown4be44fc2005-08-05 00:44:28 -0400687acpi_processor_unregister_performance(struct acpi_processor_performance
688 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 struct acpi_processor *pr;
691
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400692 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Mike Travis706546d2008-06-09 16:22:23 -0700694 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400696 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Andrew Mortona913f502006-06-10 09:54:13 -0700700 if (pr->performance)
701 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 pr->performance = NULL;
703
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400704 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
Len Brown4be44fc2005-08-05 00:44:28 -0400708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709EXPORT_SYMBOL(acpi_processor_unregister_performance);