1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
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>
13 #include "i915_sysfs.h"
15 #include "intel_gt_print.h"
16 #include "intel_gt_sysfs.h"
17 #include "intel_gt_sysfs_pm.h"
18 #include "intel_gt_types.h"
19 #include "intel_rc6.h"
21 bool is_object_gt(struct kobject *kobj)
23 return !strncmp(kobj->name, "gt", 2);
26 struct intel_gt *intel_gt_sysfs_get_drvdata(struct kobject *kobj,
30 * We are interested at knowing from where the interface
31 * has been called, whether it's called from gt/ or from
32 * the parent directory.
33 * From the interface position it depends also the value of
35 * If the interface is called from gt/ then private data is
36 * of the "struct intel_gt *" type, otherwise it's * a
37 * "struct drm_i915_private *" type.
39 if (!is_object_gt(kobj)) {
40 struct device *dev = kobj_to_dev(kobj);
41 struct drm_i915_private *i915 = kdev_minor_to_i915(dev);
46 return kobj_to_gt(kobj);
49 static struct kobject *gt_get_parent_obj(struct intel_gt *gt)
51 return >->i915->drm.primary->kdev->kobj;
54 static ssize_t id_show(struct kobject *kobj,
55 struct kobj_attribute *attr,
58 struct intel_gt *gt = intel_gt_sysfs_get_drvdata(kobj, attr->attr.name);
60 return sysfs_emit(buf, "%u\n", gt->info.id);
62 static struct kobj_attribute attr_id = __ATTR_RO(id);
64 static struct attribute *id_attrs[] = {
70 /* A kobject needs a release() method even if it does nothing */
71 static void kobj_gt_release(struct kobject *kobj)
75 static const struct kobj_type kobj_gt_type = {
76 .release = kobj_gt_release,
77 .sysfs_ops = &kobj_sysfs_ops,
78 .default_groups = id_groups,
81 void intel_gt_sysfs_register(struct intel_gt *gt)
84 * We need to make things right with the
85 * ABI compatibility. The files were originally
86 * generated under the parent directory.
88 * We generate the files only for gt 0
89 * to avoid duplicates.
92 intel_gt_sysfs_pm_init(gt, gt_get_parent_obj(gt));
94 /* init and xfer ownership to sysfs tree */
95 if (kobject_init_and_add(>->sysfs_gt, &kobj_gt_type,
96 gt->i915->sysfs_gt, "gt%d", gt->info.id))
99 gt->sysfs_defaults = kobject_create_and_add(".defaults", >->sysfs_gt);
100 if (!gt->sysfs_defaults)
103 intel_gt_sysfs_pm_init(gt, >->sysfs_gt);
108 kobject_put(>->sysfs_gt);
109 gt_warn(gt, "failed to initialize sysfs root\n");
112 void intel_gt_sysfs_unregister(struct intel_gt *gt)
114 kobject_put(gt->sysfs_defaults);
115 kobject_put(>->sysfs_gt);