Skip to contentSkip to conversation

Implement AMD Pstate EPP Driver

PATCH Superseded Perry Yuan 3 participants 23 messages Nov 10, 2022 – Dec 2, 2022

Subsystems (6)

v4 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Perry Yuan

author reviewer
#0 lore raw

v4 cover Implement AMD Pstate EPP Driver

Hi all,

This patchset implements one new AMD CPU frequency driver
"amd-pstate-epp” instance for better performance and power control.
CPPC has a parameter called energy preference performance (EPP).
The EPP is used in the CCLK DPM controller to drive the frequency that a core is going to operate during short periods of activity.
EPP values will be utilized for different OS profiles (balanced, performance, power savings).

AMD Energy Performance Preference (EPP) provides a hint to the hardware
if software wants to bias toward performance (0x0) or energy efficiency (0xff) The lowlevel power firmware will calculate the runtime frequency according to the EPP preference value. So the EPP hint will impact the CPU cores frequency responsiveness.

We use the RAPL interface with "perf" tool to get the energy data of the package power. Performance Per Watt (PPW) Calculation:

The PPW calculation is referred by below paper:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsoftware.intel.com%2Fcontent%2Fdam%2Fdevelop%2Fexternal%2Fus%2Fen%2Fdocuments%2Fperformance-per-what-paper.pdf&data=04%7C01%7CPerry.Yuan%40amd.com%7Cac66e8ce98044e9b062708d9ab47c8d8%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637729147708574423%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=TPOvCE%2Frbb0ptBreWNxHqOi9YnVhcHGKG88vviDLb00%3D&reserved=0

Below formula is referred from below spec to measure the PPW:

(F / t) / P = F * t / (t * E) = F / E,

"F" is the number of frames per second.
"P" is power measured in watts.
"E" is energy measured in joules.

Gitsouce Benchmark Data on ROME Server CPU

+------------------------------+------------------------------+------------+------------------+
| Kernel Module                | PPW (1 / s * J)              |Energy(J) | PPW Improvement (%)|
+==============================+==============================+============+==================+
| acpi-cpufreq:schedutil       | 5.85658E-05                  | 17074.8    | base             |
+------------------------------+------------------------------+------------+------------------+
| acpi-cpufreq:ondemand        | 5.03079E-05                  | 19877.6    | -14.10%          |
+------------------------------+------------------------------+------------+------------------+
| acpi-cpufreq:performance     | 5.88132E-05                  | 17003      | 0.42%            |
+------------------------------+------------------------------+------------+------------------+
| amd-pstate:ondemand          | 4.60295E-05                  | 21725.2    | -21.41%          |
+------------------------------+------------------------------+------------+------------------+
| amd-pstate:schedutil         | 4.70026E-05                  | 21275.4    | -19.7%           |
+------------------------------+------------------------------+------------+------------------+
| amd-pstate:performance       | 5.80094E-05                  | 17238.6    | -0.95%           |
+------------------------------+------------------------------+------------+------------------+
| EPP:performance              | 5.8292E-05                   | 17155      | -0.47%           |
+------------------------------+------------------------------+------------+------------------+
| EPP: balance performance:    | 6.71709E-05                  | 14887.4    | 14.69%           |
+------------------------------+------------------------------+------------+------------------+
| EPP:power                    | 6.66951E-05                  | 4993.6     | 13.88%           |
+------------------------------+------------------------------+------------+------------------+

Tbench Benchmark Data on ROME Server CPU

+---------------------------------------------+-------------------+--------------+-------------+------------------+
| Kernel Module                               | PPW MB / (s * J)  |Throughput(MB/s)| Energy (J)|PPW Improvement(%)|
+=============================================+===================+==============+=============+==================+
| acpi_cpufreq: schedutil                     | 46.39             | 17191        | 37057.3     | base             |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| acpi_cpufreq: ondemand                      | 51.51             | 19269.5      | 37406.5     | 11.04 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| acpi_cpufreq: performance                   | 45.96             | 17063.7      | 37123.7     | -0.74 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| EPP:powersave: performance(0)               | 54.46             | 20263.1      | 37205       | 17.87 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| EPP:powersave: balance performance          | 55.03             | 20481.9      | 37221.5     | 19.14 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| EPP:powersave: balance_power                | 54.43             | 20245.9      | 37194.2     | 17.77 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| EPP:powersave: power(255)                   | 54.26             | 20181.7      | 37197.4     | 17.40 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| amd-pstate: schedutil                       | 48.22             | 17844.9      | 37006.6     | 3.80 %           |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| amd-pstate: ondemand                        | 61.30             | 22988        | 37503.4     | 33.72 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+
| amd-pstate: performance                     | 54.52             | 20252.6      | 37147.8     | 17.81 %          |
+---------------------------------------------+-------------------+--------------+-------------+------------------+

changes from v3:
* add one more document update patch for the active and passive mode introducion.
* drive most of the feedbacks from Mario
* drive feedback from Rafael for the cppc_acpi driver.
* remove the epp raw data set/get function
* set the amd-pstate drive by passing kernel parameter
* set amd-pstate driver disabled by default if no kernel parameter input from booting
* get cppc_set_auto_epp and cppc_set_epp_perf combined
* pick up reviewed by flag from Mario

changes from v2:
* change pstate driver as builtin type from module
* drop patch "export cpufreq cpu release and acquire"
* squash patch of shared mem into single patch of epp implementation
* add one new patch to support frequency boost control
* add patch to expose driver working status checking
* rebase driver into v6.1-rc4 kernel release
* move some declaration to amd-pstate.h
* drive feedback from Mario for the online/offline patch
* drive feedback from Mario for the suspend/resume patch
* drive feedback from Ray for the cppc_acpi and some other patches
* drive feedback from Nathan for the epp patch

changes from v1:
* rebased to v6.0
* drive feedbacks from Mario for the suspend/resume patch
* drive feedbacks from Nathan for the EPP support on msr type
* fix some typos and code style indent problems
* update commit comments for patch 4/7
* change the epp_enabled module param name to epp
* set the default epp mode to be false
* add testing for the x86_energy_perf_policy utility patchset(will send that utility patchset with another thread)

v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

*** BLURB HERE ***

Perry Yuan (9):
ACPI: CPPC: Add AMD pstate energy performance preference cppc control
Documentation: amd-pstate: add EPP profiles introduction
cpufreq: amd-pstate: change amd-pstate driver to be built-in type
cpufreq: amd_pstate: implement Pstate EPP support for the AMD
processors
cpufreq: amd_pstate: implement amd pstate cpu online and offline
callback
cpufreq: amd-pstate: implement suspend and resume callbacks
cpufreq: amd-pstate: add frequency dynamic boost sysfs control
cpufreq: amd_pstate: add driver working mode status sysfs entry
Documentation: amd-pstate: add amd pstate driver mode introduction

 Documentation/admin-guide/pm/amd-pstate.rst |  66 +-
 drivers/acpi/cppc_acpi.c                    | 114 ++-
 drivers/cpufreq/Kconfig.x86                 |   2 +-
 drivers/cpufreq/amd-pstate.c                | 912 +++++++++++++++++++-
 include/acpi/cppc_acpi.h                    |  12 +
 include/linux/amd-pstate.h                  |  82 ++
 6 files changed, 1161 insertions(+), 27 deletions(-)
Signature · 1 line
2.34.1
author reviewer
#1 lore raw

PATCH v4 1/9 ACPI: CPPC: Add AMD pstate energy performance preference cppc control

Add support for setting and querying EPP preferences to the generic CPPC driver. This enables downstream drivers such as amd-pstate to discover and use these values

In order to get EPP worked, cppc_get_epp_caps() will query EPP preference value and cppc_set_epp_perf() will set EPP new value.
Before the EPP works, pstate driver will use cppc_set_auto_epp() to enable EPP function from firmware firstly.

---
 drivers/acpi/cppc_acpi.c | 114 +++++++++++++++++++++++++++++++++++++--
 include/acpi/cppc_acpi.h |  12 +++++
 2 files changed, 121 insertions(+), 5 deletions(-)
drivers/acpi/cppc_acpi.c +109 -5
index 093675b1a1ff..37fa75f25f62 100644
@@ -1093,6 +1093,9 @@ static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf)
 {
 	struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
 	struct cpc_register_resource *reg;
+	int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
+	struct cppc_pcc_data *pcc_ss_data = NULL;
+	int ret = -EINVAL;
 
 	if (!cpc_desc) {
 		pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
@@ -1102,10 +1105,6 @@ static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf)
 	reg = &cpc_desc->cpc_regs[reg_idx];
 
 	if (CPC_IN_PCC(reg)) {
-		int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
-		struct cppc_pcc_data *pcc_ss_data = NULL;
-		int ret = 0;
-
 		if (pcc_ss_id < 0)
 			return -EIO;
 
@@ -1125,7 +1124,7 @@ static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf)
 
 	cpc_read(cpunum, reg, perf);
 
-	return 0;
+	return ret;
 }
 
 /**
@@ -1365,6 +1364,111 @@ int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
 }
 EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
 
+/**
+ * cppc_get_epp_caps - Get the energy preference register value.
+ * @cpunum: CPU from which to get epp preference level.
+ * @perf_caps: Return address.
+ *
+ * Return: 0 for success, -EIO otherwise.
+ */
+int cppc_get_epp_caps(int cpunum, struct cppc_perf_caps *perf_caps)
+{
+	struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
+	struct cpc_register_resource *energy_perf_reg;
+	u64 energy_perf;
+
+	if (!cpc_desc) {
+		pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
+		return -ENODEV;
+	}
+
+	energy_perf_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
+
+	if (!CPC_SUPPORTED(energy_perf_reg))
+		pr_warn_once("energy perf reg update is unsupported!\n");
+
+	if (CPC_IN_PCC(energy_perf_reg)) {
+		int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
+		struct cppc_pcc_data *pcc_ss_data = NULL;
+		int ret = 0;
+
+		if (pcc_ss_id < 0)
+			return -ENODEV;
+
+		pcc_ss_data = pcc_data[pcc_ss_id];
+
+		down_write(&pcc_ss_data->pcc_lock);
+
+		if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0) {
+			cpc_read(cpunum, energy_perf_reg, &energy_perf);
+			perf_caps->energy_perf = energy_perf;
+		} else {
+			ret = -EIO;
+		}
+
+		up_write(&pcc_ss_data->pcc_lock);
+
+		return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cppc_get_epp_caps);
+
+/*
+ * Set Energy Performance Preference Register value through
+ * Performance Controls Interface
+ */
+int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable)
+{
+	int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
+	struct cpc_register_resource *epp_set_reg;
+	struct cpc_register_resource *auto_sel_reg;
+	struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
+	struct cppc_pcc_data *pcc_ss_data = NULL;
+	int ret = -EINVAL;
+
+	if (!cpc_desc) {
+		pr_debug("No CPC descriptor for CPU:%d\n", cpu);
+		return -ENODEV;
+	}
+
+	auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
+	epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
+
+	if (CPC_IN_PCC(epp_set_reg) || CPC_IN_PCC(auto_sel_reg)) {
+		if (pcc_ss_id < 0) {
+			pr_debug("Invalid pcc_ss_id\n");
+			return -ENODEV;
+		}
+
+		if (CPC_SUPPORTED(auto_sel_reg)) {
+			ret = cpc_write(cpu, auto_sel_reg, enable);
+			if (ret)
+				return ret;
+		}
+
+		if (CPC_SUPPORTED(epp_set_reg)) {
+			ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
+			if (ret)
+				return ret;
+		}
+
+		pcc_ss_data = pcc_data[pcc_ss_id];
+
+		down_write(&pcc_ss_data->pcc_lock);
+		/* after writing CPC, transfer the ownership of PCC to platform */
+		ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
+		up_write(&pcc_ss_data->pcc_lock);
+	} else {
+		ret = -ENOTSUPP;
+		pr_debug("_CPC in PCC is not supported\n");
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(cppc_set_epp_perf);
+
 /**
  * cppc_set_enable - Set to enable CPPC on the processor by writing the
  * Continuous Performance Control package EnableRegister field.
include/acpi/cppc_acpi.h +12 -0
index c5614444031f..a93393b05171 100644
@@ -108,12 +108,14 @@ struct cppc_perf_caps {
 	u32 lowest_nonlinear_perf;
 	u32 lowest_freq;
 	u32 nominal_freq;
+	u32 energy_perf;
 };
 
 struct cppc_perf_ctrls {
 	u32 max_perf;
 	u32 min_perf;
 	u32 desired_perf;
+	u32 energy_perf;
 };
 
 struct cppc_perf_fb_ctrs {
@@ -149,6 +151,8 @@ extern bool cpc_ffh_supported(void);
 extern bool cpc_supported_by_cpu(void);
 extern int cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val);
 extern int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val);
+extern int cppc_get_epp_caps(int cpunum, struct cppc_perf_caps *perf_caps);
+extern int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable);
 #else /* !CONFIG_ACPI_CPPC_LIB */
 static inline int cppc_get_desired_perf(int cpunum, u64 *desired_perf)
 {
@@ -202,6 +206,14 @@ static inline int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
 {
 	return -ENOTSUPP;
 }
+static inline int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, boot enable)
+{
+	return -ENOTSUPP;
+}
+static inline int cppc_get_epp_caps(int cpunum, struct cppc_perf_caps *perf_caps)
+{
+	return -ENOTSUPP;
+}
 #endif /* !CONFIG_ACPI_CPPC_LIB */
 
 #endif /* _CPPC_ACPI_H*/
Signature · 1 line
2.34.1
author reviewer
#2 lore raw

PATCH v4 2/9 Documentation: amd-pstate: add EPP profiles introduction

In reply to Perry Yuan

The patch add AMD pstate EPP feature introduction and what EPP preference supported for AMD processors.

User can get supported list from energy_performance_available_preferences attribute file, or update current profile to energy_performance_preference file

1) See all EPP profiles

$ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_available_preferences
default performance balance_performance balance_power power

2) Check current EPP profile

$ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference
performance

3) Set new EPP profile

$ sudo bash -c "echo power > /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference"
---
 Documentation/admin-guide/pm/amd-pstate.rst | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
Documentation/admin-guide/pm/amd-pstate.rst +19 -0
index 8f3d30c5a0d8..e7488891b12f 100644
@@ -262,6 +262,25 @@ lowest non-linear performance in `AMD CPPC Performance Capability
 <perf_cap_>`_.)
 This attribute is read-only.
 
+``energy_performance_available_preferences``
+
+A list of all the supported EPP preferences that could be used for
+``energy_performance_preference`` on this system.
+These profiles represent different hints that are provided
+to the low-level firmware about the user's desired energy vs efficiency
+tradeoff.  ``default`` represents the epp value is set by platform
+firmware. This attribute is read-only.
+
+``energy_performance_preference``
+
+The current energy performance preference can be read from this attribute.
+and user can change current preference according to energy or performance needs
+Please get all support profiles list from
+``energy_performance_available_preferences`` attribute, all the profiles are
+integer values defined between 0 to 255 when EPP feature is enabled by platform
+firmware, if EPP feature is disabled, driver will ignore the written value
+This attribute is read-write.
+
 Other performance and frequency values can be read back from
 ``/sys/devices/system/cpu/cpuX/acpi_cppc/``, see :ref:`cppc_sysfs`.
 
Signature · 1 line
2.34.1
author reviewer
#3 lore raw

PATCH v4 3/9 cpufreq: amd-pstate: change amd-pstate driver to be built-in type

In reply to Perry Yuan

Change the amd-pstate driver as the built-in type which can help to load the driver before the acpi_cpufreq driver as the default pstate driver for the AMD processors.

for the processors do not have the dedicated MSR functions, add amd-pstate=legacy_cppc to grub which enable shared memory interface to communicate with cppc_acpi module to control pstate hints.

---
 drivers/cpufreq/Kconfig.x86  |  2 +-
 drivers/cpufreq/amd-pstate.c | 25 +++++++++++++++----------
 2 files changed, 16 insertions(+), 11 deletions(-)
drivers/cpufreq/Kconfig.x86 +1 -1
index 310779b07daf..00476e94db90 100644
@@ -35,7 +35,7 @@ config X86_PCC_CPUFREQ
 	  If in doubt, say N.
 
 config X86_AMD_PSTATE
-	tristate "AMD Processor P-State driver"
+	bool "AMD Processor P-State driver"
 	depends on X86 && ACPI
 	select ACPI_PROCESSOR
 	select ACPI_CPPC_LIB if X86_64
drivers/cpufreq/amd-pstate.c +15 -10
index ace7d50cf2ac..85a0b3fb56c2 100644
@@ -59,10 +59,7 @@
  * we disable it by default to go acpi-cpufreq on these processors and add a
  * module parameter to be able to enable it manually for debugging.
  */
-static bool shared_mem = false;
-module_param(shared_mem, bool, 0444);
-MODULE_PARM_DESC(shared_mem,
-		 "enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)");
+static bool shared_mem __read_mostly;
 
 static struct cpufreq_driver amd_pstate_driver;
 
@@ -653,16 +650,24 @@ static int __init amd_pstate_init(void)
 
 	return ret;
 }
+device_initcall(amd_pstate_init);
 
-static void __exit amd_pstate_exit(void)
+static int __init amd_pstate_param(char *str)
 {
-	cpufreq_unregister_driver(&amd_pstate_driver);
+	if (!str)
+		return -EINVAL;
 
-	amd_pstate_enable(false);
-}
+	/*
+	 * support shared memory type CPPC which has no MSR function.
+	 * enable amd-pstate on processors with shared memory solution
+	 * (amd-pstate=legacy_cppc enabled), it is disabled by default.
+	 */
+	if (!strcmp(str, "legacy_cppc"))
+		shared_mem = true;
 
-module_init(amd_pstate_init);
-module_exit(amd_pstate_exit);
+	return 0;
+}
+early_param("amd-pstate", amd_pstate_param);
 
 MODULE_AUTHOR("Huang Rui <[email protected]>");
 MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
Signature · 1 line
2.34.1
author reviewer
#6 lore raw

PATCH v4 4/9 cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

In reply to Perry Yuan

Add EPP driver support for AMD SoCs which support a dedicated MSR for CPPC. EPP is used by the DPM controller to configure the frequency that a core operates at during short periods of activity.

The SoC EPP targets are configured on a scale from 0 to 255 where 0 represents maximum performance and 255 represents maximum efficiency.

The amd-pstate driver exports profile string names to userspace that are tied to specific EPP values.

The balance_performance string (0x80) provides the best balance for efficiency versus power on most systems, but users can choose other strings to meet their needs as well.

$ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences
default performance balance_performance balance_power power
$ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference
balance_performance
---
 drivers/cpufreq/amd-pstate.c | 652 ++++++++++++++++++++++++++++++++++-
 include/linux/amd-pstate.h   |  81 +++++
 2 files changed, 726 insertions(+), 7 deletions(-)
drivers/cpufreq/amd-pstate.c +645 -7
index 85a0b3fb56c2..7b3a12772582 100644
@@ -60,8 +60,131 @@
  * module parameter to be able to enable it manually for debugging.
  */
 static bool shared_mem __read_mostly;
+static int cppc_active __read_mostly;
+static int cppc_load __initdata;
+static int epp_off __initdata;
 
-static struct cpufreq_driver amd_pstate_driver;
+static struct cpufreq_driver *default_pstate_driver;
+static struct amd_cpudata **all_cpu_data;
+
+static struct amd_pstate_params global_params;
+
+static DEFINE_MUTEX(amd_pstate_limits_lock);
+static DEFINE_MUTEX(amd_pstate_driver_lock);
+
+static bool cppc_boost __read_mostly;
+struct kobject *amd_pstate_kobj;
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
+{
+	s16 epp;
+	struct cppc_perf_caps perf_caps;
+	int ret;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		if (!cppc_req_cached) {
+			epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
+					&cppc_req_cached);
+			if (epp)
+				return epp;
+		}
+		epp = (cppc_req_cached >> 24) & 0xFF;
+	} else {
+		ret = cppc_get_epp_caps(cpudata->cpu, &perf_caps);
+		if (ret < 0) {
+			pr_debug("Could not retrieve energy perf value (%d)\n", ret);
+			return -EIO;
+		}
+		epp = (s16) perf_caps.energy_perf;
+	}
+
+	return epp;
+}
+#endif
+
+static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
+{
+	s16 epp;
+	int index = -EINVAL;
+
+	epp = amd_pstate_get_epp(cpudata, 0);
+	if (epp < 0)
+		return epp;
+
+	switch (epp) {
+		case AMD_CPPC_EPP_PERFORMANCE:
+			index = EPP_INDEX_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
+			index = EPP_INDEX_BALANCE_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_POWERSAVE:
+			index = EPP_INDEX_BALANCE_POWERSAVE;
+			break;
+		case AMD_CPPC_EPP_POWERSAVE:
+			index = EPP_INDEX_POWERSAVE;
+			break;
+		default:
+			break;
+	}
+
+	return index;
+}
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
+{
+	int ret;
+	struct cppc_perf_ctrls perf_ctrls;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		u64 value = READ_ONCE(cpudata->cppc_req_cached);
+
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+		if (!ret)
+			cpudata->epp_cached = epp;
+	} else {
+		perf_ctrls.energy_perf = epp;
+		ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
+		if (ret) {
+			pr_debug("failed to set energy perf value (%d)\n", ret);
+			return ret;
+		}
+		cpudata->epp_cached = epp;
+	}
+
+	return ret;
+}
+
+static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
+		int pref_index)
+{
+	int epp = -EINVAL;
+	int ret;
+
+	if (!pref_index) {
+		pr_debug("EPP pref_index is invalid\n");
+		return -EINVAL;
+	}
+
+	if (epp == -EINVAL)
+		epp = epp_values[pref_index];
+
+	if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		pr_debug("EPP cannot be set under performance policy\n");
+		return -EBUSY;
+	}
+
+	ret = amd_pstate_set_epp(cpudata, epp);
+
+	return ret;
+}
+#endif
 
 static inline int pstate_enable(bool enable)
 {
@@ -71,11 +194,21 @@ static inline int pstate_enable(bool enable)
 static int cppc_enable(bool enable)
 {
 	int cpu, ret = 0;
+	struct cppc_perf_ctrls perf_ctrls;
 
 	for_each_present_cpu(cpu) {
 		ret = cppc_set_enable(cpu, enable);
 		if (ret)
 			return ret;
+
+		/* Enable autonomous mode for EPP */
+		if (!cppc_active) {
+			/* Set desired perf as zero to allow EPP firmware control */
+			perf_ctrls.desired_perf = 0;
+			ret = cppc_set_perf(cpu, &perf_ctrls);
+			if (ret)
+				return ret;
+		}
 	}
 
 	return ret;
@@ -418,7 +551,7 @@ static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
 		return;
 
 	cpudata->boost_supported = true;
-	amd_pstate_driver.boost_enabled = true;
+	default_pstate_driver->boost_enabled = true;
 }
 
 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
@@ -582,10 +715,62 @@ static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
 	return sprintf(&buf[0], "%u\n", perf);
 }
 
