1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2024-2026 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 if (ivpu_bo_is_resident(bo)) 67 total_npu_memory += ivpu_bo_size(bo); 68 mutex_unlock(&vdev->bo_list_lock); 69 70 return sysfs_emit(buf, "%lld\n", total_npu_memory); 71 } 72 73 static DEVICE_ATTR_RO(npu_memory_utilization); 74 75 /** 76 * DOC: sched_mode 77 * 78 * The sched_mode is used to report current NPU scheduling mode. 79 * 80 * It returns following strings: 81 * - "HW" - Hardware Scheduler mode 82 * - "OS" - Operating System Scheduler mode 83 * 84 */ 85 static ssize_t 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 frequency control and information 97 * 98 * Hardware frequency capabilities: 99 * freq/hw_min_freq - Minimum frequency supported by the NPU hardware. 100 * freq/hw_max_freq - Maximum frequency supported by the NPU hardware. 101 * freq/hw_efficient_freq - Most efficient operating frequency for the NPU. 102 * 103 * Configurable frequency limits (50XX devices and newer): 104 * freq/set_min_freq - Configured minimum operating frequency. 105 * freq/set_max_freq - Configured maximum operating frequency. 106 * 107 * Clamping behavior: Values written to set_min_freq and set_max_freq are 108 * clamped to hardware limits. If set_min_freq exceeds set_max_freq, the driver 109 * clamps set_min_freq to set_max_freq when selecting the operating frequency. 110 * 111 * Current operating frequency: 112 * freq/current_freq - Current frequency in MHz. Valid only when the device is 113 * active; returns 0 when idle. May be lower than the requested range due to 114 * power or thermal constraints. 115 * 116 * Legacy attributes (backward compatibility): 117 * npu_max_frequency_mhz - Alias for freq/hw_max_freq. 118 * npu_current_frequency_mhz - Alias for freq/current_freq. 119 */ 120 121 static ssize_t hw_min_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 122 { 123 struct drm_device *drm = dev_get_drvdata(dev); 124 struct ivpu_device *vdev = to_ivpu_device(drm); 125 u32 freq_mhz = ivpu_hw_btrs_pll_ratio_to_mhz(vdev, vdev->hw->pll.min_ratio); 126 127 return sysfs_emit(buf, "%u\n", freq_mhz); 128 } 129 130 static DEVICE_ATTR_RO(hw_min_freq); 131 132 static ssize_t hw_efficient_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 133 { 134 struct drm_device *drm = dev_get_drvdata(dev); 135 struct ivpu_device *vdev = to_ivpu_device(drm); 136 u32 freq_mhz = ivpu_hw_btrs_pll_ratio_to_mhz(vdev, vdev->hw->pll.pn_ratio); 137 138 return sysfs_emit(buf, "%u\n", freq_mhz); 139 } 140 141 static DEVICE_ATTR_RO(hw_efficient_freq); 142 143 static ssize_t hw_max_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 144 { 145 struct drm_device *drm = dev_get_drvdata(dev); 146 struct ivpu_device *vdev = to_ivpu_device(drm); 147 u32 freq_mhz = ivpu_hw_btrs_pll_ratio_to_mhz(vdev, vdev->hw->pll.max_ratio); 148 149 return sysfs_emit(buf, "%u\n", freq_mhz); 150 } 151 152 static DEVICE_ATTR_RO(hw_max_freq); 153 154 static ssize_t set_min_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 155 { 156 struct drm_device *drm = dev_get_drvdata(dev); 157 struct ivpu_device *vdev = to_ivpu_device(drm); 158 u32 freq_mhz = ivpu_hw_btrs_pll_ratio_to_mhz(vdev, vdev->hw->pll.cfg_min_ratio); 159 160 return sysfs_emit(buf, "%u\n", freq_mhz); 161 } 162 163 static ssize_t 164 set_min_freq_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 165 { 166 struct drm_device *drm = dev_get_drvdata(dev); 167 struct ivpu_device *vdev = to_ivpu_device(drm); 168 u32 freq_mhz; 169 int ret; 170 171 ret = kstrtou32(buf, 10, &freq_mhz); 172 if (ret) 173 return ret; 174 175 ret = ivpu_hw_btrs_cfg_min_freq_set(vdev, freq_mhz); 176 if (ret) 177 return ret; 178 179 return count; 180 } 181 182 static DEVICE_ATTR_RW(set_min_freq); 183 184 static ssize_t set_max_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 185 { 186 struct drm_device *drm = dev_get_drvdata(dev); 187 struct ivpu_device *vdev = to_ivpu_device(drm); 188 u32 freq_mhz = ivpu_hw_btrs_pll_ratio_to_mhz(vdev, vdev->hw->pll.cfg_max_ratio); 189 190 return sysfs_emit(buf, "%u\n", freq_mhz); 191 } 192 193 static ssize_t 194 set_max_freq_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 195 { 196 struct drm_device *drm = dev_get_drvdata(dev); 197 struct ivpu_device *vdev = to_ivpu_device(drm); 198 u32 freq_mhz; 199 int ret; 200 201 ret = kstrtou32(buf, 10, &freq_mhz); 202 if (ret) 203 return ret; 204 205 /* Convert MHz to Hz and set max frequency */ 206 ret = ivpu_hw_btrs_cfg_max_freq_set(vdev, freq_mhz); 207 if (ret) 208 return ret; 209 210 return count; 211 } 212 213 static DEVICE_ATTR_RW(set_max_freq); 214 215 static ssize_t current_freq_show(struct device *dev, struct device_attribute *attr, char *buf) 216 { 217 struct drm_device *drm = dev_get_drvdata(dev); 218 struct ivpu_device *vdev = to_ivpu_device(drm); 219 u32 freq_mhz = 0; 220 221 /* Read frequency only if device is active, otherwise frequency is 0 */ 222 if (pm_runtime_get_if_active(vdev->drm.dev) > 0) { 223 freq_mhz = ivpu_hw_btrs_current_freq_get(vdev); 224 225 pm_runtime_put_autosuspend(vdev->drm.dev); 226 } 227 228 return sysfs_emit(buf, "%u\n", freq_mhz); 229 } 230 231 static DEVICE_ATTR_RO(current_freq); 232 233 /* Alias to current_freq for legacy compat */ 234 static struct device_attribute dev_attr_npu_max_frequency_mhz = 235 __ATTR(npu_max_frequency_mhz, 0444, hw_max_freq_show, NULL); 236 237 static struct device_attribute dev_attr_npu_current_frequency_mhz = 238 __ATTR(npu_current_frequency_mhz, 0444, current_freq_show, NULL); 239 240 static struct attribute *ivpu_freq_attrs[] = { 241 &dev_attr_hw_min_freq.attr, 242 &dev_attr_hw_efficient_freq.attr, 243 &dev_attr_hw_max_freq.attr, 244 &dev_attr_current_freq.attr, 245 NULL, 246 }; 247 248 static struct attribute_group ivpu_freq_attr_group = { 249 .name = "freq", 250 .attrs = ivpu_freq_attrs, 251 }; 252 253 static struct attribute *ivpu_dev_attrs[] = { 254 &dev_attr_npu_busy_time_us.attr, 255 &dev_attr_npu_memory_utilization.attr, 256 &dev_attr_sched_mode.attr, 257 &dev_attr_npu_max_frequency_mhz.attr, 258 &dev_attr_npu_current_frequency_mhz.attr, 259 NULL, 260 }; 261 262 static struct attribute_group ivpu_dev_attr_group = { 263 .attrs = ivpu_dev_attrs, 264 }; 265 266 void ivpu_sysfs_init(struct ivpu_device *vdev) 267 { 268 int ret; 269 270 ret = devm_device_add_group(vdev->drm.dev, &ivpu_dev_attr_group); 271 if (ret) { 272 ivpu_warn(vdev, "Failed to add sysfs group to device, ret %d", ret); 273 return; 274 } 275 276 ret = devm_device_add_group(vdev->drm.dev, &ivpu_freq_attr_group); 277 if (ret) { 278 ivpu_warn(vdev, "Failed to add sysfs freq group, ret %d", ret); 279 return; 280 } 281 282 if (ivpu_hw_ip_gen(vdev) >= IVPU_HW_IP_50XX) { 283 ret = sysfs_add_file_to_group(&vdev->drm.dev->kobj, 284 &dev_attr_set_min_freq.attr, 285 "freq"); 286 if (ret) { 287 ivpu_warn(vdev, "Failed to add sysfs set_min_freq to device, ret %d", ret); 288 return; 289 } 290 291 ret = sysfs_add_file_to_group(&vdev->drm.dev->kobj, 292 &dev_attr_set_max_freq.attr, 293 "freq"); 294 if (ret) { 295 ivpu_warn(vdev, "Failed to add sysfs set_max_freq to device, ret %d", ret); 296 return; 297 } 298 } 299 } 300