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);
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);
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_set_pp_od_clk_voltage(struct device *dev,
364 struct device_attribute *attr,
368 struct drm_device *ddev = dev_get_drvdata(dev);
369 struct amdgpu_device *adev = ddev->dev_private;
371 uint32_t parameter_size = 0;
376 const char delimiter[3] = {' ', '\n', '\0'};
383 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
384 else if (*buf == 'm')
385 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
387 type = PP_OD_RESTORE_DEFAULT_TABLE;
388 else if (*buf == 'c')
389 type = PP_OD_COMMIT_DPM_TABLE;
393 memcpy(buf_cpy, buf, count+1);
397 while (isspace(*++tmp_str));
400 sub_str = strsep(&tmp_str, delimiter);
401 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
406 while (isspace(*tmp_str))
410 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
411 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
412 parameter, parameter_size);
417 if (type == PP_OD_COMMIT_DPM_TABLE) {
418 if (adev->powerplay.pp_funcs->dispatch_tasks) {
419 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
429 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
430 struct device_attribute *attr,
433 struct drm_device *ddev = dev_get_drvdata(dev);
434 struct amdgpu_device *adev = ddev->dev_private;
437 if (adev->powerplay.pp_funcs->print_clock_levels) {
438 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
439 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
442 return snprintf(buf, PAGE_SIZE, "\n");
447 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
448 struct device_attribute *attr,
451 struct drm_device *ddev = dev_get_drvdata(dev);
452 struct amdgpu_device *adev = ddev->dev_private;
454 if (adev->powerplay.pp_funcs->print_clock_levels)
455 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
457 return snprintf(buf, PAGE_SIZE, "\n");
460 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
461 struct device_attribute *attr,
465 struct drm_device *ddev = dev_get_drvdata(dev);
466 struct amdgpu_device *adev = ddev->dev_private;
469 uint32_t i, mask = 0;
472 for (i = 0; i < strlen(buf); i++) {
473 if (*(buf + i) == '\n')
475 sub_str[0] = *(buf + i);
477 ret = kstrtol(sub_str, 0, &level);
486 if (adev->powerplay.pp_funcs->force_clock_level)
487 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
493 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
494 struct device_attribute *attr,
497 struct drm_device *ddev = dev_get_drvdata(dev);
498 struct amdgpu_device *adev = ddev->dev_private;
500 if (adev->powerplay.pp_funcs->print_clock_levels)
501 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
503 return snprintf(buf, PAGE_SIZE, "\n");
506 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
507 struct device_attribute *attr,
511 struct drm_device *ddev = dev_get_drvdata(dev);
512 struct amdgpu_device *adev = ddev->dev_private;
515 uint32_t i, mask = 0;
518 for (i = 0; i < strlen(buf); i++) {
519 if (*(buf + i) == '\n')
521 sub_str[0] = *(buf + i);
523 ret = kstrtol(sub_str, 0, &level);
531 if (adev->powerplay.pp_funcs->force_clock_level)
532 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
538 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
539 struct device_attribute *attr,
542 struct drm_device *ddev = dev_get_drvdata(dev);
543 struct amdgpu_device *adev = ddev->dev_private;
545 if (adev->powerplay.pp_funcs->print_clock_levels)
546 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
548 return snprintf(buf, PAGE_SIZE, "\n");
551 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
552 struct device_attribute *attr,
556 struct drm_device *ddev = dev_get_drvdata(dev);
557 struct amdgpu_device *adev = ddev->dev_private;
560 uint32_t i, mask = 0;
563 for (i = 0; i < strlen(buf); i++) {
564 if (*(buf + i) == '\n')
566 sub_str[0] = *(buf + i);
568 ret = kstrtol(sub_str, 0, &level);
576 if (adev->powerplay.pp_funcs->force_clock_level)
577 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
583 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
584 struct device_attribute *attr,
587 struct drm_device *ddev = dev_get_drvdata(dev);
588 struct amdgpu_device *adev = ddev->dev_private;
591 if (adev->powerplay.pp_funcs->get_sclk_od)
592 value = amdgpu_dpm_get_sclk_od(adev);
594 return snprintf(buf, PAGE_SIZE, "%d\n", value);
597 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
598 struct device_attribute *attr,
602 struct drm_device *ddev = dev_get_drvdata(dev);
603 struct amdgpu_device *adev = ddev->dev_private;
607 ret = kstrtol(buf, 0, &value);
613 if (adev->powerplay.pp_funcs->set_sclk_od)
614 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
616 if (adev->powerplay.pp_funcs->dispatch_tasks) {
617 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
619 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
620 amdgpu_pm_compute_clocks(adev);
627 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
628 struct device_attribute *attr,
631 struct drm_device *ddev = dev_get_drvdata(dev);
632 struct amdgpu_device *adev = ddev->dev_private;
635 if (adev->powerplay.pp_funcs->get_mclk_od)
636 value = amdgpu_dpm_get_mclk_od(adev);
638 return snprintf(buf, PAGE_SIZE, "%d\n", value);
641 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
642 struct device_attribute *attr,
646 struct drm_device *ddev = dev_get_drvdata(dev);
647 struct amdgpu_device *adev = ddev->dev_private;
651 ret = kstrtol(buf, 0, &value);
657 if (adev->powerplay.pp_funcs->set_mclk_od)
658 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
660 if (adev->powerplay.pp_funcs->dispatch_tasks) {
661 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
663 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
664 amdgpu_pm_compute_clocks(adev);
671 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
672 struct device_attribute *attr,
675 struct drm_device *ddev = dev_get_drvdata(dev);
676 struct amdgpu_device *adev = ddev->dev_private;
678 if (adev->powerplay.pp_funcs->get_power_profile_mode)
679 return amdgpu_dpm_get_power_profile_mode(adev, buf);
681 return snprintf(buf, PAGE_SIZE, "\n");
685 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
686 struct device_attribute *attr,
691 struct drm_device *ddev = dev_get_drvdata(dev);
692 struct amdgpu_device *adev = ddev->dev_private;
693 uint32_t parameter_size = 0;
695 char *sub_str, buf_cpy[128];
699 long int profile_mode = 0;
700 const char delimiter[3] = {' ', '\n', '\0'};
704 ret = kstrtol(tmp, 0, &profile_mode);
708 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
709 if (count < 2 || count > 127)
711 while (isspace(*++buf))
713 memcpy(buf_cpy, buf, count-i);
716 sub_str = strsep(&tmp_str, delimiter);
717 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
723 while (isspace(*tmp_str))
727 parameter[parameter_size] = profile_mode;
728 if (adev->powerplay.pp_funcs->set_power_profile_mode)
729 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
737 static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
738 char *buf, struct amd_pp_profile *query)
740 struct drm_device *ddev = dev_get_drvdata(dev);
741 struct amdgpu_device *adev = ddev->dev_private;
744 if (adev->powerplay.pp_funcs->get_power_profile_state)
745 ret = amdgpu_dpm_get_power_profile_state(
751 return snprintf(buf, PAGE_SIZE,
753 query->min_sclk / 100,
754 query->min_mclk / 100,
755 query->activity_threshold,
760 static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
761 struct device_attribute *attr,
764 struct amd_pp_profile query = {0};
766 query.type = AMD_PP_GFX_PROFILE;
768 return amdgpu_get_pp_power_profile(dev, buf, &query);
771 static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
772 struct device_attribute *attr,
775 struct amd_pp_profile query = {0};
777 query.type = AMD_PP_COMPUTE_PROFILE;
779 return amdgpu_get_pp_power_profile(dev, buf, &query);
782 static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
785 struct amd_pp_profile *request)
787 struct drm_device *ddev = dev_get_drvdata(dev);
788 struct amdgpu_device *adev = ddev->dev_private;
790 char *sub_str, buf_cpy[128], *tmp_str;
791 const char delimiter[3] = {' ', '\n', '\0'};
795 if (strncmp("reset", buf, strlen("reset")) == 0) {
796 if (adev->powerplay.pp_funcs->reset_power_profile_state)
797 ret = amdgpu_dpm_reset_power_profile_state(
806 if (strncmp("set", buf, strlen("set")) == 0) {
807 if (adev->powerplay.pp_funcs->set_power_profile_state)
808 ret = amdgpu_dpm_set_power_profile_state(
818 if (count + 1 >= 128) {
823 memcpy(buf_cpy, buf, count + 1);
827 sub_str = strsep(&tmp_str, delimiter);
828 ret = kstrtol(sub_str, 0, &value);
836 /* input unit MHz convert to dpm table unit 10KHz*/
837 request->min_sclk = (uint32_t)value * 100;
840 /* input unit MHz convert to dpm table unit 10KHz*/
841 request->min_mclk = (uint32_t)value * 100;
844 request->activity_threshold = (uint16_t)value;
847 request->up_hyst = (uint8_t)value;
850 request->down_hyst = (uint8_t)value;
858 if (adev->powerplay.pp_funcs->set_power_profile_state)
859 ret = amdgpu_dpm_set_power_profile_state(adev, request);
868 static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
869 struct device_attribute *attr,
873 struct amd_pp_profile request = {0};
875 request.type = AMD_PP_GFX_PROFILE;
877 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
880 static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
881 struct device_attribute *attr,
885 struct amd_pp_profile request = {0};
887 request.type = AMD_PP_COMPUTE_PROFILE;
889 return amdgpu_set_pp_power_profile(dev, buf, count, &request);
892 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
893 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
894 amdgpu_get_dpm_forced_performance_level,
895 amdgpu_set_dpm_forced_performance_level);
896 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
897 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
898 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
899 amdgpu_get_pp_force_state,
900 amdgpu_set_pp_force_state);
901 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
903 amdgpu_set_pp_table);
904 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
905 amdgpu_get_pp_dpm_sclk,
906 amdgpu_set_pp_dpm_sclk);
907 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
908 amdgpu_get_pp_dpm_mclk,
909 amdgpu_set_pp_dpm_mclk);
910 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
911 amdgpu_get_pp_dpm_pcie,
912 amdgpu_set_pp_dpm_pcie);
913 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
914 amdgpu_get_pp_sclk_od,
915 amdgpu_set_pp_sclk_od);
916 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
917 amdgpu_get_pp_mclk_od,
918 amdgpu_set_pp_mclk_od);
919 static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
920 amdgpu_get_pp_gfx_power_profile,
921 amdgpu_set_pp_gfx_power_profile);
922 static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
923 amdgpu_get_pp_compute_power_profile,
924 amdgpu_set_pp_compute_power_profile);
925 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
926 amdgpu_get_pp_power_profile_mode,
927 amdgpu_set_pp_power_profile_mode);
928 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
929 amdgpu_get_pp_od_clk_voltage,
930 amdgpu_set_pp_od_clk_voltage);
932 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
933 struct device_attribute *attr,
936 struct amdgpu_device *adev = dev_get_drvdata(dev);
937 struct drm_device *ddev = adev->ddev;
938 int r, temp, size = sizeof(temp);
940 /* Can't get temperature when the card is off */
941 if ((adev->flags & AMD_IS_PX) &&
942 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
945 /* sanity check PP is enabled */
946 if (!(adev->powerplay.pp_funcs &&
947 adev->powerplay.pp_funcs->read_sensor))
950 /* get the temperature */
951 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
952 (void *)&temp, &size);
956 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
959 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
960 struct device_attribute *attr,
963 struct amdgpu_device *adev = dev_get_drvdata(dev);
964 int hyst = to_sensor_dev_attr(attr)->index;
968 temp = adev->pm.dpm.thermal.min_temp;
970 temp = adev->pm.dpm.thermal.max_temp;
972 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
975 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
976 struct device_attribute *attr,
979 struct amdgpu_device *adev = dev_get_drvdata(dev);
982 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
985 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
987 return sprintf(buf, "%i\n", pwm_mode);
990 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
991 struct device_attribute *attr,
995 struct amdgpu_device *adev = dev_get_drvdata(dev);
999 /* Can't adjust fan when the card is off */
1000 if ((adev->flags & AMD_IS_PX) &&
1001 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1004 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1007 err = kstrtoint(buf, 10, &value);
1011 amdgpu_dpm_set_fan_control_mode(adev, value);
1016 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1017 struct device_attribute *attr,
1020 return sprintf(buf, "%i\n", 0);
1023 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1024 struct device_attribute *attr,
1027 return sprintf(buf, "%i\n", 255);
1030 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1031 struct device_attribute *attr,
1032 const char *buf, size_t count)
1034 struct amdgpu_device *adev = dev_get_drvdata(dev);
1038 /* Can't adjust fan when the card is off */
1039 if ((adev->flags & AMD_IS_PX) &&
1040 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1043 err = kstrtou32(buf, 10, &value);
1047 value = (value * 100) / 255;
1049 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1050 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1058 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1059 struct device_attribute *attr,
1062 struct amdgpu_device *adev = dev_get_drvdata(dev);
1066 /* Can't adjust fan when the card is off */
1067 if ((adev->flags & AMD_IS_PX) &&
1068 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1071 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1072 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1077 speed = (speed * 255) / 100;
1079 return sprintf(buf, "%i\n", speed);
1082 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1083 struct device_attribute *attr,
1086 struct amdgpu_device *adev = dev_get_drvdata(dev);
1090 /* Can't adjust fan when the card is off */
1091 if ((adev->flags & AMD_IS_PX) &&
1092 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1095 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1096 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1101 return sprintf(buf, "%i\n", speed);
1104 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1105 struct device_attribute *attr,
1108 struct amdgpu_device *adev = dev_get_drvdata(dev);
1109 struct drm_device *ddev = adev->ddev;
1111 int r, size = sizeof(vddgfx);
1113 /* Can't get voltage when the card is off */
1114 if ((adev->flags & AMD_IS_PX) &&
1115 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1118 /* sanity check PP is enabled */
1119 if (!(adev->powerplay.pp_funcs &&
1120 adev->powerplay.pp_funcs->read_sensor))
1123 /* get the voltage */
1124 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1125 (void *)&vddgfx, &size);
1129 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1132 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1133 struct device_attribute *attr,
1136 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1139 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1140 struct device_attribute *attr,
1143 struct amdgpu_device *adev = dev_get_drvdata(dev);
1144 struct drm_device *ddev = adev->ddev;
1146 int r, size = sizeof(vddnb);
1148 /* only APUs have vddnb */
1149 if (adev->flags & AMD_IS_APU)
1152 /* Can't get voltage when the card is off */
1153 if ((adev->flags & AMD_IS_PX) &&
1154 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1157 /* sanity check PP is enabled */
1158 if (!(adev->powerplay.pp_funcs &&
1159 adev->powerplay.pp_funcs->read_sensor))
1162 /* get the voltage */
1163 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1164 (void *)&vddnb, &size);
1168 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1171 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1172 struct device_attribute *attr,
1175 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1178 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1179 struct device_attribute *attr,
1182 struct amdgpu_device *adev = dev_get_drvdata(dev);
1183 struct drm_device *ddev = adev->ddev;
1184 struct pp_gpu_power query = {0};
1185 int r, size = sizeof(query);
1188 /* Can't get power when the card is off */
1189 if ((adev->flags & AMD_IS_PX) &&
1190 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1193 /* sanity check PP is enabled */
1194 if (!(adev->powerplay.pp_funcs &&
1195 adev->powerplay.pp_funcs->read_sensor))
1198 /* get the voltage */
1199 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1200 (void *)&query, &size);
1204 /* convert to microwatts */
1205 uw = (query.average_gpu_power >> 8) * 1000000;
1207 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1210 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1211 struct device_attribute *attr,
1214 return sprintf(buf, "%i\n", 0);
1217 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1218 struct device_attribute *attr,
1221 struct amdgpu_device *adev = dev_get_drvdata(dev);
1224 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1225 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1226 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1228 return snprintf(buf, PAGE_SIZE, "\n");
1232 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1233 struct device_attribute *attr,
1236 struct amdgpu_device *adev = dev_get_drvdata(dev);
1239 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1240 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1241 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1243 return snprintf(buf, PAGE_SIZE, "\n");
1248 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
1249 struct device_attribute *attr,
1253 struct amdgpu_device *adev = dev_get_drvdata(dev);
1257 err = kstrtou32(buf, 10, &value);
1261 value = value / 1000000; /* convert to Watt */
1262 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
1263 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
1273 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
1274 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
1275 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
1276 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
1277 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
1278 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
1279 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
1280 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
1281 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
1282 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
1283 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
1284 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
1285 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
1286 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
1287 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
1288 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
1290 static struct attribute *hwmon_attributes[] = {
1291 &sensor_dev_attr_temp1_input.dev_attr.attr,
1292 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1293 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1294 &sensor_dev_attr_pwm1.dev_attr.attr,
1295 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1296 &sensor_dev_attr_pwm1_min.dev_attr.attr,
1297 &sensor_dev_attr_pwm1_max.dev_attr.attr,
1298 &sensor_dev_attr_fan1_input.dev_attr.attr,
1299 &sensor_dev_attr_in0_input.dev_attr.attr,
1300 &sensor_dev_attr_in0_label.dev_attr.attr,
1301 &sensor_dev_attr_in1_input.dev_attr.attr,
1302 &sensor_dev_attr_in1_label.dev_attr.attr,
1303 &sensor_dev_attr_power1_average.dev_attr.attr,
1304 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
1305 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
1306 &sensor_dev_attr_power1_cap.dev_attr.attr,
1310 static umode_t hwmon_attributes_visible(struct kobject *kobj,
1311 struct attribute *attr, int index)
1313 struct device *dev = kobj_to_dev(kobj);
1314 struct amdgpu_device *adev = dev_get_drvdata(dev);
1315 umode_t effective_mode = attr->mode;
1317 /* handle non-powerplay limitations */
1318 if (!adev->powerplay.cgs_device) {
1319 /* Skip fan attributes if fan is not present */
1320 if (adev->pm.no_fan &&
1321 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1322 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1323 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1324 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1326 /* requires powerplay */
1327 if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
1331 /* Skip limit attributes if DPM is not enabled */
1332 if (!adev->pm.dpm_enabled &&
1333 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
1334 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
1335 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1336 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1337 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1338 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1341 /* mask fan attributes if we have no bindings for this asic to expose */
1342 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
1343 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
1344 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
1345 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
1346 effective_mode &= ~S_IRUGO;
1348 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1349 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
1350 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
1351 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
1352 effective_mode &= ~S_IWUSR;
1354 if ((adev->flags & AMD_IS_APU) &&
1355 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
1356 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
1357 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
1360 /* hide max/min values if we can't both query and manage the fan */
1361 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1362 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
1363 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1364 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1367 /* only APUs have vddnb */
1368 if (!(adev->flags & AMD_IS_APU) &&
1369 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
1370 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
1373 return effective_mode;
1376 static const struct attribute_group hwmon_attrgroup = {
1377 .attrs = hwmon_attributes,
1378 .is_visible = hwmon_attributes_visible,
1381 static const struct attribute_group *hwmon_groups[] = {
1386 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1388 struct amdgpu_device *adev =
1389 container_of(work, struct amdgpu_device,
1390 pm.dpm.thermal.work);
1391 /* switch to the thermal state */
1392 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
1393 int temp, size = sizeof(temp);
1395 if (!adev->pm.dpm_enabled)
1398 if (adev->powerplay.pp_funcs &&
1399 adev->powerplay.pp_funcs->read_sensor &&
1400 !amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1401 (void *)&temp, &size)) {
1402 if (temp < adev->pm.dpm.thermal.min_temp)
1403 /* switch back the user state */
1404 dpm_state = adev->pm.dpm.user_state;
1406 if (adev->pm.dpm.thermal.high_to_low)
1407 /* switch back the user state */
1408 dpm_state = adev->pm.dpm.user_state;
1410 mutex_lock(&adev->pm.mutex);
1411 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1412 adev->pm.dpm.thermal_active = true;
1414 adev->pm.dpm.thermal_active = false;
1415 adev->pm.dpm.state = dpm_state;
1416 mutex_unlock(&adev->pm.mutex);
1418 amdgpu_pm_compute_clocks(adev);
1421 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
1422 enum amd_pm_state_type dpm_state)
1425 struct amdgpu_ps *ps;
1427 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1430 /* check if the vblank period is too short to adjust the mclk */
1431 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
1432 if (amdgpu_dpm_vblank_too_short(adev))
1433 single_display = false;
1436 /* certain older asics have a separare 3D performance state,
1437 * so try that first if the user selected performance
1439 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1440 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1441 /* balanced states don't exist at the moment */
1442 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1443 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1446 /* Pick the best power state based on current conditions */
1447 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1448 ps = &adev->pm.dpm.ps[i];
1449 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1450 switch (dpm_state) {
1452 case POWER_STATE_TYPE_BATTERY:
1453 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1454 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1461 case POWER_STATE_TYPE_BALANCED:
1462 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1463 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1470 case POWER_STATE_TYPE_PERFORMANCE:
1471 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1472 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1479 /* internal states */
1480 case POWER_STATE_TYPE_INTERNAL_UVD:
1481 if (adev->pm.dpm.uvd_ps)
1482 return adev->pm.dpm.uvd_ps;
1485 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1486 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1489 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1490 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1493 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1494 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1497 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1498 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1501 case POWER_STATE_TYPE_INTERNAL_BOOT:
1502 return adev->pm.dpm.boot_ps;
1503 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1504 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1507 case POWER_STATE_TYPE_INTERNAL_ACPI:
1508 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1511 case POWER_STATE_TYPE_INTERNAL_ULV:
1512 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1515 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1516 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1523 /* use a fallback state if we didn't match */
1524 switch (dpm_state) {
1525 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1526 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1527 goto restart_search;
1528 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1529 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1530 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1531 if (adev->pm.dpm.uvd_ps) {
1532 return adev->pm.dpm.uvd_ps;
1534 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1535 goto restart_search;
1537 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1538 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1539 goto restart_search;
1540 case POWER_STATE_TYPE_INTERNAL_ACPI:
1541 dpm_state = POWER_STATE_TYPE_BATTERY;
1542 goto restart_search;
1543 case POWER_STATE_TYPE_BATTERY:
1544 case POWER_STATE_TYPE_BALANCED:
1545 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1546 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1547 goto restart_search;
1555 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1557 struct amdgpu_ps *ps;
1558 enum amd_pm_state_type dpm_state;
1562 /* if dpm init failed */
1563 if (!adev->pm.dpm_enabled)
1566 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1567 /* add other state override checks here */
1568 if ((!adev->pm.dpm.thermal_active) &&
1569 (!adev->pm.dpm.uvd_active))
1570 adev->pm.dpm.state = adev->pm.dpm.user_state;
1572 dpm_state = adev->pm.dpm.state;
1574 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1576 adev->pm.dpm.requested_ps = ps;
1580 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
1581 printk("switching from power state:\n");
1582 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1583 printk("switching to power state:\n");
1584 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1587 /* update whether vce is active */
1588 ps->vce_active = adev->pm.dpm.vce_active;
1589 if (adev->powerplay.pp_funcs->display_configuration_changed)
1590 amdgpu_dpm_display_configuration_changed(adev);
1592 ret = amdgpu_dpm_pre_set_power_state(adev);
1596 if (adev->powerplay.pp_funcs->check_state_equal) {
1597 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1604 amdgpu_dpm_set_power_state(adev);
1605 amdgpu_dpm_post_set_power_state(adev);
1607 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1608 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1610 if (adev->powerplay.pp_funcs->force_performance_level) {
1611 if (adev->pm.dpm.thermal_active) {
1612 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
1613 /* force low perf level for thermal */
1614 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
1615 /* save the user's level */
1616 adev->pm.dpm.forced_level = level;
1618 /* otherwise, user selected level */
1619 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1624 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1626 if (adev->powerplay.pp_funcs->powergate_uvd) {
1627 /* enable/disable UVD */
1628 mutex_lock(&adev->pm.mutex);
1629 amdgpu_dpm_powergate_uvd(adev, !enable);
1630 mutex_unlock(&adev->pm.mutex);
1633 mutex_lock(&adev->pm.mutex);
1634 adev->pm.dpm.uvd_active = true;
1635 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
1636 mutex_unlock(&adev->pm.mutex);
1638 mutex_lock(&adev->pm.mutex);
1639 adev->pm.dpm.uvd_active = false;
1640 mutex_unlock(&adev->pm.mutex);
1642 amdgpu_pm_compute_clocks(adev);
1646 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1648 if (adev->powerplay.pp_funcs->powergate_vce) {
1649 /* enable/disable VCE */
1650 mutex_lock(&adev->pm.mutex);
1651 amdgpu_dpm_powergate_vce(adev, !enable);
1652 mutex_unlock(&adev->pm.mutex);
1655 mutex_lock(&adev->pm.mutex);
1656 adev->pm.dpm.vce_active = true;
1657 /* XXX select vce level based on ring/task */
1658 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
1659 mutex_unlock(&adev->pm.mutex);
1660 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1661 AMD_CG_STATE_UNGATE);
1662 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1663 AMD_PG_STATE_UNGATE);
1664 amdgpu_pm_compute_clocks(adev);
1666 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1668 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1670 mutex_lock(&adev->pm.mutex);
1671 adev->pm.dpm.vce_active = false;
1672 mutex_unlock(&adev->pm.mutex);
1673 amdgpu_pm_compute_clocks(adev);
1679 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1683 if (adev->powerplay.pp_funcs->print_power_state == NULL)
1686 for (i = 0; i < adev->pm.dpm.num_ps; i++)
1687 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
1691 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1695 if (adev->pm.sysfs_initialized)
1698 if (adev->pm.dpm_enabled == 0)
1701 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1704 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1705 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1707 "Unable to register hwmon device: %d\n", ret);
1711 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1713 DRM_ERROR("failed to create device file for dpm state\n");
1716 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1718 DRM_ERROR("failed to create device file for dpm state\n");
1723 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1725 DRM_ERROR("failed to create device file pp_num_states\n");
1728 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1730 DRM_ERROR("failed to create device file pp_cur_state\n");
1733 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1735 DRM_ERROR("failed to create device file pp_force_state\n");
1738 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1740 DRM_ERROR("failed to create device file pp_table\n");
1744 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1746 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1749 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1751 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1754 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1756 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1759 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1761 DRM_ERROR("failed to create device file pp_sclk_od\n");
1764 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1766 DRM_ERROR("failed to create device file pp_mclk_od\n");
1769 ret = device_create_file(adev->dev,
1770 &dev_attr_pp_gfx_power_profile);
1772 DRM_ERROR("failed to create device file "
1773 "pp_gfx_power_profile\n");
1776 ret = device_create_file(adev->dev,
1777 &dev_attr_pp_compute_power_profile);
1779 DRM_ERROR("failed to create device file "
1780 "pp_compute_power_profile\n");
1784 ret = device_create_file(adev->dev,
1785 &dev_attr_pp_power_profile_mode);
1787 DRM_ERROR("failed to create device file "
1788 "pp_power_profile_mode\n");
1791 ret = device_create_file(adev->dev,
1792 &dev_attr_pp_od_clk_voltage);
1794 DRM_ERROR("failed to create device file "
1795 "pp_od_clk_voltage\n");
1798 ret = amdgpu_debugfs_pm_init(adev);
1800 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1804 adev->pm.sysfs_initialized = true;
1809 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1811 if (adev->pm.dpm_enabled == 0)
1814 if (adev->pm.int_hwmon_dev)
1815 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1816 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1817 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1819 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1820 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1821 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1822 device_remove_file(adev->dev, &dev_attr_pp_table);
1824 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1825 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1826 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
1827 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
1828 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
1829 device_remove_file(adev->dev,
1830 &dev_attr_pp_gfx_power_profile);
1831 device_remove_file(adev->dev,
1832 &dev_attr_pp_compute_power_profile);
1833 device_remove_file(adev->dev,
1834 &dev_attr_pp_power_profile_mode);
1835 device_remove_file(adev->dev,
1836 &dev_attr_pp_od_clk_voltage);
1839 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1841 struct drm_device *ddev = adev->ddev;
1842 struct drm_crtc *crtc;
1843 struct amdgpu_crtc *amdgpu_crtc;
1846 if (!adev->pm.dpm_enabled)
1849 if (adev->mode_info.num_crtc)
1850 amdgpu_display_bandwidth_update(adev);
1852 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1853 struct amdgpu_ring *ring = adev->rings[i];
1854 if (ring && ring->ready)
1855 amdgpu_fence_wait_empty(ring);
1858 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1859 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
1861 mutex_lock(&adev->pm.mutex);
1862 adev->pm.dpm.new_active_crtcs = 0;
1863 adev->pm.dpm.new_active_crtc_count = 0;
1864 if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
1865 list_for_each_entry(crtc,
1866 &ddev->mode_config.crtc_list, head) {
1867 amdgpu_crtc = to_amdgpu_crtc(crtc);
1868 if (amdgpu_crtc->enabled) {
1869 adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
1870 adev->pm.dpm.new_active_crtc_count++;
1874 /* update battery/ac status */
1875 if (power_supply_is_system_supplied() > 0)
1876 adev->pm.dpm.ac_power = true;
1878 adev->pm.dpm.ac_power = false;
1880 amdgpu_dpm_change_power_state_locked(adev);
1882 mutex_unlock(&adev->pm.mutex);
1889 #if defined(CONFIG_DEBUG_FS)
1891 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1894 struct pp_gpu_power query = {0};
1897 /* sanity check PP is enabled */
1898 if (!(adev->powerplay.pp_funcs &&
1899 adev->powerplay.pp_funcs->read_sensor))
1903 size = sizeof(value);
1904 seq_printf(m, "GFX Clocks and Power:\n");
1905 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
1906 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
1907 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
1908 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
1909 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
1910 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
1911 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
1912 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
1913 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
1914 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
1915 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
1916 seq_printf(m, "\t%u mV (VDDNB)\n", value);
1917 size = sizeof(query);
1918 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
1919 seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
1920 query.vddc_power & 0xff);
1921 seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
1922 query.vddci_power & 0xff);
1923 seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
1924 query.max_gpu_power & 0xff);
1925 seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
1926 query.average_gpu_power & 0xff);
1928 size = sizeof(value);
1929 seq_printf(m, "\n");
1932 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
1933 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1936 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
1937 seq_printf(m, "GPU Load: %u %%\n", value);
1938 seq_printf(m, "\n");
1941 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
1943 seq_printf(m, "UVD: Disabled\n");
1945 seq_printf(m, "UVD: Enabled\n");
1946 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
1947 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
1948 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
1949 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1952 seq_printf(m, "\n");
1955 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
1957 seq_printf(m, "VCE: Disabled\n");
1959 seq_printf(m, "VCE: Enabled\n");
1960 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
1961 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
1968 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
1972 for (i = 0; clocks[i].flag; i++)
1973 seq_printf(m, "\t%s: %s\n", clocks[i].name,
1974 (flags & clocks[i].flag) ? "On" : "Off");
1977 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
1979 struct drm_info_node *node = (struct drm_info_node *) m->private;
1980 struct drm_device *dev = node->minor->dev;
1981 struct amdgpu_device *adev = dev->dev_private;
1982 struct drm_device *ddev = adev->ddev;
1985 amdgpu_device_ip_get_clockgating_state(adev, &flags);
1986 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
1987 amdgpu_parse_cg_state(m, flags);
1988 seq_printf(m, "\n");
1990 if (!adev->pm.dpm_enabled) {
1991 seq_printf(m, "dpm not enabled\n");
1994 if ((adev->flags & AMD_IS_PX) &&
1995 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1996 seq_printf(m, "PX asic powered off\n");
1997 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
1998 mutex_lock(&adev->pm.mutex);
1999 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
2000 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
2002 seq_printf(m, "Debugfs support not implemented for this asic\n");
2003 mutex_unlock(&adev->pm.mutex);
2005 return amdgpu_debugfs_pm_info_pp(m, adev);
2011 static const struct drm_info_list amdgpu_pm_info_list[] = {
2012 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
2016 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
2018 #if defined(CONFIG_DEBUG_FS)
2019 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));