[PATCH v2 1/2] drm/amd: Reduce sysfs and debugfs attribute boilerplate

Mario Limonciello mario.limonciello at amd.com
Fri Aug 23 14:55:26 UTC 2024


All accesses to attributes have a check for reset/suspend/runpm. Move this
code into dedicated functions. No intended functional changes.

Signed-off-by: Mario Limonciello <mario.limonciello at amd.com>
---
v1->v2:
 * Split out to two patches to reduce boilerplate with common functions
 * Different behavior for sysfs and debugfs

 drivers/gpu/drm/amd/pm/amdgpu_pm.c | 194 +++++++++--------------------
 1 file changed, 59 insertions(+), 135 deletions(-)

diff --git a/drivers/gpu/drm/amd/pm/amdgpu_pm.c b/drivers/gpu/drm/amd/pm/amdgpu_pm.c
index d5d6ab484e5ad..bbd6f3e617358 100644
--- a/drivers/gpu/drm/amd/pm/amdgpu_pm.c
+++ b/drivers/gpu/drm/amd/pm/amdgpu_pm.c
@@ -97,6 +97,20 @@ const char * const amdgpu_pp_profile_name[] = {
 	"UNCAPPED",
 };
 
+static bool amdgpu_sysfs_attr_accessible(struct amdgpu_device *adev)
+{
+	if (amdgpu_in_reset(adev))
+		return false;
+	if (adev->in_suspend && !adev->in_runpm)
+		return false;
+	return true;
+}
+
+static bool amdgpu_debugfs_attr_accessible(struct amdgpu_device *adev)
+{
+	return amdgpu_sysfs_attr_accessible(adev);
+}
+
 /**
  * DOC: power_dpm_state
  *
@@ -140,9 +154,7 @@ static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
 	enum amd_pm_state_type pm;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -171,9 +183,7 @@ static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
 	enum amd_pm_state_type  state;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (strncmp("battery", buf, strlen("battery")) == 0)
@@ -268,9 +278,7 @@ static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
 	enum amd_dpm_forced_level level = 0xff;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -307,9 +315,7 @@ static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
 	enum amd_dpm_forced_level level;
 	int ret = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (strncmp("low", buf, strlen("low")) == 0) {
@@ -369,9 +375,7 @@ static ssize_t amdgpu_get_pp_num_states(struct device *dev,
 	uint32_t i;
 	int buf_len, ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -407,9 +411,7 @@ static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
 	enum amd_pm_state_type pm = 0;
 	int i = 0, ret = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -446,9 +448,7 @@ static ssize_t amdgpu_get_pp_force_state(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (adev->pm.pp_force_state_enabled)
@@ -469,9 +469,7 @@ static ssize_t amdgpu_set_pp_force_state(struct device *dev,
 	unsigned long idx;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	adev->pm.pp_force_state_enabled = false;
@@ -539,9 +537,7 @@ static ssize_t amdgpu_get_pp_table(struct device *dev,
 	char *table = NULL;
 	int size, ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -575,9 +571,7 @@ static ssize_t amdgpu_set_pp_table(struct device *dev,
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 	int ret = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -758,9 +752,7 @@ static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
 	const char delimiter[3] = {' ', '\n', '\0'};
 	uint32_t type;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (count > 127 || count == 0)
@@ -860,9 +852,7 @@ static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
 	};
 	uint clk_index;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -920,9 +910,7 @@ static ssize_t amdgpu_set_pp_features(struct device *dev,
 	uint64_t featuremask;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = kstrtou64(buf, 0, &featuremask);
@@ -955,9 +943,7 @@ static ssize_t amdgpu_get_pp_features(struct device *dev,
 	ssize_t size;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1024,9 +1010,7 @@ static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
 	int size = 0;
 	int ret = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1093,9 +1077,7 @@ static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
 	int ret;
 	uint32_t mask = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = amdgpu_read_mask(buf, count, &mask);
@@ -1278,9 +1260,7 @@ static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
 	uint32_t value = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1307,9 +1287,7 @@ static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
 	int ret;
 	long int value;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = kstrtol(buf, 0, &value);
@@ -1340,9 +1318,7 @@ static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
 	uint32_t value = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1369,9 +1345,7 @@ static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
 	int ret;
 	long int value;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = kstrtol(buf, 0, &value);
@@ -1422,9 +1396,7 @@ static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
 	ssize_t size;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1461,9 +1433,7 @@ static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
 	long int profile_mode = 0;
 	const char delimiter[3] = {' ', '\n', '\0'};
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	tmp[0] = *(buf);
@@ -1515,9 +1485,7 @@ static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
 {
 	int r, size = sizeof(uint32_t);
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -1628,9 +1596,7 @@ static ssize_t amdgpu_get_pcie_bw(struct device *dev,
 	uint64_t count0 = 0, count1 = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (adev->flags & AMD_IS_APU)
@@ -1671,9 +1637,7 @@ static ssize_t amdgpu_get_unique_id(struct device *dev,
 	struct drm_device *ddev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(ddev);
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (adev->unique_id)
@@ -1844,9 +1808,7 @@ static ssize_t amdgpu_get_pm_metrics(struct device *dev,
 	ssize_t size = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -1885,9 +1847,7 @@ static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
 	ssize_t size = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(ddev->dev);
@@ -2003,9 +1963,7 @@ static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
 	int r = 0;
 	int bias = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	r = pm_runtime_get_sync(ddev->dev);
@@ -2284,9 +2242,7 @@ static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
 	policy_attr =
 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
@@ -2305,9 +2261,7 @@ static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
 	char *tmp, *param;
 	long val;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	count = min(count, sizeof(tmp_buf));
@@ -2767,9 +2721,7 @@ static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
 	u32 pwm_mode = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -2799,9 +2751,7 @@ static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
 	u32 pwm_mode;
 	int value;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = kstrtoint(buf, 10, &value);
@@ -2857,9 +2807,7 @@ static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
 	u32 value;
 	u32 pwm_mode;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = kstrtou32(buf, 10, &value);
@@ -2902,9 +2850,7 @@ static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
 	int err;
 	u32 speed = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -2932,9 +2878,7 @@ static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
 	int err;
 	u32 speed = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -2996,9 +2940,7 @@ static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
 	int err;
 	u32 rpm = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -3027,9 +2969,7 @@ static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
 	u32 value;
 	u32 pwm_mode;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = kstrtou32(buf, 10, &value);
@@ -3071,9 +3011,7 @@ static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
 	u32 pwm_mode = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -3103,9 +3041,7 @@ static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
 	int value;
 	u32 pwm_mode;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	err = kstrtoint(buf, 10, &value);
@@ -3243,9 +3179,7 @@ static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
 	ssize_t size;
 	int r;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
@@ -3324,9 +3258,7 @@ static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
 	int err;
 	u32 value;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	if (amdgpu_sriov_vf(adev))
@@ -3782,9 +3714,7 @@ static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
 	int size = 0;
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = pm_runtime_get_sync(adev->dev);
@@ -3866,9 +3796,7 @@ amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
 	long parameter[64];
 	int ret;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_sysfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = parse_input_od_command_lines(in_buf,
@@ -4753,9 +4681,7 @@ static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
 	u64 flags = 0;
 	int r;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_debugfs_attr_accessible(adev))
 		return -EPERM;
 
 	r = pm_runtime_get_sync(dev->dev);
@@ -4798,9 +4724,7 @@ static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
 	void *smu_prv_buf;
 	int ret = 0;
 
-	if (amdgpu_in_reset(adev))
-		return -EPERM;
-	if (adev->in_suspend && !adev->in_runpm)
+	if (!amdgpu_debugfs_attr_accessible(adev))
 		return -EPERM;
 
 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
-- 
2.43.0



More information about the amd-gfx mailing list