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