ivpu_sysfs.c 2.3 KB

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