Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm/xe/sysfs: Simplify sysfs registration

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

+41 -63
+41 -63
drivers/gpu/drm/xe/xe_device_sysfs.c
··· 71 71 72 72 static DEVICE_ATTR_RW(vram_d3cold_threshold); 73 73 74 + static struct attribute *vram_attrs[] = { 75 + &dev_attr_vram_d3cold_threshold.attr, 76 + NULL 77 + }; 78 + 79 + static const struct attribute_group vram_attr_group = { 80 + .attrs = vram_attrs, 81 + }; 82 + 74 83 static ssize_t 75 84 lb_fan_control_version_show(struct device *dev, struct device_attribute *attr, char *buf) 76 85 { ··· 158 149 } 159 150 static DEVICE_ATTR_ADMIN_RO(lb_voltage_regulator_version); 160 151 161 - static int late_bind_create_files(struct device *dev) 152 + static struct attribute *late_bind_attrs[] = { 153 + &dev_attr_lb_fan_control_version.attr, 154 + &dev_attr_lb_voltage_regulator_version.attr, 155 + NULL 156 + }; 157 + 158 + static umode_t late_bind_attr_is_visible(struct kobject *kobj, 159 + struct attribute *attr, int n) 162 160 { 161 + struct device *dev = kobj_to_dev(kobj); 163 162 struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); 164 163 struct xe_tile *root = xe_device_get_root_tile(xe); 165 164 u32 cap = 0; ··· 177 160 178 161 ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0), 179 162 &cap, NULL); 180 - if (ret) { 181 - if (ret == -ENXIO) { 182 - drm_dbg(&xe->drm, "Late binding not supported by firmware\n"); 183 - ret = 0; 184 - } 185 - goto out; 186 - } 187 - 188 - if (REG_FIELD_GET(V1_FAN_SUPPORTED, cap)) { 189 - ret = sysfs_create_file(&dev->kobj, &dev_attr_lb_fan_control_version.attr); 190 - if (ret) 191 - goto out; 192 - } 193 - 194 - if (REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap)) 195 - ret = sysfs_create_file(&dev->kobj, &dev_attr_lb_voltage_regulator_version.attr); 196 - out: 197 163 xe_pm_runtime_put(xe); 198 - 199 - return ret; 200 - } 201 - 202 - static void late_bind_remove_files(struct device *dev) 203 - { 204 - struct xe_device *xe = pdev_to_xe_device(to_pci_dev(dev)); 205 - struct xe_tile *root = xe_device_get_root_tile(xe); 206 - u32 cap = 0; 207 - int ret; 208 - 209 - xe_pm_runtime_get(xe); 210 - 211 - ret = xe_pcode_read(root, PCODE_MBOX(PCODE_LATE_BINDING, GET_CAPABILITY_STATUS, 0), 212 - &cap, NULL); 213 164 if (ret) 214 - goto out; 165 + return 0; 215 166 216 - if (REG_FIELD_GET(V1_FAN_SUPPORTED, cap)) 217 - sysfs_remove_file(&dev->kobj, &dev_attr_lb_fan_control_version.attr); 167 + if (attr == &dev_attr_lb_fan_control_version.attr && 168 + REG_FIELD_GET(V1_FAN_SUPPORTED, cap)) 169 + return attr->mode; 170 + if (attr == &dev_attr_lb_voltage_regulator_version.attr && 171 + REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap)) 172 + return attr->mode; 218 173 219 - if (REG_FIELD_GET(VR_PARAMS_SUPPORTED, cap)) 220 - sysfs_remove_file(&dev->kobj, &dev_attr_lb_voltage_regulator_version.attr); 221 - out: 222 - xe_pm_runtime_put(xe); 174 + return 0; 223 175 } 176 + 177 + static const struct attribute_group late_bind_attr_group = { 178 + .attrs = late_bind_attrs, 179 + .is_visible = late_bind_attr_is_visible, 180 + }; 224 181 225 182 /** 226 183 * DOC: PCIe Gen5 Limitations ··· 269 278 } 270 279 static DEVICE_ATTR_ADMIN_RO(auto_link_downgrade_status); 271 280 272 - static const struct attribute *auto_link_downgrade_attrs[] = { 281 + static struct attribute *auto_link_downgrade_attrs[] = { 273 282 &dev_attr_auto_link_downgrade_capable.attr, 274 283 &dev_attr_auto_link_downgrade_status.attr, 275 284 NULL 276 285 }; 277 286 278 - static void xe_device_sysfs_fini(void *arg) 279 - { 280 - struct xe_device *xe = arg; 281 - 282 - if (xe->d3cold.capable) 283 - sysfs_remove_file(&xe->drm.dev->kobj, &dev_attr_vram_d3cold_threshold.attr); 284 - 285 - if (xe->info.platform == XE_BATTLEMAGE) { 286 - sysfs_remove_files(&xe->drm.dev->kobj, auto_link_downgrade_attrs); 287 - late_bind_remove_files(xe->drm.dev); 288 - } 289 - } 287 + static const struct attribute_group auto_link_downgrade_attr_group = { 288 + .attrs = auto_link_downgrade_attrs, 289 + }; 290 290 291 291 int xe_device_sysfs_init(struct xe_device *xe) 292 292 { ··· 285 303 int ret; 286 304 287 305 if (xe->d3cold.capable) { 288 - ret = sysfs_create_file(&dev->kobj, &dev_attr_vram_d3cold_threshold.attr); 306 + ret = devm_device_add_group(dev, &vram_attr_group); 289 307 if (ret) 290 308 return ret; 291 309 } 292 310 293 311 if (xe->info.platform == XE_BATTLEMAGE && !IS_SRIOV_VF(xe)) { 294 - ret = sysfs_create_files(&dev->kobj, auto_link_downgrade_attrs); 312 + ret = devm_device_add_group(dev, &auto_link_downgrade_attr_group); 295 313 if (ret) 296 - goto cleanup; 314 + return ret; 297 315 298 - ret = late_bind_create_files(dev); 316 + ret = devm_device_add_group(dev, &late_bind_attr_group); 299 317 if (ret) 300 - goto cleanup; 318 + return ret; 301 319 } 302 320 303 - return devm_add_action_or_reset(dev, xe_device_sysfs_fini, xe); 304 - 305 - cleanup: 306 - xe_device_sysfs_fini(xe); 307 - return ret; 321 + return 0; 308 322 }