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