1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_gt_sysfs.h"
7 
8 #include <linux/kobject.h>
9 #include <linux/sysfs.h>
10 
11 #include <drm/drm_managed.h>
12 
13 #include "xe_gt.h"
14 
xe_gt_sysfs_kobj_release(struct kobject * kobj)15 static void xe_gt_sysfs_kobj_release(struct kobject *kobj)
16 {
17 	kfree(kobj);
18 }
19 
20 static const struct kobj_type xe_gt_sysfs_kobj_type = {
21 	.release = xe_gt_sysfs_kobj_release,
22 	.sysfs_ops = &kobj_sysfs_ops,
23 };
24 
gt_sysfs_fini(void * arg)25 static void gt_sysfs_fini(void *arg)
26 {
27 	struct xe_gt *gt = arg;
28 
29 	kobject_put(gt->sysfs);
30 }
31 
xe_gt_sysfs_init(struct xe_gt * gt)32 int xe_gt_sysfs_init(struct xe_gt *gt)
33 {
34 	struct xe_tile *tile = gt_to_tile(gt);
35 	struct xe_device *xe = gt_to_xe(gt);
36 	struct kobj_gt *kg;
37 	int err;
38 
39 	kg = kzalloc(sizeof(*kg), GFP_KERNEL);
40 	if (!kg)
41 		return -ENOMEM;
42 
43 	kobject_init(&kg->base, &xe_gt_sysfs_kobj_type);
44 	kg->gt = gt;
45 
46 	err = kobject_add(&kg->base, tile->sysfs, "gt%d", gt->info.id);
47 	if (err) {
48 		kobject_put(&kg->base);
49 		return err;
50 	}
51 
52 	gt->sysfs = &kg->base;
53 
54 	return devm_add_action_or_reset(xe->drm.dev, gt_sysfs_fini, gt);
55 }
56