cpufreq: introduce a new AMD CPU frequency control mechanism
Subsystems (8)
Huang Rui
maintainerv5 cover cpufreq: introduce a new AMD CPU frequency control mechanism
Hi all,
We would like to introduce a new AMD CPU frequency control mechanism as the "amd-pstate" driver for modern AMD Zen based CPU series in Linux Kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use X86_FEATURE_CPPC feature flag to distinguish the different types.
Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
https://www.amd.com/system/files/TechDocs/56569-A1-PUB.zip
Performance Per Watt (PPW) Calculation:
We use the RAPL interface with "perf" tool to get the energy data of the package power.
The data comparisons between amd-pstate and acpi-freq module are tested on AMD Cezanne processor (mobile CPU):
1) TBench CPU benchmark:
+----------------------------------------------------------------------------------------------+ | | | TBench4 (Performance Per Watt) | | Higher is better | +-------------------+------------------------+------------------------+------------------------+ | | Performance Per Watt | Performance Per Watt | Performance Per Watt | | Kernel Module | (Schedutil) | (Ondemand) | (Performance) | | | Unit: MB / J | Unit: MB / J | Unit: MB / J | +-------------------+------------------------+------------------------+------------------------+ | | | | | | acpi-cpufreq | 48.56 | 48.89 | 47.81 | | | | | | +-------------------+------------------------+------------------------+------------------------+ | | | | | | amd-pstate | 48.38 | 47.38 | 48.77 | | | | | | +-------------------+------------------------+------------------------+------------------------+
Note: The previous data was based on TBench2, as align with Suse, we use TBench4 to re-test it. The PPW is very closed to acpi-cpufreq. And we are still re-runing other tests.
Steam Game Demo on Ryzen 5900X (12 core 24 threads):
The picture to compare acpi-cpufreq vs amd-pstate:
https://drive.google.com/file/d/1PvSduykJn9U5MMOhzFWycnbmGmznalM3/view?usp=sharing
Two videos:
https://drive.google.com/file/d/1nQQEteL-v_zQxnOJpyW8JqvRW2FFDN2Z/view?usp=sharing
https://drive.google.com/file/d/1heuPgFG71SQHvGb6wfedrQciBfE2rhnu/view?usp=sharing
Actually, the amd-pstate driver doesn't change the physical maximum frequency capacity in the processor. But it's able to provide the finer grain performance control range instead of legacy 3 P-States. It has a better performance and power efficiency than before. We will continue optimize amd-pstate function on kernel governors to support different types of processors such as mobile latop, performance desktop, and etc.
See patch series in below git repo:
V1: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v1
V2: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v2
V3: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v3
V4: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v4
V5: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v5
For details introduction, please see the patch 22.
Changes from V1 -> V2:
- cpufreq:
- - Add detailed description in the commit log.
- - Clean up the "extension" postfix in the x86 feature flag.
- - Revise cppc_set_enable helper.
- - Add a fix to check online cpus in cppc_acpi.
- - Use static calls to avoid retpolines.
- - Revise the comment style.
- - Remove amd_pstate_boost_supported() function.
- - Revise the return value in sysfs attribute functions.
- cpupower:
- - Refine the commit log for cpupower patches.
- - Expose a function to get the sysfs value from specific table.
- - Move amd-pstate sysfs definitions and functions into amd helper file.
- - Move the boost init function into amd helper file and explain the details in the commit log.
- - Remove the amd_pstate_get_data in the lib/cpufreq.c to keep the lib as common operations.
- - Move print_speed function into misc helper file.
- - Add amd_pstate_show_perf_and_freq() function in amd helper for cpufreq-info print.
Changes from V2 -> V3:
- cpufreq:
- - Add a patch from Steven to add systemio register in cppc lib. (Thanks to verify the driver in his platform)
- - Update online cpu mask to present cpu.
- - Enhance cppc_set_enable to cover all valid use cases.
- - Add more description in the Kconfig definition.
- - Clean up some redundance functions and data members.
- - Revise amd-pstate trace event prints.
- - Move the amd-pstate traces into power trace system and set the driver as build-in instead of module.
- - Clean up the duplicated sysfs with core cpufreq driver.
- - Revise the amd-pstate RST documentation.
- cpupower:
- - Revise the cpupower_amd_pstate_enabled() function to use cpufreq_get_driver helper instead of read sysfs.
- - Clean up the amd-pstate max/min frequency APIs, because they are actually the same with cpufreq info sysfs.
Changes from V3 -> V4:
- cpufreq:
- - Rebase the whole series to Rafael's pm branch (5.15)
- - Fix the typo in the commit message and comment.
- - Clean up function implementation.
- - Clean up freq&perf sysfs APIs.
- - Fall back to move amd-pstate traces out of power trace system, because it's flexible to debug and fine tune processors with the shared memory solution.
- - Add a kernel param to disable shared memory on amd-pstate, it can be enabled manually.
- cpupower:
- - Introduce acpi cppc library support.
- - Clean up the duplicated amd specific perf/frequency.
We received two issues that reported on the processors with shared memory solution from the community.
First one:
https://lore.kernel.org/linux-pm/[email protected]/
Thanks Giovanni's support and suggestions, we corrected the calculation method and duplicated the performance issue in a 64 cores / 128 threads threadripper which is similiar with EPYC 7713. Check with firmware guy, the sample rate is about 1 ms that the hardware responds the frequency change. We are working to simulate the acpi pstates with cppc api and checking where is the gap with acpi-cpufreq. Will share you the status later.
Second one:
https://lore.kernel.org/linux-pm/[email protected]/
Thanks Matt to help us reproduce this issue in our side with Ryzen 5900X. Below is the video to compare with amd-pstate and acpi-cpufreq on "Control" steam games. The FPS can be increased as well. It is probably something wrong in the SBIOS.
https://lore.kernel.org/linux-pm/DM4PR12MB5136084E5DC1809FBB578075F1959@DM4PR12MB5136.namprd12.prod.outlook.com/
Due to some issues on shared memory solution, so we fallback to build as a module and add a parameter "shared_mem" to disable amd-pstate on processors with shared memory solution for the moment till we complete tunning on this function.
Changes from V4 -> V5:
- cpufreq:
- - Fix subject typo.
- - Fix Clang-13 build warning.
- - Update RST documentation for the latest update.
- cpupower:
- - Fix the table check condition at cpufreq_get_sysfs_value_from_table.
Thanks,
Ray
Huang Rui (19):
x86/cpufeatures: add AMD Collaborative Processor Performance Control
feature flag
x86/msr: add AMD CPPC MSR definitions
cpufreq: amd: introduce a new amd pstate driver to support future
processors
cpufreq: amd: add fast switch function for amd-pstate
cpufreq: amd: introduce the support for the processors with shared
memory solution
cpufreq: amd: add trace for amd-pstate module
cpufreq: amd: add boost mode support for amd-pstate
cpufreq: amd: add amd-pstate frequencies attributes
cpufreq: amd: add amd-pstate performance attributes
cpupower: add AMD P-state capability flag
cpupower: add the function to check amd-pstate enabled
cpupower: initial AMD P-state capability
cpupower: add the function to get the sysfs value from specific table
cpupower: introduce acpi cppc library
cpupower: add amd-pstate sysfs definition and access helper
cpupower: enable boost state support for amd-pstate module
cpupower: move print_speed function into misc helper
cpupower: print amd-pstate information on cpupower
Documentation: amd-pstate: add amd-pstate driver introduction
Jinzhou Su (1):
ACPI: CPPC: add cppc enable register function
Mario Limonciello (1):
ACPI: CPPC: Check present CPUs for determining _CPC is valid
Steven Noonan (1):
ACPI: CPPC: implement support for SystemIO registers
Documentation/admin-guide/acpi/cppc_sysfs.rst | 2 + Documentation/admin-guide/pm/amd-pstate.rst | 383 +++++++++++ .../admin-guide/pm/working-state.rst | 1 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 17 + drivers/acpi/cppc_acpi.c | 93 ++- drivers/cpufreq/Kconfig.x86 | 17 + drivers/cpufreq/Makefile | 5 + drivers/cpufreq/amd-pstate-trace.c | 2 + drivers/cpufreq/amd-pstate-trace.h | 77 +++ drivers/cpufreq/amd-pstate.c | 609 ++++++++++++++++++ include/acpi/cppc_acpi.h | 5 + tools/power/cpupower/Makefile | 6 +- tools/power/cpupower/lib/acpi_cppc.c | 59 ++ tools/power/cpupower/lib/acpi_cppc.h | 21 + tools/power/cpupower/lib/cpufreq.c | 21 +- tools/power/cpupower/lib/cpufreq.h | 12 + tools/power/cpupower/utils/cpufreq-info.c | 68 +- tools/power/cpupower/utils/helpers/amd.c | 76 +++ tools/power/cpupower/utils/helpers/cpuid.c | 13 + tools/power/cpupower/utils/helpers/helpers.h | 22 + tools/power/cpupower/utils/helpers/misc.c | 62 ++ 22 files changed, 1508 insertions(+), 64 deletions(-) create mode 100644 Documentation/admin-guide/pm/amd-pstate.rst create mode 100644 drivers/cpufreq/amd-pstate-trace.c create mode 100644 drivers/cpufreq/amd-pstate-trace.h create mode 100644 drivers/cpufreq/amd-pstate.c create mode 100644 tools/power/cpupower/lib/acpi_cppc.c create mode 100644 tools/power/cpupower/lib/acpi_cppc.h
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 1/22 x86/cpufeatures: add AMD Collaborative Processor Performance Control feature flag
Add Collaborative Processor Performance Control feature flag for AMD processors.
This feature flag will be used on the following amd-pstate driver. The amd-pstate driver has two approaches to implement the frequency control behavior. That depends on the CPU hardware implementation. One is "Full MSR Support" and another is "Shared Memory Support". The feature flag indicates the current processors with "Full MSR Support".
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -315,6 +315,7 @@#define X86_FEATURE_AMD_SSBD (13*32+24)/* "" Speculative Store Bypass Disable */ #define X86_FEATURE_VIRT_SSBD (13*32+25)/* Virtualized Speculative Store Bypass Disable */ #define X86_FEATURE_AMD_SSB_NO (13*32+26)/* "" Speculative Store Bypass is fixed in hardware. */ +#define X86_FEATURE_CPPC (13*32+27)/* Collaborative Processor Performance Control */ /* Thermal and Power Management Leaf, CPUID level 0x00000006 (EAX), word 14 */ #define X86_FEATURE_DTHERM (14*32+ 0)/* Digital Thermal Sensor */
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 2/22 x86/msr: add AMD CPPC MSR definitions
In reply to Huang RuiAMD CPPC (Collaborative Processor Performance Control) function uses MSR registers to manage the performance hints. So add the MSR register macro here.
--- arch/x86/include/asm/msr-index.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
arch/x86/include/asm/msr-index.h +17 -0
@@ -486,6 +486,23 @@#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f +/* AMD Collaborative Processor Performance Control MSRs */ +#define MSR_AMD_CPPC_CAP1 0xc00102b0 +#define MSR_AMD_CPPC_ENABLE 0xc00102b1 +#define MSR_AMD_CPPC_CAP2 0xc00102b2 +#define MSR_AMD_CPPC_REQ 0xc00102b3 +#define MSR_AMD_CPPC_STATUS 0xc00102b4 + +#define CAP1_LOWEST_PERF (x) (((x) >> 0) & 0xff) +#define CAP1_LOWNONLIN_PERF (x) (((x) >> 8) & 0xff) +#define CAP1_NOMINAL_PERF (x) (((x) >> 16) & 0xff) +#define CAP1_HIGHEST_PERF (x) (((x) >> 24) & 0xff) + +#define REQ_MAX_PERF (x) (((x) & 0xff) << 0) +#define REQ_MIN_PERF (x) (((x) & 0xff) << 8) +#define REQ_DES_PERF (x) (((x) & 0xff) << 16) +#define REQ_ENERGY_PERF_PREF (x) (((x) & 0xff) << 24) +/* Fam 17h MSRs */ #define MSR_F17H_IRPERF 0xc00000e9
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 02/22] x86/msr: add AMD CPPC MSR definitions
Quoted 6 lines · Huang Rui
AMD CPPC (Collaborative Processor Performance Control) function uses MSR registers to manage the performance hints. So add the MSR register macro here.
Signed-off-by Huang Rui <[email protected]>
I guess I can take this one if there are no objections from the x86 maintainers, but it would be nice to receive an ACK from one of them.
Quoted 35 lines
--- arch/x86/include/asm/msr-index.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)arch/x86/include/asm/msr-index.h +17
-1@@ -486,6 +486,23 @@#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f +/* AMD Collaborative Processor Performance Control MSRs */ +#define MSR_AMD_CPPC_CAP1 0xc00102b0 +#define MSR_AMD_CPPC_ENABLE 0xc00102b1 +#define MSR_AMD_CPPC_CAP2 0xc00102b2 +#define MSR_AMD_CPPC_REQ 0xc00102b3 +#define MSR_AMD_CPPC_STATUS 0xc00102b4 + +#define CAP1_LOWEST_PERF (x) (((x) >> 0) & 0xff) +#define CAP1_LOWNONLIN_PERF (x) (((x) >> 8) & 0xff) +#define CAP1_NOMINAL_PERF (x) (((x) >> 16) & 0xff) +#define CAP1_HIGHEST_PERF (x) (((x) >> 24) & 0xff) + +#define REQ_MAX_PERF (x) (((x) & 0xff) << 0) +#define REQ_MIN_PERF (x) (((x) & 0xff) << 8) +#define REQ_DES_PERF (x) (((x) & 0xff) << 16) +#define REQ_ENERGY_PERF_PREF (x) (((x) & 0xff) << 24) +/* Fam 17h MSRs */ #define MSR_F17H_IRPERF 0xc00000e9 --2.25.1
Huang Rui
maintainerRe: [PATCH v5 02/22] x86/msr: add AMD CPPC MSR definitions
Quoted 11 lines · Rafael J. Wysocki
Quoted 6 lines · Huang Rui
AMD CPPC (Collaborative Processor Performance Control) function uses MSR registers to manage the performance hints. So add the MSR register macro here.
Signed-off-by Huang Rui <[email protected]>I guess I can take this one if there are no objections from the x86 maintainers, but it would be nice to receive an ACK from one of them.
Hi x86 maintainers, the MSR register descriptions are documented at Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
https://www.amd.com/system/files/TechDocs/56569-A1-PUB.zip
Thanks,
Ray
Quoted 35 lines
Quoted 35 lines
--- arch/x86/include/asm/msr-index.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)arch/x86/include/asm/msr-index.h +17
-1@@ -486,6 +486,23 @@#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f +/* AMD Collaborative Processor Performance Control MSRs */ +#define MSR_AMD_CPPC_CAP1 0xc00102b0 +#define MSR_AMD_CPPC_ENABLE 0xc00102b1 +#define MSR_AMD_CPPC_CAP2 0xc00102b2 +#define MSR_AMD_CPPC_REQ 0xc00102b3 +#define MSR_AMD_CPPC_STATUS 0xc00102b4 + +#define CAP1_LOWEST_PERF (x) (((x) >> 0) & 0xff) +#define CAP1_LOWNONLIN_PERF (x) (((x) >> 8) & 0xff) +#define CAP1_NOMINAL_PERF (x) (((x) >> 16) & 0xff) +#define CAP1_HIGHEST_PERF (x) (((x) >> 24) & 0xff) + +#define REQ_MAX_PERF (x) (((x) & 0xff) << 0) +#define REQ_MIN_PERF (x) (((x) & 0xff) << 8) +#define REQ_DES_PERF (x) (((x) & 0xff) << 16) +#define REQ_ENERGY_PERF_PREF (x) (((x) & 0xff) << 24) +/* Fam 17h MSRs */ #define MSR_F17H_IRPERF 0xc00000e9 --2.25.1
Huang Rui
maintainerPATCH v5 3/22 ACPI: CPPC: implement support for SystemIO registers
In reply to Huang RuiFrom: Steven Noonan <[email protected]>
According to the ACPI v6.2 (and later) specification, SystemIO can be used for _CPC registers. This teaches cppc_acpi how to handle such registers.
This patch was tested using the amd_pstate driver on my Zephyrus G15 (model GA503QS) using the current version 410 BIOS, which uses a SystemIO register for the HighestPerformance element in _CPC.
--- drivers/acpi/cppc_acpi.c | 46 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-)
drivers/acpi/cppc_acpi.c +43 -3
@@ -746,9 +746,24 @@ int acpi_cppc_processor_probe(struct acpi_processor *pr) goto out_free; cpc_ptr->cpc_regs[i-2 ].sys_mem_vaddr = addr; } + }else if (gas_t->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +if (gas_t->access_width <1 || gas_t->access_width >3 ) { +/* 1 = 8-bit, 2 = 16-bit, and 3 = 32-bit. SystemIO doesn't +* implement 64-bit registers. +*/ +pr_debug ("Invalid access width %d for SystemIO register\n" , + gas_t->access_width); + goto out_free; + } +if (gas_t->address & ~0xFFFFULL ) { +/* SystemIO registers use 16-bit integer addresses */ +pr_debug ("Invalid IO port %llu for SystemIO register\n" , + gas_t->address); + goto out_free; + } }else {if (gas_t->space_id !=ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported ()) { -/* Support only PCC ,SYS MEM and FFH type regs */ +/* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */ pr_debug ("Unsupported register type: %d\n" , gas_t->space_id); goto out_free; }
@@ -923,7 +938,20 @@ static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) } *val =0 ; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) + +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_read_port ((acpi_io_address )reg->address, (u32 *)val, width); + +if (status !=AE_OK ) { +pr_debug ("Error: Failed to read SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr;
@@ -962,7 +990,19 @@ static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)int pcc_ss_id =per_cpu (cpu_pcc_subspace_idx, cpu);struct cpc_reg *reg = ®_res->cpc_entry.reg; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_write_port ((acpi_io_address )reg->address, (u32 )val, width); + +if (status !=AE_OK ) { +pr_debug ("Error: Failed to write SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr;
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 03/22] ACPI: CPPC: implement support for SystemIO registers
Quoted 22 lines · Huang Rui
From: Steven Noonan <[email protected]>
According to the ACPI v6.2 (and later) specification, SystemIO can be used for
_CPCregisters. This teachescppc_acpihow to handle such registers.This patch was tested using the
amd_pstatedriver on my Zephyrus G15 (model GA503QS) using the current version 410 BIOS, which uses a SystemIO register for the HighestPerformance element in_CPC.Signed-off-by Steven Noonan <[email protected]>Signed-off-by Huang Rui <[email protected]>--- drivers/acpi/cppc_acpi.c | 46 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-)drivers/acpi/cppc_acpi.c +0
-0
drivers/acpi/cppc_acpi.c int acpi_cppc_processor_probe(struct acpi_processor *pr) · line 751
Quoted 6 lines
drivers/acpi/cppc_acpi.c +3
-0@@ -746,9 +746,24 @@ int acpi_cppc_processor_probe(struct acpi_processor *pr) goto out_free; cpc_ptr->cpc_regs[i-2 ].sys_mem_vaddr = addr; } + }else if (gas_t->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +if (gas_t->access_width <1 || gas_t->access_width >3 ) { +/* 1 = 8-bit, 2 = 16-bit, and 3 = 32-bit. SystemIO doesn't
The comment format is not in agreement with the kernel coding style.
drivers/acpi/cppc_acpi.c int acpi_cppc_processor_probe(struct acpi_processor *pr) · line 758
Quoted 7 lines
drivers/acpi/cppc_acpi.c +7
-0+* implement 64-bit registers. +*/ +pr_debug ("Invalid access width %d for SystemIO register\n" , + gas_t->access_width); + goto out_free; + } +if (gas_t->address & ~0xFFFFULL ) {
It would be good to define a symbol for this mask and use it here instead of the raw vaue.
Quoted 15 lines
drivers/acpi/cppc_acpi.c +6
-1+/* SystemIO registers use 16-bit integer addresses */ +pr_debug ("Invalid IO port %llu for SystemIO register\n" , + gas_t->address); + goto out_free; + } }else {if (gas_t->space_id !=ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported ()) { -/* Support only PCC ,SYS MEM and FFH type regs */ +/* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */ pr_debug ("Unsupported register type: %d\n" , gas_t->space_id); goto out_free; }@@ -923,7 +938,20 @@ static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) }
drivers/acpi/cppc_acpi.c static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) · line 948
Quoted 10 lines
drivers/acpi/cppc_acpi.c +8
-1*val =0 ; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) + +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_read_port ((acpi_io_address )reg->address, (u32 *)val, width); + +if (status !=AE_OK ) {
Please use ACPI_FAILURE() here and in the analogous check and below.
Quoted 31 lines
drivers/acpi/cppc_acpi.c +19
-2+pr_debug ("Error: Failed to read SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr;@@ -962,7 +990,19 @@ static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)int pcc_ss_id =per_cpu (cpu_pcc_subspace_idx, cpu);struct cpc_reg *reg = ®_res->cpc_entry.reg; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_write_port ((acpi_io_address )reg->address, (u32 )val, width); + +if (status !=AE_OK ) { +pr_debug ("Error: Failed to write SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr; --
Huang Rui
maintainerRe: [PATCH v5 03/22] ACPI: CPPC: implement support for SystemIO registers
Quoted 18 lines · Rafael J. Wysocki
Quoted 17 lines · Huang Rui
From: Steven Noonan <[email protected]>
According to the ACPI v6.2 (and later) specification, SystemIO can be used for
_CPCregisters. This teachescppc_acpihow to handle such registers.This patch was tested using the
amd_pstatedriver on my Zephyrus G15 (model GA503QS) using the current version 410 BIOS, which uses a SystemIO register for the HighestPerformance element in_CPC.Signed-off-by Steven Noonan <[email protected]>Signed-off-by Huang Rui <[email protected]>--- drivers/acpi/cppc_acpi.c | 46 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-)
Quoted 13 lines
drivers/acpi/cppc_acpi.c int acpi_cppc_processor_probe(struct acpi_processor *pr) · line 751
Quoted 11 lines
drivers/acpi/cppc_acpi.c +3
-0@@ -746,9 +746,24 @@ int acpi_cppc_processor_probe(struct acpi_processor *pr) goto out_free; cpc_ptr->cpc_regs[i-2 ].sys_mem_vaddr = addr; } + }else if (gas_t->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +if (gas_t->access_width <1 || gas_t->access_width >3 ) { +/* 1 = 8-bit, 2 = 16-bit, and 3 = 32-bit. SystemIO doesn't The comment format is not in agreement with the kernel coding style.
Will update it in V6.
Quoted 11 lines
drivers/acpi/cppc_acpi.c int acpi_cppc_processor_probe(struct acpi_processor *pr) · line 758
Quoted 8 lines
drivers/acpi/cppc_acpi.c +7
-0+* implement 64-bit registers. +*/ +pr_debug ("Invalid access width %d for SystemIO register\n" , + gas_t->access_width); + goto out_free; + } +if (gas_t->address & ~0xFFFFULL ) {It would be good to define a symbol for this mask and use it here instead of the raw vaue.
OK.
Quoted 16 lines
Quoted 16 lines
drivers/acpi/cppc_acpi.c +6
-1+/* SystemIO registers use 16-bit integer addresses */ +pr_debug ("Invalid IO port %llu for SystemIO register\n" , + gas_t->address); + goto out_free; + } }else {if (gas_t->space_id !=ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported ()) { -/* Support only PCC ,SYS MEM and FFH type regs */ +/* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */ pr_debug ("Unsupported register type: %d\n" , gas_t->space_id); goto out_free; }@@ -923,7 +938,20 @@ static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) }
Quoted 12 lines
drivers/acpi/cppc_acpi.c static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val) · line 948
Quoted 10 lines
drivers/acpi/cppc_acpi.c +8
-1*val =0 ; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) + +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_read_port ((acpi_io_address )reg->address, (u32 *)val, width); + +if (status !=AE_OK ) {Please use
ACPI_FAILURE()here and in the analogous check and below.
Fine, I will update this as well.
Thanks,
Ray
Quoted 32 lines
Quoted 32 lines
drivers/acpi/cppc_acpi.c +19
-2+pr_debug ("Error: Failed to read SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr;@@ -962,7 +990,19 @@ static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)int pcc_ss_id =per_cpu (cpu_pcc_subspace_idx, cpu);struct cpc_reg *reg = ®_res->cpc_entry.reg; -if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) +if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_IO ) { +u32 width =8 << (reg->access_width -1 ); +acpi_status status; + + status =acpi_os_write_port ((acpi_io_address )reg->address, (u32 )val, width); + +if (status !=AE_OK ) { +pr_debug ("Error: Failed to write SystemIO port %llx\n" , reg->address); +return -EFAULT ; + } + +return 0 ; + }else if (reg->space_id ==ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >=0 ) vaddr =GET_PCC_VADDR (reg->address, pcc_ss_id);else if (reg->space_id ==ACPI_ADR_SPACE_SYSTEM_MEMORY ) vaddr = reg_res->sys_mem_vaddr; --
Huang Rui
maintainerPATCH v5 4/22 ACPI: CPPC: Check present CPUs for determining _CPC is valid
In reply to Huang RuiFrom: Mario Limonciello <[email protected]>
As this is a static check, it should be based upon what is currently present on the system. This makes probeing more deterministic.
While local APIC flags field (lapic_flags) of cpu core in MADT table is 0, then the cpu core won't be enabled. In this case, _CPC won't be found in this core, and return back to _CPC invalid with walking through possible cpus (include disable cpus). This is not expected, so switch to check present CPUs instead.
--- drivers/acpi/cppc_acpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/cppc_acpi.c +1 -1
@@ -411,7 +411,7 @@ bool acpi_cpc_valid(void)struct cpc_desc *cpc_ptr;int cpu; -for_each_possible_cpu (cpu) { +for_each_present_cpu (cpu) { cpc_ptr =per_cpu (cpc_desc_ptr, cpu);if (!cpc_ptr)return false;
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 5/22 ACPI: CPPC: add cppc enable register function
In reply to Huang RuiFrom: Jinzhou Su <[email protected]>
Add a new function to enable CPPC feature. This function will write Continuous Performance Control package EnableRegister field on the processor.
CPPC EnableRegister register described in section 8.4.7.1 of ACPI 6.4:
This element is optional. If supported, contains a resource descriptor with a single Register() descriptor that describes a register to which OSPM writes a One to enable CPPC on this processor. Before this register is set, the processor will be controlled by legacy mechanisms (ACPI Pstates, firmware, etc.).
This register will be used for AMD processors to enable amd-pstate function instead of legacy ACPI P-States.
--- drivers/acpi/cppc_acpi.c | 45 ++++++++++++++++++++++++++++++++++++++++ include/acpi/cppc_acpi.h | 5 +++++ 2 files changed, 50 insertions(+)
drivers/acpi/cppc_acpi.c +45 -0
@@ -1262,6 +1262,51 @@ int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs) }EXPORT_SYMBOL_GPL (cppc_get_perf_ctrs); +/** +* cppc_set_enable - Set to enable CPPC on the processor by writing the +* Continuous Performance Control package EnableRegister field. +* @cpu: CPU for which to enable CPPC register. +* @enable: 0 - disable, 1 - enable CPPC feature on the processor. +* +* Return: 0 for success, -ERRNO or -EIO otherwise. +*/ +int cppc_set_enable (int cpu,bool enable) +{ +int pcc_ss_id =per_cpu (cpu_pcc_subspace_idx, cpu); +struct cpc_register_resource *enable_reg; +struct cpc_desc *cpc_desc =per_cpu (cpc_desc_ptr, cpu); +struct cppc_pcc_data *pcc_ss_data =NULL ; +int ret = -EINVAL ; + +if (!cpc_desc) { +pr_debug ("No CPC descriptor for CPU:%d\n" , cpu); +return -EINVAL ; + } + + enable_reg = &cpc_desc->cpc_regs[ENABLE ]; + +if (CPC_IN_PCC (enable_reg)) { + +if (pcc_ss_id <0 ) +return -EIO ; + + ret =cpc_write (cpu, enable_reg, enable); +if (ret) +return ret; + + pcc_ss_data = pcc_data[pcc_ss_id]; + +down_write (&pcc_ss_data->pcc_lock); +/* after writing CPC, transfer the ownership of PCC to platfrom */ + ret =send_pcc_cmd (pcc_ss_id,CMD_WRITE ); +up_write (&pcc_ss_data->pcc_lock); +return ret; + } + +return cpc_write (cpu, enable_reg, enable); +} +EXPORT_SYMBOL_GPL (cppc_set_enable); +/** * cppc_set_perf - Set a CPU's performance controls. * @cpu: CPU for which to set performance controls.
include/acpi/cppc_acpi.h +5 -0
@@ -138,6 +138,7 @@ extern int cppc_get_desired_perf(int cpunum, u64 *desired_perf);extern int cppc_get_nominal_perf (int cpunum,u64 *nominal_perf);extern int cppc_get_perf_ctrs (int cpu,struct cppc_perf_fb_ctrs *perf_fb_ctrs);extern int cppc_set_perf (int cpu,struct cppc_perf_ctrls *perf_ctrls); +extern int cppc_set_enable (int cpu,bool enable);extern int cppc_get_perf_caps (int cpu,struct cppc_perf_caps *caps);extern bool acpi_cpc_valid (void );extern int acpi_get_psd_map (unsigned int cpu,struct cppc_cpudata *cpu_data);
@@ -162,6 +163,10 @@ static inline int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls) {return -ENOTSUPP ; } +static inline int cppc_set_enable (int cpu,bool enable) +{ +return -ENOTSUPP ; +}static inline int cppc_get_perf_caps (int cpu,struct cppc_perf_caps *caps) {return -ENOTSUPP ;
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 6/22 cpufreq: amd: introduce a new amd pstate driver to support future processors
In reply to Huang Ruiamd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on AMD Zen based CPU series in Linux kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use X86_FEATURE_CPPC feature flag to distinguish the different types.
Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
https://www.amd.com/system/files/TechDocs/56569-A1-PUB.zip
--- drivers/cpufreq/Kconfig.x86 | 17 ++ drivers/cpufreq/Makefile | 1 + drivers/cpufreq/amd-pstate.c | 398 +++++++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+) create mode 100644 drivers/cpufreq/amd-pstate.c
drivers/cpufreq/Kconfig.x86 +17 -0
@@ -34,6 +34,23 @@ config X86_PCC_CPUFREQ If in doubt, say N. +config X86_AMD_PSTATE + tristate "AMD Processor P-State driver" + depends on X86 + select ACPI_PROCESSOR if ACPI + select ACPI_CPPC_LIB if X86_64 && ACPI + select CPU_FREQ_GOV_SCHEDUTIL if SMP + help + This driver adds a CPUFreq driver which utilizes a fine grain + processor performance frequency control range instead of legacy + performance levels. This driver supports the AMD processors with + _CPC object in the SBIOS. + + For details, take a look at: + <file:Documentation/admin-guide/pm/amd-pstate.rst>. + + If in doubt, say N. + config X86_ACPI_CPUFREQ tristate "ACPI Processor P-States driver" depends on ACPI_PROCESSOR
drivers/cpufreq/Makefile +1 -0
@@ -25,6 +25,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.o
drivers/cpufreq/amd-pstate.c new file +398 -0
398 lines not on this page · raw message
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 06/22] cpufreq: amd: introduce a new amd pstate driver to support future processors
Quoted 50 lines · Huang Rui
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on AMD Zen based CPU series in Linux kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use
X86_FEATURE_CPPCfeature flag to distinguish the different types.Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
https://www.amd.com/system/files/TechDocs/56569-A1-PUB.zip
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Kconfig.x86 | 17 ++ drivers/cpufreq/Makefile | 1 + drivers/cpufreq/amd-pstate.c | 398 +++++++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+) create mode 100644 drivers/cpufreq/amd-pstate.cdrivers/cpufreq/Kconfig.x86 +1
-0@@ -34,6 +34,23 @@ config X86_PCC_CPUFREQ If in doubt, say N. +config X86_AMD_PSTATE
drivers/cpufreq/Kconfig.x86 config X86_PCC_CPUFREQ · line 47
Quoted 10 lines
drivers/cpufreq/Kconfig.x86 +10
-0+ tristate "AMD Processor P-State driver" + depends on X86 + select ACPI_PROCESSOR if ACPI + select ACPI_CPPC_LIB if X86_64 && ACPI + select CPU_FREQ_GOV_SCHEDUTIL if SMP + help + This driver adds a CPUFreq driver which utilizes a fine grain + processor performance frequency control range instead of legacy + performance levels. This driver supports the AMD processors with + _CPC object in the SBIOS.
Well, _CPC needs to be present in the ACPI tables of the system.
Quoted 27 lines
drivers/cpufreq/Kconfig.x86 +6
-0+ + For details, take a look at: + <file:Documentation/admin-guide/pm/amd-pstate.rst>. + + If in doubt, say N. + config X86_ACPI_CPUFREQ tristate "ACPI Processor P-States driver" depends on ACPI_PROCESSORdrivers/cpufreq/Makefile +1
-0@@ -25,6 +25,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate.c new file +0
-0
drivers/cpufreq/amd-pstate.c line 7
Quoted 7 lines
drivers/cpufreq/amd-pstate.c new file +7
-0@@ -0,0 +1,398 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* amd-pstate.c - AMD Processor P-state Frequency Driver +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]>
Given the lack of other documentation, it would be nice to provide at least a brief description of the driver here.
Quoted 39 lines
drivers/cpufreq/amd-pstate.c new file +39
-0+*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/smp.h> +#include <linux/sched.h> +#include <linux/cpufreq.h> +#include <linux/compiler.h> +#include <linux/dmi.h> +#include <linux/slab.h> +#include <linux/acpi.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/uaccess.h> +#include <linux/static_call.h> + +#include <acpi/processor.h> +#include <acpi/cppc_acpi.h> + +#include <asm/msr.h> +#include <asm/processor.h> +#include <asm/cpufeature.h> +#include <asm/cpu_device_id.h> + +#define AMD_PSTATE_TRANSITION_LATENCY 0x20000 +#define AMD_PSTATE_TRANSITION_DELAY 500 + +static struct cpufreq_driver amd_pstate_driver; + +struct amd_cpudata { +int cpu; + +struct freq_qos_request req[2 ]; + +u64 cppc_req_cached; +
drivers/cpufreq/amd-pstate.c line 56
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+u32 highest_perf; +u32 nominal_perf; +u32 lowest_nonlinear_perf; +u32 lowest_perf; + +u32 max_freq; +u32 min_freq; +u32 nominal_freq; +u32 lowest_nonlinear_freq; +};
Please describe the fields of this structure, preferably in a kerneldoc comment.
drivers/cpufreq/amd-pstate.c line 63
Quoted 7 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int pstate_enable (bool enable) +{ +return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); +} + +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);
Why do you need a static call here?
It doesn't get updated anywhere below.
Quoted 20 lines
drivers/cpufreq/amd-pstate.c new file +20
-0+ +static inline int amd_pstate_enable (bool enable) +{ +return static_call (amd_pstate_enable)(enable); +} + +static int pstate_init_perf (struct amd_cpudata *cpudata) +{ +u64 cap1; + +int ret =rdmsrl_safe_on_cpu (cpudata->cpu,MSR_AMD_CPPC_CAP1 , + &cap1); +if (ret) +return ret; + +/* +* TODO: Introduce AMD specific power feature. +* +* CPPC entry doesn't indicate the highest performance in some ASICs. +*/
drivers/cpufreq/amd-pstate.c line 93
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf,CAP1_NOMINAL_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_nonlinear_perf,CAP1_LOWNONLIN_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_perf,CAP1_LOWEST_PERF (cap1)); + +return 0 ; +} + +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);
And same here.
Quoted 7 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata) +{ +return static_call (amd_pstate_init_perf)(cpudata); +} + +static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
drivers/cpufreq/amd-pstate.c line 110
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+ u32 des_perf, u32 max_perf, bool fast_switch) +{ + if (fast_switch) + wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached)); + else + wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, + READ_ONCE(cpudata->cppc_req_cached)); +} + +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf);
Same here.
Quoted 174 lines
drivers/cpufreq/amd-pstate.c new file +174
-0+ +static inline void amd_pstate_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +static_call (amd_pstate_update_perf)(cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static void amd_pstate_update (struct amd_cpudata *cpudata,u32 min_perf, +u32 des_perf,u32 max_perf,bool fast_switch) +{ +u64 prev =READ_ONCE (cpudata->cppc_req_cached); +u64 value = prev; + + value &= ~REQ_MIN_PERF (~0L ); + value |=REQ_MIN_PERF (min_perf); + + value &= ~REQ_DES_PERF (~0L ); + value |=REQ_DES_PERF (des_perf); + + value &= ~REQ_MAX_PERF (~0L ); + value |=REQ_MAX_PERF (max_perf); + +if (value == prev) +return ; + +WRITE_ONCE (cpudata->cppc_req_cached, value); + +amd_pstate_update_perf (cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static int amd_pstate_verify (struct cpufreq_policy_data *policy) +{ +cpufreq_verify_within_cpu_limits (policy); + +return 0 ; +} + +static int amd_pstate_target (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +struct cpufreq_freqs freqs; +struct amd_cpudata *cpudata = policy->driver_data; +unsigned long max_perf, min_perf, des_perf, cap_perf; + +if (!cpudata->max_freq) +return -ENODEV ; + + cap_perf =READ_ONCE (cpudata->highest_perf); + min_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); + max_perf = cap_perf; + + freqs.old = policy->cur; + freqs.new = target_freq; + + des_perf =DIV_ROUND_CLOSEST (target_freq * cap_perf, + cpudata->max_freq); + +cpufreq_freq_transition_begin (policy, &freqs); +amd_pstate_update (cpudata, min_perf, des_perf, + max_perf, false); +cpufreq_freq_transition_end (policy, &freqs, false); + +return 0 ; +} + +static int amd_get_min_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.lowest_freq *1000 ; +} + +static int amd_get_max_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 max_perf, max_freq, nominal_freq, nominal_perf; +u64 boost_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + max_perf =READ_ONCE (cpudata->highest_perf); + + boost_ratio =div_u64 (max_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + max_freq = nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return max_freq *1000 ; +} + +static int amd_get_nominal_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.nominal_freq *1000 ; +} + +static int amd_get_lowest_nonlinear_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 lowest_nonlinear_freq, lowest_nonlinear_perf, + nominal_freq, nominal_perf; +u64 lowest_nonlinear_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + + lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf; + + lowest_nonlinear_ratio =div_u64 (lowest_nonlinear_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return lowest_nonlinear_freq *1000 ; +} + +static int amd_pstate_cpu_init (struct cpufreq_policy *policy) +{ +int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret; +struct device *dev; +struct amd_cpudata *cpudata; + + dev =get_cpu_device (policy->cpu); +if (!dev) +return -ENODEV ; + + cpudata =kzalloc (sizeof (*cpudata),GFP_KERNEL ); +if (!cpudata) +return -ENOMEM ; + + cpudata->cpu = policy->cpu; + + ret =amd_pstate_init_perf (cpudata); +if (ret) + goto free_cpudata1; + + min_freq =amd_get_min_freq (cpudata); + max_freq =amd_get_max_freq (cpudata); + nominal_freq =amd_get_nominal_freq (cpudata); + lowest_nonlinear_freq =amd_get_lowest_nonlinear_freq (cpudata); + +if (min_freq <0 || max_freq <0 || min_freq > max_freq) { +dev_err (dev,"min_freq(%d) or max_freq(%d) value is incorrect\n" , + min_freq, max_freq); + ret = -EINVAL ; + goto free_cpudata1; + } + + policy->cpuinfo.transition_latency =AMD_PSTATE_TRANSITION_LATENCY ;
drivers/cpufreq/amd-pstate.c line 294
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+ policy->transition_delay_us =AMD_PSTATE_TRANSITION_DELAY ; + + policy->min = min_freq; + policy->max = max_freq; + + policy->cpuinfo.min_freq = min_freq; + policy->cpuinfo.max_freq = max_freq; + +/* It will be updated by governor */ + policy->cur = policy->cpuinfo.min_freq;
The freq_qos requests below are never updated in this file. What are they for?
Quoted 107 lines
drivers/cpufreq/amd-pstate.c new file +104
-1+ + ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ], +FREQ_QOS_MIN , policy->cpuinfo.min_freq); +if (ret <0 ) { +dev_err (dev,"Failed to add min-freq constraint (%d)\n" , ret); + goto free_cpudata1; + } + + ret =freq_qos_add_request (&policy->constraints, &cpudata->req[1 ], +FREQ_QOS_MAX , policy->cpuinfo.max_freq); +if (ret <0 ) { +dev_err (dev,"Failed to add max-freq constraint (%d)\n" , ret); + goto free_cpudata2; + } + +/* Initial processor data capability frequencies */ + cpudata->max_freq = max_freq; + cpudata->min_freq = min_freq; + cpudata->nominal_freq = nominal_freq; + cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq; + + policy->driver_data = cpudata; + +return 0 ; + +free_cpudata2: +freq_qos_remove_request (&cpudata->req[0 ]); +free_cpudata1: +kfree (cpudata); +return ret; +} + +static int amd_pstate_cpu_exit (struct cpufreq_policy *policy) +{ +struct amd_cpudata *cpudata; + + cpudata = policy->driver_data; + +freq_qos_remove_request (&cpudata->req[1 ]); +freq_qos_remove_request (&cpudata->req[0 ]); +kfree (cpudata); + +return 0 ; +} + +static struct cpufreq_driver amd_pstate_driver = { + .flags =CPUFREQ_CONST_LOOPS |CPUFREQ_NEED_UPDATE_LIMITS , + .verify = amd_pstate_verify, + .target = amd_pstate_target, + .init = amd_pstate_cpu_init, + .exit = amd_pstate_cpu_exit, + .name ="amd-pstate" , +}; + +static int __init amd_pstate_init (void ) +{ +int ret; + +if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD ) +return -ENODEV ; + +if (!acpi_cpc_valid ()) { +pr_debug ("the _CPC object is not present in SBIOS\n" ); +return -ENODEV ; + } + +/* don't keep reloading if cpufreq_driver exists */ +if (cpufreq_get_current_driver ()) +return -EEXIST ; + +/* capability check */ +if (!boot_cpu_has (X86_FEATURE_CPPC )) { +pr_debug ("AMD CPPC MSR based functionality is not supported\n" ); +return -ENODEV ; + } + +/* enable amd pstate feature */ + ret =amd_pstate_enable (true); +if (ret) { +pr_err ("failed to enable amd-pstate with return %d\n" , ret); +return ret; + } + + ret =cpufreq_register_driver (&amd_pstate_driver); +if (ret) +pr_err ("failed to register amd_pstate_driver with return %d\n" , + ret); + +return ret; +} + +static void __exit amd_pstate_exit(void) +{ +cpufreq_unregister_driver (&amd_pstate_driver); + +amd_pstate_enable (false); +} + +module_init (amd_pstate_init); +module_exit (amd_pstate_exit); + +MODULE_AUTHOR ("Huang Rui <[email protected]>" ); +MODULE_DESCRIPTION ("AMD Processor P-state Frequency Driver" ); +MODULE_LICENSE ("GPL" ); --2.25.1
Huang Rui
maintainerRe: [PATCH v5 06/22] cpufreq: amd: introduce a new amd pstate driver to support future processors
Quoted 50 lines · Rafael J. Wysocki
Quoted 49 lines · Huang Rui
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on AMD Zen based CPU series in Linux kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use
X86_FEATURE_CPPCfeature flag to distinguish the different types.Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Kconfig.x86 | 17 ++ drivers/cpufreq/Makefile | 1 + drivers/cpufreq/amd-pstate.c | 398 +++++++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+) create mode 100644 drivers/cpufreq/amd-pstate.cdrivers/cpufreq/Kconfig.x86 +0
-0@@ -34,6 +34,23 @@ config X86_PCC_CPUFREQ If in doubt, say N.
Quoted 13 lines
drivers/cpufreq/Kconfig.x86 config X86_PCC_CPUFREQ · line 47
Quoted 11 lines
drivers/cpufreq/Kconfig.x86 +11
-0+config X86_AMD_PSTATE + tristate "AMD Processor P-State driver" + depends on X86 + select ACPI_PROCESSOR if ACPI + select ACPI_CPPC_LIB if X86_64 && ACPI + select CPU_FREQ_GOV_SCHEDUTIL if SMP + help + This driver adds a CPUFreq driver which utilizes a fine grain + processor performance frequency control range instead of legacy + performance levels. This driver supports the AMD processors with + _CPC object in the SBIOS.Well,
_CPCneeds to be present in the ACPI tables of the system.
Yes, will modify the words.
Quoted 30 lines
Quoted 30 lines
drivers/cpufreq/Kconfig.x86 +6
-0+ + For details, take a look at: + <file:Documentation/admin-guide/pm/amd-pstate.rst>. + + If in doubt, say N. + config X86_ACPI_CPUFREQ tristate "ACPI Processor P-States driver" depends on ACPI_PROCESSORdrivers/cpufreq/Makefile +1
-0@@ -25,6 +25,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate.c new file +2
-0@@ -0,0 +1,398 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/*
Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 7
Quoted 5 lines
drivers/cpufreq/amd-pstate.c new file +5
-0+* amd-pstate.c - AMD Processor P-state Frequency Driver +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]> Given the lack of other documentation, it would be nice to provide at least a brief description of the driver here.
Fine, will add the description in V6.
Quoted 44 lines
Quoted 44 lines
drivers/cpufreq/amd-pstate.c new file +43
-0+*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/smp.h> +#include <linux/sched.h> +#include <linux/cpufreq.h> +#include <linux/compiler.h> +#include <linux/dmi.h> +#include <linux/slab.h> +#include <linux/acpi.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/uaccess.h> +#include <linux/static_call.h> + +#include <acpi/processor.h> +#include <acpi/cppc_acpi.h> + +#include <asm/msr.h> +#include <asm/processor.h> +#include <asm/cpufeature.h> +#include <asm/cpu_device_id.h> + +#define AMD_PSTATE_TRANSITION_LATENCY 0x20000 +#define AMD_PSTATE_TRANSITION_DELAY 500 + +static struct cpufreq_driver amd_pstate_driver; + +struct amd_cpudata { +int cpu; + +struct freq_qos_request req[2 ]; + +u64 cppc_req_cached; + +u32 highest_perf; +u32 nominal_perf; +u32 lowest_nonlinear_perf; +u32 lowest_perf;
Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 56
Quoted 6 lines
drivers/cpufreq/amd-pstate.c new file +6
-0+ +u32 max_freq; +u32 min_freq; +u32 nominal_freq; +u32 lowest_nonlinear_freq; +};Please describe the fields of this structure, preferably in a kerneldoc comment.
OK.
Quoted 11 lines
drivers/cpufreq/amd-pstate.c line 63
Quoted 8 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int pstate_enable (bool enable) +{ +return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); +} + +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);Why do you need a static call here?
Quoted 1 line
It doesn't get updated anywhere below.
Ah, the update behavior is defined in the following patch 8 (shared memory support). Sorry to make you confused.
Quoted 232 lines
Quoted 21 lines
drivers/cpufreq/amd-pstate.c new file +20
-0+ +static inline int amd_pstate_enable (bool enable) +{ +return static_call (amd_pstate_enable)(enable); +} + +static int pstate_init_perf (struct amd_cpudata *cpudata) +{ +u64 cap1; + +int ret =rdmsrl_safe_on_cpu (cpudata->cpu,MSR_AMD_CPPC_CAP1 , + &cap1); +if (ret) +return ret; + +/* +* TODO: Introduce AMD specific power feature. +* +* CPPC entry doesn't indicate the highest performance in some ASICs. +*/ drivers/cpufreq/amd-pstate.c line 93
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf,CAP1_NOMINAL_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_nonlinear_perf,CAP1_LOWNONLIN_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_perf,CAP1_LOWEST_PERF (cap1)); + +return 0 ; +} + +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);And same here.
Quoted 7 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata) +{ +return static_call (amd_pstate_init_perf)(cpudata); +} + +static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,drivers/cpufreq/amd-pstate.c line 110
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+ u32 des_perf, u32 max_perf, bool fast_switch) +{ + if (fast_switch) + wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached)); + else + wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, + READ_ONCE(cpudata->cppc_req_cached)); +} + +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf);Same here.
Quoted 178 lines
drivers/cpufreq/amd-pstate.c new file +178
-0+ +static inline void amd_pstate_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +static_call (amd_pstate_update_perf)(cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static void amd_pstate_update (struct amd_cpudata *cpudata,u32 min_perf, +u32 des_perf,u32 max_perf,bool fast_switch) +{ +u64 prev =READ_ONCE (cpudata->cppc_req_cached); +u64 value = prev; + + value &= ~REQ_MIN_PERF (~0L ); + value |=REQ_MIN_PERF (min_perf); + + value &= ~REQ_DES_PERF (~0L ); + value |=REQ_DES_PERF (des_perf); + + value &= ~REQ_MAX_PERF (~0L ); + value |=REQ_MAX_PERF (max_perf); + +if (value == prev) +return ; + +WRITE_ONCE (cpudata->cppc_req_cached, value); + +amd_pstate_update_perf (cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static int amd_pstate_verify (struct cpufreq_policy_data *policy) +{ +cpufreq_verify_within_cpu_limits (policy); + +return 0 ; +} + +static int amd_pstate_target (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +struct cpufreq_freqs freqs; +struct amd_cpudata *cpudata = policy->driver_data; +unsigned long max_perf, min_perf, des_perf, cap_perf; + +if (!cpudata->max_freq) +return -ENODEV ; + + cap_perf =READ_ONCE (cpudata->highest_perf); + min_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); + max_perf = cap_perf; + + freqs.old = policy->cur; + freqs.new = target_freq; + + des_perf =DIV_ROUND_CLOSEST (target_freq * cap_perf, + cpudata->max_freq); + +cpufreq_freq_transition_begin (policy, &freqs); +amd_pstate_update (cpudata, min_perf, des_perf, + max_perf, false); +cpufreq_freq_transition_end (policy, &freqs, false); + +return 0 ; +} + +static int amd_get_min_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.lowest_freq *1000 ; +} + +static int amd_get_max_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 max_perf, max_freq, nominal_freq, nominal_perf; +u64 boost_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + max_perf =READ_ONCE (cpudata->highest_perf); + + boost_ratio =div_u64 (max_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + max_freq = nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return max_freq *1000 ; +} + +static int amd_get_nominal_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.nominal_freq *1000 ; +} + +static int amd_get_lowest_nonlinear_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 lowest_nonlinear_freq, lowest_nonlinear_perf, + nominal_freq, nominal_perf; +u64 lowest_nonlinear_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + + lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf; + + lowest_nonlinear_ratio =div_u64 (lowest_nonlinear_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return lowest_nonlinear_freq *1000 ; +} + +static int amd_pstate_cpu_init (struct cpufreq_policy *policy) +{ +int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret; +struct device *dev; +struct amd_cpudata *cpudata; + + dev =get_cpu_device (policy->cpu); +if (!dev) +return -ENODEV ; + + cpudata =kzalloc (sizeof (*cpudata),GFP_KERNEL ); +if (!cpudata) +return -ENOMEM ; + + cpudata->cpu = policy->cpu; + + ret =amd_pstate_init_perf (cpudata); +if (ret) + goto free_cpudata1; + + min_freq =amd_get_min_freq (cpudata); + max_freq =amd_get_max_freq (cpudata); + nominal_freq =amd_get_nominal_freq (cpudata); + lowest_nonlinear_freq =amd_get_lowest_nonlinear_freq (cpudata); + +if (min_freq <0 || max_freq <0 || min_freq > max_freq) { +dev_err (dev,"min_freq(%d) or max_freq(%d) value is incorrect\n" , + min_freq, max_freq); + ret = -EINVAL ; + goto free_cpudata1; + } + + policy->cpuinfo.transition_latency =AMD_PSTATE_TRANSITION_LATENCY ; + policy->transition_delay_us =AMD_PSTATE_TRANSITION_DELAY ; + + policy->min = min_freq; + policy->max = max_freq;
Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 294
Quoted 6 lines
drivers/cpufreq/amd-pstate.c new file +6
-0+ + policy->cpuinfo.min_freq = min_freq; + policy->cpuinfo.max_freq = max_freq; + +/* It will be updated by governor */ + policy->cur = policy->cpuinfo.min_freq;The
freq_qosrequests below are never updated in this file. What are they for?
The freq_qos requests will be updated while we set boost disabled or enabled in patch 10. Sorry to make you confused again. Should I move the freq_qos requests implementation into patch 10 as well?
Thanks,
Ray
Quoted 108 lines
Quoted 108 lines
drivers/cpufreq/amd-pstate.c new file +104
-1+ + ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ], +FREQ_QOS_MIN , policy->cpuinfo.min_freq); +if (ret <0 ) { +dev_err (dev,"Failed to add min-freq constraint (%d)\n" , ret); + goto free_cpudata1; + } + + ret =freq_qos_add_request (&policy->constraints, &cpudata->req[1 ], +FREQ_QOS_MAX , policy->cpuinfo.max_freq); +if (ret <0 ) { +dev_err (dev,"Failed to add max-freq constraint (%d)\n" , ret); + goto free_cpudata2; + } + +/* Initial processor data capability frequencies */ + cpudata->max_freq = max_freq; + cpudata->min_freq = min_freq; + cpudata->nominal_freq = nominal_freq; + cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq; + + policy->driver_data = cpudata; + +return 0 ; + +free_cpudata2: +freq_qos_remove_request (&cpudata->req[0 ]); +free_cpudata1: +kfree (cpudata); +return ret; +} + +static int amd_pstate_cpu_exit (struct cpufreq_policy *policy) +{ +struct amd_cpudata *cpudata; + + cpudata = policy->driver_data; + +freq_qos_remove_request (&cpudata->req[1 ]); +freq_qos_remove_request (&cpudata->req[0 ]); +kfree (cpudata); + +return 0 ; +} + +static struct cpufreq_driver amd_pstate_driver = { + .flags =CPUFREQ_CONST_LOOPS |CPUFREQ_NEED_UPDATE_LIMITS , + .verify = amd_pstate_verify, + .target = amd_pstate_target, + .init = amd_pstate_cpu_init, + .exit = amd_pstate_cpu_exit, + .name ="amd-pstate" , +}; + +static int __init amd_pstate_init (void ) +{ +int ret; + +if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD ) +return -ENODEV ; + +if (!acpi_cpc_valid ()) { +pr_debug ("the _CPC object is not present in SBIOS\n" ); +return -ENODEV ; + } + +/* don't keep reloading if cpufreq_driver exists */ +if (cpufreq_get_current_driver ()) +return -EEXIST ; + +/* capability check */ +if (!boot_cpu_has (X86_FEATURE_CPPC )) { +pr_debug ("AMD CPPC MSR based functionality is not supported\n" ); +return -ENODEV ; + } + +/* enable amd pstate feature */ + ret =amd_pstate_enable (true); +if (ret) { +pr_err ("failed to enable amd-pstate with return %d\n" , ret); +return ret; + } + + ret =cpufreq_register_driver (&amd_pstate_driver); +if (ret) +pr_err ("failed to register amd_pstate_driver with return %d\n" , + ret); + +return ret; +} + +static void __exit amd_pstate_exit(void) +{ +cpufreq_unregister_driver (&amd_pstate_driver); + +amd_pstate_enable (false); +} + +module_init (amd_pstate_init); +module_exit (amd_pstate_exit); + +MODULE_AUTHOR ("Huang Rui <[email protected]>" ); +MODULE_DESCRIPTION ("AMD Processor P-state Frequency Driver" ); +MODULE_LICENSE ("GPL" ); --2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 06/22] cpufreq: amd: introduce a new amd pstate driver to support future processors
Quoted 421 lines · Huang Rui
Quoted 50 lines · Rafael J. Wysocki
Quoted 49 lines · Huang Rui
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on AMD Zen based CPU series in Linux kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use
X86_FEATURE_CPPCfeature flag to distinguish the different types.Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Kconfig.x86 | 17 ++ drivers/cpufreq/Makefile | 1 + drivers/cpufreq/amd-pstate.c | 398 +++++++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+) create mode 100644 drivers/cpufreq/amd-pstate.cdrivers/cpufreq/Kconfig.x86 +0
-0@@ -34,6 +34,23 @@ config X86_PCC_CPUFREQ If in doubt, say N.Quoted 13 lines
drivers/cpufreq/Kconfig.x86 config X86_PCC_CPUFREQ · line 47
Quoted 11 lines
drivers/cpufreq/Kconfig.x86 +11
-0+config X86_AMD_PSTATE + tristate "AMD Processor P-State driver" + depends on X86 + select ACPI_PROCESSOR if ACPI + select ACPI_CPPC_LIB if X86_64 && ACPI + select CPU_FREQ_GOV_SCHEDUTIL if SMP + help + This driver adds a CPUFreq driver which utilizes a fine grain + processor performance frequency control range instead of legacy + performance levels. This driver supports the AMD processors with + _CPC object in the SBIOS.Well,
_CPCneeds to be present in the ACPI tables of the system.Yes, will modify the words.
Quoted 30 lines
Quoted 30 lines
drivers/cpufreq/Kconfig.x86 +6
-0+ + For details, take a look at: + <file:Documentation/admin-guide/pm/amd-pstate.rst>. + + If in doubt, say N. + config X86_ACPI_CPUFREQ tristate "ACPI Processor P-States driver" depends on ACPI_PROCESSORdrivers/cpufreq/Makefile +1
-0@@ -25,6 +25,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate.c new file +2
-0@@ -0,0 +1,398 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 7
Quoted 5 lines
drivers/cpufreq/amd-pstate.c new file +5
-0+* amd-pstate.c - AMD Processor P-state Frequency Driver +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]> Given the lack of other documentation, it would be nice to provide at least a brief description of the driver here.
Fine, will add the description in V6.
Quoted 44 lines
Quoted 44 lines
drivers/cpufreq/amd-pstate.c new file +43
-0+*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/smp.h> +#include <linux/sched.h> +#include <linux/cpufreq.h> +#include <linux/compiler.h> +#include <linux/dmi.h> +#include <linux/slab.h> +#include <linux/acpi.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/uaccess.h> +#include <linux/static_call.h> + +#include <acpi/processor.h> +#include <acpi/cppc_acpi.h> + +#include <asm/msr.h> +#include <asm/processor.h> +#include <asm/cpufeature.h> +#include <asm/cpu_device_id.h> + +#define AMD_PSTATE_TRANSITION_LATENCY 0x20000 +#define AMD_PSTATE_TRANSITION_DELAY 500 + +static struct cpufreq_driver amd_pstate_driver; + +struct amd_cpudata { +int cpu; + +struct freq_qos_request req[2 ]; + +u64 cppc_req_cached; + +u32 highest_perf; +u32 nominal_perf; +u32 lowest_nonlinear_perf; +u32 lowest_perf;Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 56
Quoted 6 lines
drivers/cpufreq/amd-pstate.c new file +6
-0+ +u32 max_freq; +u32 min_freq; +u32 nominal_freq; +u32 lowest_nonlinear_freq; +};Please describe the fields of this structure, preferably in a kerneldoc comment.
OK.
Quoted 11 lines
drivers/cpufreq/amd-pstate.c line 63
Quoted 8 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int pstate_enable (bool enable) +{ +return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); +} + +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);Why do you need a static call here?
Quoted 1 line
It doesn't get updated anywhere below.
Ah, the update behavior is defined in the following patch 8 (shared memory support). Sorry to make you confused.
Quoted 232 lines
Quoted 21 lines
drivers/cpufreq/amd-pstate.c new file +20
-0+ +static inline int amd_pstate_enable (bool enable) +{ +return static_call (amd_pstate_enable)(enable); +} + +static int pstate_init_perf (struct amd_cpudata *cpudata) +{ +u64 cap1; + +int ret =rdmsrl_safe_on_cpu (cpudata->cpu,MSR_AMD_CPPC_CAP1 , + &cap1); +if (ret) +return ret; + +/* +* TODO: Introduce AMD specific power feature. +* +* CPPC entry doesn't indicate the highest performance in some ASICs. +*/ drivers/cpufreq/amd-pstate.c line 93
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf,CAP1_NOMINAL_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_nonlinear_perf,CAP1_LOWNONLIN_PERF (cap1)); +WRITE_ONCE (cpudata->lowest_perf,CAP1_LOWEST_PERF (cap1)); + +return 0 ; +} + +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);And same here.
Quoted 7 lines
drivers/cpufreq/amd-pstate.c new file +7
-0+ +static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata) +{ +return static_call (amd_pstate_init_perf)(cpudata); +} + +static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,drivers/cpufreq/amd-pstate.c line 110
Quoted 10 lines
drivers/cpufreq/amd-pstate.c new file +10
-0+ u32 des_perf, u32 max_perf, bool fast_switch) +{ + if (fast_switch) + wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached)); + else + wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, + READ_ONCE(cpudata->cppc_req_cached)); +} + +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf);Same here.
Quoted 178 lines
drivers/cpufreq/amd-pstate.c new file +178
-0+ +static inline void amd_pstate_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +static_call (amd_pstate_update_perf)(cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static void amd_pstate_update (struct amd_cpudata *cpudata,u32 min_perf, +u32 des_perf,u32 max_perf,bool fast_switch) +{ +u64 prev =READ_ONCE (cpudata->cppc_req_cached); +u64 value = prev; + + value &= ~REQ_MIN_PERF (~0L ); + value |=REQ_MIN_PERF (min_perf); + + value &= ~REQ_DES_PERF (~0L ); + value |=REQ_DES_PERF (des_perf); + + value &= ~REQ_MAX_PERF (~0L ); + value |=REQ_MAX_PERF (max_perf); + +if (value == prev) +return ; + +WRITE_ONCE (cpudata->cppc_req_cached, value); + +amd_pstate_update_perf (cpudata, min_perf, des_perf, + max_perf, fast_switch); +} + +static int amd_pstate_verify (struct cpufreq_policy_data *policy) +{ +cpufreq_verify_within_cpu_limits (policy); + +return 0 ; +} + +static int amd_pstate_target (struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +struct cpufreq_freqs freqs; +struct amd_cpudata *cpudata = policy->driver_data; +unsigned long max_perf, min_perf, des_perf, cap_perf; + +if (!cpudata->max_freq) +return -ENODEV ; + + cap_perf =READ_ONCE (cpudata->highest_perf); + min_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); + max_perf = cap_perf; + + freqs.old = policy->cur; + freqs.new = target_freq; + + des_perf =DIV_ROUND_CLOSEST (target_freq * cap_perf, + cpudata->max_freq); + +cpufreq_freq_transition_begin (policy, &freqs); +amd_pstate_update (cpudata, min_perf, des_perf, + max_perf, false); +cpufreq_freq_transition_end (policy, &freqs, false); + +return 0 ; +} + +static int amd_get_min_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.lowest_freq *1000 ; +} + +static int amd_get_max_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 max_perf, max_freq, nominal_freq, nominal_perf; +u64 boost_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + max_perf =READ_ONCE (cpudata->highest_perf); + + boost_ratio =div_u64 (max_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + max_freq = nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return max_freq *1000 ; +} + +static int amd_get_nominal_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +/* Switch to khz */ +return cppc_perf.nominal_freq *1000 ; +} + +static int amd_get_lowest_nonlinear_freq (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; +u32 lowest_nonlinear_freq, lowest_nonlinear_perf, + nominal_freq, nominal_perf; +u64 lowest_nonlinear_ratio; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + + nominal_freq = cppc_perf.nominal_freq; + nominal_perf =READ_ONCE (cpudata->nominal_perf); + + lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf; + + lowest_nonlinear_ratio =div_u64 (lowest_nonlinear_perf <<SCHED_CAPACITY_SHIFT , + nominal_perf); + + lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >>SCHED_CAPACITY_SHIFT ; + +/* Switch to khz */ +return lowest_nonlinear_freq *1000 ; +} + +static int amd_pstate_cpu_init (struct cpufreq_policy *policy) +{ +int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret; +struct device *dev; +struct amd_cpudata *cpudata; + + dev =get_cpu_device (policy->cpu); +if (!dev) +return -ENODEV ; + + cpudata =kzalloc (sizeof (*cpudata),GFP_KERNEL ); +if (!cpudata) +return -ENOMEM ; + + cpudata->cpu = policy->cpu; + + ret =amd_pstate_init_perf (cpudata); +if (ret) + goto free_cpudata1; + + min_freq =amd_get_min_freq (cpudata); + max_freq =amd_get_max_freq (cpudata); + nominal_freq =amd_get_nominal_freq (cpudata); + lowest_nonlinear_freq =amd_get_lowest_nonlinear_freq (cpudata); + +if (min_freq <0 || max_freq <0 || min_freq > max_freq) { +dev_err (dev,"min_freq(%d) or max_freq(%d) value is incorrect\n" , + min_freq, max_freq); + ret = -EINVAL ; + goto free_cpudata1; + } + + policy->cpuinfo.transition_latency =AMD_PSTATE_TRANSITION_LATENCY ; + policy->transition_delay_us =AMD_PSTATE_TRANSITION_DELAY ; + + policy->min = min_freq; + policy->max = max_freq;Quoted 8 lines
drivers/cpufreq/amd-pstate.c line 294
Quoted 6 lines
drivers/cpufreq/amd-pstate.c new file +6
-0+ + policy->cpuinfo.min_freq = min_freq; + policy->cpuinfo.max_freq = max_freq; + +/* It will be updated by governor */ + policy->cur = policy->cpuinfo.min_freq;The
freq_qosrequests below are never updated in this file. What are they for?
Quoted 3 lines
The
freq_qosrequests will be updated while we set boost disabled or enabled in patch 10. Sorry to make you confused again. Should I move thefreq_qosrequests implementation into patch 10 as well?
Yes, please.
Huang Rui
maintainerPATCH v5 7/22 cpufreq: amd: add fast switch function for amd-pstate
In reply to Huang RuiIntroduce the fast switch function for amd-pstate on the AMD processors which support the full MSR register control. It's able to decrease the latency on interrupt context.
--- drivers/cpufreq/amd-pstate.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
drivers/cpufreq/amd-pstate.c +36 -0
@@ -177,6 +177,39 @@ static int amd_pstate_target(struct cpufreq_policy *policy,return 0 ; } +static void amd_pstate_adjust_perf (unsigned int cpu, +unsigned long _min_perf, +unsigned long target_perf, +unsigned long capacity) +{ +unsigned long max_perf, min_perf, des_perf, + cap_perf, lowest_nonlinear_perf; +struct cpufreq_policy *policy =cpufreq_cpu_get (cpu); +struct amd_cpudata *cpudata = policy->driver_data; + + cap_perf =READ_ONCE (cpudata->highest_perf); + lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); + + des_perf = cap_perf; +if (target_perf < capacity) + des_perf =DIV_ROUND_UP (cap_perf * target_perf, capacity); + + min_perf =READ_ONCE (cpudata->highest_perf); +if (_min_perf < capacity) + min_perf =DIV_ROUND_UP (cap_perf * _min_perf, capacity); + +if (min_perf < lowest_nonlinear_perf) + min_perf = lowest_nonlinear_perf; + + max_perf = cap_perf; +if (max_perf < min_perf) + max_perf = min_perf; + + des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf); + +amd_pstate_update (cpudata, min_perf, des_perf, max_perf, true); +} +static int amd_get_min_freq (struct amd_cpudata *cpudata) {struct cppc_perf_caps cppc_perf;
@@ -293,6 +326,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; + policy->fast_switch_possible = true; + ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);if (ret <0 ) {
@@ -341,6 +376,7 @@ static struct cpufreq_driver amd_pstate_driver = { .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, .verify = amd_pstate_verify, .target = amd_pstate_target, + .adjust_perf = amd_pstate_adjust_perf, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, .name = "amd-pstate",
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 8/22 cpufreq: amd: introduce the support for the processors with shared memory solution
In reply to Huang RuiIn some of Zen2 and Zen3 based processors, they are using the shared memory that exposed from ACPI SBIOS. In this kind of the processors, there is no MSR support, so we add acpi cppc function as the backend for them.
It is using a module param (shared_mem) to enable related processors manually. We will enable this by default once we address performance issue on this solution.
--- drivers/cpufreq/amd-pstate.c | 72 ++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-)
drivers/cpufreq/amd-pstate.c +68 -4
@@ -35,6 +35,19 @@#define AMD_PSTATE_TRANSITION_LATENCY 0x20000#define AMD_PSTATE_TRANSITION_DELAY 500 +/* TODO: We need more time to fine tune processors with shared memory solution +* with community together. +* +* There are some performance drops on the CPU benchmarks which reports from +* Suse. We are co-working with them to fine tune the shared memory solution. So +* we disable it by default to go acpi-cpufreq on these processors and add a +* module parameter to be able to enable it manually for debugging. +*/ +static bool shared_mem = false; +module_param (shared_mem, bool,0444 ); +MODULE_PARM_DESC (shared_mem, +"enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)" ); +static struct cpufreq_driver amd_pstate_driver;struct amd_cpudata {
@@ -60,6 +73,19 @@ static inline int pstate_enable(bool enable)return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); } +static int cppc_enable (bool enable) +{ +int cpu, ret =0 ; + +for_each_online_cpu (cpu) { + ret =cppc_set_enable (cpu, enable); +if (ret) +return ret; + } + +return ret; +} +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);static inline int amd_pstate_enable (bool enable)
@@ -90,6 +116,24 @@ static int pstate_init_perf(struct amd_cpudata *cpudata)return 0 ; } +static int cppc_init_perf (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf, cppc_perf.nominal_perf); +WRITE_ONCE (cpudata->lowest_nonlinear_perf, + cppc_perf.lowest_nonlinear_perf); +WRITE_ONCE (cpudata->lowest_perf, cppc_perf.lowest_perf); + +return 0 ; +} +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata)
@@ -107,6 +151,19 @@ static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf, READ_ONCE(cpudata->cppc_req_cached)); } +static void cppc_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +struct cppc_perf_ctrls perf_ctrls; + + perf_ctrls.max_perf = max_perf; + perf_ctrls.min_perf = min_perf; + perf_ctrls.desired_perf = des_perf; + +cppc_set_perf (cpudata->cpu, &perf_ctrls); +} +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf); static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
@@ -326,7 +383,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; - policy->fast_switch_possible = true; +if (boot_cpu_has (X86_FEATURE_CPPC )) + policy->fast_switch_possible = true; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);
@@ -376,7 +434,6 @@ static struct cpufreq_driver amd_pstate_driver = { .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, .verify = amd_pstate_verify, .target = amd_pstate_target, - .adjust_perf = amd_pstate_adjust_perf, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, .name = "amd-pstate",
@@ -399,8 +456,15 @@ static int __init amd_pstate_init(void)return -EEXIST ;/* capability check */ -if (!boot_cpu_has (X86_FEATURE_CPPC )) { -pr_debug ("AMD CPPC MSR based functionality is not supported\n" ); +if (boot_cpu_has (X86_FEATURE_CPPC )) { +pr_debug ("AMD CPPC MSR based functionality is supported\n" ); + amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf; + }else if (shared_mem) { +static_call_update (amd_pstate_enable, cppc_enable); +static_call_update (amd_pstate_init_perf, cppc_init_perf); +static_call_update (amd_pstate_update_perf, cppc_update_perf); + }else { +pr_info ("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n" );return -ENODEV ; }
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 08/22] cpufreq: amd: introduce the support for the processors with shared memory solution
Quoted 126 lines · Huang Rui
In some of Zen2 and Zen3 based processors, they are using the shared memory that exposed from ACPI SBIOS. In this kind of the processors, there is no MSR support, so we add acpi cppc function as the backend for them.
It is using a module param (
shared_mem) to enable related processors manually. We will enable this by default once we address performance issue on this solution.Signed-off-by Jinzhou Su <[email protected]>Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/amd-pstate.c | 72 ++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-)drivers/cpufreq/amd-pstate.c +59
-2@@ -35,6 +35,19 @@#define AMD_PSTATE_TRANSITION_LATENCY 0x20000#define AMD_PSTATE_TRANSITION_DELAY 500 +/* TODO: We need more time to fine tune processors with shared memory solution +* with community together. +* +* There are some performance drops on the CPU benchmarks which reports from +* Suse. We are co-working with them to fine tune the shared memory solution. So +* we disable it by default to go acpi-cpufreq on these processors and add a +* module parameter to be able to enable it manually for debugging. +*/ +static bool shared_mem = false; +module_param (shared_mem, bool,0444 ); +MODULE_PARM_DESC (shared_mem, +"enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)" ); +static struct cpufreq_driver amd_pstate_driver;struct amd_cpudata {@@ -60,6 +73,19 @@ static inline int pstate_enable(bool enable)return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); } +static int cppc_enable (bool enable) +{ +int cpu, ret =0 ; + +for_each_online_cpu (cpu) { + ret =cppc_set_enable (cpu, enable); +if (ret) +return ret; + } + +return ret; +} +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);static inline int amd_pstate_enable (bool enable)@@ -90,6 +116,24 @@ static int pstate_init_perf(struct amd_cpudata *cpudata)return 0 ; } +static int cppc_init_perf (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf, cppc_perf.nominal_perf); +WRITE_ONCE (cpudata->lowest_nonlinear_perf, + cppc_perf.lowest_nonlinear_perf); +WRITE_ONCE (cpudata->lowest_perf, cppc_perf.lowest_perf); + +return 0 ; +} +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata)@@ -107,6 +151,19 @@ static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf, READ_ONCE(cpudata->cppc_req_cached)); } +static void cppc_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +struct cppc_perf_ctrls perf_ctrls; + + perf_ctrls.max_perf = max_perf; + perf_ctrls.min_perf = min_perf; + perf_ctrls.desired_perf = des_perf; + +cppc_set_perf (cpudata->cpu, &perf_ctrls); +} +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf); static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,@@ -326,7 +383,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; - policy->fast_switch_possible = true; +if (boot_cpu_has (X86_FEATURE_CPPC )) + policy->fast_switch_possible = true; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);@@ -376,7 +434,6 @@ static struct cpufreq_driver amd_pstate_driver = { .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, .verify = amd_pstate_verify, .target = amd_pstate_target, - .adjust_perf = amd_pstate_adjust_perf, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, .name = "amd-pstate",@@ -399,8 +456,15 @@ static int __init amd_pstate_init(void)return -EEXIST ;
drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 465
Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7
-2/* capability check */ -if (!boot_cpu_has (X86_FEATURE_CPPC )) { -pr_debug ("AMD CPPC MSR based functionality is not supported\n" ); +if (boot_cpu_has (X86_FEATURE_CPPC )) { +pr_debug ("AMD CPPC MSR based functionality is supported\n" ); + amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf; + }else if (shared_mem) { +static_call_update (amd_pstate_enable, cppc_enable); +static_call_update (amd_pstate_init_perf, cppc_init_perf); +static_call_update (amd_pstate_update_perf, cppc_update_perf);
I would rather add the static call definitions in this patch, otherwise is somewhat cumbersome to review the series.
Quoted 8 lines
drivers/cpufreq/amd-pstate.c +2
-1+ }else { +pr_info ("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n" );return -ENODEV ; } --2.25.1
Huang Rui
maintainerRe: [PATCH v5 08/22] cpufreq: amd: introduce the support for the processors with shared memory solution
Quoted 127 lines · Rafael J. Wysocki
Quoted 126 lines · Huang Rui
In some of Zen2 and Zen3 based processors, they are using the shared memory that exposed from ACPI SBIOS. In this kind of the processors, there is no MSR support, so we add acpi cppc function as the backend for them.
It is using a module param (
shared_mem) to enable related processors manually. We will enable this by default once we address performance issue on this solution.Signed-off-by Jinzhou Su <[email protected]>Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/amd-pstate.c | 72 ++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-)drivers/cpufreq/amd-pstate.c +59
-2@@ -35,6 +35,19 @@#define AMD_PSTATE_TRANSITION_LATENCY 0x20000#define AMD_PSTATE_TRANSITION_DELAY 500 +/* TODO: We need more time to fine tune processors with shared memory solution +* with community together. +* +* There are some performance drops on the CPU benchmarks which reports from +* Suse. We are co-working with them to fine tune the shared memory solution. So +* we disable it by default to go acpi-cpufreq on these processors and add a +* module parameter to be able to enable it manually for debugging. +*/ +static bool shared_mem = false; +module_param (shared_mem, bool,0444 ); +MODULE_PARM_DESC (shared_mem, +"enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)" ); +static struct cpufreq_driver amd_pstate_driver;struct amd_cpudata {@@ -60,6 +73,19 @@ static inline int pstate_enable(bool enable)return wrmsrl_safe (MSR_AMD_CPPC_ENABLE , enable); } +static int cppc_enable (bool enable) +{ +int cpu, ret =0 ; + +for_each_online_cpu (cpu) { + ret =cppc_set_enable (cpu, enable); +if (ret) +return ret; + } + +return ret; +} +DEFINE_STATIC_CALL (amd_pstate_enable, pstate_enable);static inline int amd_pstate_enable (bool enable)@@ -90,6 +116,24 @@ static int pstate_init_perf(struct amd_cpudata *cpudata)return 0 ; } +static int cppc_init_perf (struct amd_cpudata *cpudata) +{ +struct cppc_perf_caps cppc_perf; + +int ret =cppc_get_perf_caps (cpudata->cpu, &cppc_perf); +if (ret) +return ret; + +WRITE_ONCE (cpudata->highest_perf,amd_get_highest_perf ()); + +WRITE_ONCE (cpudata->nominal_perf, cppc_perf.nominal_perf); +WRITE_ONCE (cpudata->lowest_nonlinear_perf, + cppc_perf.lowest_nonlinear_perf); +WRITE_ONCE (cpudata->lowest_perf, cppc_perf.lowest_perf); + +return 0 ; +} +DEFINE_STATIC_CALL (amd_pstate_init_perf, pstate_init_perf);static inline int amd_pstate_init_perf (struct amd_cpudata *cpudata)@@ -107,6 +151,19 @@ static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf, READ_ONCE(cpudata->cppc_req_cached)); } +static void cppc_update_perf (struct amd_cpudata *cpudata, +u32 min_perf,u32 des_perf, +u32 max_perf,bool fast_switch) +{ +struct cppc_perf_ctrls perf_ctrls; + + perf_ctrls.max_perf = max_perf; + perf_ctrls.min_perf = min_perf; + perf_ctrls.desired_perf = des_perf; + +cppc_set_perf (cpudata->cpu, &perf_ctrls); +} +DEFINE_STATIC_CALL (amd_pstate_update_perf, pstate_update_perf); static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,@@ -326,7 +383,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)/* It will be updated by governor */ policy->cur = policy->cpuinfo.min_freq; - policy->fast_switch_possible = true; +if (boot_cpu_has (X86_FEATURE_CPPC )) + policy->fast_switch_possible = true; ret =freq_qos_add_request (&policy->constraints, &cpudata->req[0 ],FREQ_QOS_MIN , policy->cpuinfo.min_freq);@@ -376,7 +434,6 @@ static struct cpufreq_driver amd_pstate_driver = { .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, .verify = amd_pstate_verify, .target = amd_pstate_target, - .adjust_perf = amd_pstate_adjust_perf, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, .name = "amd-pstate",@@ -399,8 +456,15 @@ static int __init amd_pstate_init(void)return -EEXIST ;
Quoted 14 lines
drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 465
Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7
-2/* capability check */ -if (!boot_cpu_has (X86_FEATURE_CPPC )) { -pr_debug ("AMD CPPC MSR based functionality is not supported\n" ); +if (boot_cpu_has (X86_FEATURE_CPPC )) { +pr_debug ("AMD CPPC MSR based functionality is supported\n" ); + amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf; + }else if (shared_mem) { +static_call_update (amd_pstate_enable, cppc_enable); +static_call_update (amd_pstate_init_perf, cppc_init_perf); +static_call_update (amd_pstate_update_perf, cppc_update_perf);I would rather add the static call definitions in this patch, otherwise is somewhat cumbersome to review the series.
I see. Will move them into this patch in V6.
Thanks,
Ray
Quoted 8 lines
Quoted 8 lines
drivers/cpufreq/amd-pstate.c +2
-1+ }else { +pr_info ("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n" );return -ENODEV ; } --2.25.1
Huang Rui
maintainerPATCH v5 9/22 cpufreq: amd: add trace for amd-pstate module
In reply to Huang RuiAdd trace event to monitor the performance value changes which is controlled by cpu governors.
--- drivers/cpufreq/Makefile | 6 ++- drivers/cpufreq/amd-pstate-trace.c | 2 + drivers/cpufreq/amd-pstate-trace.h | 77 ++++++++++++++++++++++++++++++ drivers/cpufreq/amd-pstate.c | 4 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 drivers/cpufreq/amd-pstate-trace.c create mode 100644 drivers/cpufreq/amd-pstate-trace.h
drivers/cpufreq/Makefile +5 -1
@@ -17,6 +17,10 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o +# Traces +CFLAGS_amd-pstate-trace.o := -I$(src) +amd_pstate-y := amd-pstate.o amd-pstate-trace.o + ################################################################################## # x86 drivers. # Link order matters. K8 is preferred to ACPI because of firmware bugs in early
@@ -25,7 +29,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o -obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd_pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.o
drivers/cpufreq/amd-pstate-trace.c new file +2 -0
@@ -0,0 +1,2 @@ +#define CREATE_TRACE_POINTS +#include "amd-pstate-trace.h"
drivers/cpufreq/amd-pstate-trace.h new file +77 -0
77 lines not on this page · raw message
drivers/cpufreq/amd-pstate.c +4 -0
@@ -31,6 +31,7 @@#include <asm/processor.h> #include <asm/cpufeature.h> #include <asm/cpu_device_id.h> +#include "amd-pstate-trace.h" #define AMD_PSTATE_TRANSITION_LATENCY 0x20000#define AMD_PSTATE_TRANSITION_DELAY 500
@@ -189,6 +190,9 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf, value &= ~REQ_MAX_PERF (~0L ); value |=REQ_MAX_PERF (max_perf); +trace_amd_pstate_perf (min_perf, des_perf, max_perf, + cpudata->cpu, (value != prev), fast_switch); +if (value == prev)return ;
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerRe: [PATCH v5 09/22] cpufreq: amd: add trace for amd-pstate module
Quoted 3 lines · Huang Rui
Add trace event to monitor the performance value changes which is controlled by cpu governors.
This would need an ACK from Steve.
Quoted 6 lines
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Makefile | 6 ++- drivers/cpufreq/amd-pstate-trace.c | 2 + drivers/cpufreq/amd-pstate-trace.h | 77 ++++++++++++++++++++++++++++++
Why are these two extra files necessary?
Quoted 58 lines
drivers/cpufreq/amd-pstate.c | 4 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 drivers/cpufreq/amd-pstate-trace.c create mode 100644 drivers/cpufreq/amd-pstate-trace.hdrivers/cpufreq/Makefile +5
-1@@ -17,6 +17,10 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o +# Traces +CFLAGS_amd-pstate-trace.o := -I$(src) +amd_pstate-y := amd-pstate.o amd-pstate-trace.o + ################################################################################## # x86 drivers. # Link order matters. K8 is preferred to ACPI because of firmware bugs in early@@ -25,7 +29,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o -obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd_pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate-trace.c new file +2
-0@@ -0,0 +1,2 @@ +#define CREATE_TRACE_POINTS +#include "amd-pstate-trace.h" drivers/cpufreq/amd-pstate-trace.h new file +15
-0@@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* amd-pstate-trace.h - AMD Processor P-state Frequency Driver Tracer +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]> +*/ + +#if !defined(_AMD_PSTATE_TRACE_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _AMD_PSTATE_TRACE_H + +#include <linux/cpufreq.h> +#include <linux/tracepoint.h> +#include <linux/trace_events.h>
drivers/cpufreq/amd-pstate-trace.h line 25
Quoted 10 lines
drivers/cpufreq/amd-pstate-trace.h new file +10
-0+ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM amd_cpu + +#undef TRACE_INCLUDE_FILE +#define TRACE_INCLUDE_FILE amd-pstate-trace + +#define TPS (x) tracepoint_string(x) + +TRACE_EVENT(amd_pstate_perf,
Could this be added to include/trace/events/power.h ?
Quoted 77 lines
drivers/cpufreq/amd-pstate-trace.h new file +52
-0+ + TP_PROTO(unsigned long min_perf, + unsigned long target_perf, + unsigned long capacity, + unsigned int cpu_id, + bool changed, + bool fast_switch + ), + +TP_ARGS (min_perf, + target_perf, + capacity, + cpu_id, + changed, + fast_switch + ), + +TP_STRUCT__entry ( + __field(unsigned long, min_perf) +__field (unsigned long ,target_perf ) + __field(unsigned long, capacity) + __field(unsigned int, cpu_id) +__field (bool, changed) +__field (bool, fast_switch) + ), + +TP_fast_assign ( + __entry->min_perf = min_perf; + __entry->target_perf = target_perf; + __entry->capacity = capacity; + __entry->cpu_id = cpu_id; + __entry->changed = changed; + __entry->fast_switch = fast_switch; + ), + +TP_printk ("amd_min_perf=%lu amd_des_perf=%lu amd_max_perf=%lu cpu_id=%u changed=%s fast_switch=%s" , + (unsigned long )__entry->min_perf, + (unsigned long )__entry->target_perf, + (unsigned long )__entry->capacity, + (unsigned int )__entry->cpu_id, + (__entry->changed) ?"true" :"false" , + (__entry->fast_switch) ?"true" :"false" + ) +); + +#endif /* _AMD_PSTATE_TRACE_H */ + +/* This part must be outside protection */ +#undef TRACE_INCLUDE_PATH +#define TRACE_INCLUDE_PATH . + +#include <trace/define_trace.h> drivers/cpufreq/amd-pstate.c +4
-1@@ -31,6 +31,7 @@#include <asm/processor.h> #include <asm/cpufeature.h> #include <asm/cpu_device_id.h> +#include "amd-pstate-trace.h" #define AMD_PSTATE_TRANSITION_LATENCY 0x20000#define AMD_PSTATE_TRANSITION_DELAY 500@@ -189,6 +190,9 @@ static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf, value &= ~REQ_MAX_PERF (~0L ); value |=REQ_MAX_PERF (max_perf); +trace_amd_pstate_perf (min_perf, des_perf, max_perf, + cpudata->cpu, (value != prev), fast_switch); +if (value == prev)return ; --2.25.1
Huang Rui
maintainerRe: [PATCH v5 09/22] cpufreq: amd: add trace for amd-pstate module
Quoted 8 lines · Rafael J. Wysocki
Quoted 3 lines · Huang Rui
Add trace event to monitor the performance value changes which is controlled by cpu governors.
This would need an ACK from Steve.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Makefile | 6 ++- drivers/cpufreq/amd-pstate-trace.c | 2 + drivers/cpufreq/amd-pstate-trace.h | 77 ++++++++++++++++++++++++++++++Why are these two extra files necessary?
Please see below answer.
Quoted 63 lines
Quoted 63 lines
drivers/cpufreq/amd-pstate.c | 4 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 drivers/cpufreq/amd-pstate-trace.c create mode 100644 drivers/cpufreq/amd-pstate-trace.hdrivers/cpufreq/Makefile +5
-1@@ -17,6 +17,10 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o +# Traces +CFLAGS_amd-pstate-trace.o := -I$(src) +amd_pstate-y := amd-pstate.o amd-pstate-trace.o + ################################################################################## # x86 drivers. # Link order matters. K8 is preferred to ACPI because of firmware bugs in early@@ -25,7 +29,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o -obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd_pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate-trace.c new file +2
-0@@ -0,0 +1,2 @@ +#define CREATE_TRACE_POINTS +#include "amd-pstate-trace.h" drivers/cpufreq/amd-pstate-trace.h new file +19
-0@@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* amd-pstate-trace.h - AMD Processor P-state Frequency Driver Tracer +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]> +*/ + +#if !defined(_AMD_PSTATE_TRACE_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _AMD_PSTATE_TRACE_H + +#include <linux/cpufreq.h> +#include <linux/tracepoint.h> +#include <linux/trace_events.h> + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM amd_cpu +
Quoted 9 lines
drivers/cpufreq/amd-pstate-trace.h line 25
Quoted 6 lines
drivers/cpufreq/amd-pstate-trace.h new file +6
-0+#undef TRACE_INCLUDE_FILE +#define TRACE_INCLUDE_FILE amd-pstate-trace + +#define TPS (x) tracepoint_string(x) + +TRACE_EVENT(amd_pstate_perf,Could this be added to
include/trace/events/power.h?
Actually, that is my original idea, but once I move the trace into include/trace/events/power.h, the amd-pstate driver cannot build as "ko" anymore. This is the early stage, "ko" is friendly and flexible for us to switch and compare between amd-pstate and acpi-cpufreq. I can move it into "power" trace events once we address the performance issue on shared memory solution.
Thanks,
Ray
Rafael J. Wysocki
maintainerRe: [PATCH v5 09/22] cpufreq: amd: add trace for amd-pstate module
Quoted 93 lines · Huang Rui
Quoted 8 lines · Rafael J. Wysocki
Quoted 3 lines · Huang Rui
Add trace event to monitor the performance value changes which is controlled by cpu governors.
This would need an ACK from Steve.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Signed-off-by Huang Rui <[email protected]>--- drivers/cpufreq/Makefile | 6 ++- drivers/cpufreq/amd-pstate-trace.c | 2 + drivers/cpufreq/amd-pstate-trace.h | 77 ++++++++++++++++++++++++++++++Why are these two extra files necessary?
Please see below answer.
Quoted 63 lines
Quoted 63 lines
drivers/cpufreq/amd-pstate.c | 4 ++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 drivers/cpufreq/amd-pstate-trace.c create mode 100644 drivers/cpufreq/amd-pstate-trace.hdrivers/cpufreq/Makefile +5
-1@@ -17,6 +17,10 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o +# Traces +CFLAGS_amd-pstate-trace.o := -I$(src) +amd_pstate-y := amd-pstate.o amd-pstate-trace.o + ################################################################################## # x86 drivers. # Link order matters. K8 is preferred to ACPI because of firmware bugs in early@@ -25,7 +29,7 @@ obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o # speedstep-* is preferred over p4-clockmod. obj-$(CONFIG_X86_ACPI_CPUFREQ) += acpi-cpufreq.o -obj-$(CONFIG_X86_AMD_PSTATE) += amd-pstate.o +obj-$(CONFIG_X86_AMD_PSTATE) += amd_pstate.o obj-$(CONFIG_X86_POWERNOW_K8) += powernow-k8.o obj-$(CONFIG_X86_PCC_CPUFREQ) += pcc-cpufreq.o obj-$(CONFIG_X86_POWERNOW_K6) += powernow-k6.odrivers/cpufreq/amd-pstate-trace.c new file +2
-0@@ -0,0 +1,2 @@ +#define CREATE_TRACE_POINTS +#include "amd-pstate-trace.h" drivers/cpufreq/amd-pstate-trace.h new file +19
-0@@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* amd-pstate-trace.h - AMD Processor P-state Frequency Driver Tracer +* +* Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Huang Rui <[email protected]> +*/ + +#if !defined(_AMD_PSTATE_TRACE_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _AMD_PSTATE_TRACE_H + +#include <linux/cpufreq.h> +#include <linux/tracepoint.h> +#include <linux/trace_events.h> + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM amd_cpu +Quoted 9 lines
drivers/cpufreq/amd-pstate-trace.h line 25
Quoted 6 lines
drivers/cpufreq/amd-pstate-trace.h new file +6
-0+#undef TRACE_INCLUDE_FILE +#define TRACE_INCLUDE_FILE amd-pstate-trace + +#define TPS (x) tracepoint_string(x) + +TRACE_EVENT(amd_pstate_perf,Could this be added to
include/trace/events/power.h?
Quoted 6 lines
Actually, that is my original idea, but once I move the trace into
include/trace/events/power.h, the amd-pstate driver cannot build as "ko" anymore. This is the early stage, "ko" is friendly and flexible for us to switch and compare between amd-pstate and acpi-cpufreq. I can move it into "power" trace events once we address the performance issue on shared memory solution.
OK, I guess it's fine then, but it still needs an ACK from Steve.
Huang Rui
maintainerPATCH v5 10/22 cpufreq: amd: add boost mode support for amd-pstate
In reply to Huang RuiIf the sbios supports the boost mode of amd-pstate, let's switch to boost enabled by default.
--- drivers/cpufreq/amd-pstate.c | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
drivers/cpufreq/amd-pstate.c +44 -0
@@ -67,6 +67,8 @@ struct amd_cpudata {u32 min_freq;u32 nominal_freq;u32 lowest_nonlinear_freq; + +bool boost_supported; };static inline int pstate_enable (bool enable)
@@ -343,6 +345,45 @@ static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)return lowest_nonlinear_freq *1000 ; } +static int amd_pstate_set_boost (struct cpufreq_policy *policy,int state) +{ +struct amd_cpudata *cpudata = policy->driver_data; +int ret; + +if (!cpudata->boost_supported) { +pr_err ("Boost mode is not supported by this processor or SBIOS\n" ); +return -EINVAL ; + } + +if (state) + policy->cpuinfo.max_freq = cpudata->max_freq; +else + policy->cpuinfo.max_freq = cpudata->nominal_freq; + + policy->max = policy->cpuinfo.max_freq; + + ret =freq_qos_update_request (&cpudata->req[1 ], + policy->cpuinfo.max_freq); +if (ret <0 ) +return ret; + +return 0 ; +} + +static void amd_pstate_boost_init (struct amd_cpudata *cpudata) +{ +u32 highest_perf, nominal_perf; + + highest_perf =READ_ONCE (cpudata->highest_perf); + nominal_perf =READ_ONCE (cpudata->nominal_perf); + +if (highest_perf <= nominal_perf) +return ; + + cpudata->boost_supported = true; + amd_pstate_driver.boost_enabled = true; +} +static int amd_pstate_cpu_init (struct cpufreq_policy *policy) {int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
@@ -412,6 +453,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy) policy->driver_data = cpudata; +amd_pstate_boost_init (cpudata); +return 0 ; free_cpudata2:
@@ -440,6 +483,7 @@ static struct cpufreq_driver amd_pstate_driver = { .target = amd_pstate_target, .init = amd_pstate_cpu_init, .exit = amd_pstate_cpu_exit, + .set_boost = amd_pstate_set_boost, .name = "amd-pstate", };
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 11/22 cpufreq: amd: add amd-pstate frequencies attributes
In reply to Huang RuiIntroduce sysfs attributes to get the different level processor frequencies.
--- drivers/cpufreq/amd-pstate.c | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
drivers/cpufreq/amd-pstate.c +46 -0
@@ -477,6 +477,51 @@ static int amd_pstate_cpu_exit(struct cpufreq_policy *policy)return 0 ; } +/* Sysfs attributes */ + +/* This frequency is to indicate the maximum hardware frequency. +* If boost is not active but supported, the frequency will be larger than the +* one in cpuinfo. +*/ +static ssize_t show_amd_pstate_max_freq (struct cpufreq_policy *policy, +char *buf) +{ +int max_freq; +struct amd_cpudata *cpudata; + + cpudata = policy->driver_data; + + max_freq =amd_get_max_freq (cpudata); +if (max_freq <0 ) +return max_freq; + +return sprintf (&buf[0 ],"%u\n" , max_freq); +} + +static ssize_t show_amd_pstate_lowest_nonlinear_freq (struct cpufreq_policy *policy, +char *buf) +{ +int freq; +struct amd_cpudata *cpudata; + + cpudata = policy->driver_data; + + freq =amd_get_lowest_nonlinear_freq (cpudata); +if (freq <0 ) +return freq; + +return sprintf (&buf[0 ],"%u\n" , freq); +} + +cpufreq_freq_attr_ro (amd_pstate_max_freq); +cpufreq_freq_attr_ro (amd_pstate_lowest_nonlinear_freq); + +static struct freq_attr *amd_pstate_attr[] = { + &amd_pstate_max_freq, + &amd_pstate_lowest_nonlinear_freq, +NULL , +}; +static struct cpufreq_driver amd_pstate_driver = { .flags =CPUFREQ_CONST_LOOPS |CPUFREQ_NEED_UPDATE_LIMITS , .verify = amd_pstate_verify,
@@ -485,6 +530,7 @@ static struct cpufreq_driver amd_pstate_driver = { .exit = amd_pstate_cpu_exit, .set_boost = amd_pstate_set_boost, .name = "amd-pstate", + .attr = amd_pstate_attr, };static int __init amd_pstate_init (void )
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 12/22 cpufreq: amd: add amd-pstate performance attributes
In reply to Huang RuiIntroduce sysfs attributes to get the different level amd-pstate performances.
--- drivers/cpufreq/amd-pstate.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/cpufreq/amd-pstate.c +17 -0
@@ -513,12 +513,29 @@ static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *polireturn sprintf (&buf[0 ],"%u\n" , freq); } +/* In some of ASICs, the highest_perf is not the one in the _CPC table, so we +* need to expose it to sysfs. +*/ +static ssize_t show_amd_pstate_highest_perf (struct cpufreq_policy *policy, +char *buf) +{ +u32 perf; +struct amd_cpudata *cpudata = policy->driver_data; + + perf =READ_ONCE (cpudata->highest_perf); + +return sprintf (&buf[0 ],"%u\n" , perf); +} +cpufreq_freq_attr_ro (amd_pstate_max_freq);cpufreq_freq_attr_ro (amd_pstate_lowest_nonlinear_freq); +cpufreq_freq_attr_ro (amd_pstate_highest_perf); +static struct freq_attr *amd_pstate_attr[] = { &amd_pstate_max_freq, &amd_pstate_lowest_nonlinear_freq, + &amd_pstate_highest_perf,NULL , };
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 13/22 cpupower: add AMD P-state capability flag
In reply to Huang RuiAdd AMD P-state capability flag in cpupower to indicate AMD new P-state kernel module support on Ryzen processors.
--- tools/power/cpupower/utils/helpers/helpers.h | 1 + 1 file changed, 1 insertion(+)
tools/power/cpupower/utils/helpers/helpers.h +1 -0
@@ -73,6 +73,7 @@ enum cpupower_cpu_vendor {X86_VENDOR_UNKNOWN = 0, X86_VENDOR_INTEL,#define CPUPOWER_CAP_AMD_HW_PSTATE 0x00000100#define CPUPOWER_CAP_AMD_PSTATEDEF 0x00000200#define CPUPOWER_CAP_AMD_CPB_MSR 0x00000400 +#define CPUPOWER_CAP_AMD_PSTATE 0x00000800#define CPUPOWER_AMD_CPBDIS 0x02000000
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 14/22 cpupower: add the function to check amd-pstate enabled
In reply to Huang RuiThe processor with amd-pstate function also supports legacy ACPI hardware P-States feature as well. Once driver sets amd-pstate eanbled, the processor will respond the finer grain amd-pstate feature instead of legacy ACPI P-States. So it introduces the cpupower_amd_pstate_enabled() to check whether the current kernel enables amd-pstate or acpi-cpufreq module.
--- tools/power/cpupower/utils/helpers/helpers.h | 10 ++++++++++ tools/power/cpupower/utils/helpers/misc.c | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+)
tools/power/cpupower/utils/helpers/helpers.h +10 -0
@@ -11,6 +11,7 @@#include <libintl.h> #include <locale.h> +#include <stdbool.h> #include "helpers/bitmask.h" #include <cpupower.h>
@@ -136,6 +137,12 @@ extern int decode_pstates(unsigned int cpu, int boost_states,extern int cpufreq_has_boost_support (unsigned int cpu,int *support,int *active,int * states); + +/* AMD P-States stuff **************************/ +extern bool cpupower_amd_pstate_enabled (void ); + +/* AMD P-States stuff **************************/ +/* * CPUID functions returning a single datum */
@@ -168,6 +175,9 @@ static inline int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active, int * states) {return -1 ; } +static inline bool cpupower_amd_pstate_enabled (void ) +{return false; } +/* cpuid and cpuinfo helpers **************************/ static inline unsigned int cpuid_eax (unsigned int op) {return 0 ; };
tools/power/cpupower/utils/helpers/misc.c +18 -0
@@ -3,9 +3,11 @@#include <stdio.h> #include <errno.h> #include <stdlib.h> +#include <string.h> #include "helpers/helpers.h" #include "helpers/sysfs.h" +#include "cpufreq.h" #if defined(__i386__) || defined(__x86_64__)
@@ -83,6 +85,22 @@ int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val) return 0; } +bool cpupower_amd_pstate_enabled (void ) +{ +char *driver =cpufreq_get_driver (0 ); +bool ret = false; + +if (!driver) +return ret; + +if (!strcmp (driver,"amd-pstate" )) + ret = true; + +cpufreq_put_driver (driver); + +return ret; +} +#endif /* #if defined(__i386__) || defined(__x86_64__) */ /* get_cpustate
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 15/22 cpupower: initial AMD P-state capability
In reply to Huang RuiIf kernel starts the amd-pstate module, the cpupower will initial the capability flag as CPUPOWER_CAP_AMD_PSTATE. And once amd-pstate capability is set, it won't need to set legacy ACPI relative capabilities anymore.
--- tools/power/cpupower/utils/helpers/cpuid.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/power/cpupower/utils/helpers/cpuid.c +13 -0
@@ -149,6 +149,19 @@ int get_cpu_info(struct cpupower_cpu_info *cpu_info)if (ext_cpuid_level >=0x80000008 &&cpuid_ebx (0x80000008 ) & (1 <<4 )) cpu_info->caps |=CPUPOWER_CAP_AMD_RDPRU ; + +if (cpupower_amd_pstate_enabled ()) { + cpu_info->caps |=CPUPOWER_CAP_AMD_PSTATE ; + +/* +* If AMD P-state is enabled, the firmware will treat +* AMD P-state function as high priority. +*/ + cpu_info->caps &= ~CPUPOWER_CAP_AMD_CPB ; + cpu_info->caps &= ~CPUPOWER_CAP_AMD_CPB_MSR ; + cpu_info->caps &= ~CPUPOWER_CAP_AMD_HW_PSTATE ; + cpu_info->caps &= ~CPUPOWER_CAP_AMD_PSTATEDEF ; + } }if (cpu_info->vendor ==X86_VENDOR_INTEL ) {
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 16/22 cpupower: add the function to get the sysfs value from specific table
In reply to Huang RuiExpose the helper into cpufreq header, then cpufreq driver can use this function to get the sysfs value if it has any specific sysfs interfaces.
--- tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------ tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-)
tools/power/cpupower/lib/cpufreq.c +15 -6
@@ -83,20 +83,21 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = { [STATS_NUM_TRANSITIONS] = "stats/total_trans" }; - -static unsigned long sysfs_cpufreq_get_one_value (unsigned int cpu, -enum cpufreq_value which) +unsigned long cpufreq_get_sysfs_value_from_table (unsigned int cpu, +const char **table, +unsigned index, +unsigned size) {unsigned long value;unsigned int len;char linebuf[MAX_LINE_LEN ];char *endp; -if (which >=MAX_CPUFREQ_VALUE_READ_FILES ) +if (!table || index >= size || !table[index])return 0 ; - len =sysfs_cpufreq_read_file (cpu, cpufreq_value_files[which], - linebuf,sizeof (linebuf)); + len =sysfs_cpufreq_read_file (cpu, table[index], linebuf, +sizeof (linebuf));if (len ==0 )return 0 ;
@@ -109,6 +110,14 @@ static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,return value; } +static unsigned long sysfs_cpufreq_get_one_value (unsigned int cpu, +enum cpufreq_value which) +{ +return cpufreq_get_sysfs_value_from_table (cpu, cpufreq_value_files, + which, +MAX_CPUFREQ_VALUE_READ_FILES ); +} +/* read access to files which contain one string */ enum cpufreq_string {
tools/power/cpupower/lib/cpufreq.h +12 -0
@@ -203,6 +203,18 @@ int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);int cpufreq_set_frequency (unsigned int cpu,unsigned long target_frequency); +/* +* get the sysfs value from specific table +* +* Read the value with the sysfs file name from specific table. Does +* only work if the cpufreq driver has the specific sysfs interfaces. +*/ + +unsigned long cpufreq_get_sysfs_value_from_table (unsigned int cpu, +const char **table, +unsigned index, +unsigned size); + #ifdef __cplusplus }#endif
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 17/22 cpupower: introduce acpi cppc library
In reply to Huang RuiKernel ACPI subsytem introduced the sysfs attributes for acpi cppc library in below path:
/sys/devices/system/cpu/cpuX/acpi_cppc/
And these attributes will be used for amd-pstate driver to provide some performance and frequency values.
--- tools/power/cpupower/Makefile | 6 +-- tools/power/cpupower/lib/acpi_cppc.c | 59 ++++++++++++++++++++++++++++ tools/power/cpupower/lib/acpi_cppc.h | 21 ++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tools/power/cpupower/lib/acpi_cppc.c create mode 100644 tools/power/cpupower/lib/acpi_cppc.h
tools/power/cpupower/Makefile +3 -3
@@ -143,9 +143,9 @@ UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h \ utils/helpers/bitmask.h \ utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def -LIB_HEADERS = lib/cpufreq.h lib/cpupower.h lib/cpuidle.h -LIB_SRC = lib/cpufreq.c lib/cpupower.c lib/cpuidle.c -LIB_OBJS = lib/cpufreq.o lib/cpupower.o lib/cpuidle.o +LIB_HEADERS = lib/cpufreq.h lib/cpupower.h lib/cpuidle.h lib/acpi_cppc.h +LIB_SRC = lib/cpufreq.c lib/cpupower.c lib/cpuidle.c lib/acpi_cppc.c +LIB_OBJS = lib/cpufreq.o lib/cpupower.o lib/cpuidle.o lib/acpi_cppc.o LIB_OBJS := $(addprefix $(OUTPUT),$(LIB_OBJS)) override CFLAGS += -pipe
tools/power/cpupower/lib/acpi_cppc.c new file +59 -0
59 lines not on this page · raw message
tools/power/cpupower/lib/acpi_cppc.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ACPI_CPPC_H__ +#define __ACPI_CPPC_H__ + +enum acpi_cppc_value { +HIGHEST_PERF , +LOWEST_PERF , +NOMINAL_PERF , +LOWEST_NONLINEAR_PERF , +LOWEST_FREQ , +NOMINAL_FREQ , +REFERENCE_PERF , +WRAPAROUND_TIME , +MAX_CPPC_VALUE_FILES +}; + +extern unsigned long acpi_cppc_get_data (unsigned cpu, +enum acpi_cppc_value which); + +#endif /* _ACPI_CPPC_H */
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 18/22 cpupower: add amd-pstate sysfs definition and access helper
In reply to Huang RuiIntroduce the marco definitions and access helper function for amd-pstate sysfs interfaces such as each performance goals and frequency levels in amd helper file. They will be used to read the sysfs attribute from amd-pstate cpufreq driver for cpupower utilities.
--- tools/power/cpupower/utils/helpers/amd.c | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
tools/power/cpupower/utils/helpers/amd.c +30 -0
@@ -8,7 +8,10 @@#include <pci/pci.h> #include "helpers/helpers.h" +#include "cpufreq.h" +#include "acpi_cppc.h" +/* ACPI P-States Helper Functions for AMD Processors ***************/ #define MSR_AMD_PSTATE_STATUS 0xc0010063#define MSR_AMD_PSTATE 0xc0010064#define MSR_AMD_PSTATE_LIMIT 0xc0010061
@@ -146,4 +149,31 @@ int amd_pci_get_num_boost_states(int *active, int *states)pci_cleanup (pci_acc); return 0; } + +/* ACPI P-States Helper Functions for AMD Processors ***************/ + +/* AMD P-States Helper Functions ***************/ +enum amd_pstate_value { +AMD_PSTATE_HIGHEST_PERF , +AMD_PSTATE_MAX_FREQ , +AMD_PSTATE_LOWEST_NONLINEAR_FREQ , +MAX_AMD_PSTATE_VALUE_READ_FILES , +}; + +static const char *amd_pstate_value_files[MAX_AMD_PSTATE_VALUE_READ_FILES ] = { + [AMD_PSTATE_HIGHEST_PERF ] ="amd_pstate_highest_perf" , + [AMD_PSTATE_MAX_FREQ ] ="amd_pstate_max_freq" , + [AMD_PSTATE_LOWEST_NONLINEAR_FREQ ] ="amd_pstate_lowest_nonlinear_freq" , +}; + +static unsigned long amd_pstate_get_data (unsigned int cpu, +enum amd_pstate_value value) +{ +return cpufreq_get_sysfs_value_from_table (cpu, + amd_pstate_value_files, + value, +MAX_AMD_PSTATE_VALUE_READ_FILES ); +} + +/* AMD P-States Helper Functions ***************/ #endif /* defined(__i386__) || defined(__x86_64__) */
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 19/22 cpupower: enable boost state support for amd-pstate module
In reply to Huang RuiThe legacy ACPI hardware P-States function has 3 P-States on ACPI table, the CPU frequency only can be switched between the 3 P-States. While the processor supports the boost state, it will have another boost state that the frequency can be higher than P0 state, and the state can be decoded by the function of decode_pstates() and read by amd_pci_get_num_boost_states().
However, the new AMD P-States function is different than legacy ACPI hardware P-State on AMD processors. That has a finer grain frequency range between the highest and lowest frequency. And boost frequency is actually the frequency which is mapped on highest performance ratio. The similiar previous P0 frequency is mapped on nominal performance ratio. If the highest performance on the processor is higher than nominal performance, then we think the current processor supports the boost state. And it uses amd_pstate_boost_init() to initialize boost for AMD P-States function.
--- tools/power/cpupower/utils/helpers/amd.c | 18 ++++++++++++++++++ tools/power/cpupower/utils/helpers/helpers.h | 5 +++++ tools/power/cpupower/utils/helpers/misc.c | 2 ++ 3 files changed, 25 insertions(+)
tools/power/cpupower/utils/helpers/amd.c +18 -0
@@ -175,5 +175,23 @@ static unsigned long amd_pstate_get_data(unsigned int cpu, MAX_AMD_PSTATE_VALUE_READ_FILES); } +void amd_pstate_boost_init (unsigned int cpu,int *support,int *active) +{ +unsigned long highest_perf, nominal_perf, cpuinfo_min, + cpuinfo_max, amd_pstate_max; + + highest_perf =amd_pstate_get_data (cpu,AMD_PSTATE_HIGHEST_PERF ); + nominal_perf =acpi_cppc_get_data (cpu,NOMINAL_PERF ); + + *support = highest_perf > nominal_perf ?1 :0 ; +if (!(*support)) +return ; + +cpufreq_get_hardware_limits (cpu, &cpuinfo_min, &cpuinfo_max); + amd_pstate_max =amd_pstate_get_data (cpu,AMD_PSTATE_MAX_FREQ ); + + *active = cpuinfo_max == amd_pstate_max ?1 :0 ; +} +/* AMD P-States Helper Functions ***************/ #endif /* defined(__i386__) || defined(__x86_64__) */
tools/power/cpupower/utils/helpers/helpers.h +5 -0
@@ -140,6 +140,8 @@ extern int cpufreq_has_boost_support(unsigned int cpu, int *support,/* AMD P-States stuff **************************/ extern bool cpupower_amd_pstate_enabled (void ); +extern void amd_pstate_boost_init (unsigned int cpu, +int *support,int *active);/* AMD P-States stuff **************************/
@@ -177,6 +179,9 @@ static inline int cpufreq_has_boost_support(unsigned int cpu, int *support,static inline bool cpupower_amd_pstate_enabled (void ) {return false; } +static void amd_pstate_boost_init (unsigned int cpu, +int *support,int *active) +{return ; }/* cpuid and cpuinfo helpers **************************/
tools/power/cpupower/utils/helpers/misc.c +2 -0
@@ -41,6 +41,8 @@ int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active,if (ret)return ret; } + }else if (cpupower_cpu_info.caps &CPUPOWER_CAP_AMD_PSTATE ) { +amd_pstate_boost_init (cpu, support, active); }else if (cpupower_cpu_info.caps &CPUPOWER_CAP_INTEL_IDA ) *support = *active =1 ;return 0 ;
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 20/22 cpupower: move print_speed function into misc helper
In reply to Huang RuiThe print_speed can be as a common function, and expose it into misc helper header. Then it can be used on other helper files as well.
--- tools/power/cpupower/utils/cpufreq-info.c | 59 ++++---------------- tools/power/cpupower/utils/helpers/helpers.h | 1 + tools/power/cpupower/utils/helpers/misc.c | 42 ++++++++++++++ 3 files changed, 54 insertions(+), 48 deletions(-)
tools/power/cpupower/utils/cpufreq-info.c +11 -48
@@ -84,43 +84,6 @@ static void proc_cpufreq_output(void) }static int no_rounding; -static void print_speed (unsigned long speed) -{ -unsigned long tmp; - -if (no_rounding) { -if (speed >1000000 ) -printf ("%u.%06u GHz" , ((unsigned int ) speed/1000000 ), - ((unsigned int ) speed%1000000 )); -else if (speed >1000 ) -printf ("%u.%03u MHz" , ((unsigned int ) speed/1000 ), - (unsigned int ) (speed%1000 )); -else -printf ("%lu kHz" , speed); - }else { -if (speed >1000000 ) { - tmp = speed%10000 ; -if (tmp >=5000 ) - speed +=10000 ; -printf ("%u.%02u GHz" , ((unsigned int ) speed/1000000 ), - ((unsigned int ) (speed%1000000 )/10000 )); - }else if (speed >100000 ) { - tmp = speed%1000 ; -if (tmp >=500 ) - speed +=1000 ; -printf ("%u MHz" , ((unsigned int ) speed/1000 )); - }else if (speed >1000 ) { - tmp = speed%100 ; -if (tmp >=50 ) - speed +=100 ; -printf ("%u.%01u MHz" , ((unsigned int ) speed/1000 ), - ((unsigned int ) (speed%1000 )/100 )); - } - } - -return ; -} -static void print_duration (unsigned long duration) {unsigned long tmp;
@@ -254,11 +217,11 @@ static int get_boost_mode(unsigned int cpu)if (freqs) {printf (_ (" boost frequency steps: " ));while (freqs->next) { -print_speed (freqs->frequency); +print_speed (freqs->frequency, no_rounding);printf (", " ); freqs = freqs->next; } -print_speed (freqs->frequency); +print_speed (freqs->frequency, no_rounding);printf ("\n" );cpufreq_put_available_frequencies (freqs); }
@@ -277,7 +240,7 @@ static int get_freq_kernel(unsigned int cpu, unsigned int human)return -EINVAL ; }if (human) { -print_speed (freq); +print_speed (freq, no_rounding); }else printf ("%lu" , freq);printf (_ (" (asserted by call to kernel)\n" ));
@@ -296,7 +259,7 @@ static int get_freq_hardware(unsigned int cpu, unsigned int human)return -EINVAL ; }if (human) { -print_speed (freq); +print_speed (freq, no_rounding); }else printf ("%lu" , freq);printf (_ (" (asserted by call to hardware)\n" ));
@@ -316,9 +279,9 @@ static int get_hardware_limits(unsigned int cpu, unsigned int human)if (human) {printf (_ (" hardware limits: " )); -print_speed (min); +print_speed (min, no_rounding);printf (" - " ); -print_speed (max); +print_speed (max, no_rounding);printf ("\n" ); }else {printf ("%lu %lu\n" , min, max);
@@ -350,9 +313,9 @@ static int get_policy(unsigned int cpu)return -EINVAL ; }printf (_ (" current policy: frequency should be within " )); -print_speed (policy->min); +print_speed (policy->min, no_rounding);printf (_ (" and " )); -print_speed (policy->max); +print_speed (policy->max, no_rounding);printf (".\n " ); printf(_("The governor \"%s\" may decide which speed to use\n"
@@ -436,7 +399,7 @@ static int get_freq_stats(unsigned int cpu, unsigned int human)struct cpufreq_stats *stats =cpufreq_get_stats (cpu, &total_time);while (stats) {if (human) { -print_speed (stats->frequency); +print_speed (stats->frequency, no_rounding);printf (":%.2f%%" , (100.0 * stats->time_in_state) / total_time); }else
@@ -486,11 +449,11 @@ static void debug_output_one(unsigned int cpu)if (freqs) {printf (_ (" available frequency steps: " ));while (freqs->next) { -print_speed (freqs->frequency); +print_speed (freqs->frequency, no_rounding);printf (", " ); freqs = freqs->next; } -print_speed (freqs->frequency); +print_speed (freqs->frequency, no_rounding);printf ("\n" );cpufreq_put_available_frequencies (freqs); }
tools/power/cpupower/utils/helpers/helpers.h +1 -0
@@ -200,5 +200,6 @@ extern struct bitmask *offline_cpus;void get_cpustate (void );void print_online_cpus (void );void print_offline_cpus (void ); +void print_speed (unsigned long speed,int no_rounding);#endif /* __CPUPOWERUTILS_HELPERS__ */
tools/power/cpupower/utils/helpers/misc.c +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 21/22 cpupower: print amd-pstate information on cpupower
In reply to Huang Ruiamd-pstate kernel module is using the fine grain frequency instead of acpi hardware pstate. So the performance and frequency values should be printed in frequency-info.
--- tools/power/cpupower/utils/cpufreq-info.c | 9 ++++--- tools/power/cpupower/utils/helpers/amd.c | 28 ++++++++++++++++++++ tools/power/cpupower/utils/helpers/helpers.h | 5 ++++ 3 files changed, 39 insertions(+), 3 deletions(-)
tools/power/cpupower/utils/cpufreq-info.c +6 -3
@@ -146,9 +146,12 @@ static int get_boost_mode_x86(unsigned int cpu)printf (_ (" Supported: %s\n" ), support ?_ ("yes" ) :_ ("no" ));printf (_ (" Active: %s\n" ), active ?_ ("yes" ) :_ ("no" )); -if ((cpupower_cpu_info.vendor ==X86_VENDOR_AMD && - cpupower_cpu_info.family >=0x10 ) || - cpupower_cpu_info.vendor ==X86_VENDOR_HYGON ) { +if (cpupower_cpu_info.vendor ==X86_VENDOR_AMD && + cpupower_cpu_info.caps &CPUPOWER_CAP_AMD_PSTATE ) { +amd_pstate_show_perf_and_freq (cpu, no_rounding); + }else if ((cpupower_cpu_info.vendor ==X86_VENDOR_AMD && + cpupower_cpu_info.family >=0x10 ) || + cpupower_cpu_info.vendor ==X86_VENDOR_HYGON ) { ret =decode_pstates (cpu, b_states, pstates, &pstate_no);if (ret)return ret;
tools/power/cpupower/utils/helpers/amd.c +28 -0
@@ -193,5 +193,33 @@ void amd_pstate_boost_init(unsigned int cpu, int *support, int *active) *active = cpuinfo_max == amd_pstate_max ?1 :0 ; } +void amd_pstate_show_perf_and_freq (unsigned int cpu,int no_rounding) +{ +printf (_ (" AMD PSTATE Highest Performance: %lu. Maximum Frequency: " ), +amd_pstate_get_data (cpu,AMD_PSTATE_HIGHEST_PERF )); +/* If boost isn't active, the cpuinfo_max doesn't indicate real max +* frequency. So we read it back from amd-pstate sysfs entry. +*/ +print_speed (amd_pstate_get_data (cpu,AMD_PSTATE_MAX_FREQ ), no_rounding); +printf (".\n" ); + +printf (_ (" AMD PSTATE Nominal Performance: %lu. Nominal Frequency: " ), +acpi_cppc_get_data (cpu,NOMINAL_PERF )); +print_speed (acpi_cppc_get_data (cpu,NOMINAL_FREQ ) *1000 , + no_rounding); +printf (".\n" ); + +printf (_ (" AMD PSTATE Lowest Non-linear Performance: %lu. Lowest Non-linear Frequency: " ), +acpi_cppc_get_data (cpu,LOWEST_NONLINEAR_PERF )); +print_speed (amd_pstate_get_data (cpu,AMD_PSTATE_LOWEST_NONLINEAR_FREQ ), + no_rounding); +printf (".\n" ); + +printf (_ (" AMD PSTATE Lowest Performance: %lu. Lowest Frequency: " ), +acpi_cppc_get_data (cpu,LOWEST_PERF )); +print_speed (acpi_cppc_get_data (cpu,LOWEST_FREQ ) *1000 , no_rounding); +printf (".\n" ); +} +/* AMD P-States Helper Functions ***************/ #endif /* defined(__i386__) || defined(__x86_64__) */
tools/power/cpupower/utils/helpers/helpers.h +5 -0
@@ -142,6 +142,8 @@ extern int cpufreq_has_boost_support(unsigned int cpu, int *support,extern bool cpupower_amd_pstate_enabled (void );extern void amd_pstate_boost_init (unsigned int cpu,int *support,int *active); +extern void amd_pstate_show_perf_and_freq (unsigned int cpu, +int no_rounding);/* AMD P-States stuff **************************/
@@ -182,6 +184,9 @@ static inline bool cpupower_amd_pstate_enabled(void)static void amd_pstate_boost_init (unsigned int cpu,int *support,int *active) {return ; } +static inline void amd_pstate_show_perf_and_freq (unsigned int cpu, +int no_rounding) +{return ; }/* cpuid and cpuinfo helpers **************************/
Signature · 1 line
2.25.1
Huang Rui
maintainerPATCH v5 22/22 Documentation: amd-pstate: add amd-pstate driver introduction
In reply to Huang RuiIntroduce the amd-pstate driver design and implementation.
--- Documentation/admin-guide/acpi/cppc_sysfs.rst | 2 + Documentation/admin-guide/pm/amd-pstate.rst | 383 ++++++++++++++++++ .../admin-guide/pm/working-state.rst | 1 + 3 files changed, 386 insertions(+) create mode 100644 Documentation/admin-guide/pm/amd-pstate.rst
Documentation/admin-guide/acpi/cppc_sysfs.rst +2 -0
@@ -4,6 +4,8 @@ Collaborative Processor Performance Control (CPPC) ================================================== +.. _cppc_sysfs: + CPPC ====
Documentation/admin-guide/pm/amd-pstate.rst new file +383 -0
383 lines not on this page · raw message
Documentation/admin-guide/pm/working-state.rst +1 -0
@@ -11,6 +11,7 @@ Working-State Power Management intel_idle cpufreq intel_pstate + amd-pstate cpufreq_drivers intel_epb intel-speed-select
Signature · 1 line
2.25.1
Rafael J. Wysocki
maintainerQuoted 83 lines · Huang Rui
Hi all,
We would like to introduce a new AMD CPU frequency control mechanism as the "amd-pstate" driver for modern AMD Zen based CPU series in Linux Kernel. The new mechanism is based on Collaborative processor performance control (CPPC) which is finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. AMD P-States is to replace the ACPI P-states controls, allows a flexible, low-latency interface for the Linux kernel to directly communicate the performance hints to hardware.
"amd-pstate" leverages the Linux kernel governors such as *schedutil*, *ondemand*, etc. to manage the performance hints which are provided by CPPC hardware functionality. The first version for amd-pstate is to support one of the Zen3 processors, and we will support more in future after we verify the hardware and SBIOS functionalities.
There are two types of hardware implementations for amd-pstate: one is full MSR support and another is shared memory support. It can use
X86_FEATURE_CPPCfeature flag to distinguish the different types.Using the new AMD P-States method + kernel governors (*schedutil*, *ondemand*, ...) to manage the frequency update is the most appropriate bridge between AMD Zen based hardware processor and Linux kernel, the processor is able to adjust to the most efficiency frequency according to the kernel scheduler loading.
Please check the detailed CPU feature and MSR register description in Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors:
https://www.amd.com/system/files/TechDocs/56569-A1-PUB.zip
Performance Per Watt (PPW) Calculation:
We use the RAPL interface with "perf" tool to get the energy data of the package power.
The data comparisons between amd-pstate and acpi-freq module are tested on AMD Cezanne processor (mobile CPU):
1) TBench CPU benchmark:
+----------------------------------------------------------------------------------------------+ | | | TBench4 (Performance Per Watt) | | Higher is better | +-------------------+------------------------+------------------------+------------------------+ | | Performance Per Watt | Performance Per Watt | Performance Per Watt | | Kernel Module | (Schedutil) | (Ondemand) | (Performance) | | | Unit: MB / J | Unit: MB / J | Unit: MB / J | +-------------------+------------------------+------------------------+------------------------+ | | | | | | acpi-cpufreq | 48.56 | 48.89 | 47.81 | | | | | | +-------------------+------------------------+------------------------+------------------------+ | | | | | | amd-pstate | 48.38 | 47.38 | 48.77 | | | | | | +-------------------+------------------------+------------------------+------------------------+Note: The previous data was based on TBench2, as align with Suse, we use TBench4 to re-test it. The PPW is very closed to acpi-cpufreq. And we are still re-runing other tests.
Steam Game Demo on Ryzen 5900X (12 core 24 threads):
The picture to compare acpi-cpufreq vs amd-pstate:
https://drive.google.com/file/d/1PvSduykJn9U5MMOhzFWycnbmGmznalM3/view?usp=sharingTwo videos:
https://drive.google.com/file/d/1nQQEteL-v_zQxnOJpyW8JqvRW2FFDN2Z/view?usp=sharing
https://drive.google.com/file/d/1heuPgFG71SQHvGb6wfedrQciBfE2rhnu/view?usp=sharingActually, the amd-pstate driver doesn't change the physical maximum frequency capacity in the processor. But it's able to provide the finer grain performance control range instead of legacy 3 P-States. It has a better performance and power efficiency than before. We will continue optimize amd-pstate function on kernel governors to support different types of processors such as mobile latop, performance desktop, and etc.
Quoted 8 lines
See patch series in below git repo:
V1: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v1
V2: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v2
V3: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v3
V4: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v4
V5: https://git.kernel.org/pub/scm/linux/kernel/git/rui/linux.git/log/?h=amd-pstate-dev-v5For details introduction, please see the patch 22.
After going through all of the series I'm still unsure what the freq_qos requests added in patch [06/22] are for.
The cpupower patches need ACKs from Shuah for me to take them.
The tracing patch needs an ACK from Steve Rostedt.
Thanks!