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
npu_busy_time_us_show(struct device * dev,struct device_attribute * attr,char * buf)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 mutex_lock(&vdev->submitted_jobs_lock);
34
35 total = vdev->busy_time;
36 if (!xa_empty(&vdev->submitted_jobs_xa))
37 now = ktime_sub(ktime_get(), vdev->busy_start_ts);
38 mutex_unlock(&vdev->submitted_jobs_lock);
39
40 return sysfs_emit(buf, "%lld\n", ktime_to_us(ktime_add(total, now)));
41 }
42
43 static DEVICE_ATTR_RO(npu_busy_time_us);
44
45 /**
46 * DOC: sched_mode
47 *
48 * The sched_mode is used to report current NPU scheduling mode.
49 *
50 * It returns following strings:
51 * - "HW" - Hardware Scheduler mode
52 * - "OS" - Operating System Scheduler mode
53 *
54 */
55 static ssize_t
sched_mode_show(struct device * dev,struct device_attribute * attr,char * buf)56 sched_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
57 {
58 struct drm_device *drm = dev_get_drvdata(dev);
59 struct ivpu_device *vdev = to_ivpu_device(drm);
60
61 return sysfs_emit(buf, "%s\n", vdev->fw->sched_mode ? "HW" : "OS");
62 }
63
64 static DEVICE_ATTR_RO(sched_mode);
65
66 static struct attribute *ivpu_dev_attrs[] = {
67 &dev_attr_npu_busy_time_us.attr,
68 &dev_attr_sched_mode.attr,
69 NULL,
70 };
71
72 static struct attribute_group ivpu_dev_attr_group = {
73 .attrs = ivpu_dev_attrs,
74 };
75
ivpu_sysfs_init(struct ivpu_device * vdev)76 void ivpu_sysfs_init(struct ivpu_device *vdev)
77 {
78 int ret;
79
80 ret = devm_device_add_group(vdev->drm.dev, &ivpu_dev_attr_group);
81 if (ret)
82 ivpu_warn(vdev, "Failed to add group to device, ret %d", ret);
83 }
84