1 1.5 riastrad /* $NetBSD: amdgpu_pm.c,v 1.5 2021/12/19 12:21:29 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.4 riastrad * Copyright 2017 Advanced Micro Devices, Inc. 5 1.4 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Rafa Miecki <zajec5 (at) gmail.com> 25 1.1 riastrad * Alex Deucher <alexdeucher (at) gmail.com> 26 1.1 riastrad */ 27 1.4 riastrad 28 1.1 riastrad #include <sys/cdefs.h> 29 1.5 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_pm.c,v 1.5 2021/12/19 12:21:29 riastradh Exp $"); 30 1.1 riastrad 31 1.4 riastrad #include <drm/drm_debugfs.h> 32 1.4 riastrad 33 1.1 riastrad #include "amdgpu.h" 34 1.1 riastrad #include "amdgpu_drv.h" 35 1.1 riastrad #include "amdgpu_pm.h" 36 1.1 riastrad #include "amdgpu_dpm.h" 37 1.4 riastrad #include "amdgpu_display.h" 38 1.4 riastrad #include "amdgpu_smu.h" 39 1.1 riastrad #include "atom.h" 40 1.1 riastrad #include <linux/power_supply.h> 41 1.4 riastrad #include <linux/pci.h> 42 1.1 riastrad #include <linux/hwmon.h> 43 1.1 riastrad #include <linux/hwmon-sysfs.h> 44 1.4 riastrad #include <linux/nospec.h> 45 1.4 riastrad #include <linux/pm_runtime.h> 46 1.4 riastrad #include "hwmgr.h" 47 1.4 riastrad #define WIDTH_4K 3840 48 1.1 riastrad 49 1.3 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 50 1.1 riastrad static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev); 51 1.1 riastrad 52 1.4 riastrad static const struct cg_flag_name clocks[] = { 53 1.4 riastrad {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"}, 54 1.4 riastrad {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"}, 55 1.4 riastrad {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"}, 56 1.4 riastrad {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"}, 57 1.4 riastrad {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"}, 58 1.4 riastrad {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"}, 59 1.4 riastrad {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"}, 60 1.4 riastrad {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"}, 61 1.4 riastrad {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"}, 62 1.4 riastrad {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"}, 63 1.4 riastrad {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"}, 64 1.4 riastrad {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"}, 65 1.4 riastrad {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"}, 66 1.4 riastrad {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"}, 67 1.4 riastrad {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"}, 68 1.4 riastrad {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"}, 69 1.4 riastrad {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"}, 70 1.4 riastrad {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"}, 71 1.4 riastrad {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"}, 72 1.4 riastrad {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"}, 73 1.4 riastrad {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"}, 74 1.4 riastrad {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"}, 75 1.4 riastrad {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"}, 76 1.4 riastrad {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"}, 77 1.4 riastrad 78 1.4 riastrad {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"}, 79 1.4 riastrad {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"}, 80 1.4 riastrad {0, NULL}, 81 1.4 riastrad }; 82 1.4 riastrad 83 1.4 riastrad static const struct hwmon_temp_label { 84 1.4 riastrad enum PP_HWMON_TEMP channel; 85 1.4 riastrad const char *label; 86 1.4 riastrad } temp_label[] = { 87 1.4 riastrad {PP_TEMP_EDGE, "edge"}, 88 1.4 riastrad {PP_TEMP_JUNCTION, "junction"}, 89 1.4 riastrad {PP_TEMP_MEM, "mem"}, 90 1.4 riastrad }; 91 1.5 riastrad #endif /* __NetBSD__ */ 92 1.4 riastrad 93 1.1 riastrad void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev) 94 1.1 riastrad { 95 1.1 riastrad if (adev->pm.dpm_enabled) { 96 1.1 riastrad mutex_lock(&adev->pm.mutex); 97 1.1 riastrad if (power_supply_is_system_supplied() > 0) 98 1.4 riastrad adev->pm.ac_power = true; 99 1.1 riastrad else 100 1.4 riastrad adev->pm.ac_power = false; 101 1.4 riastrad if (adev->powerplay.pp_funcs->enable_bapm) 102 1.4 riastrad amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power); 103 1.1 riastrad mutex_unlock(&adev->pm.mutex); 104 1.1 riastrad } 105 1.1 riastrad } 106 1.1 riastrad 107 1.4 riastrad int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor, 108 1.4 riastrad void *data, uint32_t *size) 109 1.4 riastrad { 110 1.4 riastrad int ret = 0; 111 1.4 riastrad 112 1.4 riastrad if (!data || !size) 113 1.4 riastrad return -EINVAL; 114 1.4 riastrad 115 1.4 riastrad if (is_support_sw_smu(adev)) 116 1.4 riastrad ret = smu_read_sensor(&adev->smu, sensor, data, size); 117 1.4 riastrad else { 118 1.4 riastrad if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor) 119 1.4 riastrad ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle, 120 1.4 riastrad sensor, data, size); 121 1.4 riastrad else 122 1.4 riastrad ret = -EINVAL; 123 1.4 riastrad } 124 1.4 riastrad 125 1.4 riastrad return ret; 126 1.4 riastrad } 127 1.4 riastrad 128 1.3 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 129 1.4 riastrad /** 130 1.4 riastrad * DOC: power_dpm_state 131 1.4 riastrad * 132 1.4 riastrad * The power_dpm_state file is a legacy interface and is only provided for 133 1.4 riastrad * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting 134 1.4 riastrad * certain power related parameters. The file power_dpm_state is used for this. 135 1.4 riastrad * It accepts the following arguments: 136 1.4 riastrad * 137 1.4 riastrad * - battery 138 1.4 riastrad * 139 1.4 riastrad * - balanced 140 1.4 riastrad * 141 1.4 riastrad * - performance 142 1.4 riastrad * 143 1.4 riastrad * battery 144 1.4 riastrad * 145 1.4 riastrad * On older GPUs, the vbios provided a special power state for battery 146 1.4 riastrad * operation. Selecting battery switched to this state. This is no 147 1.4 riastrad * longer provided on newer GPUs so the option does nothing in that case. 148 1.4 riastrad * 149 1.4 riastrad * balanced 150 1.4 riastrad * 151 1.4 riastrad * On older GPUs, the vbios provided a special power state for balanced 152 1.4 riastrad * operation. Selecting balanced switched to this state. This is no 153 1.4 riastrad * longer provided on newer GPUs so the option does nothing in that case. 154 1.4 riastrad * 155 1.4 riastrad * performance 156 1.4 riastrad * 157 1.4 riastrad * On older GPUs, the vbios provided a special power state for performance 158 1.4 riastrad * operation. Selecting performance switched to this state. This is no 159 1.4 riastrad * longer provided on newer GPUs so the option does nothing in that case. 160 1.4 riastrad * 161 1.4 riastrad */ 162 1.1 riastrad static ssize_t amdgpu_get_dpm_state(struct device *dev, 163 1.1 riastrad struct device_attribute *attr, 164 1.1 riastrad char *buf) 165 1.1 riastrad { 166 1.1 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 167 1.1 riastrad struct amdgpu_device *adev = ddev->dev_private; 168 1.4 riastrad enum amd_pm_state_type pm; 169 1.4 riastrad int ret; 170 1.4 riastrad 171 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 172 1.4 riastrad return 0; 173 1.4 riastrad 174 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 175 1.4 riastrad if (ret < 0) 176 1.4 riastrad return ret; 177 1.4 riastrad 178 1.4 riastrad if (is_support_sw_smu(adev)) { 179 1.4 riastrad if (adev->smu.ppt_funcs->get_current_power_state) 180 1.4 riastrad pm = smu_get_current_power_state(&adev->smu); 181 1.4 riastrad else 182 1.4 riastrad pm = adev->pm.dpm.user_state; 183 1.4 riastrad } else if (adev->powerplay.pp_funcs->get_current_power_state) { 184 1.4 riastrad pm = amdgpu_dpm_get_current_power_state(adev); 185 1.4 riastrad } else { 186 1.4 riastrad pm = adev->pm.dpm.user_state; 187 1.4 riastrad } 188 1.4 riastrad 189 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 190 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 191 1.1 riastrad 192 1.1 riastrad return snprintf(buf, PAGE_SIZE, "%s\n", 193 1.1 riastrad (pm == POWER_STATE_TYPE_BATTERY) ? "battery" : 194 1.1 riastrad (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance"); 195 1.1 riastrad } 196 1.1 riastrad 197 1.1 riastrad static ssize_t amdgpu_set_dpm_state(struct device *dev, 198 1.1 riastrad struct device_attribute *attr, 199 1.1 riastrad const char *buf, 200 1.1 riastrad size_t count) 201 1.1 riastrad { 202 1.1 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 203 1.1 riastrad struct amdgpu_device *adev = ddev->dev_private; 204 1.4 riastrad enum amd_pm_state_type state; 205 1.4 riastrad int ret; 206 1.4 riastrad 207 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 208 1.4 riastrad return -EINVAL; 209 1.1 riastrad 210 1.1 riastrad if (strncmp("battery", buf, strlen("battery")) == 0) 211 1.4 riastrad state = POWER_STATE_TYPE_BATTERY; 212 1.1 riastrad else if (strncmp("balanced", buf, strlen("balanced")) == 0) 213 1.4 riastrad state = POWER_STATE_TYPE_BALANCED; 214 1.1 riastrad else if (strncmp("performance", buf, strlen("performance")) == 0) 215 1.4 riastrad state = POWER_STATE_TYPE_PERFORMANCE; 216 1.4 riastrad else 217 1.4 riastrad return -EINVAL; 218 1.4 riastrad 219 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 220 1.4 riastrad if (ret < 0) 221 1.4 riastrad return ret; 222 1.4 riastrad 223 1.4 riastrad if (is_support_sw_smu(adev)) { 224 1.4 riastrad mutex_lock(&adev->pm.mutex); 225 1.4 riastrad adev->pm.dpm.user_state = state; 226 1.4 riastrad mutex_unlock(&adev->pm.mutex); 227 1.4 riastrad } else if (adev->powerplay.pp_funcs->dispatch_tasks) { 228 1.4 riastrad amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state); 229 1.4 riastrad } else { 230 1.4 riastrad mutex_lock(&adev->pm.mutex); 231 1.4 riastrad adev->pm.dpm.user_state = state; 232 1.1 riastrad mutex_unlock(&adev->pm.mutex); 233 1.4 riastrad 234 1.4 riastrad amdgpu_pm_compute_clocks(adev); 235 1.1 riastrad } 236 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 237 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 238 1.1 riastrad 239 1.1 riastrad return count; 240 1.1 riastrad } 241 1.1 riastrad 242 1.4 riastrad 243 1.4 riastrad /** 244 1.4 riastrad * DOC: power_dpm_force_performance_level 245 1.4 riastrad * 246 1.4 riastrad * The amdgpu driver provides a sysfs API for adjusting certain power 247 1.4 riastrad * related parameters. The file power_dpm_force_performance_level is 248 1.4 riastrad * used for this. It accepts the following arguments: 249 1.4 riastrad * 250 1.4 riastrad * - auto 251 1.4 riastrad * 252 1.4 riastrad * - low 253 1.4 riastrad * 254 1.4 riastrad * - high 255 1.4 riastrad * 256 1.4 riastrad * - manual 257 1.4 riastrad * 258 1.4 riastrad * - profile_standard 259 1.4 riastrad * 260 1.4 riastrad * - profile_min_sclk 261 1.4 riastrad * 262 1.4 riastrad * - profile_min_mclk 263 1.4 riastrad * 264 1.4 riastrad * - profile_peak 265 1.4 riastrad * 266 1.4 riastrad * auto 267 1.4 riastrad * 268 1.4 riastrad * When auto is selected, the driver will attempt to dynamically select 269 1.4 riastrad * the optimal power profile for current conditions in the driver. 270 1.4 riastrad * 271 1.4 riastrad * low 272 1.4 riastrad * 273 1.4 riastrad * When low is selected, the clocks are forced to the lowest power state. 274 1.4 riastrad * 275 1.4 riastrad * high 276 1.4 riastrad * 277 1.4 riastrad * When high is selected, the clocks are forced to the highest power state. 278 1.4 riastrad * 279 1.4 riastrad * manual 280 1.4 riastrad * 281 1.4 riastrad * When manual is selected, the user can manually adjust which power states 282 1.4 riastrad * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk, 283 1.4 riastrad * and pp_dpm_pcie files and adjust the power state transition heuristics 284 1.4 riastrad * via the pp_power_profile_mode sysfs file. 285 1.4 riastrad * 286 1.4 riastrad * profile_standard 287 1.4 riastrad * profile_min_sclk 288 1.4 riastrad * profile_min_mclk 289 1.4 riastrad * profile_peak 290 1.4 riastrad * 291 1.4 riastrad * When the profiling modes are selected, clock and power gating are 292 1.4 riastrad * disabled and the clocks are set for different profiling cases. This 293 1.4 riastrad * mode is recommended for profiling specific work loads where you do 294 1.4 riastrad * not want clock or power gating for clock fluctuation to interfere 295 1.4 riastrad * with your results. profile_standard sets the clocks to a fixed clock 296 1.4 riastrad * level which varies from asic to asic. profile_min_sclk forces the sclk 297 1.4 riastrad * to the lowest level. profile_min_mclk forces the mclk to the lowest level. 298 1.4 riastrad * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels. 299 1.4 riastrad * 300 1.4 riastrad */ 301 1.4 riastrad 302 1.1 riastrad static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev, 303 1.4 riastrad struct device_attribute *attr, 304 1.4 riastrad char *buf) 305 1.1 riastrad { 306 1.1 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 307 1.1 riastrad struct amdgpu_device *adev = ddev->dev_private; 308 1.4 riastrad enum amd_dpm_forced_level level = 0xff; 309 1.4 riastrad int ret; 310 1.4 riastrad 311 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 312 1.4 riastrad return 0; 313 1.4 riastrad 314 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 315 1.4 riastrad if (ret < 0) 316 1.4 riastrad return ret; 317 1.4 riastrad 318 1.4 riastrad if (is_support_sw_smu(adev)) 319 1.4 riastrad level = smu_get_performance_level(&adev->smu); 320 1.4 riastrad else if (adev->powerplay.pp_funcs->get_performance_level) 321 1.4 riastrad level = amdgpu_dpm_get_performance_level(adev); 322 1.4 riastrad else 323 1.4 riastrad level = adev->pm.dpm.forced_level; 324 1.4 riastrad 325 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 326 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 327 1.1 riastrad 328 1.1 riastrad return snprintf(buf, PAGE_SIZE, "%s\n", 329 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" : 330 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" : 331 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" : 332 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" : 333 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" : 334 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" : 335 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" : 336 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" : 337 1.4 riastrad "unknown"); 338 1.1 riastrad } 339 1.1 riastrad 340 1.1 riastrad static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev, 341 1.1 riastrad struct device_attribute *attr, 342 1.1 riastrad const char *buf, 343 1.1 riastrad size_t count) 344 1.1 riastrad { 345 1.1 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 346 1.1 riastrad struct amdgpu_device *adev = ddev->dev_private; 347 1.4 riastrad enum amd_dpm_forced_level level; 348 1.4 riastrad enum amd_dpm_forced_level current_level = 0xff; 349 1.1 riastrad int ret = 0; 350 1.1 riastrad 351 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 352 1.4 riastrad return -EINVAL; 353 1.4 riastrad 354 1.1 riastrad if (strncmp("low", buf, strlen("low")) == 0) { 355 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_LOW; 356 1.1 riastrad } else if (strncmp("high", buf, strlen("high")) == 0) { 357 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_HIGH; 358 1.1 riastrad } else if (strncmp("auto", buf, strlen("auto")) == 0) { 359 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_AUTO; 360 1.4 riastrad } else if (strncmp("manual", buf, strlen("manual")) == 0) { 361 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_MANUAL; 362 1.4 riastrad } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) { 363 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT; 364 1.4 riastrad } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) { 365 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD; 366 1.4 riastrad } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) { 367 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK; 368 1.4 riastrad } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) { 369 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK; 370 1.4 riastrad } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) { 371 1.4 riastrad level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK; 372 1.4 riastrad } else { 373 1.4 riastrad return -EINVAL; 374 1.4 riastrad } 375 1.4 riastrad 376 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 377 1.4 riastrad if (ret < 0) 378 1.4 riastrad return ret; 379 1.4 riastrad 380 1.4 riastrad if (is_support_sw_smu(adev)) 381 1.4 riastrad current_level = smu_get_performance_level(&adev->smu); 382 1.4 riastrad else if (adev->powerplay.pp_funcs->get_performance_level) 383 1.4 riastrad current_level = amdgpu_dpm_get_performance_level(adev); 384 1.4 riastrad 385 1.4 riastrad if (current_level == level) { 386 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 387 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 388 1.4 riastrad return count; 389 1.4 riastrad } 390 1.4 riastrad 391 1.4 riastrad /* profile_exit setting is valid only when current mode is in profile mode */ 392 1.4 riastrad if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD | 393 1.4 riastrad AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK | 394 1.4 riastrad AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK | 395 1.4 riastrad AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) && 396 1.4 riastrad (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) { 397 1.4 riastrad pr_err("Currently not in any profile mode!\n"); 398 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 399 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 400 1.4 riastrad return -EINVAL; 401 1.1 riastrad } 402 1.4 riastrad 403 1.4 riastrad if (is_support_sw_smu(adev)) { 404 1.4 riastrad ret = smu_force_performance_level(&adev->smu, level); 405 1.4 riastrad if (ret) { 406 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 407 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 408 1.4 riastrad return -EINVAL; 409 1.4 riastrad } 410 1.4 riastrad } else if (adev->powerplay.pp_funcs->force_performance_level) { 411 1.4 riastrad mutex_lock(&adev->pm.mutex); 412 1.1 riastrad if (adev->pm.dpm.thermal_active) { 413 1.4 riastrad mutex_unlock(&adev->pm.mutex); 414 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 415 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 416 1.4 riastrad return -EINVAL; 417 1.1 riastrad } 418 1.1 riastrad ret = amdgpu_dpm_force_performance_level(adev, level); 419 1.4 riastrad if (ret) { 420 1.4 riastrad mutex_unlock(&adev->pm.mutex); 421 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 422 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 423 1.4 riastrad return -EINVAL; 424 1.4 riastrad } else { 425 1.4 riastrad adev->pm.dpm.forced_level = level; 426 1.4 riastrad } 427 1.4 riastrad mutex_unlock(&adev->pm.mutex); 428 1.1 riastrad } 429 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 430 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 431 1.1 riastrad 432 1.1 riastrad return count; 433 1.1 riastrad } 434 1.1 riastrad 435 1.4 riastrad static ssize_t amdgpu_get_pp_num_states(struct device *dev, 436 1.4 riastrad struct device_attribute *attr, 437 1.4 riastrad char *buf) 438 1.4 riastrad { 439 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 440 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 441 1.4 riastrad struct pp_states_info data; 442 1.4 riastrad int i, buf_len, ret; 443 1.4 riastrad 444 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 445 1.4 riastrad if (ret < 0) 446 1.4 riastrad return ret; 447 1.1 riastrad 448 1.4 riastrad if (is_support_sw_smu(adev)) { 449 1.4 riastrad ret = smu_get_power_num_states(&adev->smu, &data); 450 1.4 riastrad if (ret) 451 1.4 riastrad return ret; 452 1.4 riastrad } else if (adev->powerplay.pp_funcs->get_pp_num_states) 453 1.4 riastrad amdgpu_dpm_get_pp_num_states(adev, &data); 454 1.4 riastrad 455 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 456 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 457 1.4 riastrad 458 1.4 riastrad buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums); 459 1.4 riastrad for (i = 0; i < data.nums; i++) 460 1.4 riastrad buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i, 461 1.4 riastrad (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" : 462 1.4 riastrad (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" : 463 1.4 riastrad (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" : 464 1.4 riastrad (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default"); 465 1.4 riastrad 466 1.4 riastrad return buf_len; 467 1.4 riastrad } 468 1.4 riastrad 469 1.4 riastrad static ssize_t amdgpu_get_pp_cur_state(struct device *dev, 470 1.4 riastrad struct device_attribute *attr, 471 1.4 riastrad char *buf) 472 1.1 riastrad { 473 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 474 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 475 1.4 riastrad struct pp_states_info data; 476 1.4 riastrad struct smu_context *smu = &adev->smu; 477 1.4 riastrad enum amd_pm_state_type pm = 0; 478 1.4 riastrad int i = 0, ret = 0; 479 1.4 riastrad 480 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 481 1.4 riastrad return 0; 482 1.4 riastrad 483 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 484 1.4 riastrad if (ret < 0) 485 1.4 riastrad return ret; 486 1.4 riastrad 487 1.4 riastrad if (is_support_sw_smu(adev)) { 488 1.4 riastrad pm = smu_get_current_power_state(smu); 489 1.4 riastrad ret = smu_get_power_num_states(smu, &data); 490 1.4 riastrad if (ret) 491 1.4 riastrad return ret; 492 1.4 riastrad } else if (adev->powerplay.pp_funcs->get_current_power_state 493 1.4 riastrad && adev->powerplay.pp_funcs->get_pp_num_states) { 494 1.4 riastrad pm = amdgpu_dpm_get_current_power_state(adev); 495 1.4 riastrad amdgpu_dpm_get_pp_num_states(adev, &data); 496 1.4 riastrad } 497 1.4 riastrad 498 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 499 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 500 1.4 riastrad 501 1.4 riastrad for (i = 0; i < data.nums; i++) { 502 1.4 riastrad if (pm == data.states[i]) 503 1.4 riastrad break; 504 1.4 riastrad } 505 1.1 riastrad 506 1.4 riastrad if (i == data.nums) 507 1.4 riastrad i = -EINVAL; 508 1.1 riastrad 509 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", i); 510 1.1 riastrad } 511 1.1 riastrad 512 1.4 riastrad static ssize_t amdgpu_get_pp_force_state(struct device *dev, 513 1.4 riastrad struct device_attribute *attr, 514 1.4 riastrad char *buf) 515 1.1 riastrad { 516 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 517 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 518 1.4 riastrad 519 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 520 1.4 riastrad return 0; 521 1.1 riastrad 522 1.4 riastrad if (adev->pp_force_state_enabled) 523 1.4 riastrad return amdgpu_get_pp_cur_state(dev, attr, buf); 524 1.1 riastrad else 525 1.4 riastrad return snprintf(buf, PAGE_SIZE, "\n"); 526 1.4 riastrad } 527 1.4 riastrad 528 1.4 riastrad static ssize_t amdgpu_set_pp_force_state(struct device *dev, 529 1.4 riastrad struct device_attribute *attr, 530 1.4 riastrad const char *buf, 531 1.4 riastrad size_t count) 532 1.4 riastrad { 533 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 534 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 535 1.4 riastrad enum amd_pm_state_type state = 0; 536 1.4 riastrad unsigned long idx; 537 1.4 riastrad int ret; 538 1.4 riastrad 539 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 540 1.4 riastrad return -EINVAL; 541 1.4 riastrad 542 1.4 riastrad if (strlen(buf) == 1) 543 1.4 riastrad adev->pp_force_state_enabled = false; 544 1.4 riastrad else if (is_support_sw_smu(adev)) 545 1.4 riastrad adev->pp_force_state_enabled = false; 546 1.4 riastrad else if (adev->powerplay.pp_funcs->dispatch_tasks && 547 1.4 riastrad adev->powerplay.pp_funcs->get_pp_num_states) { 548 1.4 riastrad struct pp_states_info data; 549 1.4 riastrad 550 1.4 riastrad ret = kstrtoul(buf, 0, &idx); 551 1.4 riastrad if (ret || idx >= ARRAY_SIZE(data.states)) 552 1.4 riastrad return -EINVAL; 553 1.4 riastrad 554 1.4 riastrad idx = array_index_nospec(idx, ARRAY_SIZE(data.states)); 555 1.4 riastrad 556 1.4 riastrad amdgpu_dpm_get_pp_num_states(adev, &data); 557 1.4 riastrad state = data.states[idx]; 558 1.4 riastrad 559 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 560 1.4 riastrad if (ret < 0) 561 1.4 riastrad return ret; 562 1.4 riastrad 563 1.4 riastrad /* only set user selected power states */ 564 1.4 riastrad if (state != POWER_STATE_TYPE_INTERNAL_BOOT && 565 1.4 riastrad state != POWER_STATE_TYPE_DEFAULT) { 566 1.4 riastrad amdgpu_dpm_dispatch_task(adev, 567 1.4 riastrad AMD_PP_TASK_ENABLE_USER_STATE, &state); 568 1.4 riastrad adev->pp_force_state_enabled = true; 569 1.4 riastrad } 570 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 571 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 572 1.4 riastrad } 573 1.1 riastrad 574 1.4 riastrad return count; 575 1.1 riastrad } 576 1.1 riastrad 577 1.4 riastrad /** 578 1.4 riastrad * DOC: pp_table 579 1.4 riastrad * 580 1.4 riastrad * The amdgpu driver provides a sysfs API for uploading new powerplay 581 1.4 riastrad * tables. The file pp_table is used for this. Reading the file 582 1.4 riastrad * will dump the current power play table. Writing to the file 583 1.4 riastrad * will attempt to upload a new powerplay table and re-initialize 584 1.4 riastrad * powerplay using that new table. 585 1.4 riastrad * 586 1.4 riastrad */ 587 1.4 riastrad 588 1.4 riastrad static ssize_t amdgpu_get_pp_table(struct device *dev, 589 1.4 riastrad struct device_attribute *attr, 590 1.4 riastrad char *buf) 591 1.1 riastrad { 592 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 593 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 594 1.4 riastrad char *table = NULL; 595 1.4 riastrad int size, ret; 596 1.4 riastrad 597 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 598 1.4 riastrad return 0; 599 1.4 riastrad 600 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 601 1.4 riastrad if (ret < 0) 602 1.4 riastrad return ret; 603 1.4 riastrad 604 1.4 riastrad if (is_support_sw_smu(adev)) { 605 1.4 riastrad size = smu_sys_get_pp_table(&adev->smu, (void **)&table); 606 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 607 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 608 1.4 riastrad if (size < 0) 609 1.4 riastrad return size; 610 1.4 riastrad } else if (adev->powerplay.pp_funcs->get_pp_table) { 611 1.4 riastrad size = amdgpu_dpm_get_pp_table(adev, &table); 612 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 613 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 614 1.4 riastrad if (size < 0) 615 1.4 riastrad return size; 616 1.4 riastrad } else { 617 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 618 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 619 1.4 riastrad return 0; 620 1.4 riastrad } 621 1.4 riastrad 622 1.4 riastrad if (size >= PAGE_SIZE) 623 1.4 riastrad size = PAGE_SIZE - 1; 624 1.1 riastrad 625 1.4 riastrad memcpy(buf, table, size); 626 1.1 riastrad 627 1.4 riastrad return size; 628 1.1 riastrad } 629 1.1 riastrad 630 1.4 riastrad static ssize_t amdgpu_set_pp_table(struct device *dev, 631 1.4 riastrad struct device_attribute *attr, 632 1.4 riastrad const char *buf, 633 1.4 riastrad size_t count) 634 1.1 riastrad { 635 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 636 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 637 1.4 riastrad int ret = 0; 638 1.1 riastrad 639 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 640 1.1 riastrad return -EINVAL; 641 1.1 riastrad 642 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 643 1.4 riastrad if (ret < 0) 644 1.4 riastrad return ret; 645 1.4 riastrad 646 1.4 riastrad if (is_support_sw_smu(adev)) { 647 1.4 riastrad ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count); 648 1.4 riastrad if (ret) { 649 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 650 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 651 1.4 riastrad return ret; 652 1.4 riastrad } 653 1.4 riastrad } else if (adev->powerplay.pp_funcs->set_pp_table) 654 1.4 riastrad amdgpu_dpm_set_pp_table(adev, buf, count); 655 1.1 riastrad 656 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 657 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 658 1.1 riastrad 659 1.1 riastrad return count; 660 1.1 riastrad } 661 1.1 riastrad 662 1.4 riastrad /** 663 1.4 riastrad * DOC: pp_od_clk_voltage 664 1.4 riastrad * 665 1.4 riastrad * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages 666 1.4 riastrad * in each power level within a power state. The pp_od_clk_voltage is used for 667 1.4 riastrad * this. 668 1.4 riastrad * 669 1.4 riastrad * < For Vega10 and previous ASICs > 670 1.4 riastrad * 671 1.4 riastrad * Reading the file will display: 672 1.4 riastrad * 673 1.4 riastrad * - a list of engine clock levels and voltages labeled OD_SCLK 674 1.4 riastrad * 675 1.4 riastrad * - a list of memory clock levels and voltages labeled OD_MCLK 676 1.4 riastrad * 677 1.4 riastrad * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE 678 1.4 riastrad * 679 1.4 riastrad * To manually adjust these settings, first select manual using 680 1.4 riastrad * power_dpm_force_performance_level. Enter a new value for each 681 1.4 riastrad * level by writing a string that contains "s/m level clock voltage" to 682 1.4 riastrad * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz 683 1.4 riastrad * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at 684 1.4 riastrad * 810 mV. When you have edited all of the states as needed, write 685 1.4 riastrad * "c" (commit) to the file to commit your changes. If you want to reset to the 686 1.4 riastrad * default power levels, write "r" (reset) to the file to reset them. 687 1.4 riastrad * 688 1.4 riastrad * 689 1.4 riastrad * < For Vega20 > 690 1.4 riastrad * 691 1.4 riastrad * Reading the file will display: 692 1.4 riastrad * 693 1.4 riastrad * - minimum and maximum engine clock labeled OD_SCLK 694 1.4 riastrad * 695 1.4 riastrad * - maximum memory clock labeled OD_MCLK 696 1.4 riastrad * 697 1.4 riastrad * - three <frequency, voltage> points labeled OD_VDDC_CURVE. 698 1.4 riastrad * They can be used to calibrate the sclk voltage curve. 699 1.4 riastrad * 700 1.4 riastrad * - a list of valid ranges for sclk, mclk, and voltage curve points 701 1.4 riastrad * labeled OD_RANGE 702 1.4 riastrad * 703 1.4 riastrad * To manually adjust these settings: 704 1.4 riastrad * 705 1.4 riastrad * - First select manual using power_dpm_force_performance_level 706 1.4 riastrad * 707 1.4 riastrad * - For clock frequency setting, enter a new value by writing a 708 1.4 riastrad * string that contains "s/m index clock" to the file. The index 709 1.4 riastrad * should be 0 if to set minimum clock. And 1 if to set maximum 710 1.4 riastrad * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz. 711 1.4 riastrad * "m 1 800" will update maximum mclk to be 800Mhz. 712 1.4 riastrad * 713 1.4 riastrad * For sclk voltage curve, enter the new values by writing a 714 1.4 riastrad * string that contains "vc point clock voltage" to the file. The 715 1.4 riastrad * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will 716 1.4 riastrad * update point1 with clock set as 300Mhz and voltage as 717 1.4 riastrad * 600mV. "vc 2 1000 1000" will update point3 with clock set 718 1.4 riastrad * as 1000Mhz and voltage 1000mV. 719 1.4 riastrad * 720 1.4 riastrad * - When you have edited all of the states as needed, write "c" (commit) 721 1.4 riastrad * to the file to commit your changes 722 1.4 riastrad * 723 1.4 riastrad * - If you want to reset to the default power levels, write "r" (reset) 724 1.4 riastrad * to the file to reset them 725 1.4 riastrad * 726 1.4 riastrad */ 727 1.4 riastrad 728 1.4 riastrad static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev, 729 1.4 riastrad struct device_attribute *attr, 730 1.4 riastrad const char *buf, 731 1.4 riastrad size_t count) 732 1.1 riastrad { 733 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 734 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 735 1.4 riastrad int ret; 736 1.4 riastrad uint32_t parameter_size = 0; 737 1.4 riastrad long parameter[64]; 738 1.4 riastrad char buf_cpy[128]; 739 1.4 riastrad char *tmp_str; 740 1.4 riastrad char *sub_str; 741 1.4 riastrad const char delimiter[3] = {' ', '\n', '\0'}; 742 1.4 riastrad uint32_t type; 743 1.4 riastrad 744 1.4 riastrad if (amdgpu_sriov_vf(adev)) 745 1.4 riastrad return -EINVAL; 746 1.4 riastrad 747 1.4 riastrad if (count > 127) 748 1.4 riastrad return -EINVAL; 749 1.4 riastrad 750 1.4 riastrad if (*buf == 's') 751 1.4 riastrad type = PP_OD_EDIT_SCLK_VDDC_TABLE; 752 1.4 riastrad else if (*buf == 'm') 753 1.4 riastrad type = PP_OD_EDIT_MCLK_VDDC_TABLE; 754 1.4 riastrad else if(*buf == 'r') 755 1.4 riastrad type = PP_OD_RESTORE_DEFAULT_TABLE; 756 1.4 riastrad else if (*buf == 'c') 757 1.4 riastrad type = PP_OD_COMMIT_DPM_TABLE; 758 1.4 riastrad else if (!strncmp(buf, "vc", 2)) 759 1.4 riastrad type = PP_OD_EDIT_VDDC_CURVE; 760 1.4 riastrad else 761 1.4 riastrad return -EINVAL; 762 1.4 riastrad 763 1.4 riastrad memcpy(buf_cpy, buf, count+1); 764 1.4 riastrad 765 1.4 riastrad tmp_str = buf_cpy; 766 1.4 riastrad 767 1.4 riastrad if (type == PP_OD_EDIT_VDDC_CURVE) 768 1.4 riastrad tmp_str++; 769 1.4 riastrad while (isspace(*++tmp_str)); 770 1.1 riastrad 771 1.4 riastrad while (tmp_str[0]) { 772 1.4 riastrad sub_str = strsep(&tmp_str, delimiter); 773 1.4 riastrad ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 774 1.4 riastrad if (ret) 775 1.4 riastrad return -EINVAL; 776 1.4 riastrad parameter_size++; 777 1.1 riastrad 778 1.4 riastrad while (isspace(*tmp_str)) 779 1.4 riastrad tmp_str++; 780 1.4 riastrad } 781 1.4 riastrad 782 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 783 1.4 riastrad if (ret < 0) 784 1.4 riastrad return ret; 785 1.4 riastrad 786 1.4 riastrad if (is_support_sw_smu(adev)) { 787 1.4 riastrad ret = smu_od_edit_dpm_table(&adev->smu, type, 788 1.4 riastrad parameter, parameter_size); 789 1.4 riastrad 790 1.4 riastrad if (ret) { 791 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 792 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 793 1.4 riastrad return -EINVAL; 794 1.4 riastrad } 795 1.4 riastrad } else { 796 1.4 riastrad if (adev->powerplay.pp_funcs->odn_edit_dpm_table) { 797 1.4 riastrad ret = amdgpu_dpm_odn_edit_dpm_table(adev, type, 798 1.4 riastrad parameter, parameter_size); 799 1.4 riastrad if (ret) { 800 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 801 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 802 1.4 riastrad return -EINVAL; 803 1.4 riastrad } 804 1.4 riastrad } 805 1.4 riastrad 806 1.4 riastrad if (type == PP_OD_COMMIT_DPM_TABLE) { 807 1.4 riastrad if (adev->powerplay.pp_funcs->dispatch_tasks) { 808 1.4 riastrad amdgpu_dpm_dispatch_task(adev, 809 1.4 riastrad AMD_PP_TASK_READJUST_POWER_STATE, 810 1.4 riastrad NULL); 811 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 812 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 813 1.4 riastrad return count; 814 1.4 riastrad } else { 815 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 816 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 817 1.4 riastrad return -EINVAL; 818 1.4 riastrad } 819 1.4 riastrad } 820 1.4 riastrad } 821 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 822 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 823 1.4 riastrad 824 1.4 riastrad return count; 825 1.4 riastrad } 826 1.4 riastrad 827 1.4 riastrad static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev, 828 1.4 riastrad struct device_attribute *attr, 829 1.4 riastrad char *buf) 830 1.4 riastrad { 831 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 832 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 833 1.4 riastrad ssize_t size; 834 1.4 riastrad int ret; 835 1.4 riastrad 836 1.4 riastrad if (amdgpu_sriov_vf(adev)) 837 1.4 riastrad return 0; 838 1.4 riastrad 839 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 840 1.4 riastrad if (ret < 0) 841 1.4 riastrad return ret; 842 1.4 riastrad 843 1.4 riastrad if (is_support_sw_smu(adev)) { 844 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf); 845 1.4 riastrad size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size); 846 1.4 riastrad size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size); 847 1.4 riastrad size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size); 848 1.4 riastrad } else if (adev->powerplay.pp_funcs->print_clock_levels) { 849 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf); 850 1.4 riastrad size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size); 851 1.4 riastrad size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size); 852 1.4 riastrad size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size); 853 1.4 riastrad } else { 854 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 855 1.4 riastrad } 856 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 857 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 858 1.4 riastrad 859 1.4 riastrad return size; 860 1.4 riastrad } 861 1.4 riastrad 862 1.4 riastrad /** 863 1.4 riastrad * DOC: pp_features 864 1.4 riastrad * 865 1.4 riastrad * The amdgpu driver provides a sysfs API for adjusting what powerplay 866 1.4 riastrad * features to be enabled. The file pp_features is used for this. And 867 1.4 riastrad * this is only available for Vega10 and later dGPUs. 868 1.4 riastrad * 869 1.4 riastrad * Reading back the file will show you the followings: 870 1.4 riastrad * - Current ppfeature masks 871 1.4 riastrad * - List of the all supported powerplay features with their naming, 872 1.4 riastrad * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled"). 873 1.4 riastrad * 874 1.4 riastrad * To manually enable or disable a specific feature, just set or clear 875 1.4 riastrad * the corresponding bit from original ppfeature masks and input the 876 1.4 riastrad * new ppfeature masks. 877 1.4 riastrad */ 878 1.4 riastrad static ssize_t amdgpu_set_pp_feature_status(struct device *dev, 879 1.4 riastrad struct device_attribute *attr, 880 1.4 riastrad const char *buf, 881 1.4 riastrad size_t count) 882 1.4 riastrad { 883 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 884 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 885 1.4 riastrad uint64_t featuremask; 886 1.4 riastrad int ret; 887 1.4 riastrad 888 1.4 riastrad if (amdgpu_sriov_vf(adev)) 889 1.4 riastrad return -EINVAL; 890 1.4 riastrad 891 1.4 riastrad ret = kstrtou64(buf, 0, &featuremask); 892 1.4 riastrad if (ret) 893 1.4 riastrad return -EINVAL; 894 1.4 riastrad 895 1.4 riastrad pr_debug("featuremask = 0x%llx\n", featuremask); 896 1.4 riastrad 897 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 898 1.4 riastrad if (ret < 0) 899 1.4 riastrad return ret; 900 1.4 riastrad 901 1.4 riastrad if (is_support_sw_smu(adev)) { 902 1.4 riastrad ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask); 903 1.4 riastrad if (ret) { 904 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 905 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 906 1.4 riastrad return -EINVAL; 907 1.4 riastrad } 908 1.4 riastrad } else if (adev->powerplay.pp_funcs->set_ppfeature_status) { 909 1.4 riastrad ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask); 910 1.4 riastrad if (ret) { 911 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 912 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 913 1.4 riastrad return -EINVAL; 914 1.4 riastrad } 915 1.4 riastrad } 916 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 917 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 918 1.4 riastrad 919 1.4 riastrad return count; 920 1.4 riastrad } 921 1.4 riastrad 922 1.4 riastrad static ssize_t amdgpu_get_pp_feature_status(struct device *dev, 923 1.4 riastrad struct device_attribute *attr, 924 1.4 riastrad char *buf) 925 1.4 riastrad { 926 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 927 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 928 1.4 riastrad ssize_t size; 929 1.4 riastrad int ret; 930 1.4 riastrad 931 1.4 riastrad if (amdgpu_sriov_vf(adev)) 932 1.4 riastrad return 0; 933 1.4 riastrad 934 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 935 1.4 riastrad if (ret < 0) 936 1.4 riastrad return ret; 937 1.4 riastrad 938 1.4 riastrad if (is_support_sw_smu(adev)) 939 1.4 riastrad size = smu_sys_get_pp_feature_mask(&adev->smu, buf); 940 1.4 riastrad else if (adev->powerplay.pp_funcs->get_ppfeature_status) 941 1.4 riastrad size = amdgpu_dpm_get_ppfeature_status(adev, buf); 942 1.4 riastrad else 943 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 944 1.4 riastrad 945 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 946 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 947 1.4 riastrad 948 1.4 riastrad return size; 949 1.4 riastrad } 950 1.4 riastrad 951 1.4 riastrad /** 952 1.4 riastrad * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie 953 1.4 riastrad * 954 1.4 riastrad * The amdgpu driver provides a sysfs API for adjusting what power levels 955 1.4 riastrad * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk, 956 1.4 riastrad * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for 957 1.4 riastrad * this. 958 1.4 riastrad * 959 1.4 riastrad * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for 960 1.4 riastrad * Vega10 and later ASICs. 961 1.4 riastrad * pp_dpm_fclk interface is only available for Vega20 and later ASICs. 962 1.4 riastrad * 963 1.4 riastrad * Reading back the files will show you the available power levels within 964 1.4 riastrad * the power state and the clock information for those levels. 965 1.4 riastrad * 966 1.4 riastrad * To manually adjust these states, first select manual using 967 1.4 riastrad * power_dpm_force_performance_level. 968 1.4 riastrad * Secondly, enter a new value for each level by inputing a string that 969 1.4 riastrad * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie" 970 1.4 riastrad * E.g., 971 1.4 riastrad * 972 1.4 riastrad * .. code-block:: bash 973 1.4 riastrad * 974 1.4 riastrad * echo "4 5 6" > pp_dpm_sclk 975 1.4 riastrad * 976 1.4 riastrad * will enable sclk levels 4, 5, and 6. 977 1.4 riastrad * 978 1.4 riastrad * NOTE: change to the dcefclk max dpm level is not supported now 979 1.4 riastrad */ 980 1.4 riastrad 981 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev, 982 1.4 riastrad struct device_attribute *attr, 983 1.4 riastrad char *buf) 984 1.4 riastrad { 985 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 986 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 987 1.4 riastrad ssize_t size; 988 1.4 riastrad int ret; 989 1.4 riastrad 990 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 991 1.4 riastrad return 0; 992 1.4 riastrad 993 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 994 1.4 riastrad if (ret < 0) 995 1.4 riastrad return ret; 996 1.4 riastrad 997 1.4 riastrad if (is_support_sw_smu(adev)) 998 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf); 999 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1000 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf); 1001 1.4 riastrad else 1002 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1003 1.4 riastrad 1004 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1005 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1006 1.4 riastrad 1007 1.4 riastrad return size; 1008 1.4 riastrad } 1009 1.4 riastrad 1010 1.4 riastrad /* 1011 1.4 riastrad * Worst case: 32 bits individually specified, in octal at 12 characters 1012 1.4 riastrad * per line (+1 for \n). 1013 1.4 riastrad */ 1014 1.4 riastrad #define AMDGPU_MASK_BUF_MAX (32 * 13) 1015 1.4 riastrad 1016 1.4 riastrad static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask) 1017 1.4 riastrad { 1018 1.4 riastrad int ret; 1019 1.4 riastrad long level; 1020 1.4 riastrad char *sub_str = NULL; 1021 1.4 riastrad char *tmp; 1022 1.4 riastrad char buf_cpy[AMDGPU_MASK_BUF_MAX + 1]; 1023 1.4 riastrad const char delimiter[3] = {' ', '\n', '\0'}; 1024 1.4 riastrad size_t bytes; 1025 1.4 riastrad 1026 1.4 riastrad *mask = 0; 1027 1.4 riastrad 1028 1.4 riastrad bytes = min(count, sizeof(buf_cpy) - 1); 1029 1.4 riastrad memcpy(buf_cpy, buf, bytes); 1030 1.4 riastrad buf_cpy[bytes] = '\0'; 1031 1.4 riastrad tmp = buf_cpy; 1032 1.4 riastrad while (tmp[0]) { 1033 1.4 riastrad sub_str = strsep(&tmp, delimiter); 1034 1.4 riastrad if (strlen(sub_str)) { 1035 1.4 riastrad ret = kstrtol(sub_str, 0, &level); 1036 1.4 riastrad if (ret) 1037 1.4 riastrad return -EINVAL; 1038 1.4 riastrad *mask |= 1 << level; 1039 1.4 riastrad } else 1040 1.4 riastrad break; 1041 1.4 riastrad } 1042 1.4 riastrad 1043 1.4 riastrad return 0; 1044 1.4 riastrad } 1045 1.4 riastrad 1046 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev, 1047 1.4 riastrad struct device_attribute *attr, 1048 1.4 riastrad const char *buf, 1049 1.4 riastrad size_t count) 1050 1.4 riastrad { 1051 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1052 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1053 1.4 riastrad int ret; 1054 1.4 riastrad uint32_t mask = 0; 1055 1.4 riastrad 1056 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1057 1.4 riastrad return -EINVAL; 1058 1.4 riastrad 1059 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1060 1.4 riastrad if (ret) 1061 1.4 riastrad return ret; 1062 1.4 riastrad 1063 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1064 1.4 riastrad if (ret < 0) 1065 1.4 riastrad return ret; 1066 1.4 riastrad 1067 1.4 riastrad if (is_support_sw_smu(adev)) 1068 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask, true); 1069 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1070 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask); 1071 1.4 riastrad 1072 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1073 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1074 1.4 riastrad 1075 1.4 riastrad if (ret) 1076 1.4 riastrad return -EINVAL; 1077 1.4 riastrad 1078 1.4 riastrad return count; 1079 1.4 riastrad } 1080 1.4 riastrad 1081 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev, 1082 1.4 riastrad struct device_attribute *attr, 1083 1.4 riastrad char *buf) 1084 1.4 riastrad { 1085 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1086 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1087 1.4 riastrad ssize_t size; 1088 1.4 riastrad int ret; 1089 1.4 riastrad 1090 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1091 1.4 riastrad return 0; 1092 1.4 riastrad 1093 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1094 1.4 riastrad if (ret < 0) 1095 1.4 riastrad return ret; 1096 1.4 riastrad 1097 1.4 riastrad if (is_support_sw_smu(adev)) 1098 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf); 1099 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1100 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf); 1101 1.4 riastrad else 1102 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1103 1.4 riastrad 1104 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1105 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1106 1.4 riastrad 1107 1.4 riastrad return size; 1108 1.4 riastrad } 1109 1.4 riastrad 1110 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev, 1111 1.4 riastrad struct device_attribute *attr, 1112 1.4 riastrad const char *buf, 1113 1.4 riastrad size_t count) 1114 1.4 riastrad { 1115 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1116 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1117 1.4 riastrad uint32_t mask = 0; 1118 1.4 riastrad int ret; 1119 1.4 riastrad 1120 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1121 1.4 riastrad return -EINVAL; 1122 1.4 riastrad 1123 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1124 1.4 riastrad if (ret) 1125 1.4 riastrad return ret; 1126 1.4 riastrad 1127 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1128 1.4 riastrad if (ret < 0) 1129 1.4 riastrad return ret; 1130 1.4 riastrad 1131 1.4 riastrad if (is_support_sw_smu(adev)) 1132 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask, true); 1133 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1134 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask); 1135 1.4 riastrad 1136 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1137 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1138 1.4 riastrad 1139 1.4 riastrad if (ret) 1140 1.4 riastrad return -EINVAL; 1141 1.4 riastrad 1142 1.4 riastrad return count; 1143 1.4 riastrad } 1144 1.4 riastrad 1145 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev, 1146 1.4 riastrad struct device_attribute *attr, 1147 1.4 riastrad char *buf) 1148 1.4 riastrad { 1149 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1150 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1151 1.4 riastrad ssize_t size; 1152 1.4 riastrad int ret; 1153 1.4 riastrad 1154 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1155 1.4 riastrad return 0; 1156 1.4 riastrad 1157 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1158 1.4 riastrad if (ret < 0) 1159 1.4 riastrad return ret; 1160 1.4 riastrad 1161 1.4 riastrad if (is_support_sw_smu(adev)) 1162 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf); 1163 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1164 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf); 1165 1.4 riastrad else 1166 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1167 1.4 riastrad 1168 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1169 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1170 1.4 riastrad 1171 1.4 riastrad return size; 1172 1.4 riastrad } 1173 1.4 riastrad 1174 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev, 1175 1.4 riastrad struct device_attribute *attr, 1176 1.4 riastrad const char *buf, 1177 1.4 riastrad size_t count) 1178 1.4 riastrad { 1179 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1180 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1181 1.4 riastrad int ret; 1182 1.4 riastrad uint32_t mask = 0; 1183 1.4 riastrad 1184 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1185 1.4 riastrad return -EINVAL; 1186 1.4 riastrad 1187 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1188 1.4 riastrad if (ret) 1189 1.4 riastrad return ret; 1190 1.4 riastrad 1191 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1192 1.4 riastrad if (ret < 0) 1193 1.4 riastrad return ret; 1194 1.4 riastrad 1195 1.4 riastrad if (is_support_sw_smu(adev)) 1196 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask, true); 1197 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1198 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask); 1199 1.4 riastrad else 1200 1.4 riastrad ret = 0; 1201 1.4 riastrad 1202 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1203 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1204 1.4 riastrad 1205 1.4 riastrad if (ret) 1206 1.4 riastrad return -EINVAL; 1207 1.4 riastrad 1208 1.4 riastrad return count; 1209 1.4 riastrad } 1210 1.4 riastrad 1211 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev, 1212 1.4 riastrad struct device_attribute *attr, 1213 1.4 riastrad char *buf) 1214 1.4 riastrad { 1215 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1216 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1217 1.4 riastrad ssize_t size; 1218 1.4 riastrad int ret; 1219 1.4 riastrad 1220 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1221 1.4 riastrad return 0; 1222 1.4 riastrad 1223 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1224 1.4 riastrad if (ret < 0) 1225 1.4 riastrad return ret; 1226 1.4 riastrad 1227 1.4 riastrad if (is_support_sw_smu(adev)) 1228 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf); 1229 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1230 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf); 1231 1.4 riastrad else 1232 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1233 1.4 riastrad 1234 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1235 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1236 1.4 riastrad 1237 1.4 riastrad return size; 1238 1.4 riastrad } 1239 1.4 riastrad 1240 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev, 1241 1.4 riastrad struct device_attribute *attr, 1242 1.4 riastrad const char *buf, 1243 1.4 riastrad size_t count) 1244 1.4 riastrad { 1245 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1246 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1247 1.4 riastrad int ret; 1248 1.4 riastrad uint32_t mask = 0; 1249 1.4 riastrad 1250 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1251 1.4 riastrad return -EINVAL; 1252 1.4 riastrad 1253 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1254 1.4 riastrad if (ret) 1255 1.4 riastrad return ret; 1256 1.4 riastrad 1257 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1258 1.4 riastrad if (ret < 0) 1259 1.4 riastrad return ret; 1260 1.4 riastrad 1261 1.4 riastrad if (is_support_sw_smu(adev)) 1262 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask, true); 1263 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1264 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask); 1265 1.4 riastrad else 1266 1.4 riastrad ret = 0; 1267 1.4 riastrad 1268 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1269 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1270 1.4 riastrad 1271 1.4 riastrad if (ret) 1272 1.4 riastrad return -EINVAL; 1273 1.4 riastrad 1274 1.4 riastrad return count; 1275 1.4 riastrad } 1276 1.4 riastrad 1277 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev, 1278 1.4 riastrad struct device_attribute *attr, 1279 1.4 riastrad char *buf) 1280 1.4 riastrad { 1281 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1282 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1283 1.4 riastrad ssize_t size; 1284 1.4 riastrad int ret; 1285 1.4 riastrad 1286 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1287 1.4 riastrad return 0; 1288 1.4 riastrad 1289 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1290 1.4 riastrad if (ret < 0) 1291 1.4 riastrad return ret; 1292 1.4 riastrad 1293 1.4 riastrad if (is_support_sw_smu(adev)) 1294 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf); 1295 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1296 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf); 1297 1.4 riastrad else 1298 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1299 1.4 riastrad 1300 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1301 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1302 1.4 riastrad 1303 1.4 riastrad return size; 1304 1.4 riastrad } 1305 1.4 riastrad 1306 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev, 1307 1.4 riastrad struct device_attribute *attr, 1308 1.4 riastrad const char *buf, 1309 1.4 riastrad size_t count) 1310 1.4 riastrad { 1311 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1312 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1313 1.4 riastrad int ret; 1314 1.4 riastrad uint32_t mask = 0; 1315 1.4 riastrad 1316 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1317 1.4 riastrad return -EINVAL; 1318 1.4 riastrad 1319 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1320 1.4 riastrad if (ret) 1321 1.4 riastrad return ret; 1322 1.4 riastrad 1323 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1324 1.4 riastrad if (ret < 0) 1325 1.4 riastrad return ret; 1326 1.4 riastrad 1327 1.4 riastrad if (is_support_sw_smu(adev)) 1328 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask, true); 1329 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1330 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask); 1331 1.4 riastrad else 1332 1.4 riastrad ret = 0; 1333 1.4 riastrad 1334 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1335 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1336 1.4 riastrad 1337 1.4 riastrad if (ret) 1338 1.4 riastrad return -EINVAL; 1339 1.4 riastrad 1340 1.4 riastrad return count; 1341 1.4 riastrad } 1342 1.4 riastrad 1343 1.4 riastrad static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev, 1344 1.4 riastrad struct device_attribute *attr, 1345 1.4 riastrad char *buf) 1346 1.4 riastrad { 1347 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1348 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1349 1.4 riastrad ssize_t size; 1350 1.4 riastrad int ret; 1351 1.4 riastrad 1352 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1353 1.4 riastrad return 0; 1354 1.4 riastrad 1355 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1356 1.4 riastrad if (ret < 0) 1357 1.4 riastrad return ret; 1358 1.4 riastrad 1359 1.4 riastrad if (is_support_sw_smu(adev)) 1360 1.4 riastrad size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf); 1361 1.4 riastrad else if (adev->powerplay.pp_funcs->print_clock_levels) 1362 1.4 riastrad size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf); 1363 1.4 riastrad else 1364 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1365 1.4 riastrad 1366 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1367 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1368 1.4 riastrad 1369 1.4 riastrad return size; 1370 1.4 riastrad } 1371 1.4 riastrad 1372 1.4 riastrad static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev, 1373 1.4 riastrad struct device_attribute *attr, 1374 1.4 riastrad const char *buf, 1375 1.4 riastrad size_t count) 1376 1.4 riastrad { 1377 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1378 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1379 1.4 riastrad int ret; 1380 1.4 riastrad uint32_t mask = 0; 1381 1.4 riastrad 1382 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1383 1.4 riastrad return -EINVAL; 1384 1.4 riastrad 1385 1.4 riastrad ret = amdgpu_read_mask(buf, count, &mask); 1386 1.4 riastrad if (ret) 1387 1.4 riastrad return ret; 1388 1.4 riastrad 1389 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1390 1.4 riastrad if (ret < 0) 1391 1.4 riastrad return ret; 1392 1.4 riastrad 1393 1.4 riastrad if (is_support_sw_smu(adev)) 1394 1.4 riastrad ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask, true); 1395 1.4 riastrad else if (adev->powerplay.pp_funcs->force_clock_level) 1396 1.4 riastrad ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask); 1397 1.4 riastrad else 1398 1.4 riastrad ret = 0; 1399 1.4 riastrad 1400 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1401 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1402 1.4 riastrad 1403 1.4 riastrad if (ret) 1404 1.4 riastrad return -EINVAL; 1405 1.4 riastrad 1406 1.4 riastrad return count; 1407 1.4 riastrad } 1408 1.4 riastrad 1409 1.4 riastrad static ssize_t amdgpu_get_pp_sclk_od(struct device *dev, 1410 1.4 riastrad struct device_attribute *attr, 1411 1.4 riastrad char *buf) 1412 1.4 riastrad { 1413 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1414 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1415 1.4 riastrad uint32_t value = 0; 1416 1.4 riastrad int ret; 1417 1.4 riastrad 1418 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1419 1.4 riastrad return 0; 1420 1.4 riastrad 1421 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1422 1.4 riastrad if (ret < 0) 1423 1.4 riastrad return ret; 1424 1.4 riastrad 1425 1.4 riastrad if (is_support_sw_smu(adev)) 1426 1.4 riastrad value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK); 1427 1.4 riastrad else if (adev->powerplay.pp_funcs->get_sclk_od) 1428 1.4 riastrad value = amdgpu_dpm_get_sclk_od(adev); 1429 1.4 riastrad 1430 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1431 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1432 1.4 riastrad 1433 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", value); 1434 1.4 riastrad } 1435 1.4 riastrad 1436 1.4 riastrad static ssize_t amdgpu_set_pp_sclk_od(struct device *dev, 1437 1.4 riastrad struct device_attribute *attr, 1438 1.4 riastrad const char *buf, 1439 1.4 riastrad size_t count) 1440 1.4 riastrad { 1441 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1442 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1443 1.4 riastrad int ret; 1444 1.4 riastrad long int value; 1445 1.4 riastrad 1446 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1447 1.4 riastrad return -EINVAL; 1448 1.4 riastrad 1449 1.4 riastrad ret = kstrtol(buf, 0, &value); 1450 1.4 riastrad 1451 1.4 riastrad if (ret) 1452 1.4 riastrad return -EINVAL; 1453 1.4 riastrad 1454 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1455 1.4 riastrad if (ret < 0) 1456 1.4 riastrad return ret; 1457 1.4 riastrad 1458 1.4 riastrad if (is_support_sw_smu(adev)) { 1459 1.4 riastrad value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value); 1460 1.4 riastrad } else { 1461 1.4 riastrad if (adev->powerplay.pp_funcs->set_sclk_od) 1462 1.4 riastrad amdgpu_dpm_set_sclk_od(adev, (uint32_t)value); 1463 1.4 riastrad 1464 1.4 riastrad if (adev->powerplay.pp_funcs->dispatch_tasks) { 1465 1.4 riastrad amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL); 1466 1.4 riastrad } else { 1467 1.4 riastrad adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps; 1468 1.4 riastrad amdgpu_pm_compute_clocks(adev); 1469 1.4 riastrad } 1470 1.4 riastrad } 1471 1.4 riastrad 1472 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1473 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1474 1.4 riastrad 1475 1.4 riastrad return count; 1476 1.4 riastrad } 1477 1.4 riastrad 1478 1.4 riastrad static ssize_t amdgpu_get_pp_mclk_od(struct device *dev, 1479 1.4 riastrad struct device_attribute *attr, 1480 1.4 riastrad char *buf) 1481 1.4 riastrad { 1482 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1483 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1484 1.4 riastrad uint32_t value = 0; 1485 1.4 riastrad int ret; 1486 1.4 riastrad 1487 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1488 1.4 riastrad return 0; 1489 1.4 riastrad 1490 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1491 1.4 riastrad if (ret < 0) 1492 1.4 riastrad return ret; 1493 1.4 riastrad 1494 1.4 riastrad if (is_support_sw_smu(adev)) 1495 1.4 riastrad value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK); 1496 1.4 riastrad else if (adev->powerplay.pp_funcs->get_mclk_od) 1497 1.4 riastrad value = amdgpu_dpm_get_mclk_od(adev); 1498 1.4 riastrad 1499 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1500 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1501 1.4 riastrad 1502 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", value); 1503 1.4 riastrad } 1504 1.4 riastrad 1505 1.4 riastrad static ssize_t amdgpu_set_pp_mclk_od(struct device *dev, 1506 1.4 riastrad struct device_attribute *attr, 1507 1.4 riastrad const char *buf, 1508 1.4 riastrad size_t count) 1509 1.4 riastrad { 1510 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1511 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1512 1.4 riastrad int ret; 1513 1.4 riastrad long int value; 1514 1.4 riastrad 1515 1.4 riastrad if (amdgpu_sriov_vf(adev)) 1516 1.4 riastrad return 0; 1517 1.4 riastrad 1518 1.4 riastrad ret = kstrtol(buf, 0, &value); 1519 1.4 riastrad 1520 1.4 riastrad if (ret) 1521 1.4 riastrad return -EINVAL; 1522 1.4 riastrad 1523 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1524 1.4 riastrad if (ret < 0) 1525 1.4 riastrad return ret; 1526 1.4 riastrad 1527 1.4 riastrad if (is_support_sw_smu(adev)) { 1528 1.4 riastrad value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value); 1529 1.4 riastrad } else { 1530 1.4 riastrad if (adev->powerplay.pp_funcs->set_mclk_od) 1531 1.4 riastrad amdgpu_dpm_set_mclk_od(adev, (uint32_t)value); 1532 1.4 riastrad 1533 1.4 riastrad if (adev->powerplay.pp_funcs->dispatch_tasks) { 1534 1.4 riastrad amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL); 1535 1.4 riastrad } else { 1536 1.4 riastrad adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps; 1537 1.4 riastrad amdgpu_pm_compute_clocks(adev); 1538 1.4 riastrad } 1539 1.4 riastrad } 1540 1.4 riastrad 1541 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1542 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1543 1.4 riastrad 1544 1.4 riastrad return count; 1545 1.4 riastrad } 1546 1.4 riastrad 1547 1.4 riastrad /** 1548 1.4 riastrad * DOC: pp_power_profile_mode 1549 1.4 riastrad * 1550 1.4 riastrad * The amdgpu driver provides a sysfs API for adjusting the heuristics 1551 1.4 riastrad * related to switching between power levels in a power state. The file 1552 1.4 riastrad * pp_power_profile_mode is used for this. 1553 1.4 riastrad * 1554 1.4 riastrad * Reading this file outputs a list of all of the predefined power profiles 1555 1.4 riastrad * and the relevant heuristics settings for that profile. 1556 1.4 riastrad * 1557 1.4 riastrad * To select a profile or create a custom profile, first select manual using 1558 1.4 riastrad * power_dpm_force_performance_level. Writing the number of a predefined 1559 1.4 riastrad * profile to pp_power_profile_mode will enable those heuristics. To 1560 1.4 riastrad * create a custom set of heuristics, write a string of numbers to the file 1561 1.4 riastrad * starting with the number of the custom profile along with a setting 1562 1.4 riastrad * for each heuristic parameter. Due to differences across asic families 1563 1.4 riastrad * the heuristic parameters vary from family to family. 1564 1.4 riastrad * 1565 1.4 riastrad */ 1566 1.4 riastrad 1567 1.4 riastrad static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev, 1568 1.4 riastrad struct device_attribute *attr, 1569 1.4 riastrad char *buf) 1570 1.4 riastrad { 1571 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1572 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1573 1.4 riastrad ssize_t size; 1574 1.4 riastrad int ret; 1575 1.4 riastrad 1576 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1577 1.4 riastrad return 0; 1578 1.4 riastrad 1579 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1580 1.4 riastrad if (ret < 0) 1581 1.4 riastrad return ret; 1582 1.4 riastrad 1583 1.4 riastrad if (is_support_sw_smu(adev)) 1584 1.4 riastrad size = smu_get_power_profile_mode(&adev->smu, buf); 1585 1.4 riastrad else if (adev->powerplay.pp_funcs->get_power_profile_mode) 1586 1.4 riastrad size = amdgpu_dpm_get_power_profile_mode(adev, buf); 1587 1.4 riastrad else 1588 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 1589 1.4 riastrad 1590 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1591 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1592 1.4 riastrad 1593 1.4 riastrad return size; 1594 1.4 riastrad } 1595 1.4 riastrad 1596 1.4 riastrad 1597 1.4 riastrad static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev, 1598 1.4 riastrad struct device_attribute *attr, 1599 1.4 riastrad const char *buf, 1600 1.4 riastrad size_t count) 1601 1.4 riastrad { 1602 1.4 riastrad int ret = 0xff; 1603 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1604 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1605 1.4 riastrad uint32_t parameter_size = 0; 1606 1.4 riastrad long parameter[64]; 1607 1.4 riastrad char *sub_str, buf_cpy[128]; 1608 1.4 riastrad char *tmp_str; 1609 1.4 riastrad uint32_t i = 0; 1610 1.4 riastrad char tmp[2]; 1611 1.4 riastrad long int profile_mode = 0; 1612 1.4 riastrad const char delimiter[3] = {' ', '\n', '\0'}; 1613 1.4 riastrad 1614 1.4 riastrad tmp[0] = *(buf); 1615 1.4 riastrad tmp[1] = '\0'; 1616 1.4 riastrad ret = kstrtol(tmp, 0, &profile_mode); 1617 1.4 riastrad if (ret) 1618 1.4 riastrad return -EINVAL; 1619 1.4 riastrad 1620 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1621 1.4 riastrad return -EINVAL; 1622 1.4 riastrad 1623 1.4 riastrad if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) { 1624 1.4 riastrad if (count < 2 || count > 127) 1625 1.4 riastrad return -EINVAL; 1626 1.4 riastrad while (isspace(*++buf)) 1627 1.4 riastrad i++; 1628 1.4 riastrad memcpy(buf_cpy, buf, count-i); 1629 1.4 riastrad tmp_str = buf_cpy; 1630 1.4 riastrad while (tmp_str[0]) { 1631 1.4 riastrad sub_str = strsep(&tmp_str, delimiter); 1632 1.4 riastrad ret = kstrtol(sub_str, 0, ¶meter[parameter_size]); 1633 1.4 riastrad if (ret) 1634 1.4 riastrad return -EINVAL; 1635 1.4 riastrad parameter_size++; 1636 1.4 riastrad while (isspace(*tmp_str)) 1637 1.4 riastrad tmp_str++; 1638 1.4 riastrad } 1639 1.4 riastrad } 1640 1.4 riastrad parameter[parameter_size] = profile_mode; 1641 1.4 riastrad 1642 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1643 1.4 riastrad if (ret < 0) 1644 1.4 riastrad return ret; 1645 1.4 riastrad 1646 1.4 riastrad if (is_support_sw_smu(adev)) 1647 1.4 riastrad ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true); 1648 1.4 riastrad else if (adev->powerplay.pp_funcs->set_power_profile_mode) 1649 1.4 riastrad ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size); 1650 1.4 riastrad 1651 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1652 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1653 1.4 riastrad 1654 1.4 riastrad if (!ret) 1655 1.4 riastrad return count; 1656 1.4 riastrad 1657 1.4 riastrad return -EINVAL; 1658 1.4 riastrad } 1659 1.4 riastrad 1660 1.4 riastrad /** 1661 1.4 riastrad * DOC: busy_percent 1662 1.4 riastrad * 1663 1.4 riastrad * The amdgpu driver provides a sysfs API for reading how busy the GPU 1664 1.4 riastrad * is as a percentage. The file gpu_busy_percent is used for this. 1665 1.4 riastrad * The SMU firmware computes a percentage of load based on the 1666 1.4 riastrad * aggregate activity level in the IP cores. 1667 1.4 riastrad */ 1668 1.4 riastrad static ssize_t amdgpu_get_busy_percent(struct device *dev, 1669 1.4 riastrad struct device_attribute *attr, 1670 1.4 riastrad char *buf) 1671 1.4 riastrad { 1672 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1673 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1674 1.4 riastrad int r, value, size = sizeof(value); 1675 1.4 riastrad 1676 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1677 1.4 riastrad return 0; 1678 1.4 riastrad 1679 1.4 riastrad r = pm_runtime_get_sync(ddev->dev); 1680 1.4 riastrad if (r < 0) 1681 1.4 riastrad return r; 1682 1.4 riastrad 1683 1.4 riastrad /* read the IP busy sensor */ 1684 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, 1685 1.4 riastrad (void *)&value, &size); 1686 1.4 riastrad 1687 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1688 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1689 1.4 riastrad 1690 1.4 riastrad if (r) 1691 1.4 riastrad return r; 1692 1.4 riastrad 1693 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", value); 1694 1.4 riastrad } 1695 1.4 riastrad 1696 1.4 riastrad /** 1697 1.4 riastrad * DOC: mem_busy_percent 1698 1.4 riastrad * 1699 1.4 riastrad * The amdgpu driver provides a sysfs API for reading how busy the VRAM 1700 1.4 riastrad * is as a percentage. The file mem_busy_percent is used for this. 1701 1.4 riastrad * The SMU firmware computes a percentage of load based on the 1702 1.4 riastrad * aggregate activity level in the IP cores. 1703 1.4 riastrad */ 1704 1.4 riastrad static ssize_t amdgpu_get_memory_busy_percent(struct device *dev, 1705 1.4 riastrad struct device_attribute *attr, 1706 1.4 riastrad char *buf) 1707 1.4 riastrad { 1708 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1709 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1710 1.4 riastrad int r, value, size = sizeof(value); 1711 1.4 riastrad 1712 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1713 1.4 riastrad return 0; 1714 1.4 riastrad 1715 1.4 riastrad r = pm_runtime_get_sync(ddev->dev); 1716 1.4 riastrad if (r < 0) 1717 1.4 riastrad return r; 1718 1.4 riastrad 1719 1.4 riastrad /* read the IP busy sensor */ 1720 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, 1721 1.4 riastrad (void *)&value, &size); 1722 1.4 riastrad 1723 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1724 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1725 1.4 riastrad 1726 1.4 riastrad if (r) 1727 1.4 riastrad return r; 1728 1.4 riastrad 1729 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", value); 1730 1.4 riastrad } 1731 1.4 riastrad 1732 1.4 riastrad /** 1733 1.4 riastrad * DOC: pcie_bw 1734 1.4 riastrad * 1735 1.4 riastrad * The amdgpu driver provides a sysfs API for estimating how much data 1736 1.4 riastrad * has been received and sent by the GPU in the last second through PCIe. 1737 1.4 riastrad * The file pcie_bw is used for this. 1738 1.4 riastrad * The Perf counters count the number of received and sent messages and return 1739 1.4 riastrad * those values, as well as the maximum payload size of a PCIe packet (mps). 1740 1.4 riastrad * Note that it is not possible to easily and quickly obtain the size of each 1741 1.4 riastrad * packet transmitted, so we output the max payload size (mps) to allow for 1742 1.4 riastrad * quick estimation of the PCIe bandwidth usage 1743 1.4 riastrad */ 1744 1.4 riastrad static ssize_t amdgpu_get_pcie_bw(struct device *dev, 1745 1.4 riastrad struct device_attribute *attr, 1746 1.4 riastrad char *buf) 1747 1.4 riastrad { 1748 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1749 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1750 1.4 riastrad uint64_t count0, count1; 1751 1.4 riastrad int ret; 1752 1.4 riastrad 1753 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1754 1.4 riastrad return 0; 1755 1.4 riastrad 1756 1.4 riastrad ret = pm_runtime_get_sync(ddev->dev); 1757 1.4 riastrad if (ret < 0) 1758 1.4 riastrad return ret; 1759 1.4 riastrad 1760 1.4 riastrad amdgpu_asic_get_pcie_usage(adev, &count0, &count1); 1761 1.4 riastrad 1762 1.4 riastrad pm_runtime_mark_last_busy(ddev->dev); 1763 1.4 riastrad pm_runtime_put_autosuspend(ddev->dev); 1764 1.4 riastrad 1765 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n", 1766 1.4 riastrad count0, count1, pcie_get_mps(adev->pdev)); 1767 1.4 riastrad } 1768 1.4 riastrad 1769 1.4 riastrad /** 1770 1.4 riastrad * DOC: unique_id 1771 1.4 riastrad * 1772 1.4 riastrad * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU 1773 1.4 riastrad * The file unique_id is used for this. 1774 1.4 riastrad * This will provide a Unique ID that will persist from machine to machine 1775 1.4 riastrad * 1776 1.4 riastrad * NOTE: This will only work for GFX9 and newer. This file will be absent 1777 1.4 riastrad * on unsupported ASICs (GFX8 and older) 1778 1.4 riastrad */ 1779 1.4 riastrad static ssize_t amdgpu_get_unique_id(struct device *dev, 1780 1.4 riastrad struct device_attribute *attr, 1781 1.4 riastrad char *buf) 1782 1.4 riastrad { 1783 1.4 riastrad struct drm_device *ddev = dev_get_drvdata(dev); 1784 1.4 riastrad struct amdgpu_device *adev = ddev->dev_private; 1785 1.4 riastrad 1786 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 1787 1.4 riastrad return 0; 1788 1.4 riastrad 1789 1.4 riastrad if (adev->unique_id) 1790 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id); 1791 1.4 riastrad 1792 1.4 riastrad return 0; 1793 1.4 riastrad } 1794 1.4 riastrad 1795 1.4 riastrad static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state); 1796 1.4 riastrad static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR, 1797 1.4 riastrad amdgpu_get_dpm_forced_performance_level, 1798 1.4 riastrad amdgpu_set_dpm_forced_performance_level); 1799 1.4 riastrad static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL); 1800 1.4 riastrad static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL); 1801 1.4 riastrad static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR, 1802 1.4 riastrad amdgpu_get_pp_force_state, 1803 1.4 riastrad amdgpu_set_pp_force_state); 1804 1.4 riastrad static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR, 1805 1.4 riastrad amdgpu_get_pp_table, 1806 1.4 riastrad amdgpu_set_pp_table); 1807 1.4 riastrad static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR, 1808 1.4 riastrad amdgpu_get_pp_dpm_sclk, 1809 1.4 riastrad amdgpu_set_pp_dpm_sclk); 1810 1.4 riastrad static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR, 1811 1.4 riastrad amdgpu_get_pp_dpm_mclk, 1812 1.4 riastrad amdgpu_set_pp_dpm_mclk); 1813 1.4 riastrad static DEVICE_ATTR(pp_dpm_socclk, S_IRUGO | S_IWUSR, 1814 1.4 riastrad amdgpu_get_pp_dpm_socclk, 1815 1.4 riastrad amdgpu_set_pp_dpm_socclk); 1816 1.4 riastrad static DEVICE_ATTR(pp_dpm_fclk, S_IRUGO | S_IWUSR, 1817 1.4 riastrad amdgpu_get_pp_dpm_fclk, 1818 1.4 riastrad amdgpu_set_pp_dpm_fclk); 1819 1.4 riastrad static DEVICE_ATTR(pp_dpm_dcefclk, S_IRUGO | S_IWUSR, 1820 1.4 riastrad amdgpu_get_pp_dpm_dcefclk, 1821 1.4 riastrad amdgpu_set_pp_dpm_dcefclk); 1822 1.4 riastrad static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR, 1823 1.4 riastrad amdgpu_get_pp_dpm_pcie, 1824 1.4 riastrad amdgpu_set_pp_dpm_pcie); 1825 1.4 riastrad static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR, 1826 1.4 riastrad amdgpu_get_pp_sclk_od, 1827 1.4 riastrad amdgpu_set_pp_sclk_od); 1828 1.4 riastrad static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR, 1829 1.4 riastrad amdgpu_get_pp_mclk_od, 1830 1.4 riastrad amdgpu_set_pp_mclk_od); 1831 1.4 riastrad static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR, 1832 1.4 riastrad amdgpu_get_pp_power_profile_mode, 1833 1.4 riastrad amdgpu_set_pp_power_profile_mode); 1834 1.4 riastrad static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR, 1835 1.4 riastrad amdgpu_get_pp_od_clk_voltage, 1836 1.4 riastrad amdgpu_set_pp_od_clk_voltage); 1837 1.4 riastrad static DEVICE_ATTR(gpu_busy_percent, S_IRUGO, 1838 1.4 riastrad amdgpu_get_busy_percent, NULL); 1839 1.4 riastrad static DEVICE_ATTR(mem_busy_percent, S_IRUGO, 1840 1.4 riastrad amdgpu_get_memory_busy_percent, NULL); 1841 1.4 riastrad static DEVICE_ATTR(pcie_bw, S_IRUGO, amdgpu_get_pcie_bw, NULL); 1842 1.4 riastrad static DEVICE_ATTR(pp_features, S_IRUGO | S_IWUSR, 1843 1.4 riastrad amdgpu_get_pp_feature_status, 1844 1.4 riastrad amdgpu_set_pp_feature_status); 1845 1.4 riastrad static DEVICE_ATTR(unique_id, S_IRUGO, amdgpu_get_unique_id, NULL); 1846 1.4 riastrad 1847 1.4 riastrad static ssize_t amdgpu_hwmon_show_temp(struct device *dev, 1848 1.4 riastrad struct device_attribute *attr, 1849 1.4 riastrad char *buf) 1850 1.4 riastrad { 1851 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1852 1.4 riastrad int channel = to_sensor_dev_attr(attr)->index; 1853 1.4 riastrad int r, temp = 0, size = sizeof(temp); 1854 1.4 riastrad 1855 1.4 riastrad if (channel >= PP_TEMP_MAX) 1856 1.4 riastrad return -EINVAL; 1857 1.4 riastrad 1858 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 1859 1.4 riastrad if (r < 0) 1860 1.4 riastrad return r; 1861 1.4 riastrad 1862 1.4 riastrad switch (channel) { 1863 1.4 riastrad case PP_TEMP_JUNCTION: 1864 1.4 riastrad /* get current junction temperature */ 1865 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP, 1866 1.4 riastrad (void *)&temp, &size); 1867 1.4 riastrad break; 1868 1.4 riastrad case PP_TEMP_EDGE: 1869 1.4 riastrad /* get current edge temperature */ 1870 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP, 1871 1.4 riastrad (void *)&temp, &size); 1872 1.4 riastrad break; 1873 1.4 riastrad case PP_TEMP_MEM: 1874 1.4 riastrad /* get current memory temperature */ 1875 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP, 1876 1.4 riastrad (void *)&temp, &size); 1877 1.4 riastrad break; 1878 1.4 riastrad default: 1879 1.4 riastrad r = -EINVAL; 1880 1.4 riastrad break; 1881 1.4 riastrad } 1882 1.4 riastrad 1883 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 1884 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 1885 1.4 riastrad 1886 1.4 riastrad if (r) 1887 1.4 riastrad return r; 1888 1.4 riastrad 1889 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", temp); 1890 1.4 riastrad } 1891 1.4 riastrad 1892 1.4 riastrad static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev, 1893 1.4 riastrad struct device_attribute *attr, 1894 1.4 riastrad char *buf) 1895 1.4 riastrad { 1896 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1897 1.4 riastrad int hyst = to_sensor_dev_attr(attr)->index; 1898 1.4 riastrad int temp; 1899 1.4 riastrad 1900 1.4 riastrad if (hyst) 1901 1.4 riastrad temp = adev->pm.dpm.thermal.min_temp; 1902 1.4 riastrad else 1903 1.4 riastrad temp = adev->pm.dpm.thermal.max_temp; 1904 1.4 riastrad 1905 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", temp); 1906 1.4 riastrad } 1907 1.4 riastrad 1908 1.4 riastrad static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev, 1909 1.4 riastrad struct device_attribute *attr, 1910 1.4 riastrad char *buf) 1911 1.4 riastrad { 1912 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1913 1.4 riastrad int hyst = to_sensor_dev_attr(attr)->index; 1914 1.4 riastrad int temp; 1915 1.4 riastrad 1916 1.4 riastrad if (hyst) 1917 1.4 riastrad temp = adev->pm.dpm.thermal.min_hotspot_temp; 1918 1.4 riastrad else 1919 1.4 riastrad temp = adev->pm.dpm.thermal.max_hotspot_crit_temp; 1920 1.4 riastrad 1921 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", temp); 1922 1.4 riastrad } 1923 1.4 riastrad 1924 1.4 riastrad static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev, 1925 1.4 riastrad struct device_attribute *attr, 1926 1.4 riastrad char *buf) 1927 1.4 riastrad { 1928 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1929 1.4 riastrad int hyst = to_sensor_dev_attr(attr)->index; 1930 1.4 riastrad int temp; 1931 1.4 riastrad 1932 1.4 riastrad if (hyst) 1933 1.4 riastrad temp = adev->pm.dpm.thermal.min_mem_temp; 1934 1.4 riastrad else 1935 1.4 riastrad temp = adev->pm.dpm.thermal.max_mem_crit_temp; 1936 1.4 riastrad 1937 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", temp); 1938 1.4 riastrad } 1939 1.4 riastrad 1940 1.4 riastrad static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev, 1941 1.4 riastrad struct device_attribute *attr, 1942 1.4 riastrad char *buf) 1943 1.4 riastrad { 1944 1.4 riastrad int channel = to_sensor_dev_attr(attr)->index; 1945 1.4 riastrad 1946 1.4 riastrad if (channel >= PP_TEMP_MAX) 1947 1.4 riastrad return -EINVAL; 1948 1.4 riastrad 1949 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label); 1950 1.4 riastrad } 1951 1.4 riastrad 1952 1.4 riastrad static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev, 1953 1.4 riastrad struct device_attribute *attr, 1954 1.4 riastrad char *buf) 1955 1.4 riastrad { 1956 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1957 1.4 riastrad int channel = to_sensor_dev_attr(attr)->index; 1958 1.4 riastrad int temp = 0; 1959 1.4 riastrad 1960 1.4 riastrad if (channel >= PP_TEMP_MAX) 1961 1.4 riastrad return -EINVAL; 1962 1.4 riastrad 1963 1.4 riastrad switch (channel) { 1964 1.4 riastrad case PP_TEMP_JUNCTION: 1965 1.4 riastrad temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp; 1966 1.4 riastrad break; 1967 1.4 riastrad case PP_TEMP_EDGE: 1968 1.4 riastrad temp = adev->pm.dpm.thermal.max_edge_emergency_temp; 1969 1.4 riastrad break; 1970 1.4 riastrad case PP_TEMP_MEM: 1971 1.4 riastrad temp = adev->pm.dpm.thermal.max_mem_emergency_temp; 1972 1.4 riastrad break; 1973 1.4 riastrad } 1974 1.4 riastrad 1975 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", temp); 1976 1.4 riastrad } 1977 1.4 riastrad 1978 1.4 riastrad static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev, 1979 1.4 riastrad struct device_attribute *attr, 1980 1.4 riastrad char *buf) 1981 1.4 riastrad { 1982 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 1983 1.4 riastrad u32 pwm_mode = 0; 1984 1.4 riastrad int ret; 1985 1.4 riastrad 1986 1.4 riastrad ret = pm_runtime_get_sync(adev->ddev->dev); 1987 1.4 riastrad if (ret < 0) 1988 1.4 riastrad return ret; 1989 1.4 riastrad 1990 1.4 riastrad if (is_support_sw_smu(adev)) { 1991 1.4 riastrad pwm_mode = smu_get_fan_control_mode(&adev->smu); 1992 1.4 riastrad } else { 1993 1.4 riastrad if (!adev->powerplay.pp_funcs->get_fan_control_mode) { 1994 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 1995 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 1996 1.4 riastrad return -EINVAL; 1997 1.4 riastrad } 1998 1.4 riastrad 1999 1.4 riastrad pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2000 1.4 riastrad } 2001 1.4 riastrad 2002 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2003 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2004 1.4 riastrad 2005 1.4 riastrad return sprintf(buf, "%i\n", pwm_mode); 2006 1.4 riastrad } 2007 1.4 riastrad 2008 1.4 riastrad static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev, 2009 1.4 riastrad struct device_attribute *attr, 2010 1.4 riastrad const char *buf, 2011 1.4 riastrad size_t count) 2012 1.4 riastrad { 2013 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2014 1.4 riastrad int err, ret; 2015 1.4 riastrad int value; 2016 1.4 riastrad 2017 1.4 riastrad err = kstrtoint(buf, 10, &value); 2018 1.4 riastrad if (err) 2019 1.4 riastrad return err; 2020 1.4 riastrad 2021 1.4 riastrad ret = pm_runtime_get_sync(adev->ddev->dev); 2022 1.4 riastrad if (ret < 0) 2023 1.4 riastrad return ret; 2024 1.4 riastrad 2025 1.4 riastrad if (is_support_sw_smu(adev)) { 2026 1.4 riastrad smu_set_fan_control_mode(&adev->smu, value); 2027 1.4 riastrad } else { 2028 1.4 riastrad if (!adev->powerplay.pp_funcs->set_fan_control_mode) { 2029 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2030 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2031 1.4 riastrad return -EINVAL; 2032 1.4 riastrad } 2033 1.4 riastrad 2034 1.4 riastrad amdgpu_dpm_set_fan_control_mode(adev, value); 2035 1.4 riastrad } 2036 1.4 riastrad 2037 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2038 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2039 1.4 riastrad 2040 1.4 riastrad return count; 2041 1.4 riastrad } 2042 1.4 riastrad 2043 1.4 riastrad static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev, 2044 1.4 riastrad struct device_attribute *attr, 2045 1.4 riastrad char *buf) 2046 1.4 riastrad { 2047 1.4 riastrad return sprintf(buf, "%i\n", 0); 2048 1.4 riastrad } 2049 1.4 riastrad 2050 1.4 riastrad static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev, 2051 1.4 riastrad struct device_attribute *attr, 2052 1.4 riastrad char *buf) 2053 1.4 riastrad { 2054 1.4 riastrad return sprintf(buf, "%i\n", 255); 2055 1.4 riastrad } 2056 1.4 riastrad 2057 1.4 riastrad static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev, 2058 1.4 riastrad struct device_attribute *attr, 2059 1.4 riastrad const char *buf, size_t count) 2060 1.4 riastrad { 2061 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2062 1.4 riastrad int err; 2063 1.4 riastrad u32 value; 2064 1.4 riastrad u32 pwm_mode; 2065 1.4 riastrad 2066 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2067 1.4 riastrad if (err < 0) 2068 1.4 riastrad return err; 2069 1.4 riastrad 2070 1.4 riastrad if (is_support_sw_smu(adev)) 2071 1.4 riastrad pwm_mode = smu_get_fan_control_mode(&adev->smu); 2072 1.4 riastrad else 2073 1.4 riastrad pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2074 1.4 riastrad 2075 1.4 riastrad if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2076 1.4 riastrad pr_info("manual fan speed control should be enabled first\n"); 2077 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2078 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2079 1.4 riastrad return -EINVAL; 2080 1.4 riastrad } 2081 1.4 riastrad 2082 1.4 riastrad err = kstrtou32(buf, 10, &value); 2083 1.4 riastrad if (err) { 2084 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2085 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2086 1.4 riastrad return err; 2087 1.4 riastrad } 2088 1.4 riastrad 2089 1.4 riastrad value = (value * 100) / 255; 2090 1.4 riastrad 2091 1.4 riastrad if (is_support_sw_smu(adev)) 2092 1.4 riastrad err = smu_set_fan_speed_percent(&adev->smu, value); 2093 1.4 riastrad else if (adev->powerplay.pp_funcs->set_fan_speed_percent) 2094 1.4 riastrad err = amdgpu_dpm_set_fan_speed_percent(adev, value); 2095 1.4 riastrad else 2096 1.4 riastrad err = -EINVAL; 2097 1.4 riastrad 2098 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2099 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2100 1.4 riastrad 2101 1.4 riastrad if (err) 2102 1.4 riastrad return err; 2103 1.4 riastrad 2104 1.4 riastrad return count; 2105 1.4 riastrad } 2106 1.4 riastrad 2107 1.4 riastrad static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev, 2108 1.4 riastrad struct device_attribute *attr, 2109 1.4 riastrad char *buf) 2110 1.4 riastrad { 2111 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2112 1.4 riastrad int err; 2113 1.4 riastrad u32 speed = 0; 2114 1.4 riastrad 2115 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2116 1.4 riastrad if (err < 0) 2117 1.4 riastrad return err; 2118 1.4 riastrad 2119 1.4 riastrad if (is_support_sw_smu(adev)) 2120 1.4 riastrad err = smu_get_fan_speed_percent(&adev->smu, &speed); 2121 1.4 riastrad else if (adev->powerplay.pp_funcs->get_fan_speed_percent) 2122 1.4 riastrad err = amdgpu_dpm_get_fan_speed_percent(adev, &speed); 2123 1.4 riastrad else 2124 1.4 riastrad err = -EINVAL; 2125 1.4 riastrad 2126 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2127 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2128 1.4 riastrad 2129 1.4 riastrad if (err) 2130 1.4 riastrad return err; 2131 1.4 riastrad 2132 1.4 riastrad speed = (speed * 255) / 100; 2133 1.4 riastrad 2134 1.4 riastrad return sprintf(buf, "%i\n", speed); 2135 1.4 riastrad } 2136 1.4 riastrad 2137 1.4 riastrad static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev, 2138 1.4 riastrad struct device_attribute *attr, 2139 1.4 riastrad char *buf) 2140 1.4 riastrad { 2141 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2142 1.4 riastrad int err; 2143 1.4 riastrad u32 speed = 0; 2144 1.4 riastrad 2145 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2146 1.4 riastrad if (err < 0) 2147 1.4 riastrad return err; 2148 1.4 riastrad 2149 1.4 riastrad if (is_support_sw_smu(adev)) 2150 1.4 riastrad err = smu_get_fan_speed_rpm(&adev->smu, &speed); 2151 1.4 riastrad else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) 2152 1.4 riastrad err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed); 2153 1.4 riastrad else 2154 1.4 riastrad err = -EINVAL; 2155 1.4 riastrad 2156 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2157 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2158 1.4 riastrad 2159 1.4 riastrad if (err) 2160 1.4 riastrad return err; 2161 1.4 riastrad 2162 1.4 riastrad return sprintf(buf, "%i\n", speed); 2163 1.4 riastrad } 2164 1.4 riastrad 2165 1.4 riastrad static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev, 2166 1.4 riastrad struct device_attribute *attr, 2167 1.4 riastrad char *buf) 2168 1.4 riastrad { 2169 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2170 1.4 riastrad u32 min_rpm = 0; 2171 1.4 riastrad u32 size = sizeof(min_rpm); 2172 1.4 riastrad int r; 2173 1.4 riastrad 2174 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2175 1.4 riastrad if (r < 0) 2176 1.4 riastrad return r; 2177 1.4 riastrad 2178 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM, 2179 1.4 riastrad (void *)&min_rpm, &size); 2180 1.4 riastrad 2181 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2182 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2183 1.4 riastrad 2184 1.4 riastrad if (r) 2185 1.4 riastrad return r; 2186 1.4 riastrad 2187 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm); 2188 1.4 riastrad } 2189 1.4 riastrad 2190 1.4 riastrad static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev, 2191 1.4 riastrad struct device_attribute *attr, 2192 1.4 riastrad char *buf) 2193 1.4 riastrad { 2194 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2195 1.4 riastrad u32 max_rpm = 0; 2196 1.4 riastrad u32 size = sizeof(max_rpm); 2197 1.4 riastrad int r; 2198 1.4 riastrad 2199 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2200 1.4 riastrad if (r < 0) 2201 1.4 riastrad return r; 2202 1.4 riastrad 2203 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM, 2204 1.4 riastrad (void *)&max_rpm, &size); 2205 1.4 riastrad 2206 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2207 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2208 1.4 riastrad 2209 1.4 riastrad if (r) 2210 1.4 riastrad return r; 2211 1.4 riastrad 2212 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm); 2213 1.4 riastrad } 2214 1.4 riastrad 2215 1.4 riastrad static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev, 2216 1.4 riastrad struct device_attribute *attr, 2217 1.4 riastrad char *buf) 2218 1.4 riastrad { 2219 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2220 1.4 riastrad int err; 2221 1.4 riastrad u32 rpm = 0; 2222 1.4 riastrad 2223 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2224 1.4 riastrad if (err < 0) 2225 1.4 riastrad return err; 2226 1.4 riastrad 2227 1.4 riastrad if (is_support_sw_smu(adev)) 2228 1.4 riastrad err = smu_get_fan_speed_rpm(&adev->smu, &rpm); 2229 1.4 riastrad else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) 2230 1.4 riastrad err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm); 2231 1.4 riastrad else 2232 1.4 riastrad err = -EINVAL; 2233 1.4 riastrad 2234 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2235 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2236 1.4 riastrad 2237 1.4 riastrad if (err) 2238 1.4 riastrad return err; 2239 1.4 riastrad 2240 1.4 riastrad return sprintf(buf, "%i\n", rpm); 2241 1.4 riastrad } 2242 1.4 riastrad 2243 1.4 riastrad static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev, 2244 1.1 riastrad struct device_attribute *attr, 2245 1.1 riastrad const char *buf, size_t count) 2246 1.1 riastrad { 2247 1.1 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2248 1.4 riastrad int err; 2249 1.4 riastrad u32 value; 2250 1.4 riastrad u32 pwm_mode; 2251 1.4 riastrad 2252 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2253 1.4 riastrad if (err < 0) 2254 1.4 riastrad return err; 2255 1.4 riastrad 2256 1.4 riastrad if (is_support_sw_smu(adev)) 2257 1.4 riastrad pwm_mode = smu_get_fan_control_mode(&adev->smu); 2258 1.4 riastrad else 2259 1.4 riastrad pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2260 1.4 riastrad 2261 1.4 riastrad if (pwm_mode != AMD_FAN_CTRL_MANUAL) { 2262 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2263 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2264 1.4 riastrad return -ENODATA; 2265 1.4 riastrad } 2266 1.4 riastrad 2267 1.4 riastrad err = kstrtou32(buf, 10, &value); 2268 1.4 riastrad if (err) { 2269 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2270 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2271 1.4 riastrad return err; 2272 1.4 riastrad } 2273 1.4 riastrad 2274 1.4 riastrad if (is_support_sw_smu(adev)) 2275 1.4 riastrad err = smu_set_fan_speed_rpm(&adev->smu, value); 2276 1.4 riastrad else if (adev->powerplay.pp_funcs->set_fan_speed_rpm) 2277 1.4 riastrad err = amdgpu_dpm_set_fan_speed_rpm(adev, value); 2278 1.4 riastrad else 2279 1.4 riastrad err = -EINVAL; 2280 1.4 riastrad 2281 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2282 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2283 1.4 riastrad 2284 1.4 riastrad if (err) 2285 1.4 riastrad return err; 2286 1.4 riastrad 2287 1.4 riastrad return count; 2288 1.4 riastrad } 2289 1.4 riastrad 2290 1.4 riastrad static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev, 2291 1.4 riastrad struct device_attribute *attr, 2292 1.4 riastrad char *buf) 2293 1.4 riastrad { 2294 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2295 1.4 riastrad u32 pwm_mode = 0; 2296 1.4 riastrad int ret; 2297 1.4 riastrad 2298 1.4 riastrad ret = pm_runtime_get_sync(adev->ddev->dev); 2299 1.4 riastrad if (ret < 0) 2300 1.4 riastrad return ret; 2301 1.4 riastrad 2302 1.4 riastrad if (is_support_sw_smu(adev)) { 2303 1.4 riastrad pwm_mode = smu_get_fan_control_mode(&adev->smu); 2304 1.4 riastrad } else { 2305 1.4 riastrad if (!adev->powerplay.pp_funcs->get_fan_control_mode) { 2306 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2307 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2308 1.4 riastrad return -EINVAL; 2309 1.4 riastrad } 2310 1.4 riastrad 2311 1.4 riastrad pwm_mode = amdgpu_dpm_get_fan_control_mode(adev); 2312 1.4 riastrad } 2313 1.4 riastrad 2314 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2315 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2316 1.4 riastrad 2317 1.4 riastrad return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1); 2318 1.4 riastrad } 2319 1.4 riastrad 2320 1.4 riastrad static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev, 2321 1.4 riastrad struct device_attribute *attr, 2322 1.4 riastrad const char *buf, 2323 1.4 riastrad size_t count) 2324 1.4 riastrad { 2325 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2326 1.4 riastrad int err; 2327 1.4 riastrad int value; 2328 1.4 riastrad u32 pwm_mode; 2329 1.4 riastrad 2330 1.4 riastrad err = kstrtoint(buf, 10, &value); 2331 1.4 riastrad if (err) 2332 1.4 riastrad return err; 2333 1.4 riastrad 2334 1.4 riastrad if (value == 0) 2335 1.4 riastrad pwm_mode = AMD_FAN_CTRL_AUTO; 2336 1.4 riastrad else if (value == 1) 2337 1.4 riastrad pwm_mode = AMD_FAN_CTRL_MANUAL; 2338 1.4 riastrad else 2339 1.4 riastrad return -EINVAL; 2340 1.4 riastrad 2341 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2342 1.4 riastrad if (err < 0) 2343 1.4 riastrad return err; 2344 1.4 riastrad 2345 1.4 riastrad if (is_support_sw_smu(adev)) { 2346 1.4 riastrad smu_set_fan_control_mode(&adev->smu, pwm_mode); 2347 1.4 riastrad } else { 2348 1.4 riastrad if (!adev->powerplay.pp_funcs->set_fan_control_mode) { 2349 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2350 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2351 1.4 riastrad return -EINVAL; 2352 1.4 riastrad } 2353 1.4 riastrad amdgpu_dpm_set_fan_control_mode(adev, pwm_mode); 2354 1.4 riastrad } 2355 1.4 riastrad 2356 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2357 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2358 1.4 riastrad 2359 1.4 riastrad return count; 2360 1.4 riastrad } 2361 1.4 riastrad 2362 1.4 riastrad static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev, 2363 1.4 riastrad struct device_attribute *attr, 2364 1.4 riastrad char *buf) 2365 1.4 riastrad { 2366 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2367 1.4 riastrad u32 vddgfx; 2368 1.4 riastrad int r, size = sizeof(vddgfx); 2369 1.4 riastrad 2370 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2371 1.4 riastrad if (r < 0) 2372 1.4 riastrad return r; 2373 1.4 riastrad 2374 1.4 riastrad /* get the voltage */ 2375 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, 2376 1.4 riastrad (void *)&vddgfx, &size); 2377 1.4 riastrad 2378 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2379 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2380 1.4 riastrad 2381 1.4 riastrad if (r) 2382 1.4 riastrad return r; 2383 1.4 riastrad 2384 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx); 2385 1.4 riastrad } 2386 1.4 riastrad 2387 1.4 riastrad static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev, 2388 1.4 riastrad struct device_attribute *attr, 2389 1.4 riastrad char *buf) 2390 1.4 riastrad { 2391 1.4 riastrad return snprintf(buf, PAGE_SIZE, "vddgfx\n"); 2392 1.4 riastrad } 2393 1.4 riastrad 2394 1.4 riastrad static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev, 2395 1.4 riastrad struct device_attribute *attr, 2396 1.4 riastrad char *buf) 2397 1.4 riastrad { 2398 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2399 1.4 riastrad u32 vddnb; 2400 1.4 riastrad int r, size = sizeof(vddnb); 2401 1.4 riastrad 2402 1.4 riastrad /* only APUs have vddnb */ 2403 1.4 riastrad if (!(adev->flags & AMD_IS_APU)) 2404 1.4 riastrad return -EINVAL; 2405 1.4 riastrad 2406 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2407 1.4 riastrad if (r < 0) 2408 1.4 riastrad return r; 2409 1.4 riastrad 2410 1.4 riastrad /* get the voltage */ 2411 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, 2412 1.4 riastrad (void *)&vddnb, &size); 2413 1.4 riastrad 2414 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2415 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2416 1.4 riastrad 2417 1.4 riastrad if (r) 2418 1.4 riastrad return r; 2419 1.4 riastrad 2420 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", vddnb); 2421 1.4 riastrad } 2422 1.4 riastrad 2423 1.4 riastrad static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev, 2424 1.4 riastrad struct device_attribute *attr, 2425 1.4 riastrad char *buf) 2426 1.4 riastrad { 2427 1.4 riastrad return snprintf(buf, PAGE_SIZE, "vddnb\n"); 2428 1.4 riastrad } 2429 1.4 riastrad 2430 1.4 riastrad static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev, 2431 1.4 riastrad struct device_attribute *attr, 2432 1.4 riastrad char *buf) 2433 1.4 riastrad { 2434 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2435 1.4 riastrad u32 query = 0; 2436 1.4 riastrad int r, size = sizeof(u32); 2437 1.4 riastrad unsigned uw; 2438 1.4 riastrad 2439 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2440 1.4 riastrad if (r < 0) 2441 1.4 riastrad return r; 2442 1.4 riastrad 2443 1.4 riastrad /* get the voltage */ 2444 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, 2445 1.4 riastrad (void *)&query, &size); 2446 1.4 riastrad 2447 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2448 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2449 1.4 riastrad 2450 1.4 riastrad if (r) 2451 1.4 riastrad return r; 2452 1.4 riastrad 2453 1.4 riastrad /* convert to microwatts */ 2454 1.4 riastrad uw = (query >> 8) * 1000000 + (query & 0xff) * 1000; 2455 1.4 riastrad 2456 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%u\n", uw); 2457 1.4 riastrad } 2458 1.4 riastrad 2459 1.4 riastrad static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev, 2460 1.4 riastrad struct device_attribute *attr, 2461 1.4 riastrad char *buf) 2462 1.4 riastrad { 2463 1.4 riastrad return sprintf(buf, "%i\n", 0); 2464 1.4 riastrad } 2465 1.4 riastrad 2466 1.4 riastrad static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev, 2467 1.4 riastrad struct device_attribute *attr, 2468 1.4 riastrad char *buf) 2469 1.4 riastrad { 2470 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2471 1.4 riastrad uint32_t limit = 0; 2472 1.4 riastrad ssize_t size; 2473 1.4 riastrad int r; 2474 1.4 riastrad 2475 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2476 1.4 riastrad if (r < 0) 2477 1.4 riastrad return r; 2478 1.4 riastrad 2479 1.4 riastrad if (is_support_sw_smu(adev)) { 2480 1.4 riastrad smu_get_power_limit(&adev->smu, &limit, true, true); 2481 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000); 2482 1.4 riastrad } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) { 2483 1.4 riastrad adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true); 2484 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000); 2485 1.4 riastrad } else { 2486 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 2487 1.4 riastrad } 2488 1.4 riastrad 2489 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2490 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2491 1.4 riastrad 2492 1.4 riastrad return size; 2493 1.4 riastrad } 2494 1.4 riastrad 2495 1.4 riastrad static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev, 2496 1.4 riastrad struct device_attribute *attr, 2497 1.4 riastrad char *buf) 2498 1.4 riastrad { 2499 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2500 1.4 riastrad uint32_t limit = 0; 2501 1.4 riastrad ssize_t size; 2502 1.4 riastrad int r; 2503 1.4 riastrad 2504 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2505 1.4 riastrad if (r < 0) 2506 1.4 riastrad return r; 2507 1.4 riastrad 2508 1.4 riastrad if (is_support_sw_smu(adev)) { 2509 1.4 riastrad smu_get_power_limit(&adev->smu, &limit, false, true); 2510 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000); 2511 1.4 riastrad } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) { 2512 1.4 riastrad adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false); 2513 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000); 2514 1.4 riastrad } else { 2515 1.4 riastrad size = snprintf(buf, PAGE_SIZE, "\n"); 2516 1.4 riastrad } 2517 1.4 riastrad 2518 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2519 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2520 1.4 riastrad 2521 1.4 riastrad return size; 2522 1.4 riastrad } 2523 1.4 riastrad 2524 1.4 riastrad 2525 1.4 riastrad static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev, 2526 1.4 riastrad struct device_attribute *attr, 2527 1.4 riastrad const char *buf, 2528 1.4 riastrad size_t count) 2529 1.4 riastrad { 2530 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2531 1.4 riastrad int err; 2532 1.4 riastrad u32 value; 2533 1.4 riastrad 2534 1.4 riastrad if (amdgpu_sriov_vf(adev)) 2535 1.4 riastrad return -EINVAL; 2536 1.4 riastrad 2537 1.4 riastrad err = kstrtou32(buf, 10, &value); 2538 1.4 riastrad if (err) 2539 1.4 riastrad return err; 2540 1.4 riastrad 2541 1.4 riastrad value = value / 1000000; /* convert to Watt */ 2542 1.4 riastrad 2543 1.4 riastrad 2544 1.4 riastrad err = pm_runtime_get_sync(adev->ddev->dev); 2545 1.4 riastrad if (err < 0) 2546 1.4 riastrad return err; 2547 1.4 riastrad 2548 1.4 riastrad if (is_support_sw_smu(adev)) 2549 1.4 riastrad err = smu_set_power_limit(&adev->smu, value); 2550 1.4 riastrad else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) 2551 1.4 riastrad err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value); 2552 1.4 riastrad else 2553 1.4 riastrad err = -EINVAL; 2554 1.4 riastrad 2555 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2556 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2557 1.4 riastrad 2558 1.4 riastrad if (err) 2559 1.4 riastrad return err; 2560 1.4 riastrad 2561 1.4 riastrad return count; 2562 1.4 riastrad } 2563 1.4 riastrad 2564 1.4 riastrad static ssize_t amdgpu_hwmon_show_sclk(struct device *dev, 2565 1.4 riastrad struct device_attribute *attr, 2566 1.4 riastrad char *buf) 2567 1.4 riastrad { 2568 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2569 1.4 riastrad uint32_t sclk; 2570 1.4 riastrad int r, size = sizeof(sclk); 2571 1.4 riastrad 2572 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2573 1.4 riastrad if (r < 0) 2574 1.4 riastrad return r; 2575 1.4 riastrad 2576 1.4 riastrad /* get the sclk */ 2577 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, 2578 1.4 riastrad (void *)&sclk, &size); 2579 1.4 riastrad 2580 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2581 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2582 1.4 riastrad 2583 1.4 riastrad if (r) 2584 1.4 riastrad return r; 2585 1.4 riastrad 2586 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000); 2587 1.4 riastrad } 2588 1.4 riastrad 2589 1.4 riastrad static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev, 2590 1.4 riastrad struct device_attribute *attr, 2591 1.4 riastrad char *buf) 2592 1.4 riastrad { 2593 1.4 riastrad return snprintf(buf, PAGE_SIZE, "sclk\n"); 2594 1.4 riastrad } 2595 1.4 riastrad 2596 1.4 riastrad static ssize_t amdgpu_hwmon_show_mclk(struct device *dev, 2597 1.4 riastrad struct device_attribute *attr, 2598 1.4 riastrad char *buf) 2599 1.4 riastrad { 2600 1.4 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2601 1.4 riastrad uint32_t mclk; 2602 1.4 riastrad int r, size = sizeof(mclk); 2603 1.4 riastrad 2604 1.4 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 2605 1.4 riastrad if (r < 0) 2606 1.4 riastrad return r; 2607 1.1 riastrad 2608 1.4 riastrad /* get the sclk */ 2609 1.4 riastrad r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, 2610 1.4 riastrad (void *)&mclk, &size); 2611 1.1 riastrad 2612 1.4 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 2613 1.4 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 2614 1.1 riastrad 2615 1.4 riastrad if (r) 2616 1.4 riastrad return r; 2617 1.1 riastrad 2618 1.4 riastrad return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000); 2619 1.1 riastrad } 2620 1.1 riastrad 2621 1.4 riastrad static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev, 2622 1.4 riastrad struct device_attribute *attr, 2623 1.4 riastrad char *buf) 2624 1.1 riastrad { 2625 1.4 riastrad return snprintf(buf, PAGE_SIZE, "mclk\n"); 2626 1.4 riastrad } 2627 1.1 riastrad 2628 1.4 riastrad /** 2629 1.4 riastrad * DOC: hwmon 2630 1.4 riastrad * 2631 1.4 riastrad * The amdgpu driver exposes the following sensor interfaces: 2632 1.4 riastrad * 2633 1.4 riastrad * - GPU temperature (via the on-die sensor) 2634 1.4 riastrad * 2635 1.4 riastrad * - GPU voltage 2636 1.4 riastrad * 2637 1.4 riastrad * - Northbridge voltage (APUs only) 2638 1.4 riastrad * 2639 1.4 riastrad * - GPU power 2640 1.4 riastrad * 2641 1.4 riastrad * - GPU fan 2642 1.4 riastrad * 2643 1.4 riastrad * - GPU gfx/compute engine clock 2644 1.4 riastrad * 2645 1.4 riastrad * - GPU memory clock (dGPU only) 2646 1.4 riastrad * 2647 1.4 riastrad * hwmon interfaces for GPU temperature: 2648 1.4 riastrad * 2649 1.4 riastrad * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius 2650 1.4 riastrad * - temp2_input and temp3_input are supported on SOC15 dGPUs only 2651 1.4 riastrad * 2652 1.4 riastrad * - temp[1-3]_label: temperature channel label 2653 1.4 riastrad * - temp2_label and temp3_label are supported on SOC15 dGPUs only 2654 1.4 riastrad * 2655 1.4 riastrad * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius 2656 1.4 riastrad * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only 2657 1.4 riastrad * 2658 1.4 riastrad * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius 2659 1.4 riastrad * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only 2660 1.4 riastrad * 2661 1.4 riastrad * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius 2662 1.4 riastrad * - these are supported on SOC15 dGPUs only 2663 1.4 riastrad * 2664 1.4 riastrad * hwmon interfaces for GPU voltage: 2665 1.4 riastrad * 2666 1.4 riastrad * - in0_input: the voltage on the GPU in millivolts 2667 1.4 riastrad * 2668 1.4 riastrad * - in1_input: the voltage on the Northbridge in millivolts 2669 1.4 riastrad * 2670 1.4 riastrad * hwmon interfaces for GPU power: 2671 1.4 riastrad * 2672 1.4 riastrad * - power1_average: average power used by the GPU in microWatts 2673 1.4 riastrad * 2674 1.4 riastrad * - power1_cap_min: minimum cap supported in microWatts 2675 1.4 riastrad * 2676 1.4 riastrad * - power1_cap_max: maximum cap supported in microWatts 2677 1.4 riastrad * 2678 1.4 riastrad * - power1_cap: selected power cap in microWatts 2679 1.4 riastrad * 2680 1.4 riastrad * hwmon interfaces for GPU fan: 2681 1.4 riastrad * 2682 1.4 riastrad * - pwm1: pulse width modulation fan level (0-255) 2683 1.4 riastrad * 2684 1.4 riastrad * - pwm1_enable: pulse width modulation fan control method (0: no fan speed control, 1: manual fan speed control using pwm interface, 2: automatic fan speed control) 2685 1.4 riastrad * 2686 1.4 riastrad * - pwm1_min: pulse width modulation fan control minimum level (0) 2687 1.4 riastrad * 2688 1.4 riastrad * - pwm1_max: pulse width modulation fan control maximum level (255) 2689 1.4 riastrad * 2690 1.4 riastrad * - fan1_min: an minimum value Unit: revolution/min (RPM) 2691 1.4 riastrad * 2692 1.4 riastrad * - fan1_max: an maxmum value Unit: revolution/max (RPM) 2693 1.4 riastrad * 2694 1.4 riastrad * - fan1_input: fan speed in RPM 2695 1.4 riastrad * 2696 1.4 riastrad * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM) 2697 1.4 riastrad * 2698 1.4 riastrad * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable 2699 1.4 riastrad * 2700 1.4 riastrad * hwmon interfaces for GPU clocks: 2701 1.4 riastrad * 2702 1.4 riastrad * - freq1_input: the gfx/compute clock in hertz 2703 1.4 riastrad * 2704 1.4 riastrad * - freq2_input: the memory clock in hertz 2705 1.4 riastrad * 2706 1.4 riastrad * You can use hwmon tools like sensors to view this information on your system. 2707 1.4 riastrad * 2708 1.4 riastrad */ 2709 1.1 riastrad 2710 1.4 riastrad static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE); 2711 1.1 riastrad static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0); 2712 1.1 riastrad static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1); 2713 1.4 riastrad static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE); 2714 1.4 riastrad static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION); 2715 1.4 riastrad static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0); 2716 1.4 riastrad static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1); 2717 1.4 riastrad static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION); 2718 1.4 riastrad static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM); 2719 1.4 riastrad static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0); 2720 1.4 riastrad static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1); 2721 1.4 riastrad static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM); 2722 1.4 riastrad static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE); 2723 1.4 riastrad static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION); 2724 1.4 riastrad static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM); 2725 1.1 riastrad static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0); 2726 1.1 riastrad static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0); 2727 1.1 riastrad static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0); 2728 1.1 riastrad static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0); 2729 1.4 riastrad static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0); 2730 1.4 riastrad static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0); 2731 1.4 riastrad static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0); 2732 1.4 riastrad static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0); 2733 1.4 riastrad static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0); 2734 1.4 riastrad static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0); 2735 1.4 riastrad static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0); 2736 1.4 riastrad static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0); 2737 1.4 riastrad static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0); 2738 1.4 riastrad static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0); 2739 1.4 riastrad static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0); 2740 1.4 riastrad static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0); 2741 1.4 riastrad static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0); 2742 1.4 riastrad static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0); 2743 1.4 riastrad static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0); 2744 1.4 riastrad static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0); 2745 1.4 riastrad static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0); 2746 1.1 riastrad 2747 1.1 riastrad static struct attribute *hwmon_attributes[] = { 2748 1.1 riastrad &sensor_dev_attr_temp1_input.dev_attr.attr, 2749 1.1 riastrad &sensor_dev_attr_temp1_crit.dev_attr.attr, 2750 1.1 riastrad &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, 2751 1.4 riastrad &sensor_dev_attr_temp2_input.dev_attr.attr, 2752 1.4 riastrad &sensor_dev_attr_temp2_crit.dev_attr.attr, 2753 1.4 riastrad &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, 2754 1.4 riastrad &sensor_dev_attr_temp3_input.dev_attr.attr, 2755 1.4 riastrad &sensor_dev_attr_temp3_crit.dev_attr.attr, 2756 1.4 riastrad &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, 2757 1.4 riastrad &sensor_dev_attr_temp1_emergency.dev_attr.attr, 2758 1.4 riastrad &sensor_dev_attr_temp2_emergency.dev_attr.attr, 2759 1.4 riastrad &sensor_dev_attr_temp3_emergency.dev_attr.attr, 2760 1.4 riastrad &sensor_dev_attr_temp1_label.dev_attr.attr, 2761 1.4 riastrad &sensor_dev_attr_temp2_label.dev_attr.attr, 2762 1.4 riastrad &sensor_dev_attr_temp3_label.dev_attr.attr, 2763 1.1 riastrad &sensor_dev_attr_pwm1.dev_attr.attr, 2764 1.1 riastrad &sensor_dev_attr_pwm1_enable.dev_attr.attr, 2765 1.1 riastrad &sensor_dev_attr_pwm1_min.dev_attr.attr, 2766 1.1 riastrad &sensor_dev_attr_pwm1_max.dev_attr.attr, 2767 1.4 riastrad &sensor_dev_attr_fan1_input.dev_attr.attr, 2768 1.4 riastrad &sensor_dev_attr_fan1_min.dev_attr.attr, 2769 1.4 riastrad &sensor_dev_attr_fan1_max.dev_attr.attr, 2770 1.4 riastrad &sensor_dev_attr_fan1_target.dev_attr.attr, 2771 1.4 riastrad &sensor_dev_attr_fan1_enable.dev_attr.attr, 2772 1.4 riastrad &sensor_dev_attr_in0_input.dev_attr.attr, 2773 1.4 riastrad &sensor_dev_attr_in0_label.dev_attr.attr, 2774 1.4 riastrad &sensor_dev_attr_in1_input.dev_attr.attr, 2775 1.4 riastrad &sensor_dev_attr_in1_label.dev_attr.attr, 2776 1.4 riastrad &sensor_dev_attr_power1_average.dev_attr.attr, 2777 1.4 riastrad &sensor_dev_attr_power1_cap_max.dev_attr.attr, 2778 1.4 riastrad &sensor_dev_attr_power1_cap_min.dev_attr.attr, 2779 1.4 riastrad &sensor_dev_attr_power1_cap.dev_attr.attr, 2780 1.4 riastrad &sensor_dev_attr_freq1_input.dev_attr.attr, 2781 1.4 riastrad &sensor_dev_attr_freq1_label.dev_attr.attr, 2782 1.4 riastrad &sensor_dev_attr_freq2_input.dev_attr.attr, 2783 1.4 riastrad &sensor_dev_attr_freq2_label.dev_attr.attr, 2784 1.1 riastrad NULL 2785 1.1 riastrad }; 2786 1.1 riastrad 2787 1.1 riastrad static umode_t hwmon_attributes_visible(struct kobject *kobj, 2788 1.1 riastrad struct attribute *attr, int index) 2789 1.1 riastrad { 2790 1.4 riastrad struct device *dev = kobj_to_dev(kobj); 2791 1.1 riastrad struct amdgpu_device *adev = dev_get_drvdata(dev); 2792 1.1 riastrad umode_t effective_mode = attr->mode; 2793 1.1 riastrad 2794 1.4 riastrad /* under multi-vf mode, the hwmon attributes are all not supported */ 2795 1.4 riastrad if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) 2796 1.4 riastrad return 0; 2797 1.4 riastrad 2798 1.4 riastrad /* there is no fan under pp one vf mode */ 2799 1.4 riastrad if (amdgpu_sriov_is_pp_one_vf(adev) && 2800 1.4 riastrad (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 2801 1.4 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 2802 1.4 riastrad attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 2803 1.4 riastrad attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 2804 1.4 riastrad attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 2805 1.4 riastrad attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 2806 1.4 riastrad attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 2807 1.4 riastrad attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 2808 1.4 riastrad attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 2809 1.4 riastrad return 0; 2810 1.4 riastrad 2811 1.4 riastrad /* Skip fan attributes if fan is not present */ 2812 1.4 riastrad if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 2813 1.4 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 2814 1.4 riastrad attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 2815 1.4 riastrad attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 2816 1.4 riastrad attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 2817 1.4 riastrad attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 2818 1.4 riastrad attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 2819 1.4 riastrad attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 2820 1.4 riastrad attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 2821 1.4 riastrad return 0; 2822 1.4 riastrad 2823 1.4 riastrad /* Skip fan attributes on APU */ 2824 1.4 riastrad if ((adev->flags & AMD_IS_APU) && 2825 1.4 riastrad (attr == &sensor_dev_attr_pwm1.dev_attr.attr || 2826 1.4 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 2827 1.4 riastrad attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 2828 1.4 riastrad attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 2829 1.4 riastrad attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 2830 1.4 riastrad attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 2831 1.4 riastrad attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 2832 1.4 riastrad attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 2833 1.4 riastrad attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 2834 1.4 riastrad return 0; 2835 1.4 riastrad 2836 1.4 riastrad /* Skip limit attributes if DPM is not enabled */ 2837 1.1 riastrad if (!adev->pm.dpm_enabled && 2838 1.1 riastrad (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr || 2839 1.1 riastrad attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr || 2840 1.1 riastrad attr == &sensor_dev_attr_pwm1.dev_attr.attr || 2841 1.1 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr || 2842 1.1 riastrad attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 2843 1.4 riastrad attr == &sensor_dev_attr_pwm1_min.dev_attr.attr || 2844 1.4 riastrad attr == &sensor_dev_attr_fan1_input.dev_attr.attr || 2845 1.4 riastrad attr == &sensor_dev_attr_fan1_min.dev_attr.attr || 2846 1.4 riastrad attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 2847 1.4 riastrad attr == &sensor_dev_attr_fan1_target.dev_attr.attr || 2848 1.4 riastrad attr == &sensor_dev_attr_fan1_enable.dev_attr.attr)) 2849 1.4 riastrad return 0; 2850 1.4 riastrad 2851 1.4 riastrad if (!is_support_sw_smu(adev)) { 2852 1.4 riastrad /* mask fan attributes if we have no bindings for this asic to expose */ 2853 1.4 riastrad if ((!adev->powerplay.pp_funcs->get_fan_speed_percent && 2854 1.4 riastrad attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */ 2855 1.4 riastrad (!adev->powerplay.pp_funcs->get_fan_control_mode && 2856 1.4 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */ 2857 1.4 riastrad effective_mode &= ~S_IRUGO; 2858 1.4 riastrad 2859 1.4 riastrad if ((!adev->powerplay.pp_funcs->set_fan_speed_percent && 2860 1.4 riastrad attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */ 2861 1.4 riastrad (!adev->powerplay.pp_funcs->set_fan_control_mode && 2862 1.4 riastrad attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */ 2863 1.4 riastrad effective_mode &= ~S_IWUSR; 2864 1.4 riastrad } 2865 1.4 riastrad 2866 1.4 riastrad if (((adev->flags & AMD_IS_APU) || 2867 1.4 riastrad adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 2868 1.4 riastrad adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */ 2869 1.4 riastrad (attr == &sensor_dev_attr_power1_average.dev_attr.attr || 2870 1.4 riastrad attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr || 2871 1.4 riastrad attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr|| 2872 1.4 riastrad attr == &sensor_dev_attr_power1_cap.dev_attr.attr)) 2873 1.1 riastrad return 0; 2874 1.1 riastrad 2875 1.4 riastrad if (!is_support_sw_smu(adev)) { 2876 1.4 riastrad /* hide max/min values if we can't both query and manage the fan */ 2877 1.4 riastrad if ((!adev->powerplay.pp_funcs->set_fan_speed_percent && 2878 1.4 riastrad !adev->powerplay.pp_funcs->get_fan_speed_percent) && 2879 1.4 riastrad (!adev->powerplay.pp_funcs->set_fan_speed_rpm && 2880 1.4 riastrad !adev->powerplay.pp_funcs->get_fan_speed_rpm) && 2881 1.4 riastrad (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr || 2882 1.4 riastrad attr == &sensor_dev_attr_pwm1_min.dev_attr.attr)) 2883 1.4 riastrad return 0; 2884 1.4 riastrad 2885 1.4 riastrad if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm && 2886 1.4 riastrad !adev->powerplay.pp_funcs->get_fan_speed_rpm) && 2887 1.4 riastrad (attr == &sensor_dev_attr_fan1_max.dev_attr.attr || 2888 1.4 riastrad attr == &sensor_dev_attr_fan1_min.dev_attr.attr)) 2889 1.4 riastrad return 0; 2890 1.4 riastrad } 2891 1.4 riastrad 2892 1.4 riastrad if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */ 2893 1.4 riastrad adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */ 2894 1.4 riastrad (attr == &sensor_dev_attr_in0_input.dev_attr.attr || 2895 1.4 riastrad attr == &sensor_dev_attr_in0_label.dev_attr.attr)) 2896 1.4 riastrad return 0; 2897 1.4 riastrad 2898 1.4 riastrad /* only APUs have vddnb */ 2899 1.4 riastrad if (!(adev->flags & AMD_IS_APU) && 2900 1.4 riastrad (attr == &sensor_dev_attr_in1_input.dev_attr.attr || 2901 1.4 riastrad attr == &sensor_dev_attr_in1_label.dev_attr.attr)) 2902 1.4 riastrad return 0; 2903 1.4 riastrad 2904 1.4 riastrad /* no mclk on APUs */ 2905 1.4 riastrad if ((adev->flags & AMD_IS_APU) && 2906 1.4 riastrad (attr == &sensor_dev_attr_freq2_input.dev_attr.attr || 2907 1.4 riastrad attr == &sensor_dev_attr_freq2_label.dev_attr.attr)) 2908 1.1 riastrad return 0; 2909 1.1 riastrad 2910 1.4 riastrad /* only SOC15 dGPUs support hotspot and mem temperatures */ 2911 1.4 riastrad if (((adev->flags & AMD_IS_APU) || 2912 1.4 riastrad adev->asic_type < CHIP_VEGA10) && 2913 1.4 riastrad (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr || 2914 1.4 riastrad attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr || 2915 1.4 riastrad attr == &sensor_dev_attr_temp3_crit.dev_attr.attr || 2916 1.4 riastrad attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr || 2917 1.4 riastrad attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr || 2918 1.4 riastrad attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr || 2919 1.4 riastrad attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr || 2920 1.4 riastrad attr == &sensor_dev_attr_temp2_input.dev_attr.attr || 2921 1.4 riastrad attr == &sensor_dev_attr_temp3_input.dev_attr.attr || 2922 1.4 riastrad attr == &sensor_dev_attr_temp2_label.dev_attr.attr || 2923 1.4 riastrad attr == &sensor_dev_attr_temp3_label.dev_attr.attr)) 2924 1.1 riastrad return 0; 2925 1.1 riastrad 2926 1.1 riastrad return effective_mode; 2927 1.1 riastrad } 2928 1.1 riastrad 2929 1.1 riastrad static const struct attribute_group hwmon_attrgroup = { 2930 1.1 riastrad .attrs = hwmon_attributes, 2931 1.1 riastrad .is_visible = hwmon_attributes_visible, 2932 1.1 riastrad }; 2933 1.1 riastrad 2934 1.1 riastrad static const struct attribute_group *hwmon_groups[] = { 2935 1.1 riastrad &hwmon_attrgroup, 2936 1.1 riastrad NULL 2937 1.1 riastrad }; 2938 1.1 riastrad 2939 1.3 riastrad #endif /* __NetBSD__ */ 2940 1.3 riastrad 2941 1.1 riastrad void amdgpu_dpm_thermal_work_handler(struct work_struct *work) 2942 1.1 riastrad { 2943 1.1 riastrad struct amdgpu_device *adev = 2944 1.1 riastrad container_of(work, struct amdgpu_device, 2945 1.1 riastrad pm.dpm.thermal.work); 2946 1.1 riastrad /* switch to the thermal state */ 2947 1.4 riastrad enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL; 2948 1.4 riastrad int temp, size = sizeof(temp); 2949 1.1 riastrad 2950 1.1 riastrad if (!adev->pm.dpm_enabled) 2951 1.1 riastrad return; 2952 1.1 riastrad 2953 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, 2954 1.4 riastrad (void *)&temp, &size)) { 2955 1.1 riastrad if (temp < adev->pm.dpm.thermal.min_temp) 2956 1.1 riastrad /* switch back the user state */ 2957 1.1 riastrad dpm_state = adev->pm.dpm.user_state; 2958 1.1 riastrad } else { 2959 1.1 riastrad if (adev->pm.dpm.thermal.high_to_low) 2960 1.1 riastrad /* switch back the user state */ 2961 1.1 riastrad dpm_state = adev->pm.dpm.user_state; 2962 1.1 riastrad } 2963 1.1 riastrad mutex_lock(&adev->pm.mutex); 2964 1.1 riastrad if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL) 2965 1.1 riastrad adev->pm.dpm.thermal_active = true; 2966 1.1 riastrad else 2967 1.1 riastrad adev->pm.dpm.thermal_active = false; 2968 1.1 riastrad adev->pm.dpm.state = dpm_state; 2969 1.1 riastrad mutex_unlock(&adev->pm.mutex); 2970 1.1 riastrad 2971 1.1 riastrad amdgpu_pm_compute_clocks(adev); 2972 1.1 riastrad } 2973 1.1 riastrad 2974 1.1 riastrad static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev, 2975 1.4 riastrad enum amd_pm_state_type dpm_state) 2976 1.1 riastrad { 2977 1.1 riastrad int i; 2978 1.1 riastrad struct amdgpu_ps *ps; 2979 1.1 riastrad u32 ui_class; 2980 1.1 riastrad bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ? 2981 1.1 riastrad true : false; 2982 1.1 riastrad 2983 1.1 riastrad /* check if the vblank period is too short to adjust the mclk */ 2984 1.4 riastrad if (single_display && adev->powerplay.pp_funcs->vblank_too_short) { 2985 1.1 riastrad if (amdgpu_dpm_vblank_too_short(adev)) 2986 1.1 riastrad single_display = false; 2987 1.1 riastrad } 2988 1.1 riastrad 2989 1.1 riastrad /* certain older asics have a separare 3D performance state, 2990 1.1 riastrad * so try that first if the user selected performance 2991 1.1 riastrad */ 2992 1.1 riastrad if (dpm_state == POWER_STATE_TYPE_PERFORMANCE) 2993 1.1 riastrad dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF; 2994 1.1 riastrad /* balanced states don't exist at the moment */ 2995 1.1 riastrad if (dpm_state == POWER_STATE_TYPE_BALANCED) 2996 1.1 riastrad dpm_state = POWER_STATE_TYPE_PERFORMANCE; 2997 1.1 riastrad 2998 1.1 riastrad restart_search: 2999 1.1 riastrad /* Pick the best power state based on current conditions */ 3000 1.1 riastrad for (i = 0; i < adev->pm.dpm.num_ps; i++) { 3001 1.1 riastrad ps = &adev->pm.dpm.ps[i]; 3002 1.1 riastrad ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK; 3003 1.1 riastrad switch (dpm_state) { 3004 1.1 riastrad /* user states */ 3005 1.1 riastrad case POWER_STATE_TYPE_BATTERY: 3006 1.1 riastrad if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) { 3007 1.1 riastrad if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) { 3008 1.1 riastrad if (single_display) 3009 1.1 riastrad return ps; 3010 1.1 riastrad } else 3011 1.1 riastrad return ps; 3012 1.1 riastrad } 3013 1.1 riastrad break; 3014 1.1 riastrad case POWER_STATE_TYPE_BALANCED: 3015 1.1 riastrad if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) { 3016 1.1 riastrad if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) { 3017 1.1 riastrad if (single_display) 3018 1.1 riastrad return ps; 3019 1.1 riastrad } else 3020 1.1 riastrad return ps; 3021 1.1 riastrad } 3022 1.1 riastrad break; 3023 1.1 riastrad case POWER_STATE_TYPE_PERFORMANCE: 3024 1.1 riastrad if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) { 3025 1.1 riastrad if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) { 3026 1.1 riastrad if (single_display) 3027 1.1 riastrad return ps; 3028 1.1 riastrad } else 3029 1.1 riastrad return ps; 3030 1.1 riastrad } 3031 1.1 riastrad break; 3032 1.1 riastrad /* internal states */ 3033 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD: 3034 1.1 riastrad if (adev->pm.dpm.uvd_ps) 3035 1.1 riastrad return adev->pm.dpm.uvd_ps; 3036 1.1 riastrad else 3037 1.1 riastrad break; 3038 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_SD: 3039 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE) 3040 1.1 riastrad return ps; 3041 1.1 riastrad break; 3042 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_HD: 3043 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE) 3044 1.1 riastrad return ps; 3045 1.1 riastrad break; 3046 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_HD2: 3047 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE) 3048 1.1 riastrad return ps; 3049 1.1 riastrad break; 3050 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_MVC: 3051 1.1 riastrad if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC) 3052 1.1 riastrad return ps; 3053 1.1 riastrad break; 3054 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_BOOT: 3055 1.1 riastrad return adev->pm.dpm.boot_ps; 3056 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_THERMAL: 3057 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL) 3058 1.1 riastrad return ps; 3059 1.1 riastrad break; 3060 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_ACPI: 3061 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) 3062 1.1 riastrad return ps; 3063 1.1 riastrad break; 3064 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_ULV: 3065 1.1 riastrad if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) 3066 1.1 riastrad return ps; 3067 1.1 riastrad break; 3068 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_3DPERF: 3069 1.1 riastrad if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE) 3070 1.1 riastrad return ps; 3071 1.1 riastrad break; 3072 1.1 riastrad default: 3073 1.1 riastrad break; 3074 1.1 riastrad } 3075 1.1 riastrad } 3076 1.1 riastrad /* use a fallback state if we didn't match */ 3077 1.1 riastrad switch (dpm_state) { 3078 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_SD: 3079 1.1 riastrad dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD; 3080 1.1 riastrad goto restart_search; 3081 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_HD: 3082 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_HD2: 3083 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_UVD_MVC: 3084 1.1 riastrad if (adev->pm.dpm.uvd_ps) { 3085 1.1 riastrad return adev->pm.dpm.uvd_ps; 3086 1.1 riastrad } else { 3087 1.1 riastrad dpm_state = POWER_STATE_TYPE_PERFORMANCE; 3088 1.1 riastrad goto restart_search; 3089 1.1 riastrad } 3090 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_THERMAL: 3091 1.1 riastrad dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI; 3092 1.1 riastrad goto restart_search; 3093 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_ACPI: 3094 1.1 riastrad dpm_state = POWER_STATE_TYPE_BATTERY; 3095 1.1 riastrad goto restart_search; 3096 1.1 riastrad case POWER_STATE_TYPE_BATTERY: 3097 1.1 riastrad case POWER_STATE_TYPE_BALANCED: 3098 1.1 riastrad case POWER_STATE_TYPE_INTERNAL_3DPERF: 3099 1.1 riastrad dpm_state = POWER_STATE_TYPE_PERFORMANCE; 3100 1.1 riastrad goto restart_search; 3101 1.1 riastrad default: 3102 1.1 riastrad break; 3103 1.1 riastrad } 3104 1.1 riastrad 3105 1.1 riastrad return NULL; 3106 1.1 riastrad } 3107 1.1 riastrad 3108 1.1 riastrad static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev) 3109 1.1 riastrad { 3110 1.1 riastrad struct amdgpu_ps *ps; 3111 1.4 riastrad enum amd_pm_state_type dpm_state; 3112 1.1 riastrad int ret; 3113 1.4 riastrad bool equal = false; 3114 1.1 riastrad 3115 1.1 riastrad /* if dpm init failed */ 3116 1.1 riastrad if (!adev->pm.dpm_enabled) 3117 1.1 riastrad return; 3118 1.1 riastrad 3119 1.1 riastrad if (adev->pm.dpm.user_state != adev->pm.dpm.state) { 3120 1.1 riastrad /* add other state override checks here */ 3121 1.1 riastrad if ((!adev->pm.dpm.thermal_active) && 3122 1.1 riastrad (!adev->pm.dpm.uvd_active)) 3123 1.1 riastrad adev->pm.dpm.state = adev->pm.dpm.user_state; 3124 1.1 riastrad } 3125 1.1 riastrad dpm_state = adev->pm.dpm.state; 3126 1.1 riastrad 3127 1.1 riastrad ps = amdgpu_dpm_pick_power_state(adev, dpm_state); 3128 1.1 riastrad if (ps) 3129 1.1 riastrad adev->pm.dpm.requested_ps = ps; 3130 1.1 riastrad else 3131 1.1 riastrad return; 3132 1.1 riastrad 3133 1.4 riastrad if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) { 3134 1.1 riastrad printk("switching from power state:\n"); 3135 1.1 riastrad amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps); 3136 1.1 riastrad printk("switching to power state:\n"); 3137 1.1 riastrad amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps); 3138 1.1 riastrad } 3139 1.1 riastrad 3140 1.1 riastrad /* update whether vce is active */ 3141 1.1 riastrad ps->vce_active = adev->pm.dpm.vce_active; 3142 1.4 riastrad if (adev->powerplay.pp_funcs->display_configuration_changed) 3143 1.4 riastrad amdgpu_dpm_display_configuration_changed(adev); 3144 1.1 riastrad 3145 1.1 riastrad ret = amdgpu_dpm_pre_set_power_state(adev); 3146 1.1 riastrad if (ret) 3147 1.4 riastrad return; 3148 1.1 riastrad 3149 1.4 riastrad if (adev->powerplay.pp_funcs->check_state_equal) { 3150 1.4 riastrad if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal)) 3151 1.4 riastrad equal = false; 3152 1.4 riastrad } 3153 1.1 riastrad 3154 1.4 riastrad if (equal) 3155 1.4 riastrad return; 3156 1.1 riastrad 3157 1.1 riastrad amdgpu_dpm_set_power_state(adev); 3158 1.1 riastrad amdgpu_dpm_post_set_power_state(adev); 3159 1.1 riastrad 3160 1.1 riastrad adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs; 3161 1.1 riastrad adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count; 3162 1.1 riastrad 3163 1.4 riastrad if (adev->powerplay.pp_funcs->force_performance_level) { 3164 1.1 riastrad if (adev->pm.dpm.thermal_active) { 3165 1.4 riastrad enum amd_dpm_forced_level level = adev->pm.dpm.forced_level; 3166 1.1 riastrad /* force low perf level for thermal */ 3167 1.4 riastrad amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW); 3168 1.1 riastrad /* save the user's level */ 3169 1.1 riastrad adev->pm.dpm.forced_level = level; 3170 1.1 riastrad } else { 3171 1.1 riastrad /* otherwise, user selected level */ 3172 1.1 riastrad amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level); 3173 1.1 riastrad } 3174 1.1 riastrad } 3175 1.1 riastrad } 3176 1.1 riastrad 3177 1.1 riastrad void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable) 3178 1.1 riastrad { 3179 1.4 riastrad int ret = 0; 3180 1.4 riastrad 3181 1.4 riastrad ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable); 3182 1.4 riastrad if (ret) 3183 1.4 riastrad DRM_ERROR("Dpm %s uvd failed, ret = %d. \n", 3184 1.4 riastrad enable ? "enable" : "disable", ret); 3185 1.1 riastrad 3186 1.4 riastrad /* enable/disable Low Memory PState for UVD (4k videos) */ 3187 1.4 riastrad if (adev->asic_type == CHIP_STONEY && 3188 1.4 riastrad adev->uvd.decode_image_width >= WIDTH_4K) { 3189 1.4 riastrad struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle; 3190 1.4 riastrad 3191 1.4 riastrad if (hwmgr && hwmgr->hwmgr_func && 3192 1.4 riastrad hwmgr->hwmgr_func->update_nbdpm_pstate) 3193 1.4 riastrad hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr, 3194 1.4 riastrad !enable, 3195 1.4 riastrad true); 3196 1.1 riastrad } 3197 1.1 riastrad } 3198 1.1 riastrad 3199 1.1 riastrad void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable) 3200 1.1 riastrad { 3201 1.4 riastrad int ret = 0; 3202 1.1 riastrad 3203 1.4 riastrad ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable); 3204 1.4 riastrad if (ret) 3205 1.4 riastrad DRM_ERROR("Dpm %s vce failed, ret = %d. \n", 3206 1.4 riastrad enable ? "enable" : "disable", ret); 3207 1.1 riastrad } 3208 1.1 riastrad 3209 1.1 riastrad void amdgpu_pm_print_power_states(struct amdgpu_device *adev) 3210 1.1 riastrad { 3211 1.1 riastrad int i; 3212 1.1 riastrad 3213 1.4 riastrad if (adev->powerplay.pp_funcs->print_power_state == NULL) 3214 1.4 riastrad return; 3215 1.4 riastrad 3216 1.4 riastrad for (i = 0; i < adev->pm.dpm.num_ps; i++) 3217 1.1 riastrad amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]); 3218 1.4 riastrad 3219 1.4 riastrad } 3220 1.4 riastrad 3221 1.4 riastrad void amdgpu_dpm_enable_jpeg(struct amdgpu_device *adev, bool enable) 3222 1.4 riastrad { 3223 1.4 riastrad int ret = 0; 3224 1.4 riastrad 3225 1.4 riastrad ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_JPEG, !enable); 3226 1.4 riastrad if (ret) 3227 1.4 riastrad DRM_ERROR("Dpm %s jpeg failed, ret = %d. \n", 3228 1.4 riastrad enable ? "enable" : "disable", ret); 3229 1.4 riastrad } 3230 1.4 riastrad 3231 1.4 riastrad int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version) 3232 1.4 riastrad { 3233 1.4 riastrad int r; 3234 1.4 riastrad 3235 1.4 riastrad if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) { 3236 1.4 riastrad r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle); 3237 1.4 riastrad if (r) { 3238 1.4 riastrad pr_err("smu firmware loading failed\n"); 3239 1.4 riastrad return r; 3240 1.4 riastrad } 3241 1.4 riastrad *smu_version = adev->pm.fw_version; 3242 1.1 riastrad } 3243 1.4 riastrad return 0; 3244 1.1 riastrad } 3245 1.1 riastrad 3246 1.1 riastrad int amdgpu_pm_sysfs_init(struct amdgpu_device *adev) 3247 1.1 riastrad { 3248 1.3 riastrad #ifdef __NetBSD__ /* XXX sysfs */ 3249 1.3 riastrad return 0; 3250 1.3 riastrad #else 3251 1.4 riastrad struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle; 3252 1.1 riastrad int ret; 3253 1.1 riastrad 3254 1.1 riastrad if (adev->pm.sysfs_initialized) 3255 1.1 riastrad return 0; 3256 1.1 riastrad 3257 1.4 riastrad if (adev->pm.dpm_enabled == 0) 3258 1.1 riastrad return 0; 3259 1.4 riastrad 3260 1.1 riastrad adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev, 3261 1.1 riastrad DRIVER_NAME, adev, 3262 1.1 riastrad hwmon_groups); 3263 1.1 riastrad if (IS_ERR(adev->pm.int_hwmon_dev)) { 3264 1.1 riastrad ret = PTR_ERR(adev->pm.int_hwmon_dev); 3265 1.1 riastrad dev_err(adev->dev, 3266 1.1 riastrad "Unable to register hwmon device: %d\n", ret); 3267 1.1 riastrad return ret; 3268 1.1 riastrad } 3269 1.1 riastrad 3270 1.1 riastrad ret = device_create_file(adev->dev, &dev_attr_power_dpm_state); 3271 1.1 riastrad if (ret) { 3272 1.1 riastrad DRM_ERROR("failed to create device file for dpm state\n"); 3273 1.1 riastrad return ret; 3274 1.1 riastrad } 3275 1.1 riastrad ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level); 3276 1.1 riastrad if (ret) { 3277 1.1 riastrad DRM_ERROR("failed to create device file for dpm state\n"); 3278 1.1 riastrad return ret; 3279 1.1 riastrad } 3280 1.4 riastrad 3281 1.4 riastrad 3282 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_num_states); 3283 1.4 riastrad if (ret) { 3284 1.4 riastrad DRM_ERROR("failed to create device file pp_num_states\n"); 3285 1.4 riastrad return ret; 3286 1.4 riastrad } 3287 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_cur_state); 3288 1.4 riastrad if (ret) { 3289 1.4 riastrad DRM_ERROR("failed to create device file pp_cur_state\n"); 3290 1.4 riastrad return ret; 3291 1.4 riastrad } 3292 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_force_state); 3293 1.4 riastrad if (ret) { 3294 1.4 riastrad DRM_ERROR("failed to create device file pp_force_state\n"); 3295 1.4 riastrad return ret; 3296 1.4 riastrad } 3297 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_table); 3298 1.4 riastrad if (ret) { 3299 1.4 riastrad DRM_ERROR("failed to create device file pp_table\n"); 3300 1.4 riastrad return ret; 3301 1.4 riastrad } 3302 1.4 riastrad 3303 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk); 3304 1.4 riastrad if (ret) { 3305 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_sclk\n"); 3306 1.4 riastrad return ret; 3307 1.4 riastrad } 3308 1.4 riastrad 3309 1.4 riastrad /* Arcturus does not support standalone mclk/socclk/fclk level setting */ 3310 1.4 riastrad if (adev->asic_type == CHIP_ARCTURUS) { 3311 1.4 riastrad dev_attr_pp_dpm_mclk.attr.mode &= ~S_IWUGO; 3312 1.4 riastrad dev_attr_pp_dpm_mclk.store = NULL; 3313 1.4 riastrad 3314 1.4 riastrad dev_attr_pp_dpm_socclk.attr.mode &= ~S_IWUGO; 3315 1.4 riastrad dev_attr_pp_dpm_socclk.store = NULL; 3316 1.4 riastrad 3317 1.4 riastrad dev_attr_pp_dpm_fclk.attr.mode &= ~S_IWUGO; 3318 1.4 riastrad dev_attr_pp_dpm_fclk.store = NULL; 3319 1.4 riastrad } 3320 1.4 riastrad 3321 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk); 3322 1.4 riastrad if (ret) { 3323 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_mclk\n"); 3324 1.4 riastrad return ret; 3325 1.4 riastrad } 3326 1.4 riastrad if (adev->asic_type >= CHIP_VEGA10) { 3327 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_socclk); 3328 1.4 riastrad if (ret) { 3329 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_socclk\n"); 3330 1.4 riastrad return ret; 3331 1.4 riastrad } 3332 1.4 riastrad if (adev->asic_type != CHIP_ARCTURUS) { 3333 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_dcefclk); 3334 1.4 riastrad if (ret) { 3335 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_dcefclk\n"); 3336 1.4 riastrad return ret; 3337 1.4 riastrad } 3338 1.4 riastrad } 3339 1.4 riastrad } 3340 1.4 riastrad if (adev->asic_type >= CHIP_VEGA20) { 3341 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_fclk); 3342 1.4 riastrad if (ret) { 3343 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_fclk\n"); 3344 1.4 riastrad return ret; 3345 1.4 riastrad } 3346 1.4 riastrad } 3347 1.4 riastrad if (adev->asic_type != CHIP_ARCTURUS) { 3348 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie); 3349 1.4 riastrad if (ret) { 3350 1.4 riastrad DRM_ERROR("failed to create device file pp_dpm_pcie\n"); 3351 1.4 riastrad return ret; 3352 1.4 riastrad } 3353 1.4 riastrad } 3354 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od); 3355 1.4 riastrad if (ret) { 3356 1.4 riastrad DRM_ERROR("failed to create device file pp_sclk_od\n"); 3357 1.4 riastrad return ret; 3358 1.4 riastrad } 3359 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od); 3360 1.4 riastrad if (ret) { 3361 1.4 riastrad DRM_ERROR("failed to create device file pp_mclk_od\n"); 3362 1.4 riastrad return ret; 3363 1.4 riastrad } 3364 1.4 riastrad ret = device_create_file(adev->dev, 3365 1.4 riastrad &dev_attr_pp_power_profile_mode); 3366 1.4 riastrad if (ret) { 3367 1.4 riastrad DRM_ERROR("failed to create device file " 3368 1.4 riastrad "pp_power_profile_mode\n"); 3369 1.4 riastrad return ret; 3370 1.4 riastrad } 3371 1.4 riastrad if ((is_support_sw_smu(adev) && adev->smu.od_enabled) || 3372 1.4 riastrad (!is_support_sw_smu(adev) && hwmgr->od_enabled)) { 3373 1.4 riastrad ret = device_create_file(adev->dev, 3374 1.4 riastrad &dev_attr_pp_od_clk_voltage); 3375 1.4 riastrad if (ret) { 3376 1.4 riastrad DRM_ERROR("failed to create device file " 3377 1.4 riastrad "pp_od_clk_voltage\n"); 3378 1.4 riastrad return ret; 3379 1.4 riastrad } 3380 1.4 riastrad } 3381 1.4 riastrad ret = device_create_file(adev->dev, 3382 1.4 riastrad &dev_attr_gpu_busy_percent); 3383 1.4 riastrad if (ret) { 3384 1.4 riastrad DRM_ERROR("failed to create device file " 3385 1.4 riastrad "gpu_busy_level\n"); 3386 1.4 riastrad return ret; 3387 1.4 riastrad } 3388 1.4 riastrad /* APU does not have its own dedicated memory */ 3389 1.4 riastrad if (!(adev->flags & AMD_IS_APU) && 3390 1.4 riastrad (adev->asic_type != CHIP_VEGA10)) { 3391 1.4 riastrad ret = device_create_file(adev->dev, 3392 1.4 riastrad &dev_attr_mem_busy_percent); 3393 1.4 riastrad if (ret) { 3394 1.4 riastrad DRM_ERROR("failed to create device file " 3395 1.4 riastrad "mem_busy_percent\n"); 3396 1.4 riastrad return ret; 3397 1.4 riastrad } 3398 1.4 riastrad } 3399 1.4 riastrad /* PCIe Perf counters won't work on APU nodes */ 3400 1.4 riastrad if (!(adev->flags & AMD_IS_APU)) { 3401 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_pcie_bw); 3402 1.4 riastrad if (ret) { 3403 1.4 riastrad DRM_ERROR("failed to create device file pcie_bw\n"); 3404 1.4 riastrad return ret; 3405 1.4 riastrad } 3406 1.4 riastrad } 3407 1.4 riastrad if (adev->unique_id) 3408 1.4 riastrad ret = device_create_file(adev->dev, &dev_attr_unique_id); 3409 1.4 riastrad if (ret) { 3410 1.4 riastrad DRM_ERROR("failed to create device file unique_id\n"); 3411 1.4 riastrad return ret; 3412 1.4 riastrad } 3413 1.1 riastrad ret = amdgpu_debugfs_pm_init(adev); 3414 1.1 riastrad if (ret) { 3415 1.1 riastrad DRM_ERROR("Failed to register debugfs file for dpm!\n"); 3416 1.1 riastrad return ret; 3417 1.1 riastrad } 3418 1.1 riastrad 3419 1.4 riastrad if ((adev->asic_type >= CHIP_VEGA10) && 3420 1.4 riastrad !(adev->flags & AMD_IS_APU)) { 3421 1.4 riastrad ret = device_create_file(adev->dev, 3422 1.4 riastrad &dev_attr_pp_features); 3423 1.4 riastrad if (ret) { 3424 1.4 riastrad DRM_ERROR("failed to create device file " 3425 1.4 riastrad "pp_features\n"); 3426 1.4 riastrad return ret; 3427 1.4 riastrad } 3428 1.4 riastrad } 3429 1.4 riastrad 3430 1.1 riastrad adev->pm.sysfs_initialized = true; 3431 1.1 riastrad 3432 1.1 riastrad return 0; 3433 1.3 riastrad #endif 3434 1.1 riastrad } 3435 1.1 riastrad 3436 1.1 riastrad void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev) 3437 1.1 riastrad { 3438 1.3 riastrad #ifndef __NetBSD__ 3439 1.4 riastrad struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle; 3440 1.4 riastrad 3441 1.4 riastrad if (adev->pm.dpm_enabled == 0) 3442 1.4 riastrad return; 3443 1.4 riastrad 3444 1.1 riastrad if (adev->pm.int_hwmon_dev) 3445 1.1 riastrad hwmon_device_unregister(adev->pm.int_hwmon_dev); 3446 1.1 riastrad device_remove_file(adev->dev, &dev_attr_power_dpm_state); 3447 1.1 riastrad device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level); 3448 1.4 riastrad 3449 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_num_states); 3450 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_cur_state); 3451 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_force_state); 3452 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_table); 3453 1.4 riastrad 3454 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk); 3455 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk); 3456 1.4 riastrad if (adev->asic_type >= CHIP_VEGA10) { 3457 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_socclk); 3458 1.4 riastrad if (adev->asic_type != CHIP_ARCTURUS) 3459 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_dcefclk); 3460 1.4 riastrad } 3461 1.4 riastrad if (adev->asic_type != CHIP_ARCTURUS) 3462 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie); 3463 1.4 riastrad if (adev->asic_type >= CHIP_VEGA20) 3464 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_dpm_fclk); 3465 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_sclk_od); 3466 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_mclk_od); 3467 1.4 riastrad device_remove_file(adev->dev, 3468 1.4 riastrad &dev_attr_pp_power_profile_mode); 3469 1.4 riastrad if ((is_support_sw_smu(adev) && adev->smu.od_enabled) || 3470 1.4 riastrad (!is_support_sw_smu(adev) && hwmgr->od_enabled)) 3471 1.4 riastrad device_remove_file(adev->dev, 3472 1.4 riastrad &dev_attr_pp_od_clk_voltage); 3473 1.4 riastrad device_remove_file(adev->dev, &dev_attr_gpu_busy_percent); 3474 1.4 riastrad if (!(adev->flags & AMD_IS_APU) && 3475 1.4 riastrad (adev->asic_type != CHIP_VEGA10)) 3476 1.4 riastrad device_remove_file(adev->dev, &dev_attr_mem_busy_percent); 3477 1.4 riastrad if (!(adev->flags & AMD_IS_APU)) 3478 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pcie_bw); 3479 1.4 riastrad if (adev->unique_id) 3480 1.4 riastrad device_remove_file(adev->dev, &dev_attr_unique_id); 3481 1.4 riastrad if ((adev->asic_type >= CHIP_VEGA10) && 3482 1.4 riastrad !(adev->flags & AMD_IS_APU)) 3483 1.4 riastrad device_remove_file(adev->dev, &dev_attr_pp_features); 3484 1.3 riastrad #endif 3485 1.1 riastrad } 3486 1.1 riastrad 3487 1.1 riastrad void amdgpu_pm_compute_clocks(struct amdgpu_device *adev) 3488 1.1 riastrad { 3489 1.4 riastrad int i = 0; 3490 1.1 riastrad 3491 1.1 riastrad if (!adev->pm.dpm_enabled) 3492 1.1 riastrad return; 3493 1.1 riastrad 3494 1.4 riastrad if (adev->mode_info.num_crtc) 3495 1.4 riastrad amdgpu_display_bandwidth_update(adev); 3496 1.4 riastrad 3497 1.4 riastrad for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 3498 1.4 riastrad struct amdgpu_ring *ring = adev->rings[i]; 3499 1.4 riastrad if (ring && ring->sched.ready) 3500 1.4 riastrad amdgpu_fence_wait_empty(ring); 3501 1.4 riastrad } 3502 1.1 riastrad 3503 1.4 riastrad if (is_support_sw_smu(adev)) { 3504 1.4 riastrad struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm; 3505 1.4 riastrad smu_handle_task(&adev->smu, 3506 1.4 riastrad smu_dpm->dpm_level, 3507 1.4 riastrad AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, 3508 1.4 riastrad true); 3509 1.4 riastrad } else { 3510 1.4 riastrad if (adev->powerplay.pp_funcs->dispatch_tasks) { 3511 1.4 riastrad if (!amdgpu_device_has_dc_support(adev)) { 3512 1.4 riastrad mutex_lock(&adev->pm.mutex); 3513 1.4 riastrad amdgpu_dpm_get_active_displays(adev); 3514 1.4 riastrad adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count; 3515 1.4 riastrad adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev); 3516 1.4 riastrad adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev); 3517 1.4 riastrad /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */ 3518 1.4 riastrad if (adev->pm.pm_display_cfg.vrefresh > 120) 3519 1.4 riastrad adev->pm.pm_display_cfg.min_vblank_time = 0; 3520 1.4 riastrad if (adev->powerplay.pp_funcs->display_configuration_change) 3521 1.4 riastrad adev->powerplay.pp_funcs->display_configuration_change( 3522 1.4 riastrad adev->powerplay.pp_handle, 3523 1.4 riastrad &adev->pm.pm_display_cfg); 3524 1.4 riastrad mutex_unlock(&adev->pm.mutex); 3525 1.1 riastrad } 3526 1.4 riastrad amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL); 3527 1.4 riastrad } else { 3528 1.4 riastrad mutex_lock(&adev->pm.mutex); 3529 1.4 riastrad amdgpu_dpm_get_active_displays(adev); 3530 1.4 riastrad amdgpu_dpm_change_power_state_locked(adev); 3531 1.4 riastrad mutex_unlock(&adev->pm.mutex); 3532 1.1 riastrad } 3533 1.1 riastrad } 3534 1.4 riastrad } 3535 1.1 riastrad 3536 1.4 riastrad /* 3537 1.4 riastrad * Debugfs info 3538 1.4 riastrad */ 3539 1.4 riastrad #if defined(CONFIG_DEBUG_FS) 3540 1.1 riastrad 3541 1.4 riastrad static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev) 3542 1.4 riastrad { 3543 1.4 riastrad uint32_t value; 3544 1.4 riastrad uint64_t value64; 3545 1.4 riastrad uint32_t query = 0; 3546 1.4 riastrad int size; 3547 1.4 riastrad 3548 1.4 riastrad /* GPU Clocks */ 3549 1.4 riastrad size = sizeof(value); 3550 1.4 riastrad seq_printf(m, "GFX Clocks and Power:\n"); 3551 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size)) 3552 1.4 riastrad seq_printf(m, "\t%u MHz (MCLK)\n", value/100); 3553 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size)) 3554 1.4 riastrad seq_printf(m, "\t%u MHz (SCLK)\n", value/100); 3555 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size)) 3556 1.4 riastrad seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100); 3557 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size)) 3558 1.4 riastrad seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100); 3559 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size)) 3560 1.4 riastrad seq_printf(m, "\t%u mV (VDDGFX)\n", value); 3561 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size)) 3562 1.4 riastrad seq_printf(m, "\t%u mV (VDDNB)\n", value); 3563 1.4 riastrad size = sizeof(uint32_t); 3564 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) 3565 1.4 riastrad seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff); 3566 1.4 riastrad size = sizeof(value); 3567 1.4 riastrad seq_printf(m, "\n"); 3568 1.4 riastrad 3569 1.4 riastrad /* GPU Temp */ 3570 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size)) 3571 1.4 riastrad seq_printf(m, "GPU Temperature: %u C\n", value/1000); 3572 1.4 riastrad 3573 1.4 riastrad /* GPU Load */ 3574 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size)) 3575 1.4 riastrad seq_printf(m, "GPU Load: %u %%\n", value); 3576 1.4 riastrad /* MEM Load */ 3577 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size)) 3578 1.4 riastrad seq_printf(m, "MEM Load: %u %%\n", value); 3579 1.4 riastrad 3580 1.4 riastrad seq_printf(m, "\n"); 3581 1.4 riastrad 3582 1.4 riastrad /* SMC feature mask */ 3583 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size)) 3584 1.4 riastrad seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64); 3585 1.4 riastrad 3586 1.4 riastrad if (adev->asic_type > CHIP_VEGA20) { 3587 1.4 riastrad /* VCN clocks */ 3588 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) { 3589 1.4 riastrad if (!value) { 3590 1.4 riastrad seq_printf(m, "VCN: Disabled\n"); 3591 1.4 riastrad } else { 3592 1.4 riastrad seq_printf(m, "VCN: Enabled\n"); 3593 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3594 1.4 riastrad seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3595 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3596 1.4 riastrad seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3597 1.4 riastrad } 3598 1.4 riastrad } 3599 1.4 riastrad seq_printf(m, "\n"); 3600 1.4 riastrad } else { 3601 1.4 riastrad /* UVD clocks */ 3602 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) { 3603 1.4 riastrad if (!value) { 3604 1.4 riastrad seq_printf(m, "UVD: Disabled\n"); 3605 1.4 riastrad } else { 3606 1.4 riastrad seq_printf(m, "UVD: Enabled\n"); 3607 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size)) 3608 1.4 riastrad seq_printf(m, "\t%u MHz (DCLK)\n", value/100); 3609 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size)) 3610 1.4 riastrad seq_printf(m, "\t%u MHz (VCLK)\n", value/100); 3611 1.4 riastrad } 3612 1.4 riastrad } 3613 1.4 riastrad seq_printf(m, "\n"); 3614 1.1 riastrad 3615 1.4 riastrad /* VCE clocks */ 3616 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) { 3617 1.4 riastrad if (!value) { 3618 1.4 riastrad seq_printf(m, "VCE: Disabled\n"); 3619 1.4 riastrad } else { 3620 1.4 riastrad seq_printf(m, "VCE: Enabled\n"); 3621 1.4 riastrad if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size)) 3622 1.4 riastrad seq_printf(m, "\t%u MHz (ECCLK)\n", value/100); 3623 1.4 riastrad } 3624 1.4 riastrad } 3625 1.4 riastrad } 3626 1.1 riastrad 3627 1.4 riastrad return 0; 3628 1.1 riastrad } 3629 1.1 riastrad 3630 1.4 riastrad static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags) 3631 1.4 riastrad { 3632 1.4 riastrad int i; 3633 1.4 riastrad 3634 1.4 riastrad for (i = 0; clocks[i].flag; i++) 3635 1.4 riastrad seq_printf(m, "\t%s: %s\n", clocks[i].name, 3636 1.4 riastrad (flags & clocks[i].flag) ? "On" : "Off"); 3637 1.4 riastrad } 3638 1.1 riastrad 3639 1.1 riastrad static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data) 3640 1.1 riastrad { 3641 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *) m->private; 3642 1.1 riastrad struct drm_device *dev = node->minor->dev; 3643 1.1 riastrad struct amdgpu_device *adev = dev->dev_private; 3644 1.4 riastrad u32 flags = 0; 3645 1.4 riastrad int r; 3646 1.4 riastrad 3647 1.4 riastrad r = pm_runtime_get_sync(dev->dev); 3648 1.4 riastrad if (r < 0) 3649 1.4 riastrad return r; 3650 1.4 riastrad 3651 1.4 riastrad amdgpu_device_ip_get_clockgating_state(adev, &flags); 3652 1.4 riastrad seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags); 3653 1.4 riastrad amdgpu_parse_cg_state(m, flags); 3654 1.4 riastrad seq_printf(m, "\n"); 3655 1.4 riastrad 3656 1.4 riastrad if (!adev->pm.dpm_enabled) { 3657 1.4 riastrad seq_printf(m, "dpm not enabled\n"); 3658 1.4 riastrad pm_runtime_mark_last_busy(dev->dev); 3659 1.4 riastrad pm_runtime_put_autosuspend(dev->dev); 3660 1.4 riastrad return 0; 3661 1.4 riastrad } 3662 1.1 riastrad 3663 1.4 riastrad if (!is_support_sw_smu(adev) && 3664 1.4 riastrad adev->powerplay.pp_funcs->debugfs_print_current_performance_level) { 3665 1.1 riastrad mutex_lock(&adev->pm.mutex); 3666 1.4 riastrad if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) 3667 1.4 riastrad adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m); 3668 1.1 riastrad else 3669 1.1 riastrad seq_printf(m, "Debugfs support not implemented for this asic\n"); 3670 1.1 riastrad mutex_unlock(&adev->pm.mutex); 3671 1.4 riastrad r = 0; 3672 1.4 riastrad } else { 3673 1.4 riastrad r = amdgpu_debugfs_pm_info_pp(m, adev); 3674 1.1 riastrad } 3675 1.1 riastrad 3676 1.4 riastrad pm_runtime_mark_last_busy(dev->dev); 3677 1.4 riastrad pm_runtime_put_autosuspend(dev->dev); 3678 1.4 riastrad 3679 1.4 riastrad return r; 3680 1.1 riastrad } 3681 1.1 riastrad 3682 1.4 riastrad static const struct drm_info_list amdgpu_pm_info_list[] = { 3683 1.1 riastrad {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL}, 3684 1.1 riastrad }; 3685 1.1 riastrad #endif 3686 1.1 riastrad 3687 1.3 riastrad #ifndef __NetBSD__ /* XXX sysfs */ 3688 1.1 riastrad static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev) 3689 1.1 riastrad { 3690 1.1 riastrad #if defined(CONFIG_DEBUG_FS) 3691 1.1 riastrad return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list)); 3692 1.1 riastrad #else 3693 1.1 riastrad return 0; 3694 1.1 riastrad #endif 3695 1.1 riastrad } 3696 1.3 riastrad #endif 3697