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