+static ssize_t show_energy_performance_available_preferences(
+				struct cpufreq_policy *policy, char *buf)
+{
+	int i = 0;
+	int ret = 0;
+
+	while (energy_perf_strings[i] != NULL)
+		ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
+
+	ret += sprintf(&buf[ret], "\n");
+
+	return ret;
+}
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count)
+{
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;
+
+	ret = sscanf(buf, "%20s", str_preference);
+	if (ret != 1)
+		return -EINVAL;
+
+	ret = match_string(energy_perf_strings, -1, str_preference);
+	if (ret < 0)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_limits_lock);
+	ret = amd_pstate_set_energy_pref_index(cpudata, ret);
+	mutex_unlock(&amd_pstate_limits_lock);
+
+	return ret ?: count;
+}
+
+static ssize_t show_energy_performance_preference(
+				struct cpufreq_policy *policy, char *buf)
+{
+	struct amd_cpudata *cpudata = policy->driver_data;
+	int preference;
+
+	preference = amd_pstate_get_energy_pref_index(cpudata);
+	if (preference < 0)
+		return preference;
+
+	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);
+}
+
 cpufreq_freq_attr_ro(amd_pstate_max_freq);
 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
+cpufreq_freq_attr_rw(energy_performance_preference);
+cpufreq_freq_attr_ro(energy_performance_available_preferences);
 
 static struct freq_attr *amd_pstate_attr[] = {
 	&amd_pstate_max_freq,
@@ -594,6 +779,415 @@ static struct freq_attr *amd_pstate_attr[] = {
 	NULL,
 };
 
+static struct freq_attr *amd_pstate_epp_attr[] = {
+	&amd_pstate_max_freq,
+	&amd_pstate_lowest_nonlinear_freq,
+	&amd_pstate_highest_perf,
+	&energy_performance_preference,
+	&energy_performance_available_preferences,
+	NULL,
+};
+
+static inline void update_boost_state(void)
+{
+	u64 misc_en;
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[0];
+	rdmsrl(MSR_K7_HWCR, misc_en);
+	global_params.cppc_boost_disabled = misc_en & BIT_ULL(25);
+}
+
+static int amd_pstate_init_cpu(unsigned int cpunum)
+{
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[cpunum];
+	if (!cpudata) {
+		cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
+		if (!cpudata)
+			return -ENOMEM;
+		WRITE_ONCE(all_cpu_data[cpunum], cpudata);
+
+		cpudata->cpu = cpunum;
+	}
+	cpudata->epp_powersave = -EINVAL;
+	cpudata->epp_policy = 0;
+	pr_debug("controlling: cpu %d\n", cpunum);
+	return 0;
+}
+
+static int __amd_pstate_cpu_init(struct cpufreq_policy *policy)
+{
+	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
+	struct amd_cpudata *cpudata;
+	struct device *dev;
+	int rc;
+	u64 value;
+
+	rc = amd_pstate_init_cpu(policy->cpu);
+	if (rc)
+		return rc;
+
+	cpudata = all_cpu_data[policy->cpu];
+
+	dev = get_cpu_device(policy->cpu);
+	if (!dev)
+		goto free_cpudata1;
+
+	rc = amd_pstate_init_perf(cpudata);
+	if (rc)
+		goto free_cpudata1;
+
+	min_freq = amd_get_min_freq(cpudata);
+	max_freq = amd_get_max_freq(cpudata);
+	nominal_freq = amd_get_nominal_freq(cpudata);
+	lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
+	if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
+		dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
+				min_freq, max_freq);
+		ret = -EINVAL;
+		goto free_cpudata1;
+	}
+
+	policy->min = min_freq;
+	policy->max = max_freq;
+
+	policy->cpuinfo.min_freq = min_freq;
+	policy->cpuinfo.max_freq = max_freq;
+	/* It will be updated by governor */
+	policy->cur = policy->cpuinfo.min_freq;
+
+	/* Initial processor data capability frequencies */
+	cpudata->max_freq = max_freq;
+	cpudata->min_freq = min_freq;
+	cpudata->nominal_freq = nominal_freq;
+	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
+
+	policy->driver_data = cpudata;
+
+	update_boost_state();
+	cpudata->epp_cached = amd_pstate_get_epp(cpudata, value);
+
+	policy->min = policy->cpuinfo.min_freq;
+	policy->max = policy->cpuinfo.max_freq;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC))
+		policy->fast_switch_possible = true;
+
+	if (!shared_mem && boot_cpu_has(X86_FEATURE_CPPC)) {
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_cap1_cached, value);
+	}
+	amd_pstate_boost_init(cpudata);
+
+	return 0;
+
+free_cpudata1:
+	kfree(cpudata);
+	return ret;
+}
+
+static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+{
+	int ret;
+
+	ret = __amd_pstate_cpu_init(policy);
+	if (ret)
+		return ret;
+	/*
+	 * Set the policy to powersave to provide a valid fallback value in case
+	 * the default cpufreq governor is neither powersave nor performance.
+	 */
+	policy->policy = CPUFREQ_POLICY_POWERSAVE;
+
+	return 0;
+}
+
+static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
+{
+	pr_debug("CPU %d exiting\n", policy->cpu);
+	policy->fast_switch_possible = false;
+	return 0;
+}
+
+static void amd_pstate_update_max_freq(unsigned int cpu)
+{
+	struct cpufreq_policy *policy = policy = cpufreq_cpu_get(cpu);
+
+	if (!policy)
+		return;
+
+	refresh_frequency_limits(policy);
+	cpufreq_cpu_put(policy);
+}
+
+static void amd_pstate_epp_update_limits(unsigned int cpu)
+{
+	mutex_lock(&amd_pstate_driver_lock);
+	update_boost_state();
+	if (global_params.cppc_boost_disabled) {
+		for_each_possible_cpu(cpu)
+			amd_pstate_update_max_freq(cpu);
+	} else {
+		cpufreq_update_policy(cpu);
+	}
+	mutex_unlock(&amd_pstate_driver_lock);
+}
+
+static int cppc_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
+
+static inline void amd_pstate_boost_up(struct amd_cpudata *cpudata)
+{
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u32 max_limit = (hwp_req & 0xff);
+	u32 min_limit = (hwp_req & 0xff00) >> 8;
+	u32 boost_level1;
+
+	/* If max and min are equal or already at max, nothing to boost */
+	if (max_limit == min_limit)
+		return;
+
+	/* Set boost max and min to initial value */
+	if (!cpudata->cppc_boost_min)
+		cpudata->cppc_boost_min = min_limit;
+
+	boost_level1 = ((AMD_CPPC_NOMINAL_PERF(hwp_cap) + min_limit) >> 1);
+
+	if (cpudata->cppc_boost_min < boost_level1)
+		cpudata->cppc_boost_min = boost_level1;
+	else if (cpudata->cppc_boost_min < AMD_CPPC_NOMINAL_PERF(hwp_cap))
+		cpudata->cppc_boost_min = AMD_CPPC_NOMINAL_PERF(hwp_cap);
+	else if (cpudata->cppc_boost_min == AMD_CPPC_NOMINAL_PERF(hwp_cap))
+		cpudata->cppc_boost_min = max_limit;
+	else
+		return;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(cpudata->cppc_boost_min);
+	wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req);
+	cpudata->last_update = cpudata->sample.time;
+}
+
+static inline void amd_pstate_boost_down(struct amd_cpudata *cpudata)
+{
+	bool expired;
+
+	if (cpudata->cppc_boost_min) {
+		expired = time_after64(cpudata->sample.time, cpudata->last_update +
+					cppc_boost_hold_time_ns);
+
+		if (expired) {
+			wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
+						cpudata->cppc_req_cached);
+			cpudata->cppc_boost_min = 0;
+		}
+	}
+
+	cpudata->last_update = cpudata->sample.time;
+}
+
+static inline void amd_pstate_boost_update_util(struct amd_cpudata *cpudata,
+						      u64 time)
+{
+	cpudata->sample.time = time;
+	if (smp_processor_id() != cpudata->cpu)
+		return;
+
+	if (cpudata->sched_flags & SCHED_CPUFREQ_IOWAIT) {
+		bool do_io = false;
+
+		cpudata->sched_flags = 0;
+		/*
+		 * Set iowait_boost flag and update time. Since IO WAIT flag
+		 * is set all the time, we can't just conclude that there is
+		 * some IO bound activity is scheduled on this CPU with just
+		 * one occurrence. If we receive at least two in two
+		 * consecutive ticks, then we treat as boost candidate.
+		 * This is leveraged from Intel Pstate driver.
+		 */
+		if (time_before64(time, cpudata->last_io_update + 2 * TICK_NSEC))
+			do_io = true;
+
+		cpudata->last_io_update = time;
+
+		if (do_io)
+			amd_pstate_boost_up(cpudata);
+
+	} else {
+		amd_pstate_boost_down(cpudata);
+	}
+}
+
+static inline void amd_pstate_cppc_update_hook(struct update_util_data *data,
+						u64 time, unsigned int flags)
+{
+	struct amd_cpudata *cpudata = container_of(data,
+				struct amd_cpudata, update_util);
+
+	cpudata->sched_flags |= flags;
+
+	if (smp_processor_id() == cpudata->cpu)
+		amd_pstate_boost_update_util(cpudata, time);
+}
+
+static void amd_pstate_clear_update_util_hook(unsigned int cpu)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+
+	if (!cpudata->update_util_set)
+		return;
+
+	cpufreq_remove_update_util_hook(cpu);
+	cpudata->update_util_set = false;
+	synchronize_rcu();
+}
+
+static void amd_pstate_set_update_util_hook(unsigned int cpu_num)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu_num];
+
+	if (!cppc_boost) {
+		if (cpudata->update_util_set)
+			amd_pstate_clear_update_util_hook(cpudata->cpu);
+		return;
+	}
+
+	if (cpudata->update_util_set)
+		return;
+
+	cpudata->sample.time = 0;
+	cpufreq_add_update_util_hook(cpu_num, &cpudata->update_util,
+						amd_pstate_cppc_update_hook);
+	cpudata->update_util_set = true;
+}
+
+static void amd_pstate_epp_init(unsigned int cpu)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+	u32 max_perf, min_perf;
+	u64 value;
+	s16 epp;
+	int ret;
+
+	max_perf = READ_ONCE(cpudata->highest_perf);
+	min_perf = READ_ONCE(cpudata->lowest_perf);
+
+	value = READ_ONCE(cpudata->cppc_req_cached);
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
+		min_perf = max_perf;
+
+	/* Initial min/max values for CPPC Performance Controls Register */
+	value &= ~AMD_CPPC_MIN_PERF(~0L);
+	value |= AMD_CPPC_MIN_PERF(min_perf);
+
+	value &= ~AMD_CPPC_MAX_PERF(~0L);
+	value |= AMD_CPPC_MAX_PERF(max_perf);
+
+	/* CPPC EPP feature require to set zero to the desire perf bit */
+	value &= ~AMD_CPPC_DES_PERF(~0L);
+	value |= AMD_CPPC_DES_PERF(0);
+
+	if (cpudata->epp_policy == cpudata->policy)
+		goto skip_epp;
+
+	cpudata->epp_policy = cpudata->policy;
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		epp = amd_pstate_get_epp(cpudata, value);
+		cpudata->epp_powersave = epp;
+		if (epp < 0)
+			goto skip_epp;
+		/* force the epp value to be zero for performance policy */
+		epp = 0;
+	} else {
+		if (cpudata->epp_powersave < 0)
+			goto skip_epp;
+		/* Get BIOS pre-defined epp value */
+		epp = amd_pstate_get_epp(cpudata, value);
+		if (epp)
+			goto skip_epp;
+		epp = cpudata->epp_powersave;
+	}
+	/* Set initial EPP value */
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+	}
+
+skip_epp:
+	WRITE_ONCE(cpudata->cppc_req_cached, value);
+	ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	if (!ret)
+		cpudata->epp_cached = epp;
+}
+
+static void amd_pstate_set_max_limits(struct amd_cpudata *cpudata)
+{
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u32 max_limit = (hwp_cap >> 24) & 0xff;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(max_limit);
+	wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req);
+}
+
+static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata;
+
+	if (!policy->cpuinfo.max_freq)
+		return -ENODEV;
+
+	pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
+				policy->cpuinfo.max_freq, policy->max);
+
+	cpudata = all_cpu_data[policy->cpu];
+	cpudata->policy = policy->policy;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		mutex_lock(&amd_pstate_limits_lock);
+
+		if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+			amd_pstate_clear_update_util_hook(policy->cpu);
+			amd_pstate_set_max_limits(cpudata);
+		} else {
+			amd_pstate_set_update_util_hook(policy->cpu);
+		}
+
+		if (boot_cpu_has(X86_FEATURE_CPPC))
+			amd_pstate_epp_init(policy->cpu);
+
+		mutex_unlock(&amd_pstate_limits_lock);
+	}
+
+	return 0;
+}
+
+static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
+					   struct cpufreq_policy_data *policy)
+{
+	update_boost_state();
+	cpufreq_verify_within_cpu_limits(policy);
+}
+
+static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
+{
+	amd_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
+	pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
+	return 0;
+}
+
 static struct cpufreq_driver amd_pstate_driver = {
 	.flags		= CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
 	.verify		= amd_pstate_verify,
@@ -607,8 +1201,20 @@ static struct cpufreq_driver amd_pstate_driver = {
 	.attr		= amd_pstate_attr,
 };
 
+static struct cpufreq_driver amd_pstate_epp_driver = {
+	.flags		= CPUFREQ_CONST_LOOPS,
+	.verify		= amd_pstate_epp_verify_policy,
+	.setpolicy	= amd_pstate_epp_set_policy,
+	.init		= amd_pstate_epp_cpu_init,
+	.exit		= amd_pstate_epp_cpu_exit,
+	.update_limits	= amd_pstate_epp_update_limits,
+	.name		= "amd_pstate_epp",
+	.attr		= amd_pstate_epp_attr,
+};
+
 static int __init amd_pstate_init(void)
 {
+	static struct amd_cpudata **cpudata;
 	int ret;
 
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
@@ -623,19 +1229,37 @@ static int __init amd_pstate_init(void)
 	if (cpufreq_get_current_driver())
 		return -EEXIST;
 
+	/* set the pstate driver disabled by default if there
+	 * is no early parameters set
+	 */
+	if (!cppc_load) {
+		pr_debug("disabled to load\n");
+		return -ENODEV;
+	}
+
+	if (!epp_off) {
+		WRITE_ONCE(cppc_active, 1);
+		if (!default_pstate_driver)
+			default_pstate_driver = &amd_pstate_epp_driver;
+	}
+
 	/* capability check */
 	if (boot_cpu_has(X86_FEATURE_CPPC)) {
-		pr_debug("AMD CPPC MSR based functionality is supported\n");
-		amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf;
+		if (!cppc_active)
+			default_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
 	} else if (shared_mem) {
 		static_call_update(amd_pstate_enable, cppc_enable);
 		static_call_update(amd_pstate_init_perf, cppc_init_perf);
 		static_call_update(amd_pstate_update_perf, cppc_update_perf);
 	} else {
-		pr_info("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n");
+		pr_info("This processor supports shared memory solution, enable it with amd_pstate=legacy_cppc\n");
 		return -ENODEV;
 	}
 
+	cpudata = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
+	if (!cpudata)
+		return -ENOMEM;
+	WRITE_ONCE(all_cpu_data, cpudata);
 	/* enable amd pstate feature */
 	ret = amd_pstate_enable(true);
 	if (ret) {
@@ -643,9 +1267,9 @@ static int __init amd_pstate_init(void)
 		return ret;
 	}
 
-	ret = cpufreq_register_driver(&amd_pstate_driver);
+	ret = cpufreq_register_driver(default_pstate_driver);
 	if (ret)
-		pr_err("failed to register amd_pstate_driver with return %d\n",
+		pr_err("failed to register amd pstate driver with return %d\n",
 		       ret);
 
 	return ret;
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
 	if (!str)
 		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
+	}
+
 	/*
 	 * support shared memory type CPPC which has no MSR function.
 	 * enable amd-pstate on processors with shared memory solution
include/linux/amd-pstate.h +81 -0

99 lines not on this page · raw message

Signature · 1 line
2.34.1
author reviewer
#12 lore raw

PATCH v4 5/9 cpufreq: amd_pstate: implement amd pstate cpu online and offline callback

In reply to Perry Yuan

Adds online and offline driver callback support to allow cpu cores go offline and help to restore the previous working states when core goes back online later for EPP driver mode.

---
 drivers/cpufreq/amd-pstate.c | 89 ++++++++++++++++++++++++++++++++++++
 include/linux/amd-pstate.h   |  1 +
 2 files changed, 90 insertions(+)
drivers/cpufreq/amd-pstate.c +89 -0
index 7b3a12772582..9a9abb755a18 100644
@@ -1174,6 +1174,93 @@ static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
 	return 0;
 }
 
+static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
+{
+	struct cppc_perf_ctrls perf_ctrls;
+	u64 value, max_perf;
+	int ret;
+
+	ret = amd_pstate_enable(true);
+	if (ret)
+		pr_err("failed to enable amd pstate during resume, return %d\n", ret);
+
+	value = READ_ONCE(cpudata->cppc_req_cached);
+	max_perf = READ_ONCE(cpudata->highest_perf);
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	} else {
+		perf_ctrls.max_perf = max_perf;
+		perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(cpudata->epp_cached);
+		cppc_set_perf(cpudata->cpu, &perf_ctrls);
+	}
+}
+
+static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[policy->cpu];
+
+	pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
+
+	if (cppc_active) {
+		amd_pstate_epp_reenable(cpudata);
+		cpudata->suspended = false;
+	}
+
+	return 0;
+}
+
+static void amd_pstate_epp_offline(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[policy->cpu];
+	struct cppc_perf_ctrls perf_ctrls;
+	int min_perf;
+	u64 value;
+
+	min_perf = READ_ONCE(cpudata->lowest_perf);
+	value = READ_ONCE(cpudata->cppc_req_cached);
+
+	mutex_lock(&amd_pstate_limits_lock);
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		cpudata->epp_policy = CPUFREQ_POLICY_UNKNOWN;
+
+		/* Set max perf same as min perf */
+		value &= ~AMD_CPPC_MAX_PERF(~0L);
+		value |= AMD_CPPC_MAX_PERF(min_perf);
+		value &= ~AMD_CPPC_MIN_PERF(~0L);
+		value |= AMD_CPPC_MIN_PERF(min_perf);
+		wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	} else {
+		perf_ctrls.desired_perf = 0;
+		perf_ctrls.max_perf = min_perf;
+		perf_ctrls.energy_perf = AMD_CPPC_ENERGY_PERF_PREF(AMD_CPPC_EPP_POWERSAVE);
+		cppc_set_perf(cpudata->cpu, &perf_ctrls);
+	}
+	mutex_unlock(&amd_pstate_limits_lock);
+}
+
+static int amd_pstate_cpu_offline(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[policy->cpu];
+
+	pr_debug("AMD CPU Core %d going offline\n", cpudata->cpu);
+
+	if (cpudata->suspended)
+		return 0;
+
+	if (cppc_active)
+		amd_pstate_epp_offline(policy);
+
+	return 0;
+}
+
+static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
+{
+	amd_pstate_clear_update_util_hook(policy->cpu);
+
+	return amd_pstate_cpu_offline(policy);
+}
+
 static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
 					   struct cpufreq_policy_data *policy)
 {
@@ -1208,6 +1295,8 @@ static struct cpufreq_driver amd_pstate_epp_driver = {
 	.init		= amd_pstate_epp_cpu_init,
 	.exit		= amd_pstate_epp_cpu_exit,
 	.update_limits	= amd_pstate_epp_update_limits,
+	.offline	= amd_pstate_epp_cpu_offline,
+	.online		= amd_pstate_epp_cpu_online,
 	.name		= "amd_pstate_epp",
 	.attr		= amd_pstate_epp_attr,
 };
include/linux/amd-pstate.h +1 -0
index 7e6e8cab97b3..c0ad7eedcae3 100644
@@ -99,6 +99,7 @@ struct amd_cpudata {
 	u64	cppc_cap1_cached;
 	struct	update_util_data update_util;
 	struct	amd_aperf_mperf sample;
+	bool suspended;
 };
 
 /**
Signature · 1 line
2.34.1
author reviewer
#13 lore raw

PATCH v4 6/9 cpufreq: amd-pstate: implement suspend and resume callbacks

In reply to Perry Yuan

add suspend and resume support for the AMD processors by amd_pstate_epp driver instance.

When the CPPC is suspended, EPP driver will set EPP profile to 'power' profile and set max/min perf to lowest perf value.
When resume happens, it will restore the MSR registers with previous cached value.

---
 drivers/cpufreq/amd-pstate.c | 40 ++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
drivers/cpufreq/amd-pstate.c +40 -0
index 9a9abb755a18..645706d65173 100644
@@ -1261,6 +1261,44 @@ static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
 	return amd_pstate_cpu_offline(policy);
 }
 
+static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[policy->cpu];
+	int ret;
+
+	/* avoid suspending when EPP is not enabled */
+	if (!cppc_active)
+		return 0;
+
+	/* set this flag to avoid setting core offline*/
+	cpudata->suspended = true;
+
+	/* disable CPPC in lowlevel firmware */
+	ret = amd_pstate_enable(false);
+	if (ret)
+		pr_err("failed to suspend, return %d\n", ret);
+
+	return 0;
+}
+
+static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[policy->cpu];
+
+	if (cpudata->suspended) {
+		mutex_lock(&amd_pstate_limits_lock);
+
+		/* enable amd pstate from suspend state*/
+		amd_pstate_epp_reenable(cpudata);
+
+		mutex_unlock(&amd_pstate_limits_lock);
+
+		cpudata->suspended = false;
+	}
+
+	return 0;
+}
+
 static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
 					   struct cpufreq_policy_data *policy)
 {
@@ -1297,6 +1335,8 @@ static struct cpufreq_driver amd_pstate_epp_driver = {
 	.update_limits	= amd_pstate_epp_update_limits,
 	.offline	= amd_pstate_epp_cpu_offline,
 	.online		= amd_pstate_epp_cpu_online,
+	.suspend	= amd_pstate_epp_suspend,
+	.resume		= amd_pstate_epp_resume,
 	.name		= "amd_pstate_epp",
 	.attr		= amd_pstate_epp_attr,
 };
Signature · 1 line
2.34.1
author reviewer
#14 lore raw

PATCH v4 7/9 cpufreq: amd-pstate: add frequency dynamic boost sysfs control

In reply to Perry Yuan

Add one sysfs entry to control the CPU cores frequency boost state The attribute file can allow user to set max performance boosted or keeping at normal perf level.

---
 drivers/cpufreq/amd-pstate.c | 66 ++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 2 deletions(-)
drivers/cpufreq/amd-pstate.c +64 -2
index 645706d65173..b71bfbbb7639 100644
@@ -765,12 +765,46 @@ static ssize_t show_energy_performance_preference(
 	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);
 }
 
