1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include <drm/drm_device.h> 7 #include <linux/device.h> 8 #include <linux/kobject.h> 9 #include <linux/printk.h> 10 #include <linux/sysfs.h> 11 12 #include "i915_drv.h" 13 #include "i915_sysfs.h" 14 #include "intel_gt.h" 15 #include "intel_gt_sysfs.h" 16 #include "intel_gt_sysfs_pm.h" 17 #include "intel_gt_types.h" 18 #include "intel_rc6.h" 19 20 bool is_object_gt(struct kobject *kobj) 21 { 22 return !strncmp(kobj->name, "gt", 2); 23 } 24 25 struct intel_gt *intel_gt_sysfs_get_drvdata(struct kobject *kobj, 26 const char *name) 27 { 28 /* 29 * We are interested at knowing from where the interface 30 * has been called, whether it's called from gt/ or from 31 * the parent directory. 32 * From the interface position it depends also the value of 33 * the private data. 34 * If the interface is called from gt/ then private data is 35 * of the "struct intel_gt *" type, otherwise it's * a 36 * "struct drm_i915_private *" type. 37 */ 38 if (!is_object_gt(kobj)) { 39 struct device *dev = kobj_to_dev(kobj); 40 struct drm_i915_private *i915 = kdev_minor_to_i915(dev); 41 42 return to_gt(i915); 43 } 44 45 return kobj_to_gt(kobj); 46 } 47 48 static struct kobject *gt_get_parent_obj(struct intel_gt *gt) 49 { 50 return >->i915->drm.primary->kdev->kobj; 51 } 52 53 static ssize_t id_show(struct kobject *kobj, 54 struct kobj_attribute *attr, 55 char *buf) 56 { 57 struct intel_gt *gt = intel_gt_sysfs_get_drvdata(kobj, attr->attr.name); 58 59 return sysfs_emit(buf, "%u\n", gt->info.id); 60 } 61 static struct kobj_attribute attr_id = __ATTR_RO(id); 62 63 static struct attribute *id_attrs[] = { 64 &attr_id.attr, 65 NULL, 66 }; 67 ATTRIBUTE_GROUPS(id); 68 69 /* A kobject needs a release() method even if it does nothing */ 70 static void kobj_gt_release(struct kobject *kobj) 71 { 72 } 73 74 static struct kobj_type kobj_gt_type = { 75 .release = kobj_gt_release, 76 .sysfs_ops = &kobj_sysfs_ops, 77 .default_groups = id_groups, 78 }; 79 80 void intel_gt_sysfs_register(struct intel_gt *gt) 81 { 82 /* 83 * We need to make things right with the 84 * ABI compatibility. The files were originally 85 * generated under the parent directory. 86 * 87 * We generate the files only for gt 0 88 * to avoid duplicates. 89 */ 90 if (gt_is_root(gt)) 91 intel_gt_sysfs_pm_init(gt, gt_get_parent_obj(gt)); 92 93 /* init and xfer ownership to sysfs tree */ 94 if (kobject_init_and_add(>->sysfs_gt, &kobj_gt_type, 95 gt->i915->sysfs_gt, "gt%d", gt->info.id)) 96 goto exit_fail; 97 98 gt->sysfs_defaults = kobject_create_and_add(".defaults", >->sysfs_gt); 99 if (!gt->sysfs_defaults) 100 goto exit_fail; 101 102 intel_gt_sysfs_pm_init(gt, >->sysfs_gt); 103 104 return; 105 106 exit_fail: 107 kobject_put(>->sysfs_gt); 108 drm_warn(>->i915->drm, 109 "failed to initialize gt%d sysfs root\n", gt->info.id); 110 } 111 112 void intel_gt_sysfs_unregister(struct intel_gt *gt) 113 { 114 kobject_put(gt->sysfs_defaults); 115 kobject_put(>->sysfs_gt); 116 } 117