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