+static void amd_pstate_update_policies(void)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		cpufreq_update_policy(cpu);
+}
+
+static ssize_t show_boost(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%u\n", cppc_boost);
+}
+
+static ssize_t store_boost(struct kobject *a,
+				       struct kobj_attribute *b,
+				       const char *buf, size_t count)
+{
+	bool new_state;
+	int ret;
+
+	ret = kstrtobool(buf, &new_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	cppc_boost = !!new_state;
+	amd_pstate_update_policies();
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return count;
+}
+
 cpufreq_freq_attr_ro(amd_pstate_max_freq);
 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
 cpufreq_freq_attr_ro(amd_pstate_highest_perf);
 cpufreq_freq_attr_rw(energy_performance_preference);
 cpufreq_freq_attr_ro(energy_performance_available_preferences);
+define_one_global_rw(boost);
 
 static struct freq_attr *amd_pstate_attr[] = {
 	&amd_pstate_max_freq,
@@ -788,6 +822,15 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
 	NULL,
 };
 
+static struct attribute *pstate_global_attributes[] = {
+	&boost.attr,
+	NULL
+};
+
+static const struct attribute_group amd_pstate_global_attr_group = {
+	.attrs = pstate_global_attributes,
+};
+
 static inline void update_boost_state(void)
 {
 	u64 misc_en;
@@ -1398,9 +1441,28 @@ static int __init amd_pstate_init(void)
 
 	ret = cpufreq_register_driver(default_pstate_driver);
 	if (ret)
-		pr_err("failed to register amd pstate driver with return %d\n",
-		       ret);
+		pr_err("failed to register driver with return %d\n", ret);
+
+	amd_pstate_kobj = kobject_create_and_add("amd-pstate", &cpu_subsys.dev_root->kobj);
+	if (!amd_pstate_kobj) {
+		ret = -EINVAL;
+		pr_err("global sysfs registration failed.\n");
+		goto kobject_free;
+	}
+
+	ret = sysfs_create_group(amd_pstate_kobj, &amd_pstate_global_attr_group);
+	if (ret) {
+		pr_err("sysfs attribute export failed with error %d.\n", ret);
+		goto global_attr_free;
+	}
+
+	return ret;
 
+global_attr_free:
+	kobject_put(amd_pstate_kobj);
+kobject_free:
+	kfree(cpudata);
+	cpufreq_unregister_driver(default_pstate_driver);
 	return ret;
 }
 device_initcall(amd_pstate_init);
Signature · 1 line
2.34.1
author reviewer
#17 lore raw

PATCH v4 8/9 cpufreq: amd_pstate: add driver working mode status sysfs entry

In reply to Perry Yuan

While amd-pstate driver was loaded with specific driver mode, it will need to check which mode is enabled for the pstate driver,add this sysfs entry to show the current status

$ cat /sys/devices/system/cpu/amd-pstate/status
active
---
 drivers/cpufreq/amd-pstate.c | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/cpufreq/amd-pstate.c +44 -0
index b71bfbbb7639..6a3b610cb084 100644
@@ -65,6 +65,8 @@ static int cppc_load __initdata;
 static int epp_off __initdata;
 
 static struct cpufreq_driver *default_pstate_driver;
+static struct cpufreq_driver amd_pstate_epp_driver;
+static struct cpufreq_driver amd_pstate_driver;
 static struct amd_cpudata **all_cpu_data;
 
 static struct amd_pstate_params global_params;
@@ -798,6 +800,46 @@ static ssize_t store_boost(struct kobject *a,
 	return count;
 }
 
+static ssize_t amd_pstate_show_status(char *buf)
+{
+	if (!default_pstate_driver)
+		return sprintf(buf, "off\n");
+
+	return sprintf(buf, "%s\n", default_pstate_driver == &amd_pstate_epp_driver ?
+					"active" : "passive");
+}
+
+static int amd_pstate_update_status(const char *buf, size_t size)
+{
+	/* FIXME! */
+	return -EOPNOTSUPP;
+}
+
+static ssize_t show_status(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf)
+{
+	ssize_t ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_show_status(buf);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret;
+}
+
+static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
+			    const char *buf, size_t count)
+{
+	char *p = memchr(buf, '\n', count);
+	int ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_update_status(buf, p ? p - buf : count);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret < 0 ? ret : count;
+}
+
 cpufreq_freq_attr_ro(amd_pstate_max_freq);
 cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
@@ -805,6 +847,7 @@ cpufreq_freq_attr_ro(amd_pstate_highest_perf);
 cpufreq_freq_attr_rw(energy_performance_preference);
 cpufreq_freq_attr_ro(energy_performance_available_preferences);
 define_one_global_rw(boost);
+define_one_global_rw(status);
 
 static struct freq_attr *amd_pstate_attr[] = {
 	&amd_pstate_max_freq,
@@ -824,6 +867,7 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
 
 static struct attribute *pstate_global_attributes[] = {
 	&boost.attr,
+	&status.attr,
 	NULL
 };
 
Signature · 1 line
2.34.1
author reviewer
#20 lore raw

PATCH v4 9/9 Documentation: amd-pstate: add amd pstate driver mode introduction

In reply to Perry Yuan

Introduce ``amd-pstate`` CPPC has two operation modes:
* CPPC Autonomous (active) mode
* CPPC non-autonomous (passive) mode.
active mode and passive mode can be choosed by whith different kernel parameters.

---
 Documentation/admin-guide/pm/amd-pstate.rst | 47 +++++++++++++++++++--
 1 file changed, 43 insertions(+), 4 deletions(-)
Documentation/admin-guide/pm/amd-pstate.rst +43 -4
index e7488891b12f..6ba02a658b31 100644
@@ -302,11 +302,11 @@ efficiency frequency management method on AMD processors.
 Kernel Module Options for ``amd-pstate``
 =========================================
 
-.. _shared_mem:
+.. legacy_cppc:
 
-``shared_mem``
-Use a module param (shared_mem) to enable related processors manually with
-**amd_pstate.shared_mem=1**.
+``legacy_cppc``
+Use a module param (legacy_cppc) to enable related processors manually with
+**amd_pstate=legacy_cppc**.
 Due to the performance issue on the processors with `Shared Memory Support
 <perf_cap_>`_, we disable it presently and will re-enable this by default
 once we address performance issue with this solution.
@@ -321,6 +321,45 @@ If the CPU flags have ``cppc``, then this processor supports `Full MSR Support
 <perf_cap_>`_. Otherwise, it supports `Shared Memory Support <perf_cap_>`_.
 
 
+AMD Pstae Driver Operation Modes
+=================================
+
+``amd-pstate`` CPPC has two operation modes: CPPC Autonomous(active) mode and
+CPPC non-autonomous(passive) mode.
+active mode and passive mode can be choosed by whith different kernel parameters.
+When in Autonomous mode, CPPC ignores requests done in the Desired Performance 
+Target register and takes into account only the values set to the Minimum requested
+performance, Maximum requested performance, and Energy Performance Preference
+registers. When Autonomous is disabled, it only considers the Desired Performance Target.
+
+Active Mode
+------------
+
+``amd-pstate-epp``
+
+This is the low-level firmware control mode which is implemented by ``amd-pstate-epp``
+driver with ``amd-pstate=active`` passed to the kernel in the command line.
+In this mode, ``amd-pstate-epp`` driver provides a hint to the hardware if software
+wants to bias toward performance (0x0) or energy efficiency (0xff) to the CPPC firmware.
+then CPPC power algorithm will calculate the runtime workload and adjust the realtime
+cores frequency according to the power supply and thermal, core voltage and some other
+hardware conditions.
+
+Passive Mode
+------------
+
+``amd-pstate``
+
+It will be enabled if the ``amd_pstate=passive`` is passed to the kernel in the command line.
+In this mode, ``amd-pstate``driver software specifies a desired QoS target in the CPPC
+performance scale as a relative number. This can be expressed as percentage of nominal
+performance (infrastructure max). Below the nominal sustained performance level,
+desired performance expresses the average performance level of the processor subject
+to the Performance Reduction Tolerance register. Above the nominal performance level,
+processor must provide at least nominal performance requested and go higher if current
+operating conditions allow.
+
+
 ``cpupower`` tool support for ``amd-pstate``
 ===============================================
 
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v4 9/9] Documentation: amd-pstate: add amd pstate driver mode introduction

Please run this through a spell checker before v5.

Quoted 4 lines · Perry Yuan

Introduce ``amd-pstate`` CPPC has two operation modes:
* CPPC Autonomous (active) mode
* CPPC non-autonomous (passive) mode.
active mode and passive mode can be choosed by whith different kernel parameters.

can be chosen by different kernel parameters

Quoted 27 lines
---
  Documentation/admin-guide/pm/amd-pstate.rst | 47 +++++++++++++++++++--
  1 file changed, 43 insertions(+), 4 deletions(-)
Documentation/admin-guide/pm/amd-pstate.rst +4 -4
index e7488891b12f..6ba02a658b31 100644
@@ -302,11 +302,11 @@ efficiency frequency management method on AMD processors.
  Kernel Module Options for ``amd-pstate``
  =========================================
 
-.. _shared_mem:
+.. legacy_cppc:
 
-``shared_mem``
-Use a module param (shared_mem) to enable related processors manually with
-**amd_pstate.shared_mem=1**.
+``legacy_cppc``
+Use a module param (legacy_cppc) to enable related processors manually with
+**amd_pstate=legacy_cppc**.
  Due to the performance issue on the processors with `Shared Memory Support
  <perf_cap_>`_, we disable it presently and will re-enable this by default
  once we address performance issue with this solution.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor supports `Full MSR Support · line 324

Quoted 4 lines
Documentation/admin-guide/pm/amd-pstate.rst +1 -0
@@ -321,6 +321,45 @@ If the CPU flags have ``cppc``, then this processor supports `Full MSR Support
  <perf_cap_>`_. Otherwise, it supports `Shared Memory Support <perf_cap_>`_.
 
 
+AMD Pstae Driver Operation Modes

Pstate

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor supports `Full MSR Support · line 329

Quoted 5 lines
Documentation/admin-guide/pm/amd-pstate.rst +5 -0
+=================================
+
+``amd-pstate`` CPPC has two operation modes: CPPC Autonomous(active) mode and
+CPPC non-autonomous(passive) mode.
+active mode and passive mode can be choosed by whith different kernel parameters.

can be chosen by different kernel parameters

Quoted 36 lines
Documentation/admin-guide/pm/amd-pstate.rst +33 -0
+When in Autonomous mode, CPPC ignores requests done in the Desired Performance
+Target register and takes into account only the values set to the Minimum requested
+performance, Maximum requested performance, and Energy Performance Preference
+registers. When Autonomous is disabled, it only considers the Desired Performance Target.
+
+Active Mode
+------------
+
+``amd-pstate-epp``
+
+This is the low-level firmware control mode which is implemented by ``amd-pstate-epp``
+driver with ``amd-pstate=active`` passed to the kernel in the command line.
+In this mode, ``amd-pstate-epp`` driver provides a hint to the hardware if software
+wants to bias toward performance (0x0) or energy efficiency (0xff) to the CPPC firmware.
+then CPPC power algorithm will calculate the runtime workload and adjust the realtime
+cores frequency according to the power supply and thermal, core voltage and some other
+hardware conditions.
+
+Passive Mode
+------------
+
+``amd-pstate``
+
+It will be enabled if the ``amd_pstate=passive`` is passed to the kernel in the command line.
+In this mode, ``amd-pstate``driver software specifies a desired QoS target in the CPPC
+performance scale as a relative number. This can be expressed as percentage of nominal
+performance (infrastructure max). Below the nominal sustained performance level,
+desired performance expresses the average performance level of the processor subject
+to the Performance Reduction Tolerance register. Above the nominal performance level,
+processor must provide at least nominal performance requested and go higher if current
+operating conditions allow.
+
+
  ``cpupower`` tool support for ``amd-pstate``
  ===============================================
 
#18 lore raw

Re: [PATCH v4 8/9] cpufreq: amd_pstate: add driver working mode status sysfs entry

In reply to Perry Yuan
Quoted 27 lines · Perry Yuan

While amd-pstate driver was loaded with specific driver mode, it will need to check which mode is enabled for the pstate driver,add this sysfs entry to show the current status

$ cat /sys/devices/system/cpu/amd-pstate/status
active
---
  drivers/cpufreq/amd-pstate.c | 44 ++++++++++++++++++++++++++++++++++++
  1 file changed, 44 insertions(+)
drivers/cpufreq/amd-pstate.c +2 -0
index b71bfbbb7639..6a3b610cb084 100644
@@ -65,6 +65,8 @@ static int cppc_load __initdata;
  static int epp_off __initdata;
 
  static struct cpufreq_driver *default_pstate_driver;
+static struct cpufreq_driver amd_pstate_epp_driver;
+static struct cpufreq_driver amd_pstate_driver;
  static struct amd_cpudata **all_cpu_data;
 
  static struct amd_pstate_params global_params;
@@ -798,6 +800,46 @@ static ssize_t store_boost(struct kobject *a,
  	return count;

drivers/cpufreq/amd-pstate.c static ssize_t store_boost(struct kobject *a, · line 810

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +8 -0
  }
 
+static ssize_t amd_pstate_show_status(char *buf)
+{
+	if (!default_pstate_driver)
+		return sprintf(buf, "off\n");
+
+	return sprintf(buf, "%s\n", default_pstate_driver == &amd_pstate_epp_driver ?
+					"active" : "passive");
+}

For new code I believe you should use the new helper sysfs_emit() instead.

Quoted 51 lines
drivers/cpufreq/amd-pstate.c +34 -0
+
+static int amd_pstate_update_status(const char *buf, size_t size)
+{
+	/* FIXME! */
+	return -EOPNOTSUPP;
+}
+
+static ssize_t show_status(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf)
+{
+	ssize_t ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_show_status(buf);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret;
+}
+
+static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
+			    const char *buf, size_t count)
+{
+	char *p = memchr(buf, '\n', count);
+	int ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_update_status(buf, p ? p - buf : count);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret < 0 ? ret : count;
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
@@ -805,6 +847,7 @@ cpufreq_freq_attr_ro(amd_pstate_highest_perf);
  cpufreq_freq_attr_rw(energy_performance_preference);
  cpufreq_freq_attr_ro(energy_performance_available_preferences);
  define_one_global_rw(boost);
+define_one_global_rw(status);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -824,6 +867,7 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
 
  static struct attribute *pstate_global_attributes[] = {
  	&boost.attr,
+	&status.attr,
  	NULL
  };
 
#15 lore raw

Re: [PATCH v4 7/9] cpufreq: amd-pstate: add frequency dynamic boost sysfs control

In reply to Perry Yuan
Quoted 19 lines · Perry Yuan

Add one sysfs entry to control the CPU cores frequency boost state The attribute file can allow user to set max performance boosted or keeping at normal perf level.

---
  drivers/cpufreq/amd-pstate.c | 66 ++++++++++++++++++++++++++++++++++--
  1 file changed, 64 insertions(+), 2 deletions(-)
drivers/cpufreq/amd-pstate.c +2 -0
index 645706d65173..b71bfbbb7639 100644
@@ -765,12 +765,46 @@ static ssize_t show_energy_performance_preference(
  	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);
  }
 
+static void amd_pstate_update_policies(void)
+{

drivers/cpufreq/amd-pstate.c static ssize_t show_energy_performance_preference( · line 779

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		cpufreq_update_policy(cpu);
+}
+
+static ssize_t show_boost(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%u\n", cppc_boost);

For new code I believe you should use the new helper sysfs_emit() instead.

Quoted 78 lines
drivers/cpufreq/amd-pstate.c +52 -2
+}
+
+static ssize_t store_boost(struct kobject *a,
+				       struct kobj_attribute *b,
+				       const char *buf, size_t count)
+{
+	bool new_state;
+	int ret;
+
+	ret = kstrtobool(buf, &new_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	cppc_boost = !!new_state;
+	amd_pstate_update_policies();
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return count;
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
  cpufreq_freq_attr_ro(amd_pstate_highest_perf);
  cpufreq_freq_attr_rw(energy_performance_preference);
  cpufreq_freq_attr_ro(energy_performance_available_preferences);
+define_one_global_rw(boost);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -788,6 +822,15 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
  	NULL,
  };
 
+static struct attribute *pstate_global_attributes[] = {
+	&boost.attr,
+	NULL
+};
+
+static const struct attribute_group amd_pstate_global_attr_group = {
+	.attrs = pstate_global_attributes,
+};
+
  static inline void update_boost_state(void)
  {
  	u64 misc_en;
@@ -1398,9 +1441,28 @@ static int __init amd_pstate_init(void)
 
  	ret = cpufreq_register_driver(default_pstate_driver);
  	if (ret)
-		pr_err("failed to register amd pstate driver with return %d\n",
-		       ret);
+		pr_err("failed to register driver with return %d\n", ret);
+
+	amd_pstate_kobj = kobject_create_and_add("amd-pstate", &cpu_subsys.dev_root->kobj);
+	if (!amd_pstate_kobj) {
+		ret = -EINVAL;
+		pr_err("global sysfs registration failed.\n");
+		goto kobject_free;
+	}
+
+	ret = sysfs_create_group(amd_pstate_kobj, &amd_pstate_global_attr_group);
+	if (ret) {
+		pr_err("sysfs attribute export failed with error %d.\n", ret);
+		goto global_attr_free;
+	}
+
+	return ret;
 
+global_attr_free:
+	kobject_put(amd_pstate_kobj);
+kobject_free:
+	kfree(cpudata);
+	cpufreq_unregister_driver(default_pstate_driver);
  	return ret;
  }
  device_initcall(amd_pstate_init);
#7 lore raw

Re: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

In reply to Perry Yuan
Quoted 17 lines · Perry Yuan

Add EPP driver support for AMD SoCs which support a dedicated MSR for CPPC. EPP is used by the DPM controller to configure the frequency that a core operates at during short periods of activity.

The SoC EPP targets are configured on a scale from 0 to 255 where 0 represents maximum performance and 255 represents maximum efficiency.

The amd-pstate driver exports profile string names to userspace that are tied to specific EPP values.

The balance_performance string (0x80) provides the best balance for efficiency versus power on most systems, but users can choose other strings to meet their needs as well.

$ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences
default performance balance_performance balance_power power
Quoted 9 lines
$ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference
balance_performance
---
  drivers/cpufreq/amd-pstate.c | 652 ++++++++++++++++++++++++++++++++++-
  include/linux/amd-pstate.h   |  81 +++++
  2 files changed, 726 insertions(+), 7 deletions(-)

Did you see Rafael's comment about reducing the common code w/ intel-pstate for future maintainability?

As some of it is duplicated, I think it should be possible to make a library source file that both can use and pull from. If you split out the common code in an earlier patch I think it would shrink this patch significantly too.

If you already looked into this and it doesn't /really/ change much, please comment also.

Quoted 172 lines
drivers/cpufreq/amd-pstate.c +135 -2
index 85a0b3fb56c2..7b3a12772582 100644
@@ -60,8 +60,131 @@
   * module parameter to be able to enable it manually for debugging.
   */
  static bool shared_mem __read_mostly;
+static int cppc_active __read_mostly;
+static int cppc_load __initdata;
+static int epp_off __initdata;
 
-static struct cpufreq_driver amd_pstate_driver;
+static struct cpufreq_driver *default_pstate_driver;
+static struct amd_cpudata **all_cpu_data;
+
+static struct amd_pstate_params global_params;
+
+static DEFINE_MUTEX(amd_pstate_limits_lock);
+static DEFINE_MUTEX(amd_pstate_driver_lock);
+
+static bool cppc_boost __read_mostly;
+struct kobject *amd_pstate_kobj;
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
+{
+	s16 epp;
+	struct cppc_perf_caps perf_caps;
+	int ret;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		if (!cppc_req_cached) {
+			epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
+					&cppc_req_cached);
+			if (epp)
+				return epp;
+		}
+		epp = (cppc_req_cached >> 24) & 0xFF;
+	} else {
+		ret = cppc_get_epp_caps(cpudata->cpu, &perf_caps);
+		if (ret < 0) {
+			pr_debug("Could not retrieve energy perf value (%d)\n", ret);
+			return -EIO;
+		}
+		epp = (s16) perf_caps.energy_perf;
+	}
+
+	return epp;
+}
+#endif
+
+static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
+{
+	s16 epp;
+	int index = -EINVAL;
+
+	epp = amd_pstate_get_epp(cpudata, 0);
+	if (epp < 0)
+		return epp;
+
+	switch (epp) {
+		case AMD_CPPC_EPP_PERFORMANCE:
+			index = EPP_INDEX_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
+			index = EPP_INDEX_BALANCE_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_POWERSAVE:
+			index = EPP_INDEX_BALANCE_POWERSAVE;
+			break;
+		case AMD_CPPC_EPP_POWERSAVE:
+			index = EPP_INDEX_POWERSAVE;
+			break;
+		default:
+			break;
+	}
+
+	return index;
+}
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
+{
+	int ret;
+	struct cppc_perf_ctrls perf_ctrls;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		u64 value = READ_ONCE(cpudata->cppc_req_cached);
+
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+		if (!ret)
+			cpudata->epp_cached = epp;
+	} else {
+		perf_ctrls.energy_perf = epp;
+		ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
+		if (ret) {
+			pr_debug("failed to set energy perf value (%d)\n", ret);
+			return ret;
+		}
+		cpudata->epp_cached = epp;
+	}
+
+	return ret;
+}
+
+static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
+		int pref_index)
+{
+	int epp = -EINVAL;
+	int ret;
+
+	if (!pref_index) {
+		pr_debug("EPP pref_index is invalid\n");
+		return -EINVAL;
+	}
+
+	if (epp == -EINVAL)
+		epp = epp_values[pref_index];
+
+	if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		pr_debug("EPP cannot be set under performance policy\n");
+		return -EBUSY;
+	}
+
+	ret = amd_pstate_set_epp(cpudata, epp);
+
+	return ret;
+}
+#endif
 
  static inline int pstate_enable(bool enable)
  {
@@ -71,11 +194,21 @@ static inline int pstate_enable(bool enable)
  static int cppc_enable(bool enable)
  {
  	int cpu, ret = 0;
+	struct cppc_perf_ctrls perf_ctrls;
 
  	for_each_present_cpu(cpu) {
  		ret = cppc_set_enable(cpu, enable);
  		if (ret)
  			return ret;
+
+		/* Enable autonomous mode for EPP */
+		if (!cppc_active) {
+			/* Set desired perf as zero to allow EPP firmware control */
+			perf_ctrls.desired_perf = 0;
+			ret = cppc_set_perf(cpu, &perf_ctrls);
+			if (ret)
+				return ret;
+		}
  	}
 
  	return ret;
@@ -418,7 +551,7 @@ static void amd_pstate_boost_init(struct amd_cpudata *cpudata)
  		return;
 
  	cpudata->boost_supported = true;
-	amd_pstate_driver.boost_enabled = true;
+	default_pstate_driver->boost_enabled = true;
  }
 
  static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
@@ -582,10 +715,62 @@ static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
  	return sprintf(&buf[0], "%u\n", perf);
  }
 

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 727

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+static ssize_t show_energy_performance_available_preferences(
+				struct cpufreq_policy *policy, char *buf)
+{
+	int i = 0;
+	int ret = 0;
+
+	while (energy_perf_strings[i] != NULL)
+		ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
+
+	ret += sprintf(&buf[ret], "\n");

Use sysfs_emit() instead.

Quoted 28 lines
drivers/cpufreq/amd-pstate.c +28 -0
+
+	return ret;
+}
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count)
+{
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;
+
+	ret = sscanf(buf, "%20s", str_preference);
+	if (ret != 1)
+		return -EINVAL;
+
+	ret = match_string(energy_perf_strings, -1, str_preference);
+	if (ret < 0)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_limits_lock);
+	ret = amd_pstate_set_energy_pref_index(cpudata, ret);
+	mutex_unlock(&amd_pstate_limits_lock);
+
+	return ret ?: count;
+}
+
+static ssize_t show_energy_performance_preference(

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 765

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+				struct cpufreq_policy *policy, char *buf)
+{
+	struct amd_cpudata *cpudata = policy->driver_data;
+	int preference;
+
+	preference = amd_pstate_get_energy_pref_index(cpudata);
+	if (preference < 0)
+		return preference;
+
+	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);

