amd-cppc CPU Performance Scaling Driver
v8 cover amd-cppc CPU Performance Scaling Driver
amd-cppc 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_cppc driver has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-cppc" for passive mode and "amd-cppc-epp" for active mode.
The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance tuning. While 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 in hardware will automatically 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-cppc is enabled on passive mode with a top-level cpufreq=amd-cppc option, while users add extra active flag to select active mode.
With cpufreq=amd-cppc,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 580000 KHz Avg freq 580000 KHz Avg freq 580000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```
Penny Zheng (8):
xen/cpufreq: introduce new sub-hypercall to propagate CPPC data xen/cpufreq: introduce "cpufreq=amd-cppc" xen cmdline and amd-cppc
driver
xen/cpufreq: implement amd-cppc driver for CPPC in passive mode xen/cpufreq: implement amd-cppc-epp driver for CPPC in active mode xen/cpufreq: get performance policy from governor set via xenpm tools/cpufreq: extract CPPC para from cpufreq para xen/cpufreq: bypass governor-related para for amd-cppc-epp xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPC xen_sysctl_pm_op for amd-cppc
driver
docs/misc/xen-command-line.pandoc | 14 +- tools/include/xenctrl.h | 3 +- tools/libs/ctrl/xc_pm.c | 25 +- tools/misc/xenpm.c | 94 ++- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-cppc.c | 766 ++++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 69 +- xen/arch/x86/cpu/amd.c | 8 +- xen/arch/x86/include/asm/amd.h | 2 + xen/arch/x86/include/asm/msr-index.h | 6 + xen/arch/x86/platform_hypercall.c | 19 + xen/arch/x86/x86_64/cpufreq.c | 19 + xen/arch/x86/x86_64/platform_hypercall.c | 3 + xen/drivers/acpi/pm-op.c | 68 +- xen/drivers/acpi/pmstat.c | 4 + xen/drivers/cpufreq/cpufreq.c | 137 +++- xen/drivers/cpufreq/utility.c | 15 + xen/include/acpi/cpufreq/cpufreq.h | 40 +- xen/include/acpi/cpufreq/processor_perf.h | 14 +- xen/include/public/platform.h | 26 + xen/include/public/sysctl.h | 5 +- xen/include/xen/pmstat.h | 5 + xen/include/xlat.lst | 1 + 23 files changed, 1283 insertions(+), 61 deletions(-) create mode 100644 xen/arch/x86/acpi/cpufreq/amd-cppc.c
Signature · 1 line
2.34.1
PATCH v8 1/8 xen/cpufreq: introduce new sub-hypercall to propagate CPPC data
In order to provide backward compatibility with existing governors that represent performance as frequency, like ondemand, the _CPC table can optionally provide processor frequency range values, Lowest frequency and Nominal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC performance to CPU frequency.
As Xen is uncapable of parsing the ACPI dynamic table, we'd like to introduce a new sub-hypercall "XEN_PM_CPPC" to propagate required CPPC data from dom0 kernel to Xen.
In the according handler set_cppc_pminfo(), we do _CPC and _PSD sanitization check, as both _PSD and _CPC info are necessary for correctly initializing cpufreq cores in CPPC mode.
Users shall be warned that if we failed at this point,
no fallback scheme, like legacy P-state could be switched to.
A new flag "XEN_CPPC_INIT" is also introduced for cpufreq core initialised in CPPC mode. Then all .init flag checking shall be updated to consider "XEN_CPPC_INIT" too.
We want to bypass construction of px statistic info in cpufreq_statistic_init() for CPPC mode, while not bypassing cpufreq_statistic_lock initialization for a good reason. The same check is unnecessary for cpufreq_statistic_exit(), since it has already been covered by px statistic variable "cpufreq_statistic_data" check
---
v1 -> v2:
- Give the variable des_perf an initializer of 0
- Use the strncmp()s directly in the if()
---
v3 -> v4
- refactor comments
- remove double blank lines
- replace amd_cppc_in_use flag with XEN_PROCESSOR_PM_CPPC
---
v4 -> v5:
- add new field "policy" in "struct xen_cppc_para"
- add new performamce policy XEN_CPUFREQ_POLICY_BALANCE
- drop string comparisons with "processor_pminfo[cpuid]->init & XEN_CPPC_INIT" and "cpufreq.setpolicy == NULL"
- Blank line ahead of the main "return" of a function
- refactor comments, commit message and title
---
v5 -> v6:
- remove duplicated manifest constants, and just move it to public header
- use "else if" to avoid confusion that it looks as if both paths could be taken
- add check for legitimate perf values
- use "unknown" instead of "none"
- introduce "CPUFREQ_POLICY_END" for array overrun check in user space tools
---
v6 -> v7:
- use ARRAY_SIZE() instead
- ->policy print is avoided in passive mode and print "unknown" in invalid cases
- let cpufreq_is_governorless() being the variable's initializer
- refactor with the conditional operator to increase readability
- move duplicated defination ahead and use local variable
- avoid using "else-condition" to bring "dead code" in Misra's nomeclature
- move the comment out of public header and into the respective internal struct field
- wrap set{,get}_amd_cppc_para() with CONFIG_PM_OP
- add symmetry scenario for maximum check
---
v7 -> v8:
- replace -EFAULT with -ENOMEM
- indent fix
- add comment for state_count check for CPPC mode
--- xen/arch/x86/platform_hypercall.c | 5 + xen/arch/x86/x86_64/cpufreq.c | 19 ++++ xen/arch/x86/x86_64/platform_hypercall.c | 3 + xen/drivers/acpi/pm-op.c | 7 +- xen/drivers/acpi/pmstat.c | 4 + xen/drivers/cpufreq/cpufreq.c | 124 +++++++++++++++++++++- xen/include/acpi/cpufreq/processor_perf.h | 4 +- xen/include/public/platform.h | 26 +++++ xen/include/xen/pmstat.h | 5 + xen/include/xlat.lst | 1 + 10 files changed, 193 insertions(+), 5 deletions(-)
xen/arch/x86/platform_hypercall.c +5 -0
@@ -577,6 +577,11 @@ 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 +19 -0
@@ -54,3 +54,22 @@ int compat_set_px_pminfo(uint32_t acpi_id,return set_px_pminfo (acpi_id, xen_perf); } + +int compat_set_cppc_pminfo (unsigned int acpi_id, +const 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 -ENOMEM ; + +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
@@ -14,6 +14,9 @@ EMIT_FILE;#define efi_get_info efi_compat_get_info#define efi_runtime_call (x) efi_compat_runtime_call(x) +#define xen_processor_cppc compat_processor_cppc +#define set_cppc_pminfo compat_set_cppc_pminfo +#define xen_processor_performance compat_processor_performance#define set_px_pminfo compat_set_px_pminfo
xen/drivers/acpi/pm-op.c +5 -2
@@ -91,7 +91,10 @@ static int get_cpufreq_para(struct xen_sysctl_pm_op *op) pmpt = processor_pminfo[op->cpuid]; policy =per_cpu (cpufreq_cpu_policy, op->cpuid); -if ( !pmpt || !pmpt->perf.states || +if ( !pmpt || + ((pmpt->init &XEN_PX_INIT ) && !pmpt->perf.states) || +/* Avoid NULL deref of pmpt->perf.states[] */ + ((pmpt->init &XEN_CPPC_INIT ) && pmpt->perf.state_count) || !policy || !policy->governor )return -EINVAL ;
@@ -351,7 +354,7 @@ int do_pm_op(struct xen_sysctl_pm_op *op)case CPUFREQ_PARA :if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_PX ) )return -ENODEV ; -if ( !pmpt || !(pmpt->init &XEN_PX_INIT ) ) +if ( !pmpt || !(pmpt->init & (XEN_PX_INIT |XEN_CPPC_INIT )) )return -EINVAL ;break ; }
xen/drivers/acpi/pmstat.c +4 -0
@@ -108,6 +108,10 @@ int cpufreq_statistic_init(unsigned int cpu)if ( !pmpt )return -EINVAL ; +/* Only need to initialize in Px mode */ +if ( !(pmpt->init &XEN_PX_INIT ) ) +return 0 ; +spin_lock (cpufreq_statistic_lock); pxpt =per_cpu (cpufreq_statistic_data, cpu);
xen/drivers/cpufreq/cpufreq.c +122 -2
154 lines not on this page · raw message
xen/include/acpi/cpufreq/processor_perf.h +3 -1
@@ -5,7 +5,8 @@#include <public/sysctl.h> #include <xen/acpi.h> -#define XEN_PX_INIT 0x80000000U +#define XEN_CPPC_INIT 0x40000000U +#define XEN_PX_INIT 0x80000000Uunsigned int powernow_register_driver (void );unsigned int get_measured_perf (unsigned int cpu,unsigned int flag);
@@ -43,6 +44,7 @@ struct processor_pminfo {uint32_t acpi_id;uint32_t id;struct processor_performance perf; +struct xen_processor_cppc cppc_data;uint32_t init; };
xen/include/public/platform.h +26 -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
@@ -370,6 +371,10 @@ DEFINE_XEN_GUEST_HANDLE(xenpf_getidletime_t);#define XEN_PX_PPC 4#define XEN_PX_PSD 8 +/* CPPC sub info type */ +#define XEN_CPPC_PSD (1U << 0) +#define XEN_CPPC_CPC (1U << 1) +struct xen_power_register {uint32_t space_id;uint32_t bit_width;
@@ -457,6 +462,26 @@ struct xen_processor_performance {typedef struct xen_processor_performance xen_processor_performance_t ;DEFINE_XEN_GUEST_HANDLE (xen_processor_performance_t); +struct xen_processor_cppc { +uint8_t flags;/* IN: XEN_CPPC_xxx */ +uint8_t pad[3 ]; +/* +* IN: Subset _CPC fields useful for CPPC-compatible cpufreq +* driver's initialization +*/ +struct { +uint32_t highest_perf; +uint32_t nominal_perf; +uint32_t lowest_nonlinear_perf; +uint32_t lowest_perf; +uint32_t lowest_mhz; +uint32_t nominal_mhz; + } cpc; +uint32_t shared_type;/* IN: XEN_CPUPERF_SHARED_TYPE_xxx */ +struct xen_psd_package domain_info;/* IN: _PSD */ +}; +typedef struct xen_processor_cppc xen_processor_cppc_t ; +struct xenpf_set_processor_pminfo {/* IN variables */ uint32_t id;/* ACPI CPU ID */
@@ -465,6 +490,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 */ +xen_processor_cppc_t cppc_data;/* CPPC: _CPC and _PSD */ } u; };typedef struct xenpf_set_processor_pminfo xenpf_set_processor_pminfo_t ;
xen/include/xen/pmstat.h +5 -0
@@ -7,12 +7,17 @@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); +int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data);#ifdef CONFIG_COMPAT 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 (unsigned int acpi_id, +const struct compat_processor_cppc *cppc_data);#endif uint32_t pmstat_get_cx_nr (unsigned int cpu);
xen/include/xlat.lst +1 -0
@@ -160,6 +160,7 @@ ! pct_register platform.h ! power_register platform.h +! processor_cppc platform.h ? processor_csd platform.h ! processor_cx platform.h ! processor_flags platform.h
Signature · 1 line
2.34.1
Re: [PATCH v8 1/8] xen/cpufreq: introduce new sub-hypercall to propagate CPPC data
Quoted 20 lines · Penny Zheng
In order to provide backward compatibility with existing governors that represent performance as frequency, like ondemand, the
_CPCtable can optionally provide processor frequency range values, Lowest frequency and Nominal frequency, to let OS use Lowest Frequency/ Performance and Nominal Frequency/Performance as anchor points to create linear mapping of CPPC performance to CPU frequency.As Xen is uncapable of parsing the ACPI dynamic table, we'd like to introduce a new sub-hypercall "
XEN_PM_CPPC" to propagate required CPPC data from dom0 kernel to Xen.
In the according handlerset_cppc_pminfo(), we do_CPCand_PSDsanitization check, as both_PSDand_CPCinfo are necessary for correctly initializing cpufreq cores in CPPC mode.
Users shall be warned that if we failed at this point,
no fallback scheme, like legacy P-state could be switched to.A new flag "
XEN_CPPC_INIT" is also introduced for cpufreq core initialised in CPPC mode. Then all .init flag checking shall be updated to consider "XEN_CPPC_INIT" too.
Quoted 7 lines
We want to bypass construction of px statistic info in
cpufreq_statistic_init()for CPPC mode, while not bypassingcpufreq_statistic_lockinitialization for a good reason. The same check is unnecessary forcpufreq_statistic_exit(), since it has already been covered by px statistic variable "cpufreq_statistic_data" checkSigned-off-by Penny Zheng <[email protected]>
with two cosmetic issues taken care of (which I'll do while committing):
Quoted 44 lines
xen/drivers/cpufreq/cpufreq.c +40
-0@@ -693,6 +699,120 @@ int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc)return ret; } +static void print_CPPC (const struct xen_processor_cppc *cppc_data) +{ +printk ("\t_CPC: highest_perf=%u, lowest_perf=%u, " +"nominal_perf=%u, lowest_nonlinear_perf=%u, " +"nominal_mhz=%uMHz, lowest_mhz=%uMHz\n" , + cppc_data->cpc.highest_perf, cppc_data->cpc.lowest_perf, + cppc_data->cpc.nominal_perf, cppc_data->cpc.lowest_nonlinear_perf, + cppc_data->cpc.nominal_mhz, cppc_data->cpc.lowest_mhz); +} + +int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data) +{ +int ret =0 , cpuid; +struct processor_pminfo *pm_info; + + cpuid =get_cpu_id (acpi_id); +if ( cpuid <0 ) + { + ret = -EINVAL ; + goto out; + } + +if ( cppc_data->pad[0 ] || cppc_data->pad[1 ] || cppc_data->pad[2 ] ) + { + ret = -EINVAL ; + goto out; + } + +if ( cpufreq_verbose ) +printk ("Set CPU%d (ACPI ID %u) CPPC state info:\n" , + cpuid, acpi_id); + + pm_info = processor_pminfo[cpuid]; +if ( !pm_info ) + { + pm_info = xvzalloc(struct processor_pminfo); +if ( !pm_info ) + { + ret = -ENOMEM ;
xen/drivers/cpufreq/cpufreq.c int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc) · line 751
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; + pm_info->cppc_data = *cppc_data; + +if ( (cppc_data->flags &XEN_CPPC_PSD ) && + !check_psd_pminfo (cppc_data->shared_type) )
Nit: Indentation is off by 1.
xen/drivers/cpufreq/cpufreq.c int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc) · line 722
Quoted 3 lines
xen/drivers/cpufreq/cpufreq.c +3
-0+ { + ret = -EINVAL ; + goto out;
Indentation is still wrong here.
Jan
Re: [PATCH v8 1/8] xen/cpufreq: introduce new sub-hypercall to propagate CPPC data
In reply to Penny ZhengQuoted 8 lines · Penny Zheng
xen/drivers/cpufreq/cpufreq.c +4
-0@@ -693,6 +699,120 @@ int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc)return ret; } +static void print_CPPC (const struct xen_processor_cppc *cppc_data) +{ +printk ("\t_CPC: highest_perf=%u, lowest_perf=%u, " + "nominal_perf=%u, lowest_nonlinear_perf=%u, "
xen/drivers/cpufreq/cpufreq.c int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc) · line 715
Quoted 10 lines
xen/drivers/cpufreq/cpufreq.c +10
-0+"nominal_mhz=%uMHz, lowest_mhz=%uMHz\n" , + cppc_data->cpc.highest_perf, cppc_data->cpc.lowest_perf, + cppc_data->cpc.nominal_perf, cppc_data->cpc.lowest_nonlinear_perf, + cppc_data->cpc.nominal_mhz, cppc_data->cpc.lowest_mhz); +} + +int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data) +{ +int ret =0 , cpuid;
Eclair doesn't like this:
Reports for service MC3A2.R5.3
service MC3A2.R5.3: (required) An identifier declared in an inner scope shall not hide an identifier declared in an outer scope (1 of 1 violation)
violation for MC3A2.R5.3 untagged
xen/arch/x86/include/asm/processor.h:130.20-130.24:
non-compliant function cpuid(unsigned, unsigned*, unsigned*, unsigned*, unsigned*)' (unit xen/drivers/cpufreq/cpufreq.c' with target xen/drivers/cpufreq/.cpufreq.o.tmp'): there is another identifier cpuid' xen/drivers/cpufreq/cpufreq.c:726.18-726.22:
non-compliant local variable cpuid': there is another identifier cpuid'
I'm fixing this up for you, but I have to admit that I'm getting tired of doing such cleanups for supposedly-ready-to-commit patches.
Jan
RE: [PATCH v8 1/8] xen/cpufreq: introduce new sub-hypercall to propagate CPPC data
[Public]
Quoted 45 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 8:50 PM
To: Penny, Zheng <[email protected]>
Cc: Huang, Ray <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Anthony PERARD <[email protected]>; Orzel, Michal <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]
Subject: Re: [PATCH v8 1/8] xen/cpufreq: introduce new sub-hypercall to propagate CPPC dataReview int acpi_set_pdc_bits(unsigned int acpi_id,
Quoted 1 line · Penny Zheng
(quoted diff) +0
-0@@ -693,6 +699,120 @@ int acpi_set_pdc_bits(unsigned int acpi_id,
XEN_GUEST_HANDLE(uint32)pdc)Quoted 4 lines
xen/drivers/cpufreq/cpufreq.c +1
-0return ret; } +static void print_CPPC (const struct xen_processor_cppc *cppc_data) {xen/drivers/cpufreq/cpufreq.c int acpi_set_pdc_bits(unsigned int acpi_id, XEN_GUEST_HANDLE(uint32) pdc) · line 715
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_mhz=%uMHz, lowest_mhz=%uMHz\n" , + cppc_data->cpc.highest_perf, cppc_data->cpc.lowest_perf, + cppc_data->cpc.nominal_perf, cppc_data->cpc.lowest_nonlinear_perf, + cppc_data->cpc.nominal_mhz, cppc_data->cpc.lowest_mhz); } + +int set_cppc_pminfo (unsigned int acpi_id, +const struct xen_processor_cppc *cppc_data) { +int ret =0 , cpuid;Eclair doesn't like this:
Reports for service MC3A2.R5.3
service MC3A2.R5.3: (required) An identifier declared in an inner scope shall not hide an identifier declared in an outer scope (1 of 1 violation)
violation for MC3A2.R5.3 untagged
xen/arch/x86/include/asm/processor.h:130.20-130.24:
non-compliant functioncpuid(unsigned, unsigned*, unsigned*, unsigned*, unsigned*)' (unitxen/drivers/cpufreq/cpufreq.c' with targetxen/drivers/cpufreq/.cpufreq.o.tmp'): there is another identifiercpuid' xen/drivers/cpufreq/cpufreq.c:726.18-726.22:
non-compliant local variablecpuid': there is another identifiercpuid'
Quoted 3 lines
I'm fixing this up for you, but I have to admit that I'm getting tired of doing such cleanups for supposedly-ready-to-commit patches.
So sorry. I was missing such misra CI in our internal. I'll add it to avoid such errors
Quoted 1 line
Jan
PATCH v8 2/8 xen/cpufreq: introduce "cpufreq=amd-cppc" xen cmdline and amd-cppc driver
In reply to Penny ZhengUsers need to set "cpufreq=amd-cppc" in xen cmdline to enable amd-cppc 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-cppc", a new amd-cppc driver shall be registered and used. All hooks for amd-cppc driver are transiently missing, and we temporarily make registration fail with -EOPNOTSUPP here. It will be fixed along with the implementation.
New xen-pm internal flag XEN_PROCESSOR_PM_CPPC is introduced, to stand for cpufreq driver in CPPC mode. We define XEN_PROCESSOR_PM_CPPC 0x100, as it is the next value to use after 8-bits wide public xen-pm options. We also add sanity check on compile time. All XEN_PROCESSOR_PM_xxx checking shall be updated to consider "XEN_PROCESSOR_PM_CPPC" too.
XEN_PROCESSOR_PM_CPPC and XEN_PROCESSOR_PM_PX are firstly set when Xen parsed relative driver signature from xen cmdline, and will become exclusive after cpufreq driver registration. It is because that platform could not support both or mixed mode (CPPC & legacy Px) operations, and only one cpufreq driver could be registerd in Xen at one time, such as on AMD, it is either amd-cppc or legacy P-states driver.
Xen rely on XEN_PROCESSOR_PM_CPPC flag to tell current cpufreq driver is in CPPC mode, and accepts relative hypercall. It will neglect Px request and yields success.
---
---
v1 -> v2:
- Obey to alphabetic sorting and also strict it with CONFIG_AMD
- Remove unnecessary empty comment line
- Use __initconst_cf_clobber for pre-filled structure cpufreq_driver
- Make new switch-case code apply to Hygon CPUs too
- Change ENOSYS with EOPNOTSUPP
- Blanks around binary operator
- Change all amd_/-pstate defined values to amd_/-cppc
---
v2 -> v3
- refactor too long lines
- Make sure XEN_PROCESSOR_PM_PX and XEN_PROCESSOR_PM_CPPC incompatible flags after cpufreq register registrantion
---
v3 -> v4:
- introduce XEN_PROCESSOR_PM_CPPC in xen internal header
- complement "Hygon" in log message
- remove unnecessary if()
- grow cpufreq_xen_opts[] array
---
v4 -> v5:
- remove XEN_PROCESSOR_PM_xxx flag sanitization from individual driver
- prefer ! over "== 0" in purely boolean contexts
- Blank line between non-fall-through case blocks
- add build-time checking between internal and public XEN_PROCESSOR_PM_* values
- define XEN_PROCESSOR_PM_CPPC with 0x100, as it is the next value to use after public interface, and public mask SIF_PM_MASK is 8 bits wide.
- as Dom0 will send the CPPC/Px data whenever it could, the return value shall be 0 instead of -ENOSYS/EOPNOTSUP when platform doesn't require these data.
---
v5 -> v6:
- do not register the driver when all hooks are NULL
- refactor the subject and commit message
- move pruning of xen_processor_pmbits into generic space
- add comment and build-time check for XEN_PROCESSOR_PM_CPPC
---
v6 -> v7:
- reomove pointless initializer for "unsigned int i"
- move closing brace into its own line
- insertion always at the bottom
- change to use #ifdef CONFIG_AMD code wrapping
---
v7 -> v8:
- Blank line between declaration(s) and statement(s)
--- docs/misc/xen-command-line.pandoc | 7 ++- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-cppc.c | 59 +++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 69 ++++++++++++++++++++++- xen/arch/x86/platform_hypercall.c | 14 +++++ xen/drivers/acpi/pm-op.c | 3 +- xen/drivers/cpufreq/cpufreq.c | 13 ++++- xen/include/acpi/cpufreq/cpufreq.h | 6 +- xen/include/acpi/cpufreq/processor_perf.h | 10 ++++ 9 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 xen/arch/x86/acpi/cpufreq/amd-cppc.c
docs/misc/xen-command-line.pandoc +5 -2
@@ -515,7 +515,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-cppc[:[verbose]]` > Default: `xen`
@@ -526,7 +526,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-cppc`. * `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
@@ -534,6 +534,9 @@ 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-cppc` selects ACPI Collaborative Performance and Power Control (CPPC) + on supported AMD hardware to provide finer grained frequency control + mechanism. The default is disabled. 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-$(CONFIG_AMD) += amd-cppc.o obj-y += cpufreq.o obj-$(CONFIG_INTEL) += hwp.o obj-$(CONFIG_AMD) += powernow.o
xen/arch/x86/acpi/cpufreq/amd-cppc.c new file +59 -0
59 lines not on this page · raw message
xen/arch/x86/acpi/cpufreq/cpufreq.c +67 -2
98 lines not on this page · raw message
xen/arch/x86/platform_hypercall.c +14 -0
@@ -546,6 +546,8 @@ ret_t do_platform_op( ret =0 ;break ; } +/* Xen doesn't support mixed mode */ +ASSERT (!(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC )); ret =set_px_pminfo (op->u.set_pminfo.id, &op->u.set_pminfo.u.perf);break ;
@@ -578,6 +580,18 @@ ret_t do_platform_op( }case XEN_PM_CPPC : +if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_CPPC ) ) + { +/* +* Neglect CPPC-info when registered cpufreq driver +* isn't in CPPC mode +*/ + ret =0 ; +break ; + } +/* Xen doesn't support mixed mode */ +ASSERT (!(xen_processor_pmbits &XEN_PROCESSOR_PM_PX )); + ret =set_cppc_pminfo (op->u.set_pminfo.id, &op->u.set_pminfo.u.cppc_data);break ;
xen/drivers/acpi/pm-op.c +2 -1
@@ -352,7 +352,8 @@ int do_pm_op(struct xen_sysctl_pm_op *op)switch ( op->cmd &PM_PARA_CATEGORY_MASK ) {case CPUFREQ_PARA : -if ( !(xen_processor_pmbits &XEN_PROCESSOR_PM_PX ) ) +if ( !(xen_processor_pmbits & (XEN_PROCESSOR_PM_PX | +XEN_PROCESSOR_PM_CPPC )) )return -ENODEV ;if ( !pmpt || !(pmpt->init & (XEN_PX_INIT |XEN_CPPC_INIT )) )return -EINVAL ;
xen/drivers/cpufreq/cpufreq.c +12 -1
@@ -65,7 +65,7 @@ LIST_HEAD_READ_MOSTLY(cpufreq_governor_list);/* set xen as default cpufreq */ enum cpufreq_controller cpufreq_controller = FREQCTL_xen; -enum cpufreq_xen_opt __initdata cpufreq_xen_opts[2 ] = { [0 ] = CPUFREQ_xen }; +enum cpufreq_xen_opt __initdata cpufreq_xen_opts[3 ] = { [0 ] = CPUFREQ_xen };unsigned int __initdata cpufreq_xen_cnt =1 ;static int __init cpufreq_cmdline_parse (const char *s,const char *e);
@@ -99,6 +99,10 @@ static int __init handle_cpufreq_cmdline(enum cpufreq_xen_opt option) xen_processor_pmbits |=XEN_PROCESSOR_PM_PX ;break ; +case CPUFREQ_amd_cppc: + xen_processor_pmbits |=XEN_PROCESSOR_PM_CPPC ; +break ; +default :ASSERT_UNREACHABLE (); ret = -EINVAL ;
@@ -162,6 +166,13 @@ static int __init cf_check setup_cpufreq_option(const char *str)if ( !ret && arg[0 ] && arg[1 ] ) ret =hwp_cmdline_parse (arg +1 , end); } +else if (IS_ENABLED (CONFIG_AMD ) && choice <0 && + !cmdline_strcmp (str,"amd-cppc" ) ) + { + ret =handle_cpufreq_cmdline (CPUFREQ_amd_cppc); +if ( !ret && arg[0 ] && arg[1 ] ) + ret =amd_cppc_cmdline_parse (arg +1 , end); + }else ret = -EINVAL ;
xen/include/acpi/cpufreq/cpufreq.h +5 -1
@@ -26,8 +26,9 @@ enum cpufreq_xen_opt { CPUFREQ_none, CPUFREQ_xen, CPUFREQ_hwp, + CPUFREQ_amd_cppc, }; -extern enum cpufreq_xen_opt cpufreq_xen_opts[2 ]; +extern enum cpufreq_xen_opt cpufreq_xen_opts[3 ];extern unsigned int cpufreq_xen_cnt;struct cpufreq_governor ;
@@ -272,4 +273,7 @@ int set_hwp_para(struct cpufreq_policy *policy,int acpi_cpufreq_register (void ); +int amd_cppc_cmdline_parse (const char *s,const char *e); +int amd_cppc_register_driver (void ); +#endif /* __XEN_CPUFREQ_PM_H__ */
xen/include/acpi/cpufreq/processor_perf.h +10 -0
@@ -5,6 +5,16 @@#include <public/sysctl.h> #include <xen/acpi.h> +/* +* Internal xen-pm options +* They are extension to public xen-pm options (XEN_PROCESSOR_PM_xxx) defined +* in public/platform.h, guarded by SIF_PM_MASK +*/ +#define XEN_PROCESSOR_PM_CPPC 0x100 +#if XEN_PROCESSOR_PM_CPPC &MASK_EXTR (~0 ,SIF_PM_MASK ) +# error "XEN_PROCESSOR_PM_CPPC shall not occupy bits reserved for public xen-pm options" +#endif +#define XEN_CPPC_INIT 0x40000000U#define XEN_PX_INIT 0x80000000U
Signature · 1 line
2.34.1
PATCH v8 3/8 xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
In reply to Penny Zhengamd-cppc is the AMD CPU performance scaling driver that introduces a new CPU frequency control mechanism. 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, while the new amd-cppc allows a more flexible, low-latency interface for Xen to directly communicate the performance hints to hardware.
"amd-cppc" driver is responsible for implementing CPPC in passive mode, which still leverages Xen governors such as *ondemand*, *performance*, etc, to calculate the performance hints. In the future, we will introduce an advanced active mode to enable autonomous performence level selection.
Field epp, energy performance preference, which only has meaning when active mode is enabled and will be introduced later in details, so we read pre-defined BIOS value for it in passive mode.
---
---
v1 -> v2:
- re-construct union caps and req to have anonymous struct instead
- avoid "else" when the earlier if() ends in an unconditional control flow statement
- Add check to avoid chopping off set bits from cast
- make pointers pointer-to-const wherever possible
- remove noisy log
- exclude families before 0x17 before CPPC-feature MSR op
- remove useless variable helpers
- use xvzalloc and XVFREE
- refactor error handling as ENABLE bit can only be cleared by reset
---
v2 -> v3:
- Move all MSR-definations to msr-index.h and follow the required style
- Refactor opening figure braces for struct/union
- Sort overlong lines throughout the series
- Make offset/res int covering underflow scenario
- Error out when amd_max_freq_mhz isn't set
- Introduce amd_get_freq(name) macro to decrease redundancy
- Supported CPU family checked ahead of smp-function
- Nominal freq shall be checked between the [min, max]
- Use APERF/MPREF to calculate current frequency
- Use amd_cppc_cpufreq_cpu_exit() to tidy error path
---
v3 -> v4:
- verbose print shall come with a CPU number
- deal with res <= 0 in amd_cppc_khz_to_perf()
- introduce a single helper amd_get_lowest_or_nominal_freq() to cover both lowest and nominal scenario
- reduce abuse of wrmsr_safe()/rdmsr_safe() with wrmsrl()/rdmsrl()
- move cf_check from amd_cppc_write_request() to amd_cppc_write_request_msrs()
- add comment to explain why setting non_linear_lowest in passive mode
- add check to ensure perf values in
lowest <= non_linear_lowest <= nominal <= highset
- refactor comment for "data->err != 0" scenario
- use "data->err" instead of -ENODEV
- add U suffixes for all msr macro
---
v4 -> v5:
- all freq-values shall be unsigned int type
- remove shortcuts as it is rarely taken
- checking cpc.nominal_mhz and cpc.lowest_mhz are non-zero values is enough
- drop the explicit type cast
- null pointer check is in no need for internal functions
- change amd_get_lowest_or_nominal_freq() to amd_get_cpc_freq()
- clarifying function-wide that the calculated frequency result is to be in kHz
- use array notation
- with cpu_has_cppc check, no need to do cpu family check
---
v5 -> v6
- replace "AMD_CPPC" with "AMD-CPPC" in message
- add equation(mul,div) non-zero check
- replace -EINVAL with -EOPNOTSUPP
- refactor comment
---
v6 -> v7
- used > in place of !=, to not only serve a doc aspect, but also allow to drop one part
- unify with UINT8_MAX
- return -ERANGE as we reject perf values of 0 as invalid
- replace uint32_t with unsigned int
- Move some epp introduction here, otherwise we will mis-handle this field here by always clearing it
---
v7 -> v8:
- refine message text by removing 0
--- xen/arch/x86/acpi/cpufreq/amd-cppc.c | 418 ++++++++++++++++++++++++++- xen/arch/x86/cpu/amd.c | 8 +- xen/arch/x86/include/asm/amd.h | 2 + xen/arch/x86/include/asm/msr-index.h | 6 + xen/include/public/sysctl.h | 1 + 5 files changed, 430 insertions(+), 5 deletions(-)
xen/arch/x86/acpi/cpufreq/amd-cppc.c +417 -1
@@ -14,7 +14,98 @@#include <xen/domain.h> #include <xen/init.h> #include <xen/param.h> +#include <xen/percpu.h> +#include <xen/xvmalloc.h> #include <acpi/cpufreq/cpufreq.h> +#include <asm/amd.h> +#include <asm/msr.h> + +#define amd_cppc_err(cpu, fmt, args...) \ + printk(XENLOG_ERR "AMD-CPPC: CPU%u error: " fmt, cpu, ## args) +#define amd_cppc_warn(cpu, fmt, args...) \ + printk(XENLOG_WARNING "AMD-CPPC: CPU%u warning: " fmt, cpu, ## args) +#define amd_cppc_verbose(cpu, fmt, args...) \ +({ \ + if ( cpufreq_verbose ) \ + printk(XENLOG_DEBUG "AMD-CPPC: CPU%u " fmt, cpu, ## args); \ +}) + +/* +* Field highest_perf, nominal_perf, lowest_nonlinear_perf, and lowest_perf +* contain the values read from CPPC capability MSR. They represent the limits +* of managed performance range as well as the dynamic capability, which may +* change during processor operation +* Field highest_perf represents highest performance, which is the absolute +* maximum performance an individual processor may reach, assuming ideal +* conditions. This performance level may not be sustainable for long +* durations and may only be achievable if other platform components +* are in a specific state; for example, it may require other processors be +* in an idle state. This would be equivalent to the highest frequencies +* supported by the processor. +* Field nominal_perf represents maximum sustained performance level of the +* processor, assuming ideal operating conditions. All cores/processors are +* expected to be able to sustain their nominal performance state +* simultaneously. +* Field lowest_nonlinear_perf represents Lowest Nonlinear Performance, which +* is the lowest performance level at which nonlinear power savings are +* achieved. Above this threshold, lower performance levels should be +* generally more energy efficient than higher performance levels. So in +* traditional terms, this represents the P-state range of performance levels. +* Field lowest_perf represents the absolute lowest performance level of the +* platform. Selecting it may cause an efficiency penalty but should reduce +* the instantaneous power consumption of the processor. So in traditional +* terms, this represents the T-state range of performance levels. +* +* Field max_perf, min_perf, des_perf store the values for CPPC request MSR. +* Software passes performance goals through these fields. +* Field max_perf conveys the maximum performance level at which the platform +* may run. And it may be set to any performance value in the range +* [lowest_perf, highest_perf], inclusive. +* Field min_perf conveys the minimum performance level at which the platform +* may run. And it may be set to any performance value in the range +* [lowest_perf, highest_perf], inclusive but must be less than or equal to +* max_perf. +* Field des_perf conveys performance level Xen governor is requesting. And it +* may be set to any performance value in the range [min_perf, max_perf], +* inclusive. +* Field epp represents energy performance preference, which only has meaning +* when active mode is enabled. +*/ +struct amd_cppc_drv_data +{ +const struct xen_processor_cppc *cppc_data; +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; +union { +uint64_t raw; +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; +}; + +static DEFINE_PER_CPU_READ_MOSTLY(struct amd_cppc_drv_data *, + amd_cppc_drv_data); +/* +* Core max frequency read from PstateDef as anchor point +* for freq-to-perf transition +*/ +static DEFINE_PER_CPU_READ_MOSTLY(unsigned int, pxfreq_mhz); +static DEFINE_PER_CPU_READ_MOSTLY(uint8_t, epp_init);static bool __init amd_cppc_handle_option (const char *s,const char *end) {
@@ -50,10 +141,335 @@ int __init amd_cppc_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 linear function passing by the 2 points: +* - (Low perf, Low freq) +* - (Nominal perf, Nominal freq) +* Parameter freq is always in kHz. +*/ +static int amd_cppc_khz_to_perf (const struct amd_cppc_drv_data *data, +unsigned int freq,uint8_t *perf) +{ +const struct xen_processor_cppc *cppc_data = data->cppc_data; +unsigned int mul, div; +int offset =0 , res; + +if ( cppc_data->cpc.lowest_mhz && + data->caps.nominal_perf > data->caps.lowest_perf && + cppc_data->cpc.nominal_mhz > cppc_data->cpc.lowest_mhz ) + { + mul = data->caps.nominal_perf - data->caps.lowest_perf; + div = cppc_data->cpc.nominal_mhz - cppc_data->cpc.lowest_mhz; + +/* +* We don't need to convert to kHz for computing offset and can +* directly use nominal_mhz and lowest_mhz as the division +* will remove the frequency unit. +*/ + offset = data->caps.nominal_perf - + (mul * cppc_data->cpc.nominal_mhz) / div; + } +else + { +/* Read Processor Max Speed(MHz) as anchor point */ + mul = data->caps.highest_perf; + div =this_cpu (pxfreq_mhz); +if ( !div ) +return -EOPNOTSUPP ; + } + + res = offset + (mul * freq) / (div *1000 ); +if ( res >UINT8_MAX ) + { +printk_once (XENLOG_WARNING +"Perf value exceeds maximum value 255: %d\n" , res); + *perf =UINT8_MAX ; +return 0 ; + } +if ( res <=0 ) + { +printk_once (XENLOG_WARNING +"Perf value smaller than minimum value: %d\n" , res); +return -ERANGE ; + } + *perf = res; + +return 0 ; +} + +/* +* _CPC may define nominal frequecy and lowest frequency, if not, use +* Processor Max Speed as anchor point to calculate. +* Output freq stores cpc frequency in kHz +*/ +static int amd_get_cpc_freq (const struct amd_cppc_drv_data *data, +unsigned int cpc_mhz,uint8_t perf, +unsigned int *freq) +{ +unsigned int mul, div, res; + +if ( cpc_mhz ) + { +/* Switch to kHz */ + *freq = cpc_mhz *1000 ; +return 0 ; + } + +/* Read Processor Max Speed(MHz) as anchor point */ + mul =this_cpu (pxfreq_mhz); +if ( !mul ) +return -EOPNOTSUPP ; + div = data->caps.highest_perf; + res = (mul * perf *1000 ) / div; +if (unlikely (!res) ) +return -EOPNOTSUPP ; + +return 0 ; +} + +/* Output max_freq stores calculated maximum frequency in kHz */ +static int amd_get_max_freq (const struct amd_cppc_drv_data *data, +unsigned int *max_freq) +{ +unsigned int nom_freq =0 ; +int res; + + res =amd_get_cpc_freq (data, data->cppc_data->cpc.nominal_mhz, + data->caps.nominal_perf, &nom_freq); +if ( res ) +return res; + + *max_freq = (data->caps.highest_perf * nom_freq) / data->caps.nominal_perf; + +return 0 ; +} + +static int cf_check amd_cppc_cpufreq_verify(struct cpufreq_policy *policy) +{ +cpufreq_verify_within_limits (policy, policy->cpuinfo.min_freq, + policy->cpuinfo.max_freq); + +return 0 ; +} + +static void cf_check amd_cppc_write_request_msrs(void *info) +{ +const struct amd_cppc_drv_data *data = info; + +wrmsrl (MSR_AMD_CPPC_REQ , data->req.raw); +} + +static void amd_cppc_write_request (unsigned int cpu,uint8_t min_perf, +uint8_t des_perf,uint8_t max_perf, +uint8_t epp) +{ +struct amd_cppc_drv_data *data =per_cpu (amd_cppc_drv_data, cpu); +uint64_t prev = data->req.raw; + + data->req.min_perf = min_perf; + data->req.max_perf = max_perf; + data->req.des_perf = des_perf; + data->req.epp = epp; + +if ( prev == data->req.raw ) +return ; + +on_selected_cpus (cpumask_of (cpu), amd_cppc_write_request_msrs, data,1 ); +} + +static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, +unsigned int target_freq, +unsigned int relation) +{ +unsigned int cpu = policy->cpu; +const struct amd_cppc_drv_data *data =per_cpu (amd_cppc_drv_data, cpu); +uint8_t des_perf; +int res; + +if (unlikely (!target_freq) ) +return 0 ; + + res =amd_cppc_khz_to_perf (data, target_freq, &des_perf); +if ( res ) +return res; + +/* +* Having a performance level lower than the lowest nonlinear +* performance level, such as, lowest_perf <= perf <= lowest_nonliner_perf, +* may actually cause an efficiency penalty, So when deciding the min_perf +* value, we prefer lowest nonlinear performance over lowest performance. +*/ +amd_cppc_write_request (policy->cpu, data->caps.lowest_nonlinear_perf, + des_perf, data->caps.highest_perf, +/* Pre-defined BIOS value for passive mode */ +per_cpu (epp_init, policy->cpu)); +return 0 ; +} + +static void cf_check amd_cppc_init_msrs(void *info) +{ +struct cpufreq_policy *policy = info; +struct amd_cppc_drv_data *data =this_cpu (amd_cppc_drv_data); +uint64_t val; +unsigned int min_freq =0 , nominal_freq =0 , max_freq; + +/* Package level MSR */ +rdmsrl (MSR_AMD_CPPC_ENABLE , val); +/* +* Only when Enable bit is on, the hardware will calculate the processor’s +* performance capabilities and initialize the performance level fields in +* the CPPC capability registers. +*/ +if ( !(val &AMD_CPPC_ENABLE ) ) + { + val |=AMD_CPPC_ENABLE ; +wrmsrl (MSR_AMD_CPPC_ENABLE , val); + } + +rdmsrl (MSR_AMD_CPPC_CAP1 , data->caps.raw); + +if ( data->caps.highest_perf ==0 || data->caps.lowest_perf ==0 || + data->caps.nominal_perf ==0 || data->caps.lowest_nonlinear_perf ==0 || + data->caps.lowest_perf > data->caps.lowest_nonlinear_perf || + data->caps.lowest_nonlinear_perf > data->caps.nominal_perf || + data->caps.nominal_perf > data->caps.highest_perf ) + { +amd_cppc_err (policy->cpu, +"Out of range values: highest(%u), lowest(%u), nominal(%u), lowest_nonlinear(%u)\n" , + data->caps.highest_perf, data->caps.lowest_perf, + data->caps.nominal_perf, data->caps.lowest_nonlinear_perf); + goto err; + } + +amd_process_freq (&cpu_data[policy->cpu], +NULL ,NULL , &this_cpu (pxfreq_mhz)); + + data->err =amd_get_cpc_freq (data, data->cppc_data->cpc.lowest_mhz, + data->caps.lowest_perf, &min_freq); +if ( data->err ) +return ; + + data->err =amd_get_cpc_freq (data, data->cppc_data->cpc.nominal_mhz, + data->caps.nominal_perf, &nominal_freq); +if ( data->err ) +return ; + + data->err =amd_get_max_freq (data, &max_freq); +if ( data->err ) +return ; + +if ( min_freq > nominal_freq || nominal_freq > max_freq ) + { +amd_cppc_err (policy->cpu, +"min(%u), or max(%u), or nominal(%u) freq value is incorrect\n" , + min_freq, max_freq, nominal_freq); + goto err; + } + + 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; +/* +* Set after policy->cpuinfo.perf_freq, as we are taking +* APERF/MPERF average frequency as current frequency. +*/ + policy->cur =cpufreq_driver_getavg (policy->cpu,GOV_GETAVG ); + +/* Store pre-defined BIOS value for passive mode */ +rdmsrl (MSR_AMD_CPPC_REQ , val); +this_cpu (epp_init) =MASK_EXTR (val,AMD_CPPC_EPP_MASK ); + +return ; + + err: +/* +* No fallback shceme is available here, see more explanation at call +* site in amd_cppc_cpufreq_cpu_init(). +*/ + data->err = -EINVAL ; +} + +/* +* AMD CPPC 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_cppc_boost_init (struct cpufreq_policy *policy, +const struct amd_cppc_drv_data *data) +{ +if ( data->caps.highest_perf <= data->caps.nominal_perf ) +return ; + + policy->turbo =CPUFREQ_TURBO_ENABLED ; +} + +static int cf_check amd_cppc_cpufreq_cpu_exit(struct cpufreq_policy *policy) +{ +XVFREE (per_cpu (amd_cppc_drv_data, policy->cpu)); + +return 0 ; +} + +static int cf_check amd_cppc_cpufreq_cpu_init(struct cpufreq_policy *policy) +{ +unsigned int cpu = policy->cpu; +struct amd_cppc_drv_data *data; + + data = xvzalloc(struct amd_cppc_drv_data); +if ( !data ) +return -ENOMEM ; + + data->cppc_data = &processor_pminfo[cpu]->cppc_data; + +per_cpu (amd_cppc_drv_data, cpu) = data; + +on_selected_cpus (cpumask_of (cpu), amd_cppc_init_msrs, policy,1 ); + +/* +* The enable bit is sticky, as we need to enable it at the very first +* begining, before CPPC capability values sanity check. +* If error path is taken effective, not only amd-cppc cpufreq core fails +* to initialize, but also we could not fall back to legacy P-states +* driver, irrespective of the command line specifying a fallback option. +*/ +if ( data->err ) + { +amd_cppc_err (cpu,"Could not initialize cpufreq core in CPPC mode\n" ); +amd_cppc_cpufreq_cpu_exit (policy); +return data->err; + } + + policy->governor = cpufreq_opt_governor ? :CPUFREQ_DEFAULT_GOVERNOR ; + +amd_cppc_boost_init (policy, data); + +amd_cppc_verbose (policy->cpu, +"CPU initialized with amd-cppc passive mode\n" ); + +return 0 ; +} + +static const struct cpufreq_driver __initconst_cf_clobber +amd_cppc_cpufreq_driver = +{ + .name =XEN_AMD_CPPC_DRIVER_NAME , + .verify = amd_cppc_cpufreq_verify, + .target = amd_cppc_cpufreq_target, + .init = amd_cppc_cpufreq_cpu_init, + .exit = amd_cppc_cpufreq_cpu_exit, +}; +int __init amd_cppc_register_driver (void ) {if ( !cpu_has_cppc )return -ENODEV ; -return -EOPNOTSUPP ; +return cpufreq_register_driver (&amd_cppc_cpufreq_driver); }
xen/arch/x86/cpu/amd.c +4 -4
@@ -613,10 +613,10 @@ static unsigned int attr_const amd_parse_freq(unsigned int family,return freq; } -static void amd_process_freq (const struct cpuinfo_x86 *c, -unsigned int *low_mhz, -unsigned int *nom_mhz, -unsigned int *hi_mhz) +void amd_process_freq (const struct cpuinfo_x86 *c, +unsigned int *low_mhz, +unsigned int *nom_mhz, +unsigned int *hi_mhz) {unsigned int idx =0 , h;uint64_t hi, lo, val;
xen/arch/x86/include/asm/amd.h +2 -0
@@ -173,5 +173,7 @@ extern bool amd_virt_spec_ctrl;bool amd_setup_legacy_ssbd (void );void amd_set_legacy_ssbd (bool enable);void amd_set_cpuid_user_dis (bool enable); +void amd_process_freq (const struct cpuinfo_x86 *c,unsigned int *low_mhz, +unsigned int *nom_mhz,unsigned int *hi_mhz);#endif /* __AMD_H__ */
xen/arch/x86/include/asm/msr-index.h +6 -0
@@ -241,6 +241,12 @@#define MSR_AMD_CSTATE_CFG 0xc0010296U +#define MSR_AMD_CPPC_CAP1 0xc00102b0U +#define MSR_AMD_CPPC_ENABLE 0xc00102b1U +#define AMD_CPPC_ENABLE (_AC(1, ULL) << 0) +#define MSR_AMD_CPPC_REQ 0xc00102b3U +#define AMD_CPPC_EPP_MASK (_AC(0xff, ULL) << 24) +/* * Legacy MSR constants in need of cleanup. No new MSRs below this comment. */
xen/include/public/sysctl.h +1 -0
@@ -453,6 +453,7 @@ struct xen_set_cppc_para {uint32_t activity_window; }; +#define XEN_AMD_CPPC_DRIVER_NAME "amd-cppc"#define XEN_HWP_DRIVER_NAME "hwp"/*
Signature · 1 line
2.34.1
Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
Review
Quoted 6 lines · Penny Zheng
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) +{ + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, cpu);
I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear that in principle the CPU denoted by policy->cpu can be offline. Hence its per-CPU data is also unavailable. See cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.
Since initially I was thinking of using per-CPU data also in my patch, I'm reproducing this in raw form below, for your reference. It's generally only 4.22 material now, of course. Yet in turn for your driver the new drv_data field may want to become a union, with an "acpi" and a "cppc" sub-struct. And possibly a "hwp" one: Jason, looks like the HWP driver has a similar issue (unless again something guarantees that no two CPUs / threads will be in the same domain).
Jan
cpufreq: eliminate cpufreq_drv_data[]
Possibly many slots of it may be unused (all of them when the HWP or CPPC drivers are in use), as it's always strictly associated with the CPU recorded in a policy (irrespective of that CPU intermediately being taken offline). It is shared by all CPUs sharing a policy. We could therefore put the respective pointers in struct cpufreq_policy, but even that level of indirection is pointless. Embed the driver data structure directly in the policy one.
xen/arch/x86/acpi/cpufreq/acpi.c +18 -42
@@ -174,17 +174,18 @@ static u32 get_cur_val(const cpumask_t *return 0 ; policy =per_cpu (cpufreq_cpu_policy, cpu); -if (!policy || !cpufreq_drv_data[policy->cpu]) +if ( !policy )return 0 ; -switch (cpufreq_drv_data[policy->cpu]->arch_cpu_flags) { +switch ( policy->drv_data.arch_cpu_flags ) + {case SYSTEM_INTEL_MSR_CAPABLE : cmd.type =SYSTEM_INTEL_MSR_CAPABLE ; cmd.addr.msr.reg =MSR_IA32_PERF_STATUS ;break ;case SYSTEM_IO_CAPABLE : cmd.type =SYSTEM_IO_CAPABLE ; - perf = cpufreq_drv_data[policy->cpu]->acpi_data; + perf = policy->drv_data.acpi_data; cmd.addr.io.port = perf->control_register.address; cmd.addr.io.bit_width = perf->control_register.bit_width;break ;
@@ -210,9 +211,8 @@ static unsigned int cf_check get_cur_freif (!policy)return 0 ; - data = cpufreq_drv_data[policy->cpu]; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) + data = &policy->drv_data; +if ( !data->acpi_data || !data->freq_table )return 0 ;return extract_freq (get_cur_val (cpumask_of (cpu)), data);
@@ -255,7 +255,7 @@ static int cf_check acpi_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { - struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; + struct acpi_cpufreq_data *data = &policy->drv_data;struct processor_performance *perf;struct cpufreq_freqs freqs;cpumask_t online_policy_cpus;
@@ -265,10 +265,8 @@ static int cf_check acpi_cpufreq_target(unsigned int j;int result =0 ; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) { +if ( !data->acpi_data || !data->freq_table )return -ENODEV ; - }if (policy->turbo ==CPUFREQ_TURBO_DISABLED )if (target_freq > policy->cpuinfo.second_max_freq)
@@ -334,11 +332,9 @@ static int cf_check acpi_cpufreq_target( static int cf_check acpi_cpufreq_verify(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data;struct processor_performance *perf; -if (!policy || !(data = cpufreq_drv_data[policy->cpu]) || - !processor_pminfo[policy->cpu]) +if ( !policy || !processor_pminfo[policy->cpu] )return -EINVAL ; perf = &processor_pminfo[policy->cpu]->perf;
@@ -346,7 +342,7 @@ static int cf_check acpi_cpufreq_verify(cpufreq_verify_within_limits (policy,0 , perf->states[perf->platform_limit].core_frequency *1000 ); -return cpufreq_frequency_table_verify (policy, data->freq_table); +return cpufreq_frequency_table_verify (policy, policy->drv_data.freq_table); } static unsigned long
@@ -382,17 +378,11 @@ static int cf_check acpi_cpufreq_cpu_iniunsigned int i;unsigned int valid_states =0 ;unsigned int cpu = policy->cpu; -struct acpi_cpufreq_data *data; +struct acpi_cpufreq_data *data = &policy->drv_data;unsigned int result =0 ;struct cpuinfo_x86 *c = &cpu_data[policy->cpu];struct processor_performance *perf; - data = xzalloc(struct acpi_cpufreq_data); -if (!data) -return -ENOMEM ; - - cpufreq_drv_data[cpu] = data; - data->acpi_data = &processor_pminfo[cpu]->perf; perf = data->acpi_data;
@@ -409,23 +399,18 @@ static int cf_check acpi_cpufreq_cpu_iniif (cpufreq_verbose)printk ("xen_pminfo: @acpi_cpufreq_cpu_init," "HARDWARE addr space\n" ); -if (!cpu_has (c,X86_FEATURE_EIST )) { - result = -ENODEV ; - goto err_unreg; - } +if ( !cpu_has (c,X86_FEATURE_EIST ) ) +return -ENODEV ; data->arch_cpu_flags =SYSTEM_INTEL_MSR_CAPABLE ;break ;default : - result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } data->freq_table = xmalloc_array(struct cpufreq_frequency_table, (perf->state_count+1 )); -if (!data->freq_table) { - result = -ENOMEM ; - goto err_unreg; - } +if ( !data->freq_table ) +return -ENOMEM ;/* detect transition latency */ policy->cpuinfo.transition_latency =0 ;
@@ -483,23 +468,14 @@ static int cf_check acpi_cpufreq_cpu_inireturn result; err_freqfree: -xfree (data->freq_table); -err_unreg: -xfree (data); - cpufreq_drv_data[cpu] =NULL ; +XFREE (data->freq_table);return result; } static int cf_check acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; - -if (data) { - cpufreq_drv_data[policy->cpu] =NULL ; -xfree (data->freq_table); -xfree (data); - } +XFREE (policy->drv_data.freq_table);return 0 ; }
xen/arch/x86/acpi/cpufreq/cpufreq.c +0 -2
@@ -35,8 +35,6 @@#include <acpi/cpufreq/cpufreq.h> -struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; - struct perf_pair { union { struct {
xen/arch/x86/acpi/cpufreq/powernow.c +13 -38
117 lines not on this page · raw message
xen/include/acpi/cpufreq/cpufreq.h +2 -2
@@ -37,8 +37,6 @@ struct acpi_cpufreq_data {unsigned int arch_cpu_flags; }; -extern struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; -struct cpufreq_cpuinfo {unsigned int max_freq;unsigned int second_max_freq;/* P1 if Turbo Mode is on */
@@ -80,6 +78,8 @@ struct cpufreq_policy {int8_t turbo;/* tristate flag: 0 for unsupported * -1 for disable, 1 for enabled * See CPUFREQ_TURBO_* below for defines */ + +struct acpi_cpufreq_data drv_data; }; DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
Quoted 16 lines · Jan Beulich
Review
Quoted 6 lines · Penny Zheng
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) +{ + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable. See cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.
Sorry, I'm confused by your use of "domain" here. Do you mean a per_cpu(..., policy->cpu) access racing with a cpu offline? I'm not away of anything preventing that, though I'm not particularly familiar with it.
do_pm_op() has:
if ( op->cpuid >= nr_cpu_ids || !cpu_online(op->cpuid) )
return -EINVAL;
pmpt = processor_pminfo[op->cpuid];and do_get_pm_info() has:
if ( !op || (op->cpuid >= nr_cpu_ids) || !cpu_online(op->cpuid) )
return -EINVAL;
pmpt = processor_pminfo[op->cpuid];But those are only at entry.
Regards,
Jason
Quoted 333 lines
Since initially I was thinking of using per-CPU data also in my patch, I'm reproducing this in raw form below, for your reference. It's generally only 4.22 material now, of course. Yet in turn for your driver the new
drv_datafield may want to become a union, with an "acpi" and a "cppc" sub-struct. And possibly a "hwp" one: Jason, looks like the HWP driver has a similar issue (unless again something guarantees that no two CPUs / threads will be in the same domain).Jan
cpufreq: eliminate cpufreq_drv_data[]
Possibly many slots of it may be unused (all of them when the HWP or CPPC drivers are in use), as it's always strictly associated with the CPU recorded in a policy (irrespective of that CPU intermediately being taken offline). It is shared by all CPUs sharing a policy. We could therefore put the respective pointers in struct
cpufreq_policy, but even that level of indirection is pointless. Embed the driver data structure directly in the policy one.Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/acpi/cpufreq/acpi.c +18
-42@@ -174,17 +174,18 @@ static u32 get_cur_val(const cpumask_t *return 0 ; policy =per_cpu (cpufreq_cpu_policy, cpu); -if (!policy || !cpufreq_drv_data[policy->cpu]) +if ( !policy )return 0 ; -switch (cpufreq_drv_data[policy->cpu]->arch_cpu_flags) { +switch ( policy->drv_data.arch_cpu_flags ) + {case SYSTEM_INTEL_MSR_CAPABLE : cmd.type =SYSTEM_INTEL_MSR_CAPABLE ; cmd.addr.msr.reg =MSR_IA32_PERF_STATUS ;break ;case SYSTEM_IO_CAPABLE : cmd.type =SYSTEM_IO_CAPABLE ; - perf = cpufreq_drv_data[policy->cpu]->acpi_data; + perf = policy->drv_data.acpi_data; cmd.addr.io.port = perf->control_register.address; cmd.addr.io.bit_width = perf->control_register.bit_width;break ;@@ -210,9 +211,8 @@ static unsigned int cf_check get_cur_freif (!policy)return 0 ; - data = cpufreq_drv_data[policy->cpu]; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) + data = &policy->drv_data; +if ( !data->acpi_data || !data->freq_table )return 0 ;return extract_freq (get_cur_val (cpumask_of (cpu)), data);@@ -255,7 +255,7 @@ static int cf_check acpi_cpufreq_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { - struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; + struct acpi_cpufreq_data *data = &policy->drv_data;struct processor_performance *perf;struct cpufreq_freqs freqs;cpumask_t online_policy_cpus;@@ -265,10 +265,8 @@ static int cf_check acpi_cpufreq_target(unsigned int j;int result =0 ; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) { +if ( !data->acpi_data || !data->freq_table )return -ENODEV ; - }if (policy->turbo ==CPUFREQ_TURBO_DISABLED )if (target_freq > policy->cpuinfo.second_max_freq)@@ -334,11 +332,9 @@ static int cf_check acpi_cpufreq_target( static int cf_check acpi_cpufreq_verify(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data;struct processor_performance *perf; -if (!policy || !(data = cpufreq_drv_data[policy->cpu]) || - !processor_pminfo[policy->cpu]) +if ( !policy || !processor_pminfo[policy->cpu] )return -EINVAL ; perf = &processor_pminfo[policy->cpu]->perf;@@ -346,7 +342,7 @@ static int cf_check acpi_cpufreq_verify(cpufreq_verify_within_limits (policy,0 , perf->states[perf->platform_limit].core_frequency *1000 ); -return cpufreq_frequency_table_verify (policy, data->freq_table); +return cpufreq_frequency_table_verify (policy, policy->drv_data.freq_table); } static unsigned long@@ -382,17 +378,11 @@ static int cf_check acpi_cpufreq_cpu_iniunsigned int i;unsigned int valid_states =0 ;unsigned int cpu = policy->cpu; -struct acpi_cpufreq_data *data; +struct acpi_cpufreq_data *data = &policy->drv_data;unsigned int result =0 ;struct cpuinfo_x86 *c = &cpu_data[policy->cpu];struct processor_performance *perf; - data = xzalloc(struct acpi_cpufreq_data); -if (!data) -return -ENOMEM ; - - cpufreq_drv_data[cpu] = data; - data->acpi_data = &processor_pminfo[cpu]->perf; perf = data->acpi_data;@@ -409,23 +399,18 @@ static int cf_check acpi_cpufreq_cpu_iniif (cpufreq_verbose)printk ("xen_pminfo: @acpi_cpufreq_cpu_init," "HARDWARE addr space\n" ); -if (!cpu_has (c,X86_FEATURE_EIST )) { - result = -ENODEV ; - goto err_unreg; - } +if ( !cpu_has (c,X86_FEATURE_EIST ) ) +return -ENODEV ; data->arch_cpu_flags =SYSTEM_INTEL_MSR_CAPABLE ;break ;default : - result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } data->freq_table = xmalloc_array(struct cpufreq_frequency_table, (perf->state_count+1 )); -if (!data->freq_table) { - result = -ENOMEM ; - goto err_unreg; - } +if ( !data->freq_table ) +return -ENOMEM ;/* detect transition latency */ policy->cpuinfo.transition_latency =0 ;@@ -483,23 +468,14 @@ static int cf_check acpi_cpufreq_cpu_inireturn result; err_freqfree: -xfree (data->freq_table); -err_unreg: -xfree (data); - cpufreq_drv_data[cpu] =NULL ; +XFREE (data->freq_table);return result; } static int cf_check acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; - -if (data) { - cpufreq_drv_data[policy->cpu] =NULL ; -xfree (data->freq_table); -xfree (data); - } +XFREE (policy->drv_data.freq_table);return 0 ; }xen/arch/x86/acpi/cpufreq/cpufreq.c +0
-2@@ -35,8 +35,6 @@#include <acpi/cpufreq/cpufreq.h> -struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; - struct perf_pair { union { struct {xen/arch/x86/acpi/cpufreq/powernow.c +13
-38@@ -84,16 +84,14 @@ static int cf_check powernow_cpufreq_tarstruct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { - struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; + struct acpi_cpufreq_data *data = &policy->drv_data;struct processor_performance *perf;unsigned int next_state;/* Index into freq_table */ unsigned int next_perf_state;/* Index into perf table */ int result; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) { +if ( !data->acpi_data || !data->freq_table )return -ENODEV ; - } perf = data->acpi_data; result = cpufreq_frequency_table_target(policy,@@ -185,11 +183,9 @@ static void cf_check get_cpu_data(void * static int cf_check powernow_cpufreq_verify(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data;struct processor_performance *perf; -if (!policy || !(data = cpufreq_drv_data[policy->cpu]) || - !processor_pminfo[policy->cpu]) +if ( !policy || !processor_pminfo[policy->cpu] )return -EINVAL ; perf = &processor_pminfo[policy->cpu]->perf;@@ -197,7 +193,7 @@ static int cf_check powernow_cpufreq_vercpufreq_verify_within_limits (policy,0 , perf->states[perf->platform_limit].core_frequency *1000 ); -return cpufreq_frequency_table_verify (policy, data->freq_table); +return cpufreq_frequency_table_verify (policy, policy->drv_data.freq_table); } static int cf_check powernow_cpufreq_cpu_init(struct cpufreq_policy *policy)@@ -205,18 +201,12 @@ static int cf_check powernow_cpufreq_cpuunsigned int i;unsigned int valid_states =0 ;unsigned int cpu = policy->cpu; -struct acpi_cpufreq_data *data; +struct acpi_cpufreq_data *data = &policy->drv_data;unsigned int result =0 ;struct processor_performance *perf;struct amd_cpu_data info;struct cpuinfo_x86 *c = &cpu_data[policy->cpu]; - data = xzalloc(struct acpi_cpufreq_data); -if (!data) -return -ENOMEM ; - - cpufreq_drv_data[cpu] = data; - data->acpi_data = &processor_pminfo[cpu]->perf; info.perf = perf = data->acpi_data;@@ -228,8 +218,7 @@ static int cf_check powernow_cpufreq_cpuif (cpumask_weight (policy->cpus) !=1 ) {printk (XENLOG_WARNING "Unsupported sharing type %d (%u CPUs)\n" , policy->shared_type,cpumask_weight (policy->cpus)); - result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } }else {cpumask_copy (policy->cpus,cpumask_of (cpu));@@ -238,21 +227,16 @@ static int cf_check powernow_cpufreq_cpu/* capability check */ if (perf->state_count <=1 ) {printk ("No P-States\n" ); - result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } -if (perf->control_register.space_id != perf->status_register.space_id) { - result = -ENODEV ; - goto err_unreg; - } +if ( perf->control_register.space_id != perf->status_register.space_id ) +return -ENODEV ; data->freq_table = xmalloc_array(struct cpufreq_frequency_table, (perf->state_count+1 )); -if (!data->freq_table) { - result = -ENOMEM ; - goto err_unreg; - } +if ( !data->freq_table ) +return -ENOMEM ;/* detect transition latency */ policy->cpuinfo.transition_latency =0 ;@@ -298,23 +282,14 @@ static int cf_check powernow_cpufreq_cpureturn result; err_freqfree: -xfree (data->freq_table); -err_unreg: -xfree (data); - cpufreq_drv_data[cpu] =NULL ; +XFREE (data->freq_table);return result; } static int cf_check powernow_cpufreq_cpu_exit(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; - -if (data) { - cpufreq_drv_data[policy->cpu] =NULL ; -xfree (data->freq_table); -xfree (data); - } +XFREE (policy->drv_data.freq_table);return 0 ; }xen/include/acpi/cpufreq/cpufreq.h +2
-2@@ -37,8 +37,6 @@ struct acpi_cpufreq_data {unsigned int arch_cpu_flags; }; -extern struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; -struct cpufreq_cpuinfo {unsigned int max_freq;unsigned int second_max_freq;/* P1 if Turbo Mode is on */ @@ -80,6 +78,8 @@ struct cpufreq_policy {int8_t turbo;/* tristate flag: 0 for unsupported * -1 for disable, 1 for enabled * See CPUFREQ_TURBO_* below for defines */ + +struct acpi_cpufreq_data drv_data; }; DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
Quoted 18 lines · Jason Andryuk
Quoted 16 lines · Jan Beulich
Review
Quoted 6 lines · Penny Zheng
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) +{ + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable. See cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.
Quoted 1 line
Sorry, I'm confused by your use of "domain" here.
I agree it's confusing, but that's the terminology used in cpufreq.c (see e.g. "struct cpufreq_dom" or "const struct xen_psd_package *domain_info").
Quoted 2 lines
Do you mean a
per_cpu(..., policy->cpu)access racing with a cpu offline?
Yes (I wouldn't call it "racing" though, as it's not a timing issue).
Quoted 11 lines
I'm not
away of anything preventing that, though I'm not particularly familiar with it.
do_pm_op()has:if ( op->cpuid >= nr_cpu_ids || !cpu_online(op->cpuid) ) return -EINVAL; pmpt = processor_pminfo[op->cpuid];
Quoted 6 lines
and
do_get_pm_info()has:if ( !op || (op->cpuid >= nr_cpu_ids) || !cpu_online(op->cpuid) ) return -EINVAL; pmpt = processor_pminfo[op->cpuid];But those are only at entry.
That's not accessing struct cpufreq_policy, though. Per-CPU accesses using policy->cpu are the problematic ones, as - from all I can tell - the CPU named there can have gone offline, with the policy surviving when some other CPU is also part of the same "domain".
As said in the reply to Penny, main question is whether the data controlling what a "domain" covers may be constrained in the HWP case, demanding that no two CPUs (threads) can be in the same "domain". Then adding merely a sanity check somewhere would suffice.
Jan
RE: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
In reply to Jan Beulich[Public]
Quoted 14 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 7:23 PM
To: Penny, Zheng <[email protected]>; Andryuk, Jason <[email protected]>
Cc: Huang, Ray <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Anthony PERARD <[email protected]>; Orzel, Michal <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]
Subject: Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive modeOn 28.08.2025 12:03, Penny Zheng wrote:
Quoted 17 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) { + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, +cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable.policy->See
cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.
Correct me if I understand you wrongly:
No, my env is always per pcpu per cpufreq domain. So it never occurred to me that cpus, other than the first one in domain, will never call .init(), and of course, no per_cpu(amd_cppc_drv_data) ever gets allocated then.
Quoted 4 lines
Since initially I was thinking of using per-CPU data also in my patch, I'm reproducing this in raw form below, for your reference. It's generally only 4.22 material now, of course. Yet in turn for your driver the new
drv_datafield may want to become a union, with an "acpi" and a "cppc" sub-struct.
How about I embed my new driver data " struct amd_cppc_drv_data * " into cpufreq policy, maybe pointer is enough? Later, maybe, all "cppc", "acpi" and "hwp" could constitute an union in policy.
Quoted 324 lines
And possibly a "hwp" one: Jason, looks like the HWP driver has a similar issue (unless again something guarantees that no two CPUs / threads will be in the same domain).
Jan
cpufreq: eliminate cpufreq_drv_data[]
Possibly many slots of it may be unused (all of them when the HWP or CPPC drivers are in use), as it's always strictly associated with the CPU recorded in a policy (irrespective of that CPU intermediately being taken offline). It is shared by all CPUs sharing a policy. We could therefore put the respective pointers in struct
cpufreq_policy, but even that level of indirection is pointless. Embed the driver data structure directly in the policy one.Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/acpi/cpufreq/acpi.c +6
-6@@ -174,17 +174,18 @@ static u32 get_cur_val(const cpumask_t *return 0 ; policy =per_cpu (cpufreq_cpu_policy, cpu); -if (!policy || !cpufreq_drv_data[policy->cpu]) +if ( !policy )return 0 ; -switch (cpufreq_drv_data[policy->cpu]->arch_cpu_flags) { +switch ( policy->drv_data.arch_cpu_flags ) + {case SYSTEM_INTEL_MSR_CAPABLE : cmd.type =SYSTEM_INTEL_MSR_CAPABLE ; cmd.addr.msr.reg =MSR_IA32_PERF_STATUS ;break ;case SYSTEM_IO_CAPABLE : cmd.type =SYSTEM_IO_CAPABLE ; - perf = cpufreq_drv_data[policy->cpu]->acpi_data; + perf = policy->drv_data.acpi_data; cmd.addr.io.port = perf->control_register.address; cmd.addr.io.bit_width = perf->control_register.bit_width;break ;@@ -210,9 +211,8 @@ static unsigned int cf_check get_cur_freif (!policy)return 0 ; - data = cpufreq_drv_data[policy->cpu]; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) + data = &policy->drv_data; +if ( !data->acpi_data || !data->freq_table )return 0 ; return extract_freq(get_cur_val(cpumask_of(cpu)), data); @@ -255,7 +255,7@@ static int
cf_checkacpi_cpufreq_target(
structcpufreq_policy*policy,
unsigned inttarget_freq, unsigned int relation) {xen/arch/x86/acpi/cpufreq/acpi.c +2
-4-struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; +struct acpi_cpufreq_data *data = &policy->drv_data;struct processor_performance *perf;struct cpufreq_freqs freqs;cpumask_t online_policy_cpus;@@ -265,10 +265,8 @@ static int cf_check acpi_cpufreq_target(unsigned int j;int result =0 ; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) { +if ( !data->acpi_data || !data->freq_table )return -ENODEV ; - }if (policy->turbo ==CPUFREQ_TURBO_DISABLED ) if (target_freq > policy->cpuinfo.second_max_freq) @@ -334,11 +332,9@@ static int
cf_checkacpi_cpufreq_target(static int
cf_checkacpi_cpufreq_verify(struct cpufreq_policy *policy){xen/arch/x86/acpi/cpufreq/acpi.c +9
-21-struct acpi_cpufreq_data *data;struct processor_performance *perf; -if (!policy || !(data = cpufreq_drv_data[policy->cpu]) || - !processor_pminfo[policy->cpu]) +if ( !policy || !processor_pminfo[policy->cpu] )return -EINVAL ; perf = &processor_pminfo[policy->cpu]->perf;@@ -346,7 +342,7 @@ static int cf_check acpi_cpufreq_verify(cpufreq_verify_within_limits (policy,0 , perf->states[perf->platform_limit].core_frequency *1000 ); -return cpufreq_frequency_table_verify (policy, data->freq_table); +return cpufreq_frequency_table_verify (policy, + policy->drv_data.freq_table); } static unsigned long@@ -382,17 +378,11 @@ static int cf_check acpi_cpufreq_cpu_iniunsigned int i;unsigned int valid_states =0 ;unsigned int cpu = policy->cpu; -struct acpi_cpufreq_data *data; +struct acpi_cpufreq_data *data = &policy->drv_data;unsigned int result =0 ;struct cpuinfo_x86 *c = &cpu_data[policy->cpu];struct processor_performance *perf; - data = xzalloc(struct acpi_cpufreq_data); -if (!data) -return -ENOMEM ; - - cpufreq_drv_data[cpu] = data; - data->acpi_data = &processor_pminfo[cpu]->perf; perf = data->acpi_data;@@ -409,23 +399,18 @@ static int cf_check acpi_cpufreq_cpu_iniif (cpufreq_verbose)printk ("xen_pminfo: @acpi_cpufreq_cpu_init," "HARDWARE addr space\n" ); -if (!cpu_has (c,X86_FEATURE_EIST )) { - result = -ENODEV ; - goto err_unreg; - } +if ( !cpu_has (c,X86_FEATURE_EIST ) ) +return -ENODEV ; data->arch_cpu_flags =SYSTEM_INTEL_MSR_CAPABLE ;break ;default : - result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } data->freq_table = xmalloc_array(struct cpufreq_frequency_table, (perf->state_count+1 )); -if (!data->freq_table) { - result = -ENOMEM ; - goto err_unreg; - } +if ( !data->freq_table ) +return -ENOMEM ;/* detect transition latency */ policy->cpuinfo.transition_latency = 0; @@ -483,23 +468,14 @@ static int
cf_checkacpi_cpufreq_cpu_inireturn result;
err_freqfree:xen/arch/x86/acpi/cpufreq/acpi.c +2
-11-xfree (data->freq_table); -err_unreg: -xfree (data); - cpufreq_drv_data[cpu] =NULL ; +XFREE (data->freq_table);return result; } static int cf_check acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; - -if (data) { - cpufreq_drv_data[policy->cpu] =NULL ; -xfree (data->freq_table); -xfree (data); - } +XFREE (policy->drv_data.freq_table);return 0 ; }xen/arch/x86/acpi/cpufreq/cpufreq.c +0
-2@@ -35,8 +35,6 @@#include <acpi/cpufreq/cpufreq.h> -struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; - struct perf_pair { union { struct {xen/arch/x86/acpi/cpufreq/powernow.c +6
-15@@ -84,16 +84,14 @@ static int cf_check powernow_cpufreq_tarstruct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation) { - struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; + struct acpi_cpufreq_data *data = &policy->drv_data;struct processor_performance *perf;unsigned int next_state;/* Index into freq_table */ unsigned int next_perf_state;/* Index into perf table */ int result; -if (unlikely (data ==NULL || - data->acpi_data ==NULL || data->freq_table ==NULL )) { +if ( !data->acpi_data || !data->freq_table )return -ENODEV ; - } perf = data->acpi_data; result = cpufreq_frequency_table_target(policy,@@ -185,11 +183,9 @@ static void cf_check get_cpu_data(void * static int cf_check powernow_cpufreq_verify(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data;struct processor_performance *perf; -if (!policy || !(data = cpufreq_drv_data[policy->cpu]) || - !processor_pminfo[policy->cpu]) +if ( !policy || !processor_pminfo[policy->cpu] )return -EINVAL ; perf = &processor_pminfo[policy->cpu]->perf;@@ -197,7 +193,7 @@ static int cf_check powernow_cpufreq_vercpufreq_verify_within_limits (policy,0 , perf->states[perf->platform_limit].core_frequency *1000 ); -return cpufreq_frequency_table_verify (policy, data->freq_table); +return cpufreq_frequency_table_verify (policy, + policy->drv_data.freq_table); } static int cf_check powernow_cpufreq_cpu_init(struct cpufreq_policy *policy)@@ -205,18 +201,12 @@ static int cf_check powernow_cpufreq_cpuunsigned int i;unsigned int valid_states =0 ;unsigned int cpu = policy->cpu; -struct acpi_cpufreq_data *data; +struct acpi_cpufreq_data *data = &policy->drv_data;unsigned int result =0 ;struct processor_performance *perf;struct amd_cpu_data info;struct cpuinfo_x86 *c = &cpu_data[policy->cpu]; - data = xzalloc(struct acpi_cpufreq_data); -if (!data) -return -ENOMEM ; - - cpufreq_drv_data[cpu] = data; - data->acpi_data = &processor_pminfo[cpu]->perf; info.perf = perf = data->acpi_data; @@ -228,8 +218,7 @@ static int cf_check
powernow_cpufreq_cpu
if (cpumask_weight(policy->cpus)!= 1) {
printk(XENLOG_WARNING "Unsupported sharing type %d (%u
CPUs)\n",policy->shared_type, cpumask_weight(policy->cpus));xen/arch/x86/acpi/cpufreq/powernow.c +1
-2- result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } }else { cpumask_copy(policy->cpus, cpumask_of(cpu)); @@ -238,21 +227,16@@ static int
cf_checkpowernow_cpufreq_cpu/* capability check */ if (perf->state_count <= 1) { printk("No P-States\n");xen/arch/x86/acpi/cpufreq/powernow.c +5
-10- result = -ENODEV ; - goto err_unreg; +return -ENODEV ; } -if (perf->control_register.space_id != perf->status_register.space_id) { - result = -ENODEV ; - goto err_unreg; - } +if ( perf->control_register.space_id != perf->status_register.space_id ) +return -ENODEV ; data->freq_table = xmalloc_array(struct cpufreq_frequency_table, (perf->state_count+1 )); -if (!data->freq_table) { - result = -ENOMEM ; - goto err_unreg; - } +if ( !data->freq_table ) +return -ENOMEM ;/* detect transition latency */ policy->cpuinfo.transition_latency = 0; @@ -298,23 +282,14 @@ static int
cf_checkpowernow_cpufreq_cpureturn result;
err_freqfree:xen/arch/x86/acpi/cpufreq/powernow.c +2
-11-xfree (data->freq_table); -err_unreg: -xfree (data); - cpufreq_drv_data[cpu] =NULL ; +XFREE (data->freq_table);return result; } static int cf_check powernow_cpufreq_cpu_exit(struct cpufreq_policy *policy) { -struct acpi_cpufreq_data *data = cpufreq_drv_data[policy->cpu]; - -if (data) { - cpufreq_drv_data[policy->cpu] =NULL ; -xfree (data->freq_table); -xfree (data); - } +XFREE (policy->drv_data.freq_table);return 0 ; }xen/include/acpi/cpufreq/cpufreq.h +2
-2@@ -37,8 +37,6 @@ struct acpi_cpufreq_data {unsigned int arch_cpu_flags; }; -extern struct acpi_cpufreq_data *cpufreq_drv_data[NR_CPUS ]; -struct cpufreq_cpuinfo {unsigned int max_freq;unsigned int second_max_freq;/* P1 if Turbo Mode is on */ @@ -80,6 +78,8 @@ struct cpufreq_policy {int8_t turbo;/* tristate flag: 0 for unsupported * -1 for disable, 1 for enabled * See CPUFREQ_TURBO_* below for defines */ + +struct acpi_cpufreq_data drv_data; }; DECLARE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_policy);
Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
Quoted 23 lines · Zheng
Quoted 5 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 7:23 PMOn 28.08.2025 12:03, Penny Zheng wrote:
Quoted 17 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) { + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = per_cpu(amd_cppc_drv_data, +cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable.policy->See
cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.
Quoted 2 lines
Correct me if I understand you wrongly:
No, my env is always per pcpu per cpufreq domain. So it never occurred to me that cpus, other than the first one in domain, will never call .init(), and of course, noper_cpu(amd_cppc_drv_data)ever gets allocated then.
Well, the question is how domains are organized when using the CPPC driver. Aiui that's still driven by data passed in by Dom0, so in turn the question is whether there are any constraints on what ACPI may surface. If there are, all that may be necessary is adding a check. If there aren't, ...
Quoted 7 lines
Quoted 4 lines
Since initially I was thinking of using per-CPU data also in my patch, I'm reproducing this in raw form below, for your reference. It's generally only 4.22 material now, of course. Yet in turn for your driver the new
drv_datafield may want to become a union, with an "acpi" and a "cppc" sub-struct.How about I embed my new driver data " struct
amd_cppc_drv_data* " into cpufreq policy, maybe pointer is enough? Later, maybe, all "cppc", "acpi" and "hwp" could constitute an union in policy.
... I'd prefer to go the union approach right away. Whether then to take my patch as a prereq is tbd; that largely depends on what (if anything) is needed on the HWP side. If HWP needs fixing, that wants to to come first, as it would want backporting.
Jan
RE: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
[Public]
Quoted 41 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Friday, August 29, 2025 2:12 PM
To: Penny, Zheng <[email protected]>
Cc: Huang, Ray <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Anthony PERARD <[email protected]>; Orzel, Michal <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Andryuk, Jason <[email protected]>
Subject: Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive modeQuoted 25 lines · Zheng
Quoted 15 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 7:23 PMReview
Quoted 6 lines · Penny Zheng
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) { + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = +per_cpu(amd_cppc_drv_data, cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear
Quoted 9 lines
that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable.policy->See
cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable
comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.Quoted 2 lines
Correct me if I understand you wrongly:
No, my env is always per pcpu per cpufreq domain. So it never occurred to me
Quoted 8 lines
that cpus, other than the first one in domain, will never call .init(), and of course, no
per_cpu(amd_cppc_drv_data)ever gets allocated then.Well, the question is how domains are organized when using the CPPC driver. Aiui that's still driven by data passed in by Dom0, so in turn the question is whether there are any constraints on what ACPI may surface. If there are, all that may be necessary is adding a check. If there aren't, ...
According to ACPI spec, _PSD controls both P-state or CPPC, so in my implementation of getting CPPC data passed by Dom0(set_cppc_pminfo()), I demand both entry exist, _PSD and _CPC.
```
if ( cppc_data->flags == (XEN_CPPC_PSD | XEN_CPPC_CPC) )
{
...
pm_info->init = XEN_CPPC_INIT;
ret = cpufreq_cpu_init(cpuid);
...
}
```Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Since initially I was thinking of using per-CPU data also in my patch, I'm reproducing this in raw form below, for your reference. It's generally only
4.22 material now, of course. Yet in turn for your driver the newdrv_datafield may want to become a union, with an "acpi" and a "cppc" sub-struct.
Quoted 2 lines
How about I embed my new driver data " struct
amd_cppc_drv_data* " intocpufreq policy, maybe pointer is enough?
Quoted 1 line
Later, maybe, all "cppc", "acpi" and "hwp" could constitute an union in policy.
... I'd prefer to go the union approach right away. Whether then to take my patch as a prereq is tbd; that largely depends on what (if anything) is needed on the HWP side. If HWP needs fixing, that wants to to come first, as it would want backporting.
Jan
Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive mode
Quoted 53 lines · Zheng
[Public]
Quoted 41 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Friday, August 29, 2025 2:12 PM
To: Penny, Zheng <[email protected]>
Cc: Huang, Ray <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Anthony PERARD <[email protected]>; Orzel, Michal <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Andryuk, Jason <[email protected]>
Subject: Re: [PATCH v8 3/8] xen/cpufreq: implement amd-cppc driver for CPPC in passive modeQuoted 25 lines · Zheng
Quoted 15 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 7:23 PMReview
Quoted 6 lines · Penny Zheng
(quoted diff) +6
-0+static int cf_check amd_cppc_cpufreq_target(struct cpufreq_policy *policy, + unsigned int target_freq, + unsigned int relation) { + unsigned int cpu = policy->cpu; + const struct amd_cppc_drv_data *data = +per_cpu(amd_cppc_drv_data, cpu);I fear there's a problem here that I so far overlooked. As it happens, just yesterday I made a patch to eliminate cpufreq_drv_data[] global. In the course of doing so it became clear
Quoted 9 lines
that in principle the CPU denoted by
policy->cpucan be offline. Hence its per-CPU data is also unavailable.policy->See
cpufreq_add_cpu()'s invocation of .init() and cpufreq_del_cpu()'s invocation of .exit(). Is there anything well-hidden (and likely lacking some suitable
comment) which guarantees that no two CPUs (threads) will be in the same domain? If not, I fear you simply can't use per-CPU data here.Quoted 2 lines
Correct me if I understand you wrongly:
No, my env is always per pcpu per cpufreq domain. So it never occurred to meQuoted 8 lines
that cpus, other than the first one in domain, will never call .init(), and of course, no
per_cpu(amd_cppc_drv_data)ever gets allocated then.Well, the question is how domains are organized when using the CPPC driver. Aiui that's still driven by data passed in by Dom0, so in turn the question is whether there are any constraints on what ACPI may surface. If there are, all that may be necessary is adding a check. If there aren't, ...
According to ACPI spec,
_PSDcontrols both P-state or CPPC, so in my implementation of getting CPPC data passed by Dom0(set_cppc_pminfo()), I demand both entry exist,_PSDand_CPC.
Quoted 9 lines
```
if ( cppc_data->flags == (XEN_CPPC_PSD | XEN_CPPC_CPC) )
{
...
pm_info->init = XEN_CPPC_INIT;
ret = cpufreq_cpu_init(cpuid);
...
}
```That's only about presence of the data though. My remark, otoh, was about its content. It could in principle be specified somewhere that no domain may cover more than a single CPU / thread. In the absence of such, the case needs handling correctly.
Jan
Quoted 43 lines · Penny Zheng
amd-cppc 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_cppcdriver has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-cppc" for passive mode and "amd-cppc-epp" for active mode.The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance tuning. While 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 in hardware will automatically 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-cppc is enabled on passive mode with a top-level
cpufreq=amd-cppcoption, while users add extraactiveflag to select active mode.With
cpufreq=amd-cppc,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 580000 KHz Avg freq 580000 KHz Avg freq 580000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```
Quoted 11 lines
Penny Zheng (8):
xen/cpufreq: introduce new sub-hypercall to propagate CPPC data xen/cpufreq: introduce "cpufreq=amd-cppc" xen cmdline and amd-cppc
driver
xen/cpufreq: implement amd-cppc driver for CPPC in passive mode xen/cpufreq: implement amd-cppc-epp driver for CPPC in active mode xen/cpufreq: get performance policy from governor set via xenpmtools/cpufreq: extract CPPC para from cpufreq para xen/cpufreq: bypass governor-related para for amd-cppc-epp xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPCxen_sysctl_pm_opfor amd-cppc
driver
I'm somewhat confused by the submission: Both my mailbox and
https://lists.xen.org/archives/html/xen-devel/2025-08/threads.html agree that cover letter and patch 1 were submitted twice, while patches 4 through 8 came through un-threaded. I can only assume that the duplicates are identical.
Jan
Quoted 72 lines · Penny Zheng
amd-cppc 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_cppcdriver has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-cppc" for passive mode and "amd-cppc-epp" for active mode.The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance tuning. While 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 in hardware will automatically 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-cppc is enabled on passive mode with a top-level
cpufreq=amd-cppcoption, while users add extraactiveflag to select active mode.With
cpufreq=amd-cppc,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 580000 KHz Avg freq 580000 KHz Avg freq 580000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```Penny Zheng (8):
xen/cpufreq: introduce new sub-hypercall to propagate CPPC data xen/cpufreq: introduce "cpufreq=amd-cppc" xen cmdline and amd-cppc
driver
xen/cpufreq: implement amd-cppc driver for CPPC in passive mode xen/cpufreq: implement amd-cppc-epp driver for CPPC in active mode xen/cpufreq: get performance policy from governor set via xenpmtools/cpufreq: extract CPPC para from cpufreq para xen/cpufreq: bypass governor-related para for amd-cppc-epp xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPCxen_sysctl_pm_opfor amd-cppc
driverdocs/misc/xen-command-line.pandoc | 14 +- tools/include/xenctrl.h | 3 +- tools/libs/ctrl/xc_pm.c | 25 +- tools/misc/xenpm.c | 94 ++- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-cppc.c | 766 ++++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 69 +- xen/arch/x86/cpu/amd.c | 8 +- xen/arch/x86/include/asm/amd.h | 2 + xen/arch/x86/include/asm/msr-index.h | 6 + xen/arch/x86/platform_hypercall.c | 19 + xen/arch/x86/x86_64/cpufreq.c | 19 + xen/arch/x86/x86_64/platform_hypercall.c | 3 + xen/drivers/acpi/pm-op.c | 68 +- xen/drivers/acpi/pmstat.c | 4 + xen/drivers/cpufreq/cpufreq.c | 137 +++- xen/drivers/cpufreq/utility.c | 15 +
Quoted 8 lines
xen/include/acpi/cpufreq/cpufreq.h | 40 +- xen/include/acpi/cpufreq/processor_perf.h | 14 +- xen/include/public/platform.h | 26 + xen/include/public/sysctl.h | 5 +- xen/include/xen/pmstat.h | 5 + xen/include/xlat.lst | 1 + 23 files changed, 1283 insertions(+), 61 deletions(-) create mode 100644 xen/arch/x86/acpi/cpufreq/amd-cppc.c
Oh, and - what is still missing is a CHANGELOG.md entry.
Jan
[Public]
Quoted 96 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Thursday, August 28, 2025 8:10 PM
To: Penny, Zheng <[email protected]>
Cc: Huang, Ray <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Anthony PERARD <[email protected]>; Orzel, Michal <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Juergen Gross <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v8 0/8] amd-cppc CPU Performance Scaling DriverQuoted 23 lines · Penny Zheng
amd-cppc 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_cppcdriver has 2 operation modes: autonomous (active) mode, and non-autonomous (passive) mode. We register different CPUFreq driver for different modes, "amd-cppc" for passive mode and "amd-cppc-epp" for active mode.The passive mode leverages common governors such as *ondemand*, *performance*, etc, to manage the performance tuning. While 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 in hardware will automatically 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 2 lines
amd-cppc is enabled on passive mode with a top-level
cpufreq=amd-cppcoption, while users add extraactiveflag to select activemode.
Quoted 18 lines
With
cpufreq=amd-cppc,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 580000 KHz Avg freq 580000 KHz Avg freq 580000 KHz Setting CPU in performance mode Sampling and Outputs: Avg freq 4640000 KHz Avg freq 4220000 KHz Avg freq 4640000 KHz ```Quoted 10 lines
Penny Zheng (8):
xen/cpufreq: introduce new sub-hypercall to propagate CPPC data xen/cpufreq: introduce "cpufreq=amd-cppc" xen cmdline and amd-cppc
driver
xen/cpufreq: implement amd-cppc driver for CPPC in passive mode xen/cpufreq: implement amd-cppc-epp driver for CPPC in active mode xen/cpufreq: get performance policy from governor set via xenpmtools/cpufreq: extract CPPC para from cpufreq para xen/cpufreq: bypass governor-related para for amd-cppc-epp xen/cpufreq: Adapt SET/GET_CPUFREQ_CPPCxen_sysctl_pm_opfor amd-cppc
Quoted 26 lines
driver
docs/misc/xen-command-line.pandoc | 14 +- tools/include/xenctrl.h | 3 +- tools/libs/ctrl/xc_pm.c | 25 +- tools/misc/xenpm.c | 94 ++- xen/arch/x86/acpi/cpufreq/Makefile | 1 + xen/arch/x86/acpi/cpufreq/amd-cppc.c | 766 ++++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/cpufreq.c | 69 +- xen/arch/x86/cpu/amd.c | 8 +- xen/arch/x86/include/asm/amd.h | 2 + xen/arch/x86/include/asm/msr-index.h | 6 + xen/arch/x86/platform_hypercall.c | 19 + xen/arch/x86/x86_64/cpufreq.c | 19 + xen/arch/x86/x86_64/platform_hypercall.c | 3 + xen/drivers/acpi/pm-op.c | 68 +- xen/drivers/acpi/pmstat.c | 4 + xen/drivers/cpufreq/cpufreq.c | 137 +++- xen/drivers/cpufreq/utility.c | 15 + xen/include/acpi/cpufreq/cpufreq.h | 40 +- xen/include/acpi/cpufreq/processor_perf.h | 14 +- xen/include/public/platform.h | 26 + xen/include/public/sysctl.h | 5 +- xen/include/xen/pmstat.h | 5 + xen/include/xlat.lst | 1 + 23 files changed, 1283 insertions(+), 61 deletions(-) create modeQuoted 1 line
100644
xen/arch/x86/acpi/cpufreq/amd-cppc.c
Quoted 1 line
Oh, and - what is still missing is a CHANGELOG.md entry.
Thx, understood.
Quoted 2 lines
Jan