amd-pstate CPU Performance Scaling Driver
cover amd-pstate CPU Performance Scaling Driver
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series in Xen. The new mechanism is based on Collaborative Processor Performance Control (CPPC) which provides finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU/APU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. CPPC replaces the ACPI P-states controls and allows a flexible, low-latency interface for Xen to directly communicate the performance hints to hardware.
amd_pstate CPPC has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-pstate" for passive mode and "amd-pstate-epp" for active mode.
The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance hints. And the active mode uses epp to provides a hint to the hardware if software wants to bias toward performance (0x0) or energy efficiency (0xff). CPPC power algorithm will calculate the runtime workload and adjust the realtime cpu cores frequency according to the power supply and thermal, core voltage and some other hardware conditions.
amd-pstate is enabled with a top-level cpufreq=amd-pstate option. It will fallback to cpufreq=xen if amd-pstate is unavailable.
With cpufreq=amd-pstate,active, We did a 60s sampling test to see the CPU frequency change, through tweaking the energy_perf preference from xenpm set-cpufreq-cppc powersave to xenpm set-cpufreq-cppc performance. The outputs are as follows:
``` Setting CPU in powersave mode Sampling and Outputs: Avg freq 2000000 KHz Avg freq 2000000 KHz Avg freq 2000000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```
Penny Zheng (11):
xen/x86: add CPPC feature flag for AMD processors xen/x86: introduce new sub-hypercall to get CPPC data xen/x86: introduce "cpufreq=amd-pstate" xen cmdline xen/x86: get processor max speed from DMI table xen/x86: introduce a new amd pstate driver for cpufreq scaling xen/cpufreq: introduce policy type when cpufreq_driver->setpolicy
exists
xen/cpufreq: only set gov NULL when cpufreq_driver.target() exists x86/cpufreq: add "cpufreq=amd-pstate,active" para xen/x86: implement EPP support for the AMD processors tools/xenpm: Print CPPC parameters for amd-pstate driver xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPC xen_sysctl_pm_op for
amd-pstate driver
docs/misc/xen-command-line.pandoc | 15 +- tools/misc/xenpm.c | 19 +- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-pstate.c | 641 ++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 28 + xen/arch/x86/acpi/cpufreq/hwp.c | 10 +- xen/arch/x86/dmi_scan.c | 19 + xen/arch/x86/include/asm/cpufeature.h | 1 + xen/arch/x86/include/asm/msr-index.h | 5 + xen/arch/x86/platform_hypercall.c | 12 + xen/arch/x86/x86_64/cpufreq.c | 18 + xen/arch/x86/x86_64/platform_hypercall.c | 3 + xen/drivers/acpi/pmstat.c | 24 +- xen/drivers/cpufreq/cpufreq.c | 71 ++- xen/drivers/cpufreq/utility.c | 11 + xen/include/acpi/cpufreq/cpufreq.h | 27 + xen/include/acpi/cpufreq/processor_perf.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + xen/include/public/platform.h | 12 + xen/include/public/sysctl.h | 3 +- xen/include/xen/dmi.h | 5 + xen/include/xen/pmstat.h | 3 + xen/include/xlat.lst | 1 + 23 files changed, 910 insertions(+), 21 deletions(-) create mode 100644 xen/arch/x86/acpi/cpufreq/amd-pstate.c
Signature · 1 line
2.34.1
PATCH 1/11 xen/x86: add CPPC feature flag for AMD processors
Add Collaborative Processor Performance Control feature flag for AMD processors.
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series.
There are two types of hardware implementations: "Full MSR Support" and "Shared Memory Support".
Right now, xen will only implement "Full MSR Support", and this new feature flag indicates whether processor has this feature or not.
--- xen/arch/x86/include/asm/cpufeature.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + 2 files changed, 2 insertions(+)
xen/arch/x86/include/asm/cpufeature.h +1 -0
@@ -170,6 +170,7 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_amd_ssbd boot_cpu_has(X86_FEATURE_AMD_SSBD)#define cpu_has_virt_ssbd boot_cpu_has(X86_FEATURE_VIRT_SSBD)#define cpu_has_ssb_no boot_cpu_has(X86_FEATURE_SSB_NO) +#define cpu_has_cppc boot_cpu_has(X86_FEATURE_CPPC)#define cpu_has_auto_ibrs boot_cpu_has(X86_FEATURE_AUTO_IBRS)/* CPUID level 0x00000007:0.edx */
xen/include/public/arch-x86/cpufeatureset.h +1 -0
@@ -265,6 +265,7 @@ XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* Protected Processor Inventory Number XEN_CPUFEATURE(AMD_SSBD, 8*32+24) /*S MSR_SPEC_CTRL.SSBD available */ XEN_CPUFEATURE(VIRT_SSBD, 8*32+25) /*! MSR_VIRT_SPEC_CTRL.SSBD */ XEN_CPUFEATURE(SSB_NO, 8*32+26) /*A Hardware not vulnerable to SSB */ +XEN_CPUFEATURE(CPPC, 8*32+27) /*A Collaborative Processor Performance Control */ XEN_CPUFEATURE(PSFD, 8*32+28) /*S MSR_SPEC_CTRL.PSFD */ XEN_CPUFEATURE(BTC_NO, 8*32+29) /*A Hardware not vulnerable to Branch Type Confusion */XEN_CPUFEATURE (IBPB_RET ,8 *32 +30 )/*A IBPB clears RSB/RAS too. */
Signature · 1 line
2.34.1
Re: [PATCH v1 01/11] xen/x86: add CPPC feature flag for AMD processors
Quoted 11 lines · Penny Zheng
Add Collaborative Processor Performance Control feature flag for AMD processors.
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series.
There are two types of hardware implementations: "Full MSR Support" and "Shared Memory Support".Right now, xen will only implement "Full MSR Support", and this new feature flag indicates whether processor has this feature or not.
Do you have a reference to where this is documented?
Quoted 24 lines
Signed-off-by Penny Zheng <[email protected]>--- xen/arch/x86/include/asm/cpufeature.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + 2 files changed, 2 insertions(+)xen/arch/x86/include/asm/cpufeature.h +1
-0@@ -170,6 +170,7 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_amd_ssbd boot_cpu_has(X86_FEATURE_AMD_SSBD)#define cpu_has_virt_ssbd boot_cpu_has(X86_FEATURE_VIRT_SSBD)#define cpu_has_ssb_no boot_cpu_has(X86_FEATURE_SSB_NO) +#define cpu_has_cppc boot_cpu_has(X86_FEATURE_CPPC)#define cpu_has_auto_ibrs boot_cpu_has(X86_FEATURE_AUTO_IBRS)/* CPUID level 0x00000007:0.edx */ xen/include/public/arch-x86/cpufeatureset.h +0
-0
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* Protected Processor Inventory Number · line 268
Quoted 4 lines
xen/include/public/arch-x86/cpufeatureset.h +1
-0@@ -265,6 +265,7 @@ XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* Protected Processor Inventory Number XEN_CPUFEATURE(AMD_SSBD, 8*32+24) /*S MSR_SPEC_CTRL.SSBD available */ XEN_CPUFEATURE(VIRT_SSBD, 8*32+25) /*! MSR_VIRT_SPEC_CTRL.SSBD */ XEN_CPUFEATURE(SSB_NO, 8*32+26) /*A Hardware not vulnerable to SSB */ +XEN_CPUFEATURE (CPPC ,8 *32 +27 )/*A Collaborative Processor Performance Control */
There needs to be no "A" in this patch, because it's certainly not ready to advertise to guests yet.
If you're intending it for eventual use by guests, then the "A" only goes back in at the end of the series when the support to correctly virtualise it appears, but it looks like it will be Xen-only.
~Andrew
RE: [PATCH v1 01/11] xen/x86: add CPPC feature flag for AMD processors
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 20 lines
-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: Tuesday, December 3, 2024 4:32 PM
To: Penny, Zheng <[email protected]>; [email protected]
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monné <[email protected]>
Subject: Re: [PATCH v1 01/11] xen/x86: add CPPC feature flag for AMD processorsQuoted 8 lines · Penny Zheng
Add Collaborative Processor Performance Control feature flag for AMD processors.
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series. There are two types of hardware implementations: "Full MSR Support" and "Shared Memory Support".
Quoted 5 lines
Quoted 2 lines
Right now, xen will only implement "Full MSR Support", and this new feature flag indicates whether processor has this feature or not.
Do you have a reference to where this is documented?
Yes, https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/24594.pdf
See page 628 for CPUID Fn8000_0008_EBX Extended Feature Identifiers
Quoted 34 lines
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /*
Quoted 26 lines
Signed-off-by Penny Zheng <[email protected]>--- xen/arch/x86/include/asm/cpufeature.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + 2 files changed, 2 insertions(+)xen/arch/x86/include/asm/cpufeature.h +1
-0@@ -170,6 +170,7 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_amd_ssbd boot_cpu_has(X86_FEATURE_AMD_SSBD)#define cpu_has_virt_ssbd boot_cpu_has(X86_FEATURE_VIRT_SSBD)#define cpu_has_ssb_no boot_cpu_has(X86_FEATURE_SSB_NO) +#define cpu_has_cppc boot_cpu_has(X86_FEATURE_CPPC)#define cpu_has_auto_ibrs boot_cpu_has(X86_FEATURE_AUTO_IBRS)/* CPUID level 0x00000007:0.edx */ xen/include/public/arch-x86/cpufeatureset.h +0
-0@@ -265,6 +265,7 @@ XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /*Protected Processor Inventory Number
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* · line 265
Quoted 1 line
xen/include/public/arch-x86/cpufeatureset.h +0
-0XEN_CPUFEATURE (AMD_SSBD ,8 *32 +24 )/*S MSR_SPEC_CTRL.SSBD available */
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* · line 266
Quoted 1 line
xen/include/public/arch-x86/cpufeatureset.h +0
-0XEN_CPUFEATURE (VIRT_SSBD ,8 *32 +25 )/*! MSR_VIRT_SPEC_CTRL.SSBD */
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* · line 267
Quoted 1 line
xen/include/public/arch-x86/cpufeatureset.h +0
-0XEN_CPUFEATURE (SSB_NO ,8 *32 +26 )/*A Hardware not vulnerable to SSB */
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* · line 268
Quoted 1 line
xen/include/public/arch-x86/cpufeatureset.h +1
-0+XEN_CPUFEATURE (CPPC ,8 *32 +27 )/*A Collaborative Processor
Quoted 9 lines
Performance Control */
There needs to be no "A" in this patch, because it's certainly not ready to advertise to guests yet.
If you're intending it for eventual use by guests, then the "A" only goes back in at the end of the series when the support to correctly virtualise it appears, but it looks like it will be Xen-only.
Understood! Thx for the explanation. I'll remove in next Serie
Quoted 1 line
~Andrew
Many thanks,
Penny
PATCH 2/11 xen/x86: introduce new sub-hypercall to get CPPC data
In reply to Penny ZhengIn order to provide backward compatibility with existing governors that represent performance as frequencies, like ondemand, the _CPC table can optionally provide processor frequency range values, Lowest frequency and Norminal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC abstract performance to CPU frequency.
As Xen is uncapable of parsing the ACPI dynamic table, this commit introduces a new sub-hypercall to get required CPPC data from dom0 kernel.
--- xen/arch/x86/platform_hypercall.c | 6 +++ xen/arch/x86/x86_64/cpufreq.c | 18 ++++++++ xen/arch/x86/x86_64/platform_hypercall.c | 3 ++ xen/drivers/cpufreq/cpufreq.c | 50 +++++++++++++++++++++++ xen/include/acpi/cpufreq/processor_perf.h | 1 + xen/include/public/platform.h | 11 +++++ xen/include/xen/pmstat.h | 3 ++ xen/include/xlat.lst | 1 + 8 files changed, 93 insertions(+)
xen/arch/x86/platform_hypercall.c +6 -0
@@ -572,6 +572,12 @@ ret_t do_platform_op(break ; } +case XEN_PM_CPPC : + { + ret =set_cppc_pminfo (op->u.set_pminfo.id, &op->u.set_pminfo.u.cppc_data); + } +break ; +default : ret = -EINVAL ;break ;
xen/arch/x86/x86_64/cpufreq.c +18 -0
@@ -54,3 +54,21 @@ int compat_set_px_pminfo(uint32_t acpi_id,return set_px_pminfo (acpi_id, xen_perf); } + +int compat_set_cppc_pminfo (uint32_t acpi_id, +struct compat_processor_cppc *cppc_data) +{ +struct xen_processor_cppc *xen_cppc; +unsigned long xlat_page_current; + +xlat_malloc_init (xlat_page_current); + + xen_cppc =xlat_malloc_array (xlat_page_current, + struct xen_processor_cppc, 1); +if (unlikely (xen_cppc ==NULL ) ) +return -EFAULT ; + +XLAT_processor_cppc (xen_cppc, cppc_data); + +return set_cppc_pminfo (acpi_id, xen_cppc); +}
xen/arch/x86/x86_64/platform_hypercall.c +3 -0
@@ -20,6 +20,9 @@ EMIT_FILE;#define xen_processor_power compat_processor_power#define set_cx_pminfo compat_set_cx_pminfo +#define xen_processor_cppc compat_processor_cppc +#define set_cppc_pminfo compat_set_cppc_pminfo +#define xen_pf_pcpuinfo xenpf_pcpuinfo CHECK_pf_pcpuinfo;#undef xen_pf_pcpuinfo
xen/drivers/cpufreq/cpufreq.c +50 -0
56 lines not on this page · raw message
xen/include/acpi/cpufreq/processor_perf.h +1 -0
@@ -37,6 +37,7 @@ struct processor_pminfo {uint32_t acpi_id;uint32_t id;struct processor_performance perf; +struct xen_processor_cppc cppc_data; };extern struct processor_pminfo *processor_pminfo[NR_CPUS ];
xen/include/public/platform.h +11 -0
@@ -363,6 +363,7 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_getidletime_t);#define XEN_PM_PX 1#define XEN_PM_TX 2#define XEN_PM_PDC 3 +#define XEN_PM_CPPC 4/* Px sub info type */ #define XEN_PX_PCT 1
@@ -432,6 +433,15 @@ struct xen_processor_px {typedef struct xen_processor_px xen_processor_px_t ;DEFINE_XEN_GUEST_HANDLE (xen_processor_px_t); +struct xen_processor_cppc { +uint32_t highest_perf; +uint32_t nominal_perf; +uint32_t lowest_perf; +uint32_t lowest_nonlinear_perf; +uint32_t lowest_freq; +uint32_t nominal_freq; +}; +struct xen_psd_package {uint64_t num_entries;uint64_t revision;
@@ -465,6 +475,7 @@ struct xenpf_set_processor_pminfo {struct xen_processor_power power;/* Cx: _CST/_CSD */ struct xen_processor_performance perf;/* Px: _PPC/_PCT/_PSS/_PSD */ XEN_GUEST_HANDLE (uint32) pdc;/* _PDC */ +struct xen_processor_cppc cppc_data;/*_CPC */ } u; };typedef struct xenpf_set_processor_pminfo xenpf_set_processor_pminfo_t ;
xen/include/xen/pmstat.h +3 -0
@@ -5,6 +5,7 @@#include <public/platform.h> /* for struct xen_processor_power */ #include <public/sysctl.h> /* for struct pm_cx_stat */ +int set_cppc_pminfo (uint32_t cpu,struct xen_processor_cppc *cppc_data);int set_px_pminfo (uint32_t acpi_id,struct xen_processor_performance *perf);long set_cx_pminfo (uint32_t acpi_id,struct xen_processor_power *power);
@@ -13,6 +14,8 @@ struct compat_processor_performance;int compat_set_px_pminfo (uint32_t acpi_id,struct compat_processor_performance *perf);struct compat_processor_power ;long compat_set_cx_pminfo (uint32_t acpi_id,struct compat_processor_power *power); +struct compat_processor_cppc ; +int compat_set_cppc_pminfo (uint32_t acpi_id,struct compat_processor_cppc *cppc);#endif uint32_t pmstat_get_cx_nr (unsigned int cpu);
xen/include/xlat.lst +1 -0
@@ -166,6 +166,7 @@ ! processor_cx platform.h ! processor_flags platform.h ! processor_performance platform.h +! processor_cppc platform.h ! processor_power platform.h ? processor_px platform.h ! psd_package platform.h
Signature · 1 line
2.34.1
Re: [PATCH v1 02/11] xen/x86: introduce new sub-hypercall to get CPPC data
Quoted 10 lines · Penny Zheng
In order to provide backward compatibility with existing governors that represent performance as frequencies, like ondemand, the
_CPCtable can optionally provide processor frequency range values, Lowest frequency and Norminal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC abstract performance to CPU frequency.As Xen is uncapable of parsing the ACPI dynamic table, this commit introduces a new sub-hypercall to get required CPPC data from dom0 kernel.
"get" as used both here and in the title is, to me, something an entity does actively. Xen is entirely passive here, though. (Reading the title I was first assuming this is about a sub-op to get certain data out of Xen.)
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 11 lines
xen/arch/x86/platform_hypercall.c +5
-0@@ -572,6 +572,12 @@ ret_t do_platform_op(break ; } +case XEN_PM_CPPC : + { + ret =set_cppc_pminfo (op->u.set_pminfo.id, &op->u.set_pminfo.u.cppc_data); + } +break ;
No such unnecessary figure braces please, which - once dropped - will also call for "break" to be indented one level deeper.
Quoted 14 lines
xen/arch/x86/x86_64/cpufreq.c +8
-0@@ -54,3 +54,21 @@ int compat_set_px_pminfo(uint32_t acpi_id,return set_px_pminfo (acpi_id, xen_perf); } + +int compat_set_cppc_pminfo (uint32_t acpi_id, +struct compat_processor_cppc *cppc_data) +{ +struct xen_processor_cppc *xen_cppc; +unsigned long xlat_page_current; + +xlat_malloc_init (xlat_page_current);
xen/arch/x86/x86_64/cpufreq.c int compat_set_px_pminfo(uint32_t acpi_id, · line 74
Quoted 10 lines
xen/arch/x86/x86_64/cpufreq.c +10
-0+ + xen_cppc =xlat_malloc_array (xlat_page_current, + struct xen_processor_cppc, 1); +if (unlikely (xen_cppc ==NULL ) ) +return -EFAULT ; + +XLAT_processor_cppc (xen_cppc, cppc_data); + +return set_cppc_pminfo (acpi_id, xen_cppc); +}
Why's this needed? The structure - for now at least - consists of only uint32_t-s, and hence has identical layout for compat callers.
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 461
Quoted 7 lines
xen/drivers/cpufreq/cpufreq.c +1
-0@@ -458,6 +458,56 @@ static void print_PPC(unsigned int platform_limit)printk ("\t_PPC: %d\n" , platform_limit); } +static void print_CPPC (struct xen_processor_cppc *cppc_data)
Pointer-to-const?
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 471
Quoted 10 lines
xen/drivers/cpufreq/cpufreq.c +10
-0+{ +printk ("\t_CPC: highest_perf=%u, lowest_perf=%u, " +"nominal_perf=%u, lowest_nonlinear_perf=%u, " +"nominal_freq=%uMhz, lowest_freq=%uMhz\n" , + cppc_data->highest_perf, cppc_data->lowest_perf, + cppc_data->nominal_perf, cppc_data->lowest_nonlinear_perf, + cppc_data->nominal_freq, cppc_data->lowest_freq); +} + +int set_cppc_pminfo (uint32_t acpi_id,struct xen_processor_cppc *cppc_data)
Pointer-to-const?
Quoted 8 lines
xen/drivers/cpufreq/cpufreq.c +8
-0+{ +int ret =0 , cpuid; +struct processor_pminfo *pm_info; + + cpuid =get_cpu_id (acpi_id); +if ( cpuid <0 || !cppc_data ) + { + ret = -EINVAL ;
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 489
Quoted 10 lines
xen/drivers/cpufreq/cpufreq.c +10
-0+ goto out; + } +if ( cpufreq_verbose ) +printk ("Set CPU acpi_id(%d) cpuid(%d) CPPC State info:\n" , + acpi_id, cpuid); + + pm_info = processor_pminfo[cpuid]; +if ( !pm_info ) + { + pm_info = xzalloc(struct processor_pminfo);
Please be aware that new code is supposed to be using xvmalloc().
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 502
Quoted 13 lines
xen/drivers/cpufreq/cpufreq.c +13
-0+if ( !pm_info ) + { + ret = -ENOMEM ; + goto out; + } + processor_pminfo[cpuid] = pm_info; + } + pm_info->acpi_id = acpi_id; + pm_info->id = cpuid; + +memcpy ((void *)&pm_info->cppc_data, + (void *)cppc_data, +sizeof (struct xen_processor_cppc ));
What use are these casts? Also please no blank before the opening parenthesis of a function call, and please sizeof(*cppc_data). Yet then - why memcpy() in the first place? This can be a (type safe) structure assignment, can't it?
Quoted 12 lines
xen/include/public/platform.h +1
-0@@ -363,6 +363,7 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_getidletime_t);#define XEN_PM_PX 1#define XEN_PM_TX 2#define XEN_PM_PDC 3 +#define XEN_PM_CPPC 4/* Px sub info type */ #define XEN_PX_PCT 1@@ -432,6 +433,15 @@ struct xen_processor_px {typedef struct xen_processor_px xen_processor_px_t ;
xen/include/public/platform.h struct xen_processor_px { · line 443
Quoted 10 lines
xen/include/public/platform.h +8
-0DEFINE_XEN_GUEST_HANDLE (xen_processor_px_t); +struct xen_processor_cppc { +uint32_t highest_perf; +uint32_t nominal_perf; +uint32_t lowest_perf; +uint32_t lowest_nonlinear_perf; +uint32_t lowest_freq; +uint32_t nominal_freq; +};
_CPC contains a lot more data. Please clarify on what basis this subset was chosen. (Keeping the chosen fields in the order _CPC has them might also be a good idea.)
xen/include/xlat.lst line 172
Quoted 10 lines
xen/include/xlat.lst +1
-0@@ -166,6 +166,7 @@ ! processor_cx platform.h ! processor_flags platform.h ! processor_performance platform.h +! processor_cppc platform.h ! processor_power platform.h ? processor_px platform.h ! psd_package platform.h
Please obey to alphabetic sorting. As per an earlier comment I also expect this wants to be using '?' in place of '!'.
Jan
RE: [PATCH v1 02/11] xen/x86: introduce new sub-hypercall to get CPPC data
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 21 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 5:46 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 02/11] xen/x86: introduce new sub-hypercall to get CPPC dataQuoted 7 lines · Penny Zheng
In order to provide backward compatibility with existing governors that represent performance as frequencies, like ondemand, the
_CPCtable can optionally provide processor frequency range values, Lowest frequency and Norminal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC abstract performance to CPU frequency.
Quoted 8 lines
Quoted 3 lines
As Xen is uncapable of parsing the ACPI dynamic table, this commit introduces a new sub-hypercall to get required CPPC data from dom0 kernel.
"get" as used both here and in the title is, to me, something an entity does actively. Xen is entirely passive here, though. (Reading the title I was first assuming this is about a sub-op to get certain data out of
Xen.)
How about "a new sub-hypercall to pass/deliver CPPC to Xen"? or any better suggestions?
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/platform_hypercall.c +0
-0@@ -572,6 +572,12 @@ ret_t do_platform_op(break ; }
Quoted 9 lines
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 6 lines
xen/arch/x86/platform_hypercall.c +5
-0+case XEN_PM_CPPC : + { + ret =set_cppc_pminfo (op->u.set_pminfo.id, &op- u.set_pminfo.u.cppc_data); + } +break ;No such unnecessary figure braces please, which - once dropped - will also call for "break" to be indented one level deeper.
Understood.
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/x86_64/cpufreq.c +11
-0@@ -54,3 +54,21 @@ int compat_set_px_pminfo(uint32_t acpi_id,return set_px_pminfo (acpi_id, xen_perf); } + +int compat_set_cppc_pminfo (uint32_t acpi_id, +struct compat_processor_cppc *cppc_data) { +struct xen_processor_cppc *xen_cppc; +unsigned long xlat_page_current; + +xlat_malloc_init (xlat_page_current); + + xen_cppc =xlat_malloc_array (xlat_page_current, + struct xen_processor_cppc, 1); +if (unlikely (xen_cppc ==NULL ) )
Quoted 9 lines
xen/arch/x86/x86_64/cpufreq.c int compat_set_px_pminfo(uint32_t acpi_id,
Quoted 5 lines
xen/arch/x86/x86_64/cpufreq.c +5
-0+return -EFAULT ; + +XLAT_processor_cppc (xen_cppc, cppc_data); + +return set_cppc_pminfo (acpi_id, xen_cppc); }Why's this needed? The structure - for now at least - consists of only uint32_t-s, and hence has identical layout for compat callers.
Understood. Not familiar with the compat framework
Quoted 9 lines
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 460
Quoted 6 lines
xen/drivers/cpufreq/cpufreq.c +1
-0@@ -458,6 +458,56 @@ static void print_PPC(unsigned int platform_limit)printk ("\t_PPC: %d\n" , platform_limit); } +static void print_CPPC (struct xen_processor_cppc *cppc_data)Pointer-to-const?
Sure.
Quoted 4 lines
Quoted 4 lines
xen/drivers/cpufreq/cpufreq.c +4
-0+{ + printk("\t_CPC: highest_perf=%u, lowest_perf=%u, " + "nominal_perf=%u, lowest_nonlinear_perf=%u, " + "nominal_freq=%uMhz, lowest_freq=%uMhz\n",
Quoted 9 lines
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit)
Quoted 6 lines
xen/drivers/cpufreq/cpufreq.c +6
-0+ cppc_data->highest_perf, cppc_data->lowest_perf, + cppc_data->nominal_perf, cppc_data->lowest_nonlinear_perf, + cppc_data->nominal_freq, cppc_data->lowest_freq); } + +int set_cppc_pminfo (uint32_t acpi_id,struct xen_processor_cppc +*cppc_data)Pointer-to-const?
Sure.
Quoted 12 lines
Quoted 12 lines
xen/drivers/cpufreq/cpufreq.c +12
-0+{ +int ret =0 , cpuid; +struct processor_pminfo *pm_info; + + cpuid =get_cpu_id (acpi_id); +if ( cpuid <0 || !cppc_data ) + { + ret = -EINVAL ; + goto out; + } +if ( cpufreq_verbose ) + printk("Set CPU acpi_id(%d) cpuid(%d) CPPC State info:\n",
Quoted 8 lines
xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 489
Quoted 6 lines
xen/drivers/cpufreq/cpufreq.c +6
-0+ acpi_id, cpuid); + + pm_info = processor_pminfo[cpuid]; +if ( !pm_info ) + { + pm_info = xzalloc(struct processor_pminfo);Please be aware that new code is supposed to be using
xvmalloc().
Thanks for the update.
Quoted 15 lines
Quoted 4 lines
xen/drivers/cpufreq/cpufreq.c +3
-0+if ( !pm_info ) + { + ret = -ENOMEM ;xen/drivers/cpufreq/cpufreq.c static void print_PPC(unsigned int platform_limit) · line 502
Quoted 10 lines
xen/drivers/cpufreq/cpufreq.c +10
-0+ goto out; + } + processor_pminfo[cpuid] = pm_info; + } + pm_info->acpi_id = acpi_id; + pm_info->id = cpuid; + +memcpy ((void *)&pm_info->cppc_data, + (void *)cppc_data, +sizeof (struct xen_processor_cppc ));
Quoted 4 lines
What use are these casts? Also please no blank before the opening parenthesis of a function call, and please
sizeof(*cppc_data). Yet then - whymemcpy()in the first place? This can be a (type safe) structure assignment, can't it?
Yes, it can be a (type safe) structure assignment
Quoted 14 lines
Quoted 14 lines
xen/include/public/platform.h +1
-0@@ -363,6 +363,7 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_getidletime_t);#define XEN_PM_PX 1#define XEN_PM_TX 2#define XEN_PM_PDC 3 +#define XEN_PM_CPPC 4/* Px sub info type */ #define XEN_PX_PCT 1@@ -432,6 +433,15 @@ struct xen_processor_px { typedef structxen_processor_px xen_processor_px_t;DEFINE_XEN_GUEST_HANDLE (xen_processor_px_t);
Quoted 13 lines
xen/include/public/platform.h struct xen_processor_px { · line 443
Quoted 8 lines
xen/include/public/platform.h +8
-0+struct xen_processor_cppc { +uint32_t highest_perf; +uint32_t nominal_perf; +uint32_t lowest_perf; +uint32_t lowest_nonlinear_perf; +uint32_t lowest_freq; +uint32_t nominal_freq; +};
_CPCcontains a lot more data. Please clarify on what basis this subset was chosen. (Keeping the chosen fields in the order_CPChas them might also be a good idea.)
I'll comment with "
/* Subset fields useful for CPPC-compatible cpufreq driver's initialization */ "
I'll stay the same order with the _CPC has them
Quoted 5 lines
Quoted 5 lines
xen/include/xlat.lst +0
-0@@ -166,6 +166,7 @@ ! processor_cx platform.h ! processor_flags platform.h
Quoted 9 lines
xen/include/xlat.lst line 172
Quoted 5 lines
xen/include/xlat.lst +1
-0! processor_performance platform.h +! processor_cppc platform.h ! processor_power platform.h ? processor_px platform.h ! psd_package platform.hPlease obey to alphabetic sorting. As per an earlier comment I also expect this wants to be using '?' in place of '!'.
Sure.
Quoted 1 line
Jan
Many thanks
Penny
Re: [PATCH v1 02/11] xen/x86: introduce new sub-hypercall to get CPPC data
Quoted 16 lines · Zheng
Quoted 16 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 5:46 PMQuoted 10 lines · Penny Zheng
In order to provide backward compatibility with existing governors that represent performance as frequencies, like ondemand, the
_CPCtable can optionally provide processor frequency range values, Lowest frequency and Norminal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC abstract performance to CPU frequency.As Xen is uncapable of parsing the ACPI dynamic table, this commit introduces a new sub-hypercall to get required CPPC data from dom0 kernel.
Quoted 6 lines
Quoted 4 lines
"get" as used both here and in the title is, to me, something an entity does actively. Xen is entirely passive here, though. (Reading the title I was first assuming this is about a sub-op to get certain data out of
Xen.)How about "a new sub-hypercall to pass/deliver CPPC to Xen"? or any better suggestions?
That or "propagate" or "report" (and perhaps there are yet more alternatives).
Jan
PATCH 3/11 xen/x86: introduce "cpufreq=amd-pstate" xen cmdline
In reply to Penny ZhengUsers need to set "cpufreq=amd-pstate" in xen cmdline to enable amd-pstate driver, which selects ACPI Collaborative Performance and Power Control (CPPC) on supported AMD hardware to provide a finer grained frequency control mechanism.verbose option can also be included to support verbose print.
When users setting "cpufreq=amd-pstate", a new amd-pstate driver shall be registered and used. Actual implmentation will be introduced in the following commits.
--- docs/misc/xen-command-line.pandoc | 8 +++- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-pstate.c | 66 ++++++++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 28 +++++++++++ xen/arch/x86/platform_hypercall.c | 6 +++ xen/drivers/cpufreq/cpufreq.c | 13 ++++- xen/include/acpi/cpufreq/cpufreq.h | 4 ++ xen/include/public/platform.h | 1 + xen/include/public/sysctl.h | 1 + 9 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 xen/arch/x86/acpi/cpufreq/amd-pstate.c
docs/misc/xen-command-line.pandoc +6 -2
@@ -499,7 +499,7 @@ If set, force use of the performance counters for oprofile, rather than detectin available support. ### cpufreq -> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } [,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]]` +> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } [,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | amd-pstate[:[verbose]]` > Default: `xen`
@@ -510,7 +510,7 @@ choice of `dom0-kernel` is deprecated and not supported by all Dom0 kernels. * `<maxfreq>` and `<minfreq>` are integers which represent max and min processor frequencies respectively. * `verbose` option can be included as a string or also as `verbose=<integer>` - for `xen`. It is a boolean for `hwp`. + for `xen`. It is a boolean for `hwp` and `amd-pstate`. * `hwp` selects Hardware-Controlled Performance States (HWP) on supported Intel hardware. HWP is a Skylake+ feature which provides better CPU power management. The default is disabled. If `hwp` is selected, but hardware
@@ -518,6 +518,10 @@ choice of `dom0-kernel` is deprecated and not supported by all Dom0 kernels. * `<hdc>` is a boolean to enable Hardware Duty Cycling (HDC). HDC enables the processor to autonomously force physical package components into idle state. The default is enabled, but the option only applies when `hwp` is enabled. +* `amd-pstate` selects ACPI Collaborative Performance and Power Control (CPPC) + on supported AMD hardware to provide a finer grained frequency control mechanism. + The default is disabled. If `amd-pstate` is selected, but hardware support + is not available, Xen will fallback to cpufreq=xen. There is also support for `;`-separated fallback options: `cpufreq=hwp;xen,verbose`. This first tries `hwp` and falls back to `xen` if
xen/arch/x86/acpi/cpufreq/Makefile +1 -0
@@ -1,4 +1,5 @@ obj-$(CONFIG_INTEL) += acpi.o obj-y += cpufreq.o +obj-y += amd-pstate.o obj-$(CONFIG_INTEL) += hwp.o obj-$(CONFIG_AMD) += powernow.o
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +66 -0
66 lines not on this page · raw message
xen/arch/x86/acpi/cpufreq/cpufreq.c +28 -0
@@ -148,6 +148,9 @@ static int __init cf_check cpufreq_driver_init(void)case CPUFREQ_none: ret =0 ;break ; +default : +printk (XENLOG_WARNING "Unsupported cpufreq driver for vendor Intel\n" ); +break ; }if ( ret != -ENODEV )
@@ -156,6 +159,31 @@ static int __init cf_check cpufreq_driver_init(void)break ;case X86_VENDOR_AMD : + ret = -ENOENT ; + +for (unsigned int i =0 ; i < cpufreq_xen_cnt; i++ ) + { +switch ( cpufreq_xen_opts[i] ) + { +case CPUFREQ_xen: + ret =powernow_register_driver (); +break ; +case CPUFREQ_amd_pstate: + ret =amd_pstate_register_driver (); +break ; +case CPUFREQ_none: + ret =0 ; +break ; +default : +printk (XENLOG_WARNING "Unsupported cpufreq driver for vendor AMD\n" ); +break ; + } + +if ( ret != -ENODEV ) +break ; + } +break ; +case X86_VENDOR_HYGON : ret =IS_ENABLED (CONFIG_AMD ) ?powernow_register_driver () : -ENODEV ;break ;
xen/arch/x86/platform_hypercall.c +6 -0
@@ -574,6 +574,12 @@ ret_t do_platform_op(case XEN_PM_CPPC : { +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { + ret = -ENOSYS ; +break ; + } + ret =set_cppc_pminfo (op->u.set_pminfo.id, &op->u.set_pminfo.u.cppc_data); }break ;
xen/drivers/cpufreq/cpufreq.c +11 -2
@@ -84,7 +84,7 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( choice <0 && !cmdline_strcmp (str,"dom0-kernel" ) ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC ); cpufreq_controller = FREQCTL_dom0_kernel; opt_dom0_vcpus_pin =1 ;return 0 ;
@@ -92,7 +92,7 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( choice ==0 || !cmdline_strcmp (str,"none" ) ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC ); cpufreq_controller = FREQCTL_none;return 0 ; }
@@ -130,6 +130,15 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( arg[0 ] && arg[1 ] ) ret =hwp_cmdline_parse (arg +1 , end); } +else if ( choice <0 && !cmdline_strcmp (str,"amd-pstate" ) ) + { + xen_processor_pmbits |=XEN_PROCESSOR_PM_CPPC ; + cpufreq_controller = FREQCTL_xen; + cpufreq_xen_opts[cpufreq_xen_cnt++] = CPUFREQ_amd_pstate; + ret =0 ; +if ( arg[0 ] && arg[1 ] ) + ret =amd_pstate_cmdline_parse (arg +1 , end); + }else ret = -EINVAL ;
xen/include/acpi/cpufreq/cpufreq.h +4 -0
@@ -28,6 +28,7 @@ enum cpufreq_xen_opt { CPUFREQ_none, CPUFREQ_xen, CPUFREQ_hwp, + CPUFREQ_amd_pstate, };extern enum cpufreq_xen_opt cpufreq_xen_opts[2 ];extern unsigned int cpufreq_xen_cnt;
@@ -267,4 +268,7 @@ int set_hwp_para(struct cpufreq_policy *policy,int acpi_cpufreq_register (void ); +int amd_pstate_cmdline_parse (const char *s,const char *e); +int amd_pstate_register_driver (void ); +#endif /* __XEN_CPUFREQ_PM_H__ */
xen/include/public/platform.h +1 -0
@@ -357,6 +357,7 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_getidletime_t);#define XEN_PROCESSOR_PM_CX 1#define XEN_PROCESSOR_PM_PX 2#define XEN_PROCESSOR_PM_TX 4 +#define XEN_PROCESSOR_PM_CPPC 8/* cmd type */ #define XEN_PM_CX 0
xen/include/public/sysctl.h +1 -0
@@ -424,6 +424,7 @@ struct xen_set_cppc_para { };#define XEN_HWP_DRIVER_NAME "hwp" +#define XEN_AMD_PSTATE_DRIVER_NAME "amd-pstate"/* * cpufreq para name of this structure named
Signature · 1 line
2.34.1
Re: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdline
xen/arch/x86/acpi/cpufreq/Makefile line 5
Quoted 8 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/Makefile +1
-0@@ -1,4 +1,5 @@ obj-$(CONFIG_INTEL) += acpi.o obj-y += cpufreq.o +obj-y += amd-pstate.o obj-$(CONFIG_INTEL) += hwp.o obj-$(CONFIG_AMD) += powernow.o
Please obey to alphabetic sorting.
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +3
-0@@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* amd-pstate.c - AMD Processor P-state Frequency Driver
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 13
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +10
-0+* +* Copyright (C) 2024 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Penny Zheng <[email protected]> +* +* AMD P-State introduces a new CPU performance scaling design for AMD +* processors using the ACPI Collaborative Performance and Power Control (CPPC) +* feature which provides a finer grained frequency control range. +* +*/
Nit: Unnecessary empty comment line at the end.
Quoted 31 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +31
-0+#include <xen/init.h> +#include <xen/param.h> +#include <acpi/cpufreq/cpufreq.h> + +uint16_t __read_mostly dmi_max_speed_mhz; + +static bool __init amd_pstate_handle_option (const char *s,const char *end) +{ +int ret; + + ret =parse_boolean ("verbose" , s, end); +if ( ret >=0 ) + { + cpufreq_verbose = ret; +return true; + } + +return false; +} + +int __init amd_pstate_cmdline_parse (const char *s,const char *e) +{ + do + { + const char *end = strpbrk(s, ",;"); + + if ( !amd_pstate_handle_option(s, end) ) + { + printk(XENLOG_WARNING "cpufreq/amd-pstate: option '%.*s' not recognized\n", + (int)((end ?: e) - s), s); +
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 55
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +10
-0+return -EINVAL ; + } + + s = end ? ++end : end; + }while ( s && s < e ); + +return 0 ; +} + +static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver =
__initconst_cf_clobber
xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void) · line 152
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +2
-0@@ -148,6 +148,9 @@ static int __init cf_check cpufreq_driver_init(void)case CPUFREQ_none: ret =0 ;break ; +default : +printk (XENLOG_WARNING "Unsupported cpufreq driver for vendor Intel\n" );
Too long line (the format string itself shall be kept all on one line, but the XENLOG_WARNING doesn't need to).
Quoted 22 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +18
-0@@ -156,6 +159,31 @@ static int __init cf_check cpufreq_driver_init(void)break ;case X86_VENDOR_AMD : + ret = -ENOENT ; + +for (unsigned int i =0 ; i < cpufreq_xen_cnt; i++ ) + { +switch ( cpufreq_xen_opts[i] ) + { +case CPUFREQ_xen: + ret =powernow_register_driver (); +break ; +case CPUFREQ_amd_pstate: + ret =amd_pstate_register_driver (); +break ; +case CPUFREQ_none: + ret =0 ; +break ; +default : +printk (XENLOG_WARNING "Unsupported cpufreq driver for vendor AMD\n" ); +break ;
xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void) · line 189
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +7
-0+ } + +if ( ret != -ENODEV ) +break ; + } +break ; +case X86_VENDOR_HYGON : ret =IS_ENABLED (CONFIG_AMD ) ?powernow_register_driver () : -ENODEV ;break ;
Is the code to handle CPPC not applicable to Hygon CPUs?
What about the IS_ENABLED(CONFIG_AMD) that the original code had? Don't you need to mirror this in some way?
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 9 lines
xen/arch/x86/platform_hypercall.c +3
-0@@ -574,6 +574,12 @@ ret_t do_platform_op(case XEN_PM_CPPC : { +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { + ret = -ENOSYS ;
ENOSYS isn't appropriate for such a situation.
Quoted 12 lines
xen/drivers/cpufreq/cpufreq.c +1
-1@@ -84,7 +84,7 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( choice <0 && !cmdline_strcmp (str,"dom0-kernel" ) ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC ); cpufreq_controller = FREQCTL_dom0_kernel; opt_dom0_vcpus_pin =1 ;return 0 ;
xen/drivers/cpufreq/cpufreq.c static int __init cf_check setup_cpufreq_option(const char *str) · line 95
Quoted 5 lines
xen/drivers/cpufreq/cpufreq.c +1
-1@@ -92,7 +92,7 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( choice ==0 || !cmdline_strcmp (str,"none" ) ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC );
Nit (style): Blanks please around binary operators.
xen/include/acpi/cpufreq/cpufreq.h enum cpufreq_xen_opt { · line 31
Quoted 7 lines
xen/include/acpi/cpufreq/cpufreq.h +1
-0@@ -28,6 +28,7 @@ enum cpufreq_xen_opt { CPUFREQ_none, CPUFREQ_xen, CPUFREQ_hwp, + CPUFREQ_amd_pstate,
Might this better be CPUFREQ_amd_cppc? "pstate" may mean various methods.
xen/include/public/sysctl.h struct xen_set_cppc_para { · line 427
Quoted 7 lines
xen/include/public/sysctl.h +1
-0@@ -424,6 +424,7 @@ struct xen_set_cppc_para { };#define XEN_HWP_DRIVER_NAME "hwp" +#define XEN_AMD_PSTATE_DRIVER_NAME "amd-pstate"
Similarly here.
Jan
RE: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdline
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 14 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 5:59 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Roger Pau Monné <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdlineOn 03.12.2024 09:11, Penny Zheng wrote:
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/Makefile line 5
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/Makefile +1
-0@@ -1,4 +1,5 @@ obj-$(CONFIG_INTEL) += acpi.o obj-y += cpufreq.o +obj-y += amd-pstate.o obj-$(CONFIG_INTEL) += hwp.o obj-$(CONFIG_AMD) += powernow.oPlease obey to alphabetic sorting.
Sure, and I'll also strict it with CONFIG_AMD
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +9
-0@@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* amd-pstate.c - AMD Processor P-state Frequency Driver +* +* Copyright (C) 2024 Advanced Micro Devices, Inc. All Rights Reserved. +* +* Author: Penny Zheng <[email protected]> +* +* AMD P-State introduces a new CPU performance scaling design for
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 13
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +6
-0+AMD +* processors using the ACPI Collaborative Performance and Power +Control (CPPC) +* feature which provides a finer grained frequency control range. +* +*/ Nit: Unnecessary empty comment line at the end.
Ack
Quoted 30 lines
Quoted 19 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +18
-0+#include <xen/init.h> +#include <xen/param.h> +#include <acpi/cpufreq/cpufreq.h> + +uint16_t __read_mostly dmi_max_speed_mhz; + +static bool __init amd_pstate_handle_option (const char *s,const char +*end) { +int ret; + + ret =parse_boolean ("verbose" , s, end); +if ( ret >=0 ) + { + cpufreq_verbose = ret; +return true; + } + +return false;xen/arch/x86/acpi/cpufreq/amd-pstate.c
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +10
-0+} + +int __init amd_pstate_cmdline_parse(const char *s, const char *e) { + do + { + const char *end = strpbrk(s, ",;"); + + if ( !amd_pstate_handle_option(s, end) ) + { + printk(XENLOG_WARNING "cpufreq/amd-pstate: option '%.*s' notrecognized\n",
Quoted 16 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c
Quoted 13 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c new file +13
-0+ (int)((end ?: e) - s), s); + +return -EINVAL ; + } + + s = end ? ++end : end; + }while ( s && s < e ); + +return 0 ; +} + +static const struct cpufreq_driver __initconstrel +amd_pstate_cpufreq_driver =
__initconst_cf_clobber
Ack
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +0
-0@@ -148,6 +148,9 @@ static int __init cf_check cpufreq_driver_init(void)case CPUFREQ_none:
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void)
Quoted 5 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +3
-0ret =0 ;break ; +default : + printk(XENLOG_WARNING "Unsupported cpufreq driver + for vendor Intel\n");Too long line (the format string itself shall be kept all on one line, but the
XENLOG_WARNINGdoesn't need to).
Understood
Quoted 33 lines
Quoted 11 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +7
-0@@ -156,6 +159,31 @@ static int __init cf_check cpufreq_driver_init(void)break ;case X86_VENDOR_AMD : + ret = -ENOENT ; + +for (unsigned int i =0 ; i < cpufreq_xen_cnt; i++ ) + { +switch ( cpufreq_xen_opts[i] ) + { +case CPUFREQ_xen:xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void) · line 178
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +10
-0+ ret =powernow_register_driver (); +break ; +case CPUFREQ_amd_pstate: + ret =amd_pstate_register_driver (); +break ; +case CPUFREQ_none: + ret =0 ; +break ; +default : + printk(XENLOG_WARNING "Unsupported cpufreq driver forvendor AMD\n");
xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void)
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c +8
-0+break ; + } + +if ( ret != -ENODEV ) +break ; + } +break ; +case X86_VENDOR_HYGON : ret = IS_ENABLED(CONFIG_AMD) ? powernow_register_driver() : -ENODEV;
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/cpufreq.c static int __init cf_check cpufreq_driver_init(void) · line 189
Quoted 1 line
xen/arch/x86/acpi/cpufreq/cpufreq.c +0
-0break ;Is the code to handle CPPC not applicable to Hygon CPUs?
What about the
IS_ENABLED(CONFIG_AMD)that the original code had? Don't you need to mirror this in some way?
Googling the Hygon cpu, as it is based on Zen serie and also amd-pstate feature is developed for Zen serie for the first place, the new switch-case code shall apply to Hygon CPUs too
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/platform_hypercall.c +0
-0@@ -574,6 +574,12 @@ ret_t do_platform_op(
Quoted 8 lines
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 5 lines
xen/arch/x86/platform_hypercall.c +3
-0case XEN_PM_CPPC : { +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { + ret = -ENOSYS ;ENOSYS isn't appropriate for such a situation.
I've mirrored the return value, so maybe -EINVAL is better?
Quoted 16 lines
Quoted 16 lines
xen/drivers/cpufreq/cpufreq.c +2
-1@@ -84,7 +84,7 @@ static int __init cf_check setup_cpufreq_option(const char *str) if ( choice < 0 && !cmdline_strcmp(str, "dom0-kernel") ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= + ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC ); cpufreq_controller = FREQCTL_dom0_kernel; opt_dom0_vcpus_pin =1 ;return 0 ;@@ -92,7 +92,7 @@ static int __init cf_check setup_cpufreq_option(const char *str)
Quoted 7 lines
xen/drivers/cpufreq/cpufreq.c static int __init cf_check setup_cpufreq_option(const char *str)
Quoted 5 lines
xen/drivers/cpufreq/cpufreq.c +2
-1if ( choice ==0 || !cmdline_strcmp (str,"none" ) ) { - xen_processor_pmbits &= ~XEN_PROCESSOR_PM_PX ; + xen_processor_pmbits &= + ~(XEN_PROCESSOR_PM_PX |XEN_PROCESSOR_PM_CPPC );Nit (style): Blanks please around binary operators.
Ack
Quoted 11 lines
xen/include/acpi/cpufreq/cpufreq.h enum cpufreq_xen_opt { · line 31
Quoted 8 lines
xen/include/acpi/cpufreq/cpufreq.h +1
-0@@ -28,6 +28,7 @@ enum cpufreq_xen_opt { CPUFREQ_none, CPUFREQ_xen, CPUFREQ_hwp, + CPUFREQ_amd_pstate,Might this better be
CPUFREQ_amd_cppc? "pstate" may mean various methods.
Okay. I'll change all amd_/-pstate defined values to amd_/-cppc
Quoted 9 lines
xen/include/public/sysctl.h struct xen_set_cppc_para { }; · line 426
Quoted 6 lines
xen/include/public/sysctl.h +1
-0@@ -424,6 +424,7 @@ struct xen_set_cppc_para { };#define XEN_HWP_DRIVER_NAME "hwp" +#define XEN_AMD_PSTATE_DRIVER_NAME "amd-pstate"Similarly here.
Ack
Quoted 1 line
Jan
Mant thanks
Penny
Re: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdline
Quoted 17 lines · Zheng
Quoted 9 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 5:59 PMQuoted 4 lines · Penny Zheng
xen/arch/x86/platform_hypercall.c +0
-0@@ -574,6 +574,12 @@ ret_t do_platform_op(Quoted 7 lines
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 5 lines
xen/arch/x86/platform_hypercall.c +3
-0case XEN_PM_CPPC : { +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { + ret = -ENOSYS ;ENOSYS isn't appropriate for such a situation.
Quoted 1 line
I've mirrored the return value, so maybe
-EINVALis better?
Generally most wrong uses of ENOSYS want replacing by EOPNOTSUPP.
Jan
RE: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdline
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 31 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Wednesday, January 15, 2025 4:32 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Roger Pau Monné <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 03/11] xen/x86: introduce "cpufreq=amd-pstate" xen cmdlineQuoted 17 lines · Zheng
Quoted 16 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 5:59 PMxen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 579
Quoted 9 lines · Penny Zheng
xen/arch/x86/platform_hypercall.c +3
-0@@ -574,6 +574,12 @@ ret_t do_platform_op(case XEN_PM_CPPC : { +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { + ret = -ENOSYS ;ENOSYS isn't appropriate for such a situation.
Quoted 3 lines
Quoted 1 line
I've mirrored the return value, so maybe
-EINVALis better?Generally most wrong uses of ENOSYS want replacing by EOPNOTSUPP.
Oh, understood.
Quoted 2 lines
Jan
Many thanks,
Penny
PATCH 4/11 xen/x86: get processor max speed from DMI table
In reply to Penny ZhengWhen _CPC table could not provide processor frequency range values for OS governor, we need to read processor max frequency as anchor point.
For AMD processors, we rely on parsing DMI table to get processor max speed.
--- xen/arch/x86/dmi_scan.c | 19 +++++++++++++++++++ xen/include/acpi/cpufreq/cpufreq.h | 2 ++ xen/include/xen/dmi.h | 5 +++++ 3 files changed, 26 insertions(+)
xen/arch/x86/dmi_scan.c +19 -0
@@ -10,6 +10,8 @@#include <xen/efi.h> #include <xen/pci.h> #include <xen/pci_regs.h> +#include <xen/unaligned.h> +#include <acpi/cpufreq/cpufreq.h> #define memcpy_fromio memcpy#define alloc_bootmem (l) xmalloc_bytes(l)
@@ -680,6 +682,23 @@ static void __init cf_check dmi_decode(const struct dmi_header *dm) dmi_string(dm, data[6])));dmi_save_ident (dm,DMI_BOARD_VERSION ,6 );break ; +case DMI_ENTRY_PROCESSOR : + dmi_max_speed_mhz =0 ; +if ( dm->length >=DMI_ENTRY_PROCESSOR_MIN_LENGTH ) + { + dmi_max_speed_mhz = (uint16_t )get_unaligned ((const uint16_t *) + (dm +DMI_PROCESSOR_MAX_SPEED_OFFSET )); +if ( !dmi_max_speed_mhz ) +dmi_printk (("Warnning: read zero value for Processor Max Speed\n" )); + } +/* +* Real stupid fallback value, just in case there is no +* actual value set. +*/ + dmi_max_speed_mhz = dmi_max_speed_mhz ? :1 ; + +dmi_printk (("Processor Max Speed: %u\n" , dmi_max_speed)); +break ; } }
xen/include/acpi/cpufreq/cpufreq.h +2 -0
@@ -271,4 +271,6 @@ int acpi_cpufreq_register(void);int amd_pstate_cmdline_parse (const char *s,const char *e);int amd_pstate_register_driver (void ); +extern uint16_t dmi_max_speed_mhz; +#endif /* __XEN_CPUFREQ_PM_H__ */
xen/include/xen/dmi.h +5 -0
@@ -1,6 +1,11 @@#ifndef __DMI_H__#define __DMI_H__ +/* Minimum struct length needed for the DMI processor entry we want */ +#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48 +/* Offset in the DMI processor entry for the max frequency */ +#define DMI_PROCESSOR_MAX_SPEED_OFFSET 0x14 +enum dmi_field {DMI_NONE , DMI_BIOS_VENDOR,
Signature · 1 line
2.34.1
Re: [PATCH v1 04/11] xen/x86: get processor max speed from DMI table
Quoted 6 lines · Penny Zheng
When
_CPCtable could not provide processor frequency range values for OS governor, we need to read processor max frequency as anchor point.For AMD processors, we rely on parsing DMI table to get processor max speed.
That sounds entirely fragile. There are better sources for this info, aren't there? See e.g. amd_log_freq().
Jan
RE: [PATCH v1 04/11] xen/x86: get processor max speed from DMI table
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 13 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 6:13 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 04/11] xen/x86: get processor max speed from DMI tableOn 03.12.2024 09:11, Penny Zheng wrote:
Quoted 9 lines
Quoted 5 lines
When
_CPCtable could not provide processor frequency range values for OS governor, we need to read processor max frequency as anchor point.For AMD processors, we rely on parsing DMI table to get processor max speed.
That sounds entirely fragile. There are better sources for this info, aren't there? See e.g.
amd_log_freq().
Thanks for the refer. Then I may introduce a new per-cpu value to export max frequency from amd_log_freq().
Quoted 1 line
Jan
Many thanks,
Penny
PATCH 5/11 xen/x86: introduce a new amd pstate driver for cpufreq scaling
In reply to Penny Zhengamd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on AMD Zen based CPU series. The new mechanism is based on Collaborative Processor Performance Control (CPPC) which is a 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. The new amd-pstate allows a more flexible, low-latency interface for Xen to directly communicate the performance hints to hardware.
The first version "amd-pstate" could leverage common governors such as *ondemand*, *performance*, etc, to manage the performance hints. In the future, we will introduce an advanced active mode to enable autonomous performence level selection.
--- xen/arch/x86/acpi/cpufreq/amd-pstate.c | 336 +++++++++++++++++++++++++ xen/arch/x86/include/asm/msr-index.h | 5 + 2 files changed, 341 insertions(+)
xen/arch/x86/acpi/cpufreq/amd-pstate.c +336 -0
@@ -15,6 +15,53 @@#include <xen/init.h> #include <xen/param.h> #include <acpi/cpufreq/cpufreq.h> +#include <asm/msr.h> + +#define amd_pstate_err(cpu, fmt, args...) \ + printk(XENLOG_ERR "AMD_PSTATE: CPU%u error: " fmt, cpu, ## args) +#define amd_pstate_verbose(fmt, args...) \ +({ \ + if ( cpufreq_verbose ) \ + printk(XENLOG_DEBUG "AMD_PSTATE: " fmt, ## args); \ +}) +#define amd_pstate_warn(fmt, args...) \ + printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu, ## args) + +struct amd_pstate_drv_data +{ +struct xen_processor_cppc *cppc_data; +union + { +uint64_t amd_caps; +struct + { +unsigned int lowest_perf:8 ; +unsigned int lowest_nonlinear_perf:8 ; +unsigned int nominal_perf:8 ; +unsigned int highest_perf:8 ; + unsigned int :32; + } hw; + }; +union + { +uint64_t amd_req; +struct + { +unsigned int max_perf:8 ; +unsigned int min_perf:8 ; +unsigned int des_perf:8 ; +unsigned int epp:8 ; + unsigned int :32; + } req; + }; +int err; + +uint32_t max_freq; +uint32_t min_freq; +uint32_t nominal_freq; +}; + +static DEFINE_PER_CPU_READ_MOSTLY(struct amd_pstate_drv_data *, amd_pstate_drv_data);uint16_t __read_mostly dmi_max_speed_mhz;
@@ -52,9 +99,298 @@ int __init amd_pstate_cmdline_parse(const char *s, const char *e)return 0 ; } +/* +* If CPPC lowest_freq and nominal_freq registers are exposed then we can +* use them to convert perf to freq and vice versa. The conversion is +* extrapolated as an affine function passing by the 2 points: +* - (Low perf, Low freq) +* - (Nominal perf, Nominal freq) +*/ +static unsigned int amd_pstate_khz_to_perf (struct amd_pstate_drv_data *data,unsigned int freq) +{ +struct xen_processor_cppc * cppc_data = data->cppc_data; +uint64_t mul, div, offset =0 ; + +if ( freq == (cppc_data->nominal_freq *1000 ) ) +return data->hw.nominal_perf; + +if ( freq == (cppc_data->lowest_freq *1000 ) ) +return data->hw.lowest_perf; + +if ( (cppc_data->lowest_freq) && (cppc_data->nominal_freq) ) + { + mul = data->hw.nominal_perf - data->hw.lowest_perf; + div = cppc_data->nominal_freq - cppc_data->lowest_freq; +/* +* We don't need to convert to kHz for computing offset and can +* directly use nominal_freq and lowest_freq as the division +* will remove the frequency unit. +*/ + div = div ?:1 ; + offset = data->hw.nominal_perf - (mul * cppc_data->nominal_freq) / div; + } +else + { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = data->hw.highest_perf; + div = dmi_max_speed_mhz; + } + +return (unsigned int )(offset + (mul * freq ) / (div *1000 )); +} + +static unsigned int amd_get_min_freq (struct amd_pstate_drv_data *data) +{ +struct xen_processor_cppc *cppc_data = data->cppc_data; +uint64_t mul, div; + +if ( cppc_data->lowest_freq ) +/* Switch to khz */ +return cppc_data->lowest_freq *1000 ; +else + { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.lowest_perf *1000 ; + } +} + +static unsigned int amd_get_nominal_freq (struct amd_pstate_drv_data *data) +{ +struct xen_processor_cppc *cppc_data = data->cppc_data; +uint64_t mul, div; + +if ( cppc_data->nominal_freq ) +/* Switch to khz */ +return cppc_data->nominal_freq *1000 ; +else + { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.nominal_perf *1000 ; + } +} + +static unsigned int amd_get_max_freq (struct amd_pstate_drv_data *data) +{ +uint64_t max_perf, max_freq, nominal_freq, nominal_perf; +uint64_t boost_ratio; + + nominal_freq =amd_get_nominal_freq (data); + nominal_perf = data->hw.nominal_perf; + max_perf = data->hw.highest_perf; + + boost_ratio = (uint64_t )(max_perf / nominal_perf); + max_freq = nominal_freq * boost_ratio; + +return max_freq; +} + +static int cf_check amd_pstate_cpufreq_verify(struct cpufreq_policy *policy) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu); + +cpufreq_verify_within_limits (policy, data->min_freq, data->max_freq); + +return 0 ; +} + +static void amd_pstate_write_request_msrs (void *info) +{ +struct amd_pstate_drv_data *data =(struct amd_pstate_drv_data *)info; + +if (wrmsr_safe (MSR_AMD_CPPC_REQ , data->amd_req) ) + { +amd_pstate_verbose ("Failed to wrmsr_safe(MSR_AMD_CPPC_REQ, %lx)\n" , + data->amd_req); + data->err = -EINVAL ; +return ; + } + data->err =0 ; +} + +static int cf_check amd_pstate_write_request(int cpu, uint8_t min_perf, +uint8_t des_perf,uint8_t max_perf) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); +uint64_t prev = data->amd_req; + + data->req.min_perf = min_perf; + data->req.max_perf = max_perf; + data->req.des_perf = des_perf; + +if ( prev == data->amd_req ) +return 0 ; + +on_selected_cpus (cpumask_of (cpu), amd_pstate_write_request_msrs, data,1 ); + +return data->err; +} + +static int cf_check amd_pstate_cpufreq_target(struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +unsigned int cpu = policy->cpu; +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); +uint64_t max_perf, min_perf, des_perf; + +if (unlikely (!target_freq) ) + { +amd_pstate_warn ("Not setting target frequency to zero\n" ); +return 0 ; + } + max_perf = data->hw.highest_perf; + min_perf = data->hw.lowest_nonlinear_perf; + des_perf =amd_pstate_khz_to_perf (data, target_freq); + +return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf); +} + +static void cf_check amd_pstate_init_msrs(void *info) +{ +struct cpufreq_policy *policy = info; +struct amd_pstate_drv_data *data =this_cpu (amd_pstate_drv_data); +uint64_t val; +unsigned int min_freq, nominal_freq, max_freq; + +/* Package level MSR */ +if (rdmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) + { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_ENABLE)\n" ); + data->err = -EINVAL ; +return ; + } + +if ( !(val &AMD_CPPC_ENABLE ) ) + { + val |=AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) + { +amd_pstate_err (policy->cpu,"wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n" , val); + data->err = -EINVAL ; +return ; + } + } + +if (rdmsr_safe (MSR_AMD_CPPC_CAP1 , data->amd_caps) ) + { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_CAP1)\n" ); + goto error; + } + +if ( data->hw.highest_perf ==0 || data->hw.lowest_perf ==0 || + data->hw.nominal_perf ==0 || data->hw.lowest_nonlinear_perf ==0 ) + { +amd_pstate_err (policy->cpu,"Platform malfunction, read CPPC highest_perf: %u, lowest_perf: %u, nominal_perf: %u, lowest_nonlinear_perf: %u zero value\n" , + data->hw.highest_perf, data->hw.lowest_perf, + data->hw.nominal_perf, data->hw.lowest_nonlinear_perf); + goto error; + } + + min_freq =amd_get_min_freq (data); + nominal_freq =amd_get_nominal_freq (data); + max_freq =amd_get_max_freq (data); +if ( min_freq > max_freq ) + { +amd_pstate_err (policy->cpu,"min_freq(%u) or max_freq(%u) value is incorrect\n" , + min_freq, max_freq); + goto error; + } + + policy->min = min_freq; + policy->max = max_freq; + + policy->cpuinfo.min_freq = min_freq; + policy->cpuinfo.max_freq = max_freq; + policy->cpuinfo.perf_freq = nominal_freq; + policy->cur = nominal_freq; + +/* Initial processor data capability frequencies */ + data->min_freq = min_freq; + data->nominal_freq = nominal_freq; + data->max_freq = max_freq; + + data->err =0 ; +return ; + + error: + data->err = -EINVAL ; + val &= ~AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) +amd_pstate_err (policy->cpu,"wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n" , val); +} + +/* +* The new AMD P-States driver is different than legacy ACPI hardware P-State, +* which 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 legacy P0 frequency is actually mapped on +* nominal performance ratio. +*/ +static void amd_pstate_boost_init (struct cpufreq_policy *policy,struct amd_pstate_drv_data *data) +{ +uint32_t highest_perf, nominal_perf; + + highest_perf = data->hw.highest_perf; + nominal_perf = data->hw.nominal_perf; + +if ( highest_perf <= nominal_perf ) +return ; + + policy->turbo =CPUFREQ_TURBO_ENABLED ; +} + +static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +{ +unsigned int cpu = policy->cpu; +struct amd_pstate_drv_data *data; + + data = xzalloc(struct amd_pstate_drv_data); +if ( !data ) +return -ENOMEM ; + + data->cppc_data = &processor_pminfo[cpu]->cppc_data; + + policy->governor = cpufreq_opt_governor ? :CPUFREQ_DEFAULT_GOVERNOR ; + +per_cpu (amd_pstate_drv_data, cpu) = data; + +on_selected_cpus (cpumask_of (cpu), amd_pstate_init_msrs, policy,1 ); + +if ( data->err ) + { +amd_pstate_err (cpu,"Could not initialize AMD CPPC MSR properly\n" ); +per_cpu (amd_pstate_drv_data, cpu) =NULL ; +xfree (data); +return -ENODEV ; + } + +amd_pstate_boost_init (policy, data); +return 0 ; +} + +static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu); + +per_cpu (amd_pstate_drv_data, policy->cpu) =NULL ; +xfree (data); + +return 0 ; +} + static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver = { .name =XEN_AMD_PSTATE_DRIVER_NAME , + .verify = amd_pstate_cpufreq_verify, + .target = amd_pstate_cpufreq_target, + .init = amd_pstate_cpufreq_cpu_init, + .exit = amd_pstate_cpufreq_cpu_exit, };int __init amd_pstate_register_driver (void )
xen/arch/x86/include/asm/msr-index.h +5 -0
@@ -455,6 +455,11 @@#define MSR_AMD_PPIN_CTL 0xc00102f0U#define MSR_AMD_PPIN 0xc00102f1U +#define MSR_AMD_CPPC_CAP1 0xc00102b0 +#define MSR_AMD_CPPC_ENABLE 0xc00102b1 +#define MSR_AMD_CPPC_REQ 0xc00102b3 +#define AMD_CPPC_ENABLE BIT(0, ULL) +/* VIA Cyrix defined MSRs*/ #define MSR_VIA_FCR 0x00001107#define MSR_VIA_RNG 0x0000110b
Signature · 1 line
2.34.1
Re: [PATCH v1 05/11] xen/x86: introduce a new amd pstate driver for cpufreq scaling
Quoted 22 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/amd-pstate.c +16
-0@@ -15,6 +15,53 @@#include <xen/init.h> #include <xen/param.h> #include <acpi/cpufreq/cpufreq.h> +#include <asm/msr.h> + +#define amd_pstate_err(cpu, fmt, args...) \ + printk(XENLOG_ERR "AMD_PSTATE: CPU%u error: " fmt, cpu, ## args) +#define amd_pstate_verbose(fmt, args...) \ +({ \ + if ( cpufreq_verbose ) \ + printk(XENLOG_DEBUG "AMD_PSTATE: " fmt, ## args); \ +}) +#define amd_pstate_warn(fmt, args...) \ + printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu, ## args) + +struct amd_pstate_drv_data +{ +struct xen_processor_cppc *cppc_data; + union
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 43
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ { +uint64_t amd_caps; +struct + { +unsigned int lowest_perf:8 ; +unsigned int lowest_nonlinear_perf:8 ; +unsigned int nominal_perf:8 ; +unsigned int highest_perf:8 ; + unsigned int :32; + } hw;
Please can this be
union {
uint64_t raw;
struct {
unsigned int lowest_perf:8;
unsigned int lowest_nonlinear_perf:8;
unsigned int nominal_perf:8;
unsigned int highest_perf:8;
unsigned int :32;
};
} caps;such that at use sites (e.g. amd_pstate_write_request()) it is possible to actually spot that the same thing is being accessed through the two parts of the union?
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 56
Quoted 13 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +13
-0+ }; +union + { +uint64_t amd_req; +struct + { +unsigned int max_perf:8 ; +unsigned int min_perf:8 ; +unsigned int des_perf:8 ; +unsigned int epp:8 ; + unsigned int :32; + } req; + };
Along the same lines here then.
Quoted 13 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +8
-0+int err; + +uint32_t max_freq; +uint32_t min_freq; +uint32_t nominal_freq; +}; + +static DEFINE_PER_CPU_READ_MOSTLY(struct amd_pstate_drv_data *, amd_pstate_drv_data);uint16_t __read_mostly dmi_max_speed_mhz;@@ -52,9 +99,298 @@ int __init amd_pstate_cmdline_parse(const char *s, const char *e)return 0 ;
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 109
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +8
-0} +/* +* If CPPC lowest_freq and nominal_freq registers are exposed then we can +* use them to convert perf to freq and vice versa. The conversion is +* extrapolated as an affine function passing by the 2 points: +* - (Low perf, Low freq) +* - (Nominal perf, Nominal freq) +*/ +static unsigned int amd_pstate_khz_to_perf (struct amd_pstate_drv_data *data,unsigned int freq)
Const?
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 111
Quoted 2 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +2
-0+{ +struct xen_processor_cppc * cppc_data = data->cppc_data;
Nit: Misplaced *. Const?
Quoted 15 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +15
-0+uint64_t mul, div, offset =0 ; + +if ( freq == (cppc_data->nominal_freq *1000 ) ) +return data->hw.nominal_perf; + +if ( freq == (cppc_data->lowest_freq *1000 ) ) +return data->hw.lowest_perf; + +if ( (cppc_data->lowest_freq) && (cppc_data->nominal_freq) ) + { + mul = data->hw.nominal_perf - data->hw.lowest_perf; + div = cppc_data->nominal_freq - cppc_data->lowest_freq; +/* +* We don't need to convert to kHz for computing offset and can +* directly use nominal_freq and lowest_freq as the division
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 136
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+* will remove the frequency unit. +*/ + div = div ?:1 ; + offset = data->hw.nominal_perf - (mul * cppc_data->nominal_freq) / div; + } +else + { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = data->hw.highest_perf; + div = dmi_max_speed_mhz;
What if dmi_max_speed_mhz is still 0?
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 139
Quoted 3 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+ } + +return (unsigned int )(offset + (mul * freq ) / (div *1000 ));
Nit: Excess blank before a closing parenthesis.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 150
Quoted 11 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +11
-0+} + +static unsigned int amd_get_min_freq (struct amd_pstate_drv_data *data) +{ +struct xen_processor_cppc *cppc_data = data->cppc_data; +uint64_t mul, div; + +if ( cppc_data->lowest_freq ) +/* Switch to khz */ +return cppc_data->lowest_freq *1000 ; +else
Please avoid "else" when the earlier if() ends in an unconditional control flow statement.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 156
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+ { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.lowest_perf *1000 ;
No risk of the cast chopping off set bits?
Quoted 18 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +18
-0+static unsigned int amd_get_nominal_freq (struct amd_pstate_drv_data *data) +{ +struct xen_processor_cppc *cppc_data = data->cppc_data; +uint64_t mul, div; + +if ( cppc_data->nominal_freq ) +/* Switch to khz */ +return cppc_data->nominal_freq *1000 ; +else + { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.nominal_perf *1000 ; + } +} +
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 187
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+static unsigned int amd_get_max_freq (struct amd_pstate_drv_data *data) +{ +uint64_t max_perf, max_freq, nominal_freq, nominal_perf; +uint64_t boost_ratio; + + nominal_freq =amd_get_nominal_freq (data); + nominal_perf = data->hw.nominal_perf; + max_perf = data->hw.highest_perf; + + boost_ratio = (uint64_t )(max_perf / nominal_perf);
What's the point of the cast here?
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 195
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +8
-0+ max_freq = nominal_freq * boost_ratio; + +return max_freq; +} + +static int cf_check amd_pstate_cpufreq_verify(struct cpufreq_policy *policy) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu);
Const? (I won't further repeat this. Please make pointers pointer-to- const wherever possible. We aim at having fully const-correct code.)
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 204
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +8
-0+cpufreq_verify_within_limits (policy, data->min_freq, data->max_freq); + +return 0 ; +} + +static void amd_pstate_write_request_msrs (void *info) +{ +struct amd_pstate_drv_data *data =(struct amd_pstate_drv_data *)info;
Nit: Blank after = and no need for a cast.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 209
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +4
-0+if (wrmsr_safe (MSR_AMD_CPPC_REQ , data->amd_req) ) + { +amd_pstate_verbose ("Failed to wrmsr_safe(MSR_AMD_CPPC_REQ, %lx)\n" , + data->amd_req);
If the was to ever trigger, it would likely get very noisy in the log.
Quoted 25 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +25
-0+ data->err = -EINVAL ; +return ; + } + data->err =0 ; +} + +static int cf_check amd_pstate_write_request(int cpu, uint8_t min_perf, +uint8_t des_perf,uint8_t max_perf) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); +uint64_t prev = data->amd_req; + + data->req.min_perf = min_perf; + data->req.max_perf = max_perf; + data->req.des_perf = des_perf; + +if ( prev == data->amd_req ) +return 0 ; + +on_selected_cpus (cpumask_of (cpu), amd_pstate_write_request_msrs, data,1 ); + +return data->err; +} + +static int cf_check amd_pstate_cpufreq_target(struct cpufreq_policy *policy,
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 244
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ unsigned int target_freq, + unsigned int relation) +{ +unsigned int cpu = policy->cpu; +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); +uint64_t max_perf, min_perf, des_perf; + +if (unlikely (!target_freq) ) + { +amd_pstate_warn ("Not setting target frequency to zero\n" );
Potentially overly noisy again.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 251
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+return 0 ; + } + max_perf = data->hw.highest_perf; + min_perf = data->hw.lowest_nonlinear_perf; + des_perf =amd_pstate_khz_to_perf (data, target_freq); + +return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf);
The use of intermadiate variables doesn't look very helpful here. Even more so that in the course of the call 64-bit quantaties will be truncated to 8-bit ones.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 262
Quoted 11 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +11
-0+} + +static void cf_check amd_pstate_init_msrs(void *info) +{ +struct cpufreq_policy *policy = info; +struct amd_pstate_drv_data *data =this_cpu (amd_pstate_drv_data); +uint64_t val; +unsigned int min_freq, nominal_freq, max_freq; + +/* Package level MSR */ +if (rdmsr_safe (MSR_AMD_CPPC_ENABLE , val) )
Before trying this, wouldn't you better exclude certain very old families? Even looking at a random Fam17 PPR I can't spot documentation of this MSR (nor the other two), despite you merely saying Zen elsewhere (without any version number).
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+ { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_ENABLE)\n" ); + data->err = -EINVAL ; +return ; + } +
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 278
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+if ( !(val &AMD_CPPC_ENABLE ) ) + { + val |=AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) + { +amd_pstate_err (policy->cpu,"wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n" , val); + data->err = -EINVAL ; +return ; + } + }
Do you really need to enable befor reading ...
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 280
Quoted 1 line
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0+if (rdmsr_safe (MSR_AMD_CPPC_CAP1 , data->amd_caps) )
... capabilities and ...
Quoted 13 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +13
-0+ { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_CAP1)\n" ); + goto error; + } + +if ( data->hw.highest_perf ==0 || data->hw.lowest_perf ==0 || + data->hw.nominal_perf ==0 || data->hw.lowest_nonlinear_perf ==0 ) + { +amd_pstate_err (policy->cpu,"Platform malfunction, read CPPC highest_perf: %u, lowest_perf: %u, nominal_perf: %u, lowest_nonlinear_perf: %u zero value\n" , + data->hw.highest_perf, data->hw.lowest_perf, + data->hw.nominal_perf, data->hw.lowest_nonlinear_perf); + goto error; + }
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 303
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ + min_freq =amd_get_min_freq (data); + nominal_freq =amd_get_nominal_freq (data); + max_freq =amd_get_max_freq (data); +if ( min_freq > max_freq ) + { +amd_pstate_err (policy->cpu,"min_freq(%u) or max_freq(%u) value is incorrect\n" , + min_freq, max_freq); + goto error; + }
... checking them? Error handling would be easier if you enabled only afterwards.
Quoted 23 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +23
-0+ policy->min = min_freq; + policy->max = max_freq; + + policy->cpuinfo.min_freq = min_freq; + policy->cpuinfo.max_freq = max_freq; + policy->cpuinfo.perf_freq = nominal_freq; + policy->cur = nominal_freq; + +/* Initial processor data capability frequencies */ + data->min_freq = min_freq; + data->nominal_freq = nominal_freq; + data->max_freq = max_freq; + + data->err =0 ; +return ; + + error: + data->err = -EINVAL ; + val &= ~AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) +amd_pstate_err (policy->cpu,"wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n" , val); +} +
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 337
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+/* +* The new AMD P-States driver is different than legacy ACPI hardware P-State, +* which 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 legacy P0 frequency is actually mapped on +* nominal performance ratio. +*/ +static void amd_pstate_boost_init (struct cpufreq_policy *policy,struct amd_pstate_drv_data *data) +{ +uint32_t highest_perf, nominal_perf;
Why uint32_t when the fields read are both 8-bit only?
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 346
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +8
-0+ highest_perf = data->hw.highest_perf; + nominal_perf = data->hw.nominal_perf; + +if ( highest_perf <= nominal_perf ) +return ; + + policy->turbo =CPUFREQ_TURBO_ENABLED ; +}
And why the helper variables in the first place?
Quoted 12 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +12
-0+static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +{ +unsigned int cpu = policy->cpu; +struct amd_pstate_drv_data *data; + + data = xzalloc(struct amd_pstate_drv_data); +if ( !data ) +return -ENOMEM ; + + data->cppc_data = &processor_pminfo[cpu]->cppc_data; + + policy->governor = cpufreq_opt_governor ? :CPUFREQ_DEFAULT_GOVERNOR ;
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 369
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ +per_cpu (amd_pstate_drv_data, cpu) = data; + +on_selected_cpus (cpumask_of (cpu), amd_pstate_init_msrs, policy,1 ); + +if ( data->err ) + { +amd_pstate_err (cpu,"Could not initialize AMD CPPC MSR properly\n" ); +per_cpu (amd_pstate_drv_data, cpu) =NULL ; +xfree (data);
Please use XFREE() (really XVFREE() after respective conversion) in such cases.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 370
Quoted 1 line
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0+return -ENODEV ;
You're no undoing the policy->governor adjustment - maybe best to delay that until after all errors were handled?
xen/arch/x86/include/asm/msr-index.h line 461
Quoted 10 lines
xen/arch/x86/include/asm/msr-index.h +4
-0@@ -455,6 +455,11 @@#define MSR_AMD_PPIN_CTL 0xc00102f0U#define MSR_AMD_PPIN 0xc00102f1U +#define MSR_AMD_CPPC_CAP1 0xc00102b0 +#define MSR_AMD_CPPC_ENABLE 0xc00102b1 +#define MSR_AMD_CPPC_REQ 0xc00102b3 +#define AMD_CPPC_ENABLE BIT(0, ULL)
Bits of individual MSRs want to follow their MSR #define directly, with suitably different indentation. Please further pay attention to this comment in the file (as well as the bigger one at the top):
/*
* Legacy MSR constants in need of cleanup. No new MSRs below this comment. */
Jan
RE: [PATCH v1 05/11] xen/x86: introduce a new amd pstate driver for cpufreq scaling
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 62 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 6:55 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 05/11] xen/x86: introduce a new amd pstate driver for cpufreq scalingQuoted 7 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0@@ -15,6 +15,53 @@#include <xen/init.h> #include <xen/param.h> #include <acpi/cpufreq/cpufreq.h> +#include <asm/msr.h> xen/arch/x86/acpi/cpufreq/amd-pstate.c line 28
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ +#define amd_pstate_err(cpu, fmt, args...) \ + printk(XENLOG_ERR "AMD_PSTATE: CPU%u error: " fmt, cpu, ## args) +#define amd_pstate_verbose(fmt, args...) \ +({ \ + if ( cpufreq_verbose ) \ + printk(XENLOG_DEBUG "AMD_PSTATE: " fmt, ## args); \ +}) +#define amd_pstate_warn(fmt, args...) \ + printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu,##
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+args) + +struct amd_pstate_drv_data +{ + struct xen_processor_cppc *cppc_data; + unionxen/arch/x86/acpi/cpufreq/amd-pstate.c line 43
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ { +uint64_t amd_caps; +struct + { +unsigned int lowest_perf:8 ; +unsigned int lowest_nonlinear_perf:8 ; +unsigned int nominal_perf:8 ; +unsigned int highest_perf:8 ; + unsigned int :32; + } hw;Please can this be
union { uint64_t raw; struct { unsigned int lowest_perf:8; unsigned int lowest_nonlinear_perf:8; unsigned int nominal_perf:8; unsigned int highest_perf:8; unsigned int :32; }; } caps;
Quoted 3 lines
such that at use sites (e.g.
amd_pstate_write_request()) it is possible to actually spot that the same thing is being accessed through the two parts of the union?
Understood.
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+ { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.lowest_perf * +1000 ;No risk of the cast chopping off set bits?
Mul shall be uint64_t, highest_perf and lowest_perf shall be uint8_t, and normally the equation output shall not be a too big value...
If you think it's safer to do cast after comparing with the UINT32_MAX, I will add the check
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 56
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +5
-0+ +static int cf_check amd_pstate_cpufreq_verify(struct cpufreq_policy +*policy) { +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, +policy->cpu);Const? (I won't further repeat this. Please make pointers pointer-to- const wherever possible. We aim at having fully const-correct code.)
Sure.
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+} + +static void cf_check amd_pstate_init_msrs(void *info) { +struct cpufreq_policy *policy = info; +struct amd_pstate_drv_data *data =this_cpu (amd_pstate_drv_data); +uint64_t val; +unsigned int min_freq, nominal_freq, max_freq;
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 262
Quoted 3 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+ +/* Package level MSR */ +if (rdmsr_safe (MSR_AMD_CPPC_ENABLE , val) )Before trying this, wouldn't you better exclude certain very old families? Even looking at a random Fam17 PPR I can't spot documentation of this MSR (nor the other two), despite you merely saying Zen elsewhere (without any version number).
I shall comment more specifically, this feature is firstly introduced on some Zen2 serie, like Renoir I'll exclude the families before Zen(Fam17h)
Quoted 19 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c
Quoted 2 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +2
-0+ { + amd_pstate_err(policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_ENABLE)\n");
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ data->err = -EINVAL ; +return ; + } + +if ( !(val &AMD_CPPC_ENABLE ) ) + { + val |=AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) + { + amd_pstate_err(policy->cpu,"
wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n", val);xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 214
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +4
-0+ data->err = -EINVAL ; +return ; + } + }
Quoted 6 lines
Do you really need to enable befor reading ...
Quoted 1 line
+ if (
rdmsr_safe(MSR_AMD_CPPC_CAP1, data->amd_caps))... capabilities and ...
Yes.
Only when CPPC is enabled, the hardware will calculate the processor’s performance capabilities and initialize the performance level fields in the CPPC capability registers.
See 17.6.3 https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/24593.pdf
Quoted 29 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +9
-0+ { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_CAP1)\n" ); + goto error; + } + +if ( data->hw.highest_perf ==0 || data->hw.lowest_perf ==0 || + data->hw.nominal_perf ==0 || data->hw.lowest_nonlinear_perf ==0 ) + { + amd_pstate_err(policy->cpu, "Platform malfunction, read CPPC
highest_perf: %u,lowest_perf: %u,nominal_perf: %u,lowest_nonlinear_perf: %u zero value\n",xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 11 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +11
-0+ data->hw.highest_perf, data->hw.lowest_perf, + data->hw.nominal_perf, data->hw.lowest_nonlinear_perf); + goto error; + } + + min_freq =amd_get_min_freq (data); + nominal_freq =amd_get_nominal_freq (data); + max_freq =amd_get_max_freq (data); +if ( min_freq > max_freq ) + { + amd_pstate_err(policy->cpu, "min_freq(%u) or max_freq(%u) value isincorrect\n",
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 303
Quoted 3 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+ min_freq, max_freq); + goto error; + }... checking them? Error handling would be easier if you enabled only afterwards.
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+ highest_perf = data->hw.highest_perf; + nominal_perf = data->hw.nominal_perf; + +if ( highest_perf <= nominal_perf ) +return ; + + policy->turbo =CPUFREQ_TURBO_ENABLED ; }And why the helper variables in the first place?
Sorry, maybe a bit more specific, got confused about the question ;/
Quoted 2 lines
Jan
Many thanks,
Penny
Re: [PATCH v1 05/11] xen/x86: introduce a new amd pstate driver for cpufreq scaling
Quoted 13 lines · Zheng
Quoted 13 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 6:55 PMQuoted 7 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+ { +/* Read Processor Max Speed(mhz) from DMI table as anchor point */ + mul = dmi_max_speed_mhz; + div = data->hw.highest_perf; + +return (unsigned int )(mul / div) * data->hw.lowest_perf * +1000 ;
Quoted 5 lines
Quoted 1 line
No risk of the cast chopping off set bits?
Mul shall be
uint64_t,highest_perfandlowest_perfshall beuint8_t, and normally the equation output shall not be a too big value...
If you think it's safer to do cast after comparing with theUINT32_MAX, I will add the check
Well, I can only say as much: Dividing a uint64_t by a uint8_t has ample opportunity for there being more than 32 significant bits in the result.
Quoted 16 lines
Quoted 16 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+if ( !(val &AMD_CPPC_ENABLE ) ) + { + val |=AMD_CPPC_ENABLE ; +if (wrmsr_safe (MSR_AMD_CPPC_ENABLE , val) ) + { + amd_pstate_err(policy->cpu,"
wrmsr_safe(MSR_AMD_CPPC_ENABLE, %lx)\n", val);xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 214
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +4
-0+ data->err = -EINVAL ; +return ; + } + }Do you really need to enable befor reading ...
Quoted 1 line
+ if (
rdmsr_safe(MSR_AMD_CPPC_CAP1, data->amd_caps))
Quoted 7 lines
Quoted 2 lines
... capabilities and ...
Yes.
Only when CPPC is enabled, the hardware will calculate the processor’s performance capabilities and initialize the performance level fields in the CPPC capability registers.
See 17.6.3 https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/24593.pdf
Ah yes, I see. Yet then the text there also says that the ENABLE bit can't be cleared (except by reset), so your error handling is questionable.
Quoted 39 lines
Quoted 29 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +9
-0+ { +amd_pstate_err (policy->cpu,"rdmsr_safe(MSR_AMD_CPPC_CAP1)\n" ); + goto error; + } + +if ( data->hw.highest_perf ==0 || data->hw.lowest_perf ==0 || + data->hw.nominal_perf ==0 || data->hw.lowest_nonlinear_perf ==0 ) + { + amd_pstate_err(policy->cpu, "Platform malfunction, read CPPC
highest_perf: %u,lowest_perf: %u,nominal_perf: %u,lowest_nonlinear_perf: %u zero value\n",xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 11 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +11
-0+ data->hw.highest_perf, data->hw.lowest_perf, + data->hw.nominal_perf, data->hw.lowest_nonlinear_perf); + goto error; + } + + min_freq =amd_get_min_freq (data); + nominal_freq =amd_get_nominal_freq (data); + max_freq =amd_get_max_freq (data); +if ( min_freq > max_freq ) + { + amd_pstate_err(policy->cpu, "min_freq(%u) or max_freq(%u) value isincorrect\n",
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e) · line 303
Quoted 3 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+ min_freq, max_freq); + goto error; + }... checking them? Error handling would be easier if you enabled only afterwards.
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_cmdline_parse(const char *s, const char *e)
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+ highest_perf = data->hw.highest_perf; + nominal_perf = data->hw.nominal_perf; + +if ( highest_perf <= nominal_perf ) +return ; + + policy->turbo =CPUFREQ_TURBO_ENABLED ; }And why the helper variables in the first place?
Quoted 1 line
Sorry, maybe a bit more specific, got confused about the question ;/
With local variables like these, you end up with more code volume for no real gain. IOW what's wrong with
if ( data->hw.highest_perf <= data->hw.nominal_perf )
return;? (As an aside, even if the variables were actually useful, it would still be better to have them have initializers than to use separate declarations and assignments.)
Jan
PATCH 6/11 xen/cpufreq: introduce policy type when cpufreq_driver->setpolicy exists
In reply to Penny ZhengFrom: Penny Zheng <[email protected]>
If cpufreq_driver->target exists, the ->governor decides what frequency within the limits is used. But when cpufreq_driver->setpolicy exists, the CPUFreq scaling driver bypasses the governor layer and implement their own performance scaling algorithms. And we introduce the following two generic policies CPUFREQ_POLICY_POWERSAVE and CPUFREQ_POLICY_PERFORMANCE, to have 1:1 map with governor info, to still benefit from cpufreq_opt_governor cmdline.
--- xen/drivers/cpufreq/utility.c | 11 +++++++++++ xen/include/acpi/cpufreq/cpufreq.h | 11 +++++++++++ 2 files changed, 22 insertions(+)
xen/drivers/cpufreq/utility.c +11 -0
@@ -484,3 +484,14 @@ int __cpufreq_set_policy(struct cpufreq_policy *data,return __cpufreq_governor (data,CPUFREQ_GOV_LIMITS ); } + +unsigned int cpufreq_parse_policy (struct cpufreq_governor *gov) +{ +if ( !strncasecmp (gov->name,"performance" ,CPUFREQ_NAME_LEN ) ) +return CPUFREQ_POLICY_PERFORMANCE ; + +if ( !strncasecmp (gov->name,"powersave" ,CPUFREQ_NAME_LEN ) ) +return CPUFREQ_POLICY_POWERSAVE ; + +return CPUFREQ_POLICY_UNKNOWN ; +}
xen/include/acpi/cpufreq/cpufreq.h +11 -0
@@ -133,6 +133,17 @@ extern int cpufreq_register_governor(struct cpufreq_governor *governor);extern struct cpufreq_governor *__find_governor (const char *governor);#define CPUFREQ_DEFAULT_GOVERNOR &cpufreq_gov_dbs +#define CPUFREQ_POLICY_UNKNOWN 0 +/* +* If cpufreq_driver->target() exists, the ->governor decides what frequency +* within the limits is used. If cpufreq_driver->setpolicy() exists, these +* two generic policies are available: +*/ +#define CPUFREQ_POLICY_POWERSAVE 1 +#define CPUFREQ_POLICY_PERFORMANCE 2 + +unsigned int cpufreq_parse_policy (struct cpufreq_governor *gov); +/* pass a target to the cpufreq driver */ extern int __cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq,
Signature · 1 line
2.34.1
Re: [PATCH v1 06/11] xen/cpufreq: introduce policy type when cpufreq_driver->setpolicy exists
Quoted 29 lines · Penny Zheng
xen/drivers/cpufreq/utility.c +11
-0@@ -484,3 +484,14 @@ int __cpufreq_set_policy(struct cpufreq_policy *data,return __cpufreq_governor (data,CPUFREQ_GOV_LIMITS ); } + +unsigned int cpufreq_parse_policy (struct cpufreq_governor *gov) +{ +if ( !strncasecmp (gov->name,"performance" ,CPUFREQ_NAME_LEN ) ) +return CPUFREQ_POLICY_PERFORMANCE ; + +if ( !strncasecmp (gov->name,"powersave" ,CPUFREQ_NAME_LEN ) ) +return CPUFREQ_POLICY_POWERSAVE ; + +return CPUFREQ_POLICY_UNKNOWN ; +}xen/include/acpi/cpufreq/cpufreq.h +4
-0@@ -133,6 +133,17 @@ extern int cpufreq_register_governor(struct cpufreq_governor *governor);extern struct cpufreq_governor *__find_governor (const char *governor);#define CPUFREQ_DEFAULT_GOVERNOR &cpufreq_gov_dbs +#define CPUFREQ_POLICY_UNKNOWN 0 +/* +* If cpufreq_driver->target() exists, the ->governor decides what frequency +* within the limits is used. If cpufreq_driver->setpolicy() exists, these
xen/include/acpi/cpufreq/cpufreq.h extern int cpufreq_register_governor(struct cpufreq_governor *governor); · line 149
Quoted 10 lines
xen/include/acpi/cpufreq/cpufreq.h +7
-0+* two generic policies are available: +*/ +#define CPUFREQ_POLICY_POWERSAVE 1 +#define CPUFREQ_POLICY_PERFORMANCE 2 + +unsigned int cpufreq_parse_policy (struct cpufreq_governor *gov); +/* pass a target to the cpufreq driver */ extern int __cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq,
The new function has no callers, making it difficult to review the change (not seeing how it is used) and violating Misra (by introducing unreachable code).
Jan
PATCH 7/11 xen/cpufreq: only set gov NULL when cpufreq_driver.target() exists
In reply to Penny ZhengFrom: Penny Zheng <[email protected]>
amd-pstate on active mode bypasses the scaling governor layer, and provides its own P-state selection algorithms. Consequently, when it is used, the driver's -> setpolicy() callback is invoked to register per-CPU utilization update callbacks, not the ->target() callback.
So, only when cpufreq_driver.target() exists, we need to deliberately set old gov as NULL to trigger the according gov starting.
--- xen/drivers/cpufreq/cpufreq.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
xen/drivers/cpufreq/cpufreq.c +7 -1
@@ -316,7 +316,13 @@ int cpufreq_add_cpu(unsigned int cpu)if (hw_all || (cpumask_weight (cpufreq_dom->map) == perf->domain_info.num_processors)) {memcpy (&new_policy, policy,sizeof (struct cpufreq_policy )); - policy->governor =NULL ; + +/* +* Only when cpufreq_driver.target exists, we need to deliberately set old gov as NULL +* to trigger the according gov starting. +*/ +if ( cpufreq_driver.target ) + policy->governor =NULL ;cpufreq_cmdline_common_para (&new_policy);
Signature · 1 line
2.34.1
Re: [PATCH v1 07/11] xen/cpufreq: only set gov NULL when cpufreq_driver.target() exists
Quoted 6 lines · Penny Zheng
xen/drivers/cpufreq/cpufreq.c +0
-0@@ -316,7 +316,13 @@ int cpufreq_add_cpu(unsigned int cpu)if (hw_all || (cpumask_weight (cpufreq_dom->map) == perf->domain_info.num_processors)) {memcpy (&new_policy, policy,sizeof (struct cpufreq_policy ));
xen/drivers/cpufreq/cpufreq.c int cpufreq_add_cpu(unsigned int cpu) · line 327
Quoted 10 lines
xen/drivers/cpufreq/cpufreq.c +7
-1- policy->governor =NULL ; + +/* +* Only when cpufreq_driver.target exists, we need to deliberately set old gov as NULL +* to trigger the according gov starting. +*/ +if ( cpufreq_driver.target ) + policy->governor =NULL ;cpufreq_cmdline_common_para (&new_policy);
Looking at __cpufreq_set_policy(), wouldn't the condition better check .setpolicy being NULL?
Jan
PATCH 8/11 x86/cpufreq: add "cpufreq=amd-pstate,active" para
In reply to Penny ZhengFrom: Penny Zheng <[email protected]>
The amd-pstate driver may support multiple working modes, passive and active.
Introduce a new variable to keep track of which mode is currently enabled. This variable will also help to choose which cpufreq driver to be registered.
--- docs/misc/xen-command-line.pandoc | 9 ++++++++- xen/arch/x86/acpi/cpufreq/amd-pstate.c | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.pandoc +8 -1
@@ -499,7 +499,8 @@ If set, force use of the performance counters for oprofile, rather than detectin available support. ### cpufreq -> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } [,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | amd-pstate[:[verbose]]` +> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } +[,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | amd-pstate[:[active][,verbose]]` > Default: `xen`
@@ -522,6 +523,12 @@ choice of `dom0-kernel` is deprecated and not supported by all Dom0 kernels. on supported AMD hardware to provide a finer grained frequency control mechanism. The default is disabled. If `amd-pstate` is selected, but hardware support is not available, Xen will fallback to cpufreq=xen. +* `active` is a boolean to enable amd-pstate driver in active(autonomous) mode. + In this mode, users could provide a hint with energy performance preference + register to the hardware if they want to bias toward performance(0x0) or + energy efficiency(0xff), then CPPC power algorithm will calculate the runtime + workload and adjust the realtime cores frequency according to the power supply + and themal, core voltage and some other hardware conditions. There is also support for `;`-separated fallback options: `cpufreq=hwp;xen,verbose`. This first tries `hwp` and falls back to `xen` if
xen/arch/x86/acpi/cpufreq/amd-pstate.c +11 -1
@@ -27,6 +27,8 @@ #define amd_pstate_warn(fmt, args...) \ printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu, ## args) +static bool __ro_after_init opt_cpufreq_active = false; +struct amd_pstate_drv_data {struct xen_processor_cppc *cppc_data;
@@ -76,6 +78,13 @@ static bool __init amd_pstate_handle_option(const char *s, const char *end)return true; } + ret =parse_boolean ("active" , s, end); +if ( ret >=0 ) + { + opt_cpufreq_active = ret; +return true; + } +return false; }
@@ -398,5 +407,6 @@ int __init amd_pstate_register_driver(void)if ( !cpu_has_cppc )return -ENODEV ; -return cpufreq_register_driver (&amd_pstate_cpufreq_driver); +if ( !opt_cpufreq_active ) +return cpufreq_register_driver (&amd_pstate_cpufreq_driver); }
Signature · 1 line
2.34.1
Re: [PATCH v1 08/11] x86/cpufreq: add "cpufreq=amd-pstate,active" para
Quoted 28 lines · Penny Zheng
From: Penny Zheng <[email protected]>
The amd-pstate driver may support multiple working modes, passive and active.
Introduce a new variable to keep track of which mode is currently enabled. This variable will also help to choose which cpufreq driver to be registered.
Signed-off-by Penny Zheng <[email protected]>--- docs/misc/xen-command-line.pandoc | 9 ++++++++- xen/arch/x86/acpi/cpufreq/amd-pstate.c | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-)docs/misc/xen-command-line.pandoc +2
-1@@ -499,7 +499,8 @@ If set, force use of the performance counters for oprofile, rather than detectin available support. ### cpufreq -> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } [,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | amd-pstate[:[verbose]]` +> `= none | {{ <boolean> | xen } { [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfreq>]]] } +[,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | amd-pstate[:[active][,verbose]]` > Default: `xen`
docs/misc/xen-command-line.pandoc choice of `dom0-kernel` is deprecated and not supported by all Dom0 kernels. · line 531
Quoted 9 lines
docs/misc/xen-command-line.pandoc +6
-0@@ -522,6 +523,12 @@ choice of `dom0-kernel` is deprecated and not supported by all Dom0 kernels. on supported AMD hardware to provide a finer grained frequency control mechanism. The default is disabled. If `amd-pstate` is selected, but hardware support is not available, Xen will fallback to cpufreq=xen. +* `active` is a boolean to enable amd-pstate driver in active(autonomous) mode. + In this mode, users could provide a hint with energy performance preference + register to the hardware if they want to bias toward performance(0x0) or + energy efficiency(0xff), then CPPC power algorithm will calculate the runtime + workload and adjust the realtime cores frequency according to the power supply + and themal, core voltage and some other hardware conditions.
Nit: thermal
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 30
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0@@ -27,6 +27,8 @@ #define amd_pstate_warn(fmt, args...) \ printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu, ## args) +static bool __ro_after_init opt_cpufreq_active = false;
Pointless initializer.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_register_driver(void) · line 412
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +2
-1@@ -398,5 +407,6 @@ int __init amd_pstate_register_driver(void)if ( !cpu_has_cppc )return -ENODEV ; -return cpufreq_register_driver (&amd_pstate_cpufreq_driver); +if ( !opt_cpufreq_active ) +return cpufreq_register_driver (&amd_pstate_cpufreq_driver); }
I'm afraid the description is of no help in determining why this is a correct change to make (here). How would the user provided hint (see cmdline option description) be communicated to hardware when the driver isn't even registered?
Finally I don't think the change above would build, as it leaves a return from the function without return value.
Jan
RE: [PATCH v1 08/11] x86/cpufreq: add "cpufreq=amd-pstate,active" para
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 81 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 7:24 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Roger Pau Monné <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 08/11] x86/cpufreq: add "cpufreq=amd-pstate,active" paradocs/misc/xen-command-line.pandoc choice of `dom0-kernel` is deprecated and not
Quoted 35 lines · Penny Zheng
From: Penny Zheng <[email protected]>
The amd-pstate driver may support multiple working modes, passive and active.
Introduce a new variable to keep track of which mode is currently enabled. This variable will also help to choose which cpufreq driver to be registered.
Signed-off-by Penny Zheng <[email protected]>--- docs/misc/xen-command-line.pandoc | 9 ++++++++- xen/arch/x86/acpi/cpufreq/amd-pstate.c | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-)docs/misc/xen-command-line.pandoc +0
-0@@ -499,7 +499,8 @@ If set, force use of the performance counters foroprofile, rather than detectin available support.
### cpufreq
-> `= none | {{ <boolean> | xen } {
-> [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfr -> eq>]]] } [,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] | -> amd-pstate[:[verbose]]`
+> `= none | {{ <boolean> | xen } {
+> [:[powersave|performance|ondemand|userspace][,[<maxfreq>]][,[<minfr +> eq>]]] }
+[,verbose]} | dom0-kernel | hwp[:[<hdc>][,verbose]] |docs/misc/xen-command-line.pandoc +1
-0+amd-pstate[:[active][,verbose]]` > Default: `xen`@@ -522,6 +523,12 @@ choice of `dom0-kernel` is deprecated and notsupported by all Dom0 kernels.
docs/misc/xen-command-line.pandoc choice of `dom0-kernel` is deprecated and not · line 523
Quoted 1 line
docs/misc/xen-command-line.pandoc +0
-0on supported AMD hardware to provide a finer grained frequency controlmechanism.
docs/misc/xen-command-line.pandoc choice of `dom0-kernel` is deprecated and not
Quoted 12 lines
docs/misc/xen-command-line.pandoc +10
-0The default is disabled. If `amd-pstate` is selected, but hardware support is not available, Xen will fallback to cpufreq=xen. +* `active` is a boolean to enable amd-pstate driver in active(autonomous) mode. + In this mode, users could provide a hint with energy performance +preference + register to the hardware if they want to bias toward +performance(0x0) or + energy efficiency(0xff), then CPPC power algorithm will calculate +the runtime + workload and adjust the realtime cores frequency according to the +power supply + and themal, core voltage and some other hardware conditions.Nit: thermal
xen/arch/x86/acpi/cpufreq/amd-pstate.c line 31
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0@@ -27,6 +27,8 @@ #define amd_pstate_warn(fmt, args...) \ printk(XENLOG_WARNING "AMD_PSTATE: CPU%u warning: " fmt, cpu, ## args) +static bool __ro_after_init opt_cpufreq_active = false;Pointless initializer.
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +0
-0@@ -398,5 +407,6 @@ int __init amd_pstate_register_driver(void)if ( !cpu_has_cppc )return -ENODEV ;
Quoted 9 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_register_driver(void) · line 412
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +2
-1-return cpufreq_register_driver (&amd_pstate_cpufreq_driver); +if ( !opt_cpufreq_active ) +return cpufreq_register_driver (&amd_pstate_cpufreq_driver); }I'm afraid the description is of no help in determining why this is a correct change to make (here). How would the user provided hint (see cmdline option description) be communicated to hardware when the driver isn't even registered?
Maybe I shall combine this commit into the next one about implementing epp driver for active mode, and the changes will be like:
- return cpufreq_register_driver(&amd_pstate_cpufreq_driver);
+ if ( opt_cpufreq_active )
+ return cpufreq_register_driver(&&amd_cppc_epp_driver);
+ else
+ return cpufreq_register_driver(&amd_cppc_cpufreq_driver);
Now, the description makes more sense.
Quoted 4 lines
Finally I don't think the change above would build, as it leaves a return from the function without return value.
Jan
Many thanks
Penny
PATCH 9/11 xen/x86: implement EPP support for the AMD processors
In reply to Penny Zhengamd-pstate has 2 operation modes: autonomous (active) mode, non-autonomous (passive) mode.
In active mode, platform ignores the requestd done in the Desired Performance Target register and takes into account only the values set to the minimum, maximum and energy performance preference(EPP) registers.
The EPP is used in the CCLK DPM controller to drive the frequency that a core is going to operate during short periods of activity.
The SOC EPP targets are configured on a scale from 0 to 255 where 0 represents maximum performance and 255 represents maximum efficiency.
This commit implements one new AMD CPU frequency driver amd-pstate-epp for active mode.
--- xen/arch/x86/acpi/cpufreq/amd-pstate.c | 122 +++++++++++++++++++++++-- xen/arch/x86/acpi/cpufreq/hwp.c | 10 +- xen/include/acpi/cpufreq/cpufreq.h | 5 + xen/include/public/sysctl.h | 2 +- 4 files changed, 125 insertions(+), 14 deletions(-)
xen/arch/x86/acpi/cpufreq/amd-pstate.c +116 -6
@@ -57,6 +57,8 @@ struct amd_pstate_drv_data } req; };int err; +/* EPP feature related attributes */ +unsigned int policy;uint32_t max_freq;uint32_t min_freq;
@@ -67,6 +69,8 @@ static DEFINE_PER_CPU_READ_MOSTLY(struct amd_pstate_drv_data *, amd_pstate_drv_duint16_t __read_mostly dmi_max_speed_mhz; +static bool __ro_after_init epp_mode; +static bool __init amd_pstate_handle_option (const char *s,const char *end) {int ret;
@@ -223,14 +227,29 @@ static void amd_pstate_write_request_msrs(void *info) } static int cf_check amd_pstate_write_request(int cpu, uint8_t min_perf, -uint8_t des_perf,uint8_t max_perf) +uint8_t des_perf,uint8_t max_perf, +int epp) {struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); -uint64_t prev = data->amd_req; +uint64_t prev = data->amd_req, val; data->req.min_perf = min_perf; data->req.max_perf = max_perf; - data->req.des_perf = des_perf; +if ( !epp_mode ) + data->req.des_perf = des_perf; +else + { + data->req.des_perf =0 ; +/* Get pre-defined BIOS value */ +if ( epp <0 ) + { +if (rdmsr_safe (MSR_AMD_CPPC_REQ , val) ) +return -EINVAL ; + data->req.epp = (val >>24 ) &0xFF ; + } +else + data->req.epp = epp; + }if ( prev == data->amd_req )return 0 ;
@@ -257,7 +276,7 @@ static int cf_check amd_pstate_cpufreq_target(struct cpufreq_policy *policy, min_perf = data->hw.lowest_nonlinear_perf; des_perf =amd_pstate_khz_to_perf (data, target_freq); -return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf); +return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf,-1 ); } static void cf_check amd_pstate_init_msrs(void *info)
@@ -354,7 +373,7 @@ static void amd_pstate_boost_init(struct cpufreq_policy *policy, struct amd_psta policy->turbo =CPUFREQ_TURBO_ENABLED ; } -static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +static int amd_pstate_cpufreq_init_perf (struct cpufreq_policy *policy) {unsigned int cpu = policy->cpu;struct amd_pstate_drv_data *data;
@@ -379,10 +398,23 @@ static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy)return -ENODEV ; } -amd_pstate_boost_init (policy, data);return 0 ; } +static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +{ +int ret =0 ; +struct amd_pstate_drv_data *data; + + ret =amd_pstate_cpufreq_init_perf (policy); +if ( ret ) +return ret; + + data =per_cpu (amd_pstate_drv_data, policy->cpu); +amd_pstate_boost_init (policy, data); +return ret; +} + static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) {struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu);
@@ -393,6 +425,70 @@ static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy)return 0 ; } +static void amd_perf_ctl_reset (void *data) +{ +wrmsr_safe (MSR_K8_PSTATE_CTRL ,0 ); +} + +static int cf_check amd_pstate_epp_cpu_init(struct cpufreq_policy *policy) +{ +int ret =0 ; +struct amd_pstate_drv_data *data; + +/* +* Resetting P-State Control register will put the CPU in P0 frequency, +* which is ideal for initialization process. +*/ +on_selected_cpus (cpumask_of (policy->cpu), amd_perf_ctl_reset,NULL ,1 ); + + ret =amd_pstate_cpufreq_init_perf (policy); +if ( ret ) +return ret; + + data =per_cpu (amd_pstate_drv_data, policy->cpu); + + policy->policy =cpufreq_parse_policy (policy->governor); + +amd_pstate_boost_init (policy, data); + +amd_pstate_verbose ("CPU %u initialized with amd-pstate active mode\n" , policy->cpu); + +return 0 ; +} + +static int cf_check amd_pstate_epp_update_limit(struct cpufreq_policy *policy) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu); +uint8_t max_perf, min_perf, des_perf; +int epp =-1 ; + +/* Initial min/max values for CPPC Performance Controls Register */ + max_perf = data->hw.highest_perf; + min_perf = data->hw.lowest_perf; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) + min_perf = max_perf; + +/* CPPC EPP feature require to set zero to the desire perf bit */ + des_perf =0 ; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) +/* Force the epp value to be zero for performance policy */ + epp =CPPC_ENERGY_PERF_MAX_PERFORMANCE ; +else if ( data->policy ==CPUFREQ_POLICY_POWERSAVE ) +/* Force the epp value to be 0xff for powersave policy */ + epp =CPPC_ENERGY_PERF_MAX_POWERSAVE ; + +return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf, epp); +} + +static int cf_check amd_pstate_epp_set_policy(struct cpufreq_policy *policy) +{ +amd_pstate_verbose ("Set new policy for CPU %u\n" , policy->cpu); + +return amd_pstate_epp_update_limit (policy); +} + static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver = { .name =XEN_AMD_PSTATE_DRIVER_NAME ,
@@ -402,6 +498,15 @@ static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver = .exit = amd_pstate_cpufreq_cpu_exit, }; +static const struct cpufreq_driver __initconstrel amd_pstate_epp_driver = +{ + .name =XEN_AMD_PSTATE_EPP_DRIVER_NAME , + .verify = amd_pstate_cpufreq_verify, + .setpolicy = amd_pstate_epp_set_policy, + .init = amd_pstate_epp_cpu_init, + .exit = amd_pstate_cpufreq_cpu_exit, +}; +int __init amd_pstate_register_driver (void ) {if ( !cpu_has_cppc )
@@ -409,4 +514,9 @@ int __init amd_pstate_register_driver(void)if ( !opt_cpufreq_active )return cpufreq_register_driver (&amd_pstate_cpufreq_driver); +else + { + epp_mode = true; +return cpufreq_register_driver (&amd_pstate_epp_driver); + } }
xen/arch/x86/acpi/cpufreq/hwp.c +3 -7
@@ -21,10 +21,6 @@ static bool __ro_after_init feature_hdc;static bool __ro_after_init opt_cpufreq_hdc = true; -#define HWP_ENERGY_PERF_MAX_PERFORMANCE 0 -#define HWP_ENERGY_PERF_BALANCE 0x80 -#define HWP_ENERGY_PERF_MAX_POWERSAVE 0xff -union hwp_request { struct
@@ -597,7 +593,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.lowest; data->maximum = data->hw.lowest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_MAX_POWERSAVE ; + data->energy_perf =CPPC_ENERGY_PERF_MAX_POWERSAVE ; data->desired =0 ;break ;
@@ -605,7 +601,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.highest; data->maximum = data->hw.highest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_MAX_PERFORMANCE ; + data->energy_perf =CPPC_ENERGY_PERF_MAX_PERFORMANCE ; data->desired =0 ;break ;
@@ -613,7 +609,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.lowest; data->maximum = data->hw.highest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_BALANCE ; + data->energy_perf =CPPC_ENERGY_PERF_BALANCE ; data->desired =0 ;break ;
xen/include/acpi/cpufreq/cpufreq.h +5 -0
@@ -83,6 +83,7 @@ struct cpufreq_policy {int8_t turbo;/* tristate flag: 0 for unsupported * -1 for disable, 1 for enabled * See CPUFREQ_TURBO_* below for defines */ +unsigned int policy; }; DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
@@ -264,6 +265,10 @@ void cpufreq_dbs_timer_resume(void);void intel_feature_detect (struct cpufreq_policy *policy); +#define CPPC_ENERGY_PERF_MAX_PERFORMANCE 0 +#define CPPC_ENERGY_PERF_BALANCE 0x80 +#define CPPC_ENERGY_PERF_MAX_POWERSAVE 0xff +int hwp_cmdline_parse (const char *s,const char *e);int hwp_register_driver (void );#ifdef CONFIG_INTEL
xen/include/public/sysctl.h +1 -1
@@ -425,7 +425,7 @@ struct xen_set_cppc_para {#define XEN_HWP_DRIVER_NAME "hwp"#define XEN_AMD_PSTATE_DRIVER_NAME "amd-pstate" - +#define XEN_AMD_PSTATE_EPP_DRIVER_NAME "amd-pstate-epp"/* * cpufreq para name of this structure named * same as sysfs file name of native linux
Signature · 1 line
2.34.1
Re: [PATCH v1 09/11] xen/x86: implement EPP support for the AMD processors
Quoted 16 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/amd-pstate.c +4
-3@@ -223,14 +227,29 @@ static void amd_pstate_write_request_msrs(void *info) } static int cf_check amd_pstate_write_request(int cpu, uint8_t min_perf, -uint8_t des_perf,uint8_t max_perf) +uint8_t des_perf,uint8_t max_perf, +int epp) {struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, cpu); -uint64_t prev = data->amd_req; +uint64_t prev = data->amd_req, val; data->req.min_perf = min_perf; data->req.max_perf = max_perf; - data->req.des_perf = des_perf; +if ( !epp_mode )
xen/arch/x86/acpi/cpufreq/amd-pstate.c static void amd_pstate_write_request_msrs(void *info) · line 248
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+ data->req.des_perf = des_perf; + else + { + data->req.des_perf =0 ; +/* Get pre-defined BIOS value */ +if ( epp <0 ) + { +if (rdmsr_safe (MSR_AMD_CPPC_REQ , val) ) +return -EINVAL ; + data->req.epp = (val >>24 ) &0xFF ;
This reading may better live in the sole caller where it's relevant. Plus it might be yet better if this was read just once, assuming "pre-defined BIOS value" is what it says (and hence doesn't change during the runtime of the system).
Quoted 19 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +2
-2@@ -257,7 +276,7 @@ static int cf_check amd_pstate_cpufreq_target(struct cpufreq_policy *policy, min_perf = data->hw.lowest_nonlinear_perf; des_perf =amd_pstate_khz_to_perf (data, target_freq); -return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf); +return amd_pstate_write_request (policy->cpu, min_perf, des_perf, max_perf,-1 ); } static void cf_check amd_pstate_init_msrs(void *info)@@ -354,7 +373,7 @@ static void amd_pstate_boost_init(struct cpufreq_policy *policy, struct amd_psta policy->turbo =CPUFREQ_TURBO_ENABLED ; } -static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +static int amd_pstate_cpufreq_init_perf (struct cpufreq_policy *policy) {unsigned int cpu = policy->cpu;struct amd_pstate_drv_data *data;
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) · line 406
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-1@@ -379,10 +398,23 @@ static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy)return -ENODEV ; } -amd_pstate_boost_init (policy, data);return 0 ; } +static int cf_check amd_pstate_cpufreq_cpu_init(struct cpufreq_policy *policy) +{ +int ret =0 ;
Pointless initializer.
Quoted 23 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +16
-0+struct amd_pstate_drv_data *data; + + ret =amd_pstate_cpufreq_init_perf (policy); +if ( ret ) +return ret; + + data =per_cpu (amd_pstate_drv_data, policy->cpu); +amd_pstate_boost_init (policy, data); +return ret; +} + static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) {struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu);@@ -393,6 +425,70 @@ static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy)return 0 ; } +static void amd_perf_ctl_reset (void *data) +{ +wrmsr_safe (MSR_K8_PSTATE_CTRL ,0 ); +} +
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 442
Quoted 10 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +10
-0+static int cf_check amd_pstate_epp_cpu_init(struct cpufreq_policy *policy) +{ +int ret =0 ; +struct amd_pstate_drv_data *data; + +/* +* Resetting P-State Control register will put the CPU in P0 frequency, +* which is ideal for initialization process. +*/ +on_selected_cpus (cpumask_of (policy->cpu), amd_perf_ctl_reset,NULL ,1 );
How do you/we know what's ideal for initialization? I can think of cases where it might be better to still conserve power.
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 470
Quoted 12 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +12
-0+static int cf_check amd_pstate_epp_update_limit(struct cpufreq_policy *policy) +{ +struct amd_pstate_drv_data *data =per_cpu (amd_pstate_drv_data, policy->cpu); +uint8_t max_perf, min_perf, des_perf; +int epp =-1 ; + +/* Initial min/max values for CPPC Performance Controls Register */ + max_perf = data->hw.highest_perf; + min_perf = data->hw.lowest_perf; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) + min_perf = max_perf;
Why can't this be done ...
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 477
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+/* CPPC EPP feature require to set zero to the desire perf bit */ + des_perf =0 ; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) +/* Force the epp value to be zero for performance policy */ + epp =CPPC_ENERGY_PERF_MAX_PERFORMANCE ;
... here as well? And why is there nothing respective for ...
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 480
Quoted 3 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+else if ( data->policy ==CPUFREQ_POLICY_POWERSAVE ) +/* Force the epp value to be 0xff for powersave policy */ + epp =CPPC_ENERGY_PERF_MAX_POWERSAVE ;
... this case (e.g. setting max_perf from min_perf)?
xen/arch/x86/acpi/cpufreq/amd-pstate.c static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver = · line 501
Quoted 5 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +1
-0@@ -402,6 +498,15 @@ static const struct cpufreq_driver __initconstrel amd_pstate_cpufreq_driver = .exit = amd_pstate_cpufreq_cpu_exit, }; +static const struct cpufreq_driver __initconstrel amd_pstate_epp_driver =
Again: __initconst_cf_clobber.
xen/arch/x86/acpi/cpufreq/amd-pstate.c int __init amd_pstate_register_driver(void) · line 519
Quoted 7 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0@@ -409,4 +514,9 @@ int __init amd_pstate_register_driver(void)if ( !opt_cpufreq_active )return cpufreq_register_driver (&amd_pstate_cpufreq_driver); +else + { + epp_mode = true;
Why a 2nd global variable? Can't you go from opt_cpufreq_active?
Quoted 53 lines
xen/arch/x86/acpi/cpufreq/hwp.c +3
-7@@ -21,10 +21,6 @@ static bool __ro_after_init feature_hdc;static bool __ro_after_init opt_cpufreq_hdc = true; -#define HWP_ENERGY_PERF_MAX_PERFORMANCE 0 -#define HWP_ENERGY_PERF_BALANCE 0x80 -#define HWP_ENERGY_PERF_MAX_POWERSAVE 0xff -union hwp_request { struct@@ -597,7 +593,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.lowest; data->maximum = data->hw.lowest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_MAX_POWERSAVE ; + data->energy_perf =CPPC_ENERGY_PERF_MAX_POWERSAVE ; data->desired =0 ;break ;@@ -605,7 +601,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.highest; data->maximum = data->hw.highest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_MAX_PERFORMANCE ; + data->energy_perf =CPPC_ENERGY_PERF_MAX_PERFORMANCE ; data->desired =0 ;break ;@@ -613,7 +609,7 @@ int set_hwp_para(struct cpufreq_policy *policy, data->minimum = data->hw.lowest; data->maximum = data->hw.highest; data->activity_window =0 ; - data->energy_perf =HWP_ENERGY_PERF_BALANCE ; + data->energy_perf =CPPC_ENERGY_PERF_BALANCE ; data->desired =0 ;break ;xen/include/acpi/cpufreq/cpufreq.h +1
-0@@ -83,6 +83,7 @@ struct cpufreq_policy {int8_t turbo;/* tristate flag: 0 for unsupported * -1 for disable, 1 for enabled * See CPUFREQ_TURBO_* below for defines */ +unsigned int policy; }; DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
xen/include/acpi/cpufreq/cpufreq.h void cpufreq_dbs_timer_resume(void); · line 270
Quoted 6 lines
xen/include/acpi/cpufreq/cpufreq.h +3
-0@@ -264,6 +265,10 @@ void cpufreq_dbs_timer_resume(void);void intel_feature_detect (struct cpufreq_policy *policy); +#define CPPC_ENERGY_PERF_MAX_PERFORMANCE 0 +#define CPPC_ENERGY_PERF_BALANCE 0x80 +#define CPPC_ENERGY_PERF_MAX_POWERSAVE 0xff
I guess this renaming / movement might better be a separate patch. The more that the description here doesn't even mention, let alone justify, it.
xen/include/public/sysctl.h struct xen_set_cppc_para { · line 429
Quoted 9 lines
xen/include/public/sysctl.h +1
-1@@ -425,7 +425,7 @@ struct xen_set_cppc_para {#define XEN_HWP_DRIVER_NAME "hwp"#define XEN_AMD_PSTATE_DRIVER_NAME "amd-pstate" - +#define XEN_AMD_PSTATE_EPP_DRIVER_NAME "amd-pstate-epp"/*
Please don't lose blank lines like this.
Jan
RE: [PATCH v1 09/11] xen/x86: implement EPP support for the AMD processors
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 24 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, January 9, 2025 7:38 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v1 09/11] xen/x86: implement EPP support for the AMD processorsxen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 470
Quoted 7 lines · Penny Zheng
xen/arch/x86/acpi/cpufreq/amd-pstate.c +7
-0+ +/* Initial min/max values for CPPC Performance Controls Register */ + max_perf = data->hw.highest_perf; + min_perf = data->hw.lowest_perf; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) + min_perf = max_perf;Why can't this be done ...
Quoted 8 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 477
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +6
-0+/* CPPC EPP feature require to set zero to the desire perf bit */ + des_perf =0 ; + +if ( data->policy ==CPUFREQ_POLICY_PERFORMANCE ) +/* Force the epp value to be zero for performance policy */ + epp =CPPC_ENERGY_PERF_MAX_PERFORMANCE ;... here as well? And why is there nothing respective for ...
Ack
Quoted 6 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c static int cf_check amd_pstate_cpufreq_cpu_exit(struct cpufreq_policy *policy) · line 480
Quoted 4 lines
xen/arch/x86/acpi/cpufreq/amd-pstate.c +3
-0+else if ( data->policy ==CPUFREQ_POLICY_POWERSAVE ) +/* Force the epp value to be 0xff for powersave policy */ + epp =CPPC_ENERGY_PERF_MAX_POWERSAVE ;... this case (e.g. setting
max_perffrommin_perf)?
If we put max_perf = min_perf = lowest_perf/lowest_nonlinear_perf, then
we are putting core in idle state. That's how we offline the cpu core in Linux
amd pstate epp driver, see https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cpufreq/amd-pstate.c?h=v6.13#n1643
Quoted 2 lines
Jan
Many thanks,
Penny
Quoted 43 lines · Penny Zheng
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series in Xen. The new mechanism is based on Collaborative Processor Performance Control (CPPC) which provides finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU/APU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. CPPC replaces the ACPI P-states controls and allows a flexible, low-latency interface for Xen to directly communicate the performance hints to hardware.
amd_pstateCPPC has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-pstate" for passive mode and "amd-pstate-epp" for active mode.The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance hints. And the active mode uses epp to provides a hint to the hardware if software wants to bias toward performance (
0x0) or energy efficiency (0xff). CPPC power algorithm will calculate the runtime workload and adjust the realtime cpu cores frequency according to the power supply and thermal, core voltage and some other hardware conditions.amd-pstate is enabled with a top-level cpufreq=amd-pstate option. It will fallback to cpufreq=xen if amd-pstate is unavailable.
With
cpufreq=amd-pstate,active, We did a 60s sampling test to see the CPU frequency change, through tweaking theenergy_perfpreference fromxenpm set-cpufreq-cppc powersavetoxenpm set-cpufreq-cppc performance. The outputs are as follows:``` Setting CPU in powersave mode Sampling and Outputs: Avg freq 2000000 KHz Avg freq 2000000 KHz Avg freq 2000000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```
Quoted 14 lines
Penny Zheng (11):
xen/x86: add CPPC feature flag for AMD processors xen/x86: introduce new sub-hypercall to get CPPC data xen/x86: introduce "cpufreq=amd-pstate" xen cmdline xen/x86: get processor max speed from DMI table xen/x86: introduce a new amd pstate driver for cpufreq scaling xen/cpufreq: introduce policy type whencpufreq_driver->setpolicy
exists
xen/cpufreq: only set gov NULL whencpufreq_driver.target()exists x86/cpufreq: add "cpufreq=amd-pstate,active" para xen/x86: implement EPP support for the AMD processorstools/xenpm: Print CPPC parameters for amd-pstate driver xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPCxen_sysctl_pm_opfor
amd-pstate driver
Just to clarify: While certainly fine, it is a little surprising to see such a submission just after the submission deadline for 4.20 was passed. This is intended for 4.21 then, I expect? Or else have you talked to the release manager?
Jan
[AMD Official Use Only - AMD Internal Distribution Only]
Hi,
Quoted 72 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Tuesday, December 3, 2024 5:01 PM
To: Penny, Zheng <[email protected]>
Cc: Stabellini, Stefano <[email protected]>; Huang, Ray <[email protected]>; Ragiadakou, Xenia <[email protected]>; Andryuk, Jason <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Anthony PERARD <[email protected]>; [email protected]
Subject: Re: [PATCH v1 00/11] amd-pstate CPU Performance Scaling DriverQuoted 23 lines · Penny Zheng
amd-pstate is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism on modern AMD APU and CPU series in Xen. The new mechanism is based on Collaborative Processor Performance Control (CPPC) which provides finer grain frequency management than legacy ACPI hardware P-States. Current AMD CPU/APU platforms are using the ACPI P-states driver to manage CPU frequency and clocks with switching only in 3 P-states. CPPC replaces the ACPI P-states controls and allows a flexible, low-latency interface for Xen to directly communicate the performance hints to hardware.
amd_pstateCPPC has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-pstate" for passive mode and "amd-pstate-epp" for active mode.The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance hints. And the active mode uses epp to provides a hint to the hardware if software wants to bias toward performance (
0x0) or energy efficiency (0xff). CPPC power algorithm will calculate the runtime workload and adjust the realtime cpu cores frequency according to the power supply and thermal, core voltage and some other hardware conditions.Quoted 6 lines
amd-pstate is enabled with a top-level cpufreq=amd-pstate option. It will fallback to cpufreq=xen if amd-pstate is unavailable.
With
cpufreq=amd-pstate,active, We did a 60s sampling test to see the CPU frequency change, through tweaking theenergy_perfpreference fromxenpm set-cpufreq-cppc powersaveto `xenpm set-cpufreq-cppcperformance`.
Quoted 14 lines
The outputs are as follows:
``` Setting CPU in powersave mode Sampling and Outputs: Avg freq 2000000 KHz Avg freq 2000000 KHz Avg freq 2000000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```Quoted 14 lines
Penny Zheng (11):
xen/x86: add CPPC feature flag for AMD processors xen/x86: introduce new sub-hypercall to get CPPC data xen/x86: introduce "cpufreq=amd-pstate" xen cmdline xen/x86: get processor max speed from DMI table xen/x86: introduce a new amd pstate driver for cpufreq scaling xen/cpufreq: introduce policy type whencpufreq_driver->setpolicy
exists
xen/cpufreq: only set gov NULL whencpufreq_driver.target()exists x86/cpufreq: add "cpufreq=amd-pstate,active" para xen/x86: implement EPP support for the AMD processorstools/xenpm: Print CPPC parameters for amd-pstate driver xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPCxen_sysctl_pm_opfor
amd-pstate driver
Quoted 2 lines
Just to clarify: While certainly fine, it is a little surprising to see such a submission just after the submission deadline for 4.20 was passed.
So sorry.... Forgot we're in feature frozen window
Quoted 2 lines
This is intended for 4.21 then, I expect? Or else have you talked to the release manager?
Yes, it is for 4.21
Quoted 2 lines
Jan
Many thanks,
Penny,