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_FILE_PERFORMANCE "performance"
26 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
27 ACPI_MODULE_NAME("processor_perflib");
29 static DEFINE_MUTEX(performance_mutex
);
32 * _PPC support is implemented as a CPUfreq policy notifier:
33 * This means each time a CPUfreq driver registered also with
34 * the ACPI core is asked to change the speed policy, the maximum
35 * value is adjusted so that it is within the platform limit.
37 * Also, when a new platform limit value is detected, the CPUfreq
38 * policy is adjusted accordingly.
42 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
44 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
45 * 1 -> ignore _PPC totally -> forced by user through boot param
47 static int ignore_ppc
= -1;
48 module_param(ignore_ppc
, int, 0644);
49 MODULE_PARM_DESC(ignore_ppc
, "If the frequency of your machine gets wrongly" \
50 "limited by BIOS, this should help");
52 static bool acpi_processor_ppc_in_use
;
54 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
56 acpi_status status
= 0;
57 unsigned long long ppc
= 0;
64 * _PPC indicates the maximum state currently supported by the platform
65 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
67 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
69 if (status
!= AE_NOT_FOUND
)
70 acpi_processor_ppc_in_use
= true;
72 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
73 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PPC"));
77 pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr
->id
,
78 (int)ppc
, ppc
? "" : "not");
80 pr
->performance_platform_limit
= (int)ppc
;
82 if (ppc
>= pr
->performance
->state_count
||
83 unlikely(!freq_qos_request_active(&pr
->perflib_req
)))
86 ret
= freq_qos_update_request(&pr
->perflib_req
,
87 pr
->performance
->states
[ppc
].core_frequency
* 1000);
89 pr_warn("Failed to update perflib freq constraint: CPU%d (%d)\n",
96 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
98 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
99 * @handle: ACPI processor handle
100 * @status: the status code of _PPC evaluation
101 * 0: success. OSPM is now using the performance state specificed.
102 * 1: failure. OSPM has not changed the number of P-states in use
104 static void acpi_processor_ppc_ost(acpi_handle handle
, int status
)
106 if (acpi_has_method(handle
, "_OST"))
107 acpi_evaluate_ost(handle
, ACPI_PROCESSOR_NOTIFY_PERFORMANCE
,
111 void acpi_processor_ppc_has_changed(struct acpi_processor
*pr
, int event_flag
)
115 if (ignore_ppc
|| !pr
->performance
) {
117 * Only when it is notification event, the _OST object
118 * will be evaluated. Otherwise it is skipped.
121 acpi_processor_ppc_ost(pr
->handle
, 1);
125 ret
= acpi_processor_get_platform_limit(pr
);
127 * Only when it is notification event, the _OST object
128 * will be evaluated. Otherwise it is skipped.
132 acpi_processor_ppc_ost(pr
->handle
, 1);
134 acpi_processor_ppc_ost(pr
->handle
, 0);
137 cpufreq_update_limits(pr
->id
);
140 int acpi_processor_get_bios_limit(int cpu
, unsigned int *limit
)
142 struct acpi_processor
*pr
;
144 pr
= per_cpu(processors
, cpu
);
145 if (!pr
|| !pr
->performance
|| !pr
->performance
->state_count
)
147 *limit
= pr
->performance
->states
[pr
->performance_platform_limit
].
148 core_frequency
* 1000;
151 EXPORT_SYMBOL(acpi_processor_get_bios_limit
);
153 void acpi_processor_ignore_ppc_init(void)
159 void acpi_processor_ppc_init(struct cpufreq_policy
*policy
)
163 for_each_cpu(cpu
, policy
->related_cpus
) {
164 struct acpi_processor
*pr
= per_cpu(processors
, cpu
);
170 ret
= freq_qos_add_request(&policy
->constraints
,
172 FREQ_QOS_MAX
, INT_MAX
);
174 pr_err("Failed to add freq constraint for CPU%d (%d)\n",
179 void acpi_processor_ppc_exit(struct cpufreq_policy
*policy
)
183 for_each_cpu(cpu
, policy
->related_cpus
) {
184 struct acpi_processor
*pr
= per_cpu(processors
, cpu
);
187 freq_qos_remove_request(&pr
->perflib_req
);
191 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
194 acpi_status status
= 0;
195 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
196 union acpi_object
*pct
= NULL
;
197 union acpi_object obj
= { 0 };
200 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
201 if (ACPI_FAILURE(status
)) {
202 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
206 pct
= (union acpi_object
*)buffer
.pointer
;
207 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
208 || (pct
->package
.count
!= 2)) {
209 printk(KERN_ERR PREFIX
"Invalid _PCT data\n");
218 obj
= pct
->package
.elements
[0];
220 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
221 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
222 || (obj
.buffer
.pointer
== NULL
)) {
223 printk(KERN_ERR PREFIX
"Invalid _PCT data (control_register)\n");
227 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
228 sizeof(struct acpi_pct_register
));
234 obj
= pct
->package
.elements
[1];
236 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
237 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
238 || (obj
.buffer
.pointer
== NULL
)) {
239 printk(KERN_ERR PREFIX
"Invalid _PCT data (status_register)\n");
244 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
245 sizeof(struct acpi_pct_register
));
248 kfree(buffer
.pointer
);
255 * Some AMDs have 50MHz frequency multiples, but only provide 100MHz rounding
256 * in their ACPI data. Calculate the real values and fix up the _PSS data.
258 static void amd_fixup_frequency(struct acpi_processor_px
*px
, int i
)
260 u32 hi
, lo
, fid
, did
;
261 int index
= px
->control
& 0x00000007;
263 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_AMD
)
266 if ((boot_cpu_data
.x86
== 0x10 && boot_cpu_data
.x86_model
< 10)
267 || boot_cpu_data
.x86
== 0x11) {
268 rdmsr(MSR_AMD_PSTATE_DEF_BASE
+ index
, lo
, hi
);
271 * Bit 63: PstateEn. Read-write. If set, the P-state is valid.
278 if (boot_cpu_data
.x86
== 0x10)
279 px
->core_frequency
= (100 * (fid
+ 0x10)) >> did
;
281 px
->core_frequency
= (100 * (fid
+ 8)) >> did
;
285 static void amd_fixup_frequency(struct acpi_processor_px
*px
, int i
) {};
288 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
291 acpi_status status
= AE_OK
;
292 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
293 struct acpi_buffer format
= { sizeof("NNNNNN"), "NNNNNN" };
294 struct acpi_buffer state
= { 0, NULL
};
295 union acpi_object
*pss
= NULL
;
297 int last_invalid
= -1;
300 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
301 if (ACPI_FAILURE(status
)) {
302 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
306 pss
= buffer
.pointer
;
307 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
308 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
314 pss
->package
.count
));
316 pr
->performance
->state_count
= pss
->package
.count
;
317 pr
->performance
->states
=
318 kmalloc_array(pss
->package
.count
,
319 sizeof(struct acpi_processor_px
),
321 if (!pr
->performance
->states
) {
326 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
328 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
330 state
.length
= sizeof(struct acpi_processor_px
);
333 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
335 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
337 if (ACPI_FAILURE(status
)) {
338 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
340 kfree(pr
->performance
->states
);
344 amd_fixup_frequency(px
, i
);
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
347 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
349 (u32
) px
->core_frequency
,
351 (u32
) px
->transition_latency
,
352 (u32
) px
->bus_master_latency
,
353 (u32
) px
->control
, (u32
) px
->status
));
356 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
358 if (!px
->core_frequency
||
359 ((u32
)(px
->core_frequency
* 1000) !=
360 (px
->core_frequency
* 1000))) {
361 printk(KERN_ERR FW_BUG PREFIX
362 "Invalid BIOS _PSS frequency found for processor %d: 0x%llx MHz\n",
363 pr
->id
, px
->core_frequency
);
364 if (last_invalid
== -1)
367 if (last_invalid
!= -1) {
369 * Copy this valid entry over last_invalid entry
371 memcpy(&(pr
->performance
->states
[last_invalid
]),
372 px
, sizeof(struct acpi_processor_px
));
378 if (last_invalid
== 0) {
379 printk(KERN_ERR FW_BUG PREFIX
380 "No valid BIOS _PSS frequency found for processor %d\n", pr
->id
);
382 kfree(pr
->performance
->states
);
383 pr
->performance
->states
= NULL
;
386 if (last_invalid
> 0)
387 pr
->performance
->state_count
= last_invalid
;
390 kfree(buffer
.pointer
);
395 int acpi_processor_get_performance_info(struct acpi_processor
*pr
)
399 if (!pr
|| !pr
->performance
|| !pr
->handle
)
402 if (!acpi_has_method(pr
->handle
, "_PCT")) {
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
404 "ACPI-based processor performance control unavailable\n"));
408 result
= acpi_processor_get_performance_control(pr
);
412 result
= acpi_processor_get_performance_states(pr
);
416 /* We need to call _PPC once when cpufreq starts */
418 result
= acpi_processor_get_platform_limit(pr
);
423 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
424 * the BIOS is older than the CPU and does not know its frequencies
428 if (acpi_has_method(pr
->handle
, "_PPC")) {
429 if(boot_cpu_has(X86_FEATURE_EST
))
430 printk(KERN_WARNING FW_BUG
"BIOS needs update for CPU "
431 "frequency support\n");
436 EXPORT_SYMBOL_GPL(acpi_processor_get_performance_info
);
438 int acpi_processor_pstate_control(void)
442 if (!acpi_gbl_FADT
.smi_command
|| !acpi_gbl_FADT
.pstate_control
)
445 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
446 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
447 acpi_gbl_FADT
.pstate_control
, acpi_gbl_FADT
.smi_command
));
449 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
450 (u32
)acpi_gbl_FADT
.pstate_control
, 8);
451 if (ACPI_SUCCESS(status
))
454 ACPI_EXCEPTION((AE_INFO
, status
,
455 "Failed to write pstate_control [0x%x] to smi_command [0x%x]",
456 acpi_gbl_FADT
.pstate_control
, acpi_gbl_FADT
.smi_command
));
460 int acpi_processor_notify_smm(struct module
*calling_module
)
462 static int is_done
= 0;
465 if (!acpi_processor_cpufreq_init
)
468 if (!try_module_get(calling_module
))
471 /* is_done is set to negative if an error occurred,
472 * and to postitive if _no_ error occurred, but SMM
473 * was already notified. This avoids double notification
474 * which might lead to unexpected results...
477 module_put(calling_module
);
479 } else if (is_done
< 0) {
480 module_put(calling_module
);
486 result
= acpi_processor_pstate_control();
488 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No SMI port or pstate_control\n"));
489 module_put(calling_module
);
493 module_put(calling_module
);
497 /* Success. If there's no _PPC, we need to fear nothing, so
498 * we can allow the cpufreq driver to be rmmod'ed. */
501 if (!acpi_processor_ppc_in_use
)
502 module_put(calling_module
);
507 EXPORT_SYMBOL(acpi_processor_notify_smm
);
509 int acpi_processor_get_psd(acpi_handle handle
, struct acpi_psd_package
*pdomain
)
512 acpi_status status
= AE_OK
;
513 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
514 struct acpi_buffer format
= {sizeof("NNNNN"), "NNNNN"};
515 struct acpi_buffer state
= {0, NULL
};
516 union acpi_object
*psd
= NULL
;
518 status
= acpi_evaluate_object(handle
, "_PSD", NULL
, &buffer
);
519 if (ACPI_FAILURE(status
)) {
523 psd
= buffer
.pointer
;
524 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
525 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
530 if (psd
->package
.count
!= 1) {
531 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
536 state
.length
= sizeof(struct acpi_psd_package
);
537 state
.pointer
= pdomain
;
539 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
541 if (ACPI_FAILURE(status
)) {
542 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
547 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
548 printk(KERN_ERR PREFIX
"Unknown _PSD:num_entries\n");
553 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
554 printk(KERN_ERR PREFIX
"Unknown _PSD:revision\n");
559 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
560 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
561 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
562 printk(KERN_ERR PREFIX
"Invalid _PSD:coord_type\n");
567 kfree(buffer
.pointer
);
570 EXPORT_SYMBOL(acpi_processor_get_psd
);
572 int acpi_processor_preregister_performance(
573 struct acpi_processor_performance __percpu
*performance
)
578 cpumask_var_t covered_cpus
;
579 struct acpi_processor
*pr
;
580 struct acpi_psd_package
*pdomain
;
581 struct acpi_processor
*match_pr
;
582 struct acpi_psd_package
*match_pdomain
;
584 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
587 mutex_lock(&performance_mutex
);
590 * Check if another driver has already registered, and abort before
591 * changing pr->performance if it has. Check input data as well.
593 for_each_possible_cpu(i
) {
594 pr
= per_cpu(processors
, i
);
596 /* Look only at processors in ACPI namespace */
600 if (pr
->performance
) {
605 if (!performance
|| !per_cpu_ptr(performance
, i
)) {
611 /* Call _PSD for all CPUs */
612 for_each_possible_cpu(i
) {
613 pr
= per_cpu(processors
, i
);
617 pr
->performance
= per_cpu_ptr(performance
, i
);
618 pdomain
= &(pr
->performance
->domain_info
);
619 if (acpi_processor_get_psd(pr
->handle
, pdomain
)) {
628 * Now that we have _PSD data from all CPUs, lets setup P-state
631 for_each_possible_cpu(i
) {
632 pr
= per_cpu(processors
, i
);
636 if (cpumask_test_cpu(i
, covered_cpus
))
639 pdomain
= &(pr
->performance
->domain_info
);
640 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
641 cpumask_set_cpu(i
, covered_cpus
);
642 if (pdomain
->num_processors
<= 1)
645 /* Validate the Domain info */
646 count_target
= pdomain
->num_processors
;
647 if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ALL
)
648 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
649 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_HW_ALL
)
650 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
651 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ANY
)
652 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ANY
;
654 for_each_possible_cpu(j
) {
658 match_pr
= per_cpu(processors
, j
);
662 match_pdomain
= &(match_pr
->performance
->domain_info
);
663 if (match_pdomain
->domain
!= pdomain
->domain
)
666 /* Here i and j are in the same domain */
668 if (match_pdomain
->num_processors
!= count_target
) {
673 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
678 cpumask_set_cpu(j
, covered_cpus
);
679 cpumask_set_cpu(j
, pr
->performance
->shared_cpu_map
);
682 for_each_possible_cpu(j
) {
686 match_pr
= per_cpu(processors
, j
);
690 match_pdomain
= &(match_pr
->performance
->domain_info
);
691 if (match_pdomain
->domain
!= pdomain
->domain
)
694 match_pr
->performance
->shared_type
=
695 pr
->performance
->shared_type
;
696 cpumask_copy(match_pr
->performance
->shared_cpu_map
,
697 pr
->performance
->shared_cpu_map
);
702 for_each_possible_cpu(i
) {
703 pr
= per_cpu(processors
, i
);
704 if (!pr
|| !pr
->performance
)
707 /* Assume no coordination on any error parsing domain info */
709 cpumask_clear(pr
->performance
->shared_cpu_map
);
710 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
711 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_NONE
;
713 pr
->performance
= NULL
; /* Will be set for real in register */
717 mutex_unlock(&performance_mutex
);
718 free_cpumask_var(covered_cpus
);
721 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
724 acpi_processor_register_performance(struct acpi_processor_performance
725 *performance
, unsigned int cpu
)
727 struct acpi_processor
*pr
;
729 if (!acpi_processor_cpufreq_init
)
732 mutex_lock(&performance_mutex
);
734 pr
= per_cpu(processors
, cpu
);
736 mutex_unlock(&performance_mutex
);
740 if (pr
->performance
) {
741 mutex_unlock(&performance_mutex
);
745 WARN_ON(!performance
);
747 pr
->performance
= performance
;
749 if (acpi_processor_get_performance_info(pr
)) {
750 pr
->performance
= NULL
;
751 mutex_unlock(&performance_mutex
);
755 mutex_unlock(&performance_mutex
);
759 EXPORT_SYMBOL(acpi_processor_register_performance
);
761 void acpi_processor_unregister_performance(unsigned int cpu
)
763 struct acpi_processor
*pr
;
765 mutex_lock(&performance_mutex
);
767 pr
= per_cpu(processors
, cpu
);
769 mutex_unlock(&performance_mutex
);
774 kfree(pr
->performance
->states
);
775 pr
->performance
= NULL
;
777 mutex_unlock(&performance_mutex
);
782 EXPORT_SYMBOL(acpi_processor_unregister_performance
);