Use sysfs_emit() instead.

Quoted 628 lines
drivers/cpufreq/amd-pstate.c +462 -5
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
  cpufreq_freq_attr_ro(amd_pstate_highest_perf);
+cpufreq_freq_attr_rw(energy_performance_preference);
+cpufreq_freq_attr_ro(energy_performance_available_preferences);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -594,6 +779,415 @@ static struct freq_attr *amd_pstate_attr[] = {
  	NULL,
  };
 
+static struct freq_attr *amd_pstate_epp_attr[] = {
+	&amd_pstate_max_freq,
+	&amd_pstate_lowest_nonlinear_freq,
+	&amd_pstate_highest_perf,
+	&energy_performance_preference,
+	&energy_performance_available_preferences,
+	NULL,
+};
+
+static inline void update_boost_state(void)
+{
+	u64 misc_en;
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[0];
+	rdmsrl(MSR_K7_HWCR, misc_en);
+	global_params.cppc_boost_disabled = misc_en & BIT_ULL(25);
+}
+
+static int amd_pstate_init_cpu(unsigned int cpunum)
+{
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[cpunum];
+	if (!cpudata) {
+		cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
+		if (!cpudata)
+			return -ENOMEM;
+		WRITE_ONCE(all_cpu_data[cpunum], cpudata);
+
+		cpudata->cpu = cpunum;
+	}
+	cpudata->epp_powersave = -EINVAL;
+	cpudata->epp_policy = 0;
+	pr_debug("controlling: cpu %d\n", cpunum);
+	return 0;
+}
+
+static int __amd_pstate_cpu_init(struct cpufreq_policy *policy)
+{
+	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
+	struct amd_cpudata *cpudata;
+	struct device *dev;
+	int rc;
+	u64 value;
+
+	rc = amd_pstate_init_cpu(policy->cpu);
+	if (rc)
+		return rc;
+
+	cpudata = all_cpu_data[policy->cpu];
+
+	dev = get_cpu_device(policy->cpu);
+	if (!dev)
+		goto free_cpudata1;
+
+	rc = amd_pstate_init_perf(cpudata);
+	if (rc)
+		goto free_cpudata1;
+
+	min_freq = amd_get_min_freq(cpudata);
+	max_freq = amd_get_max_freq(cpudata);
+	nominal_freq = amd_get_nominal_freq(cpudata);
+	lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
+	if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
+		dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n",
+				min_freq, max_freq);
+		ret = -EINVAL;
+		goto free_cpudata1;
+	}
+
+	policy->min = min_freq;
+	policy->max = max_freq;
+
+	policy->cpuinfo.min_freq = min_freq;
+	policy->cpuinfo.max_freq = max_freq;
+	/* It will be updated by governor */
+	policy->cur = policy->cpuinfo.min_freq;
+
+	/* Initial processor data capability frequencies */
+	cpudata->max_freq = max_freq;
+	cpudata->min_freq = min_freq;
+	cpudata->nominal_freq = nominal_freq;
+	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
+
+	policy->driver_data = cpudata;
+
+	update_boost_state();
+	cpudata->epp_cached = amd_pstate_get_epp(cpudata, value);
+
+	policy->min = policy->cpuinfo.min_freq;
+	policy->max = policy->cpuinfo.max_freq;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC))
+		policy->fast_switch_possible = true;
+
+	if (!shared_mem && boot_cpu_has(X86_FEATURE_CPPC)) {
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_cap1_cached, value);
+	}
+	amd_pstate_boost_init(cpudata);
+
+	return 0;
+
+free_cpudata1:
+	kfree(cpudata);
+	return ret;
+}
+
+static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
+{
+	int ret;
+
+	ret = __amd_pstate_cpu_init(policy);
+	if (ret)
+		return ret;
+	/*
+	 * Set the policy to powersave to provide a valid fallback value in case
+	 * the default cpufreq governor is neither powersave nor performance.
+	 */
+	policy->policy = CPUFREQ_POLICY_POWERSAVE;
+
+	return 0;
+}
+
+static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
+{
+	pr_debug("CPU %d exiting\n", policy->cpu);
+	policy->fast_switch_possible = false;
+	return 0;
+}
+
+static void amd_pstate_update_max_freq(unsigned int cpu)
+{
+	struct cpufreq_policy *policy = policy = cpufreq_cpu_get(cpu);
+
+	if (!policy)
+		return;
+
+	refresh_frequency_limits(policy);
+	cpufreq_cpu_put(policy);
+}
+
+static void amd_pstate_epp_update_limits(unsigned int cpu)
+{
+	mutex_lock(&amd_pstate_driver_lock);
+	update_boost_state();
+	if (global_params.cppc_boost_disabled) {
+		for_each_possible_cpu(cpu)
+			amd_pstate_update_max_freq(cpu);
+	} else {
+		cpufreq_update_policy(cpu);
+	}
+	mutex_unlock(&amd_pstate_driver_lock);
+}
+
+static int cppc_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
+
+static inline void amd_pstate_boost_up(struct amd_cpudata *cpudata)
+{
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u32 max_limit = (hwp_req & 0xff);
+	u32 min_limit = (hwp_req & 0xff00) >> 8;
+	u32 boost_level1;
+
+	/* If max and min are equal or already at max, nothing to boost */
+	if (max_limit == min_limit)
+		return;
+
+	/* Set boost max and min to initial value */
+	if (!cpudata->cppc_boost_min)
+		cpudata->cppc_boost_min = min_limit;
+
+	boost_level1 = ((AMD_CPPC_NOMINAL_PERF(hwp_cap) + min_limit) >> 1);
+
+	if (cpudata->cppc_boost_min < boost_level1)
+		cpudata->cppc_boost_min = boost_level1;
+	else if (cpudata->cppc_boost_min < AMD_CPPC_NOMINAL_PERF(hwp_cap))
+		cpudata->cppc_boost_min = AMD_CPPC_NOMINAL_PERF(hwp_cap);
+	else if (cpudata->cppc_boost_min == AMD_CPPC_NOMINAL_PERF(hwp_cap))
+		cpudata->cppc_boost_min = max_limit;
+	else
+		return;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(cpudata->cppc_boost_min);
+	wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req);
+	cpudata->last_update = cpudata->sample.time;
+}
+
+static inline void amd_pstate_boost_down(struct amd_cpudata *cpudata)
+{
+	bool expired;
+
+	if (cpudata->cppc_boost_min) {
+		expired = time_after64(cpudata->sample.time, cpudata->last_update +
+					cppc_boost_hold_time_ns);
+
+		if (expired) {
+			wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
+						cpudata->cppc_req_cached);
+			cpudata->cppc_boost_min = 0;
+		}
+	}
+
+	cpudata->last_update = cpudata->sample.time;
+}
+
+static inline void amd_pstate_boost_update_util(struct amd_cpudata *cpudata,
+						      u64 time)
+{
+	cpudata->sample.time = time;
+	if (smp_processor_id() != cpudata->cpu)
+		return;
+
+	if (cpudata->sched_flags & SCHED_CPUFREQ_IOWAIT) {
+		bool do_io = false;
+
+		cpudata->sched_flags = 0;
+		/*
+		 * Set iowait_boost flag and update time. Since IO WAIT flag
+		 * is set all the time, we can't just conclude that there is
+		 * some IO bound activity is scheduled on this CPU with just
+		 * one occurrence. If we receive at least two in two
+		 * consecutive ticks, then we treat as boost candidate.
+		 * This is leveraged from Intel Pstate driver.
+		 */
+		if (time_before64(time, cpudata->last_io_update + 2 * TICK_NSEC))
+			do_io = true;
+
+		cpudata->last_io_update = time;
+
+		if (do_io)
+			amd_pstate_boost_up(cpudata);
+
+	} else {
+		amd_pstate_boost_down(cpudata);
+	}
+}
+
+static inline void amd_pstate_cppc_update_hook(struct update_util_data *data,
+						u64 time, unsigned int flags)
+{
+	struct amd_cpudata *cpudata = container_of(data,
+				struct amd_cpudata, update_util);
+
+	cpudata->sched_flags |= flags;
+
+	if (smp_processor_id() == cpudata->cpu)
+		amd_pstate_boost_update_util(cpudata, time);
+}
+
+static void amd_pstate_clear_update_util_hook(unsigned int cpu)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+
+	if (!cpudata->update_util_set)
+		return;
+
+	cpufreq_remove_update_util_hook(cpu);
+	cpudata->update_util_set = false;
+	synchronize_rcu();
+}
+
+static void amd_pstate_set_update_util_hook(unsigned int cpu_num)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu_num];
+
+	if (!cppc_boost) {
+		if (cpudata->update_util_set)
+			amd_pstate_clear_update_util_hook(cpudata->cpu);
+		return;
+	}
+
+	if (cpudata->update_util_set)
+		return;
+
+	cpudata->sample.time = 0;
+	cpufreq_add_update_util_hook(cpu_num, &cpudata->update_util,
+						amd_pstate_cppc_update_hook);
+	cpudata->update_util_set = true;
+}
+
+static void amd_pstate_epp_init(unsigned int cpu)
+{
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+	u32 max_perf, min_perf;
+	u64 value;
+	s16 epp;
+	int ret;
+
+	max_perf = READ_ONCE(cpudata->highest_perf);
+	min_perf = READ_ONCE(cpudata->lowest_perf);
+
+	value = READ_ONCE(cpudata->cppc_req_cached);
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
+		min_perf = max_perf;
+
+	/* Initial min/max values for CPPC Performance Controls Register */
+	value &= ~AMD_CPPC_MIN_PERF(~0L);
+	value |= AMD_CPPC_MIN_PERF(min_perf);
+
+	value &= ~AMD_CPPC_MAX_PERF(~0L);
+	value |= AMD_CPPC_MAX_PERF(max_perf);
+
+	/* CPPC EPP feature require to set zero to the desire perf bit */
+	value &= ~AMD_CPPC_DES_PERF(~0L);
+	value |= AMD_CPPC_DES_PERF(0);
+
+	if (cpudata->epp_policy == cpudata->policy)
+		goto skip_epp;
+
+	cpudata->epp_policy = cpudata->policy;
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		epp = amd_pstate_get_epp(cpudata, value);
+		cpudata->epp_powersave = epp;
+		if (epp < 0)
+			goto skip_epp;
+		/* force the epp value to be zero for performance policy */
+		epp = 0;
+	} else {
+		if (cpudata->epp_powersave < 0)
+			goto skip_epp;
+		/* Get BIOS pre-defined epp value */
+		epp = amd_pstate_get_epp(cpudata, value);
+		if (epp)
+			goto skip_epp;
+		epp = cpudata->epp_powersave;
+	}
+	/* Set initial EPP value */
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+	}
+
+skip_epp:
+	WRITE_ONCE(cpudata->cppc_req_cached, value);
+	ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	if (!ret)
+		cpudata->epp_cached = epp;
+}
+
+static void amd_pstate_set_max_limits(struct amd_cpudata *cpudata)
+{
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u32 max_limit = (hwp_cap >> 24) & 0xff;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(max_limit);
+	wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req);
+}
+
+static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
+{
+	struct amd_cpudata *cpudata;
+
+	if (!policy->cpuinfo.max_freq)
+		return -ENODEV;
+
+	pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
+				policy->cpuinfo.max_freq, policy->max);
+
+	cpudata = all_cpu_data[policy->cpu];
+	cpudata->policy = policy->policy;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		mutex_lock(&amd_pstate_limits_lock);
+
+		if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+			amd_pstate_clear_update_util_hook(policy->cpu);
+			amd_pstate_set_max_limits(cpudata);
+		} else {
+			amd_pstate_set_update_util_hook(policy->cpu);
+		}
+
+		if (boot_cpu_has(X86_FEATURE_CPPC))
+			amd_pstate_epp_init(policy->cpu);
+
+		mutex_unlock(&amd_pstate_limits_lock);
+	}
+
+	return 0;
+}
+
+static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
+					   struct cpufreq_policy_data *policy)
+{
+	update_boost_state();
+	cpufreq_verify_within_cpu_limits(policy);
+}
+
+static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
+{
+	amd_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
+	pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
+	return 0;
+}
+
  static struct cpufreq_driver amd_pstate_driver = {
  	.flags		= CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
  	.verify		= amd_pstate_verify,
@@ -607,8 +1201,20 @@ static struct cpufreq_driver amd_pstate_driver = {
  	.attr		= amd_pstate_attr,
  };
 
+static struct cpufreq_driver amd_pstate_epp_driver = {
+	.flags		= CPUFREQ_CONST_LOOPS,
+	.verify		= amd_pstate_epp_verify_policy,
+	.setpolicy	= amd_pstate_epp_set_policy,
+	.init		= amd_pstate_epp_cpu_init,
+	.exit		= amd_pstate_epp_cpu_exit,
+	.update_limits	= amd_pstate_epp_update_limits,
+	.name		= "amd_pstate_epp",
+	.attr		= amd_pstate_epp_attr,
+};
+
  static int __init amd_pstate_init(void)
  {
+	static struct amd_cpudata **cpudata;
  	int ret;
 
  	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
@@ -623,19 +1229,37 @@ static int __init amd_pstate_init(void)
  	if (cpufreq_get_current_driver())
  		return -EEXIST;
 
+	/* set the pstate driver disabled by default if there
+	 * is no early parameters set
+	 */
+	if (!cppc_load) {
+		pr_debug("disabled to load\n");
+		return -ENODEV;
+	}
+
+	if (!epp_off) {
+		WRITE_ONCE(cppc_active, 1);
+		if (!default_pstate_driver)
+			default_pstate_driver = &amd_pstate_epp_driver;
+	}
+
  	/* capability check */
  	if (boot_cpu_has(X86_FEATURE_CPPC)) {
-		pr_debug("AMD CPPC MSR based functionality is supported\n");
-		amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf;
+		if (!cppc_active)
+			default_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
  	} else if (shared_mem) {
  		static_call_update(amd_pstate_enable, cppc_enable);
  		static_call_update(amd_pstate_init_perf, cppc_init_perf);
  		static_call_update(amd_pstate_update_perf, cppc_update_perf);
  	} else {
-		pr_info("This processor supports shared memory solution, you can enable it with amd_pstate.shared_mem=1\n");
+		pr_info("This processor supports shared memory solution, enable it with amd_pstate=legacy_cppc\n");
  		return -ENODEV;
  	}
 
+	cpudata = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
+	if (!cpudata)
+		return -ENOMEM;
+	WRITE_ONCE(all_cpu_data, cpudata);
  	/* enable amd pstate feature */
  	ret = amd_pstate_enable(true);
  	if (ret) {
@@ -643,9 +1267,9 @@ static int __init amd_pstate_init(void)
  		return ret;
  	}
 
-	ret = cpufreq_register_driver(&amd_pstate_driver);
+	ret = cpufreq_register_driver(default_pstate_driver);
  	if (ret)
-		pr_err("failed to register amd_pstate_driver with return %d\n",
+		pr_err("failed to register amd pstate driver with return %d\n",
  		       ret);
 
  	return ret;
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
  	if (!str)
  		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
+	}
+
  	/*
  	 * support shared memory type CPPC which has no MSR function.
  	 * enable amd-pstate on processors with shared memory solution
include/linux/amd-pstate.h +81 -0
index 1c4b8659f171..7e6e8cab97b3 100644
@@ -25,6 +25,7 @@ struct amd_aperf_mperf {
  	u64 aperf;
  	u64 mperf;
  	u64 tsc;
+	u64 time;
  };
 
  /**
@@ -47,6 +48,18 @@ struct amd_aperf_mperf {
   * @prev: Last Aperf/Mperf/tsc count value read from register
   * @freq: current cpu frequency value
   * @boost_supported: check whether the Processor or SBIOS supports boost mode
+ * @epp_powersave: Last saved CPPC energy performance preference
+				when policy switched to performance
+ * @epp_policy: Last saved policy used to set energy-performance preference
+ * @epp_cached: Cached CPPC energy-performance preference value
+ * @policy: Cpufreq policy value
+ * @sched_flags: Store scheduler flags for possible cross CPU update
+ * @update_util_set: CPUFreq utility callback is set
+ * @last_update: Time stamp of the last performance state update
+ * @cppc_boost_min: Last CPPC boosted min performance state
+ * @cppc_cap1_cached: Cached value of the last CPPC Capabilities MSR
+ * @update_util: Cpufreq utility callback information
+ * @sample: the stored performance sample
   *
   * The amd_cpudata is key private data for each CPU thread in AMD P-State, and
   * represents all the attributes and goals that AMD P-State requests at runtime.
@@ -72,6 +85,74 @@ struct amd_cpudata {
 
  	u64	freq;
  	bool	boost_supported;
+
+	/* EPP feature related attributes*/
+	s16	epp_powersave;
+	s16	epp_policy;
+	s16	epp_cached;
+	u32	policy;
+	u32	sched_flags;
+	bool	update_util_set;
+	u64	last_update;
+	u64	last_io_update;
+	u32	cppc_boost_min;
+	u64	cppc_cap1_cached;
+	struct	update_util_data update_util;
+	struct	amd_aperf_mperf sample;
+};
+
+/**
+ * struct amd_pstate_params - global parameters for the performance control
+ * @ cppc_boost_disabled wheher the core performance boost disabled
+ */
+struct amd_pstate_params {
+	bool cppc_boost_disabled;
+};
+
+#define AMD_CPPC_EPP_PERFORMANCE		0x00
+#define AMD_CPPC_EPP_BALANCE_PERFORMANCE	0x80
+#define AMD_CPPC_EPP_BALANCE_POWERSAVE		0xBF
+#define AMD_CPPC_EPP_POWERSAVE			0xFF
+
+/*
+ * AMD Energy Preference Performance (EPP)
+ * The EPP is used in the CCLK DPM controller to drive
+ * the frequency that a core is going to operate during
+ * short periods of activity. EPP values will be utilized for
+ * different OS profiles (balanced, performance, power savings)
+ * display strings corresponding to EPP index in the
+ * energy_perf_strings[]
+ *	index		String
+ *-------------------------------------
+ *	0		default
+ *	1		performance
+ *	2		balance_performance
+ *	3		balance_power
+ *	4		power
+ */
+enum energy_perf_value_index {
+	EPP_INDEX_DEFAULT = 0,
+	EPP_INDEX_PERFORMANCE,
+	EPP_INDEX_BALANCE_PERFORMANCE,
+	EPP_INDEX_BALANCE_POWERSAVE,
+	EPP_INDEX_POWERSAVE,
+};
+
+static const char * const energy_perf_strings[] = {
+	[EPP_INDEX_DEFAULT] = "default",
+	[EPP_INDEX_PERFORMANCE] = "performance",
+	[EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
+	[EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
+	[EPP_INDEX_POWERSAVE] = "power",
+	NULL
+};
+
+static unsigned int epp_values[] = {
+	[EPP_INDEX_DEFAULT] = 0,
+	[EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
+	[EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
+	[EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
+	[EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
  };
 
  #endif /* _LINUX_AMD_PSTATE_H */
#4 lore raw

Re: [PATCH v4 3/9] cpufreq: amd-pstate: change amd-pstate driver to be built-in type

In reply to Perry Yuan
Quoted 7 lines · Perry Yuan

Change the amd-pstate driver as the built-in type which can help to load the driver before the acpi_cpufreq driver as the default pstate driver for the AMD processors.

for the processors do not have the dedicated MSR functions, add amd-pstate=legacy_cppc to grub which enable shared memory interface to communicate with cppc_acpi module to control pstate hints.

Did you sync with Wyes already as I had suggested? Was this the outcome?

I was a bit surprised to see this still as legacy_cppc when reviewing v4.

Quoted 54 lines
---
  drivers/cpufreq/Kconfig.x86  |  2 +-
  drivers/cpufreq/amd-pstate.c | 25 +++++++++++++++----------
  2 files changed, 16 insertions(+), 11 deletions(-)
drivers/cpufreq/Kconfig.x86 +1 -1
index 310779b07daf..00476e94db90 100644
@@ -35,7 +35,7 @@ config X86_PCC_CPUFREQ
  	  If in doubt, say N.
 
  config X86_AMD_PSTATE
-	tristate "AMD Processor P-State driver"
+	bool "AMD Processor P-State driver"
  	depends on X86 && ACPI
  	select ACPI_PROCESSOR
  	select ACPI_CPPC_LIB if X86_64
drivers/cpufreq/amd-pstate.c +8 -8
index ace7d50cf2ac..85a0b3fb56c2 100644
@@ -59,10 +59,7 @@
   * we disable it by default to go acpi-cpufreq on these processors and add a
   * module parameter to be able to enable it manually for debugging.
   */
-static bool shared_mem = false;
-module_param(shared_mem, bool, 0444);
-MODULE_PARM_DESC(shared_mem,
-		 "enable amd-pstate on processors with shared memory solution (false = disabled (default), true = enabled)");
+static bool shared_mem __read_mostly;
 
  static struct cpufreq_driver amd_pstate_driver;
 
@@ -653,16 +650,24 @@ static int __init amd_pstate_init(void)
 
  	return ret;
  }
+device_initcall(amd_pstate_init);
 
-static void __exit amd_pstate_exit(void)
+static int __init amd_pstate_param(char *str)
  {
-	cpufreq_unregister_driver(&amd_pstate_driver);
+	if (!str)
+		return -EINVAL;
 
-	amd_pstate_enable(false);
-}
+	/*
+	 * support shared memory type CPPC which has no MSR function.
+	 * enable amd-pstate on processors with shared memory solution

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 670

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7 -2
+	 * (amd-pstate=legacy_cppc enabled), it is disabled by default.
+	 */
+	if (!strcmp(str, "legacy_cppc"))
+		shared_mem = true;
 
-module_init(amd_pstate_init);
-module_exit(amd_pstate_exit);
+	return 0;
+}
+early_param("amd-pstate", amd_pstate_param);

Documentation/kernel-parameters.txt needs to be updated for this early parameter support.

Quoted 3 lines
drivers/cpufreq/amd-pstate.c +0 -0
  MODULE_AUTHOR("Huang Rui <[email protected]>");
  MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
author reviewer
#5 lore raw

RE: [PATCH v4 3/9] cpufreq: amd-pstate: change amd-pstate driver to be built-in type

[AMD Official Use Only - General]

Hi Mario.

Quoted 19 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:46 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v4 3/9] cpufreq: amd-pstate: change amd-pstate driver to be built-in type

Quoted 4 lines · Perry Yuan

Change the amd-pstate driver as the built-in type which can help to load the driver before the acpi_cpufreq driver as the default pstate driver for the AMD processors.

Quoted 7 lines
Quoted 3 lines

for the processors do not have the dedicated MSR functions, add amd-pstate=legacy_cppc to grub which enable shared memory interface to communicate with cppc_acpi module to control pstate hints.

Did you sync with Wyes already as I had suggested? Was this the outcome?

I was a bit surprised to see this still as legacy_cppc when reviewing v4.

The discussion we made will be in V5 soon.
I am working on it.

Quoted 62 lines
Quoted 26 lines
---
  drivers/cpufreq/Kconfig.x86  |  2 +-
  drivers/cpufreq/amd-pstate.c | 25 +++++++++++++++----------
  2 files changed, 16 insertions(+), 11 deletions(-)
drivers/cpufreq/Kconfig.x86 +1 -1
index 310779b07daf..00476e94db90 100644
@@ -35,7 +35,7 @@ config X86_PCC_CPUFREQ
  	  If in doubt, say N.
 
  config X86_AMD_PSTATE
-	tristate "AMD Processor P-State driver"
+	bool "AMD Processor P-State driver"
  	depends on X86 && ACPI
  	select ACPI_PROCESSOR
  	select ACPI_CPPC_LIB if X86_64
(quoted diff) +0 -0

b/drivers/cpufreq/amd-pstate.c index ace7d50cf2ac..85a0b3fb56c2 100644

drivers/cpufreq/amd-pstate.c line 59

Quoted 1 line
drivers/cpufreq/amd-pstate.c +0 -0
@@ -59,10 +59,7 @@
   * we disable it by default to go acpi-cpufreq on these processors and add

a

drivers/cpufreq/amd-pstate.c

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -2
   * module parameter to be able to enable it manually for debugging.
   */
-static bool shared_mem = false;
-module_param(shared_mem, bool, 0444); -

MODULE_PARM_DESC(shared_mem,

drivers/cpufreq/amd-pstate.c line 64

Quoted 1 line
drivers/cpufreq/amd-pstate.c +0 -1
-		 "enable amd-pstate on processors with shared memory

solution (false = disabled (default), true = enabled)");

Quoted 27 lines
drivers/cpufreq/amd-pstate.c +12 -4
+static bool shared_mem __read_mostly;
 
  static struct cpufreq_driver amd_pstate_driver;
 
@@ -653,16 +650,24 @@ static int __init amd_pstate_init(void)
 
  	return ret;
  }
+device_initcall(amd_pstate_init);
 
-static void __exit amd_pstate_exit(void)
+static int __init amd_pstate_param(char *str)
  {
-	cpufreq_unregister_driver(&amd_pstate_driver);
+	if (!str)
+		return -EINVAL;
 
-	amd_pstate_enable(false);
-}
+	/*
+	 * support shared memory type CPPC which has no MSR function.
+	 * enable amd-pstate on processors with shared memory solution
+	 * (amd-pstate=legacy_cppc enabled), it is disabled by default.
+	 */
+	if (!strcmp(str, "legacy_cppc"))
+		shared_mem = true;
 
Quoted 8 lines

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 670

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +3 -2
-module_init(amd_pstate_init);
-module_exit(amd_pstate_exit);
+	return 0;
+}
+early_param("amd-pstate", amd_pstate_param);

Documentation/kernel-parameters.txt needs to be updated for this early parameter support.

Yeah, that will also be in V5 together.

Quoted 4 lines
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -0
  MODULE_AUTHOR("Huang Rui <[email protected]>");
  MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");
#10 lore raw

Re: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

In reply to Perry Yuan

Hi Perry,

On 11/10/2022 11:28 PM, Perry Yuan wrote:
[snip]

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 738

Quoted 8 lines
drivers/cpufreq/amd-pstate.c +8 -0
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count)
+{
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;

Here epp is a unused variable.
[snip]

Quoted 11 lines
drivers/cpufreq/amd-pstate.c +7 -0
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
 	if (!str)
 		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_param(char *str) · line 1300

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7 -0
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
+	}
+
 	/*
 	 * support shared memory type CPPC which has no MSR function.
 	 * enable amd-pstate on processors with shared memory solution

In the shared memory support section also cppc_load has to be set otherwise amd_pstate=legacy_cppc will not work.

if (!strcmp(str, "legacy_cppc")) {
	shared_mem = true;
	cppc_load = 1;
}
Signature · 2 lines
Thanks & Regards,
Wyes
author reviewer
#11 lore raw

RE: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

[AMD Official Use Only - General]

Quoted 45 lines

-----Original Message-----
From: Karny, Wyes <[email protected]>
Sent: Thursday, November 17, 2022 10:03 PM
To: Yuan, Perry <[email protected]>; [email protected]; Limonciello, Mario <[email protected]>; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; [email protected]; linux- [email protected]
Subject: Re: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

Hi Perry,

On 11/10/2022 11:28 PM, Perry Yuan wrote:
[snip]

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 738

Quoted 7 lines
drivers/cpufreq/amd-pstate.c +7 -0
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count) {
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;

Here epp is a unused variable.
[snip]

Quoted 16 lines
drivers/cpufreq/amd-pstate.c +12 -0
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
 	if (!str)
 		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
Quoted 8 lines

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_param(char *str) · line 1300

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +2 -0
+	}
+
 	/*
 	 * support shared memory type CPPC which has no MSR function.
 	 * enable amd-pstate on processors with shared memory solution

In the shared memory support section also cppc_load has to be set otherwise amd_pstate=legacy_cppc will not work.

If the patches just got reviewed are accepted, the legacy_cppcc will not be needed any more. I will rebase the EPP patches based on the that.

Quoted 5 lines
if (!strcmp(str, "legacy_cppc")) {
	shared_mem = true;
	cppc_load = 1;
}

Previous plan was to add two parameters to kernel command line, passive or active and legacy_cppc for shared memory platform.

Quoted 5 lines

--
Thanks & Regards,
Wyes

author reviewer
#8 lore raw

RE: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

In reply to Limonciello, Mario

[AMD Official Use Only - General]

Quoted 40 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:41 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; linux- [email protected]; [email protected]
Subject: Re: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

Quoted 18 lines · Perry Yuan

Add EPP driver support for AMD SoCs which support a dedicated MSR for CPPC. EPP is used by the DPM controller to configure the frequency that a core operates at during short periods of activity.

The SoC EPP targets are configured on a scale from 0 to 255 where 0 represents maximum performance and 255 represents maximum efficiency.

The amd-pstate driver exports profile string names to userspace that are tied to specific EPP values.

The balance_performance string (0x80) provides the best balance for efficiency versus power on most systems, but users can choose other strings to meet their needs as well.

$ cat
/sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_p
references default performance balance_performance balance_power power
Quoted 7 lines
$ cat
/sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference
balance_performance
---
  drivers/cpufreq/amd-pstate.c | 652
Quoted 8 lines

++++++++++++++++++++++++++++++++++-

Quoted 3 lines
  include/linux/amd-pstate.h   |  81 +++++
  2 files changed, 726 insertions(+), 7 deletions(-)

Did you see Rafael's comment about reducing the common code w/ intel-pstate for future maintainability?

I am sure that epp set/get functions are not compatible on intel and amd drivers. But the macro definitions EPP profiles can surely consider to make it as common as possible. I make this change in the V5 version.

Quoted 6 lines

As some of it is duplicated, I think it should be possible to make a library source file that both can use and pull from. If you split out the common code in an earlier patch I think it would shrink this patch significantly too.

If you already looked into this and it doesn't /really/ change much, please comment also.

amd_pstate_get_epp and amd_pstate_set_epp have some shared memory platform interfaces support on AMD driver, I am afraid of that we cannot make it as common code.

Quoted 864 lines
Quoted 24 lines
(quoted diff) +0 -0

b/drivers/cpufreq/amd-pstate.c index 85a0b3fb56c2..7b3a12772582 100644

drivers/cpufreq/amd-pstate.c +13 -1
@@ -60,8 +60,131 @@
   * module parameter to be able to enable it manually for debugging.
   */
  static bool shared_mem __read_mostly;
+static int cppc_active __read_mostly; static int cppc_load
+__initdata; static int epp_off __initdata;
 
-static struct cpufreq_driver amd_pstate_driver;
+static struct cpufreq_driver *default_pstate_driver; static struct
+amd_cpudata **all_cpu_data;
+
+static struct amd_pstate_params global_params;
+
+static DEFINE_MUTEX(amd_pstate_limits_lock);
+static DEFINE_MUTEX(amd_pstate_driver_lock);
+
+static bool cppc_boost __read_mostly; struct kobject
+*amd_pstate_kobj;
+

drivers/cpufreq/amd-pstate.c line 86

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+#ifdef CONFIG_ACPI_CPPC_LIB
+static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64
+cppc_req_cached) {
+	s16 epp;
+	struct cppc_perf_caps perf_caps;
+	int ret;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		if (!cppc_req_cached) {
+			epp = rdmsrl_on_cpu(cpudata->cpu,

MSR_AMD_CPPC_REQ,

drivers/cpufreq/amd-pstate.c

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +9 -0
+					&cppc_req_cached);
+			if (epp)
+				return epp;
+		}
+		epp = (cppc_req_cached >> 24) & 0xFF;
+	} else {
+		ret = cppc_get_epp_caps(cpudata->cpu, &perf_caps);
+		if (ret < 0) {
+			pr_debug("Could not retrieve energy perf value (%d)\n",

ret);

Quoted 41 lines
drivers/cpufreq/amd-pstate.c +41 -0
+			return -EIO;
+		}
+		epp = (s16) perf_caps.energy_perf;
+	}
+
+	return epp;
+}
+#endif
+
+static int amd_pstate_get_energy_pref_index(struct amd_cpudata
+*cpudata) {
+	s16 epp;
+	int index = -EINVAL;
+
+	epp = amd_pstate_get_epp(cpudata, 0);
+	if (epp < 0)
+		return epp;
+
+	switch (epp) {
+		case AMD_CPPC_EPP_PERFORMANCE:
+			index = EPP_INDEX_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
+			index = EPP_INDEX_BALANCE_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_POWERSAVE:
+			index = EPP_INDEX_BALANCE_POWERSAVE;
+			break;
+		case AMD_CPPC_EPP_POWERSAVE:
+			index = EPP_INDEX_POWERSAVE;
+			break;
+		default:
+			break;
+	}
+
+	return index;
+}
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp) {
+	int ret;

drivers/cpufreq/amd-pstate.c

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	struct cppc_perf_ctrls perf_ctrls;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		u64 value = READ_ONCE(cpudata->cppc_req_cached);
+
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,

value);

