2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "amdgpu_drv.h"
28 #include "amdgpu_pm.h"
29 #include "amdgpu_dpm.h"
31 #include <linux/pci.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/nospec.h>
35 #include <linux/pm_runtime.h>
36 #include <asm/processor.h>
39 static const struct cg_flag_name clocks[] = {
40 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
41 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
42 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
43 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
44 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
45 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
46 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
47 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
48 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
49 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
50 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
51 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
52 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
53 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
54 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
55 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
56 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
57 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
58 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
59 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
60 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
61 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
62 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
63 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
64 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
65 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
66 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
67 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
68 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
69 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG 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"},
86 * DOC: power_dpm_state
88 * The power_dpm_state file is a legacy interface and is only provided for
89 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
90 * certain power related parameters. The file power_dpm_state is used for this.
91 * It accepts the following arguments:
101 * On older GPUs, the vbios provided a special power state for battery
102 * operation. Selecting battery switched to this state. This is no
103 * longer provided on newer GPUs so the option does nothing in that case.
107 * On older GPUs, the vbios provided a special power state for balanced
108 * operation. Selecting balanced switched to this state. This is no
109 * longer provided on newer GPUs so the option does nothing in that case.
113 * On older GPUs, the vbios provided a special power state for performance
114 * operation. Selecting performance switched to this state. This is no
115 * longer provided on newer GPUs so the option does nothing in that case.
119 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
120 struct device_attribute *attr,
123 struct drm_device *ddev = dev_get_drvdata(dev);
124 struct amdgpu_device *adev = drm_to_adev(ddev);
125 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
126 enum amd_pm_state_type pm;
129 if (amdgpu_in_reset(adev))
131 if (adev->in_suspend && !adev->in_runpm)
134 ret = pm_runtime_get_sync(ddev->dev);
136 pm_runtime_put_autosuspend(ddev->dev);
140 if (pp_funcs->get_current_power_state) {
141 pm = amdgpu_dpm_get_current_power_state(adev);
143 pm = adev->pm.dpm.user_state;
146 pm_runtime_mark_last_busy(ddev->dev);
147 pm_runtime_put_autosuspend(ddev->dev);
149 return sysfs_emit(buf, "%s\n",
150 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
151 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
154 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
155 struct device_attribute *attr,
159 struct drm_device *ddev = dev_get_drvdata(dev);
160 struct amdgpu_device *adev = drm_to_adev(ddev);
161 enum amd_pm_state_type state;
164 if (amdgpu_in_reset(adev))
166 if (adev->in_suspend && !adev->in_runpm)
169 if (strncmp("battery", buf, strlen("battery")) == 0)
170 state = POWER_STATE_TYPE_BATTERY;
171 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
172 state = POWER_STATE_TYPE_BALANCED;
173 else if (strncmp("performance", buf, strlen("performance")) == 0)
174 state = POWER_STATE_TYPE_PERFORMANCE;
178 ret = pm_runtime_get_sync(ddev->dev);
180 pm_runtime_put_autosuspend(ddev->dev);
184 if (is_support_sw_smu(adev)) {
185 mutex_lock(&adev->pm.mutex);
186 adev->pm.dpm.user_state = state;
187 mutex_unlock(&adev->pm.mutex);
188 } else if (adev->powerplay.pp_funcs->dispatch_tasks) {
189 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
191 mutex_lock(&adev->pm.mutex);
192 adev->pm.dpm.user_state = state;
193 mutex_unlock(&adev->pm.mutex);
195 amdgpu_pm_compute_clocks(adev);
197 pm_runtime_mark_last_busy(ddev->dev);
198 pm_runtime_put_autosuspend(ddev->dev);
205 * DOC: power_dpm_force_performance_level
207 * The amdgpu driver provides a sysfs API for adjusting certain power
208 * related parameters. The file power_dpm_force_performance_level is
209 * used for this. It accepts the following arguments:
229 * When auto is selected, the driver will attempt to dynamically select
230 * the optimal power profile for current conditions in the driver.
234 * When low is selected, the clocks are forced to the lowest power state.
238 * When high is selected, the clocks are forced to the highest power state.
242 * When manual is selected, the user can manually adjust which power states
243 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
244 * and pp_dpm_pcie files and adjust the power state transition heuristics
245 * via the pp_power_profile_mode sysfs file.
252 * When the profiling modes are selected, clock and power gating are
253 * disabled and the clocks are set for different profiling cases. This
254 * mode is recommended for profiling specific work loads where you do
255 * not want clock or power gating for clock fluctuation to interfere
256 * with your results. profile_standard sets the clocks to a fixed clock
257 * level which varies from asic to asic. profile_min_sclk forces the sclk
258 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
259 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
263 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
264 struct device_attribute *attr,
267 struct drm_device *ddev = dev_get_drvdata(dev);
268 struct amdgpu_device *adev = drm_to_adev(ddev);
269 enum amd_dpm_forced_level level = 0xff;
272 if (amdgpu_in_reset(adev))
274 if (adev->in_suspend && !adev->in_runpm)
277 ret = pm_runtime_get_sync(ddev->dev);
279 pm_runtime_put_autosuspend(ddev->dev);
283 if (adev->powerplay.pp_funcs->get_performance_level)
284 level = amdgpu_dpm_get_performance_level(adev);
286 level = adev->pm.dpm.forced_level;
288 pm_runtime_mark_last_busy(ddev->dev);
289 pm_runtime_put_autosuspend(ddev->dev);
291 return sysfs_emit(buf, "%s\n",
292 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
293 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
294 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
295 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
296 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
297 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
298 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
299 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
300 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
304 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
305 struct device_attribute *attr,
309 struct drm_device *ddev = dev_get_drvdata(dev);
310 struct amdgpu_device *adev = drm_to_adev(ddev);
311 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
312 enum amd_dpm_forced_level level;
313 enum amd_dpm_forced_level current_level;
316 if (amdgpu_in_reset(adev))
318 if (adev->in_suspend && !adev->in_runpm)
321 if (strncmp("low", buf, strlen("low")) == 0) {
322 level = AMD_DPM_FORCED_LEVEL_LOW;
323 } else if (strncmp("high", buf, strlen("high")) == 0) {
324 level = AMD_DPM_FORCED_LEVEL_HIGH;
325 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
326 level = AMD_DPM_FORCED_LEVEL_AUTO;
327 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
328 level = AMD_DPM_FORCED_LEVEL_MANUAL;
329 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
330 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
331 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
332 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
333 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
334 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
335 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
336 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
337 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
338 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
339 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
340 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
345 ret = pm_runtime_get_sync(ddev->dev);
347 pm_runtime_put_autosuspend(ddev->dev);
351 if (pp_funcs->get_performance_level)
352 current_level = amdgpu_dpm_get_performance_level(adev);
354 current_level = adev->pm.dpm.forced_level;
356 if (current_level == level) {
357 pm_runtime_mark_last_busy(ddev->dev);
358 pm_runtime_put_autosuspend(ddev->dev);
362 if (adev->asic_type == CHIP_RAVEN) {
363 if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
364 if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
365 amdgpu_gfx_off_ctrl(adev, false);
366 else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
367 amdgpu_gfx_off_ctrl(adev, true);
371 /* profile_exit setting is valid only when current mode is in profile mode */
372 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
373 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
374 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
375 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
376 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
377 pr_err("Currently not in any profile mode!\n");
378 pm_runtime_mark_last_busy(ddev->dev);
379 pm_runtime_put_autosuspend(ddev->dev);
383 if (pp_funcs->force_performance_level) {
384 mutex_lock(&adev->pm.mutex);
385 if (adev->pm.dpm.thermal_active) {
386 mutex_unlock(&adev->pm.mutex);
387 pm_runtime_mark_last_busy(ddev->dev);
388 pm_runtime_put_autosuspend(ddev->dev);
391 ret = amdgpu_dpm_force_performance_level(adev, level);
393 mutex_unlock(&adev->pm.mutex);
394 pm_runtime_mark_last_busy(ddev->dev);
395 pm_runtime_put_autosuspend(ddev->dev);
398 adev->pm.dpm.forced_level = level;
400 mutex_unlock(&adev->pm.mutex);
402 pm_runtime_mark_last_busy(ddev->dev);
403 pm_runtime_put_autosuspend(ddev->dev);
408 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
409 struct device_attribute *attr,
412 struct drm_device *ddev = dev_get_drvdata(dev);
413 struct amdgpu_device *adev = drm_to_adev(ddev);
414 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
415 struct pp_states_info data;
419 if (amdgpu_in_reset(adev))
421 if (adev->in_suspend && !adev->in_runpm)
424 ret = pm_runtime_get_sync(ddev->dev);
426 pm_runtime_put_autosuspend(ddev->dev);
430 if (pp_funcs->get_pp_num_states) {
431 amdgpu_dpm_get_pp_num_states(adev, &data);
433 memset(&data, 0, sizeof(data));
436 pm_runtime_mark_last_busy(ddev->dev);
437 pm_runtime_put_autosuspend(ddev->dev);
439 buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
440 for (i = 0; i < data.nums; i++)
441 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
442 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
443 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
444 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
445 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
450 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
451 struct device_attribute *attr,
454 struct drm_device *ddev = dev_get_drvdata(dev);
455 struct amdgpu_device *adev = drm_to_adev(ddev);
456 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
457 struct pp_states_info data = {0};
458 enum amd_pm_state_type pm = 0;
461 if (amdgpu_in_reset(adev))
463 if (adev->in_suspend && !adev->in_runpm)
466 ret = pm_runtime_get_sync(ddev->dev);
468 pm_runtime_put_autosuspend(ddev->dev);
472 if (pp_funcs->get_current_power_state
473 && pp_funcs->get_pp_num_states) {
474 pm = amdgpu_dpm_get_current_power_state(adev);
475 amdgpu_dpm_get_pp_num_states(adev, &data);
478 pm_runtime_mark_last_busy(ddev->dev);
479 pm_runtime_put_autosuspend(ddev->dev);
481 for (i = 0; i < data.nums; i++) {
482 if (pm == data.states[i])
489 return sysfs_emit(buf, "%d\n", i);
492 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
493 struct device_attribute *attr,
496 struct drm_device *ddev = dev_get_drvdata(dev);
497 struct amdgpu_device *adev = drm_to_adev(ddev);
499 if (amdgpu_in_reset(adev))
501 if (adev->in_suspend && !adev->in_runpm)
504 if (adev->pp_force_state_enabled)
505 return amdgpu_get_pp_cur_state(dev, attr, buf);
507 return sysfs_emit(buf, "\n");
510 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
511 struct device_attribute *attr,
515 struct drm_device *ddev = dev_get_drvdata(dev);
516 struct amdgpu_device *adev = drm_to_adev(ddev);
517 enum amd_pm_state_type state = 0;
521 if (amdgpu_in_reset(adev))
523 if (adev->in_suspend && !adev->in_runpm)
526 if (strlen(buf) == 1)
527 adev->pp_force_state_enabled = false;
528 else if (is_support_sw_smu(adev))
529 adev->pp_force_state_enabled = false;
530 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
531 adev->powerplay.pp_funcs->get_pp_num_states) {
532 struct pp_states_info data;
534 ret = kstrtoul(buf, 0, &idx);
535 if (ret || idx >= ARRAY_SIZE(data.states))
538 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
540 amdgpu_dpm_get_pp_num_states(adev, &data);
541 state = data.states[idx];
543 ret = pm_runtime_get_sync(ddev->dev);
545 pm_runtime_put_autosuspend(ddev->dev);
549 /* only set user selected power states */
550 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
551 state != POWER_STATE_TYPE_DEFAULT) {
552 amdgpu_dpm_dispatch_task(adev,
553 AMD_PP_TASK_ENABLE_USER_STATE, &state);
554 adev->pp_force_state_enabled = true;
556 pm_runtime_mark_last_busy(ddev->dev);
557 pm_runtime_put_autosuspend(ddev->dev);
566 * The amdgpu driver provides a sysfs API for uploading new powerplay
567 * tables. The file pp_table is used for this. Reading the file
568 * will dump the current power play table. Writing to the file
569 * will attempt to upload a new powerplay table and re-initialize
570 * powerplay using that new table.
574 static ssize_t amdgpu_get_pp_table(struct device *dev,
575 struct device_attribute *attr,
578 struct drm_device *ddev = dev_get_drvdata(dev);
579 struct amdgpu_device *adev = drm_to_adev(ddev);
583 if (amdgpu_in_reset(adev))
585 if (adev->in_suspend && !adev->in_runpm)
588 ret = pm_runtime_get_sync(ddev->dev);
590 pm_runtime_put_autosuspend(ddev->dev);
594 if (adev->powerplay.pp_funcs->get_pp_table) {
595 size = amdgpu_dpm_get_pp_table(adev, &table);
596 pm_runtime_mark_last_busy(ddev->dev);
597 pm_runtime_put_autosuspend(ddev->dev);
601 pm_runtime_mark_last_busy(ddev->dev);
602 pm_runtime_put_autosuspend(ddev->dev);
606 if (size >= PAGE_SIZE)
607 size = PAGE_SIZE - 1;
609 memcpy(buf, table, size);
614 static ssize_t amdgpu_set_pp_table(struct device *dev,
615 struct device_attribute *attr,
619 struct drm_device *ddev = dev_get_drvdata(dev);
620 struct amdgpu_device *adev = drm_to_adev(ddev);
623 if (amdgpu_in_reset(adev))
625 if (adev->in_suspend && !adev->in_runpm)
628 ret = pm_runtime_get_sync(ddev->dev);
630 pm_runtime_put_autosuspend(ddev->dev);
634 ret = amdgpu_dpm_set_pp_table(adev, buf, count);
636 pm_runtime_mark_last_busy(ddev->dev);
637 pm_runtime_put_autosuspend(ddev->dev);
641 pm_runtime_mark_last_busy(ddev->dev);
642 pm_runtime_put_autosuspend(ddev->dev);
648 * DOC: pp_od_clk_voltage
650 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
651 * in each power level within a power state. The pp_od_clk_voltage is used for
654 * Note that the actual memory controller clock rate are exposed, not
655 * the effective memory clock of the DRAMs. To translate it, use the
658 * Clock conversion (Mhz):
660 * HBM: effective_memory_clock = memory_controller_clock * 1
662 * G5: effective_memory_clock = memory_controller_clock * 1
664 * G6: effective_memory_clock = memory_controller_clock * 2
666 * DRAM data rate (MT/s):
668 * HBM: effective_memory_clock * 2 = data_rate
670 * G5: effective_memory_clock * 4 = data_rate
672 * G6: effective_memory_clock * 8 = data_rate
676 * data_rate * vram_bit_width / 8 = memory_bandwidth
682 * memory_controller_clock = 1750 Mhz
684 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
686 * data rate = 1750 * 4 = 7000 MT/s
688 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
692 * memory_controller_clock = 875 Mhz
694 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
696 * data rate = 1750 * 8 = 14000 MT/s
698 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
700 * < For Vega10 and previous ASICs >
702 * Reading the file will display:
704 * - a list of engine clock levels and voltages labeled OD_SCLK
706 * - a list of memory clock levels and voltages labeled OD_MCLK
708 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
710 * To manually adjust these settings, first select manual using
711 * power_dpm_force_performance_level. Enter a new value for each
712 * level by writing a string that contains "s/m level clock voltage" to
713 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
714 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
715 * 810 mV. When you have edited all of the states as needed, write
716 * "c" (commit) to the file to commit your changes. If you want to reset to the
717 * default power levels, write "r" (reset) to the file to reset them.
720 * < For Vega20 and newer ASICs >
722 * Reading the file will display:
724 * - minimum and maximum engine clock labeled OD_SCLK
726 * - minimum(not available for Vega20 and Navi1x) and maximum memory
727 * clock labeled OD_MCLK
729 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
730 * They can be used to calibrate the sclk voltage curve.
732 * - voltage offset(in mV) applied on target voltage calculation.
733 * This is available for Sienna Cichlid, Navy Flounder and Dimgrey
734 * Cavefish. For these ASICs, the target voltage calculation can be
735 * illustrated by "voltage = voltage calculated from v/f curve +
736 * overdrive vddgfx offset"
738 * - a list of valid ranges for sclk, mclk, and voltage curve points
743 * Reading the file will display:
745 * - minimum and maximum engine clock labeled OD_SCLK
747 * - a list of valid ranges for sclk labeled OD_RANGE
751 * Reading the file will display:
753 * - minimum and maximum engine clock labeled OD_SCLK
754 * - minimum and maximum core clocks labeled OD_CCLK
756 * - a list of valid ranges for sclk and cclk labeled OD_RANGE
758 * To manually adjust these settings:
760 * - First select manual using power_dpm_force_performance_level
762 * - For clock frequency setting, enter a new value by writing a
763 * string that contains "s/m index clock" to the file. The index
764 * should be 0 if to set minimum clock. And 1 if to set maximum
765 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
766 * "m 1 800" will update maximum mclk to be 800Mhz. For core
767 * clocks on VanGogh, the string contains "p core index clock".
768 * E.g., "p 2 0 800" would set the minimum core clock on core
771 * For sclk voltage curve, enter the new values by writing a
772 * string that contains "vc point clock voltage" to the file. The
773 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
774 * update point1 with clock set as 300Mhz and voltage as
775 * 600mV. "vc 2 1000 1000" will update point3 with clock set
776 * as 1000Mhz and voltage 1000mV.
778 * To update the voltage offset applied for gfxclk/voltage calculation,
779 * enter the new value by writing a string that contains "vo offset".
780 * This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
781 * And the offset can be a positive or negative value.
783 * - When you have edited all of the states as needed, write "c" (commit)
784 * to the file to commit your changes
786 * - If you want to reset to the default power levels, write "r" (reset)
787 * to the file to reset them
791 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
792 struct device_attribute *attr,
796 struct drm_device *ddev = dev_get_drvdata(dev);
797 struct amdgpu_device *adev = drm_to_adev(ddev);
799 uint32_t parameter_size = 0;
804 const char delimiter[3] = {' ', '\n', '\0'};
807 if (amdgpu_in_reset(adev))
809 if (adev->in_suspend && !adev->in_runpm)
816 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
817 else if (*buf == 'p')
818 type = PP_OD_EDIT_CCLK_VDDC_TABLE;
819 else if (*buf == 'm')
820 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
822 type = PP_OD_RESTORE_DEFAULT_TABLE;
823 else if (*buf == 'c')
824 type = PP_OD_COMMIT_DPM_TABLE;
825 else if (!strncmp(buf, "vc", 2))
826 type = PP_OD_EDIT_VDDC_CURVE;
827 else if (!strncmp(buf, "vo", 2))
828 type = PP_OD_EDIT_VDDGFX_OFFSET;
832 memcpy(buf_cpy, buf, count+1);
836 if ((type == PP_OD_EDIT_VDDC_CURVE) ||
837 (type == PP_OD_EDIT_VDDGFX_OFFSET))
839 while (isspace(*++tmp_str));
841 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
842 if (strlen(sub_str) == 0)
844 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
849 while (isspace(*tmp_str))
853 ret = pm_runtime_get_sync(ddev->dev);
855 pm_runtime_put_autosuspend(ddev->dev);
859 if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) {
860 ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type,
864 pm_runtime_mark_last_busy(ddev->dev);
865 pm_runtime_put_autosuspend(ddev->dev);
870 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
871 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
872 parameter, parameter_size);
874 pm_runtime_mark_last_busy(ddev->dev);
875 pm_runtime_put_autosuspend(ddev->dev);
880 if (type == PP_OD_COMMIT_DPM_TABLE) {
881 if (adev->powerplay.pp_funcs->dispatch_tasks) {
882 amdgpu_dpm_dispatch_task(adev,
883 AMD_PP_TASK_READJUST_POWER_STATE,
885 pm_runtime_mark_last_busy(ddev->dev);
886 pm_runtime_put_autosuspend(ddev->dev);
889 pm_runtime_mark_last_busy(ddev->dev);
890 pm_runtime_put_autosuspend(ddev->dev);
895 pm_runtime_mark_last_busy(ddev->dev);
896 pm_runtime_put_autosuspend(ddev->dev);
901 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
902 struct device_attribute *attr,
905 struct drm_device *ddev = dev_get_drvdata(dev);
906 struct amdgpu_device *adev = drm_to_adev(ddev);
910 if (amdgpu_in_reset(adev))
912 if (adev->in_suspend && !adev->in_runpm)
915 ret = pm_runtime_get_sync(ddev->dev);
917 pm_runtime_put_autosuspend(ddev->dev);
921 if (adev->powerplay.pp_funcs->print_clock_levels) {
922 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
923 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
924 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
925 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf+size);
926 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
927 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf+size);
929 size = sysfs_emit(buf, "\n");
931 pm_runtime_mark_last_busy(ddev->dev);
932 pm_runtime_put_autosuspend(ddev->dev);
940 * The amdgpu driver provides a sysfs API for adjusting what powerplay
941 * features to be enabled. The file pp_features is used for this. And
942 * this is only available for Vega10 and later dGPUs.
944 * Reading back the file will show you the followings:
945 * - Current ppfeature masks
946 * - List of the all supported powerplay features with their naming,
947 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
949 * To manually enable or disable a specific feature, just set or clear
950 * the corresponding bit from original ppfeature masks and input the
951 * new ppfeature masks.
953 static ssize_t amdgpu_set_pp_features(struct device *dev,
954 struct device_attribute *attr,
958 struct drm_device *ddev = dev_get_drvdata(dev);
959 struct amdgpu_device *adev = drm_to_adev(ddev);
960 uint64_t featuremask;
963 if (amdgpu_in_reset(adev))
965 if (adev->in_suspend && !adev->in_runpm)
968 ret = kstrtou64(buf, 0, &featuremask);
972 ret = pm_runtime_get_sync(ddev->dev);
974 pm_runtime_put_autosuspend(ddev->dev);
978 if (adev->powerplay.pp_funcs->set_ppfeature_status) {
979 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
981 pm_runtime_mark_last_busy(ddev->dev);
982 pm_runtime_put_autosuspend(ddev->dev);
986 pm_runtime_mark_last_busy(ddev->dev);
987 pm_runtime_put_autosuspend(ddev->dev);
992 static ssize_t amdgpu_get_pp_features(struct device *dev,
993 struct device_attribute *attr,
996 struct drm_device *ddev = dev_get_drvdata(dev);
997 struct amdgpu_device *adev = drm_to_adev(ddev);
1001 if (amdgpu_in_reset(adev))
1003 if (adev->in_suspend && !adev->in_runpm)
1006 ret = pm_runtime_get_sync(ddev->dev);
1008 pm_runtime_put_autosuspend(ddev->dev);
1012 if (adev->powerplay.pp_funcs->get_ppfeature_status)
1013 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
1015 size = sysfs_emit(buf, "\n");
1017 pm_runtime_mark_last_busy(ddev->dev);
1018 pm_runtime_put_autosuspend(ddev->dev);
1024 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
1026 * The amdgpu driver provides a sysfs API for adjusting what power levels
1027 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
1028 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
1031 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
1032 * Vega10 and later ASICs.
1033 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
1035 * Reading back the files will show you the available power levels within
1036 * the power state and the clock information for those levels.
1038 * To manually adjust these states, first select manual using
1039 * power_dpm_force_performance_level.
1040 * Secondly, enter a new value for each level by inputing a string that
1041 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1044 * .. code-block:: bash
1046 * echo "4 5 6" > pp_dpm_sclk
1048 * will enable sclk levels 4, 5, and 6.
1050 * NOTE: change to the dcefclk max dpm level is not supported now
1053 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
1054 enum pp_clock_type type,
1057 struct drm_device *ddev = dev_get_drvdata(dev);
1058 struct amdgpu_device *adev = drm_to_adev(ddev);
1062 if (amdgpu_in_reset(adev))
1064 if (adev->in_suspend && !adev->in_runpm)
1067 ret = pm_runtime_get_sync(ddev->dev);
1069 pm_runtime_put_autosuspend(ddev->dev);
1073 if (adev->powerplay.pp_funcs->print_clock_levels)
1074 size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1076 size = sysfs_emit(buf, "\n");
1078 pm_runtime_mark_last_busy(ddev->dev);
1079 pm_runtime_put_autosuspend(ddev->dev);
1085 * Worst case: 32 bits individually specified, in octal at 12 characters
1086 * per line (+1 for \n).
1088 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1090 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1093 unsigned long level;
1094 char *sub_str = NULL;
1096 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1097 const char delimiter[3] = {' ', '\n', '\0'};
1102 bytes = min(count, sizeof(buf_cpy) - 1);
1103 memcpy(buf_cpy, buf, bytes);
1104 buf_cpy[bytes] = '\0';
1106 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1107 if (strlen(sub_str)) {
1108 ret = kstrtoul(sub_str, 0, &level);
1109 if (ret || level > 31)
1111 *mask |= 1 << level;
1119 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1120 enum pp_clock_type type,
1124 struct drm_device *ddev = dev_get_drvdata(dev);
1125 struct amdgpu_device *adev = drm_to_adev(ddev);
1129 if (amdgpu_in_reset(adev))
1131 if (adev->in_suspend && !adev->in_runpm)
1134 ret = amdgpu_read_mask(buf, count, &mask);
1138 ret = pm_runtime_get_sync(ddev->dev);
1140 pm_runtime_put_autosuspend(ddev->dev);
1144 if (adev->powerplay.pp_funcs->force_clock_level)
1145 ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1149 pm_runtime_mark_last_busy(ddev->dev);
1150 pm_runtime_put_autosuspend(ddev->dev);
1158 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1159 struct device_attribute *attr,
1162 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1165 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1166 struct device_attribute *attr,
1170 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1173 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1174 struct device_attribute *attr,
1177 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1180 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1181 struct device_attribute *attr,
1185 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1188 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1189 struct device_attribute *attr,
1192 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1195 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1196 struct device_attribute *attr,
1200 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1203 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1204 struct device_attribute *attr,
1207 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1210 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1211 struct device_attribute *attr,
1215 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1218 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1219 struct device_attribute *attr,
1222 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
1225 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1226 struct device_attribute *attr,
1230 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
1233 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1234 struct device_attribute *attr,
1237 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
1240 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1241 struct device_attribute *attr,
1245 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
1248 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1249 struct device_attribute *attr,
1252 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1255 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1256 struct device_attribute *attr,
1260 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1263 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1264 struct device_attribute *attr,
1267 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1270 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1271 struct device_attribute *attr,
1275 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1278 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1279 struct device_attribute *attr,
1282 struct drm_device *ddev = dev_get_drvdata(dev);
1283 struct amdgpu_device *adev = drm_to_adev(ddev);
1287 if (amdgpu_in_reset(adev))
1289 if (adev->in_suspend && !adev->in_runpm)
1292 ret = pm_runtime_get_sync(ddev->dev);
1294 pm_runtime_put_autosuspend(ddev->dev);
1298 if (is_support_sw_smu(adev))
1300 else if (adev->powerplay.pp_funcs->get_sclk_od)
1301 value = amdgpu_dpm_get_sclk_od(adev);
1303 pm_runtime_mark_last_busy(ddev->dev);
1304 pm_runtime_put_autosuspend(ddev->dev);
1306 return sysfs_emit(buf, "%d\n", value);
1309 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1310 struct device_attribute *attr,
1314 struct drm_device *ddev = dev_get_drvdata(dev);
1315 struct amdgpu_device *adev = drm_to_adev(ddev);
1319 if (amdgpu_in_reset(adev))
1321 if (adev->in_suspend && !adev->in_runpm)
1324 ret = kstrtol(buf, 0, &value);
1329 ret = pm_runtime_get_sync(ddev->dev);
1331 pm_runtime_put_autosuspend(ddev->dev);
1335 if (is_support_sw_smu(adev)) {
1338 if (adev->powerplay.pp_funcs->set_sclk_od)
1339 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1341 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1342 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1344 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1345 amdgpu_pm_compute_clocks(adev);
1349 pm_runtime_mark_last_busy(ddev->dev);
1350 pm_runtime_put_autosuspend(ddev->dev);
1355 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1356 struct device_attribute *attr,
1359 struct drm_device *ddev = dev_get_drvdata(dev);
1360 struct amdgpu_device *adev = drm_to_adev(ddev);
1364 if (amdgpu_in_reset(adev))
1366 if (adev->in_suspend && !adev->in_runpm)
1369 ret = pm_runtime_get_sync(ddev->dev);
1371 pm_runtime_put_autosuspend(ddev->dev);
1375 if (is_support_sw_smu(adev))
1377 else if (adev->powerplay.pp_funcs->get_mclk_od)
1378 value = amdgpu_dpm_get_mclk_od(adev);
1380 pm_runtime_mark_last_busy(ddev->dev);
1381 pm_runtime_put_autosuspend(ddev->dev);
1383 return sysfs_emit(buf, "%d\n", value);
1386 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1387 struct device_attribute *attr,
1391 struct drm_device *ddev = dev_get_drvdata(dev);
1392 struct amdgpu_device *adev = drm_to_adev(ddev);
1396 if (amdgpu_in_reset(adev))
1398 if (adev->in_suspend && !adev->in_runpm)
1401 ret = kstrtol(buf, 0, &value);
1406 ret = pm_runtime_get_sync(ddev->dev);
1408 pm_runtime_put_autosuspend(ddev->dev);
1412 if (is_support_sw_smu(adev)) {
1415 if (adev->powerplay.pp_funcs->set_mclk_od)
1416 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1418 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1419 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1421 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1422 amdgpu_pm_compute_clocks(adev);
1426 pm_runtime_mark_last_busy(ddev->dev);
1427 pm_runtime_put_autosuspend(ddev->dev);
1433 * DOC: pp_power_profile_mode
1435 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1436 * related to switching between power levels in a power state. The file
1437 * pp_power_profile_mode is used for this.
1439 * Reading this file outputs a list of all of the predefined power profiles
1440 * and the relevant heuristics settings for that profile.
1442 * To select a profile or create a custom profile, first select manual using
1443 * power_dpm_force_performance_level. Writing the number of a predefined
1444 * profile to pp_power_profile_mode will enable those heuristics. To
1445 * create a custom set of heuristics, write a string of numbers to the file
1446 * starting with the number of the custom profile along with a setting
1447 * for each heuristic parameter. Due to differences across asic families
1448 * the heuristic parameters vary from family to family.
1452 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1453 struct device_attribute *attr,
1456 struct drm_device *ddev = dev_get_drvdata(dev);
1457 struct amdgpu_device *adev = drm_to_adev(ddev);
1461 if (amdgpu_in_reset(adev))
1463 if (adev->in_suspend && !adev->in_runpm)
1466 ret = pm_runtime_get_sync(ddev->dev);
1468 pm_runtime_put_autosuspend(ddev->dev);
1472 if (adev->powerplay.pp_funcs->get_power_profile_mode)
1473 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1475 size = sysfs_emit(buf, "\n");
1477 pm_runtime_mark_last_busy(ddev->dev);
1478 pm_runtime_put_autosuspend(ddev->dev);
1484 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1485 struct device_attribute *attr,
1490 struct drm_device *ddev = dev_get_drvdata(dev);
1491 struct amdgpu_device *adev = drm_to_adev(ddev);
1492 uint32_t parameter_size = 0;
1494 char *sub_str, buf_cpy[128];
1498 long int profile_mode = 0;
1499 const char delimiter[3] = {' ', '\n', '\0'};
1501 if (amdgpu_in_reset(adev))
1503 if (adev->in_suspend && !adev->in_runpm)
1508 ret = kstrtol(tmp, 0, &profile_mode);
1512 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1513 if (count < 2 || count > 127)
1515 while (isspace(*++buf))
1517 memcpy(buf_cpy, buf, count-i);
1519 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1520 if (strlen(sub_str) == 0)
1522 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1526 while (isspace(*tmp_str))
1530 parameter[parameter_size] = profile_mode;
1532 ret = pm_runtime_get_sync(ddev->dev);
1534 pm_runtime_put_autosuspend(ddev->dev);
1538 if (adev->powerplay.pp_funcs->set_power_profile_mode)
1539 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1541 pm_runtime_mark_last_busy(ddev->dev);
1542 pm_runtime_put_autosuspend(ddev->dev);
1551 * DOC: gpu_busy_percent
1553 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1554 * is as a percentage. The file gpu_busy_percent is used for this.
1555 * The SMU firmware computes a percentage of load based on the
1556 * aggregate activity level in the IP cores.
1558 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1559 struct device_attribute *attr,
1562 struct drm_device *ddev = dev_get_drvdata(dev);
1563 struct amdgpu_device *adev = drm_to_adev(ddev);
1564 int r, value, size = sizeof(value);
1566 if (amdgpu_in_reset(adev))
1568 if (adev->in_suspend && !adev->in_runpm)
1571 r = pm_runtime_get_sync(ddev->dev);
1573 pm_runtime_put_autosuspend(ddev->dev);
1577 /* read the IP busy sensor */
1578 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1579 (void *)&value, &size);
1581 pm_runtime_mark_last_busy(ddev->dev);
1582 pm_runtime_put_autosuspend(ddev->dev);
1587 return sysfs_emit(buf, "%d\n", value);
1591 * DOC: mem_busy_percent
1593 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1594 * is as a percentage. The file mem_busy_percent is used for this.
1595 * The SMU firmware computes a percentage of load based on the
1596 * aggregate activity level in the IP cores.
1598 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1599 struct device_attribute *attr,
1602 struct drm_device *ddev = dev_get_drvdata(dev);
1603 struct amdgpu_device *adev = drm_to_adev(ddev);
1604 int r, value, size = sizeof(value);
1606 if (amdgpu_in_reset(adev))
1608 if (adev->in_suspend && !adev->in_runpm)
1611 r = pm_runtime_get_sync(ddev->dev);
1613 pm_runtime_put_autosuspend(ddev->dev);
1617 /* read the IP busy sensor */
1618 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1619 (void *)&value, &size);
1621 pm_runtime_mark_last_busy(ddev->dev);
1622 pm_runtime_put_autosuspend(ddev->dev);
1627 return sysfs_emit(buf, "%d\n", value);
1633 * The amdgpu driver provides a sysfs API for estimating how much data
1634 * has been received and sent by the GPU in the last second through PCIe.
1635 * The file pcie_bw is used for this.
1636 * The Perf counters count the number of received and sent messages and return
1637 * those values, as well as the maximum payload size of a PCIe packet (mps).
1638 * Note that it is not possible to easily and quickly obtain the size of each
1639 * packet transmitted, so we output the max payload size (mps) to allow for
1640 * quick estimation of the PCIe bandwidth usage
1642 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1643 struct device_attribute *attr,
1646 struct drm_device *ddev = dev_get_drvdata(dev);
1647 struct amdgpu_device *adev = drm_to_adev(ddev);
1648 uint64_t count0 = 0, count1 = 0;
1651 if (amdgpu_in_reset(adev))
1653 if (adev->in_suspend && !adev->in_runpm)
1656 if (adev->flags & AMD_IS_APU)
1659 if (!adev->asic_funcs->get_pcie_usage)
1662 ret = pm_runtime_get_sync(ddev->dev);
1664 pm_runtime_put_autosuspend(ddev->dev);
1668 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1670 pm_runtime_mark_last_busy(ddev->dev);
1671 pm_runtime_put_autosuspend(ddev->dev);
1673 return sysfs_emit(buf, "%llu %llu %i\n",
1674 count0, count1, pcie_get_mps(adev->pdev));
1680 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1681 * The file unique_id is used for this.
1682 * This will provide a Unique ID that will persist from machine to machine
1684 * NOTE: This will only work for GFX9 and newer. This file will be absent
1685 * on unsupported ASICs (GFX8 and older)
1687 static ssize_t amdgpu_get_unique_id(struct device *dev,
1688 struct device_attribute *attr,
1691 struct drm_device *ddev = dev_get_drvdata(dev);
1692 struct amdgpu_device *adev = drm_to_adev(ddev);
1694 if (amdgpu_in_reset(adev))
1696 if (adev->in_suspend && !adev->in_runpm)
1699 if (adev->unique_id)
1700 return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1706 * DOC: thermal_throttling_logging
1708 * Thermal throttling pulls down the clock frequency and thus the performance.
1709 * It's an useful mechanism to protect the chip from overheating. Since it
1710 * impacts performance, the user controls whether it is enabled and if so,
1711 * the log frequency.
1713 * Reading back the file shows you the status(enabled or disabled) and
1714 * the interval(in seconds) between each thermal logging.
1716 * Writing an integer to the file, sets a new logging interval, in seconds.
1717 * The value should be between 1 and 3600. If the value is less than 1,
1718 * thermal logging is disabled. Values greater than 3600 are ignored.
1720 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1721 struct device_attribute *attr,
1724 struct drm_device *ddev = dev_get_drvdata(dev);
1725 struct amdgpu_device *adev = drm_to_adev(ddev);
1727 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1728 adev_to_drm(adev)->unique,
1729 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1730 adev->throttling_logging_rs.interval / HZ + 1);
1733 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1734 struct device_attribute *attr,
1738 struct drm_device *ddev = dev_get_drvdata(dev);
1739 struct amdgpu_device *adev = drm_to_adev(ddev);
1740 long throttling_logging_interval;
1741 unsigned long flags;
1744 ret = kstrtol(buf, 0, &throttling_logging_interval);
1748 if (throttling_logging_interval > 3600)
1751 if (throttling_logging_interval > 0) {
1752 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1754 * Reset the ratelimit timer internals.
1755 * This can effectively restart the timer.
1757 adev->throttling_logging_rs.interval =
1758 (throttling_logging_interval - 1) * HZ;
1759 adev->throttling_logging_rs.begin = 0;
1760 adev->throttling_logging_rs.printed = 0;
1761 adev->throttling_logging_rs.missed = 0;
1762 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1764 atomic_set(&adev->throttling_logging_enabled, 1);
1766 atomic_set(&adev->throttling_logging_enabled, 0);
1775 * The amdgpu driver provides a sysfs API for retrieving current gpu
1776 * metrics data. The file gpu_metrics is used for this. Reading the
1777 * file will dump all the current gpu metrics data.
1779 * These data include temperature, frequency, engines utilization,
1780 * power consume, throttler status, fan speed and cpu core statistics(
1781 * available for APU only). That's it will give a snapshot of all sensors
1784 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1785 struct device_attribute *attr,
1788 struct drm_device *ddev = dev_get_drvdata(dev);
1789 struct amdgpu_device *adev = drm_to_adev(ddev);
1794 if (amdgpu_in_reset(adev))
1796 if (adev->in_suspend && !adev->in_runpm)
1799 ret = pm_runtime_get_sync(ddev->dev);
1801 pm_runtime_put_autosuspend(ddev->dev);
1805 if (adev->powerplay.pp_funcs->get_gpu_metrics)
1806 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1811 if (size >= PAGE_SIZE)
1812 size = PAGE_SIZE - 1;
1814 memcpy(buf, gpu_metrics, size);
1817 pm_runtime_mark_last_busy(ddev->dev);
1818 pm_runtime_put_autosuspend(ddev->dev);
1824 * DOC: smartshift_apu_power
1826 * The amdgpu driver provides a sysfs API for reporting APU power
1827 * share if it supports smartshift. The value is expressed as
1828 * the proportion of stapm limit where stapm limit is the total APU
1829 * power limit. The result is in percentage. If APU power is 130% of
1830 * STAPM, then APU is using 30% of the dGPU's headroom.
1833 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1836 struct drm_device *ddev = dev_get_drvdata(dev);
1837 struct amdgpu_device *adev = drm_to_adev(ddev);
1838 uint32_t ss_power, size;
1841 if (amdgpu_in_reset(adev))
1843 if (adev->in_suspend && !adev->in_runpm)
1846 r = pm_runtime_get_sync(ddev->dev);
1848 pm_runtime_put_autosuspend(ddev->dev);
1852 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1853 (void *)&ss_power, &size);
1857 r = sysfs_emit(buf, "%u%%\n", ss_power);
1860 pm_runtime_mark_last_busy(ddev->dev);
1861 pm_runtime_put_autosuspend(ddev->dev);
1866 * DOC: smartshift_dgpu_power
1868 * The amdgpu driver provides a sysfs API for reporting the dGPU power
1869 * share if the device is in HG and supports smartshift. The value
1870 * is expressed as the proportion of stapm limit where stapm limit
1871 * is the total APU power limit. The value is in percentage. If dGPU
1872 * power is 20% higher than STAPM power(120%), it's using 20% of the
1873 * APU's power headroom.
1876 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1879 struct drm_device *ddev = dev_get_drvdata(dev);
1880 struct amdgpu_device *adev = drm_to_adev(ddev);
1881 uint32_t ss_power, size;
1884 if (amdgpu_in_reset(adev))
1886 if (adev->in_suspend && !adev->in_runpm)
1889 r = pm_runtime_get_sync(ddev->dev);
1891 pm_runtime_put_autosuspend(ddev->dev);
1895 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1896 (void *)&ss_power, &size);
1901 r = sysfs_emit(buf, "%u%%\n", ss_power);
1904 pm_runtime_mark_last_busy(ddev->dev);
1905 pm_runtime_put_autosuspend(ddev->dev);
1910 * DOC: smartshift_bias
1912 * The amdgpu driver provides a sysfs API for reporting the
1913 * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1914 * and the default is 0. -100 sets maximum preference to APU
1915 * and 100 sets max perference to dGPU.
1918 static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1919 struct device_attribute *attr,
1924 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1929 static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1930 struct device_attribute *attr,
1931 const char *buf, size_t count)
1933 struct drm_device *ddev = dev_get_drvdata(dev);
1934 struct amdgpu_device *adev = drm_to_adev(ddev);
1938 if (amdgpu_in_reset(adev))
1940 if (adev->in_suspend && !adev->in_runpm)
1943 r = pm_runtime_get_sync(ddev->dev);
1945 pm_runtime_put_autosuspend(ddev->dev);
1949 r = kstrtoint(buf, 10, &bias);
1953 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
1954 bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
1955 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
1956 bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
1958 amdgpu_smartshift_bias = bias;
1961 /* TODO: upadte bias level with SMU message */
1964 pm_runtime_mark_last_busy(ddev->dev);
1965 pm_runtime_put_autosuspend(ddev->dev);
1970 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1971 uint32_t mask, enum amdgpu_device_attr_states *states)
1973 uint32_t ss_power, size;
1975 if (!amdgpu_acpi_is_power_shift_control_supported())
1976 *states = ATTR_STATE_UNSUPPORTED;
1977 else if ((adev->flags & AMD_IS_PX) &&
1978 !amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1979 *states = ATTR_STATE_UNSUPPORTED;
1980 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1981 (void *)&ss_power, &size))
1982 *states = ATTR_STATE_UNSUPPORTED;
1983 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1984 (void *)&ss_power, &size))
1985 *states = ATTR_STATE_UNSUPPORTED;
1990 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1991 uint32_t mask, enum amdgpu_device_attr_states *states)
1993 uint32_t ss_power, size;
1995 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1996 *states = ATTR_STATE_UNSUPPORTED;
1997 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1998 (void *)&ss_power, &size))
1999 *states = ATTR_STATE_UNSUPPORTED;
2000 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
2001 (void *)&ss_power, &size))
2002 *states = ATTR_STATE_UNSUPPORTED;
2007 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2008 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2009 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2010 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2011 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2012 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2013 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2014 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2015 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2016 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2017 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2018 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2019 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2020 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC),
2021 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC),
2022 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
2023 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
2024 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2025 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC),
2026 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2027 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2028 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
2029 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2030 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2031 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2032 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2033 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
2034 .attr_update = ss_power_attr_update),
2035 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
2036 .attr_update = ss_power_attr_update),
2037 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
2038 .attr_update = ss_bias_attr_update),
2041 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2042 uint32_t mask, enum amdgpu_device_attr_states *states)
2044 struct device_attribute *dev_attr = &attr->dev_attr;
2045 const char *attr_name = dev_attr->attr.name;
2046 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2047 enum amd_asic_type asic_type = adev->asic_type;
2049 if (!(attr->flags & mask)) {
2050 *states = ATTR_STATE_UNSUPPORTED;
2054 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name))
2056 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2057 if (asic_type < CHIP_VEGA10)
2058 *states = ATTR_STATE_UNSUPPORTED;
2059 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2060 if (asic_type < CHIP_VEGA10 ||
2061 asic_type == CHIP_ARCTURUS ||
2062 asic_type == CHIP_ALDEBARAN)
2063 *states = ATTR_STATE_UNSUPPORTED;
2064 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2065 if (asic_type < CHIP_VEGA20)
2066 *states = ATTR_STATE_UNSUPPORTED;
2067 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2068 *states = ATTR_STATE_UNSUPPORTED;
2069 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2070 (is_support_sw_smu(adev) && adev->smu.is_apu) ||
2071 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2072 *states = ATTR_STATE_SUPPORTED;
2073 } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2074 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2075 *states = ATTR_STATE_UNSUPPORTED;
2076 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2077 /* PCIe Perf counters won't work on APU nodes */
2078 if (adev->flags & AMD_IS_APU)
2079 *states = ATTR_STATE_UNSUPPORTED;
2080 } else if (DEVICE_ATTR_IS(unique_id)) {
2081 if (asic_type != CHIP_VEGA10 &&
2082 asic_type != CHIP_VEGA20 &&
2083 asic_type != CHIP_ARCTURUS)
2084 *states = ATTR_STATE_UNSUPPORTED;
2085 } else if (DEVICE_ATTR_IS(pp_features)) {
2086 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2087 *states = ATTR_STATE_UNSUPPORTED;
2088 } else if (DEVICE_ATTR_IS(gpu_metrics)) {
2089 if (asic_type < CHIP_VEGA12)
2090 *states = ATTR_STATE_UNSUPPORTED;
2091 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2092 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID))
2093 *states = ATTR_STATE_UNSUPPORTED;
2094 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2095 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID))
2096 *states = ATTR_STATE_UNSUPPORTED;
2099 switch (asic_type) {
2101 case CHIP_ALDEBARAN:
2102 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2103 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2104 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2105 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2106 dev_attr->attr.mode &= ~S_IWUGO;
2107 dev_attr->store = NULL;
2114 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2115 /* SMU MP1 does not support dcefclk level setting */
2116 if (asic_type >= CHIP_NAVI10) {
2117 dev_attr->attr.mode &= ~S_IWUGO;
2118 dev_attr->store = NULL;
2122 #undef DEVICE_ATTR_IS
2128 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2129 struct amdgpu_device_attr *attr,
2130 uint32_t mask, struct list_head *attr_list)
2133 struct device_attribute *dev_attr = &attr->dev_attr;
2134 const char *name = dev_attr->attr.name;
2135 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2136 struct amdgpu_device_attr_entry *attr_entry;
2138 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2139 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2143 attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2145 ret = attr_update(adev, attr, mask, &attr_states);
2147 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2152 if (attr_states == ATTR_STATE_UNSUPPORTED)
2155 ret = device_create_file(adev->dev, dev_attr);
2157 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2161 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2165 attr_entry->attr = attr;
2166 INIT_LIST_HEAD(&attr_entry->entry);
2168 list_add_tail(&attr_entry->entry, attr_list);
2173 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2175 struct device_attribute *dev_attr = &attr->dev_attr;
2177 device_remove_file(adev->dev, dev_attr);
2180 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2181 struct list_head *attr_list);
2183 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2184 struct amdgpu_device_attr *attrs,
2187 struct list_head *attr_list)
2192 for (i = 0; i < counts; i++) {
2193 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2201 amdgpu_device_attr_remove_groups(adev, attr_list);
2206 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2207 struct list_head *attr_list)
2209 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2211 if (list_empty(attr_list))
2214 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2215 amdgpu_device_attr_remove(adev, entry->attr);
2216 list_del(&entry->entry);
2221 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2222 struct device_attribute *attr,
2225 struct amdgpu_device *adev = dev_get_drvdata(dev);
2226 int channel = to_sensor_dev_attr(attr)->index;
2227 int r, temp = 0, size = sizeof(temp);
2229 if (amdgpu_in_reset(adev))
2231 if (adev->in_suspend && !adev->in_runpm)
2234 if (channel >= PP_TEMP_MAX)
2237 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2239 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2244 case PP_TEMP_JUNCTION:
2245 /* get current junction temperature */
2246 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2247 (void *)&temp, &size);
2250 /* get current edge temperature */
2251 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2252 (void *)&temp, &size);
2255 /* get current memory temperature */
2256 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2257 (void *)&temp, &size);
2264 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2265 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2270 return sysfs_emit(buf, "%d\n", temp);
2273 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2274 struct device_attribute *attr,
2277 struct amdgpu_device *adev = dev_get_drvdata(dev);
2278 int hyst = to_sensor_dev_attr(attr)->index;
2282 temp = adev->pm.dpm.thermal.min_temp;
2284 temp = adev->pm.dpm.thermal.max_temp;
2286 return sysfs_emit(buf, "%d\n", temp);
2289 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2290 struct device_attribute *attr,
2293 struct amdgpu_device *adev = dev_get_drvdata(dev);
2294 int hyst = to_sensor_dev_attr(attr)->index;
2298 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2300 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2302 return sysfs_emit(buf, "%d\n", temp);
2305 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2306 struct device_attribute *attr,
2309 struct amdgpu_device *adev = dev_get_drvdata(dev);
2310 int hyst = to_sensor_dev_attr(attr)->index;
2314 temp = adev->pm.dpm.thermal.min_mem_temp;
2316 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2318 return sysfs_emit(buf, "%d\n", temp);
2321 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2322 struct device_attribute *attr,
2325 int channel = to_sensor_dev_attr(attr)->index;
2327 if (channel >= PP_TEMP_MAX)
2330 return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2333 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2334 struct device_attribute *attr,
2337 struct amdgpu_device *adev = dev_get_drvdata(dev);
2338 int channel = to_sensor_dev_attr(attr)->index;
2341 if (channel >= PP_TEMP_MAX)
2345 case PP_TEMP_JUNCTION:
2346 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2349 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2352 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2356 return sysfs_emit(buf, "%d\n", temp);
2359 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2360 struct device_attribute *attr,
2363 struct amdgpu_device *adev = dev_get_drvdata(dev);
2367 if (amdgpu_in_reset(adev))
2369 if (adev->in_suspend && !adev->in_runpm)
2372 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2374 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2378 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2379 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2380 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2384 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2386 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2387 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2389 return sysfs_emit(buf, "%u\n", pwm_mode);
2392 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2393 struct device_attribute *attr,
2397 struct amdgpu_device *adev = dev_get_drvdata(dev);
2401 if (amdgpu_in_reset(adev))
2403 if (adev->in_suspend && !adev->in_runpm)
2406 err = kstrtoint(buf, 10, &value);
2410 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2412 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2416 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2417 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2418 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2422 amdgpu_dpm_set_fan_control_mode(adev, value);
2424 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2425 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2430 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2431 struct device_attribute *attr,
2434 return sysfs_emit(buf, "%i\n", 0);
2437 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2438 struct device_attribute *attr,
2441 return sysfs_emit(buf, "%i\n", 255);
2444 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2445 struct device_attribute *attr,
2446 const char *buf, size_t count)
2448 struct amdgpu_device *adev = dev_get_drvdata(dev);
2453 if (amdgpu_in_reset(adev))
2455 if (adev->in_suspend && !adev->in_runpm)
2458 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2460 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2464 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2465 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2466 pr_info("manual fan speed control should be enabled first\n");
2467 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2468 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2472 err = kstrtou32(buf, 10, &value);
2474 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2475 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2479 if (adev->powerplay.pp_funcs->set_fan_speed_pwm)
2480 err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2484 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2485 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2493 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2494 struct device_attribute *attr,
2497 struct amdgpu_device *adev = dev_get_drvdata(dev);
2501 if (amdgpu_in_reset(adev))
2503 if (adev->in_suspend && !adev->in_runpm)
2506 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2508 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2512 if (adev->powerplay.pp_funcs->get_fan_speed_pwm)
2513 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2517 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2518 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2523 return sysfs_emit(buf, "%i\n", speed);
2526 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2527 struct device_attribute *attr,
2530 struct amdgpu_device *adev = dev_get_drvdata(dev);
2534 if (amdgpu_in_reset(adev))
2536 if (adev->in_suspend && !adev->in_runpm)
2539 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2541 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2545 if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2546 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2550 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2551 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2556 return sysfs_emit(buf, "%i\n", speed);
2559 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2560 struct device_attribute *attr,
2563 struct amdgpu_device *adev = dev_get_drvdata(dev);
2565 u32 size = sizeof(min_rpm);
2568 if (amdgpu_in_reset(adev))
2570 if (adev->in_suspend && !adev->in_runpm)
2573 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2575 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2579 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2580 (void *)&min_rpm, &size);
2582 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2583 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2588 return sysfs_emit(buf, "%d\n", min_rpm);
2591 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2592 struct device_attribute *attr,
2595 struct amdgpu_device *adev = dev_get_drvdata(dev);
2597 u32 size = sizeof(max_rpm);
2600 if (amdgpu_in_reset(adev))
2602 if (adev->in_suspend && !adev->in_runpm)
2605 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2607 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2611 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2612 (void *)&max_rpm, &size);
2614 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2615 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2620 return sysfs_emit(buf, "%d\n", max_rpm);
2623 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2624 struct device_attribute *attr,
2627 struct amdgpu_device *adev = dev_get_drvdata(dev);
2631 if (amdgpu_in_reset(adev))
2633 if (adev->in_suspend && !adev->in_runpm)
2636 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2638 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2642 if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2643 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2647 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2648 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2653 return sysfs_emit(buf, "%i\n", rpm);
2656 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2660 struct amdgpu_device *adev = dev_get_drvdata(dev);
2665 if (amdgpu_in_reset(adev))
2667 if (adev->in_suspend && !adev->in_runpm)
2670 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2672 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2676 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2678 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2679 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2680 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2684 err = kstrtou32(buf, 10, &value);
2686 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2687 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2691 if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2692 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2696 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2697 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2705 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2706 struct device_attribute *attr,
2709 struct amdgpu_device *adev = dev_get_drvdata(dev);
2713 if (amdgpu_in_reset(adev))
2715 if (adev->in_suspend && !adev->in_runpm)
2718 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2720 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2724 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2725 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2726 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2730 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2732 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2733 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2735 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2738 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2739 struct device_attribute *attr,
2743 struct amdgpu_device *adev = dev_get_drvdata(dev);
2748 if (amdgpu_in_reset(adev))
2750 if (adev->in_suspend && !adev->in_runpm)
2753 err = kstrtoint(buf, 10, &value);
2758 pwm_mode = AMD_FAN_CTRL_AUTO;
2759 else if (value == 1)
2760 pwm_mode = AMD_FAN_CTRL_MANUAL;
2764 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2766 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2770 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2771 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2772 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2775 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2777 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2778 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2783 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2784 struct device_attribute *attr,
2787 struct amdgpu_device *adev = dev_get_drvdata(dev);
2789 int r, size = sizeof(vddgfx);
2791 if (amdgpu_in_reset(adev))
2793 if (adev->in_suspend && !adev->in_runpm)
2796 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2798 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2802 /* get the voltage */
2803 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2804 (void *)&vddgfx, &size);
2806 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2807 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2812 return sysfs_emit(buf, "%d\n", vddgfx);
2815 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2816 struct device_attribute *attr,
2819 return sysfs_emit(buf, "vddgfx\n");
2822 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2823 struct device_attribute *attr,
2826 struct amdgpu_device *adev = dev_get_drvdata(dev);
2828 int r, size = sizeof(vddnb);
2830 if (amdgpu_in_reset(adev))
2832 if (adev->in_suspend && !adev->in_runpm)
2835 /* only APUs have vddnb */
2836 if (!(adev->flags & AMD_IS_APU))
2839 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2841 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2845 /* get the voltage */
2846 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2847 (void *)&vddnb, &size);
2849 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2850 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2855 return sysfs_emit(buf, "%d\n", vddnb);
2858 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2859 struct device_attribute *attr,
2862 return sysfs_emit(buf, "vddnb\n");
2865 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2866 struct device_attribute *attr,
2869 struct amdgpu_device *adev = dev_get_drvdata(dev);
2871 int r, size = sizeof(u32);
2874 if (amdgpu_in_reset(adev))
2876 if (adev->in_suspend && !adev->in_runpm)
2879 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2881 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2885 /* get the voltage */
2886 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2887 (void *)&query, &size);
2889 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2890 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2895 /* convert to microwatts */
2896 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2898 return sysfs_emit(buf, "%u\n", uw);
2901 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2902 struct device_attribute *attr,
2905 return sysfs_emit(buf, "%i\n", 0);
2909 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2910 struct device_attribute *attr,
2912 enum pp_power_limit_level pp_limit_level)
2914 struct amdgpu_device *adev = dev_get_drvdata(dev);
2915 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
2916 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2921 if (amdgpu_in_reset(adev))
2923 if (adev->in_suspend && !adev->in_runpm)
2926 if ( !(pp_funcs && pp_funcs->get_power_limit))
2929 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2931 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2935 r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,
2936 pp_limit_level, power_type);
2939 size = sysfs_emit(buf, "%u\n", limit * 1000000);
2941 size = sysfs_emit(buf, "\n");
2943 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2944 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2950 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2951 struct device_attribute *attr,
2954 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
2958 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2959 struct device_attribute *attr,
2962 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2966 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
2967 struct device_attribute *attr,
2970 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
2974 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2975 struct device_attribute *attr,
2978 int limit_type = to_sensor_dev_attr(attr)->index;
2980 return sysfs_emit(buf, "%s\n",
2981 limit_type == SMU_FAST_PPT_LIMIT ? "fastPPT" : "slowPPT");
2984 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2985 struct device_attribute *attr,
2989 struct amdgpu_device *adev = dev_get_drvdata(dev);
2990 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
2991 int limit_type = to_sensor_dev_attr(attr)->index;
2995 if (amdgpu_in_reset(adev))
2997 if (adev->in_suspend && !adev->in_runpm)
3000 if (amdgpu_sriov_vf(adev))
3003 err = kstrtou32(buf, 10, &value);
3007 value = value / 1000000; /* convert to Watt */
3008 value |= limit_type << 24;
3010 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3012 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3016 if (pp_funcs && pp_funcs->set_power_limit)
3017 err = pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
3021 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3022 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3030 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3031 struct device_attribute *attr,
3034 struct amdgpu_device *adev = dev_get_drvdata(dev);
3036 int r, size = sizeof(sclk);
3038 if (amdgpu_in_reset(adev))
3040 if (adev->in_suspend && !adev->in_runpm)
3043 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3045 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3050 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3051 (void *)&sclk, &size);
3053 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3054 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3059 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3062 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3063 struct device_attribute *attr,
3066 return sysfs_emit(buf, "sclk\n");
3069 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3070 struct device_attribute *attr,
3073 struct amdgpu_device *adev = dev_get_drvdata(dev);
3075 int r, size = sizeof(mclk);
3077 if (amdgpu_in_reset(adev))
3079 if (adev->in_suspend && !adev->in_runpm)
3082 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3084 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3089 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3090 (void *)&mclk, &size);
3092 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3093 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3098 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3101 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3102 struct device_attribute *attr,
3105 return sysfs_emit(buf, "mclk\n");
3111 * The amdgpu driver exposes the following sensor interfaces:
3113 * - GPU temperature (via the on-die sensor)
3117 * - Northbridge voltage (APUs only)
3123 * - GPU gfx/compute engine clock
3125 * - GPU memory clock (dGPU only)
3127 * hwmon interfaces for GPU temperature:
3129 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3130 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3132 * - temp[1-3]_label: temperature channel label
3133 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3135 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3136 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3138 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3139 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3141 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3142 * - these are supported on SOC15 dGPUs only
3144 * hwmon interfaces for GPU voltage:
3146 * - in0_input: the voltage on the GPU in millivolts
3148 * - in1_input: the voltage on the Northbridge in millivolts
3150 * hwmon interfaces for GPU power:
3152 * - power1_average: average power used by the GPU in microWatts
3154 * - power1_cap_min: minimum cap supported in microWatts
3156 * - power1_cap_max: maximum cap supported in microWatts
3158 * - power1_cap: selected power cap in microWatts
3160 * hwmon interfaces for GPU fan:
3162 * - pwm1: pulse width modulation fan level (0-255)
3164 * - 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)
3166 * - pwm1_min: pulse width modulation fan control minimum level (0)
3168 * - pwm1_max: pulse width modulation fan control maximum level (255)
3170 * - fan1_min: a minimum value Unit: revolution/min (RPM)
3172 * - fan1_max: a maximum value Unit: revolution/max (RPM)
3174 * - fan1_input: fan speed in RPM
3176 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3178 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3180 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3181 * That will get the former one overridden.
3183 * hwmon interfaces for GPU clocks:
3185 * - freq1_input: the gfx/compute clock in hertz
3187 * - freq2_input: the memory clock in hertz
3189 * You can use hwmon tools like sensors to view this information on your system.
3193 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3194 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3195 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3196 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3197 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3198 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3199 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3200 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3201 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3202 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3203 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3204 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3205 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3206 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3207 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3208 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3209 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3210 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3211 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3212 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3213 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3214 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3215 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3216 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3217 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3218 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3219 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3220 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3221 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3222 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3223 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3224 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3225 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3226 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3227 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3228 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3229 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3230 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3231 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3232 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3233 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3234 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3235 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3236 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3238 static struct attribute *hwmon_attributes[] = {
3239 &sensor_dev_attr_temp1_input.dev_attr.attr,
3240 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3241 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3242 &sensor_dev_attr_temp2_input.dev_attr.attr,
3243 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3244 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3245 &sensor_dev_attr_temp3_input.dev_attr.attr,
3246 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3247 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3248 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3249 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3250 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3251 &sensor_dev_attr_temp1_label.dev_attr.attr,
3252 &sensor_dev_attr_temp2_label.dev_attr.attr,
3253 &sensor_dev_attr_temp3_label.dev_attr.attr,
3254 &sensor_dev_attr_pwm1.dev_attr.attr,
3255 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3256 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3257 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3258 &sensor_dev_attr_fan1_input.dev_attr.attr,
3259 &sensor_dev_attr_fan1_min.dev_attr.attr,
3260 &sensor_dev_attr_fan1_max.dev_attr.attr,
3261 &sensor_dev_attr_fan1_target.dev_attr.attr,
3262 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3263 &sensor_dev_attr_in0_input.dev_attr.attr,
3264 &sensor_dev_attr_in0_label.dev_attr.attr,
3265 &sensor_dev_attr_in1_input.dev_attr.attr,
3266 &sensor_dev_attr_in1_label.dev_attr.attr,
3267 &sensor_dev_attr_power1_average.dev_attr.attr,
3268 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3269 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3270 &sensor_dev_attr_power1_cap.dev_attr.attr,
3271 &sensor_dev_attr_power1_cap_default.dev_attr.attr,
3272 &sensor_dev_attr_power1_label.dev_attr.attr,
3273 &sensor_dev_attr_power2_average.dev_attr.attr,
3274 &sensor_dev_attr_power2_cap_max.dev_attr.attr,
3275 &sensor_dev_attr_power2_cap_min.dev_attr.attr,
3276 &sensor_dev_attr_power2_cap.dev_attr.attr,
3277 &sensor_dev_attr_power2_cap_default.dev_attr.attr,
3278 &sensor_dev_attr_power2_label.dev_attr.attr,
3279 &sensor_dev_attr_freq1_input.dev_attr.attr,
3280 &sensor_dev_attr_freq1_label.dev_attr.attr,
3281 &sensor_dev_attr_freq2_input.dev_attr.attr,
3282 &sensor_dev_attr_freq2_label.dev_attr.attr,
3286 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3287 struct attribute *attr, int index)
3289 struct device *dev = kobj_to_dev(kobj);
3290 struct amdgpu_device *adev = dev_get_drvdata(dev);
3291 umode_t effective_mode = attr->mode;
3293 /* under multi-vf mode, the hwmon attributes are all not supported */
3294 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3297 /* there is no fan under pp one vf mode */
3298 if (amdgpu_sriov_is_pp_one_vf(adev) &&
3299 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3300 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3301 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3302 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3303 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3304 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3305 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3306 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3307 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3310 /* Skip fan attributes if fan is not present */
3311 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3312 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3313 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3314 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3315 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3316 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3317 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3318 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3319 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3322 /* Skip fan attributes on APU */
3323 if ((adev->flags & AMD_IS_APU) &&
3324 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3325 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3326 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3327 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3328 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3329 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3330 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3331 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3332 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3335 /* Skip crit temp on APU */
3336 if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3337 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3338 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3341 /* Skip limit attributes if DPM is not enabled */
3342 if (!adev->pm.dpm_enabled &&
3343 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3344 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3345 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3346 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3347 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3348 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3349 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3350 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3351 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3352 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3353 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3356 if (!is_support_sw_smu(adev)) {
3357 /* mask fan attributes if we have no bindings for this asic to expose */
3358 if ((!adev->powerplay.pp_funcs->get_fan_speed_pwm &&
3359 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3360 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3361 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3362 effective_mode &= ~S_IRUGO;
3364 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3365 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3366 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3367 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3368 effective_mode &= ~S_IWUSR;
3371 if (((adev->family == AMDGPU_FAMILY_SI) ||
3372 ((adev->flags & AMD_IS_APU) &&
3373 (adev->asic_type != CHIP_VANGOGH))) && /* not implemented yet */
3374 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3375 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3376 attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3377 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3380 if (((adev->family == AMDGPU_FAMILY_SI) ||
3381 ((adev->flags & AMD_IS_APU) &&
3382 (adev->asic_type < CHIP_RENOIR))) && /* not implemented yet */
3383 (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3386 if (!is_support_sw_smu(adev)) {
3387 /* hide max/min values if we can't both query and manage the fan */
3388 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3389 !adev->powerplay.pp_funcs->get_fan_speed_pwm) &&
3390 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3391 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3392 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3393 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3396 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3397 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3398 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3399 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3403 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3404 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3405 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3406 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3409 /* only APUs have vddnb */
3410 if (!(adev->flags & AMD_IS_APU) &&
3411 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3412 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3415 /* no mclk on APUs */
3416 if ((adev->flags & AMD_IS_APU) &&
3417 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3418 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3421 /* only SOC15 dGPUs support hotspot and mem temperatures */
3422 if (((adev->flags & AMD_IS_APU) ||
3423 adev->asic_type < CHIP_VEGA10) &&
3424 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3425 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3426 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3427 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3428 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3429 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3430 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3431 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3432 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3433 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3434 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3437 /* only Vangogh has fast PPT limit and power labels */
3438 if (!(adev->asic_type == CHIP_VANGOGH) &&
3439 (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3440 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3441 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3442 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3443 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3444 attr == &sensor_dev_attr_power2_label.dev_attr.attr ||
3445 attr == &sensor_dev_attr_power1_label.dev_attr.attr))
3448 return effective_mode;
3451 static const struct attribute_group hwmon_attrgroup = {
3452 .attrs = hwmon_attributes,
3453 .is_visible = hwmon_attributes_visible,
3456 static const struct attribute_group *hwmon_groups[] = {
3461 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3466 if (adev->pm.sysfs_initialized)
3469 if (adev->pm.dpm_enabled == 0)
3472 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3474 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3477 if (IS_ERR(adev->pm.int_hwmon_dev)) {
3478 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3480 "Unable to register hwmon device: %d\n", ret);
3484 switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3485 case SRIOV_VF_MODE_ONE_VF:
3486 mask = ATTR_FLAG_ONEVF;
3488 case SRIOV_VF_MODE_MULTI_VF:
3491 case SRIOV_VF_MODE_BARE_METAL:
3493 mask = ATTR_FLAG_MASK_ALL;
3497 ret = amdgpu_device_attr_create_groups(adev,
3498 amdgpu_device_attrs,
3499 ARRAY_SIZE(amdgpu_device_attrs),
3501 &adev->pm.pm_attr_list);
3505 adev->pm.sysfs_initialized = true;
3510 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3512 if (adev->pm.dpm_enabled == 0)
3515 if (adev->pm.int_hwmon_dev)
3516 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3518 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3524 #if defined(CONFIG_DEBUG_FS)
3526 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3527 struct amdgpu_device *adev) {
3532 if (is_support_cclk_dpm(adev)) {
3533 p_val = kcalloc(adev->smu.cpu_core_num, sizeof(uint16_t),
3536 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3537 (void *)p_val, &size)) {
3538 for (i = 0; i < adev->smu.cpu_core_num; i++)
3539 seq_printf(m, "\t%u MHz (CPU%d)\n",
3547 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3550 uint64_t value64 = 0;
3555 size = sizeof(value);
3556 seq_printf(m, "GFX Clocks and Power:\n");
3558 amdgpu_debugfs_prints_cpu_info(m, adev);
3560 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3561 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3562 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3563 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3564 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3565 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3566 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3567 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3568 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3569 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3570 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3571 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3572 size = sizeof(uint32_t);
3573 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3574 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3575 size = sizeof(value);
3576 seq_printf(m, "\n");
3579 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3580 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3583 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3584 seq_printf(m, "GPU Load: %u %%\n", value);
3586 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3587 seq_printf(m, "MEM Load: %u %%\n", value);
3589 seq_printf(m, "\n");
3591 /* SMC feature mask */
3592 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3593 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3595 if (adev->asic_type > CHIP_VEGA20) {
3597 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3599 seq_printf(m, "VCN: Disabled\n");
3601 seq_printf(m, "VCN: Enabled\n");
3602 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3603 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3604 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3605 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3608 seq_printf(m, "\n");
3611 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3613 seq_printf(m, "UVD: Disabled\n");
3615 seq_printf(m, "UVD: Enabled\n");
3616 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3617 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3618 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3619 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3622 seq_printf(m, "\n");
3625 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3627 seq_printf(m, "VCE: Disabled\n");
3629 seq_printf(m, "VCE: Enabled\n");
3630 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3631 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3639 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3643 for (i = 0; clocks[i].flag; i++)
3644 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3645 (flags & clocks[i].flag) ? "On" : "Off");
3648 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3650 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3651 struct drm_device *dev = adev_to_drm(adev);
3655 if (amdgpu_in_reset(adev))
3657 if (adev->in_suspend && !adev->in_runpm)
3660 r = pm_runtime_get_sync(dev->dev);
3662 pm_runtime_put_autosuspend(dev->dev);
3666 if (!adev->pm.dpm_enabled) {
3667 seq_printf(m, "dpm not enabled\n");
3668 pm_runtime_mark_last_busy(dev->dev);
3669 pm_runtime_put_autosuspend(dev->dev);
3673 if (!is_support_sw_smu(adev) &&
3674 adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3675 mutex_lock(&adev->pm.mutex);
3676 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3677 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3679 seq_printf(m, "Debugfs support not implemented for this asic\n");
3680 mutex_unlock(&adev->pm.mutex);
3683 r = amdgpu_debugfs_pm_info_pp(m, adev);
3688 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3690 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3691 amdgpu_parse_cg_state(m, flags);
3692 seq_printf(m, "\n");
3695 pm_runtime_mark_last_busy(dev->dev);
3696 pm_runtime_put_autosuspend(dev->dev);
3701 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3704 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
3706 * Reads debug memory region allocated to PMFW
3708 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
3709 size_t size, loff_t *pos)
3711 struct amdgpu_device *adev = file_inode(f)->i_private;
3712 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
3713 void *pp_handle = adev->powerplay.pp_handle;
3714 size_t smu_prv_buf_size;
3717 if (amdgpu_in_reset(adev))
3719 if (adev->in_suspend && !adev->in_runpm)
3722 if (pp_funcs && pp_funcs->get_smu_prv_buf_details)
3723 pp_funcs->get_smu_prv_buf_details(pp_handle, &smu_prv_buf,
3728 if (!smu_prv_buf || !smu_prv_buf_size)
3731 return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
3735 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
3736 .owner = THIS_MODULE,
3737 .open = simple_open,
3738 .read = amdgpu_pm_prv_buffer_read,
3739 .llseek = default_llseek,
3744 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3746 #if defined(CONFIG_DEBUG_FS)
3747 struct drm_minor *minor = adev_to_drm(adev)->primary;
3748 struct dentry *root = minor->debugfs_root;
3750 debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3751 &amdgpu_debugfs_pm_info_fops);
3753 if (adev->pm.smu_prv_buffer_size > 0)
3754 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
3756 &amdgpu_debugfs_pm_prv_buffer_fops,
3757 adev->pm.smu_prv_buffer_size);