intel_idle: Add C1 demotion on/off sysfs knob
Subsystems (2)
Artem Bityutskiy
maintainerv3 cover intel_idle: Add C1 demotion on/off sysfs knob
From: Artem Bityutskiy <[email protected]>
Version 3 of the patch-set.
Changelog
v2->v3:
* Address comments from Rafael Wysocki:
1. Instead of passing a pointer to a bool value to intel_c1_demotion_toggle(), cast the bool value to "void *. This removes a need for pointer dereferencing in intel_c1_demotion_toggle().
2. Use reverse Xmass tree ordering for local variable definitions.
3. A spelling fix.v1->v2:
* Address comments from Rafael Wysocki:
1. Rename the sysfs file from 'c1_demotion' to 'intel_c1_demotion'
2. Add a commentary about reading the feature only for 1 CPU.
3. Update Documentation/API (new patch 3/3).
Artem Bityutskiy (3):
intel_idle: Add C1 demotion on/off sysfs knob
Documentation: admin-guide: pm: Document intel_idle C1 demotion
Documentation: ABI: testing: document the new cpuidle sysfs file
.../ABI/testing/sysfs-devices-system-cpu | 7 +- Documentation/admin-guide/pm/intel_idle.rst | 21 ++++ drivers/idle/intel_idle.c | 102 ++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.1
Artem Bityutskiy
maintainerPATCH v3 1/3 intel_idle: Add C1 demotion on/off sysfs knob
From: Artem Bityutskiy <[email protected]>
Add a sysfs knob to enable/disable C1 demotion for the following Intel
platforms: Sapphire Rapids Xeon, Emerald Rapids Xeon, Granite Rapids Xeon, Sierra Forest Xeon, and Grand Ridge SoC.
This sysfs file shows up as "/sys/devices/system/cpu/cpuidle/intel_c1_demotion".
The C1 demotion feature involves the platform firmware demoting deep C-state requests from the OS (e.g., C6 requests) to C1. The idea is that firmware monitors CPU wake-up rate, and if it is higher than a platform-specific threshold, the firmware demotes deep C-state requests to C1. For example, Linux requests C6, but firmware noticed too many wake-ups per second, and it keeps the CPU in C1. When the CPU stays in C1 long enough, the platform promotes it back to C6.
The default value for C1 demotion is whatever is configured by BIOS.
--- drivers/idle/intel_idle.c | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+)
drivers/idle/intel_idle.c +102 -0
@@ -48,9 +48,11 @@#include <trace/events/power.h> #include <linux/sched.h> #include <linux/sched/smt.h> +#include <linux/mutex.h> #include <linux/notifier.h> #include <linux/cpu.h> #include <linux/moduleparam.h> +#include <linux/sysfs.h> #include <asm/cpuid.h> #include <asm/cpu_device_id.h> #include <asm/intel-family.h>
@@ -91,9 +93,15 @@ struct idle_cpu {*/ unsigned long auto_demotion_disable_flags;bool disable_promotion_to_c1e; +bool c1_demotion_supported;bool use_acpi; }; +static bool c1_demotion_supported; +static DEFINE_MUTEX (c1_demotion_mutex); + +static struct device *sysfs_root__initdata ; +static const struct idle_cpu *icpu__initdata ;static struct cpuidle_state *cpuidle_state_table__initdata ;
@@ -1539,18 +1547,21 @@ static const struct idle_cpu idle_cpu_gmt __initconst = {static const struct idle_cpu idle_cpu_spr__initconst = { .state_table = spr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_gnr__initconst = { .state_table = gnr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_gnrd__initconst = { .state_table = gnrd_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };
@@ -1589,12 +1600,14 @@ static const struct idle_cpu idle_cpu_snr __initconst = {static const struct idle_cpu idle_cpu_grr__initconst = { .state_table = grr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_srf__initconst = { .state_table = srf_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };
@@ -2310,6 +2323,88 @@ static void __init intel_idle_cpuidle_devices_uninit(void)cpuidle_unregister_device (per_cpu_ptr (intel_idle_cpuidle_devices, i)); } +static void intel_c1_demotion_toggle (void *enable) +{ +unsigned long long msr_val; + +rdmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +/* +* Enable/disable C1 undemotion along with C1 demotion, as this is the +* most sensible configuration in general. +*/ +if (enable) + msr_val |=NHM_C1_AUTO_DEMOTE |SNB_C1_AUTO_UNDEMOTE ; +else + msr_val &= ~(NHM_C1_AUTO_DEMOTE |SNB_C1_AUTO_UNDEMOTE ); +wrmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +} + +static ssize_t intel_c1_demotion_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +bool enable; +int err; + + err =kstrtobool (buf, &enable); +if (err) +return err; + +mutex_lock (&c1_demotion_mutex); +/* Enable/disable C1 demotion on all CPUs */ +on_each_cpu (intel_c1_demotion_toggle, (void *)enable,1 ); +mutex_unlock (&c1_demotion_mutex); + +return count; +} + +static ssize_t intel_c1_demotion_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +unsigned long long msr_val; + +/* +* Read the MSR value for a CPU and assume it is the same for all CPUs. Any other +* configuration would be a BIOS bug. +*/ +rdmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +return sysfs_emit (buf,"%d\n" , !!(msr_val &NHM_C1_AUTO_DEMOTE )); +} +static DEVICE_ATTR_RW (intel_c1_demotion); + +static int __init intel_idle_sysfs_init (void ) +{ +int err; + +if (!c1_demotion_supported) +return 0 ; + + sysfs_root =bus_get_dev_root (&cpu_subsys); +if (!sysfs_root) +return 0 ; + + err =sysfs_add_file_to_group (&sysfs_root->kobj, + &dev_attr_intel_c1_demotion.attr, +"cpuidle" ); +if (err) { +put_device (sysfs_root); +return err; + } + +return 0 ; +} + +static void __init intel_idle_sysfs_uninit (void ) +{ +if (!sysfs_root) +return ; + +sysfs_remove_file_from_group (&sysfs_root->kobj, + &dev_attr_intel_c1_demotion.attr, +"cpuidle" ); +put_device (sysfs_root); +} +static int __init intel_idle_init (void ) {const struct x86_cpu_id *id;
@@ -2360,6 +2455,8 @@ static int __init intel_idle_init(void) auto_demotion_disable_flags = icpu->auto_demotion_disable_flags;if (icpu->disable_promotion_to_c1e) c1e_promotion =C1E_PROMOTION_DISABLE ; +if (icpu->c1_demotion_supported) + c1_demotion_supported = true;if (icpu->use_acpi || force_use_acpi)intel_idle_acpi_cst_extract (); }else if (!intel_idle_acpi_cst_extract ()) {
@@ -2373,6 +2470,10 @@ static int __init intel_idle_init(void)if (!intel_idle_cpuidle_devices)return -ENOMEM ; + retval =intel_idle_sysfs_init (); +if (retval) +pr_warn ("failed to initialized sysfs" ); +intel_idle_cpuidle_driver_init (&intel_idle_driver); retval =cpuidle_register_driver (&intel_idle_driver);
@@ -2397,6 +2498,7 @@ static int __init intel_idle_init(void)intel_idle_cpuidle_devices_uninit ();cpuidle_unregister_driver (&intel_idle_driver); init_driver_fail: +intel_idle_sysfs_uninit ();free_percpu (intel_idle_cpuidle_devices);return retval;
Signature · 1 line
2.48.1
Quoted 204 lines · Artem Bityutskiy
From: Artem Bityutskiy <[email protected]>
Add a sysfs knob to enable/disable C1 demotion for the following Intel
platforms: Sapphire Rapids Xeon, Emerald Rapids Xeon, Granite Rapids Xeon, Sierra Forest Xeon, and Grand Ridge SoC.This sysfs file shows up as "
/sys/devices/system/cpu/cpuidle/intel_c1_demotion".The C1 demotion feature involves the platform firmware demoting deep C-state requests from the OS (e.g., C6 requests) to C1. The idea is that firmware monitors CPU wake-up rate, and if it is higher than a platform-specific threshold, the firmware demotes deep C-state requests to C1. For example, Linux requests C6, but firmware noticed too many wake-ups per second, and it keeps the CPU in C1. When the CPU stays in C1 long enough, the platform promotes it back to C6.
The default value for C1 demotion is whatever is configured by BIOS.
Signed-off-by Artem Bityutskiy <[email protected]>--- drivers/idle/intel_idle.c | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+)drivers/idle/intel_idle.c +101
-0@@ -48,9 +48,11 @@#include <trace/events/power.h> #include <linux/sched.h> #include <linux/sched/smt.h> +#include <linux/mutex.h> #include <linux/notifier.h> #include <linux/cpu.h> #include <linux/moduleparam.h> +#include <linux/sysfs.h> #include <asm/cpuid.h> #include <asm/cpu_device_id.h> #include <asm/intel-family.h> @@ -91,9 +93,15 @@ struct idle_cpu {*/ unsigned long auto_demotion_disable_flags;bool disable_promotion_to_c1e; +bool c1_demotion_supported;bool use_acpi; }; +static bool c1_demotion_supported; +static DEFINE_MUTEX (c1_demotion_mutex); + +static struct device *sysfs_root__initdata ; +static const struct idle_cpu *icpu__initdata ;static struct cpuidle_state *cpuidle_state_table__initdata ;@@ -1539,18 +1547,21 @@ static const struct idle_cpu idle_cpu_gmt __initconst = {static const struct idle_cpu idle_cpu_spr__initconst = { .state_table = spr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_gnr__initconst = { .state_table = gnr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_gnrd__initconst = { .state_table = gnrd_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };@@ -1589,12 +1600,14 @@ static const struct idle_cpu idle_cpu_snr __initconst = {static const struct idle_cpu idle_cpu_grr__initconst = { .state_table = grr_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };static const struct idle_cpu idle_cpu_srf__initconst = { .state_table = srf_cstates, .disable_promotion_to_c1e = true, + .c1_demotion_supported = true, .use_acpi = true, };@@ -2310,6 +2323,88 @@ static void __init intel_idle_cpuidle_devices_uninit(void)cpuidle_unregister_device (per_cpu_ptr (intel_idle_cpuidle_devices, i)); } +static void intel_c1_demotion_toggle (void *enable) +{ +unsigned long long msr_val; + +rdmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +/* +* Enable/disable C1 undemotion along with C1 demotion, as this is the +* most sensible configuration in general. +*/ +if (enable) + msr_val |=NHM_C1_AUTO_DEMOTE |SNB_C1_AUTO_UNDEMOTE ; +else + msr_val &= ~(NHM_C1_AUTO_DEMOTE |SNB_C1_AUTO_UNDEMOTE ); +wrmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +} + +static ssize_t intel_c1_demotion_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +bool enable; +int err; + + err =kstrtobool (buf, &enable); +if (err) +return err; + +mutex_lock (&c1_demotion_mutex); +/* Enable/disable C1 demotion on all CPUs */ +on_each_cpu (intel_c1_demotion_toggle, (void *)enable,1 ); +mutex_unlock (&c1_demotion_mutex); + +return count; +} + +static ssize_t intel_c1_demotion_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +unsigned long long msr_val; + +/* +* Read the MSR value for a CPU and assume it is the same for all CPUs. Any other +* configuration would be a BIOS bug. +*/ +rdmsrl (MSR_PKG_CST_CONFIG_CONTROL , msr_val); +return sysfs_emit (buf,"%d\n" , !!(msr_val &NHM_C1_AUTO_DEMOTE )); +} +static DEVICE_ATTR_RW (intel_c1_demotion); + +static int __init intel_idle_sysfs_init (void ) +{ +int err; + +if (!c1_demotion_supported) +return 0 ; + + sysfs_root =bus_get_dev_root (&cpu_subsys); +if (!sysfs_root) +return 0 ; + + err =sysfs_add_file_to_group (&sysfs_root->kobj, + &dev_attr_intel_c1_demotion.attr, +"cpuidle" ); +if (err) { +put_device (sysfs_root); +return err; + } + +return 0 ; +} + +static void __init intel_idle_sysfs_uninit (void ) +{ +if (!sysfs_root) +return ; + +sysfs_remove_file_from_group (&sysfs_root->kobj, + &dev_attr_intel_c1_demotion.attr, +"cpuidle" ); +put_device (sysfs_root); +} +static int __init intel_idle_init (void ) {const struct x86_cpu_id *id;@@ -2360,6 +2455,8 @@ static int __init intel_idle_init(void) auto_demotion_disable_flags = icpu->auto_demotion_disable_flags;if (icpu->disable_promotion_to_c1e) c1e_promotion =C1E_PROMOTION_DISABLE ; +if (icpu->c1_demotion_supported) + c1_demotion_supported = true;if (icpu->use_acpi || force_use_acpi)intel_idle_acpi_cst_extract (); }else if (!intel_idle_acpi_cst_extract ()) {@@ -2373,6 +2470,10 @@ static int __init intel_idle_init(void)if (!intel_idle_cpuidle_devices)return -ENOMEM ; + retval =intel_idle_sysfs_init (); +if (retval) +pr_warn ("failed to initialized sysfs" ); +intel_idle_cpuidle_driver_init (&intel_idle_driver); retval =cpuidle_register_driver (&intel_idle_driver);
drivers/idle/intel_idle.c static int __init intel_idle_init(void)
Quoted 8 lines
drivers/idle/intel_idle.c +1
-1@@ -2397,6 +2498,7 @@ static int __init intel_idle_init(void)intel_idle_cpuidle_devices_uninit ();cpuidle_unregister_driver (&intel_idle_driver); init_driver_fail: +intel_idle_sysfs_uninit ();free_percpu (intel_idle_cpuidle_devices);return retval; --
Applied as 6.16 material along with the [2-3/3], thanks!
Artem Bityutskiy
maintainerPATCH v3 2/3 Documentation: admin-guide: pm: Document intel_idle C1 demotion
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Document the intel_idle driver sysfs file for enabling/disabling C1 demotion.
--- Documentation/admin-guide/pm/intel_idle.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
Documentation/admin-guide/pm/intel_idle.rst +21 -0
@@ -38,6 +38,27 @@ instruction at all. only way to pass early-configuration-time parameters to it is via the kernel command line. +Sysfs Interface +=============== + +The ``intel_idle`` driver exposes the following ``sysfs`` attributes in +``/sys/devices/system/cpu/cpuidle/``: + +``intel_c1_demotion`` + Enable or disable C1 demotion for all CPUs in the system. This file is + only exposed on platforms that support the C1 demotion feature and where + it was tested. Value 0 means that C1 demotion is disabled, value 1 means + that it is enabled. Write 0 or 1 to disable or enable C1 demotion for + all CPUs. + + The C1 demotion feature involves the platform firmware demoting deep + C-state requests from the OS (e.g., C6 requests) to C1. The idea is that + firmware monitors CPU wake-up rate, and if it is higher than a + platform-specific threshold, the firmware demotes deep C-state requests + to C1. For example, Linux requests C6, but firmware noticed too many + wake-ups per second, and it keeps the CPU in C1. When the CPU stays in + C1 long enough, the platform promotes it back to C6. This may improve + some workloads' performance, but it may also increase power consumption. .. _intel-idle-enumeration-of-states:
Signature · 1 line
2.48.1
Artem Bityutskiy
maintainerPATCH v3 3/3 Documentation: ABI: testing: document the new cpuidle sysfs file
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Mention the new 'intel_c1_demotion' sysfs file in the "cpuidle" section and refer to "Documentation/admin-guide/pm/intel_idle.rst" for more information.
--- Documentation/ABI/testing/sysfs-devices-system-cpu | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-devices-system-cpu +6 -1
@@ -111,6 +111,7 @@ What: /sys/devices/system/cpu/cpuidle/available_governors /sys/devices/system/cpu/cpuidle/current_driver /sys/devices/system/cpu/cpuidle/current_governor /sys/devices/system/cpu/cpuidle/current_governer_ro + /sys/devices/system/cpu/cpuidle/intel_c1_demotion Date: September 2007 Contact: Linux kernel mailing list <[email protected]> Description: Discover cpuidle policy and mechanism
@@ -132,7 +133,11 @@ Description: Discover cpuidle policy and mechanism current_governor_ro: (RO) displays current idle policy. - See Documentation/admin-guide/pm/cpuidle.rst and + intel_c1_demotion: (RW) enables/disables the C1 demotion + feature on Intel CPUs. + + See Documentation/admin-guide/pm/cpuidle.rst, + Documentation/admin-guide/pm/intel_idle.rst, and Documentation/driver-api/pm/cpuidle.rst for more information.
Signature · 1 line
2.48.1