blob: 8ba0ed0b9ddbc912684d398e2eda14357920aed4 [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
Len Brown4be44fc2005-08-05 00:44:28 -0400170void acpi_processor_ppc_init(void)
171{
172 if (!cpufreq_register_notifier
173 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 acpi_processor_ppc_status |= PPC_REGISTERED;
175 else
Len Brown4be44fc2005-08-05 00:44:28 -0400176 printk(KERN_DEBUG
177 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Len Brown4be44fc2005-08-05 00:44:28 -0400180void acpi_processor_ppc_exit(void)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400183 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
184 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 acpi_processor_ppc_status &= ~PPC_REGISTERED;
187}
188
Len Brown4be44fc2005-08-05 00:44:28 -0400189static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Len Brown4be44fc2005-08-05 00:44:28 -0400191 int result = 0;
192 acpi_status status = 0;
193 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
194 union acpi_object *pct = NULL;
195 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400199 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400200 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400206 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400207 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 result = -EFAULT;
209 goto end;
210 }
211
212 /*
213 * control_register
214 */
215
216 obj = pct->package.elements[0];
217
218 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400219 || (obj.buffer.length < sizeof(struct acpi_pct_register))
220 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400221 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 result = -EFAULT;
223 goto end;
224 }
Len Brown4be44fc2005-08-05 00:44:28 -0400225 memcpy(&pr->performance->control_register, obj.buffer.pointer,
226 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
229 * status_register
230 */
231
232 obj = pct->package.elements[1];
233
234 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400235 || (obj.buffer.length < sizeof(struct acpi_pct_register))
236 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400237 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 result = -EFAULT;
239 goto end;
240 }
241
Len Brown4be44fc2005-08-05 00:44:28 -0400242 memcpy(&pr->performance->status_register, obj.buffer.pointer,
243 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Len Brown4be44fc2005-08-05 00:44:28 -0400245 end:
Len Brown02438d82006-06-30 03:19:10 -0400246 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Patrick Mocheld550d982006-06-27 00:41:40 -0400248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Len Brown4be44fc2005-08-05 00:44:28 -0400251static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Len Brown4be44fc2005-08-05 00:44:28 -0400253 int result = 0;
254 acpi_status status = AE_OK;
255 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
256 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
257 struct acpi_buffer state = { 0, NULL };
258 union acpi_object *pss = NULL;
259 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400263 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400264 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200268 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400270 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 result = -EFAULT;
272 goto end;
273 }
274
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400276 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400279 pr->performance->states =
280 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
281 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!pr->performance->states) {
283 result = -ENOMEM;
284 goto end;
285 }
286
287 for (i = 0; i < pr->performance->state_count; i++) {
288
289 struct acpi_processor_px *px = &(pr->performance->states[i]);
290
291 state.length = sizeof(struct acpi_processor_px);
292 state.pointer = px;
293
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
295
296 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400297 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400299 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 result = -EFAULT;
301 kfree(pr->performance->states);
302 goto end;
303 }
304
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400306 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
307 i,
308 (u32) px->core_frequency,
309 (u32) px->power,
310 (u32) px->transition_latency,
311 (u32) px->bus_master_latency,
312 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Len Brown34d531e2009-05-26 15:11:06 -0400314 /*
315 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
316 */
317 if (!px->core_frequency ||
318 ((u32)(px->core_frequency * 1000) !=
319 (px->core_frequency * 1000))) {
320 printk(KERN_ERR FW_BUG PREFIX
321 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
322 px->core_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 result = -EFAULT;
324 kfree(pr->performance->states);
325 goto end;
326 }
327 }
328
Len Brown4be44fc2005-08-05 00:44:28 -0400329 end:
Len Brown02438d82006-06-30 03:19:10 -0400330 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Patrick Mocheld550d982006-06-27 00:41:40 -0400332 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Len Brown4be44fc2005-08-05 00:44:28 -0400335static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Len Brown4be44fc2005-08-05 00:44:28 -0400337 int result = 0;
338 acpi_status status = AE_OK;
339 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 status = acpi_get_handle(pr->handle, "_PCT", &handle);
345 if (ACPI_FAILURE(status)) {
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400347 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400348 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 result = acpi_processor_get_performance_control(pr);
352 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200353 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 result = acpi_processor_get_performance_states(pr);
356 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200357 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Patrick Mocheld550d982006-06-27 00:41:40 -0400359 return 0;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200360
361 /*
362 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
363 * the BIOS is older than the CPU and does not know its frequencies
364 */
365 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800366#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200367 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
368 if(boot_cpu_has(X86_FEATURE_EST))
369 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
370 "frequency support\n");
371 }
Miao Xie16be87e2008-10-24 17:22:04 +0800372#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200373 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Len Brown4be44fc2005-08-05 00:44:28 -0400376int acpi_processor_notify_smm(struct module *calling_module)
377{
378 acpi_status status;
379 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* is_done is set to negative if an error occured,
389 * and to postitive if _no_ error occured, but SMM
390 * was already notified. This avoids double notification
391 * which might lead to unexpected results...
392 */
393 if (is_done > 0) {
394 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400395 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400396 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400398 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 is_done = -EIO;
402
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300403 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300404 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300405 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300411 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300412 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300414 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
415 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400416 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400417 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300418 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300419 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
420 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400422 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 /* Success. If there's no _PPC, we need to fear nothing, so
426 * we can allow the cpufreq driver to be rmmod'ed. */
427 is_done = 1;
428
429 if (!(acpi_processor_ppc_status & PPC_IN_USE))
430 module_put(calling_module);
431
Patrick Mocheld550d982006-06-27 00:41:40 -0400432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Len Brown4be44fc2005-08-05 00:44:28 -0400435EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500437static int acpi_processor_get_psd(struct acpi_processor *pr)
438{
439 int result = 0;
440 acpi_status status = AE_OK;
441 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
442 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
443 struct acpi_buffer state = {0, NULL};
444 union acpi_object *psd = NULL;
445 struct acpi_psd_package *pdomain;
446
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500447 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
448 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400449 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500450 }
451
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200452 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500453 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800454 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500455 result = -EFAULT;
456 goto end;
457 }
458
459 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800460 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500461 result = -EFAULT;
462 goto end;
463 }
464
465 pdomain = &(pr->performance->domain_info);
466
467 state.length = sizeof(struct acpi_psd_package);
468 state.pointer = pdomain;
469
470 status = acpi_extract_package(&(psd->package.elements[0]),
471 &format, &state);
472 if (ACPI_FAILURE(status)) {
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 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800479 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500480 result = -EFAULT;
481 goto end;
482 }
483
484 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800485 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500486 result = -EFAULT;
487 goto end;
488 }
489
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100490 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
491 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
492 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
493 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
494 result = -EFAULT;
495 goto end;
496 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500497end:
Len Brown02438d82006-06-30 03:19:10 -0400498 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400499 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500500}
501
502int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400503 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500504{
505 int count, count_target;
506 int retval = 0;
507 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800508 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500509 struct acpi_processor *pr;
510 struct acpi_psd_package *pdomain;
511 struct acpi_processor *match_pr;
512 struct acpi_psd_package *match_pdomain;
513
Li Zefan79f55992009-06-15 14:58:26 +0800514 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800515 return -ENOMEM;
516
Len Brown785fccc2006-06-15 22:19:31 -0400517 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500518
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100519 /*
520 * Check if another driver has already registered, and abort before
521 * changing pr->performance if it has. Check input data as well.
522 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400523 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700524 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500525 if (!pr) {
526 /* Look only at processors in ACPI namespace */
527 continue;
528 }
529
530 if (pr->performance) {
531 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100532 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500533 }
534
Rusty Russellb36128c2009-02-20 16:29:08 +0900535 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500536 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100537 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500538 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100539 }
540
541 /* Call _PSD for all CPUs */
542 for_each_possible_cpu(i) {
543 pr = per_cpu(processors, i);
544 if (!pr)
545 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500546
Rusty Russellb36128c2009-02-20 16:29:08 +0900547 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800548 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500549 if (acpi_processor_get_psd(pr)) {
550 retval = -EINVAL;
551 continue;
552 }
553 }
554 if (retval)
555 goto err_ret;
556
557 /*
558 * Now that we have _PSD data from all CPUs, lets setup P-state
559 * domain info.
560 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400561 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700562 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500563 if (!pr)
564 continue;
565
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800566 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500567 continue;
568
569 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800570 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
571 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500572 if (pdomain->num_processors <= 1)
573 continue;
574
575 /* Validate the Domain info */
576 count_target = pdomain->num_processors;
577 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400578 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500579 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400580 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
581 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
582 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500583 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500584
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400585 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500586 if (i == j)
587 continue;
588
Mike Travis706546d2008-06-09 16:22:23 -0700589 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500590 if (!match_pr)
591 continue;
592
593 match_pdomain = &(match_pr->performance->domain_info);
594 if (match_pdomain->domain != pdomain->domain)
595 continue;
596
597 /* Here i and j are in the same domain */
598
599 if (match_pdomain->num_processors != count_target) {
600 retval = -EINVAL;
601 goto err_ret;
602 }
603
604 if (pdomain->coord_type != match_pdomain->coord_type) {
605 retval = -EINVAL;
606 goto err_ret;
607 }
608
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800609 cpumask_set_cpu(j, covered_cpus);
610 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500611 count++;
612 }
613
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400614 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500615 if (i == j)
616 continue;
617
Mike Travis706546d2008-06-09 16:22:23 -0700618 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500619 if (!match_pr)
620 continue;
621
622 match_pdomain = &(match_pr->performance->domain_info);
623 if (match_pdomain->domain != pdomain->domain)
624 continue;
625
626 match_pr->performance->shared_type =
627 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800628 cpumask_copy(match_pr->performance->shared_cpu_map,
629 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500630 }
631 }
632
633err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400634 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700635 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500636 if (!pr || !pr->performance)
637 continue;
638
639 /* Assume no coordination on any error parsing domain info */
640 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800641 cpumask_clear(pr->performance->shared_cpu_map);
642 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500643 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
644 }
645 pr->performance = NULL; /* Will be set for real in register */
646 }
647
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100648err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400649 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800650 free_cpumask_var(covered_cpus);
Len Brown9011bff42006-05-11 00:28:12 -0400651 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500652}
653EXPORT_SYMBOL(acpi_processor_preregister_performance);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655int
Len Brown4be44fc2005-08-05 00:44:28 -0400656acpi_processor_register_performance(struct acpi_processor_performance
657 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct acpi_processor *pr;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400664 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Mike Travis706546d2008-06-09 16:22:23 -0700666 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400668 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400673 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Andrew Mortona913f502006-06-10 09:54:13 -0700677 WARN_ON(!performance);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pr->performance = performance;
680
681 if (acpi_processor_get_performance_info(pr)) {
682 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400683 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400687 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Len Brown4be44fc2005-08-05 00:44:28 -0400690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691EXPORT_SYMBOL(acpi_processor_register_performance);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693void
Len Brown4be44fc2005-08-05 00:44:28 -0400694acpi_processor_unregister_performance(struct acpi_processor_performance
695 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct acpi_processor *pr;
698
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400699 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Mike Travis706546d2008-06-09 16:22:23 -0700701 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400703 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Andrew Mortona913f502006-06-10 09:54:13 -0700707 if (pr->performance)
708 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 pr->performance = NULL;
710
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400711 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick Mocheld550d982006-06-27 00:41:40 -0400713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Len Brown4be44fc2005-08-05 00:44:28 -0400715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716EXPORT_SYMBOL(acpi_processor_unregister_performance);