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