blob: f667cb9eb614c77111e7a503687ef264b63e8d28 [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"
30#include "atom.h"
31#include <linux/power_supply.h>
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34
Rex Zhu1b5708f2015-11-10 18:25:24 -050035
Alex Deucherd38ceaf2015-04-20 16:55:21 -040036static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
37
Huang Ruia8503b12017-01-05 19:17:13 +080038static const struct cg_flag_name clocks[] = {
39 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
40 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
41 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
42 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
Huang Rui54170222017-01-11 09:55:34 +080043 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080044 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
45 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
46 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
Huang Rui12ad27f2017-03-24 09:58:11 +080047 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
48 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080049 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
50 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
51 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
52 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
Huang Ruie96487a2017-03-24 10:12:32 +080053 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080054 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
55 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
56 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
57 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
58 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080059 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
60 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080061 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080062 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080063 {0, NULL},
64};
65
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
67{
68 if (adev->pm.dpm_enabled) {
69 mutex_lock(&adev->pm.mutex);
70 if (power_supply_is_system_supplied() > 0)
71 adev->pm.dpm.ac_power = true;
72 else
73 adev->pm.dpm.ac_power = false;
Rex Zhucd4d7462017-09-06 18:43:52 +080074 if (adev->powerplay.pp_funcs->enable_bapm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
76 mutex_unlock(&adev->pm.mutex);
77 }
78}
79
Alex Deucherca8d40c2018-04-19 13:56:41 -050080/**
81 * DOC: power_dpm_state
82 *
Alex Deucherdc85db22018-06-01 12:28:14 -050083 * The power_dpm_state file is a legacy interface and is only provided for
84 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
85 * certain power related parameters. The file power_dpm_state is used for this.
Alex Deucherca8d40c2018-04-19 13:56:41 -050086 * It accepts the following arguments:
Alex Deucherdc85db22018-06-01 12:28:14 -050087 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050088 * - battery
Alex Deucherdc85db22018-06-01 12:28:14 -050089 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050090 * - balanced
Alex Deucherdc85db22018-06-01 12:28:14 -050091 *
Alex Deucherca8d40c2018-04-19 13:56:41 -050092 * - performance
93 *
94 * battery
95 *
96 * On older GPUs, the vbios provided a special power state for battery
97 * operation. Selecting battery switched to this state. This is no
98 * longer provided on newer GPUs so the option does nothing in that case.
99 *
100 * balanced
101 *
102 * On older GPUs, the vbios provided a special power state for balanced
103 * operation. Selecting balanced switched to this state. This is no
104 * longer provided on newer GPUs so the option does nothing in that case.
105 *
106 * performance
107 *
108 * On older GPUs, the vbios provided a special power state for performance
109 * operation. Selecting performance switched to this state. This is no
110 * longer provided on newer GPUs so the option does nothing in that case.
111 *
112 */
113
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114static ssize_t amdgpu_get_dpm_state(struct device *dev,
115 struct device_attribute *attr,
116 char *buf)
117{
118 struct drm_device *ddev = dev_get_drvdata(dev);
119 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500120 enum amd_pm_state_type pm;
121
Rex Zhucd4d7462017-09-06 18:43:52 +0800122 if (adev->powerplay.pp_funcs->get_current_power_state)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500123 pm = amdgpu_dpm_get_current_power_state(adev);
Rex Zhucd4d7462017-09-06 18:43:52 +0800124 else
Rex Zhu1b5708f2015-11-10 18:25:24 -0500125 pm = adev->pm.dpm.user_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126
127 return snprintf(buf, PAGE_SIZE, "%s\n",
128 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
129 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
130}
131
132static ssize_t amdgpu_set_dpm_state(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf,
135 size_t count)
136{
137 struct drm_device *ddev = dev_get_drvdata(dev);
138 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500139 enum amd_pm_state_type state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 if (strncmp("battery", buf, strlen("battery")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500142 state = POWER_STATE_TYPE_BATTERY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500144 state = POWER_STATE_TYPE_BALANCED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 else if (strncmp("performance", buf, strlen("performance")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500146 state = POWER_STATE_TYPE_PERFORMANCE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 count = -EINVAL;
149 goto fail;
150 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151
Rex Zhu6d07fe72017-09-25 18:51:50 +0800152 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800153 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
Rex Zhu1b5708f2015-11-10 18:25:24 -0500154 } else {
155 mutex_lock(&adev->pm.mutex);
156 adev->pm.dpm.user_state = state;
157 mutex_unlock(&adev->pm.mutex);
158
159 /* Can't set dpm state when the card is off */
160 if (!(adev->flags & AMD_IS_PX) ||
161 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
162 amdgpu_pm_compute_clocks(adev);
163 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164fail:
165 return count;
166}
167
Alex Deucher8567f682018-04-19 13:46:03 -0500168
169/**
170 * DOC: power_dpm_force_performance_level
171 *
172 * The amdgpu driver provides a sysfs API for adjusting certain power
173 * related parameters. The file power_dpm_force_performance_level is
174 * used for this. It accepts the following arguments:
Alex Deucherdc85db22018-06-01 12:28:14 -0500175 *
Alex Deucher8567f682018-04-19 13:46:03 -0500176 * - auto
Alex Deucherdc85db22018-06-01 12:28:14 -0500177 *
Alex Deucher8567f682018-04-19 13:46:03 -0500178 * - low
Alex Deucherdc85db22018-06-01 12:28:14 -0500179 *
Alex Deucher8567f682018-04-19 13:46:03 -0500180 * - high
Alex Deucherdc85db22018-06-01 12:28:14 -0500181 *
Alex Deucher8567f682018-04-19 13:46:03 -0500182 * - manual
Alex Deucherdc85db22018-06-01 12:28:14 -0500183 *
Alex Deucher8567f682018-04-19 13:46:03 -0500184 * - profile_standard
Alex Deucherdc85db22018-06-01 12:28:14 -0500185 *
Alex Deucher8567f682018-04-19 13:46:03 -0500186 * - profile_min_sclk
Alex Deucherdc85db22018-06-01 12:28:14 -0500187 *
Alex Deucher8567f682018-04-19 13:46:03 -0500188 * - profile_min_mclk
Alex Deucherdc85db22018-06-01 12:28:14 -0500189 *
Alex Deucher8567f682018-04-19 13:46:03 -0500190 * - profile_peak
191 *
192 * auto
193 *
194 * When auto is selected, the driver will attempt to dynamically select
195 * the optimal power profile for current conditions in the driver.
196 *
197 * low
198 *
199 * When low is selected, the clocks are forced to the lowest power state.
200 *
201 * high
202 *
203 * When high is selected, the clocks are forced to the highest power state.
204 *
205 * manual
206 *
207 * When manual is selected, the user can manually adjust which power states
208 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
209 * and pp_dpm_pcie files and adjust the power state transition heuristics
210 * via the pp_power_profile_mode sysfs file.
211 *
212 * profile_standard
213 * profile_min_sclk
214 * profile_min_mclk
215 * profile_peak
216 *
217 * When the profiling modes are selected, clock and power gating are
218 * disabled and the clocks are set for different profiling cases. This
219 * mode is recommended for profiling specific work loads where you do
220 * not want clock or power gating for clock fluctuation to interfere
221 * with your results. profile_standard sets the clocks to a fixed clock
222 * level which varies from asic to asic. profile_min_sclk forces the sclk
223 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
224 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
225 *
226 */
227
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
Rex Zhu1b5708f2015-11-10 18:25:24 -0500229 struct device_attribute *attr,
230 char *buf)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231{
232 struct drm_device *ddev = dev_get_drvdata(dev);
233 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhucd4d7462017-09-06 18:43:52 +0800234 enum amd_dpm_forced_level level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235
Alex Deucher0c67df42016-02-19 15:30:15 -0500236 if ((adev->flags & AMD_IS_PX) &&
237 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
238 return snprintf(buf, PAGE_SIZE, "off\n");
239
Rex Zhucd4d7462017-09-06 18:43:52 +0800240 if (adev->powerplay.pp_funcs->get_performance_level)
241 level = amdgpu_dpm_get_performance_level(adev);
242 else
243 level = adev->pm.dpm.forced_level;
244
Rex Zhue5d03ac2016-12-23 14:39:41 +0800245 return snprintf(buf, PAGE_SIZE, "%s\n",
Rex Zhu570272d2017-01-06 13:32:49 +0800246 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
247 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
248 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
249 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
250 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
251 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
252 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
253 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
254 "unknown");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255}
256
257static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
258 struct device_attribute *attr,
259 const char *buf,
260 size_t count)
261{
262 struct drm_device *ddev = dev_get_drvdata(dev);
263 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhue5d03ac2016-12-23 14:39:41 +0800264 enum amd_dpm_forced_level level;
Rex Zhucd4d7462017-09-06 18:43:52 +0800265 enum amd_dpm_forced_level current_level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 int ret = 0;
267
Alex Deucher0c67df42016-02-19 15:30:15 -0500268 /* Can't force performance level when the card is off */
269 if ((adev->flags & AMD_IS_PX) &&
270 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
271 return -EINVAL;
272
Rex Zhucd4d7462017-09-06 18:43:52 +0800273 if (adev->powerplay.pp_funcs->get_performance_level)
274 current_level = amdgpu_dpm_get_performance_level(adev);
Rex Zhu3bd58972016-12-23 15:24:37 +0800275
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276 if (strncmp("low", buf, strlen("low")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800277 level = AMD_DPM_FORCED_LEVEL_LOW;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278 } else if (strncmp("high", buf, strlen("high")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800279 level = AMD_DPM_FORCED_LEVEL_HIGH;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400280 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800281 level = AMD_DPM_FORCED_LEVEL_AUTO;
Eric Huangf3898ea2015-12-11 16:24:34 -0500282 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800283 level = AMD_DPM_FORCED_LEVEL_MANUAL;
Rex Zhu570272d2017-01-06 13:32:49 +0800284 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
285 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
286 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
287 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
288 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
289 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
290 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
291 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
292 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
293 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
294 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295 count = -EINVAL;
296 goto fail;
297 }
Rex Zhu1b5708f2015-11-10 18:25:24 -0500298
Rex Zhu3bd58972016-12-23 15:24:37 +0800299 if (current_level == level)
Rex Zhu8e7afd32017-01-09 15:18:01 +0800300 return count;
Rex Zhu3bd58972016-12-23 15:24:37 +0800301
Rex Zhucd4d7462017-09-06 18:43:52 +0800302 if (adev->powerplay.pp_funcs->force_performance_level) {
Rex Zhu1b5708f2015-11-10 18:25:24 -0500303 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 if (adev->pm.dpm.thermal_active) {
305 count = -EINVAL;
Alex Deucher10f950f2016-02-19 15:18:45 -0500306 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307 goto fail;
308 }
309 ret = amdgpu_dpm_force_performance_level(adev, level);
310 if (ret)
311 count = -EINVAL;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500312 else
313 adev->pm.dpm.forced_level = level;
314 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 }
Rex Zhu570272d2017-01-06 13:32:49 +0800316
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400317fail:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400318 return count;
319}
320
Eric Huangf3898ea2015-12-11 16:24:34 -0500321static ssize_t amdgpu_get_pp_num_states(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324{
325 struct drm_device *ddev = dev_get_drvdata(dev);
326 struct amdgpu_device *adev = ddev->dev_private;
327 struct pp_states_info data;
328 int i, buf_len;
329
Rex Zhucd4d7462017-09-06 18:43:52 +0800330 if (adev->powerplay.pp_funcs->get_pp_num_states)
Eric Huangf3898ea2015-12-11 16:24:34 -0500331 amdgpu_dpm_get_pp_num_states(adev, &data);
332
333 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
334 for (i = 0; i < data.nums; i++)
335 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
336 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
337 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
338 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
339 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
340
341 return buf_len;
342}
343
344static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
345 struct device_attribute *attr,
346 char *buf)
347{
348 struct drm_device *ddev = dev_get_drvdata(dev);
349 struct amdgpu_device *adev = ddev->dev_private;
350 struct pp_states_info data;
351 enum amd_pm_state_type pm = 0;
352 int i = 0;
353
Rex Zhucd4d7462017-09-06 18:43:52 +0800354 if (adev->powerplay.pp_funcs->get_current_power_state
355 && adev->powerplay.pp_funcs->get_pp_num_states) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500356 pm = amdgpu_dpm_get_current_power_state(adev);
357 amdgpu_dpm_get_pp_num_states(adev, &data);
358
359 for (i = 0; i < data.nums; i++) {
360 if (pm == data.states[i])
361 break;
362 }
363
364 if (i == data.nums)
365 i = -EINVAL;
366 }
367
368 return snprintf(buf, PAGE_SIZE, "%d\n", i);
369}
370
371static ssize_t amdgpu_get_pp_force_state(struct device *dev,
372 struct device_attribute *attr,
373 char *buf)
374{
375 struct drm_device *ddev = dev_get_drvdata(dev);
376 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500377
Rex Zhucd4d7462017-09-06 18:43:52 +0800378 if (adev->pp_force_state_enabled)
379 return amdgpu_get_pp_cur_state(dev, attr, buf);
380 else
Eric Huangf3898ea2015-12-11 16:24:34 -0500381 return snprintf(buf, PAGE_SIZE, "\n");
382}
383
384static ssize_t amdgpu_set_pp_force_state(struct device *dev,
385 struct device_attribute *attr,
386 const char *buf,
387 size_t count)
388{
389 struct drm_device *ddev = dev_get_drvdata(dev);
390 struct amdgpu_device *adev = ddev->dev_private;
391 enum amd_pm_state_type state = 0;
Dan Carpenter041bf022016-06-16 11:30:23 +0300392 unsigned long idx;
Eric Huangf3898ea2015-12-11 16:24:34 -0500393 int ret;
394
395 if (strlen(buf) == 1)
396 adev->pp_force_state_enabled = false;
Rex Zhu6d07fe72017-09-25 18:51:50 +0800397 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
398 adev->powerplay.pp_funcs->get_pp_num_states) {
Dan Carpenter041bf022016-06-16 11:30:23 +0300399 struct pp_states_info data;
Eric Huangf3898ea2015-12-11 16:24:34 -0500400
Dan Carpenter041bf022016-06-16 11:30:23 +0300401 ret = kstrtoul(buf, 0, &idx);
402 if (ret || idx >= ARRAY_SIZE(data.states)) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500403 count = -EINVAL;
404 goto fail;
405 }
406
Dan Carpenter041bf022016-06-16 11:30:23 +0300407 amdgpu_dpm_get_pp_num_states(adev, &data);
408 state = data.states[idx];
409 /* only set user selected power states */
410 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
411 state != POWER_STATE_TYPE_DEFAULT) {
412 amdgpu_dpm_dispatch_task(adev,
Evan Quan39199b82017-12-29 14:46:13 +0800413 AMD_PP_TASK_ENABLE_USER_STATE, &state);
Dan Carpenter041bf022016-06-16 11:30:23 +0300414 adev->pp_force_state_enabled = true;
Eric Huangf3898ea2015-12-11 16:24:34 -0500415 }
416 }
417fail:
418 return count;
419}
420
Alex Deucherd54bb402018-04-19 14:02:52 -0500421/**
422 * DOC: pp_table
423 *
424 * The amdgpu driver provides a sysfs API for uploading new powerplay
425 * tables. The file pp_table is used for this. Reading the file
426 * will dump the current power play table. Writing to the file
427 * will attempt to upload a new powerplay table and re-initialize
428 * powerplay using that new table.
429 *
430 */
431
Eric Huangf3898ea2015-12-11 16:24:34 -0500432static ssize_t amdgpu_get_pp_table(struct device *dev,
433 struct device_attribute *attr,
434 char *buf)
435{
436 struct drm_device *ddev = dev_get_drvdata(dev);
437 struct amdgpu_device *adev = ddev->dev_private;
438 char *table = NULL;
Eric Huang1684d3b2016-07-28 17:25:01 -0400439 int size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500440
Rex Zhucd4d7462017-09-06 18:43:52 +0800441 if (adev->powerplay.pp_funcs->get_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500442 size = amdgpu_dpm_get_pp_table(adev, &table);
443 else
444 return 0;
445
446 if (size >= PAGE_SIZE)
447 size = PAGE_SIZE - 1;
448
Eric Huang1684d3b2016-07-28 17:25:01 -0400449 memcpy(buf, table, size);
Eric Huangf3898ea2015-12-11 16:24:34 -0500450
451 return size;
452}
453
454static ssize_t amdgpu_set_pp_table(struct device *dev,
455 struct device_attribute *attr,
456 const char *buf,
457 size_t count)
458{
459 struct drm_device *ddev = dev_get_drvdata(dev);
460 struct amdgpu_device *adev = ddev->dev_private;
461
Rex Zhucd4d7462017-09-06 18:43:52 +0800462 if (adev->powerplay.pp_funcs->set_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500463 amdgpu_dpm_set_pp_table(adev, buf, count);
464
465 return count;
466}
467
Alex Deucher4e418c32018-04-19 14:59:55 -0500468/**
469 * DOC: pp_od_clk_voltage
470 *
471 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
472 * in each power level within a power state. The pp_od_clk_voltage is used for
473 * this.
474 *
475 * Reading the file will display:
Alex Deucherdc85db22018-06-01 12:28:14 -0500476 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500477 * - a list of engine clock levels and voltages labeled OD_SCLK
Alex Deucherdc85db22018-06-01 12:28:14 -0500478 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500479 * - a list of memory clock levels and voltages labeled OD_MCLK
Alex Deucherdc85db22018-06-01 12:28:14 -0500480 *
Alex Deucher4e418c32018-04-19 14:59:55 -0500481 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
482 *
483 * To manually adjust these settings, first select manual using
484 * power_dpm_force_performance_level. Enter a new value for each
485 * level by writing a string that contains "s/m level clock voltage" to
486 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
487 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
488 * 810 mV. When you have edited all of the states as needed, write
489 * "c" (commit) to the file to commit your changes. If you want to reset to the
490 * default power levels, write "r" (reset) to the file to reset them.
491 *
492 */
493
Rex Zhue3933f22018-01-16 18:35:15 +0800494static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf,
497 size_t count)
498{
499 struct drm_device *ddev = dev_get_drvdata(dev);
500 struct amdgpu_device *adev = ddev->dev_private;
501 int ret;
502 uint32_t parameter_size = 0;
503 long parameter[64];
504 char buf_cpy[128];
505 char *tmp_str;
506 char *sub_str;
507 const char delimiter[3] = {' ', '\n', '\0'};
508 uint32_t type;
509
510 if (count > 127)
511 return -EINVAL;
512
513 if (*buf == 's')
514 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
515 else if (*buf == 'm')
516 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
517 else if(*buf == 'r')
518 type = PP_OD_RESTORE_DEFAULT_TABLE;
519 else if (*buf == 'c')
520 type = PP_OD_COMMIT_DPM_TABLE;
521 else
522 return -EINVAL;
523
524 memcpy(buf_cpy, buf, count+1);
525
526 tmp_str = buf_cpy;
527
528 while (isspace(*++tmp_str));
529
530 while (tmp_str[0]) {
531 sub_str = strsep(&tmp_str, delimiter);
532 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
533 if (ret)
534 return -EINVAL;
535 parameter_size++;
536
537 while (isspace(*tmp_str))
538 tmp_str++;
539 }
540
541 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
542 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
543 parameter, parameter_size);
544
545 if (ret)
546 return -EINVAL;
547
548 if (type == PP_OD_COMMIT_DPM_TABLE) {
549 if (adev->powerplay.pp_funcs->dispatch_tasks) {
550 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
551 return count;
552 } else {
553 return -EINVAL;
554 }
555 }
556
557 return count;
558}
559
560static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
561 struct device_attribute *attr,
562 char *buf)
563{
564 struct drm_device *ddev = dev_get_drvdata(dev);
565 struct amdgpu_device *adev = ddev->dev_private;
566 uint32_t size = 0;
567
568 if (adev->powerplay.pp_funcs->print_clock_levels) {
569 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
570 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
Rex Zhua3c991f2018-04-19 10:39:17 +0800571 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
Rex Zhue3933f22018-01-16 18:35:15 +0800572 return size;
573 } else {
574 return snprintf(buf, PAGE_SIZE, "\n");
575 }
576
577}
578
Alex Deucher271dc902018-04-19 14:22:24 -0500579/**
580 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_pcie
581 *
582 * The amdgpu driver provides a sysfs API for adjusting what power levels
583 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
584 * and pp_dpm_pcie are used for this.
585 *
586 * Reading back the files will show you the available power levels within
587 * the power state and the clock information for those levels.
588 *
589 * To manually adjust these states, first select manual using
welu48edde32018-04-24 09:13:20 -0400590 * power_dpm_force_performance_level.
591 * Secondly,Enter a new value for each level by inputing a string that
592 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
593 * 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 -0500594 */
595
Eric Huangf3898ea2015-12-11 16:24:34 -0500596static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
597 struct device_attribute *attr,
598 char *buf)
599{
600 struct drm_device *ddev = dev_get_drvdata(dev);
601 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500602
Rex Zhucd4d7462017-09-06 18:43:52 +0800603 if (adev->powerplay.pp_funcs->print_clock_levels)
604 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
605 else
606 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500607}
608
609static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf,
612 size_t count)
613{
614 struct drm_device *ddev = dev_get_drvdata(dev);
615 struct amdgpu_device *adev = ddev->dev_private;
616 int ret;
617 long level;
welu48edde32018-04-24 09:13:20 -0400618 uint32_t mask = 0;
619 char *sub_str = NULL;
620 char *tmp;
621 char buf_cpy[count];
622 const char delimiter[3] = {' ', '\n', '\0'};
Eric Huangf3898ea2015-12-11 16:24:34 -0500623
welu48edde32018-04-24 09:13:20 -0400624 memcpy(buf_cpy, buf, count+1);
625 tmp = buf_cpy;
626 while (tmp[0]) {
627 sub_str = strsep(&tmp, delimiter);
628 if (strlen(sub_str)) {
629 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500630
welu48edde32018-04-24 09:13:20 -0400631 if (ret) {
632 count = -EINVAL;
633 goto fail;
634 }
635 mask |= 1 << level;
636 } else
637 break;
Eric Huangf3898ea2015-12-11 16:24:34 -0500638 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800639 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400640 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800641
Eric Huangf3898ea2015-12-11 16:24:34 -0500642fail:
643 return count;
644}
645
646static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
647 struct device_attribute *attr,
648 char *buf)
649{
650 struct drm_device *ddev = dev_get_drvdata(dev);
651 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500652
Rex Zhucd4d7462017-09-06 18:43:52 +0800653 if (adev->powerplay.pp_funcs->print_clock_levels)
654 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
655 else
656 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500657}
658
659static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
660 struct device_attribute *attr,
661 const char *buf,
662 size_t count)
663{
664 struct drm_device *ddev = dev_get_drvdata(dev);
665 struct amdgpu_device *adev = ddev->dev_private;
666 int ret;
667 long level;
welu48edde32018-04-24 09:13:20 -0400668 uint32_t mask = 0;
669 char *sub_str = NULL;
670 char *tmp;
671 char buf_cpy[count];
672 const char delimiter[3] = {' ', '\n', '\0'};
Eric Huangf3898ea2015-12-11 16:24:34 -0500673
welu48edde32018-04-24 09:13:20 -0400674 memcpy(buf_cpy, buf, count+1);
675 tmp = buf_cpy;
676 while (tmp[0]) {
677 sub_str = strsep(&tmp, delimiter);
678 if (strlen(sub_str)) {
679 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500680
welu48edde32018-04-24 09:13:20 -0400681 if (ret) {
682 count = -EINVAL;
683 goto fail;
684 }
685 mask |= 1 << level;
686 } else
687 break;
Eric Huangf3898ea2015-12-11 16:24:34 -0500688 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800689 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400690 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800691
Eric Huangf3898ea2015-12-11 16:24:34 -0500692fail:
693 return count;
694}
695
696static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
697 struct device_attribute *attr,
698 char *buf)
699{
700 struct drm_device *ddev = dev_get_drvdata(dev);
701 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500702
Rex Zhucd4d7462017-09-06 18:43:52 +0800703 if (adev->powerplay.pp_funcs->print_clock_levels)
704 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
705 else
706 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500707}
708
709static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
710 struct device_attribute *attr,
711 const char *buf,
712 size_t count)
713{
714 struct drm_device *ddev = dev_get_drvdata(dev);
715 struct amdgpu_device *adev = ddev->dev_private;
716 int ret;
717 long level;
welu48edde32018-04-24 09:13:20 -0400718 uint32_t mask = 0;
719 char *sub_str = NULL;
720 char *tmp;
721 char buf_cpy[count];
722 const char delimiter[3] = {' ', '\n', '\0'};
Eric Huangf3898ea2015-12-11 16:24:34 -0500723
welu48edde32018-04-24 09:13:20 -0400724 memcpy(buf_cpy, buf, count+1);
725 tmp = buf_cpy;
Eric Huangf3898ea2015-12-11 16:24:34 -0500726
welu48edde32018-04-24 09:13:20 -0400727 while (tmp[0]) {
728 sub_str = strsep(&tmp, delimiter);
729 if (strlen(sub_str)) {
730 ret = kstrtol(sub_str, 0, &level);
731
732 if (ret) {
733 count = -EINVAL;
734 goto fail;
735 }
736 mask |= 1 << level;
737 } else
738 break;
Eric Huangf3898ea2015-12-11 16:24:34 -0500739 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800740 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400741 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800742
Eric Huangf3898ea2015-12-11 16:24:34 -0500743fail:
744 return count;
745}
746
Eric Huang428bafa2016-05-12 14:51:21 -0400747static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
748 struct device_attribute *attr,
749 char *buf)
750{
751 struct drm_device *ddev = dev_get_drvdata(dev);
752 struct amdgpu_device *adev = ddev->dev_private;
753 uint32_t value = 0;
754
Rex Zhucd4d7462017-09-06 18:43:52 +0800755 if (adev->powerplay.pp_funcs->get_sclk_od)
Eric Huang428bafa2016-05-12 14:51:21 -0400756 value = amdgpu_dpm_get_sclk_od(adev);
757
758 return snprintf(buf, PAGE_SIZE, "%d\n", value);
759}
760
761static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
762 struct device_attribute *attr,
763 const char *buf,
764 size_t count)
765{
766 struct drm_device *ddev = dev_get_drvdata(dev);
767 struct amdgpu_device *adev = ddev->dev_private;
768 int ret;
769 long int value;
770
771 ret = kstrtol(buf, 0, &value);
772
773 if (ret) {
774 count = -EINVAL;
775 goto fail;
776 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800777 if (adev->powerplay.pp_funcs->set_sclk_od)
778 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
Eric Huang428bafa2016-05-12 14:51:21 -0400779
Rex Zhu6d07fe72017-09-25 18:51:50 +0800780 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800781 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800782 } else {
Eric Huang8b2e5742016-05-19 15:46:10 -0400783 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
784 amdgpu_pm_compute_clocks(adev);
785 }
Eric Huang428bafa2016-05-12 14:51:21 -0400786
787fail:
788 return count;
789}
790
Eric Huangf2bdc052016-05-24 15:11:17 -0400791static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
792 struct device_attribute *attr,
793 char *buf)
794{
795 struct drm_device *ddev = dev_get_drvdata(dev);
796 struct amdgpu_device *adev = ddev->dev_private;
797 uint32_t value = 0;
798
Rex Zhucd4d7462017-09-06 18:43:52 +0800799 if (adev->powerplay.pp_funcs->get_mclk_od)
Eric Huangf2bdc052016-05-24 15:11:17 -0400800 value = amdgpu_dpm_get_mclk_od(adev);
Eric Huangf2bdc052016-05-24 15:11:17 -0400801
802 return snprintf(buf, PAGE_SIZE, "%d\n", value);
803}
804
805static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
806 struct device_attribute *attr,
807 const char *buf,
808 size_t count)
809{
810 struct drm_device *ddev = dev_get_drvdata(dev);
811 struct amdgpu_device *adev = ddev->dev_private;
812 int ret;
813 long int value;
814
815 ret = kstrtol(buf, 0, &value);
816
817 if (ret) {
818 count = -EINVAL;
819 goto fail;
820 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800821 if (adev->powerplay.pp_funcs->set_mclk_od)
822 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
Eric Huangf2bdc052016-05-24 15:11:17 -0400823
Rex Zhu6d07fe72017-09-25 18:51:50 +0800824 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800825 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800826 } else {
Eric Huangf2bdc052016-05-24 15:11:17 -0400827 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
828 amdgpu_pm_compute_clocks(adev);
829 }
830
831fail:
832 return count;
833}
834
Alex Deucher6b2576f2018-04-19 14:38:31 -0500835/**
836 * DOC: pp_power_profile_mode
837 *
838 * The amdgpu driver provides a sysfs API for adjusting the heuristics
839 * related to switching between power levels in a power state. The file
840 * pp_power_profile_mode is used for this.
841 *
842 * Reading this file outputs a list of all of the predefined power profiles
843 * and the relevant heuristics settings for that profile.
844 *
845 * To select a profile or create a custom profile, first select manual using
846 * power_dpm_force_performance_level. Writing the number of a predefined
847 * profile to pp_power_profile_mode will enable those heuristics. To
848 * create a custom set of heuristics, write a string of numbers to the file
849 * starting with the number of the custom profile along with a setting
850 * for each heuristic parameter. Due to differences across asic families
851 * the heuristic parameters vary from family to family.
852 *
853 */
854
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800855static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
856 struct device_attribute *attr,
857 char *buf)
858{
859 struct drm_device *ddev = dev_get_drvdata(dev);
860 struct amdgpu_device *adev = ddev->dev_private;
861
862 if (adev->powerplay.pp_funcs->get_power_profile_mode)
863 return amdgpu_dpm_get_power_profile_mode(adev, buf);
864
865 return snprintf(buf, PAGE_SIZE, "\n");
866}
867
868
869static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
870 struct device_attribute *attr,
871 const char *buf,
872 size_t count)
873{
874 int ret = 0xff;
875 struct drm_device *ddev = dev_get_drvdata(dev);
876 struct amdgpu_device *adev = ddev->dev_private;
877 uint32_t parameter_size = 0;
878 long parameter[64];
879 char *sub_str, buf_cpy[128];
880 char *tmp_str;
881 uint32_t i = 0;
882 char tmp[2];
883 long int profile_mode = 0;
884 const char delimiter[3] = {' ', '\n', '\0'};
885
886 tmp[0] = *(buf);
887 tmp[1] = '\0';
888 ret = kstrtol(tmp, 0, &profile_mode);
889 if (ret)
890 goto fail;
891
892 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
893 if (count < 2 || count > 127)
894 return -EINVAL;
895 while (isspace(*++buf))
896 i++;
897 memcpy(buf_cpy, buf, count-i);
898 tmp_str = buf_cpy;
899 while (tmp_str[0]) {
900 sub_str = strsep(&tmp_str, delimiter);
901 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
902 if (ret) {
903 count = -EINVAL;
904 goto fail;
905 }
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800906 parameter_size++;
907 while (isspace(*tmp_str))
908 tmp_str++;
909 }
910 }
911 parameter[parameter_size] = profile_mode;
912 if (adev->powerplay.pp_funcs->set_power_profile_mode)
913 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
914
915 if (!ret)
916 return count;
917fail:
918 return -EINVAL;
919}
920
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
922static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
923 amdgpu_get_dpm_forced_performance_level,
924 amdgpu_set_dpm_forced_performance_level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500925static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
926static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
927static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
928 amdgpu_get_pp_force_state,
929 amdgpu_set_pp_force_state);
930static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
931 amdgpu_get_pp_table,
932 amdgpu_set_pp_table);
933static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
934 amdgpu_get_pp_dpm_sclk,
935 amdgpu_set_pp_dpm_sclk);
936static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
937 amdgpu_get_pp_dpm_mclk,
938 amdgpu_set_pp_dpm_mclk);
939static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
940 amdgpu_get_pp_dpm_pcie,
941 amdgpu_set_pp_dpm_pcie);
Eric Huang428bafa2016-05-12 14:51:21 -0400942static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
943 amdgpu_get_pp_sclk_od,
944 amdgpu_set_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -0400945static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
946 amdgpu_get_pp_mclk_od,
947 amdgpu_set_pp_mclk_od);
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800948static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
949 amdgpu_get_pp_power_profile_mode,
950 amdgpu_set_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +0800951static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
952 amdgpu_get_pp_od_clk_voltage,
953 amdgpu_set_pp_od_clk_voltage);
954
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
956 struct device_attribute *attr,
957 char *buf)
958{
959 struct amdgpu_device *adev = dev_get_drvdata(dev);
Alex Deucher0c67df42016-02-19 15:30:15 -0500960 struct drm_device *ddev = adev->ddev;
Alex Deucher71c9b9a2018-01-24 17:27:54 -0500961 int r, temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962
Alex Deucher0c67df42016-02-19 15:30:15 -0500963 /* Can't get temperature when the card is off */
964 if ((adev->flags & AMD_IS_PX) &&
965 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
966 return -EINVAL;
967
Alex Deucher71c9b9a2018-01-24 17:27:54 -0500968 /* sanity check PP is enabled */
969 if (!(adev->powerplay.pp_funcs &&
970 adev->powerplay.pp_funcs->read_sensor))
971 return -EINVAL;
972
973 /* get the temperature */
974 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
975 (void *)&temp, &size);
976 if (r)
977 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978
979 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
980}
981
982static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
983 struct device_attribute *attr,
984 char *buf)
985{
986 struct amdgpu_device *adev = dev_get_drvdata(dev);
987 int hyst = to_sensor_dev_attr(attr)->index;
988 int temp;
989
990 if (hyst)
991 temp = adev->pm.dpm.thermal.min_temp;
992 else
993 temp = adev->pm.dpm.thermal.max_temp;
994
995 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
996}
997
998static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
999 struct device_attribute *attr,
1000 char *buf)
1001{
1002 struct amdgpu_device *adev = dev_get_drvdata(dev);
1003 u32 pwm_mode = 0;
1004
Rex Zhucd4d7462017-09-06 18:43:52 +08001005 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
Rex Zhu8804b8d2015-11-10 18:29:11 -05001006 return -EINVAL;
1007
1008 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009
Rex Zhuaad22ca2017-05-05 16:56:45 +08001010 return sprintf(buf, "%i\n", pwm_mode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011}
1012
1013static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1014 struct device_attribute *attr,
1015 const char *buf,
1016 size_t count)
1017{
1018 struct amdgpu_device *adev = dev_get_drvdata(dev);
1019 int err;
1020 int value;
1021
Alex Deucher5ec36e22018-01-24 16:41:50 -05001022 /* Can't adjust fan when the card is off */
1023 if ((adev->flags & AMD_IS_PX) &&
1024 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1025 return -EINVAL;
1026
Rex Zhucd4d7462017-09-06 18:43:52 +08001027 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001028 return -EINVAL;
1029
1030 err = kstrtoint(buf, 10, &value);
1031 if (err)
1032 return err;
1033
Rex Zhuaad22ca2017-05-05 16:56:45 +08001034 amdgpu_dpm_set_fan_control_mode(adev, value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001035
1036 return count;
1037}
1038
1039static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1040 struct device_attribute *attr,
1041 char *buf)
1042{
1043 return sprintf(buf, "%i\n", 0);
1044}
1045
1046static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1047 struct device_attribute *attr,
1048 char *buf)
1049{
1050 return sprintf(buf, "%i\n", 255);
1051}
1052
1053static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1054 struct device_attribute *attr,
1055 const char *buf, size_t count)
1056{
1057 struct amdgpu_device *adev = dev_get_drvdata(dev);
1058 int err;
1059 u32 value;
1060
Alex Deucher5ec36e22018-01-24 16:41:50 -05001061 /* Can't adjust fan when the card is off */
1062 if ((adev->flags & AMD_IS_PX) &&
1063 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1064 return -EINVAL;
1065
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066 err = kstrtou32(buf, 10, &value);
1067 if (err)
1068 return err;
1069
1070 value = (value * 100) / 255;
1071
Rex Zhucd4d7462017-09-06 18:43:52 +08001072 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1073 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1074 if (err)
1075 return err;
1076 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077
1078 return count;
1079}
1080
1081static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1082 struct device_attribute *attr,
1083 char *buf)
1084{
1085 struct amdgpu_device *adev = dev_get_drvdata(dev);
1086 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001087 u32 speed = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001088
Alex Deucher5ec36e22018-01-24 16:41:50 -05001089 /* Can't adjust fan when the card is off */
1090 if ((adev->flags & AMD_IS_PX) &&
1091 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1092 return -EINVAL;
1093
Rex Zhucd4d7462017-09-06 18:43:52 +08001094 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1095 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1096 if (err)
1097 return err;
1098 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099
1100 speed = (speed * 255) / 100;
1101
1102 return sprintf(buf, "%i\n", speed);
1103}
1104
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001105static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1106 struct device_attribute *attr,
1107 char *buf)
1108{
1109 struct amdgpu_device *adev = dev_get_drvdata(dev);
1110 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001111 u32 speed = 0;
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001112
Alex Deucher5ec36e22018-01-24 16:41:50 -05001113 /* Can't adjust fan when the card is off */
1114 if ((adev->flags & AMD_IS_PX) &&
1115 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1116 return -EINVAL;
1117
Rex Zhucd4d7462017-09-06 18:43:52 +08001118 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1119 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1120 if (err)
1121 return err;
1122 }
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001123
1124 return sprintf(buf, "%i\n", speed);
1125}
1126
Alex Deucher2bd376b2018-01-24 17:19:33 -05001127static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1128 struct device_attribute *attr,
1129 char *buf)
1130{
1131 struct amdgpu_device *adev = dev_get_drvdata(dev);
1132 struct drm_device *ddev = adev->ddev;
1133 u32 vddgfx;
1134 int r, size = sizeof(vddgfx);
1135
1136 /* Can't get voltage when the card is off */
1137 if ((adev->flags & AMD_IS_PX) &&
1138 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1139 return -EINVAL;
1140
1141 /* sanity check PP is enabled */
1142 if (!(adev->powerplay.pp_funcs &&
1143 adev->powerplay.pp_funcs->read_sensor))
1144 return -EINVAL;
1145
1146 /* get the voltage */
1147 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1148 (void *)&vddgfx, &size);
1149 if (r)
1150 return r;
1151
1152 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1153}
1154
1155static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1156 struct device_attribute *attr,
1157 char *buf)
1158{
1159 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1160}
1161
1162static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1163 struct device_attribute *attr,
1164 char *buf)
1165{
1166 struct amdgpu_device *adev = dev_get_drvdata(dev);
1167 struct drm_device *ddev = adev->ddev;
1168 u32 vddnb;
1169 int r, size = sizeof(vddnb);
1170
1171 /* only APUs have vddnb */
1172 if (adev->flags & AMD_IS_APU)
1173 return -EINVAL;
1174
1175 /* Can't get voltage when the card is off */
1176 if ((adev->flags & AMD_IS_PX) &&
1177 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1178 return -EINVAL;
1179
1180 /* sanity check PP is enabled */
1181 if (!(adev->powerplay.pp_funcs &&
1182 adev->powerplay.pp_funcs->read_sensor))
1183 return -EINVAL;
1184
1185 /* get the voltage */
1186 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1187 (void *)&vddnb, &size);
1188 if (r)
1189 return r;
1190
1191 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1192}
1193
1194static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1195 struct device_attribute *attr,
1196 char *buf)
1197{
1198 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1199}
1200
Alex Deucher2976fc22018-01-24 18:34:26 -05001201static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1202 struct device_attribute *attr,
1203 char *buf)
1204{
1205 struct amdgpu_device *adev = dev_get_drvdata(dev);
1206 struct drm_device *ddev = adev->ddev;
Rex Zhu5b79d042018-04-04 15:37:35 +08001207 u32 query = 0;
1208 int r, size = sizeof(u32);
Alex Deucher2976fc22018-01-24 18:34:26 -05001209 unsigned uw;
1210
1211 /* Can't get power when the card is off */
1212 if ((adev->flags & AMD_IS_PX) &&
1213 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1214 return -EINVAL;
1215
1216 /* sanity check PP is enabled */
1217 if (!(adev->powerplay.pp_funcs &&
1218 adev->powerplay.pp_funcs->read_sensor))
1219 return -EINVAL;
1220
1221 /* get the voltage */
1222 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1223 (void *)&query, &size);
1224 if (r)
1225 return r;
1226
1227 /* convert to microwatts */
Rex Zhu5b79d042018-04-04 15:37:35 +08001228 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
Alex Deucher2976fc22018-01-24 18:34:26 -05001229
1230 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1231}
1232
Rex Zhu8d81bce2018-01-29 18:07:01 +08001233static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1234 struct device_attribute *attr,
1235 char *buf)
1236{
1237 return sprintf(buf, "%i\n", 0);
1238}
1239
1240static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1241 struct device_attribute *attr,
1242 char *buf)
1243{
1244 struct amdgpu_device *adev = dev_get_drvdata(dev);
1245 uint32_t limit = 0;
1246
1247 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1248 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1249 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1250 } else {
1251 return snprintf(buf, PAGE_SIZE, "\n");
1252 }
1253}
1254
1255static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1256 struct device_attribute *attr,
1257 char *buf)
1258{
1259 struct amdgpu_device *adev = dev_get_drvdata(dev);
1260 uint32_t limit = 0;
1261
1262 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1263 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1264 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1265 } else {
1266 return snprintf(buf, PAGE_SIZE, "\n");
1267 }
1268}
1269
1270
1271static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
1272 struct device_attribute *attr,
1273 const char *buf,
1274 size_t count)
1275{
1276 struct amdgpu_device *adev = dev_get_drvdata(dev);
1277 int err;
1278 u32 value;
1279
1280 err = kstrtou32(buf, 10, &value);
1281 if (err)
1282 return err;
1283
1284 value = value / 1000000; /* convert to Watt */
1285 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
1286 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
1287 if (err)
1288 return err;
1289 } else {
1290 return -EINVAL;
1291 }
1292
1293 return count;
1294}
1295
Alex Deucher844c5412018-03-26 12:56:56 -05001296
1297/**
1298 * DOC: hwmon
1299 *
1300 * The amdgpu driver exposes the following sensor interfaces:
Alex Deucherdc85db22018-06-01 12:28:14 -05001301 *
Alex Deucher844c5412018-03-26 12:56:56 -05001302 * - GPU temperature (via the on-die sensor)
Alex Deucherdc85db22018-06-01 12:28:14 -05001303 *
Alex Deucher844c5412018-03-26 12:56:56 -05001304 * - GPU voltage
Alex Deucherdc85db22018-06-01 12:28:14 -05001305 *
Alex Deucher844c5412018-03-26 12:56:56 -05001306 * - Northbridge voltage (APUs only)
Alex Deucherdc85db22018-06-01 12:28:14 -05001307 *
Alex Deucher844c5412018-03-26 12:56:56 -05001308 * - GPU power
Alex Deucherdc85db22018-06-01 12:28:14 -05001309 *
Alex Deucher844c5412018-03-26 12:56:56 -05001310 * - GPU fan
1311 *
1312 * hwmon interfaces for GPU temperature:
Alex Deucherdc85db22018-06-01 12:28:14 -05001313 *
Alex Deucher844c5412018-03-26 12:56:56 -05001314 * - temp1_input: the on die GPU temperature in millidegrees Celsius
Alex Deucherdc85db22018-06-01 12:28:14 -05001315 *
Alex Deucher844c5412018-03-26 12:56:56 -05001316 * - temp1_crit: temperature critical max value in millidegrees Celsius
Alex Deucherdc85db22018-06-01 12:28:14 -05001317 *
Alex Deucher844c5412018-03-26 12:56:56 -05001318 * - temp1_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
1319 *
1320 * hwmon interfaces for GPU voltage:
Alex Deucherdc85db22018-06-01 12:28:14 -05001321 *
Alex Deucher844c5412018-03-26 12:56:56 -05001322 * - in0_input: the voltage on the GPU in millivolts
Alex Deucherdc85db22018-06-01 12:28:14 -05001323 *
Alex Deucher844c5412018-03-26 12:56:56 -05001324 * - in1_input: the voltage on the Northbridge in millivolts
1325 *
1326 * hwmon interfaces for GPU power:
Alex Deucherdc85db22018-06-01 12:28:14 -05001327 *
Alex Deucher844c5412018-03-26 12:56:56 -05001328 * - power1_average: average power used by the GPU in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001329 *
Alex Deucher844c5412018-03-26 12:56:56 -05001330 * - power1_cap_min: minimum cap supported in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001331 *
Alex Deucher844c5412018-03-26 12:56:56 -05001332 * - power1_cap_max: maximum cap supported in microWatts
Alex Deucherdc85db22018-06-01 12:28:14 -05001333 *
Alex Deucher844c5412018-03-26 12:56:56 -05001334 * - power1_cap: selected power cap in microWatts
1335 *
1336 * hwmon interfaces for GPU fan:
Alex Deucherdc85db22018-06-01 12:28:14 -05001337 *
Alex Deucher844c5412018-03-26 12:56:56 -05001338 * - pwm1: pulse width modulation fan level (0-255)
Alex Deucherdc85db22018-06-01 12:28:14 -05001339 *
1340 * - 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)
1341 *
Alex Deucher844c5412018-03-26 12:56:56 -05001342 * - pwm1_min: pulse width modulation fan control minimum level (0)
Alex Deucherdc85db22018-06-01 12:28:14 -05001343 *
Alex Deucher844c5412018-03-26 12:56:56 -05001344 * - pwm1_max: pulse width modulation fan control maximum level (255)
Alex Deucherdc85db22018-06-01 12:28:14 -05001345 *
Alex Deucher844c5412018-03-26 12:56:56 -05001346 * - fan1_input: fan speed in RPM
1347 *
1348 * You can use hwmon tools like sensors to view this information on your system.
1349 *
1350 */
1351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
1353static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
1354static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
1355static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
1356static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
1357static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
1358static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001359static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
Alex Deucher2bd376b2018-01-24 17:19:33 -05001360static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
1361static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
1362static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
1363static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
Alex Deucher2976fc22018-01-24 18:34:26 -05001364static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
Rex Zhu8d81bce2018-01-29 18:07:01 +08001365static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
1366static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
1367static 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 -04001368
1369static struct attribute *hwmon_attributes[] = {
1370 &sensor_dev_attr_temp1_input.dev_attr.attr,
1371 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1372 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1373 &sensor_dev_attr_pwm1.dev_attr.attr,
1374 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1375 &sensor_dev_attr_pwm1_min.dev_attr.attr,
1376 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001377 &sensor_dev_attr_fan1_input.dev_attr.attr,
Alex Deucher2bd376b2018-01-24 17:19:33 -05001378 &sensor_dev_attr_in0_input.dev_attr.attr,
1379 &sensor_dev_attr_in0_label.dev_attr.attr,
1380 &sensor_dev_attr_in1_input.dev_attr.attr,
1381 &sensor_dev_attr_in1_label.dev_attr.attr,
Alex Deucher2976fc22018-01-24 18:34:26 -05001382 &sensor_dev_attr_power1_average.dev_attr.attr,
Rex Zhu8d81bce2018-01-29 18:07:01 +08001383 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
1384 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
1385 &sensor_dev_attr_power1_cap.dev_attr.attr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 NULL
1387};
1388
1389static umode_t hwmon_attributes_visible(struct kobject *kobj,
1390 struct attribute *attr, int index)
1391{
Geliang Tangcc29ec82016-01-13 22:48:42 +08001392 struct device *dev = kobj_to_dev(kobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 struct amdgpu_device *adev = dev_get_drvdata(dev);
1394 umode_t effective_mode = attr->mode;
1395
Rex Zhufc5a1362018-04-27 13:46:08 +08001396
1397 /* Skip fan attributes if fan is not present */
1398 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1399 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1400 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1401 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
1402 attr == &sensor_dev_attr_fan1_input.dev_attr.attr))
1403 return 0;
Alex Deucher135f9712017-11-20 17:49:53 -05001404
Rex Zhu1b5708f2015-11-10 18:25:24 -05001405 /* Skip limit attributes if DPM is not enabled */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001406 if (!adev->pm.dpm_enabled &&
1407 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
Alex Deucher27100732015-10-19 15:49:11 -04001408 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
1409 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1410 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1411 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1412 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001413 return 0;
1414
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 /* mask fan attributes if we have no bindings for this asic to expose */
Rex Zhucd4d7462017-09-06 18:43:52 +08001416 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001418 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
1420 effective_mode &= ~S_IRUGO;
1421
Rex Zhucd4d7462017-09-06 18:43:52 +08001422 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001424 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001425 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
1426 effective_mode &= ~S_IWUSR;
1427
Rex Zhu8d81bce2018-01-29 18:07:01 +08001428 if ((adev->flags & AMD_IS_APU) &&
1429 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
1430 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
1431 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
1432 return 0;
1433
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 /* hide max/min values if we can't both query and manage the fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001435 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1436 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1438 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1439 return 0;
1440
Alex Deucher0d35bc782018-01-24 17:57:19 -05001441 /* only APUs have vddnb */
1442 if (!(adev->flags & AMD_IS_APU) &&
1443 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
1444 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001445 return 0;
1446
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 return effective_mode;
1448}
1449
1450static const struct attribute_group hwmon_attrgroup = {
1451 .attrs = hwmon_attributes,
1452 .is_visible = hwmon_attributes_visible,
1453};
1454
1455static const struct attribute_group *hwmon_groups[] = {
1456 &hwmon_attrgroup,
1457 NULL
1458};
1459
1460void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1461{
1462 struct amdgpu_device *adev =
1463 container_of(work, struct amdgpu_device,
1464 pm.dpm.thermal.work);
1465 /* switch to the thermal state */
Rex Zhu3a2c7882015-08-25 15:57:43 +08001466 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001467 int temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468
1469 if (!adev->pm.dpm_enabled)
1470 return;
1471
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001472 if (adev->powerplay.pp_funcs &&
1473 adev->powerplay.pp_funcs->read_sensor &&
1474 !amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1475 (void *)&temp, &size)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 if (temp < adev->pm.dpm.thermal.min_temp)
1477 /* switch back the user state */
1478 dpm_state = adev->pm.dpm.user_state;
1479 } else {
1480 if (adev->pm.dpm.thermal.high_to_low)
1481 /* switch back the user state */
1482 dpm_state = adev->pm.dpm.user_state;
1483 }
1484 mutex_lock(&adev->pm.mutex);
1485 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1486 adev->pm.dpm.thermal_active = true;
1487 else
1488 adev->pm.dpm.thermal_active = false;
1489 adev->pm.dpm.state = dpm_state;
1490 mutex_unlock(&adev->pm.mutex);
1491
1492 amdgpu_pm_compute_clocks(adev);
1493}
1494
1495static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
Rex Zhu3a2c7882015-08-25 15:57:43 +08001496 enum amd_pm_state_type dpm_state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497{
1498 int i;
1499 struct amdgpu_ps *ps;
1500 u32 ui_class;
1501 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1502 true : false;
1503
1504 /* check if the vblank period is too short to adjust the mclk */
Rex Zhucd4d7462017-09-06 18:43:52 +08001505 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001506 if (amdgpu_dpm_vblank_too_short(adev))
1507 single_display = false;
1508 }
1509
1510 /* certain older asics have a separare 3D performance state,
1511 * so try that first if the user selected performance
1512 */
1513 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1514 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1515 /* balanced states don't exist at the moment */
1516 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1517 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1518
1519restart_search:
1520 /* Pick the best power state based on current conditions */
1521 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1522 ps = &adev->pm.dpm.ps[i];
1523 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1524 switch (dpm_state) {
1525 /* user states */
1526 case POWER_STATE_TYPE_BATTERY:
1527 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1528 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1529 if (single_display)
1530 return ps;
1531 } else
1532 return ps;
1533 }
1534 break;
1535 case POWER_STATE_TYPE_BALANCED:
1536 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1537 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1538 if (single_display)
1539 return ps;
1540 } else
1541 return ps;
1542 }
1543 break;
1544 case POWER_STATE_TYPE_PERFORMANCE:
1545 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1546 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1547 if (single_display)
1548 return ps;
1549 } else
1550 return ps;
1551 }
1552 break;
1553 /* internal states */
1554 case POWER_STATE_TYPE_INTERNAL_UVD:
1555 if (adev->pm.dpm.uvd_ps)
1556 return adev->pm.dpm.uvd_ps;
1557 else
1558 break;
1559 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1560 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1561 return ps;
1562 break;
1563 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1564 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1565 return ps;
1566 break;
1567 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1568 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1569 return ps;
1570 break;
1571 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1572 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1573 return ps;
1574 break;
1575 case POWER_STATE_TYPE_INTERNAL_BOOT:
1576 return adev->pm.dpm.boot_ps;
1577 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1578 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1579 return ps;
1580 break;
1581 case POWER_STATE_TYPE_INTERNAL_ACPI:
1582 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1583 return ps;
1584 break;
1585 case POWER_STATE_TYPE_INTERNAL_ULV:
1586 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1587 return ps;
1588 break;
1589 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1590 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1591 return ps;
1592 break;
1593 default:
1594 break;
1595 }
1596 }
1597 /* use a fallback state if we didn't match */
1598 switch (dpm_state) {
1599 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1600 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1601 goto restart_search;
1602 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1603 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1604 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1605 if (adev->pm.dpm.uvd_ps) {
1606 return adev->pm.dpm.uvd_ps;
1607 } else {
1608 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1609 goto restart_search;
1610 }
1611 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1612 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1613 goto restart_search;
1614 case POWER_STATE_TYPE_INTERNAL_ACPI:
1615 dpm_state = POWER_STATE_TYPE_BATTERY;
1616 goto restart_search;
1617 case POWER_STATE_TYPE_BATTERY:
1618 case POWER_STATE_TYPE_BALANCED:
1619 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1620 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1621 goto restart_search;
1622 default:
1623 break;
1624 }
1625
1626 return NULL;
1627}
1628
1629static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1630{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631 struct amdgpu_ps *ps;
Rex Zhu3a2c7882015-08-25 15:57:43 +08001632 enum amd_pm_state_type dpm_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001633 int ret;
Rex Zhucd4d7462017-09-06 18:43:52 +08001634 bool equal = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635
1636 /* if dpm init failed */
1637 if (!adev->pm.dpm_enabled)
1638 return;
1639
1640 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1641 /* add other state override checks here */
1642 if ((!adev->pm.dpm.thermal_active) &&
1643 (!adev->pm.dpm.uvd_active))
1644 adev->pm.dpm.state = adev->pm.dpm.user_state;
1645 }
1646 dpm_state = adev->pm.dpm.state;
1647
1648 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1649 if (ps)
1650 adev->pm.dpm.requested_ps = ps;
1651 else
1652 return;
1653
Rex Zhucd4d7462017-09-06 18:43:52 +08001654 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 printk("switching from power state:\n");
1656 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1657 printk("switching to power state:\n");
1658 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1659 }
1660
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661 /* update whether vce is active */
1662 ps->vce_active = adev->pm.dpm.vce_active;
Rex Zhucd4d7462017-09-06 18:43:52 +08001663 if (adev->powerplay.pp_funcs->display_configuration_changed)
1664 amdgpu_dpm_display_configuration_changed(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001665
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 ret = amdgpu_dpm_pre_set_power_state(adev);
1667 if (ret)
Christian Königa27de352016-01-21 11:28:53 +01001668 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669
Rex Zhucd4d7462017-09-06 18:43:52 +08001670 if (adev->powerplay.pp_funcs->check_state_equal) {
1671 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1672 equal = false;
1673 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674
Rex Zhu5e876c62016-10-14 19:23:34 +08001675 if (equal)
1676 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001677
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 amdgpu_dpm_set_power_state(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001679 amdgpu_dpm_post_set_power_state(adev);
1680
Alex Deuchereda1d1c2016-02-24 17:18:25 -05001681 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1682 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1683
Rex Zhucd4d7462017-09-06 18:43:52 +08001684 if (adev->powerplay.pp_funcs->force_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 if (adev->pm.dpm.thermal_active) {
Rex Zhue5d03ac2016-12-23 14:39:41 +08001686 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 /* force low perf level for thermal */
Rex Zhue5d03ac2016-12-23 14:39:41 +08001688 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689 /* save the user's level */
1690 adev->pm.dpm.forced_level = level;
1691 } else {
1692 /* otherwise, user selected level */
1693 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1694 }
1695 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001696}
1697
1698void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1699{
Rex Zhucd4d7462017-09-06 18:43:52 +08001700 if (adev->powerplay.pp_funcs->powergate_uvd) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001701 /* enable/disable UVD */
1702 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 amdgpu_dpm_powergate_uvd(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001704 mutex_unlock(&adev->pm.mutex);
1705 } else {
1706 if (enable) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001707 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001708 adev->pm.dpm.uvd_active = true;
1709 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001710 mutex_unlock(&adev->pm.mutex);
1711 } else {
Tom St Denise95a14a2016-07-28 09:40:07 -04001712 mutex_lock(&adev->pm.mutex);
1713 adev->pm.dpm.uvd_active = false;
1714 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001715 }
Tom St Denise95a14a2016-07-28 09:40:07 -04001716 amdgpu_pm_compute_clocks(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001717 }
1718}
1719
1720void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1721{
Rex Zhucd4d7462017-09-06 18:43:52 +08001722 if (adev->powerplay.pp_funcs->powergate_vce) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001723 /* enable/disable VCE */
1724 mutex_lock(&adev->pm.mutex);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001725 amdgpu_dpm_powergate_vce(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001726 mutex_unlock(&adev->pm.mutex);
1727 } else {
1728 if (enable) {
Sonny Jiangb7a077692015-05-28 15:47:53 -04001729 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001730 adev->pm.dpm.vce_active = true;
1731 /* XXX select vce level based on ring/task */
Rex Zhu0d8de7c2016-10-12 15:13:29 +08001732 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
Sonny Jiangb7a077692015-05-28 15:47:53 -04001733 mutex_unlock(&adev->pm.mutex);
Alex Deucher2990a1f2017-12-15 16:18:00 -05001734 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1735 AMD_CG_STATE_UNGATE);
1736 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1737 AMD_PG_STATE_UNGATE);
Rex Zhu03a5f1d2017-03-06 11:29:26 +08001738 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001739 } else {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001740 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1741 AMD_PG_STATE_GATE);
1742 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1743 AMD_CG_STATE_GATE);
Tom St Denise95a14a2016-07-28 09:40:07 -04001744 mutex_lock(&adev->pm.mutex);
1745 adev->pm.dpm.vce_active = false;
1746 mutex_unlock(&adev->pm.mutex);
Rex Zhubeeea982017-01-26 16:25:05 +08001747 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001748 }
Rex Zhubeeea982017-01-26 16:25:05 +08001749
Sonny Jiangb7a077692015-05-28 15:47:53 -04001750 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751}
1752
1753void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1754{
1755 int i;
1756
Rex Zhucd4d7462017-09-06 18:43:52 +08001757 if (adev->powerplay.pp_funcs->print_power_state == NULL)
Rex Zhu1b5708f2015-11-10 18:25:24 -05001758 return;
1759
1760 for (i = 0; i < adev->pm.dpm.num_ps; i++)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001761 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001762
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763}
1764
1765int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1766{
1767 int ret;
1768
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001769 if (adev->pm.sysfs_initialized)
1770 return 0;
1771
Rex Zhud2f52ac2017-09-22 17:47:27 +08001772 if (adev->pm.dpm_enabled == 0)
1773 return 0;
1774
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1776 DRIVER_NAME, adev,
1777 hwmon_groups);
1778 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1779 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1780 dev_err(adev->dev,
1781 "Unable to register hwmon device: %d\n", ret);
1782 return ret;
1783 }
1784
1785 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1786 if (ret) {
1787 DRM_ERROR("failed to create device file for dpm state\n");
1788 return ret;
1789 }
1790 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1791 if (ret) {
1792 DRM_ERROR("failed to create device file for dpm state\n");
1793 return ret;
1794 }
Eric Huangf3898ea2015-12-11 16:24:34 -05001795
Rex Zhu6d07fe72017-09-25 18:51:50 +08001796
1797 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1798 if (ret) {
1799 DRM_ERROR("failed to create device file pp_num_states\n");
1800 return ret;
1801 }
1802 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1803 if (ret) {
1804 DRM_ERROR("failed to create device file pp_cur_state\n");
1805 return ret;
1806 }
1807 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1808 if (ret) {
1809 DRM_ERROR("failed to create device file pp_force_state\n");
1810 return ret;
1811 }
1812 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1813 if (ret) {
1814 DRM_ERROR("failed to create device file pp_table\n");
1815 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -05001816 }
Eric Huangc85e2992016-05-19 15:41:25 -04001817
1818 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1819 if (ret) {
1820 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1821 return ret;
1822 }
1823 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1824 if (ret) {
1825 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1826 return ret;
1827 }
1828 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1829 if (ret) {
1830 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1831 return ret;
1832 }
Eric Huang8b2e5742016-05-19 15:46:10 -04001833 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1834 if (ret) {
1835 DRM_ERROR("failed to create device file pp_sclk_od\n");
1836 return ret;
1837 }
Eric Huangf2bdc052016-05-24 15:11:17 -04001838 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1839 if (ret) {
1840 DRM_ERROR("failed to create device file pp_mclk_od\n");
1841 return ret;
1842 }
Eric Huang34bb2732016-09-12 16:17:44 -04001843 ret = device_create_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08001844 &dev_attr_pp_power_profile_mode);
1845 if (ret) {
1846 DRM_ERROR("failed to create device file "
1847 "pp_power_profile_mode\n");
1848 return ret;
1849 }
Rex Zhue3933f22018-01-16 18:35:15 +08001850 ret = device_create_file(adev->dev,
1851 &dev_attr_pp_od_clk_voltage);
1852 if (ret) {
1853 DRM_ERROR("failed to create device file "
1854 "pp_od_clk_voltage\n");
1855 return ret;
1856 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857 ret = amdgpu_debugfs_pm_init(adev);
1858 if (ret) {
1859 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1860 return ret;
1861 }
1862
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001863 adev->pm.sysfs_initialized = true;
1864
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001865 return 0;
1866}
1867
1868void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1869{
Rex Zhud2f52ac2017-09-22 17:47:27 +08001870 if (adev->pm.dpm_enabled == 0)
1871 return;
1872
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873 if (adev->pm.int_hwmon_dev)
1874 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1875 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1876 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
Rex Zhu6d07fe72017-09-25 18:51:50 +08001877
1878 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1879 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1880 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1881 device_remove_file(adev->dev, &dev_attr_pp_table);
1882
Eric Huangc85e2992016-05-19 15:41:25 -04001883 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1884 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1885 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
Eric Huang8b2e5742016-05-19 15:46:10 -04001886 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -04001887 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
Eric Huang34bb2732016-09-12 16:17:44 -04001888 device_remove_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08001889 &dev_attr_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +08001890 device_remove_file(adev->dev,
1891 &dev_attr_pp_od_clk_voltage);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892}
1893
1894void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1895{
Rex Zhu5e876c62016-10-14 19:23:34 +08001896 int i = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001897
1898 if (!adev->pm.dpm_enabled)
1899 return;
1900
Alex Deucherc10c8f72017-02-10 18:09:32 -05001901 if (adev->mode_info.num_crtc)
1902 amdgpu_display_bandwidth_update(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001903
1904 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1905 struct amdgpu_ring *ring = adev->rings[i];
1906 if (ring && ring->ready)
1907 amdgpu_fence_wait_empty(ring);
1908 }
1909
Rex Zhu6d07fe72017-09-25 18:51:50 +08001910 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhu51d45cb2018-04-27 14:09:30 +08001911 if (!amdgpu_device_has_dc_support(adev)) {
1912 mutex_lock(&adev->pm.mutex);
1913 amdgpu_dpm_get_active_displays(adev);
1914 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtcs;
1915 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
1916 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
1917 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
1918 if (adev->pm.pm_display_cfg.vrefresh > 120)
1919 adev->pm.pm_display_cfg.min_vblank_time = 0;
1920 if (adev->powerplay.pp_funcs->display_configuration_change)
1921 adev->powerplay.pp_funcs->display_configuration_change(
1922 adev->powerplay.pp_handle,
1923 &adev->pm.pm_display_cfg);
1924 mutex_unlock(&adev->pm.mutex);
1925 }
Evan Quan39199b82017-12-29 14:46:13 +08001926 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001927 } else {
1928 mutex_lock(&adev->pm.mutex);
Rex Zhu51d45cb2018-04-27 14:09:30 +08001929 amdgpu_dpm_get_active_displays(adev);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001930 /* update battery/ac status */
1931 if (power_supply_is_system_supplied() > 0)
1932 adev->pm.dpm.ac_power = true;
1933 else
1934 adev->pm.dpm.ac_power = false;
1935
1936 amdgpu_dpm_change_power_state_locked(adev);
1937
1938 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940}
1941
1942/*
1943 * Debugfs info
1944 */
1945#if defined(CONFIG_DEBUG_FS)
1946
Tom St Denis3de4ec52016-09-19 12:48:52 -04001947static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1948{
Eric Huangcd7b0c62017-02-07 16:37:48 -05001949 uint32_t value;
Rex Zhu5b79d042018-04-04 15:37:35 +08001950 uint32_t query = 0;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001951 int size;
Tom St Denis3de4ec52016-09-19 12:48:52 -04001952
1953 /* sanity check PP is enabled */
1954 if (!(adev->powerplay.pp_funcs &&
1955 adev->powerplay.pp_funcs->read_sensor))
1956 return -EINVAL;
1957
1958 /* GPU Clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001959 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001960 seq_printf(m, "GFX Clocks and Power:\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001961 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001962 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001963 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001964 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
Rex Zhu5ed8d652018-01-08 13:59:05 +08001965 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
1966 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
1967 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
1968 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001969 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001970 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001971 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001972 seq_printf(m, "\t%u mV (VDDNB)\n", value);
Rex Zhu5b79d042018-04-04 15:37:35 +08001973 size = sizeof(uint32_t);
1974 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
1975 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001976 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001977 seq_printf(m, "\n");
1978
1979 /* GPU Temp */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001980 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001981 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1982
1983 /* GPU Load */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001984 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001985 seq_printf(m, "GPU Load: %u %%\n", value);
1986 seq_printf(m, "\n");
1987
1988 /* UVD clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001989 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04001990 if (!value) {
1991 seq_printf(m, "UVD: Disabled\n");
1992 } else {
1993 seq_printf(m, "UVD: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001994 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001995 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001996 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001997 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1998 }
1999 }
2000 seq_printf(m, "\n");
2001
2002 /* VCE clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002003 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04002004 if (!value) {
2005 seq_printf(m, "VCE: Disabled\n");
2006 } else {
2007 seq_printf(m, "VCE: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05002008 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04002009 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
2010 }
2011 }
2012
2013 return 0;
2014}
2015
Huang Ruia8503b12017-01-05 19:17:13 +08002016static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
2017{
2018 int i;
2019
2020 for (i = 0; clocks[i].flag; i++)
2021 seq_printf(m, "\t%s: %s\n", clocks[i].name,
2022 (flags & clocks[i].flag) ? "On" : "Off");
2023}
2024
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
2026{
2027 struct drm_info_node *node = (struct drm_info_node *) m->private;
2028 struct drm_device *dev = node->minor->dev;
2029 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher0c67df42016-02-19 15:30:15 -05002030 struct drm_device *ddev = adev->ddev;
Huang Rui6cb2d4e2017-01-05 18:44:41 +08002031 u32 flags = 0;
2032
Alex Deucher2990a1f2017-12-15 16:18:00 -05002033 amdgpu_device_ip_get_clockgating_state(adev, &flags);
Huang Rui6cb2d4e2017-01-05 18:44:41 +08002034 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
Huang Ruia8503b12017-01-05 19:17:13 +08002035 amdgpu_parse_cg_state(m, flags);
2036 seq_printf(m, "\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037
Rex Zhu1b5708f2015-11-10 18:25:24 -05002038 if (!adev->pm.dpm_enabled) {
2039 seq_printf(m, "dpm not enabled\n");
2040 return 0;
2041 }
Alex Deucher0c67df42016-02-19 15:30:15 -05002042 if ((adev->flags & AMD_IS_PX) &&
2043 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
2044 seq_printf(m, "PX asic powered off\n");
Rex Zhu6d07fe72017-09-25 18:51:50 +08002045 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046 mutex_lock(&adev->pm.mutex);
Rex Zhucd4d7462017-09-06 18:43:52 +08002047 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
2048 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049 else
2050 seq_printf(m, "Debugfs support not implemented for this asic\n");
2051 mutex_unlock(&adev->pm.mutex);
Rex Zhu6d07fe72017-09-25 18:51:50 +08002052 } else {
2053 return amdgpu_debugfs_pm_info_pp(m, adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002054 }
2055
2056 return 0;
2057}
2058
Nils Wallménius06ab6832016-05-02 12:46:15 -04002059static const struct drm_info_list amdgpu_pm_info_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002060 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
2061};
2062#endif
2063
2064static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
2065{
2066#if defined(CONFIG_DEBUG_FS)
2067 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
2068#else
2069 return 0;
2070#endif
2071}