generic: add common struct for dma map operations
[linux-2.6-block.git] / drivers / acpi / processor_perflib.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/cpufreq.h>
33
34#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
65c19bbd 37#include <linux/mutex.h>
1da177e4
LT
38
39#include <asm/uaccess.h>
40#endif
16be87ea
MX
41
42#ifdef CONFIG_X86
910dfae2 43#include <asm/cpufeature.h>
16be87ea 44#endif
1da177e4
LT
45
46#include <acpi/acpi_bus.h>
89595b8f 47#include <acpi/acpi_drivers.h>
1da177e4
LT
48#include <acpi/processor.h>
49
1da177e4 50#define ACPI_PROCESSOR_CLASS "processor"
1da177e4
LT
51#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
52#define _COMPONENT ACPI_PROCESSOR_COMPONENT
f52fd66d 53ACPI_MODULE_NAME("processor_perflib");
1da177e4 54
65c19bbd 55static DEFINE_MUTEX(performance_mutex);
1da177e4 56
919158d1
TR
57/* Use cpufreq debug layer for _PPC changes. */
58#define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
59 "cpufreq-core", msg)
60
1da177e4
LT
61/*
62 * _PPC support is implemented as a CPUfreq policy notifier:
63 * This means each time a CPUfreq driver registered also with
64 * the ACPI core is asked to change the speed policy, the maximum
65 * value is adjusted so that it is within the platform limit.
66 *
67 * Also, when a new platform limit value is detected, the CPUfreq
68 * policy is adjusted accordingly.
69 */
70
a1531acd
TR
71/* ignore_ppc:
72 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
73 * ignore _PPC
74 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
75 * 1 -> ignore _PPC totally -> forced by user through boot param
76 */
9f497bcc 77static int ignore_ppc = -1;
613e5f33 78module_param(ignore_ppc, int, 0644);
623b78c3
TR
79MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
80 "limited by BIOS, this should help");
81
1da177e4
LT
82#define PPC_REGISTERED 1
83#define PPC_IN_USE 2
84
a1531acd 85static int acpi_processor_ppc_status;
1da177e4
LT
86
87static int acpi_processor_ppc_notifier(struct notifier_block *nb,
4be44fcd 88 unsigned long event, void *data)
1da177e4
LT
89{
90 struct cpufreq_policy *policy = data;
91 struct acpi_processor *pr;
92 unsigned int ppc = 0;
93
a1531acd
TR
94 if (event == CPUFREQ_START && ignore_ppc <= 0) {
95 ignore_ppc = 0;
96 return 0;
97 }
98
623b78c3
TR
99 if (ignore_ppc)
100 return 0;
101
1da177e4 102 if (event != CPUFREQ_INCOMPATIBLE)
9b67c5d4
TR
103 return 0;
104
105 mutex_lock(&performance_mutex);
1da177e4 106
706546d0 107 pr = per_cpu(processors, policy->cpu);
1da177e4
LT
108 if (!pr || !pr->performance)
109 goto out;
110
4be44fcd 111 ppc = (unsigned int)pr->performance_platform_limit;
1da177e4 112
0916bd3e 113 if (ppc >= pr->performance->state_count)
1da177e4
LT
114 goto out;
115
116 cpufreq_verify_within_limits(policy, 0,
4be44fcd
LB
117 pr->performance->states[ppc].
118 core_frequency * 1000);
1da177e4 119
4be44fcd 120 out:
65c19bbd 121 mutex_unlock(&performance_mutex);
1da177e4
LT
122
123 return 0;
124}
125
1da177e4
LT
126static struct notifier_block acpi_ppc_notifier_block = {
127 .notifier_call = acpi_processor_ppc_notifier,
128};
129
4be44fcd 130static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
1da177e4 131{
4be44fcd 132 acpi_status status = 0;
27663c58 133 unsigned long long ppc = 0;
1da177e4 134
1da177e4
LT
135
136 if (!pr)
d550d98d 137 return -EINVAL;
1da177e4
LT
138
139 /*
140 * _PPC indicates the maximum state currently supported by the platform
141 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
142 */
143 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
144
145 if (status != AE_NOT_FOUND)
146 acpi_processor_ppc_status |= PPC_IN_USE;
147
4be44fcd 148 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
a6fc6720 149 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
d550d98d 150 return -ENODEV;
1da177e4
LT
151 }
152
919158d1
TR
153 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
154 (int)ppc, ppc ? "" : "not");
155
4be44fcd 156 pr->performance_platform_limit = (int)ppc;
1da177e4 157
d550d98d 158 return 0;
1da177e4
LT
159}
160
4be44fcd 161int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
1da177e4 162{
623b78c3
TR
163 int ret;
164
165 if (ignore_ppc)
166 return 0;
167
168 ret = acpi_processor_get_platform_limit(pr);
169
1da177e4
LT
170 if (ret < 0)
171 return (ret);
172 else
173 return cpufreq_update_policy(pr->id);
174}
175
4be44fcd
LB
176void acpi_processor_ppc_init(void)
177{
178 if (!cpufreq_register_notifier
179 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
1da177e4
LT
180 acpi_processor_ppc_status |= PPC_REGISTERED;
181 else
4be44fcd
LB
182 printk(KERN_DEBUG
183 "Warning: Processor Platform Limit not supported.\n");
1da177e4
LT
184}
185
4be44fcd
LB
186void acpi_processor_ppc_exit(void)
187{
1da177e4 188 if (acpi_processor_ppc_status & PPC_REGISTERED)
4be44fcd
LB
189 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
190 CPUFREQ_POLICY_NOTIFIER);
1da177e4
LT
191
192 acpi_processor_ppc_status &= ~PPC_REGISTERED;
193}
194
4be44fcd 195static int acpi_processor_get_performance_control(struct acpi_processor *pr)
1da177e4 196{
4be44fcd
LB
197 int result = 0;
198 acpi_status status = 0;
199 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
200 union acpi_object *pct = NULL;
201 union acpi_object obj = { 0 };
1da177e4 202
1da177e4
LT
203
204 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
4be44fcd 205 if (ACPI_FAILURE(status)) {
a6fc6720 206 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
d550d98d 207 return -ENODEV;
1da177e4
LT
208 }
209
4be44fcd 210 pct = (union acpi_object *)buffer.pointer;
1da177e4 211 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
4be44fcd 212 || (pct->package.count != 2)) {
6468463a 213 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
1da177e4
LT
214 result = -EFAULT;
215 goto end;
216 }
217
218 /*
219 * control_register
220 */
221
222 obj = pct->package.elements[0];
223
224 if ((obj.type != ACPI_TYPE_BUFFER)
4be44fcd
LB
225 || (obj.buffer.length < sizeof(struct acpi_pct_register))
226 || (obj.buffer.pointer == NULL)) {
6468463a 227 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
1da177e4
LT
228 result = -EFAULT;
229 goto end;
230 }
4be44fcd
LB
231 memcpy(&pr->performance->control_register, obj.buffer.pointer,
232 sizeof(struct acpi_pct_register));
1da177e4
LT
233
234 /*
235 * status_register
236 */
237
238 obj = pct->package.elements[1];
239
240 if ((obj.type != ACPI_TYPE_BUFFER)
4be44fcd
LB
241 || (obj.buffer.length < sizeof(struct acpi_pct_register))
242 || (obj.buffer.pointer == NULL)) {
6468463a 243 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
1da177e4
LT
244 result = -EFAULT;
245 goto end;
246 }
247
4be44fcd
LB
248 memcpy(&pr->performance->status_register, obj.buffer.pointer,
249 sizeof(struct acpi_pct_register));
1da177e4 250
4be44fcd 251 end:
02438d87 252 kfree(buffer.pointer);
1da177e4 253
d550d98d 254 return result;
1da177e4
LT
255}
256
4be44fcd 257static int acpi_processor_get_performance_states(struct acpi_processor *pr)
1da177e4 258{
4be44fcd
LB
259 int result = 0;
260 acpi_status status = AE_OK;
261 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
262 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
263 struct acpi_buffer state = { 0, NULL };
264 union acpi_object *pss = NULL;
265 int i;
1da177e4 266
1da177e4
LT
267
268 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
4be44fcd 269 if (ACPI_FAILURE(status)) {
a6fc6720 270 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
d550d98d 271 return -ENODEV;
1da177e4
LT
272 }
273
50dd0969 274 pss = buffer.pointer;
1da177e4 275 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
6468463a 276 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
1da177e4
LT
277 result = -EFAULT;
278 goto end;
279 }
280
281 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
4be44fcd 282 pss->package.count));
1da177e4
LT
283
284 pr->performance->state_count = pss->package.count;
4be44fcd
LB
285 pr->performance->states =
286 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
287 GFP_KERNEL);
1da177e4
LT
288 if (!pr->performance->states) {
289 result = -ENOMEM;
290 goto end;
291 }
292
293 for (i = 0; i < pr->performance->state_count; i++) {
294
295 struct acpi_processor_px *px = &(pr->performance->states[i]);
296
297 state.length = sizeof(struct acpi_processor_px);
298 state.pointer = px;
299
300 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
301
302 status = acpi_extract_package(&(pss->package.elements[i]),
4be44fcd 303 &format, &state);
1da177e4 304 if (ACPI_FAILURE(status)) {
a6fc6720 305 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
1da177e4
LT
306 result = -EFAULT;
307 kfree(pr->performance->states);
308 goto end;
309 }
310
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
312 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
313 i,
314 (u32) px->core_frequency,
315 (u32) px->power,
316 (u32) px->transition_latency,
317 (u32) px->bus_master_latency,
318 (u32) px->control, (u32) px->status));
1da177e4
LT
319
320 if (!px->core_frequency) {
6468463a
LB
321 printk(KERN_ERR PREFIX
322 "Invalid _PSS data: freq is zero\n");
1da177e4
LT
323 result = -EFAULT;
324 kfree(pr->performance->states);
325 goto end;
326 }
327 }
328
4be44fcd 329 end:
02438d87 330 kfree(buffer.pointer);
1da177e4 331
d550d98d 332 return result;
1da177e4
LT
333}
334
4be44fcd 335static int acpi_processor_get_performance_info(struct acpi_processor *pr)
1da177e4 336{
4be44fcd
LB
337 int result = 0;
338 acpi_status status = AE_OK;
339 acpi_handle handle = NULL;
1da177e4 340
1da177e4 341 if (!pr || !pr->performance || !pr->handle)
d550d98d 342 return -EINVAL;
1da177e4 343
1da177e4
LT
344 status = acpi_get_handle(pr->handle, "_PCT", &handle);
345 if (ACPI_FAILURE(status)) {
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 347 "ACPI-based processor performance control unavailable\n"));
d550d98d 348 return -ENODEV;
1da177e4
LT
349 }
350
351 result = acpi_processor_get_performance_control(pr);
352 if (result)
910dfae2 353 goto update_bios;
1da177e4
LT
354
355 result = acpi_processor_get_performance_states(pr);
356 if (result)
910dfae2 357 goto update_bios;
1da177e4 358
d550d98d 359 return 0;
910dfae2
TR
360
361 /*
362 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
363 * the BIOS is older than the CPU and does not know its frequencies
364 */
365 update_bios:
16be87ea 366#ifdef CONFIG_X86
910dfae2
TR
367 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
368 if(boot_cpu_has(X86_FEATURE_EST))
369 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
370 "frequency support\n");
371 }
16be87ea 372#endif
910dfae2 373 return result;
1da177e4
LT
374}
375
4be44fcd
LB
376int acpi_processor_notify_smm(struct module *calling_module)
377{
378 acpi_status status;
379 static int is_done = 0;
1da177e4 380
1da177e4
LT
381
382 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
d550d98d 383 return -EBUSY;
1da177e4
LT
384
385 if (!try_module_get(calling_module))
d550d98d 386 return -EINVAL;
1da177e4
LT
387
388 /* is_done is set to negative if an error occured,
389 * and to postitive if _no_ error occured, but SMM
390 * was already notified. This avoids double notification
391 * which might lead to unexpected results...
392 */
393 if (is_done > 0) {
394 module_put(calling_module);
d550d98d 395 return 0;
4be44fcd 396 } else if (is_done < 0) {
1da177e4 397 module_put(calling_module);
d550d98d 398 return is_done;
1da177e4
LT
399 }
400
401 is_done = -EIO;
402
ad71860a 403 /* Can't write pstate_control to smi_command if either value is zero */
cee324b1 404 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
ad71860a 405 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
1da177e4 406 module_put(calling_module);
d550d98d 407 return 0;
1da177e4
LT
408 }
409
4be44fcd 410 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
ad71860a 411 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
cee324b1 412 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
1da177e4 413
cee324b1
AS
414 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
415 (u32) acpi_gbl_FADT.pstate_control, 8);
4be44fcd 416 if (ACPI_FAILURE(status)) {
a6fc6720 417 ACPI_EXCEPTION((AE_INFO, status,
ad71860a 418 "Failed to write pstate_control [0x%x] to "
cee324b1
AS
419 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
420 acpi_gbl_FADT.smi_command));
1da177e4 421 module_put(calling_module);
d550d98d 422 return status;
1da177e4
LT
423 }
424
425 /* Success. If there's no _PPC, we need to fear nothing, so
426 * we can allow the cpufreq driver to be rmmod'ed. */
427 is_done = 1;
428
429 if (!(acpi_processor_ppc_status & PPC_IN_USE))
430 module_put(calling_module);
431
d550d98d 432 return 0;
1da177e4 433}
1da177e4 434
4be44fcd 435EXPORT_SYMBOL(acpi_processor_notify_smm);
1da177e4
LT
436
437#ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
438/* /proc/acpi/processor/../performance interface (DEPRECATED) */
439
440static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
441static struct file_operations acpi_processor_perf_fops = {
cf7acfab 442 .owner = THIS_MODULE,
4be44fcd
LB
443 .open = acpi_processor_perf_open_fs,
444 .read = seq_read,
445 .llseek = seq_lseek,
446 .release = single_release,
1da177e4
LT
447};
448
449static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
450{
50dd0969 451 struct acpi_processor *pr = seq->private;
4be44fcd 452 int i;
1da177e4 453
1da177e4
LT
454
455 if (!pr)
456 goto end;
457
458 if (!pr->performance) {
459 seq_puts(seq, "<not supported>\n");
460 goto end;
461 }
462
463 seq_printf(seq, "state count: %d\n"
4be44fcd
LB
464 "active state: P%d\n",
465 pr->performance->state_count, pr->performance->state);
1da177e4
LT
466
467 seq_puts(seq, "states:\n");
468 for (i = 0; i < pr->performance->state_count; i++)
4be44fcd
LB
469 seq_printf(seq,
470 " %cP%d: %d MHz, %d mW, %d uS\n",
471 (i == pr->performance->state ? '*' : ' '), i,
472 (u32) pr->performance->states[i].core_frequency,
473 (u32) pr->performance->states[i].power,
474 (u32) pr->performance->states[i].transition_latency);
475
476 end:
d550d98d 477 return 0;
1da177e4
LT
478}
479
480static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
481{
482 return single_open(file, acpi_processor_perf_seq_show,
4be44fcd 483 PDE(inode)->data);
1da177e4
LT
484}
485
4be44fcd 486static void acpi_cpufreq_add_file(struct acpi_processor *pr)
1da177e4 487{
4be44fcd 488 struct acpi_device *device = NULL;
1da177e4 489
1da177e4
LT
490
491 if (acpi_bus_get_device(pr->handle, &device))
d550d98d 492 return;
1da177e4
LT
493
494 /* add file 'performance' [R/W] */
cf7acfab
DL
495 proc_create_data(ACPI_PROCESSOR_FILE_PERFORMANCE, S_IFREG | S_IRUGO,
496 acpi_device_dir(device),
497 &acpi_processor_perf_fops, acpi_driver_data(device));
d550d98d 498 return;
1da177e4
LT
499}
500
4be44fcd 501static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
1da177e4 502{
4be44fcd 503 struct acpi_device *device = NULL;
1da177e4 504
1da177e4
LT
505
506 if (acpi_bus_get_device(pr->handle, &device))
d550d98d 507 return;
1da177e4
LT
508
509 /* remove file 'performance' */
510 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
4be44fcd 511 acpi_device_dir(device));
1da177e4 512
d550d98d 513 return;
1da177e4
LT
514}
515
516#else
4be44fcd
LB
517static void acpi_cpufreq_add_file(struct acpi_processor *pr)
518{
519 return;
520}
521static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
522{
523 return;
524}
525#endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
1da177e4 526
3b2d9942
VP
527static int acpi_processor_get_psd(struct acpi_processor *pr)
528{
529 int result = 0;
530 acpi_status status = AE_OK;
531 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
532 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
533 struct acpi_buffer state = {0, NULL};
534 union acpi_object *psd = NULL;
535 struct acpi_psd_package *pdomain;
536
3b2d9942
VP
537 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
538 if (ACPI_FAILURE(status)) {
9011bff4 539 return -ENODEV;
3b2d9942
VP
540 }
541
50dd0969 542 psd = buffer.pointer;
3b2d9942 543 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
55ac9a01 544 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
545 result = -EFAULT;
546 goto end;
547 }
548
549 if (psd->package.count != 1) {
55ac9a01 550 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
551 result = -EFAULT;
552 goto end;
553 }
554
555 pdomain = &(pr->performance->domain_info);
556
557 state.length = sizeof(struct acpi_psd_package);
558 state.pointer = pdomain;
559
560 status = acpi_extract_package(&(psd->package.elements[0]),
561 &format, &state);
562 if (ACPI_FAILURE(status)) {
55ac9a01 563 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
564 result = -EFAULT;
565 goto end;
566 }
567
568 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
55ac9a01 569 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
3b2d9942
VP
570 result = -EFAULT;
571 goto end;
572 }
573
574 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
55ac9a01 575 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
3b2d9942
VP
576 result = -EFAULT;
577 goto end;
578 }
579
580end:
02438d87 581 kfree(buffer.pointer);
9011bff4 582 return result;
3b2d9942
VP
583}
584
585int acpi_processor_preregister_performance(
50109292 586 struct acpi_processor_performance *performance)
3b2d9942
VP
587{
588 int count, count_target;
589 int retval = 0;
590 unsigned int i, j;
2fdf66b4 591 cpumask_var_t covered_cpus;
3b2d9942
VP
592 struct acpi_processor *pr;
593 struct acpi_psd_package *pdomain;
594 struct acpi_processor *match_pr;
595 struct acpi_psd_package *match_pdomain;
596
2fdf66b4
RR
597 if (!alloc_cpumask_var(&covered_cpus, GFP_KERNEL))
598 return -ENOMEM;
599
785fcccd 600 mutex_lock(&performance_mutex);
3b2d9942
VP
601
602 retval = 0;
603
604 /* Call _PSD for all CPUs */
193de0c7 605 for_each_possible_cpu(i) {
706546d0 606 pr = per_cpu(processors, i);
3b2d9942
VP
607 if (!pr) {
608 /* Look only at processors in ACPI namespace */
609 continue;
610 }
611
612 if (pr->performance) {
613 retval = -EBUSY;
614 continue;
615 }
616
50109292 617 if (!performance || !percpu_ptr(performance, i)) {
3b2d9942
VP
618 retval = -EINVAL;
619 continue;
620 }
621
50109292 622 pr->performance = percpu_ptr(performance, i);
2fdf66b4 623 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
3b2d9942
VP
624 if (acpi_processor_get_psd(pr)) {
625 retval = -EINVAL;
626 continue;
627 }
628 }
629 if (retval)
630 goto err_ret;
631
632 /*
633 * Now that we have _PSD data from all CPUs, lets setup P-state
634 * domain info.
635 */
193de0c7 636 for_each_possible_cpu(i) {
706546d0 637 pr = per_cpu(processors, i);
3b2d9942
VP
638 if (!pr)
639 continue;
640
641 /* Basic validity check for domain info */
642 pdomain = &(pr->performance->domain_info);
643 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
644 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
645 retval = -EINVAL;
646 goto err_ret;
647 }
648 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
649 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
650 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
651 retval = -EINVAL;
652 goto err_ret;
653 }
654 }
655
2fdf66b4 656 cpumask_clear(covered_cpus);
193de0c7 657 for_each_possible_cpu(i) {
706546d0 658 pr = per_cpu(processors, i);
3b2d9942
VP
659 if (!pr)
660 continue;
661
2fdf66b4 662 if (cpumask_test_cpu(i, covered_cpus))
3b2d9942
VP
663 continue;
664
665 pdomain = &(pr->performance->domain_info);
2fdf66b4
RR
666 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
667 cpumask_set_cpu(i, covered_cpus);
3b2d9942
VP
668 if (pdomain->num_processors <= 1)
669 continue;
670
671 /* Validate the Domain info */
672 count_target = pdomain->num_processors;
673 count = 1;
46f18e3a 674 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
3b2d9942 675 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
46f18e3a
VP
676 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
677 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
678 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
3b2d9942 679 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
3b2d9942 680
193de0c7 681 for_each_possible_cpu(j) {
3b2d9942
VP
682 if (i == j)
683 continue;
684
706546d0 685 match_pr = per_cpu(processors, j);
3b2d9942
VP
686 if (!match_pr)
687 continue;
688
689 match_pdomain = &(match_pr->performance->domain_info);
690 if (match_pdomain->domain != pdomain->domain)
691 continue;
692
693 /* Here i and j are in the same domain */
694
695 if (match_pdomain->num_processors != count_target) {
696 retval = -EINVAL;
697 goto err_ret;
698 }
699
700 if (pdomain->coord_type != match_pdomain->coord_type) {
701 retval = -EINVAL;
702 goto err_ret;
703 }
704
2fdf66b4
RR
705 cpumask_set_cpu(j, covered_cpus);
706 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
3b2d9942
VP
707 count++;
708 }
709
193de0c7 710 for_each_possible_cpu(j) {
3b2d9942
VP
711 if (i == j)
712 continue;
713
706546d0 714 match_pr = per_cpu(processors, j);
3b2d9942
VP
715 if (!match_pr)
716 continue;
717
718 match_pdomain = &(match_pr->performance->domain_info);
719 if (match_pdomain->domain != pdomain->domain)
720 continue;
721
722 match_pr->performance->shared_type =
723 pr->performance->shared_type;
2fdf66b4
RR
724 cpumask_copy(match_pr->performance->shared_cpu_map,
725 pr->performance->shared_cpu_map);
3b2d9942
VP
726 }
727 }
728
729err_ret:
193de0c7 730 for_each_possible_cpu(i) {
706546d0 731 pr = per_cpu(processors, i);
3b2d9942
VP
732 if (!pr || !pr->performance)
733 continue;
734
735 /* Assume no coordination on any error parsing domain info */
736 if (retval) {
2fdf66b4
RR
737 cpumask_clear(pr->performance->shared_cpu_map);
738 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
3b2d9942
VP
739 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
740 }
741 pr->performance = NULL; /* Will be set for real in register */
742 }
743
785fcccd 744 mutex_unlock(&performance_mutex);
2fdf66b4 745 free_cpumask_var(covered_cpus);
9011bff4 746 return retval;
3b2d9942
VP
747}
748EXPORT_SYMBOL(acpi_processor_preregister_performance);
749
750
1da177e4 751int
4be44fcd
LB
752acpi_processor_register_performance(struct acpi_processor_performance
753 *performance, unsigned int cpu)
1da177e4
LT
754{
755 struct acpi_processor *pr;
756
1da177e4
LT
757
758 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
d550d98d 759 return -EINVAL;
1da177e4 760
65c19bbd 761 mutex_lock(&performance_mutex);
1da177e4 762
706546d0 763 pr = per_cpu(processors, cpu);
1da177e4 764 if (!pr) {
65c19bbd 765 mutex_unlock(&performance_mutex);
d550d98d 766 return -ENODEV;
1da177e4
LT
767 }
768
769 if (pr->performance) {
65c19bbd 770 mutex_unlock(&performance_mutex);
d550d98d 771 return -EBUSY;
1da177e4
LT
772 }
773
a913f507
AM
774 WARN_ON(!performance);
775
1da177e4
LT
776 pr->performance = performance;
777
778 if (acpi_processor_get_performance_info(pr)) {
779 pr->performance = NULL;
65c19bbd 780 mutex_unlock(&performance_mutex);
d550d98d 781 return -EIO;
1da177e4
LT
782 }
783
784 acpi_cpufreq_add_file(pr);
785
65c19bbd 786 mutex_unlock(&performance_mutex);
d550d98d 787 return 0;
1da177e4 788}
1da177e4 789
4be44fcd 790EXPORT_SYMBOL(acpi_processor_register_performance);
1da177e4
LT
791
792void
4be44fcd
LB
793acpi_processor_unregister_performance(struct acpi_processor_performance
794 *performance, unsigned int cpu)
1da177e4
LT
795{
796 struct acpi_processor *pr;
797
1da177e4 798
65c19bbd 799 mutex_lock(&performance_mutex);
1da177e4 800
706546d0 801 pr = per_cpu(processors, cpu);
1da177e4 802 if (!pr) {
65c19bbd 803 mutex_unlock(&performance_mutex);
d550d98d 804 return;
1da177e4
LT
805 }
806
a913f507
AM
807 if (pr->performance)
808 kfree(pr->performance->states);
1da177e4
LT
809 pr->performance = NULL;
810
811 acpi_cpufreq_remove_file(pr);
812
65c19bbd 813 mutex_unlock(&performance_mutex);
1da177e4 814
d550d98d 815 return;
1da177e4 816}
4be44fcd 817
1da177e4 818EXPORT_SYMBOL(acpi_processor_unregister_performance);