drm/amdgpu: fix pm sysfs node handling (v2)
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_pm.c
1 /*
2  * Copyright 2017 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Rafał Miłecki <zajec5@gmail.com>
23  *          Alex Deucher <alexdeucher@gmail.com>
24  */
25
26 #include <drm/drm_debugfs.h>
27
28 #include "amdgpu.h"
29 #include "amdgpu_drv.h"
30 #include "amdgpu_pm.h"
31 #include "amdgpu_dpm.h"
32 #include "amdgpu_display.h"
33 #include "amdgpu_smu.h"
34 #include "atom.h"
35 #include <linux/power_supply.h>
36 #include <linux/pci.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
39 #include <linux/nospec.h>
40 #include <linux/pm_runtime.h>
41 #include "hwmgr.h"
42 #define WIDTH_4K 3840
43
44 static const struct cg_flag_name clocks[] = {
45         {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
46         {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
47         {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
48         {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
49         {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
50         {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
51         {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
52         {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
53         {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
54         {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
55         {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
56         {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
57         {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
58         {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
59         {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
60         {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
61         {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
62         {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
63         {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
64         {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
65         {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
66         {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
67         {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
68         {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
69
70         {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
71         {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
72         {0, NULL},
73 };
74
75 static const struct hwmon_temp_label {
76         enum PP_HWMON_TEMP channel;
77         const char *label;
78 } temp_label[] = {
79         {PP_TEMP_EDGE, "edge"},
80         {PP_TEMP_JUNCTION, "junction"},
81         {PP_TEMP_MEM, "mem"},
82 };
83
84 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
85 {
86         if (adev->pm.dpm_enabled) {
87                 mutex_lock(&adev->pm.mutex);
88                 if (power_supply_is_system_supplied() > 0)
89                         adev->pm.ac_power = true;
90                 else
91                         adev->pm.ac_power = false;
92                 if (adev->powerplay.pp_funcs &&
93                     adev->powerplay.pp_funcs->enable_bapm)
94                         amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
95                 mutex_unlock(&adev->pm.mutex);
96
97                 if (is_support_sw_smu(adev))
98                         smu_set_ac_dc(&adev->smu);
99         }
100 }
101
102 int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor,
103                            void *data, uint32_t *size)
104 {
105         int ret = 0;
106
107         if (!data || !size)
108                 return -EINVAL;
109
110         if (is_support_sw_smu(adev))
111                 ret = smu_read_sensor(&adev->smu, sensor, data, size);
112         else {
113                 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
114                         ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle,
115                                                                     sensor, data, size);
116                 else
117                         ret = -EINVAL;
118         }
119
120         return ret;
121 }
122
123 /**
124  * DOC: power_dpm_state
125  *
126  * The power_dpm_state file is a legacy interface and is only provided for
127  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
128  * certain power related parameters.  The file power_dpm_state is used for this.
129  * It accepts the following arguments:
130  *
131  * - battery
132  *
133  * - balanced
134  *
135  * - performance
136  *
137  * battery
138  *
139  * On older GPUs, the vbios provided a special power state for battery
140  * operation.  Selecting battery switched to this state.  This is no
141  * longer provided on newer GPUs so the option does nothing in that case.
142  *
143  * balanced
144  *
145  * On older GPUs, the vbios provided a special power state for balanced
146  * operation.  Selecting balanced switched to this state.  This is no
147  * longer provided on newer GPUs so the option does nothing in that case.
148  *
149  * performance
150  *
151  * On older GPUs, the vbios provided a special power state for performance
152  * operation.  Selecting performance switched to this state.  This is no
153  * longer provided on newer GPUs so the option does nothing in that case.
154  *
155  */
156
157 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
158                                           struct device_attribute *attr,
159                                           char *buf)
160 {
161         struct drm_device *ddev = dev_get_drvdata(dev);
162         struct amdgpu_device *adev = ddev->dev_private;
163         enum amd_pm_state_type pm;
164         int ret;
165
166         ret = pm_runtime_get_sync(ddev->dev);
167         if (ret < 0)
168                 return ret;
169
170         if (is_support_sw_smu(adev)) {
171                 if (adev->smu.ppt_funcs->get_current_power_state)
172                         pm = smu_get_current_power_state(&adev->smu);
173                 else
174                         pm = adev->pm.dpm.user_state;
175         } else if (adev->powerplay.pp_funcs->get_current_power_state) {
176                 pm = amdgpu_dpm_get_current_power_state(adev);
177         } else {
178                 pm = adev->pm.dpm.user_state;
179         }
180
181         pm_runtime_mark_last_busy(ddev->dev);
182         pm_runtime_put_autosuspend(ddev->dev);
183
184         return snprintf(buf, PAGE_SIZE, "%s\n",
185                         (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
186                         (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
187 }
188
189 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
190                                           struct device_attribute *attr,
191                                           const char *buf,
192                                           size_t count)
193 {
194         struct drm_device *ddev = dev_get_drvdata(dev);
195         struct amdgpu_device *adev = ddev->dev_private;
196         enum amd_pm_state_type  state;
197         int ret;
198
199         if (strncmp("battery", buf, strlen("battery")) == 0)
200                 state = POWER_STATE_TYPE_BATTERY;
201         else if (strncmp("balanced", buf, strlen("balanced")) == 0)
202                 state = POWER_STATE_TYPE_BALANCED;
203         else if (strncmp("performance", buf, strlen("performance")) == 0)
204                 state = POWER_STATE_TYPE_PERFORMANCE;
205         else
206                 return -EINVAL;
207
208         ret = pm_runtime_get_sync(ddev->dev);
209         if (ret < 0)
210                 return ret;
211
212         if (is_support_sw_smu(adev)) {
213                 mutex_lock(&adev->pm.mutex);
214                 adev->pm.dpm.user_state = state;
215                 mutex_unlock(&adev->pm.mutex);
216         } else if (adev->powerplay.pp_funcs->dispatch_tasks) {
217                 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
218         } else {
219                 mutex_lock(&adev->pm.mutex);
220                 adev->pm.dpm.user_state = state;
221                 mutex_unlock(&adev->pm.mutex);
222
223                 amdgpu_pm_compute_clocks(adev);
224         }
225         pm_runtime_mark_last_busy(ddev->dev);
226         pm_runtime_put_autosuspend(ddev->dev);
227
228         return count;
229 }
230
231
232 /**
233  * DOC: power_dpm_force_performance_level
234  *
235  * The amdgpu driver provides a sysfs API for adjusting certain power
236  * related parameters.  The file power_dpm_force_performance_level is
237  * used for this.  It accepts the following arguments:
238  *
239  * - auto
240  *
241  * - low
242  *
243  * - high
244  *
245  * - manual
246  *
247  * - profile_standard
248  *
249  * - profile_min_sclk
250  *
251  * - profile_min_mclk
252  *
253  * - profile_peak
254  *
255  * auto
256  *
257  * When auto is selected, the driver will attempt to dynamically select
258  * the optimal power profile for current conditions in the driver.
259  *
260  * low
261  *
262  * When low is selected, the clocks are forced to the lowest power state.
263  *
264  * high
265  *
266  * When high is selected, the clocks are forced to the highest power state.
267  *
268  * manual
269  *
270  * When manual is selected, the user can manually adjust which power states
271  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
272  * and pp_dpm_pcie files and adjust the power state transition heuristics
273  * via the pp_power_profile_mode sysfs file.
274  *
275  * profile_standard
276  * profile_min_sclk
277  * profile_min_mclk
278  * profile_peak
279  *
280  * When the profiling modes are selected, clock and power gating are
281  * disabled and the clocks are set for different profiling cases. This
282  * mode is recommended for profiling specific work loads where you do
283  * not want clock or power gating for clock fluctuation to interfere
284  * with your results. profile_standard sets the clocks to a fixed clock
285  * level which varies from asic to asic.  profile_min_sclk forces the sclk
286  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
287  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
288  *
289  */
290
291 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
292                                                             struct device_attribute *attr,
293                                                             char *buf)
294 {
295         struct drm_device *ddev = dev_get_drvdata(dev);
296         struct amdgpu_device *adev = ddev->dev_private;
297         enum amd_dpm_forced_level level = 0xff;
298         int ret;
299
300         ret = pm_runtime_get_sync(ddev->dev);
301         if (ret < 0)
302                 return ret;
303
304         if (is_support_sw_smu(adev))
305                 level = smu_get_performance_level(&adev->smu);
306         else if (adev->powerplay.pp_funcs->get_performance_level)
307                 level = amdgpu_dpm_get_performance_level(adev);
308         else
309                 level = adev->pm.dpm.forced_level;
310
311         pm_runtime_mark_last_busy(ddev->dev);
312         pm_runtime_put_autosuspend(ddev->dev);
313
314         return snprintf(buf, PAGE_SIZE, "%s\n",
315                         (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
316                         (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
317                         (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
318                         (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
319                         (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
320                         (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
321                         (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
322                         (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
323                         "unknown");
324 }
325
326 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
327                                                             struct device_attribute *attr,
328                                                             const char *buf,
329                                                             size_t count)
330 {
331         struct drm_device *ddev = dev_get_drvdata(dev);
332         struct amdgpu_device *adev = ddev->dev_private;
333         enum amd_dpm_forced_level level;
334         enum amd_dpm_forced_level current_level = 0xff;
335         int ret = 0;
336
337         if (strncmp("low", buf, strlen("low")) == 0) {
338                 level = AMD_DPM_FORCED_LEVEL_LOW;
339         } else if (strncmp("high", buf, strlen("high")) == 0) {
340                 level = AMD_DPM_FORCED_LEVEL_HIGH;
341         } else if (strncmp("auto", buf, strlen("auto")) == 0) {
342                 level = AMD_DPM_FORCED_LEVEL_AUTO;
343         } else if (strncmp("manual", buf, strlen("manual")) == 0) {
344                 level = AMD_DPM_FORCED_LEVEL_MANUAL;
345         } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
346                 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
347         } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
348                 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
349         } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
350                 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
351         } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
352                 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
353         } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
354                 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
355         }  else {
356                 return -EINVAL;
357         }
358
359         ret = pm_runtime_get_sync(ddev->dev);
360         if (ret < 0)
361                 return ret;
362
363         if (is_support_sw_smu(adev))
364                 current_level = smu_get_performance_level(&adev->smu);
365         else if (adev->powerplay.pp_funcs->get_performance_level)
366                 current_level = amdgpu_dpm_get_performance_level(adev);
367
368         if (current_level == level) {
369                 pm_runtime_mark_last_busy(ddev->dev);
370                 pm_runtime_put_autosuspend(ddev->dev);
371                 return count;
372         }
373
374         /* profile_exit setting is valid only when current mode is in profile mode */
375         if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
376             AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
377             AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
378             AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
379             (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
380                 pr_err("Currently not in any profile mode!\n");
381                 pm_runtime_mark_last_busy(ddev->dev);
382                 pm_runtime_put_autosuspend(ddev->dev);
383                 return -EINVAL;
384         }
385
386         if (is_support_sw_smu(adev)) {
387                 ret = smu_force_performance_level(&adev->smu, level);
388                 if (ret) {
389                         pm_runtime_mark_last_busy(ddev->dev);
390                         pm_runtime_put_autosuspend(ddev->dev);
391                         return -EINVAL;
392                 }
393         } else if (adev->powerplay.pp_funcs->force_performance_level) {
394                 mutex_lock(&adev->pm.mutex);
395                 if (adev->pm.dpm.thermal_active) {
396                         mutex_unlock(&adev->pm.mutex);
397                         pm_runtime_mark_last_busy(ddev->dev);
398                         pm_runtime_put_autosuspend(ddev->dev);
399                         return -EINVAL;
400                 }
401                 ret = amdgpu_dpm_force_performance_level(adev, level);
402                 if (ret) {
403                         mutex_unlock(&adev->pm.mutex);
404                         pm_runtime_mark_last_busy(ddev->dev);
405                         pm_runtime_put_autosuspend(ddev->dev);
406                         return -EINVAL;
407                 } else {
408                         adev->pm.dpm.forced_level = level;
409                 }
410                 mutex_unlock(&adev->pm.mutex);
411         }
412         pm_runtime_mark_last_busy(ddev->dev);
413         pm_runtime_put_autosuspend(ddev->dev);
414
415         return count;
416 }
417
418 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
419                 struct device_attribute *attr,
420                 char *buf)
421 {
422         struct drm_device *ddev = dev_get_drvdata(dev);
423         struct amdgpu_device *adev = ddev->dev_private;
424         struct pp_states_info data;
425         int i, buf_len, ret;
426
427         ret = pm_runtime_get_sync(ddev->dev);
428         if (ret < 0)
429                 return ret;
430
431         if (is_support_sw_smu(adev)) {
432                 ret = smu_get_power_num_states(&adev->smu, &data);
433                 if (ret)
434                         return ret;
435         } else if (adev->powerplay.pp_funcs->get_pp_num_states) {
436                 amdgpu_dpm_get_pp_num_states(adev, &data);
437         } else {
438                 memset(&data, 0, sizeof(data));
439         }
440
441         pm_runtime_mark_last_busy(ddev->dev);
442         pm_runtime_put_autosuspend(ddev->dev);
443
444         buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
445         for (i = 0; i < data.nums; i++)
446                 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
447                                 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
448                                 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
449                                 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
450                                 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
451
452         return buf_len;
453 }
454
455 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
456                 struct device_attribute *attr,
457                 char *buf)
458 {
459         struct drm_device *ddev = dev_get_drvdata(dev);
460         struct amdgpu_device *adev = ddev->dev_private;
461         struct pp_states_info data;
462         struct smu_context *smu = &adev->smu;
463         enum amd_pm_state_type pm = 0;
464         int i = 0, ret = 0;
465
466         ret = pm_runtime_get_sync(ddev->dev);
467         if (ret < 0)
468                 return ret;
469
470         if (is_support_sw_smu(adev)) {
471                 pm = smu_get_current_power_state(smu);
472                 ret = smu_get_power_num_states(smu, &data);
473                 if (ret)
474                         return ret;
475         } else if (adev->powerplay.pp_funcs->get_current_power_state
476                  && adev->powerplay.pp_funcs->get_pp_num_states) {
477                 pm = amdgpu_dpm_get_current_power_state(adev);
478                 amdgpu_dpm_get_pp_num_states(adev, &data);
479         }
480
481         pm_runtime_mark_last_busy(ddev->dev);
482         pm_runtime_put_autosuspend(ddev->dev);
483
484         for (i = 0; i < data.nums; i++) {
485                 if (pm == data.states[i])
486                         break;
487         }
488
489         if (i == data.nums)
490                 i = -EINVAL;
491
492         return snprintf(buf, PAGE_SIZE, "%d\n", i);
493 }
494
495 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
496                 struct device_attribute *attr,
497                 char *buf)
498 {
499         struct drm_device *ddev = dev_get_drvdata(dev);
500         struct amdgpu_device *adev = ddev->dev_private;
501
502         if (adev->pp_force_state_enabled)
503                 return amdgpu_get_pp_cur_state(dev, attr, buf);
504         else
505                 return snprintf(buf, PAGE_SIZE, "\n");
506 }
507
508 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
509                 struct device_attribute *attr,
510                 const char *buf,
511                 size_t count)
512 {
513         struct drm_device *ddev = dev_get_drvdata(dev);
514         struct amdgpu_device *adev = ddev->dev_private;
515         enum amd_pm_state_type state = 0;
516         unsigned long idx;
517         int ret;
518
519         if (strlen(buf) == 1)
520                 adev->pp_force_state_enabled = false;
521         else if (is_support_sw_smu(adev))
522                 adev->pp_force_state_enabled = false;
523         else if (adev->powerplay.pp_funcs->dispatch_tasks &&
524                         adev->powerplay.pp_funcs->get_pp_num_states) {
525                 struct pp_states_info data;
526
527                 ret = kstrtoul(buf, 0, &idx);
528                 if (ret || idx >= ARRAY_SIZE(data.states))
529                         return -EINVAL;
530
531                 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
532
533                 amdgpu_dpm_get_pp_num_states(adev, &data);
534                 state = data.states[idx];
535
536                 ret = pm_runtime_get_sync(ddev->dev);
537                 if (ret < 0)
538                         return ret;
539
540                 /* only set user selected power states */
541                 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
542                     state != POWER_STATE_TYPE_DEFAULT) {
543                         amdgpu_dpm_dispatch_task(adev,
544                                         AMD_PP_TASK_ENABLE_USER_STATE, &state);
545                         adev->pp_force_state_enabled = true;
546                 }
547                 pm_runtime_mark_last_busy(ddev->dev);
548                 pm_runtime_put_autosuspend(ddev->dev);
549         }
550
551         return count;
552 }
553
554 /**
555  * DOC: pp_table
556  *
557  * The amdgpu driver provides a sysfs API for uploading new powerplay
558  * tables.  The file pp_table is used for this.  Reading the file
559  * will dump the current power play table.  Writing to the file
560  * will attempt to upload a new powerplay table and re-initialize
561  * powerplay using that new table.
562  *
563  */
564
565 static ssize_t amdgpu_get_pp_table(struct device *dev,
566                 struct device_attribute *attr,
567                 char *buf)
568 {
569         struct drm_device *ddev = dev_get_drvdata(dev);
570         struct amdgpu_device *adev = ddev->dev_private;
571         char *table = NULL;
572         int size, ret;
573
574         ret = pm_runtime_get_sync(ddev->dev);
575         if (ret < 0)
576                 return ret;
577
578         if (is_support_sw_smu(adev)) {
579                 size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
580                 pm_runtime_mark_last_busy(ddev->dev);
581                 pm_runtime_put_autosuspend(ddev->dev);
582                 if (size < 0)
583                         return size;
584         } else if (adev->powerplay.pp_funcs->get_pp_table) {
585                 size = amdgpu_dpm_get_pp_table(adev, &table);
586                 pm_runtime_mark_last_busy(ddev->dev);
587                 pm_runtime_put_autosuspend(ddev->dev);
588                 if (size < 0)
589                         return size;
590         } else {
591                 pm_runtime_mark_last_busy(ddev->dev);
592                 pm_runtime_put_autosuspend(ddev->dev);
593                 return 0;
594         }
595
596         if (size >= PAGE_SIZE)
597                 size = PAGE_SIZE - 1;
598
599         memcpy(buf, table, size);
600
601         return size;
602 }
603
604 static ssize_t amdgpu_set_pp_table(struct device *dev,
605                 struct device_attribute *attr,
606                 const char *buf,
607                 size_t count)
608 {
609         struct drm_device *ddev = dev_get_drvdata(dev);
610         struct amdgpu_device *adev = ddev->dev_private;
611         int ret = 0;
612
613         ret = pm_runtime_get_sync(ddev->dev);
614         if (ret < 0)
615                 return ret;
616
617         if (is_support_sw_smu(adev)) {
618                 ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
619                 if (ret) {
620                         pm_runtime_mark_last_busy(ddev->dev);
621                         pm_runtime_put_autosuspend(ddev->dev);
622                         return ret;
623                 }
624         } else if (adev->powerplay.pp_funcs->set_pp_table)
625                 amdgpu_dpm_set_pp_table(adev, buf, count);
626
627         pm_runtime_mark_last_busy(ddev->dev);
628         pm_runtime_put_autosuspend(ddev->dev);
629
630         return count;
631 }
632
633 /**
634  * DOC: pp_od_clk_voltage
635  *
636  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
637  * in each power level within a power state.  The pp_od_clk_voltage is used for
638  * this.
639  *
640  * < For Vega10 and previous ASICs >
641  *
642  * Reading the file will display:
643  *
644  * - a list of engine clock levels and voltages labeled OD_SCLK
645  *
646  * - a list of memory clock levels and voltages labeled OD_MCLK
647  *
648  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
649  *
650  * To manually adjust these settings, first select manual using
651  * power_dpm_force_performance_level. Enter a new value for each
652  * level by writing a string that contains "s/m level clock voltage" to
653  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
654  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
655  * 810 mV.  When you have edited all of the states as needed, write
656  * "c" (commit) to the file to commit your changes.  If you want to reset to the
657  * default power levels, write "r" (reset) to the file to reset them.
658  *
659  *
660  * < For Vega20 >
661  *
662  * Reading the file will display:
663  *
664  * - minimum and maximum engine clock labeled OD_SCLK
665  *
666  * - maximum memory clock labeled OD_MCLK
667  *
668  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
669  *   They can be used to calibrate the sclk voltage curve.
670  *
671  * - a list of valid ranges for sclk, mclk, and voltage curve points
672  *   labeled OD_RANGE
673  *
674  * To manually adjust these settings:
675  *
676  * - First select manual using power_dpm_force_performance_level
677  *
678  * - For clock frequency setting, enter a new value by writing a
679  *   string that contains "s/m index clock" to the file. The index
680  *   should be 0 if to set minimum clock. And 1 if to set maximum
681  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
682  *   "m 1 800" will update maximum mclk to be 800Mhz.
683  *
684  *   For sclk voltage curve, enter the new values by writing a
685  *   string that contains "vc point clock voltage" to the file. The
686  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
687  *   update point1 with clock set as 300Mhz and voltage as
688  *   600mV. "vc 2 1000 1000" will update point3 with clock set
689  *   as 1000Mhz and voltage 1000mV.
690  *
691  * - When you have edited all of the states as needed, write "c" (commit)
692  *   to the file to commit your changes
693  *
694  * - If you want to reset to the default power levels, write "r" (reset)
695  *   to the file to reset them
696  *
697  */
698
699 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
700                 struct device_attribute *attr,
701                 const char *buf,
702                 size_t count)
703 {
704         struct drm_device *ddev = dev_get_drvdata(dev);
705         struct amdgpu_device *adev = ddev->dev_private;
706         int ret;
707         uint32_t parameter_size = 0;
708         long parameter[64];
709         char buf_cpy[128];
710         char *tmp_str;
711         char *sub_str;
712         const char delimiter[3] = {' ', '\n', '\0'};
713         uint32_t type;
714
715         if (count > 127)
716                 return -EINVAL;
717
718         if (*buf == 's')
719                 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
720         else if (*buf == 'm')
721                 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
722         else if(*buf == 'r')
723                 type = PP_OD_RESTORE_DEFAULT_TABLE;
724         else if (*buf == 'c')
725                 type = PP_OD_COMMIT_DPM_TABLE;
726         else if (!strncmp(buf, "vc", 2))
727                 type = PP_OD_EDIT_VDDC_CURVE;
728         else
729                 return -EINVAL;
730
731         memcpy(buf_cpy, buf, count+1);
732
733         tmp_str = buf_cpy;
734
735         if (type == PP_OD_EDIT_VDDC_CURVE)
736                 tmp_str++;
737         while (isspace(*++tmp_str));
738
739         while (tmp_str[0]) {
740                 sub_str = strsep(&tmp_str, delimiter);
741                 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
742                 if (ret)
743                         return -EINVAL;
744                 parameter_size++;
745
746                 while (isspace(*tmp_str))
747                         tmp_str++;
748         }
749
750         ret = pm_runtime_get_sync(ddev->dev);
751         if (ret < 0)
752                 return ret;
753
754         if (is_support_sw_smu(adev)) {
755                 ret = smu_od_edit_dpm_table(&adev->smu, type,
756                                             parameter, parameter_size);
757
758                 if (ret) {
759                         pm_runtime_mark_last_busy(ddev->dev);
760                         pm_runtime_put_autosuspend(ddev->dev);
761                         return -EINVAL;
762                 }
763         } else {
764                 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
765                         ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
766                                                 parameter, parameter_size);
767                         if (ret) {
768                                 pm_runtime_mark_last_busy(ddev->dev);
769                                 pm_runtime_put_autosuspend(ddev->dev);
770                                 return -EINVAL;
771                         }
772                 }
773
774                 if (type == PP_OD_COMMIT_DPM_TABLE) {
775                         if (adev->powerplay.pp_funcs->dispatch_tasks) {
776                                 amdgpu_dpm_dispatch_task(adev,
777                                                 AMD_PP_TASK_READJUST_POWER_STATE,
778                                                 NULL);
779                                 pm_runtime_mark_last_busy(ddev->dev);
780                                 pm_runtime_put_autosuspend(ddev->dev);
781                                 return count;
782                         } else {
783                                 pm_runtime_mark_last_busy(ddev->dev);
784                                 pm_runtime_put_autosuspend(ddev->dev);
785                                 return -EINVAL;
786                         }
787                 }
788         }
789         pm_runtime_mark_last_busy(ddev->dev);
790         pm_runtime_put_autosuspend(ddev->dev);
791
792         return count;
793 }
794
795 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
796                 struct device_attribute *attr,
797                 char *buf)
798 {
799         struct drm_device *ddev = dev_get_drvdata(dev);
800         struct amdgpu_device *adev = ddev->dev_private;
801         ssize_t size;
802         int ret;
803
804         ret = pm_runtime_get_sync(ddev->dev);
805         if (ret < 0)
806                 return ret;
807
808         if (is_support_sw_smu(adev)) {
809                 size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
810                 size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
811                 size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
812                 size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
813         } else if (adev->powerplay.pp_funcs->print_clock_levels) {
814                 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
815                 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
816                 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
817                 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
818         } else {
819                 size = snprintf(buf, PAGE_SIZE, "\n");
820         }
821         pm_runtime_mark_last_busy(ddev->dev);
822         pm_runtime_put_autosuspend(ddev->dev);
823
824         return size;
825 }
826
827 /**
828  * DOC: pp_features
829  *
830  * The amdgpu driver provides a sysfs API for adjusting what powerplay
831  * features to be enabled. The file pp_features is used for this. And
832  * this is only available for Vega10 and later dGPUs.
833  *
834  * Reading back the file will show you the followings:
835  * - Current ppfeature masks
836  * - List of the all supported powerplay features with their naming,
837  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
838  *
839  * To manually enable or disable a specific feature, just set or clear
840  * the corresponding bit from original ppfeature masks and input the
841  * new ppfeature masks.
842  */
843 static ssize_t amdgpu_set_pp_features(struct device *dev,
844                                       struct device_attribute *attr,
845                                       const char *buf,
846                                       size_t count)
847 {
848         struct drm_device *ddev = dev_get_drvdata(dev);
849         struct amdgpu_device *adev = ddev->dev_private;
850         uint64_t featuremask;
851         int ret;
852
853         ret = kstrtou64(buf, 0, &featuremask);
854         if (ret)
855                 return -EINVAL;
856
857         pr_debug("featuremask = 0x%llx\n", featuremask);
858
859         ret = pm_runtime_get_sync(ddev->dev);
860         if (ret < 0)
861                 return ret;
862
863         if (is_support_sw_smu(adev)) {
864                 ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask);
865                 if (ret) {
866                         pm_runtime_mark_last_busy(ddev->dev);
867                         pm_runtime_put_autosuspend(ddev->dev);
868                         return -EINVAL;
869                 }
870         } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
871                 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
872                 if (ret) {
873                         pm_runtime_mark_last_busy(ddev->dev);
874                         pm_runtime_put_autosuspend(ddev->dev);
875                         return -EINVAL;
876                 }
877         }
878         pm_runtime_mark_last_busy(ddev->dev);
879         pm_runtime_put_autosuspend(ddev->dev);
880
881         return count;
882 }
883
884 static ssize_t amdgpu_get_pp_features(struct device *dev,
885                                       struct device_attribute *attr,
886                                       char *buf)
887 {
888         struct drm_device *ddev = dev_get_drvdata(dev);
889         struct amdgpu_device *adev = ddev->dev_private;
890         ssize_t size;
891         int ret;
892
893         ret = pm_runtime_get_sync(ddev->dev);
894         if (ret < 0)
895                 return ret;
896
897         if (is_support_sw_smu(adev))
898                 size = smu_sys_get_pp_feature_mask(&adev->smu, buf);
899         else if (adev->powerplay.pp_funcs->get_ppfeature_status)
900                 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
901         else
902                 size = snprintf(buf, PAGE_SIZE, "\n");
903
904         pm_runtime_mark_last_busy(ddev->dev);
905         pm_runtime_put_autosuspend(ddev->dev);
906
907         return size;
908 }
909
910 /**
911  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
912  *
913  * The amdgpu driver provides a sysfs API for adjusting what power levels
914  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
915  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
916  * this.
917  *
918  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
919  * Vega10 and later ASICs.
920  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
921  *
922  * Reading back the files will show you the available power levels within
923  * the power state and the clock information for those levels.
924  *
925  * To manually adjust these states, first select manual using
926  * power_dpm_force_performance_level.
927  * Secondly, enter a new value for each level by inputing a string that
928  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
929  * E.g.,
930  *
931  * .. code-block:: bash
932  *
933  *      echo "4 5 6" > pp_dpm_sclk
934  *
935  * will enable sclk levels 4, 5, and 6.
936  *
937  * NOTE: change to the dcefclk max dpm level is not supported now
938  */
939
940 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
941                 struct device_attribute *attr,
942                 char *buf)
943 {
944         struct drm_device *ddev = dev_get_drvdata(dev);
945         struct amdgpu_device *adev = ddev->dev_private;
946         ssize_t size;
947         int ret;
948
949         ret = pm_runtime_get_sync(ddev->dev);
950         if (ret < 0)
951                 return ret;
952
953         if (is_support_sw_smu(adev))
954                 size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
955         else if (adev->powerplay.pp_funcs->print_clock_levels)
956                 size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
957         else
958                 size = snprintf(buf, PAGE_SIZE, "\n");
959
960         pm_runtime_mark_last_busy(ddev->dev);
961         pm_runtime_put_autosuspend(ddev->dev);
962
963         return size;
964 }
965
966 /*
967  * Worst case: 32 bits individually specified, in octal at 12 characters
968  * per line (+1 for \n).
969  */
970 #define AMDGPU_MASK_BUF_MAX     (32 * 13)
971
972 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
973 {
974         int ret;
975         long level;
976         char *sub_str = NULL;
977         char *tmp;
978         char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
979         const char delimiter[3] = {' ', '\n', '\0'};
980         size_t bytes;
981
982         *mask = 0;
983
984         bytes = min(count, sizeof(buf_cpy) - 1);
985         memcpy(buf_cpy, buf, bytes);
986         buf_cpy[bytes] = '\0';
987         tmp = buf_cpy;
988         while (tmp[0]) {
989                 sub_str = strsep(&tmp, delimiter);
990                 if (strlen(sub_str)) {
991                         ret = kstrtol(sub_str, 0, &level);
992                         if (ret)
993                                 return -EINVAL;
994                         *mask |= 1 << level;
995                 } else
996                         break;
997         }
998
999         return 0;
1000 }
1001
1002 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1003                 struct device_attribute *attr,
1004                 const char *buf,
1005                 size_t count)
1006 {
1007         struct drm_device *ddev = dev_get_drvdata(dev);
1008         struct amdgpu_device *adev = ddev->dev_private;
1009         int ret;
1010         uint32_t mask = 0;
1011
1012         ret = amdgpu_read_mask(buf, count, &mask);
1013         if (ret)
1014                 return ret;
1015
1016         ret = pm_runtime_get_sync(ddev->dev);
1017         if (ret < 0)
1018                 return ret;
1019
1020         if (is_support_sw_smu(adev))
1021                 ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask, true);
1022         else if (adev->powerplay.pp_funcs->force_clock_level)
1023                 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
1024
1025         pm_runtime_mark_last_busy(ddev->dev);
1026         pm_runtime_put_autosuspend(ddev->dev);
1027
1028         if (ret)
1029                 return -EINVAL;
1030
1031         return count;
1032 }
1033
1034 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1035                 struct device_attribute *attr,
1036                 char *buf)
1037 {
1038         struct drm_device *ddev = dev_get_drvdata(dev);
1039         struct amdgpu_device *adev = ddev->dev_private;
1040         ssize_t size;
1041         int ret;
1042
1043         ret = pm_runtime_get_sync(ddev->dev);
1044         if (ret < 0)
1045                 return ret;
1046
1047         if (is_support_sw_smu(adev))
1048                 size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
1049         else if (adev->powerplay.pp_funcs->print_clock_levels)
1050                 size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
1051         else
1052                 size = snprintf(buf, PAGE_SIZE, "\n");
1053
1054         pm_runtime_mark_last_busy(ddev->dev);
1055         pm_runtime_put_autosuspend(ddev->dev);
1056
1057         return size;
1058 }
1059
1060 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1061                 struct device_attribute *attr,
1062                 const char *buf,
1063                 size_t count)
1064 {
1065         struct drm_device *ddev = dev_get_drvdata(dev);
1066         struct amdgpu_device *adev = ddev->dev_private;
1067         uint32_t mask = 0;
1068         int ret;
1069
1070         ret = amdgpu_read_mask(buf, count, &mask);
1071         if (ret)
1072                 return ret;
1073
1074         ret = pm_runtime_get_sync(ddev->dev);
1075         if (ret < 0)
1076                 return ret;
1077
1078         if (is_support_sw_smu(adev))
1079                 ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask, true);
1080         else if (adev->powerplay.pp_funcs->force_clock_level)
1081                 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
1082
1083         pm_runtime_mark_last_busy(ddev->dev);
1084         pm_runtime_put_autosuspend(ddev->dev);
1085
1086         if (ret)
1087                 return -EINVAL;
1088
1089         return count;
1090 }
1091
1092 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1093                 struct device_attribute *attr,
1094                 char *buf)
1095 {
1096         struct drm_device *ddev = dev_get_drvdata(dev);
1097         struct amdgpu_device *adev = ddev->dev_private;
1098         ssize_t size;
1099         int ret;
1100
1101         ret = pm_runtime_get_sync(ddev->dev);
1102         if (ret < 0)
1103                 return ret;
1104
1105         if (is_support_sw_smu(adev))
1106                 size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
1107         else if (adev->powerplay.pp_funcs->print_clock_levels)
1108                 size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
1109         else
1110                 size = snprintf(buf, PAGE_SIZE, "\n");
1111
1112         pm_runtime_mark_last_busy(ddev->dev);
1113         pm_runtime_put_autosuspend(ddev->dev);
1114
1115         return size;
1116 }
1117
1118 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1119                 struct device_attribute *attr,
1120                 const char *buf,
1121                 size_t count)
1122 {
1123         struct drm_device *ddev = dev_get_drvdata(dev);
1124         struct amdgpu_device *adev = ddev->dev_private;
1125         int ret;
1126         uint32_t mask = 0;
1127
1128         ret = amdgpu_read_mask(buf, count, &mask);
1129         if (ret)
1130                 return ret;
1131
1132         ret = pm_runtime_get_sync(ddev->dev);
1133         if (ret < 0)
1134                 return ret;
1135
1136         if (is_support_sw_smu(adev))
1137                 ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask, true);
1138         else if (adev->powerplay.pp_funcs->force_clock_level)
1139                 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
1140         else
1141                 ret = 0;
1142
1143         pm_runtime_mark_last_busy(ddev->dev);
1144         pm_runtime_put_autosuspend(ddev->dev);
1145
1146         if (ret)
1147                 return -EINVAL;
1148
1149         return count;
1150 }
1151
1152 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1153                 struct device_attribute *attr,
1154                 char *buf)
1155 {
1156         struct drm_device *ddev = dev_get_drvdata(dev);
1157         struct amdgpu_device *adev = ddev->dev_private;
1158         ssize_t size;
1159         int ret;
1160
1161         ret = pm_runtime_get_sync(ddev->dev);
1162         if (ret < 0)
1163                 return ret;
1164
1165         if (is_support_sw_smu(adev))
1166                 size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1167         else if (adev->powerplay.pp_funcs->print_clock_levels)
1168                 size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1169         else
1170                 size = snprintf(buf, PAGE_SIZE, "\n");
1171
1172         pm_runtime_mark_last_busy(ddev->dev);
1173         pm_runtime_put_autosuspend(ddev->dev);
1174
1175         return size;
1176 }
1177
1178 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1179                 struct device_attribute *attr,
1180                 const char *buf,
1181                 size_t count)
1182 {
1183         struct drm_device *ddev = dev_get_drvdata(dev);
1184         struct amdgpu_device *adev = ddev->dev_private;
1185         int ret;
1186         uint32_t mask = 0;
1187
1188         ret = amdgpu_read_mask(buf, count, &mask);
1189         if (ret)
1190                 return ret;
1191
1192         ret = pm_runtime_get_sync(ddev->dev);
1193         if (ret < 0)
1194                 return ret;
1195
1196         if (is_support_sw_smu(adev))
1197                 ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask, true);
1198         else if (adev->powerplay.pp_funcs->force_clock_level)
1199                 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1200         else
1201                 ret = 0;
1202
1203         pm_runtime_mark_last_busy(ddev->dev);
1204         pm_runtime_put_autosuspend(ddev->dev);
1205
1206         if (ret)
1207                 return -EINVAL;
1208
1209         return count;
1210 }
1211
1212 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1213                 struct device_attribute *attr,
1214                 char *buf)
1215 {
1216         struct drm_device *ddev = dev_get_drvdata(dev);
1217         struct amdgpu_device *adev = ddev->dev_private;
1218         ssize_t size;
1219         int ret;
1220
1221         ret = pm_runtime_get_sync(ddev->dev);
1222         if (ret < 0)
1223                 return ret;
1224
1225         if (is_support_sw_smu(adev))
1226                 size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1227         else if (adev->powerplay.pp_funcs->print_clock_levels)
1228                 size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1229         else
1230                 size = snprintf(buf, PAGE_SIZE, "\n");
1231
1232         pm_runtime_mark_last_busy(ddev->dev);
1233         pm_runtime_put_autosuspend(ddev->dev);
1234
1235         return size;
1236 }
1237
1238 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1239                 struct device_attribute *attr,
1240                 const char *buf,
1241                 size_t count)
1242 {
1243         struct drm_device *ddev = dev_get_drvdata(dev);
1244         struct amdgpu_device *adev = ddev->dev_private;
1245         int ret;
1246         uint32_t mask = 0;
1247
1248         ret = amdgpu_read_mask(buf, count, &mask);
1249         if (ret)
1250                 return ret;
1251
1252         ret = pm_runtime_get_sync(ddev->dev);
1253         if (ret < 0)
1254                 return ret;
1255
1256         if (is_support_sw_smu(adev))
1257                 ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask, true);
1258         else if (adev->powerplay.pp_funcs->force_clock_level)
1259                 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1260         else
1261                 ret = 0;
1262
1263         pm_runtime_mark_last_busy(ddev->dev);
1264         pm_runtime_put_autosuspend(ddev->dev);
1265
1266         if (ret)
1267                 return -EINVAL;
1268
1269         return count;
1270 }
1271
1272 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1273                 struct device_attribute *attr,
1274                 char *buf)
1275 {
1276         struct drm_device *ddev = dev_get_drvdata(dev);
1277         struct amdgpu_device *adev = ddev->dev_private;
1278         ssize_t size;
1279         int ret;
1280
1281         ret = pm_runtime_get_sync(ddev->dev);
1282         if (ret < 0)
1283                 return ret;
1284
1285         if (is_support_sw_smu(adev))
1286                 size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1287         else if (adev->powerplay.pp_funcs->print_clock_levels)
1288                 size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1289         else
1290                 size = snprintf(buf, PAGE_SIZE, "\n");
1291
1292         pm_runtime_mark_last_busy(ddev->dev);
1293         pm_runtime_put_autosuspend(ddev->dev);
1294
1295         return size;
1296 }
1297
1298 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1299                 struct device_attribute *attr,
1300                 const char *buf,
1301                 size_t count)
1302 {
1303         struct drm_device *ddev = dev_get_drvdata(dev);
1304         struct amdgpu_device *adev = ddev->dev_private;
1305         int ret;
1306         uint32_t mask = 0;
1307
1308         ret = amdgpu_read_mask(buf, count, &mask);
1309         if (ret)
1310                 return ret;
1311
1312         ret = pm_runtime_get_sync(ddev->dev);
1313         if (ret < 0)
1314                 return ret;
1315
1316         if (is_support_sw_smu(adev))
1317                 ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask, true);
1318         else if (adev->powerplay.pp_funcs->force_clock_level)
1319                 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1320         else
1321                 ret = 0;
1322
1323         pm_runtime_mark_last_busy(ddev->dev);
1324         pm_runtime_put_autosuspend(ddev->dev);
1325
1326         if (ret)
1327                 return -EINVAL;
1328
1329         return count;
1330 }
1331
1332 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1333                 struct device_attribute *attr,
1334                 char *buf)
1335 {
1336         struct drm_device *ddev = dev_get_drvdata(dev);
1337         struct amdgpu_device *adev = ddev->dev_private;
1338         uint32_t value = 0;
1339         int ret;
1340
1341         ret = pm_runtime_get_sync(ddev->dev);
1342         if (ret < 0)
1343                 return ret;
1344
1345         if (is_support_sw_smu(adev))
1346                 value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK);
1347         else if (adev->powerplay.pp_funcs->get_sclk_od)
1348                 value = amdgpu_dpm_get_sclk_od(adev);
1349
1350         pm_runtime_mark_last_busy(ddev->dev);
1351         pm_runtime_put_autosuspend(ddev->dev);
1352
1353         return snprintf(buf, PAGE_SIZE, "%d\n", value);
1354 }
1355
1356 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1357                 struct device_attribute *attr,
1358                 const char *buf,
1359                 size_t count)
1360 {
1361         struct drm_device *ddev = dev_get_drvdata(dev);
1362         struct amdgpu_device *adev = ddev->dev_private;
1363         int ret;
1364         long int value;
1365
1366         ret = kstrtol(buf, 0, &value);
1367
1368         if (ret)
1369                 return -EINVAL;
1370
1371         ret = pm_runtime_get_sync(ddev->dev);
1372         if (ret < 0)
1373                 return ret;
1374
1375         if (is_support_sw_smu(adev)) {
1376                 value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value);
1377         } else {
1378                 if (adev->powerplay.pp_funcs->set_sclk_od)
1379                         amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1380
1381                 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1382                         amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1383                 } else {
1384                         adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1385                         amdgpu_pm_compute_clocks(adev);
1386                 }
1387         }
1388
1389         pm_runtime_mark_last_busy(ddev->dev);
1390         pm_runtime_put_autosuspend(ddev->dev);
1391
1392         return count;
1393 }
1394
1395 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1396                 struct device_attribute *attr,
1397                 char *buf)
1398 {
1399         struct drm_device *ddev = dev_get_drvdata(dev);
1400         struct amdgpu_device *adev = ddev->dev_private;
1401         uint32_t value = 0;
1402         int ret;
1403
1404         ret = pm_runtime_get_sync(ddev->dev);
1405         if (ret < 0)
1406                 return ret;
1407
1408         if (is_support_sw_smu(adev))
1409                 value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK);
1410         else if (adev->powerplay.pp_funcs->get_mclk_od)
1411                 value = amdgpu_dpm_get_mclk_od(adev);
1412
1413         pm_runtime_mark_last_busy(ddev->dev);
1414         pm_runtime_put_autosuspend(ddev->dev);
1415
1416         return snprintf(buf, PAGE_SIZE, "%d\n", value);
1417 }
1418
1419 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1420                 struct device_attribute *attr,
1421                 const char *buf,
1422                 size_t count)
1423 {
1424         struct drm_device *ddev = dev_get_drvdata(dev);
1425         struct amdgpu_device *adev = ddev->dev_private;
1426         int ret;
1427         long int value;
1428
1429         ret = kstrtol(buf, 0, &value);
1430
1431         if (ret)
1432                 return -EINVAL;
1433
1434         ret = pm_runtime_get_sync(ddev->dev);
1435         if (ret < 0)
1436                 return ret;
1437
1438         if (is_support_sw_smu(adev)) {
1439                 value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value);
1440         } else {
1441                 if (adev->powerplay.pp_funcs->set_mclk_od)
1442                         amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1443
1444                 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1445                         amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1446                 } else {
1447                         adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1448                         amdgpu_pm_compute_clocks(adev);
1449                 }
1450         }
1451
1452         pm_runtime_mark_last_busy(ddev->dev);
1453         pm_runtime_put_autosuspend(ddev->dev);
1454
1455         return count;
1456 }
1457
1458 /**
1459  * DOC: pp_power_profile_mode
1460  *
1461  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1462  * related to switching between power levels in a power state.  The file
1463  * pp_power_profile_mode is used for this.
1464  *
1465  * Reading this file outputs a list of all of the predefined power profiles
1466  * and the relevant heuristics settings for that profile.
1467  *
1468  * To select a profile or create a custom profile, first select manual using
1469  * power_dpm_force_performance_level.  Writing the number of a predefined
1470  * profile to pp_power_profile_mode will enable those heuristics.  To
1471  * create a custom set of heuristics, write a string of numbers to the file
1472  * starting with the number of the custom profile along with a setting
1473  * for each heuristic parameter.  Due to differences across asic families
1474  * the heuristic parameters vary from family to family.
1475  *
1476  */
1477
1478 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1479                 struct device_attribute *attr,
1480                 char *buf)
1481 {
1482         struct drm_device *ddev = dev_get_drvdata(dev);
1483         struct amdgpu_device *adev = ddev->dev_private;
1484         ssize_t size;
1485         int ret;
1486
1487         ret = pm_runtime_get_sync(ddev->dev);
1488         if (ret < 0)
1489                 return ret;
1490
1491         if (is_support_sw_smu(adev))
1492                 size = smu_get_power_profile_mode(&adev->smu, buf);
1493         else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1494                 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1495         else
1496                 size = snprintf(buf, PAGE_SIZE, "\n");
1497
1498         pm_runtime_mark_last_busy(ddev->dev);
1499         pm_runtime_put_autosuspend(ddev->dev);
1500
1501         return size;
1502 }
1503
1504
1505 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1506                 struct device_attribute *attr,
1507                 const char *buf,
1508                 size_t count)
1509 {
1510         int ret = 0xff;
1511         struct drm_device *ddev = dev_get_drvdata(dev);
1512         struct amdgpu_device *adev = ddev->dev_private;
1513         uint32_t parameter_size = 0;
1514         long parameter[64];
1515         char *sub_str, buf_cpy[128];
1516         char *tmp_str;
1517         uint32_t i = 0;
1518         char tmp[2];
1519         long int profile_mode = 0;
1520         const char delimiter[3] = {' ', '\n', '\0'};
1521
1522         tmp[0] = *(buf);
1523         tmp[1] = '\0';
1524         ret = kstrtol(tmp, 0, &profile_mode);
1525         if (ret)
1526                 return -EINVAL;
1527
1528         if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1529                 if (count < 2 || count > 127)
1530                         return -EINVAL;
1531                 while (isspace(*++buf))
1532                         i++;
1533                 memcpy(buf_cpy, buf, count-i);
1534                 tmp_str = buf_cpy;
1535                 while (tmp_str[0]) {
1536                         sub_str = strsep(&tmp_str, delimiter);
1537                         ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1538                         if (ret)
1539                                 return -EINVAL;
1540                         parameter_size++;
1541                         while (isspace(*tmp_str))
1542                                 tmp_str++;
1543                 }
1544         }
1545         parameter[parameter_size] = profile_mode;
1546
1547         ret = pm_runtime_get_sync(ddev->dev);
1548         if (ret < 0)
1549                 return ret;
1550
1551         if (is_support_sw_smu(adev))
1552                 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true);
1553         else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1554                 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1555
1556         pm_runtime_mark_last_busy(ddev->dev);
1557         pm_runtime_put_autosuspend(ddev->dev);
1558
1559         if (!ret)
1560                 return count;
1561
1562         return -EINVAL;
1563 }
1564
1565 /**
1566  * DOC: busy_percent
1567  *
1568  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1569  * is as a percentage.  The file gpu_busy_percent is used for this.
1570  * The SMU firmware computes a percentage of load based on the
1571  * aggregate activity level in the IP cores.
1572  */
1573 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1574                                            struct device_attribute *attr,
1575                                            char *buf)
1576 {
1577         struct drm_device *ddev = dev_get_drvdata(dev);
1578         struct amdgpu_device *adev = ddev->dev_private;
1579         int r, value, size = sizeof(value);
1580
1581         r = pm_runtime_get_sync(ddev->dev);
1582         if (r < 0)
1583                 return r;
1584
1585         /* read the IP busy sensor */
1586         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1587                                    (void *)&value, &size);
1588
1589         pm_runtime_mark_last_busy(ddev->dev);
1590         pm_runtime_put_autosuspend(ddev->dev);
1591
1592         if (r)
1593                 return r;
1594
1595         return snprintf(buf, PAGE_SIZE, "%d\n", value);
1596 }
1597
1598 /**
1599  * DOC: mem_busy_percent
1600  *
1601  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1602  * is as a percentage.  The file mem_busy_percent is used for this.
1603  * The SMU firmware computes a percentage of load based on the
1604  * aggregate activity level in the IP cores.
1605  */
1606 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1607                                            struct device_attribute *attr,
1608                                            char *buf)
1609 {
1610         struct drm_device *ddev = dev_get_drvdata(dev);
1611         struct amdgpu_device *adev = ddev->dev_private;
1612         int r, value, size = sizeof(value);
1613
1614         r = pm_runtime_get_sync(ddev->dev);
1615         if (r < 0)
1616                 return r;
1617
1618         /* read the IP busy sensor */
1619         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1620                                    (void *)&value, &size);
1621
1622         pm_runtime_mark_last_busy(ddev->dev);
1623         pm_runtime_put_autosuspend(ddev->dev);
1624
1625         if (r)
1626                 return r;
1627
1628         return snprintf(buf, PAGE_SIZE, "%d\n", value);
1629 }
1630
1631 /**
1632  * DOC: pcie_bw
1633  *
1634  * The amdgpu driver provides a sysfs API for estimating how much data
1635  * has been received and sent by the GPU in the last second through PCIe.
1636  * The file pcie_bw is used for this.
1637  * The Perf counters count the number of received and sent messages and return
1638  * those values, as well as the maximum payload size of a PCIe packet (mps).
1639  * Note that it is not possible to easily and quickly obtain the size of each
1640  * packet transmitted, so we output the max payload size (mps) to allow for
1641  * quick estimation of the PCIe bandwidth usage
1642  */
1643 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1644                 struct device_attribute *attr,
1645                 char *buf)
1646 {
1647         struct drm_device *ddev = dev_get_drvdata(dev);
1648         struct amdgpu_device *adev = ddev->dev_private;
1649         uint64_t count0 = 0, count1 = 0;
1650         int ret;
1651
1652         if (adev->flags & AMD_IS_APU)
1653                 return -ENODATA;
1654
1655         if (!adev->asic_funcs->get_pcie_usage)
1656                 return -ENODATA;
1657
1658         ret = pm_runtime_get_sync(ddev->dev);
1659         if (ret < 0)
1660                 return ret;
1661
1662         amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1663
1664         pm_runtime_mark_last_busy(ddev->dev);
1665         pm_runtime_put_autosuspend(ddev->dev);
1666
1667         return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1668                         count0, count1, pcie_get_mps(adev->pdev));
1669 }
1670
1671 /**
1672  * DOC: unique_id
1673  *
1674  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1675  * The file unique_id is used for this.
1676  * This will provide a Unique ID that will persist from machine to machine
1677  *
1678  * NOTE: This will only work for GFX9 and newer. This file will be absent
1679  * on unsupported ASICs (GFX8 and older)
1680  */
1681 static ssize_t amdgpu_get_unique_id(struct device *dev,
1682                 struct device_attribute *attr,
1683                 char *buf)
1684 {
1685         struct drm_device *ddev = dev_get_drvdata(dev);
1686         struct amdgpu_device *adev = ddev->dev_private;
1687
1688         if (adev->unique_id)
1689                 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1690
1691         return 0;
1692 }
1693
1694 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1695         AMDGPU_DEVICE_ATTR_RW(power_dpm_state,                          ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1696         AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,        ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1697         AMDGPU_DEVICE_ATTR_RO(pp_num_states,                            ATTR_FLAG_BASIC),
1698         AMDGPU_DEVICE_ATTR_RO(pp_cur_state,                             ATTR_FLAG_BASIC),
1699         AMDGPU_DEVICE_ATTR_RW(pp_force_state,                           ATTR_FLAG_BASIC),
1700         AMDGPU_DEVICE_ATTR_RW(pp_table,                                 ATTR_FLAG_BASIC),
1701         AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,                              ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1702         AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,                              ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1703         AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,                            ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1704         AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,                              ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1705         AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,                           ATTR_FLAG_BASIC),
1706         AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,                              ATTR_FLAG_BASIC),
1707         AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,                               ATTR_FLAG_BASIC),
1708         AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,                               ATTR_FLAG_BASIC),
1709         AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,                    ATTR_FLAG_BASIC),
1710         AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,                        ATTR_FLAG_BASIC),
1711         AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,                         ATTR_FLAG_BASIC),
1712         AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,                         ATTR_FLAG_BASIC),
1713         AMDGPU_DEVICE_ATTR_RO(pcie_bw,                                  ATTR_FLAG_BASIC),
1714         AMDGPU_DEVICE_ATTR_RW(pp_features,                              ATTR_FLAG_BASIC),
1715         AMDGPU_DEVICE_ATTR_RO(unique_id,                                ATTR_FLAG_BASIC),
1716 };
1717
1718 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1719                                uint32_t mask)
1720 {
1721         struct device_attribute *dev_attr = &attr->dev_attr;
1722         const char *attr_name = dev_attr->attr.name;
1723         struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
1724         enum amd_asic_type asic_type = adev->asic_type;
1725
1726         if (!(attr->flags & mask)) {
1727                 attr->states = ATTR_STATE_UNSUPPORTED;
1728                 return 0;
1729         }
1730
1731 #define DEVICE_ATTR_IS(_name)   (!strcmp(attr_name, #_name))
1732
1733         if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
1734                 if (asic_type < CHIP_VEGA10)
1735                         attr->states = ATTR_STATE_UNSUPPORTED;
1736         } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
1737                 if (asic_type < CHIP_VEGA10 || asic_type == CHIP_ARCTURUS)
1738                         attr->states = ATTR_STATE_UNSUPPORTED;
1739         } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
1740                 if (asic_type < CHIP_VEGA20)
1741                         attr->states = ATTR_STATE_UNSUPPORTED;
1742         } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
1743                 if (asic_type == CHIP_ARCTURUS)
1744                         attr->states = ATTR_STATE_UNSUPPORTED;
1745         } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
1746                 attr->states = ATTR_STATE_UNSUPPORTED;
1747                 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
1748                     (!is_support_sw_smu(adev) && hwmgr->od_enabled))
1749                         attr->states = ATTR_STATE_SUPPORTED;
1750         } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
1751                 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
1752                         attr->states = ATTR_STATE_UNSUPPORTED;
1753         } else if (DEVICE_ATTR_IS(pcie_bw)) {
1754                 /* PCIe Perf counters won't work on APU nodes */
1755                 if (adev->flags & AMD_IS_APU)
1756                         attr->states = ATTR_STATE_UNSUPPORTED;
1757         } else if (DEVICE_ATTR_IS(unique_id)) {
1758                 if (!adev->unique_id)
1759                         attr->states = ATTR_STATE_UNSUPPORTED;
1760         } else if (DEVICE_ATTR_IS(pp_features)) {
1761                 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
1762                         attr->states = ATTR_STATE_UNSUPPORTED;
1763         }
1764
1765         if (asic_type == CHIP_ARCTURUS) {
1766                 /* Arcturus does not support standalone mclk/socclk/fclk level setting */
1767                 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
1768                     DEVICE_ATTR_IS(pp_dpm_socclk) ||
1769                     DEVICE_ATTR_IS(pp_dpm_fclk)) {
1770                         dev_attr->attr.mode &= ~S_IWUGO;
1771                         dev_attr->store = NULL;
1772                 }
1773         }
1774
1775 #undef DEVICE_ATTR_IS
1776
1777         return 0;
1778 }
1779
1780
1781 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
1782                                      struct amdgpu_device_attr *attr,
1783                                      uint32_t mask)
1784 {
1785         int ret = 0;
1786         struct device_attribute *dev_attr = &attr->dev_attr;
1787         const char *name = dev_attr->attr.name;
1788         int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1789                            uint32_t mask) = default_attr_update;
1790
1791         BUG_ON(!attr);
1792
1793         attr_update = attr->attr_update ? attr_update : default_attr_update;
1794
1795         ret = attr_update(adev, attr, mask);
1796         if (ret) {
1797                 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
1798                         name, ret);
1799                 return ret;
1800         }
1801
1802         /* the attr->states maybe changed after call attr->attr_update function */
1803         if (attr->states == ATTR_STATE_UNSUPPORTED)
1804                 return 0;
1805
1806         ret = device_create_file(adev->dev, dev_attr);
1807         if (ret) {
1808                 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
1809                         name, ret);
1810         }
1811
1812         attr->states = ATTR_STATE_SUPPORTED;
1813
1814         return ret;
1815 }
1816
1817 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
1818 {
1819         struct device_attribute *dev_attr = &attr->dev_attr;
1820
1821         if (attr->states == ATTR_STATE_UNSUPPORTED)
1822                 return;
1823
1824         device_remove_file(adev->dev, dev_attr);
1825
1826         attr->states = ATTR_STATE_UNSUPPORTED;
1827 }
1828
1829 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
1830                                             struct amdgpu_device_attr *attrs,
1831                                             uint32_t counts,
1832                                             uint32_t mask)
1833 {
1834         int ret = 0;
1835         uint32_t i = 0;
1836
1837         for (i = 0; i < counts; i++) {
1838                 ret = amdgpu_device_attr_create(adev, &attrs[i], mask);
1839                 if (ret)
1840                         goto failed;
1841         }
1842
1843         return 0;
1844
1845 failed:
1846         while (i--)
1847                 amdgpu_device_attr_remove(adev, &attrs[i]);
1848
1849         return ret;
1850 }
1851
1852 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
1853                                              struct amdgpu_device_attr *attrs,
1854                                              uint32_t counts)
1855 {
1856         uint32_t i = 0;
1857
1858         for (i = 0; i < counts; i++)
1859                 amdgpu_device_attr_remove(adev, &attrs[i]);
1860 }
1861
1862 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1863                                       struct device_attribute *attr,
1864                                       char *buf)
1865 {
1866         struct amdgpu_device *adev = dev_get_drvdata(dev);
1867         int channel = to_sensor_dev_attr(attr)->index;
1868         int r, temp = 0, size = sizeof(temp);
1869
1870         if (channel >= PP_TEMP_MAX)
1871                 return -EINVAL;
1872
1873         r = pm_runtime_get_sync(adev->ddev->dev);
1874         if (r < 0)
1875                 return r;
1876
1877         switch (channel) {
1878         case PP_TEMP_JUNCTION:
1879                 /* get current junction temperature */
1880                 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1881                                            (void *)&temp, &size);
1882                 break;
1883         case PP_TEMP_EDGE:
1884                 /* get current edge temperature */
1885                 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
1886                                            (void *)&temp, &size);
1887                 break;
1888         case PP_TEMP_MEM:
1889                 /* get current memory temperature */
1890                 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
1891                                            (void *)&temp, &size);
1892                 break;
1893         default:
1894                 r = -EINVAL;
1895                 break;
1896         }
1897
1898         pm_runtime_mark_last_busy(adev->ddev->dev);
1899         pm_runtime_put_autosuspend(adev->ddev->dev);
1900
1901         if (r)
1902                 return r;
1903
1904         return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1905 }
1906
1907 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1908                                              struct device_attribute *attr,
1909                                              char *buf)
1910 {
1911         struct amdgpu_device *adev = dev_get_drvdata(dev);
1912         int hyst = to_sensor_dev_attr(attr)->index;
1913         int temp;
1914
1915         if (hyst)
1916                 temp = adev->pm.dpm.thermal.min_temp;
1917         else
1918                 temp = adev->pm.dpm.thermal.max_temp;
1919
1920         return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1921 }
1922
1923 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
1924                                              struct device_attribute *attr,
1925                                              char *buf)
1926 {
1927         struct amdgpu_device *adev = dev_get_drvdata(dev);
1928         int hyst = to_sensor_dev_attr(attr)->index;
1929         int temp;
1930
1931         if (hyst)
1932                 temp = adev->pm.dpm.thermal.min_hotspot_temp;
1933         else
1934                 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
1935
1936         return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1937 }
1938
1939 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
1940                                              struct device_attribute *attr,
1941                                              char *buf)
1942 {
1943         struct amdgpu_device *adev = dev_get_drvdata(dev);
1944         int hyst = to_sensor_dev_attr(attr)->index;
1945         int temp;
1946
1947         if (hyst)
1948                 temp = adev->pm.dpm.thermal.min_mem_temp;
1949         else
1950                 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
1951
1952         return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1953 }
1954
1955 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
1956                                              struct device_attribute *attr,
1957                                              char *buf)
1958 {
1959         int channel = to_sensor_dev_attr(attr)->index;
1960
1961         if (channel >= PP_TEMP_MAX)
1962                 return -EINVAL;
1963
1964         return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
1965 }
1966
1967 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
1968                                              struct device_attribute *attr,
1969                                              char *buf)
1970 {
1971         struct amdgpu_device *adev = dev_get_drvdata(dev);
1972         int channel = to_sensor_dev_attr(attr)->index;
1973         int temp = 0;
1974
1975         if (channel >= PP_TEMP_MAX)
1976                 return -EINVAL;
1977
1978         switch (channel) {
1979         case PP_TEMP_JUNCTION:
1980                 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
1981                 break;
1982         case PP_TEMP_EDGE:
1983                 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
1984                 break;
1985         case PP_TEMP_MEM:
1986                 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
1987                 break;
1988         }
1989
1990         return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1991 }
1992
1993 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1994                                             struct device_attribute *attr,
1995                                             char *buf)
1996 {
1997         struct amdgpu_device *adev = dev_get_drvdata(dev);
1998         u32 pwm_mode = 0;
1999         int ret;
2000
2001         ret = pm_runtime_get_sync(adev->ddev->dev);
2002         if (ret < 0)
2003                 return ret;
2004
2005         if (is_support_sw_smu(adev)) {
2006                 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2007         } else {
2008                 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2009                         pm_runtime_mark_last_busy(adev->ddev->dev);
2010                         pm_runtime_put_autosuspend(adev->ddev->dev);
2011                         return -EINVAL;
2012                 }
2013
2014                 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2015         }
2016
2017         pm_runtime_mark_last_busy(adev->ddev->dev);
2018         pm_runtime_put_autosuspend(adev->ddev->dev);
2019
2020         return sprintf(buf, "%i\n", pwm_mode);
2021 }
2022
2023 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2024                                             struct device_attribute *attr,
2025                                             const char *buf,
2026                                             size_t count)
2027 {
2028         struct amdgpu_device *adev = dev_get_drvdata(dev);
2029         int err, ret;
2030         int value;
2031
2032         err = kstrtoint(buf, 10, &value);
2033         if (err)
2034                 return err;
2035
2036         ret = pm_runtime_get_sync(adev->ddev->dev);
2037         if (ret < 0)
2038                 return ret;
2039
2040         if (is_support_sw_smu(adev)) {
2041                 smu_set_fan_control_mode(&adev->smu, value);
2042         } else {
2043                 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2044                         pm_runtime_mark_last_busy(adev->ddev->dev);
2045                         pm_runtime_put_autosuspend(adev->ddev->dev);
2046                         return -EINVAL;
2047                 }
2048
2049                 amdgpu_dpm_set_fan_control_mode(adev, value);
2050         }
2051
2052         pm_runtime_mark_last_busy(adev->ddev->dev);
2053         pm_runtime_put_autosuspend(adev->ddev->dev);
2054
2055         return count;
2056 }
2057
2058 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2059                                          struct device_attribute *attr,
2060                                          char *buf)
2061 {
2062         return sprintf(buf, "%i\n", 0);
2063 }
2064
2065 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2066                                          struct device_attribute *attr,
2067                                          char *buf)
2068 {
2069         return sprintf(buf, "%i\n", 255);
2070 }
2071
2072 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2073                                      struct device_attribute *attr,
2074                                      const char *buf, size_t count)
2075 {
2076         struct amdgpu_device *adev = dev_get_drvdata(dev);
2077         int err;
2078         u32 value;
2079         u32 pwm_mode;
2080
2081         err = pm_runtime_get_sync(adev->ddev->dev);
2082         if (err < 0)
2083                 return err;
2084
2085         if (is_support_sw_smu(adev))
2086                 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2087         else
2088                 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2089
2090         if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2091                 pr_info("manual fan speed control should be enabled first\n");
2092                 pm_runtime_mark_last_busy(adev->ddev->dev);
2093                 pm_runtime_put_autosuspend(adev->ddev->dev);
2094                 return -EINVAL;
2095         }
2096
2097         err = kstrtou32(buf, 10, &value);
2098         if (err) {
2099                 pm_runtime_mark_last_busy(adev->ddev->dev);
2100                 pm_runtime_put_autosuspend(adev->ddev->dev);
2101                 return err;
2102         }
2103
2104         value = (value * 100) / 255;
2105
2106         if (is_support_sw_smu(adev))
2107                 err = smu_set_fan_speed_percent(&adev->smu, value);
2108         else if (adev->powerplay.pp_funcs->set_fan_speed_percent)
2109                 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
2110         else
2111                 err = -EINVAL;
2112
2113         pm_runtime_mark_last_busy(adev->ddev->dev);
2114         pm_runtime_put_autosuspend(adev->ddev->dev);
2115
2116         if (err)
2117                 return err;
2118
2119         return count;
2120 }
2121
2122 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2123                                      struct device_attribute *attr,
2124                                      char *buf)
2125 {
2126         struct amdgpu_device *adev = dev_get_drvdata(dev);
2127         int err;
2128         u32 speed = 0;
2129
2130         err = pm_runtime_get_sync(adev->ddev->dev);
2131         if (err < 0)
2132                 return err;
2133
2134         if (is_support_sw_smu(adev))
2135                 err = smu_get_fan_speed_percent(&adev->smu, &speed);
2136         else if (adev->powerplay.pp_funcs->get_fan_speed_percent)
2137                 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
2138         else
2139                 err = -EINVAL;
2140
2141         pm_runtime_mark_last_busy(adev->ddev->dev);
2142         pm_runtime_put_autosuspend(adev->ddev->dev);
2143
2144         if (err)
2145                 return err;
2146
2147         speed = (speed * 255) / 100;
2148
2149         return sprintf(buf, "%i\n", speed);
2150 }
2151
2152 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2153                                            struct device_attribute *attr,
2154                                            char *buf)
2155 {
2156         struct amdgpu_device *adev = dev_get_drvdata(dev);
2157         int err;
2158         u32 speed = 0;
2159
2160         err = pm_runtime_get_sync(adev->ddev->dev);
2161         if (err < 0)
2162                 return err;
2163
2164         if (is_support_sw_smu(adev))
2165                 err = smu_get_fan_speed_rpm(&adev->smu, &speed);
2166         else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2167                 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2168         else
2169                 err = -EINVAL;
2170
2171         pm_runtime_mark_last_busy(adev->ddev->dev);
2172         pm_runtime_put_autosuspend(adev->ddev->dev);
2173
2174         if (err)
2175                 return err;
2176
2177         return sprintf(buf, "%i\n", speed);
2178 }
2179
2180 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2181                                          struct device_attribute *attr,
2182                                          char *buf)
2183 {
2184         struct amdgpu_device *adev = dev_get_drvdata(dev);
2185         u32 min_rpm = 0;
2186         u32 size = sizeof(min_rpm);
2187         int r;
2188
2189         r = pm_runtime_get_sync(adev->ddev->dev);
2190         if (r < 0)
2191                 return r;
2192
2193         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2194                                    (void *)&min_rpm, &size);
2195
2196         pm_runtime_mark_last_busy(adev->ddev->dev);
2197         pm_runtime_put_autosuspend(adev->ddev->dev);
2198
2199         if (r)
2200                 return r;
2201
2202         return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
2203 }
2204
2205 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2206                                          struct device_attribute *attr,
2207                                          char *buf)
2208 {
2209         struct amdgpu_device *adev = dev_get_drvdata(dev);
2210         u32 max_rpm = 0;
2211         u32 size = sizeof(max_rpm);
2212         int r;
2213
2214         r = pm_runtime_get_sync(adev->ddev->dev);
2215         if (r < 0)
2216                 return r;
2217
2218         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2219                                    (void *)&max_rpm, &size);
2220
2221         pm_runtime_mark_last_busy(adev->ddev->dev);
2222         pm_runtime_put_autosuspend(adev->ddev->dev);
2223
2224         if (r)
2225                 return r;
2226
2227         return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
2228 }
2229
2230 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2231                                            struct device_attribute *attr,
2232                                            char *buf)
2233 {
2234         struct amdgpu_device *adev = dev_get_drvdata(dev);
2235         int err;
2236         u32 rpm = 0;
2237
2238         err = pm_runtime_get_sync(adev->ddev->dev);
2239         if (err < 0)
2240                 return err;
2241
2242         if (is_support_sw_smu(adev))
2243                 err = smu_get_fan_speed_rpm(&adev->smu, &rpm);
2244         else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2245                 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2246         else
2247                 err = -EINVAL;
2248
2249         pm_runtime_mark_last_busy(adev->ddev->dev);
2250         pm_runtime_put_autosuspend(adev->ddev->dev);
2251
2252         if (err)
2253                 return err;
2254
2255         return sprintf(buf, "%i\n", rpm);
2256 }
2257
2258 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2259                                      struct device_attribute *attr,
2260                                      const char *buf, size_t count)
2261 {
2262         struct amdgpu_device *adev = dev_get_drvdata(dev);
2263         int err;
2264         u32 value;
2265         u32 pwm_mode;
2266
2267         err = pm_runtime_get_sync(adev->ddev->dev);
2268         if (err < 0)
2269                 return err;
2270
2271         if (is_support_sw_smu(adev))
2272                 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2273         else
2274                 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2275
2276         if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2277                 pm_runtime_mark_last_busy(adev->ddev->dev);
2278                 pm_runtime_put_autosuspend(adev->ddev->dev);
2279                 return -ENODATA;
2280         }
2281
2282         err = kstrtou32(buf, 10, &value);
2283         if (err) {
2284                 pm_runtime_mark_last_busy(adev->ddev->dev);
2285                 pm_runtime_put_autosuspend(adev->ddev->dev);
2286                 return err;
2287         }
2288
2289         if (is_support_sw_smu(adev))
2290                 err = smu_set_fan_speed_rpm(&adev->smu, value);
2291         else if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2292                 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2293         else
2294                 err = -EINVAL;
2295
2296         pm_runtime_mark_last_busy(adev->ddev->dev);
2297         pm_runtime_put_autosuspend(adev->ddev->dev);
2298
2299         if (err)
2300                 return err;
2301
2302         return count;
2303 }
2304
2305 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2306                                             struct device_attribute *attr,
2307                                             char *buf)
2308 {
2309         struct amdgpu_device *adev = dev_get_drvdata(dev);
2310         u32 pwm_mode = 0;
2311         int ret;
2312
2313         ret = pm_runtime_get_sync(adev->ddev->dev);
2314         if (ret < 0)
2315                 return ret;
2316
2317         if (is_support_sw_smu(adev)) {
2318                 pwm_mode = smu_get_fan_control_mode(&adev->smu);
2319         } else {
2320                 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2321                         pm_runtime_mark_last_busy(adev->ddev->dev);
2322                         pm_runtime_put_autosuspend(adev->ddev->dev);
2323                         return -EINVAL;
2324                 }
2325
2326                 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2327         }
2328
2329         pm_runtime_mark_last_busy(adev->ddev->dev);
2330         pm_runtime_put_autosuspend(adev->ddev->dev);
2331
2332         return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2333 }
2334
2335 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2336                                             struct device_attribute *attr,
2337                                             const char *buf,
2338                                             size_t count)
2339 {
2340         struct amdgpu_device *adev = dev_get_drvdata(dev);
2341         int err;
2342         int value;
2343         u32 pwm_mode;
2344
2345         err = kstrtoint(buf, 10, &value);
2346         if (err)
2347                 return err;
2348
2349         if (value == 0)
2350                 pwm_mode = AMD_FAN_CTRL_AUTO;
2351         else if (value == 1)
2352                 pwm_mode = AMD_FAN_CTRL_MANUAL;
2353         else
2354                 return -EINVAL;
2355
2356         err = pm_runtime_get_sync(adev->ddev->dev);
2357         if (err < 0)
2358                 return err;
2359
2360         if (is_support_sw_smu(adev)) {
2361                 smu_set_fan_control_mode(&adev->smu, pwm_mode);
2362         } else {
2363                 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2364                         pm_runtime_mark_last_busy(adev->ddev->dev);
2365                         pm_runtime_put_autosuspend(adev->ddev->dev);
2366                         return -EINVAL;
2367                 }
2368                 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2369         }
2370
2371         pm_runtime_mark_last_busy(adev->ddev->dev);
2372         pm_runtime_put_autosuspend(adev->ddev->dev);
2373
2374         return count;
2375 }
2376
2377 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2378                                         struct device_attribute *attr,
2379                                         char *buf)
2380 {
2381         struct amdgpu_device *adev = dev_get_drvdata(dev);
2382         u32 vddgfx;
2383         int r, size = sizeof(vddgfx);
2384
2385         r = pm_runtime_get_sync(adev->ddev->dev);
2386         if (r < 0)
2387                 return r;
2388
2389         /* get the voltage */
2390         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2391                                    (void *)&vddgfx, &size);
2392
2393         pm_runtime_mark_last_busy(adev->ddev->dev);
2394         pm_runtime_put_autosuspend(adev->ddev->dev);
2395
2396         if (r)
2397                 return r;
2398
2399         return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
2400 }
2401
2402 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2403                                               struct device_attribute *attr,
2404                                               char *buf)
2405 {
2406         return snprintf(buf, PAGE_SIZE, "vddgfx\n");
2407 }
2408
2409 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2410                                        struct device_attribute *attr,
2411                                        char *buf)
2412 {
2413         struct amdgpu_device *adev = dev_get_drvdata(dev);
2414         u32 vddnb;
2415         int r, size = sizeof(vddnb);
2416
2417         /* only APUs have vddnb */
2418         if  (!(adev->flags & AMD_IS_APU))
2419                 return -EINVAL;
2420
2421         r = pm_runtime_get_sync(adev->ddev->dev);
2422         if (r < 0)
2423                 return r;
2424
2425         /* get the voltage */
2426         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2427                                    (void *)&vddnb, &size);
2428
2429         pm_runtime_mark_last_busy(adev->ddev->dev);
2430         pm_runtime_put_autosuspend(adev->ddev->dev);
2431
2432         if (r)
2433                 return r;
2434
2435         return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
2436 }
2437
2438 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2439                                               struct device_attribute *attr,
2440                                               char *buf)
2441 {
2442         return snprintf(buf, PAGE_SIZE, "vddnb\n");
2443 }
2444
2445 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2446                                            struct device_attribute *attr,
2447                                            char *buf)
2448 {
2449         struct amdgpu_device *adev = dev_get_drvdata(dev);
2450         u32 query = 0;
2451         int r, size = sizeof(u32);
2452         unsigned uw;
2453
2454         r = pm_runtime_get_sync(adev->ddev->dev);
2455         if (r < 0)
2456                 return r;
2457
2458         /* get the voltage */
2459         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2460                                    (void *)&query, &size);
2461
2462         pm_runtime_mark_last_busy(adev->ddev->dev);
2463         pm_runtime_put_autosuspend(adev->ddev->dev);
2464
2465         if (r)
2466                 return r;
2467
2468         /* convert to microwatts */
2469         uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2470
2471         return snprintf(buf, PAGE_SIZE, "%u\n", uw);
2472 }
2473
2474 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2475                                          struct device_attribute *attr,
2476                                          char *buf)
2477 {
2478         return sprintf(buf, "%i\n", 0);
2479 }
2480
2481 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2482                                          struct device_attribute *attr,
2483                                          char *buf)
2484 {
2485         struct amdgpu_device *adev = dev_get_drvdata(dev);
2486         uint32_t limit = 0;
2487         ssize_t size;
2488         int r;
2489
2490         r = pm_runtime_get_sync(adev->ddev->dev);
2491         if (r < 0)
2492                 return r;
2493
2494         if (is_support_sw_smu(adev)) {
2495                 smu_get_power_limit(&adev->smu, &limit, true, true);
2496                 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2497         } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2498                 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2499                 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2500         } else {
2501                 size = snprintf(buf, PAGE_SIZE, "\n");
2502         }
2503
2504         pm_runtime_mark_last_busy(adev->ddev->dev);
2505         pm_runtime_put_autosuspend(adev->ddev->dev);
2506
2507         return size;
2508 }
2509
2510 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2511                                          struct device_attribute *attr,
2512                                          char *buf)
2513 {
2514         struct amdgpu_device *adev = dev_get_drvdata(dev);
2515         uint32_t limit = 0;
2516         ssize_t size;
2517         int r;
2518
2519         r = pm_runtime_get_sync(adev->ddev->dev);
2520         if (r < 0)
2521                 return r;
2522
2523         if (is_support_sw_smu(adev)) {
2524                 smu_get_power_limit(&adev->smu, &limit, false,  true);
2525                 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2526         } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2527                 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2528                 size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2529         } else {
2530                 size = snprintf(buf, PAGE_SIZE, "\n");
2531         }
2532
2533         pm_runtime_mark_last_busy(adev->ddev->dev);
2534         pm_runtime_put_autosuspend(adev->ddev->dev);
2535
2536         return size;
2537 }
2538
2539
2540 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2541                 struct device_attribute *attr,
2542                 const char *buf,
2543                 size_t count)
2544 {
2545         struct amdgpu_device *adev = dev_get_drvdata(dev);
2546         int err;
2547         u32 value;
2548
2549         if (amdgpu_sriov_vf(adev))
2550                 return -EINVAL;
2551
2552         err = kstrtou32(buf, 10, &value);
2553         if (err)
2554                 return err;
2555
2556         value = value / 1000000; /* convert to Watt */
2557
2558
2559         err = pm_runtime_get_sync(adev->ddev->dev);
2560         if (err < 0)
2561                 return err;
2562
2563         if (is_support_sw_smu(adev))
2564                 err = smu_set_power_limit(&adev->smu, value);
2565         else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit)
2566                 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2567         else
2568                 err = -EINVAL;
2569
2570         pm_runtime_mark_last_busy(adev->ddev->dev);
2571         pm_runtime_put_autosuspend(adev->ddev->dev);
2572
2573         if (err)
2574                 return err;
2575
2576         return count;
2577 }
2578
2579 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2580                                       struct device_attribute *attr,
2581                                       char *buf)
2582 {
2583         struct amdgpu_device *adev = dev_get_drvdata(dev);
2584         uint32_t sclk;
2585         int r, size = sizeof(sclk);
2586
2587         r = pm_runtime_get_sync(adev->ddev->dev);
2588         if (r < 0)
2589                 return r;
2590
2591         /* get the sclk */
2592         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2593                                    (void *)&sclk, &size);
2594
2595         pm_runtime_mark_last_busy(adev->ddev->dev);
2596         pm_runtime_put_autosuspend(adev->ddev->dev);
2597
2598         if (r)
2599                 return r;
2600
2601         return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2602 }
2603
2604 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2605                                             struct device_attribute *attr,
2606                                             char *buf)
2607 {
2608         return snprintf(buf, PAGE_SIZE, "sclk\n");
2609 }
2610
2611 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2612                                       struct device_attribute *attr,
2613                                       char *buf)
2614 {
2615         struct amdgpu_device *adev = dev_get_drvdata(dev);
2616         uint32_t mclk;
2617         int r, size = sizeof(mclk);
2618
2619         r = pm_runtime_get_sync(adev->ddev->dev);
2620         if (r < 0)
2621                 return r;
2622
2623         /* get the sclk */
2624         r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2625                                    (void *)&mclk, &size);
2626
2627         pm_runtime_mark_last_busy(adev->ddev->dev);
2628         pm_runtime_put_autosuspend(adev->ddev->dev);
2629
2630         if (r)
2631                 return r;
2632
2633         return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
2634 }
2635
2636 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2637                                             struct device_attribute *attr,
2638                                             char *buf)
2639 {
2640         return snprintf(buf, PAGE_SIZE, "mclk\n");
2641 }
2642
2643 /**
2644  * DOC: hwmon
2645  *
2646  * The amdgpu driver exposes the following sensor interfaces:
2647  *
2648  * - GPU temperature (via the on-die sensor)
2649  *
2650  * - GPU voltage
2651  *
2652  * - Northbridge voltage (APUs only)
2653  *
2654  * - GPU power
2655  *
2656  * - GPU fan
2657  *
2658  * - GPU gfx/compute engine clock
2659  *
2660  * - GPU memory clock (dGPU only)
2661  *
2662  * hwmon interfaces for GPU temperature:
2663  *
2664  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
2665  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
2666  *
2667  * - temp[1-3]_label: temperature channel label
2668  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
2669  *
2670  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
2671  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
2672  *
2673  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
2674  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
2675  *
2676  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
2677  *   - these are supported on SOC15 dGPUs only
2678  *
2679  * hwmon interfaces for GPU voltage:
2680  *
2681  * - in0_input: the voltage on the GPU in millivolts
2682  *
2683  * - in1_input: the voltage on the Northbridge in millivolts
2684  *
2685  * hwmon interfaces for GPU power:
2686  *
2687  * - power1_average: average power used by the GPU in microWatts
2688  *
2689  * - power1_cap_min: minimum cap supported in microWatts
2690  *
2691  * - power1_cap_max: maximum cap supported in microWatts
2692  *
2693  * - power1_cap: selected power cap in microWatts
2694  *
2695  * hwmon interfaces for GPU fan:
2696  *
2697  * - pwm1: pulse width modulation fan level (0-255)
2698  *
2699  * - 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)
2700  *
2701  * - pwm1_min: pulse width modulation fan control minimum level (0)
2702  *
2703  * - pwm1_max: pulse width modulation fan control maximum level (255)
2704  *
2705  * - fan1_min: an minimum value Unit: revolution/min (RPM)
2706  *
2707  * - fan1_max: an maxmum value Unit: revolution/max (RPM)
2708  *
2709  * - fan1_input: fan speed in RPM
2710  *
2711  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
2712  *
2713  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
2714  *
2715  * hwmon interfaces for GPU clocks:
2716  *
2717  * - freq1_input: the gfx/compute clock in hertz
2718  *
2719  * - freq2_input: the memory clock in hertz
2720  *
2721  * You can use hwmon tools like sensors to view this information on your system.
2722  *
2723  */
2724
2725 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
2726 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
2727 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
2728 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
2729 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
2730 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
2731 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
2732 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
2733 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
2734 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
2735 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
2736 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2737 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
2738 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
2739 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
2740 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
2741 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
2742 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
2743 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
2744 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
2745 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
2746 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
2747 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
2748 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2749 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
2750 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
2751 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
2752 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
2753 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
2754 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
2755 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
2756 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
2757 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
2758 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
2759 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
2760 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
2761
2762 static struct attribute *hwmon_attributes[] = {
2763         &sensor_dev_attr_temp1_input.dev_attr.attr,
2764         &sensor_dev_attr_temp1_crit.dev_attr.attr,
2765         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2766         &sensor_dev_attr_temp2_input.dev_attr.attr,
2767         &sensor_dev_attr_temp2_crit.dev_attr.attr,
2768         &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2769         &sensor_dev_attr_temp3_input.dev_attr.attr,
2770         &sensor_dev_attr_temp3_crit.dev_attr.attr,
2771         &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2772         &sensor_dev_attr_temp1_emergency.dev_attr.attr,
2773         &sensor_dev_attr_temp2_emergency.dev_attr.attr,
2774         &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2775         &sensor_dev_attr_temp1_label.dev_attr.attr,
2776         &sensor_dev_attr_temp2_label.dev_attr.attr,
2777         &sensor_dev_attr_temp3_label.dev_attr.attr,
2778         &sensor_dev_attr_pwm1.dev_attr.attr,
2779         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2780         &sensor_dev_attr_pwm1_min.dev_attr.attr,
2781         &sensor_dev_attr_pwm1_max.dev_attr.attr,
2782         &sensor_dev_attr_fan1_input.dev_attr.attr,
2783         &sensor_dev_attr_fan1_min.dev_attr.attr,
2784         &sensor_dev_attr_fan1_max.dev_attr.attr,
2785         &sensor_dev_attr_fan1_target.dev_attr.attr,
2786         &sensor_dev_attr_fan1_enable.dev_attr.attr,
2787         &sensor_dev_attr_in0_input.dev_attr.attr,
2788         &sensor_dev_attr_in0_label.dev_attr.attr,
2789         &sensor_dev_attr_in1_input.dev_attr.attr,
2790         &sensor_dev_attr_in1_label.dev_attr.attr,
2791         &sensor_dev_attr_power1_average.dev_attr.attr,
2792         &sensor_dev_attr_power1_cap_max.dev_attr.attr,
2793         &sensor_dev_attr_power1_cap_min.dev_attr.attr,
2794         &sensor_dev_attr_power1_cap.dev_attr.attr,
2795         &sensor_dev_attr_freq1_input.dev_attr.attr,
2796         &sensor_dev_attr_freq1_label.dev_attr.attr,
2797         &sensor_dev_attr_freq2_input.dev_attr.attr,
2798         &sensor_dev_attr_freq2_label.dev_attr.attr,
2799         NULL
2800 };
2801
2802 static umode_t hwmon_attributes_visible(struct kobject *kobj,
2803                                         struct attribute *attr, int index)
2804 {
2805         struct device *dev = kobj_to_dev(kobj);
2806         struct amdgpu_device *adev = dev_get_drvdata(dev);
2807         umode_t effective_mode = attr->mode;
2808
2809         /* under multi-vf mode, the hwmon attributes are all not supported */
2810         if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2811                 return 0;
2812
2813         /* there is no fan under pp one vf mode */
2814         if (amdgpu_sriov_is_pp_one_vf(adev) &&
2815             (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2816              attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2817              attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2818              attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2819              attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2820              attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2821              attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2822              attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2823              attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2824                 return 0;
2825
2826         /* Skip fan attributes if fan is not present */
2827         if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2828             attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2829             attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2830             attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2831             attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2832             attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2833             attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2834             attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2835             attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2836                 return 0;
2837
2838         /* Skip fan attributes on APU */
2839         if ((adev->flags & AMD_IS_APU) &&
2840             (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2841              attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2842              attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2843              attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2844              attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2845              attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2846              attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2847              attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2848              attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2849                 return 0;
2850
2851         /* Skip limit attributes if DPM is not enabled */
2852         if (!adev->pm.dpm_enabled &&
2853             (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
2854              attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
2855              attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2856              attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2857              attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2858              attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2859              attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2860              attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2861              attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2862              attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2863              attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2864                 return 0;
2865
2866         if (!is_support_sw_smu(adev)) {
2867                 /* mask fan attributes if we have no bindings for this asic to expose */
2868                 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
2869                      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
2870                     (!adev->powerplay.pp_funcs->get_fan_control_mode &&
2871                      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
2872                         effective_mode &= ~S_IRUGO;
2873
2874                 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2875                      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
2876                     (!adev->powerplay.pp_funcs->set_fan_control_mode &&
2877                      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
2878                         effective_mode &= ~S_IWUSR;
2879         }
2880
2881         if (((adev->flags & AMD_IS_APU) ||
2882              adev->family == AMDGPU_FAMILY_SI ||        /* not implemented yet */
2883              adev->family == AMDGPU_FAMILY_KV) &&       /* not implemented yet */
2884             (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
2885              attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
2886              attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
2887              attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
2888                 return 0;
2889
2890         if (!is_support_sw_smu(adev)) {
2891                 /* hide max/min values if we can't both query and manage the fan */
2892                 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2893                      !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
2894                      (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2895                      !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2896                     (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2897                      attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
2898                         return 0;
2899
2900                 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2901                      !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2902                     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2903                      attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
2904                         return 0;
2905         }
2906
2907         if ((adev->family == AMDGPU_FAMILY_SI ||        /* not implemented yet */
2908              adev->family == AMDGPU_FAMILY_KV) &&       /* not implemented yet */
2909             (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
2910              attr == &sensor_dev_attr_in0_label.dev_attr.attr))
2911                 return 0;
2912
2913         /* only APUs have vddnb */
2914         if (!(adev->flags & AMD_IS_APU) &&
2915             (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
2916              attr == &sensor_dev_attr_in1_label.dev_attr.attr))
2917                 return 0;
2918
2919         /* no mclk on APUs */
2920         if ((adev->flags & AMD_IS_APU) &&
2921             (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
2922              attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
2923                 return 0;
2924
2925         /* only SOC15 dGPUs support hotspot and mem temperatures */
2926         if (((adev->flags & AMD_IS_APU) ||
2927              adev->asic_type < CHIP_VEGA10) &&
2928             (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
2929              attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
2930              attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
2931              attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
2932              attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
2933              attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
2934              attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
2935              attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
2936              attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
2937              attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
2938              attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
2939                 return 0;
2940
2941         return effective_mode;
2942 }
2943
2944 static const struct attribute_group hwmon_attrgroup = {
2945         .attrs = hwmon_attributes,
2946         .is_visible = hwmon_attributes_visible,
2947 };
2948
2949 static const struct attribute_group *hwmon_groups[] = {
2950         &hwmon_attrgroup,
2951         NULL
2952 };
2953
2954 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
2955 {
2956         struct amdgpu_device *adev =
2957                 container_of(work, struct amdgpu_device,
2958                              pm.dpm.thermal.work);
2959         /* switch to the thermal state */
2960         enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
2961         int temp, size = sizeof(temp);
2962
2963         if (!adev->pm.dpm_enabled)
2964                 return;
2965
2966         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
2967                                     (void *)&temp, &size)) {
2968                 if (temp < adev->pm.dpm.thermal.min_temp)
2969                         /* switch back the user state */
2970                         dpm_state = adev->pm.dpm.user_state;
2971         } else {
2972                 if (adev->pm.dpm.thermal.high_to_low)
2973                         /* switch back the user state */
2974                         dpm_state = adev->pm.dpm.user_state;
2975         }
2976         mutex_lock(&adev->pm.mutex);
2977         if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
2978                 adev->pm.dpm.thermal_active = true;
2979         else
2980                 adev->pm.dpm.thermal_active = false;
2981         adev->pm.dpm.state = dpm_state;
2982         mutex_unlock(&adev->pm.mutex);
2983
2984         amdgpu_pm_compute_clocks(adev);
2985 }
2986
2987 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
2988                                                      enum amd_pm_state_type dpm_state)
2989 {
2990         int i;
2991         struct amdgpu_ps *ps;
2992         u32 ui_class;
2993         bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
2994                 true : false;
2995
2996         /* check if the vblank period is too short to adjust the mclk */
2997         if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
2998                 if (amdgpu_dpm_vblank_too_short(adev))
2999                         single_display = false;
3000         }
3001
3002         /* certain older asics have a separare 3D performance state,
3003          * so try that first if the user selected performance
3004          */
3005         if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
3006                 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
3007         /* balanced states don't exist at the moment */
3008         if (dpm_state == POWER_STATE_TYPE_BALANCED)
3009                 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3010
3011 restart_search:
3012         /* Pick the best power state based on current conditions */
3013         for (i = 0; i < adev->pm.dpm.num_ps; i++) {
3014                 ps = &adev->pm.dpm.ps[i];
3015                 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
3016                 switch (dpm_state) {
3017                 /* user states */
3018                 case POWER_STATE_TYPE_BATTERY:
3019                         if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
3020                                 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3021                                         if (single_display)
3022                                                 return ps;
3023                                 } else
3024                                         return ps;
3025                         }
3026                         break;
3027                 case POWER_STATE_TYPE_BALANCED:
3028                         if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
3029                                 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3030                                         if (single_display)
3031                                                 return ps;
3032                                 } else
3033                                         return ps;
3034                         }
3035                         break;
3036                 case POWER_STATE_TYPE_PERFORMANCE:
3037                         if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
3038                                 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
3039                                         if (single_display)
3040                                                 return ps;
3041                                 } else
3042                                         return ps;
3043                         }
3044                         break;
3045                 /* internal states */
3046                 case POWER_STATE_TYPE_INTERNAL_UVD:
3047                         if (adev->pm.dpm.uvd_ps)
3048                                 return adev->pm.dpm.uvd_ps;
3049                         else
3050                                 break;
3051                 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3052                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
3053                                 return ps;
3054                         break;
3055                 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3056                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
3057                                 return ps;
3058                         break;
3059                 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3060                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
3061                                 return ps;
3062                         break;
3063                 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3064                         if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
3065                                 return ps;
3066                         break;
3067                 case POWER_STATE_TYPE_INTERNAL_BOOT:
3068                         return adev->pm.dpm.boot_ps;
3069                 case POWER_STATE_TYPE_INTERNAL_THERMAL:
3070                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
3071                                 return ps;
3072                         break;
3073                 case POWER_STATE_TYPE_INTERNAL_ACPI:
3074                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
3075                                 return ps;
3076                         break;
3077                 case POWER_STATE_TYPE_INTERNAL_ULV:
3078                         if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
3079                                 return ps;
3080                         break;
3081                 case POWER_STATE_TYPE_INTERNAL_3DPERF:
3082                         if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
3083                                 return ps;
3084                         break;
3085                 default:
3086                         break;
3087                 }
3088         }
3089         /* use a fallback state if we didn't match */
3090         switch (dpm_state) {
3091         case POWER_STATE_TYPE_INTERNAL_UVD_SD:
3092                 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
3093                 goto restart_search;
3094         case POWER_STATE_TYPE_INTERNAL_UVD_HD:
3095         case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
3096         case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
3097                 if (adev->pm.dpm.uvd_ps) {
3098                         return adev->pm.dpm.uvd_ps;
3099                 } else {
3100                         dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3101                         goto restart_search;
3102                 }
3103         case POWER_STATE_TYPE_INTERNAL_THERMAL:
3104                 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
3105                 goto restart_search;
3106         case POWER_STATE_TYPE_INTERNAL_ACPI:
3107                 dpm_state = POWER_STATE_TYPE_BATTERY;
3108                 goto restart_search;
3109         case POWER_STATE_TYPE_BATTERY:
3110         case POWER_STATE_TYPE_BALANCED:
3111         case POWER_STATE_TYPE_INTERNAL_3DPERF:
3112                 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
3113                 goto restart_search;
3114         default:
3115                 break;
3116         }
3117
3118         return NULL;
3119 }
3120
3121 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
3122 {
3123         struct amdgpu_ps *ps;
3124         enum amd_pm_state_type dpm_state;
3125         int ret;
3126         bool equal = false;
3127
3128         /* if dpm init failed */
3129         if (!adev->pm.dpm_enabled)
3130                 return;
3131
3132         if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
3133                 /* add other state override checks here */
3134                 if ((!adev->pm.dpm.thermal_active) &&
3135                     (!adev->pm.dpm.uvd_active))
3136                         adev->pm.dpm.state = adev->pm.dpm.user_state;
3137         }
3138         dpm_state = adev->pm.dpm.state;
3139
3140         ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
3141         if (ps)
3142                 adev->pm.dpm.requested_ps = ps;
3143         else
3144                 return;
3145
3146         if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
3147                 printk("switching from power state:\n");
3148                 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
3149                 printk("switching to power state:\n");
3150                 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
3151         }
3152
3153         /* update whether vce is active */
3154         ps->vce_active = adev->pm.dpm.vce_active;
3155         if (adev->powerplay.pp_funcs->display_configuration_changed)
3156                 amdgpu_dpm_display_configuration_changed(adev);
3157
3158         ret = amdgpu_dpm_pre_set_power_state(adev);
3159         if (ret)
3160                 return;
3161
3162         if (adev->powerplay.pp_funcs->check_state_equal) {
3163                 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
3164                         equal = false;
3165         }
3166
3167         if (equal)
3168                 return;
3169
3170         amdgpu_dpm_set_power_state(adev);
3171         amdgpu_dpm_post_set_power_state(adev);
3172
3173         adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
3174         adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
3175
3176         if (adev->powerplay.pp_funcs->force_performance_level) {
3177                 if (adev->pm.dpm.thermal_active) {
3178                         enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
3179                         /* force low perf level for thermal */
3180                         amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
3181                         /* save the user's level */
3182                         adev->pm.dpm.forced_level = level;
3183                 } else {
3184                         /* otherwise, user selected level */
3185                         amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
3186                 }
3187         }
3188 }
3189
3190 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
3191 {
3192         int ret = 0;
3193
3194         ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
3195         if (ret)
3196                 DRM_ERROR("Dpm %s uvd failed, ret = %d. \n",
3197                           enable ? "enable" : "disable", ret);
3198
3199         /* enable/disable Low Memory PState for UVD (4k videos) */
3200         if (adev->asic_type == CHIP_STONEY &&
3201                 adev->uvd.decode_image_width >= WIDTH_4K) {
3202                 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
3203
3204                 if (hwmgr && hwmgr->hwmgr_func &&
3205                     hwmgr->hwmgr_func->update_nbdpm_pstate)
3206                         hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
3207                                                                !enable,
3208                                                                true);
3209         }
3210 }
3211
3212 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
3213 {
3214         int ret = 0;
3215
3216         ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
3217         if (ret)
3218                 DRM_ERROR("Dpm %s vce failed, ret = %d. \n",
3219                           enable ? "enable" : "disable", ret);
3220 }
3221
3222 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
3223 {
3224         int i;
3225
3226         if (adev->powerplay.pp_funcs->print_power_state == NULL)
3227                 return;
3228
3229         for (i = 0; i < adev->pm.dpm.num_ps; i++)
3230                 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
3231
3232 }
3233
3234 void amdgpu_dpm_enable_jpeg(struct amdgpu_device *adev, bool enable)
3235 {
3236         int ret = 0;
3237
3238         ret = amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_JPEG, !enable);
3239         if (ret)
3240                 DRM_ERROR("Dpm %s jpeg failed, ret = %d. \n",
3241                           enable ? "enable" : "disable", ret);
3242 }
3243
3244 int amdgpu_pm_load_smu_firmware(struct amdgpu_device *adev, uint32_t *smu_version)
3245 {
3246         int r;
3247
3248         if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->load_firmware) {
3249                 r = adev->powerplay.pp_funcs->load_firmware(adev->powerplay.pp_handle);
3250                 if (r) {
3251                         pr_err("smu firmware loading failed\n");
3252                         return r;
3253                 }
3254                 *smu_version = adev->pm.fw_version;
3255         }
3256         return 0;
3257 }
3258
3259 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3260 {
3261         int ret;
3262         uint32_t mask = 0;
3263
3264         if (adev->pm.sysfs_initialized)
3265                 return 0;
3266
3267         if (adev->pm.dpm_enabled == 0)
3268                 return 0;
3269
3270         adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3271                                                                    DRIVER_NAME, adev,
3272                                                                    hwmon_groups);
3273         if (IS_ERR(adev->pm.int_hwmon_dev)) {
3274                 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3275                 dev_err(adev->dev,
3276                         "Unable to register hwmon device: %d\n", ret);
3277                 return ret;
3278         }
3279
3280         switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3281         case SRIOV_VF_MODE_ONE_VF:
3282                 mask = ATTR_FLAG_ONEVF;
3283                 break;
3284         case SRIOV_VF_MODE_MULTI_VF:
3285                 mask = 0;
3286                 break;
3287         case SRIOV_VF_MODE_BARE_METAL:
3288         default:
3289                 mask = ATTR_FLAG_MASK_ALL;
3290                 break;
3291         }
3292
3293         ret = amdgpu_device_attr_create_groups(adev,
3294                                                amdgpu_device_attrs,
3295                                                ARRAY_SIZE(amdgpu_device_attrs),
3296                                                mask);
3297         if (ret)
3298                 return ret;
3299
3300         adev->pm.sysfs_initialized = true;
3301
3302         return 0;
3303 }
3304
3305 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3306 {
3307         if (adev->pm.dpm_enabled == 0)
3308                 return;
3309
3310         if (adev->pm.int_hwmon_dev)
3311                 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3312
3313         amdgpu_device_attr_remove_groups(adev,
3314                                          amdgpu_device_attrs,
3315                                          ARRAY_SIZE(amdgpu_device_attrs));
3316 }
3317
3318 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
3319 {
3320         int i = 0;
3321
3322         if (!adev->pm.dpm_enabled)
3323                 return;
3324
3325         if (adev->mode_info.num_crtc)
3326                 amdgpu_display_bandwidth_update(adev);
3327
3328         for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3329                 struct amdgpu_ring *ring = adev->rings[i];
3330                 if (ring && ring->sched.ready)
3331                         amdgpu_fence_wait_empty(ring);
3332         }
3333
3334         if (is_support_sw_smu(adev)) {
3335                 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
3336                 smu_handle_task(&adev->smu,
3337                                 smu_dpm->dpm_level,
3338                                 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE,
3339                                 true);
3340         } else {
3341                 if (adev->powerplay.pp_funcs->dispatch_tasks) {
3342                         if (!amdgpu_device_has_dc_support(adev)) {
3343                                 mutex_lock(&adev->pm.mutex);
3344                                 amdgpu_dpm_get_active_displays(adev);
3345                                 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
3346                                 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
3347                                 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
3348                                 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
3349                                 if (adev->pm.pm_display_cfg.vrefresh > 120)
3350                                         adev->pm.pm_display_cfg.min_vblank_time = 0;
3351                                 if (adev->powerplay.pp_funcs->display_configuration_change)
3352                                         adev->powerplay.pp_funcs->display_configuration_change(
3353                                                                         adev->powerplay.pp_handle,
3354                                                                         &adev->pm.pm_display_cfg);
3355                                 mutex_unlock(&adev->pm.mutex);
3356                         }
3357                         amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
3358                 } else {
3359                         mutex_lock(&adev->pm.mutex);
3360                         amdgpu_dpm_get_active_displays(adev);
3361                         amdgpu_dpm_change_power_state_locked(adev);
3362                         mutex_unlock(&adev->pm.mutex);
3363                 }
3364         }
3365 }
3366
3367 /*
3368  * Debugfs info
3369  */
3370 #if defined(CONFIG_DEBUG_FS)
3371
3372 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3373 {
3374         uint32_t value;
3375         uint64_t value64;
3376         uint32_t query = 0;
3377         int size;
3378
3379         /* GPU Clocks */
3380         size = sizeof(value);
3381         seq_printf(m, "GFX Clocks and Power:\n");
3382         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3383                 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3384         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3385                 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3386         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3387                 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3388         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3389                 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3390         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3391                 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3392         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3393                 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3394         size = sizeof(uint32_t);
3395         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3396                 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3397         size = sizeof(value);
3398         seq_printf(m, "\n");
3399
3400         /* GPU Temp */
3401         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3402                 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3403
3404         /* GPU Load */
3405         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3406                 seq_printf(m, "GPU Load: %u %%\n", value);
3407         /* MEM Load */
3408         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3409                 seq_printf(m, "MEM Load: %u %%\n", value);
3410
3411         seq_printf(m, "\n");
3412
3413         /* SMC feature mask */
3414         if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3415                 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3416
3417         if (adev->asic_type > CHIP_VEGA20) {
3418                 /* VCN clocks */
3419                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3420                         if (!value) {
3421                                 seq_printf(m, "VCN: Disabled\n");
3422                         } else {
3423                                 seq_printf(m, "VCN: Enabled\n");
3424                                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3425                                         seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3426                                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3427                                         seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3428                         }
3429                 }
3430                 seq_printf(m, "\n");
3431         } else {
3432                 /* UVD clocks */
3433                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3434                         if (!value) {
3435                                 seq_printf(m, "UVD: Disabled\n");
3436                         } else {
3437                                 seq_printf(m, "UVD: Enabled\n");
3438                                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3439                                         seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3440                                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3441                                         seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3442                         }
3443                 }
3444                 seq_printf(m, "\n");
3445
3446                 /* VCE clocks */
3447                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3448                         if (!value) {
3449                                 seq_printf(m, "VCE: Disabled\n");
3450                         } else {
3451                                 seq_printf(m, "VCE: Enabled\n");
3452                                 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3453                                         seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3454                         }
3455                 }
3456         }
3457
3458         return 0;
3459 }
3460
3461 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3462 {
3463         int i;
3464
3465         for (i = 0; clocks[i].flag; i++)
3466                 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3467                            (flags & clocks[i].flag) ? "On" : "Off");
3468 }
3469
3470 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3471 {
3472         struct drm_info_node *node = (struct drm_info_node *) m->private;
3473         struct drm_device *dev = node->minor->dev;
3474         struct amdgpu_device *adev = dev->dev_private;
3475         u32 flags = 0;
3476         int r;
3477
3478         r = pm_runtime_get_sync(dev->dev);
3479         if (r < 0)
3480                 return r;
3481
3482         amdgpu_device_ip_get_clockgating_state(adev, &flags);
3483         seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3484         amdgpu_parse_cg_state(m, flags);
3485         seq_printf(m, "\n");
3486
3487         if (!adev->pm.dpm_enabled) {
3488                 seq_printf(m, "dpm not enabled\n");
3489                 pm_runtime_mark_last_busy(dev->dev);
3490                 pm_runtime_put_autosuspend(dev->dev);
3491                 return 0;
3492         }
3493
3494         if (!is_support_sw_smu(adev) &&
3495             adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3496                 mutex_lock(&adev->pm.mutex);
3497                 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3498                         adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3499                 else
3500                         seq_printf(m, "Debugfs support not implemented for this asic\n");
3501                 mutex_unlock(&adev->pm.mutex);
3502                 r = 0;
3503         } else {
3504                 r = amdgpu_debugfs_pm_info_pp(m, adev);
3505         }
3506
3507         pm_runtime_mark_last_busy(dev->dev);
3508         pm_runtime_put_autosuspend(dev->dev);
3509
3510         return r;
3511 }
3512
3513 static const struct drm_info_list amdgpu_pm_info_list[] = {
3514         {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3515 };
3516 #endif
3517
3518 int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3519 {
3520 #if defined(CONFIG_DEBUG_FS)
3521         return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
3522 #else
3523         return 0;
3524 #endif
3525 }