Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / accel / ivpu / ivpu_sysfs.c
blob616477fc17fa071c5a3168de4e151d08c9bede8d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2024 Intel Corporation
4 */
6 #include <linux/device.h>
7 #include <linux/err.h>
9 #include "ivpu_drv.h"
10 #include "ivpu_fw.h"
11 #include "ivpu_hw.h"
12 #include "ivpu_sysfs.h"
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.
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.
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.
26 static ssize_t
27 npu_busy_time_us_show(struct device *dev, struct device_attribute *attr, char *buf)
29 struct drm_device *drm = dev_get_drvdata(dev);
30 struct ivpu_device *vdev = to_ivpu_device(drm);
31 ktime_t total, now = 0;
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);
39 return sysfs_emit(buf, "%lld\n", ktime_to_us(ktime_add(total, now)));
42 static DEVICE_ATTR_RO(npu_busy_time_us);
44 /**
45 * DOC: sched_mode
47 * The sched_mode is used to report current NPU scheduling mode.
49 * It returns following strings:
50 * - "HW" - Hardware Scheduler mode
51 * - "OS" - Operating System Scheduler mode
54 static ssize_t
55 sched_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
57 struct drm_device *drm = dev_get_drvdata(dev);
58 struct ivpu_device *vdev = to_ivpu_device(drm);
60 return sysfs_emit(buf, "%s\n", vdev->fw->sched_mode ? "HW" : "OS");
63 static DEVICE_ATTR_RO(sched_mode);
65 static struct attribute *ivpu_dev_attrs[] = {
66 &dev_attr_npu_busy_time_us.attr,
67 &dev_attr_sched_mode.attr,
68 NULL,
71 static struct attribute_group ivpu_dev_attr_group = {
72 .attrs = ivpu_dev_attrs,
75 void ivpu_sysfs_init(struct ivpu_device *vdev)
77 int ret;
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);