blob: 27d8dd77860dd3a9b29a5c2aa91fc6fdb8986f46 [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 *
83 * This is a legacy interface and is only provided for backwards compatibility.
84 * The amdgpu driver provides a sysfs API for adjusting certain power
85 * related parameters. The file power_dpm_state is used for this.
86 * It accepts the following arguments:
87 * - battery
88 * - balanced
89 * - performance
90 *
91 * battery
92 *
93 * On older GPUs, the vbios provided a special power state for battery
94 * operation. Selecting battery switched to this state. This is no
95 * longer provided on newer GPUs so the option does nothing in that case.
96 *
97 * balanced
98 *
99 * On older GPUs, the vbios provided a special power state for balanced
100 * operation. Selecting balanced switched to this state. This is no
101 * longer provided on newer GPUs so the option does nothing in that case.
102 *
103 * performance
104 *
105 * On older GPUs, the vbios provided a special power state for performance
106 * operation. Selecting performance switched to this state. This is no
107 * longer provided on newer GPUs so the option does nothing in that case.
108 *
109 */
110
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111static ssize_t amdgpu_get_dpm_state(struct device *dev,
112 struct device_attribute *attr,
113 char *buf)
114{
115 struct drm_device *ddev = dev_get_drvdata(dev);
116 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500117 enum amd_pm_state_type pm;
118
Rex Zhucd4d7462017-09-06 18:43:52 +0800119 if (adev->powerplay.pp_funcs->get_current_power_state)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500120 pm = amdgpu_dpm_get_current_power_state(adev);
Rex Zhucd4d7462017-09-06 18:43:52 +0800121 else
Rex Zhu1b5708f2015-11-10 18:25:24 -0500122 pm = adev->pm.dpm.user_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
124 return snprintf(buf, PAGE_SIZE, "%s\n",
125 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
126 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
127}
128
129static ssize_t amdgpu_set_dpm_state(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf,
132 size_t count)
133{
134 struct drm_device *ddev = dev_get_drvdata(dev);
135 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500136 enum amd_pm_state_type state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 if (strncmp("battery", buf, strlen("battery")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500139 state = POWER_STATE_TYPE_BATTERY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500141 state = POWER_STATE_TYPE_BALANCED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 else if (strncmp("performance", buf, strlen("performance")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500143 state = POWER_STATE_TYPE_PERFORMANCE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 count = -EINVAL;
146 goto fail;
147 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148
Rex Zhu6d07fe72017-09-25 18:51:50 +0800149 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800150 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
Rex Zhu1b5708f2015-11-10 18:25:24 -0500151 } else {
152 mutex_lock(&adev->pm.mutex);
153 adev->pm.dpm.user_state = state;
154 mutex_unlock(&adev->pm.mutex);
155
156 /* Can't set dpm state when the card is off */
157 if (!(adev->flags & AMD_IS_PX) ||
158 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
159 amdgpu_pm_compute_clocks(adev);
160 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161fail:
162 return count;
163}
164
Alex Deucher8567f682018-04-19 13:46:03 -0500165
166/**
167 * DOC: power_dpm_force_performance_level
168 *
169 * The amdgpu driver provides a sysfs API for adjusting certain power
170 * related parameters. The file power_dpm_force_performance_level is
171 * used for this. It accepts the following arguments:
172 * - auto
173 * - low
174 * - high
175 * - manual
176 * - GPU fan
177 * - profile_standard
178 * - profile_min_sclk
179 * - profile_min_mclk
180 * - profile_peak
181 *
182 * auto
183 *
184 * When auto is selected, the driver will attempt to dynamically select
185 * the optimal power profile for current conditions in the driver.
186 *
187 * low
188 *
189 * When low is selected, the clocks are forced to the lowest power state.
190 *
191 * high
192 *
193 * When high is selected, the clocks are forced to the highest power state.
194 *
195 * manual
196 *
197 * When manual is selected, the user can manually adjust which power states
198 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
199 * and pp_dpm_pcie files and adjust the power state transition heuristics
200 * via the pp_power_profile_mode sysfs file.
201 *
202 * profile_standard
203 * profile_min_sclk
204 * profile_min_mclk
205 * profile_peak
206 *
207 * When the profiling modes are selected, clock and power gating are
208 * disabled and the clocks are set for different profiling cases. This
209 * mode is recommended for profiling specific work loads where you do
210 * not want clock or power gating for clock fluctuation to interfere
211 * with your results. profile_standard sets the clocks to a fixed clock
212 * level which varies from asic to asic. profile_min_sclk forces the sclk
213 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
214 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
215 *
216 */
217
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400218static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
Rex Zhu1b5708f2015-11-10 18:25:24 -0500219 struct device_attribute *attr,
220 char *buf)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221{
222 struct drm_device *ddev = dev_get_drvdata(dev);
223 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhucd4d7462017-09-06 18:43:52 +0800224 enum amd_dpm_forced_level level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225
Alex Deucher0c67df42016-02-19 15:30:15 -0500226 if ((adev->flags & AMD_IS_PX) &&
227 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
228 return snprintf(buf, PAGE_SIZE, "off\n");
229
Rex Zhucd4d7462017-09-06 18:43:52 +0800230 if (adev->powerplay.pp_funcs->get_performance_level)
231 level = amdgpu_dpm_get_performance_level(adev);
232 else
233 level = adev->pm.dpm.forced_level;
234
Rex Zhue5d03ac2016-12-23 14:39:41 +0800235 return snprintf(buf, PAGE_SIZE, "%s\n",
Rex Zhu570272d2017-01-06 13:32:49 +0800236 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
237 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
238 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
239 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
240 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
241 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
242 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
243 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
244 "unknown");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400245}
246
247static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
248 struct device_attribute *attr,
249 const char *buf,
250 size_t count)
251{
252 struct drm_device *ddev = dev_get_drvdata(dev);
253 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhue5d03ac2016-12-23 14:39:41 +0800254 enum amd_dpm_forced_level level;
Rex Zhucd4d7462017-09-06 18:43:52 +0800255 enum amd_dpm_forced_level current_level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256 int ret = 0;
257
Alex Deucher0c67df42016-02-19 15:30:15 -0500258 /* Can't force performance level when the card is off */
259 if ((adev->flags & AMD_IS_PX) &&
260 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
261 return -EINVAL;
262
Rex Zhucd4d7462017-09-06 18:43:52 +0800263 if (adev->powerplay.pp_funcs->get_performance_level)
264 current_level = amdgpu_dpm_get_performance_level(adev);
Rex Zhu3bd58972016-12-23 15:24:37 +0800265
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 if (strncmp("low", buf, strlen("low")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800267 level = AMD_DPM_FORCED_LEVEL_LOW;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 } else if (strncmp("high", buf, strlen("high")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800269 level = AMD_DPM_FORCED_LEVEL_HIGH;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800271 level = AMD_DPM_FORCED_LEVEL_AUTO;
Eric Huangf3898ea2015-12-11 16:24:34 -0500272 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800273 level = AMD_DPM_FORCED_LEVEL_MANUAL;
Rex Zhu570272d2017-01-06 13:32:49 +0800274 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
275 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
276 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
277 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
278 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
279 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
280 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
281 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
282 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
283 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
284 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285 count = -EINVAL;
286 goto fail;
287 }
Rex Zhu1b5708f2015-11-10 18:25:24 -0500288
Rex Zhu3bd58972016-12-23 15:24:37 +0800289 if (current_level == level)
Rex Zhu8e7afd32017-01-09 15:18:01 +0800290 return count;
Rex Zhu3bd58972016-12-23 15:24:37 +0800291
Rex Zhucd4d7462017-09-06 18:43:52 +0800292 if (adev->powerplay.pp_funcs->force_performance_level) {
Rex Zhu1b5708f2015-11-10 18:25:24 -0500293 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294 if (adev->pm.dpm.thermal_active) {
295 count = -EINVAL;
Alex Deucher10f950f2016-02-19 15:18:45 -0500296 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400297 goto fail;
298 }
299 ret = amdgpu_dpm_force_performance_level(adev, level);
300 if (ret)
301 count = -EINVAL;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500302 else
303 adev->pm.dpm.forced_level = level;
304 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 }
Rex Zhu570272d2017-01-06 13:32:49 +0800306
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307fail:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308 return count;
309}
310
Eric Huangf3898ea2015-12-11 16:24:34 -0500311static ssize_t amdgpu_get_pp_num_states(struct device *dev,
312 struct device_attribute *attr,
313 char *buf)
314{
315 struct drm_device *ddev = dev_get_drvdata(dev);
316 struct amdgpu_device *adev = ddev->dev_private;
317 struct pp_states_info data;
318 int i, buf_len;
319
Rex Zhucd4d7462017-09-06 18:43:52 +0800320 if (adev->powerplay.pp_funcs->get_pp_num_states)
Eric Huangf3898ea2015-12-11 16:24:34 -0500321 amdgpu_dpm_get_pp_num_states(adev, &data);
322
323 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
324 for (i = 0; i < data.nums; i++)
325 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
326 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
327 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
328 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
329 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
330
331 return buf_len;
332}
333
334static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
335 struct device_attribute *attr,
336 char *buf)
337{
338 struct drm_device *ddev = dev_get_drvdata(dev);
339 struct amdgpu_device *adev = ddev->dev_private;
340 struct pp_states_info data;
341 enum amd_pm_state_type pm = 0;
342 int i = 0;
343
Rex Zhucd4d7462017-09-06 18:43:52 +0800344 if (adev->powerplay.pp_funcs->get_current_power_state
345 && adev->powerplay.pp_funcs->get_pp_num_states) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500346 pm = amdgpu_dpm_get_current_power_state(adev);
347 amdgpu_dpm_get_pp_num_states(adev, &data);
348
349 for (i = 0; i < data.nums; i++) {
350 if (pm == data.states[i])
351 break;
352 }
353
354 if (i == data.nums)
355 i = -EINVAL;
356 }
357
358 return snprintf(buf, PAGE_SIZE, "%d\n", i);
359}
360
361static ssize_t amdgpu_get_pp_force_state(struct device *dev,
362 struct device_attribute *attr,
363 char *buf)
364{
365 struct drm_device *ddev = dev_get_drvdata(dev);
366 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500367
Rex Zhucd4d7462017-09-06 18:43:52 +0800368 if (adev->pp_force_state_enabled)
369 return amdgpu_get_pp_cur_state(dev, attr, buf);
370 else
Eric Huangf3898ea2015-12-11 16:24:34 -0500371 return snprintf(buf, PAGE_SIZE, "\n");
372}
373
374static ssize_t amdgpu_set_pp_force_state(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf,
377 size_t count)
378{
379 struct drm_device *ddev = dev_get_drvdata(dev);
380 struct amdgpu_device *adev = ddev->dev_private;
381 enum amd_pm_state_type state = 0;
Dan Carpenter041bf022016-06-16 11:30:23 +0300382 unsigned long idx;
Eric Huangf3898ea2015-12-11 16:24:34 -0500383 int ret;
384
385 if (strlen(buf) == 1)
386 adev->pp_force_state_enabled = false;
Rex Zhu6d07fe72017-09-25 18:51:50 +0800387 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
388 adev->powerplay.pp_funcs->get_pp_num_states) {
Dan Carpenter041bf022016-06-16 11:30:23 +0300389 struct pp_states_info data;
Eric Huangf3898ea2015-12-11 16:24:34 -0500390
Dan Carpenter041bf022016-06-16 11:30:23 +0300391 ret = kstrtoul(buf, 0, &idx);
392 if (ret || idx >= ARRAY_SIZE(data.states)) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500393 count = -EINVAL;
394 goto fail;
395 }
396
Dan Carpenter041bf022016-06-16 11:30:23 +0300397 amdgpu_dpm_get_pp_num_states(adev, &data);
398 state = data.states[idx];
399 /* only set user selected power states */
400 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
401 state != POWER_STATE_TYPE_DEFAULT) {
402 amdgpu_dpm_dispatch_task(adev,
Evan Quan39199b82017-12-29 14:46:13 +0800403 AMD_PP_TASK_ENABLE_USER_STATE, &state);
Dan Carpenter041bf022016-06-16 11:30:23 +0300404 adev->pp_force_state_enabled = true;
Eric Huangf3898ea2015-12-11 16:24:34 -0500405 }
406 }
407fail:
408 return count;
409}
410
Alex Deucherd54bb402018-04-19 14:02:52 -0500411/**
412 * DOC: pp_table
413 *
414 * The amdgpu driver provides a sysfs API for uploading new powerplay
415 * tables. The file pp_table is used for this. Reading the file
416 * will dump the current power play table. Writing to the file
417 * will attempt to upload a new powerplay table and re-initialize
418 * powerplay using that new table.
419 *
420 */
421
Eric Huangf3898ea2015-12-11 16:24:34 -0500422static ssize_t amdgpu_get_pp_table(struct device *dev,
423 struct device_attribute *attr,
424 char *buf)
425{
426 struct drm_device *ddev = dev_get_drvdata(dev);
427 struct amdgpu_device *adev = ddev->dev_private;
428 char *table = NULL;
Eric Huang1684d3b2016-07-28 17:25:01 -0400429 int size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500430
Rex Zhucd4d7462017-09-06 18:43:52 +0800431 if (adev->powerplay.pp_funcs->get_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500432 size = amdgpu_dpm_get_pp_table(adev, &table);
433 else
434 return 0;
435
436 if (size >= PAGE_SIZE)
437 size = PAGE_SIZE - 1;
438
Eric Huang1684d3b2016-07-28 17:25:01 -0400439 memcpy(buf, table, size);
Eric Huangf3898ea2015-12-11 16:24:34 -0500440
441 return size;
442}
443
444static ssize_t amdgpu_set_pp_table(struct device *dev,
445 struct device_attribute *attr,
446 const char *buf,
447 size_t count)
448{
449 struct drm_device *ddev = dev_get_drvdata(dev);
450 struct amdgpu_device *adev = ddev->dev_private;
451
Rex Zhucd4d7462017-09-06 18:43:52 +0800452 if (adev->powerplay.pp_funcs->set_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500453 amdgpu_dpm_set_pp_table(adev, buf, count);
454
455 return count;
456}
457
Alex Deucher4e418c32018-04-19 14:59:55 -0500458/**
459 * DOC: pp_od_clk_voltage
460 *
461 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
462 * in each power level within a power state. The pp_od_clk_voltage is used for
463 * this.
464 *
465 * Reading the file will display:
466 * - a list of engine clock levels and voltages labeled OD_SCLK
467 * - a list of memory clock levels and voltages labeled OD_MCLK
468 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
469 *
470 * To manually adjust these settings, first select manual using
471 * power_dpm_force_performance_level. Enter a new value for each
472 * level by writing a string that contains "s/m level clock voltage" to
473 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
474 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
475 * 810 mV. When you have edited all of the states as needed, write
476 * "c" (commit) to the file to commit your changes. If you want to reset to the
477 * default power levels, write "r" (reset) to the file to reset them.
478 *
479 */
480
Rex Zhue3933f22018-01-16 18:35:15 +0800481static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf,
484 size_t count)
485{
486 struct drm_device *ddev = dev_get_drvdata(dev);
487 struct amdgpu_device *adev = ddev->dev_private;
488 int ret;
489 uint32_t parameter_size = 0;
490 long parameter[64];
491 char buf_cpy[128];
492 char *tmp_str;
493 char *sub_str;
494 const char delimiter[3] = {' ', '\n', '\0'};
495 uint32_t type;
496
497 if (count > 127)
498 return -EINVAL;
499
500 if (*buf == 's')
501 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
502 else if (*buf == 'm')
503 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
504 else if(*buf == 'r')
505 type = PP_OD_RESTORE_DEFAULT_TABLE;
506 else if (*buf == 'c')
507 type = PP_OD_COMMIT_DPM_TABLE;
508 else
509 return -EINVAL;
510
511 memcpy(buf_cpy, buf, count+1);
512
513 tmp_str = buf_cpy;
514
515 while (isspace(*++tmp_str));
516
517 while (tmp_str[0]) {
518 sub_str = strsep(&tmp_str, delimiter);
519 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
520 if (ret)
521 return -EINVAL;
522 parameter_size++;
523
524 while (isspace(*tmp_str))
525 tmp_str++;
526 }
527
528 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
529 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
530 parameter, parameter_size);
531
532 if (ret)
533 return -EINVAL;
534
535 if (type == PP_OD_COMMIT_DPM_TABLE) {
536 if (adev->powerplay.pp_funcs->dispatch_tasks) {
537 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
538 return count;
539 } else {
540 return -EINVAL;
541 }
542 }
543
544 return count;
545}
546
547static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
548 struct device_attribute *attr,
549 char *buf)
550{
551 struct drm_device *ddev = dev_get_drvdata(dev);
552 struct amdgpu_device *adev = ddev->dev_private;
553 uint32_t size = 0;
554
555 if (adev->powerplay.pp_funcs->print_clock_levels) {
556 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
557 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
Rex Zhua3c991f2018-04-19 10:39:17 +0800558 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
Rex Zhue3933f22018-01-16 18:35:15 +0800559 return size;
560 } else {
561 return snprintf(buf, PAGE_SIZE, "\n");
562 }
563
564}
565
Alex Deucher271dc902018-04-19 14:22:24 -0500566/**
567 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_pcie
568 *
569 * The amdgpu driver provides a sysfs API for adjusting what power levels
570 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
571 * and pp_dpm_pcie are used for this.
572 *
573 * Reading back the files will show you the available power levels within
574 * the power state and the clock information for those levels.
575 *
576 * To manually adjust these states, first select manual using
577 * power_dpm_force_performance_level. Writing a string of the level
578 * numbers to the file will select which levels you want to enable.
579 * E.g., writing 456 to the file will enable levels 4, 5, and 6.
580 *
581 */
582
Eric Huangf3898ea2015-12-11 16:24:34 -0500583static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
584 struct device_attribute *attr,
585 char *buf)
586{
587 struct drm_device *ddev = dev_get_drvdata(dev);
588 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500589
Rex Zhucd4d7462017-09-06 18:43:52 +0800590 if (adev->powerplay.pp_funcs->print_clock_levels)
591 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
592 else
593 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500594}
595
596static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
597 struct device_attribute *attr,
598 const char *buf,
599 size_t count)
600{
601 struct drm_device *ddev = dev_get_drvdata(dev);
602 struct amdgpu_device *adev = ddev->dev_private;
603 int ret;
604 long level;
Eric Huang56327082016-04-12 14:57:23 -0400605 uint32_t i, mask = 0;
606 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500607
Eric Huang14b33072016-06-14 15:08:22 -0400608 for (i = 0; i < strlen(buf); i++) {
609 if (*(buf + i) == '\n')
610 continue;
Eric Huang56327082016-04-12 14:57:23 -0400611 sub_str[0] = *(buf + i);
612 sub_str[1] = '\0';
613 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500614
Eric Huang56327082016-04-12 14:57:23 -0400615 if (ret) {
616 count = -EINVAL;
617 goto fail;
618 }
619 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500620 }
621
Rex Zhucd4d7462017-09-06 18:43:52 +0800622 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400623 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800624
Eric Huangf3898ea2015-12-11 16:24:34 -0500625fail:
626 return count;
627}
628
629static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
630 struct device_attribute *attr,
631 char *buf)
632{
633 struct drm_device *ddev = dev_get_drvdata(dev);
634 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500635
Rex Zhucd4d7462017-09-06 18:43:52 +0800636 if (adev->powerplay.pp_funcs->print_clock_levels)
637 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
638 else
639 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500640}
641
642static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
643 struct device_attribute *attr,
644 const char *buf,
645 size_t count)
646{
647 struct drm_device *ddev = dev_get_drvdata(dev);
648 struct amdgpu_device *adev = ddev->dev_private;
649 int ret;
650 long level;
Eric Huang56327082016-04-12 14:57:23 -0400651 uint32_t i, mask = 0;
652 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500653
Eric Huang14b33072016-06-14 15:08:22 -0400654 for (i = 0; i < strlen(buf); i++) {
655 if (*(buf + i) == '\n')
656 continue;
Eric Huang56327082016-04-12 14:57:23 -0400657 sub_str[0] = *(buf + i);
658 sub_str[1] = '\0';
659 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500660
Eric Huang56327082016-04-12 14:57:23 -0400661 if (ret) {
662 count = -EINVAL;
663 goto fail;
664 }
665 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500666 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800667 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400668 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800669
Eric Huangf3898ea2015-12-11 16:24:34 -0500670fail:
671 return count;
672}
673
674static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
675 struct device_attribute *attr,
676 char *buf)
677{
678 struct drm_device *ddev = dev_get_drvdata(dev);
679 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500680
Rex Zhucd4d7462017-09-06 18:43:52 +0800681 if (adev->powerplay.pp_funcs->print_clock_levels)
682 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
683 else
684 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500685}
686
687static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
688 struct device_attribute *attr,
689 const char *buf,
690 size_t count)
691{
692 struct drm_device *ddev = dev_get_drvdata(dev);
693 struct amdgpu_device *adev = ddev->dev_private;
694 int ret;
695 long level;
Eric Huang56327082016-04-12 14:57:23 -0400696 uint32_t i, mask = 0;
697 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500698
Eric Huang14b33072016-06-14 15:08:22 -0400699 for (i = 0; i < strlen(buf); i++) {
700 if (*(buf + i) == '\n')
701 continue;
Eric Huang56327082016-04-12 14:57:23 -0400702 sub_str[0] = *(buf + i);
703 sub_str[1] = '\0';
704 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500705
Eric Huang56327082016-04-12 14:57:23 -0400706 if (ret) {
707 count = -EINVAL;
708 goto fail;
709 }
710 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500711 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800712 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400713 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800714
Eric Huangf3898ea2015-12-11 16:24:34 -0500715fail:
716 return count;
717}
718
Eric Huang428bafa2016-05-12 14:51:21 -0400719static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
720 struct device_attribute *attr,
721 char *buf)
722{
723 struct drm_device *ddev = dev_get_drvdata(dev);
724 struct amdgpu_device *adev = ddev->dev_private;
725 uint32_t value = 0;
726
Rex Zhucd4d7462017-09-06 18:43:52 +0800727 if (adev->powerplay.pp_funcs->get_sclk_od)
Eric Huang428bafa2016-05-12 14:51:21 -0400728 value = amdgpu_dpm_get_sclk_od(adev);
729
730 return snprintf(buf, PAGE_SIZE, "%d\n", value);
731}
732
733static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
734 struct device_attribute *attr,
735 const char *buf,
736 size_t count)
737{
738 struct drm_device *ddev = dev_get_drvdata(dev);
739 struct amdgpu_device *adev = ddev->dev_private;
740 int ret;
741 long int value;
742
743 ret = kstrtol(buf, 0, &value);
744
745 if (ret) {
746 count = -EINVAL;
747 goto fail;
748 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800749 if (adev->powerplay.pp_funcs->set_sclk_od)
750 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
Eric Huang428bafa2016-05-12 14:51:21 -0400751
Rex Zhu6d07fe72017-09-25 18:51:50 +0800752 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800753 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800754 } else {
Eric Huang8b2e5742016-05-19 15:46:10 -0400755 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
756 amdgpu_pm_compute_clocks(adev);
757 }
Eric Huang428bafa2016-05-12 14:51:21 -0400758
759fail:
760 return count;
761}
762
Eric Huangf2bdc052016-05-24 15:11:17 -0400763static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
764 struct device_attribute *attr,
765 char *buf)
766{
767 struct drm_device *ddev = dev_get_drvdata(dev);
768 struct amdgpu_device *adev = ddev->dev_private;
769 uint32_t value = 0;
770
Rex Zhucd4d7462017-09-06 18:43:52 +0800771 if (adev->powerplay.pp_funcs->get_mclk_od)
Eric Huangf2bdc052016-05-24 15:11:17 -0400772 value = amdgpu_dpm_get_mclk_od(adev);
Eric Huangf2bdc052016-05-24 15:11:17 -0400773
774 return snprintf(buf, PAGE_SIZE, "%d\n", value);
775}
776
777static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
778 struct device_attribute *attr,
779 const char *buf,
780 size_t count)
781{
782 struct drm_device *ddev = dev_get_drvdata(dev);
783 struct amdgpu_device *adev = ddev->dev_private;
784 int ret;
785 long int value;
786
787 ret = kstrtol(buf, 0, &value);
788
789 if (ret) {
790 count = -EINVAL;
791 goto fail;
792 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800793 if (adev->powerplay.pp_funcs->set_mclk_od)
794 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
Eric Huangf2bdc052016-05-24 15:11:17 -0400795
Rex Zhu6d07fe72017-09-25 18:51:50 +0800796 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +0800797 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800798 } else {
Eric Huangf2bdc052016-05-24 15:11:17 -0400799 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
800 amdgpu_pm_compute_clocks(adev);
801 }
802
803fail:
804 return count;
805}
806
Alex Deucher6b2576f2018-04-19 14:38:31 -0500807/**
808 * DOC: pp_power_profile_mode
809 *
810 * The amdgpu driver provides a sysfs API for adjusting the heuristics
811 * related to switching between power levels in a power state. The file
812 * pp_power_profile_mode is used for this.
813 *
814 * Reading this file outputs a list of all of the predefined power profiles
815 * and the relevant heuristics settings for that profile.
816 *
817 * To select a profile or create a custom profile, first select manual using
818 * power_dpm_force_performance_level. Writing the number of a predefined
819 * profile to pp_power_profile_mode will enable those heuristics. To
820 * create a custom set of heuristics, write a string of numbers to the file
821 * starting with the number of the custom profile along with a setting
822 * for each heuristic parameter. Due to differences across asic families
823 * the heuristic parameters vary from family to family.
824 *
825 */
826
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800827static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
828 struct device_attribute *attr,
829 char *buf)
830{
831 struct drm_device *ddev = dev_get_drvdata(dev);
832 struct amdgpu_device *adev = ddev->dev_private;
833
834 if (adev->powerplay.pp_funcs->get_power_profile_mode)
835 return amdgpu_dpm_get_power_profile_mode(adev, buf);
836
837 return snprintf(buf, PAGE_SIZE, "\n");
838}
839
840
841static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
842 struct device_attribute *attr,
843 const char *buf,
844 size_t count)
845{
846 int ret = 0xff;
847 struct drm_device *ddev = dev_get_drvdata(dev);
848 struct amdgpu_device *adev = ddev->dev_private;
849 uint32_t parameter_size = 0;
850 long parameter[64];
851 char *sub_str, buf_cpy[128];
852 char *tmp_str;
853 uint32_t i = 0;
854 char tmp[2];
855 long int profile_mode = 0;
856 const char delimiter[3] = {' ', '\n', '\0'};
857
858 tmp[0] = *(buf);
859 tmp[1] = '\0';
860 ret = kstrtol(tmp, 0, &profile_mode);
861 if (ret)
862 goto fail;
863
864 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
865 if (count < 2 || count > 127)
866 return -EINVAL;
867 while (isspace(*++buf))
868 i++;
869 memcpy(buf_cpy, buf, count-i);
870 tmp_str = buf_cpy;
871 while (tmp_str[0]) {
872 sub_str = strsep(&tmp_str, delimiter);
873 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
874 if (ret) {
875 count = -EINVAL;
876 goto fail;
877 }
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800878 parameter_size++;
879 while (isspace(*tmp_str))
880 tmp_str++;
881 }
882 }
883 parameter[parameter_size] = profile_mode;
884 if (adev->powerplay.pp_funcs->set_power_profile_mode)
885 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
886
887 if (!ret)
888 return count;
889fail:
890 return -EINVAL;
891}
892
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400893static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
894static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
895 amdgpu_get_dpm_forced_performance_level,
896 amdgpu_set_dpm_forced_performance_level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500897static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
898static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
899static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
900 amdgpu_get_pp_force_state,
901 amdgpu_set_pp_force_state);
902static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
903 amdgpu_get_pp_table,
904 amdgpu_set_pp_table);
905static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
906 amdgpu_get_pp_dpm_sclk,
907 amdgpu_set_pp_dpm_sclk);
908static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
909 amdgpu_get_pp_dpm_mclk,
910 amdgpu_set_pp_dpm_mclk);
911static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
912 amdgpu_get_pp_dpm_pcie,
913 amdgpu_set_pp_dpm_pcie);
Eric Huang428bafa2016-05-12 14:51:21 -0400914static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
915 amdgpu_get_pp_sclk_od,
916 amdgpu_set_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -0400917static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
918 amdgpu_get_pp_mclk_od,
919 amdgpu_set_pp_mclk_od);
Rex Zhu37c5c4d2018-01-10 18:42:36 +0800920static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
921 amdgpu_get_pp_power_profile_mode,
922 amdgpu_set_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +0800923static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
924 amdgpu_get_pp_od_clk_voltage,
925 amdgpu_set_pp_od_clk_voltage);
926
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
928 struct device_attribute *attr,
929 char *buf)
930{
931 struct amdgpu_device *adev = dev_get_drvdata(dev);
Alex Deucher0c67df42016-02-19 15:30:15 -0500932 struct drm_device *ddev = adev->ddev;
Alex Deucher71c9b9a2018-01-24 17:27:54 -0500933 int r, temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934
Alex Deucher0c67df42016-02-19 15:30:15 -0500935 /* Can't get temperature when the card is off */
936 if ((adev->flags & AMD_IS_PX) &&
937 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
938 return -EINVAL;
939
Alex Deucher71c9b9a2018-01-24 17:27:54 -0500940 /* sanity check PP is enabled */
941 if (!(adev->powerplay.pp_funcs &&
942 adev->powerplay.pp_funcs->read_sensor))
943 return -EINVAL;
944
945 /* get the temperature */
946 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
947 (void *)&temp, &size);
948 if (r)
949 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950
951 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
952}
953
954static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
955 struct device_attribute *attr,
956 char *buf)
957{
958 struct amdgpu_device *adev = dev_get_drvdata(dev);
959 int hyst = to_sensor_dev_attr(attr)->index;
960 int temp;
961
962 if (hyst)
963 temp = adev->pm.dpm.thermal.min_temp;
964 else
965 temp = adev->pm.dpm.thermal.max_temp;
966
967 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
968}
969
970static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
971 struct device_attribute *attr,
972 char *buf)
973{
974 struct amdgpu_device *adev = dev_get_drvdata(dev);
975 u32 pwm_mode = 0;
976
Rex Zhucd4d7462017-09-06 18:43:52 +0800977 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
Rex Zhu8804b8d2015-11-10 18:29:11 -0500978 return -EINVAL;
979
980 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981
Rex Zhuaad22ca2017-05-05 16:56:45 +0800982 return sprintf(buf, "%i\n", pwm_mode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400983}
984
985static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
986 struct device_attribute *attr,
987 const char *buf,
988 size_t count)
989{
990 struct amdgpu_device *adev = dev_get_drvdata(dev);
991 int err;
992 int value;
993
Alex Deucher5ec36e22018-01-24 16:41:50 -0500994 /* Can't adjust fan when the card is off */
995 if ((adev->flags & AMD_IS_PX) &&
996 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
997 return -EINVAL;
998
Rex Zhucd4d7462017-09-06 18:43:52 +0800999 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 return -EINVAL;
1001
1002 err = kstrtoint(buf, 10, &value);
1003 if (err)
1004 return err;
1005
Rex Zhuaad22ca2017-05-05 16:56:45 +08001006 amdgpu_dpm_set_fan_control_mode(adev, value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007
1008 return count;
1009}
1010
1011static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1012 struct device_attribute *attr,
1013 char *buf)
1014{
1015 return sprintf(buf, "%i\n", 0);
1016}
1017
1018static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1019 struct device_attribute *attr,
1020 char *buf)
1021{
1022 return sprintf(buf, "%i\n", 255);
1023}
1024
1025static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1026 struct device_attribute *attr,
1027 const char *buf, size_t count)
1028{
1029 struct amdgpu_device *adev = dev_get_drvdata(dev);
1030 int err;
1031 u32 value;
1032
Alex Deucher5ec36e22018-01-24 16:41:50 -05001033 /* Can't adjust fan when the card is off */
1034 if ((adev->flags & AMD_IS_PX) &&
1035 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1036 return -EINVAL;
1037
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001038 err = kstrtou32(buf, 10, &value);
1039 if (err)
1040 return err;
1041
1042 value = (value * 100) / 255;
1043
Rex Zhucd4d7462017-09-06 18:43:52 +08001044 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1045 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1046 if (err)
1047 return err;
1048 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049
1050 return count;
1051}
1052
1053static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1054 struct device_attribute *attr,
1055 char *buf)
1056{
1057 struct amdgpu_device *adev = dev_get_drvdata(dev);
1058 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001059 u32 speed = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060
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
Rex Zhucd4d7462017-09-06 18:43:52 +08001066 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1067 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1068 if (err)
1069 return err;
1070 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071
1072 speed = (speed * 255) / 100;
1073
1074 return sprintf(buf, "%i\n", speed);
1075}
1076
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001077static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1078 struct device_attribute *attr,
1079 char *buf)
1080{
1081 struct amdgpu_device *adev = dev_get_drvdata(dev);
1082 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +08001083 u32 speed = 0;
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001084
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->get_fan_speed_rpm) {
1091 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1092 if (err)
1093 return err;
1094 }
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001095
1096 return sprintf(buf, "%i\n", speed);
1097}
1098
Alex Deucher2bd376b2018-01-24 17:19:33 -05001099static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1100 struct device_attribute *attr,
1101 char *buf)
1102{
1103 struct amdgpu_device *adev = dev_get_drvdata(dev);
1104 struct drm_device *ddev = adev->ddev;
1105 u32 vddgfx;
1106 int r, size = sizeof(vddgfx);
1107
1108 /* Can't get voltage when the card is off */
1109 if ((adev->flags & AMD_IS_PX) &&
1110 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1111 return -EINVAL;
1112
1113 /* sanity check PP is enabled */
1114 if (!(adev->powerplay.pp_funcs &&
1115 adev->powerplay.pp_funcs->read_sensor))
1116 return -EINVAL;
1117
1118 /* get the voltage */
1119 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1120 (void *)&vddgfx, &size);
1121 if (r)
1122 return r;
1123
1124 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1125}
1126
1127static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1128 struct device_attribute *attr,
1129 char *buf)
1130{
1131 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1132}
1133
1134static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1135 struct device_attribute *attr,
1136 char *buf)
1137{
1138 struct amdgpu_device *adev = dev_get_drvdata(dev);
1139 struct drm_device *ddev = adev->ddev;
1140 u32 vddnb;
1141 int r, size = sizeof(vddnb);
1142
1143 /* only APUs have vddnb */
1144 if (adev->flags & AMD_IS_APU)
1145 return -EINVAL;
1146
1147 /* Can't get voltage when the card is off */
1148 if ((adev->flags & AMD_IS_PX) &&
1149 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1150 return -EINVAL;
1151
1152 /* sanity check PP is enabled */
1153 if (!(adev->powerplay.pp_funcs &&
1154 adev->powerplay.pp_funcs->read_sensor))
1155 return -EINVAL;
1156
1157 /* get the voltage */
1158 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1159 (void *)&vddnb, &size);
1160 if (r)
1161 return r;
1162
1163 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1164}
1165
1166static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1167 struct device_attribute *attr,
1168 char *buf)
1169{
1170 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1171}
1172
Alex Deucher2976fc22018-01-24 18:34:26 -05001173static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1174 struct device_attribute *attr,
1175 char *buf)
1176{
1177 struct amdgpu_device *adev = dev_get_drvdata(dev);
1178 struct drm_device *ddev = adev->ddev;
Rex Zhu5b79d042018-04-04 15:37:35 +08001179 u32 query = 0;
1180 int r, size = sizeof(u32);
Alex Deucher2976fc22018-01-24 18:34:26 -05001181 unsigned uw;
1182
1183 /* Can't get power when the card is off */
1184 if ((adev->flags & AMD_IS_PX) &&
1185 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1186 return -EINVAL;
1187
1188 /* sanity check PP is enabled */
1189 if (!(adev->powerplay.pp_funcs &&
1190 adev->powerplay.pp_funcs->read_sensor))
1191 return -EINVAL;
1192
1193 /* get the voltage */
1194 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1195 (void *)&query, &size);
1196 if (r)
1197 return r;
1198
1199 /* convert to microwatts */
Rex Zhu5b79d042018-04-04 15:37:35 +08001200 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
Alex Deucher2976fc22018-01-24 18:34:26 -05001201
1202 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1203}
1204
Rex Zhu8d81bce2018-01-29 18:07:01 +08001205static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1206 struct device_attribute *attr,
1207 char *buf)
1208{
1209 return sprintf(buf, "%i\n", 0);
1210}
1211
1212static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1213 struct device_attribute *attr,
1214 char *buf)
1215{
1216 struct amdgpu_device *adev = dev_get_drvdata(dev);
1217 uint32_t limit = 0;
1218
1219 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1220 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1221 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1222 } else {
1223 return snprintf(buf, PAGE_SIZE, "\n");
1224 }
1225}
1226
1227static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1228 struct device_attribute *attr,
1229 char *buf)
1230{
1231 struct amdgpu_device *adev = dev_get_drvdata(dev);
1232 uint32_t limit = 0;
1233
1234 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1235 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1236 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1237 } else {
1238 return snprintf(buf, PAGE_SIZE, "\n");
1239 }
1240}
1241
1242
1243static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
1244 struct device_attribute *attr,
1245 const char *buf,
1246 size_t count)
1247{
1248 struct amdgpu_device *adev = dev_get_drvdata(dev);
1249 int err;
1250 u32 value;
1251
1252 err = kstrtou32(buf, 10, &value);
1253 if (err)
1254 return err;
1255
1256 value = value / 1000000; /* convert to Watt */
1257 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
1258 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
1259 if (err)
1260 return err;
1261 } else {
1262 return -EINVAL;
1263 }
1264
1265 return count;
1266}
1267
Alex Deucher844c5412018-03-26 12:56:56 -05001268
1269/**
1270 * DOC: hwmon
1271 *
1272 * The amdgpu driver exposes the following sensor interfaces:
1273 * - GPU temperature (via the on-die sensor)
1274 * - GPU voltage
1275 * - Northbridge voltage (APUs only)
1276 * - GPU power
1277 * - GPU fan
1278 *
1279 * hwmon interfaces for GPU temperature:
1280 * - temp1_input: the on die GPU temperature in millidegrees Celsius
1281 * - temp1_crit: temperature critical max value in millidegrees Celsius
1282 * - temp1_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
1283 *
1284 * hwmon interfaces for GPU voltage:
1285 * - in0_input: the voltage on the GPU in millivolts
1286 * - in1_input: the voltage on the Northbridge in millivolts
1287 *
1288 * hwmon interfaces for GPU power:
1289 * - power1_average: average power used by the GPU in microWatts
1290 * - power1_cap_min: minimum cap supported in microWatts
1291 * - power1_cap_max: maximum cap supported in microWatts
1292 * - power1_cap: selected power cap in microWatts
1293 *
1294 * hwmon interfaces for GPU fan:
1295 * - pwm1: pulse width modulation fan level (0-255)
1296 * - pwm1_enable: pulse width modulation fan control method
1297 * 0: no fan speed control
1298 * 1: manual fan speed control using pwm interface
1299 * 2: automatic fan speed control
1300 * - pwm1_min: pulse width modulation fan control minimum level (0)
1301 * - pwm1_max: pulse width modulation fan control maximum level (255)
1302 * - fan1_input: fan speed in RPM
1303 *
1304 * You can use hwmon tools like sensors to view this information on your system.
1305 *
1306 */
1307
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
1309static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
1310static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
1311static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
1312static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
1313static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
1314static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001315static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
Alex Deucher2bd376b2018-01-24 17:19:33 -05001316static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
1317static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
1318static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
1319static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
Alex Deucher2976fc22018-01-24 18:34:26 -05001320static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
Rex Zhu8d81bce2018-01-29 18:07:01 +08001321static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
1322static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
1323static 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 -04001324
1325static struct attribute *hwmon_attributes[] = {
1326 &sensor_dev_attr_temp1_input.dev_attr.attr,
1327 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1328 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1329 &sensor_dev_attr_pwm1.dev_attr.attr,
1330 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1331 &sensor_dev_attr_pwm1_min.dev_attr.attr,
1332 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001333 &sensor_dev_attr_fan1_input.dev_attr.attr,
Alex Deucher2bd376b2018-01-24 17:19:33 -05001334 &sensor_dev_attr_in0_input.dev_attr.attr,
1335 &sensor_dev_attr_in0_label.dev_attr.attr,
1336 &sensor_dev_attr_in1_input.dev_attr.attr,
1337 &sensor_dev_attr_in1_label.dev_attr.attr,
Alex Deucher2976fc22018-01-24 18:34:26 -05001338 &sensor_dev_attr_power1_average.dev_attr.attr,
Rex Zhu8d81bce2018-01-29 18:07:01 +08001339 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
1340 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
1341 &sensor_dev_attr_power1_cap.dev_attr.attr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 NULL
1343};
1344
1345static umode_t hwmon_attributes_visible(struct kobject *kobj,
1346 struct attribute *attr, int index)
1347{
Geliang Tangcc29ec82016-01-13 22:48:42 +08001348 struct device *dev = kobj_to_dev(kobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 struct amdgpu_device *adev = dev_get_drvdata(dev);
1350 umode_t effective_mode = attr->mode;
1351
Alex Deucher0d35bc782018-01-24 17:57:19 -05001352 /* handle non-powerplay limitations */
Rex Zhub9050902018-03-12 19:52:23 +08001353 if (!adev->powerplay.pp_handle) {
Alex Deucher0d35bc782018-01-24 17:57:19 -05001354 /* Skip fan attributes if fan is not present */
1355 if (adev->pm.no_fan &&
1356 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1357 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1358 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1359 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1360 return 0;
1361 /* requires powerplay */
1362 if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
1363 return 0;
1364 }
Alex Deucher135f9712017-11-20 17:49:53 -05001365
Rex Zhu1b5708f2015-11-10 18:25:24 -05001366 /* Skip limit attributes if DPM is not enabled */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001367 if (!adev->pm.dpm_enabled &&
1368 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
Alex Deucher27100732015-10-19 15:49:11 -04001369 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
1370 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1371 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1372 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1373 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001374 return 0;
1375
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001376 /* mask fan attributes if we have no bindings for this asic to expose */
Rex Zhucd4d7462017-09-06 18:43:52 +08001377 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001379 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001380 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
1381 effective_mode &= ~S_IRUGO;
1382
Rex Zhucd4d7462017-09-06 18:43:52 +08001383 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001385 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
1387 effective_mode &= ~S_IWUSR;
1388
Rex Zhu8d81bce2018-01-29 18:07:01 +08001389 if ((adev->flags & AMD_IS_APU) &&
1390 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
1391 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
1392 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
1393 return 0;
1394
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001395 /* hide max/min values if we can't both query and manage the fan */
Rex Zhucd4d7462017-09-06 18:43:52 +08001396 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1397 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1399 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1400 return 0;
1401
Alex Deucher0d35bc782018-01-24 17:57:19 -05001402 /* only APUs have vddnb */
1403 if (!(adev->flags & AMD_IS_APU) &&
1404 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
1405 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
Grazvydas Ignotas81c15142016-10-29 23:28:59 +03001406 return 0;
1407
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408 return effective_mode;
1409}
1410
1411static const struct attribute_group hwmon_attrgroup = {
1412 .attrs = hwmon_attributes,
1413 .is_visible = hwmon_attributes_visible,
1414};
1415
1416static const struct attribute_group *hwmon_groups[] = {
1417 &hwmon_attrgroup,
1418 NULL
1419};
1420
1421void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1422{
1423 struct amdgpu_device *adev =
1424 container_of(work, struct amdgpu_device,
1425 pm.dpm.thermal.work);
1426 /* switch to the thermal state */
Rex Zhu3a2c7882015-08-25 15:57:43 +08001427 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001428 int temp, size = sizeof(temp);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429
1430 if (!adev->pm.dpm_enabled)
1431 return;
1432
Alex Deucher71c9b9a2018-01-24 17:27:54 -05001433 if (adev->powerplay.pp_funcs &&
1434 adev->powerplay.pp_funcs->read_sensor &&
1435 !amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1436 (void *)&temp, &size)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 if (temp < adev->pm.dpm.thermal.min_temp)
1438 /* switch back the user state */
1439 dpm_state = adev->pm.dpm.user_state;
1440 } else {
1441 if (adev->pm.dpm.thermal.high_to_low)
1442 /* switch back the user state */
1443 dpm_state = adev->pm.dpm.user_state;
1444 }
1445 mutex_lock(&adev->pm.mutex);
1446 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1447 adev->pm.dpm.thermal_active = true;
1448 else
1449 adev->pm.dpm.thermal_active = false;
1450 adev->pm.dpm.state = dpm_state;
1451 mutex_unlock(&adev->pm.mutex);
1452
1453 amdgpu_pm_compute_clocks(adev);
1454}
1455
1456static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
Rex Zhu3a2c7882015-08-25 15:57:43 +08001457 enum amd_pm_state_type dpm_state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458{
1459 int i;
1460 struct amdgpu_ps *ps;
1461 u32 ui_class;
1462 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1463 true : false;
1464
1465 /* check if the vblank period is too short to adjust the mclk */
Rex Zhucd4d7462017-09-06 18:43:52 +08001466 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001467 if (amdgpu_dpm_vblank_too_short(adev))
1468 single_display = false;
1469 }
1470
1471 /* certain older asics have a separare 3D performance state,
1472 * so try that first if the user selected performance
1473 */
1474 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1475 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1476 /* balanced states don't exist at the moment */
1477 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1478 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1479
1480restart_search:
1481 /* Pick the best power state based on current conditions */
1482 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1483 ps = &adev->pm.dpm.ps[i];
1484 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1485 switch (dpm_state) {
1486 /* user states */
1487 case POWER_STATE_TYPE_BATTERY:
1488 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1489 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1490 if (single_display)
1491 return ps;
1492 } else
1493 return ps;
1494 }
1495 break;
1496 case POWER_STATE_TYPE_BALANCED:
1497 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1498 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1499 if (single_display)
1500 return ps;
1501 } else
1502 return ps;
1503 }
1504 break;
1505 case POWER_STATE_TYPE_PERFORMANCE:
1506 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1507 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1508 if (single_display)
1509 return ps;
1510 } else
1511 return ps;
1512 }
1513 break;
1514 /* internal states */
1515 case POWER_STATE_TYPE_INTERNAL_UVD:
1516 if (adev->pm.dpm.uvd_ps)
1517 return adev->pm.dpm.uvd_ps;
1518 else
1519 break;
1520 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1521 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1522 return ps;
1523 break;
1524 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1525 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1526 return ps;
1527 break;
1528 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1529 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1530 return ps;
1531 break;
1532 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1533 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1534 return ps;
1535 break;
1536 case POWER_STATE_TYPE_INTERNAL_BOOT:
1537 return adev->pm.dpm.boot_ps;
1538 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1539 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1540 return ps;
1541 break;
1542 case POWER_STATE_TYPE_INTERNAL_ACPI:
1543 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1544 return ps;
1545 break;
1546 case POWER_STATE_TYPE_INTERNAL_ULV:
1547 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1548 return ps;
1549 break;
1550 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1551 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1552 return ps;
1553 break;
1554 default:
1555 break;
1556 }
1557 }
1558 /* use a fallback state if we didn't match */
1559 switch (dpm_state) {
1560 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1561 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1562 goto restart_search;
1563 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1564 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1565 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1566 if (adev->pm.dpm.uvd_ps) {
1567 return adev->pm.dpm.uvd_ps;
1568 } else {
1569 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1570 goto restart_search;
1571 }
1572 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1573 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1574 goto restart_search;
1575 case POWER_STATE_TYPE_INTERNAL_ACPI:
1576 dpm_state = POWER_STATE_TYPE_BATTERY;
1577 goto restart_search;
1578 case POWER_STATE_TYPE_BATTERY:
1579 case POWER_STATE_TYPE_BALANCED:
1580 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1581 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1582 goto restart_search;
1583 default:
1584 break;
1585 }
1586
1587 return NULL;
1588}
1589
1590static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1591{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 struct amdgpu_ps *ps;
Rex Zhu3a2c7882015-08-25 15:57:43 +08001593 enum amd_pm_state_type dpm_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 int ret;
Rex Zhucd4d7462017-09-06 18:43:52 +08001595 bool equal = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596
1597 /* if dpm init failed */
1598 if (!adev->pm.dpm_enabled)
1599 return;
1600
1601 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1602 /* add other state override checks here */
1603 if ((!adev->pm.dpm.thermal_active) &&
1604 (!adev->pm.dpm.uvd_active))
1605 adev->pm.dpm.state = adev->pm.dpm.user_state;
1606 }
1607 dpm_state = adev->pm.dpm.state;
1608
1609 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1610 if (ps)
1611 adev->pm.dpm.requested_ps = ps;
1612 else
1613 return;
1614
Rex Zhucd4d7462017-09-06 18:43:52 +08001615 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 printk("switching from power state:\n");
1617 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1618 printk("switching to power state:\n");
1619 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1620 }
1621
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001622 /* update whether vce is active */
1623 ps->vce_active = adev->pm.dpm.vce_active;
Rex Zhucd4d7462017-09-06 18:43:52 +08001624 if (adev->powerplay.pp_funcs->display_configuration_changed)
1625 amdgpu_dpm_display_configuration_changed(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001626
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001627 ret = amdgpu_dpm_pre_set_power_state(adev);
1628 if (ret)
Christian Königa27de352016-01-21 11:28:53 +01001629 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630
Rex Zhucd4d7462017-09-06 18:43:52 +08001631 if (adev->powerplay.pp_funcs->check_state_equal) {
1632 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1633 equal = false;
1634 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635
Rex Zhu5e876c62016-10-14 19:23:34 +08001636 if (equal)
1637 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639 amdgpu_dpm_set_power_state(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 amdgpu_dpm_post_set_power_state(adev);
1641
Alex Deuchereda1d1c2016-02-24 17:18:25 -05001642 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1643 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1644
Rex Zhucd4d7462017-09-06 18:43:52 +08001645 if (adev->powerplay.pp_funcs->force_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 if (adev->pm.dpm.thermal_active) {
Rex Zhue5d03ac2016-12-23 14:39:41 +08001647 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648 /* force low perf level for thermal */
Rex Zhue5d03ac2016-12-23 14:39:41 +08001649 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 /* save the user's level */
1651 adev->pm.dpm.forced_level = level;
1652 } else {
1653 /* otherwise, user selected level */
1654 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1655 }
1656 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001657}
1658
1659void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1660{
Rex Zhucd4d7462017-09-06 18:43:52 +08001661 if (adev->powerplay.pp_funcs->powergate_uvd) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001662 /* enable/disable UVD */
1663 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001664 amdgpu_dpm_powergate_uvd(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001665 mutex_unlock(&adev->pm.mutex);
1666 } else {
1667 if (enable) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001668 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001669 adev->pm.dpm.uvd_active = true;
1670 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 mutex_unlock(&adev->pm.mutex);
1672 } else {
Tom St Denise95a14a2016-07-28 09:40:07 -04001673 mutex_lock(&adev->pm.mutex);
1674 adev->pm.dpm.uvd_active = false;
1675 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676 }
Tom St Denise95a14a2016-07-28 09:40:07 -04001677 amdgpu_pm_compute_clocks(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 }
1679}
1680
1681void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1682{
Rex Zhucd4d7462017-09-06 18:43:52 +08001683 if (adev->powerplay.pp_funcs->powergate_vce) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001684 /* enable/disable VCE */
1685 mutex_lock(&adev->pm.mutex);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001686 amdgpu_dpm_powergate_vce(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001687 mutex_unlock(&adev->pm.mutex);
1688 } else {
1689 if (enable) {
Sonny Jiangb7a077692015-05-28 15:47:53 -04001690 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001691 adev->pm.dpm.vce_active = true;
1692 /* XXX select vce level based on ring/task */
Rex Zhu0d8de7c2016-10-12 15:13:29 +08001693 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
Sonny Jiangb7a077692015-05-28 15:47:53 -04001694 mutex_unlock(&adev->pm.mutex);
Alex Deucher2990a1f2017-12-15 16:18:00 -05001695 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1696 AMD_CG_STATE_UNGATE);
1697 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1698 AMD_PG_STATE_UNGATE);
Rex Zhu03a5f1d2017-03-06 11:29:26 +08001699 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001700 } else {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001701 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1702 AMD_PG_STATE_GATE);
1703 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1704 AMD_CG_STATE_GATE);
Tom St Denise95a14a2016-07-28 09:40:07 -04001705 mutex_lock(&adev->pm.mutex);
1706 adev->pm.dpm.vce_active = false;
1707 mutex_unlock(&adev->pm.mutex);
Rex Zhubeeea982017-01-26 16:25:05 +08001708 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a077692015-05-28 15:47:53 -04001709 }
Rex Zhubeeea982017-01-26 16:25:05 +08001710
Sonny Jiangb7a077692015-05-28 15:47:53 -04001711 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712}
1713
1714void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1715{
1716 int i;
1717
Rex Zhucd4d7462017-09-06 18:43:52 +08001718 if (adev->powerplay.pp_funcs->print_power_state == NULL)
Rex Zhu1b5708f2015-11-10 18:25:24 -05001719 return;
1720
1721 for (i = 0; i < adev->pm.dpm.num_ps; i++)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001723
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001724}
1725
1726int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1727{
1728 int ret;
1729
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001730 if (adev->pm.sysfs_initialized)
1731 return 0;
1732
Rex Zhud2f52ac2017-09-22 17:47:27 +08001733 if (adev->pm.dpm_enabled == 0)
1734 return 0;
1735
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1737 DRIVER_NAME, adev,
1738 hwmon_groups);
1739 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1740 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1741 dev_err(adev->dev,
1742 "Unable to register hwmon device: %d\n", ret);
1743 return ret;
1744 }
1745
1746 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1747 if (ret) {
1748 DRM_ERROR("failed to create device file for dpm state\n");
1749 return ret;
1750 }
1751 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1752 if (ret) {
1753 DRM_ERROR("failed to create device file for dpm state\n");
1754 return ret;
1755 }
Eric Huangf3898ea2015-12-11 16:24:34 -05001756
Rex Zhu6d07fe72017-09-25 18:51:50 +08001757
1758 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1759 if (ret) {
1760 DRM_ERROR("failed to create device file pp_num_states\n");
1761 return ret;
1762 }
1763 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1764 if (ret) {
1765 DRM_ERROR("failed to create device file pp_cur_state\n");
1766 return ret;
1767 }
1768 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1769 if (ret) {
1770 DRM_ERROR("failed to create device file pp_force_state\n");
1771 return ret;
1772 }
1773 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1774 if (ret) {
1775 DRM_ERROR("failed to create device file pp_table\n");
1776 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -05001777 }
Eric Huangc85e2992016-05-19 15:41:25 -04001778
1779 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1780 if (ret) {
1781 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1782 return ret;
1783 }
1784 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1785 if (ret) {
1786 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1787 return ret;
1788 }
1789 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1790 if (ret) {
1791 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1792 return ret;
1793 }
Eric Huang8b2e5742016-05-19 15:46:10 -04001794 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1795 if (ret) {
1796 DRM_ERROR("failed to create device file pp_sclk_od\n");
1797 return ret;
1798 }
Eric Huangf2bdc052016-05-24 15:11:17 -04001799 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1800 if (ret) {
1801 DRM_ERROR("failed to create device file pp_mclk_od\n");
1802 return ret;
1803 }
Eric Huang34bb2732016-09-12 16:17:44 -04001804 ret = device_create_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08001805 &dev_attr_pp_power_profile_mode);
1806 if (ret) {
1807 DRM_ERROR("failed to create device file "
1808 "pp_power_profile_mode\n");
1809 return ret;
1810 }
Rex Zhue3933f22018-01-16 18:35:15 +08001811 ret = device_create_file(adev->dev,
1812 &dev_attr_pp_od_clk_voltage);
1813 if (ret) {
1814 DRM_ERROR("failed to create device file "
1815 "pp_od_clk_voltage\n");
1816 return ret;
1817 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 ret = amdgpu_debugfs_pm_init(adev);
1819 if (ret) {
1820 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1821 return ret;
1822 }
1823
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001824 adev->pm.sysfs_initialized = true;
1825
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 return 0;
1827}
1828
1829void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1830{
Rex Zhud2f52ac2017-09-22 17:47:27 +08001831 if (adev->pm.dpm_enabled == 0)
1832 return;
1833
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 if (adev->pm.int_hwmon_dev)
1835 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1836 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1837 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
Rex Zhu6d07fe72017-09-25 18:51:50 +08001838
1839 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1840 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1841 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1842 device_remove_file(adev->dev, &dev_attr_pp_table);
1843
Eric Huangc85e2992016-05-19 15:41:25 -04001844 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1845 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1846 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
Eric Huang8b2e5742016-05-19 15:46:10 -04001847 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -04001848 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
Eric Huang34bb2732016-09-12 16:17:44 -04001849 device_remove_file(adev->dev,
Rex Zhu37c5c4d2018-01-10 18:42:36 +08001850 &dev_attr_pp_power_profile_mode);
Rex Zhue3933f22018-01-16 18:35:15 +08001851 device_remove_file(adev->dev,
1852 &dev_attr_pp_od_clk_voltage);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001853}
1854
1855void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1856{
Rex Zhu5e876c62016-10-14 19:23:34 +08001857 int i = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858
1859 if (!adev->pm.dpm_enabled)
1860 return;
1861
Alex Deucherc10c8f72017-02-10 18:09:32 -05001862 if (adev->mode_info.num_crtc)
1863 amdgpu_display_bandwidth_update(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001864
1865 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1866 struct amdgpu_ring *ring = adev->rings[i];
1867 if (ring && ring->ready)
1868 amdgpu_fence_wait_empty(ring);
1869 }
1870
Rex Zhud91ea492018-03-26 22:08:29 +08001871 if (!amdgpu_device_has_dc_support(adev)) {
1872 mutex_lock(&adev->pm.mutex);
1873 amdgpu_dpm_get_active_displays(adev);
1874 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtcs;
1875 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
1876 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
1877 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
1878 if (adev->pm.pm_display_cfg.vrefresh > 120)
1879 adev->pm.pm_display_cfg.min_vblank_time = 0;
1880 if (adev->powerplay.pp_funcs->display_configuration_change)
1881 adev->powerplay.pp_funcs->display_configuration_change(
1882 adev->powerplay.pp_handle,
1883 &adev->pm.pm_display_cfg);
1884 mutex_unlock(&adev->pm.mutex);
1885 }
1886
Rex Zhu6d07fe72017-09-25 18:51:50 +08001887 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Evan Quan39199b82017-12-29 14:46:13 +08001888 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001889 } else {
1890 mutex_lock(&adev->pm.mutex);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001891 /* update battery/ac status */
1892 if (power_supply_is_system_supplied() > 0)
1893 adev->pm.dpm.ac_power = true;
1894 else
1895 adev->pm.dpm.ac_power = false;
1896
1897 amdgpu_dpm_change_power_state_locked(adev);
1898
1899 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901}
1902
1903/*
1904 * Debugfs info
1905 */
1906#if defined(CONFIG_DEBUG_FS)
1907
Tom St Denis3de4ec52016-09-19 12:48:52 -04001908static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1909{
Eric Huangcd7b0c62017-02-07 16:37:48 -05001910 uint32_t value;
Rex Zhu5b79d042018-04-04 15:37:35 +08001911 uint32_t query = 0;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001912 int size;
Tom St Denis3de4ec52016-09-19 12:48:52 -04001913
1914 /* sanity check PP is enabled */
1915 if (!(adev->powerplay.pp_funcs &&
1916 adev->powerplay.pp_funcs->read_sensor))
1917 return -EINVAL;
1918
1919 /* GPU Clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001920 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001921 seq_printf(m, "GFX Clocks and Power:\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001922 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001923 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001924 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001925 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
Rex Zhu5ed8d652018-01-08 13:59:05 +08001926 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
1927 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
1928 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
1929 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001930 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001931 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001932 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001933 seq_printf(m, "\t%u mV (VDDNB)\n", value);
Rex Zhu5b79d042018-04-04 15:37:35 +08001934 size = sizeof(uint32_t);
1935 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
1936 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001937 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001938 seq_printf(m, "\n");
1939
1940 /* GPU Temp */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001941 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001942 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1943
1944 /* GPU Load */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001945 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001946 seq_printf(m, "GPU Load: %u %%\n", value);
1947 seq_printf(m, "\n");
1948
1949 /* UVD clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001950 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04001951 if (!value) {
1952 seq_printf(m, "UVD: Disabled\n");
1953 } else {
1954 seq_printf(m, "UVD: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001955 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001956 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001957 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001958 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1959 }
1960 }
1961 seq_printf(m, "\n");
1962
1963 /* VCE clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001964 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04001965 if (!value) {
1966 seq_printf(m, "VCE: Disabled\n");
1967 } else {
1968 seq_printf(m, "VCE: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001969 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001970 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
1971 }
1972 }
1973
1974 return 0;
1975}
1976
Huang Ruia8503b12017-01-05 19:17:13 +08001977static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
1978{
1979 int i;
1980
1981 for (i = 0; clocks[i].flag; i++)
1982 seq_printf(m, "\t%s: %s\n", clocks[i].name,
1983 (flags & clocks[i].flag) ? "On" : "Off");
1984}
1985
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
1987{
1988 struct drm_info_node *node = (struct drm_info_node *) m->private;
1989 struct drm_device *dev = node->minor->dev;
1990 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher0c67df42016-02-19 15:30:15 -05001991 struct drm_device *ddev = adev->ddev;
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001992 u32 flags = 0;
1993
Alex Deucher2990a1f2017-12-15 16:18:00 -05001994 amdgpu_device_ip_get_clockgating_state(adev, &flags);
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001995 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
Huang Ruia8503b12017-01-05 19:17:13 +08001996 amdgpu_parse_cg_state(m, flags);
1997 seq_printf(m, "\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998
Rex Zhu1b5708f2015-11-10 18:25:24 -05001999 if (!adev->pm.dpm_enabled) {
2000 seq_printf(m, "dpm not enabled\n");
2001 return 0;
2002 }
Alex Deucher0c67df42016-02-19 15:30:15 -05002003 if ((adev->flags & AMD_IS_PX) &&
2004 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
2005 seq_printf(m, "PX asic powered off\n");
Rex Zhu6d07fe72017-09-25 18:51:50 +08002006 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007 mutex_lock(&adev->pm.mutex);
Rex Zhucd4d7462017-09-06 18:43:52 +08002008 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
2009 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 else
2011 seq_printf(m, "Debugfs support not implemented for this asic\n");
2012 mutex_unlock(&adev->pm.mutex);
Rex Zhu6d07fe72017-09-25 18:51:50 +08002013 } else {
2014 return amdgpu_debugfs_pm_info_pp(m, adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015 }
2016
2017 return 0;
2018}
2019
Nils Wallménius06ab6832016-05-02 12:46:15 -04002020static const struct drm_info_list amdgpu_pm_info_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
2022};
2023#endif
2024
2025static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
2026{
2027#if defined(CONFIG_DEBUG_FS)
2028 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
2029#else
2030 return 0;
2031#endif
2032}