mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vishal Badole <Vishal.Badole@amd.com>
To: <ray.huang@amd.com>, <mario.limonciello@amd.com>,
	<rafael@kernel.org>, <viresh.kumar@linaro.org>,
	<perry.yuan@amd.com>, <kprateek.nayak@amd.com>,
	<hansg@kernel.org>, <ilpo.jarvinen@linux.intel.com>,
	<platform-driver-x86@vger.kernel.org>, <linux-pm@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: Vishal Badole <Vishal.Badole@amd.com>
Subject: [PATCH v2 2/3] cpufreq: amd-pstate: Set asymmetric CPU capacity on hybrid platforms
Date: Wed, 25 Nov 2026 22:50:30 +0530	[thread overview]
Message-ID: <20261125172031.741740-3-Vishal.Badole@amd.com> (raw)
In-Reply-To: <20261125172031.741740-1-Vishal.Badole@amd.com>

AMD heterogeneous processors combine multiple core types (e.g.
performance and efficiency cores) whose maximum sustained performance
and frequency differ. On such platforms the Linux scheduler relies on
per-CPU capacity values to place and balance tasks across cores.

Today amd-pstate does not export any per-CPU capacity information. As a
result the scheduler sees every CPU as having identical capacity and
treats performance and efficiency cores as interchangeable. Tasks are
therefore placed and balanced without regard to core strength, which
hurts both throughput and energy efficiency on hybrid systems.

This happens because the scheduler defaults to a uniform
SCHED_CAPACITY_SCALE when no arch- or driver-supplied capacity exists,
and amd-pstate never feeds the asymmetry it knows (via CPPC highest
frequency or the effective highest performance register) into the arch
hybrid capacity scaling interface.

Enable capacity aware scheduling by scaling each CPU's capacity relative
to a reference CPU. Use the maximum attainable frequencies when available
for both CPUs, or their effective highest performance values otherwise.
If reference setup fails, keep CAS disabled and continue normal CPUFreq
registration.

Capacity aware scheduling is enabled by default on supported hardware and
can be turned off with the "amd_cas=disable" boot parameter. Its effective
state is exposed through the "cas" sysfs attribute, and also exported via
amd_pstate_cas_active() so amd-hfi can stand down when capacity aware
scheduling owns the heterogeneous cores.

Signed-off-by: Vishal Badole <Vishal.Badole@amd.com>
---
 MAINTAINERS                           |   1 +
 arch/x86/include/asm/amd/amd-pstate.h |  16 +++
 arch/x86/kernel/acpi/cppc.c           |   4 +-
 drivers/cpufreq/amd-pstate.c          | 150 +++++++++++++++++++++++++-
 4 files changed, 167 insertions(+), 4 deletions(-)
 create mode 100644 arch/x86/include/asm/amd/amd-pstate.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb2b80..300dce181c1c4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1322,6 +1322,7 @@ R:	K Prateek Nayak <kprateek.nayak@amd.com>
 L:	linux-pm@vger.kernel.org
 S:	Supported
 F:	Documentation/admin-guide/pm/amd-pstate.rst
+F:	arch/x86/include/asm/amd/amd-pstate.h
 F:	drivers/cpufreq/amd-pstate*
 F:	tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py
 
diff --git a/arch/x86/include/asm/amd/amd-pstate.h b/arch/x86/include/asm/amd/amd-pstate.h
new file mode 100644
index 0000000000000..1c0eb56c8f867
--- /dev/null
+++ b/arch/x86/include/asm/amd/amd-pstate.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Public interface for the AMD P-state driver.
+ */
+#ifndef _ASM_X86_AMD_PSTATE_H_
+#define _ASM_X86_AMD_PSTATE_H_
+
+#include <linux/types.h>
+
+#if IS_ENABLED(CONFIG_X86_AMD_PSTATE)
+bool amd_pstate_cas_active(void);
+#else
+static inline bool amd_pstate_cas_active(void) { return false; }
+#endif
+
+#endif /* _ASM_X86_AMD_PSTATE_H_ */
diff --git a/arch/x86/kernel/acpi/cppc.c b/arch/x86/kernel/acpi/cppc.c
index 1c6bf56c3215b..15590e9da2dca 100644
--- a/arch/x86/kernel/acpi/cppc.c
+++ b/arch/x86/kernel/acpi/cppc.c
@@ -248,8 +248,8 @@ int amd_get_effective_highest_perf(unsigned int cpu)
 	if (ret < 0)
 		return ret;
 
