blob: 3a73a93596e88a29c1e66fabec91dd2d0db96f6c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Miao Xie16be87e2008-10-24 17:22:04 +080035#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +020036#include <asm/cpufeature.h>
Miao Xie16be87e2008-10-24 17:22:04 +080037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <acpi/acpi_bus.h>
Bjorn Helgaas89595b82008-11-07 16:57:45 -070040#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <acpi/processor.h>
42
Len Browna192a952009-07-28 16:45:54 -040043#define PREFIX "ACPI: "
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
47#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050048ACPI_MODULE_NAME("processor_perflib");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Arjan van de Ven65c19bb2006-04-27 05:25:00 -040050static DEFINE_MUTEX(performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Renninger919158d2007-10-31 15:41:42 +010052/* Use cpufreq debug layer for _PPC changes. */
53#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
54 "cpufreq-core", msg)
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * _PPC support is implemented as a CPUfreq policy notifier:
58 * This means each time a CPUfreq driver registered also with
59 * the ACPI core is asked to change the speed policy, the maximum
60 * value is adjusted so that it is within the platform limit.
61 *
62 * Also, when a new platform limit value is detected, the CPUfreq
63 * policy is adjusted accordingly.
64 */
65
Thomas Renningera1531ac2008-07-29 22:32:58 -070066/* ignore_ppc:
67 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
68 * ignore _PPC
69 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
70 * 1 -> ignore _PPC totally -> forced by user through boot param
71 */
Milan Broz9f497bc2008-08-12 17:48:27 +020072static int ignore_ppc = -1;
Milan Broz613e5f32008-08-16 02:11:28 +020073module_param(ignore_ppc, int, 0644);
Thomas Renninger623b78c2007-05-18 21:59:28 -050074MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
75 "limited by BIOS, this should help");
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define PPC_REGISTERED 1
78#define PPC_IN_USE 2
79
Thomas Renningera1531ac2008-07-29 22:32:58 -070080static int acpi_processor_ppc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static int acpi_processor_ppc_notifier(struct notifier_block *nb,
Len Brown4be44fc2005-08-05 00:44:28 -040083 unsigned long event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct cpufreq_policy *policy = data;
86 struct acpi_processor *pr;
87 unsigned int ppc = 0;
88
Thomas Renningera1531ac2008-07-29 22:32:58 -070089 if (event == CPUFREQ_START && ignore_ppc <= 0) {
90 ignore_ppc = 0;
91 return 0;
92 }
93
Thomas Renninger623b78c2007-05-18 21:59:28 -050094 if (ignore_ppc)
95 return 0;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (event != CPUFREQ_INCOMPATIBLE)
Thomas Renninger9b67c5d2008-07-29 22:32:59 -070098 return 0;
99
100 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mike Travis706546d2008-06-09 16:22:23 -0700102 pr = per_cpu(processors, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!pr || !pr->performance)
104 goto out;
105
Len Brown4be44fc2005-08-05 00:44:28 -0400106 ppc = (unsigned int)pr->performance_platform_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dave Jones0916bd32006-11-22 20:42:01 -0500108 if (ppc >= pr->performance->state_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 goto out;
110
111 cpufreq_verify_within_limits(policy, 0,
Len Brown4be44fc2005-08-05 00:44:28 -0400112 pr->performance->states[ppc].
113 core_frequency * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Len Brown4be44fc2005-08-05 00:44:28 -0400115 out:
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400116 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return 0;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct notifier_block acpi_ppc_notifier_block = {
122 .notifier_call = acpi_processor_ppc_notifier,
123};
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Len Brown4be44fc2005-08-05 00:44:28 -0400127 acpi_status status = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400128 unsigned long long ppc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /*
135 * _PPC indicates the maximum state currently supported by the platform
136 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
137 */
138 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
139
140 if (status != AE_NOT_FOUND)
141 acpi_processor_ppc_status |= PPC_IN_USE;
142
Len Brown4be44fc2005-08-05 00:44:28 -0400143 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400144 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400145 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Thomas Renninger919158d2007-10-31 15:41:42 +0100148 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
149 (int)ppc, ppc ? "" : "not");
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151 pr->performance_platform_limit = (int)ppc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Patrick Mocheld550d982006-06-27 00:41:40 -0400153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Zhao Yakuid81c45e2009-10-16 09:20:41 +0800156#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
157/*
158 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
159 * @handle: ACPI processor handle
160 * @status: the status code of _PPC evaluation
161 * 0: success. OSPM is now using the performance state specificed.
162 * 1: failure. OSPM has not changed the number of P-states in use
163 */
164static void acpi_processor_ppc_ost(acpi_handle handle, int status)
165{
166 union acpi_object params[2] = {
167 {.type = ACPI_TYPE_INTEGER,},
168 {.type = ACPI_TYPE_INTEGER,},
169 };
170 struct acpi_object_list arg_list = {2, params};
171 acpi_handle temp;
172
173 params[0].integer.value = ACPI_PROCESSOR_NOTIFY_PERFORMANCE;
174 params[1].integer.value = status;
175
176 /* when there is no _OST , skip it */
177 if (ACPI_FAILURE(acpi_get_handle(handle, "_OST", &temp)))
178 return;
179
180 acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
181 return;
182}
183
184int acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Thomas Renninger623b78c2007-05-18 21:59:28 -0500186 int ret;
187
Zhao Yakuid81c45e2009-10-16 09:20:41 +0800188 if (ignore_ppc) {
189 /*
190 * Only when it is notification event, the _OST object
191 * will be evaluated. Otherwise it is skipped.
192 */
193 if (event_flag)
194 acpi_processor_ppc_ost(pr->handle, 1);
Thomas Renninger623b78c2007-05-18 21:59:28 -0500195 return 0;
Zhao Yakuid81c45e2009-10-16 09:20:41 +0800196 }
Thomas Renninger623b78c2007-05-18 21:59:28 -0500197
198 ret = acpi_processor_get_platform_limit(pr);
Zhao Yakuid81c45e2009-10-16 09:20:41 +0800199 /*
200 * Only when it is notification event, the _OST object
201 * will be evaluated. Otherwise it is skipped.
202 */
203 if (event_flag) {
204 if (ret < 0)
205 acpi_processor_ppc_ost(pr->handle, 1);
206 else
207 acpi_processor_ppc_ost(pr->handle, 0);
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (ret < 0)
210 return (ret);
211 else
212 return cpufreq_update_policy(pr->id);
213}
214
Thomas Renningere2f74f32009-11-19 12:31:01 +0100215int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
216{
217 struct acpi_processor *pr;
218
219 pr = per_cpu(processors, cpu);
220 if (!pr || !pr->performance || !pr->performance->state_count)
221 return -ENODEV;
222 *limit = pr->performance->states[pr->performance_platform_limit].
223 core_frequency * 1000;
224 return 0;
225}
226EXPORT_SYMBOL(acpi_processor_get_bios_limit);
227
Len Brown4be44fc2005-08-05 00:44:28 -0400228void acpi_processor_ppc_init(void)
229{
230 if (!cpufreq_register_notifier
231 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 acpi_processor_ppc_status |= PPC_REGISTERED;
233 else
Len Brown4be44fc2005-08-05 00:44:28 -0400234 printk(KERN_DEBUG
235 "Warning: Processor Platform Limit not supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Len Brown4be44fc2005-08-05 00:44:28 -0400238void acpi_processor_ppc_exit(void)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (acpi_processor_ppc_status & PPC_REGISTERED)
Len Brown4be44fc2005-08-05 00:44:28 -0400241 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
242 CPUFREQ_POLICY_NOTIFIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 acpi_processor_ppc_status &= ~PPC_REGISTERED;
245}
246
Len Brown4be44fc2005-08-05 00:44:28 -0400247static int acpi_processor_get_performance_control(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Len Brown4be44fc2005-08-05 00:44:28 -0400249 int result = 0;
250 acpi_status status = 0;
251 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
252 union acpi_object *pct = NULL;
253 union acpi_object obj = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400257 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400258 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262 pct = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
Len Brown4be44fc2005-08-05 00:44:28 -0400264 || (pct->package.count != 2)) {
Len Brown64684632006-06-26 23:41:38 -0400265 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 result = -EFAULT;
267 goto end;
268 }
269
270 /*
271 * control_register
272 */
273
274 obj = pct->package.elements[0];
275
276 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400277 || (obj.buffer.length < sizeof(struct acpi_pct_register))
278 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400279 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 result = -EFAULT;
281 goto end;
282 }
Len Brown4be44fc2005-08-05 00:44:28 -0400283 memcpy(&pr->performance->control_register, obj.buffer.pointer,
284 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
287 * status_register
288 */
289
290 obj = pct->package.elements[1];
291
292 if ((obj.type != ACPI_TYPE_BUFFER)
Len Brown4be44fc2005-08-05 00:44:28 -0400293 || (obj.buffer.length < sizeof(struct acpi_pct_register))
294 || (obj.buffer.pointer == NULL)) {
Len Brown64684632006-06-26 23:41:38 -0400295 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 result = -EFAULT;
297 goto end;
298 }
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300 memcpy(&pr->performance->status_register, obj.buffer.pointer,
301 sizeof(struct acpi_pct_register));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Len Brown4be44fc2005-08-05 00:44:28 -0400303 end:
Len Brown02438d82006-06-30 03:19:10 -0400304 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309static int acpi_processor_get_performance_states(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Len Brown4be44fc2005-08-05 00:44:28 -0400311 int result = 0;
312 acpi_status status = AE_OK;
313 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
314 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
315 struct acpi_buffer state = { 0, NULL };
316 union acpi_object *pss = NULL;
317 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
Len Brown4be44fc2005-08-05 00:44:28 -0400321 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400322 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200326 pss = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400328 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 result = -EFAULT;
330 goto end;
331 }
332
333 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400334 pss->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 pr->performance->state_count = pss->package.count;
Len Brown4be44fc2005-08-05 00:44:28 -0400337 pr->performance->states =
338 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
339 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!pr->performance->states) {
341 result = -ENOMEM;
342 goto end;
343 }
344
345 for (i = 0; i < pr->performance->state_count; i++) {
346
347 struct acpi_processor_px *px = &(pr->performance->states[i]);
348
349 state.length = sizeof(struct acpi_processor_px);
350 state.pointer = px;
351
352 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
353
354 status = acpi_extract_package(&(pss->package.elements[i]),
Len Brown4be44fc2005-08-05 00:44:28 -0400355 &format, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400357 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 result = -EFAULT;
359 kfree(pr->performance->states);
360 goto end;
361 }
362
363 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400364 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
365 i,
366 (u32) px->core_frequency,
367 (u32) px->power,
368 (u32) px->transition_latency,
369 (u32) px->bus_master_latency,
370 (u32) px->control, (u32) px->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Len Brown34d531e2009-05-26 15:11:06 -0400372 /*
373 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
374 */
375 if (!px->core_frequency ||
376 ((u32)(px->core_frequency * 1000) !=
377 (px->core_frequency * 1000))) {
378 printk(KERN_ERR FW_BUG PREFIX
379 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
380 px->core_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 result = -EFAULT;
382 kfree(pr->performance->states);
383 goto end;
384 }
385 }
386
Len Brown4be44fc2005-08-05 00:44:28 -0400387 end:
Len Brown02438d82006-06-30 03:19:10 -0400388 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Len Brown4be44fc2005-08-05 00:44:28 -0400393static int acpi_processor_get_performance_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Len Brown4be44fc2005-08-05 00:44:28 -0400395 int result = 0;
396 acpi_status status = AE_OK;
397 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!pr || !pr->performance || !pr->handle)
Patrick Mocheld550d982006-06-27 00:41:40 -0400400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 status = acpi_get_handle(pr->handle, "_PCT", &handle);
403 if (ACPI_FAILURE(status)) {
404 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400405 "ACPI-based processor performance control unavailable\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400406 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 result = acpi_processor_get_performance_control(pr);
410 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200411 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 result = acpi_processor_get_performance_states(pr);
414 if (result)
Thomas Renninger910dfae2008-09-01 14:27:04 +0200415 goto update_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Darrick J. Wong455c0d72010-02-18 10:28:20 -0800417 /* We need to call _PPC once when cpufreq starts */
418 if (ignore_ppc != 1)
419 result = acpi_processor_get_platform_limit(pr);
420
421 return result;
Thomas Renninger910dfae2008-09-01 14:27:04 +0200422
423 /*
424 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
425 * the BIOS is older than the CPU and does not know its frequencies
426 */
427 update_bios:
Miao Xie16be87e2008-10-24 17:22:04 +0800428#ifdef CONFIG_X86
Thomas Renninger910dfae2008-09-01 14:27:04 +0200429 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
430 if(boot_cpu_has(X86_FEATURE_EST))
431 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
432 "frequency support\n");
433 }
Miao Xie16be87e2008-10-24 17:22:04 +0800434#endif
Thomas Renninger910dfae2008-09-01 14:27:04 +0200435 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Len Brown4be44fc2005-08-05 00:44:28 -0400438int acpi_processor_notify_smm(struct module *calling_module)
439{
440 acpi_status status;
441 static int is_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400445 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (!try_module_get(calling_module))
Patrick Mocheld550d982006-06-27 00:41:40 -0400448 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Lucas De Marchi58f87ed2010-09-07 12:49:45 -0400450 /* is_done is set to negative if an error occurred,
451 * and to postitive if _no_ error occurred, but SMM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * was already notified. This avoids double notification
453 * which might lead to unexpected results...
454 */
455 if (is_done > 0) {
456 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400458 } else if (is_done < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400460 return is_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
463 is_done = -EIO;
464
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300465 /* Can't write pstate_control to smi_command if either value is zero */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300466 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300467 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300473 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300474 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300476 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
477 (u32) acpi_gbl_FADT.pstate_control, 8);
Len Brown4be44fc2005-08-05 00:44:28 -0400478 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400479 ACPI_EXCEPTION((AE_INFO, status,
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300480 "Failed to write pstate_control [0x%x] to "
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300481 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
482 acpi_gbl_FADT.smi_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 module_put(calling_module);
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
487 /* Success. If there's no _PPC, we need to fear nothing, so
488 * we can allow the cpufreq driver to be rmmod'ed. */
489 is_done = 1;
490
491 if (!(acpi_processor_ppc_status & PPC_IN_USE))
492 module_put(calling_module);
493
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Len Brown4be44fc2005-08-05 00:44:28 -0400497EXPORT_SYMBOL(acpi_processor_notify_smm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500499static int acpi_processor_get_psd(struct acpi_processor *pr)
500{
501 int result = 0;
502 acpi_status status = AE_OK;
503 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
504 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
505 struct acpi_buffer state = {0, NULL};
506 union acpi_object *psd = NULL;
507 struct acpi_psd_package *pdomain;
508
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500509 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
510 if (ACPI_FAILURE(status)) {
Len Brown9011bff2006-05-11 00:28:12 -0400511 return -ENODEV;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500512 }
513
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200514 psd = buffer.pointer;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500515 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800516 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500517 result = -EFAULT;
518 goto end;
519 }
520
521 if (psd->package.count != 1) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800522 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500523 result = -EFAULT;
524 goto end;
525 }
526
527 pdomain = &(pr->performance->domain_info);
528
529 state.length = sizeof(struct acpi_psd_package);
530 state.pointer = pdomain;
531
532 status = acpi_extract_package(&(psd->package.elements[0]),
533 &format, &state);
534 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800535 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500536 result = -EFAULT;
537 goto end;
538 }
539
540 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800541 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500542 result = -EFAULT;
543 goto end;
544 }
545
546 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800547 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500548 result = -EFAULT;
549 goto end;
550 }
551
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100552 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
553 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
554 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
555 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
556 result = -EFAULT;
557 goto end;
558 }
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500559end:
Len Brown02438d82006-06-30 03:19:10 -0400560 kfree(buffer.pointer);
Len Brown9011bff2006-05-11 00:28:12 -0400561 return result;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500562}
563
564int acpi_processor_preregister_performance(
Tejun Heoa29d8b82010-02-02 14:39:15 +0900565 struct acpi_processor_performance __percpu *performance)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500566{
567 int count, count_target;
568 int retval = 0;
569 unsigned int i, j;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800570 cpumask_var_t covered_cpus;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500571 struct acpi_processor *pr;
572 struct acpi_psd_package *pdomain;
573 struct acpi_processor *match_pr;
574 struct acpi_psd_package *match_pdomain;
575
Li Zefan79f55992009-06-15 14:58:26 +0800576 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800577 return -ENOMEM;
578
Len Brown785fccc2006-06-15 22:19:31 -0400579 mutex_lock(&performance_mutex);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500580
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100581 /*
582 * Check if another driver has already registered, and abort before
583 * changing pr->performance if it has. Check input data as well.
584 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400585 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700586 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500587 if (!pr) {
588 /* Look only at processors in ACPI namespace */
589 continue;
590 }
591
592 if (pr->performance) {
593 retval = -EBUSY;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100594 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500595 }
596
Rusty Russellb36128c2009-02-20 16:29:08 +0900597 if (!performance || !per_cpu_ptr(performance, i)) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500598 retval = -EINVAL;
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100599 goto err_out;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500600 }
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100601 }
602
603 /* Call _PSD for all CPUs */
604 for_each_possible_cpu(i) {
605 pr = per_cpu(processors, i);
606 if (!pr)
607 continue;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500608
Rusty Russellb36128c2009-02-20 16:29:08 +0900609 pr->performance = per_cpu_ptr(performance, i);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800610 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500611 if (acpi_processor_get_psd(pr)) {
612 retval = -EINVAL;
613 continue;
614 }
615 }
616 if (retval)
617 goto err_ret;
618
619 /*
620 * Now that we have _PSD data from all CPUs, lets setup P-state
621 * domain info.
622 */
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400623 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700624 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500625 if (!pr)
626 continue;
627
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800628 if (cpumask_test_cpu(i, covered_cpus))
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500629 continue;
630
631 pdomain = &(pr->performance->domain_info);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800632 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
633 cpumask_set_cpu(i, covered_cpus);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500634 if (pdomain->num_processors <= 1)
635 continue;
636
637 /* Validate the Domain info */
638 count_target = pdomain->num_processors;
639 count = 1;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400640 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500641 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
Venkatesh Pallipadi46f18e32006-06-26 00:34:43 -0400642 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
643 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
644 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500645 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500646
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400647 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500648 if (i == j)
649 continue;
650
Mike Travis706546d2008-06-09 16:22:23 -0700651 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500652 if (!match_pr)
653 continue;
654
655 match_pdomain = &(match_pr->performance->domain_info);
656 if (match_pdomain->domain != pdomain->domain)
657 continue;
658
659 /* Here i and j are in the same domain */
660
661 if (match_pdomain->num_processors != count_target) {
662 retval = -EINVAL;
663 goto err_ret;
664 }
665
666 if (pdomain->coord_type != match_pdomain->coord_type) {
667 retval = -EINVAL;
668 goto err_ret;
669 }
670
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800671 cpumask_set_cpu(j, covered_cpus);
672 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500673 count++;
674 }
675
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400676 for_each_possible_cpu(j) {
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500677 if (i == j)
678 continue;
679
Mike Travis706546d2008-06-09 16:22:23 -0700680 match_pr = per_cpu(processors, j);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500681 if (!match_pr)
682 continue;
683
684 match_pdomain = &(match_pr->performance->domain_info);
685 if (match_pdomain->domain != pdomain->domain)
686 continue;
687
688 match_pr->performance->shared_type =
689 pr->performance->shared_type;
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800690 cpumask_copy(match_pr->performance->shared_cpu_map,
691 pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500692 }
693 }
694
695err_ret:
KAMEZAWA Hiroyuki193de0c2006-04-27 05:25:00 -0400696 for_each_possible_cpu(i) {
Mike Travis706546d2008-06-09 16:22:23 -0700697 pr = per_cpu(processors, i);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500698 if (!pr || !pr->performance)
699 continue;
700
701 /* Assume no coordination on any error parsing domain info */
702 if (retval) {
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800703 cpumask_clear(pr->performance->shared_cpu_map);
704 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500705 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
706 }
707 pr->performance = NULL; /* Will be set for real in register */
708 }
709
Stanislaw Gruszkae1eb4772009-03-24 13:41:59 +0100710err_out:
Len Brown785fccc2006-06-15 22:19:31 -0400711 mutex_unlock(&performance_mutex);
Rusty Russell2fdf66b2008-12-31 18:08:47 -0800712 free_cpumask_var(covered_cpus);
Len Brown9011bff2006-05-11 00:28:12 -0400713 return retval;
Venkatesh Pallipadi3b2d9942005-12-14 15:05:00 -0500714}
715EXPORT_SYMBOL(acpi_processor_preregister_performance);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717int
Len Brown4be44fc2005-08-05 00:44:28 -0400718acpi_processor_register_performance(struct acpi_processor_performance
719 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct acpi_processor *pr;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400724 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400726 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Mike Travis706546d2008-06-09 16:22:23 -0700728 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400730 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400731 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
734 if (pr->performance) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400735 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Andrew Mortona913f502006-06-10 09:54:13 -0700739 WARN_ON(!performance);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 pr->performance = performance;
742
743 if (acpi_processor_get_performance_info(pr)) {
744 pr->performance = NULL;
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400745 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400746 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400749 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
Len Brown4be44fc2005-08-05 00:44:28 -0400752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753EXPORT_SYMBOL(acpi_processor_register_performance);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755void
Len Brown4be44fc2005-08-05 00:44:28 -0400756acpi_processor_unregister_performance(struct acpi_processor_performance
757 *performance, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct acpi_processor *pr;
760
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400761 mutex_lock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Mike Travis706546d2008-06-09 16:22:23 -0700763 pr = per_cpu(processors, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!pr) {
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400765 mutex_unlock(&performance_mutex);
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Andrew Mortona913f502006-06-10 09:54:13 -0700769 if (pr->performance)
770 kfree(pr->performance->states);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 pr->performance = NULL;
772
Arjan van de Ven65c19bb2006-04-27 05:25:00 -0400773 mutex_unlock(&performance_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Len Brown4be44fc2005-08-05 00:44:28 -0400777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778EXPORT_SYMBOL(acpi_processor_unregister_performance);