blob: 6c570d4e4516488b81cd385353b8d02c7e428912 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
16 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
18 * OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Authors: Rafał Miłecki <zajec5@gmail.com>
21 * Alex Deucher <alexdeucher@gmail.com>
22 */
23#include <drm/drmP.h>
24#include "amdgpu.h"
25#include "amdgpu_drv.h"
26#include "amdgpu_pm.h"
27#include "amdgpu_dpm.h"
28#include "atom.h"
29#include <linux/power_supply.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
32
Rex Zhu1b5708f2015-11-10 18:25:24 -050033#include "amd_powerplay.h"
34
Alex Deucherd38ceaf2015-04-20 16:55:21 -040035static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
36
Huang Ruia8503b12017-01-05 19:17:13 +080037static const struct cg_flag_name clocks[] = {
38 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
39 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
40 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
41 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
Huang Rui54170222017-01-11 09:55:34 +080042 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080043 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
44 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
45 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
Huang Rui12ad27f2017-03-24 09:58:11 +080046 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
47 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080048 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
49 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
50 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
51 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
Huang Ruie96487a2017-03-24 10:12:32 +080052 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080053 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
54 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
55 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
56 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
57 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080058 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
59 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
Huang Ruia8503b12017-01-05 19:17:13 +080060 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
Huang Ruif9abe352017-03-24 10:46:16 +080061 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
Huang Ruia8503b12017-01-05 19:17:13 +080062 {0, NULL},
63};
64
Alex Deucherd38ceaf2015-04-20 16:55:21 -040065void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
66{
67 if (adev->pm.dpm_enabled) {
68 mutex_lock(&adev->pm.mutex);
69 if (power_supply_is_system_supplied() > 0)
70 adev->pm.dpm.ac_power = true;
71 else
72 adev->pm.dpm.ac_power = false;
Rex Zhucd4d7462017-09-06 18:43:52 +080073 if (adev->powerplay.pp_funcs->enable_bapm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
75 mutex_unlock(&adev->pm.mutex);
76 }
77}
78
79static ssize_t amdgpu_get_dpm_state(struct device *dev,
80 struct device_attribute *attr,
81 char *buf)
82{
83 struct drm_device *ddev = dev_get_drvdata(dev);
84 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -050085 enum amd_pm_state_type pm;
86
Rex Zhucd4d7462017-09-06 18:43:52 +080087 if (adev->powerplay.pp_funcs->get_current_power_state)
Rex Zhu1b5708f2015-11-10 18:25:24 -050088 pm = amdgpu_dpm_get_current_power_state(adev);
Rex Zhucd4d7462017-09-06 18:43:52 +080089 else
Rex Zhu1b5708f2015-11-10 18:25:24 -050090 pm = adev->pm.dpm.user_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091
92 return snprintf(buf, PAGE_SIZE, "%s\n",
93 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
94 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
95}
96
97static ssize_t amdgpu_set_dpm_state(struct device *dev,
98 struct device_attribute *attr,
99 const char *buf,
100 size_t count)
101{
102 struct drm_device *ddev = dev_get_drvdata(dev);
103 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500104 enum amd_pm_state_type state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 if (strncmp("battery", buf, strlen("battery")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500107 state = POWER_STATE_TYPE_BATTERY;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500109 state = POWER_STATE_TYPE_BALANCED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 else if (strncmp("performance", buf, strlen("performance")) == 0)
Rex Zhu1b5708f2015-11-10 18:25:24 -0500111 state = POWER_STATE_TYPE_PERFORMANCE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112 else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 count = -EINVAL;
114 goto fail;
115 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116
Rex Zhu6d07fe72017-09-25 18:51:50 +0800117 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhudf1e6392017-09-01 13:46:20 +0800118 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state, NULL);
Rex Zhu1b5708f2015-11-10 18:25:24 -0500119 } else {
120 mutex_lock(&adev->pm.mutex);
121 adev->pm.dpm.user_state = state;
122 mutex_unlock(&adev->pm.mutex);
123
124 /* Can't set dpm state when the card is off */
125 if (!(adev->flags & AMD_IS_PX) ||
126 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
127 amdgpu_pm_compute_clocks(adev);
128 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129fail:
130 return count;
131}
132
133static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
Rex Zhu1b5708f2015-11-10 18:25:24 -0500134 struct device_attribute *attr,
135 char *buf)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136{
137 struct drm_device *ddev = dev_get_drvdata(dev);
138 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhucd4d7462017-09-06 18:43:52 +0800139 enum amd_dpm_forced_level level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
Alex Deucher0c67df42016-02-19 15:30:15 -0500141 if ((adev->flags & AMD_IS_PX) &&
142 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
143 return snprintf(buf, PAGE_SIZE, "off\n");
144
Rex Zhucd4d7462017-09-06 18:43:52 +0800145 if (adev->powerplay.pp_funcs->get_performance_level)
146 level = amdgpu_dpm_get_performance_level(adev);
147 else
148 level = adev->pm.dpm.forced_level;
149
Rex Zhue5d03ac2016-12-23 14:39:41 +0800150 return snprintf(buf, PAGE_SIZE, "%s\n",
Rex Zhu570272d2017-01-06 13:32:49 +0800151 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
152 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
153 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
154 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
155 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
156 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
157 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
158 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
159 "unknown");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400160}
161
162static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf,
165 size_t count)
166{
167 struct drm_device *ddev = dev_get_drvdata(dev);
168 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhue5d03ac2016-12-23 14:39:41 +0800169 enum amd_dpm_forced_level level;
Rex Zhucd4d7462017-09-06 18:43:52 +0800170 enum amd_dpm_forced_level current_level = 0xff;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 int ret = 0;
172
Alex Deucher0c67df42016-02-19 15:30:15 -0500173 /* Can't force performance level when the card is off */
174 if ((adev->flags & AMD_IS_PX) &&
175 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
176 return -EINVAL;
177
Rex Zhucd4d7462017-09-06 18:43:52 +0800178 if (adev->powerplay.pp_funcs->get_performance_level)
179 current_level = amdgpu_dpm_get_performance_level(adev);
Rex Zhu3bd58972016-12-23 15:24:37 +0800180
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400181 if (strncmp("low", buf, strlen("low")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800182 level = AMD_DPM_FORCED_LEVEL_LOW;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183 } else if (strncmp("high", buf, strlen("high")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800184 level = AMD_DPM_FORCED_LEVEL_HIGH;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800186 level = AMD_DPM_FORCED_LEVEL_AUTO;
Eric Huangf3898ea2015-12-11 16:24:34 -0500187 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
Rex Zhue5d03ac2016-12-23 14:39:41 +0800188 level = AMD_DPM_FORCED_LEVEL_MANUAL;
Rex Zhu570272d2017-01-06 13:32:49 +0800189 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
190 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
191 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
192 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
193 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
194 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
195 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
196 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
197 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
198 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
199 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200 count = -EINVAL;
201 goto fail;
202 }
Rex Zhu1b5708f2015-11-10 18:25:24 -0500203
Rex Zhu3bd58972016-12-23 15:24:37 +0800204 if (current_level == level)
Rex Zhu8e7afd32017-01-09 15:18:01 +0800205 return count;
Rex Zhu3bd58972016-12-23 15:24:37 +0800206
Rex Zhucd4d7462017-09-06 18:43:52 +0800207 if (adev->powerplay.pp_funcs->force_performance_level) {
Rex Zhu1b5708f2015-11-10 18:25:24 -0500208 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400209 if (adev->pm.dpm.thermal_active) {
210 count = -EINVAL;
Alex Deucher10f950f2016-02-19 15:18:45 -0500211 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 goto fail;
213 }
214 ret = amdgpu_dpm_force_performance_level(adev, level);
215 if (ret)
216 count = -EINVAL;
Rex Zhu1b5708f2015-11-10 18:25:24 -0500217 else
218 adev->pm.dpm.forced_level = level;
219 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 }
Rex Zhu570272d2017-01-06 13:32:49 +0800221
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400222fail:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223 return count;
224}
225
Eric Huangf3898ea2015-12-11 16:24:34 -0500226static ssize_t amdgpu_get_pp_num_states(struct device *dev,
227 struct device_attribute *attr,
228 char *buf)
229{
230 struct drm_device *ddev = dev_get_drvdata(dev);
231 struct amdgpu_device *adev = ddev->dev_private;
232 struct pp_states_info data;
233 int i, buf_len;
234
Rex Zhucd4d7462017-09-06 18:43:52 +0800235 if (adev->powerplay.pp_funcs->get_pp_num_states)
Eric Huangf3898ea2015-12-11 16:24:34 -0500236 amdgpu_dpm_get_pp_num_states(adev, &data);
237
238 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
239 for (i = 0; i < data.nums; i++)
240 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
241 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
242 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
243 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
244 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
245
246 return buf_len;
247}
248
249static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
250 struct device_attribute *attr,
251 char *buf)
252{
253 struct drm_device *ddev = dev_get_drvdata(dev);
254 struct amdgpu_device *adev = ddev->dev_private;
255 struct pp_states_info data;
256 enum amd_pm_state_type pm = 0;
257 int i = 0;
258
Rex Zhucd4d7462017-09-06 18:43:52 +0800259 if (adev->powerplay.pp_funcs->get_current_power_state
260 && adev->powerplay.pp_funcs->get_pp_num_states) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500261 pm = amdgpu_dpm_get_current_power_state(adev);
262 amdgpu_dpm_get_pp_num_states(adev, &data);
263
264 for (i = 0; i < data.nums; i++) {
265 if (pm == data.states[i])
266 break;
267 }
268
269 if (i == data.nums)
270 i = -EINVAL;
271 }
272
273 return snprintf(buf, PAGE_SIZE, "%d\n", i);
274}
275
276static ssize_t amdgpu_get_pp_force_state(struct device *dev,
277 struct device_attribute *attr,
278 char *buf)
279{
280 struct drm_device *ddev = dev_get_drvdata(dev);
281 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500282
Rex Zhucd4d7462017-09-06 18:43:52 +0800283 if (adev->pp_force_state_enabled)
284 return amdgpu_get_pp_cur_state(dev, attr, buf);
285 else
Eric Huangf3898ea2015-12-11 16:24:34 -0500286 return snprintf(buf, PAGE_SIZE, "\n");
287}
288
289static ssize_t amdgpu_set_pp_force_state(struct device *dev,
290 struct device_attribute *attr,
291 const char *buf,
292 size_t count)
293{
294 struct drm_device *ddev = dev_get_drvdata(dev);
295 struct amdgpu_device *adev = ddev->dev_private;
296 enum amd_pm_state_type state = 0;
Dan Carpenter041bf022016-06-16 11:30:23 +0300297 unsigned long idx;
Eric Huangf3898ea2015-12-11 16:24:34 -0500298 int ret;
299
300 if (strlen(buf) == 1)
301 adev->pp_force_state_enabled = false;
Rex Zhu6d07fe72017-09-25 18:51:50 +0800302 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
303 adev->powerplay.pp_funcs->get_pp_num_states) {
Dan Carpenter041bf022016-06-16 11:30:23 +0300304 struct pp_states_info data;
Eric Huangf3898ea2015-12-11 16:24:34 -0500305
Dan Carpenter041bf022016-06-16 11:30:23 +0300306 ret = kstrtoul(buf, 0, &idx);
307 if (ret || idx >= ARRAY_SIZE(data.states)) {
Eric Huangf3898ea2015-12-11 16:24:34 -0500308 count = -EINVAL;
309 goto fail;
310 }
311
Dan Carpenter041bf022016-06-16 11:30:23 +0300312 amdgpu_dpm_get_pp_num_states(adev, &data);
313 state = data.states[idx];
314 /* only set user selected power states */
315 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
316 state != POWER_STATE_TYPE_DEFAULT) {
317 amdgpu_dpm_dispatch_task(adev,
Rex Zhudf1e6392017-09-01 13:46:20 +0800318 AMD_PP_TASK_ENABLE_USER_STATE, &state, NULL);
Dan Carpenter041bf022016-06-16 11:30:23 +0300319 adev->pp_force_state_enabled = true;
Eric Huangf3898ea2015-12-11 16:24:34 -0500320 }
321 }
322fail:
323 return count;
324}
325
326static ssize_t amdgpu_get_pp_table(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
330 struct drm_device *ddev = dev_get_drvdata(dev);
331 struct amdgpu_device *adev = ddev->dev_private;
332 char *table = NULL;
Eric Huang1684d3b2016-07-28 17:25:01 -0400333 int size;
Eric Huangf3898ea2015-12-11 16:24:34 -0500334
Rex Zhucd4d7462017-09-06 18:43:52 +0800335 if (adev->powerplay.pp_funcs->get_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500336 size = amdgpu_dpm_get_pp_table(adev, &table);
337 else
338 return 0;
339
340 if (size >= PAGE_SIZE)
341 size = PAGE_SIZE - 1;
342
Eric Huang1684d3b2016-07-28 17:25:01 -0400343 memcpy(buf, table, size);
Eric Huangf3898ea2015-12-11 16:24:34 -0500344
345 return size;
346}
347
348static ssize_t amdgpu_set_pp_table(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf,
351 size_t count)
352{
353 struct drm_device *ddev = dev_get_drvdata(dev);
354 struct amdgpu_device *adev = ddev->dev_private;
355
Rex Zhucd4d7462017-09-06 18:43:52 +0800356 if (adev->powerplay.pp_funcs->set_pp_table)
Eric Huangf3898ea2015-12-11 16:24:34 -0500357 amdgpu_dpm_set_pp_table(adev, buf, count);
358
359 return count;
360}
361
362static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
363 struct device_attribute *attr,
364 char *buf)
365{
366 struct drm_device *ddev = dev_get_drvdata(dev);
367 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500368
Rex Zhucd4d7462017-09-06 18:43:52 +0800369 if (adev->powerplay.pp_funcs->print_clock_levels)
370 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
371 else
372 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500373}
374
375static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
376 struct device_attribute *attr,
377 const char *buf,
378 size_t count)
379{
380 struct drm_device *ddev = dev_get_drvdata(dev);
381 struct amdgpu_device *adev = ddev->dev_private;
382 int ret;
383 long level;
Eric Huang56327082016-04-12 14:57:23 -0400384 uint32_t i, mask = 0;
385 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500386
Eric Huang14b33072016-06-14 15:08:22 -0400387 for (i = 0; i < strlen(buf); i++) {
388 if (*(buf + i) == '\n')
389 continue;
Eric Huang56327082016-04-12 14:57:23 -0400390 sub_str[0] = *(buf + i);
391 sub_str[1] = '\0';
392 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500393
Eric Huang56327082016-04-12 14:57:23 -0400394 if (ret) {
395 count = -EINVAL;
396 goto fail;
397 }
398 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500399 }
400
Rex Zhucd4d7462017-09-06 18:43:52 +0800401 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400402 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800403
Eric Huangf3898ea2015-12-11 16:24:34 -0500404fail:
405 return count;
406}
407
408static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct drm_device *ddev = dev_get_drvdata(dev);
413 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500414
Rex Zhucd4d7462017-09-06 18:43:52 +0800415 if (adev->powerplay.pp_funcs->print_clock_levels)
416 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
417 else
418 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500419}
420
421static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
422 struct device_attribute *attr,
423 const char *buf,
424 size_t count)
425{
426 struct drm_device *ddev = dev_get_drvdata(dev);
427 struct amdgpu_device *adev = ddev->dev_private;
428 int ret;
429 long level;
Eric Huang56327082016-04-12 14:57:23 -0400430 uint32_t i, mask = 0;
431 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500432
Eric Huang14b33072016-06-14 15:08:22 -0400433 for (i = 0; i < strlen(buf); i++) {
434 if (*(buf + i) == '\n')
435 continue;
Eric Huang56327082016-04-12 14:57:23 -0400436 sub_str[0] = *(buf + i);
437 sub_str[1] = '\0';
438 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500439
Eric Huang56327082016-04-12 14:57:23 -0400440 if (ret) {
441 count = -EINVAL;
442 goto fail;
443 }
444 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500445 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800446 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400447 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800448
Eric Huangf3898ea2015-12-11 16:24:34 -0500449fail:
450 return count;
451}
452
453static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
456{
457 struct drm_device *ddev = dev_get_drvdata(dev);
458 struct amdgpu_device *adev = ddev->dev_private;
Eric Huangf3898ea2015-12-11 16:24:34 -0500459
Rex Zhucd4d7462017-09-06 18:43:52 +0800460 if (adev->powerplay.pp_funcs->print_clock_levels)
461 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
462 else
463 return snprintf(buf, PAGE_SIZE, "\n");
Eric Huangf3898ea2015-12-11 16:24:34 -0500464}
465
466static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf,
469 size_t count)
470{
471 struct drm_device *ddev = dev_get_drvdata(dev);
472 struct amdgpu_device *adev = ddev->dev_private;
473 int ret;
474 long level;
Eric Huang56327082016-04-12 14:57:23 -0400475 uint32_t i, mask = 0;
476 char sub_str[2];
Eric Huangf3898ea2015-12-11 16:24:34 -0500477
Eric Huang14b33072016-06-14 15:08:22 -0400478 for (i = 0; i < strlen(buf); i++) {
479 if (*(buf + i) == '\n')
480 continue;
Eric Huang56327082016-04-12 14:57:23 -0400481 sub_str[0] = *(buf + i);
482 sub_str[1] = '\0';
483 ret = kstrtol(sub_str, 0, &level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500484
Eric Huang56327082016-04-12 14:57:23 -0400485 if (ret) {
486 count = -EINVAL;
487 goto fail;
488 }
489 mask |= 1 << level;
Eric Huangf3898ea2015-12-11 16:24:34 -0500490 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800491 if (adev->powerplay.pp_funcs->force_clock_level)
Eric Huang56327082016-04-12 14:57:23 -0400492 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
Rex Zhucd4d7462017-09-06 18:43:52 +0800493
Eric Huangf3898ea2015-12-11 16:24:34 -0500494fail:
495 return count;
496}
497
Eric Huang428bafa2016-05-12 14:51:21 -0400498static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
499 struct device_attribute *attr,
500 char *buf)
501{
502 struct drm_device *ddev = dev_get_drvdata(dev);
503 struct amdgpu_device *adev = ddev->dev_private;
504 uint32_t value = 0;
505
Rex Zhucd4d7462017-09-06 18:43:52 +0800506 if (adev->powerplay.pp_funcs->get_sclk_od)
Eric Huang428bafa2016-05-12 14:51:21 -0400507 value = amdgpu_dpm_get_sclk_od(adev);
508
509 return snprintf(buf, PAGE_SIZE, "%d\n", value);
510}
511
512static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
513 struct device_attribute *attr,
514 const char *buf,
515 size_t count)
516{
517 struct drm_device *ddev = dev_get_drvdata(dev);
518 struct amdgpu_device *adev = ddev->dev_private;
519 int ret;
520 long int value;
521
522 ret = kstrtol(buf, 0, &value);
523
524 if (ret) {
525 count = -EINVAL;
526 goto fail;
527 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800528 if (adev->powerplay.pp_funcs->set_sclk_od)
529 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
Eric Huang428bafa2016-05-12 14:51:21 -0400530
Rex Zhu6d07fe72017-09-25 18:51:50 +0800531 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhudf1e6392017-09-01 13:46:20 +0800532 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800533 } else {
Eric Huang8b2e5742016-05-19 15:46:10 -0400534 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
535 amdgpu_pm_compute_clocks(adev);
536 }
Eric Huang428bafa2016-05-12 14:51:21 -0400537
538fail:
539 return count;
540}
541
Eric Huangf2bdc052016-05-24 15:11:17 -0400542static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
543 struct device_attribute *attr,
544 char *buf)
545{
546 struct drm_device *ddev = dev_get_drvdata(dev);
547 struct amdgpu_device *adev = ddev->dev_private;
548 uint32_t value = 0;
549
Rex Zhucd4d7462017-09-06 18:43:52 +0800550 if (adev->powerplay.pp_funcs->get_mclk_od)
Eric Huangf2bdc052016-05-24 15:11:17 -0400551 value = amdgpu_dpm_get_mclk_od(adev);
Eric Huangf2bdc052016-05-24 15:11:17 -0400552
553 return snprintf(buf, PAGE_SIZE, "%d\n", value);
554}
555
556static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
557 struct device_attribute *attr,
558 const char *buf,
559 size_t count)
560{
561 struct drm_device *ddev = dev_get_drvdata(dev);
562 struct amdgpu_device *adev = ddev->dev_private;
563 int ret;
564 long int value;
565
566 ret = kstrtol(buf, 0, &value);
567
568 if (ret) {
569 count = -EINVAL;
570 goto fail;
571 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800572 if (adev->powerplay.pp_funcs->set_mclk_od)
573 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
Eric Huangf2bdc052016-05-24 15:11:17 -0400574
Rex Zhu6d07fe72017-09-25 18:51:50 +0800575 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhudf1e6392017-09-01 13:46:20 +0800576 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
Rex Zhucd4d7462017-09-06 18:43:52 +0800577 } else {
Eric Huangf2bdc052016-05-24 15:11:17 -0400578 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
579 amdgpu_pm_compute_clocks(adev);
580 }
581
582fail:
583 return count;
584}
585
Eric Huang34bb2732016-09-12 16:17:44 -0400586static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
587 char *buf, struct amd_pp_profile *query)
588{
589 struct drm_device *ddev = dev_get_drvdata(dev);
590 struct amdgpu_device *adev = ddev->dev_private;
Rex Zhucd4d7462017-09-06 18:43:52 +0800591 int ret = 0xff;
Eric Huang34bb2732016-09-12 16:17:44 -0400592
Rex Zhucd4d7462017-09-06 18:43:52 +0800593 if (adev->powerplay.pp_funcs->get_power_profile_state)
Eric Huang34bb2732016-09-12 16:17:44 -0400594 ret = amdgpu_dpm_get_power_profile_state(
595 adev, query);
Eric Huang34bb2732016-09-12 16:17:44 -0400596
597 if (ret)
598 return ret;
599
600 return snprintf(buf, PAGE_SIZE,
601 "%d %d %d %d %d\n",
602 query->min_sclk / 100,
603 query->min_mclk / 100,
604 query->activity_threshold,
605 query->up_hyst,
606 query->down_hyst);
607}
608
609static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
610 struct device_attribute *attr,
611 char *buf)
612{
613 struct amd_pp_profile query = {0};
614
615 query.type = AMD_PP_GFX_PROFILE;
616
617 return amdgpu_get_pp_power_profile(dev, buf, &query);
618}
619
620static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
621 struct device_attribute *attr,
622 char *buf)
623{
624 struct amd_pp_profile query = {0};
625
626 query.type = AMD_PP_COMPUTE_PROFILE;
627
628 return amdgpu_get_pp_power_profile(dev, buf, &query);
629}
630
631static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
632 const char *buf,
633 size_t count,
634 struct amd_pp_profile *request)
635{
636 struct drm_device *ddev = dev_get_drvdata(dev);
637 struct amdgpu_device *adev = ddev->dev_private;
638 uint32_t loop = 0;
639 char *sub_str, buf_cpy[128], *tmp_str;
640 const char delimiter[3] = {' ', '\n', '\0'};
641 long int value;
Rex Zhucd4d7462017-09-06 18:43:52 +0800642 int ret = 0xff;
Eric Huang34bb2732016-09-12 16:17:44 -0400643
644 if (strncmp("reset", buf, strlen("reset")) == 0) {
Rex Zhucd4d7462017-09-06 18:43:52 +0800645 if (adev->powerplay.pp_funcs->reset_power_profile_state)
Eric Huang34bb2732016-09-12 16:17:44 -0400646 ret = amdgpu_dpm_reset_power_profile_state(
647 adev, request);
Eric Huang34bb2732016-09-12 16:17:44 -0400648 if (ret) {
649 count = -EINVAL;
650 goto fail;
651 }
652 return count;
653 }
654
655 if (strncmp("set", buf, strlen("set")) == 0) {
Rex Zhucd4d7462017-09-06 18:43:52 +0800656 if (adev->powerplay.pp_funcs->set_power_profile_state)
Eric Huang34bb2732016-09-12 16:17:44 -0400657 ret = amdgpu_dpm_set_power_profile_state(
658 adev, request);
Rex Zhucd4d7462017-09-06 18:43:52 +0800659
Eric Huang34bb2732016-09-12 16:17:44 -0400660 if (ret) {
661 count = -EINVAL;
662 goto fail;
663 }
664 return count;
665 }
666
667 if (count + 1 >= 128) {
668 count = -EINVAL;
669 goto fail;
670 }
671
672 memcpy(buf_cpy, buf, count + 1);
673 tmp_str = buf_cpy;
674
675 while (tmp_str[0]) {
676 sub_str = strsep(&tmp_str, delimiter);
677 ret = kstrtol(sub_str, 0, &value);
678 if (ret) {
679 count = -EINVAL;
680 goto fail;
681 }
682
683 switch (loop) {
684 case 0:
685 /* input unit MHz convert to dpm table unit 10KHz*/
686 request->min_sclk = (uint32_t)value * 100;
687 break;
688 case 1:
689 /* input unit MHz convert to dpm table unit 10KHz*/
690 request->min_mclk = (uint32_t)value * 100;
691 break;
692 case 2:
693 request->activity_threshold = (uint16_t)value;
694 break;
695 case 3:
696 request->up_hyst = (uint8_t)value;
697 break;
698 case 4:
699 request->down_hyst = (uint8_t)value;
700 break;
701 default:
702 break;
703 }
704
705 loop++;
706 }
Rex Zhucd4d7462017-09-06 18:43:52 +0800707 if (adev->powerplay.pp_funcs->set_power_profile_state)
708 ret = amdgpu_dpm_set_power_profile_state(adev, request);
Eric Huang34bb2732016-09-12 16:17:44 -0400709
710 if (ret)
711 count = -EINVAL;
712
713fail:
714 return count;
715}
716
717static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
718 struct device_attribute *attr,
719 const char *buf,
720 size_t count)
721{
722 struct amd_pp_profile request = {0};
723
724 request.type = AMD_PP_GFX_PROFILE;
725
726 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
727}
728
729static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
730 struct device_attribute *attr,
731 const char *buf,
732 size_t count)
733{
734 struct amd_pp_profile request = {0};
735
736 request.type = AMD_PP_COMPUTE_PROFILE;
737
738 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
739}
740
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
742static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
743 amdgpu_get_dpm_forced_performance_level,
744 amdgpu_set_dpm_forced_performance_level);
Eric Huangf3898ea2015-12-11 16:24:34 -0500745static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
746static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
747static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
748 amdgpu_get_pp_force_state,
749 amdgpu_set_pp_force_state);
750static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
751 amdgpu_get_pp_table,
752 amdgpu_set_pp_table);
753static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
754 amdgpu_get_pp_dpm_sclk,
755 amdgpu_set_pp_dpm_sclk);
756static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
757 amdgpu_get_pp_dpm_mclk,
758 amdgpu_set_pp_dpm_mclk);
759static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
760 amdgpu_get_pp_dpm_pcie,
761 amdgpu_set_pp_dpm_pcie);
Eric Huang428bafa2016-05-12 14:51:21 -0400762static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
763 amdgpu_get_pp_sclk_od,
764 amdgpu_set_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -0400765static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
766 amdgpu_get_pp_mclk_od,
767 amdgpu_set_pp_mclk_od);
Eric Huang34bb2732016-09-12 16:17:44 -0400768static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
769 amdgpu_get_pp_gfx_power_profile,
770 amdgpu_set_pp_gfx_power_profile);
771static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
772 amdgpu_get_pp_compute_power_profile,
773 amdgpu_set_pp_compute_power_profile);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400774
775static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
776 struct device_attribute *attr,
777 char *buf)
778{
779 struct amdgpu_device *adev = dev_get_drvdata(dev);
Alex Deucher0c67df42016-02-19 15:30:15 -0500780 struct drm_device *ddev = adev->ddev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781 int temp;
782
Alex Deucher0c67df42016-02-19 15:30:15 -0500783 /* Can't get temperature when the card is off */
784 if ((adev->flags & AMD_IS_PX) &&
785 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
786 return -EINVAL;
787
Rex Zhucd4d7462017-09-06 18:43:52 +0800788 if (!adev->powerplay.pp_funcs->get_temperature)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 temp = 0;
Rex Zhu8804b8d2015-11-10 18:29:11 -0500790 else
791 temp = amdgpu_dpm_get_temperature(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792
793 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
794}
795
796static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
797 struct device_attribute *attr,
798 char *buf)
799{
800 struct amdgpu_device *adev = dev_get_drvdata(dev);
801 int hyst = to_sensor_dev_attr(attr)->index;
802 int temp;
803
804 if (hyst)
805 temp = adev->pm.dpm.thermal.min_temp;
806 else
807 temp = adev->pm.dpm.thermal.max_temp;
808
809 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
810}
811
812static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
813 struct device_attribute *attr,
814 char *buf)
815{
816 struct amdgpu_device *adev = dev_get_drvdata(dev);
817 u32 pwm_mode = 0;
818
Rex Zhucd4d7462017-09-06 18:43:52 +0800819 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
Rex Zhu8804b8d2015-11-10 18:29:11 -0500820 return -EINVAL;
821
822 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823
Rex Zhuaad22ca2017-05-05 16:56:45 +0800824 return sprintf(buf, "%i\n", pwm_mode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400825}
826
827static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
828 struct device_attribute *attr,
829 const char *buf,
830 size_t count)
831{
832 struct amdgpu_device *adev = dev_get_drvdata(dev);
833 int err;
834 int value;
835
Rex Zhucd4d7462017-09-06 18:43:52 +0800836 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837 return -EINVAL;
838
839 err = kstrtoint(buf, 10, &value);
840 if (err)
841 return err;
842
Rex Zhuaad22ca2017-05-05 16:56:45 +0800843 amdgpu_dpm_set_fan_control_mode(adev, value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844
845 return count;
846}
847
848static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
849 struct device_attribute *attr,
850 char *buf)
851{
852 return sprintf(buf, "%i\n", 0);
853}
854
855static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
856 struct device_attribute *attr,
857 char *buf)
858{
859 return sprintf(buf, "%i\n", 255);
860}
861
862static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
863 struct device_attribute *attr,
864 const char *buf, size_t count)
865{
866 struct amdgpu_device *adev = dev_get_drvdata(dev);
867 int err;
868 u32 value;
869
870 err = kstrtou32(buf, 10, &value);
871 if (err)
872 return err;
873
874 value = (value * 100) / 255;
875
Rex Zhucd4d7462017-09-06 18:43:52 +0800876 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
877 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
878 if (err)
879 return err;
880 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881
882 return count;
883}
884
885static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
886 struct device_attribute *attr,
887 char *buf)
888{
889 struct amdgpu_device *adev = dev_get_drvdata(dev);
890 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +0800891 u32 speed = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892
Rex Zhucd4d7462017-09-06 18:43:52 +0800893 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
894 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
895 if (err)
896 return err;
897 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898
899 speed = (speed * 255) / 100;
900
901 return sprintf(buf, "%i\n", speed);
902}
903
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300904static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
905 struct device_attribute *attr,
906 char *buf)
907{
908 struct amdgpu_device *adev = dev_get_drvdata(dev);
909 int err;
Rex Zhucd4d7462017-09-06 18:43:52 +0800910 u32 speed = 0;
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300911
Rex Zhucd4d7462017-09-06 18:43:52 +0800912 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
913 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
914 if (err)
915 return err;
916 }
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300917
918 return sprintf(buf, "%i\n", speed);
919}
920
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
922static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
923static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
924static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
925static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
926static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
927static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300928static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929
930static struct attribute *hwmon_attributes[] = {
931 &sensor_dev_attr_temp1_input.dev_attr.attr,
932 &sensor_dev_attr_temp1_crit.dev_attr.attr,
933 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
934 &sensor_dev_attr_pwm1.dev_attr.attr,
935 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
936 &sensor_dev_attr_pwm1_min.dev_attr.attr,
937 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300938 &sensor_dev_attr_fan1_input.dev_attr.attr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939 NULL
940};
941
942static umode_t hwmon_attributes_visible(struct kobject *kobj,
943 struct attribute *attr, int index)
944{
Geliang Tangcc29ec82016-01-13 22:48:42 +0800945 struct device *dev = kobj_to_dev(kobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 struct amdgpu_device *adev = dev_get_drvdata(dev);
947 umode_t effective_mode = attr->mode;
948
Alex Deucher135f9712017-11-20 17:49:53 -0500949 /* no skipping for powerplay */
950 if (adev->powerplay.cgs_device)
951 return effective_mode;
952
Rex Zhu1b5708f2015-11-10 18:25:24 -0500953 /* Skip limit attributes if DPM is not enabled */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 if (!adev->pm.dpm_enabled &&
955 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
Alex Deucher27100732015-10-19 15:49:11 -0400956 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
957 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
958 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
959 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
960 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 return 0;
962
963 /* Skip fan attributes if fan is not present */
964 if (adev->pm.no_fan &&
965 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
966 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
967 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
968 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
969 return 0;
970
971 /* mask fan attributes if we have no bindings for this asic to expose */
Rex Zhucd4d7462017-09-06 18:43:52 +0800972 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
Rex Zhucd4d7462017-09-06 18:43:52 +0800974 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
976 effective_mode &= ~S_IRUGO;
977
Rex Zhucd4d7462017-09-06 18:43:52 +0800978 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
Rex Zhucd4d7462017-09-06 18:43:52 +0800980 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
982 effective_mode &= ~S_IWUSR;
983
984 /* hide max/min values if we can't both query and manage the fan */
Rex Zhucd4d7462017-09-06 18:43:52 +0800985 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
986 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400987 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
988 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
989 return 0;
990
Grazvydas Ignotas81c15142016-10-29 23:28:59 +0300991 /* requires powerplay */
992 if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
993 return 0;
994
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 return effective_mode;
996}
997
998static const struct attribute_group hwmon_attrgroup = {
999 .attrs = hwmon_attributes,
1000 .is_visible = hwmon_attributes_visible,
1001};
1002
1003static const struct attribute_group *hwmon_groups[] = {
1004 &hwmon_attrgroup,
1005 NULL
1006};
1007
1008void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1009{
1010 struct amdgpu_device *adev =
1011 container_of(work, struct amdgpu_device,
1012 pm.dpm.thermal.work);
1013 /* switch to the thermal state */
Rex Zhu3a2c7882015-08-25 15:57:43 +08001014 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015
1016 if (!adev->pm.dpm_enabled)
1017 return;
1018
Rex Zhucd4d7462017-09-06 18:43:52 +08001019 if (adev->powerplay.pp_funcs->get_temperature) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 int temp = amdgpu_dpm_get_temperature(adev);
1021
1022 if (temp < adev->pm.dpm.thermal.min_temp)
1023 /* switch back the user state */
1024 dpm_state = adev->pm.dpm.user_state;
1025 } else {
1026 if (adev->pm.dpm.thermal.high_to_low)
1027 /* switch back the user state */
1028 dpm_state = adev->pm.dpm.user_state;
1029 }
1030 mutex_lock(&adev->pm.mutex);
1031 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1032 adev->pm.dpm.thermal_active = true;
1033 else
1034 adev->pm.dpm.thermal_active = false;
1035 adev->pm.dpm.state = dpm_state;
1036 mutex_unlock(&adev->pm.mutex);
1037
1038 amdgpu_pm_compute_clocks(adev);
1039}
1040
1041static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
Rex Zhu3a2c7882015-08-25 15:57:43 +08001042 enum amd_pm_state_type dpm_state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043{
1044 int i;
1045 struct amdgpu_ps *ps;
1046 u32 ui_class;
1047 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1048 true : false;
1049
1050 /* check if the vblank period is too short to adjust the mclk */
Rex Zhucd4d7462017-09-06 18:43:52 +08001051 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052 if (amdgpu_dpm_vblank_too_short(adev))
1053 single_display = false;
1054 }
1055
1056 /* certain older asics have a separare 3D performance state,
1057 * so try that first if the user selected performance
1058 */
1059 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1060 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1061 /* balanced states don't exist at the moment */
1062 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1063 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1064
1065restart_search:
1066 /* Pick the best power state based on current conditions */
1067 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1068 ps = &adev->pm.dpm.ps[i];
1069 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1070 switch (dpm_state) {
1071 /* user states */
1072 case POWER_STATE_TYPE_BATTERY:
1073 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1074 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1075 if (single_display)
1076 return ps;
1077 } else
1078 return ps;
1079 }
1080 break;
1081 case POWER_STATE_TYPE_BALANCED:
1082 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1083 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1084 if (single_display)
1085 return ps;
1086 } else
1087 return ps;
1088 }
1089 break;
1090 case POWER_STATE_TYPE_PERFORMANCE:
1091 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1092 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1093 if (single_display)
1094 return ps;
1095 } else
1096 return ps;
1097 }
1098 break;
1099 /* internal states */
1100 case POWER_STATE_TYPE_INTERNAL_UVD:
1101 if (adev->pm.dpm.uvd_ps)
1102 return adev->pm.dpm.uvd_ps;
1103 else
1104 break;
1105 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1106 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1107 return ps;
1108 break;
1109 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1110 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1111 return ps;
1112 break;
1113 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1114 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1115 return ps;
1116 break;
1117 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1118 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1119 return ps;
1120 break;
1121 case POWER_STATE_TYPE_INTERNAL_BOOT:
1122 return adev->pm.dpm.boot_ps;
1123 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1124 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1125 return ps;
1126 break;
1127 case POWER_STATE_TYPE_INTERNAL_ACPI:
1128 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1129 return ps;
1130 break;
1131 case POWER_STATE_TYPE_INTERNAL_ULV:
1132 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1133 return ps;
1134 break;
1135 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1136 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1137 return ps;
1138 break;
1139 default:
1140 break;
1141 }
1142 }
1143 /* use a fallback state if we didn't match */
1144 switch (dpm_state) {
1145 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1146 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1147 goto restart_search;
1148 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1149 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1150 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1151 if (adev->pm.dpm.uvd_ps) {
1152 return adev->pm.dpm.uvd_ps;
1153 } else {
1154 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1155 goto restart_search;
1156 }
1157 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1158 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1159 goto restart_search;
1160 case POWER_STATE_TYPE_INTERNAL_ACPI:
1161 dpm_state = POWER_STATE_TYPE_BATTERY;
1162 goto restart_search;
1163 case POWER_STATE_TYPE_BATTERY:
1164 case POWER_STATE_TYPE_BALANCED:
1165 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1166 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1167 goto restart_search;
1168 default:
1169 break;
1170 }
1171
1172 return NULL;
1173}
1174
1175static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1176{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 struct amdgpu_ps *ps;
Rex Zhu3a2c7882015-08-25 15:57:43 +08001178 enum amd_pm_state_type dpm_state;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179 int ret;
Rex Zhucd4d7462017-09-06 18:43:52 +08001180 bool equal = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181
1182 /* if dpm init failed */
1183 if (!adev->pm.dpm_enabled)
1184 return;
1185
1186 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1187 /* add other state override checks here */
1188 if ((!adev->pm.dpm.thermal_active) &&
1189 (!adev->pm.dpm.uvd_active))
1190 adev->pm.dpm.state = adev->pm.dpm.user_state;
1191 }
1192 dpm_state = adev->pm.dpm.state;
1193
1194 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1195 if (ps)
1196 adev->pm.dpm.requested_ps = ps;
1197 else
1198 return;
1199
Rex Zhucd4d7462017-09-06 18:43:52 +08001200 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 printk("switching from power state:\n");
1202 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1203 printk("switching to power state:\n");
1204 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1205 }
1206
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 /* update whether vce is active */
1208 ps->vce_active = adev->pm.dpm.vce_active;
Rex Zhucd4d7462017-09-06 18:43:52 +08001209 if (adev->powerplay.pp_funcs->display_configuration_changed)
1210 amdgpu_dpm_display_configuration_changed(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001211
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 ret = amdgpu_dpm_pre_set_power_state(adev);
1213 if (ret)
Christian Königa27de352016-01-21 11:28:53 +01001214 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215
Rex Zhucd4d7462017-09-06 18:43:52 +08001216 if (adev->powerplay.pp_funcs->check_state_equal) {
1217 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1218 equal = false;
1219 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220
Rex Zhu5e876c62016-10-14 19:23:34 +08001221 if (equal)
1222 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224 amdgpu_dpm_set_power_state(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 amdgpu_dpm_post_set_power_state(adev);
1226
Alex Deuchereda1d1c2016-02-24 17:18:25 -05001227 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1228 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1229
Rex Zhucd4d7462017-09-06 18:43:52 +08001230 if (adev->powerplay.pp_funcs->force_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 if (adev->pm.dpm.thermal_active) {
Rex Zhue5d03ac2016-12-23 14:39:41 +08001232 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 /* force low perf level for thermal */
Rex Zhue5d03ac2016-12-23 14:39:41 +08001234 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001235 /* save the user's level */
1236 adev->pm.dpm.forced_level = level;
1237 } else {
1238 /* otherwise, user selected level */
1239 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1240 }
1241 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001242}
1243
1244void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1245{
Rex Zhucd4d7462017-09-06 18:43:52 +08001246 if (adev->powerplay.pp_funcs->powergate_uvd) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001247 /* enable/disable UVD */
1248 mutex_lock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 amdgpu_dpm_powergate_uvd(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001250 mutex_unlock(&adev->pm.mutex);
1251 } else {
1252 if (enable) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001254 adev->pm.dpm.uvd_active = true;
1255 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 mutex_unlock(&adev->pm.mutex);
1257 } else {
Tom St Denise95a14a2016-07-28 09:40:07 -04001258 mutex_lock(&adev->pm.mutex);
1259 adev->pm.dpm.uvd_active = false;
1260 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 }
Tom St Denise95a14a2016-07-28 09:40:07 -04001262 amdgpu_pm_compute_clocks(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 }
1264}
1265
1266void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1267{
Rex Zhucd4d7462017-09-06 18:43:52 +08001268 if (adev->powerplay.pp_funcs->powergate_vce) {
Tom St Denise95a14a2016-07-28 09:40:07 -04001269 /* enable/disable VCE */
1270 mutex_lock(&adev->pm.mutex);
Sonny Jiangb7a07762015-05-28 15:47:53 -04001271 amdgpu_dpm_powergate_vce(adev, !enable);
Tom St Denise95a14a2016-07-28 09:40:07 -04001272 mutex_unlock(&adev->pm.mutex);
1273 } else {
1274 if (enable) {
Sonny Jiangb7a07762015-05-28 15:47:53 -04001275 mutex_lock(&adev->pm.mutex);
Tom St Denise95a14a2016-07-28 09:40:07 -04001276 adev->pm.dpm.vce_active = true;
1277 /* XXX select vce level based on ring/task */
Rex Zhu0d8de7c2016-10-12 15:13:29 +08001278 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
Sonny Jiangb7a07762015-05-28 15:47:53 -04001279 mutex_unlock(&adev->pm.mutex);
Rex Zhubeeea982017-01-26 16:25:05 +08001280 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1281 AMD_CG_STATE_UNGATE);
Rex Zhu03a5f1d2017-03-06 11:29:26 +08001282 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1283 AMD_PG_STATE_UNGATE);
1284 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a07762015-05-28 15:47:53 -04001285 } else {
Rex Zhubeeea982017-01-26 16:25:05 +08001286 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1287 AMD_PG_STATE_GATE);
1288 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1289 AMD_CG_STATE_GATE);
Tom St Denise95a14a2016-07-28 09:40:07 -04001290 mutex_lock(&adev->pm.mutex);
1291 adev->pm.dpm.vce_active = false;
1292 mutex_unlock(&adev->pm.mutex);
Rex Zhubeeea982017-01-26 16:25:05 +08001293 amdgpu_pm_compute_clocks(adev);
Sonny Jiangb7a07762015-05-28 15:47:53 -04001294 }
Rex Zhubeeea982017-01-26 16:25:05 +08001295
Sonny Jiangb7a07762015-05-28 15:47:53 -04001296 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297}
1298
1299void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1300{
1301 int i;
1302
Rex Zhucd4d7462017-09-06 18:43:52 +08001303 if (adev->powerplay.pp_funcs->print_power_state == NULL)
Rex Zhu1b5708f2015-11-10 18:25:24 -05001304 return;
1305
1306 for (i = 0; i < adev->pm.dpm.num_ps; i++)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001308
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309}
1310
1311int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1312{
1313 int ret;
1314
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001315 if (adev->pm.sysfs_initialized)
1316 return 0;
1317
Rex Zhud2f52ac2017-09-22 17:47:27 +08001318 if (adev->pm.dpm_enabled == 0)
1319 return 0;
1320
Rex Zhucd4d7462017-09-06 18:43:52 +08001321 if (adev->powerplay.pp_funcs->get_temperature == NULL)
1322 return 0;
Rex Zhu1b5708f2015-11-10 18:25:24 -05001323
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1325 DRIVER_NAME, adev,
1326 hwmon_groups);
1327 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1328 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1329 dev_err(adev->dev,
1330 "Unable to register hwmon device: %d\n", ret);
1331 return ret;
1332 }
1333
1334 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1335 if (ret) {
1336 DRM_ERROR("failed to create device file for dpm state\n");
1337 return ret;
1338 }
1339 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1340 if (ret) {
1341 DRM_ERROR("failed to create device file for dpm state\n");
1342 return ret;
1343 }
Eric Huangf3898ea2015-12-11 16:24:34 -05001344
Rex Zhu6d07fe72017-09-25 18:51:50 +08001345
1346 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1347 if (ret) {
1348 DRM_ERROR("failed to create device file pp_num_states\n");
1349 return ret;
1350 }
1351 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1352 if (ret) {
1353 DRM_ERROR("failed to create device file pp_cur_state\n");
1354 return ret;
1355 }
1356 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1357 if (ret) {
1358 DRM_ERROR("failed to create device file pp_force_state\n");
1359 return ret;
1360 }
1361 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1362 if (ret) {
1363 DRM_ERROR("failed to create device file pp_table\n");
1364 return ret;
Eric Huangf3898ea2015-12-11 16:24:34 -05001365 }
Eric Huangc85e2992016-05-19 15:41:25 -04001366
1367 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1368 if (ret) {
1369 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1370 return ret;
1371 }
1372 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1373 if (ret) {
1374 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1375 return ret;
1376 }
1377 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1378 if (ret) {
1379 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1380 return ret;
1381 }
Eric Huang8b2e5742016-05-19 15:46:10 -04001382 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1383 if (ret) {
1384 DRM_ERROR("failed to create device file pp_sclk_od\n");
1385 return ret;
1386 }
Eric Huangf2bdc052016-05-24 15:11:17 -04001387 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1388 if (ret) {
1389 DRM_ERROR("failed to create device file pp_mclk_od\n");
1390 return ret;
1391 }
Eric Huang34bb2732016-09-12 16:17:44 -04001392 ret = device_create_file(adev->dev,
1393 &dev_attr_pp_gfx_power_profile);
1394 if (ret) {
1395 DRM_ERROR("failed to create device file "
1396 "pp_gfx_power_profile\n");
1397 return ret;
1398 }
1399 ret = device_create_file(adev->dev,
1400 &dev_attr_pp_compute_power_profile);
1401 if (ret) {
1402 DRM_ERROR("failed to create device file "
1403 "pp_compute_power_profile\n");
1404 return ret;
1405 }
Eric Huangc85e2992016-05-19 15:41:25 -04001406
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 ret = amdgpu_debugfs_pm_init(adev);
1408 if (ret) {
1409 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1410 return ret;
1411 }
1412
Alex Deucherc86f5ebf2015-10-23 10:45:14 -04001413 adev->pm.sysfs_initialized = true;
1414
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 return 0;
1416}
1417
1418void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1419{
Rex Zhud2f52ac2017-09-22 17:47:27 +08001420 if (adev->pm.dpm_enabled == 0)
1421 return;
1422
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 if (adev->pm.int_hwmon_dev)
1424 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1425 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1426 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
Rex Zhu6d07fe72017-09-25 18:51:50 +08001427
1428 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1429 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1430 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1431 device_remove_file(adev->dev, &dev_attr_pp_table);
1432
Eric Huangc85e2992016-05-19 15:41:25 -04001433 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1434 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1435 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
Eric Huang8b2e5742016-05-19 15:46:10 -04001436 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
Eric Huangf2bdc052016-05-24 15:11:17 -04001437 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
Eric Huang34bb2732016-09-12 16:17:44 -04001438 device_remove_file(adev->dev,
1439 &dev_attr_pp_gfx_power_profile);
1440 device_remove_file(adev->dev,
1441 &dev_attr_pp_compute_power_profile);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001442}
1443
1444void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1445{
1446 struct drm_device *ddev = adev->ddev;
1447 struct drm_crtc *crtc;
1448 struct amdgpu_crtc *amdgpu_crtc;
Rex Zhu5e876c62016-10-14 19:23:34 +08001449 int i = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450
1451 if (!adev->pm.dpm_enabled)
1452 return;
1453
Alex Deucherc10c8f72017-02-10 18:09:32 -05001454 if (adev->mode_info.num_crtc)
1455 amdgpu_display_bandwidth_update(adev);
Rex Zhu5e876c62016-10-14 19:23:34 +08001456
1457 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1458 struct amdgpu_ring *ring = adev->rings[i];
1459 if (ring && ring->ready)
1460 amdgpu_fence_wait_empty(ring);
1461 }
1462
Rex Zhu6d07fe72017-09-25 18:51:50 +08001463 if (adev->powerplay.pp_funcs->dispatch_tasks) {
Rex Zhudf1e6392017-09-01 13:46:20 +08001464 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL, NULL);
Rex Zhu1b5708f2015-11-10 18:25:24 -05001465 } else {
1466 mutex_lock(&adev->pm.mutex);
1467 adev->pm.dpm.new_active_crtcs = 0;
1468 adev->pm.dpm.new_active_crtc_count = 0;
1469 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
1470 list_for_each_entry(crtc,
1471 &ddev->mode_config.crtc_list, head) {
1472 amdgpu_crtc = to_amdgpu_crtc(crtc);
Harry Wentland45622362017-09-12 15:58:20 -04001473 if (amdgpu_crtc->enabled) {
Rex Zhu1b5708f2015-11-10 18:25:24 -05001474 adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
1475 adev->pm.dpm.new_active_crtc_count++;
1476 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477 }
1478 }
Rex Zhu1b5708f2015-11-10 18:25:24 -05001479 /* update battery/ac status */
1480 if (power_supply_is_system_supplied() > 0)
1481 adev->pm.dpm.ac_power = true;
1482 else
1483 adev->pm.dpm.ac_power = false;
1484
1485 amdgpu_dpm_change_power_state_locked(adev);
1486
1487 mutex_unlock(&adev->pm.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489}
1490
1491/*
1492 * Debugfs info
1493 */
1494#if defined(CONFIG_DEBUG_FS)
1495
Tom St Denis3de4ec52016-09-19 12:48:52 -04001496static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1497{
Eric Huangcd7b0c62017-02-07 16:37:48 -05001498 uint32_t value;
Eric Huang4f9afc92017-01-24 16:59:27 -05001499 struct pp_gpu_power query = {0};
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001500 int size;
Tom St Denis3de4ec52016-09-19 12:48:52 -04001501
1502 /* sanity check PP is enabled */
1503 if (!(adev->powerplay.pp_funcs &&
1504 adev->powerplay.pp_funcs->read_sensor))
1505 return -EINVAL;
1506
1507 /* GPU Clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001508 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001509 seq_printf(m, "GFX Clocks and Power:\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001510 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001511 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001512 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001513 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001514 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001515 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001516 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001517 seq_printf(m, "\t%u mV (VDDNB)\n", value);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001518 size = sizeof(query);
1519 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
Eric Huang4f9afc92017-01-24 16:59:27 -05001520 seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
1521 query.vddc_power & 0xff);
1522 seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
1523 query.vddci_power & 0xff);
1524 seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
1525 query.max_gpu_power & 0xff);
1526 seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
1527 query.average_gpu_power & 0xff);
1528 }
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001529 size = sizeof(value);
Tom St Denis3de4ec52016-09-19 12:48:52 -04001530 seq_printf(m, "\n");
1531
1532 /* GPU Temp */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001533 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001534 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1535
1536 /* GPU Load */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001537 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001538 seq_printf(m, "GPU Load: %u %%\n", value);
1539 seq_printf(m, "\n");
1540
1541 /* UVD clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001542 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04001543 if (!value) {
1544 seq_printf(m, "UVD: Disabled\n");
1545 } else {
1546 seq_printf(m, "UVD: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001547 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001548 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001549 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001550 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1551 }
1552 }
1553 seq_printf(m, "\n");
1554
1555 /* VCE clocks */
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001556 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
Tom St Denis3de4ec52016-09-19 12:48:52 -04001557 if (!value) {
1558 seq_printf(m, "VCE: Disabled\n");
1559 } else {
1560 seq_printf(m, "VCE: Enabled\n");
Tom St Denis9f8df7d2017-02-09 14:29:01 -05001561 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
Tom St Denis3de4ec52016-09-19 12:48:52 -04001562 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
1563 }
1564 }
1565
1566 return 0;
1567}
1568
Huang Ruia8503b12017-01-05 19:17:13 +08001569static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
1570{
1571 int i;
1572
1573 for (i = 0; clocks[i].flag; i++)
1574 seq_printf(m, "\t%s: %s\n", clocks[i].name,
1575 (flags & clocks[i].flag) ? "On" : "Off");
1576}
1577
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001578static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
1579{
1580 struct drm_info_node *node = (struct drm_info_node *) m->private;
1581 struct drm_device *dev = node->minor->dev;
1582 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher0c67df42016-02-19 15:30:15 -05001583 struct drm_device *ddev = adev->ddev;
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001584 u32 flags = 0;
1585
1586 amdgpu_get_clockgating_state(adev, &flags);
1587 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
Huang Ruia8503b12017-01-05 19:17:13 +08001588 amdgpu_parse_cg_state(m, flags);
1589 seq_printf(m, "\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001590
Rex Zhu1b5708f2015-11-10 18:25:24 -05001591 if (!adev->pm.dpm_enabled) {
1592 seq_printf(m, "dpm not enabled\n");
1593 return 0;
1594 }
Alex Deucher0c67df42016-02-19 15:30:15 -05001595 if ((adev->flags & AMD_IS_PX) &&
1596 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1597 seq_printf(m, "PX asic powered off\n");
Rex Zhu6d07fe72017-09-25 18:51:50 +08001598 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599 mutex_lock(&adev->pm.mutex);
Rex Zhucd4d7462017-09-06 18:43:52 +08001600 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
1601 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 else
1603 seq_printf(m, "Debugfs support not implemented for this asic\n");
1604 mutex_unlock(&adev->pm.mutex);
Rex Zhu6d07fe72017-09-25 18:51:50 +08001605 } else {
1606 return amdgpu_debugfs_pm_info_pp(m, adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 }
1608
1609 return 0;
1610}
1611
Nils Wallménius06ab6832016-05-02 12:46:15 -04001612static const struct drm_info_list amdgpu_pm_info_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
1614};
1615#endif
1616
1617static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
1618{
1619#if defined(CONFIG_DEBUG_FS)
1620 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
1621#else
1622 return 0;
1623#endif
1624}