blob: 94055a485e01300e5106fa261b6ce51b636360ab [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
Alex Deucher9ce6aae2017-11-30 21:29:47 -05002 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -04004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Rafał Miłecki <zajec5@gmail.com>
23 * Alex Deucher <alexdeucher@gmail.com>
24 */
25#include <drm/drmP.h>
26#include "amdgpu.h"
27#include "amdgpu_drv.h"
28#include "amdgpu_pm.h"
29#include "amdgpu_dpm.h"
Huang Rui5df585252018-08-09 09:50:12 -050030#include "amdgpu_display.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include "atom.h"
32#include <linux/power_supply.h>
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
Gustavo A. R. Silvaddf74e72018-07-23 11:32:32 -050035#include <linux/nospec.h>
Rex Zhu1b5708f2015-11-10 18:25:24 -050036
Alex Deucherd38ceaf2015-04-20 16:55:21 -040037static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
38
Huang Ruia8503b12017-01-05 19:17:13 +080039static const struct cg_flag_name clocks[] = {
40 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
41 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
42 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
43 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
Huang Rui54170222017-01-11 09:55:34 +080044 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080045 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
46 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
47 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
Huang Rui12ad27f2017-03-24 09:58:11 +080048 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
49 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080050 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
51 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
52 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
53 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
Huang Ruie96487a2017-03-24 10:12:32 +080054 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080055 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
56 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
57 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
58 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
59 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080060 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
61 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080062 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080063 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080064 {0, NULL},
65};
66
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
68{
69 if (adev->pm.dpm_enabled) {
70 mutex_lock(&adev->pm.mutex);
71 if (power_supply_is_system_supplied() > 0)
Rex Zhu600ae892018-06-04 16:39:38 +080072 adev->pm.ac_power = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 else
Rex Zhu600ae892018-06-04 16:39:38 +080074 adev->pm.ac_power = false;
Rex Zhucd4d7462017-09-06 18:43:52 +080075 if (adev->powerplay.pp_funcs->enable_bapm)
Rex Zhu600ae892018-06-04 16:39:38 +080076 amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 mutex_unlock(&adev->pm.mutex);
78 }
79}
80
Alex Deucherca8d40c2018-04-19 13:56:41 -050081/**
82 * DOC: power_dpm_state
83 *
Alex Deucherdc85db22018-06-01 12:28:14 -050084 * The power_dpm_state file is a legacy interface and is only provided for
85 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
86 * certain power related parameters. The file power_dpm_state is used for this.
Alex Deucherca8d40c2018-04-19 13:56:41 -050087 * It accepts the following arguments:
Alex Deucherdc85db22018-06-01 12:28:14 -050088 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050089 * - battery
Alex Deucherdc85db22018-06-01 12:28:14 -050090 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050091 * - balanced
Alex Deucherdc85db22018-06-01 12:28:14 -050092 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050093 * - performance
94 *
95 * battery
96 *
97 * On older GPUs, the vbios provided a special power state for battery
98 * operation. Selecting battery switched to this state. This is no
99 * longer provided on newer GPUs so the option does nothing in that case.
100 *
101 * balanced
102 *
103 * On older GPUs, the vbios provided a special power state for balanced
104 * operation. Selecting balanced switched to this state. This is no
105 * longer provided on newer GPUs so the option does nothing in that case.
106 *
107 * performance
108 *
109 * On older GPUs, the vbios provided a special power state for performance
110 * operation. Selecting performance switched to this state. This is no
111 * longer provided on newer GPUs so the option does nothing in that case.
112 *
113 */
114
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115static ssize_t amdgpu_get_dpm_state(struct device *dev,
116 struct device_attribute *attr,
117 char *buf)
118{
119 struct drm_device *ddev = dev_get_drvdata(dev);
120 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500121 enum amd_pm_state_type pm;
122
Rex Zhucd4d7462017-09-06 18:43:52 +0800123 if (adev->powerplay.pp_funcs->get_current_power_state)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500124 pm = amdgpu_dpm_get_current_power_state(adev);
Rex Zhucd4d7462017-09-06 18:43:52 +0800125 else
Rex Zhu1b5708f2015-11-10 18:25:24 -0500126 pm = adev->pm.dpm.user_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127
128 return snprintf(buf, PAGE_SIZE, "%s\n",
129 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
130 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
131}
132
133static ssize_t amdgpu_set_dpm_state(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf,
136 size_t count)
137{
138 struct drm_device *ddev = dev_get_drvdata(dev);
139 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500140 enum amd_pm_state_type state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 if (strncmp("battery", buf, strlen("battery")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500143 state = POWER_STATE_TYPE_BATTERY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500145 state = POWER_STATE_TYPE_BALANCED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 else if (strncmp("performance", buf, strlen("performance")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500147 state = POWER_STATE_TYPE_PERFORMANCE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 count = -EINVAL;
150 goto fail;
151 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152
Rex Zhu6d07fe72017-09-25 18:51:50 +0800153 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800154 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
Rex Zhu1b5708f2015-11-10 18:25:24 -0500155 } else {
156 mutex_lock(&adev->pm.mutex);
157 adev->pm.dpm.user_state = state;
158 mutex_unlock(&adev->pm.mutex);
159
160 /* Can't set dpm state when the card is off */
161 if (!(adev->flags & AMD_IS_PX) ||
162 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
163 amdgpu_pm_compute_clocks(adev);
164 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165fail:
166 return count;
167}
168
Alex Deucher8567f682018-04-19 13:46:03 -0500169
170/**
171 * DOC: power_dpm_force_performance_level
172 *
173 * The amdgpu driver provides a sysfs API for adjusting certain power
174 * related parameters. The file power_dpm_force_performance_level is
175 * used for this. It accepts the following arguments:
Alex Deucherdc85db22018-06-01 12:28:14 -0500176 *
Alex Deucher8567f682018-04-19 13:46:03 -0500177 * - auto
Alex Deucherdc85db22018-06-01 12:28:14 -0500178 *
Alex Deucher8567f682018-04-19 13:46:03 -0500179 * - low
Alex Deucherdc85db22018-06-01 12:28:14 -0500180 *
Alex Deucher8567f682018-04-19 13:46:03 -0500181 * - high
Alex Deucherdc85db22018-06-01 12:28:14 -0500182 *
Alex Deucher8567f682018-04-19 13:46:03 -0500183 * - manual
Alex Deucherdc85db22018-06-01 12:28:14 -0500184 *
Alex Deucher8567f682018-04-19 13:46:03 -0500185 * - profile_standard
Alex Deucherdc85db22018-06-01 12:28:14 -0500186 *
Alex Deucher8567f682018-04-19 13:46:03 -0500187 * - profile_min_sclk
Alex Deucherdc85db22018-06-01 12:28:14 -0500188 *
Alex Deucher8567f682018-04-19 13:46:03 -0500189 * - profile_min_mclk
Alex Deucherdc85db22018-06-01 12:28:14 -0500190 *
Alex Deucher8567f682018-04-19 13:46:03 -0500191 * - profile_peak
192 *
193 * auto
194 *
195 * When auto is selected, the driver will attempt to dynamically select
196 * the optimal power profile for current conditions in the driver.
197 *
198 * low
199 *
200 * When low is selected, the clocks are forced to the lowest power state.
201 *
202 * high
203 *
204 * When high is selected, the clocks are forced to the highest power state.
205 *
206 * manual
207 *
208 * When manual is selected, the user can manually adjust which power states
209 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
210 * and pp_dpm_pcie files and adjust the power state transition heuristics
211 * via the pp_power_profile_mode sysfs file.
212 *
213 * profile_standard
214 * profile_min_sclk
215 * profile_min_mclk
216 * profile_peak
217 *
218 * When the profiling modes are selected, clock and power gating are
219 * disabled and the clocks are set for different profiling cases. This
220 * mode is recommended for profiling specific work loads where you do
221 * not want clock or power gating for clock fluctuation to interfere
222 * with your results. profile_standard sets the clocks to a fixed clock
223 * level which varies from asic to asic. profile_min_sclk forces the sclk
224 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
225 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
226 *
227 */
228
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400229static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
Rex Zhu1b5708f2015-11-10 18:25:24 -0500230 struct device_attribute *attr,
231 char *buf)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232{
233 struct drm_device *ddev = dev_get_drvdata(dev);
234 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhucd4d7462017-09-06 18:43:52 +0800235 enum amd_dpm_forced_level level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236
Alex Deucher0c67df42016-02-19 15:30:15 -0500237 if ((adev->flags & AMD_IS_PX) &&
238 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
239 return snprintf(buf, PAGE_SIZE, "off\n");
240
Rex Zhucd4d7462017-09-06 18:43:52 +0800241 if (adev->powerplay.pp_funcs->get_performance_level)
242 level = amdgpu_dpm_get_performance_level(adev);
243 else
244 level = adev->pm.dpm.forced_level;
245
Rex Zhue5d03ac2016-12-23 14:39:41 +0800246 return snprintf(buf, PAGE_SIZE, "%s\n",
Rex Zhu570272d2017-01-06 13:32:49 +0800247 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
248 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
249 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
250 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
251 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
252 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
253 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
254 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
255 "unknown");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256}
257
258static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
259 struct device_attribute *attr,
260 const char *buf,
261 size_t count)
262{
263 struct drm_device *ddev = dev_get_drvdata(dev);
264 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhue5d03ac2016-12-23 14:39:41 +0800265 enum amd_dpm_forced_level level;
Rex Zhucd4d7462017-09-06 18:43:52 +0800266 enum amd_dpm_forced_level current_level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267 int ret = 0;
268
Alex Deucher0c67df42016-02-19 15:30:15 -0500269 /* Can't force performance level when the card is off */
270 if ((adev->flags & AMD_IS_PX) &&
271 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
272 return -EINVAL;
273
Rex Zhucd4d7462017-09-06 18:43:52 +0800274 if (adev->powerplay.pp_funcs->get_performance_level)
275 current_level = amdgpu_dpm_get_performance_level(adev);
Rex Zhu3bd58972016-12-23 15:24:37 +0800276
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400277 if (strncmp("low", buf, strlen("low")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800278 level = AMD_DPM_FORCED_LEVEL_LOW;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279 } else if (strncmp("high", buf, strlen("high")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800280 level = AMD_DPM_FORCED_LEVEL_HIGH;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800282 level = AMD_DPM_FORCED_LEVEL_AUTO;
Eric Huangf3898ea2015-12-11 16:24:34 -0500283 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800284 level = AMD_DPM_FORCED_LEVEL_MANUAL;
Rex Zhu570272d2017-01-06 13:32:49 +0800285 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
286 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
287 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
288 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
289 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
290 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
291 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
292 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
293 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
294 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
295 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296 count = -EINVAL;
297 goto fail;
298 }
Rex Zhu1b5708f2015-11-10 18:25:24 -0500299
Rex Zhu3bd58972016-12-23 15:24:37 +0800300 if (current_level == level)
Rex Zhu8e7afd32017-01-09 15:18:01 +0800301 return count;
Rex Zhu3bd58972016-12-23 15:24:37 +0800302
Rex Zhucd4d7462017-09-06 18:43:52 +0800303 if (adev->powerplay.pp_funcs->force_performance_level) {
Rex Zhu1b5708f2015-11-10 18:25:24 -0500304 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 if (adev->pm.dpm.thermal_active) {
306 count = -EINVAL;
Alex Deucher10f950f2016-02-19 15:18:45 -0500307 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308 goto fail;
309 }
310 ret = amdgpu_dpm_force_performance_level(adev, level);
311 if (ret)
312 count = -EINVAL;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500313 else
314 adev->pm.dpm.forced_level = level;
315 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316 }
Rex Zhu570272d2017-01-06 13:32:49 +0800317
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400318fail:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319 return count;
320}
321
Eric Huangf3898ea2015-12-11 16:24:34 -0500322static ssize_t amdgpu_get_pp_num_states(struct device *dev,
323 struct device_attribute *attr,
324 char *buf)
325{
326 struct drm_device *ddev = dev_get_drvdata(dev);
327 struct amdgpu_device *adev = ddev->dev_private;
328 struct pp_states_info data;
329 int i, buf_len;
330
Rex Zhucd4d7462017-09-06 18:43:52 +0800331 if (adev->powerplay.pp_funcs->get_pp_num_states)
Eric Huangf3898ea2015-12-11 16:24:34 -0500332 amdgpu_dpm_get_pp_num_states(adev, &data);
333
334 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
335 for (i = 0; i < data.nums; i++)
336 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
337 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
338 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
339 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
340 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
341
342 return buf_len;
343}
344
345static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
346 struct device_attribute *attr,
347 char *buf)
348{
349 struct drm_device *ddev = dev_get_drvdata(dev);
350 struct amdgpu_device *adev = ddev->dev_private;
351 struct pp_states_info data;
352 enum amd_pm_state_type pm = 0;
353 int i = 0;
354
Rex Zhucd4d7462017-09-06 18:43:52 +0800355 if (adev->powerplay.pp_funcs->get_current_power_state
356 && adev->powerplay.pp_funcs->get_pp_num_states) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500357 pm = amdgpu_dpm_get_current_power_state(adev);
358 amdgpu_dpm_get_pp_num_states(adev, &data);
359
360 for (i = 0; i < data.nums; i++) {
361 if (pm == data.states[i])
362 break;
363 }
364
365 if (i == data.nums)
366 i = -EINVAL;
367 }
368
369 return snprintf(buf, PAGE_SIZE, "%d\n", i);
370}
371
372static ssize_t amdgpu_get_pp_force_state(struct device *dev,
373 struct device_attribute *attr,
374 char *buf)
375{
376 struct drm_device *ddev = dev_get_drvdata(dev);
377 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500378
Rex Zhucd4d7462017-09-06 18:43:52 +0800379 if (adev->pp_force_state_enabled)
380 return amdgpu_get_pp_cur_state(dev, attr, buf);
381 else
Eric Huangf3898ea2015-12-11 16:24:34 -0500382 return snprintf(buf, PAGE_SIZE, "\n");
383}
384
385static ssize_t amdgpu_set_pp_force_state(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf,
388 size_t count)
389{
390 struct drm_device *ddev = dev_get_drvdata(dev);
391 struct amdgpu_device *adev = ddev->dev_private;
392 enum amd_pm_state_type state = 0;
Dan Carpenter041bf022016-06-16 11:30:23 +0300393 unsigned long idx;
Eric Huangf3898ea2015-12-11 16:24:34 -0500394 int ret;
395
396 if (strlen(buf) == 1)
397 adev->pp_force_state_enabled = false;
Rex Zhu6d07fe72017-09-25 18:51:50 +0800398 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
399 adev->powerplay.pp_funcs->get_pp_num_states) {
Dan Carpenter041bf022016-06-16 11:30:23 +0300400 struct pp_states_info data;
Eric Huangf3898ea2015-12-11 16:24:34 -0500401
Dan Carpenter041bf022016-06-16 11:30:23 +0300402 ret = kstrtoul(buf, 0, &idx);
403 if (ret || idx >= ARRAY_SIZE(data.states)) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500404 count = -EINVAL;
405 goto fail;
406 }
Gustavo A. R. Silvaddf74e72018-07-23 11:32:32 -0500407 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
Eric Huangf3898ea2015-12-11 16:24:34 -0500408
Dan Carpenter041bf022016-06-16 11:30:23 +0300409 amdgpu_dpm_get_pp_num_states(adev, &data);
410 state = data.states[idx];
411 /* only set user selected power states */
412 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
413 state != POWER_STATE_TYPE_DEFAULT) {
414 amdgpu_dpm_dispatch_task(adev,
Evan Quan39199b82017-12-29 14:46:13 +0800415 AMD_PP_TASK_ENABLE_USER_STATE, &state);
Dan Carpenter041bf022016-06-16 11:30:23 +0300416 adev->pp_force_state_enabled = true;
Eric Huangf3898ea2015-12-11 16:24:34 -0500417 }
418 }
419fail:
420 return count;
421}
422
Alex Deucherd54bb402018-04-19 14:02:52 -0500423/**
424 * DOC: pp_table
425 *
426 * The amdgpu driver provides a sysfs API for uploading new powerplay
427 * tables. The file pp_table is used for this. Reading the file
428 * will dump the current power play table. Writing to the file
429 * will attempt to upload a new powerplay table and re-initialize
430 * powerplay using that new table.
431 *
432 */
433
Eric Huangf3898ea2015-12-11 16:24:34 -0500434static ssize_t amdgpu_get_pp_table(struct device *dev,
435 struct device_attribute *attr,
436 char *buf)
437{
438 struct drm_device *ddev = dev_get_drvdata(dev);
439 struct amdgpu_device *adev = ddev->dev_private;
440 char *table = NULL;
Eric Huang1684d3b2016-07-28 17:25:01 -0400441 int size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500442
Rex Zhucd4d7462017-09-06 18:43:52 +0800443 if (adev->powerplay.pp_funcs->get_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500444 size = amdgpu_dpm_get_pp_table(adev, &table);
445 else
446 return 0;
447
448 if (size >= PAGE_SIZE)
449 size = PAGE_SIZE - 1;
450
Eric Huang1684d3b2016-07-28 17:25:01 -0400451 memcpy(buf, table, size);
Eric Huangf3898ea2015-12-11 16:24:34 -0500452
453 return size;
454}
455
456static ssize_t amdgpu_set_pp_table(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf,
459 size_t count)
460{
461 struct drm_device *ddev = dev_get_drvdata(dev);
462 struct amdgpu_device *adev = ddev->dev_private;
463
Rex Zhucd4d7462017-09-06 18:43:52 +0800464 if (adev->powerplay.pp_funcs->set_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500465 amdgpu_dpm_set_pp_table(adev, buf, count);
466
467 return count;
468}
469
Alex Deucher4e418c32018-04-19 14:59:55 -0500470/**
471 * DOC: pp_od_clk_voltage
472 *
473 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
474 * in each power level within a power state. The pp_od_clk_voltage is used for
475 * this.
476 *
Evan Quand5bf2652018-08-29 14:38:50 +0800477 * < For Vega10 and previous ASICs >
478 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500479 * Reading the file will display:
Alex Deucherdc85db22018-06-01 12:28:14 -0500480 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500481 * - a list of engine clock levels and voltages labeled OD_SCLK
Alex Deucherdc85db22018-06-01 12:28:14 -0500482 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500483 * - a list of memory clock levels and voltages labeled OD_MCLK
Alex Deucherdc85db22018-06-01 12:28:14 -0500484 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500485 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
486 *
487 * To manually adjust these settings, first select manual using
488 * power_dpm_force_performance_level. Enter a new value for each
489 * level by writing a string that contains "s/m level clock voltage" to
490 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
491 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
492 * 810 mV. When you have edited all of the states as needed, write
493 * "c" (commit) to the file to commit your changes. If you want to reset to the
494 * default power levels, write "r" (reset) to the file to reset them.
495 *
Evan Quand5bf2652018-08-29 14:38:50 +0800496 *
497 * < For Vega20 >
498 *
499 * Reading the file will display:
500 *
501 * - minimum and maximum engine clock labeled OD_SCLK
502 *
503 * - maximum memory clock labeled OD_MCLK
504 *
Evan Quanb1f82cb2018-09-13 16:14:33 +0800505 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
Evan Quand5bf2652018-08-29 14:38:50 +0800506 * They can be used to calibrate the sclk voltage curve.
507 *
508 * - a list of valid ranges for sclk, mclk, and voltage curve points
509 * labeled OD_RANGE
510 *
511 * To manually adjust these settings:
512 *
513 * - First select manual using power_dpm_force_performance_level
514 *
515 * - For clock frequency setting, enter a new value by writing a
516 * string that contains "s/m index clock" to the file. The index
517 * should be 0 if to set minimum clock. And 1 if to set maximum
518 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
519 * "m 1 800" will update maximum mclk to be 800Mhz.
520 *
521 * For sclk voltage curve, enter the new values by writing a
Evan Quanb1f82cb2018-09-13 16:14:33 +0800522 * string that contains "vc point clock voltage" to the file. The
523 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
524 * update point1 with clock set as 300Mhz and voltage as
525 * 600mV. "vc 2 1000 1000" will update point3 with clock set
526 * as 1000Mhz and voltage 1000mV.
Evan Quand5bf2652018-08-29 14:38:50 +0800527 *
528 * - When you have edited all of the states as needed, write "c" (commit)
529 * to the file to commit your changes
530 *
531 * - If you want to reset to the default power levels, write "r" (reset)
532 * to the file to reset them
533 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500534 */
535
Rex Zhue3933f22018-01-16 18:35:15 +0800536static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
537 struct device_attribute *attr,
538 const char *buf,
539 size_t count)
540{
541 struct drm_device *ddev = dev_get_drvdata(dev);
542 struct amdgpu_device *adev = ddev->dev_private;
543 int ret;
544 uint32_t parameter_size = 0;
545 long parameter[64];
546 char buf_cpy[128];
547 char *tmp_str;
548 char *sub_str;
549 const char delimiter[3] = {' ', '\n', '\0'};
550 uint32_t type;
551
552 if (count > 127)
553 return -EINVAL;
554
555 if (*buf == 's')
556 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
557 else if (*buf == 'm')
558 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
559 else if(*buf == 'r')
560 type = PP_OD_RESTORE_DEFAULT_TABLE;
561 else if (*buf == 'c')
562 type = PP_OD_COMMIT_DPM_TABLE;
Evan Quand5bf2652018-08-29 14:38:50 +0800563 else if (!strncmp(buf, "vc", 2))
564 type = PP_OD_EDIT_VDDC_CURVE;
Rex Zhue3933f22018-01-16 18:35:15 +0800565 else
566 return -EINVAL;
567
568 memcpy(buf_cpy, buf, count+1);
569
570 tmp_str = buf_cpy;
571
Evan Quand5bf2652018-08-29 14:38:50 +0800572 if (type == PP_OD_EDIT_VDDC_CURVE)
573 tmp_str++;
Rex Zhue3933f22018-01-16 18:35:15 +0800574 while (isspace(*++tmp_str));
575
576 while (tmp_str[0]) {
577 sub_str = strsep(&tmp_str, delimiter);
578 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
579 if (ret)
580 return -EINVAL;
581 parameter_size++;
582
583 while (isspace(*tmp_str))
584 tmp_str++;
585 }
586
587 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
588 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
589 parameter, parameter_size);
590
591 if (ret)
592 return -EINVAL;
593
594 if (type == PP_OD_COMMIT_DPM_TABLE) {
595 if (adev->powerplay.pp_funcs->dispatch_tasks) {
596 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
597 return count;
598 } else {
599 return -EINVAL;
600 }
601 }
602
603 return count;
604}
605
606static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
607 struct device_attribute *attr,
608 char *buf)
609{
610 struct drm_device *ddev = dev_get_drvdata(dev);
611 struct amdgpu_device *adev = ddev->dev_private;
612 uint32_t size = 0;
613
614 if (adev->powerplay.pp_funcs->print_clock_levels) {
615 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
616 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
Evan Quand5bf2652018-08-29 14:38:50 +0800617 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
Rex Zhua3c991f2018-04-19 10:39:17 +0800618 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
Rex Zhue3933f22018-01-16 18:35:15 +0800619 return size;
620 } else {
621 return snprintf(buf, PAGE_SIZE, "\n");
622 }
623
624}
625
Alex Deucher271dc902018-04-19 14:22:24 -0500626/**
627 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_pcie
628 *
629 * The amdgpu driver provides a sysfs API for adjusting what power levels
630 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
631 * and pp_dpm_pcie are used for this.
632 *
633 * Reading back the files will show you the available power levels within
634 * the power state and the clock information for those levels.
635 *
636 * To manually adjust these states, first select manual using
welu48edde32018-04-24 09:13:20 -0400637 * power_dpm_force_performance_level.
638 * Secondly,Enter a new value for each level by inputing a string that
639 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
640 * E.g., echo 4 5 6 to > pp_dpm_sclk will enable sclk levels 4, 5, and 6.
Alex Deucher271dc902018-04-19 14:22:24 -0500641 */
642
Eric Huangf3898ea2015-12-11 16:24:34 -0500643static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
644 struct device_attribute *attr,
645 char *buf)
646{
647 struct drm_device *ddev = dev_get_drvdata(dev);
648 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500649
Rex Zhucd4d7462017-09-06 18:43:52 +0800650 if (adev->powerplay.pp_funcs->print_clock_levels)
651 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
652 else
653 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500654}
655
Kees Cook4b4bd042018-06-20 11:26:47 -0700656/*
657 * Worst case: 32 bits individually specified, in octal at 12 characters
658 * per line (+1 for \n).
659 */
660#define AMDGPU_MASK_BUF_MAX (32 * 13)
661
662static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
663{
664 int ret;
665 long level;
666 char *sub_str = NULL;
667 char *tmp;
668 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
669 const char delimiter[3] = {' ', '\n', '\0'};
670 size_t bytes;
671
672 *mask = 0;
673
674 bytes = min(count, sizeof(buf_cpy) - 1);
675 memcpy(buf_cpy, buf, bytes);
676 buf_cpy[bytes] = '\0';
677 tmp = buf_cpy;
678 while (tmp[0]) {
679 sub_str = strsep(&tmp, delimiter);
680 if (strlen(sub_str)) {
681 ret = kstrtol(sub_str, 0, &level);
682 if (ret)
683 return -EINVAL;
684 *mask |= 1 << level;
685 } else
686 break;
687 }
688
689 return 0;
690}
691
Eric Huangf3898ea2015-12-11 16:24:34 -0500692static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
693 struct device_attribute *attr,
694 const char *buf,
695 size_t count)
696{
697 struct drm_device *ddev = dev_get_drvdata(dev);
698 struct amdgpu_device *adev = ddev->dev_private;
699 int ret;
welu48edde32018-04-24 09:13:20 -0400700 uint32_t mask = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500701
Kees Cook4b4bd042018-06-20 11:26:47 -0700702 ret = amdgpu_read_mask(buf, count, &mask);
703 if (ret)
704 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500705
Rex Zhucd4d7462017-09-06 18:43:52 +0800706 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400707 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800708
Eric Huangf3898ea2015-12-11 16:24:34 -0500709 return count;
710}
711
712static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
713 struct device_attribute *attr,
714 char *buf)
715{
716 struct drm_device *ddev = dev_get_drvdata(dev);
717 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500718
Rex Zhucd4d7462017-09-06 18:43:52 +0800719 if (adev->powerplay.pp_funcs->print_clock_levels)
720 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
721 else
722 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500723}
724
725static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
726 struct device_attribute *attr,
727 const char *buf,
728 size_t count)
729{
730 struct drm_device *ddev = dev_get_drvdata(dev);
731 struct amdgpu_device *adev = ddev->dev_private;
732 int ret;
welu48edde32018-04-24 09:13:20 -0400733 uint32_t mask = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500734
Kees Cook4b4bd042018-06-20 11:26:47 -0700735 ret = amdgpu_read_mask(buf, count, &mask);
736 if (ret)
737 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500738
Rex Zhucd4d7462017-09-06 18:43:52 +0800739 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400740 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800741
Eric Huangf3898ea2015-12-11 16:24:34 -0500742 return count;
743}
744
745static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
746 struct device_attribute *attr,
747 char *buf)
748{
749 struct drm_device *ddev = dev_get_drvdata(dev);
750 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500751
Rex Zhucd4d7462017-09-06 18:43:52 +0800752 if (adev->powerplay.pp_funcs->print_clock_levels)
753 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
754 else
755 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500756}
757
758static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
759 struct device_attribute *attr,
760 const char *buf,
761 size_t count)
762{
763 struct drm_device *ddev = dev_get_drvdata(dev);
764 struct amdgpu_device *adev = ddev->dev_private;
765 int ret;
welu48edde32018-04-24 09:13:20 -0400766 uint32_t mask = 0;
Eric Huangf3898ea2015-12-11 16:24:34 -0500767
Kees Cook4b4bd042018-06-20 11:26:47 -0700768 ret = amdgpu_read_mask(buf, count, &mask);
769 if (ret)
770 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -0500771
Rex Zhucd4d7462017-09-06 18:43:52 +0800772 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400773 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800774
Eric Huangf3898ea2015-12-11 16:24:34 -0500775 return count;
776}
777
Eric Huang428bafa2016-05-12 14:51:21 -0400778static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
779 struct device_attribute *attr,
780 char *buf)
781{
782 struct drm_device *ddev = dev_get_drvdata(dev);
783 struct amdgpu_device *adev = ddev->dev_private;
784 uint32_t value = 0;
785
Rex Zhucd4d7462017-09-06 18:43:52 +0800786 if (adev->powerplay.pp_funcs->get_sclk_od)
Eric Huang428bafa2016-05-12 14:51:21 -0400787 value = amdgpu_dpm_get_sclk_od(adev);
788
789 return snprintf(buf, PAGE_SIZE, "%d\n", value);
790}
791
792static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
793 struct device_attribute *attr,
794 const char *buf,
795 size_t count)
796{
797 struct drm_device *ddev = dev_get_drvdata(dev);
798 struct amdgpu_device *adev = ddev->dev_private;
799 int ret;
800 long int value;
801
802 ret = kstrtol(buf, 0, &value);
803
804 if (ret) {
805 count = -EINVAL;
806 goto fail;
807 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800808 if (adev->powerplay.pp_funcs->set_sclk_od)
809 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
Eric Huang428bafa2016-05-12 14:51:21 -0400810
Rex Zhu6d07fe72017-09-25 18:51:50 +0800811 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800812 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800813 } else {
Eric Huang8b2e5742016-05-19 15:46:10 -0400814 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
815 amdgpu_pm_compute_clocks(adev);
816 }
Eric Huang428bafa2016-05-12 14:51:21 -0400817
818fail:
819 return count;
820}
821
Eric Huangf2bdc052016-05-24 15:11:17 -0400822static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
823 struct device_attribute *attr,
824 char *buf)
825{
826 struct drm_device *ddev = dev_get_drvdata(dev);
827 struct amdgpu_device *adev = ddev->dev_private;
828 uint32_t value = 0;
829
Rex Zhucd4d7462017-09-06 18:43:52 +0800830 if (adev->powerplay.pp_funcs->get_mclk_od)
Eric Huangf2bdc052016-05-24 15:11:17 -0400831 value = amdgpu_dpm_get_mclk_od(adev);
Eric Huangf2bdc052016-05-24 15:11:17 -0400832
833 return snprintf(buf, PAGE_SIZE, "%d\n", value);
834}
835
836static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf,
839 size_t count)
840{
841 struct drm_device *ddev = dev_get_drvdata(dev);
842 struct amdgpu_device *adev = ddev->dev_private;
843 int ret;
844 long int value;
845
846 ret = kstrtol(buf, 0, &value);
847
848 if (ret) {
849 count = -EINVAL;
850 goto fail;
851 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800852 if (adev->powerplay.pp_funcs->set_mclk_od)
853 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
Eric Huangf2bdc052016-05-24 15:11:17 -0400854
Rex Zhu6d07fe72017-09-25 18:51:50 +0800855 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800856 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800857 } else {
Eric Huangf2bdc052016-05-24 15:11:17 -0400858 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
859 amdgpu_pm_compute_clocks(adev);
860 }
861
862fail:
863 return count;
864}
865
Alex Deucher6b2576f2018-04-19 14:38:31 -0500866/**
867 * DOC: pp_power_profile_mode
868 *
869 * The amdgpu driver provides a sysfs API for adjusting the heuristics
870 * related to switching between power levels in a power state. The file
871 * pp_power_profile_mode is used for this.
872 *
873 * Reading this file outputs a list of all of the predefined power profiles
874 * and the relevant heuristics settings for that profile.
875 *
876 * To select a profile or create a custom profile, first select manual using
877 * power_dpm_force_performance_level. Writing the number of a predefined
878 * profile to pp_power_profile_mode will enable those heuristics. To
879 * create a custom set of heuristics, write a string of numbers to the file
880 * starting with the number of the custom profile along with a setting
881 * for each heuristic parameter. Due to differences across asic families
882 * the heuristic parameters vary from family to family.
883 *
884 */
885
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800886static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
887 struct device_attribute *attr,
888 char *buf)
889{
890 struct drm_device *ddev = dev_get_drvdata(dev);
891 struct amdgpu_device *adev = ddev->dev_private;
892
893 if (adev->powerplay.pp_funcs->get_power_profile_mode)
894 return amdgpu_dpm_get_power_profile_mode(adev, buf);
895
896 return snprintf(buf, PAGE_SIZE, "\n");
897}
898
899
900static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
901 struct device_attribute *attr,
902 const char *buf,
903 size_t count)
904{
905 int ret = 0xff;
906 struct drm_device *ddev = dev_get_drvdata(dev);
907 struct amdgpu_device *adev = ddev->dev_private;
908 uint32_t parameter_size = 0;
909 long parameter[64];
910 char *sub_str, buf_cpy[128];
911 char *tmp_str;
912 uint32_t i = 0;
913 char tmp[2];
914 long int profile_mode = 0;
915 const char delimiter[3] = {' ', '\n', '\0'};
916
917 tmp[0] = *(buf);
918 tmp[1] = '\0';
919 ret = kstrtol(tmp, 0, &profile_mode);
920 if (ret)
921 goto fail;
922
923 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
924 if (count < 2 || count > 127)
925 return -EINVAL;
926 while (isspace(*++buf))
927 i++;
928 memcpy(buf_cpy, buf, count-i);
929 tmp_str = buf_cpy;
930 while (tmp_str[0]) {
931 sub_str = strsep(&tmp_str, delimiter);
932 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
933 if (ret) {
934 count = -EINVAL;
935 goto fail;
936 }
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800937 parameter_size++;
938 while (isspace(*tmp_str))
939 tmp_str++;
940 }
941 }
942 parameter[parameter_size] = profile_mode;
943 if (adev->powerplay.pp_funcs->set_power_profile_mode)
944 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
945
946 if (!ret)
947 return count;
948fail:
949 return -EINVAL;
950}
951
Tom St Denisb374d822018-06-20 07:55:39 -0400952/**
953 * DOC: busy_percent
954 *
955 * The amdgpu driver provides a sysfs API for reading how busy the GPU
956 * is as a percentage. The file gpu_busy_percent is used for this.
957 * The SMU firmware computes a percentage of load based on the
958 * aggregate activity level in the IP cores.
959 */
960static ssize_t amdgpu_get_busy_percent(struct device *dev,
961 struct device_attribute *attr,
962 char *buf)
963{
964 struct drm_device *ddev = dev_get_drvdata(dev);
965 struct amdgpu_device *adev = ddev->dev_private;
966 int r, value, size = sizeof(value);
967
968 /* sanity check PP is enabled */
969 if (!(adev->powerplay.pp_funcs &&
970 adev->powerplay.pp_funcs->read_sensor))
971 return -EINVAL;
972
973 /* read the IP busy sensor */
974 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
975 (void *)&value, &size);
976 if (r)
977 return r;
978
979 return snprintf(buf, PAGE_SIZE, "%d\n", value);
980}
981
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
983static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
984 amdgpu_get_dpm_forced_performance_level,
985 amdgpu_set_dpm_forced_performance_level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500986static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
987static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
988static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
989 amdgpu_get_pp_force_state,
990 amdgpu_set_pp_force_state);
991static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
992 amdgpu_get_pp_table,
993 amdgpu_set_pp_table);
994static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
995 amdgpu_get_pp_dpm_sclk,
996 amdgpu_set_pp_dpm_sclk);
997static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
998 amdgpu_get_pp_dpm_mclk,
999 amdgpu_set_pp_dpm_mclk);
1000static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
1001 amdgpu_get_pp_dpm_pcie,
1002 amdgpu_set_pp_dpm_pcie);
Eric Huang428bafa2016-05-12 14:51:21 -04001003static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
1004 amdgpu_get_pp_sclk_od,
1005 amdgpu_set_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -04001006static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
1007 amdgpu_get_pp_mclk_od,
1008 amdgpu_set_pp_mclk_od);
Rex Zhu37c5c4d2018-01-10 18:42:36 +08001009static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
1010 amdgpu_get_pp_power_profile_mode,
1011 amdgpu_set_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +08001012static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
1013 amdgpu_get_pp_od_clk_voltage,
1014 amdgpu_set_pp_od_clk_voltage);
Tom St Denisb374d822018-06-20 07:55:39 -04001015static DEVICE_ATTR(gpu_busy_percent, S_IRUGO,
1016 amdgpu_get_busy_percent, NULL);
Rex Zhue3933f22018-01-16 18:35:15 +08001017
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1019 struct device_attribute *attr,
1020 char *buf)
1021{
1022 struct amdgpu_device *adev = dev_get_drvdata(dev);
Alex Deucher0c67df42016-02-19 15:30:15 -05001023 struct drm_device *ddev = adev->ddev;
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001024 int r, temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025
Alex Deucher0c67df42016-02-19 15:30:15 -05001026 /* Can't get temperature when the card is off */
1027 if ((adev->flags & AMD_IS_PX) &&
1028 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1029 return -EINVAL;
1030
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001031 /* sanity check PP is enabled */
1032 if (!(adev->powerplay.pp_funcs &&
1033 adev->powerplay.pp_funcs->read_sensor))
1034 return -EINVAL;
1035
1036 /* get the temperature */
1037 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1038 (void *)&temp, &size);
1039 if (r)
1040 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041
1042 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1043}
1044
1045static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1046 struct device_attribute *attr,
1047 char *buf)
1048{
1049 struct amdgpu_device *adev = dev_get_drvdata(dev);
1050 int hyst = to_sensor_dev_attr(attr)->index;
1051 int temp;
1052
1053 if (hyst)
1054 temp = adev->pm.dpm.thermal.min_temp;
1055 else
1056 temp = adev->pm.dpm.thermal.max_temp;
1057
1058 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1059}
1060
1061static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1062 struct device_attribute *attr,
1063 char *buf)
1064{
1065 struct amdgpu_device *adev = dev_get_drvdata(dev);
1066 u32 pwm_mode = 0;
1067
Rex Zhucd4d7462017-09-06 18:43:52 +08001068 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
Rex Zhu8804b8d2015-11-10 18:29:11 -05001069 return -EINVAL;
1070
1071 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072
Rex Zhuaad22ca2017-05-05 16:56:45 +08001073 return sprintf(buf, "%i\n", pwm_mode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074}
1075
1076static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1077 struct device_attribute *attr,
1078 const char *buf,
1079 size_t count)
1080{
1081 struct amdgpu_device *adev = dev_get_drvdata(dev);
1082 int err;
1083 int value;
1084
Alex Deucher5ec36e22018-01-24 16:41:50 -05001085 /* Can't adjust fan when the card is off */
1086 if ((adev->flags & AMD_IS_PX) &&
1087 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1088 return -EINVAL;
1089
Rex Zhucd4d7462017-09-06 18:43:52 +08001090 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 return -EINVAL;
1092
1093 err = kstrtoint(buf, 10, &value);
1094 if (err)
1095 return err;
1096
Rex Zhuaad22ca2017-05-05 16:56:45 +08001097 amdgpu_dpm_set_fan_control_mode(adev, value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098
1099 return count;
1100}
1101
1102static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1103 struct device_attribute *attr,
1104 char *buf)
1105{
1106 return sprintf(buf, "%i\n", 0);
1107}
1108
1109static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1110 struct device_attribute *attr,
1111 char *buf)
1112{
1113 return sprintf(buf, "%i\n", 255);
1114}
1115
1116static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1117 struct device_attribute *attr,
1118 const char *buf, size_t count)
1119{
1120 struct amdgpu_device *adev = dev_get_drvdata(dev);
1121 int err;
1122 u32 value;
Rex Zhub8a9c002018-09-28 16:01:48 +08001123 u32 pwm_mode;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124
Alex Deucher5ec36e22018-01-24 16:41:50 -05001125 /* Can't adjust fan when the card is off */
1126 if ((adev->flags & AMD_IS_PX) &&
1127 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1128 return -EINVAL;
1129
Rex Zhub8a9c002018-09-28 16:01:48 +08001130 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1131 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
1132 pr_info("manual fan speed control should be enabled first\n");
1133 return -EINVAL;
1134 }
1135
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 err = kstrtou32(buf, 10, &value);
1137 if (err)
1138 return err;
1139
1140 value = (value * 100) / 255;
1141
Rex Zhucd4d7462017-09-06 18:43:52 +08001142 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1143 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1144 if (err)
1145 return err;
1146 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001147
1148 return count;
1149}
1150
1151static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1152 struct device_attribute *attr,
1153 char *buf)
1154{
1155 struct amdgpu_device *adev = dev_get_drvdata(dev);
1156 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001157 u32 speed = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158
Alex Deucher5ec36e22018-01-24 16:41:50 -05001159 /* Can't adjust fan when the card is off */
1160 if ((adev->flags & AMD_IS_PX) &&
1161 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1162 return -EINVAL;
1163
Rex Zhucd4d7462017-09-06 18:43:52 +08001164 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1165 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1166 if (err)
1167 return err;
1168 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169
1170 speed = (speed * 255) / 100;
1171
1172 return sprintf(buf, "%i\n", speed);
1173}
1174
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001175static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1176 struct device_attribute *attr,
1177 char *buf)
1178{
1179 struct amdgpu_device *adev = dev_get_drvdata(dev);
1180 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001181 u32 speed = 0;
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001182
Alex Deucher5ec36e22018-01-24 16:41:50 -05001183 /* Can't adjust fan when the card is off */
1184 if ((adev->flags & AMD_IS_PX) &&
1185 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1186 return -EINVAL;
1187
Rex Zhucd4d7462017-09-06 18:43:52 +08001188 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1189 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1190 if (err)
1191 return err;
1192 }
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001193
1194 return sprintf(buf, "%i\n", speed);
1195}
1196
Rex Zhuc2870522018-09-20 14:30:55 +08001197static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
1198 struct device_attribute *attr,
1199 char *buf)
1200{
1201 struct amdgpu_device *adev = dev_get_drvdata(dev);
1202 u32 min_rpm = 0;
1203 u32 size = sizeof(min_rpm);
1204 int r;
1205
1206 if (!adev->powerplay.pp_funcs->read_sensor)
1207 return -EINVAL;
1208
1209 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
1210 (void *)&min_rpm, &size);
1211 if (r)
1212 return r;
1213
1214 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
1215}
1216
1217static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
1218 struct device_attribute *attr,
1219 char *buf)
1220{
1221 struct amdgpu_device *adev = dev_get_drvdata(dev);
1222 u32 max_rpm = 0;
1223 u32 size = sizeof(max_rpm);
1224 int r;
1225
1226 if (!adev->powerplay.pp_funcs->read_sensor)
1227 return -EINVAL;
1228
1229 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
1230 (void *)&max_rpm, &size);
1231 if (r)
1232 return r;
1233
1234 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
1235}
1236
1237static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
1238 struct device_attribute *attr,
1239 char *buf)
1240{
1241 struct amdgpu_device *adev = dev_get_drvdata(dev);
1242 int err;
1243 u32 rpm = 0;
Rex Zhuc2870522018-09-20 14:30:55 +08001244
1245 /* Can't adjust fan when the card is off */
1246 if ((adev->flags & AMD_IS_PX) &&
1247 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1248 return -EINVAL;
1249
1250 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1251 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
1252 if (err)
1253 return err;
1254 }
1255
1256 return sprintf(buf, "%i\n", rpm);
1257}
1258
1259static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
1260 struct device_attribute *attr,
1261 const char *buf, size_t count)
1262{
1263 struct amdgpu_device *adev = dev_get_drvdata(dev);
1264 int err;
1265 u32 value;
1266 u32 pwm_mode;
1267
1268 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1269 if (pwm_mode != AMD_FAN_CTRL_MANUAL)
1270 return -ENODATA;
1271
1272 /* Can't adjust fan when the card is off */
1273 if ((adev->flags & AMD_IS_PX) &&
1274 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1275 return -EINVAL;
1276
1277 err = kstrtou32(buf, 10, &value);
1278 if (err)
1279 return err;
1280
1281 if (adev->powerplay.pp_funcs->set_fan_speed_rpm) {
1282 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
1283 if (err)
1284 return err;
1285 }
1286
1287 return count;
1288}
1289
1290static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
1291 struct device_attribute *attr,
1292 char *buf)
1293{
1294 struct amdgpu_device *adev = dev_get_drvdata(dev);
1295 u32 pwm_mode = 0;
1296
1297 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1298 return -EINVAL;
1299
1300 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1301
1302 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
1303}
1304
1305static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
1306 struct device_attribute *attr,
1307 const char *buf,
1308 size_t count)
1309{
1310 struct amdgpu_device *adev = dev_get_drvdata(dev);
1311 int err;
1312 int value;
1313 u32 pwm_mode;
1314
1315 /* Can't adjust fan when the card is off */
1316 if ((adev->flags & AMD_IS_PX) &&
1317 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1318 return -EINVAL;
1319
1320 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1321 return -EINVAL;
1322
1323 err = kstrtoint(buf, 10, &value);
1324 if (err)
1325 return err;
1326
1327 if (value == 0)
1328 pwm_mode = AMD_FAN_CTRL_AUTO;
1329 else if (value == 1)
1330 pwm_mode = AMD_FAN_CTRL_MANUAL;
1331 else
1332 return -EINVAL;
1333
1334 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
1335
1336 return count;
1337}
1338
Alex Deucher2bd376b2018-01-24 17:19:33 -05001339static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1340 struct device_attribute *attr,
1341 char *buf)
1342{
1343 struct amdgpu_device *adev = dev_get_drvdata(dev);
1344 struct drm_device *ddev = adev->ddev;
1345 u32 vddgfx;
1346 int r, size = sizeof(vddgfx);
1347
1348 /* Can't get voltage when the card is off */
1349 if ((adev->flags & AMD_IS_PX) &&
1350 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1351 return -EINVAL;
1352
1353 /* sanity check PP is enabled */
1354 if (!(adev->powerplay.pp_funcs &&
1355 adev->powerplay.pp_funcs->read_sensor))
1356 return -EINVAL;
1357
1358 /* get the voltage */
1359 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1360 (void *)&vddgfx, &size);
1361 if (r)
1362 return r;
1363
1364 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1365}
1366
1367static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1368 struct device_attribute *attr,
1369 char *buf)
1370{
1371 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1372}
1373
1374static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1375 struct device_attribute *attr,
1376 char *buf)
1377{
1378 struct amdgpu_device *adev = dev_get_drvdata(dev);
1379 struct drm_device *ddev = adev->ddev;
1380 u32 vddnb;
1381 int r, size = sizeof(vddnb);
1382
1383 /* only APUs have vddnb */
Rex Zhuccf9ef02018-07-25 11:51:46 +08001384 if (!(adev->flags & AMD_IS_APU))
Alex Deucher2bd376b2018-01-24 17:19:33 -05001385 return -EINVAL;
1386
1387 /* Can't get voltage when the card is off */
1388 if ((adev->flags & AMD_IS_PX) &&
1389 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1390 return -EINVAL;
1391
1392 /* sanity check PP is enabled */
1393 if (!(adev->powerplay.pp_funcs &&
1394 adev->powerplay.pp_funcs->read_sensor))
1395 return -EINVAL;
1396
1397 /* get the voltage */
1398 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1399 (void *)&vddnb, &size);
1400 if (r)
1401 return r;
1402
1403 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1404}
1405
1406static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1407 struct device_attribute *attr,
1408 char *buf)
1409{
1410 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1411}
1412
Alex Deucher2976fc22018-01-24 18:34:26 -05001413static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1414 struct device_attribute *attr,
1415 char *buf)
1416{
1417 struct amdgpu_device *adev = dev_get_drvdata(dev);
1418 struct drm_device *ddev = adev->ddev;
Rex Zhu5b79d042018-04-04 15:37:35 +08001419 u32 query = 0;
1420 int r, size = sizeof(u32);
Alex Deucher2976fc22018-01-24 18:34:26 -05001421 unsigned uw;
1422
1423 /* Can't get power when the card is off */
1424 if ((adev->flags & AMD_IS_PX) &&
1425 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1426 return -EINVAL;
1427
1428 /* sanity check PP is enabled */
1429 if (!(adev->powerplay.pp_funcs &&
1430 adev->powerplay.pp_funcs->read_sensor))
1431 return -EINVAL;
1432
1433 /* get the voltage */
1434 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1435 (void *)&query, &size);
1436 if (r)
1437 return r;
1438
1439 /* convert to microwatts */
Rex Zhu5b79d042018-04-04 15:37:35 +08001440 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
Alex Deucher2976fc22018-01-24 18:34:26 -05001441
1442 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1443}
1444
Rex Zhu8d81bce2018-01-29 18:07:01 +08001445static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1446 struct device_attribute *attr,
1447 char *buf)
1448{
1449 return sprintf(buf, "%i\n", 0);
1450}
1451
1452static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1453 struct device_attribute *attr,
1454 char *buf)
1455{
1456 struct amdgpu_device *adev = dev_get_drvdata(dev);
1457 uint32_t limit = 0;
1458
1459 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1460 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1461 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1462 } else {
1463 return snprintf(buf, PAGE_SIZE, "\n");
1464 }
1465}
1466
1467static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1468 struct device_attribute *attr,
1469 char *buf)
1470{
1471 struct amdgpu_device *adev = dev_get_drvdata(dev);
1472 uint32_t limit = 0;
1473
1474 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1475 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1476 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1477 } else {
1478 return snprintf(buf, PAGE_SIZE, "\n");
1479 }
1480}
1481
1482
1483static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
1484 struct device_attribute *attr,
1485 const char *buf,
1486 size_t count)
1487{
1488 struct amdgpu_device *adev = dev_get_drvdata(dev);
1489 int err;
1490 u32 value;
1491
1492 err = kstrtou32(buf, 10, &value);
1493 if (err)
1494 return err;
1495
1496 value = value / 1000000; /* convert to Watt */
1497 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
1498 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
1499 if (err)
1500 return err;
1501 } else {
1502 return -EINVAL;
1503 }
1504
1505 return count;
1506}
1507
Alex Deucher844c5412018-03-26 12:56:56 -05001508
1509/**
1510 * DOC: hwmon
1511 *
1512 * The amdgpu driver exposes the following sensor interfaces:
Alex Deucherdc85db22018-06-01 12:28:14 -05001513 *
Alex Deucher844c5412018-03-26 12:56:56 -05001514 * - GPU temperature (via the on-die sensor)
Alex Deucherdc85db22018-06-01 12:28:14 -05001515 *
Alex Deucher844c5412018-03-26 12:56:56 -05001516 * - GPU voltage
Alex Deucherdc85db22018-06-01 12:28:14 -05001517 *
Alex Deucher844c5412018-03-26 12:56:56 -05001518 * - Northbridge voltage (APUs only)
Alex Deucherdc85db22018-06-01 12:28:14 -05001519 *
Alex Deucher844c5412018-03-26 12:56:56 -05001520 * - GPU power
Alex Deucherdc85db22018-06-01 12:28:14 -05001521 *
Alex Deucher844c5412018-03-26 12:56:56 -05001522 * - GPU fan
1523 *
1524 * hwmon interfaces for GPU temperature:
Alex Deucherdc85db22018-06-01 12:28:14 -05001525 *
Alex Deucher844c5412018-03-26 12:56:56 -05001526 * - temp1_input: the on die GPU temperature in millidegrees Celsius
Alex Deucherdc85db22018-06-01 12:28:14 -05001527 *
Alex Deucher844c5412018-03-26 12:56:56 -05001528 * - temp1_crit: temperature critical max value in millidegrees Celsius
Alex Deucherdc85db22018-06-01 12:28:14 -05001529 *
Alex Deucher844c5412018-03-26 12:56:56 -05001530 * - temp1_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
1531 *
1532 * hwmon interfaces for GPU voltage:
Alex Deucherdc85db22018-06-01 12:28:14 -05001533 *
Alex Deucher844c5412018-03-26 12:56:56 -05001534 * - in0_input: the voltage on the GPU in millivolts
Alex Deucherdc85db22018-06-01 12:28:14 -05001535 *
Alex Deucher844c5412018-03-26 12:56:56 -05001536 * - in1_input: the voltage on the Northbridge in millivolts
1537 *
1538 * hwmon interfaces for GPU power:
Alex Deucherdc85db22018-06-01 12:28:14 -05001539 *
Alex Deucher844c5412018-03-26 12:56:56 -05001540 * - power1_average: average power used by the GPU in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001541 *
Alex Deucher844c5412018-03-26 12:56:56 -05001542 * - power1_cap_min: minimum cap supported in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001543 *
Alex Deucher844c5412018-03-26 12:56:56 -05001544 * - power1_cap_max: maximum cap supported in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001545 *
Alex Deucher844c5412018-03-26 12:56:56 -05001546 * - power1_cap: selected power cap in microWatts
1547 *
1548 * hwmon interfaces for GPU fan:
Alex Deucherdc85db22018-06-01 12:28:14 -05001549 *
Alex Deucher844c5412018-03-26 12:56:56 -05001550 * - pwm1: pulse width modulation fan level (0-255)
Alex Deucherdc85db22018-06-01 12:28:14 -05001551 *
1552 * - 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)
1553 *
Alex Deucher844c5412018-03-26 12:56:56 -05001554 * - pwm1_min: pulse width modulation fan control minimum level (0)
Alex Deucherdc85db22018-06-01 12:28:14 -05001555 *
Alex Deucher844c5412018-03-26 12:56:56 -05001556 * - pwm1_max: pulse width modulation fan control maximum level (255)
Alex Deucherdc85db22018-06-01 12:28:14 -05001557 *
Rex Zhuc2870522018-09-20 14:30:55 +08001558 * - fan1_min: an minimum value Unit: revolution/min (RPM)
1559 *
1560 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
1561 *
Alex Deucher844c5412018-03-26 12:56:56 -05001562 * - fan1_input: fan speed in RPM
1563 *
Rex Zhuc2870522018-09-20 14:30:55 +08001564 * - fan[1-*]_target: Desired fan speed Unit: revolution/min (RPM)
1565 *
1566 * - fan[1-*]_enable: Enable or disable the sensors.1: Enable 0: Disable
1567 *
Alex Deucher844c5412018-03-26 12:56:56 -05001568 * You can use hwmon tools like sensors to view this information on your system.
1569 *
1570 */
1571
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
1573static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
1574static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
1575static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
1576static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
1577static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
1578static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001579static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
Rex Zhuc2870522018-09-20 14:30:55 +08001580static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
1581static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
1582static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
1583static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
Alex Deucher2bd376b2018-01-24 17:19:33 -05001584static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
1585static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
1586static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
1587static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
Alex Deucher2976fc22018-01-24 18:34:26 -05001588static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
Rex Zhu8d81bce2018-01-29 18:07:01 +08001589static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
1590static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
1591static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592
1593static struct attribute *hwmon_attributes[] = {
1594 &sensor_dev_attr_temp1_input.dev_attr.attr,
1595 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1596 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1597 &sensor_dev_attr_pwm1.dev_attr.attr,
1598 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1599 &sensor_dev_attr_pwm1_min.dev_attr.attr,
1600 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001601 &sensor_dev_attr_fan1_input.dev_attr.attr,
Rex Zhuc2870522018-09-20 14:30:55 +08001602 &sensor_dev_attr_fan1_min.dev_attr.attr,
1603 &sensor_dev_attr_fan1_max.dev_attr.attr,
1604 &sensor_dev_attr_fan1_target.dev_attr.attr,
1605 &sensor_dev_attr_fan1_enable.dev_attr.attr,
Alex Deucher2bd376b2018-01-24 17:19:33 -05001606 &sensor_dev_attr_in0_input.dev_attr.attr,
1607 &sensor_dev_attr_in0_label.dev_attr.attr,
1608 &sensor_dev_attr_in1_input.dev_attr.attr,
1609 &sensor_dev_attr_in1_label.dev_attr.attr,
Alex Deucher2976fc22018-01-24 18:34:26 -05001610 &sensor_dev_attr_power1_average.dev_attr.attr,
Rex Zhu8d81bce2018-01-29 18:07:01 +08001611 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
1612 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
1613 &sensor_dev_attr_power1_cap.dev_attr.attr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001614 NULL
1615};
1616
1617static umode_t hwmon_attributes_visible(struct kobject *kobj,
1618 struct attribute *attr, int index)
1619{
Geliang Tangcc29ec82016-01-13 22:48:42 +08001620 struct device *dev = kobj_to_dev(kobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 struct amdgpu_device *adev = dev_get_drvdata(dev);
1622 umode_t effective_mode = attr->mode;
1623
Rex Zhufc5a1362018-04-27 13:46:08 +08001624 /* Skip fan attributes if fan is not present */
1625 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1626 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1627 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1628 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
Rex Zhuc2870522018-09-20 14:30:55 +08001629 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
1630 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
1631 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
1632 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
1633 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
Rex Zhufc5a1362018-04-27 13:46:08 +08001634 return 0;
Alex Deucher135f9712017-11-20 17:49:53 -05001635
Rex Zhu1b5708f2015-11-10 18:25:24 -05001636 /* Skip limit attributes if DPM is not enabled */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 if (!adev->pm.dpm_enabled &&
1638 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
Alex Deucher27100732015-10-19 15:49:11 -04001639 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
1640 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1641 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1642 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
Rex Zhuc2870522018-09-20 14:30:55 +08001643 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
1644 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
1645 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
1646 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
1647 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
1648 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 return 0;
1650
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001651 /* mask fan attributes if we have no bindings for this asic to expose */
Rex Zhucd4d7462017-09-06 18:43:52 +08001652 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001654 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
1656 effective_mode &= ~S_IRUGO;
1657
Rex Zhucd4d7462017-09-06 18:43:52 +08001658 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001659 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001660 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
1662 effective_mode &= ~S_IWUSR;
1663
Rex Zhu8d81bce2018-01-29 18:07:01 +08001664 if ((adev->flags & AMD_IS_APU) &&
1665 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
1666 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
1667 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
1668 return 0;
1669
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 /* hide max/min values if we can't both query and manage the fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001671 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1672 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
Rex Zhuc2870522018-09-20 14:30:55 +08001673 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
1674 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1676 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1677 return 0;
1678
Rex Zhuc2870522018-09-20 14:30:55 +08001679 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
1680 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
1681 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
1682 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
1683 return 0;
1684
Alex Deucher0d35bc782018-01-24 17:57:19 -05001685 /* only APUs have vddnb */
1686 if (!(adev->flags & AMD_IS_APU) &&
1687 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
1688 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001689 return 0;
1690
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001691 return effective_mode;
1692}
1693
1694static const struct attribute_group hwmon_attrgroup = {
1695 .attrs = hwmon_attributes,
1696 .is_visible = hwmon_attributes_visible,
1697};
1698
1699static const struct attribute_group *hwmon_groups[] = {
1700 &hwmon_attrgroup,
1701 NULL
1702};
1703
1704void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1705{
1706 struct amdgpu_device *adev =
1707 container_of(work, struct amdgpu_device,
1708 pm.dpm.thermal.work);
1709 /* switch to the thermal state */
Rex Zhu3a2c7882015-08-25 15:57:43 +08001710 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001711 int temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712
1713 if (!adev->pm.dpm_enabled)
1714 return;
1715
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001716 if (adev->powerplay.pp_funcs &&
1717 adev->powerplay.pp_funcs->read_sensor &&
1718 !amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1719 (void *)&temp, &size)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001720 if (temp < adev->pm.dpm.thermal.min_temp)
1721 /* switch back the user state */
1722 dpm_state = adev->pm.dpm.user_state;
1723 } else {
1724 if (adev->pm.dpm.thermal.high_to_low)
1725 /* switch back the user state */
1726 dpm_state = adev->pm.dpm.user_state;
1727 }
1728 mutex_lock(&adev->pm.mutex);
1729 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1730 adev->pm.dpm.thermal_active = true;
1731 else
1732 adev->pm.dpm.thermal_active = false;
1733 adev->pm.dpm.state = dpm_state;
1734 mutex_unlock(&adev->pm.mutex);
1735
1736 amdgpu_pm_compute_clocks(adev);
1737}
1738
1739static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
Rex Zhu3a2c7882015-08-25 15:57:43 +08001740 enum amd_pm_state_type dpm_state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001741{
1742 int i;
1743 struct amdgpu_ps *ps;
1744 u32 ui_class;
1745 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1746 true : false;
1747
1748 /* check if the vblank period is too short to adjust the mclk */
Rex Zhucd4d7462017-09-06 18:43:52 +08001749 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001750 if (amdgpu_dpm_vblank_too_short(adev))
1751 single_display = false;
1752 }
1753
1754 /* certain older asics have a separare 3D performance state,
1755 * so try that first if the user selected performance
1756 */
1757 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1758 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1759 /* balanced states don't exist at the moment */
1760 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1761 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1762
1763restart_search:
1764 /* Pick the best power state based on current conditions */
1765 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1766 ps = &adev->pm.dpm.ps[i];
1767 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1768 switch (dpm_state) {
1769 /* user states */
1770 case POWER_STATE_TYPE_BATTERY:
1771 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1772 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1773 if (single_display)
1774 return ps;
1775 } else
1776 return ps;
1777 }
1778 break;
1779 case POWER_STATE_TYPE_BALANCED:
1780 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1781 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1782 if (single_display)
1783 return ps;
1784 } else
1785 return ps;
1786 }
1787 break;
1788 case POWER_STATE_TYPE_PERFORMANCE:
1789 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1790 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1791 if (single_display)
1792 return ps;
1793 } else
1794 return ps;
1795 }
1796 break;
1797 /* internal states */
1798 case POWER_STATE_TYPE_INTERNAL_UVD:
1799 if (adev->pm.dpm.uvd_ps)
1800 return adev->pm.dpm.uvd_ps;
1801 else
1802 break;
1803 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1804 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1805 return ps;
1806 break;
1807 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1808 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1809 return ps;
1810 break;
1811 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1812 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1813 return ps;
1814 break;
1815 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1816 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1817 return ps;
1818 break;
1819 case POWER_STATE_TYPE_INTERNAL_BOOT:
1820 return adev->pm.dpm.boot_ps;
1821 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1822 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1823 return ps;
1824 break;
1825 case POWER_STATE_TYPE_INTERNAL_ACPI:
1826 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1827 return ps;
1828 break;
1829 case POWER_STATE_TYPE_INTERNAL_ULV:
1830 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1831 return ps;
1832 break;
1833 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1834 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1835 return ps;
1836 break;
1837 default:
1838 break;
1839 }
1840 }
1841 /* use a fallback state if we didn't match */
1842 switch (dpm_state) {
1843 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1844 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1845 goto restart_search;
1846 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1847 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1848 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1849 if (adev->pm.dpm.uvd_ps) {
1850 return adev->pm.dpm.uvd_ps;
1851 } else {
1852 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1853 goto restart_search;
1854 }
1855 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1856 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1857 goto restart_search;
1858 case POWER_STATE_TYPE_INTERNAL_ACPI:
1859 dpm_state = POWER_STATE_TYPE_BATTERY;
1860 goto restart_search;
1861 case POWER_STATE_TYPE_BATTERY:
1862 case POWER_STATE_TYPE_BALANCED:
1863 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1864 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1865 goto restart_search;
1866 default:
1867 break;
1868 }
1869
1870 return NULL;
1871}
1872
1873static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1874{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 struct amdgpu_ps *ps;
Rex Zhu3a2c7882015-08-25 15:57:43 +08001876 enum amd_pm_state_type dpm_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001877 int ret;
Rex Zhucd4d7462017-09-06 18:43:52 +08001878 bool equal = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879
1880 /* if dpm init failed */
1881 if (!adev->pm.dpm_enabled)
1882 return;
1883
1884 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1885 /* add other state override checks here */
1886 if ((!adev->pm.dpm.thermal_active) &&
1887 (!adev->pm.dpm.uvd_active))
1888 adev->pm.dpm.state = adev->pm.dpm.user_state;
1889 }
1890 dpm_state = adev->pm.dpm.state;
1891
1892 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1893 if (ps)
1894 adev->pm.dpm.requested_ps = ps;
1895 else
1896 return;
1897
Rex Zhucd4d7462017-09-06 18:43:52 +08001898 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899 printk("switching from power state:\n");
1900 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1901 printk("switching to power state:\n");
1902 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1903 }
1904
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 /* update whether vce is active */
1906 ps->vce_active = adev->pm.dpm.vce_active;
Rex Zhucd4d7462017-09-06 18:43:52 +08001907 if (adev->powerplay.pp_funcs->display_configuration_changed)
1908 amdgpu_dpm_display_configuration_changed(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001909
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910 ret = amdgpu_dpm_pre_set_power_state(adev);
1911 if (ret)
Christian Königa27de352016-01-21 11:28:53 +01001912 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913
Rex Zhucd4d7462017-09-06 18:43:52 +08001914 if (adev->powerplay.pp_funcs->check_state_equal) {
1915 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1916 equal = false;
1917 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918
Rex Zhu5e876c62016-10-14 19:23:34 +08001919 if (equal)
1920 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001922 amdgpu_dpm_set_power_state(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 amdgpu_dpm_post_set_power_state(adev);
1924
Alex Deuchereda1d1c2016-02-24 17:18:25 -05001925 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1926 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1927
Rex Zhucd4d7462017-09-06 18:43:52 +08001928 if (adev->powerplay.pp_funcs->force_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929 if (adev->pm.dpm.thermal_active) {
Rex Zhue5d03ac2016-12-23 14:39:41 +08001930 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 /* force low perf level for thermal */
Rex Zhue5d03ac2016-12-23 14:39:41 +08001932 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 /* save the user's level */
1934 adev->pm.dpm.forced_level = level;
1935 } else {
1936 /* otherwise, user selected level */
1937 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1938 }
1939 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940}
1941
1942void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1943{
Rex Zhub92c6282018-06-05 13:06:11 +08001944 if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001945 /* enable/disable UVD */
1946 mutex_lock(&adev->pm.mutex);
Rex Zhub92c6282018-06-05 13:06:11 +08001947 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001948 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949 }
1950}
1951
1952void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1953{
Rex Zhub92c6282018-06-05 13:06:11 +08001954 if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001955 /* enable/disable VCE */
1956 mutex_lock(&adev->pm.mutex);
Rex Zhub92c6282018-06-05 13:06:11 +08001957 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001958 mutex_unlock(&adev->pm.mutex);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001959 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001960}
1961
1962void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1963{
1964 int i;
1965
Rex Zhucd4d7462017-09-06 18:43:52 +08001966 if (adev->powerplay.pp_funcs->print_power_state == NULL)
Rex Zhu1b5708f2015-11-10 18:25:24 -05001967 return;
1968
1969 for (i = 0; i < adev->pm.dpm.num_ps; i++)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001970 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001971
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001972}
1973
1974int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1975{
1976 int ret;
1977
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001978 if (adev->pm.sysfs_initialized)
1979 return 0;
1980
Rex Zhud2f52ac2017-09-22 17:47:27 +08001981 if (adev->pm.dpm_enabled == 0)
1982 return 0;
1983
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1985 DRIVER_NAME, adev,
1986 hwmon_groups);
1987 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1988 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1989 dev_err(adev->dev,
1990 "Unable to register hwmon device: %d\n", ret);
1991 return ret;
1992 }
1993
1994 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1995 if (ret) {
1996 DRM_ERROR("failed to create device file for dpm state\n");
1997 return ret;
1998 }
1999 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2000 if (ret) {
2001 DRM_ERROR("failed to create device file for dpm state\n");
2002 return ret;
2003 }
Eric Huangf3898ea2015-12-11 16:24:34 -05002004
Rex Zhu6d07fe72017-09-25 18:51:50 +08002005
2006 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
2007 if (ret) {
2008 DRM_ERROR("failed to create device file pp_num_states\n");
2009 return ret;
2010 }
2011 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
2012 if (ret) {
2013 DRM_ERROR("failed to create device file pp_cur_state\n");
2014 return ret;
2015 }
2016 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
2017 if (ret) {
2018 DRM_ERROR("failed to create device file pp_force_state\n");
2019 return ret;
2020 }
2021 ret = device_create_file(adev->dev, &dev_attr_pp_table);
2022 if (ret) {
2023 DRM_ERROR("failed to create device file pp_table\n");
2024 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -05002025 }
Eric Huangc85e2992016-05-19 15:41:25 -04002026
2027 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2028 if (ret) {
2029 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2030 return ret;
2031 }
2032 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2033 if (ret) {
2034 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2035 return ret;
2036 }
2037 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
2038 if (ret) {
2039 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
2040 return ret;
2041 }
Eric Huang8b2e5742016-05-19 15:46:10 -04002042 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
2043 if (ret) {
2044 DRM_ERROR("failed to create device file pp_sclk_od\n");
2045 return ret;
2046 }
Eric Huangf2bdc052016-05-24 15:11:17 -04002047 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
2048 if (ret) {
2049 DRM_ERROR("failed to create device file pp_mclk_od\n");
2050 return ret;
2051 }
Eric Huang34bb2732016-09-12 16:17:44 -04002052 ret = device_create_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08002053 &dev_attr_pp_power_profile_mode);
2054 if (ret) {
2055 DRM_ERROR("failed to create device file "
2056 "pp_power_profile_mode\n");
2057 return ret;
2058 }
Rex Zhue3933f22018-01-16 18:35:15 +08002059 ret = device_create_file(adev->dev,
2060 &dev_attr_pp_od_clk_voltage);
2061 if (ret) {
2062 DRM_ERROR("failed to create device file "
2063 "pp_od_clk_voltage\n");
2064 return ret;
2065 }
Tom St Denisb374d822018-06-20 07:55:39 -04002066 ret = device_create_file(adev->dev,
2067 &dev_attr_gpu_busy_percent);
2068 if (ret) {
2069 DRM_ERROR("failed to create device file "
2070 "gpu_busy_level\n");
2071 return ret;
2072 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 ret = amdgpu_debugfs_pm_init(adev);
2074 if (ret) {
2075 DRM_ERROR("Failed to register debugfs file for dpm!\n");
2076 return ret;
2077 }
2078
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04002079 adev->pm.sysfs_initialized = true;
2080
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081 return 0;
2082}
2083
2084void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
2085{
Rex Zhud2f52ac2017-09-22 17:47:27 +08002086 if (adev->pm.dpm_enabled == 0)
2087 return;
2088
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002089 if (adev->pm.int_hwmon_dev)
2090 hwmon_device_unregister(adev->pm.int_hwmon_dev);
2091 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
2092 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
Rex Zhu6d07fe72017-09-25 18:51:50 +08002093
2094 device_remove_file(adev->dev, &dev_attr_pp_num_states);
2095 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
2096 device_remove_file(adev->dev, &dev_attr_pp_force_state);
2097 device_remove_file(adev->dev, &dev_attr_pp_table);
2098
Eric Huangc85e2992016-05-19 15:41:25 -04002099 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2100 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2101 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
Eric Huang8b2e5742016-05-19 15:46:10 -04002102 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -04002103 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
Eric Huang34bb2732016-09-12 16:17:44 -04002104 device_remove_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08002105 &dev_attr_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +08002106 device_remove_file(adev->dev,
2107 &dev_attr_pp_od_clk_voltage);
Tom St Denisb374d822018-06-20 07:55:39 -04002108 device_remove_file(adev->dev, &dev_attr_gpu_busy_percent);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109}
2110
2111void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
2112{
Rex Zhu5e876c62016-10-14 19:23:34 +08002113 int i = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002114
2115 if (!adev->pm.dpm_enabled)
2116 return;
2117
Alex Deucherc10c8f72017-02-10 18:09:32 -05002118 if (adev->mode_info.num_crtc)
2119 amdgpu_display_bandwidth_update(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08002120
2121 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
2122 struct amdgpu_ring *ring = adev->rings[i];
2123 if (ring && ring->ready)
2124 amdgpu_fence_wait_empty(ring);
2125 }
2126
Rex Zhu6d07fe72017-09-25 18:51:50 +08002127 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhu51d45cb2018-04-27 14:09:30 +08002128 if (!amdgpu_device_has_dc_support(adev)) {
2129 mutex_lock(&adev->pm.mutex);
2130 amdgpu_dpm_get_active_displays(adev);
Alex Deucher70eb46d2018-06-28 12:44:25 -05002131 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
Rex Zhu51d45cb2018-04-27 14:09:30 +08002132 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
2133 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
2134 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
2135 if (adev->pm.pm_display_cfg.vrefresh > 120)
2136 adev->pm.pm_display_cfg.min_vblank_time = 0;
2137 if (adev->powerplay.pp_funcs->display_configuration_change)
2138 adev->powerplay.pp_funcs->display_configuration_change(
2139 adev->powerplay.pp_handle,
2140 &adev->pm.pm_display_cfg);
2141 mutex_unlock(&adev->pm.mutex);
2142 }
Evan Quan39199b82017-12-29 14:46:13 +08002143 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
Rex Zhu1b5708f2015-11-10 18:25:24 -05002144 } else {
2145 mutex_lock(&adev->pm.mutex);
Rex Zhu51d45cb2018-04-27 14:09:30 +08002146 amdgpu_dpm_get_active_displays(adev);
Rex Zhu1b5708f2015-11-10 18:25:24 -05002147 amdgpu_dpm_change_power_state_locked(adev);
Rex Zhu1b5708f2015-11-10 18:25:24 -05002148 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002149 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002150}
2151
2152/*
2153 * Debugfs info
2154 */
2155#if defined(CONFIG_DEBUG_FS)
2156
Tom St Denis3de4ec52016-09-19 12:48:52 -04002157static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
2158{
Eric Huangcd7b0c62017-02-07 16:37:48 -05002159 uint32_t value;
Alex Deucher505f8db2018-09-20 22:50:07 -05002160 uint64_t value64;
Rex Zhu5b79d042018-04-04 15:37:35 +08002161 uint32_t query = 0;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002162 int size;
Tom St Denis3de4ec52016-09-19 12:48:52 -04002163
2164 /* sanity check PP is enabled */
2165 if (!(adev->powerplay.pp_funcs &&
2166 adev->powerplay.pp_funcs->read_sensor))
2167 return -EINVAL;
2168
2169 /* GPU Clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002170 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04002171 seq_printf(m, "GFX Clocks and Power:\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002172 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002173 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002174 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002175 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
Rex Zhu5ed8d652018-01-08 13:59:05 +08002176 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
2177 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
2178 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
2179 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002180 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002181 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002182 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002183 seq_printf(m, "\t%u mV (VDDNB)\n", value);
Rex Zhu5b79d042018-04-04 15:37:35 +08002184 size = sizeof(uint32_t);
2185 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
2186 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002187 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04002188 seq_printf(m, "\n");
2189
2190 /* GPU Temp */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002191 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002192 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
2193
2194 /* GPU Load */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002195 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002196 seq_printf(m, "GPU Load: %u %%\n", value);
2197 seq_printf(m, "\n");
2198
Alex Deucher505f8db2018-09-20 22:50:07 -05002199 /* SMC feature mask */
2200 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
2201 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
2202
Tom St Denis3de4ec52016-09-19 12:48:52 -04002203 /* UVD clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002204 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04002205 if (!value) {
2206 seq_printf(m, "UVD: Disabled\n");
2207 } else {
2208 seq_printf(m, "UVD: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002209 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002210 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002211 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002212 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
2213 }
2214 }
2215 seq_printf(m, "\n");
2216
2217 /* VCE clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002218 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04002219 if (!value) {
2220 seq_printf(m, "VCE: Disabled\n");
2221 } else {
2222 seq_printf(m, "VCE: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002223 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002224 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
2225 }
2226 }
2227
2228 return 0;
2229}
2230
Huang Ruia8503b12017-01-05 19:17:13 +08002231static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
2232{
2233 int i;
2234
2235 for (i = 0; clocks[i].flag; i++)
2236 seq_printf(m, "\t%s: %s\n", clocks[i].name,
2237 (flags & clocks[i].flag) ? "On" : "Off");
2238}
2239
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
2241{
2242 struct drm_info_node *node = (struct drm_info_node *) m->private;
2243 struct drm_device *dev = node->minor->dev;
2244 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher0c67df42016-02-19 15:30:15 -05002245 struct drm_device *ddev = adev->ddev;
Huang Rui6cb2d4e2017-01-05 18:44:41 +08002246 u32 flags = 0;
2247
Alex Deucher2990a1f2017-12-15 16:18:00 -05002248 amdgpu_device_ip_get_clockgating_state(adev, &flags);
Huang Rui6cb2d4e2017-01-05 18:44:41 +08002249 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
Huang Ruia8503b12017-01-05 19:17:13 +08002250 amdgpu_parse_cg_state(m, flags);
2251 seq_printf(m, "\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252
Rex Zhu1b5708f2015-11-10 18:25:24 -05002253 if (!adev->pm.dpm_enabled) {
2254 seq_printf(m, "dpm not enabled\n");
2255 return 0;
2256 }
Alex Deucher0c67df42016-02-19 15:30:15 -05002257 if ((adev->flags & AMD_IS_PX) &&
2258 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
2259 seq_printf(m, "PX asic powered off\n");
Rex Zhu6d07fe72017-09-25 18:51:50 +08002260 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002261 mutex_lock(&adev->pm.mutex);
Rex Zhucd4d7462017-09-06 18:43:52 +08002262 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
2263 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 else
2265 seq_printf(m, "Debugfs support not implemented for this asic\n");
2266 mutex_unlock(&adev->pm.mutex);
Rex Zhu6d07fe72017-09-25 18:51:50 +08002267 } else {
2268 return amdgpu_debugfs_pm_info_pp(m, adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269 }
2270
2271 return 0;
2272}
2273
Nils Wallménius06ab6832016-05-02 12:46:15 -04002274static const struct drm_info_list amdgpu_pm_info_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002275 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
2276};
2277#endif
2278
2279static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
2280{
2281#if defined(CONFIG_DEBUG_FS)
2282 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
2283#else
2284 return 0;
2285#endif
2286}