xref: /linux/drivers/accel/ivpu/ivpu_sysfs.c (revision 793baff3f24f16dab9061045e23eea67724feae6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2024 Intel Corporation
4  */
5 
6 #include <linux/device.h>
7 #include <linux/err.h>
8 
9 #include "ivpu_drv.h"
10 #include "ivpu_fw.h"
11 #include "ivpu_hw.h"
12 #include "ivpu_sysfs.h"
13 
14 /*
15  * npu_busy_time_us is the time that the device spent executing jobs.
16  * The time is counted when and only when there are jobs submitted to firmware.
17  *
18  * This time can be used to measure the utilization of NPU, either by calculating
19  * npu_busy_time_us difference between two timepoints (i.e. measuring the time
20  * that the NPU was active during some workload) or monitoring utilization percentage
21  * by reading npu_busy_time_us periodically.
22  *
23  * When reading the value periodically, it shouldn't be read too often as it may have
24  * an impact on job submission performance. Recommended period is 1 second.
25  */
26 static ssize_t
27 npu_busy_time_us_show(struct device *dev, struct device_attribute *attr, char *buf)
28 {
29 	struct drm_device *drm = dev_get_drvdata(dev);
30 	struct ivpu_device *vdev = to_ivpu_device(drm);
31 	ktime_t total, now = 0;
32 
33 	xa_lock(&vdev->submitted_jobs_xa);
34 	total = vdev->busy_time;
35 	if (!xa_empty(&vdev->submitted_jobs_xa))
36 		now = ktime_sub(ktime_get(), vdev->busy_start_ts);
37 	xa_unlock(&vdev->submitted_jobs_xa);
38 
39 	return sysfs_emit(buf, "%lld\n", ktime_to_us(ktime_add(total, now)));
40 }
41 
42 static DEVICE_ATTR_RO(npu_busy_time_us);
43 
44 /**
45  * DOC: sched_mode
46  *
47  * The sched_mode is used to report current NPU scheduling mode.
48  *
49  * It returns following strings:
50  * - "HW"		- Hardware Scheduler mode
51  * - "OS"		- Operating System Scheduler mode
52  *
53  */
54 static ssize_t
55 sched_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
56 {
57 	struct drm_device *drm = dev_get_drvdata(dev);
58 	struct ivpu_device *vdev = to_ivpu_device(drm);
59 
60 	return sysfs_emit(buf, "%s\n", vdev->fw->sched_mode ? "HW" : "OS");
61 }
62 
63 static DEVICE_ATTR_RO(sched_mode);
64 
65 static struct attribute *ivpu_dev_attrs[] = {
66 	&dev_attr_npu_busy_time_us.attr,
67 	&dev_attr_sched_mode.attr,
68 	NULL,
69 };
70 
71 static struct attribute_group ivpu_dev_attr_group = {
72 	.attrs = ivpu_dev_attrs,
73 };
74 
75 void ivpu_sysfs_init(struct ivpu_device *vdev)
76 {
77 	int ret;
78 
79 	ret = devm_device_add_group(vdev->drm.dev, &ivpu_dev_attr_group);
80 	if (ret)
81 		ivpu_warn(vdev, "Failed to add group to device, ret %d", ret);
82 }
83