Quoted 46 lines
drivers/cpufreq/amd-pstate.c +39 -0
+		if (!ret)
+			cpudata->epp_cached = epp;
+	} else {
+		perf_ctrls.energy_perf = epp;
+		ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
+		if (ret) {
+			pr_debug("failed to set energy perf value (%d)\n", ret);
+			return ret;
+		}
+		cpudata->epp_cached = epp;
+	}
+
+	return ret;
+}
+
+static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
+		int pref_index)
+{
+	int epp = -EINVAL;
+	int ret;
+
+	if (!pref_index) {
+		pr_debug("EPP pref_index is invalid\n");
+		return -EINVAL;
+	}
+
+	if (epp == -EINVAL)
+		epp = epp_values[pref_index];
+
+	if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		pr_debug("EPP cannot be set under performance policy\n");
+		return -EBUSY;
+	}
+
+	ret = amd_pstate_set_epp(cpudata, epp);
+
+	return ret;
+}
+#endif
 
  static inline int pstate_enable(bool enable)
  {
@@ -71,11 +194,21 @@ static inline int pstate_enable(bool enable)
  static int cppc_enable(bool enable)
  {
  	int cpu, ret = 0;

drivers/cpufreq/amd-pstate.c static inline int pstate_enable(bool enable) · line 206

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +5 -0
+	struct cppc_perf_ctrls perf_ctrls;
 
  	for_each_present_cpu(cpu) {
  		ret = cppc_set_enable(cpu, enable);
  		if (ret)
  			return ret;
+
+		/* Enable autonomous mode for EPP */
+		if (!cppc_active) {
+			/* Set desired perf as zero to allow EPP firmware

control */

drivers/cpufreq/amd-pstate.c static void amd_pstate_boost_init(struct

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +5 -0
+			perf_ctrls.desired_perf = 0;
+			ret = cppc_set_perf(cpu, &perf_ctrls);
+			if (ret)
+				return ret;
+		}
  	}
 
  	return ret;
@@ -418,7 +551,7 @@ static void amd_pstate_boost_init(struct

amd_cpudata *cpudata)

drivers/cpufreq/amd-pstate.c static void amd_pstate_boost_init(struct amd_cpudata *cpudata)

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +1 -2
  		return;
 
  	cpudata->boost_supported = true;
-	amd_pstate_driver.boost_enabled = true;
+	default_pstate_driver->boost_enabled = true;
  }
 
  static int amd_pstate_cpu_init(struct cpufreq_policy *policy) @@
-582,10 +715,62 @@ static ssize_t show_amd_pstate_highest_perf(struct

cpufreq_policy *policy,

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 727

Quoted 12 lines
  	return sprintf(&buf[0], "%u\n", perf);
  }
drivers/cpufreq/amd-pstate.c +9 -0
+static ssize_t show_energy_performance_available_preferences(
+				struct cpufreq_policy *policy, char *buf) {
+	int i = 0;
+	int ret = 0;
+
+	while (energy_perf_strings[i] != NULL)
+		ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
+
+	ret += sprintf(&buf[ret], "\n");

Use sysfs_emit() instead.

Quoted 26 lines
drivers/cpufreq/amd-pstate.c +26 -0
+
+	return ret;
+}
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count) {
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;
+
+	ret = sscanf(buf, "%20s", str_preference);
+	if (ret != 1)
+		return -EINVAL;
+
+	ret = match_string(energy_perf_strings, -1, str_preference);
+	if (ret < 0)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_limits_lock);
+	ret = amd_pstate_set_energy_pref_index(cpudata, ret);
+	mutex_unlock(&amd_pstate_limits_lock);
+
+	return ret ?: count;
+}
+

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 765

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+static ssize_t show_energy_performance_preference(
+				struct cpufreq_policy *policy, char *buf) {
+	struct amd_cpudata *cpudata = policy->driver_data;
+	int preference;
+
+	preference = amd_pstate_get_energy_pref_index(cpudata);
+	if (preference < 0)
+		return preference;
+
+	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);

Use sysfs_emit() instead.

Quoted 67 lines
drivers/cpufreq/amd-pstate.c +56 -0
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
  cpufreq_freq_attr_ro(amd_pstate_highest_perf);
+cpufreq_freq_attr_rw(energy_performance_preference);
+cpufreq_freq_attr_ro(energy_performance_available_preferences);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -594,6 +779,415 @@ static struct freq_attr *amd_pstate_attr[] = {
  	NULL,
  };
 
+static struct freq_attr *amd_pstate_epp_attr[] = {
+	&amd_pstate_max_freq,
+	&amd_pstate_lowest_nonlinear_freq,
+	&amd_pstate_highest_perf,
+	&energy_performance_preference,
+	&energy_performance_available_preferences,
+	NULL,
+};
+
+static inline void update_boost_state(void) {
+	u64 misc_en;
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[0];
+	rdmsrl(MSR_K7_HWCR, misc_en);
+	global_params.cppc_boost_disabled = misc_en & BIT_ULL(25); }
+
+static int amd_pstate_init_cpu(unsigned int cpunum) {
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[cpunum];
+	if (!cpudata) {
+		cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
+		if (!cpudata)
+			return -ENOMEM;
+		WRITE_ONCE(all_cpu_data[cpunum], cpudata);
+
+		cpudata->cpu = cpunum;
+	}
+	cpudata->epp_powersave = -EINVAL;
+	cpudata->epp_policy = 0;
+	pr_debug("controlling: cpu %d\n", cpunum);
+	return 0;
+}
+
+static int __amd_pstate_cpu_init(struct cpufreq_policy *policy) {
+	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
+	struct amd_cpudata *cpudata;
+	struct device *dev;
+	int rc;
+	u64 value;
+
+	rc = amd_pstate_init_cpu(policy->cpu);
+	if (rc)
+		return rc;
+
+	cpudata = all_cpu_data[policy->cpu];
+
+	dev = get_cpu_device(policy->cpu);
+	if (!dev)
+		goto free_cpudata1;
+

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 843

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	rc = amd_pstate_init_perf(cpudata);
+	if (rc)
+		goto free_cpudata1;
+
+	min_freq = amd_get_min_freq(cpudata);
+	max_freq = amd_get_max_freq(cpudata);
+	nominal_freq = amd_get_nominal_freq(cpudata);
+	lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
+	if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
+		dev_err(dev, "min_freq(%d) or max_freq(%d) value is

incorrect\n",

Quoted 22 lines
drivers/cpufreq/amd-pstate.c +22 -0
+				min_freq, max_freq);
+		ret = -EINVAL;
+		goto free_cpudata1;
+	}
+
+	policy->min = min_freq;
+	policy->max = max_freq;
+
+	policy->cpuinfo.min_freq = min_freq;
+	policy->cpuinfo.max_freq = max_freq;
+	/* It will be updated by governor */
+	policy->cur = policy->cpuinfo.min_freq;
+
+	/* Initial processor data capability frequencies */
+	cpudata->max_freq = max_freq;
+	cpudata->min_freq = min_freq;
+	cpudata->nominal_freq = nominal_freq;
+	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
+
+	policy->driver_data = cpudata;
+
+	update_boost_state();

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	cpudata->epp_cached = amd_pstate_get_epp(cpudata, value);
+
+	policy->min = policy->cpuinfo.min_freq;
+	policy->max = policy->cpuinfo.max_freq;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC))
+		policy->fast_switch_possible = true;
+
+	if (!shared_mem && boot_cpu_has(X86_FEATURE_CPPC)) {
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,

&value);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +5 -0
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,

