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