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_sysfs.h"
16 #include "intel_gt_sysfs_pm.h"
17 #include "intel_gt_types.h"
18 #include "intel_rc6.h"
20 bool is_object_gt(struct kobject *kobj)
22 return !strncmp(kobj->name, "gt", 2);
25 static struct intel_gt *kobj_to_gt(struct kobject *kobj)
27 return container_of(kobj, struct intel_gt, sysfs_gt);
30 struct intel_gt *intel_gt_sysfs_get_drvdata(struct device *dev,
33 struct kobject *kobj = &dev->kobj;
36 * We are interested at knowing from where the interface
37 * has been called, whether it's called from gt/ or from
38 * the parent directory.
39 * From the interface position it depends also the value of
41 * If the interface is called from gt/ then private data is
42 * of the "struct intel_gt *" type, otherwise it's * a
43 * "struct drm_i915_private *" type.
45 if (!is_object_gt(kobj)) {
46 struct drm_i915_private *i915 = kdev_minor_to_i915(dev);
51 return kobj_to_gt(kobj);
54 static struct kobject *gt_get_parent_obj(struct intel_gt *gt)
56 return >->i915->drm.primary->kdev->kobj;
59 static ssize_t id_show(struct device *dev,
60 struct device_attribute *attr,
63 struct intel_gt *gt = intel_gt_sysfs_get_drvdata(dev, attr->attr.name);
65 return sysfs_emit(buf, "%u\n", gt->info.id);
67 static DEVICE_ATTR_RO(id);
69 static struct attribute *id_attrs[] = {
75 /* A kobject needs a release() method even if it does nothing */
76 static void kobj_gt_release(struct kobject *kobj)
80 static struct kobj_type kobj_gt_type = {
81 .release = kobj_gt_release,
82 .sysfs_ops = &kobj_sysfs_ops,
83 .default_groups = id_groups,
86 void intel_gt_sysfs_register(struct intel_gt *gt)
89 * We need to make things right with the
90 * ABI compatibility. The files were originally
91 * generated under the parent directory.
93 * We generate the files only for gt 0
94 * to avoid duplicates.
97 intel_gt_sysfs_pm_init(gt, gt_get_parent_obj(gt));
99 /* init and xfer ownership to sysfs tree */
100 if (kobject_init_and_add(>->sysfs_gt, &kobj_gt_type,
101 gt->i915->sysfs_gt, "gt%d", gt->info.id))
104 intel_gt_sysfs_pm_init(gt, >->sysfs_gt);
109 kobject_put(>->sysfs_gt);
110 drm_warn(>->i915->drm,
111 "failed to initialize gt%d sysfs root\n", gt->info.id);
114 void intel_gt_sysfs_unregister(struct intel_gt *gt)
116 kobject_put(>->sysfs_gt);