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"},
71 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
72 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
76 static const struct hwmon_temp_label {
77 enum PP_HWMON_TEMP channel;
80 {PP_TEMP_EDGE, "edge"},
81 {PP_TEMP_JUNCTION, "junction"},
85 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
87 if (adev->pm.dpm_enabled) {
88 mutex_lock(&adev->pm.mutex);
89 if (power_supply_is_system_supplied() > 0)
90 adev->pm.ac_power = true;
92 adev->pm.ac_power = false;
93 if (adev->powerplay.pp_funcs->enable_bapm)
94 amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
95 mutex_unlock(&adev->pm.mutex);
99 int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor,
100 void *data, uint32_t *size)
107 if (is_support_sw_smu(adev))
108 ret = smu_read_sensor(&adev->smu, sensor, data, size);
110 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
111 ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle,
121 * DOC: power_dpm_state
123 * The power_dpm_state file is a legacy interface and is only provided for
124 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
125 * certain power related parameters. The file power_dpm_state is used for this.
126 * It accepts the following arguments:
136 * On older GPUs, the vbios provided a special power state for battery
137 * operation. Selecting battery switched to this state. This is no
138 * longer provided on newer GPUs so the option does nothing in that case.
142 * On older GPUs, the vbios provided a special power state for balanced
143 * operation. Selecting balanced switched to this state. This is no
144 * longer provided on newer GPUs so the option does nothing in that case.
148 * On older GPUs, the vbios provided a special power state for performance
149 * operation. Selecting performance switched to this state. This is no
150 * longer provided on newer GPUs so the option does nothing in that case.
154 static ssize_t amdgpu_get_dpm_state(struct device *dev,
155 struct device_attribute *attr,
158 struct drm_device *ddev = dev_get_drvdata(dev);
159 struct amdgpu_device *adev = ddev->dev_private;
160 enum amd_pm_state_type pm;
162 if (is_support_sw_smu(adev) && adev->smu.ppt_funcs->get_current_power_state)
163 pm = amdgpu_smu_get_current_power_state(adev);
164 else if (adev->powerplay.pp_funcs->get_current_power_state)
165 pm = amdgpu_dpm_get_current_power_state(adev);
167 pm = adev->pm.dpm.user_state;
169 return snprintf(buf, PAGE_SIZE, "%s\n",
170 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
171 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
174 static ssize_t amdgpu_set_dpm_state(struct device *dev,
175 struct device_attribute *attr,
179 struct drm_device *ddev = dev_get_drvdata(dev);
180 struct amdgpu_device *adev = ddev->dev_private;
181 enum amd_pm_state_type state;
183 if (strncmp("battery", buf, strlen("battery")) == 0)
184 state = POWER_STATE_TYPE_BATTERY;
185 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
186 state = POWER_STATE_TYPE_BALANCED;
187 else if (strncmp("performance", buf, strlen("performance")) == 0)
188 state = POWER_STATE_TYPE_PERFORMANCE;
194 if (adev->powerplay.pp_funcs->dispatch_tasks) {
195 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
197 mutex_lock(&adev->pm.mutex);
198 adev->pm.dpm.user_state = state;
199 mutex_unlock(&adev->pm.mutex);
201 /* Can't set dpm state when the card is off */
202 if (!(adev->flags & AMD_IS_PX) ||
203 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
204 amdgpu_pm_compute_clocks(adev);
212 * DOC: power_dpm_force_performance_level
214 * The amdgpu driver provides a sysfs API for adjusting certain power
215 * related parameters. The file power_dpm_force_performance_level is
216 * used for this. It accepts the following arguments:
236 * When auto is selected, the driver will attempt to dynamically select
237 * the optimal power profile for current conditions in the driver.
241 * When low is selected, the clocks are forced to the lowest power state.
245 * When high is selected, the clocks are forced to the highest power state.
249 * When manual is selected, the user can manually adjust which power states
250 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
251 * and pp_dpm_pcie files and adjust the power state transition heuristics
252 * via the pp_power_profile_mode sysfs file.
259 * When the profiling modes are selected, clock and power gating are
260 * disabled and the clocks are set for different profiling cases. This
261 * mode is recommended for profiling specific work loads where you do
262 * not want clock or power gating for clock fluctuation to interfere
263 * with your results. profile_standard sets the clocks to a fixed clock
264 * level which varies from asic to asic. profile_min_sclk forces the sclk
265 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
266 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
270 static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
271 struct device_attribute *attr,
274 struct drm_device *ddev = dev_get_drvdata(dev);
275 struct amdgpu_device *adev = ddev->dev_private;
276 enum amd_dpm_forced_level level = 0xff;
278 if (amdgpu_sriov_vf(adev))
281 if ((adev->flags & AMD_IS_PX) &&
282 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
283 return snprintf(buf, PAGE_SIZE, "off\n");
285 if (is_support_sw_smu(adev))
286 level = smu_get_performance_level(&adev->smu);
287 else if (adev->powerplay.pp_funcs->get_performance_level)
288 level = amdgpu_dpm_get_performance_level(adev);
290 level = adev->pm.dpm.forced_level;
292 return snprintf(buf, PAGE_SIZE, "%s\n",
293 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
294 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
295 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
296 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
297 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
298 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
299 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
300 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
304 static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
305 struct device_attribute *attr,
309 struct drm_device *ddev = dev_get_drvdata(dev);
310 struct amdgpu_device *adev = ddev->dev_private;
311 enum amd_dpm_forced_level level;
312 enum amd_dpm_forced_level current_level = 0xff;
315 /* Can't force performance level when the card is off */
316 if ((adev->flags & AMD_IS_PX) &&
317 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
320 if (!amdgpu_sriov_vf(adev)) {
321 if (is_support_sw_smu(adev))
322 current_level = smu_get_performance_level(&adev->smu);
323 else if (adev->powerplay.pp_funcs->get_performance_level)
324 current_level = amdgpu_dpm_get_performance_level(adev);
327 if (strncmp("low", buf, strlen("low")) == 0) {
328 level = AMD_DPM_FORCED_LEVEL_LOW;
329 } else if (strncmp("high", buf, strlen("high")) == 0) {
330 level = AMD_DPM_FORCED_LEVEL_HIGH;
331 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
332 level = AMD_DPM_FORCED_LEVEL_AUTO;
333 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
334 level = AMD_DPM_FORCED_LEVEL_MANUAL;
335 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
336 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
337 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
338 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
339 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
340 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
341 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
342 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
343 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
344 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
350 if (amdgpu_sriov_vf(adev)) {
351 if (amdgim_is_hwperf(adev) &&
352 adev->virt.ops->force_dpm_level) {
353 mutex_lock(&adev->pm.mutex);
354 adev->virt.ops->force_dpm_level(adev, level);
355 mutex_unlock(&adev->pm.mutex);
362 if (current_level == level)
365 /* profile_exit setting is valid only when current mode is in profile mode */
366 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
367 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
368 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
369 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
370 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
371 pr_err("Currently not in any profile mode!\n");
375 if (is_support_sw_smu(adev)) {
376 ret = smu_force_performance_level(&adev->smu, level);
379 } else if (adev->powerplay.pp_funcs->force_performance_level) {
380 mutex_lock(&adev->pm.mutex);
381 if (adev->pm.dpm.thermal_active) {
383 mutex_unlock(&adev->pm.mutex);
386 ret = amdgpu_dpm_force_performance_level(adev, level);
390 adev->pm.dpm.forced_level = level;
391 mutex_unlock(&adev->pm.mutex);
398 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
399 struct device_attribute *attr,
402 struct drm_device *ddev = dev_get_drvdata(dev);
403 struct amdgpu_device *adev = ddev->dev_private;
404 struct pp_states_info data;
407 if (is_support_sw_smu(adev)) {
408 ret = smu_get_power_num_states(&adev->smu, &data);
411 } else if (adev->powerplay.pp_funcs->get_pp_num_states)
412 amdgpu_dpm_get_pp_num_states(adev, &data);
414 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
415 for (i = 0; i < data.nums; i++)
416 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
417 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
418 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
419 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
420 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
425 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
426 struct device_attribute *attr,
429 struct drm_device *ddev = dev_get_drvdata(dev);
430 struct amdgpu_device *adev = ddev->dev_private;
431 struct pp_states_info data;
432 struct smu_context *smu = &adev->smu;
433 enum amd_pm_state_type pm = 0;
436 if (is_support_sw_smu(adev)) {
437 pm = smu_get_current_power_state(smu);
438 ret = smu_get_power_num_states(smu, &data);
441 } else if (adev->powerplay.pp_funcs->get_current_power_state
442 && adev->powerplay.pp_funcs->get_pp_num_states) {
443 pm = amdgpu_dpm_get_current_power_state(adev);
444 amdgpu_dpm_get_pp_num_states(adev, &data);
447 for (i = 0; i < data.nums; i++) {
448 if (pm == data.states[i])
455 return snprintf(buf, PAGE_SIZE, "%d\n", i);
458 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
459 struct device_attribute *attr,
462 struct drm_device *ddev = dev_get_drvdata(dev);
463 struct amdgpu_device *adev = ddev->dev_private;
465 if (adev->pp_force_state_enabled)
466 return amdgpu_get_pp_cur_state(dev, attr, buf);
468 return snprintf(buf, PAGE_SIZE, "\n");
471 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
472 struct device_attribute *attr,
476 struct drm_device *ddev = dev_get_drvdata(dev);
477 struct amdgpu_device *adev = ddev->dev_private;
478 enum amd_pm_state_type state = 0;
482 if (strlen(buf) == 1)
483 adev->pp_force_state_enabled = false;
484 else if (is_support_sw_smu(adev))
485 adev->pp_force_state_enabled = false;
486 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
487 adev->powerplay.pp_funcs->get_pp_num_states) {
488 struct pp_states_info data;
490 ret = kstrtoul(buf, 0, &idx);
491 if (ret || idx >= ARRAY_SIZE(data.states)) {
495 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
497 amdgpu_dpm_get_pp_num_states(adev, &data);
498 state = data.states[idx];
499 /* only set user selected power states */
500 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
501 state != POWER_STATE_TYPE_DEFAULT) {
502 amdgpu_dpm_dispatch_task(adev,
503 AMD_PP_TASK_ENABLE_USER_STATE, &state);
504 adev->pp_force_state_enabled = true;
514 * The amdgpu driver provides a sysfs API for uploading new powerplay
515 * tables. The file pp_table is used for this. Reading the file
516 * will dump the current power play table. Writing to the file
517 * will attempt to upload a new powerplay table and re-initialize
518 * powerplay using that new table.
522 static ssize_t amdgpu_get_pp_table(struct device *dev,
523 struct device_attribute *attr,
526 struct drm_device *ddev = dev_get_drvdata(dev);
527 struct amdgpu_device *adev = ddev->dev_private;
531 if (is_support_sw_smu(adev)) {
532 size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
536 else if (adev->powerplay.pp_funcs->get_pp_table)
537 size = amdgpu_dpm_get_pp_table(adev, &table);
541 if (size >= PAGE_SIZE)
542 size = PAGE_SIZE - 1;
544 memcpy(buf, table, size);
549 static ssize_t amdgpu_set_pp_table(struct device *dev,
550 struct device_attribute *attr,
554 struct drm_device *ddev = dev_get_drvdata(dev);
555 struct amdgpu_device *adev = ddev->dev_private;
558 if (is_support_sw_smu(adev)) {
559 ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
562 } else if (adev->powerplay.pp_funcs->set_pp_table)
563 amdgpu_dpm_set_pp_table(adev, buf, count);
569 * DOC: pp_od_clk_voltage
571 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
572 * in each power level within a power state. The pp_od_clk_voltage is used for
575 * < For Vega10 and previous ASICs >
577 * Reading the file will display:
579 * - a list of engine clock levels and voltages labeled OD_SCLK
581 * - a list of memory clock levels and voltages labeled OD_MCLK
583 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
585 * To manually adjust these settings, first select manual using
586 * power_dpm_force_performance_level. Enter a new value for each
587 * level by writing a string that contains "s/m level clock voltage" to
588 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
589 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
590 * 810 mV. When you have edited all of the states as needed, write
591 * "c" (commit) to the file to commit your changes. If you want to reset to the
592 * default power levels, write "r" (reset) to the file to reset them.
597 * Reading the file will display:
599 * - minimum and maximum engine clock labeled OD_SCLK
601 * - maximum memory clock labeled OD_MCLK
603 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
604 * They can be used to calibrate the sclk voltage curve.
606 * - a list of valid ranges for sclk, mclk, and voltage curve points
609 * To manually adjust these settings:
611 * - First select manual using power_dpm_force_performance_level
613 * - For clock frequency setting, enter a new value by writing a
614 * string that contains "s/m index clock" to the file. The index
615 * should be 0 if to set minimum clock. And 1 if to set maximum
616 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
617 * "m 1 800" will update maximum mclk to be 800Mhz.
619 * For sclk voltage curve, enter the new values by writing a
620 * string that contains "vc point clock voltage" to the file. The
621 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
622 * update point1 with clock set as 300Mhz and voltage as
623 * 600mV. "vc 2 1000 1000" will update point3 with clock set
624 * as 1000Mhz and voltage 1000mV.
626 * - When you have edited all of the states as needed, write "c" (commit)
627 * to the file to commit your changes
629 * - If you want to reset to the default power levels, write "r" (reset)
630 * to the file to reset them
634 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
635 struct device_attribute *attr,
639 struct drm_device *ddev = dev_get_drvdata(dev);
640 struct amdgpu_device *adev = ddev->dev_private;
642 uint32_t parameter_size = 0;
647 const char delimiter[3] = {' ', '\n', '\0'};
654 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
655 else if (*buf == 'm')
656 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
658 type = PP_OD_RESTORE_DEFAULT_TABLE;
659 else if (*buf == 'c')
660 type = PP_OD_COMMIT_DPM_TABLE;
661 else if (!strncmp(buf, "vc", 2))
662 type = PP_OD_EDIT_VDDC_CURVE;
666 memcpy(buf_cpy, buf, count+1);
670 if (type == PP_OD_EDIT_VDDC_CURVE)
672 while (isspace(*++tmp_str));
675 sub_str = strsep(&tmp_str, delimiter);
676 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
681 while (isspace(*tmp_str))
685 if (is_support_sw_smu(adev)) {
686 ret = smu_od_edit_dpm_table(&adev->smu, type,
687 parameter, parameter_size);
692 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
693 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
694 parameter, parameter_size);
699 if (type == PP_OD_COMMIT_DPM_TABLE) {
700 if (adev->powerplay.pp_funcs->dispatch_tasks) {
701 amdgpu_dpm_dispatch_task(adev,
702 AMD_PP_TASK_READJUST_POWER_STATE,
714 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
715 struct device_attribute *attr,
718 struct drm_device *ddev = dev_get_drvdata(dev);
719 struct amdgpu_device *adev = ddev->dev_private;
722 if (is_support_sw_smu(adev)) {
723 size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
724 size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
725 size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
726 size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
728 } else if (adev->powerplay.pp_funcs->print_clock_levels) {
729 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
730 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
731 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
732 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
735 return snprintf(buf, PAGE_SIZE, "\n");
743 * The amdgpu driver provides a sysfs API for adjusting what powerplay
744 * features to be enabled. The file ppfeatures is used for this. And
745 * this is only available for Vega10 and later dGPUs.
747 * Reading back the file will show you the followings:
748 * - Current ppfeature masks
749 * - List of the all supported powerplay features with their naming,
750 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
752 * To manually enable or disable a specific feature, just set or clear
753 * the corresponding bit from original ppfeature masks and input the
754 * new ppfeature masks.
756 static ssize_t amdgpu_set_ppfeature_status(struct device *dev,
757 struct device_attribute *attr,
761 struct drm_device *ddev = dev_get_drvdata(dev);
762 struct amdgpu_device *adev = ddev->dev_private;
763 uint64_t featuremask;
766 ret = kstrtou64(buf, 0, &featuremask);
770 pr_debug("featuremask = 0x%llx\n", featuremask);
772 if (is_support_sw_smu(adev)) {
773 ret = smu_set_ppfeature_status(&adev->smu, featuremask);
776 } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
777 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
785 static ssize_t amdgpu_get_ppfeature_status(struct device *dev,
786 struct device_attribute *attr,
789 struct drm_device *ddev = dev_get_drvdata(dev);
790 struct amdgpu_device *adev = ddev->dev_private;
792 if (is_support_sw_smu(adev)) {
793 return smu_get_ppfeature_status(&adev->smu, buf);
794 } else if (adev->powerplay.pp_funcs->get_ppfeature_status)
795 return amdgpu_dpm_get_ppfeature_status(adev, buf);
797 return snprintf(buf, PAGE_SIZE, "\n");
801 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk
804 * The amdgpu driver provides a sysfs API for adjusting what power levels
805 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
806 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
809 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
810 * Vega10 and later ASICs.
811 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
813 * Reading back the files will show you the available power levels within
814 * the power state and the clock information for those levels.
816 * To manually adjust these states, first select manual using
817 * power_dpm_force_performance_level.
818 * Secondly,Enter a new value for each level by inputing a string that
819 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
820 * E.g., echo 4 5 6 to > pp_dpm_sclk will enable sclk levels 4, 5, and 6.
822 * NOTE: change to the dcefclk max dpm level is not supported now
825 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
826 struct device_attribute *attr,
829 struct drm_device *ddev = dev_get_drvdata(dev);
830 struct amdgpu_device *adev = ddev->dev_private;
832 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev) &&
833 adev->virt.ops->get_pp_clk)
834 return adev->virt.ops->get_pp_clk(adev, PP_SCLK, buf);
836 if (is_support_sw_smu(adev))
837 return smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
838 else if (adev->powerplay.pp_funcs->print_clock_levels)
839 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
841 return snprintf(buf, PAGE_SIZE, "\n");
845 * Worst case: 32 bits individually specified, in octal at 12 characters
846 * per line (+1 for \n).
848 #define AMDGPU_MASK_BUF_MAX (32 * 13)
850 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
854 char *sub_str = NULL;
856 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
857 const char delimiter[3] = {' ', '\n', '\0'};
862 bytes = min(count, sizeof(buf_cpy) - 1);
863 memcpy(buf_cpy, buf, bytes);
864 buf_cpy[bytes] = '\0';
867 sub_str = strsep(&tmp, delimiter);
868 if (strlen(sub_str)) {
869 ret = kstrtol(sub_str, 0, &level);
880 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
881 struct device_attribute *attr,
885 struct drm_device *ddev = dev_get_drvdata(dev);
886 struct amdgpu_device *adev = ddev->dev_private;
890 if (amdgpu_sriov_vf(adev))
893 ret = amdgpu_read_mask(buf, count, &mask);
897 if (is_support_sw_smu(adev))
898 ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask);
899 else if (adev->powerplay.pp_funcs->force_clock_level)
900 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
908 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
909 struct device_attribute *attr,
912 struct drm_device *ddev = dev_get_drvdata(dev);
913 struct amdgpu_device *adev = ddev->dev_private;
915 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev) &&
916 adev->virt.ops->get_pp_clk)
917 return adev->virt.ops->get_pp_clk(adev, PP_MCLK, buf);
919 if (is_support_sw_smu(adev))
920 return smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
921 else if (adev->powerplay.pp_funcs->print_clock_levels)
922 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
924 return snprintf(buf, PAGE_SIZE, "\n");
927 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
928 struct device_attribute *attr,
932 struct drm_device *ddev = dev_get_drvdata(dev);
933 struct amdgpu_device *adev = ddev->dev_private;
937 if (amdgpu_sriov_vf(adev))
940 ret = amdgpu_read_mask(buf, count, &mask);
944 if (is_support_sw_smu(adev))
945 ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask);
946 else if (adev->powerplay.pp_funcs->force_clock_level)
947 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
955 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
956 struct device_attribute *attr,
959 struct drm_device *ddev = dev_get_drvdata(dev);
960 struct amdgpu_device *adev = ddev->dev_private;
962 if (is_support_sw_smu(adev))
963 return smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
964 else if (adev->powerplay.pp_funcs->print_clock_levels)
965 return amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
967 return snprintf(buf, PAGE_SIZE, "\n");
970 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
971 struct device_attribute *attr,
975 struct drm_device *ddev = dev_get_drvdata(dev);
976 struct amdgpu_device *adev = ddev->dev_private;
980 ret = amdgpu_read_mask(buf, count, &mask);
984 if (is_support_sw_smu(adev))
985 ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask);
986 else if (adev->powerplay.pp_funcs->force_clock_level)
987 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
995 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
996 struct device_attribute *attr,
999 struct drm_device *ddev = dev_get_drvdata(dev);
1000 struct amdgpu_device *adev = ddev->dev_private;
1002 if (is_support_sw_smu(adev))
1003 return smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1004 else if (adev->powerplay.pp_funcs->print_clock_levels)
1005 return amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1007 return snprintf(buf, PAGE_SIZE, "\n");
1010 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1011 struct device_attribute *attr,
1015 struct drm_device *ddev = dev_get_drvdata(dev);
1016 struct amdgpu_device *adev = ddev->dev_private;
1020 ret = amdgpu_read_mask(buf, count, &mask);
1024 if (is_support_sw_smu(adev))
1025 ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask);
1026 else if (adev->powerplay.pp_funcs->force_clock_level)
1027 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1035 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1036 struct device_attribute *attr,
1039 struct drm_device *ddev = dev_get_drvdata(dev);
1040 struct amdgpu_device *adev = ddev->dev_private;
1042 if (is_support_sw_smu(adev))
1043 return smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1044 else if (adev->powerplay.pp_funcs->print_clock_levels)
1045 return amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1047 return snprintf(buf, PAGE_SIZE, "\n");
1050 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1051 struct device_attribute *attr,
1055 struct drm_device *ddev = dev_get_drvdata(dev);
1056 struct amdgpu_device *adev = ddev->dev_private;
1060 ret = amdgpu_read_mask(buf, count, &mask);
1064 if (is_support_sw_smu(adev))
1065 ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask);
1066 else if (adev->powerplay.pp_funcs->force_clock_level)
1067 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1075 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1076 struct device_attribute *attr,
1079 struct drm_device *ddev = dev_get_drvdata(dev);
1080 struct amdgpu_device *adev = ddev->dev_private;
1082 if (is_support_sw_smu(adev))
1083 return smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1084 else if (adev->powerplay.pp_funcs->print_clock_levels)
1085 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1087 return snprintf(buf, PAGE_SIZE, "\n");
1090 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1091 struct device_attribute *attr,
1095 struct drm_device *ddev = dev_get_drvdata(dev);
1096 struct amdgpu_device *adev = ddev->dev_private;
1100 ret = amdgpu_read_mask(buf, count, &mask);
1104 if (is_support_sw_smu(adev))
1105 ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask);
1106 else if (adev->powerplay.pp_funcs->force_clock_level)
1107 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1115 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1116 struct device_attribute *attr,
1119 struct drm_device *ddev = dev_get_drvdata(dev);
1120 struct amdgpu_device *adev = ddev->dev_private;
1123 if (is_support_sw_smu(adev))
1124 value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK);
1125 else if (adev->powerplay.pp_funcs->get_sclk_od)
1126 value = amdgpu_dpm_get_sclk_od(adev);
1128 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1131 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1132 struct device_attribute *attr,
1136 struct drm_device *ddev = dev_get_drvdata(dev);
1137 struct amdgpu_device *adev = ddev->dev_private;
1141 ret = kstrtol(buf, 0, &value);
1148 if (is_support_sw_smu(adev)) {
1149 value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value);
1151 if (adev->powerplay.pp_funcs->set_sclk_od)
1152 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1154 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1155 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1157 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1158 amdgpu_pm_compute_clocks(adev);
1166 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1167 struct device_attribute *attr,
1170 struct drm_device *ddev = dev_get_drvdata(dev);
1171 struct amdgpu_device *adev = ddev->dev_private;
1174 if (is_support_sw_smu(adev))
1175 value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK);
1176 else if (adev->powerplay.pp_funcs->get_mclk_od)
1177 value = amdgpu_dpm_get_mclk_od(adev);
1179 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1182 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1183 struct device_attribute *attr,
1187 struct drm_device *ddev = dev_get_drvdata(dev);
1188 struct amdgpu_device *adev = ddev->dev_private;
1192 ret = kstrtol(buf, 0, &value);
1199 if (is_support_sw_smu(adev)) {
1200 value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value);
1202 if (adev->powerplay.pp_funcs->set_mclk_od)
1203 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1205 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1206 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1208 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1209 amdgpu_pm_compute_clocks(adev);
1218 * DOC: pp_power_profile_mode
1220 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1221 * related to switching between power levels in a power state. The file
1222 * pp_power_profile_mode is used for this.
1224 * Reading this file outputs a list of all of the predefined power profiles
1225 * and the relevant heuristics settings for that profile.
1227 * To select a profile or create a custom profile, first select manual using
1228 * power_dpm_force_performance_level. Writing the number of a predefined
1229 * profile to pp_power_profile_mode will enable those heuristics. To
1230 * create a custom set of heuristics, write a string of numbers to the file
1231 * starting with the number of the custom profile along with a setting
1232 * for each heuristic parameter. Due to differences across asic families
1233 * the heuristic parameters vary from family to family.
1237 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1238 struct device_attribute *attr,
1241 struct drm_device *ddev = dev_get_drvdata(dev);
1242 struct amdgpu_device *adev = ddev->dev_private;
1244 if (is_support_sw_smu(adev))
1245 return smu_get_power_profile_mode(&adev->smu, buf);
1246 else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1247 return amdgpu_dpm_get_power_profile_mode(adev, buf);
1249 return snprintf(buf, PAGE_SIZE, "\n");
1253 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1254 struct device_attribute *attr,
1259 struct drm_device *ddev = dev_get_drvdata(dev);
1260 struct amdgpu_device *adev = ddev->dev_private;
1261 uint32_t parameter_size = 0;
1263 char *sub_str, buf_cpy[128];
1267 long int profile_mode = 0;
1268 const char delimiter[3] = {' ', '\n', '\0'};
1272 ret = kstrtol(tmp, 0, &profile_mode);
1276 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1277 if (count < 2 || count > 127)
1279 while (isspace(*++buf))
1281 memcpy(buf_cpy, buf, count-i);
1283 while (tmp_str[0]) {
1284 sub_str = strsep(&tmp_str, delimiter);
1285 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1291 while (isspace(*tmp_str))
1295 parameter[parameter_size] = profile_mode;
1296 if (is_support_sw_smu(adev))
1297 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size);
1298 else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1299 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1309 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1310 * is as a percentage. The file gpu_busy_percent is used for this.
1311 * The SMU firmware computes a percentage of load based on the
1312 * aggregate activity level in the IP cores.
1314 static ssize_t amdgpu_get_busy_percent(struct device *dev,
1315 struct device_attribute *attr,
1318 struct drm_device *ddev = dev_get_drvdata(dev);
1319 struct amdgpu_device *adev = ddev->dev_private;
1320 int r, value, size = sizeof(value);
1322 /* read the IP busy sensor */
1323 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1324 (void *)&value, &size);
1329 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1333 * DOC: mem_busy_percent
1335 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1336 * is as a percentage. The file mem_busy_percent is used for this.
1337 * The SMU firmware computes a percentage of load based on the
1338 * aggregate activity level in the IP cores.
1340 static ssize_t amdgpu_get_memory_busy_percent(struct device *dev,
1341 struct device_attribute *attr,
1344 struct drm_device *ddev = dev_get_drvdata(dev);
1345 struct amdgpu_device *adev = ddev->dev_private;
1346 int r, value, size = sizeof(value);
1348 /* read the IP busy sensor */
1349 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1350 (void *)&value, &size);
1355 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1361 * The amdgpu driver provides a sysfs API for estimating how much data
1362 * has been received and sent by the GPU in the last second through PCIe.
1363 * The file pcie_bw is used for this.
1364 * The Perf counters count the number of received and sent messages and return
1365 * those values, as well as the maximum payload size of a PCIe packet (mps).
1366 * Note that it is not possible to easily and quickly obtain the size of each
1367 * packet transmitted, so we output the max payload size (mps) to allow for
1368 * quick estimation of the PCIe bandwidth usage
1370 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1371 struct device_attribute *attr,
1374 struct drm_device *ddev = dev_get_drvdata(dev);
1375 struct amdgpu_device *adev = ddev->dev_private;
1376 uint64_t count0, count1;
1378 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1379 return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1380 count0, count1, pcie_get_mps(adev->pdev));
1386 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1387 * The file unique_id is used for this.
1388 * This will provide a Unique ID that will persist from machine to machine
1390 * NOTE: This will only work for GFX9 and newer. This file will be absent
1391 * on unsupported ASICs (GFX8 and older)
1393 static ssize_t amdgpu_get_unique_id(struct device *dev,
1394 struct device_attribute *attr,
1397 struct drm_device *ddev = dev_get_drvdata(dev);
1398 struct amdgpu_device *adev = ddev->dev_private;
1400 if (adev->unique_id)
1401 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1406 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
1407 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
1408 amdgpu_get_dpm_forced_performance_level,
1409 amdgpu_set_dpm_forced_performance_level);
1410 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
1411 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
1412 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
1413 amdgpu_get_pp_force_state,
1414 amdgpu_set_pp_force_state);
1415 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
1416 amdgpu_get_pp_table,
1417 amdgpu_set_pp_table);
1418 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
1419 amdgpu_get_pp_dpm_sclk,
1420 amdgpu_set_pp_dpm_sclk);
1421 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
1422 amdgpu_get_pp_dpm_mclk,
1423 amdgpu_set_pp_dpm_mclk);
1424 static DEVICE_ATTR(pp_dpm_socclk, S_IRUGO | S_IWUSR,
1425 amdgpu_get_pp_dpm_socclk,
1426 amdgpu_set_pp_dpm_socclk);
1427 static DEVICE_ATTR(pp_dpm_fclk, S_IRUGO | S_IWUSR,
1428 amdgpu_get_pp_dpm_fclk,
1429 amdgpu_set_pp_dpm_fclk);
1430 static DEVICE_ATTR(pp_dpm_dcefclk, S_IRUGO | S_IWUSR,
1431 amdgpu_get_pp_dpm_dcefclk,
1432 amdgpu_set_pp_dpm_dcefclk);
1433 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
1434 amdgpu_get_pp_dpm_pcie,
1435 amdgpu_set_pp_dpm_pcie);
1436 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
1437 amdgpu_get_pp_sclk_od,
1438 amdgpu_set_pp_sclk_od);
1439 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
1440 amdgpu_get_pp_mclk_od,
1441 amdgpu_set_pp_mclk_od);
1442 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
1443 amdgpu_get_pp_power_profile_mode,
1444 amdgpu_set_pp_power_profile_mode);
1445 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
1446 amdgpu_get_pp_od_clk_voltage,
1447 amdgpu_set_pp_od_clk_voltage);
1448 static DEVICE_ATTR(gpu_busy_percent, S_IRUGO,
1449 amdgpu_get_busy_percent, NULL);
1450 static DEVICE_ATTR(mem_busy_percent, S_IRUGO,
1451 amdgpu_get_memory_busy_percent, NULL);
1452 static DEVICE_ATTR(pcie_bw, S_IRUGO, amdgpu_get_pcie_bw, NULL);
1453 static DEVICE_ATTR(ppfeatures, S_IRUGO | S_IWUSR,
1454 amdgpu_get_ppfeature_status,
1455 amdgpu_set_ppfeature_status);
1456 static DEVICE_ATTR(unique_id, S_IRUGO, amdgpu_get_unique_id, NULL);
1458 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1459 struct device_attribute *attr,
1462 struct amdgpu_device *adev = dev_get_drvdata(dev);
1463 struct drm_device *ddev = adev->ddev;
1464 int channel = to_sensor_dev_attr(attr)->index;
1465 int r, temp = 0, size = sizeof(temp);
1467 /* Can't get temperature when the card is off */
1468 if ((adev->flags & AMD_IS_PX) &&
1469 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1472 if (channel >= PP_TEMP_MAX)
1476 case PP_TEMP_JUNCTION:
1477 /* get current junction temperature */
1478 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1479 (void *)&temp, &size);
1484 /* get current edge temperature */
1485 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
1486 (void *)&temp, &size);
1491 /* get current memory temperature */
1492 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
1493 (void *)&temp, &size);
1499 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1502 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1503 struct device_attribute *attr,
1506 struct amdgpu_device *adev = dev_get_drvdata(dev);
1507 int hyst = to_sensor_dev_attr(attr)->index;
1511 temp = adev->pm.dpm.thermal.min_temp;
1513 temp = adev->pm.dpm.thermal.max_temp;
1515 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1518 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
1519 struct device_attribute *attr,
1522 struct amdgpu_device *adev = dev_get_drvdata(dev);
1523 int hyst = to_sensor_dev_attr(attr)->index;
1527 temp = adev->pm.dpm.thermal.min_hotspot_temp;
1529 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
1531 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1534 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
1535 struct device_attribute *attr,
1538 struct amdgpu_device *adev = dev_get_drvdata(dev);
1539 int hyst = to_sensor_dev_attr(attr)->index;
1543 temp = adev->pm.dpm.thermal.min_mem_temp;
1545 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
1547 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1550 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
1551 struct device_attribute *attr,
1554 int channel = to_sensor_dev_attr(attr)->index;
1556 if (channel >= PP_TEMP_MAX)
1559 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
1562 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
1563 struct device_attribute *attr,
1566 struct amdgpu_device *adev = dev_get_drvdata(dev);
1567 int channel = to_sensor_dev_attr(attr)->index;
1570 if (channel >= PP_TEMP_MAX)
1574 case PP_TEMP_JUNCTION:
1575 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
1578 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
1581 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
1585 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1588 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1589 struct device_attribute *attr,
1592 struct amdgpu_device *adev = dev_get_drvdata(dev);
1594 if (is_support_sw_smu(adev)) {
1595 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1597 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1600 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1603 return sprintf(buf, "%i\n", pwm_mode);
1606 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1607 struct device_attribute *attr,
1611 struct amdgpu_device *adev = dev_get_drvdata(dev);
1615 /* Can't adjust fan when the card is off */
1616 if ((adev->flags & AMD_IS_PX) &&
1617 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1620 if (is_support_sw_smu(adev)) {
1621 err = kstrtoint(buf, 10, &value);
1625 smu_set_fan_control_mode(&adev->smu, value);
1627 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1630 err = kstrtoint(buf, 10, &value);
1634 amdgpu_dpm_set_fan_control_mode(adev, value);
1640 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1641 struct device_attribute *attr,
1644 return sprintf(buf, "%i\n", 0);
1647 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1648 struct device_attribute *attr,
1651 return sprintf(buf, "%i\n", 255);
1654 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1655 struct device_attribute *attr,
1656 const char *buf, size_t count)
1658 struct amdgpu_device *adev = dev_get_drvdata(dev);
1663 /* Can't adjust fan when the card is off */
1664 if ((adev->flags & AMD_IS_PX) &&
1665 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1667 if (is_support_sw_smu(adev))
1668 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1670 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1671 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
1672 pr_info("manual fan speed control should be enabled first\n");
1676 err = kstrtou32(buf, 10, &value);
1680 value = (value * 100) / 255;
1682 if (is_support_sw_smu(adev)) {
1683 err = smu_set_fan_speed_percent(&adev->smu, value);
1686 } else if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1687 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1695 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1696 struct device_attribute *attr,
1699 struct amdgpu_device *adev = dev_get_drvdata(dev);
1703 /* Can't adjust fan when the card is off */
1704 if ((adev->flags & AMD_IS_PX) &&
1705 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1708 if (is_support_sw_smu(adev)) {
1709 err = smu_get_fan_speed_percent(&adev->smu, &speed);
1712 } else if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1713 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1718 speed = (speed * 255) / 100;
1720 return sprintf(buf, "%i\n", speed);
1723 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1724 struct device_attribute *attr,
1727 struct amdgpu_device *adev = dev_get_drvdata(dev);
1731 /* Can't adjust fan when the card is off */
1732 if ((adev->flags & AMD_IS_PX) &&
1733 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1736 if (is_support_sw_smu(adev)) {
1737 err = smu_get_current_rpm(&adev->smu, &speed);
1740 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1741 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1746 return sprintf(buf, "%i\n", speed);
1749 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
1750 struct device_attribute *attr,
1753 struct amdgpu_device *adev = dev_get_drvdata(dev);
1755 u32 size = sizeof(min_rpm);
1758 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
1759 (void *)&min_rpm, &size);
1763 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
1766 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
1767 struct device_attribute *attr,
1770 struct amdgpu_device *adev = dev_get_drvdata(dev);
1772 u32 size = sizeof(max_rpm);
1775 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
1776 (void *)&max_rpm, &size);
1780 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
1783 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
1784 struct device_attribute *attr,
1787 struct amdgpu_device *adev = dev_get_drvdata(dev);
1791 /* Can't adjust fan when the card is off */
1792 if ((adev->flags & AMD_IS_PX) &&
1793 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1796 if (is_support_sw_smu(adev)) {
1797 err = smu_get_current_rpm(&adev->smu, &rpm);
1800 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1801 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
1806 return sprintf(buf, "%i\n", rpm);
1809 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
1810 struct device_attribute *attr,
1811 const char *buf, size_t count)
1813 struct amdgpu_device *adev = dev_get_drvdata(dev);
1818 if (is_support_sw_smu(adev))
1819 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1821 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1823 if (pwm_mode != AMD_FAN_CTRL_MANUAL)
1826 /* Can't adjust fan when the card is off */
1827 if ((adev->flags & AMD_IS_PX) &&
1828 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1831 err = kstrtou32(buf, 10, &value);
1835 if (is_support_sw_smu(adev)) {
1836 err = smu_set_fan_speed_rpm(&adev->smu, value);
1839 } else if (adev->powerplay.pp_funcs->set_fan_speed_rpm) {
1840 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
1848 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
1849 struct device_attribute *attr,
1852 struct amdgpu_device *adev = dev_get_drvdata(dev);
1855 if (is_support_sw_smu(adev)) {
1856 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1858 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1861 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1863 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
1866 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
1867 struct device_attribute *attr,
1871 struct amdgpu_device *adev = dev_get_drvdata(dev);
1876 /* Can't adjust fan when the card is off */
1877 if ((adev->flags & AMD_IS_PX) &&
1878 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1882 err = kstrtoint(buf, 10, &value);
1887 pwm_mode = AMD_FAN_CTRL_AUTO;
1888 else if (value == 1)
1889 pwm_mode = AMD_FAN_CTRL_MANUAL;
1893 if (is_support_sw_smu(adev)) {
1894 smu_set_fan_control_mode(&adev->smu, pwm_mode);
1896 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1898 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
1904 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1905 struct device_attribute *attr,
1908 struct amdgpu_device *adev = dev_get_drvdata(dev);
1909 struct drm_device *ddev = adev->ddev;
1911 int r, size = sizeof(vddgfx);
1913 /* Can't get voltage when the card is off */
1914 if ((adev->flags & AMD_IS_PX) &&
1915 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1918 /* get the voltage */
1919 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1920 (void *)&vddgfx, &size);
1924 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1927 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1928 struct device_attribute *attr,
1931 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1934 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1935 struct device_attribute *attr,
1938 struct amdgpu_device *adev = dev_get_drvdata(dev);
1939 struct drm_device *ddev = adev->ddev;
1941 int r, size = sizeof(vddnb);
1943 /* only APUs have vddnb */
1944 if (!(adev->flags & AMD_IS_APU))
1947 /* Can't get voltage when the card is off */
1948 if ((adev->flags & AMD_IS_PX) &&
1949 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1952 /* get the voltage */
1953 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1954 (void *)&vddnb, &size);
1958 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1961 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1962 struct device_attribute *attr,
1965 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1968 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1969 struct device_attribute *attr,
1972 struct amdgpu_device *adev = dev_get_drvdata(dev);
1973 struct drm_device *ddev = adev->ddev;
1975 int r, size = sizeof(u32);
1978 /* Can't get power when the card is off */
1979 if ((adev->flags & AMD_IS_PX) &&
1980 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1983 /* get the voltage */
1984 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1985 (void *)&query, &size);
1989 /* convert to microwatts */
1990 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
1992 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1995 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1996 struct device_attribute *attr,
1999 return sprintf(buf, "%i\n", 0);
2002 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2003 struct device_attribute *attr,
2006 struct amdgpu_device *adev = dev_get_drvdata(dev);
2009 if (is_support_sw_smu(adev)) {
2010 smu_get_power_limit(&adev->smu, &limit, true);
2011 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2012 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2013 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2014 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2016 return snprintf(buf, PAGE_SIZE, "\n");
2020 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2021 struct device_attribute *attr,
2024 struct amdgpu_device *adev = dev_get_drvdata(dev);
2027 if (is_support_sw_smu(adev)) {
2028 smu_get_power_limit(&adev->smu, &limit, false);
2029 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2030 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2031 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2032 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2034 return snprintf(buf, PAGE_SIZE, "\n");
2039 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2040 struct device_attribute *attr,
2044 struct amdgpu_device *adev = dev_get_drvdata(dev);
2048 err = kstrtou32(buf, 10, &value);
2052 value = value / 1000000; /* convert to Watt */
2053 if (is_support_sw_smu(adev)) {
2054 adev->smu.funcs->set_power_limit(&adev->smu, value);
2055 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
2056 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2066 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2067 struct device_attribute *attr,
2070 struct amdgpu_device *adev = dev_get_drvdata(dev);
2071 struct drm_device *ddev = adev->ddev;
2073 int r, size = sizeof(sclk);
2075 /* Can't get voltage when the card is off */
2076 if ((adev->flags & AMD_IS_PX) &&
2077 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2080 /* sanity check PP is enabled */
2081 if (!(adev->powerplay.pp_funcs &&
2082 adev->powerplay.pp_funcs->read_sensor))
2086 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2087 (void *)&sclk, &size);
2091 return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2094 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2095 struct device_attribute *attr,
2098 return snprintf(buf, PAGE_SIZE, "sclk\n");
2101 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2102 struct device_attribute *attr,
2105 struct amdgpu_device *adev = dev_get_drvdata(dev);
2106 struct drm_device *ddev = adev->ddev;
2108 int r, size = sizeof(mclk);
2110 /* Can't get voltage when the card is off */
2111 if ((adev->flags & AMD_IS_PX) &&
2112 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2115 /* sanity check PP is enabled */
2116 if (!(adev->powerplay.pp_funcs &&
2117 adev->powerplay.pp_funcs->read_sensor))
2121 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2122 (void *)&mclk, &size);
2126 return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
2129 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2130 struct device_attribute *attr,
2133 return snprintf(buf, PAGE_SIZE, "mclk\n");
2139 * The amdgpu driver exposes the following sensor interfaces:
2141 * - GPU temperature (via the on-die sensor)
2145 * - Northbridge voltage (APUs only)
2151 * - GPU gfx/compute engine clock
2153 * - GPU memory clock (dGPU only)
2155 * hwmon interfaces for GPU temperature:
2157 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
2158 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
2160 * - temp[1-3]_label: temperature channel label
2161 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
2163 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
2164 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
2166 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
2167 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
2169 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
2170 * - these are supported on SOC15 dGPUs only
2172 * hwmon interfaces for GPU voltage:
2174 * - in0_input: the voltage on the GPU in millivolts
2176 * - in1_input: the voltage on the Northbridge in millivolts
2178 * hwmon interfaces for GPU power:
2180 * - power1_average: average power used by the GPU in microWatts
2182 * - power1_cap_min: minimum cap supported in microWatts
2184 * - power1_cap_max: maximum cap supported in microWatts
2186 * - power1_cap: selected power cap in microWatts
2188 * hwmon interfaces for GPU fan:
2190 * - pwm1: pulse width modulation fan level (0-255)
2192 * - 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)
2194 * - pwm1_min: pulse width modulation fan control minimum level (0)
2196 * - pwm1_max: pulse width modulation fan control maximum level (255)
2198 * - fan1_min: an minimum value Unit: revolution/min (RPM)
2200 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
2202 * - fan1_input: fan speed in RPM
2204 * - fan[1-*]_target: Desired fan speed Unit: revolution/min (RPM)
2206 * - fan[1-*]_enable: Enable or disable the sensors.1: Enable 0: Disable
2208 * hwmon interfaces for GPU clocks:
2210 * - freq1_input: the gfx/compute clock in hertz
2212 * - freq2_input: the memory clock in hertz
2214 * You can use hwmon tools like sensors to view this information on your system.
2218 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
2219 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
2220 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
2221 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
2222 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
2223 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
2224 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
2225 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
2226 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
2227 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
2228 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
2229 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2230 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
2231 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
2232 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
2233 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
2234 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
2235 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
2236 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
2237 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
2238 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
2239 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
2240 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
2241 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2242 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
2243 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
2244 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
2245 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
2246 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
2247 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
2248 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
2249 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
2250 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
2251 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
2252 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
2253 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
2255 static struct attribute *hwmon_attributes[] = {
2256 &sensor_dev_attr_temp1_input.dev_attr.attr,
2257 &sensor_dev_attr_temp1_crit.dev_attr.attr,
2258 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2259 &sensor_dev_attr_temp2_input.dev_attr.attr,
2260 &sensor_dev_attr_temp2_crit.dev_attr.attr,
2261 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2262 &sensor_dev_attr_temp3_input.dev_attr.attr,
2263 &sensor_dev_attr_temp3_crit.dev_attr.attr,
2264 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2265 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
2266 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
2267 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2268 &sensor_dev_attr_temp1_label.dev_attr.attr,
2269 &sensor_dev_attr_temp2_label.dev_attr.attr,
2270 &sensor_dev_attr_temp3_label.dev_attr.attr,
2271 &sensor_dev_attr_pwm1.dev_attr.attr,
2272 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2273 &sensor_dev_attr_pwm1_min.dev_attr.attr,
2274 &sensor_dev_attr_pwm1_max.dev_attr.attr,
2275 &sensor_dev_attr_fan1_input.dev_attr.attr,
2276 &sensor_dev_attr_fan1_min.dev_attr.attr,
2277 &sensor_dev_attr_fan1_max.dev_attr.attr,
2278 &sensor_dev_attr_fan1_target.dev_attr.attr,
2279 &sensor_dev_attr_fan1_enable.dev_attr.attr,
2280 &sensor_dev_attr_in0_input.dev_attr.attr,
2281 &sensor_dev_attr_in0_label.dev_attr.attr,
2282 &sensor_dev_attr_in1_input.dev_attr.attr,
2283 &sensor_dev_attr_in1_label.dev_attr.attr,
2284 &sensor_dev_attr_power1_average.dev_attr.attr,
2285 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
2286 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
2287 &sensor_dev_attr_power1_cap.dev_attr.attr,
2288 &sensor_dev_attr_freq1_input.dev_attr.attr,
2289 &sensor_dev_attr_freq1_label.dev_attr.attr,
2290 &sensor_dev_attr_freq2_input.dev_attr.attr,
2291 &sensor_dev_attr_freq2_label.dev_attr.attr,
2295 static umode_t hwmon_attributes_visible(struct kobject *kobj,
2296 struct attribute *attr, int index)
2298 struct device *dev = kobj_to_dev(kobj);
2299 struct amdgpu_device *adev = dev_get_drvdata(dev);
2300 umode_t effective_mode = attr->mode;
2302 /* Skip fan attributes if fan is not present */
2303 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2304 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2305 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2306 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2307 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2308 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2309 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2310 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2311 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2314 /* Skip fan attributes on APU */
2315 if ((adev->flags & AMD_IS_APU) &&
2316 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2317 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2318 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2319 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2320 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2321 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2322 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2323 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2324 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2327 /* Skip limit attributes if DPM is not enabled */
2328 if (!adev->pm.dpm_enabled &&
2329 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
2330 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
2331 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2332 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2333 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2334 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2335 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2336 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2337 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2338 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2339 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2342 if (!is_support_sw_smu(adev)) {
2343 /* mask fan attributes if we have no bindings for this asic to expose */
2344 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
2345 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
2346 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
2347 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
2348 effective_mode &= ~S_IRUGO;
2350 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2351 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
2352 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
2353 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
2354 effective_mode &= ~S_IWUSR;
2357 if ((adev->flags & AMD_IS_APU) &&
2358 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
2359 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
2360 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
2361 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
2364 if (!is_support_sw_smu(adev)) {
2365 /* hide max/min values if we can't both query and manage the fan */
2366 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2367 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
2368 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2369 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2370 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2371 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
2374 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2375 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2376 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2377 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
2381 /* only APUs have vddnb */
2382 if (!(adev->flags & AMD_IS_APU) &&
2383 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
2384 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
2387 /* no mclk on APUs */
2388 if ((adev->flags & AMD_IS_APU) &&
2389 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
2390 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
2393 /* only SOC15 dGPUs support hotspot and mem temperatures */
2394 if (((adev->flags & AMD_IS_APU) ||
2395 adev->asic_type < CHIP_VEGA10) &&
2396 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
2397 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
2398 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
2399 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
2400 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
2401 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
2402 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
2403 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
2404 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
2405 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
2406 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
2409 return effective_mode;
2412 static const struct attribute_group hwmon_attrgroup = {
2413 .attrs = hwmon_attributes,
2414 .is_visible = hwmon_attributes_visible,
2417 static const struct attribute_group *hwmon_groups[] = {
2422 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
2424 struct amdgpu_device *adev =
2425 container_of(work, struct amdgpu_device,
2426 pm.dpm.thermal.work);
2427 /* switch to the thermal state */
2428 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
2429 int temp, size = sizeof(temp);
2431 if (!adev->pm.dpm_enabled)
2434 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
2435 (void *)&temp, &size)) {
2436 if (temp < adev->pm.dpm.thermal.min_temp)
2437 /* switch back the user state */
2438 dpm_state = adev->pm.dpm.user_state;
2440 if (adev->pm.dpm.thermal.high_to_low)
2441 /* switch back the user state */
2442 dpm_state = adev->pm.dpm.user_state;
2444 mutex_lock(&adev->pm.mutex);
2445 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
2446 adev->pm.dpm.thermal_active = true;
2448 adev->pm.dpm.thermal_active = false;
2449 adev->pm.dpm.state = dpm_state;
2450 mutex_unlock(&adev->pm.mutex);
2452 amdgpu_pm_compute_clocks(adev);
2455 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
2456 enum amd_pm_state_type dpm_state)
2459 struct amdgpu_ps *ps;
2461 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
2464 /* check if the vblank period is too short to adjust the mclk */
2465 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
2466 if (amdgpu_dpm_vblank_too_short(adev))
2467 single_display = false;
2470 /* certain older asics have a separare 3D performance state,
2471 * so try that first if the user selected performance
2473 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
2474 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
2475 /* balanced states don't exist at the moment */
2476 if (dpm_state == POWER_STATE_TYPE_BALANCED)
2477 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2480 /* Pick the best power state based on current conditions */
2481 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
2482 ps = &adev->pm.dpm.ps[i];
2483 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
2484 switch (dpm_state) {
2486 case POWER_STATE_TYPE_BATTERY:
2487 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
2488 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2495 case POWER_STATE_TYPE_BALANCED:
2496 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
2497 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2504 case POWER_STATE_TYPE_PERFORMANCE:
2505 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
2506 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2513 /* internal states */
2514 case POWER_STATE_TYPE_INTERNAL_UVD:
2515 if (adev->pm.dpm.uvd_ps)
2516 return adev->pm.dpm.uvd_ps;
2519 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2520 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
2523 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2524 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
2527 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2528 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
2531 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2532 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
2535 case POWER_STATE_TYPE_INTERNAL_BOOT:
2536 return adev->pm.dpm.boot_ps;
2537 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2538 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
2541 case POWER_STATE_TYPE_INTERNAL_ACPI:
2542 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
2545 case POWER_STATE_TYPE_INTERNAL_ULV:
2546 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
2549 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2550 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2557 /* use a fallback state if we didn't match */
2558 switch (dpm_state) {
2559 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2560 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
2561 goto restart_search;
2562 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2563 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2564 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2565 if (adev->pm.dpm.uvd_ps) {
2566 return adev->pm.dpm.uvd_ps;
2568 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2569 goto restart_search;
2571 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2572 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
2573 goto restart_search;
2574 case POWER_STATE_TYPE_INTERNAL_ACPI:
2575 dpm_state = POWER_STATE_TYPE_BATTERY;
2576 goto restart_search;
2577 case POWER_STATE_TYPE_BATTERY:
2578 case POWER_STATE_TYPE_BALANCED:
2579 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2580 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2581 goto restart_search;
2589 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
2591 struct amdgpu_ps *ps;
2592 enum amd_pm_state_type dpm_state;
2596 /* if dpm init failed */
2597 if (!adev->pm.dpm_enabled)
2600 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
2601 /* add other state override checks here */
2602 if ((!adev->pm.dpm.thermal_active) &&
2603 (!adev->pm.dpm.uvd_active))
2604 adev->pm.dpm.state = adev->pm.dpm.user_state;
2606 dpm_state = adev->pm.dpm.state;
2608 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
2610 adev->pm.dpm.requested_ps = ps;
2614 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
2615 printk("switching from power state:\n");
2616 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
2617 printk("switching to power state:\n");
2618 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
2621 /* update whether vce is active */
2622 ps->vce_active = adev->pm.dpm.vce_active;
2623 if (adev->powerplay.pp_funcs->display_configuration_changed)
2624 amdgpu_dpm_display_configuration_changed(adev);
2626 ret = amdgpu_dpm_pre_set_power_state(adev);
2630 if (adev->powerplay.pp_funcs->check_state_equal) {
2631 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
2638 amdgpu_dpm_set_power_state(adev);
2639 amdgpu_dpm_post_set_power_state(adev);
2641 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
2642 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
2644 if (adev->powerplay.pp_funcs->force_performance_level) {
2645 if (adev->pm.dpm.thermal_active) {
2646 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
2647 /* force low perf level for thermal */
2648 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
2649 /* save the user's level */
2650 adev->pm.dpm.forced_level = level;
2652 /* otherwise, user selected level */
2653 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
2658 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
2661 if (is_support_sw_smu(adev)) {
2662 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_UVD, enable);
2664 DRM_ERROR("[SW SMU]: dpm enable uvd failed, state = %s, ret = %d. \n",
2665 enable ? "true" : "false", ret);
2666 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2667 /* enable/disable UVD */
2668 mutex_lock(&adev->pm.mutex);
2669 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
2670 mutex_unlock(&adev->pm.mutex);
2672 /* enable/disable Low Memory PState for UVD (4k videos) */
2673 if (adev->asic_type == CHIP_STONEY &&
2674 adev->uvd.decode_image_width >= WIDTH_4K) {
2675 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2677 if (hwmgr && hwmgr->hwmgr_func &&
2678 hwmgr->hwmgr_func->update_nbdpm_pstate)
2679 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
2685 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
2688 if (is_support_sw_smu(adev)) {
2689 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_VCE, enable);
2691 DRM_ERROR("[SW SMU]: dpm enable vce failed, state = %s, ret = %d. \n",
2692 enable ? "true" : "false", ret);
2693 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2694 /* enable/disable VCE */
2695 mutex_lock(&adev->pm.mutex);
2696 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
2697 mutex_unlock(&adev->pm.mutex);
2701 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
2705 if (adev->powerplay.pp_funcs->print_power_state == NULL)
2708 for (i = 0; i < adev->pm.dpm.num_ps; i++)
2709 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
2713 int amdgpu_pm_virt_sysfs_init(struct amdgpu_device *adev)
2717 if (!(amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev)))
2720 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2722 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2726 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2728 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2732 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2734 DRM_ERROR("failed to create device file for dpm state\n");
2741 void amdgpu_pm_virt_sysfs_fini(struct amdgpu_device *adev)
2743 if (!(amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev)))
2746 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2747 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2748 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2751 int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version)
2755 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
2756 r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
2758 pr_err("smu firmware loading failed\n");
2761 *smu_version = adev->pm.fw_version;
2766 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
2768 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2771 if (adev->pm.sysfs_initialized)
2774 if (adev->pm.dpm_enabled == 0)
2777 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
2780 if (IS_ERR(adev->pm.int_hwmon_dev)) {
2781 ret = PTR_ERR(adev->pm.int_hwmon_dev);
2783 "Unable to register hwmon device: %d\n", ret);
2787 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
2789 DRM_ERROR("failed to create device file for dpm state\n");
2792 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2794 DRM_ERROR("failed to create device file for dpm state\n");
2799 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
2801 DRM_ERROR("failed to create device file pp_num_states\n");
2804 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
2806 DRM_ERROR("failed to create device file pp_cur_state\n");
2809 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
2811 DRM_ERROR("failed to create device file pp_force_state\n");
2814 ret = device_create_file(adev->dev, &dev_attr_pp_table);
2816 DRM_ERROR("failed to create device file pp_table\n");
2820 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2822 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2825 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2827 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2830 if (adev->asic_type >= CHIP_VEGA10) {
2831 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_socclk);
2833 DRM_ERROR("failed to create device file pp_dpm_socclk\n");
2836 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2838 DRM_ERROR("failed to create device file pp_dpm_dcefclk\n");
2842 if (adev->asic_type >= CHIP_VEGA20) {
2843 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_fclk);
2845 DRM_ERROR("failed to create device file pp_dpm_fclk\n");
2849 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
2851 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
2854 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
2856 DRM_ERROR("failed to create device file pp_sclk_od\n");
2859 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
2861 DRM_ERROR("failed to create device file pp_mclk_od\n");
2864 ret = device_create_file(adev->dev,
2865 &dev_attr_pp_power_profile_mode);
2867 DRM_ERROR("failed to create device file "
2868 "pp_power_profile_mode\n");
2871 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2872 (!is_support_sw_smu(adev) && hwmgr->od_enabled)) {
2873 ret = device_create_file(adev->dev,
2874 &dev_attr_pp_od_clk_voltage);
2876 DRM_ERROR("failed to create device file "
2877 "pp_od_clk_voltage\n");
2881 ret = device_create_file(adev->dev,
2882 &dev_attr_gpu_busy_percent);
2884 DRM_ERROR("failed to create device file "
2885 "gpu_busy_level\n");
2888 /* APU does not have its own dedicated memory */
2889 if (!(adev->flags & AMD_IS_APU) &&
2890 (adev->asic_type != CHIP_VEGA10)) {
2891 ret = device_create_file(adev->dev,
2892 &dev_attr_mem_busy_percent);
2894 DRM_ERROR("failed to create device file "
2895 "mem_busy_percent\n");
2899 /* PCIe Perf counters won't work on APU nodes */
2900 if (!(adev->flags & AMD_IS_APU)) {
2901 ret = device_create_file(adev->dev, &dev_attr_pcie_bw);
2903 DRM_ERROR("failed to create device file pcie_bw\n");
2907 if (adev->unique_id)
2908 ret = device_create_file(adev->dev, &dev_attr_unique_id);
2910 DRM_ERROR("failed to create device file unique_id\n");
2913 ret = amdgpu_debugfs_pm_init(adev);
2915 DRM_ERROR("Failed to register debugfs file for dpm!\n");
2919 if ((adev->asic_type >= CHIP_VEGA10) &&
2920 !(adev->flags & AMD_IS_APU)) {
2921 ret = device_create_file(adev->dev,
2922 &dev_attr_ppfeatures);
2924 DRM_ERROR("failed to create device file "
2930 adev->pm.sysfs_initialized = true;
2935 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
2937 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2939 if (adev->pm.dpm_enabled == 0)
2942 if (adev->pm.int_hwmon_dev)
2943 hwmon_device_unregister(adev->pm.int_hwmon_dev);
2944 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
2945 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2947 device_remove_file(adev->dev, &dev_attr_pp_num_states);
2948 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
2949 device_remove_file(adev->dev, &dev_attr_pp_force_state);
2950 device_remove_file(adev->dev, &dev_attr_pp_table);
2952 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2953 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2954 if (adev->asic_type >= CHIP_VEGA10) {
2955 device_remove_file(adev->dev, &dev_attr_pp_dpm_socclk);
2956 device_remove_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2958 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
2959 if (adev->asic_type >= CHIP_VEGA20)
2960 device_remove_file(adev->dev, &dev_attr_pp_dpm_fclk);
2961 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
2962 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
2963 device_remove_file(adev->dev,
2964 &dev_attr_pp_power_profile_mode);
2965 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2966 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2967 device_remove_file(adev->dev,
2968 &dev_attr_pp_od_clk_voltage);
2969 device_remove_file(adev->dev, &dev_attr_gpu_busy_percent);
2970 if (!(adev->flags & AMD_IS_APU) &&
2971 (adev->asic_type != CHIP_VEGA10))
2972 device_remove_file(adev->dev, &dev_attr_mem_busy_percent);
2973 if (!(adev->flags & AMD_IS_APU))
2974 device_remove_file(adev->dev, &dev_attr_pcie_bw);
2975 if (adev->unique_id)
2976 device_remove_file(adev->dev, &dev_attr_unique_id);
2977 if ((adev->asic_type >= CHIP_VEGA10) &&
2978 !(adev->flags & AMD_IS_APU))
2979 device_remove_file(adev->dev, &dev_attr_ppfeatures);
2982 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
2986 if (!adev->pm.dpm_enabled)
2989 if (adev->mode_info.num_crtc)
2990 amdgpu_display_bandwidth_update(adev);
2992 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
2993 struct amdgpu_ring *ring = adev->rings[i];
2994 if (ring && ring->sched.ready)
2995 amdgpu_fence_wait_empty(ring);
2998 if (is_support_sw_smu(adev)) {
2999 struct smu_context *smu = &adev->smu;
3000 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
3001 mutex_lock(&(smu->mutex));
3002 smu_handle_task(&adev->smu,
3004 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE);
3005 mutex_unlock(&(smu->mutex));
3007 if (adev->powerplay.pp_funcs->dispatch_tasks) {
3008 if (!amdgpu_device_has_dc_support(adev)) {
3009 mutex_lock(&adev->pm.mutex);
3010 amdgpu_dpm_get_active_displays(adev);
3011 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
3012 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
3013 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
3014 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
3015 if (adev->pm.pm_display_cfg.vrefresh > 120)
3016 adev->pm.pm_display_cfg.min_vblank_time = 0;
3017 if (adev->powerplay.pp_funcs->display_configuration_change)
3018 adev->powerplay.pp_funcs->display_configuration_change(
3019 adev->powerplay.pp_handle,
3020 &adev->pm.pm_display_cfg);
3021 mutex_unlock(&adev->pm.mutex);
3023 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
3025 mutex_lock(&adev->pm.mutex);
3026 amdgpu_dpm_get_active_displays(adev);
3027 amdgpu_dpm_change_power_state_locked(adev);
3028 mutex_unlock(&adev->pm.mutex);