1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * processor_perflib.c - ACPI Processor P-States Library ($Revision: 71 $)
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
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>
20 #include <asm/cpufeature.h>
23 #define PREFIX "ACPI: "
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");
30 static DEFINE_MUTEX(performance_mutex
);
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.
38 * Also, when a new platform limit value is detected, the CPUfreq
39 * policy is adjusted accordingly.
43 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
45 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
46 * 1 -> ignore _PPC totally -> forced by user through boot param
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");
53 #define PPC_REGISTERED 1
56 static int acpi_processor_ppc_status
;
58 static int acpi_processor_ppc_notifier(struct notifier_block
*nb
,
59 unsigned long event
, void *data
)
61 struct cpufreq_policy
*policy
= data
;
62 struct acpi_processor
*pr
;
71 if (event
!= CPUFREQ_ADJUST
)
74 mutex_lock(&performance_mutex
);
76 pr
= per_cpu(processors
, policy
->cpu
);
77 if (!pr
|| !pr
->performance
)
80 ppc
= (unsigned int)pr
->performance_platform_limit
;
82 if (ppc
>= pr
->performance
->state_count
)
85 cpufreq_verify_within_limits(policy
, 0,
86 pr
->performance
->states
[ppc
].
87 core_frequency
* 1000);
90 mutex_unlock(&performance_mutex
);
95 static struct notifier_block acpi_ppc_notifier_block
= {
96 .notifier_call
= acpi_processor_ppc_notifier
,
99 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
101 acpi_status status
= 0;
102 unsigned long long ppc
= 0;
109 * _PPC indicates the maximum state currently supported by the platform
110 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
112 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
114 if (status
!= AE_NOT_FOUND
)
115 acpi_processor_ppc_status
|= PPC_IN_USE
;
117 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
118 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PPC"));
122 pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr
->id
,
123 (int)ppc
, ppc
? "" : "not");
125 pr
->performance_platform_limit
= (int)ppc
;
130 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
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
138 static void acpi_processor_ppc_ost(acpi_handle handle
, int status
)
140 if (acpi_has_method(handle
, "_OST"))
141 acpi_evaluate_ost(handle
, ACPI_PROCESSOR_NOTIFY_PERFORMANCE
,
145 void acpi_processor_ppc_has_changed(struct acpi_processor
*pr
, int event_flag
)
149 if (ignore_ppc
|| !pr
->performance
) {
151 * Only when it is notification event, the _OST object
152 * will be evaluated. Otherwise it is skipped.
155 acpi_processor_ppc_ost(pr
->handle
, 1);
159 ret
= acpi_processor_get_platform_limit(pr
);
161 * Only when it is notification event, the _OST object
162 * will be evaluated. Otherwise it is skipped.
166 acpi_processor_ppc_ost(pr
->handle
, 1);
168 acpi_processor_ppc_ost(pr
->handle
, 0);
171 cpufreq_update_limits(pr
->id
);
174 int acpi_processor_get_bios_limit(int cpu
, unsigned int *limit
)
176 struct acpi_processor
*pr
;
178 pr
= per_cpu(processors
, cpu
);
179 if (!pr
|| !pr
->performance
|| !pr
->performance
->state_count
)
181 *limit
= pr
->performance
->states
[pr
->performance_platform_limit
].
182 core_frequency
* 1000;
185 EXPORT_SYMBOL(acpi_processor_get_bios_limit
);
187 void acpi_processor_ppc_init(void)
189 if (!cpufreq_register_notifier
190 (&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
191 acpi_processor_ppc_status
|= PPC_REGISTERED
;
194 "Warning: Processor Platform Limit not supported.\n");
197 void acpi_processor_ppc_exit(void)
199 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
200 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
,
201 CPUFREQ_POLICY_NOTIFIER
);
203 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
206 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
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 };
215 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
216 if (ACPI_FAILURE(status
)) {
217 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
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");
233 obj
= pct
->package
.elements
[0];
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");
242 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
243 sizeof(struct acpi_pct_register
));
249 obj
= pct
->package
.elements
[1];
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");
259 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
260 sizeof(struct acpi_pct_register
));
263 kfree(buffer
.pointer
);
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.
273 static void amd_fixup_frequency(struct acpi_processor_px
*px
, int i
)
275 u32 hi
, lo
, fid
, did
;
276 int index
= px
->control
& 0x00000007;
278 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
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
);
286 * Bit 63: PstateEn. Read-write. If set, the P-state is valid.
293 if (boot_cpu_data
.x86
== 0x10)
294 px
->core_frequency
= (100 * (fid
+ 0x10)) >> did
;
296 px
->core_frequency
= (100 * (fid
+ 8)) >> did
;
300 static void amd_fixup_frequency(struct acpi_processor_px
*px
, int i
) {};
303 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
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
;
312 int last_invalid
= -1;
315 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
316 if (ACPI_FAILURE(status
)) {
317 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
321 pss
= buffer
.pointer
;
322 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
323 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
328 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
329 pss
->package
.count
));
331 pr
->performance
->state_count
= pss
->package
.count
;
332 pr
->performance
->states
=
333 kmalloc_array(pss
->package
.count
,
334 sizeof(struct acpi_processor_px
),
336 if (!pr
->performance
->states
) {
341 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
343 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
345 state
.length
= sizeof(struct acpi_processor_px
);
348 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
350 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
352 if (ACPI_FAILURE(status
)) {
353 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
355 kfree(pr
->performance
->states
);
359 amd_fixup_frequency(px
, i
);
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",
364 (u32
) px
->core_frequency
,
366 (u32
) px
->transition_latency
,
367 (u32
) px
->bus_master_latency
,
368 (u32
) px
->control
, (u32
) px
->status
));
371 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
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)
382 if (last_invalid
!= -1) {
384 * Copy this valid entry over last_invalid entry
386 memcpy(&(pr
->performance
->states
[last_invalid
]),
387 px
, sizeof(struct acpi_processor_px
));
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
);
397 kfree(pr
->performance
->states
);
398 pr
->performance
->states
= NULL
;
401 if (last_invalid
> 0)
402 pr
->performance
->state_count
= last_invalid
;
405 kfree(buffer
.pointer
);
410 int acpi_processor_get_performance_info(struct acpi_processor
*pr
)
414 if (!pr
|| !pr
->performance
|| !pr
->handle
)
417 if (!acpi_has_method(pr
->handle
, "_PCT")) {
418 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
419 "ACPI-based processor performance control unavailable\n"));
423 result
= acpi_processor_get_performance_control(pr
);
427 result
= acpi_processor_get_performance_states(pr
);
431 /* We need to call _PPC once when cpufreq starts */
433 result
= acpi_processor_get_platform_limit(pr
);
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
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");
451 EXPORT_SYMBOL_GPL(acpi_processor_get_performance_info
);
453 int acpi_processor_pstate_control(void)
457 if (!acpi_gbl_FADT
.smi_command
|| !acpi_gbl_FADT
.pstate_control
)
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
));
464 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
465 (u32
)acpi_gbl_FADT
.pstate_control
, 8);
466 if (ACPI_SUCCESS(status
))
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
));
475 int acpi_processor_notify_smm(struct module
*calling_module
)
477 static int is_done
= 0;
480 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
483 if (!try_module_get(calling_module
))
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...
492 module_put(calling_module
);
494 } else if (is_done
< 0) {
495 module_put(calling_module
);
501 result
= acpi_processor_pstate_control();
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No SMI port or pstate_control\n"));
504 module_put(calling_module
);
508 module_put(calling_module
);
512 /* Success. If there's no _PPC, we need to fear nothing, so
513 * we can allow the cpufreq driver to be rmmod'ed. */
516 if (!(acpi_processor_ppc_status
& PPC_IN_USE
))
517 module_put(calling_module
);
522 EXPORT_SYMBOL(acpi_processor_notify_smm
);
524 int acpi_processor_get_psd(acpi_handle handle
, struct acpi_psd_package
*pdomain
)
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
;
533 status
= acpi_evaluate_object(handle
, "_PSD", NULL
, &buffer
);
534 if (ACPI_FAILURE(status
)) {
538 psd
= buffer
.pointer
;
539 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
540 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
545 if (psd
->package
.count
!= 1) {
546 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
551 state
.length
= sizeof(struct acpi_psd_package
);
552 state
.pointer
= pdomain
;
554 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
556 if (ACPI_FAILURE(status
)) {
557 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
562 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
563 printk(KERN_ERR PREFIX
"Unknown _PSD:num_entries\n");
568 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
569 printk(KERN_ERR PREFIX
"Unknown _PSD:revision\n");
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");
582 kfree(buffer
.pointer
);
585 EXPORT_SYMBOL(acpi_processor_get_psd
);
587 int acpi_processor_preregister_performance(
588 struct acpi_processor_performance __percpu
*performance
)
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
;
599 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
602 mutex_lock(&performance_mutex
);
605 * Check if another driver has already registered, and abort before
606 * changing pr->performance if it has. Check input data as well.
608 for_each_possible_cpu(i
) {
609 pr
= per_cpu(processors
, i
);
611 /* Look only at processors in ACPI namespace */
615 if (pr
->performance
) {
620 if (!performance
|| !per_cpu_ptr(performance
, i
)) {
626 /* Call _PSD for all CPUs */
627 for_each_possible_cpu(i
) {
628 pr
= per_cpu(processors
, i
);
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
)) {
644 * Now that we have _PSD data from all CPUs, lets setup P-state
647 for_each_possible_cpu(i
) {
648 pr
= per_cpu(processors
, i
);
652 if (cpumask_test_cpu(i
, covered_cpus
))
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)
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
;
670 for_each_possible_cpu(j
) {
674 match_pr
= per_cpu(processors
, j
);
678 match_pdomain
= &(match_pr
->performance
->domain_info
);
679 if (match_pdomain
->domain
!= pdomain
->domain
)
682 /* Here i and j are in the same domain */
684 if (match_pdomain
->num_processors
!= count_target
) {
689 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
694 cpumask_set_cpu(j
, covered_cpus
);
695 cpumask_set_cpu(j
, pr
->performance
->shared_cpu_map
);
698 for_each_possible_cpu(j
) {
702 match_pr
= per_cpu(processors
, j
);
706 match_pdomain
= &(match_pr
->performance
->domain_info
);
707 if (match_pdomain
->domain
!= pdomain
->domain
)
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
);
718 for_each_possible_cpu(i
) {
719 pr
= per_cpu(processors
, i
);
720 if (!pr
|| !pr
->performance
)
723 /* Assume no coordination on any error parsing domain info */
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
;
729 pr
->performance
= NULL
; /* Will be set for real in register */
733 mutex_unlock(&performance_mutex
);
734 free_cpumask_var(covered_cpus
);
737 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
740 acpi_processor_register_performance(struct acpi_processor_performance
741 *performance
, unsigned int cpu
)
743 struct acpi_processor
*pr
;
745 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
748 mutex_lock(&performance_mutex
);
750 pr
= per_cpu(processors
, cpu
);
752 mutex_unlock(&performance_mutex
);
756 if (pr
->performance
) {
757 mutex_unlock(&performance_mutex
);
761 WARN_ON(!performance
);
763 pr
->performance
= performance
;
765 if (acpi_processor_get_performance_info(pr
)) {
766 pr
->performance
= NULL
;
767 mutex_unlock(&performance_mutex
);
771 mutex_unlock(&performance_mutex
);
775 EXPORT_SYMBOL(acpi_processor_register_performance
);
777 void acpi_processor_unregister_performance(unsigned int cpu
)
779 struct acpi_processor
*pr
;
781 mutex_lock(&performance_mutex
);
783 pr
= per_cpu(processors
, cpu
);
785 mutex_unlock(&performance_mutex
);
790 kfree(pr
->performance
->states
);
791 pr
->performance
= NULL
;
793 mutex_unlock(&performance_mutex
);
798 EXPORT_SYMBOL(acpi_processor_unregister_performance
);