]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
drm/xe/sysfs: Simplify sysfs registration
authorMichal Wajdeczko <michal.wajdeczko@intel.com>
Tue, 16 Sep 2025 17:00:29 +0000 (19:00 +0200)
committerMichal Wajdeczko <michal.wajdeczko@intel.com>
Wed, 17 Sep 2025 19:59:31 +0000 (21:59 +0200)
Instead of manually maintaining each sysfs file define and use
attribute groups and register them using device managed function.
Then use is_visible() to filter-out unsupported attributes.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Raag Jadav <raag.jadav@intel.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20250916170029.3313-3-michal.wajdeczko@intel.com
drivers/gpu/drm/xe/xe_device_sysfs.c

index 896484c8fbcc73049cb0e850d27cca1e14052cc6..c5151c86a98ae3fdb8bf0213a145c37471df2897 100644 (file)
@@ -71,6 +71,15 @@ vram_d3cold_threshold_store(struct device *dev, struct device_attribute *attr,
 
 static DEVICE_ATTR_RW(vram_d3cold_threshold);
 
+static struct attribute *vram_attrs[] = {
+       &dev_attr_vram_d3cold_threshold.attr,
+       NULL
+};
+
+static const struct attribute_group vram_attr_group = {
+       .attrs = vram_attrs,
+};
+
 static ssize_t
 lb_fan_control_version_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -149,41 +158,16 @@ out:
 }
 static DEVICE_ATTR_ADMIN_RO(lb_voltage_regulator_version);
 
-static int late_bind_create_files(struct device *dev)
-{
-       struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
-       struct xe_tile *root = xe_device_get_root_tile(xe);
-       u32 cap = 0;
-       int ret;
-
-       xe_pm_runtime_get(xe);
-
-       ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
-                           &cap, NULL);
-       if (ret) {
-               if (ret == -ENXIO) {
-                       drm_dbg(&xe->drm, "Late binding not supported by firmware\n");
-                       ret = 0;
-               }
-               goto out;
-       }
-
-       if (REG_FIELD_GET(V1_FAN_SUPPORTED, cap)) {
-               ret = sysfs_create_file(&dev->kobj, &dev_attr_lb_fan_control_version.attr);
-               if (ret)
-                       goto out;
-       }
-
-       if (REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap))
-               ret = sysfs_create_file(&dev->kobj, &dev_attr_lb_voltage_regulator_version.attr);
-out:
-       xe_pm_runtime_put(xe);
-
-       return ret;
-}
+static struct attribute *late_bind_attrs[] = {
+       &dev_attr_lb_fan_control_version.attr,
+       &dev_attr_lb_voltage_regulator_version.attr,
+       NULL
+};
 
-static void late_bind_remove_files(struct device *dev)
+static umode_t late_bind_attr_is_visible(struct kobject *kobj,
+                                        struct attribute *attr, int n)
 {
+       struct device *dev = kobj_to_dev(kobj);
        struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev));
        struct xe_tile *root = xe_device_get_root_tile(xe);
        u32 cap = 0;
@@ -193,18 +177,25 @@ static void late_bind_remove_files(struct device *dev)
 
        ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0),
                            &cap, NULL);
+       xe_pm_runtime_put(xe);
        if (ret)
-               goto out;
+               return 0;
 
-       if (REG_FIELD_GET(V1_FAN_SUPPORTED, cap))
-               sysfs_remove_file(&dev->kobj, &dev_attr_lb_fan_control_version.attr);
+       if (attr == &dev_attr_lb_fan_control_version.attr &&
+           REG_FIELD_GET(V1_FAN_SUPPORTED, cap))
+               return attr->mode;
+       if (attr == &dev_attr_lb_voltage_regulator_version.attr &&
+           REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap))
+               return attr->mode;
 
-       if (REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap))
-               sysfs_remove_file(&dev->kobj, &dev_attr_lb_voltage_regulator_version.attr);
-out:
-       xe_pm_runtime_put(xe);
+       return 0;
 }
 
+static const struct attribute_group late_bind_attr_group = {
+       .attrs = late_bind_attrs,
+       .is_visible = late_bind_attr_is_visible,
+};
+
 /**
  * DOC: PCIe Gen5 Limitations
  *
@@ -278,24 +269,15 @@ auto_link_downgrade_status_show(struct device *dev, struct device_attribute *att
 }
 static DEVICE_ATTR_ADMIN_RO(auto_link_downgrade_status);
 
-static const struct attribute *auto_link_downgrade_attrs[] = {
+static struct attribute *auto_link_downgrade_attrs[] = {
        &dev_attr_auto_link_downgrade_capable.attr,
        &dev_attr_auto_link_downgrade_status.attr,
        NULL
 };
 
-static void xe_device_sysfs_fini(void *arg)
-{
-       struct xe_device *xe = arg;
-
-       if (xe->d3cold.capable)
-               sysfs_remove_file(&xe->drm.dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
-
-       if (xe->info.platform == XE_BATTLEMAGE) {
-               sysfs_remove_files(&xe->drm.dev->kobj, auto_link_downgrade_attrs);
-               late_bind_remove_files(xe->drm.dev);
-       }
-}
+static const struct attribute_group auto_link_downgrade_attr_group = {
+       .attrs = auto_link_downgrade_attrs,
+};
 
 int xe_device_sysfs_init(struct xe_device *xe)
 {
@@ -303,24 +285,20 @@ int xe_device_sysfs_init(struct xe_device *xe)
        int ret;
 
        if (xe->d3cold.capable) {
-               ret = sysfs_create_file(&dev->kobj, &dev_attr_vram_d3cold_threshold.attr);
+               ret = devm_device_add_group(dev, &vram_attr_group);
                if (ret)
                        return ret;
        }
 
        if (xe->info.platform == XE_BATTLEMAGE && !IS_SRIOV_VF(xe)) {
-               ret = sysfs_create_files(&dev->kobj, auto_link_downgrade_attrs);
+               ret = devm_device_add_group(dev, &auto_link_downgrade_attr_group);
                if (ret)
-                       goto cleanup;
+                       return ret;
 
-               ret = late_bind_create_files(dev);
+               ret = devm_device_add_group(dev, &late_bind_attr_group);
                if (ret)
-                       goto cleanup;
+                       return ret;
        }
 
-       return devm_add_action_or_reset(dev, xe_device_sysfs_fini, xe);
-
-cleanup:
-       xe_device_sysfs_fini(xe);
-       return ret;
+       return 0;
 }