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