&value);

Quoted 64 lines
drivers/cpufreq/amd-pstate.c +64 -0
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_cap1_cached, value);
+	}
+	amd_pstate_boost_init(cpudata);
+
+	return 0;
+
+free_cpudata1:
+	kfree(cpudata);
+	return ret;
+}
+
+static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy) {
+	int ret;
+
+	ret = __amd_pstate_cpu_init(policy);
+	if (ret)
+		return ret;
+	/*
+	 * Set the policy to powersave to provide a valid fallback value in case
+	 * the default cpufreq governor is neither powersave nor performance.
+	 */
+	policy->policy = CPUFREQ_POLICY_POWERSAVE;
+
+	return 0;
+}
+
+static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy) {
+	pr_debug("CPU %d exiting\n", policy->cpu);
+	policy->fast_switch_possible = false;
+	return 0;
+}
+
+static void amd_pstate_update_max_freq(unsigned int cpu) {
+	struct cpufreq_policy *policy = policy = cpufreq_cpu_get(cpu);
+
+	if (!policy)
+		return;
+
+	refresh_frequency_limits(policy);
+	cpufreq_cpu_put(policy);
+}
+
+static void amd_pstate_epp_update_limits(unsigned int cpu) {
+	mutex_lock(&amd_pstate_driver_lock);
+	update_boost_state();
+	if (global_params.cppc_boost_disabled) {
+		for_each_possible_cpu(cpu)
+			amd_pstate_update_max_freq(cpu);
+	} else {
+		cpufreq_update_policy(cpu);
+	}
+	mutex_unlock(&amd_pstate_driver_lock);
+}
+
+static int cppc_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
+
+static inline void amd_pstate_boost_up(struct amd_cpudata *cpudata) {
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u32 max_limit = (hwp_req & 0xff);
+	u32 min_limit = (hwp_req & 0xff00) >> 8;
+	u32 boost_level1;

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+
+	/* If max and min are equal or already at max, nothing to boost */
+	if (max_limit == min_limit)
+		return;
+
+	/* Set boost max and min to initial value */
+	if (!cpudata->cppc_boost_min)
+		cpudata->cppc_boost_min = min_limit;
+
+	boost_level1 = ((AMD_CPPC_NOMINAL_PERF(hwp_cap) + min_limit) >>

1);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +4 -0
+
+	if (cpudata->cppc_boost_min < boost_level1)
+		cpudata->cppc_boost_min = boost_level1;
+	else if (cpudata->cppc_boost_min <

AMD_CPPC_NOMINAL_PERF(hwp_cap))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 959

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+		cpudata->cppc_boost_min =

AMD_CPPC_NOMINAL_PERF(hwp_cap);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 960

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+	else if (cpudata->cppc_boost_min ==

AMD_CPPC_NOMINAL_PERF(hwp_cap))

Quoted 16 lines
drivers/cpufreq/amd-pstate.c +15 -0
+		cpudata->cppc_boost_min = max_limit;
+	else
+		return;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(cpudata->cppc_boost_min);
+	wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req);
+	cpudata->last_update = cpudata->sample.time; }
+
+static inline void amd_pstate_boost_down(struct amd_cpudata *cpudata)
+{
+	bool expired;
+
+	if (cpudata->cppc_boost_min) {
+		expired = time_after64(cpudata->sample.time, cpudata-

last_update +

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +4 -0
+					cppc_boost_hold_time_ns);
+
+		if (expired) {
+			wrmsrl_safe_on_cpu(cpudata->cpu,

MSR_AMD_CPPC_REQ,

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 8 lines
drivers/cpufreq/amd-pstate.c +8 -0
+						cpudata->cppc_req_cached);
+			cpudata->cppc_boost_min = 0;
+		}
+	}
+
+	cpudata->last_update = cpudata->sample.time; }
+
+static inline void amd_pstate_boost_update_util(struct amd_cpudata

*cpudata,

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +9 -0
+						      u64 time)
+{
+	cpudata->sample.time = time;
+	if (smp_processor_id() != cpudata->cpu)
+		return;
+
+	if (cpudata->sched_flags & SCHED_CPUFREQ_IOWAIT) {
+		bool do_io = false;
+

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+		cpudata->sched_flags = 0;
+		/*
+		 * Set iowait_boost flag and update time. Since IO WAIT flag
+		 * is set all the time, we can't just conclude that there is
+		 * some IO bound activity is scheduled on this CPU with just
+		 * one occurrence. If we receive at least two in two
+		 * consecutive ticks, then we treat as boost candidate.
+		 * This is leveraged from Intel Pstate driver.
+		 */
+		if (time_before64(time, cpudata->last_io_update + 2 *

TICK_NSEC))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 13 lines
drivers/cpufreq/amd-pstate.c +13 -0
+			do_io = true;
+
+		cpudata->last_io_update = time;
+
+		if (do_io)
+			amd_pstate_boost_up(cpudata);
+
+	} else {
+		amd_pstate_boost_down(cpudata);
+	}
+}
+
+static inline void amd_pstate_cppc_update_hook(struct update_util_data

*data,

Quoted 26 lines
drivers/cpufreq/amd-pstate.c +26 -0
+						u64 time, unsigned int flags)
+{
+	struct amd_cpudata *cpudata = container_of(data,
+				struct amd_cpudata, update_util);
+
+	cpudata->sched_flags |= flags;
+
+	if (smp_processor_id() == cpudata->cpu)
+		amd_pstate_boost_update_util(cpudata, time); }
+
+static void amd_pstate_clear_update_util_hook(unsigned int cpu) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+
+	if (!cpudata->update_util_set)
+		return;
+
+	cpufreq_remove_update_util_hook(cpu);
+	cpudata->update_util_set = false;
+	synchronize_rcu();
+}
+
+static void amd_pstate_set_update_util_hook(unsigned int cpu_num) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu_num];
+
+	if (!cppc_boost) {
+		if (cpudata->update_util_set)

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 1072

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+			amd_pstate_clear_update_util_hook(cpudata->cpu);
+		return;
+	}
+
+	if (cpudata->update_util_set)
+		return;
+
+	cpudata->sample.time = 0;
+	cpufreq_add_update_util_hook(cpu_num, &cpudata->update_util,
+
	amd_pstate_cppc_update_hook);
