blob: a959f6a075083153580902746aabb8f178d425f4 [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
Thomas Renningere2f74f32009-11-19 12:31:01 +0100214int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
215{
216 struct acpi_processor *pr;
217
218 pr = per_cpu(processors, cpu);
219 if (!pr || !pr->performance || !pr->performance->state_count)
220 return -ENODEV;
221 *limit = pr->performance->states[pr->performance_platform_limit].
222 core_frequency * 1000;
223 return 0;
224}
225EXPORT_SYMBOL(acpi_processor_get_bios_limit);
226
Len Brown4be44fc2005-08-05 00:44:28 -0400227void acpi_processor_ppc_init(void)
228{
229 if (!cpufreq_register_notifier
230 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 acpi_processor_ppc_status |= PPC_REGISTERED;
232 else
Len Brown4be44fc2005-08-05 00:44:28 -0400233 printk(KERN_DEBUG
234 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Len Brown4be44fc2005-08-05 00:44:28 -0400237void acpi_processor_ppc_exit(void)
238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400240 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
241 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 acpi_processor_ppc_status &= ~PPC_REGISTERED;
244}
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 int result = 0;
249 acpi_status status = 0;
250 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
251 union acpi_object *pct = NULL;
252 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400256 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400257 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400263 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400264 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 result = -EFAULT;
266 goto end;
267 }
268
269 /*
270 * control_register
271 */
272
273 obj = pct->package.elements[0];
274
275 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400276 || (obj.buffer.length < sizeof(struct acpi_pct_register))
277 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400278 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 result = -EFAULT;
280 goto end;
281 }
Len Brown4be44fc2005-08-05 00:44:28 -0400282 memcpy(&pr->performance->control_register, obj.buffer.pointer,
283 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * status_register
287 */
288
289 obj = pct->package.elements[1];
290
291 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400292 || (obj.buffer.length < sizeof(struct acpi_pct_register))
293 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400294 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 result = -EFAULT;
296 goto end;
297 }
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 memcpy(&pr->performance->status_register, obj.buffer.pointer,
300 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 end:
Len Brown02438d82006-06-30 03:19:10 -0400303 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Patrick Mocheld550d982006-06-27 00:41:40 -0400305 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Len Brown4be44fc2005-08-05 00:44:28 -0400310 int result = 0;
311 acpi_status status = AE_OK;
312 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
313 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
314 struct acpi_buffer state = { 0, NULL };
315 union acpi_object *pss = NULL;
316 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400320 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400321 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200325 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400327 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 result = -EFAULT;
329 goto end;
330 }
331
332 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400333 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400336 pr->performance->states =
337 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
338 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!pr->performance->states) {
340 result = -ENOMEM;
341 goto end;
342 }
343
344 for (i = 0; i < pr->performance->state_count; i++) {
345
346 struct acpi_processor_px *px = &(pr->performance->states[i]);
347
348 state.length = sizeof(struct acpi_processor_px);
349 state.pointer = px;
350
351 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
352
353 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400354 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400356 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 result = -EFAULT;
358 kfree(pr->performance->states);
359 goto end;
360 }
361
362 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400363 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
364 i,
365 (u32) px->core_frequency,
366 (u32) px->power,
367 (u32) px->transition_latency,
368 (u32) px->bus_master_latency,
369 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Len Brown34d531e2009-05-26 15:11:06 -0400371 /*
372 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
373 */
374 if (!px->core_frequency ||
375 ((u32)(px->core_frequency * 1000) !=
376 (px->core_frequency * 1000))) {
377 printk(KERN_ERR FW_BUG PREFIX
378 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
379 px->core_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 result = -EFAULT;
381 kfree(pr->performance->states);
382 goto end;
383 }
384 }
385
Len Brown4be44fc2005-08-05 00:44:28 -0400386 end:
Len Brown02438d82006-06-30 03:19:10 -0400387 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Len Brown4be44fc2005-08-05 00:44:28 -0400392static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Len Brown4be44fc2005-08-05 00:44:28 -0400394 int result = 0;
395 acpi_status status = AE_OK;
396 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 status = acpi_get_handle(pr->handle, "_PCT", &handle);
402 if (ACPI_FAILURE(status)) {
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400404 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400405 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 result = acpi_processor_get_performance_control(pr);
409 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200410 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 result = acpi_processor_get_performance_states(pr);
413 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200414 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Darrick J. Wong455c0d72010-02-18 10:28:20 -0800416 /* We need to call _PPC once when cpufreq starts */
417 if (ignore_ppc != 1)
418 result = acpi_processor_get_platform_limit(pr);
419
420 return result;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200421
422 /*
423 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
424 * the BIOS is older than the CPU and does not know its frequencies
425 */
426 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800427#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200428 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
429 if(boot_cpu_has(X86_FEATURE_EST))
430 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
431 "frequency support\n");
432 }
Miao Xie16be87e2008-10-24 17:22:04 +0800433#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200434 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Len Brown4be44fc2005-08-05 00:44:28 -0400437int acpi_processor_notify_smm(struct module *calling_module)
438{
439 acpi_status status;
440 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400444 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400447 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* is_done is set to negative if an error occured,
450 * and to postitive if _no_ error occured, but SMM
451 * was already notified. This avoids double notification
452 * which might lead to unexpected results...
453 */
454 if (is_done > 0) {
455 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400456 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400457 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400459 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 is_done = -EIO;
463
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300464 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300465 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300466 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Len Brown4be44fc2005-08-05 00:44:28 -0400471 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300472 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300473 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300475 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
476 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400477 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400478 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300479 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300480 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
481 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
486 /* Success. If there's no _PPC, we need to fear nothing, so
487 * we can allow the cpufreq driver to be rmmod'ed. */
488 is_done = 1;
489
490 if (!(acpi_processor_ppc_status & PPC_IN_USE))
491 module_put(calling_module);
492
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Len Brown4be44fc2005-08-05 00:44:28 -0400496EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500498static int acpi_processor_get_psd(struct acpi_processor *pr)
499{
500 int result = 0;
501 acpi_status status = AE_OK;
502 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
503 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
504 struct acpi_buffer state = {0, NULL};
505 union acpi_object *psd = NULL;
506 struct acpi_psd_package *pdomain;
507
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500508 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
509 if (ACPI_FAILURE(status)) {
Len Brown9011bff42006-05-11 00:28:12 -0400510 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500511 }
512
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200513 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500514 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800515 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500516 result = -EFAULT;
517 goto end;
518 }
519
520 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800521 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500522 result = -EFAULT;
523 goto end;
524 }
525
526 pdomain = &(pr->performance->domain_info);
527
528 state.length = sizeof(struct acpi_psd_package);
529 state.pointer = pdomain;
530
531 status = acpi_extract_package(&(psd->package.elements[0]),
532 &format, &state);
533 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800534 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500535 result = -EFAULT;
536 goto end;
537 }
538
539 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800540 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500541 result = -EFAULT;
542 goto end;
543 }
544
545 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800546 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500547 result = -EFAULT;
548 goto end;
549 }
550
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100551 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
552 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
553 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
554 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
555 result = -EFAULT;
556 goto end;
557 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500558end:
Len Brown02438d82006-06-30 03:19:10 -0400559 kfree(buffer.pointer);
Len Brown9011bff42006-05-11 00:28:12 -0400560 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500561}
562
563int acpi_processor_preregister_performance(
Fenghua Yu50109292007-08-07 18:40:30 -0400564 struct acpi_processor_performance *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500565{
566 int count, count_target;
567 int retval = 0;
568 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800569 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500570 struct acpi_processor *pr;
571 struct acpi_psd_package *pdomain;
572 struct acpi_processor *match_pr;
573 struct acpi_psd_package *match_pdomain;
574
Li Zefan79f55992009-06-15 14:58:26 +0800575 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800576 return -ENOMEM;
577
Len Brown785fccc2006-06-15 22:19:31 -0400578 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500579
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100580 /*
581 * Check if another driver has already registered, and abort before
582 * changing pr->performance if it has. Check input data as well.
583 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400584 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700585 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500586 if (!pr) {
587 /* Look only at processors in ACPI namespace */
588 continue;
589 }
590
591 if (pr->performance) {
592 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100593 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500594 }
595
Rusty Russellb36128c2009-02-20 16:29:08 +0900596 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500597 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100598 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500599 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100600 }
601
602 /* Call _PSD for all CPUs */
603 for_each_possible_cpu(i) {
604 pr = per_cpu(processors, i);
605 if (!pr)
606 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500607
Rusty Russellb36128c2009-02-20 16:29:08 +0900608 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800609 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500610 if (acpi_processor_get_psd(pr)) {
611 retval = -EINVAL;
612 continue;
613 }
614 }
615 if (retval)
616 goto err_ret;
617
618 /*
619 * Now that we have _PSD data from all CPUs, lets setup P-state
620 * domain info.
621 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400622 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700623 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500624 if (!pr)
625 continue;
626
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800627 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500628 continue;
629
630 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800631 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
632 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500633 if (pdomain->num_processors <= 1)
634 continue;
635
636 /* Validate the Domain info */
637 count_target = pdomain->num_processors;
638 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400639 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500640 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400641 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
642 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
643 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500644 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500645
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400646 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500647 if (i == j)
648 continue;
649
Mike Travis706546d2008-06-09 16:22:23 -0700650 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500651 if (!match_pr)
652 continue;
653
654 match_pdomain = &(match_pr->performance->domain_info);
655 if (match_pdomain->domain != pdomain->domain)
656 continue;
657
658 /* Here i and j are in the same domain */
659
660 if (match_pdomain->num_processors != count_target) {
661 retval = -EINVAL;
662 goto err_ret;
663 }
664
665 if (pdomain->coord_type != match_pdomain->coord_type) {
666 retval = -EINVAL;
667 goto err_ret;
668 }
669
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800670 cpumask_set_cpu(j, covered_cpus);
671 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500672 count++;
673 }
674
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400675 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500676 if (i == j)
677 continue;
678
Mike Travis706546d2008-06-09 16:22:23 -0700679 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500680 if (!match_pr)
681 continue;
682
683 match_pdomain = &(match_pr->performance->domain_info);
684 if (match_pdomain->domain != pdomain->domain)
685 continue;
686
687 match_pr->performance->shared_type =
688 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800689 cpumask_copy(match_pr->performance->shared_cpu_map,
690 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500691 }
692 }
693
694err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400695 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700696 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500697 if (!pr || !pr->performance)
698 continue;
699
700 /* Assume no coordination on any error parsing domain info */
701 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800702 cpumask_clear(pr->performance->shared_cpu_map);
703 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500704 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
705 }
706 pr->performance = NULL; /* Will be set for real in register */
707 }
708
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100709err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400710 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800711 free_cpumask_var(covered_cpus);
Len Brown9011bff42006-05-11 00:28:12 -0400712 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500713}
714EXPORT_SYMBOL(acpi_processor_preregister_performance);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716int
Len Brown4be44fc2005-08-05 00:44:28 -0400717acpi_processor_register_performance(struct acpi_processor_performance
718 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct acpi_processor *pr;
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400725 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Mike Travis706546d2008-06-09 16:22:23 -0700727 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400729 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400734 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400735 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Andrew Mortona913f502006-06-10 09:54:13 -0700738 WARN_ON(!performance);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 pr->performance = performance;
741
742 if (acpi_processor_get_performance_info(pr)) {
743 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400744 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400748 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Len Brown4be44fc2005-08-05 00:44:28 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752EXPORT_SYMBOL(acpi_processor_register_performance);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754void
Len Brown4be44fc2005-08-05 00:44:28 -0400755acpi_processor_unregister_performance(struct acpi_processor_performance
756 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct acpi_processor *pr;
759
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400760 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Mike Travis706546d2008-06-09 16:22:23 -0700762 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400764 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400765 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
Andrew Mortona913f502006-06-10 09:54:13 -0700768 if (pr->performance)
769 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pr->performance = NULL;
771
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400772 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Patrick Mocheld550d982006-06-27 00:41:40 -0400774 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
Len Brown4be44fc2005-08-05 00:44:28 -0400776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777EXPORT_SYMBOL(acpi_processor_unregister_performance);