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);
81 * DOC: power_dpm_state
83 * This is a legacy interface and is only provided for backwards compatibility.
84 * The amdgpu driver provides a sysfs API for adjusting certain power
85 * related parameters. The file power_dpm_state is used for this.
86 * It accepts the following arguments:
93 * On older GPUs, the vbios provided a special power state for battery
94 * operation. Selecting battery switched to this state. This is no
95 * longer provided on newer GPUs so the option does nothing in that case.
99 * On older GPUs, the vbios provided a special power state for balanced
100 * operation. Selecting balanced switched to this state. This is no
101 * longer provided on newer GPUs so the option does nothing in that case.
105 * On older GPUs, the vbios provided a special power state for performance
106 * operation. Selecting performance switched to this state. This is no
107 * longer provided on newer GPUs so the option does nothing in that case.
111 static ssize_t amdgpu_get_dpm_state(struct device *dev,
112 struct device_attribute *attr,
115 struct drm_device *ddev = dev_get_drvdata(dev);
116 struct amdgpu_device *adev = ddev->dev_private;
117 enum amd_pm_state_type pm;
119 if (adev->powerplay.pp_funcs->get_current_power_state)
120 pm = amdgpu_dpm_get_current_power_state(adev);
122 pm = adev->pm.dpm.user_state;
124 return snprintf(buf, PAGE_SIZE, "%s\n",
125 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
126 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
129 static ssize_t amdgpu_set_dpm_state(struct device *dev,
130 struct device_attribute *attr,
134 struct drm_device *ddev = dev_get_drvdata(dev);
135 struct amdgpu_device *adev = ddev->dev_private;
136 enum amd_pm_state_type state;
138 if (strncmp("battery", buf, strlen("battery")) == 0)
139 state = POWER_STATE_TYPE_BATTERY;
140 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
141 state = POWER_STATE_TYPE_BALANCED;
142 else if (strncmp("performance", buf, strlen("performance")) == 0)
143 state = POWER_STATE_TYPE_PERFORMANCE;
149 if (adev->powerplay.pp_funcs->dispatch_tasks) {
150 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
152 mutex_lock(&adev->pm.mutex);
153 adev->pm.dpm.user_state = state;
154 mutex_unlock(&adev->pm.mutex);
156 /* Can't set dpm state when the card is off */
157 if (!(adev->flags & AMD_IS_PX) ||
158 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
159 amdgpu_pm_compute_clocks(adev);
167 * DOC: power_dpm_force_performance_level
169 * The amdgpu driver provides a sysfs API for adjusting certain power
170 * related parameters. The file power_dpm_force_performance_level is
171 * used for this. It accepts the following arguments:
184 * When auto is selected, the driver will attempt to dynamically select
185 * the optimal power profile for current conditions in the driver.
189 * When low is selected, the clocks are forced to the lowest power state.
193 * When high is selected, the clocks are forced to the highest power state.
197 * When manual is selected, the user can manually adjust which power states
198 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
199 * and pp_dpm_pcie files and adjust the power state transition heuristics
200 * via the pp_power_profile_mode sysfs file.
207 * When the profiling modes are selected, clock and power gating are
208 * disabled and the clocks are set for different profiling cases. This
209 * mode is recommended for profiling specific work loads where you do
210 * not want clock or power gating for clock fluctuation to interfere
211 * with your results. profile_standard sets the clocks to a fixed clock
212 * level which varies from asic to asic. profile_min_sclk forces the sclk
213 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
214 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
218 static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
219 struct device_attribute *attr,
222 struct drm_device *ddev = dev_get_drvdata(dev);
223 struct amdgpu_device *adev = ddev->dev_private;
224 enum amd_dpm_forced_level level = 0xff;
226 if ((adev->flags & AMD_IS_PX) &&
227 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
228 return snprintf(buf, PAGE_SIZE, "off\n");
230 if (adev->powerplay.pp_funcs->get_performance_level)
231 level = amdgpu_dpm_get_performance_level(adev);
233 level = adev->pm.dpm.forced_level;
235 return snprintf(buf, PAGE_SIZE, "%s\n",
236 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
237 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
238 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
239 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
240 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
241 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
242 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
243 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
247 static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
248 struct device_attribute *attr,
252 struct drm_device *ddev = dev_get_drvdata(dev);
253 struct amdgpu_device *adev = ddev->dev_private;
254 enum amd_dpm_forced_level level;
255 enum amd_dpm_forced_level current_level = 0xff;
258 /* Can't force performance level when the card is off */
259 if ((adev->flags & AMD_IS_PX) &&
260 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
263 if (adev->powerplay.pp_funcs->get_performance_level)
264 current_level = amdgpu_dpm_get_performance_level(adev);
266 if (strncmp("low", buf, strlen("low")) == 0) {
267 level = AMD_DPM_FORCED_LEVEL_LOW;
268 } else if (strncmp("high", buf, strlen("high")) == 0) {
269 level = AMD_DPM_FORCED_LEVEL_HIGH;
270 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
271 level = AMD_DPM_FORCED_LEVEL_AUTO;
272 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
273 level = AMD_DPM_FORCED_LEVEL_MANUAL;
274 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
275 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
276 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
277 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
278 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
279 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
280 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
281 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
282 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
283 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
289 if (current_level == level)
292 if (adev->powerplay.pp_funcs->force_performance_level) {
293 mutex_lock(&adev->pm.mutex);
294 if (adev->pm.dpm.thermal_active) {
296 mutex_unlock(&adev->pm.mutex);
299 ret = amdgpu_dpm_force_performance_level(adev, level);
303 adev->pm.dpm.forced_level = level;
304 mutex_unlock(&adev->pm.mutex);
311 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
312 struct device_attribute *attr,
315 struct drm_device *ddev = dev_get_drvdata(dev);
316 struct amdgpu_device *adev = ddev->dev_private;
317 struct pp_states_info data;
320 if (adev->powerplay.pp_funcs->get_pp_num_states)
321 amdgpu_dpm_get_pp_num_states(adev, &data);
323 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
324 for (i = 0; i < data.nums; i++)
325 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
326 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
327 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
328 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
329 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
334 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
335 struct device_attribute *attr,
338 struct drm_device *ddev = dev_get_drvdata(dev);
339 struct amdgpu_device *adev = ddev->dev_private;
340 struct pp_states_info data;
341 enum amd_pm_state_type pm = 0;
344 if (adev->powerplay.pp_funcs->get_current_power_state
345 && adev->powerplay.pp_funcs->get_pp_num_states) {
346 pm = amdgpu_dpm_get_current_power_state(adev);
347 amdgpu_dpm_get_pp_num_states(adev, &data);
349 for (i = 0; i < data.nums; i++) {
350 if (pm == data.states[i])
358 return snprintf(buf, PAGE_SIZE, "%d\n", i);
361 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
362 struct device_attribute *attr,
365 struct drm_device *ddev = dev_get_drvdata(dev);
366 struct amdgpu_device *adev = ddev->dev_private;
368 if (adev->pp_force_state_enabled)
369 return amdgpu_get_pp_cur_state(dev, attr, buf);
371 return snprintf(buf, PAGE_SIZE, "\n");
374 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
375 struct device_attribute *attr,
379 struct drm_device *ddev = dev_get_drvdata(dev);
380 struct amdgpu_device *adev = ddev->dev_private;
381 enum amd_pm_state_type state = 0;
385 if (strlen(buf) == 1)
386 adev->pp_force_state_enabled = false;
387 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
388 adev->powerplay.pp_funcs->get_pp_num_states) {
389 struct pp_states_info data;
391 ret = kstrtoul(buf, 0, &idx);
392 if (ret || idx >= ARRAY_SIZE(data.states)) {
397 amdgpu_dpm_get_pp_num_states(adev, &data);
398 state = data.states[idx];
399 /* only set user selected power states */
400 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
401 state != POWER_STATE_TYPE_DEFAULT) {
402 amdgpu_dpm_dispatch_task(adev,
403 AMD_PP_TASK_ENABLE_USER_STATE, &state);
404 adev->pp_force_state_enabled = true;
414 * The amdgpu driver provides a sysfs API for uploading new powerplay
415 * tables. The file pp_table is used for this. Reading the file
416 * will dump the current power play table. Writing to the file
417 * will attempt to upload a new powerplay table and re-initialize
418 * powerplay using that new table.
422 static ssize_t amdgpu_get_pp_table(struct device *dev,
423 struct device_attribute *attr,
426 struct drm_device *ddev = dev_get_drvdata(dev);
427 struct amdgpu_device *adev = ddev->dev_private;
431 if (adev->powerplay.pp_funcs->get_pp_table)
432 size = amdgpu_dpm_get_pp_table(adev, &table);
436 if (size >= PAGE_SIZE)
437 size = PAGE_SIZE - 1;
439 memcpy(buf, table, size);
444 static ssize_t amdgpu_set_pp_table(struct device *dev,
445 struct device_attribute *attr,
449 struct drm_device *ddev = dev_get_drvdata(dev);
450 struct amdgpu_device *adev = ddev->dev_private;
452 if (adev->powerplay.pp_funcs->set_pp_table)
453 amdgpu_dpm_set_pp_table(adev, buf, count);
458 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
459 struct device_attribute *attr,
463 struct drm_device *ddev = dev_get_drvdata(dev);
464 struct amdgpu_device *adev = ddev->dev_private;
466 uint32_t parameter_size = 0;
471 const char delimiter[3] = {' ', '\n', '\0'};
478 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
479 else if (*buf == 'm')
480 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
482 type = PP_OD_RESTORE_DEFAULT_TABLE;
483 else if (*buf == 'c')
484 type = PP_OD_COMMIT_DPM_TABLE;
488 memcpy(buf_cpy, buf, count+1);
492 while (isspace(*++tmp_str));
495 sub_str = strsep(&tmp_str, delimiter);
496 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
501 while (isspace(*tmp_str))
505 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
506 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
507 parameter, parameter_size);
512 if (type == PP_OD_COMMIT_DPM_TABLE) {
513 if (adev->powerplay.pp_funcs->dispatch_tasks) {
514 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
524 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
525 struct device_attribute *attr,
528 struct drm_device *ddev = dev_get_drvdata(dev);
529 struct amdgpu_device *adev = ddev->dev_private;
532 if (adev->powerplay.pp_funcs->print_clock_levels) {
533 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
534 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
537 return snprintf(buf, PAGE_SIZE, "\n");
543 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_pcie
545 * The amdgpu driver provides a sysfs API for adjusting what power levels
546 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
547 * and pp_dpm_pcie are used for this.
549 * Reading back the files will show you the available power levels within
550 * the power state and the clock information for those levels.
552 * To manually adjust these states, first select manual using
553 * power_dpm_force_performance_level. Writing a string of the level
554 * numbers to the file will select which levels you want to enable.
555 * E.g., writing 456 to the file will enable levels 4, 5, and 6.
559 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
560 struct device_attribute *attr,
563 struct drm_device *ddev = dev_get_drvdata(dev);
564 struct amdgpu_device *adev = ddev->dev_private;
566 if (adev->powerplay.pp_funcs->print_clock_levels)
567 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
569 return snprintf(buf, PAGE_SIZE, "\n");
572 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
573 struct device_attribute *attr,
577 struct drm_device *ddev = dev_get_drvdata(dev);
578 struct amdgpu_device *adev = ddev->dev_private;
581 uint32_t i, mask = 0;
584 for (i = 0; i < strlen(buf); i++) {
585 if (*(buf + i) == '\n')
587 sub_str[0] = *(buf + i);
589 ret = kstrtol(sub_str, 0, &level);
598 if (adev->powerplay.pp_funcs->force_clock_level)
599 amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
605 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
606 struct device_attribute *attr,
609 struct drm_device *ddev = dev_get_drvdata(dev);
610 struct amdgpu_device *adev = ddev->dev_private;
612 if (adev->powerplay.pp_funcs->print_clock_levels)
613 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
615 return snprintf(buf, PAGE_SIZE, "\n");
618 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
619 struct device_attribute *attr,
623 struct drm_device *ddev = dev_get_drvdata(dev);
624 struct amdgpu_device *adev = ddev->dev_private;
627 uint32_t i, mask = 0;
630 for (i = 0; i < strlen(buf); i++) {
631 if (*(buf + i) == '\n')
633 sub_str[0] = *(buf + i);
635 ret = kstrtol(sub_str, 0, &level);
643 if (adev->powerplay.pp_funcs->force_clock_level)
644 amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
650 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
651 struct device_attribute *attr,
654 struct drm_device *ddev = dev_get_drvdata(dev);
655 struct amdgpu_device *adev = ddev->dev_private;
657 if (adev->powerplay.pp_funcs->print_clock_levels)
658 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
660 return snprintf(buf, PAGE_SIZE, "\n");
663 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
664 struct device_attribute *attr,
668 struct drm_device *ddev = dev_get_drvdata(dev);
669 struct amdgpu_device *adev = ddev->dev_private;
672 uint32_t i, mask = 0;
675 for (i = 0; i < strlen(buf); i++) {
676 if (*(buf + i) == '\n')
678 sub_str[0] = *(buf + i);
680 ret = kstrtol(sub_str, 0, &level);
688 if (adev->powerplay.pp_funcs->force_clock_level)
689 amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
695 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
696 struct device_attribute *attr,
699 struct drm_device *ddev = dev_get_drvdata(dev);
700 struct amdgpu_device *adev = ddev->dev_private;
703 if (adev->powerplay.pp_funcs->get_sclk_od)
704 value = amdgpu_dpm_get_sclk_od(adev);
706 return snprintf(buf, PAGE_SIZE, "%d\n", value);
709 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
710 struct device_attribute *attr,
714 struct drm_device *ddev = dev_get_drvdata(dev);
715 struct amdgpu_device *adev = ddev->dev_private;
719 ret = kstrtol(buf, 0, &value);
725 if (adev->powerplay.pp_funcs->set_sclk_od)
726 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
728 if (adev->powerplay.pp_funcs->dispatch_tasks) {
729 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
731 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
732 amdgpu_pm_compute_clocks(adev);
739 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
740 struct device_attribute *attr,
743 struct drm_device *ddev = dev_get_drvdata(dev);
744 struct amdgpu_device *adev = ddev->dev_private;
747 if (adev->powerplay.pp_funcs->get_mclk_od)
748 value = amdgpu_dpm_get_mclk_od(adev);
750 return snprintf(buf, PAGE_SIZE, "%d\n", value);
753 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
754 struct device_attribute *attr,
758 struct drm_device *ddev = dev_get_drvdata(dev);
759 struct amdgpu_device *adev = ddev->dev_private;
763 ret = kstrtol(buf, 0, &value);
769 if (adev->powerplay.pp_funcs->set_mclk_od)
770 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
772 if (adev->powerplay.pp_funcs->dispatch_tasks) {
773 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
775 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
776 amdgpu_pm_compute_clocks(adev);
783 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
784 struct device_attribute *attr,
787 struct drm_device *ddev = dev_get_drvdata(dev);
788 struct amdgpu_device *adev = ddev->dev_private;
790 if (adev->powerplay.pp_funcs->get_power_profile_mode)
791 return amdgpu_dpm_get_power_profile_mode(adev, buf);
793 return snprintf(buf, PAGE_SIZE, "\n");
797 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
798 struct device_attribute *attr,
803 struct drm_device *ddev = dev_get_drvdata(dev);
804 struct amdgpu_device *adev = ddev->dev_private;
805 uint32_t parameter_size = 0;
807 char *sub_str, buf_cpy[128];
811 long int profile_mode = 0;
812 const char delimiter[3] = {' ', '\n', '\0'};
816 ret = kstrtol(tmp, 0, &profile_mode);
820 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
821 if (count < 2 || count > 127)
823 while (isspace(*++buf))
825 memcpy(buf_cpy, buf, count-i);
828 sub_str = strsep(&tmp_str, delimiter);
829 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
835 while (isspace(*tmp_str))
839 parameter[parameter_size] = profile_mode;
840 if (adev->powerplay.pp_funcs->set_power_profile_mode)
841 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
849 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
850 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
851 amdgpu_get_dpm_forced_performance_level,
852 amdgpu_set_dpm_forced_performance_level);
853 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
854 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
855 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
856 amdgpu_get_pp_force_state,
857 amdgpu_set_pp_force_state);
858 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
860 amdgpu_set_pp_table);
861 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
862 amdgpu_get_pp_dpm_sclk,
863 amdgpu_set_pp_dpm_sclk);
864 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
865 amdgpu_get_pp_dpm_mclk,
866 amdgpu_set_pp_dpm_mclk);
867 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
868 amdgpu_get_pp_dpm_pcie,
869 amdgpu_set_pp_dpm_pcie);
870 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
871 amdgpu_get_pp_sclk_od,
872 amdgpu_set_pp_sclk_od);
873 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
874 amdgpu_get_pp_mclk_od,
875 amdgpu_set_pp_mclk_od);
876 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
877 amdgpu_get_pp_power_profile_mode,
878 amdgpu_set_pp_power_profile_mode);
879 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
880 amdgpu_get_pp_od_clk_voltage,
881 amdgpu_set_pp_od_clk_voltage);
883 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
884 struct device_attribute *attr,
887 struct amdgpu_device *adev = dev_get_drvdata(dev);
888 struct drm_device *ddev = adev->ddev;
889 int r, temp, size = sizeof(temp);
891 /* Can't get temperature when the card is off */
892 if ((adev->flags & AMD_IS_PX) &&
893 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
896 /* sanity check PP is enabled */
897 if (!(adev->powerplay.pp_funcs &&
898 adev->powerplay.pp_funcs->read_sensor))
901 /* get the temperature */
902 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
903 (void *)&temp, &size);
907 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
910 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
911 struct device_attribute *attr,
914 struct amdgpu_device *adev = dev_get_drvdata(dev);
915 int hyst = to_sensor_dev_attr(attr)->index;
919 temp = adev->pm.dpm.thermal.min_temp;
921 temp = adev->pm.dpm.thermal.max_temp;
923 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
926 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
927 struct device_attribute *attr,
930 struct amdgpu_device *adev = dev_get_drvdata(dev);
933 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
936 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
938 return sprintf(buf, "%i\n", pwm_mode);
941 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
942 struct device_attribute *attr,
946 struct amdgpu_device *adev = dev_get_drvdata(dev);
950 /* Can't adjust fan when the card is off */
951 if ((adev->flags & AMD_IS_PX) &&
952 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
955 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
958 err = kstrtoint(buf, 10, &value);
962 amdgpu_dpm_set_fan_control_mode(adev, value);
967 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
968 struct device_attribute *attr,
971 return sprintf(buf, "%i\n", 0);
974 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
975 struct device_attribute *attr,
978 return sprintf(buf, "%i\n", 255);
981 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
982 struct device_attribute *attr,
983 const char *buf, size_t count)
985 struct amdgpu_device *adev = dev_get_drvdata(dev);
989 /* Can't adjust fan when the card is off */
990 if ((adev->flags & AMD_IS_PX) &&
991 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
994 err = kstrtou32(buf, 10, &value);
998 value = (value * 100) / 255;
1000 if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1001 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1009 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1010 struct device_attribute *attr,
1013 struct amdgpu_device *adev = dev_get_drvdata(dev);
1017 /* Can't adjust fan when the card is off */
1018 if ((adev->flags & AMD_IS_PX) &&
1019 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1022 if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1023 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1028 speed = (speed * 255) / 100;
1030 return sprintf(buf, "%i\n", speed);
1033 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1034 struct device_attribute *attr,
1037 struct amdgpu_device *adev = dev_get_drvdata(dev);
1041 /* Can't adjust fan when the card is off */
1042 if ((adev->flags & AMD_IS_PX) &&
1043 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1046 if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1047 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1052 return sprintf(buf, "%i\n", speed);
1055 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1056 struct device_attribute *attr,
1059 struct amdgpu_device *adev = dev_get_drvdata(dev);
1060 struct drm_device *ddev = adev->ddev;
1062 int r, size = sizeof(vddgfx);
1064 /* Can't get voltage when the card is off */
1065 if ((adev->flags & AMD_IS_PX) &&
1066 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1069 /* sanity check PP is enabled */
1070 if (!(adev->powerplay.pp_funcs &&
1071 adev->powerplay.pp_funcs->read_sensor))
1074 /* get the voltage */
1075 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1076 (void *)&vddgfx, &size);
1080 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1083 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1084 struct device_attribute *attr,
1087 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1090 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1091 struct device_attribute *attr,
1094 struct amdgpu_device *adev = dev_get_drvdata(dev);
1095 struct drm_device *ddev = adev->ddev;
1097 int r, size = sizeof(vddnb);
1099 /* only APUs have vddnb */
1100 if (adev->flags & AMD_IS_APU)
1103 /* Can't get voltage when the card is off */
1104 if ((adev->flags & AMD_IS_PX) &&
1105 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1108 /* sanity check PP is enabled */
1109 if (!(adev->powerplay.pp_funcs &&
1110 adev->powerplay.pp_funcs->read_sensor))
1113 /* get the voltage */
1114 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1115 (void *)&vddnb, &size);
1119 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1122 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1123 struct device_attribute *attr,
1126 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1129 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1130 struct device_attribute *attr,
1133 struct amdgpu_device *adev = dev_get_drvdata(dev);
1134 struct drm_device *ddev = adev->ddev;
1136 int r, size = sizeof(u32);
1139 /* Can't get power when the card is off */
1140 if ((adev->flags & AMD_IS_PX) &&
1141 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1144 /* sanity check PP is enabled */
1145 if (!(adev->powerplay.pp_funcs &&
1146 adev->powerplay.pp_funcs->read_sensor))
1149 /* get the voltage */
1150 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1151 (void *)&query, &size);
1155 /* convert to microwatts */
1156 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
1158 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1161 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1162 struct device_attribute *attr,
1165 return sprintf(buf, "%i\n", 0);
1168 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1169 struct device_attribute *attr,
1172 struct amdgpu_device *adev = dev_get_drvdata(dev);
1175 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1176 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1177 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1179 return snprintf(buf, PAGE_SIZE, "\n");
1183 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1184 struct device_attribute *attr,
1187 struct amdgpu_device *adev = dev_get_drvdata(dev);
1190 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1191 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1192 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1194 return snprintf(buf, PAGE_SIZE, "\n");
1199 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
1200 struct device_attribute *attr,
1204 struct amdgpu_device *adev = dev_get_drvdata(dev);
1208 err = kstrtou32(buf, 10, &value);
1212 value = value / 1000000; /* convert to Watt */
1213 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
1214 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
1228 * The amdgpu driver exposes the following sensor interfaces:
1229 * - GPU temperature (via the on-die sensor)
1231 * - Northbridge voltage (APUs only)
1235 * hwmon interfaces for GPU temperature:
1236 * - temp1_input: the on die GPU temperature in millidegrees Celsius
1237 * - temp1_crit: temperature critical max value in millidegrees Celsius
1238 * - temp1_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
1240 * hwmon interfaces for GPU voltage:
1241 * - in0_input: the voltage on the GPU in millivolts
1242 * - in1_input: the voltage on the Northbridge in millivolts
1244 * hwmon interfaces for GPU power:
1245 * - power1_average: average power used by the GPU in microWatts
1246 * - power1_cap_min: minimum cap supported in microWatts
1247 * - power1_cap_max: maximum cap supported in microWatts
1248 * - power1_cap: selected power cap in microWatts
1250 * hwmon interfaces for GPU fan:
1251 * - pwm1: pulse width modulation fan level (0-255)
1252 * - pwm1_enable: pulse width modulation fan control method
1253 * 0: no fan speed control
1254 * 1: manual fan speed control using pwm interface
1255 * 2: automatic fan speed control
1256 * - pwm1_min: pulse width modulation fan control minimum level (0)
1257 * - pwm1_max: pulse width modulation fan control maximum level (255)
1258 * - fan1_input: fan speed in RPM
1260 * You can use hwmon tools like sensors to view this information on your system.
1264 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
1265 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
1266 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
1267 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
1268 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
1269 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
1270 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
1271 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
1272 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
1273 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
1274 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
1275 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
1276 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
1277 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
1278 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
1279 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
1281 static struct attribute *hwmon_attributes[] = {
1282 &sensor_dev_attr_temp1_input.dev_attr.attr,
1283 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1284 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1285 &sensor_dev_attr_pwm1.dev_attr.attr,
1286 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1287 &sensor_dev_attr_pwm1_min.dev_attr.attr,
1288 &sensor_dev_attr_pwm1_max.dev_attr.attr,
1289 &sensor_dev_attr_fan1_input.dev_attr.attr,
1290 &sensor_dev_attr_in0_input.dev_attr.attr,
1291 &sensor_dev_attr_in0_label.dev_attr.attr,
1292 &sensor_dev_attr_in1_input.dev_attr.attr,
1293 &sensor_dev_attr_in1_label.dev_attr.attr,
1294 &sensor_dev_attr_power1_average.dev_attr.attr,
1295 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
1296 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
1297 &sensor_dev_attr_power1_cap.dev_attr.attr,
1301 static umode_t hwmon_attributes_visible(struct kobject *kobj,
1302 struct attribute *attr, int index)
1304 struct device *dev = kobj_to_dev(kobj);
1305 struct amdgpu_device *adev = dev_get_drvdata(dev);
1306 umode_t effective_mode = attr->mode;
1308 /* handle non-powerplay limitations */
1309 if (!adev->powerplay.pp_handle) {
1310 /* Skip fan attributes if fan is not present */
1311 if (adev->pm.no_fan &&
1312 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1313 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1314 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1315 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1317 /* requires powerplay */
1318 if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
1322 /* Skip limit attributes if DPM is not enabled */
1323 if (!adev->pm.dpm_enabled &&
1324 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
1325 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
1326 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
1327 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
1328 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1329 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1332 /* mask fan attributes if we have no bindings for this asic to expose */
1333 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
1334 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
1335 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
1336 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
1337 effective_mode &= ~S_IRUGO;
1339 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1340 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
1341 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
1342 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
1343 effective_mode &= ~S_IWUSR;
1345 if ((adev->flags & AMD_IS_APU) &&
1346 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
1347 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
1348 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
1351 /* hide max/min values if we can't both query and manage the fan */
1352 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
1353 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
1354 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
1355 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
1358 /* only APUs have vddnb */
1359 if (!(adev->flags & AMD_IS_APU) &&
1360 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
1361 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
1364 return effective_mode;
1367 static const struct attribute_group hwmon_attrgroup = {
1368 .attrs = hwmon_attributes,
1369 .is_visible = hwmon_attributes_visible,
1372 static const struct attribute_group *hwmon_groups[] = {
1377 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
1379 struct amdgpu_device *adev =
1380 container_of(work, struct amdgpu_device,
1381 pm.dpm.thermal.work);
1382 /* switch to the thermal state */
1383 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
1384 int temp, size = sizeof(temp);
1386 if (!adev->pm.dpm_enabled)
1389 if (adev->powerplay.pp_funcs &&
1390 adev->powerplay.pp_funcs->read_sensor &&
1391 !amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
1392 (void *)&temp, &size)) {
1393 if (temp < adev->pm.dpm.thermal.min_temp)
1394 /* switch back the user state */
1395 dpm_state = adev->pm.dpm.user_state;
1397 if (adev->pm.dpm.thermal.high_to_low)
1398 /* switch back the user state */
1399 dpm_state = adev->pm.dpm.user_state;
1401 mutex_lock(&adev->pm.mutex);
1402 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
1403 adev->pm.dpm.thermal_active = true;
1405 adev->pm.dpm.thermal_active = false;
1406 adev->pm.dpm.state = dpm_state;
1407 mutex_unlock(&adev->pm.mutex);
1409 amdgpu_pm_compute_clocks(adev);
1412 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
1413 enum amd_pm_state_type dpm_state)
1416 struct amdgpu_ps *ps;
1418 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
1421 /* check if the vblank period is too short to adjust the mclk */
1422 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
1423 if (amdgpu_dpm_vblank_too_short(adev))
1424 single_display = false;
1427 /* certain older asics have a separare 3D performance state,
1428 * so try that first if the user selected performance
1430 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
1431 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
1432 /* balanced states don't exist at the moment */
1433 if (dpm_state == POWER_STATE_TYPE_BALANCED)
1434 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1437 /* Pick the best power state based on current conditions */
1438 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
1439 ps = &adev->pm.dpm.ps[i];
1440 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
1441 switch (dpm_state) {
1443 case POWER_STATE_TYPE_BATTERY:
1444 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
1445 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1452 case POWER_STATE_TYPE_BALANCED:
1453 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
1454 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1461 case POWER_STATE_TYPE_PERFORMANCE:
1462 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
1463 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
1470 /* internal states */
1471 case POWER_STATE_TYPE_INTERNAL_UVD:
1472 if (adev->pm.dpm.uvd_ps)
1473 return adev->pm.dpm.uvd_ps;
1476 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1477 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
1480 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1481 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
1484 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1485 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
1488 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1489 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1492 case POWER_STATE_TYPE_INTERNAL_BOOT:
1493 return adev->pm.dpm.boot_ps;
1494 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1495 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1498 case POWER_STATE_TYPE_INTERNAL_ACPI:
1499 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1502 case POWER_STATE_TYPE_INTERNAL_ULV:
1503 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1506 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1507 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1514 /* use a fallback state if we didn't match */
1515 switch (dpm_state) {
1516 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1517 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1518 goto restart_search;
1519 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1520 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1521 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1522 if (adev->pm.dpm.uvd_ps) {
1523 return adev->pm.dpm.uvd_ps;
1525 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1526 goto restart_search;
1528 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1529 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1530 goto restart_search;
1531 case POWER_STATE_TYPE_INTERNAL_ACPI:
1532 dpm_state = POWER_STATE_TYPE_BATTERY;
1533 goto restart_search;
1534 case POWER_STATE_TYPE_BATTERY:
1535 case POWER_STATE_TYPE_BALANCED:
1536 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1537 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1538 goto restart_search;
1546 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
1548 struct amdgpu_ps *ps;
1549 enum amd_pm_state_type dpm_state;
1553 /* if dpm init failed */
1554 if (!adev->pm.dpm_enabled)
1557 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
1558 /* add other state override checks here */
1559 if ((!adev->pm.dpm.thermal_active) &&
1560 (!adev->pm.dpm.uvd_active))
1561 adev->pm.dpm.state = adev->pm.dpm.user_state;
1563 dpm_state = adev->pm.dpm.state;
1565 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
1567 adev->pm.dpm.requested_ps = ps;
1571 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
1572 printk("switching from power state:\n");
1573 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
1574 printk("switching to power state:\n");
1575 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
1578 /* update whether vce is active */
1579 ps->vce_active = adev->pm.dpm.vce_active;
1580 if (adev->powerplay.pp_funcs->display_configuration_changed)
1581 amdgpu_dpm_display_configuration_changed(adev);
1583 ret = amdgpu_dpm_pre_set_power_state(adev);
1587 if (adev->powerplay.pp_funcs->check_state_equal) {
1588 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
1595 amdgpu_dpm_set_power_state(adev);
1596 amdgpu_dpm_post_set_power_state(adev);
1598 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
1599 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
1601 if (adev->powerplay.pp_funcs->force_performance_level) {
1602 if (adev->pm.dpm.thermal_active) {
1603 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
1604 /* force low perf level for thermal */
1605 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
1606 /* save the user's level */
1607 adev->pm.dpm.forced_level = level;
1609 /* otherwise, user selected level */
1610 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
1615 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
1617 if (adev->powerplay.pp_funcs->powergate_uvd) {
1618 /* enable/disable UVD */
1619 mutex_lock(&adev->pm.mutex);
1620 amdgpu_dpm_powergate_uvd(adev, !enable);
1621 mutex_unlock(&adev->pm.mutex);
1624 mutex_lock(&adev->pm.mutex);
1625 adev->pm.dpm.uvd_active = true;
1626 adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
1627 mutex_unlock(&adev->pm.mutex);
1629 mutex_lock(&adev->pm.mutex);
1630 adev->pm.dpm.uvd_active = false;
1631 mutex_unlock(&adev->pm.mutex);
1633 amdgpu_pm_compute_clocks(adev);
1637 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
1639 if (adev->powerplay.pp_funcs->powergate_vce) {
1640 /* enable/disable VCE */
1641 mutex_lock(&adev->pm.mutex);
1642 amdgpu_dpm_powergate_vce(adev, !enable);
1643 mutex_unlock(&adev->pm.mutex);
1646 mutex_lock(&adev->pm.mutex);
1647 adev->pm.dpm.vce_active = true;
1648 /* XXX select vce level based on ring/task */
1649 adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
1650 mutex_unlock(&adev->pm.mutex);
1651 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1652 AMD_CG_STATE_UNGATE);
1653 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1654 AMD_PG_STATE_UNGATE);
1655 amdgpu_pm_compute_clocks(adev);
1657 amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1659 amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
1661 mutex_lock(&adev->pm.mutex);
1662 adev->pm.dpm.vce_active = false;
1663 mutex_unlock(&adev->pm.mutex);
1664 amdgpu_pm_compute_clocks(adev);
1670 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
1674 if (adev->powerplay.pp_funcs->print_power_state == NULL)
1677 for (i = 0; i < adev->pm.dpm.num_ps; i++)
1678 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
1682 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
1686 if (adev->pm.sysfs_initialized)
1689 if (adev->pm.dpm_enabled == 0)
1692 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
1695 if (IS_ERR(adev->pm.int_hwmon_dev)) {
1696 ret = PTR_ERR(adev->pm.int_hwmon_dev);
1698 "Unable to register hwmon device: %d\n", ret);
1702 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
1704 DRM_ERROR("failed to create device file for dpm state\n");
1707 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1709 DRM_ERROR("failed to create device file for dpm state\n");
1714 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
1716 DRM_ERROR("failed to create device file pp_num_states\n");
1719 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
1721 DRM_ERROR("failed to create device file pp_cur_state\n");
1724 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
1726 DRM_ERROR("failed to create device file pp_force_state\n");
1729 ret = device_create_file(adev->dev, &dev_attr_pp_table);
1731 DRM_ERROR("failed to create device file pp_table\n");
1735 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
1737 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
1740 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
1742 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
1745 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
1747 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
1750 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
1752 DRM_ERROR("failed to create device file pp_sclk_od\n");
1755 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
1757 DRM_ERROR("failed to create device file pp_mclk_od\n");
1760 ret = device_create_file(adev->dev,
1761 &dev_attr_pp_power_profile_mode);
1763 DRM_ERROR("failed to create device file "
1764 "pp_power_profile_mode\n");
1767 ret = device_create_file(adev->dev,
1768 &dev_attr_pp_od_clk_voltage);
1770 DRM_ERROR("failed to create device file "
1771 "pp_od_clk_voltage\n");
1774 ret = amdgpu_debugfs_pm_init(adev);
1776 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1780 adev->pm.sysfs_initialized = true;
1785 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
1787 if (adev->pm.dpm_enabled == 0)
1790 if (adev->pm.int_hwmon_dev)
1791 hwmon_device_unregister(adev->pm.int_hwmon_dev);
1792 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
1793 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
1795 device_remove_file(adev->dev, &dev_attr_pp_num_states);
1796 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
1797 device_remove_file(adev->dev, &dev_attr_pp_force_state);
1798 device_remove_file(adev->dev, &dev_attr_pp_table);
1800 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
1801 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
1802 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
1803 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
1804 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
1805 device_remove_file(adev->dev,
1806 &dev_attr_pp_power_profile_mode);
1807 device_remove_file(adev->dev,
1808 &dev_attr_pp_od_clk_voltage);
1811 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
1815 if (!adev->pm.dpm_enabled)
1818 if (adev->mode_info.num_crtc)
1819 amdgpu_display_bandwidth_update(adev);
1821 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1822 struct amdgpu_ring *ring = adev->rings[i];
1823 if (ring && ring->ready)
1824 amdgpu_fence_wait_empty(ring);
1827 if (!amdgpu_device_has_dc_support(adev)) {
1828 mutex_lock(&adev->pm.mutex);
1829 amdgpu_dpm_get_active_displays(adev);
1830 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtcs;
1831 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
1832 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
1833 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
1834 if (adev->pm.pm_display_cfg.vrefresh > 120)
1835 adev->pm.pm_display_cfg.min_vblank_time = 0;
1836 if (adev->powerplay.pp_funcs->display_configuration_change)
1837 adev->powerplay.pp_funcs->display_configuration_change(
1838 adev->powerplay.pp_handle,
1839 &adev->pm.pm_display_cfg);
1840 mutex_unlock(&adev->pm.mutex);
1843 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1844 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
1846 mutex_lock(&adev->pm.mutex);
1847 /* update battery/ac status */
1848 if (power_supply_is_system_supplied() > 0)
1849 adev->pm.dpm.ac_power = true;
1851 adev->pm.dpm.ac_power = false;
1853 amdgpu_dpm_change_power_state_locked(adev);
1855 mutex_unlock(&adev->pm.mutex);
1862 #if defined(CONFIG_DEBUG_FS)
1864 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
1870 /* sanity check PP is enabled */
1871 if (!(adev->powerplay.pp_funcs &&
1872 adev->powerplay.pp_funcs->read_sensor))
1876 size = sizeof(value);
1877 seq_printf(m, "GFX Clocks and Power:\n");
1878 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
1879 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
1880 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
1881 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
1882 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
1883 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
1884 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
1885 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
1886 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
1887 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
1888 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
1889 seq_printf(m, "\t%u mV (VDDNB)\n", value);
1890 size = sizeof(uint32_t);
1891 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
1892 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
1893 size = sizeof(value);
1894 seq_printf(m, "\n");
1897 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
1898 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
1901 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
1902 seq_printf(m, "GPU Load: %u %%\n", value);
1903 seq_printf(m, "\n");
1906 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
1908 seq_printf(m, "UVD: Disabled\n");
1910 seq_printf(m, "UVD: Enabled\n");
1911 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
1912 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
1913 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
1914 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
1917 seq_printf(m, "\n");
1920 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
1922 seq_printf(m, "VCE: Disabled\n");
1924 seq_printf(m, "VCE: Enabled\n");
1925 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
1926 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
1933 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
1937 for (i = 0; clocks[i].flag; i++)
1938 seq_printf(m, "\t%s: %s\n", clocks[i].name,
1939 (flags & clocks[i].flag) ? "On" : "Off");
1942 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
1944 struct drm_info_node *node = (struct drm_info_node *) m->private;
1945 struct drm_device *dev = node->minor->dev;
1946 struct amdgpu_device *adev = dev->dev_private;
1947 struct drm_device *ddev = adev->ddev;
1950 amdgpu_device_ip_get_clockgating_state(adev, &flags);
1951 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
1952 amdgpu_parse_cg_state(m, flags);
1953 seq_printf(m, "\n");
1955 if (!adev->pm.dpm_enabled) {
1956 seq_printf(m, "dpm not enabled\n");
1959 if ((adev->flags & AMD_IS_PX) &&
1960 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1961 seq_printf(m, "PX asic powered off\n");
1962 } else if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
1963 mutex_lock(&adev->pm.mutex);
1964 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
1965 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
1967 seq_printf(m, "Debugfs support not implemented for this asic\n");
1968 mutex_unlock(&adev->pm.mutex);
1970 return amdgpu_debugfs_pm_info_pp(m, adev);
1976 static const struct drm_info_list amdgpu_pm_info_list[] = {
1977 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
1981 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
1983 #if defined(CONFIG_DEBUG_FS)
1984 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));