Quoted 114 lines
drivers/cpufreq/amd-pstate.c +113 -0
+	cpudata->update_util_set = true;
+}
+
+static void amd_pstate_epp_init(unsigned int cpu) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+	u32 max_perf, min_perf;
+	u64 value;
+	s16 epp;
+	int ret;
+
+	max_perf = READ_ONCE(cpudata->highest_perf);
+	min_perf = READ_ONCE(cpudata->lowest_perf);
+
+	value = READ_ONCE(cpudata->cppc_req_cached);
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
+		min_perf = max_perf;
+
+	/* Initial min/max values for CPPC Performance Controls Register */
+	value &= ~AMD_CPPC_MIN_PERF(~0L);
+	value |= AMD_CPPC_MIN_PERF(min_perf);
+
+	value &= ~AMD_CPPC_MAX_PERF(~0L);
+	value |= AMD_CPPC_MAX_PERF(max_perf);
+
+	/* CPPC EPP feature require to set zero to the desire perf bit */
+	value &= ~AMD_CPPC_DES_PERF(~0L);
+	value |= AMD_CPPC_DES_PERF(0);
+
+	if (cpudata->epp_policy == cpudata->policy)
+		goto skip_epp;
+
+	cpudata->epp_policy = cpudata->policy;
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		epp = amd_pstate_get_epp(cpudata, value);
+		cpudata->epp_powersave = epp;
+		if (epp < 0)
+			goto skip_epp;
+		/* force the epp value to be zero for performance policy */
+		epp = 0;
+	} else {
+		if (cpudata->epp_powersave < 0)
+			goto skip_epp;
+		/* Get BIOS pre-defined epp value */
+		epp = amd_pstate_get_epp(cpudata, value);
+		if (epp)
+			goto skip_epp;
+		epp = cpudata->epp_powersave;
+	}
+	/* Set initial EPP value */
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+	}
+
+skip_epp:
+	WRITE_ONCE(cpudata->cppc_req_cached, value);
+	ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	if (!ret)
+		cpudata->epp_cached = epp;
+}
+
+static void amd_pstate_set_max_limits(struct amd_cpudata *cpudata) {
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u32 max_limit = (hwp_cap >> 24) & 0xff;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(max_limit);
+	wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req); }
+
+static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy) {
+	struct amd_cpudata *cpudata;
+
+	if (!policy->cpuinfo.max_freq)
+		return -ENODEV;
+
+	pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
+				policy->cpuinfo.max_freq, policy->max);
+
+	cpudata = all_cpu_data[policy->cpu];
+	cpudata->policy = policy->policy;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		mutex_lock(&amd_pstate_limits_lock);
+
+		if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+			amd_pstate_clear_update_util_hook(policy->cpu);
+			amd_pstate_set_max_limits(cpudata);
+		} else {
+			amd_pstate_set_update_util_hook(policy->cpu);
+		}
+
+		if (boot_cpu_has(X86_FEATURE_CPPC))
+			amd_pstate_epp_init(policy->cpu);
+
+		mutex_unlock(&amd_pstate_limits_lock);
+	}
+
+	return 0;
+}
+
+static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
+					   struct cpufreq_policy_data *policy) {
+	update_boost_state();
+	cpufreq_verify_within_cpu_limits(policy);
+}
+
+static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data
+*policy) {
+	amd_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
+	pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy-

min);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +3 -0
+	return 0;
+}
+
  static struct cpufreq_driver amd_pstate_driver = {
  	.flags		= CPUFREQ_CONST_LOOPS |

CPUFREQ_NEED_UPDATE_LIMITS,

Quoted 12 lines

.verify = amd_pstate_verify,

drivers/cpufreq/amd-pstate.c +7 -0
@@ -607,8 +1201,20 @@ static struct cpufreq_driver amd_pstate_driver = {
  	.attr		= amd_pstate_attr,
  };
 
+static struct cpufreq_driver amd_pstate_epp_driver = {
+	.flags		= CPUFREQ_CONST_LOOPS,
+	.verify		= amd_pstate_epp_verify_policy,
+	.setpolicy	= amd_pstate_epp_set_policy,
+	.init		= amd_pstate_epp_cpu_init,
+	.exit		= amd_pstate_epp_cpu_exit,
+	.update_limits	= amd_pstate_epp_update_limits,

drivers/cpufreq/amd-pstate.c static struct cpufreq_driver amd_pstate_driver = { · line 1220

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +5 -0
+	.name		= "amd_pstate_epp",
+	.attr		= amd_pstate_epp_attr,
+};
+
  static int __init amd_pstate_init(void)
  {
+	static struct amd_cpudata **cpudata;
  	int ret;
 
  	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) @@ -623,19

+1229,37

Quoted 14 lines

@@ static int __init amd_pstate_init(void)
if (cpufreq_get_current_driver())
return -EEXIST;

drivers/cpufreq/amd-pstate.c +10 -0
+	/* set the pstate driver disabled by default if there
+	 * is no early parameters set
+	 */
+	if (!cppc_load) {
+		pr_debug("disabled to load\n");
+		return -ENODEV;
+	}
+
+	if (!epp_off) {
+		WRITE_ONCE(cppc_active, 1);

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void)

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +6 -2
+		if (!default_pstate_driver)
+			default_pstate_driver = &amd_pstate_epp_driver;
+	}
+
  	/* capability check */
  	if (boot_cpu_has(X86_FEATURE_CPPC)) {
-		pr_debug("AMD CPPC MSR based functionality is supported\n");
-		amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf;
+		if (!cppc_active)
+			default_pstate_driver->adjust_perf =

amd_pstate_adjust_perf;

Quoted 4 lines
  	} else if (shared_mem) {
  		static_call_update(amd_pstate_enable, cppc_enable);
  		static_call_update(amd_pstate_init_perf, cppc_init_perf);
  		static_call_update(amd_pstate_update_perf,

cppc_update_perf);

drivers/cpufreq/amd-pstate.c

Quoted 2 lines
  	} else {
drivers/cpufreq/amd-pstate.c +0 -1
-		pr_info("This processor supports shared memory solution, you

can enable it with amd_pstate.shared_mem=1\n");

drivers/cpufreq/amd-pstate.c

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+		pr_info("This processor supports shared memory solution,

enable it

Quoted 66 lines
drivers/cpufreq/amd-pstate.c +21 -3
+with amd_pstate=legacy_cppc\n");
  		return -ENODEV;
  	}
 
+	cpudata = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
+	if (!cpudata)
+		return -ENOMEM;
+	WRITE_ONCE(all_cpu_data, cpudata);
  	/* enable amd pstate feature */
  	ret = amd_pstate_enable(true);
  	if (ret) {
@@ -643,9 +1267,9 @@ static int __init amd_pstate_init(void)
  		return ret;
  	}
 
-	ret = cpufreq_register_driver(&amd_pstate_driver);
+	ret = cpufreq_register_driver(default_pstate_driver);
  	if (ret)
-		pr_err("failed to register amd_pstate_driver with return %d\n",
+		pr_err("failed to register amd pstate driver with return %d\n",
  		       ret);
 
  	return ret;
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
  	if (!str)
  		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
+	}
+
  	/*
  	 * support shared memory type CPPC which has no MSR function.
  	 * enable amd-pstate on processors with shared memory solution diff
--git a/include/linux/amd-pstate.h b/include/linux/amd-pstate.h index

1c4b8659f171..7e6e8cab97b3 100644

include/linux/amd-pstate.h +1 -0
@@ -25,6 +25,7 @@ struct amd_aperf_mperf {
  	u64 aperf;
  	u64 mperf;
  	u64 tsc;
+	u64 time;
  };
 
  /**
@@ -47,6 +48,18 @@ struct amd_aperf_mperf {
   * @prev: Last Aperf/Mperf/tsc count value read from register
   * @freq: current cpu frequency value
   * @boost_supported: check whether the Processor or SBIOS supports

boost mode
+ * @epp_powersave: Last saved CPPC energy performance preference

include/linux/amd-pstate.h +4 -0
+				when policy switched to performance
+ * @epp_policy: Last saved policy used to set energy-performance
+preference
+ * @epp_cached: Cached CPPC energy-performance preference value

include/linux/amd-pstate.h struct amd_aperf_mperf {

Quoted 10 lines
include/linux/amd-pstate.h +8 -0
+ * @policy: Cpufreq policy value
+ * @sched_flags: Store scheduler flags for possible cross CPU update
+ * @update_util_set: CPUFreq utility callback is set
+ * @last_update: Time stamp of the last performance state update
+ * @cppc_boost_min: Last CPPC boosted min performance state
+ * @cppc_cap1_cached: Cached value of the last CPPC Capabilities MSR
+ * @update_util: Cpufreq utility callback information
+ * @sample: the stored performance sample
   *
   * The amd_cpudata is key private data for each CPU thread in AMD P-State,

and

Quoted 1 line

* represents all the attributes and goals that AMD P-State requests at

runtime.

Quoted 60 lines
include/linux/amd-pstate.h +56 -0
@@ -72,6 +85,74 @@ struct amd_cpudata {
 
  	u64	freq;
  	bool	boost_supported;
+
+	/* EPP feature related attributes*/
+	s16	epp_powersave;
+	s16	epp_policy;
+	s16	epp_cached;
+	u32	policy;
+	u32	sched_flags;
+	bool	update_util_set;
+	u64	last_update;
+	u64	last_io_update;
+	u32	cppc_boost_min;
+	u64	cppc_cap1_cached;
+	struct	update_util_data update_util;
+	struct	amd_aperf_mperf sample;
+};
+
+/**
+ * struct amd_pstate_params - global parameters for the performance
+control
+ * @ cppc_boost_disabled wheher the core performance boost disabled
+*/ struct amd_pstate_params {
+	bool cppc_boost_disabled;
+};
+
+#define AMD_CPPC_EPP_PERFORMANCE		0x00
+#define AMD_CPPC_EPP_BALANCE_PERFORMANCE	0x80
+#define AMD_CPPC_EPP_BALANCE_POWERSAVE		0xBF
+#define AMD_CPPC_EPP_POWERSAVE			0xFF
+
+/*
+ * AMD Energy Preference Performance (EPP)
+ * The EPP is used in the CCLK DPM controller to drive
+ * the frequency that a core is going to operate during
+ * short periods of activity. EPP values will be utilized for
+ * different OS profiles (balanced, performance, power savings)
+ * display strings corresponding to EPP index in the
+ * energy_perf_strings[]
+ *	index		String
+ *-------------------------------------
+ *	0		default
+ *	1		performance
+ *	2		balance_performance
+ *	3		balance_power
+ *	4		power
+ */
+enum energy_perf_value_index {
+	EPP_INDEX_DEFAULT = 0,
+	EPP_INDEX_PERFORMANCE,
+	EPP_INDEX_BALANCE_PERFORMANCE,
+	EPP_INDEX_BALANCE_POWERSAVE,
+	EPP_INDEX_POWERSAVE,
+};
+
+static const char * const energy_perf_strings[] = {
+	[EPP_INDEX_DEFAULT] = "default",
+	[EPP_INDEX_PERFORMANCE] = "performance",

include/linux/amd-pstate.h struct amd_cpudata { · line 153

Quoted 10 lines
include/linux/amd-pstate.h +10 -0
+	[EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
+	[EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
+	[EPP_INDEX_POWERSAVE] = "power",
+	NULL
+};
+
+static unsigned int epp_values[] = {
+	[EPP_INDEX_DEFAULT] = 0,
+	[EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
+	[EPP_INDEX_BALANCE_PERFORMANCE] =

AMD_CPPC_EPP_BALANCE_PERFORMANCE,

include/linux/amd-pstate.h struct amd_cpudata { · line 154

Quoted 1 line
include/linux/amd-pstate.h +1 -0
+	[EPP_INDEX_BALANCE_POWERSAVE] =

AMD_CPPC_EPP_BALANCE_POWERSAVE,

Quoted 4 lines
include/linux/amd-pstate.h +1 -0
+	[EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
  };
 
  #endif /* _LINUX_AMD_PSTATE_H */
author reviewer
#16 lore raw

RE: [PATCH v4 7/9] cpufreq: amd-pstate: add frequency dynamic boost sysfs control

In reply to Limonciello, Mario

[AMD Official Use Only - General]

Quoted 35 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:30 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v4 7/9] cpufreq: amd-pstate: add frequency dynamic boost sysfs control

Quoted 7 lines · Perry Yuan

Add one sysfs entry to control the CPU cores frequency boost state The attribute file can allow user to set max performance boosted or keeping at normal perf level.

---
  drivers/cpufreq/amd-pstate.c | 66

++++++++++++++++++++++++++++++++++--

Quoted 4 lines
  1 file changed, 64 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/cpufreq/amd-pstate.c index 645706d65173..b71bfbbb7639

100644

drivers/cpufreq/amd-pstate.c static ssize_t

Quoted 3 lines
drivers/cpufreq/amd-pstate.c +0 -0
@@ -765,12 +765,46 @@ static ssize_t

show_energy_performance_preference(

Quoted 3 lines
drivers/cpufreq/amd-pstate.c +0 -0
  	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);
  }
 
Quoted 12 lines

drivers/cpufreq/amd-pstate.c static ssize_t show_energy_performance_preference( · line 779

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+static void amd_pstate_update_policies(void) {
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		cpufreq_update_policy(cpu);
+}
+
+static ssize_t show_boost(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf) {
+	return sprintf(buf, "%u\n", cppc_boost);

For new code I believe you should use the new helper sysfs_emit() instead.

Make sense, changed in v5.
Thanks .

Perry.

Quoted 81 lines
Quoted 48 lines
drivers/cpufreq/amd-pstate.c +30 -0
+}
+
+static ssize_t store_boost(struct kobject *a,
+				       struct kobj_attribute *b,
+				       const char *buf, size_t count) {
+	bool new_state;
+	int ret;
+
+	ret = kstrtobool(buf, &new_state);
+	if (ret)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	cppc_boost = !!new_state;
+	amd_pstate_update_policies();
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return count;
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
  cpufreq_freq_attr_ro(amd_pstate_highest_perf);
  cpufreq_freq_attr_rw(energy_performance_preference);
  cpufreq_freq_attr_ro(energy_performance_available_preferences);
+define_one_global_rw(boost);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -788,6 +822,15 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
  	NULL,
  };
 
+static struct attribute *pstate_global_attributes[] = {
+	&boost.attr,
+	NULL
+};
+
+static const struct attribute_group amd_pstate_global_attr_group = {
+	.attrs = pstate_global_attributes,
+};
+
  static inline void update_boost_state(void)
  {
  	u64 misc_en;

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 1401

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -1
@@ -1398,9 +1441,28 @@ static int __init amd_pstate_init(void)
 
  	ret = cpufreq_register_driver(default_pstate_driver);
  	if (ret)
-		pr_err("failed to register amd pstate driver with

return %d\n",

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void)

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +3 -1
-		       ret);
+		pr_err("failed to register driver with return %d\n", ret);
+
+	amd_pstate_kobj = kobject_create_and_add("amd-pstate",

&cpu_subsys.dev_root->kobj);

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void)

Quoted 7 lines
drivers/cpufreq/amd-pstate.c +7 -0
+	if (!amd_pstate_kobj) {
+		ret = -EINVAL;
+		pr_err("global sysfs registration failed.\n");
+		goto kobject_free;
+	}
+
+	ret = sysfs_create_group(amd_pstate_kobj,

&amd_pstate_global_attr_group);

Quoted 15 lines
drivers/cpufreq/amd-pstate.c +11 -0
+	if (ret) {
+		pr_err("sysfs attribute export failed with error %d.\n", ret);
+		goto global_attr_free;
+	}
+
+	return ret;
 
+global_attr_free:
+	kobject_put(amd_pstate_kobj);
+kobject_free:
+	kfree(cpudata);
+	cpufreq_unregister_driver(default_pstate_driver);
  	return ret;
  }
  device_initcall(amd_pstate_init);
author reviewer
#19 lore raw

RE: [PATCH v4 8/9] cpufreq: amd_pstate: add driver working mode status sysfs entry

In reply to Limonciello, Mario

[AMD Official Use Only - General]

Quoted 52 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:30 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v4 8/9] cpufreq: amd_pstate: add driver working mode status sysfs entry

Quoted 10 lines · Perry Yuan

While amd-pstate driver was loaded with specific driver mode, it will need to check which mode is enabled for the pstate driver,add this sysfs entry to show the current status

$ cat /sys/devices/system/cpu/amd-pstate/status
active
---
  drivers/cpufreq/amd-pstate.c | 44

++++++++++++++++++++++++++++++++++++

Quoted 4 lines
  1 file changed, 44 insertions(+)
(quoted diff) +0 -0

b/drivers/cpufreq/amd-pstate.c index b71bfbbb7639..6a3b610cb084

100644

Quoted 15 lines
drivers/cpufreq/amd-pstate.c +2 -0
@@ -65,6 +65,8 @@ static int cppc_load __initdata;
  static int epp_off __initdata;
 
  static struct cpufreq_driver *default_pstate_driver;
+static struct cpufreq_driver amd_pstate_epp_driver; static struct
+cpufreq_driver amd_pstate_driver;
  static struct amd_cpudata **all_cpu_data;
 
  static struct amd_pstate_params global_params; @@ -798,6 +800,46 @@

static ssize_t store_boost(struct kobject *a,
return count;
}

drivers/cpufreq/amd-pstate.c static ssize_t store_boost(struct kobject *a,

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +5 -0
+static ssize_t amd_pstate_show_status(char *buf) {
+	if (!default_pstate_driver)
+		return sprintf(buf, "off\n");
+
+	return sprintf(buf, "%s\n", default_pstate_driver ==

&amd_pstate_epp_driver ?

Quoted 4 lines

drivers/cpufreq/amd-pstate.c

Quoted 2 lines
drivers/cpufreq/amd-pstate.c +2 -0
+					"active" : "passive");
+}

For new code I believe you should use the new helper sysfs_emit() instead.

Make sense, changed in v5.
Thanks .

Perry.

Quoted 49 lines
Quoted 49 lines
drivers/cpufreq/amd-pstate.c +31 -0
+
+static int amd_pstate_update_status(const char *buf, size_t size) {
+	/* FIXME! */
+	return -EOPNOTSUPP;
+}
+
+static ssize_t show_status(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf) {
+	ssize_t ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_show_status(buf);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret;
+}
+
+static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
+			    const char *buf, size_t count) {
+	char *p = memchr(buf, '\n', count);
+	int ret;
+
+	mutex_lock(&amd_pstate_driver_lock);
+	ret = amd_pstate_update_status(buf, p ? p - buf : count);
+	mutex_unlock(&amd_pstate_driver_lock);
+
+	return ret < 0 ? ret : count;
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
@@ -805,6 +847,7 @@ cpufreq_freq_attr_ro(amd_pstate_highest_perf);
  cpufreq_freq_attr_rw(energy_performance_preference);
  cpufreq_freq_attr_ro(energy_performance_available_preferences);
  define_one_global_rw(boost);
+define_one_global_rw(status);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -824,6 +867,7 @@ static struct freq_attr *amd_pstate_epp_attr[] = {
 
  static struct attribute *pstate_global_attributes[] = {
  	&boost.attr,
+	&status.attr,
  	NULL
  };
 
author reviewer
#9 lore raw

RE: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

In reply to Limonciello, Mario

[AMD Official Use Only - General]

Hi Mario

Quoted 54 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:41 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v4 4/9] cpufreq: amd_pstate: implement Pstate EPP support for the AMD processors

Quoted 6 lines · Perry Yuan

Add EPP driver support for AMD SoCs which support a dedicated MSR for CPPC. EPP is used by the DPM controller to configure the frequency that a core operates at during short periods of activity.

The SoC EPP targets are configured on a scale from 0 to 255 where 0 represents maximum performance and 255 represents maximum

efficiency.

Quoted 10 lines

The amd-pstate driver exports profile string names to userspace that are tied to specific EPP values.

The balance_performance string (0x80) provides the best balance for efficiency versus power on most systems, but users can choose other strings to meet their needs as well.

$ cat

/sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_ p

Quoted 1 line

references default performance balance_performance balance_power

power

Quoted 3 lines
$ cat

/sys/devices/system/cpu/cpufreq/policy0/energy_performance_preferenc e

Quoted 5 lines

balance_performance

---
  drivers/cpufreq/amd-pstate.c | 652

++++++++++++++++++++++++++++++++++-

Quoted 3 lines
  include/linux/amd-pstate.h   |  81 +++++
  2 files changed, 726 insertions(+), 7 deletions(-)

Did you see Rafael's comment about reducing the common code w/ intel- pstate for future maintainability?

Quoted 6 lines

As some of it is duplicated, I think it should be possible to make a library source file that both can use and pull from. If you split out the common code in an earlier patch I think it would shrink this patch significantly too.

If you already looked into this and it doesn't /really/ change much, please comment also.

Because the amd pstate driver support shared memory type EPP driver, it makses most of the epp set/get functions are not compatible with intel_pstate.
And intel_pstate support EPB feature which we do not support.

However, the EPP profiles definitions can be extracted as common header. I have changed this in the V6 release.
Please help to take a look if it is acceptable.

Perry.

Quoted 875 lines
Quoted 24 lines
(quoted diff) +0 -0

b/drivers/cpufreq/amd-pstate.c index 85a0b3fb56c2..7b3a12772582 100644

drivers/cpufreq/amd-pstate.c +13 -1
@@ -60,8 +60,131 @@
   * module parameter to be able to enable it manually for debugging.
   */
  static bool shared_mem __read_mostly;
+static int cppc_active __read_mostly; static int cppc_load
+__initdata; static int epp_off __initdata;
 
-static struct cpufreq_driver amd_pstate_driver;
+static struct cpufreq_driver *default_pstate_driver; static struct
+amd_cpudata **all_cpu_data;
+
+static struct amd_pstate_params global_params;
+
+static DEFINE_MUTEX(amd_pstate_limits_lock);
+static DEFINE_MUTEX(amd_pstate_driver_lock);
+
+static bool cppc_boost __read_mostly; struct kobject
+*amd_pstate_kobj;
+

drivers/cpufreq/amd-pstate.c line 86

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+#ifdef CONFIG_ACPI_CPPC_LIB
+static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64
+cppc_req_cached) {
+	s16 epp;
+	struct cppc_perf_caps perf_caps;
+	int ret;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		if (!cppc_req_cached) {
+			epp = rdmsrl_on_cpu(cpudata->cpu,

MSR_AMD_CPPC_REQ,

drivers/cpufreq/amd-pstate.c

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +9 -0
+					&cppc_req_cached);
+			if (epp)
+				return epp;
+		}
+		epp = (cppc_req_cached >> 24) & 0xFF;
+	} else {
+		ret = cppc_get_epp_caps(cpudata->cpu, &perf_caps);
+		if (ret < 0) {
+			pr_debug("Could not retrieve energy perf value

(%d)\n", ret);

Quoted 41 lines
drivers/cpufreq/amd-pstate.c +41 -0
+			return -EIO;
+		}
+		epp = (s16) perf_caps.energy_perf;
+	}
+
+	return epp;
+}
+#endif
+
+static int amd_pstate_get_energy_pref_index(struct amd_cpudata
+*cpudata) {
+	s16 epp;
+	int index = -EINVAL;
+
+	epp = amd_pstate_get_epp(cpudata, 0);
+	if (epp < 0)
+		return epp;
+
+	switch (epp) {
+		case AMD_CPPC_EPP_PERFORMANCE:
+			index = EPP_INDEX_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
+			index = EPP_INDEX_BALANCE_PERFORMANCE;
+			break;
+		case AMD_CPPC_EPP_BALANCE_POWERSAVE:
+			index = EPP_INDEX_BALANCE_POWERSAVE;
+			break;
+		case AMD_CPPC_EPP_POWERSAVE:
+			index = EPP_INDEX_POWERSAVE;
+			break;
+		default:
+			break;
+	}
+
+	return index;
+}
+
+#ifdef CONFIG_ACPI_CPPC_LIB
+static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp) {
+	int ret;

drivers/cpufreq/amd-pstate.c

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	struct cppc_perf_ctrls perf_ctrls;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		u64 value = READ_ONCE(cpudata->cppc_req_cached);
+
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,

value);

drivers/cpufreq/amd-pstate.c

Quoted 7 lines
drivers/cpufreq/amd-pstate.c +7 -0
+		if (!ret)
+			cpudata->epp_cached = epp;
+	} else {
+		perf_ctrls.energy_perf = epp;
+		ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
+		if (ret) {
+			pr_debug("failed to set energy perf value (%d)\n",

ret);

drivers/cpufreq/amd-pstate.c

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +9 -0
+			return ret;
+		}
+		cpudata->epp_cached = epp;
+	}
+
+	return ret;
+}
+
+static int amd_pstate_set_energy_pref_index(struct amd_cpudata

*cpudata,

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +4 -0
+		int pref_index)
+{
+	int epp = -EINVAL;
+	int ret;

drivers/cpufreq/amd-pstate.c

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+
+	if (!pref_index) {
+		pr_debug("EPP pref_index is invalid\n");
+		return -EINVAL;
+	}
+
+	if (epp == -EINVAL)
+		epp = epp_values[pref_index];
+
+	if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)

{

Quoted 16 lines
drivers/cpufreq/amd-pstate.c +9 -0
+		pr_debug("EPP cannot be set under performance policy\n");
+		return -EBUSY;
+	}
+
+	ret = amd_pstate_set_epp(cpudata, epp);
+
+	return ret;
+}
+#endif
 
  static inline int pstate_enable(bool enable)
  {
@@ -71,11 +194,21 @@ static inline int pstate_enable(bool enable)
  static int cppc_enable(bool enable)
  {
  	int cpu, ret = 0;

drivers/cpufreq/amd-pstate.c static inline int pstate_enable(bool enable) · line 206

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +5 -0
+	struct cppc_perf_ctrls perf_ctrls;
 
  	for_each_present_cpu(cpu) {
  		ret = cppc_set_enable(cpu, enable);
  		if (ret)
  			return ret;
+
+		/* Enable autonomous mode for EPP */
+		if (!cppc_active) {
+			/* Set desired perf as zero to allow EPP firmware

control */

drivers/cpufreq/amd-pstate.c static void amd_pstate_boost_init(struct

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +5 -0
+			perf_ctrls.desired_perf = 0;
+			ret = cppc_set_perf(cpu, &perf_ctrls);
+			if (ret)
+				return ret;
+		}
  	}
 
  	return ret;
@@ -418,7 +551,7 @@ static void amd_pstate_boost_init(struct

amd_cpudata *cpudata)

drivers/cpufreq/amd-pstate.c static void amd_pstate_boost_init(struct amd_cpudata *cpudata)

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +1 -2
  		return;
 
  	cpudata->boost_supported = true;
-	amd_pstate_driver.boost_enabled = true;
+	default_pstate_driver->boost_enabled = true;
  }
 
  static int amd_pstate_cpu_init(struct cpufreq_policy *policy) @@
-582,10 +715,62 @@ static ssize_t show_amd_pstate_highest_perf(struct

cpufreq_policy *policy,

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 727

Quoted 12 lines
  	return sprintf(&buf[0], "%u\n", perf);
  }
drivers/cpufreq/amd-pstate.c +9 -0
+static ssize_t show_energy_performance_available_preferences(
+				struct cpufreq_policy *policy, char *buf) {
+	int i = 0;
+	int ret = 0;
+
+	while (energy_perf_strings[i] != NULL)
+		ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
+
+	ret += sprintf(&buf[ret], "\n");

Use sysfs_emit() instead.

Quoted 26 lines
drivers/cpufreq/amd-pstate.c +26 -0
+
+	return ret;
+}
+
+static ssize_t store_energy_performance_preference(
+		struct cpufreq_policy *policy, const char *buf, size_t count) {
+	struct amd_cpudata *cpudata = policy->driver_data;
+	char str_preference[21];
+	ssize_t ret;
+	u32 epp = 0;
+
+	ret = sscanf(buf, "%20s", str_preference);
+	if (ret != 1)
+		return -EINVAL;
+
+	ret = match_string(energy_perf_strings, -1, str_preference);
+	if (ret < 0)
+		return -EINVAL;
+
+	mutex_lock(&amd_pstate_limits_lock);
+	ret = amd_pstate_set_energy_pref_index(cpudata, ret);
+	mutex_unlock(&amd_pstate_limits_lock);
+
+	return ret ?: count;
+}
+

drivers/cpufreq/amd-pstate.c static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, · line 765

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+static ssize_t show_energy_performance_preference(
+				struct cpufreq_policy *policy, char *buf) {
+	struct amd_cpudata *cpudata = policy->driver_data;
+	int preference;
+
+	preference = amd_pstate_get_energy_pref_index(cpudata);
+	if (preference < 0)
+		return preference;
+
+	return  sprintf(buf, "%s\n", energy_perf_strings[preference]);

Use sysfs_emit() instead.

Quoted 67 lines
drivers/cpufreq/amd-pstate.c +56 -0
+}
+
  cpufreq_freq_attr_ro(amd_pstate_max_freq);
  cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
 
  cpufreq_freq_attr_ro(amd_pstate_highest_perf);
+cpufreq_freq_attr_rw(energy_performance_preference);
+cpufreq_freq_attr_ro(energy_performance_available_preferences);
 
  static struct freq_attr *amd_pstate_attr[] = {
  	&amd_pstate_max_freq,
@@ -594,6 +779,415 @@ static struct freq_attr *amd_pstate_attr[] = {
  	NULL,
  };
 
+static struct freq_attr *amd_pstate_epp_attr[] = {
+	&amd_pstate_max_freq,
+	&amd_pstate_lowest_nonlinear_freq,
+	&amd_pstate_highest_perf,
+	&energy_performance_preference,
+	&energy_performance_available_preferences,
+	NULL,
+};
+
+static inline void update_boost_state(void) {
+	u64 misc_en;
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[0];
+	rdmsrl(MSR_K7_HWCR, misc_en);
+	global_params.cppc_boost_disabled = misc_en & BIT_ULL(25); }
+
+static int amd_pstate_init_cpu(unsigned int cpunum) {
+	struct amd_cpudata *cpudata;
+
+	cpudata = all_cpu_data[cpunum];
+	if (!cpudata) {
+		cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
+		if (!cpudata)
+			return -ENOMEM;
+		WRITE_ONCE(all_cpu_data[cpunum], cpudata);
+
+		cpudata->cpu = cpunum;
+	}
+	cpudata->epp_powersave = -EINVAL;
+	cpudata->epp_policy = 0;
+	pr_debug("controlling: cpu %d\n", cpunum);
+	return 0;
+}
+
+static int __amd_pstate_cpu_init(struct cpufreq_policy *policy) {
+	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
+	struct amd_cpudata *cpudata;
+	struct device *dev;
+	int rc;
+	u64 value;
+
+	rc = amd_pstate_init_cpu(policy->cpu);
+	if (rc)
+		return rc;
+
+	cpudata = all_cpu_data[policy->cpu];
+
+	dev = get_cpu_device(policy->cpu);
+	if (!dev)
+		goto free_cpudata1;
+

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 843

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	rc = amd_pstate_init_perf(cpudata);
+	if (rc)
+		goto free_cpudata1;
+
+	min_freq = amd_get_min_freq(cpudata);
+	max_freq = amd_get_max_freq(cpudata);
+	nominal_freq = amd_get_nominal_freq(cpudata);
+	lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata);
+	if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) {
+		dev_err(dev, "min_freq(%d) or max_freq(%d) value is

incorrect\n",

Quoted 22 lines
drivers/cpufreq/amd-pstate.c +22 -0
+				min_freq, max_freq);
+		ret = -EINVAL;
+		goto free_cpudata1;
+	}
+
+	policy->min = min_freq;
+	policy->max = max_freq;
+
+	policy->cpuinfo.min_freq = min_freq;
+	policy->cpuinfo.max_freq = max_freq;
+	/* It will be updated by governor */
+	policy->cur = policy->cpuinfo.min_freq;
+
+	/* Initial processor data capability frequencies */
+	cpudata->max_freq = max_freq;
+	cpudata->min_freq = min_freq;
+	cpudata->nominal_freq = nominal_freq;
+	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
+
+	policy->driver_data = cpudata;
+
+	update_boost_state();

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+	cpudata->epp_cached = amd_pstate_get_epp(cpudata, value);
+
+	policy->min = policy->cpuinfo.min_freq;
+	policy->max = policy->cpuinfo.max_freq;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC))
+		policy->fast_switch_possible = true;
+
+	if (!shared_mem && boot_cpu_has(X86_FEATURE_CPPC)) {
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,

&value);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +5 -0
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_req_cached, value);
+
+		ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,

&value);

Quoted 12 lines
drivers/cpufreq/amd-pstate.c +12 -0
+		if (ret)
+			return ret;
+		WRITE_ONCE(cpudata->cppc_cap1_cached, value);
+	}
+	amd_pstate_boost_init(cpudata);
+
+	return 0;
+
+free_cpudata1:
+	kfree(cpudata);
+	return ret;
+}

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+
+static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy) {
+	int ret;
+
+	ret = __amd_pstate_cpu_init(policy);
+	if (ret)
+		return ret;
+	/*
+	 * Set the policy to powersave to provide a valid fallback value in case
+	 * the default cpufreq governor is neither powersave nor

performance.

Quoted 42 lines
drivers/cpufreq/amd-pstate.c +42 -0
+	 */
+	policy->policy = CPUFREQ_POLICY_POWERSAVE;
+
+	return 0;
+}
+
+static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy) {
+	pr_debug("CPU %d exiting\n", policy->cpu);
+	policy->fast_switch_possible = false;
+	return 0;
+}
+
+static void amd_pstate_update_max_freq(unsigned int cpu) {
+	struct cpufreq_policy *policy = policy = cpufreq_cpu_get(cpu);
+
+	if (!policy)
+		return;
+
+	refresh_frequency_limits(policy);
+	cpufreq_cpu_put(policy);
+}
+
+static void amd_pstate_epp_update_limits(unsigned int cpu) {
+	mutex_lock(&amd_pstate_driver_lock);
+	update_boost_state();
+	if (global_params.cppc_boost_disabled) {
+		for_each_possible_cpu(cpu)
+			amd_pstate_update_max_freq(cpu);
+	} else {
+		cpufreq_update_policy(cpu);
+	}
+	mutex_unlock(&amd_pstate_driver_lock);
+}
+
+static int cppc_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
+
+static inline void amd_pstate_boost_up(struct amd_cpudata *cpudata) {
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u32 max_limit = (hwp_req & 0xff);
+	u32 min_limit = (hwp_req & 0xff00) >> 8;
+	u32 boost_level1;

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+
+	/* If max and min are equal or already at max, nothing to boost */
+	if (max_limit == min_limit)
+		return;
+
+	/* Set boost max and min to initial value */
+	if (!cpudata->cppc_boost_min)
+		cpudata->cppc_boost_min = min_limit;
+
+	boost_level1 = ((AMD_CPPC_NOMINAL_PERF(hwp_cap) +

min_limit) >> 1);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 4 lines
drivers/cpufreq/amd-pstate.c +4 -0
+
+	if (cpudata->cppc_boost_min < boost_level1)
+		cpudata->cppc_boost_min = boost_level1;
+	else if (cpudata->cppc_boost_min <

AMD_CPPC_NOMINAL_PERF(hwp_cap))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 959

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+		cpudata->cppc_boost_min =

AMD_CPPC_NOMINAL_PERF(hwp_cap);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 960

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+	else if (cpudata->cppc_boost_min ==

AMD_CPPC_NOMINAL_PERF(hwp_cap))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 989

Quoted 7 lines
drivers/cpufreq/amd-pstate.c +7 -0
+		cpudata->cppc_boost_min = max_limit;
+	else
+		return;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(cpudata->cppc_boost_min);
+	wrmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,

hwp_req);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 13 lines
drivers/cpufreq/amd-pstate.c +8 -0
+	cpudata->last_update = cpudata->sample.time; }
+
+static inline void amd_pstate_boost_down(struct amd_cpudata *cpudata)
+{
+	bool expired;
+
+	if (cpudata->cppc_boost_min) {
+		expired = time_after64(cpudata->sample.time, cpudata-

last_update +

drivers/cpufreq/amd-pstate.c +4 -0
+					cppc_boost_hold_time_ns);
+
+		if (expired) {
+			wrmsrl_safe_on_cpu(cpudata->cpu,

MSR_AMD_CPPC_REQ,

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 8 lines
drivers/cpufreq/amd-pstate.c +8 -0
+						cpudata->cppc_req_cached);
+			cpudata->cppc_boost_min = 0;
+		}
+	}
+
+	cpudata->last_update = cpudata->sample.time; }
+
+static inline void amd_pstate_boost_update_util(struct amd_cpudata

*cpudata,

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +9 -0
+						      u64 time)
+{
+	cpudata->sample.time = time;
+	if (smp_processor_id() != cpudata->cpu)
+		return;
+
+	if (cpudata->sched_flags & SCHED_CPUFREQ_IOWAIT) {
+		bool do_io = false;
+

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+		cpudata->sched_flags = 0;
+		/*
+		 * Set iowait_boost flag and update time. Since IO WAIT flag
+		 * is set all the time, we can't just conclude that there is
+		 * some IO bound activity is scheduled on this CPU with just
+		 * one occurrence. If we receive at least two in two
+		 * consecutive ticks, then we treat as boost candidate.
+		 * This is leveraged from Intel Pstate driver.
+		 */
+		if (time_before64(time, cpudata->last_io_update + 2 *

TICK_NSEC))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 13 lines
drivers/cpufreq/amd-pstate.c +13 -0
+			do_io = true;
+
+		cpudata->last_io_update = time;
+
+		if (do_io)
+			amd_pstate_boost_up(cpudata);
+
+	} else {
+		amd_pstate_boost_down(cpudata);
+	}
+}
+
+static inline void amd_pstate_cppc_update_hook(struct update_util_data

*data,

Quoted 26 lines
drivers/cpufreq/amd-pstate.c +26 -0
+						u64 time, unsigned int flags)
+{
+	struct amd_cpudata *cpudata = container_of(data,
+				struct amd_cpudata, update_util);
+
+	cpudata->sched_flags |= flags;
+
+	if (smp_processor_id() == cpudata->cpu)
+		amd_pstate_boost_update_util(cpudata, time); }
+
+static void amd_pstate_clear_update_util_hook(unsigned int cpu) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+
+	if (!cpudata->update_util_set)
+		return;
+
+	cpufreq_remove_update_util_hook(cpu);
+	cpudata->update_util_set = false;
+	synchronize_rcu();
+}
+
+static void amd_pstate_set_update_util_hook(unsigned int cpu_num) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu_num];
+
+	if (!cppc_boost) {
+		if (cpudata->update_util_set)

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 1072

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+			amd_pstate_clear_update_util_hook(cpudata->cpu);
+		return;
+	}
+
+	if (cpudata->update_util_set)
+		return;
+
+	cpudata->sample.time = 0;
+	cpufreq_add_update_util_hook(cpu_num, &cpudata->update_util,
+
	amd_pstate_cppc_update_hook);