-	/* without preferred cores, return the highest perf register value */
-	if (!prefcore)
+	/* Reuse the cached value for homogeneous CPUs without preferred cores. */
+	if (!prefcore && !cpu_feature_enabled(X86_FEATURE_AMD_HTR_CORES))
 		return boost_numerator;
 
 	/*
diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 3e0308f644778..415553109fc26 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -39,10 +39,12 @@
 #include <linux/power_supply.h>
 #include <linux/static_call.h>
 #include <linux/topology.h>
+#include <linux/cpuset.h>
 
 #include <acpi/processor.h>
 #include <acpi/cppc_acpi.h>
 
+#include <asm/amd/amd-pstate.h>
 #include <asm/msr.h>
 #include <asm/cpufeature.h>
 #include <asm/cpu_device_id.h>
@@ -86,6 +88,10 @@ static struct cpufreq_driver amd_pstate_driver;
 static struct cpufreq_driver amd_pstate_epp_driver;
 static int cppc_state = AMD_PSTATE_UNDEFINED;
 static bool amd_pstate_prefcore = true;
+static bool amd_pstate_cas = IS_ENABLED(CONFIG_SMP);
+static bool amd_pstate_cas_initialized;
+static u32 amd_pstate_ref_freq_khz;
+static u32 amd_pstate_ref_highest_perf;
 static struct quirk_entry *quirks;
 
 /*
@@ -962,6 +968,117 @@ static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
 	sched_set_itmt_core_prio((int)READ_ONCE(cpudata->prefcore_ranking), cpudata->cpu);
 }
 
+bool amd_pstate_cas_active(void)
+{
+	return READ_ONCE(amd_pstate_cas_initialized);
+}
+EXPORT_SYMBOL_GPL(amd_pstate_cas_active);
+
+static void __init amd_pstate_init_cas(void)
+{
+	int ref_highest_perf = 0, ref_max_freq = 0;
+	int cpu, highest_perf, max_freq;
+
+	if (!amd_pstate_cas)
+		return;
+
+	amd_pstate_cas = false;
+	if (!cpu_feature_enabled(X86_FEATURE_AMD_HTR_CORES))
+		return;
+
+	for_each_online_cpu(cpu) {
+		highest_perf = amd_get_effective_highest_perf(cpu);
+		if (highest_perf <= 0) {
+			pr_warn("Invalid highest performance (%d) on CPU%d\n",
+				highest_perf, cpu);
+			return;
+		}
+
+		max_freq = amd_get_max_frequency(cpu);
+		if (max_freq < 0 || (u32)max_freq > U32_MAX / 1000) {
+			pr_warn("Invalid maximum frequency (%d) on CPU%d\n",
+				max_freq, cpu);
+			return;
+		}
+
+		if (highest_perf < ref_highest_perf ||
+		    (highest_perf == ref_highest_perf && max_freq <= ref_max_freq))
+			continue;
+
+		ref_highest_perf = highest_perf;
+		ref_max_freq = max_freq;
+	}
+
+	if (!ref_highest_perf) {
+		pr_warn("No reference CPU available for capacity scaling\n");
+		return;
+	}
+
+	amd_pstate_ref_highest_perf = ref_highest_perf;
+	amd_pstate_ref_freq_khz = ref_max_freq * 1000U;
+
+	amd_pstate_cas = arch_enable_hybrid_capacity_scale();
+}
+
+static void amd_pstate_update_cpu_capacity(struct amd_cpudata *data,
+					   bool highest_freq_available)
+{
+	unsigned long capacity, max_capacity, capacity_freq, base_freq;
+	int cpu = data->cpu;
+
+	lockdep_assert_held(&sched_domains_mutex);
+
+	if (highest_freq_available && amd_pstate_ref_freq_khz) {
+		capacity = data->max_freq;
+		max_capacity = amd_pstate_ref_freq_khz;
+		capacity_freq = capacity;
+		base_freq = max_capacity;
+	} else {
+		union perf_cached perf = READ_ONCE(data->perf);
+
+		capacity = perf.highest_perf;
+		max_capacity = amd_pstate_ref_highest_perf;
+		capacity_freq = perf_to_freq(perf, data->nominal_freq, perf.highest_perf);
+		base_freq = perf_to_freq(perf, data->nominal_freq, max_capacity);
+	}
+
+	arch_set_cpu_capacity(cpu, capacity, max_capacity, capacity_freq, base_freq);
+	topology_set_cpu_scale(cpu, arch_scale_cpu_capacity(cpu));
+}
+
+static void amd_pstate_set_cpu_capacity(struct amd_cpudata *cpudata,
+					bool highest_freq_available)
+{
+	union perf_cached perf = READ_ONCE(cpudata->perf);
+	int cpu;
+
+	lockdep_assert_cpus_held();
+
+	guard(mutex)(&sched_domains_mutex);
+
+	if (perf.highest_perf > amd_pstate_ref_highest_perf) {
+		amd_pstate_ref_highest_perf = perf.highest_perf;
+		amd_pstate_ref_freq_khz = highest_freq_available ? cpudata->max_freq : 0;
+
+		for_each_possible_cpu(cpu) {
+			struct cpufreq_policy *policy;
+
+			if (cpu == cpudata->cpu)
+				continue;
+
+			policy = cpufreq_cpu_policy(cpu);
+			if (policy && policy->driver_data)
+				amd_pstate_update_cpu_capacity(policy->driver_data,
+							       amd_pstate_ref_freq_khz &&
+							       amd_get_max_frequency(cpu) > 0);
+		}
+	}
+
+	amd_pstate_update_cpu_capacity(cpudata, highest_freq_available);
+	x86_topology_update = true;
+	WRITE_ONCE(amd_pstate_cas_initialized, true);
+}
+
 static void amd_pstate_update_limits(struct cpufreq_policy *policy)
 {
 	struct amd_cpudata *cpudata;
@@ -1036,6 +1153,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 	u32 min_freq, max_freq, nominal_freq, lowest_nonlinear_freq;
 	struct cppc_perf_caps cppc_perf;
 	union perf_cached perf;
+	bool highest_freq_available;
 	int ret;
 
 	ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
@@ -1062,6 +1180,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 
 	/* try to look up from BIOS/quirk first, fall back to (nominal_freq * highest_perf)/nominal_perf */
 	max_freq = amd_get_max_frequency(cpudata->cpu) * 1000;
