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