2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * 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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include "amdgpu_drv.h"
28 #include "amdgpu_pm.h"
29 #include "amdgpu_dpm.h"
31 #include <linux/power_supply.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
36 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
38 static 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"},
43 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
44 {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"},
47 {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"},
49 {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"},
53 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
54 {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"},
59 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
60 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
61 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
62 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
66 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
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;
73 adev->pm.dpm.ac_power = false;
74 if (adev->powerplay.pp_funcs->enable_bapm)
75 amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
76 mutex_unlock(&adev->pm.mutex);
80 static ssize_t amdgpu_get_dpm_state(struct device *dev,
81 struct device_attribute *attr,
84 struct drm_device *ddev = dev_get_drvdata(dev);
85 struct amdgpu_device *adev = ddev->dev_private;
86 enum amd_pm_state_type pm;
88 if (adev->powerplay.pp_funcs->get_current_power_state)
89 pm = amdgpu_dpm_get_current_power_state(adev);
91 pm = adev->pm.dpm.user_state;
93 return snprintf(buf, PAGE_SIZE, "%s\n",
94 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
95 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
98 static ssize_t amdgpu_set_dpm_state(struct device *dev,
99 struct device_attribute *attr,
103 struct drm_device *ddev = dev_get_drvdata(dev);
104 struct amdgpu_device *adev = ddev->dev_private;
105 enum amd_pm_state_type state;
107 if (strncmp("battery", buf, strlen("battery")) == 0)
108 state = POWER_STATE_TYPE_BATTERY;
109 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
110 state = POWER_STATE_TYPE_BALANCED;
111 else if (strncmp("performance", buf, strlen("performance")) == 0)
112 state = POWER_STATE_TYPE_PERFORMANCE;
118 if (adev->powerplay.pp_funcs->dispatch_tasks) {
119 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state, NULL);
121 mutex_lock(&adev->pm.mutex);
122 adev->pm.dpm.user_state = state;
123 mutex_unlock(&adev->pm.mutex);
125 /* Can't set dpm state when the card is off */
126 if (!(adev->flags & AMD_IS_PX) ||
127 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
128 amdgpu_pm_compute_clocks(adev);
134 static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
135 struct device_attribute *attr,
138 struct drm_device *ddev = dev_get_drvdata(dev);
139 struct amdgpu_device *adev = ddev->dev_private;
140 enum amd_dpm_forced_level level = 0xff;
142 if ((adev->flags & AMD_IS_PX) &&
143 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
144 return snprintf(buf, PAGE_SIZE, "off\n");
146 if (adev->powerplay.pp_funcs->get_performance_level)
147 level = amdgpu_dpm_get_performance_level(adev);
149 level = adev->pm.dpm.forced_level;
151 return snprintf(buf, PAGE_SIZE, "%s\n",
152 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
153 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
154 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
155 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
156 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
157 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
158 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
159 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
163 static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
164 struct device_attribute *attr,
168 struct drm_device *ddev = dev_get_drvdata(dev);
169 struct amdgpu_device *adev = ddev->dev_private;
170 enum amd_dpm_forced_level level;
171 enum amd_dpm_forced_level current_level = 0xff;
174 /* Can't force performance level when the card is off */
175 if ((adev->flags & AMD_IS_PX) &&
176 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
179 if (adev->powerplay.pp_funcs->get_performance_level)
180 current_level = amdgpu_dpm_get_performance_level(adev);
182 if (strncmp("low", buf, strlen("low")) == 0) {
183 level = AMD_DPM_FORCED_LEVEL_LOW;
184 } else if (strncmp("high", buf, strlen("high")) == 0) {
185 level = AMD_DPM_FORCED_LEVEL_HIGH;
186 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
187 level = AMD_DPM_FORCED_LEVEL_AUTO;
188 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
189 level = AMD_DPM_FORCED_LEVEL_MANUAL;
190 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
191 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
192 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
193 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
194 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
195 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
196 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
197 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
198 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
199 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
205 if (current_level == level)
208 if (adev->powerplay.pp_funcs->force_performance_level) {
209 mutex_lock(&adev->pm.mutex);
210 if (adev->pm.dpm.thermal_active) {
212 mutex_unlock(&adev->pm.mutex);
215 ret = amdgpu_dpm_force_performance_level(adev, level);
219 adev->pm.dpm.forced_level = level;
220 mutex_unlock(&adev->pm.mutex);
227 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
228 struct device_attribute *attr,
231 struct drm_device *ddev = dev_get_drvdata(dev);
232 struct amdgpu_device *adev = ddev->dev_private;
233 struct pp_states_info data;
236 if (adev->powerplay.pp_funcs->get_pp_num_states)
237 amdgpu_dpm_get_pp_num_states(adev, &data);
239 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
240 for (i = 0; i < data.nums; i++)
241 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
242 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
243 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
244 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
245 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
250 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
251 struct device_attribute *attr,
254 struct drm_device *ddev = dev_get_drvdata(dev);
255 struct amdgpu_device *adev = ddev->dev_private;
256 struct pp_states_info data;
257 enum amd_pm_state_type pm = 0;
260 if (adev->powerplay.pp_funcs->get_current_power_state
261 && adev->powerplay.pp_funcs->get_pp_num_states) {
262 pm = amdgpu_dpm_get_current_power_state(adev);
263 amdgpu_dpm_get_pp_num_states(adev, &data);
265 for (i = 0; i < data.nums; i++) {
266 if (pm == data.states[i])
274 return snprintf(buf, PAGE_SIZE, "%d\n", i);
277 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
278 struct device_attribute *attr,
281 struct drm_device *ddev = dev_get_drvdata(dev);
282 struct amdgpu_device *adev = ddev->dev_private;
284 if (adev->pp_force_state_enabled)
285 return amdgpu_get_pp_cur_state(dev, attr, buf);
287 return snprintf(buf, PAGE_SIZE, "\n");
290 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
291 struct device_attribute *attr,
295 struct drm_device *ddev = dev_get_drvdata(dev);
296 struct amdgpu_device *adev = ddev->dev_private;
297 enum amd_pm_state_type state = 0;
301 if (strlen(buf) == 1)
302 adev->pp_force_state_enabled = false;
303 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
304 adev->powerplay.pp_funcs->get_pp_num_states) {
305 struct pp_states_info data;
307 ret = kstrtoul(buf, 0, &idx);
308 if (ret || idx >= ARRAY_SIZE(data.states)) {
313 amdgpu_dpm_get_pp_num_states(adev, &data);
314 state = data.states[idx];
315 /* only set user selected power states */
316 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
317 state != POWER_STATE_TYPE_DEFAULT) {
318 amdgpu_dpm_dispatch_task(adev,
319 AMD_PP_TASK_ENABLE_USER_STATE, &state, NULL);
320 adev->pp_force_state_enabled = true;
327 static ssize_t amdgpu_get_pp_table(struct device *dev,
328 struct device_attribute *attr,
331 struct drm_device *ddev = dev_get_drvdata(dev);
332 struct amdgpu_device *adev = ddev->dev_private;
336 if (adev->powerplay.pp_funcs->get_pp_table)
337 size = amdgpu_dpm_get_pp_table(adev, &table);
341 if (size >= PAGE_SIZE)
342 size = PAGE_SIZE - 1;
344 memcpy(buf, table, size);
349 static ssize_t amdgpu_set_pp_table(struct device *dev,
350 struct device_attribute *attr,
354 struct drm_device *ddev = dev_get_drvdata(dev);
355 struct amdgpu_device *adev = ddev->dev_private;
357 if (adev->powerplay.pp_funcs->set_pp_table)
358 amdgpu_dpm_set_pp_table(adev, buf, count);
363 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
364 struct device_attribute *attr,
367 struct drm_device *ddev = dev_get_drvdata(dev);
368 struct amdgpu_device *adev = ddev->dev_private;
370 if (adev->powerplay.pp_funcs->print_clock_levels)
371 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
373 return snprintf(buf, PAGE_SIZE, "\n");
376 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
377 struct device_attribute *attr,
381 struct drm_device *ddev = dev_get_drvdata(dev);
382 struct amdgpu_device *adev = ddev->dev_private;
385 uint32_t i, mask = 0;
388 for (i = 0; i < strlen(buf); i++) {
389 if (*(buf + i) == '\n')
391 sub_str[0] = *(buf + i);
393 ret = kstrtol(sub_str, 0, &level);
402 if (adev->powerplay.pp_funcs->force_clock_level)
403 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
409 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
410 struct device_attribute *attr,
413 struct drm_device *ddev = dev_get_drvdata(dev);
414 struct amdgpu_device *adev = ddev->dev_private;
416 if (adev->powerplay.pp_funcs->print_clock_levels)
417 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
419 return snprintf(buf, PAGE_SIZE, "\n");
422 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
423 struct device_attribute *attr,
427 struct drm_device *ddev = dev_get_drvdata(dev);
428 struct amdgpu_device *adev = ddev->dev_private;
431 uint32_t i, mask = 0;
434 for (i = 0; i < strlen(buf); i++) {
435 if (*(buf + i) == '\n')
437 sub_str[0] = *(buf + i);
439 ret = kstrtol(sub_str, 0, &level);
447 if (adev->powerplay.pp_funcs->force_clock_level)
448 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
454 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
455 struct device_attribute *attr,
458 struct drm_device *ddev = dev_get_drvdata(dev);
459 struct amdgpu_device *adev = ddev->dev_private;
461 if (adev->powerplay.pp_funcs->print_clock_levels)
462 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
464 return snprintf(buf, PAGE_SIZE, "\n");
467 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
468 struct device_attribute *attr,
472 struct drm_device *ddev = dev_get_drvdata(dev);
473 struct amdgpu_device *adev = ddev->dev_private;
476 uint32_t i, mask = 0;
479 for (i = 0; i < strlen(buf); i++) {
480 if (*(buf + i) == '\n')
482 sub_str[0] = *(buf + i);
484 ret = kstrtol(sub_str, 0, &level);
492 if (adev->powerplay.pp_funcs->force_clock_level)
493 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
499 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
500 struct device_attribute *attr,
503 struct drm_device *ddev = dev_get_drvdata(dev);
504 struct amdgpu_device *adev = ddev->dev_private;
507 if (adev->powerplay.pp_funcs->get_sclk_od)
508 value = amdgpu_dpm_get_sclk_od(adev);
510 return snprintf(buf, PAGE_SIZE, "%d\n", value);
513 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
514 struct device_attribute *attr,
518 struct drm_device *ddev = dev_get_drvdata(dev);
519 struct amdgpu_device *adev = ddev->dev_private;
523 ret = kstrtol(buf, 0, &value);
529 if (adev->powerplay.pp_funcs->set_sclk_od)
530 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
532 if (adev->powerplay.pp_funcs->dispatch_tasks) {
533 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
535 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
536 amdgpu_pm_compute_clocks(adev);
543 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
544 struct device_attribute *attr,
547 struct drm_device *ddev = dev_get_drvdata(dev);
548 struct amdgpu_device *adev = ddev->dev_private;
551 if (adev->powerplay.pp_funcs->get_mclk_od)
552 value = amdgpu_dpm_get_mclk_od(adev);
554 return snprintf(buf, PAGE_SIZE, "%d\n", value);
557 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
558 struct device_attribute *attr,
562 struct drm_device *ddev = dev_get_drvdata(dev);
563 struct amdgpu_device *adev = ddev->dev_private;
567 ret = kstrtol(buf, 0, &value);
573 if (adev->powerplay.pp_funcs->set_mclk_od)
574 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
576 if (adev->powerplay.pp_funcs->dispatch_tasks) {
577 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL, NULL);
579 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
580 amdgpu_pm_compute_clocks(adev);
587 static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
588 char *buf, struct amd_pp_profile *query)
590 struct drm_device *ddev = dev_get_drvdata(dev);
591 struct amdgpu_device *adev = ddev->dev_private;
594 if (adev->powerplay.pp_funcs->get_power_profile_state)
595 ret = amdgpu_dpm_get_power_profile_state(
601 return snprintf(buf, PAGE_SIZE,
603 query->min_sclk / 100,
604 query->min_mclk / 100,
605 query->activity_threshold,
610 static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
611 struct device_attribute *attr,
614 struct amd_pp_profile query = {0};
616 query.type = AMD_PP_GFX_PROFILE;
618 return amdgpu_get_pp_power_profile(dev, buf, &query);
621 static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
622 struct device_attribute *attr,
625 struct amd_pp_profile query = {0};
627 query.type = AMD_PP_COMPUTE_PROFILE;
629 return amdgpu_get_pp_power_profile(dev, buf, &query);
632 static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
635 struct amd_pp_profile *request)
637 struct drm_device *ddev = dev_get_drvdata(dev);
638 struct amdgpu_device *adev = ddev->dev_private;
640 char *sub_str, buf_cpy[128], *tmp_str;
641 const char delimiter[3] = {' ', '\n', '\0'};
645 if (strncmp("reset", buf, strlen("reset")) == 0) {
646 if (adev->powerplay.pp_funcs->reset_power_profile_state)
647 ret = amdgpu_dpm_reset_power_profile_state(
656 if (strncmp("set", buf, strlen("set")) == 0) {
657 if (adev->powerplay.pp_funcs->set_power_profile_state)
658 ret = amdgpu_dpm_set_power_profile_state(
668 if (count + 1 >= 128) {
673 memcpy(buf_cpy, buf, count + 1);
677 sub_str = strsep(&tmp_str, delimiter);
678 ret = kstrtol(sub_str, 0, &value);
686 /* input unit MHz convert to dpm table unit 10KHz*/
687 request->min_sclk = (uint32_t)value * 100;
690 /* input unit MHz convert to dpm table unit 10KHz*/
691 request->min_mclk = (uint32_t)value * 100;
694 request->activity_threshold = (uint16_t)value;
697 request->up_hyst = (uint8_t)value;
700 request->down_hyst = (uint8_t)value;
708 if (adev->powerplay.pp_funcs->set_power_profile_state)
709 ret = amdgpu_dpm_set_power_profile_state(adev, request);
718 static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
719 struct device_attribute *attr,
723 struct amd_pp_profile request = {0};
725 request.type = AMD_PP_GFX_PROFILE;
727 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
730 static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
731 struct device_attribute *attr,
735 struct amd_pp_profile request = {0};
737 request.type = AMD_PP_COMPUTE_PROFILE;
739 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
742 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
743 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
744 amdgpu_get_dpm_forced_performance_level,
745 amdgpu_set_dpm_forced_performance_level);
746 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
747 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
748 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
749 amdgpu_get_pp_force_state,
750 amdgpu_set_pp_force_state);
751 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
753 amdgpu_set_pp_table);
754 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
755 amdgpu_get_pp_dpm_sclk,
756 amdgpu_set_pp_dpm_sclk);
757 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
758 amdgpu_get_pp_dpm_mclk,
759 amdgpu_set_pp_dpm_mclk);
760 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
761 amdgpu_get_pp_dpm_pcie,
762 amdgpu_set_pp_dpm_pcie);
763 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
764 amdgpu_get_pp_sclk_od,
765 amdgpu_set_pp_sclk_od);
766 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
767 amdgpu_get_pp_mclk_od,
768 amdgpu_set_pp_mclk_od);
769 static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
770 amdgpu_get_pp_gfx_power_profile,
771 amdgpu_set_pp_gfx_power_profile);
772 static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
773 amdgpu_get_pp_compute_power_profile,
774 amdgpu_set_pp_compute_power_profile);
776 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
777 struct device_attribute *attr,
780 struct amdgpu_device *adev = dev_get_drvdata(dev);
781 struct drm_device *ddev = adev->ddev;
784 /* Can't get temperature when the card is off */
785 if ((adev->flags & AMD_IS_PX) &&
786 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
789 if (!adev->powerplay.pp_funcs->get_temperature)
792 temp = amdgpu_dpm_get_temperature(adev);
794 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
797 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
798 struct device_attribute *attr,
801 struct amdgpu_device *adev = dev_get_drvdata(dev);
802 int hyst = to_sensor_dev_attr(attr)->index;
806 temp = adev->pm.dpm.thermal.min_temp;
808 temp = adev->pm.dpm.thermal.max_temp;
810 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
813 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
814 struct device_attribute *attr,
817 struct amdgpu_device *adev = dev_get_drvdata(dev);
820 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
823 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
825 return sprintf(buf, "%i\n", pwm_mode);
828 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
829 struct device_attribute *attr,
833 struct amdgpu_device *adev = dev_get_drvdata(dev);
837 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
840 err = kstrtoint(buf, 10, &value);
844 amdgpu_dpm_set_fan_control_mode(adev, value);
849 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
850 struct device_attribute *attr,
853 return sprintf(buf, "%i\n", 0);
856 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
857 struct device_attribute *attr,
860 return sprintf(buf, "%i\n", 255);
863 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
864 struct device_attribute *attr,
865 const char *buf, size_t count)
867 struct amdgpu_device *adev = dev_get_drvdata(dev);
871 err = kstrtou32(buf, 10, &value);
875 value = (value * 100) / 255;
877 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
878 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
886 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
887 struct device_attribute *attr,
890 struct amdgpu_device *adev = dev_get_drvdata(dev);
894 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
895 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
900 speed = (speed * 255) / 100;
902 return sprintf(buf, "%i\n", speed);
905 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
906 struct device_attribute *attr,
909 struct amdgpu_device *adev = dev_get_drvdata(dev);
913 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
914 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
919 return sprintf(buf, "%i\n", speed);
922 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
923 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
924 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
925 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
926 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
927 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
928 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
929 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
931 static struct attribute *hwmon_attributes[] = {
932 &sensor_dev_attr_temp1_input.dev_attr.attr,
933 &sensor_dev_attr_temp1_crit.dev_attr.attr,
934 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
935 &sensor_dev_attr_pwm1.dev_attr.attr,
936 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
937 &sensor_dev_attr_pwm1_min.dev_attr.attr,
938 &sensor_dev_attr_pwm1_max.dev_attr.attr,
939 &sensor_dev_attr_fan1_input.dev_attr.attr,
943 static umode_t hwmon_attributes_visible(struct kobject *kobj,
944 struct attribute *attr, int index)
946 struct device *dev = kobj_to_dev(kobj);
947 struct amdgpu_device *adev = dev_get_drvdata(dev);
948 umode_t effective_mode = attr->mode;
950 /* no skipping for powerplay */
951 if (adev->powerplay.cgs_device)
952 return effective_mode;
954 /* Skip limit attributes if DPM is not enabled */
955 if (!adev->pm.dpm_enabled &&
956 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
957 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
958 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
959 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
960 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
961 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
964 /* Skip fan attributes if fan is not present */
965 if (adev->pm.no_fan &&
966 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
967 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
968 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
969 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
972 /* mask fan attributes if we have no bindings for this asic to expose */
973 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
974 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
975 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
976 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
977 effective_mode &= ~S_IRUGO;
979 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
980 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
981 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
982 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
983 effective_mode &= ~S_IWUSR;
985 /* hide max/min values if we can't both query and manage the fan */
986 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
987 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
988 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
989 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
992 /* requires powerplay */
993 if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
996 return effective_mode;
999 static const struct attribute_group hwmon_attrgroup = {
1000 .attrs = hwmon_attributes,
1001 .is_visible = hwmon_attributes_visible,
1004 static const struct attribute_group *hwmon_groups[] = {
1009 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1011 struct amdgpu_device *adev =
1012 container_of(work, struct amdgpu_device,
1013 pm.dpm.thermal.work);
1014 /* switch to the thermal state */
1015 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
1017 if (!adev->pm.dpm_enabled)
1020 if (adev->powerplay.pp_funcs->get_temperature) {
1021 int temp = amdgpu_dpm_get_temperature(adev);
1023 if (temp < adev->pm.dpm.thermal.min_temp)
1024 /* switch back the user state */
1025 dpm_state = adev->pm.dpm.user_state;
1027 if (adev->pm.dpm.thermal.high_to_low)
1028 /* switch back the user state */
1029 dpm_state = adev->pm.dpm.user_state;
1031 mutex_lock(&adev->pm.mutex);
1032 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1033 adev->pm.dpm.thermal_active = true;
1035 adev->pm.dpm.thermal_active = false;
1036 adev->pm.dpm.state = dpm_state;
1037 mutex_unlock(&adev->pm.mutex);
1039 amdgpu_pm_compute_clocks(adev);
1042 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
1043 enum amd_pm_state_type dpm_state)
1046 struct amdgpu_ps *ps;
1048 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1051 /* check if the vblank period is too short to adjust the mclk */
1052 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
1053 if (amdgpu_dpm_vblank_too_short(adev))
1054 single_display = false;
1057 /* certain older asics have a separare 3D performance state,
1058 * so try that first if the user selected performance
1060 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1061 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1062 /* balanced states don't exist at the moment */
1063 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1064 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1067 /* Pick the best power state based on current conditions */
1068 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1069 ps = &adev->pm.dpm.ps[i];
1070 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1071 switch (dpm_state) {
1073 case POWER_STATE_TYPE_BATTERY:
1074 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1075 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1082 case POWER_STATE_TYPE_BALANCED:
1083 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1084 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1091 case POWER_STATE_TYPE_PERFORMANCE:
1092 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1093 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1100 /* internal states */
1101 case POWER_STATE_TYPE_INTERNAL_UVD:
1102 if (adev->pm.dpm.uvd_ps)
1103 return adev->pm.dpm.uvd_ps;
1106 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1107 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1110 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1111 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1114 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1115 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1118 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1119 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1122 case POWER_STATE_TYPE_INTERNAL_BOOT:
1123 return adev->pm.dpm.boot_ps;
1124 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1125 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1128 case POWER_STATE_TYPE_INTERNAL_ACPI:
1129 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1132 case POWER_STATE_TYPE_INTERNAL_ULV:
1133 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1136 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1137 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1144 /* use a fallback state if we didn't match */
1145 switch (dpm_state) {
1146 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1147 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1148 goto restart_search;
1149 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1150 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1151 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1152 if (adev->pm.dpm.uvd_ps) {
1153 return adev->pm.dpm.uvd_ps;
1155 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1156 goto restart_search;
1158 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1159 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1160 goto restart_search;
1161 case POWER_STATE_TYPE_INTERNAL_ACPI:
1162 dpm_state = POWER_STATE_TYPE_BATTERY;
1163 goto restart_search;
1164 case POWER_STATE_TYPE_BATTERY:
1165 case POWER_STATE_TYPE_BALANCED:
1166 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1167 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1168 goto restart_search;
1176 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1178 struct amdgpu_ps *ps;
1179 enum amd_pm_state_type dpm_state;
1183 /* if dpm init failed */
1184 if (!adev->pm.dpm_enabled)
1187 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1188 /* add other state override checks here */
1189 if ((!adev->pm.dpm.thermal_active) &&
1190 (!adev->pm.dpm.uvd_active))
1191 adev->pm.dpm.state = adev->pm.dpm.user_state;
1193 dpm_state = adev->pm.dpm.state;
1195 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1197 adev->pm.dpm.requested_ps = ps;
1201 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
1202 printk("switching from power state:\n");
1203 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1204 printk("switching to power state:\n");
1205 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1208 /* update whether vce is active */
1209 ps->vce_active = adev->pm.dpm.vce_active;
1210 if (adev->powerplay.pp_funcs->display_configuration_changed)
1211 amdgpu_dpm_display_configuration_changed(adev);
1213 ret = amdgpu_dpm_pre_set_power_state(adev);
1217 if (adev->powerplay.pp_funcs->check_state_equal) {
1218 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1225 amdgpu_dpm_set_power_state(adev);
1226 amdgpu_dpm_post_set_power_state(adev);
1228 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1229 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1231 if (adev->powerplay.pp_funcs->force_performance_level) {
1232 if (adev->pm.dpm.thermal_active) {
1233 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
1234 /* force low perf level for thermal */
1235 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
1236 /* save the user's level */
1237 adev->pm.dpm.forced_level = level;
1239 /* otherwise, user selected level */
1240 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1245 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1247 if (adev->powerplay.pp_funcs->powergate_uvd) {
1248 /* enable/disable UVD */
1249 mutex_lock(&adev->pm.mutex);
1250 amdgpu_dpm_powergate_uvd(adev, !enable);
1251 mutex_unlock(&adev->pm.mutex);
1254 mutex_lock(&adev->pm.mutex);
1255 adev->pm.dpm.uvd_active = true;
1256 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
1257 mutex_unlock(&adev->pm.mutex);
1259 mutex_lock(&adev->pm.mutex);
1260 adev->pm.dpm.uvd_active = false;
1261 mutex_unlock(&adev->pm.mutex);
1263 amdgpu_pm_compute_clocks(adev);
1267 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1269 if (adev->powerplay.pp_funcs->powergate_vce) {
1270 /* enable/disable VCE */
1271 mutex_lock(&adev->pm.mutex);
1272 amdgpu_dpm_powergate_vce(adev, !enable);
1273 mutex_unlock(&adev->pm.mutex);
1276 mutex_lock(&adev->pm.mutex);
1277 adev->pm.dpm.vce_active = true;
1278 /* XXX select vce level based on ring/task */
1279 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
1280 mutex_unlock(&adev->pm.mutex);
1281 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1282 AMD_CG_STATE_UNGATE);
1283 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1284 AMD_PG_STATE_UNGATE);
1285 amdgpu_pm_compute_clocks(adev);
1287 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1289 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1291 mutex_lock(&adev->pm.mutex);
1292 adev->pm.dpm.vce_active = false;
1293 mutex_unlock(&adev->pm.mutex);
1294 amdgpu_pm_compute_clocks(adev);
1300 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1304 if (adev->powerplay.pp_funcs->print_power_state == NULL)
1307 for (i = 0; i < adev->pm.dpm.num_ps; i++)
1308 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
1312 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1316 if (adev->pm.sysfs_initialized)
1319 if (adev->pm.dpm_enabled == 0)
1322 if (adev->powerplay.pp_funcs->get_temperature == NULL)
1325 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1328 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1329 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1331 "Unable to register hwmon device: %d\n", ret);
1335 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1337 DRM_ERROR("failed to create device file for dpm state\n");
1340 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1342 DRM_ERROR("failed to create device file for dpm state\n");
1347 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1349 DRM_ERROR("failed to create device file pp_num_states\n");
1352 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1354 DRM_ERROR("failed to create device file pp_cur_state\n");
1357 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1359 DRM_ERROR("failed to create device file pp_force_state\n");
1362 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1364 DRM_ERROR("failed to create device file pp_table\n");
1368 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1370 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1373 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1375 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1378 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1380 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1383 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1385 DRM_ERROR("failed to create device file pp_sclk_od\n");
1388 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1390 DRM_ERROR("failed to create device file pp_mclk_od\n");
1393 ret = device_create_file(adev->dev,
1394 &dev_attr_pp_gfx_power_profile);
1396 DRM_ERROR("failed to create device file "
1397 "pp_gfx_power_profile\n");
1400 ret = device_create_file(adev->dev,
1401 &dev_attr_pp_compute_power_profile);
1403 DRM_ERROR("failed to create device file "
1404 "pp_compute_power_profile\n");
1408 ret = amdgpu_debugfs_pm_init(adev);
1410 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1414 adev->pm.sysfs_initialized = true;
1419 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1421 if (adev->pm.dpm_enabled == 0)
1424 if (adev->pm.int_hwmon_dev)
1425 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1426 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1427 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1429 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1430 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1431 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1432 device_remove_file(adev->dev, &dev_attr_pp_table);
1434 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1435 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1436 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
1437 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
1438 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
1439 device_remove_file(adev->dev,
1440 &dev_attr_pp_gfx_power_profile);
1441 device_remove_file(adev->dev,
1442 &dev_attr_pp_compute_power_profile);
1445 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1447 struct drm_device *ddev = adev->ddev;
1448 struct drm_crtc *crtc;
1449 struct amdgpu_crtc *amdgpu_crtc;
1452 if (!adev->pm.dpm_enabled)
1455 if (adev->mode_info.num_crtc)
1456 amdgpu_display_bandwidth_update(adev);
1458 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1459 struct amdgpu_ring *ring = adev->rings[i];
1460 if (ring && ring->ready)
1461 amdgpu_fence_wait_empty(ring);
1464 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1465 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL, NULL);
1467 mutex_lock(&adev->pm.mutex);
1468 adev->pm.dpm.new_active_crtcs = 0;
1469 adev->pm.dpm.new_active_crtc_count = 0;
1470 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
1471 list_for_each_entry(crtc,
1472 &ddev->mode_config.crtc_list, head) {
1473 amdgpu_crtc = to_amdgpu_crtc(crtc);
1474 if (amdgpu_crtc->enabled) {
1475 adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
1476 adev->pm.dpm.new_active_crtc_count++;
1480 /* update battery/ac status */
1481 if (power_supply_is_system_supplied() > 0)
1482 adev->pm.dpm.ac_power = true;
1484 adev->pm.dpm.ac_power = false;
1486 amdgpu_dpm_change_power_state_locked(adev);
1488 mutex_unlock(&adev->pm.mutex);
1495 #if defined(CONFIG_DEBUG_FS)
1497 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1500 struct pp_gpu_power query = {0};
1503 /* sanity check PP is enabled */
1504 if (!(adev->powerplay.pp_funcs &&
1505 adev->powerplay.pp_funcs->read_sensor))
1509 size = sizeof(value);
1510 seq_printf(m, "GFX Clocks and Power:\n");
1511 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
1512 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
1513 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
1514 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
1515 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
1516 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
1517 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
1518 seq_printf(m, "\t%u mV (VDDNB)\n", value);
1519 size = sizeof(query);
1520 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
1521 seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
1522 query.vddc_power & 0xff);
1523 seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
1524 query.vddci_power & 0xff);
1525 seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
1526 query.max_gpu_power & 0xff);
1527 seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
1528 query.average_gpu_power & 0xff);
1530 size = sizeof(value);
1531 seq_printf(m, "\n");
1534 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
1535 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1538 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
1539 seq_printf(m, "GPU Load: %u %%\n", value);
1540 seq_printf(m, "\n");
1543 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
1545 seq_printf(m, "UVD: Disabled\n");
1547 seq_printf(m, "UVD: Enabled\n");
1548 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
1549 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
1550 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
1551 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1554 seq_printf(m, "\n");
1557 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
1559 seq_printf(m, "VCE: Disabled\n");
1561 seq_printf(m, "VCE: Enabled\n");
1562 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
1563 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
1570 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
1574 for (i = 0; clocks[i].flag; i++)
1575 seq_printf(m, "\t%s: %s\n", clocks[i].name,
1576 (flags & clocks[i].flag) ? "On" : "Off");
1579 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
1581 struct drm_info_node *node = (struct drm_info_node *) m->private;
1582 struct drm_device *dev = node->minor->dev;
1583 struct amdgpu_device *adev = dev->dev_private;
1584 struct drm_device *ddev = adev->ddev;
1587 amdgpu_device_ip_get_clockgating_state(adev, &flags);
1588 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
1589 amdgpu_parse_cg_state(m, flags);
1590 seq_printf(m, "\n");
1592 if (!adev->pm.dpm_enabled) {
1593 seq_printf(m, "dpm not enabled\n");
1596 if ((adev->flags & AMD_IS_PX) &&
1597 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1598 seq_printf(m, "PX asic powered off\n");
1599 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
1600 mutex_lock(&adev->pm.mutex);
1601 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
1602 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
1604 seq_printf(m, "Debugfs support not implemented for this asic\n");
1605 mutex_unlock(&adev->pm.mutex);
1607 return amdgpu_debugfs_pm_info_pp(m, adev);
1613 static const struct drm_info_list amdgpu_pm_info_list[] = {
1614 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
1618 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
1620 #if defined(CONFIG_DEBUG_FS)
1621 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));