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