Allow amd-pstate to change modes at runtime

Rather than checking for active mode at startup and then never checking
it again check the mode when trying to write a value.

If it's not active, log something to the journal. This is intentionally
not failing because users could still have an ACPI platform-profile change
occur.

Fixes: https://gitlab.freedesktop.org/upower/power-profiles-daemon/-/issues/168
This commit is contained in:
Mario Limonciello 2024-10-07 15:47:42 -05:00 committed by Mario Limonciello
parent 4a367430e8
commit 10aa0f2322
2 changed files with 73 additions and 16 deletions

View file

@ -58,18 +58,12 @@ probe_epp (PpdDriverAmdPstate *pstate)
g_autoptr(GDir) dir = NULL;
g_autofree char *policy_dir = NULL;
g_autofree char *pstate_status_path = NULL;
g_autofree char *status = NULL;
const char *dirname;
/* Verify that AMD P-State is running in active mode */
pstate_status_path = ppd_utils_get_sysfs_path (PSTATE_STATUS_PATH);
if (!g_file_get_contents (pstate_status_path, &status, NULL, NULL))
if (!g_file_test (pstate_status_path, G_FILE_TEST_EXISTS))
return PPD_PROBE_RESULT_FAIL;
status = g_strchomp (status);
if (g_strcmp0 (status, "active") != 0) {
g_debug ("AMD P-State is not running in active mode");
return PPD_PROBE_RESULT_FAIL;
}
policy_dir = ppd_utils_get_sysfs_path (CPUFREQ_POLICY_DIR);
dir = g_dir_open (policy_dir, 0, NULL);
@ -200,10 +194,21 @@ apply_pref_to_devices (GPtrArray *devices,
const char *gov_pref;
const char *cpb_pref;
const char *min_freq;
g_autofree char *status = NULL;
g_autofree char *pstate_status_path = NULL;
if (profile == PPD_PROFILE_UNSET)
return TRUE;
pstate_status_path = ppd_utils_get_sysfs_path (PSTATE_STATUS_PATH);
if (!g_file_get_contents (pstate_status_path, &status, NULL, error))
return FALSE;
status = g_strchomp (status);
if (g_strcmp0 (status, "active") != 0) {
g_warning ("AMD P-State is not in active mode");
return TRUE;
}
epp_pref = profile_to_epp_pref (profile, battery);
gov_pref = profile_to_gov_pref (profile);
cpb_pref = profile_to_cpb_pref (profile);

View file

@ -1112,6 +1112,58 @@ class Tests(dbusmock.DBusTestCase):
b"balance_performance",
)
# pylint: disable=too-many-statements
def test_amd_pstate_state_machine(self):
# Create 2 CPUs with preferences
dir1 = os.path.join(
self.testbed.get_root_dir(), "sys/devices/system/cpu/cpufreq/policy0/"
)
os.makedirs(dir1)
self.write_file_contents(os.path.join(dir1, "scaling_governor"), "powersave\n")
self.write_file_contents(
os.path.join(dir1, "energy_performance_preference"), "performance\n"
)
dir2 = os.path.join(
self.testbed.get_root_dir(), "sys/devices/system/cpu/cpufreq/policy1/"
)
os.makedirs(dir2)
self.write_file_contents(os.path.join(dir2, "scaling_governor"), "powersave\n")
self.write_file_contents(
os.path.join(dir2, "energy_performance_preference"), "performance\n"
)
# Create AMD P-State configuration
pstate_dir = os.path.join(
self.testbed.get_root_dir(), "sys/devices/system/cpu/amd_pstate"
)
os.makedirs(pstate_dir)
self.write_file_contents(os.path.join(pstate_dir, "status"), "active\n")
self.start_daemon()
profiles = self.get_dbus_property("Profiles")
self.assertEqual(len(profiles), 3)
self.assertEqual(profiles[0]["Driver"], "multiple")
self.assertEqual(profiles[0]["CpuDriver"], "amd_pstate")
self.assertEqual(profiles[0]["Profile"], "power-saver")
energy_prefs = os.path.join(dir2, "energy_performance_preference")
scaling_governor = os.path.join(dir2, "scaling_governor")
self.assert_file_eventually_contains(energy_prefs, "balance_performance")
self.assert_file_eventually_contains(scaling_governor, "powersave")
self.write_file_contents(os.path.join(pstate_dir, "status"), "passive\n")
# Set performance mode
self.set_dbus_property("ActiveProfile", GLib.Variant.new_string("performance"))
self.assertEqual(self.get_dbus_property("ActiveProfile"), "performance")
# ensure nothing changed
self.assert_file_eventually_contains(energy_prefs, "balance_performance")
self.assert_file_eventually_contains(scaling_governor, "powersave")
# pylint: disable=too-many-statements
def test_amd_pstate(self):
"""AMD P-State driver (no UPower)"""
@ -1387,7 +1439,7 @@ class Tests(dbusmock.DBusTestCase):
os.makedirs(dir1)
self.write_file_contents(os.path.join(dir1, "scaling_governor"), "powersave\n")
self.write_file_contents(
os.path.join(dir1, "energy_performance_preference"), "performance\n"
os.path.join(dir1, "energy_performance_preference"), "balance_performance\n"
)
# Create AMD P-State configuration
@ -1400,19 +1452,19 @@ class Tests(dbusmock.DBusTestCase):
self.start_daemon()
profiles = self.get_dbus_property("Profiles")
self.assertEqual(len(profiles), 2)
self.assertEqual(profiles[0]["Driver"], "placeholder")
self.assertEqual(len(profiles), 3)
self.assertEqual(profiles[0]["Driver"], "multiple")
self.assertEqual(profiles[0]["PlatformDriver"], "placeholder")
self.assertEqual(profiles[0]["CpuDriver"], "amd_pstate")
self.assertEqual(self.get_dbus_property("ActiveProfile"), "balanced")
energy_prefs = os.path.join(dir1, "energy_performance_preference")
self.assert_file_eventually_contains(energy_prefs, "performance\n")
# Set performance mode
self.set_dbus_property("ActiveProfile", GLib.Variant.new_string("power-saver"))
self.assertEqual(self.get_dbus_property("ActiveProfile"), "power-saver")
self.set_dbus_property("ActiveProfile", GLib.Variant.new_string("performance"))
self.assertEqual(self.get_dbus_property("ActiveProfile"), "performance")
self.assert_file_eventually_contains(energy_prefs, "performance\n")
# Shouldn't have updated
energy_prefs = os.path.join(dir1, "energy_performance_preference")
self.assert_file_eventually_contains(energy_prefs, "balance_performance")
def test_dytc_performance_driver(self):
"""Lenovo DYTC performance driver"""