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.
22 * Authors: Rafał Miłecki <zajec5@gmail.com>
23 * Alex Deucher <alexdeucher@gmail.com>
26 #include <drm/drm_debugfs.h>
29 #include "amdgpu_drv.h"
30 #include "amdgpu_pm.h"
31 #include "amdgpu_dpm.h"
32 #include "amdgpu_display.h"
33 #include "amdgpu_smu.h"
35 #include <linux/power_supply.h>
36 #include <linux/pci.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
39 #include <linux/nospec.h>
43 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
45 static const struct cg_flag_name clocks[] = {
46 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
47 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
48 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
49 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
50 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
51 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
52 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
53 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
54 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
55 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
56 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
57 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
58 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
59 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
60 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
61 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
62 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
63 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
64 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
65 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
66 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
67 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
68 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
69 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
73 static const struct hwmon_temp_label {
74 enum PP_HWMON_TEMP channel;
77 {PP_TEMP_EDGE, "edge"},
78 {PP_TEMP_JUNCTION, "junction"},
82 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
84 if (adev->pm.dpm_enabled) {
85 mutex_lock(&adev->pm.mutex);
86 if (power_supply_is_system_supplied() > 0)
87 adev->pm.ac_power = true;
89 adev->pm.ac_power = false;
90 if (adev->powerplay.pp_funcs->enable_bapm)
91 amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
92 mutex_unlock(&adev->pm.mutex);
96 int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor,
97 void *data, uint32_t *size)
104 if (is_support_sw_smu(adev))
105 ret = smu_read_sensor(&adev->smu, sensor, data, size);
107 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
108 ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle,
118 * DOC: power_dpm_state
120 * The power_dpm_state file is a legacy interface and is only provided for
121 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
122 * certain power related parameters. The file power_dpm_state is used for this.
123 * It accepts the following arguments:
133 * On older GPUs, the vbios provided a special power state for battery
134 * operation. Selecting battery switched to this state. This is no
135 * longer provided on newer GPUs so the option does nothing in that case.
139 * On older GPUs, the vbios provided a special power state for balanced
140 * operation. Selecting balanced switched to this state. This is no
141 * longer provided on newer GPUs so the option does nothing in that case.
145 * On older GPUs, the vbios provided a special power state for performance
146 * operation. Selecting performance switched to this state. This is no
147 * longer provided on newer GPUs so the option does nothing in that case.
151 static ssize_t amdgpu_get_dpm_state(struct device *dev,
152 struct device_attribute *attr,
155 struct drm_device *ddev = dev_get_drvdata(dev);
156 struct amdgpu_device *adev = ddev->dev_private;
157 enum amd_pm_state_type pm;
159 if (is_support_sw_smu(adev) && adev->smu.ppt_funcs->get_current_power_state)
160 pm = amdgpu_smu_get_current_power_state(adev);
161 else if (adev->powerplay.pp_funcs->get_current_power_state)
162 pm = amdgpu_dpm_get_current_power_state(adev);
164 pm = adev->pm.dpm.user_state;
166 return snprintf(buf, PAGE_SIZE, "%s\n",
167 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
168 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
171 static ssize_t amdgpu_set_dpm_state(struct device *dev,
172 struct device_attribute *attr,
176 struct drm_device *ddev = dev_get_drvdata(dev);
177 struct amdgpu_device *adev = ddev->dev_private;
178 enum amd_pm_state_type state;
180 if (strncmp("battery", buf, strlen("battery")) == 0)
181 state = POWER_STATE_TYPE_BATTERY;
182 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
183 state = POWER_STATE_TYPE_BALANCED;
184 else if (strncmp("performance", buf, strlen("performance")) == 0)
185 state = POWER_STATE_TYPE_PERFORMANCE;
191 if (adev->powerplay.pp_funcs->dispatch_tasks) {
192 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
194 mutex_lock(&adev->pm.mutex);
195 adev->pm.dpm.user_state = state;
196 mutex_unlock(&adev->pm.mutex);
198 /* Can't set dpm state when the card is off */
199 if (!(adev->flags & AMD_IS_PX) ||
200 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
201 amdgpu_pm_compute_clocks(adev);
209 * DOC: power_dpm_force_performance_level
211 * The amdgpu driver provides a sysfs API for adjusting certain power
212 * related parameters. The file power_dpm_force_performance_level is
213 * used for this. It accepts the following arguments:
233 * When auto is selected, the driver will attempt to dynamically select
234 * the optimal power profile for current conditions in the driver.
238 * When low is selected, the clocks are forced to the lowest power state.
242 * When high is selected, the clocks are forced to the highest power state.
246 * When manual is selected, the user can manually adjust which power states
247 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
248 * and pp_dpm_pcie files and adjust the power state transition heuristics
249 * via the pp_power_profile_mode sysfs file.
256 * When the profiling modes are selected, clock and power gating are
257 * disabled and the clocks are set for different profiling cases. This
258 * mode is recommended for profiling specific work loads where you do
259 * not want clock or power gating for clock fluctuation to interfere
260 * with your results. profile_standard sets the clocks to a fixed clock
261 * level which varies from asic to asic. profile_min_sclk forces the sclk
262 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
263 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
267 static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
268 struct device_attribute *attr,
271 struct drm_device *ddev = dev_get_drvdata(dev);
272 struct amdgpu_device *adev = ddev->dev_private;
273 enum amd_dpm_forced_level level = 0xff;
275 if ((adev->flags & AMD_IS_PX) &&
276 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
277 return snprintf(buf, PAGE_SIZE, "off\n");
279 if (is_support_sw_smu(adev))
280 level = smu_get_performance_level(&adev->smu);
281 else if (adev->powerplay.pp_funcs->get_performance_level)
282 level = amdgpu_dpm_get_performance_level(adev);
284 level = adev->pm.dpm.forced_level;
286 return snprintf(buf, PAGE_SIZE, "%s\n",
287 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
288 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
289 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
290 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
291 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
292 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
293 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
294 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
298 static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
299 struct device_attribute *attr,
303 struct drm_device *ddev = dev_get_drvdata(dev);
304 struct amdgpu_device *adev = ddev->dev_private;
305 enum amd_dpm_forced_level level;
306 enum amd_dpm_forced_level current_level = 0xff;
309 /* Can't force performance level when the card is off */
310 if ((adev->flags & AMD_IS_PX) &&
311 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
314 if (is_support_sw_smu(adev))
315 current_level = smu_get_performance_level(&adev->smu);
316 else if (adev->powerplay.pp_funcs->get_performance_level)
317 current_level = amdgpu_dpm_get_performance_level(adev);
319 if (strncmp("low", buf, strlen("low")) == 0) {
320 level = AMD_DPM_FORCED_LEVEL_LOW;
321 } else if (strncmp("high", buf, strlen("high")) == 0) {
322 level = AMD_DPM_FORCED_LEVEL_HIGH;
323 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
324 level = AMD_DPM_FORCED_LEVEL_AUTO;
325 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
326 level = AMD_DPM_FORCED_LEVEL_MANUAL;
327 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
328 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
329 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
330 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
331 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
332 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
333 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
334 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
335 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
336 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
342 if (amdgpu_sriov_vf(adev)) {
343 if (amdgim_is_hwperf(adev) &&
344 adev->virt.ops->force_dpm_level) {
345 mutex_lock(&adev->pm.mutex);
346 adev->virt.ops->force_dpm_level(adev, level);
347 mutex_unlock(&adev->pm.mutex);
354 if (current_level == level)
357 /* profile_exit setting is valid only when current mode is in profile mode */
358 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
359 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
360 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
361 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
362 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
363 pr_err("Currently not in any profile mode!\n");
367 if (is_support_sw_smu(adev)) {
368 mutex_lock(&adev->pm.mutex);
369 if (adev->pm.dpm.thermal_active) {
371 mutex_unlock(&adev->pm.mutex);
374 ret = smu_force_performance_level(&adev->smu, level);
378 adev->pm.dpm.forced_level = level;
379 mutex_unlock(&adev->pm.mutex);
380 } else if (adev->powerplay.pp_funcs->force_performance_level) {
381 mutex_lock(&adev->pm.mutex);
382 if (adev->pm.dpm.thermal_active) {
384 mutex_unlock(&adev->pm.mutex);
387 ret = amdgpu_dpm_force_performance_level(adev, level);
391 adev->pm.dpm.forced_level = level;
392 mutex_unlock(&adev->pm.mutex);
399 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
400 struct device_attribute *attr,
403 struct drm_device *ddev = dev_get_drvdata(dev);
404 struct amdgpu_device *adev = ddev->dev_private;
405 struct pp_states_info data;
408 if (is_support_sw_smu(adev)) {
409 ret = smu_get_power_num_states(&adev->smu, &data);
412 } else if (adev->powerplay.pp_funcs->get_pp_num_states)
413 amdgpu_dpm_get_pp_num_states(adev, &data);
415 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
416 for (i = 0; i < data.nums; i++)
417 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
418 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
419 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
420 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
421 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
426 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
427 struct device_attribute *attr,
430 struct drm_device *ddev = dev_get_drvdata(dev);
431 struct amdgpu_device *adev = ddev->dev_private;
432 struct pp_states_info data;
433 struct smu_context *smu = &adev->smu;
434 enum amd_pm_state_type pm = 0;
437 if (is_support_sw_smu(adev)) {
438 pm = smu_get_current_power_state(smu);
439 ret = smu_get_power_num_states(smu, &data);
442 } else if (adev->powerplay.pp_funcs->get_current_power_state
443 && adev->powerplay.pp_funcs->get_pp_num_states) {
444 pm = amdgpu_dpm_get_current_power_state(adev);
445 amdgpu_dpm_get_pp_num_states(adev, &data);
448 for (i = 0; i < data.nums; i++) {
449 if (pm == data.states[i])
456 return snprintf(buf, PAGE_SIZE, "%d\n", i);
459 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
460 struct device_attribute *attr,
463 struct drm_device *ddev = dev_get_drvdata(dev);
464 struct amdgpu_device *adev = ddev->dev_private;
466 if (adev->pp_force_state_enabled)
467 return amdgpu_get_pp_cur_state(dev, attr, buf);
469 return snprintf(buf, PAGE_SIZE, "\n");
472 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
473 struct device_attribute *attr,
477 struct drm_device *ddev = dev_get_drvdata(dev);
478 struct amdgpu_device *adev = ddev->dev_private;
479 enum amd_pm_state_type state = 0;
483 if (strlen(buf) == 1)
484 adev->pp_force_state_enabled = false;
485 else if (is_support_sw_smu(adev))
486 adev->pp_force_state_enabled = false;
487 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
488 adev->powerplay.pp_funcs->get_pp_num_states) {
489 struct pp_states_info data;
491 ret = kstrtoul(buf, 0, &idx);
492 if (ret || idx >= ARRAY_SIZE(data.states)) {
496 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
498 amdgpu_dpm_get_pp_num_states(adev, &data);
499 state = data.states[idx];
500 /* only set user selected power states */
501 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
502 state != POWER_STATE_TYPE_DEFAULT) {
503 amdgpu_dpm_dispatch_task(adev,
504 AMD_PP_TASK_ENABLE_USER_STATE, &state);
505 adev->pp_force_state_enabled = true;
515 * The amdgpu driver provides a sysfs API for uploading new powerplay
516 * tables. The file pp_table is used for this. Reading the file
517 * will dump the current power play table. Writing to the file
518 * will attempt to upload a new powerplay table and re-initialize
519 * powerplay using that new table.
523 static ssize_t amdgpu_get_pp_table(struct device *dev,
524 struct device_attribute *attr,
527 struct drm_device *ddev = dev_get_drvdata(dev);
528 struct amdgpu_device *adev = ddev->dev_private;
532 if (is_support_sw_smu(adev)) {
533 size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
537 else if (adev->powerplay.pp_funcs->get_pp_table)
538 size = amdgpu_dpm_get_pp_table(adev, &table);
542 if (size >= PAGE_SIZE)
543 size = PAGE_SIZE - 1;
545 memcpy(buf, table, size);
550 static ssize_t amdgpu_set_pp_table(struct device *dev,
551 struct device_attribute *attr,
555 struct drm_device *ddev = dev_get_drvdata(dev);
556 struct amdgpu_device *adev = ddev->dev_private;
559 if (is_support_sw_smu(adev)) {
560 ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
563 } else if (adev->powerplay.pp_funcs->set_pp_table)
564 amdgpu_dpm_set_pp_table(adev, buf, count);
570 * DOC: pp_od_clk_voltage
572 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
573 * in each power level within a power state. The pp_od_clk_voltage is used for
576 * < For Vega10 and previous ASICs >
578 * Reading the file will display:
580 * - a list of engine clock levels and voltages labeled OD_SCLK
582 * - a list of memory clock levels and voltages labeled OD_MCLK
584 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
586 * To manually adjust these settings, first select manual using
587 * power_dpm_force_performance_level. Enter a new value for each
588 * level by writing a string that contains "s/m level clock voltage" to
589 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
590 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
591 * 810 mV. When you have edited all of the states as needed, write
592 * "c" (commit) to the file to commit your changes. If you want to reset to the
593 * default power levels, write "r" (reset) to the file to reset them.
598 * Reading the file will display:
600 * - minimum and maximum engine clock labeled OD_SCLK
602 * - maximum memory clock labeled OD_MCLK
604 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
605 * They can be used to calibrate the sclk voltage curve.
607 * - a list of valid ranges for sclk, mclk, and voltage curve points
610 * To manually adjust these settings:
612 * - First select manual using power_dpm_force_performance_level
614 * - For clock frequency setting, enter a new value by writing a
615 * string that contains "s/m index clock" to the file. The index
616 * should be 0 if to set minimum clock. And 1 if to set maximum
617 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
618 * "m 1 800" will update maximum mclk to be 800Mhz.
620 * For sclk voltage curve, enter the new values by writing a
621 * string that contains "vc point clock voltage" to the file. The
622 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
623 * update point1 with clock set as 300Mhz and voltage as
624 * 600mV. "vc 2 1000 1000" will update point3 with clock set
625 * as 1000Mhz and voltage 1000mV.
627 * - When you have edited all of the states as needed, write "c" (commit)
628 * to the file to commit your changes
630 * - If you want to reset to the default power levels, write "r" (reset)
631 * to the file to reset them
635 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
636 struct device_attribute *attr,
640 struct drm_device *ddev = dev_get_drvdata(dev);
641 struct amdgpu_device *adev = ddev->dev_private;
643 uint32_t parameter_size = 0;
648 const char delimiter[3] = {' ', '\n', '\0'};
655 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
656 else if (*buf == 'm')
657 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
659 type = PP_OD_RESTORE_DEFAULT_TABLE;
660 else if (*buf == 'c')
661 type = PP_OD_COMMIT_DPM_TABLE;
662 else if (!strncmp(buf, "vc", 2))
663 type = PP_OD_EDIT_VDDC_CURVE;
667 memcpy(buf_cpy, buf, count+1);
671 if (type == PP_OD_EDIT_VDDC_CURVE)
673 while (isspace(*++tmp_str));
676 sub_str = strsep(&tmp_str, delimiter);
677 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
682 while (isspace(*tmp_str))
686 if (is_support_sw_smu(adev)) {
687 ret = smu_od_edit_dpm_table(&adev->smu, type,
688 parameter, parameter_size);
693 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
694 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
695 parameter, parameter_size);
700 if (type == PP_OD_COMMIT_DPM_TABLE) {
701 if (adev->powerplay.pp_funcs->dispatch_tasks) {
702 amdgpu_dpm_dispatch_task(adev,
703 AMD_PP_TASK_READJUST_POWER_STATE,
715 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
716 struct device_attribute *attr,
719 struct drm_device *ddev = dev_get_drvdata(dev);
720 struct amdgpu_device *adev = ddev->dev_private;
723 if (is_support_sw_smu(adev)) {
724 size = smu_print_clk_levels(&adev->smu, OD_SCLK, buf);
725 size += smu_print_clk_levels(&adev->smu, OD_MCLK, buf+size);
726 size += smu_print_clk_levels(&adev->smu, OD_VDDC_CURVE, buf+size);
727 size += smu_print_clk_levels(&adev->smu, OD_RANGE, buf+size);
729 } else if (adev->powerplay.pp_funcs->print_clock_levels) {
730 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
731 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
732 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
733 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
736 return snprintf(buf, PAGE_SIZE, "\n");
744 * The amdgpu driver provides a sysfs API for adjusting what powerplay
745 * features to be enabled. The file ppfeatures is used for this. And
746 * this is only available for Vega10 and later dGPUs.
748 * Reading back the file will show you the followings:
749 * - Current ppfeature masks
750 * - List of the all supported powerplay features with their naming,
751 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
753 * To manually enable or disable a specific feature, just set or clear
754 * the corresponding bit from original ppfeature masks and input the
755 * new ppfeature masks.
757 static ssize_t amdgpu_set_ppfeature_status(struct device *dev,
758 struct device_attribute *attr,
762 struct drm_device *ddev = dev_get_drvdata(dev);
763 struct amdgpu_device *adev = ddev->dev_private;
764 uint64_t featuremask;
767 ret = kstrtou64(buf, 0, &featuremask);
771 pr_debug("featuremask = 0x%llx\n", featuremask);
773 if (is_support_sw_smu(adev)) {
774 ret = smu_set_ppfeature_status(&adev->smu, featuremask);
777 } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
778 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
786 static ssize_t amdgpu_get_ppfeature_status(struct device *dev,
787 struct device_attribute *attr,
790 struct drm_device *ddev = dev_get_drvdata(dev);
791 struct amdgpu_device *adev = ddev->dev_private;
793 if (is_support_sw_smu(adev)) {
794 return smu_get_ppfeature_status(&adev->smu, buf);
795 } else if (adev->powerplay.pp_funcs->get_ppfeature_status)
796 return amdgpu_dpm_get_ppfeature_status(adev, buf);
798 return snprintf(buf, PAGE_SIZE, "\n");
802 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk
805 * The amdgpu driver provides a sysfs API for adjusting what power levels
806 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
807 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
810 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
811 * Vega10 and later ASICs.
812 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
814 * Reading back the files will show you the available power levels within
815 * the power state and the clock information for those levels.
817 * To manually adjust these states, first select manual using
818 * power_dpm_force_performance_level.
819 * Secondly,Enter a new value for each level by inputing a string that
820 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
821 * E.g., echo 4 5 6 to > pp_dpm_sclk will enable sclk levels 4, 5, and 6.
823 * NOTE: change to the dcefclk max dpm level is not supported now
826 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
827 struct device_attribute *attr,
830 struct drm_device *ddev = dev_get_drvdata(dev);
831 struct amdgpu_device *adev = ddev->dev_private;
833 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev) &&
834 adev->virt.ops->get_pp_clk)
835 return adev->virt.ops->get_pp_clk(adev, PP_SCLK, buf);
837 if (is_support_sw_smu(adev))
838 return smu_print_clk_levels(&adev->smu, PP_SCLK, buf);
839 else if (adev->powerplay.pp_funcs->print_clock_levels)
840 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
842 return snprintf(buf, PAGE_SIZE, "\n");
846 * Worst case: 32 bits individually specified, in octal at 12 characters
847 * per line (+1 for \n).
849 #define AMDGPU_MASK_BUF_MAX (32 * 13)
851 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
855 char *sub_str = NULL;
857 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
858 const char delimiter[3] = {' ', '\n', '\0'};
863 bytes = min(count, sizeof(buf_cpy) - 1);
864 memcpy(buf_cpy, buf, bytes);
865 buf_cpy[bytes] = '\0';
868 sub_str = strsep(&tmp, delimiter);
869 if (strlen(sub_str)) {
870 ret = kstrtol(sub_str, 0, &level);
881 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
882 struct device_attribute *attr,
886 struct drm_device *ddev = dev_get_drvdata(dev);
887 struct amdgpu_device *adev = ddev->dev_private;
891 ret = amdgpu_read_mask(buf, count, &mask);
895 if (is_support_sw_smu(adev))
896 ret = smu_force_clk_levels(&adev->smu, PP_SCLK, mask);
897 else if (adev->powerplay.pp_funcs->force_clock_level)
898 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
906 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
907 struct device_attribute *attr,
910 struct drm_device *ddev = dev_get_drvdata(dev);
911 struct amdgpu_device *adev = ddev->dev_private;
913 if (is_support_sw_smu(adev))
914 return smu_print_clk_levels(&adev->smu, PP_MCLK, buf);
915 else if (adev->powerplay.pp_funcs->print_clock_levels)
916 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
918 return snprintf(buf, PAGE_SIZE, "\n");
921 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
922 struct device_attribute *attr,
926 struct drm_device *ddev = dev_get_drvdata(dev);
927 struct amdgpu_device *adev = ddev->dev_private;
931 ret = amdgpu_read_mask(buf, count, &mask);
935 if (is_support_sw_smu(adev))
936 ret = smu_force_clk_levels(&adev->smu, PP_MCLK, mask);
937 else if (adev->powerplay.pp_funcs->force_clock_level)
938 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
946 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
947 struct device_attribute *attr,
950 struct drm_device *ddev = dev_get_drvdata(dev);
951 struct amdgpu_device *adev = ddev->dev_private;
953 if (is_support_sw_smu(adev))
954 return smu_print_clk_levels(&adev->smu, PP_SOCCLK, buf);
955 else if (adev->powerplay.pp_funcs->print_clock_levels)
956 return amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
958 return snprintf(buf, PAGE_SIZE, "\n");
961 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
962 struct device_attribute *attr,
966 struct drm_device *ddev = dev_get_drvdata(dev);
967 struct amdgpu_device *adev = ddev->dev_private;
971 ret = amdgpu_read_mask(buf, count, &mask);
975 if (is_support_sw_smu(adev))
976 ret = smu_force_clk_levels(&adev->smu, PP_SOCCLK, mask);
977 else if (adev->powerplay.pp_funcs->force_clock_level)
978 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
986 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
987 struct device_attribute *attr,
990 struct drm_device *ddev = dev_get_drvdata(dev);
991 struct amdgpu_device *adev = ddev->dev_private;
993 if (is_support_sw_smu(adev))
994 return smu_print_clk_levels(&adev->smu, PP_FCLK, buf);
995 else if (adev->powerplay.pp_funcs->print_clock_levels)
996 return amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
998 return snprintf(buf, PAGE_SIZE, "\n");
1001 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1002 struct device_attribute *attr,
1006 struct drm_device *ddev = dev_get_drvdata(dev);
1007 struct amdgpu_device *adev = ddev->dev_private;
1011 ret = amdgpu_read_mask(buf, count, &mask);
1015 if (is_support_sw_smu(adev))
1016 ret = smu_force_clk_levels(&adev->smu, PP_FCLK, mask);
1017 else if (adev->powerplay.pp_funcs->force_clock_level)
1018 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1026 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1027 struct device_attribute *attr,
1030 struct drm_device *ddev = dev_get_drvdata(dev);
1031 struct amdgpu_device *adev = ddev->dev_private;
1033 if (is_support_sw_smu(adev))
1034 return smu_print_clk_levels(&adev->smu, PP_DCEFCLK, buf);
1035 else if (adev->powerplay.pp_funcs->print_clock_levels)
1036 return amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1038 return snprintf(buf, PAGE_SIZE, "\n");
1041 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1042 struct device_attribute *attr,
1046 struct drm_device *ddev = dev_get_drvdata(dev);
1047 struct amdgpu_device *adev = ddev->dev_private;
1051 ret = amdgpu_read_mask(buf, count, &mask);
1055 if (is_support_sw_smu(adev))
1056 ret = smu_force_clk_levels(&adev->smu, PP_DCEFCLK, mask);
1057 else if (adev->powerplay.pp_funcs->force_clock_level)
1058 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1066 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1067 struct device_attribute *attr,
1070 struct drm_device *ddev = dev_get_drvdata(dev);
1071 struct amdgpu_device *adev = ddev->dev_private;
1073 if (is_support_sw_smu(adev))
1074 return smu_print_clk_levels(&adev->smu, PP_PCIE, buf);
1075 else if (adev->powerplay.pp_funcs->print_clock_levels)
1076 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1078 return snprintf(buf, PAGE_SIZE, "\n");
1081 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1082 struct device_attribute *attr,
1086 struct drm_device *ddev = dev_get_drvdata(dev);
1087 struct amdgpu_device *adev = ddev->dev_private;
1091 ret = amdgpu_read_mask(buf, count, &mask);
1095 if (is_support_sw_smu(adev))
1096 ret = smu_force_clk_levels(&adev->smu, PP_PCIE, mask);
1097 else if (adev->powerplay.pp_funcs->force_clock_level)
1098 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1106 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1107 struct device_attribute *attr,
1110 struct drm_device *ddev = dev_get_drvdata(dev);
1111 struct amdgpu_device *adev = ddev->dev_private;
1114 if (is_support_sw_smu(adev))
1115 value = smu_get_od_percentage(&(adev->smu), OD_SCLK);
1116 else if (adev->powerplay.pp_funcs->get_sclk_od)
1117 value = amdgpu_dpm_get_sclk_od(adev);
1119 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1122 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1123 struct device_attribute *attr,
1127 struct drm_device *ddev = dev_get_drvdata(dev);
1128 struct amdgpu_device *adev = ddev->dev_private;
1132 ret = kstrtol(buf, 0, &value);
1139 if (is_support_sw_smu(adev)) {
1140 value = smu_set_od_percentage(&(adev->smu), OD_SCLK, (uint32_t)value);
1142 if (adev->powerplay.pp_funcs->set_sclk_od)
1143 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1145 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1146 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1148 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1149 amdgpu_pm_compute_clocks(adev);
1157 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1158 struct device_attribute *attr,
1161 struct drm_device *ddev = dev_get_drvdata(dev);
1162 struct amdgpu_device *adev = ddev->dev_private;
1165 if (is_support_sw_smu(adev))
1166 value = smu_get_od_percentage(&(adev->smu), OD_MCLK);
1167 else if (adev->powerplay.pp_funcs->get_mclk_od)
1168 value = amdgpu_dpm_get_mclk_od(adev);
1170 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1173 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1174 struct device_attribute *attr,
1178 struct drm_device *ddev = dev_get_drvdata(dev);
1179 struct amdgpu_device *adev = ddev->dev_private;
1183 ret = kstrtol(buf, 0, &value);
1190 if (is_support_sw_smu(adev)) {
1191 value = smu_set_od_percentage(&(adev->smu), OD_MCLK, (uint32_t)value);
1193 if (adev->powerplay.pp_funcs->set_mclk_od)
1194 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1196 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1197 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1199 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1200 amdgpu_pm_compute_clocks(adev);
1209 * DOC: pp_power_profile_mode
1211 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1212 * related to switching between power levels in a power state. The file
1213 * pp_power_profile_mode is used for this.
1215 * Reading this file outputs a list of all of the predefined power profiles
1216 * and the relevant heuristics settings for that profile.
1218 * To select a profile or create a custom profile, first select manual using
1219 * power_dpm_force_performance_level. Writing the number of a predefined
1220 * profile to pp_power_profile_mode will enable those heuristics. To
1221 * create a custom set of heuristics, write a string of numbers to the file
1222 * starting with the number of the custom profile along with a setting
1223 * for each heuristic parameter. Due to differences across asic families
1224 * the heuristic parameters vary from family to family.
1228 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1229 struct device_attribute *attr,
1232 struct drm_device *ddev = dev_get_drvdata(dev);
1233 struct amdgpu_device *adev = ddev->dev_private;
1235 if (is_support_sw_smu(adev))
1236 return smu_get_power_profile_mode(&adev->smu, buf);
1237 else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1238 return amdgpu_dpm_get_power_profile_mode(adev, buf);
1240 return snprintf(buf, PAGE_SIZE, "\n");
1244 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1245 struct device_attribute *attr,
1250 struct drm_device *ddev = dev_get_drvdata(dev);
1251 struct amdgpu_device *adev = ddev->dev_private;
1252 uint32_t parameter_size = 0;
1254 char *sub_str, buf_cpy[128];
1258 long int profile_mode = 0;
1259 const char delimiter[3] = {' ', '\n', '\0'};
1263 ret = kstrtol(tmp, 0, &profile_mode);
1267 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1268 if (count < 2 || count > 127)
1270 while (isspace(*++buf))
1272 memcpy(buf_cpy, buf, count-i);
1274 while (tmp_str[0]) {
1275 sub_str = strsep(&tmp_str, delimiter);
1276 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1282 while (isspace(*tmp_str))
1286 parameter[parameter_size] = profile_mode;
1287 if (is_support_sw_smu(adev))
1288 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size);
1289 else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1290 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1300 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1301 * is as a percentage. The file gpu_busy_percent is used for this.
1302 * The SMU firmware computes a percentage of load based on the
1303 * aggregate activity level in the IP cores.
1305 static ssize_t amdgpu_get_busy_percent(struct device *dev,
1306 struct device_attribute *attr,
1309 struct drm_device *ddev = dev_get_drvdata(dev);
1310 struct amdgpu_device *adev = ddev->dev_private;
1311 int r, value, size = sizeof(value);
1313 /* read the IP busy sensor */
1314 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1315 (void *)&value, &size);
1320 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1324 * DOC: mem_busy_percent
1326 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1327 * is as a percentage. The file mem_busy_percent is used for this.
1328 * The SMU firmware computes a percentage of load based on the
1329 * aggregate activity level in the IP cores.
1331 static ssize_t amdgpu_get_memory_busy_percent(struct device *dev,
1332 struct device_attribute *attr,
1335 struct drm_device *ddev = dev_get_drvdata(dev);
1336 struct amdgpu_device *adev = ddev->dev_private;
1337 int r, value, size = sizeof(value);
1339 /* read the IP busy sensor */
1340 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1341 (void *)&value, &size);
1346 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1352 * The amdgpu driver provides a sysfs API for estimating how much data
1353 * has been received and sent by the GPU in the last second through PCIe.
1354 * The file pcie_bw is used for this.
1355 * The Perf counters count the number of received and sent messages and return
1356 * those values, as well as the maximum payload size of a PCIe packet (mps).
1357 * Note that it is not possible to easily and quickly obtain the size of each
1358 * packet transmitted, so we output the max payload size (mps) to allow for
1359 * quick estimation of the PCIe bandwidth usage
1361 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1362 struct device_attribute *attr,
1365 struct drm_device *ddev = dev_get_drvdata(dev);
1366 struct amdgpu_device *adev = ddev->dev_private;
1367 uint64_t count0, count1;
1369 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1370 return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1371 count0, count1, pcie_get_mps(adev->pdev));
1377 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1378 * The file unique_id is used for this.
1379 * This will provide a Unique ID that will persist from machine to machine
1381 * NOTE: This will only work for GFX9 and newer. This file will be absent
1382 * on unsupported ASICs (GFX8 and older)
1384 static ssize_t amdgpu_get_unique_id(struct device *dev,
1385 struct device_attribute *attr,
1388 struct drm_device *ddev = dev_get_drvdata(dev);
1389 struct amdgpu_device *adev = ddev->dev_private;
1391 if (adev->unique_id)
1392 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1397 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
1398 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
1399 amdgpu_get_dpm_forced_performance_level,
1400 amdgpu_set_dpm_forced_performance_level);
1401 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
1402 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
1403 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
1404 amdgpu_get_pp_force_state,
1405 amdgpu_set_pp_force_state);
1406 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
1407 amdgpu_get_pp_table,
1408 amdgpu_set_pp_table);
1409 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
1410 amdgpu_get_pp_dpm_sclk,
1411 amdgpu_set_pp_dpm_sclk);
1412 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
1413 amdgpu_get_pp_dpm_mclk,
1414 amdgpu_set_pp_dpm_mclk);
1415 static DEVICE_ATTR(pp_dpm_socclk, S_IRUGO | S_IWUSR,
1416 amdgpu_get_pp_dpm_socclk,
1417 amdgpu_set_pp_dpm_socclk);
1418 static DEVICE_ATTR(pp_dpm_fclk, S_IRUGO | S_IWUSR,
1419 amdgpu_get_pp_dpm_fclk,
1420 amdgpu_set_pp_dpm_fclk);
1421 static DEVICE_ATTR(pp_dpm_dcefclk, S_IRUGO | S_IWUSR,
1422 amdgpu_get_pp_dpm_dcefclk,
1423 amdgpu_set_pp_dpm_dcefclk);
1424 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
1425 amdgpu_get_pp_dpm_pcie,
1426 amdgpu_set_pp_dpm_pcie);
1427 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
1428 amdgpu_get_pp_sclk_od,
1429 amdgpu_set_pp_sclk_od);
1430 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
1431 amdgpu_get_pp_mclk_od,
1432 amdgpu_set_pp_mclk_od);
1433 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
1434 amdgpu_get_pp_power_profile_mode,
1435 amdgpu_set_pp_power_profile_mode);
1436 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
1437 amdgpu_get_pp_od_clk_voltage,
1438 amdgpu_set_pp_od_clk_voltage);
1439 static DEVICE_ATTR(gpu_busy_percent, S_IRUGO,
1440 amdgpu_get_busy_percent, NULL);
1441 static DEVICE_ATTR(mem_busy_percent, S_IRUGO,
1442 amdgpu_get_memory_busy_percent, NULL);
1443 static DEVICE_ATTR(pcie_bw, S_IRUGO, amdgpu_get_pcie_bw, NULL);
1444 static DEVICE_ATTR(ppfeatures, S_IRUGO | S_IWUSR,
1445 amdgpu_get_ppfeature_status,
1446 amdgpu_set_ppfeature_status);
1447 static DEVICE_ATTR(unique_id, S_IRUGO, amdgpu_get_unique_id, NULL);
1449 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1450 struct device_attribute *attr,
1453 struct amdgpu_device *adev = dev_get_drvdata(dev);
1454 struct drm_device *ddev = adev->ddev;
1455 int channel = to_sensor_dev_attr(attr)->index;
1456 int r, temp, size = sizeof(temp);
1458 /* Can't get temperature when the card is off */
1459 if ((adev->flags & AMD_IS_PX) &&
1460 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1463 if (channel >= PP_TEMP_MAX)
1467 case PP_TEMP_JUNCTION:
1468 /* get current junction temperature */
1469 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1470 (void *)&temp, &size);
1475 /* get current edge temperature */
1476 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
1477 (void *)&temp, &size);
1482 /* get current memory temperature */
1483 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
1484 (void *)&temp, &size);
1490 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1493 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1494 struct device_attribute *attr,
1497 struct amdgpu_device *adev = dev_get_drvdata(dev);
1498 int hyst = to_sensor_dev_attr(attr)->index;
1502 temp = adev->pm.dpm.thermal.min_temp;
1504 temp = adev->pm.dpm.thermal.max_temp;
1506 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1509 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
1510 struct device_attribute *attr,
1513 struct amdgpu_device *adev = dev_get_drvdata(dev);
1514 int hyst = to_sensor_dev_attr(attr)->index;
1518 temp = adev->pm.dpm.thermal.min_hotspot_temp;
1520 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
1522 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1525 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
1526 struct device_attribute *attr,
1529 struct amdgpu_device *adev = dev_get_drvdata(dev);
1530 int hyst = to_sensor_dev_attr(attr)->index;
1534 temp = adev->pm.dpm.thermal.min_mem_temp;
1536 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
1538 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1541 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
1542 struct device_attribute *attr,
1545 int channel = to_sensor_dev_attr(attr)->index;
1547 if (channel >= PP_TEMP_MAX)
1550 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
1553 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
1554 struct device_attribute *attr,
1557 struct amdgpu_device *adev = dev_get_drvdata(dev);
1558 int channel = to_sensor_dev_attr(attr)->index;
1561 if (channel >= PP_TEMP_MAX)
1565 case PP_TEMP_JUNCTION:
1566 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
1569 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
1572 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
1576 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1579 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1580 struct device_attribute *attr,
1583 struct amdgpu_device *adev = dev_get_drvdata(dev);
1585 if (is_support_sw_smu(adev)) {
1586 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1588 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1591 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1594 return sprintf(buf, "%i\n", pwm_mode);
1597 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1598 struct device_attribute *attr,
1602 struct amdgpu_device *adev = dev_get_drvdata(dev);
1606 /* Can't adjust fan when the card is off */
1607 if ((adev->flags & AMD_IS_PX) &&
1608 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1611 if (is_support_sw_smu(adev)) {
1612 err = kstrtoint(buf, 10, &value);
1616 smu_set_fan_control_mode(&adev->smu, value);
1618 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1621 err = kstrtoint(buf, 10, &value);
1625 amdgpu_dpm_set_fan_control_mode(adev, value);
1631 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1632 struct device_attribute *attr,
1635 return sprintf(buf, "%i\n", 0);
1638 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1639 struct device_attribute *attr,
1642 return sprintf(buf, "%i\n", 255);
1645 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1649 struct amdgpu_device *adev = dev_get_drvdata(dev);
1654 /* Can't adjust fan when the card is off */
1655 if ((adev->flags & AMD_IS_PX) &&
1656 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1658 if (is_support_sw_smu(adev))
1659 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1661 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1662 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
1663 pr_info("manual fan speed control should be enabled first\n");
1667 err = kstrtou32(buf, 10, &value);
1671 value = (value * 100) / 255;
1673 if (is_support_sw_smu(adev)) {
1674 err = smu_set_fan_speed_percent(&adev->smu, value);
1677 } else if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1678 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1686 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1687 struct device_attribute *attr,
1690 struct amdgpu_device *adev = dev_get_drvdata(dev);
1694 /* Can't adjust fan when the card is off */
1695 if ((adev->flags & AMD_IS_PX) &&
1696 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1699 if (is_support_sw_smu(adev)) {
1700 err = smu_get_fan_speed_percent(&adev->smu, &speed);
1703 } else if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1704 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1709 speed = (speed * 255) / 100;
1711 return sprintf(buf, "%i\n", speed);
1714 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1715 struct device_attribute *attr,
1718 struct amdgpu_device *adev = dev_get_drvdata(dev);
1722 /* Can't adjust fan when the card is off */
1723 if ((adev->flags & AMD_IS_PX) &&
1724 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1727 if (is_support_sw_smu(adev)) {
1728 err = smu_get_current_rpm(&adev->smu, &speed);
1731 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1732 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1737 return sprintf(buf, "%i\n", speed);
1740 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
1741 struct device_attribute *attr,
1744 struct amdgpu_device *adev = dev_get_drvdata(dev);
1746 u32 size = sizeof(min_rpm);
1749 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
1750 (void *)&min_rpm, &size);
1754 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
1757 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
1758 struct device_attribute *attr,
1761 struct amdgpu_device *adev = dev_get_drvdata(dev);
1763 u32 size = sizeof(max_rpm);
1766 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
1767 (void *)&max_rpm, &size);
1771 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
1774 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
1775 struct device_attribute *attr,
1778 struct amdgpu_device *adev = dev_get_drvdata(dev);
1782 /* Can't adjust fan when the card is off */
1783 if ((adev->flags & AMD_IS_PX) &&
1784 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1787 if (is_support_sw_smu(adev)) {
1788 err = smu_get_current_rpm(&adev->smu, &rpm);
1791 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1792 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
1797 return sprintf(buf, "%i\n", rpm);
1800 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
1801 struct device_attribute *attr,
1802 const char *buf, size_t count)
1804 struct amdgpu_device *adev = dev_get_drvdata(dev);
1809 if (is_support_sw_smu(adev))
1810 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1812 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1814 if (pwm_mode != AMD_FAN_CTRL_MANUAL)
1817 /* Can't adjust fan when the card is off */
1818 if ((adev->flags & AMD_IS_PX) &&
1819 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1822 err = kstrtou32(buf, 10, &value);
1826 if (is_support_sw_smu(adev)) {
1827 err = smu_set_fan_speed_rpm(&adev->smu, value);
1830 } else if (adev->powerplay.pp_funcs->set_fan_speed_rpm) {
1831 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
1839 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
1840 struct device_attribute *attr,
1843 struct amdgpu_device *adev = dev_get_drvdata(dev);
1846 if (is_support_sw_smu(adev)) {
1847 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1849 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1852 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1854 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
1857 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
1858 struct device_attribute *attr,
1862 struct amdgpu_device *adev = dev_get_drvdata(dev);
1867 /* Can't adjust fan when the card is off */
1868 if ((adev->flags & AMD_IS_PX) &&
1869 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1873 err = kstrtoint(buf, 10, &value);
1878 pwm_mode = AMD_FAN_CTRL_AUTO;
1879 else if (value == 1)
1880 pwm_mode = AMD_FAN_CTRL_MANUAL;
1884 if (is_support_sw_smu(adev)) {
1885 smu_set_fan_control_mode(&adev->smu, pwm_mode);
1887 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1889 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
1895 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1896 struct device_attribute *attr,
1899 struct amdgpu_device *adev = dev_get_drvdata(dev);
1900 struct drm_device *ddev = adev->ddev;
1902 int r, size = sizeof(vddgfx);
1904 /* Can't get voltage when the card is off */
1905 if ((adev->flags & AMD_IS_PX) &&
1906 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1909 /* get the voltage */
1910 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1911 (void *)&vddgfx, &size);
1915 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1918 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1919 struct device_attribute *attr,
1922 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1925 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1926 struct device_attribute *attr,
1929 struct amdgpu_device *adev = dev_get_drvdata(dev);
1930 struct drm_device *ddev = adev->ddev;
1932 int r, size = sizeof(vddnb);
1934 /* only APUs have vddnb */
1935 if (!(adev->flags & AMD_IS_APU))
1938 /* Can't get voltage when the card is off */
1939 if ((adev->flags & AMD_IS_PX) &&
1940 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1943 /* get the voltage */
1944 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1945 (void *)&vddnb, &size);
1949 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1952 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1953 struct device_attribute *attr,
1956 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1959 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1960 struct device_attribute *attr,
1963 struct amdgpu_device *adev = dev_get_drvdata(dev);
1964 struct drm_device *ddev = adev->ddev;
1966 int r, size = sizeof(u32);
1969 /* Can't get power when the card is off */
1970 if ((adev->flags & AMD_IS_PX) &&
1971 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1974 /* get the voltage */
1975 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1976 (void *)&query, &size);
1980 /* convert to microwatts */
1981 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
1983 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1986 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1987 struct device_attribute *attr,
1990 return sprintf(buf, "%i\n", 0);
1993 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1994 struct device_attribute *attr,
1997 struct amdgpu_device *adev = dev_get_drvdata(dev);
2000 if (is_support_sw_smu(adev)) {
2001 smu_get_power_limit(&adev->smu, &limit, true);
2002 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2003 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2004 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2005 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2007 return snprintf(buf, PAGE_SIZE, "\n");
2011 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2012 struct device_attribute *attr,
2015 struct amdgpu_device *adev = dev_get_drvdata(dev);
2018 if (is_support_sw_smu(adev)) {
2019 smu_get_power_limit(&adev->smu, &limit, false);
2020 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2021 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2022 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2023 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2025 return snprintf(buf, PAGE_SIZE, "\n");
2030 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2031 struct device_attribute *attr,
2035 struct amdgpu_device *adev = dev_get_drvdata(dev);
2039 err = kstrtou32(buf, 10, &value);
2043 value = value / 1000000; /* convert to Watt */
2044 if (is_support_sw_smu(adev)) {
2045 adev->smu.funcs->set_power_limit(&adev->smu, value);
2046 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
2047 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2057 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2058 struct device_attribute *attr,
2061 struct amdgpu_device *adev = dev_get_drvdata(dev);
2062 struct drm_device *ddev = adev->ddev;
2064 int r, size = sizeof(sclk);
2066 /* Can't get voltage when the card is off */
2067 if ((adev->flags & AMD_IS_PX) &&
2068 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2071 /* sanity check PP is enabled */
2072 if (!(adev->powerplay.pp_funcs &&
2073 adev->powerplay.pp_funcs->read_sensor))
2077 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2078 (void *)&sclk, &size);
2082 return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2085 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2086 struct device_attribute *attr,
2089 return snprintf(buf, PAGE_SIZE, "sclk\n");
2092 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2093 struct device_attribute *attr,
2096 struct amdgpu_device *adev = dev_get_drvdata(dev);
2097 struct drm_device *ddev = adev->ddev;
2099 int r, size = sizeof(mclk);
2101 /* Can't get voltage when the card is off */
2102 if ((adev->flags & AMD_IS_PX) &&
2103 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2106 /* sanity check PP is enabled */
2107 if (!(adev->powerplay.pp_funcs &&
2108 adev->powerplay.pp_funcs->read_sensor))
2112 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2113 (void *)&mclk, &size);
2117 return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
2120 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2121 struct device_attribute *attr,
2124 return snprintf(buf, PAGE_SIZE, "mclk\n");
2130 * The amdgpu driver exposes the following sensor interfaces:
2132 * - GPU temperature (via the on-die sensor)
2136 * - Northbridge voltage (APUs only)
2142 * - GPU gfx/compute engine clock
2144 * - GPU memory clock (dGPU only)
2146 * hwmon interfaces for GPU temperature:
2148 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
2149 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
2151 * - temp[1-3]_label: temperature channel label
2152 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
2154 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
2155 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
2157 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
2158 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
2160 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
2161 * - these are supported on SOC15 dGPUs only
2163 * hwmon interfaces for GPU voltage:
2165 * - in0_input: the voltage on the GPU in millivolts
2167 * - in1_input: the voltage on the Northbridge in millivolts
2169 * hwmon interfaces for GPU power:
2171 * - power1_average: average power used by the GPU in microWatts
2173 * - power1_cap_min: minimum cap supported in microWatts
2175 * - power1_cap_max: maximum cap supported in microWatts
2177 * - power1_cap: selected power cap in microWatts
2179 * hwmon interfaces for GPU fan:
2181 * - pwm1: pulse width modulation fan level (0-255)
2183 * - pwm1_enable: pulse width modulation fan control method (0: no fan speed control, 1: manual fan speed control using pwm interface, 2: automatic fan speed control)
2185 * - pwm1_min: pulse width modulation fan control minimum level (0)
2187 * - pwm1_max: pulse width modulation fan control maximum level (255)
2189 * - fan1_min: an minimum value Unit: revolution/min (RPM)
2191 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
2193 * - fan1_input: fan speed in RPM
2195 * - fan[1-*]_target: Desired fan speed Unit: revolution/min (RPM)
2197 * - fan[1-*]_enable: Enable or disable the sensors.1: Enable 0: Disable
2199 * hwmon interfaces for GPU clocks:
2201 * - freq1_input: the gfx/compute clock in hertz
2203 * - freq2_input: the memory clock in hertz
2205 * You can use hwmon tools like sensors to view this information on your system.
2209 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
2210 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
2211 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
2212 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
2213 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
2214 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
2215 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
2216 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
2217 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
2218 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
2219 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
2220 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2221 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
2222 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
2223 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
2224 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
2225 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
2226 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
2227 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
2228 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
2229 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
2230 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
2231 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
2232 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2233 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
2234 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
2235 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
2236 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
2237 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
2238 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
2239 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
2240 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
2241 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
2242 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
2243 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
2244 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
2246 static struct attribute *hwmon_attributes[] = {
2247 &sensor_dev_attr_temp1_input.dev_attr.attr,
2248 &sensor_dev_attr_temp1_crit.dev_attr.attr,
2249 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2250 &sensor_dev_attr_temp2_input.dev_attr.attr,
2251 &sensor_dev_attr_temp2_crit.dev_attr.attr,
2252 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2253 &sensor_dev_attr_temp3_input.dev_attr.attr,
2254 &sensor_dev_attr_temp3_crit.dev_attr.attr,
2255 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2256 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
2257 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
2258 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2259 &sensor_dev_attr_temp1_label.dev_attr.attr,
2260 &sensor_dev_attr_temp2_label.dev_attr.attr,
2261 &sensor_dev_attr_temp3_label.dev_attr.attr,
2262 &sensor_dev_attr_pwm1.dev_attr.attr,
2263 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2264 &sensor_dev_attr_pwm1_min.dev_attr.attr,
2265 &sensor_dev_attr_pwm1_max.dev_attr.attr,
2266 &sensor_dev_attr_fan1_input.dev_attr.attr,
2267 &sensor_dev_attr_fan1_min.dev_attr.attr,
2268 &sensor_dev_attr_fan1_max.dev_attr.attr,
2269 &sensor_dev_attr_fan1_target.dev_attr.attr,
2270 &sensor_dev_attr_fan1_enable.dev_attr.attr,
2271 &sensor_dev_attr_in0_input.dev_attr.attr,
2272 &sensor_dev_attr_in0_label.dev_attr.attr,
2273 &sensor_dev_attr_in1_input.dev_attr.attr,
2274 &sensor_dev_attr_in1_label.dev_attr.attr,
2275 &sensor_dev_attr_power1_average.dev_attr.attr,
2276 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
2277 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
2278 &sensor_dev_attr_power1_cap.dev_attr.attr,
2279 &sensor_dev_attr_freq1_input.dev_attr.attr,
2280 &sensor_dev_attr_freq1_label.dev_attr.attr,
2281 &sensor_dev_attr_freq2_input.dev_attr.attr,
2282 &sensor_dev_attr_freq2_label.dev_attr.attr,
2286 static umode_t hwmon_attributes_visible(struct kobject *kobj,
2287 struct attribute *attr, int index)
2289 struct device *dev = kobj_to_dev(kobj);
2290 struct amdgpu_device *adev = dev_get_drvdata(dev);
2291 umode_t effective_mode = attr->mode;
2293 /* Skip fan attributes if fan is not present */
2294 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2295 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2296 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2297 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2298 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2299 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2300 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2301 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2302 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2305 /* Skip fan attributes on APU */
2306 if ((adev->flags & AMD_IS_APU) &&
2307 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2308 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2309 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2310 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2311 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2312 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2313 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2314 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2315 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2318 /* Skip limit attributes if DPM is not enabled */
2319 if (!adev->pm.dpm_enabled &&
2320 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
2321 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
2322 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2323 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2324 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2325 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2326 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2327 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2328 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2329 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2330 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2333 if (!is_support_sw_smu(adev)) {
2334 /* mask fan attributes if we have no bindings for this asic to expose */
2335 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
2336 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
2337 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
2338 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
2339 effective_mode &= ~S_IRUGO;
2341 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2342 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
2343 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
2344 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
2345 effective_mode &= ~S_IWUSR;
2348 if ((adev->flags & AMD_IS_APU) &&
2349 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
2350 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
2351 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
2352 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
2355 if (!is_support_sw_smu(adev)) {
2356 /* hide max/min values if we can't both query and manage the fan */
2357 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2358 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
2359 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2360 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2361 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2362 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
2365 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2366 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2367 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2368 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
2372 /* only APUs have vddnb */
2373 if (!(adev->flags & AMD_IS_APU) &&
2374 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
2375 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
2378 /* no mclk on APUs */
2379 if ((adev->flags & AMD_IS_APU) &&
2380 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
2381 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
2384 /* only SOC15 dGPUs support hotspot and mem temperatures */
2385 if (((adev->flags & AMD_IS_APU) ||
2386 adev->asic_type < CHIP_VEGA10) &&
2387 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
2388 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
2389 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
2390 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
2391 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
2392 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
2393 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
2394 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
2395 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
2396 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
2397 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
2400 return effective_mode;
2403 static const struct attribute_group hwmon_attrgroup = {
2404 .attrs = hwmon_attributes,
2405 .is_visible = hwmon_attributes_visible,
2408 static const struct attribute_group *hwmon_groups[] = {
2413 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
2415 struct amdgpu_device *adev =
2416 container_of(work, struct amdgpu_device,
2417 pm.dpm.thermal.work);
2418 /* switch to the thermal state */
2419 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
2420 int temp, size = sizeof(temp);
2422 if (!adev->pm.dpm_enabled)
2425 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
2426 (void *)&temp, &size)) {
2427 if (temp < adev->pm.dpm.thermal.min_temp)
2428 /* switch back the user state */
2429 dpm_state = adev->pm.dpm.user_state;
2431 if (adev->pm.dpm.thermal.high_to_low)
2432 /* switch back the user state */
2433 dpm_state = adev->pm.dpm.user_state;
2435 mutex_lock(&adev->pm.mutex);
2436 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
2437 adev->pm.dpm.thermal_active = true;
2439 adev->pm.dpm.thermal_active = false;
2440 adev->pm.dpm.state = dpm_state;
2441 mutex_unlock(&adev->pm.mutex);
2443 amdgpu_pm_compute_clocks(adev);
2446 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
2447 enum amd_pm_state_type dpm_state)
2450 struct amdgpu_ps *ps;
2452 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
2455 /* check if the vblank period is too short to adjust the mclk */
2456 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
2457 if (amdgpu_dpm_vblank_too_short(adev))
2458 single_display = false;
2461 /* certain older asics have a separare 3D performance state,
2462 * so try that first if the user selected performance
2464 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
2465 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
2466 /* balanced states don't exist at the moment */
2467 if (dpm_state == POWER_STATE_TYPE_BALANCED)
2468 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2471 /* Pick the best power state based on current conditions */
2472 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
2473 ps = &adev->pm.dpm.ps[i];
2474 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
2475 switch (dpm_state) {
2477 case POWER_STATE_TYPE_BATTERY:
2478 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
2479 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2486 case POWER_STATE_TYPE_BALANCED:
2487 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
2488 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2495 case POWER_STATE_TYPE_PERFORMANCE:
2496 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
2497 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2504 /* internal states */
2505 case POWER_STATE_TYPE_INTERNAL_UVD:
2506 if (adev->pm.dpm.uvd_ps)
2507 return adev->pm.dpm.uvd_ps;
2510 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2511 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
2514 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2515 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
2518 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2519 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
2522 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2523 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
2526 case POWER_STATE_TYPE_INTERNAL_BOOT:
2527 return adev->pm.dpm.boot_ps;
2528 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2529 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
2532 case POWER_STATE_TYPE_INTERNAL_ACPI:
2533 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
2536 case POWER_STATE_TYPE_INTERNAL_ULV:
2537 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
2540 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2541 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2548 /* use a fallback state if we didn't match */
2549 switch (dpm_state) {
2550 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2551 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
2552 goto restart_search;
2553 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2554 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2555 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2556 if (adev->pm.dpm.uvd_ps) {
2557 return adev->pm.dpm.uvd_ps;
2559 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2560 goto restart_search;
2562 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2563 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
2564 goto restart_search;
2565 case POWER_STATE_TYPE_INTERNAL_ACPI:
2566 dpm_state = POWER_STATE_TYPE_BATTERY;
2567 goto restart_search;
2568 case POWER_STATE_TYPE_BATTERY:
2569 case POWER_STATE_TYPE_BALANCED:
2570 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2571 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2572 goto restart_search;
2580 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
2582 struct amdgpu_ps *ps;
2583 enum amd_pm_state_type dpm_state;
2587 /* if dpm init failed */
2588 if (!adev->pm.dpm_enabled)
2591 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
2592 /* add other state override checks here */
2593 if ((!adev->pm.dpm.thermal_active) &&
2594 (!adev->pm.dpm.uvd_active))
2595 adev->pm.dpm.state = adev->pm.dpm.user_state;
2597 dpm_state = adev->pm.dpm.state;
2599 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
2601 adev->pm.dpm.requested_ps = ps;
2605 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
2606 printk("switching from power state:\n");
2607 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
2608 printk("switching to power state:\n");
2609 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
2612 /* update whether vce is active */
2613 ps->vce_active = adev->pm.dpm.vce_active;
2614 if (adev->powerplay.pp_funcs->display_configuration_changed)
2615 amdgpu_dpm_display_configuration_changed(adev);
2617 ret = amdgpu_dpm_pre_set_power_state(adev);
2621 if (adev->powerplay.pp_funcs->check_state_equal) {
2622 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
2629 amdgpu_dpm_set_power_state(adev);
2630 amdgpu_dpm_post_set_power_state(adev);
2632 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
2633 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
2635 if (adev->powerplay.pp_funcs->force_performance_level) {
2636 if (adev->pm.dpm.thermal_active) {
2637 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
2638 /* force low perf level for thermal */
2639 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
2640 /* save the user's level */
2641 adev->pm.dpm.forced_level = level;
2643 /* otherwise, user selected level */
2644 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
2649 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
2652 if (is_support_sw_smu(adev)) {
2653 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_UVD, enable);
2655 DRM_ERROR("[SW SMU]: dpm enable uvd failed, state = %s, ret = %d. \n",
2656 enable ? "true" : "false", ret);
2657 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2658 /* enable/disable UVD */
2659 mutex_lock(&adev->pm.mutex);
2660 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
2661 mutex_unlock(&adev->pm.mutex);
2663 /* enable/disable Low Memory PState for UVD (4k videos) */
2664 if (adev->asic_type == CHIP_STONEY &&
2665 adev->uvd.decode_image_width >= WIDTH_4K) {
2666 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2668 if (hwmgr && hwmgr->hwmgr_func &&
2669 hwmgr->hwmgr_func->update_nbdpm_pstate)
2670 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
2676 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
2679 if (is_support_sw_smu(adev)) {
2680 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_VCE, enable);
2682 DRM_ERROR("[SW SMU]: dpm enable vce failed, state = %s, ret = %d. \n",
2683 enable ? "true" : "false", ret);
2684 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2685 /* enable/disable VCE */
2686 mutex_lock(&adev->pm.mutex);
2687 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
2688 mutex_unlock(&adev->pm.mutex);
2692 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
2696 if (adev->powerplay.pp_funcs->print_power_state == NULL)
2699 for (i = 0; i < adev->pm.dpm.num_ps; i++)
2700 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
2704 int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version)
2708 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
2709 r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
2711 pr_err("smu firmware loading failed\n");
2714 *smu_version = adev->pm.fw_version;
2719 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
2721 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2724 if (adev->pm.sysfs_initialized)
2727 if (adev->pm.dpm_enabled == 0)
2730 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
2733 if (IS_ERR(adev->pm.int_hwmon_dev)) {
2734 ret = PTR_ERR(adev->pm.int_hwmon_dev);
2736 "Unable to register hwmon device: %d\n", ret);
2740 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
2742 DRM_ERROR("failed to create device file for dpm state\n");
2745 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2747 DRM_ERROR("failed to create device file for dpm state\n");
2752 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
2754 DRM_ERROR("failed to create device file pp_num_states\n");
2757 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
2759 DRM_ERROR("failed to create device file pp_cur_state\n");
2762 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
2764 DRM_ERROR("failed to create device file pp_force_state\n");
2767 ret = device_create_file(adev->dev, &dev_attr_pp_table);
2769 DRM_ERROR("failed to create device file pp_table\n");
2773 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2775 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2778 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2780 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2783 if (adev->asic_type >= CHIP_VEGA10) {
2784 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_socclk);
2786 DRM_ERROR("failed to create device file pp_dpm_socclk\n");
2789 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2791 DRM_ERROR("failed to create device file pp_dpm_dcefclk\n");
2795 if (adev->asic_type >= CHIP_VEGA20) {
2796 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_fclk);
2798 DRM_ERROR("failed to create device file pp_dpm_fclk\n");
2802 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
2804 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
2807 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
2809 DRM_ERROR("failed to create device file pp_sclk_od\n");
2812 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
2814 DRM_ERROR("failed to create device file pp_mclk_od\n");
2817 ret = device_create_file(adev->dev,
2818 &dev_attr_pp_power_profile_mode);
2820 DRM_ERROR("failed to create device file "
2821 "pp_power_profile_mode\n");
2824 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2825 (!is_support_sw_smu(adev) && hwmgr->od_enabled)) {
2826 ret = device_create_file(adev->dev,
2827 &dev_attr_pp_od_clk_voltage);
2829 DRM_ERROR("failed to create device file "
2830 "pp_od_clk_voltage\n");
2834 ret = device_create_file(adev->dev,
2835 &dev_attr_gpu_busy_percent);
2837 DRM_ERROR("failed to create device file "
2838 "gpu_busy_level\n");
2841 /* APU does not have its own dedicated memory */
2842 if (!(adev->flags & AMD_IS_APU)) {
2843 ret = device_create_file(adev->dev,
2844 &dev_attr_mem_busy_percent);
2846 DRM_ERROR("failed to create device file "
2847 "mem_busy_percent\n");
2851 /* PCIe Perf counters won't work on APU nodes */
2852 if (!(adev->flags & AMD_IS_APU)) {
2853 ret = device_create_file(adev->dev, &dev_attr_pcie_bw);
2855 DRM_ERROR("failed to create device file pcie_bw\n");
2859 if (adev->unique_id)
2860 ret = device_create_file(adev->dev, &dev_attr_unique_id);
2862 DRM_ERROR("failed to create device file unique_id\n");
2865 ret = amdgpu_debugfs_pm_init(adev);
2867 DRM_ERROR("Failed to register debugfs file for dpm!\n");
2871 if ((adev->asic_type >= CHIP_VEGA10) &&
2872 !(adev->flags & AMD_IS_APU)) {
2873 ret = device_create_file(adev->dev,
2874 &dev_attr_ppfeatures);
2876 DRM_ERROR("failed to create device file "
2882 adev->pm.sysfs_initialized = true;
2887 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
2889 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2891 if (adev->pm.dpm_enabled == 0)
2894 if (adev->pm.int_hwmon_dev)
2895 hwmon_device_unregister(adev->pm.int_hwmon_dev);
2896 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
2897 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2899 device_remove_file(adev->dev, &dev_attr_pp_num_states);
2900 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
2901 device_remove_file(adev->dev, &dev_attr_pp_force_state);
2902 device_remove_file(adev->dev, &dev_attr_pp_table);
2904 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2905 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2906 if (adev->asic_type >= CHIP_VEGA10) {
2907 device_remove_file(adev->dev, &dev_attr_pp_dpm_socclk);
2908 device_remove_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2910 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
2911 if (adev->asic_type >= CHIP_VEGA20)
2912 device_remove_file(adev->dev, &dev_attr_pp_dpm_fclk);
2913 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
2914 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
2915 device_remove_file(adev->dev,
2916 &dev_attr_pp_power_profile_mode);
2917 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2918 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2919 device_remove_file(adev->dev,
2920 &dev_attr_pp_od_clk_voltage);
2921 device_remove_file(adev->dev, &dev_attr_gpu_busy_percent);
2922 if (!(adev->flags & AMD_IS_APU))
2923 device_remove_file(adev->dev, &dev_attr_mem_busy_percent);
2924 if (!(adev->flags & AMD_IS_APU))
2925 device_remove_file(adev->dev, &dev_attr_pcie_bw);
2926 if (adev->unique_id)
2927 device_remove_file(adev->dev, &dev_attr_unique_id);
2928 if ((adev->asic_type >= CHIP_VEGA10) &&
2929 !(adev->flags & AMD_IS_APU))
2930 device_remove_file(adev->dev, &dev_attr_ppfeatures);
2933 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
2937 if (!adev->pm.dpm_enabled)
2940 if (adev->mode_info.num_crtc)
2941 amdgpu_display_bandwidth_update(adev);
2943 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
2944 struct amdgpu_ring *ring = adev->rings[i];
2945 if (ring && ring->sched.ready)
2946 amdgpu_fence_wait_empty(ring);
2949 if (is_support_sw_smu(adev)) {
2950 struct smu_context *smu = &adev->smu;
2951 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
2952 mutex_lock(&(smu->mutex));
2953 smu_handle_task(&adev->smu,
2955 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE);
2956 mutex_unlock(&(smu->mutex));
2958 if (adev->powerplay.pp_funcs->dispatch_tasks) {
2959 if (!amdgpu_device_has_dc_support(adev)) {
2960 mutex_lock(&adev->pm.mutex);
2961 amdgpu_dpm_get_active_displays(adev);
2962 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
2963 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
2964 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
2965 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
2966 if (adev->pm.pm_display_cfg.vrefresh > 120)
2967 adev->pm.pm_display_cfg.min_vblank_time = 0;
2968 if (adev->powerplay.pp_funcs->display_configuration_change)
2969 adev->powerplay.pp_funcs->display_configuration_change(
2970 adev->powerplay.pp_handle,
2971 &adev->pm.pm_display_cfg);
2972 mutex_unlock(&adev->pm.mutex);
2974 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
2976 mutex_lock(&adev->pm.mutex);
2977 amdgpu_dpm_get_active_displays(adev);
2978 amdgpu_dpm_change_power_state_locked(adev);
2979 mutex_unlock(&adev->pm.mutex);
2987 #if defined(CONFIG_DEBUG_FS)
2989 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
2997 size = sizeof(value);
2998 seq_printf(m, "GFX Clocks and Power:\n");
2999 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3000 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3001 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3002 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3003 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3004 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3005 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3006 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3007 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3008 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3009 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3010 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3011 size = sizeof(uint32_t);
3012 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3013 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3014 size = sizeof(value);
3015 seq_printf(m, "\n");
3018 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3019 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3022 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3023 seq_printf(m, "GPU Load: %u %%\n", value);
3025 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3026 seq_printf(m, "MEM Load: %u %%\n", value);
3028 seq_printf(m, "\n");
3030 /* SMC feature mask */
3031 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3032 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3035 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3037 seq_printf(m, "UVD: Disabled\n");
3039 seq_printf(m, "UVD: Enabled\n");
3040 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3041 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3042 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3043 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3046 seq_printf(m, "\n");
3049 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3051 seq_printf(m, "VCE: Disabled\n");
3053 seq_printf(m, "VCE: Enabled\n");
3054 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3055 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3062 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3066 for (i = 0; clocks[i].flag; i++)
3067 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3068 (flags & clocks[i].flag) ? "On" : "Off");
3071 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3073 struct drm_info_node *node = (struct drm_info_node *) m->private;
3074 struct drm_device *dev = node->minor->dev;
3075 struct amdgpu_device *adev = dev->dev_private;
3076 struct drm_device *ddev = adev->ddev;
3079 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3080 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3081 amdgpu_parse_cg_state(m, flags);
3082 seq_printf(m, "\n");
3084 if (!adev->pm.dpm_enabled) {
3085 seq_printf(m, "dpm not enabled\n");
3088 if ((adev->flags & AMD_IS_PX) &&
3089 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
3090 seq_printf(m, "PX asic powered off\n");
3091 } else if (!is_support_sw_smu(adev) && adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3092 mutex_lock(&adev->pm.mutex);
3093 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3094 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3096 seq_printf(m, "Debugfs support not implemented for this asic\n");
3097 mutex_unlock(&adev->pm.mutex);
3099 return amdgpu_debugfs_pm_info_pp(m, adev);
3105 static const struct drm_info_list amdgpu_pm_info_list[] = {
3106 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3110 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3112 #if defined(CONFIG_DEBUG_FS)
3113 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));