ivpu_sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024-2025 Intel Corporation
  4. */
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/pm_runtime.h>
  8. #include <linux/units.h>
  9. #include "ivpu_drv.h"
  10. #include "ivpu_gem.h"
  11. #include "ivpu_fw.h"
  12. #include "ivpu_hw.h"
  13. #include "ivpu_sysfs.h"
  14. /**
  15. * DOC: npu_busy_time_us
  16. *
  17. * npu_busy_time_us is the time that the device spent executing jobs.
  18. * The time is counted when and only when there are jobs submitted to firmware.
  19. *
  20. * This time can be used to measure the utilization of NPU, either by calculating
  21. * npu_busy_time_us difference between two timepoints (i.e. measuring the time
  22. * that the NPU was active during some workload) or monitoring utilization percentage
  23. * by reading npu_busy_time_us periodically.
  24. *
  25. * When reading the value periodically, it shouldn't be read too often as it may have
  26. * an impact on job submission performance. Recommended period is 1 second.
  27. */
  28. static ssize_t
  29. npu_busy_time_us_show(struct device *dev, struct device_attribute *attr, char *buf)
  30. {
  31. struct drm_device *drm = dev_get_drvdata(dev);
  32. struct ivpu_device *vdev = to_ivpu_device(drm);
  33. ktime_t total, now = 0;
  34. mutex_lock(&vdev->submitted_jobs_lock);
  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. return sysfs_emit(buf, "%lld\n", ktime_to_us(ktime_add(total, now)));
  40. }
  41. static DEVICE_ATTR_RO(npu_busy_time_us);
  42. /**
  43. * DOC: npu_memory_utilization
  44. *
  45. * The npu_memory_utilization is used to report in bytes a current NPU memory utilization.
  46. *
  47. */
  48. static ssize_t
  49. npu_memory_utilization_show(struct device *dev, struct device_attribute *attr, char *buf)
  50. {
  51. struct drm_device *drm = dev_get_drvdata(dev);
  52. struct ivpu_device *vdev = to_ivpu_device(drm);
  53. struct ivpu_bo *bo;
  54. u64 total_npu_memory = 0;
  55. mutex_lock(&vdev->bo_list_lock);
  56. list_for_each_entry(bo, &vdev->bo_list, bo_list_node)
  57. if (ivpu_bo_is_resident(bo))
  58. total_npu_memory += ivpu_bo_size(bo);
  59. mutex_unlock(&vdev->bo_list_lock);
  60. return sysfs_emit(buf, "%lld\n", total_npu_memory);
  61. }
  62. static DEVICE_ATTR_RO(npu_memory_utilization);
  63. /**
  64. * DOC: sched_mode
  65. *
  66. * The sched_mode is used to report current NPU scheduling mode.
  67. *
  68. * It returns following strings:
  69. * - "HW" - Hardware Scheduler mode
  70. * - "OS" - Operating System Scheduler mode
  71. *
  72. */
  73. static ssize_t
  74. sched_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  75. {
  76. struct drm_device *drm = dev_get_drvdata(dev);
  77. struct ivpu_device *vdev = to_ivpu_device(drm);
  78. return sysfs_emit(buf, "%s\n", vdev->fw->sched_mode ? "HW" : "OS");
  79. }
  80. static DEVICE_ATTR_RO(sched_mode);
  81. /**
  82. * DOC: npu_max_frequency
  83. *
  84. * The npu_max_frequency shows maximum frequency in MHz of the NPU's data
  85. * processing unit
  86. */
  87. static ssize_t
  88. npu_max_frequency_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
  89. {
  90. struct drm_device *drm = dev_get_drvdata(dev);
  91. struct ivpu_device *vdev = to_ivpu_device(drm);
  92. u32 freq = ivpu_hw_dpu_max_freq_get(vdev);
  93. return sysfs_emit(buf, "%lu\n", freq / HZ_PER_MHZ);
  94. }
  95. static DEVICE_ATTR_RO(npu_max_frequency_mhz);
  96. /**
  97. * DOC: npu_current_frequency_mhz
  98. *
  99. * The npu_current_frequency_mhz shows current frequency in MHz of the NPU's
  100. * data processing unit
  101. */
  102. static ssize_t
  103. npu_current_frequency_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
  104. {
  105. struct drm_device *drm = dev_get_drvdata(dev);
  106. struct ivpu_device *vdev = to_ivpu_device(drm);
  107. u32 freq = 0;
  108. /* Read frequency only if device is active, otherwise frequency is 0 */
  109. if (pm_runtime_get_if_active(vdev->drm.dev) > 0) {
  110. freq = ivpu_hw_dpu_freq_get(vdev);
  111. pm_runtime_put_autosuspend(vdev->drm.dev);
  112. }
  113. return sysfs_emit(buf, "%lu\n", freq / HZ_PER_MHZ);
  114. }
  115. static DEVICE_ATTR_RO(npu_current_frequency_mhz);
  116. static struct attribute *ivpu_dev_attrs[] = {
  117. &dev_attr_npu_busy_time_us.attr,
  118. &dev_attr_npu_memory_utilization.attr,
  119. &dev_attr_sched_mode.attr,
  120. &dev_attr_npu_max_frequency_mhz.attr,
  121. &dev_attr_npu_current_frequency_mhz.attr,
  122. NULL,
  123. };
  124. static struct attribute_group ivpu_dev_attr_group = {
  125. .attrs = ivpu_dev_attrs,
  126. };
  127. void ivpu_sysfs_init(struct ivpu_device *vdev)
  128. {
  129. int ret;
  130. ret = devm_device_add_group(vdev->drm.dev, &ivpu_dev_attr_group);
  131. if (ret)
  132. ivpu_warn(vdev, "Failed to add group to device, ret %d", ret);
  133. }