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