blob: fc1f49bbbeef1cc88b804a3b428d8ee69b1220e1 [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
Zhao Yakuid81c45e12009-10-16 09:20:41 +0800155#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
156/*
157 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
158 * @handle: ACPI processor handle
159 * @status: the status code of _PPC evaluation
160 * 0: success. OSPM is now using the performance state specificed.
161 * 1: failure. OSPM has not changed the number of P-states in use
162 */
163static void acpi_processor_ppc_ost(acpi_handle handle, int status)
164{
165 union acpi_object params[2] = {
166 {.type = ACPI_TYPE_INTEGER,},
167 {.type = ACPI_TYPE_INTEGER,},
168 };
169 struct acpi_object_list arg_list = {2, params};
170 acpi_handle temp;
171
172 params[0].integer.value = ACPI_PROCESSOR_NOTIFY_PERFORMANCE;
173 params[1].integer.value = status;
174
175 /* when there is no _OST , skip it */
176 if (ACPI_FAILURE(acpi_get_handle(handle, "_OST", &temp)))
177 return;
178
179 acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
180 return;
181}
182
183int acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500185 int ret;
186
Zhao Yakuid81c45e12009-10-16 09:20:41 +0800187 if (ignore_ppc) {
188 /*
189 * Only when it is notification event, the _OST object
190 * will be evaluated. Otherwise it is skipped.
191 */
192 if (event_flag)
193 acpi_processor_ppc_ost(pr->handle, 1);
Thomas Renninger623b78c2007-05-18 21:59:28 -0500194 return 0;
Zhao Yakuid81c45e12009-10-16 09:20:41 +0800195 }
Thomas Renninger623b78c2007-05-18 21:59:28 -0500196
197 ret = acpi_processor_get_platform_limit(pr);
Zhao Yakuid81c45e12009-10-16 09:20:41 +0800198 /*
199 * Only when it is notification event, the _OST object
200 * will be evaluated. Otherwise it is skipped.
201 */
202 if (event_flag) {
203 if (ret < 0)
204 acpi_processor_ppc_ost(pr->handle, 1);
205 else
206 acpi_processor_ppc_ost(pr->handle, 0);
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (ret < 0)
209 return (ret);
210 else
211 return cpufreq_update_policy(pr->id);
212}
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214void acpi_processor_ppc_init(void)
215{
216 if (!cpufreq_register_notifier
217 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 acpi_processor_ppc_status |= PPC_REGISTERED;
219 else
Len Brown4be44fc2005-08-05 00:44:28 -0400220 printk(KERN_DEBUG
221 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Len Brown4be44fc2005-08-05 00:44:28 -0400224void acpi_processor_ppc_exit(void)
225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400227 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
228 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 acpi_processor_ppc_status &= ~PPC_REGISTERED;
231}
232
Len Brown4be44fc2005-08-05 00:44:28 -0400233static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Len Brown4be44fc2005-08-05 00:44:28 -0400235 int result = 0;
236 acpi_status status = 0;
237 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
238 union acpi_object *pct = NULL;
239 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400243 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400244 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400245 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400250 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400251 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 result = -EFAULT;
253 goto end;
254 }
255
256 /*
257 * control_register
258 */
259
260 obj = pct->package.elements[0];
261
262 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400263 || (obj.buffer.length < sizeof(struct acpi_pct_register))
264 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400265 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 result = -EFAULT;
267 goto end;
268 }
Len Brown4be44fc2005-08-05 00:44:28 -0400269 memcpy(&pr->performance->control_register, obj.buffer.pointer,
270 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /*
273 * status_register
274 */
275
276 obj = pct->package.elements[1];
277
278 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400279 || (obj.buffer.length < sizeof(struct acpi_pct_register))
280 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400281 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 result = -EFAULT;
283 goto end;
284 }
285
Len Brown4be44fc2005-08-05 00:44:28 -0400286 memcpy(&pr->performance->status_register, obj.buffer.pointer,
287 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Len Brown4be44fc2005-08-05 00:44:28 -0400289 end:
Len Brown02438d82006-06-30 03:19:10 -0400290 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Len Brown4be44fc2005-08-05 00:44:28 -0400297 int result = 0;
298 acpi_status status = AE_OK;
299 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
300 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
301 struct acpi_buffer state = { 0, NULL };
302 union acpi_object *pss = NULL;
303 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400307 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400308 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200312 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400314 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 result = -EFAULT;
316 goto end;
317 }
318
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400320 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400323 pr->performance->states =
324 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
325 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!pr->performance->states) {
327 result = -ENOMEM;
328 goto end;
329 }
330
331 for (i = 0; i < pr->performance->state_count; i++) {
332
333 struct acpi_processor_px *px = &(pr->performance->states[i]);
334
335 state.length = sizeof(struct acpi_processor_px);
336 state.pointer = px;
337
338 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
339
340 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400341 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400343 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 result = -EFAULT;
345 kfree(pr->performance->states);
346 goto end;
347 }
348
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400350 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
351 i,
352 (u32) px->core_frequency,
353 (u32) px->power,
354 (u32) px->transition_latency,
355 (u32) px->bus_master_latency,
356 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Len Brown34d531e2009-05-26 15:11:06 -0400358 /*
359 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
360 */
361 if (!px->core_frequency ||
362 ((u32)(px->core_frequency * 1000) !=
363 (px->core_frequency * 1000))) {
364 printk(KERN_ERR FW_BUG PREFIX
365 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
366 px->core_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 result = -EFAULT;
368 kfree(pr->performance->states);
369 goto end;
370 }
371 }
372
Len Brown4be44fc2005-08-05 00:44:28 -0400373 end:
Len Brown02438d82006-06-30 03:19:10 -0400374 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Patrick Mocheld550d982006-06-27 00:41:40 -0400376 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Len Brown4be44fc2005-08-05 00:44:28 -0400379static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Len Brown4be44fc2005-08-05 00:44:28 -0400381 int result = 0;
382 acpi_status status = AE_OK;
383 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 status = acpi_get_handle(pr->handle, "_PCT", &handle);
389 if (ACPI_FAILURE(status)) {
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400391 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400392 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 result = acpi_processor_get_performance_control(pr);
396 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200397 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 result = acpi_processor_get_performance_states(pr);
400 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200401 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Patrick Mocheld550d982006-06-27 00:41:40 -0400403 return 0;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200404
405 /*
406 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
407 * the BIOS is older than the CPU and does not know its frequencies
408 */
409 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800410#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200411 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
412 if(boot_cpu_has(X86_FEATURE_EST))
413 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
414 "frequency support\n");
415 }
Miao Xie16be87e2008-10-24 17:22:04 +0800416#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200417 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Len Brown4be44fc2005-08-05 00:44:28 -0400420int acpi_processor_notify_smm(struct module *calling_module)
421{
422 acpi_status status;
423 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* is_done is set to negative if an error occured,
433 * and to postitive if _no_ error occured, but SMM
434 * was already notified. This avoids double notification
435 * which might lead to unexpected results...
436 */
437 if (is_done > 0) {
438 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400439 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400440 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 is_done = -EIO;
446
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300447 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300448 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300449 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300455 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300456 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300458 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
459 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400460 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400461 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300462 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300463 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
464 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 /* Success. If there's no _PPC, we need to fear nothing, so
470 * we can allow the cpufreq driver to be rmmod'ed. */
471 is_done = 1;
472
473 if (!(acpi_processor_ppc_status & PPC_IN_USE))
474 module_put(calling_module);
475
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Len Brown4be44fc2005-08-05 00:44:28 -0400479EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500481static int acpi_processor_get_psd(struct acpi_processor *pr)
482{
483 int result = 0;
484 acpi_status status = AE_OK;
485 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
486 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
487 struct acpi_buffer state = {0, NULL};
488 union acpi_object *psd = NULL;
489 struct acpi_psd_package *pdomain;
490
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500491 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
492 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400493 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500494 }
495
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200496 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500497 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800498 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500499 result = -EFAULT;
500 goto end;
501 }
502
503 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800504 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500505 result = -EFAULT;
506 goto end;
507 }
508
509 pdomain = &(pr->performance->domain_info);
510
511 state.length = sizeof(struct acpi_psd_package);
512 state.pointer = pdomain;
513
514 status = acpi_extract_package(&(psd->package.elements[0]),
515 &format, &state);
516 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800517 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500518 result = -EFAULT;
519 goto end;
520 }
521
522 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800523 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500524 result = -EFAULT;
525 goto end;
526 }
527
528 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800529 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500530 result = -EFAULT;
531 goto end;
532 }
533
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100534 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
535 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
536 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
537 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
538 result = -EFAULT;
539 goto end;
540 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500541end:
Len Brown02438d82006-06-30 03:19:10 -0400542 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400543 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500544}
545
546int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400547 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500548{
549 int count, count_target;
550 int retval = 0;
551 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800552 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500553 struct acpi_processor *pr;
554 struct acpi_psd_package *pdomain;
555 struct acpi_processor *match_pr;
556 struct acpi_psd_package *match_pdomain;
557
Li Zefan79f55992009-06-15 14:58:26 +0800558 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800559 return -ENOMEM;
560
Len Brown785fccc2006-06-15 22:19:31 -0400561 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500562
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100563 /*
564 * Check if another driver has already registered, and abort before
565 * changing pr->performance if it has. Check input data as well.
566 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400567 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700568 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500569 if (!pr) {
570 /* Look only at processors in ACPI namespace */
571 continue;
572 }
573
574 if (pr->performance) {
575 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100576 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500577 }
578
Rusty Russellb36128c2009-02-20 16:29:08 +0900579 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500580 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100581 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500582 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100583 }
584
585 /* Call _PSD for all CPUs */
586 for_each_possible_cpu(i) {
587 pr = per_cpu(processors, i);
588 if (!pr)
589 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500590
Rusty Russellb36128c2009-02-20 16:29:08 +0900591 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800592 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500593 if (acpi_processor_get_psd(pr)) {
594 retval = -EINVAL;
595 continue;
596 }
597 }
598 if (retval)
599 goto err_ret;
600
601 /*
602 * Now that we have _PSD data from all CPUs, lets setup P-state
603 * domain info.
604 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400605 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700606 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500607 if (!pr)
608 continue;
609
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800610 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500611 continue;
612
613 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800614 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
615 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500616 if (pdomain->num_processors <= 1)
617 continue;
618
619 /* Validate the Domain info */
620 count_target = pdomain->num_processors;
621 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400622 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500623 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400624 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
625 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
626 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500627 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500628
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400629 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500630 if (i == j)
631 continue;
632
Mike Travis706546d2008-06-09 16:22:23 -0700633 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500634 if (!match_pr)
635 continue;
636
637 match_pdomain = &(match_pr->performance->domain_info);
638 if (match_pdomain->domain != pdomain->domain)
639 continue;
640
641 /* Here i and j are in the same domain */
642
643 if (match_pdomain->num_processors != count_target) {
644 retval = -EINVAL;
645 goto err_ret;
646 }
647
648 if (pdomain->coord_type != match_pdomain->coord_type) {
649 retval = -EINVAL;
650 goto err_ret;
651 }
652
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800653 cpumask_set_cpu(j, covered_cpus);
654 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500655 count++;
656 }
657
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400658 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500659 if (i == j)
660 continue;
661
Mike Travis706546d2008-06-09 16:22:23 -0700662 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500663 if (!match_pr)
664 continue;
665
666 match_pdomain = &(match_pr->performance->domain_info);
667 if (match_pdomain->domain != pdomain->domain)
668 continue;
669
670 match_pr->performance->shared_type =
671 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800672 cpumask_copy(match_pr->performance->shared_cpu_map,
673 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500674 }
675 }
676
677err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400678 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700679 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500680 if (!pr || !pr->performance)
681 continue;
682
683 /* Assume no coordination on any error parsing domain info */
684 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800685 cpumask_clear(pr->performance->shared_cpu_map);
686 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500687 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
688 }
689 pr->performance = NULL; /* Will be set for real in register */
690 }
691
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100692err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400693 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800694 free_cpumask_var(covered_cpus);
Len Brown9011bff42006-05-11 00:28:12 -0400695 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500696}
697EXPORT_SYMBOL(acpi_processor_preregister_performance);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699int
Len Brown4be44fc2005-08-05 00:44:28 -0400700acpi_processor_register_performance(struct acpi_processor_performance
701 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct acpi_processor *pr;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400708 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Mike Travis706546d2008-06-09 16:22:23 -0700710 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400712 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400713 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400717 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400718 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Andrew Mortona913f502006-06-10 09:54:13 -0700721 WARN_ON(!performance);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 pr->performance = performance;
724
725 if (acpi_processor_get_performance_info(pr)) {
726 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400727 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400731 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Len Brown4be44fc2005-08-05 00:44:28 -0400734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735EXPORT_SYMBOL(acpi_processor_register_performance);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737void
Len Brown4be44fc2005-08-05 00:44:28 -0400738acpi_processor_unregister_performance(struct acpi_processor_performance
739 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 struct acpi_processor *pr;
742
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400743 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Mike Travis706546d2008-06-09 16:22:23 -0700745 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400747 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400748 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Andrew Mortona913f502006-06-10 09:54:13 -0700751 if (pr->performance)
752 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 pr->performance = NULL;
754
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400755 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Len Brown4be44fc2005-08-05 00:44:28 -0400759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760EXPORT_SYMBOL(acpi_processor_unregister_performance);