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