Quoted 95 lines
drivers/cpufreq/amd-pstate.c +95 -0
+	cpudata->update_util_set = true;
+}
+
+static void amd_pstate_epp_init(unsigned int cpu) {
+	struct amd_cpudata *cpudata = all_cpu_data[cpu];
+	u32 max_perf, min_perf;
+	u64 value;
+	s16 epp;
+	int ret;
+
+	max_perf = READ_ONCE(cpudata->highest_perf);
+	min_perf = READ_ONCE(cpudata->lowest_perf);
+
+	value = READ_ONCE(cpudata->cppc_req_cached);
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
+		min_perf = max_perf;
+
+	/* Initial min/max values for CPPC Performance Controls Register */
+	value &= ~AMD_CPPC_MIN_PERF(~0L);
+	value |= AMD_CPPC_MIN_PERF(min_perf);
+
+	value &= ~AMD_CPPC_MAX_PERF(~0L);
+	value |= AMD_CPPC_MAX_PERF(max_perf);
+
+	/* CPPC EPP feature require to set zero to the desire perf bit */
+	value &= ~AMD_CPPC_DES_PERF(~0L);
+	value |= AMD_CPPC_DES_PERF(0);
+
+	if (cpudata->epp_policy == cpudata->policy)
+		goto skip_epp;
+
+	cpudata->epp_policy = cpudata->policy;
+
+	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+		epp = amd_pstate_get_epp(cpudata, value);
+		cpudata->epp_powersave = epp;
+		if (epp < 0)
+			goto skip_epp;
+		/* force the epp value to be zero for performance policy */
+		epp = 0;
+	} else {
+		if (cpudata->epp_powersave < 0)
+			goto skip_epp;
+		/* Get BIOS pre-defined epp value */
+		epp = amd_pstate_get_epp(cpudata, value);
+		if (epp)
+			goto skip_epp;
+		epp = cpudata->epp_powersave;
+	}
+	/* Set initial EPP value */
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		value &= ~GENMASK_ULL(31, 24);
+		value |= (u64)epp << 24;
+	}
+
+skip_epp:
+	WRITE_ONCE(cpudata->cppc_req_cached, value);
+	ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
+	if (!ret)
+		cpudata->epp_cached = epp;
+}
+
+static void amd_pstate_set_max_limits(struct amd_cpudata *cpudata) {
+	u64 hwp_cap = READ_ONCE(cpudata->cppc_cap1_cached);
+	u64 hwp_req = READ_ONCE(cpudata->cppc_req_cached);
+	u32 max_limit = (hwp_cap >> 24) & 0xff;
+
+	hwp_req &= ~AMD_CPPC_MIN_PERF(~0L);
+	hwp_req |= AMD_CPPC_MIN_PERF(max_limit);
+	wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, hwp_req); }
+
+static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy) {
+	struct amd_cpudata *cpudata;
+
+	if (!policy->cpuinfo.max_freq)
+		return -ENODEV;
+
+	pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
+				policy->cpuinfo.max_freq, policy->max);
+
+	cpudata = all_cpu_data[policy->cpu];
+	cpudata->policy = policy->policy;
+
+	if (boot_cpu_has(X86_FEATURE_CPPC)) {
+		mutex_lock(&amd_pstate_limits_lock);
+
+		if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
+			amd_pstate_clear_update_util_hook(policy->cpu);
+			amd_pstate_set_max_limits(cpudata);
+		} else {
+			amd_pstate_set_update_util_hook(policy->cpu);
+		}
+
+		if (boot_cpu_has(X86_FEATURE_CPPC))

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = { · line 1178

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +10 -0
+			amd_pstate_epp_init(policy->cpu);
+
+		mutex_unlock(&amd_pstate_limits_lock);
+	}
+
+	return 0;
+}
+
+static void amd_pstate_verify_cpu_policy(struct amd_cpudata *cpudata,
+					   struct cpufreq_policy_data *policy)

{

Quoted 9 lines
drivers/cpufreq/amd-pstate.c +8 -0
+	update_boost_state();
+	cpufreq_verify_within_cpu_limits(policy);
+}
+
+static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data
+*policy) {
+	amd_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
+	pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy-

min);

drivers/cpufreq/amd-pstate.c static struct freq_attr *amd_pstate_attr[] = {

Quoted 5 lines
drivers/cpufreq/amd-pstate.c +3 -0
+	return 0;
+}
+
  static struct cpufreq_driver amd_pstate_driver = {
  	.flags		= CPUFREQ_CONST_LOOPS |

CPUFREQ_NEED_UPDATE_LIMITS,

drivers/cpufreq/amd-pstate.c static struct cpufreq_driver amd_pstate_driver =

Quoted 2 lines

.verify = amd_pstate_verify,

drivers/cpufreq/amd-pstate.c +0 -0
@@ -607,8 +1201,20 @@ static struct cpufreq_driver amd_pstate_driver =

{

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7 -0
  	.attr		= amd_pstate_attr,
  };
 
+static struct cpufreq_driver amd_pstate_epp_driver = {
+	.flags		= CPUFREQ_CONST_LOOPS,
+	.verify		= amd_pstate_epp_verify_policy,
+	.setpolicy	= amd_pstate_epp_set_policy,
+	.init		= amd_pstate_epp_cpu_init,
+	.exit		= amd_pstate_epp_cpu_exit,
+	.update_limits	= amd_pstate_epp_update_limits,

drivers/cpufreq/amd-pstate.c static struct cpufreq_driver amd_pstate_driver = {

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +5 -0
+	.name		= "amd_pstate_epp",
+	.attr		= amd_pstate_epp_attr,
+};
+
  static int __init amd_pstate_init(void)
  {
+	static struct amd_cpudata **cpudata;
  	int ret;
 
  	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) @@ -623,19

+1229,37

Quoted 11 lines

@@ static int __init amd_pstate_init(void)
if (cpufreq_get_current_driver())
return -EEXIST;

drivers/cpufreq/amd-pstate.c +7 -0
+	/* set the pstate driver disabled by default if there
+	 * is no early parameters set
+	 */
+	if (!cppc_load) {
+		pr_debug("disabled to load\n");
+		return -ENODEV;
+	}

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void)

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7 -1
+
+	if (!epp_off) {
+		WRITE_ONCE(cppc_active, 1);
+		if (!default_pstate_driver)
+			default_pstate_driver = &amd_pstate_epp_driver;
+	}
+
  	/* capability check */
  	if (boot_cpu_has(X86_FEATURE_CPPC)) {
-		pr_debug("AMD CPPC MSR based functionality is

supported\n");

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void)

Quoted 3 lines
drivers/cpufreq/amd-pstate.c +2 -1
-		amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf;
+		if (!cppc_active)
+			default_pstate_driver->adjust_perf =

amd_pstate_adjust_perf;

Quoted 4 lines
  	} else if (shared_mem) {
  		static_call_update(amd_pstate_enable, cppc_enable);
  		static_call_update(amd_pstate_init_perf, cppc_init_perf);
  		static_call_update(amd_pstate_update_perf,

cppc_update_perf);

drivers/cpufreq/amd-pstate.c

Quoted 2 lines
  	} else {
drivers/cpufreq/amd-pstate.c +0 -1
-		pr_info("This processor supports shared memory solution,

you can enable it with amd_pstate.shared_mem=1\n");

drivers/cpufreq/amd-pstate.c

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+		pr_info("This processor supports shared memory solution,

enable it

Quoted 12 lines
drivers/cpufreq/amd-pstate.c +5 -0
+with amd_pstate=legacy_cppc\n");
  		return -ENODEV;
  	}
 
+	cpudata = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
+	if (!cpudata)
+		return -ENOMEM;
+	WRITE_ONCE(all_cpu_data, cpudata);
  	/* enable amd pstate feature */
  	ret = amd_pstate_enable(true);
  	if (ret) {

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 648

Quoted 7 lines
drivers/cpufreq/amd-pstate.c +1 -2
@@ -643,9 +1267,9 @@ static int __init amd_pstate_init(void)
  		return ret;
  	}
 
-	ret = cpufreq_register_driver(&amd_pstate_driver);
+	ret = cpufreq_register_driver(default_pstate_driver);
  	if (ret)
-		pr_err("failed to register amd_pstate_driver with

return %d\n",

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_init(void) · line 1272

Quoted 1 line
drivers/cpufreq/amd-pstate.c +1 -0
+		pr_err("failed to register amd pstate driver with

return %d\n",

Quoted 14 lines
drivers/cpufreq/amd-pstate.c +7 -0
  		       ret);
 
  	return ret;
@@ -657,6 +1281,20 @@ static int __init amd_pstate_param(char *str)
  	if (!str)
  		return -EINVAL;
 
+	/*
+	 * Disable amd-pstate driver by default for now
+	 * append amd-pstate=active or amd-pstate=active
+	 * to enable driver loaded
+	 */
+	if (!strcmp(str, "active")) {
+		cppc_load = 1;

drivers/cpufreq/amd-pstate.c static int __init amd_pstate_param(char *str) · line 1300

Quoted 10 lines
drivers/cpufreq/amd-pstate.c +7 -0
+		default_pstate_driver = &amd_pstate_epp_driver;
+	} else if (!strcmp(str, "passive")) {
+		epp_off = 1;
+		cppc_load = 1;
+		default_pstate_driver = &amd_pstate_driver;
+	}
+
  	/*
  	 * support shared memory type CPPC which has no MSR function.
  	 * enable amd-pstate on processors with shared memory solution

diff

Quoted 22 lines

--git a/include/linux/amd-pstate.h b/include/linux/amd-pstate.h index 1c4b8659f171..7e6e8cab97b3 100644

include/linux/amd-pstate.h +1 -0
@@ -25,6 +25,7 @@ struct amd_aperf_mperf {
  	u64 aperf;
  	u64 mperf;
  	u64 tsc;
+	u64 time;
  };
 
  /**
@@ -47,6 +48,18 @@ struct amd_aperf_mperf {
   * @prev: Last Aperf/Mperf/tsc count value read from register
   * @freq: current cpu frequency value
   * @boost_supported: check whether the Processor or SBIOS supports

boost mode
+ * @epp_powersave: Last saved CPPC energy performance preference

include/linux/amd-pstate.h +4 -0
+				when policy switched to performance
+ * @epp_policy: Last saved policy used to set energy-performance
+preference
+ * @epp_cached: Cached CPPC energy-performance preference value

include/linux/amd-pstate.h struct amd_aperf_mperf {

Quoted 10 lines
include/linux/amd-pstate.h +8 -0
+ * @policy: Cpufreq policy value
+ * @sched_flags: Store scheduler flags for possible cross CPU update
+ * @update_util_set: CPUFreq utility callback is set
+ * @last_update: Time stamp of the last performance state update
+ * @cppc_boost_min: Last CPPC boosted min performance state
+ * @cppc_cap1_cached: Cached value of the last CPPC Capabilities MSR
+ * @update_util: Cpufreq utility callback information
+ * @sample: the stored performance sample
   *
   * The amd_cpudata is key private data for each CPU thread in AMD P-

State, and

Quoted 1 line

* represents all the attributes and goals that AMD P-State requests at

runtime.

Quoted 60 lines
include/linux/amd-pstate.h +56 -0
@@ -72,6 +85,74 @@ struct amd_cpudata {
 
  	u64	freq;
  	bool	boost_supported;
+
+	/* EPP feature related attributes*/
+	s16	epp_powersave;
+	s16	epp_policy;
+	s16	epp_cached;
+	u32	policy;
+	u32	sched_flags;
+	bool	update_util_set;
+	u64	last_update;
+	u64	last_io_update;
+	u32	cppc_boost_min;
+	u64	cppc_cap1_cached;
+	struct	update_util_data update_util;
+	struct	amd_aperf_mperf sample;
+};
+
+/**
+ * struct amd_pstate_params - global parameters for the performance
+control
+ * @ cppc_boost_disabled wheher the core performance boost disabled
+*/ struct amd_pstate_params {
+	bool cppc_boost_disabled;
+};
+
+#define AMD_CPPC_EPP_PERFORMANCE		0x00
+#define AMD_CPPC_EPP_BALANCE_PERFORMANCE	0x80
+#define AMD_CPPC_EPP_BALANCE_POWERSAVE		0xBF
+#define AMD_CPPC_EPP_POWERSAVE			0xFF
+
+/*
+ * AMD Energy Preference Performance (EPP)
+ * The EPP is used in the CCLK DPM controller to drive
+ * the frequency that a core is going to operate during
+ * short periods of activity. EPP values will be utilized for
+ * different OS profiles (balanced, performance, power savings)
+ * display strings corresponding to EPP index in the
+ * energy_perf_strings[]
+ *	index		String
+ *-------------------------------------
+ *	0		default
+ *	1		performance
+ *	2		balance_performance
+ *	3		balance_power
+ *	4		power
+ */
+enum energy_perf_value_index {
+	EPP_INDEX_DEFAULT = 0,
+	EPP_INDEX_PERFORMANCE,
+	EPP_INDEX_BALANCE_PERFORMANCE,
+	EPP_INDEX_BALANCE_POWERSAVE,
+	EPP_INDEX_POWERSAVE,
+};
+
+static const char * const energy_perf_strings[] = {
+	[EPP_INDEX_DEFAULT] = "default",
+	[EPP_INDEX_PERFORMANCE] = "performance",

include/linux/amd-pstate.h struct amd_cpudata { · line 153

Quoted 10 lines
include/linux/amd-pstate.h +10 -0
+	[EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
+	[EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
+	[EPP_INDEX_POWERSAVE] = "power",
+	NULL
+};
+
+static unsigned int epp_values[] = {
+	[EPP_INDEX_DEFAULT] = 0,
+	[EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
+	[EPP_INDEX_BALANCE_PERFORMANCE] =

AMD_CPPC_EPP_BALANCE_PERFORMANCE,

include/linux/amd-pstate.h struct amd_cpudata { · line 154

Quoted 1 line
include/linux/amd-pstate.h +1 -0
+	[EPP_INDEX_BALANCE_POWERSAVE] =

AMD_CPPC_EPP_BALANCE_POWERSAVE,

Quoted 4 lines
include/linux/amd-pstate.h +1 -0
+	[EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
  };
 
  #endif /* _LINUX_AMD_PSTATE_H */
author reviewer
#22 lore raw

RE: [PATCH v4 9/9] Documentation: amd-pstate: add amd pstate driver mode introduction

In reply to Limonciello, Mario

[AMD Official Use Only - General]

Hi Mario

Quoted 16 lines

-----Original Message-----
From: Limonciello, Mario <[email protected]>
Sent: Tuesday, November 15, 2022 6:29 AM
To: Yuan, Perry <[email protected]>; [email protected]; Huang, Ray <[email protected]>; [email protected]
Cc: Sharma, Deepak <[email protected]>; Fontenot, Nathan <[email protected]>; Deucher, Alexander <[email protected]>; Huang, Shimmer <[email protected]>; Du, Xiaojian <[email protected]>; Meng, Li (Jassmine) <[email protected]>; Karny, Wyes <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v4 9/9] Documentation: amd-pstate: add amd pstate driver mode introduction

Please run this through a spell checker before v5.

Quoted 8 lines
Quoted 4 lines · Perry Yuan

Introduce ``amd-pstate`` CPPC has two operation modes:
* CPPC Autonomous (active) mode
* CPPC non-autonomous (passive) mode.
active mode and passive mode can be choosed by whith different kernel

parameters.

can be chosen by different kernel parameters

Forget to reply this,
I have fixed the words typo in the v6.
Thanks for your review.
Perry.

Quoted 92 lines
Quoted 5 lines
---
  Documentation/admin-guide/pm/amd-pstate.rst | 47

+++++++++++++++++++--

Documentation/admin-guide/pm/amd-pstate.rst efficiency frequency management method on

Quoted 8 lines
  1 file changed, 43 insertions(+), 4 deletions(-)
Documentation/admin-guide/pm/amd-pstate.rst +0 -0
index e7488891b12f..6ba02a658b31 100644
@@ -302,11 +302,11 @@ efficiency frequency management method on

AMD processors.

Documentation/admin-guide/pm/amd-pstate.rst efficiency frequency management method on AMD processors.

Quoted 8 lines
Documentation/admin-guide/pm/amd-pstate.rst +1 -3
  Kernel Module Options for ``amd-pstate``
  =========================================
 
-.. _shared_mem:
+.. legacy_cppc:
 
-``shared_mem``
-Use a module param (shared_mem) to enable related processors

manually

Documentation/admin-guide/pm/amd-pstate.rst

Quoted 5 lines

with -**amd_pstate.shared_mem=1**.
+``legacy_cppc``

Documentation/admin-guide/pm/amd-pstate.rst +2 -0
+Use a module param (legacy_cppc) to enable related processors
+manually with **amd_pstate=legacy_cppc**.
  Due to the performance issue on the processors with `Shared Memory

Support

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor

Quoted 3 lines

<perf_cap_>`_, we disable it presently and will re-enable this by default once we address performance issue with this solution.

Documentation/admin-guide/pm/amd-pstate.rst +0 -0
@@ -321,6 +321,45 @@ If the CPU flags have ``cppc``, then this processor

supports `Full MSR Support

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor · line 321

Quoted 1 line
Documentation/admin-guide/pm/amd-pstate.rst +0 -0
  <perf_cap_>`_. Otherwise, it supports `Shared Memory Support

<perf_cap_>`_.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor supports `Full MSR Support · line 324

Quoted 3 lines
Documentation/admin-guide/pm/amd-pstate.rst +1 -0
+AMD Pstae Driver Operation Modes

Pstate

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor · line 327

Quoted 5 lines
Documentation/admin-guide/pm/amd-pstate.rst +5 -0
+=================================
+
+``amd-pstate`` CPPC has two operation modes: CPPC Autonomous(active)
+mode and CPPC non-autonomous(passive) mode.
+active mode and passive mode can be choosed by whith different kernel

parameters.

can be chosen by different kernel parameters

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor · line 331

Quoted 4 lines
Documentation/admin-guide/pm/amd-pstate.rst +4 -0
+When in Autonomous mode, CPPC ignores requests done in the Desired
+Performance Target register and takes into account only the values
+set to the Minimum requested performance, Maximum requested
+performance, and Energy Performance Preference registers. When

Autonomous is disabled, it only considers the Desired Performance Target.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor supports `Full MSR Support

Quoted 8 lines
Documentation/admin-guide/pm/amd-pstate.rst +8 -0
+
+Active Mode
+------------
+
+``amd-pstate-epp``
+
+This is the low-level firmware control mode which is implemented by
+``amd-pstate-epp`` driver with ``amd-pstate=active`` passed to the kernel

in the command line.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor · line 341

Quoted 2 lines
Documentation/admin-guide/pm/amd-pstate.rst +2 -0
+In this mode, ``amd-pstate-epp`` driver provides a hint to the
+hardware if software wants to bias toward performance (0x0) or energy

efficiency (0xff) to the CPPC firmware.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor supports `Full MSR Support

Quoted 10 lines
Documentation/admin-guide/pm/amd-pstate.rst +10 -0
+then CPPC power algorithm will calculate the runtime workload and
+adjust the realtime cores frequency according to the power supply and
+thermal, core voltage and some other hardware conditions.
+
+Passive Mode
+------------
+
+``amd-pstate``
+
+It will be enabled if the ``amd_pstate=passive`` is passed to the kernel in

the command line.

Documentation/admin-guide/pm/amd-pstate.rst If the CPU flags have ``cppc``, then this processor · line 358

Quoted 7 lines
Documentation/admin-guide/pm/amd-pstate.rst +7 -0
+In this mode, ``amd-pstate``driver software specifies a desired QoS
+target in the CPPC performance scale as a relative number. This can
+be expressed as percentage of nominal performance (infrastructure
+max). Below the nominal sustained performance level, desired
+performance expresses the average performance level of the processor
+subject to the Performance Reduction Tolerance register. Above the
+nominal performance level, processor must provide at least nominal

performance requested and go higher if current operating conditions allow.

Quoted 5 lines
Documentation/admin-guide/pm/amd-pstate.rst +2 -0
+
+
  ``cpupower`` tool support for ``amd-pstate``
  ===============================================