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