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_hw.h"
10 #include "ivpu_sysfs.h"
11
12 /*
13 * npu_busy_time_us is the time that the device spent executing jobs.
14 * The time is counted when and only when there are jobs submitted to firmware.
15 *
16 * This time can be used to measure the utilization of NPU, either by calculating
17 * npu_busy_time_us difference between two timepoints (i.e. measuring the time
18 * that the NPU was active during some workload) or monitoring utilization percentage
19 * by reading npu_busy_time_us periodically.
20 *
21 * When reading the value periodically, it shouldn't be read too often as it may have
22 * an impact on job submission performance. Recommended period is 1 second.
23 */
24 static ssize_t
npu_busy_time_us_show(struct device * dev,struct device_attribute * attr,char * buf)25 npu_busy_time_us_show(struct device *dev, struct device_attribute *attr, char *buf)
26 {
27 struct drm_device *drm = dev_get_drvdata(dev);
28 struct ivpu_device *vdev = to_ivpu_device(drm);
29 ktime_t total, now = 0;
30
31 xa_lock(&vdev->submitted_jobs_xa);
32 total = vdev->busy_time;
33 if (!xa_empty(&vdev->submitted_jobs_xa))
34 now = ktime_sub(ktime_get(), vdev->busy_start_ts);
35 xa_unlock(&vdev->submitted_jobs_xa);
36
37 return sysfs_emit(buf, "%lld\n", ktime_to_us(ktime_add(total, now)));
38 }
39
40 static DEVICE_ATTR_RO(npu_busy_time_us);
41
42 static struct attribute *ivpu_dev_attrs[] = {
43 &dev_attr_npu_busy_time_us.attr,
44 NULL,
45 };
46
47 static struct attribute_group ivpu_dev_attr_group = {
48 .attrs = ivpu_dev_attrs,
49 };
50
ivpu_sysfs_init(struct ivpu_device * vdev)51 void ivpu_sysfs_init(struct ivpu_device *vdev)
52 {
53 int ret;
54
55 ret = devm_device_add_group(vdev->drm.dev, &ivpu_dev_attr_group);
56 if (ret)
57 ivpu_warn(vdev, "Failed to add group to device, ret %d", ret);
58 }
59