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