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