+	highest_freq_available = !!max_freq;
 	if (!max_freq)
 		max_freq = perf_to_freq(perf, nominal_freq, perf.highest_perf);
 	WRITE_ONCE(cpudata->max_freq, max_freq);
@@ -1093,6 +1212,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 			 bios_min_freq, cpudata->cpu);
 	}
 
+	if (IS_ENABLED(CONFIG_SMP) && amd_pstate_cas)
+		amd_pstate_set_cpu_capacity(cpudata, highest_freq_available);
+
 	return 0;
 }
 
@@ -1203,6 +1325,7 @@ static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
 	freq_qos_remove_request(&cpudata->req[0]);
 	policy->fast_switch_possible = false;
 	kfree(cpudata);
+	policy->driver_data = NULL;
 }
 
 static int amd_pstate_get_balanced_epp(struct cpufreq_policy *policy)
@@ -1916,12 +2039,20 @@ static ssize_t prefcore_show(struct device *dev,
 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
 }
 
+static ssize_t cas_show(struct device *dev,
+			struct device_attribute *attr, char *buf)
+{
+	return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_cas_active()));
+}
+
 static DEVICE_ATTR_RW(status);
 static DEVICE_ATTR_RO(prefcore);
+static DEVICE_ATTR_RO(cas);
 
 static struct attribute *pstate_global_attributes[] = {
 	&dev_attr_status.attr,
 	&dev_attr_prefcore.attr,
+	&dev_attr_cas.attr,
 	NULL
 };
 
@@ -2431,8 +2562,10 @@ static int __init amd_pstate_init(void)
 			return ret;
 	}
 
-	if (cpu_feature_enabled(X86_FEATURE_AMD_WORKLOAD_CLASS) &&
-	    IS_ENABLED(CONFIG_AMD_HFI))
+	amd_pstate_init_cas();
+
+	if ((cpu_feature_enabled(X86_FEATURE_AMD_WORKLOAD_CLASS) &&
+	     IS_ENABLED(CONFIG_AMD_HFI)) || amd_pstate_cas)
 		amd_pstate_prefcore = false;
 
 	ret = amd_pstate_register_driver(cppc_state);
@@ -2441,6 +2574,10 @@ static int __init amd_pstate_init(void)
 		return ret;
 	}
 
+	/* Per-CPU capacities are set during registration; rebuild domains once. */
+	if (amd_pstate_cas)
+		rebuild_sched_domains();
+
 	dev_root = bus_get_dev_root(&cpu_subsys);
 	if (dev_root) {
 		ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
@@ -2481,8 +2618,17 @@ static int __init amd_prefcore_param(char *str)
 	return 0;
 }
 
+static int __init amd_cas_param(char *str)
+{
+	if (!strcmp(str, "disable"))
+		amd_pstate_cas = false;
+
+	return 0;
+}
+
 early_param("amd_pstate", amd_pstate_param);
 early_param("amd_prefcore", amd_prefcore_param);
+early_param("amd_cas", amd_cas_param);
 
 MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
-- 
2.34.1


  parent reply	other threads:[~2026-11-25 17:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 17:20 [PATCH v2 0/3] cpufreq: amd-pstate: Capacity Aware Scheduling " Vishal Badole
2026-11-25 17:20 ` [PATCH v2 1/3] cpufreq: amd-pstate: Finalize prefcore before registration Vishal Badole
2026-11-25 17:20 ` Vishal Badole [this message]
2026-11-25 17:20 ` [PATCH v2 3/3] platform/x86/amd: hfi: Stand down when amd-pstate CAS is active Vishal Badole

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125172031.741740-3-Vishal.Badole@amd.com \
    --to=vishal.badole@amd.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=perry.yuan@amd.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=ray.huang@amd.com \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®