Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright © 2022 Intel Corporation
0004  */
0005 
0006 #include <drm/drm_device.h>
0007 #include <linux/device.h>
0008 #include <linux/kobject.h>
0009 #include <linux/printk.h>
0010 #include <linux/sysfs.h>
0011 
0012 #include "i915_drv.h"
0013 #include "i915_sysfs.h"
0014 #include "intel_gt.h"
0015 #include "intel_gt_sysfs.h"
0016 #include "intel_gt_sysfs_pm.h"
0017 #include "intel_gt_types.h"
0018 #include "intel_rc6.h"
0019 
0020 bool is_object_gt(struct kobject *kobj)
0021 {
0022     return !strncmp(kobj->name, "gt", 2);
0023 }
0024 
0025 static struct intel_gt *kobj_to_gt(struct kobject *kobj)
0026 {
0027     return container_of(kobj, struct intel_gt, sysfs_gt);
0028 }
0029 
0030 struct intel_gt *intel_gt_sysfs_get_drvdata(struct device *dev,
0031                         const char *name)
0032 {
0033     struct kobject *kobj = &dev->kobj;
0034 
0035     /*
0036      * We are interested at knowing from where the interface
0037      * has been called, whether it's called from gt/ or from
0038      * the parent directory.
0039      * From the interface position it depends also the value of
0040      * the private data.
0041      * If the interface is called from gt/ then private data is
0042      * of the "struct intel_gt *" type, otherwise it's * a
0043      * "struct drm_i915_private *" type.
0044      */
0045     if (!is_object_gt(kobj)) {
0046         struct drm_i915_private *i915 = kdev_minor_to_i915(dev);
0047 
0048         return to_gt(i915);
0049     }
0050 
0051     return kobj_to_gt(kobj);
0052 }
0053 
0054 static struct kobject *gt_get_parent_obj(struct intel_gt *gt)
0055 {
0056     return &gt->i915->drm.primary->kdev->kobj;
0057 }
0058 
0059 static ssize_t id_show(struct device *dev,
0060                struct device_attribute *attr,
0061                char *buf)
0062 {
0063     struct intel_gt *gt = intel_gt_sysfs_get_drvdata(dev, attr->attr.name);
0064 
0065     return sysfs_emit(buf, "%u\n", gt->info.id);
0066 }
0067 static DEVICE_ATTR_RO(id);
0068 
0069 static struct attribute *id_attrs[] = {
0070     &dev_attr_id.attr,
0071     NULL,
0072 };
0073 ATTRIBUTE_GROUPS(id);
0074 
0075 /* A kobject needs a release() method even if it does nothing */
0076 static void kobj_gt_release(struct kobject *kobj)
0077 {
0078 }
0079 
0080 static struct kobj_type kobj_gt_type = {
0081     .release = kobj_gt_release,
0082     .sysfs_ops = &kobj_sysfs_ops,
0083     .default_groups = id_groups,
0084 };
0085 
0086 void intel_gt_sysfs_register(struct intel_gt *gt)
0087 {
0088     /*
0089      * We need to make things right with the
0090      * ABI compatibility. The files were originally
0091      * generated under the parent directory.
0092      *
0093      * We generate the files only for gt 0
0094      * to avoid duplicates.
0095      */
0096     if (gt_is_root(gt))
0097         intel_gt_sysfs_pm_init(gt, gt_get_parent_obj(gt));
0098 
0099     /* init and xfer ownership to sysfs tree */
0100     if (kobject_init_and_add(&gt->sysfs_gt, &kobj_gt_type,
0101                  gt->i915->sysfs_gt, "gt%d", gt->info.id))
0102         goto exit_fail;
0103 
0104     intel_gt_sysfs_pm_init(gt, &gt->sysfs_gt);
0105 
0106     return;
0107 
0108 exit_fail:
0109     kobject_put(&gt->sysfs_gt);
0110     drm_warn(&gt->i915->drm,
0111          "failed to initialize gt%d sysfs root\n", gt->info.id);
0112 }
0113 
0114 void intel_gt_sysfs_unregister(struct intel_gt *gt)
0115 {
0116     kobject_put(&gt->sysfs_gt);
0117 }