Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / drivers / gpu / drm / amd / pm / amdgpu_pm.c
CommitLineData
d38ceaf9 1/*
9ce6aae1
AD
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
d38ceaf9
AD
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 */
fdf2f6c5 25
d38ceaf9
AD
26#include "amdgpu.h"
27#include "amdgpu_drv.h"
28#include "amdgpu_pm.h"
29#include "amdgpu_dpm.h"
30#include "atom.h"
fdf2f6c5 31#include <linux/pci.h>
d38ceaf9
AD
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
ddf74e79 34#include <linux/nospec.h>
b9a9294b 35#include <linux/pm_runtime.h>
517cb957 36#include <asm/processor.h>
1b5708ff 37
3e38b634
EQ
38#define MAX_NUM_OF_FEATURES_PER_SUBSET 8
39#define MAX_NUM_OF_SUBSETS 8
40
166a3c73
YW
41#define DEVICE_ATTR_IS(_name) (attr_id == device_attr_id__##_name)
42
3e38b634
EQ
43struct od_attribute {
44 struct kobj_attribute attribute;
45 struct list_head entry;
46};
47
48struct od_kobj {
49 struct kobject kobj;
50 struct list_head entry;
51 struct list_head attribute;
52 void *priv;
53};
54
55struct od_feature_ops {
56 umode_t (*is_visible)(struct amdgpu_device *adev);
57 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
58 char *buf);
59 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
60 const char *buf, size_t count);
61};
62
63struct od_feature_item {
64 const char *name;
65 struct od_feature_ops ops;
66};
67
68struct od_feature_container {
69 char *name;
70 struct od_feature_ops ops;
71 struct od_feature_item sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
72};
73
74struct od_feature_set {
75 struct od_feature_container containers[MAX_NUM_OF_SUBSETS];
76};
77
2adc1156
EQ
78static const struct hwmon_temp_label {
79 enum PP_HWMON_TEMP channel;
80 const char *label;
81} temp_label[] = {
82 {PP_TEMP_EDGE, "edge"},
83 {PP_TEMP_JUNCTION, "junction"},
84 {PP_TEMP_MEM, "mem"},
85};
86
3867e370
DP
87const char * const amdgpu_pp_profile_name[] = {
88 "BOOTUP_DEFAULT",
89 "3D_FULL_SCREEN",
90 "POWER_SAVING",
91 "VIDEO",
92 "VR",
93 "COMPUTE",
334682ae
KF
94 "CUSTOM",
95 "WINDOW_3D",
31865e96
PY
96 "CAPPED",
97 "UNCAPPED",
3867e370
DP
98};
99
55aa33c3
LL
100/**
101 * amdgpu_pm_dev_state_check - Check if device can be accessed.
102 * @adev: Target device.
543f6e71 103 * @runpm: Check runpm status for suspend state checks.
55aa33c3
LL
104 *
105 * Checks the state of the @adev for access. Return 0 if the device is
106 * accessible or a negative error code otherwise.
107 */
543f6e71 108static int amdgpu_pm_dev_state_check(struct amdgpu_device *adev, bool runpm)
55aa33c3 109{
543f6e71
LL
110 bool runpm_check = runpm ? adev->in_runpm : false;
111
55aa33c3
LL
112 if (amdgpu_in_reset(adev))
113 return -EPERM;
543f6e71 114 if (adev->in_suspend && !runpm_check)
55aa33c3
LL
115 return -EPERM;
116
117 return 0;
118}
119
120/**
121 * amdgpu_pm_get_access - Check if device can be accessed, resume if needed.
122 * @adev: Target device.
123 *
124 * Checks the state of the @adev for access. Use runtime pm API to resume if
125 * needed. Return 0 if the device is accessible or a negative error code
126 * otherwise.
127 */
128static int amdgpu_pm_get_access(struct amdgpu_device *adev)
129{
130 int ret;
131
543f6e71 132 ret = amdgpu_pm_dev_state_check(adev, true);
55aa33c3
LL
133 if (ret)
134 return ret;
135
136 return pm_runtime_resume_and_get(adev->dev);
137}
138
139/**
140 * amdgpu_pm_get_access_if_active - Check if device is active for access.
141 * @adev: Target device.
142 *
143 * Checks the state of the @adev for access. Use runtime pm API to determine
144 * if device is active. Allow access only if device is active.Return 0 if the
145 * device is accessible or a negative error code otherwise.
146 */
147static int amdgpu_pm_get_access_if_active(struct amdgpu_device *adev)
148{
149 int ret;
150
543f6e71
LL
151 /* Ignore runpm status. If device is in suspended state, deny access */
152 ret = amdgpu_pm_dev_state_check(adev, false);
55aa33c3
LL
153 if (ret)
154 return ret;
155
543f6e71
LL
156 /*
157 * Allow only if device is active. If runpm is disabled also, as in
158 * kernels without CONFIG_PM, allow access.
159 */
55aa33c3 160 ret = pm_runtime_get_if_active(adev->dev);
543f6e71
LL
161 if (!ret)
162 return -EPERM;
55aa33c3
LL
163
164 return 0;
165}
166
167/**
168 * amdgpu_pm_put_access - Put to auto suspend mode after a device access.
169 * @adev: Target device.
170 *
171 * Should be paired with amdgpu_pm_get_access* calls
172 */
173static inline void amdgpu_pm_put_access(struct amdgpu_device *adev)
174{
175 pm_runtime_mark_last_busy(adev->dev);
176 pm_runtime_put_autosuspend(adev->dev);
177}
178
ca8d40ca
AD
179/**
180 * DOC: power_dpm_state
181 *
dc85db25
AD
182 * The power_dpm_state file is a legacy interface and is only provided for
183 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
184 * certain power related parameters. The file power_dpm_state is used for this.
ca8d40ca 185 * It accepts the following arguments:
dc85db25 186 *
ca8d40ca 187 * - battery
dc85db25 188 *
ca8d40ca 189 * - balanced
dc85db25 190 *
ca8d40ca
AD
191 * - performance
192 *
193 * battery
194 *
195 * On older GPUs, the vbios provided a special power state for battery
196 * operation. Selecting battery switched to this state. This is no
197 * longer provided on newer GPUs so the option does nothing in that case.
198 *
199 * balanced
200 *
201 * On older GPUs, the vbios provided a special power state for balanced
202 * operation. Selecting balanced switched to this state. This is no
203 * longer provided on newer GPUs so the option does nothing in that case.
204 *
205 * performance
206 *
207 * On older GPUs, the vbios provided a special power state for performance
208 * operation. Selecting performance switched to this state. This is no
209 * longer provided on newer GPUs so the option does nothing in that case.
210 *
211 */
212
4e01847c
KW
213static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
214 struct device_attribute *attr,
215 char *buf)
d38ceaf9
AD
216{
217 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 218 struct amdgpu_device *adev = drm_to_adev(ddev);
1b5708ff 219 enum amd_pm_state_type pm;
b9a9294b 220 int ret;
1b5708ff 221
55aa33c3
LL
222 ret = amdgpu_pm_get_access_if_active(adev);
223 if (ret)
224 return ret;
b9a9294b 225
79c65f3f 226 amdgpu_dpm_get_current_power_state(adev, &pm);
d38ceaf9 227
55aa33c3 228 amdgpu_pm_put_access(adev);
b9a9294b 229
a9ca9bb3
TT
230 return sysfs_emit(buf, "%s\n",
231 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
232 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
d38ceaf9
AD
233}
234
4e01847c
KW
235static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
236 struct device_attribute *attr,
237 const char *buf,
238 size_t count)
d38ceaf9
AD
239{
240 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 241 struct amdgpu_device *adev = drm_to_adev(ddev);
1b5708ff 242 enum amd_pm_state_type state;
b9a9294b 243 int ret;
d38ceaf9 244
d38ceaf9 245 if (strncmp("battery", buf, strlen("battery")) == 0)
1b5708ff 246 state = POWER_STATE_TYPE_BATTERY;
d38ceaf9 247 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
1b5708ff 248 state = POWER_STATE_TYPE_BALANCED;
d38ceaf9 249 else if (strncmp("performance", buf, strlen("performance")) == 0)
1b5708ff 250 state = POWER_STATE_TYPE_PERFORMANCE;
27414cd4
AD
251 else
252 return -EINVAL;
d38ceaf9 253
55aa33c3 254 ret = amdgpu_pm_get_access(adev);
ded57e49 255 if (ret < 0)
b9a9294b
AD
256 return ret;
257
79c65f3f 258 amdgpu_dpm_set_power_state(adev, state);
1b5708ff 259
55aa33c3 260 amdgpu_pm_put_access(adev);
b9a9294b 261
d38ceaf9
AD
262 return count;
263}
264
8567f681
AD
265
266/**
267 * DOC: power_dpm_force_performance_level
268 *
269 * The amdgpu driver provides a sysfs API for adjusting certain power
270 * related parameters. The file power_dpm_force_performance_level is
271 * used for this. It accepts the following arguments:
dc85db25 272 *
8567f681 273 * - auto
dc85db25 274 *
8567f681 275 * - low
dc85db25 276 *
8567f681 277 * - high
dc85db25 278 *
8567f681 279 * - manual
dc85db25 280 *
8567f681 281 * - profile_standard
dc85db25 282 *
8567f681 283 * - profile_min_sclk
dc85db25 284 *
8567f681 285 * - profile_min_mclk
dc85db25 286 *
8567f681
AD
287 * - profile_peak
288 *
289 * auto
290 *
291 * When auto is selected, the driver will attempt to dynamically select
292 * the optimal power profile for current conditions in the driver.
293 *
294 * low
295 *
296 * When low is selected, the clocks are forced to the lowest power state.
297 *
298 * high
299 *
300 * When high is selected, the clocks are forced to the highest power state.
301 *
302 * manual
303 *
304 * When manual is selected, the user can manually adjust which power states
305 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
306 * and pp_dpm_pcie files and adjust the power state transition heuristics
307 * via the pp_power_profile_mode sysfs file.
308 *
309 * profile_standard
310 * profile_min_sclk
311 * profile_min_mclk
312 * profile_peak
313 *
314 * When the profiling modes are selected, clock and power gating are
315 * disabled and the clocks are set for different profiling cases. This
316 * mode is recommended for profiling specific work loads where you do
317 * not want clock or power gating for clock fluctuation to interfere
318 * with your results. profile_standard sets the clocks to a fixed clock
319 * level which varies from asic to asic. profile_min_sclk forces the sclk
320 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
321 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
322 *
323 */
324
4e01847c
KW
325static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
326 struct device_attribute *attr,
327 char *buf)
d38ceaf9
AD
328{
329 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 330 struct amdgpu_device *adev = drm_to_adev(ddev);
cd4d7464 331 enum amd_dpm_forced_level level = 0xff;
b9a9294b 332 int ret;
d38ceaf9 333
55aa33c3
LL
334 ret = amdgpu_pm_get_access_if_active(adev);
335 if (ret)
336 return ret;
0c67df48 337
79c65f3f 338 level = amdgpu_dpm_get_performance_level(adev);
cd4d7464 339
55aa33c3 340 amdgpu_pm_put_access(adev);
b9a9294b 341
a9ca9bb3
TT
342 return sysfs_emit(buf, "%s\n",
343 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
344 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
345 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
346 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
347 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
348 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
349 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
350 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
351 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
352 "unknown");
d38ceaf9
AD
353}
354
4e01847c
KW
355static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
356 struct device_attribute *attr,
357 const char *buf,
358 size_t count)
d38ceaf9
AD
359{
360 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 361 struct amdgpu_device *adev = drm_to_adev(ddev);
e5d03ac2 362 enum amd_dpm_forced_level level;
d38ceaf9
AD
363 int ret = 0;
364
d38ceaf9 365 if (strncmp("low", buf, strlen("low")) == 0) {
e5d03ac2 366 level = AMD_DPM_FORCED_LEVEL_LOW;
d38ceaf9 367 } else if (strncmp("high", buf, strlen("high")) == 0) {
e5d03ac2 368 level = AMD_DPM_FORCED_LEVEL_HIGH;
d38ceaf9 369 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
e5d03ac2 370 level = AMD_DPM_FORCED_LEVEL_AUTO;
f3898ea1 371 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
e5d03ac2 372 level = AMD_DPM_FORCED_LEVEL_MANUAL;
570272d2
RZ
373 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
374 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
375 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
376 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
377 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
378 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
379 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
380 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
381 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
382 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
6be64246
LL
383 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
384 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
570272d2 385 } else {
b9a9294b 386 return -EINVAL;
d38ceaf9 387 }
1b5708ff 388
55aa33c3 389 ret = amdgpu_pm_get_access(adev);
ded57e49 390 if (ret < 0)
b9a9294b
AD
391 return ret;
392
8cda7a4f 393 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
79c65f3f 394 if (amdgpu_dpm_force_performance_level(adev, level)) {
55aa33c3 395 amdgpu_pm_put_access(adev);
8cda7a4f 396 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
79c65f3f 397 return -EINVAL;
d38ceaf9 398 }
8cda7a4f
AD
399 /* override whatever a user ctx may have set */
400 adev->pm.stable_pstate_ctx = NULL;
401 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
79c65f3f 402
55aa33c3 403 amdgpu_pm_put_access(adev);
570272d2 404
f1403342 405 return count;
d38ceaf9
AD
406}
407
f3898ea1
EH
408static ssize_t amdgpu_get_pp_num_states(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 413 struct amdgpu_device *adev = drm_to_adev(ddev);
f3898ea1 414 struct pp_states_info data;
09b6744c
DP
415 uint32_t i;
416 int buf_len, ret;
f3898ea1 417
55aa33c3
LL
418 ret = amdgpu_pm_get_access_if_active(adev);
419 if (ret)
420 return ret;
b9a9294b 421
79c65f3f 422 if (amdgpu_dpm_get_pp_num_states(adev, &data))
6f81b2d0 423 memset(&data, 0, sizeof(data));
f3898ea1 424
55aa33c3 425 amdgpu_pm_put_access(adev);
b9a9294b 426
09b6744c 427 buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
f3898ea1 428 for (i = 0; i < data.nums; i++)
09b6744c 429 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
f3898ea1
EH
430 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
431 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
432 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
433 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
434
435 return buf_len;
436}
437
438static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
439 struct device_attribute *attr,
440 char *buf)
441{
442 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 443 struct amdgpu_device *adev = drm_to_adev(ddev);
2b24c199 444 struct pp_states_info data = {0};
f3898ea1 445 enum amd_pm_state_type pm = 0;
ea2d0bf8 446 int i = 0, ret = 0;
f3898ea1 447
55aa33c3
LL
448 ret = amdgpu_pm_get_access_if_active(adev);
449 if (ret)
450 return ret;
b9a9294b 451
79c65f3f
EQ
452 amdgpu_dpm_get_current_power_state(adev, &pm);
453
454 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
f3898ea1 455
55aa33c3 456 amdgpu_pm_put_access(adev);
b9a9294b 457
79c65f3f
EQ
458 if (ret)
459 return ret;
460
ea2d0bf8
KW
461 for (i = 0; i < data.nums; i++) {
462 if (pm == data.states[i])
463 break;
f3898ea1
EH
464 }
465
ea2d0bf8
KW
466 if (i == data.nums)
467 i = -EINVAL;
468
a9ca9bb3 469 return sysfs_emit(buf, "%d\n", i);
f3898ea1
EH
470}
471
472static ssize_t amdgpu_get_pp_force_state(struct device *dev,
473 struct device_attribute *attr,
474 char *buf)
475{
476 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 477 struct amdgpu_device *adev = drm_to_adev(ddev);
48b270bb 478
d698a2c4 479 if (adev->pm.pp_force_state_enabled)
cd4d7464
RZ
480 return amdgpu_get_pp_cur_state(dev, attr, buf);
481 else
a9ca9bb3 482 return sysfs_emit(buf, "\n");
f3898ea1
EH
483}
484
485static ssize_t amdgpu_set_pp_force_state(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf,
488 size_t count)
489{
490 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 491 struct amdgpu_device *adev = drm_to_adev(ddev);
f3898ea1 492 enum amd_pm_state_type state = 0;
79c65f3f 493 struct pp_states_info data;
041bf022 494 unsigned long idx;
f3898ea1
EH
495 int ret;
496
d698a2c4 497 adev->pm.pp_force_state_enabled = false;
b9a9294b 498
79c65f3f
EQ
499 if (strlen(buf) == 1)
500 return count;
f3898ea1 501
79c65f3f
EQ
502 ret = kstrtoul(buf, 0, &idx);
503 if (ret || idx >= ARRAY_SIZE(data.states))
504 return -EINVAL;
b9a9294b 505
79c65f3f 506 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
b9a9294b 507
55aa33c3 508 ret = amdgpu_pm_get_access(adev);
ded57e49 509 if (ret < 0)
79c65f3f 510 return ret;
79c65f3f
EQ
511
512 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
513 if (ret)
514 goto err_out;
515
516 state = data.states[idx];
517
518 /* only set user selected power states */
519 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
520 state != POWER_STATE_TYPE_DEFAULT) {
521 ret = amdgpu_dpm_dispatch_task(adev,
522 AMD_PP_TASK_ENABLE_USER_STATE, &state);
523 if (ret)
524 goto err_out;
525
d698a2c4 526 adev->pm.pp_force_state_enabled = true;
f3898ea1 527 }
b9a9294b 528
55aa33c3 529 amdgpu_pm_put_access(adev);
79c65f3f 530
f3898ea1 531 return count;
79c65f3f
EQ
532
533err_out:
55aa33c3
LL
534 amdgpu_pm_put_access(adev);
535
79c65f3f 536 return ret;
f3898ea1
EH
537}
538
d54bb40f
AD
539/**
540 * DOC: pp_table
541 *
542 * The amdgpu driver provides a sysfs API for uploading new powerplay
543 * tables. The file pp_table is used for this. Reading the file
544 * will dump the current power play table. Writing to the file
545 * will attempt to upload a new powerplay table and re-initialize
546 * powerplay using that new table.
547 *
548 */
549
f3898ea1
EH
550static ssize_t amdgpu_get_pp_table(struct device *dev,
551 struct device_attribute *attr,
552 char *buf)
553{
554 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 555 struct amdgpu_device *adev = drm_to_adev(ddev);
f3898ea1 556 char *table = NULL;
b9a9294b 557 int size, ret;
f3898ea1 558
55aa33c3
LL
559 ret = amdgpu_pm_get_access_if_active(adev);
560 if (ret)
561 return ret;
b9a9294b 562
79c65f3f
EQ
563 size = amdgpu_dpm_get_pp_table(adev, &table);
564
55aa33c3 565 amdgpu_pm_put_access(adev);
79c65f3f
EQ
566
567 if (size <= 0)
568 return size;
f3898ea1
EH
569
570 if (size >= PAGE_SIZE)
571 size = PAGE_SIZE - 1;
572
1684d3ba 573 memcpy(buf, table, size);
f3898ea1
EH
574
575 return size;
576}
577
578static ssize_t amdgpu_set_pp_table(struct device *dev,
579 struct device_attribute *attr,
580 const char *buf,
581 size_t count)
582{
583 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 584 struct amdgpu_device *adev = drm_to_adev(ddev);
289921b0 585 int ret = 0;
f3898ea1 586
55aa33c3 587 ret = amdgpu_pm_get_access(adev);
ded57e49 588 if (ret < 0)
b9a9294b
AD
589 return ret;
590
8f4828d0 591 ret = amdgpu_dpm_set_pp_table(adev, buf, count);
f3898ea1 592
55aa33c3 593 amdgpu_pm_put_access(adev);
b9a9294b 594
79c65f3f
EQ
595 if (ret)
596 return ret;
597
f3898ea1
EH
598 return count;
599}
600
4e418c34
AD
601/**
602 * DOC: pp_od_clk_voltage
603 *
604 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
605 * in each power level within a power state. The pp_od_clk_voltage is used for
606 * this.
607 *
ccda42a4
AD
608 * Note that the actual memory controller clock rate are exposed, not
609 * the effective memory clock of the DRAMs. To translate it, use the
610 * following formula:
611 *
612 * Clock conversion (Mhz):
613 *
614 * HBM: effective_memory_clock = memory_controller_clock * 1
615 *
616 * G5: effective_memory_clock = memory_controller_clock * 1
617 *
618 * G6: effective_memory_clock = memory_controller_clock * 2
619 *
620 * DRAM data rate (MT/s):
621 *
622 * HBM: effective_memory_clock * 2 = data_rate
623 *
624 * G5: effective_memory_clock * 4 = data_rate
625 *
626 * G6: effective_memory_clock * 8 = data_rate
627 *
628 * Bandwidth (MB/s):
629 *
630 * data_rate * vram_bit_width / 8 = memory_bandwidth
631 *
632 * Some examples:
633 *
634 * G5 on RX460:
635 *
636 * memory_controller_clock = 1750 Mhz
637 *
638 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
639 *
640 * data rate = 1750 * 4 = 7000 MT/s
641 *
642 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
643 *
644 * G6 on RX5700:
645 *
646 * memory_controller_clock = 875 Mhz
647 *
648 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
649 *
650 * data rate = 1750 * 8 = 14000 MT/s
651 *
652 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
653 *
d5bf2653
EQ
654 * < For Vega10 and previous ASICs >
655 *
4e418c34 656 * Reading the file will display:
dc85db25 657 *
4e418c34 658 * - a list of engine clock levels and voltages labeled OD_SCLK
dc85db25 659 *
4e418c34 660 * - a list of memory clock levels and voltages labeled OD_MCLK
dc85db25 661 *
4e418c34
AD
662 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
663 *
664 * To manually adjust these settings, first select manual using
665 * power_dpm_force_performance_level. Enter a new value for each
666 * level by writing a string that contains "s/m level clock voltage" to
667 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
668 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
669 * 810 mV. When you have edited all of the states as needed, write
670 * "c" (commit) to the file to commit your changes. If you want to reset to the
671 * default power levels, write "r" (reset) to the file to reset them.
672 *
d5bf2653 673 *
bd09331a 674 * < For Vega20 and newer ASICs >
d5bf2653
EQ
675 *
676 * Reading the file will display:
677 *
678 * - minimum and maximum engine clock labeled OD_SCLK
679 *
37a58f69
EQ
680 * - minimum(not available for Vega20 and Navi1x) and maximum memory
681 * clock labeled OD_MCLK
d5bf2653 682 *
b1f82cb2 683 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
8f4f5f0b
EQ
684 * They can be used to calibrate the sclk voltage curve. This is
685 * available for Vega20 and NV1X.
686 *
a2b6df4f 687 * - voltage offset(in mV) applied on target voltage calculation.
e835bc26
EQ
688 * This is available for Sienna Cichlid, Navy Flounder, Dimgrey
689 * Cavefish and some later SMU13 ASICs. For these ASICs, the target
690 * voltage calculation can be illustrated by "voltage = voltage
691 * calculated from v/f curve + overdrive vddgfx offset"
a2b6df4f 692 *
e835bc26
EQ
693 * - a list of valid ranges for sclk, mclk, voltage curve points
694 * or voltage offset labeled OD_RANGE
d5bf2653 695 *
0487bbb4
AD
696 * < For APUs >
697 *
698 * Reading the file will display:
699 *
700 * - minimum and maximum engine clock labeled OD_SCLK
701 *
702 * - a list of valid ranges for sclk labeled OD_RANGE
703 *
3dc8077f
AD
704 * < For VanGogh >
705 *
706 * Reading the file will display:
707 *
708 * - minimum and maximum engine clock labeled OD_SCLK
709 * - minimum and maximum core clocks labeled OD_CCLK
710 *
711 * - a list of valid ranges for sclk and cclk labeled OD_RANGE
712 *
d5bf2653
EQ
713 * To manually adjust these settings:
714 *
715 * - First select manual using power_dpm_force_performance_level
716 *
717 * - For clock frequency setting, enter a new value by writing a
718 * string that contains "s/m index clock" to the file. The index
719 * should be 0 if to set minimum clock. And 1 if to set maximum
720 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
3dc8077f
AD
721 * "m 1 800" will update maximum mclk to be 800Mhz. For core
722 * clocks on VanGogh, the string contains "p core index clock".
723 * E.g., "p 2 0 800" would set the minimum core clock on core
724 * 2 to 800Mhz.
d5bf2653 725 *
e835bc26
EQ
726 * For sclk voltage curve supported by Vega20 and NV1X, enter the new
727 * values by writing a string that contains "vc point clock voltage"
728 * to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
729 * 600" will update point1 with clock set as 300Mhz and voltage as 600mV.
730 * "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
731 * voltage 1000mV.
732 *
733 * For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
734 * Cavefish and some later SMU13 ASICs, enter the new value by writing a
735 * string that contains "vo offset". E.g., "vo -10" will update the extra
736 * voltage offset applied to the whole v/f curve line as -10mv.
a2b6df4f 737 *
d5bf2653
EQ
738 * - When you have edited all of the states as needed, write "c" (commit)
739 * to the file to commit your changes
740 *
741 * - If you want to reset to the default power levels, write "r" (reset)
742 * to the file to reset them
743 *
4e418c34
AD
744 */
745
e3933f26
RZ
746static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
747 struct device_attribute *attr,
748 const char *buf,
749 size_t count)
750{
751 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 752 struct amdgpu_device *adev = drm_to_adev(ddev);
e3933f26
RZ
753 int ret;
754 uint32_t parameter_size = 0;
755 long parameter[64];
756 char buf_cpy[128];
757 char *tmp_str;
758 char *sub_str;
759 const char delimiter[3] = {' ', '\n', '\0'};
760 uint32_t type;
761
08e9ebc7 762 if (count > 127 || count == 0)
e3933f26
RZ
763 return -EINVAL;
764
765 if (*buf == 's')
766 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
0d90d0dd
HR
767 else if (*buf == 'p')
768 type = PP_OD_EDIT_CCLK_VDDC_TABLE;
e3933f26
RZ
769 else if (*buf == 'm')
770 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
e1b3bcaa 771 else if (*buf == 'r')
e3933f26
RZ
772 type = PP_OD_RESTORE_DEFAULT_TABLE;
773 else if (*buf == 'c')
774 type = PP_OD_COMMIT_DPM_TABLE;
d5bf2653
EQ
775 else if (!strncmp(buf, "vc", 2))
776 type = PP_OD_EDIT_VDDC_CURVE;
a2b6df4f
EQ
777 else if (!strncmp(buf, "vo", 2))
778 type = PP_OD_EDIT_VDDGFX_OFFSET;
e3933f26
RZ
779 else
780 return -EINVAL;
781
08e9ebc7
BN
782 memcpy(buf_cpy, buf, count);
783 buf_cpy[count] = 0;
e3933f26
RZ
784
785 tmp_str = buf_cpy;
786
a2b6df4f
EQ
787 if ((type == PP_OD_EDIT_VDDC_CURVE) ||
788 (type == PP_OD_EDIT_VDDGFX_OFFSET))
d5bf2653 789 tmp_str++;
e3933f26
RZ
790 while (isspace(*++tmp_str));
791
ce7c1d04 792 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
aec1d870
MC
793 if (strlen(sub_str) == 0)
794 continue;
e3933f26
RZ
795 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
796 if (ret)
797 return -EINVAL;
798 parameter_size++;
799
08e9ebc7
BN
800 if (!tmp_str)
801 break;
802
e3933f26
RZ
803 while (isspace(*tmp_str))
804 tmp_str++;
805 }
806
55aa33c3 807 ret = amdgpu_pm_get_access(adev);
ded57e49 808 if (ret < 0)
b9a9294b
AD
809 return ret;
810
79c65f3f
EQ
811 if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
812 type,
813 parameter,
814 parameter_size))
815 goto err_out;
12a6727d 816
79c65f3f
EQ
817 if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
818 parameter, parameter_size))
819 goto err_out;
e388cc47 820
8f4828d0 821 if (type == PP_OD_COMMIT_DPM_TABLE) {
79c65f3f
EQ
822 if (amdgpu_dpm_dispatch_task(adev,
823 AMD_PP_TASK_READJUST_POWER_STATE,
824 NULL))
825 goto err_out;
e3933f26 826 }
8f4828d0 827
55aa33c3 828 amdgpu_pm_put_access(adev);
e3933f26 829
f1403342 830 return count;
79c65f3f
EQ
831
832err_out:
55aa33c3
LL
833 amdgpu_pm_put_access(adev);
834
79c65f3f 835 return -EINVAL;
e3933f26
RZ
836}
837
838static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
839 struct device_attribute *attr,
840 char *buf)
841{
842 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 843 struct amdgpu_device *adev = drm_to_adev(ddev);
c8cb19c7 844 int size = 0;
b9a9294b 845 int ret;
c8cb19c7
DP
846 enum pp_clock_type od_clocks[6] = {
847 OD_SCLK,
848 OD_MCLK,
849 OD_VDDC_CURVE,
850 OD_RANGE,
851 OD_VDDGFX_OFFSET,
852 OD_CCLK,
853 };
854 uint clk_index;
e3933f26 855
55aa33c3
LL
856 ret = amdgpu_pm_get_access_if_active(adev);
857 if (ret)
858 return ret;
b9a9294b 859
c8cb19c7
DP
860 for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
861 ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
862 if (ret)
863 break;
e3933f26 864 }
c8cb19c7
DP
865 if (ret == -ENOENT) {
866 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
ab22ecab
JE
867 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
868 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
869 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
870 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
871 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
c8cb19c7
DP
872 }
873
874 if (size == 0)
875 size = sysfs_emit(buf, "\n");
876
55aa33c3 877 amdgpu_pm_put_access(adev);
e3933f26 878
b9a9294b 879 return size;
e3933f26
RZ
880}
881
7ca881a8 882/**
98eb03bb 883 * DOC: pp_features
7ca881a8
EQ
884 *
885 * The amdgpu driver provides a sysfs API for adjusting what powerplay
98eb03bb 886 * features to be enabled. The file pp_features is used for this. And
7ca881a8
EQ
887 * this is only available for Vega10 and later dGPUs.
888 *
889 * Reading back the file will show you the followings:
890 * - Current ppfeature masks
891 * - List of the all supported powerplay features with their naming,
892 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
893 *
894 * To manually enable or disable a specific feature, just set or clear
895 * the corresponding bit from original ppfeature masks and input the
896 * new ppfeature masks.
897 */
4e01847c
KW
898static ssize_t amdgpu_set_pp_features(struct device *dev,
899 struct device_attribute *attr,
900 const char *buf,
901 size_t count)
7ca881a8
EQ
902{
903 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 904 struct amdgpu_device *adev = drm_to_adev(ddev);
7ca881a8
EQ
905 uint64_t featuremask;
906 int ret;
907
908 ret = kstrtou64(buf, 0, &featuremask);
909 if (ret)
910 return -EINVAL;
911
55aa33c3 912 ret = amdgpu_pm_get_access(adev);
ded57e49 913 if (ret < 0)
b9a9294b
AD
914 return ret;
915
79c65f3f
EQ
916 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
917
55aa33c3 918 amdgpu_pm_put_access(adev);
7ca881a8 919
79c65f3f
EQ
920 if (ret)
921 return -EINVAL;
922
7ca881a8
EQ
923 return count;
924}
925
4e01847c
KW
926static ssize_t amdgpu_get_pp_features(struct device *dev,
927 struct device_attribute *attr,
928 char *buf)
7ca881a8
EQ
929{
930 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 931 struct amdgpu_device *adev = drm_to_adev(ddev);
b9a9294b
AD
932 ssize_t size;
933 int ret;
7ca881a8 934
55aa33c3
LL
935 ret = amdgpu_pm_get_access_if_active(adev);
936 if (ret)
937 return ret;
b9a9294b 938
79c65f3f
EQ
939 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
940 if (size <= 0)
09b6744c 941 size = sysfs_emit(buf, "\n");
b9a9294b 942
55aa33c3 943 amdgpu_pm_put_access(adev);
7ca881a8 944
b9a9294b 945 return size;
7ca881a8
EQ
946}
947
271dc908 948/**
a667b75c 949 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
271dc908
AD
950 *
951 * The amdgpu driver provides a sysfs API for adjusting what power levels
952 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
d7e28e2d
EQ
953 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
954 * this.
d7337ca2 955 *
d7e28e2d
EQ
956 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
957 * Vega10 and later ASICs.
828e37ef 958 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
271dc908
AD
959 *
960 * Reading back the files will show you the available power levels within
615585d0
LL
961 * the power state and the clock information for those levels. If deep sleep is
962 * applied to a clock, the level will be denoted by a special level 'S:'
bb619539
HC
963 * E.g., ::
964 *
965 * S: 19Mhz *
966 * 0: 615Mhz
967 * 1: 800Mhz
968 * 2: 888Mhz
969 * 3: 1000Mhz
615585d0 970 *
271dc908
AD
971 *
972 * To manually adjust these states, first select manual using
48edde39 973 * power_dpm_force_performance_level.
a667b75c 974 * Secondly, enter a new value for each level by inputing a string that
48edde39 975 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
a667b75c
AD
976 * E.g.,
977 *
978 * .. code-block:: bash
979 *
980 * echo "4 5 6" > pp_dpm_sclk
981 *
982 * will enable sclk levels 4, 5, and 6.
d7e28e2d
EQ
983 *
984 * NOTE: change to the dcefclk max dpm level is not supported now
271dc908
AD
985 */
986
2ea092e5
DP
987static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
988 enum pp_clock_type type,
f3898ea1
EH
989 char *buf)
990{
991 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 992 struct amdgpu_device *adev = drm_to_adev(ddev);
c8cb19c7
DP
993 int size = 0;
994 int ret = 0;
f3898ea1 995
55aa33c3
LL
996 ret = amdgpu_pm_get_access_if_active(adev);
997 if (ret)
998 return ret;
b9a9294b 999
c8cb19c7
DP
1000 ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1001 if (ret == -ENOENT)
1002 size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1003
1004 if (size == 0)
09b6744c 1005 size = sysfs_emit(buf, "\n");
b9a9294b 1006
55aa33c3 1007 amdgpu_pm_put_access(adev);
b9a9294b
AD
1008
1009 return size;
f3898ea1
EH
1010}
1011
4b4bd048
KC
1012/*
1013 * Worst case: 32 bits individually specified, in octal at 12 characters
1014 * per line (+1 for \n).
1015 */
1016#define AMDGPU_MASK_BUF_MAX (32 * 13)
1017
1018static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
f3898ea1 1019{
f3898ea1 1020 int ret;
c915ef89 1021 unsigned long level;
48edde39 1022 char *sub_str = NULL;
1023 char *tmp;
4b4bd048 1024 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
48edde39 1025 const char delimiter[3] = {' ', '\n', '\0'};
4b4bd048 1026 size_t bytes;
f3898ea1 1027
4b4bd048
KC
1028 *mask = 0;
1029
1030 bytes = min(count, sizeof(buf_cpy) - 1);
1031 memcpy(buf_cpy, buf, bytes);
1032 buf_cpy[bytes] = '\0';
48edde39 1033 tmp = buf_cpy;
ce7c1d04 1034 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
48edde39 1035 if (strlen(sub_str)) {
c915ef89
DC
1036 ret = kstrtoul(sub_str, 0, &level);
1037 if (ret || level > 31)
4b4bd048
KC
1038 return -EINVAL;
1039 *mask |= 1 << level;
48edde39 1040 } else
1041 break;
f3898ea1 1042 }
4b4bd048
KC
1043
1044 return 0;
1045}
1046
2ea092e5
DP
1047static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1048 enum pp_clock_type type,
4b4bd048
KC
1049 const char *buf,
1050 size_t count)
1051{
1052 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1053 struct amdgpu_device *adev = drm_to_adev(ddev);
4b4bd048
KC
1054 int ret;
1055 uint32_t mask = 0;
1056
1057 ret = amdgpu_read_mask(buf, count, &mask);
1058 if (ret)
1059 return ret;
1060
55aa33c3 1061 ret = amdgpu_pm_get_access(adev);
ded57e49 1062 if (ret < 0)
b9a9294b
AD
1063 return ret;
1064
79c65f3f 1065 ret = amdgpu_dpm_force_clock_level(adev, type, mask);
241dbbb1 1066
55aa33c3 1067 amdgpu_pm_put_access(adev);
b9a9294b 1068
241dbbb1
EQ
1069 if (ret)
1070 return -EINVAL;
cd4d7464 1071
f3898ea1
EH
1072 return count;
1073}
1074
2ea092e5 1075static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
f3898ea1
EH
1076 struct device_attribute *attr,
1077 char *buf)
1078{
2ea092e5
DP
1079 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1080}
b9a9294b 1081
2ea092e5
DP
1082static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1083 struct device_attribute *attr,
1084 const char *buf,
1085 size_t count)
1086{
1087 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1088}
b9a9294b 1089
2ea092e5
DP
1090static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1091 struct device_attribute *attr,
1092 char *buf)
1093{
1094 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
f3898ea1
EH
1095}
1096
1097static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1098 struct device_attribute *attr,
1099 const char *buf,
1100 size_t count)
1101{
2ea092e5 1102 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
f3898ea1
EH
1103}
1104
d7337ca2
EQ
1105static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1106 struct device_attribute *attr,
1107 char *buf)
1108{
2ea092e5 1109 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
d7337ca2
EQ
1110}
1111
1112static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1113 struct device_attribute *attr,
1114 const char *buf,
1115 size_t count)
1116{
2ea092e5 1117 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
d7337ca2
EQ
1118}
1119
828e37ef
EQ
1120static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1121 struct device_attribute *attr,
1122 char *buf)
1123{
2ea092e5 1124 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
828e37ef
EQ
1125}
1126
1127static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1128 struct device_attribute *attr,
1129 const char *buf,
1130 size_t count)
1131{
2ea092e5 1132 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
828e37ef
EQ
1133}
1134
9577b0ec
XD
1135static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1136 struct device_attribute *attr,
1137 char *buf)
1138{
2ea092e5 1139 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
9577b0ec
XD
1140}
1141
1142static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1143 struct device_attribute *attr,
1144 const char *buf,
1145 size_t count)
1146{
2ea092e5 1147 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
9577b0ec
XD
1148}
1149
d7001e72
TL
1150static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1151 struct device_attribute *attr,
1152 char *buf)
1153{
1154 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1155}
1156
1157static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1158 struct device_attribute *attr,
1159 const char *buf,
1160 size_t count)
1161{
1162 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1163}
1164
9577b0ec
XD
1165static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1166 struct device_attribute *attr,
1167 char *buf)
1168{
2ea092e5 1169 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
9577b0ec
XD
1170}
1171
1172static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1173 struct device_attribute *attr,
1174 const char *buf,
1175 size_t count)
1176{
2ea092e5 1177 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
9577b0ec
XD
1178}
1179
d7001e72
TL
1180static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1181 struct device_attribute *attr,
1182 char *buf)
1183{
1184 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1185}
1186
1187static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1188 struct device_attribute *attr,
1189 const char *buf,
1190 size_t count)
1191{
1192 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1193}
1194
d7e28e2d
EQ
1195static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1196 struct device_attribute *attr,
1197 char *buf)
1198{
2ea092e5 1199 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
d7e28e2d
EQ
1200}
1201
1202static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1203 struct device_attribute *attr,
1204 const char *buf,
1205 size_t count)
1206{
2ea092e5 1207 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
d7e28e2d
EQ
1208}
1209
f3898ea1
EH
1210static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1211 struct device_attribute *attr,
1212 char *buf)
1213{
2ea092e5 1214 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
f3898ea1
EH
1215}
1216
1217static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1218 struct device_attribute *attr,
1219 const char *buf,
1220 size_t count)
1221{
2ea092e5 1222 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
f3898ea1
EH
1223}
1224
428bafa8
EH
1225static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1226 struct device_attribute *attr,
1227 char *buf)
1228{
1229 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1230 struct amdgpu_device *adev = drm_to_adev(ddev);
428bafa8 1231 uint32_t value = 0;
b9a9294b 1232 int ret;
428bafa8 1233
55aa33c3
LL
1234 ret = amdgpu_pm_get_access_if_active(adev);
1235 if (ret)
1236 return ret;
b9a9294b 1237
79c65f3f 1238 value = amdgpu_dpm_get_sclk_od(adev);
428bafa8 1239
55aa33c3 1240 amdgpu_pm_put_access(adev);
b9a9294b 1241
a9ca9bb3 1242 return sysfs_emit(buf, "%d\n", value);
428bafa8
EH
1243}
1244
1245static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1246 struct device_attribute *attr,
1247 const char *buf,
1248 size_t count)
1249{
1250 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1251 struct amdgpu_device *adev = drm_to_adev(ddev);
428bafa8
EH
1252 int ret;
1253 long int value;
1254
1255 ret = kstrtol(buf, 0, &value);
1256
b9a9294b
AD
1257 if (ret)
1258 return -EINVAL;
1259
55aa33c3 1260 ret = amdgpu_pm_get_access(adev);
ded57e49 1261 if (ret < 0)
b9a9294b 1262 return ret;
428bafa8 1263
79c65f3f 1264 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
428bafa8 1265
55aa33c3 1266 amdgpu_pm_put_access(adev);
b9a9294b 1267
428bafa8
EH
1268 return count;
1269}
1270
f2bdc05f
EH
1271static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1272 struct device_attribute *attr,
1273 char *buf)
1274{
1275 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1276 struct amdgpu_device *adev = drm_to_adev(ddev);
f2bdc05f 1277 uint32_t value = 0;
b9a9294b 1278 int ret;
f2bdc05f 1279
55aa33c3
LL
1280 ret = amdgpu_pm_get_access_if_active(adev);
1281 if (ret)
1282 return ret;
b9a9294b 1283
79c65f3f 1284 value = amdgpu_dpm_get_mclk_od(adev);
f2bdc05f 1285
55aa33c3 1286 amdgpu_pm_put_access(adev);
b9a9294b 1287
a9ca9bb3 1288 return sysfs_emit(buf, "%d\n", value);
f2bdc05f
EH
1289}
1290
1291static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1292 struct device_attribute *attr,
1293 const char *buf,
1294 size_t count)
1295{
1296 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1297 struct amdgpu_device *adev = drm_to_adev(ddev);
f2bdc05f
EH
1298 int ret;
1299 long int value;
1300
1301 ret = kstrtol(buf, 0, &value);
1302
b9a9294b
AD
1303 if (ret)
1304 return -EINVAL;
1305
55aa33c3 1306 ret = amdgpu_pm_get_access(adev);
ded57e49 1307 if (ret < 0)
b9a9294b 1308 return ret;
f2bdc05f 1309
79c65f3f 1310 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
f2bdc05f 1311
55aa33c3 1312 amdgpu_pm_put_access(adev);
b9a9294b 1313
f2bdc05f
EH
1314 return count;
1315}
1316
6b2576f5
AD
1317/**
1318 * DOC: pp_power_profile_mode
1319 *
1320 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1321 * related to switching between power levels in a power state. The file
1322 * pp_power_profile_mode is used for this.
1323 *
1324 * Reading this file outputs a list of all of the predefined power profiles
1325 * and the relevant heuristics settings for that profile.
1326 *
1327 * To select a profile or create a custom profile, first select manual using
1328 * power_dpm_force_performance_level. Writing the number of a predefined
1329 * profile to pp_power_profile_mode will enable those heuristics. To
1330 * create a custom set of heuristics, write a string of numbers to the file
1331 * starting with the number of the custom profile along with a setting
1332 * for each heuristic parameter. Due to differences across asic families
1443dd3c
AD
1333 * the heuristic parameters vary from family to family. Additionally,
1334 * you can apply the custom heuristics to different clock domains. Each
1335 * clock domain is considered a distinct operation so if you modify the
1336 * gfxclk heuristics and then the memclk heuristics, the all of the
1337 * custom heuristics will be retained until you switch to another profile.
6b2576f5
AD
1338 *
1339 */
1340
37c5c4db
RZ
1341static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1342 struct device_attribute *attr,
1343 char *buf)
1344{
1345 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1346 struct amdgpu_device *adev = drm_to_adev(ddev);
b9a9294b
AD
1347 ssize_t size;
1348 int ret;
37c5c4db 1349
55aa33c3
LL
1350 ret = amdgpu_pm_get_access_if_active(adev);
1351 if (ret)
1352 return ret;
b9a9294b 1353
79c65f3f
EQ
1354 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1355 if (size <= 0)
09b6744c 1356 size = sysfs_emit(buf, "\n");
b9a9294b 1357
55aa33c3 1358 amdgpu_pm_put_access(adev);
37c5c4db 1359
b9a9294b 1360 return size;
37c5c4db
RZ
1361}
1362
1363
1364static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1365 struct device_attribute *attr,
1366 const char *buf,
1367 size_t count)
1368{
7c8e0835 1369 int ret;
37c5c4db 1370 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1371 struct amdgpu_device *adev = drm_to_adev(ddev);
37c5c4db
RZ
1372 uint32_t parameter_size = 0;
1373 long parameter[64];
1374 char *sub_str, buf_cpy[128];
1375 char *tmp_str;
1376 uint32_t i = 0;
1377 char tmp[2];
1378 long int profile_mode = 0;
1379 const char delimiter[3] = {' ', '\n', '\0'};
1380
1381 tmp[0] = *(buf);
1382 tmp[1] = '\0';
1383 ret = kstrtol(tmp, 0, &profile_mode);
1384 if (ret)
b9a9294b 1385 return -EINVAL;
37c5c4db
RZ
1386
1387 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1388 if (count < 2 || count > 127)
1389 return -EINVAL;
1390 while (isspace(*++buf))
1391 i++;
1392 memcpy(buf_cpy, buf, count-i);
1393 tmp_str = buf_cpy;
ce7c1d04 1394 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
c2efbc3f
EQ
1395 if (strlen(sub_str) == 0)
1396 continue;
37c5c4db 1397 ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
b9a9294b
AD
1398 if (ret)
1399 return -EINVAL;
37c5c4db
RZ
1400 parameter_size++;
1401 while (isspace(*tmp_str))
1402 tmp_str++;
1403 }
1404 }
1405 parameter[parameter_size] = profile_mode;
b9a9294b 1406
55aa33c3 1407 ret = amdgpu_pm_get_access(adev);
ded57e49 1408 if (ret < 0)
b9a9294b
AD
1409 return ret;
1410
79c65f3f 1411 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
b9a9294b 1412
55aa33c3 1413 amdgpu_pm_put_access(adev);
b9a9294b 1414
37c5c4db
RZ
1415 if (!ret)
1416 return count;
b9a9294b 1417
37c5c4db
RZ
1418 return -EINVAL;
1419}
1420
a5600853
AD
1421static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1422 enum amd_pp_sensors sensor,
1423 void *query)
d78c227f
ML
1424{
1425 int r, size = sizeof(uint32_t);
1426
55aa33c3
LL
1427 r = amdgpu_pm_get_access_if_active(adev);
1428 if (r)
1429 return r;
d78c227f
ML
1430
1431 /* get the sensor value */
1432 r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1433
55aa33c3 1434 amdgpu_pm_put_access(adev);
d78c227f
ML
1435
1436 return r;
1437}
1438
b374d82d 1439/**
f503fe69 1440 * DOC: gpu_busy_percent
b374d82d
TSD
1441 *
1442 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1443 * is as a percentage. The file gpu_busy_percent is used for this.
1444 * The SMU firmware computes a percentage of load based on the
1445 * aggregate activity level in the IP cores.
1446 */
4e01847c
KW
1447static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1448 struct device_attribute *attr,
1449 char *buf)
b374d82d
TSD
1450{
1451 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1452 struct amdgpu_device *adev = drm_to_adev(ddev);
d78c227f
ML
1453 unsigned int value;
1454 int r;
b9a9294b 1455
d78c227f 1456 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
b374d82d
TSD
1457 if (r)
1458 return r;
1459
a9ca9bb3 1460 return sysfs_emit(buf, "%d\n", value);
b374d82d
TSD
1461}
1462
f120386d
EQ
1463/**
1464 * DOC: mem_busy_percent
1465 *
1466 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1467 * is as a percentage. The file mem_busy_percent is used for this.
1468 * The SMU firmware computes a percentage of load based on the
1469 * aggregate activity level in the IP cores.
1470 */
4e01847c
KW
1471static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1472 struct device_attribute *attr,
1473 char *buf)
f120386d
EQ
1474{
1475 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1476 struct amdgpu_device *adev = drm_to_adev(ddev);
d78c227f
ML
1477 unsigned int value;
1478 int r;
b9a9294b 1479
d78c227f 1480 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
f120386d
EQ
1481 if (r)
1482 return r;
1483
a9ca9bb3 1484 return sysfs_emit(buf, "%d\n", value);
f120386d
EQ
1485}
1486
d1b2703c
XD
1487/**
1488 * DOC: vcn_busy_percent
1489 *
1490 * The amdgpu driver provides a sysfs API for reading how busy the VCN
1491 * is as a percentage. The file vcn_busy_percent is used for this.
1492 * The SMU firmware computes a percentage of load based on the
1493 * aggregate activity level in the IP cores.
1494 */
1495static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev,
1496 struct device_attribute *attr,
1497 char *buf)
1498{
1499 struct drm_device *ddev = dev_get_drvdata(dev);
1500 struct amdgpu_device *adev = drm_to_adev(ddev);
1501 unsigned int value;
1502 int r;
1503
1504 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value);
1505 if (r)
1506 return r;
1507
1508 return sysfs_emit(buf, "%d\n", value);
1509}
1510
b45e18ac
KR
1511/**
1512 * DOC: pcie_bw
1513 *
1514 * The amdgpu driver provides a sysfs API for estimating how much data
1515 * has been received and sent by the GPU in the last second through PCIe.
1516 * The file pcie_bw is used for this.
1517 * The Perf counters count the number of received and sent messages and return
1518 * those values, as well as the maximum payload size of a PCIe packet (mps).
1519 * Note that it is not possible to easily and quickly obtain the size of each
1520 * packet transmitted, so we output the max payload size (mps) to allow for
1521 * quick estimation of the PCIe bandwidth usage
1522 */
1523static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1524 struct device_attribute *attr,
1525 char *buf)
1526{
1527 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1528 struct amdgpu_device *adev = drm_to_adev(ddev);
d08d692e 1529 uint64_t count0 = 0, count1 = 0;
b9a9294b 1530 int ret;
b45e18ac 1531
d08d692e
AD
1532 if (adev->flags & AMD_IS_APU)
1533 return -ENODATA;
1534
1535 if (!adev->asic_funcs->get_pcie_usage)
1536 return -ENODATA;
1537
55aa33c3
LL
1538 ret = amdgpu_pm_get_access_if_active(adev);
1539 if (ret)
1540 return ret;
b9a9294b 1541
b45e18ac 1542 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
b9a9294b 1543
55aa33c3 1544 amdgpu_pm_put_access(adev);
b9a9294b 1545
a9ca9bb3
TT
1546 return sysfs_emit(buf, "%llu %llu %i\n",
1547 count0, count1, pcie_get_mps(adev->pdev));
b45e18ac
KR
1548}
1549
fb2dbfd2
KR
1550/**
1551 * DOC: unique_id
1552 *
1553 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1554 * The file unique_id is used for this.
1555 * This will provide a Unique ID that will persist from machine to machine
1556 *
1557 * NOTE: This will only work for GFX9 and newer. This file will be absent
1558 * on unsupported ASICs (GFX8 and older)
1559 */
1560static ssize_t amdgpu_get_unique_id(struct device *dev,
1561 struct device_attribute *attr,
1562 char *buf)
1563{
1564 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1565 struct amdgpu_device *adev = drm_to_adev(ddev);
fb2dbfd2
KR
1566
1567 if (adev->unique_id)
a9ca9bb3 1568 return sysfs_emit(buf, "%016llx\n", adev->unique_id);
fb2dbfd2
KR
1569
1570 return 0;
1571}
1572
b265bdbd
EQ
1573/**
1574 * DOC: thermal_throttling_logging
1575 *
1576 * Thermal throttling pulls down the clock frequency and thus the performance.
1577 * It's an useful mechanism to protect the chip from overheating. Since it
1578 * impacts performance, the user controls whether it is enabled and if so,
1579 * the log frequency.
1580 *
1581 * Reading back the file shows you the status(enabled or disabled) and
1582 * the interval(in seconds) between each thermal logging.
1583 *
1584 * Writing an integer to the file, sets a new logging interval, in seconds.
1585 * The value should be between 1 and 3600. If the value is less than 1,
1586 * thermal logging is disabled. Values greater than 3600 are ignored.
1587 */
1588static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1589 struct device_attribute *attr,
1590 char *buf)
1591{
1592 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1593 struct amdgpu_device *adev = drm_to_adev(ddev);
b265bdbd 1594
a9ca9bb3
TT
1595 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1596 adev_to_drm(adev)->unique,
1597 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1598 adev->throttling_logging_rs.interval / HZ + 1);
b265bdbd
EQ
1599}
1600
1601static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1602 struct device_attribute *attr,
1603 const char *buf,
1604 size_t count)
1605{
1606 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1607 struct amdgpu_device *adev = drm_to_adev(ddev);
b265bdbd 1608 long throttling_logging_interval;
b265bdbd
EQ
1609 int ret = 0;
1610
1611 ret = kstrtol(buf, 0, &throttling_logging_interval);
1612 if (ret)
1613 return ret;
1614
1615 if (throttling_logging_interval > 3600)
1616 return -EINVAL;
1617
1618 if (throttling_logging_interval > 0) {
b265bdbd
EQ
1619 /*
1620 * Reset the ratelimit timer internals.
1621 * This can effectively restart the timer.
1622 */
c6f7f1b2
PM
1623 ratelimit_state_reset_interval(&adev->throttling_logging_rs,
1624 (throttling_logging_interval - 1) * HZ);
b265bdbd
EQ
1625 atomic_set(&adev->throttling_logging_enabled, 1);
1626 } else {
1627 atomic_set(&adev->throttling_logging_enabled, 0);
1628 }
1629
1630 return count;
1631}
1632
c3ed0e72
KL
1633/**
1634 * DOC: apu_thermal_cap
1635 *
1636 * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1637 * limit temperature in millidegrees Celsius
1638 *
1639 * Reading back the file shows you core limit value
1640 *
1641 * Writing an integer to the file, sets a new thermal limit. The value
1642 * should be between 0 and 100. If the value is less than 0 or greater
1643 * than 100, then the write request will be ignored.
1644 */
1645static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1646 struct device_attribute *attr,
1647 char *buf)
1648{
1649 int ret, size;
1650 u32 limit;
1651 struct drm_device *ddev = dev_get_drvdata(dev);
1652 struct amdgpu_device *adev = drm_to_adev(ddev);
1653
55aa33c3
LL
1654 ret = amdgpu_pm_get_access_if_active(adev);
1655 if (ret)
1656 return ret;
c3ed0e72
KL
1657
1658 ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1659 if (!ret)
1660 size = sysfs_emit(buf, "%u\n", limit);
1661 else
1662 size = sysfs_emit(buf, "failed to get thermal limit\n");
1663
55aa33c3 1664 amdgpu_pm_put_access(adev);
c3ed0e72
KL
1665
1666 return size;
1667}
1668
1669static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1670 struct device_attribute *attr,
1671 const char *buf,
1672 size_t count)
1673{
1674 int ret;
1675 u32 value;
1676 struct drm_device *ddev = dev_get_drvdata(dev);
1677 struct amdgpu_device *adev = drm_to_adev(ddev);
1678
1679 ret = kstrtou32(buf, 10, &value);
1680 if (ret)
1681 return ret;
1682
4d2c09d6 1683 if (value > 100) {
c3ed0e72
KL
1684 dev_err(dev, "Invalid argument !\n");
1685 return -EINVAL;
1686 }
1687
55aa33c3 1688 ret = amdgpu_pm_get_access(adev);
ded57e49 1689 if (ret < 0)
c3ed0e72 1690 return ret;
c3ed0e72
KL
1691
1692 ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1693 if (ret) {
55aa33c3 1694 amdgpu_pm_put_access(adev);
c3ed0e72
KL
1695 dev_err(dev, "failed to update thermal limit\n");
1696 return ret;
1697 }
1698
55aa33c3 1699 amdgpu_pm_put_access(adev);
c3ed0e72
KL
1700
1701 return count;
1702}
1703
223aad1b
LL
1704static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev,
1705 struct amdgpu_device_attr *attr,
1706 uint32_t mask,
1707 enum amdgpu_device_attr_states *states)
1708{
1709 if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP)
1710 *states = ATTR_STATE_UNSUPPORTED;
1711
1712 return 0;
1713}
1714
1715static ssize_t amdgpu_get_pm_metrics(struct device *dev,
1716 struct device_attribute *attr, char *buf)
1717{
1718 struct drm_device *ddev = dev_get_drvdata(dev);
1719 struct amdgpu_device *adev = drm_to_adev(ddev);
1720 ssize_t size = 0;
1721 int ret;
1722
55aa33c3
LL
1723 ret = amdgpu_pm_get_access_if_active(adev);
1724 if (ret)
1725 return ret;
223aad1b
LL
1726
1727 size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE);
1728
55aa33c3 1729 amdgpu_pm_put_access(adev);
223aad1b
LL
1730
1731 return size;
1732}
1733
25c933b1
EQ
1734/**
1735 * DOC: gpu_metrics
1736 *
1737 * The amdgpu driver provides a sysfs API for retrieving current gpu
1738 * metrics data. The file gpu_metrics is used for this. Reading the
1739 * file will dump all the current gpu metrics data.
1740 *
1741 * These data include temperature, frequency, engines utilization,
1742 * power consume, throttler status, fan speed and cpu core statistics(
1743 * available for APU only). That's it will give a snapshot of all sensors
1744 * at the same time.
1745 */
1746static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1747 struct device_attribute *attr,
1748 char *buf)
1749{
1750 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 1751 struct amdgpu_device *adev = drm_to_adev(ddev);
25c933b1
EQ
1752 void *gpu_metrics;
1753 ssize_t size = 0;
1754 int ret;
1755
55aa33c3
LL
1756 ret = amdgpu_pm_get_access_if_active(adev);
1757 if (ret)
1758 return ret;
25c933b1 1759
79c65f3f 1760 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
25c933b1
EQ
1761 if (size <= 0)
1762 goto out;
1763
1764 if (size >= PAGE_SIZE)
1765 size = PAGE_SIZE - 1;
1766
1767 memcpy(buf, gpu_metrics, size);
1768
1769out:
55aa33c3 1770 amdgpu_pm_put_access(adev);
25c933b1
EQ
1771
1772 return size;
1773}
1774
494c1432 1775static int amdgpu_show_powershift_percent(struct device *dev,
d78c227f 1776 char *buf, enum amd_pp_sensors sensor)
a7673a1c
S
1777{
1778 struct drm_device *ddev = dev_get_drvdata(dev);
1779 struct amdgpu_device *adev = drm_to_adev(ddev);
494c1432
S
1780 uint32_t ss_power;
1781 int r = 0, i;
1782
d78c227f 1783 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
494c1432
S
1784 if (r == -EOPNOTSUPP) {
1785 /* sensor not available on dGPU, try to read from APU */
1786 adev = NULL;
1787 mutex_lock(&mgpu_info.mutex);
1788 for (i = 0; i < mgpu_info.num_gpu; i++) {
1789 if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1790 adev = mgpu_info.gpu_ins[i].adev;
1791 break;
1792 }
1793 }
1794 mutex_unlock(&mgpu_info.mutex);
1795 if (adev)
d78c227f 1796 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
a7673a1c
S
1797 }
1798
d78c227f
ML
1799 if (r)
1800 return r;
a7673a1c 1801
d78c227f 1802 return sysfs_emit(buf, "%u%%\n", ss_power);
494c1432 1803}
d78c227f 1804
494c1432
S
1805/**
1806 * DOC: smartshift_apu_power
1807 *
1808 * The amdgpu driver provides a sysfs API for reporting APU power
1809 * shift in percentage if platform supports smartshift. Value 0 means that
1810 * there is no powershift and values between [1-100] means that the power
1811 * is shifted to APU, the percentage of boost is with respect to APU power
1812 * limit on the platform.
1813 */
a7673a1c 1814
494c1432
S
1815static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1816 char *buf)
1817{
d78c227f 1818 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
494c1432 1819}
a7673a1c 1820
494c1432
S
1821/**
1822 * DOC: smartshift_dgpu_power
1823 *
1824 * The amdgpu driver provides a sysfs API for reporting dGPU power
1825 * shift in percentage if platform supports smartshift. Value 0 means that
1826 * there is no powershift and values between [1-100] means that the power is
1827 * shifted to dGPU, the percentage of boost is with respect to dGPU power
1828 * limit on the platform.
1829 */
1830
1831static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1832 char *buf)
1833{
d78c227f 1834 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
a7673a1c
S
1835}
1836
30d95a37
S
1837/**
1838 * DOC: smartshift_bias
1839 *
1840 * The amdgpu driver provides a sysfs API for reporting the
1841 * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1842 * and the default is 0. -100 sets maximum preference to APU
1843 * and 100 sets max perference to dGPU.
1844 */
1845
1846static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1847 struct device_attribute *attr,
1848 char *buf)
1849{
1850 int r = 0;
1851
1852 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1853
1854 return r;
1855}
1856
1857static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1858 struct device_attribute *attr,
1859 const char *buf, size_t count)
1860{
1861 struct drm_device *ddev = dev_get_drvdata(dev);
1862 struct amdgpu_device *adev = drm_to_adev(ddev);
1863 int r = 0;
1864 int bias = 0;
1865
30d95a37
S
1866 r = kstrtoint(buf, 10, &bias);
1867 if (r)
1868 goto out;
1869
55aa33c3
LL
1870 r = amdgpu_pm_get_access(adev);
1871 if (r < 0)
1872 return r;
1873
30d95a37
S
1874 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
1875 bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
1876 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
1877 bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
1878
1879 amdgpu_smartshift_bias = bias;
1880 r = count;
1881
bd4b9bb7 1882 /* TODO: update bias level with SMU message */
30d95a37
S
1883
1884out:
55aa33c3
LL
1885 amdgpu_pm_put_access(adev);
1886
30d95a37
S
1887 return r;
1888}
1889
a7673a1c
S
1890static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1891 uint32_t mask, enum amdgpu_device_attr_states *states)
1892{
494c1432 1893 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
a7673a1c
S
1894 *states = ATTR_STATE_UNSUPPORTED;
1895
1896 return 0;
1897}
1898
30d95a37
S
1899static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1900 uint32_t mask, enum amdgpu_device_attr_states *states)
1901{
d78c227f 1902 uint32_t ss_power;
30d95a37
S
1903
1904 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1905 *states = ATTR_STATE_UNSUPPORTED;
d78c227f
ML
1906 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1907 (void *)&ss_power))
30d95a37 1908 *states = ATTR_STATE_UNSUPPORTED;
d78c227f
ML
1909 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1910 (void *)&ss_power))
30d95a37
S
1911 *states = ATTR_STATE_UNSUPPORTED;
1912
1913 return 0;
1914}
1915
98a936c3
YW
1916static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1917 uint32_t mask, enum amdgpu_device_attr_states *states)
1918{
1919 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
1920
1921 *states = ATTR_STATE_SUPPORTED;
1922
1923 if (!amdgpu_dpm_is_overdrive_supported(adev)) {
1924 *states = ATTR_STATE_UNSUPPORTED;
1925 return 0;
1926 }
1927
c003b5cc 1928 /* Enable pp_od_clk_voltage node for gc 9.4.3, 9.4.4, 9.5.0 SRIOV/BM support */
5f571c61 1929 if (gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
1930 gc_ver == IP_VERSION(9, 4, 4) ||
1931 gc_ver == IP_VERSION(9, 5, 0)) {
8d5e70ba 1932 if (amdgpu_sriov_multi_vf_mode(adev))
98a936c3
YW
1933 *states = ATTR_STATE_UNSUPPORTED;
1934 return 0;
1935 }
1936
1937 if (!(attr->flags & mask))
1938 *states = ATTR_STATE_UNSUPPORTED;
1939
1940 return 0;
1941}
1942
190145f6
YW
1943static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1944 uint32_t mask, enum amdgpu_device_attr_states *states)
1945{
1946 struct device_attribute *dev_attr = &attr->dev_attr;
1947 uint32_t gc_ver;
1948
1949 *states = ATTR_STATE_SUPPORTED;
1950
1951 if (!(attr->flags & mask)) {
1952 *states = ATTR_STATE_UNSUPPORTED;
1953 return 0;
1954 }
1955
1956 gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
1957 /* dcefclk node is not available on gfx 11.0.3 sriov */
1958 if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
1959 gc_ver < IP_VERSION(9, 0, 0) ||
1960 !amdgpu_device_has_display_hardware(adev))
1961 *states = ATTR_STATE_UNSUPPORTED;
1962
1963 /* SMU MP1 does not support dcefclk level setting,
1964 * setting should not be allowed from VF if not in one VF mode.
1965 */
1966 if (gc_ver >= IP_VERSION(10, 0, 0) ||
8d5e70ba 1967 (amdgpu_sriov_multi_vf_mode(adev))) {
190145f6
YW
1968 dev_attr->attr.mode &= ~S_IWUGO;
1969 dev_attr->store = NULL;
1970 }
1971
1972 return 0;
1973}
1974
166a3c73
YW
1975static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1976 uint32_t mask, enum amdgpu_device_attr_states *states)
1977{
1978 struct device_attribute *dev_attr = &attr->dev_attr;
1979 enum amdgpu_device_attr_id attr_id = attr->attr_id;
1980 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
1981 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
1982
1983 *states = ATTR_STATE_SUPPORTED;
1984
1985 if (!(attr->flags & mask)) {
1986 *states = ATTR_STATE_UNSUPPORTED;
1987 return 0;
1988 }
1989
1990 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
1991 if (gc_ver < IP_VERSION(9, 0, 0))
1992 *states = ATTR_STATE_UNSUPPORTED;
1993 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
1994 if (mp1_ver < IP_VERSION(10, 0, 0))
1995 *states = ATTR_STATE_UNSUPPORTED;
1996 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
1997 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
1998 gc_ver == IP_VERSION(10, 3, 3) ||
1999 gc_ver == IP_VERSION(10, 3, 6) ||
2000 gc_ver == IP_VERSION(10, 3, 7) ||
2001 gc_ver == IP_VERSION(10, 3, 0) ||
2002 gc_ver == IP_VERSION(10, 1, 2) ||
2003 gc_ver == IP_VERSION(11, 0, 0) ||
2004 gc_ver == IP_VERSION(11, 0, 1) ||
2005 gc_ver == IP_VERSION(11, 0, 4) ||
2006 gc_ver == IP_VERSION(11, 5, 0) ||
2007 gc_ver == IP_VERSION(11, 0, 2) ||
2008 gc_ver == IP_VERSION(11, 0, 3) ||
5f571c61 2009 gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
2010 gc_ver == IP_VERSION(9, 4, 4) ||
2011 gc_ver == IP_VERSION(9, 5, 0)))
166a3c73
YW
2012 *states = ATTR_STATE_UNSUPPORTED;
2013 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2014 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2015 gc_ver == IP_VERSION(10, 3, 0) ||
2016 gc_ver == IP_VERSION(11, 0, 2) ||
2017 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2018 *states = ATTR_STATE_UNSUPPORTED;
2019 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2020 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2021 gc_ver == IP_VERSION(10, 3, 3) ||
2022 gc_ver == IP_VERSION(10, 3, 6) ||
2023 gc_ver == IP_VERSION(10, 3, 7) ||
2024 gc_ver == IP_VERSION(10, 3, 0) ||
2025 gc_ver == IP_VERSION(10, 1, 2) ||
2026 gc_ver == IP_VERSION(11, 0, 0) ||
2027 gc_ver == IP_VERSION(11, 0, 1) ||
2028 gc_ver == IP_VERSION(11, 0, 4) ||
2029 gc_ver == IP_VERSION(11, 5, 0) ||
2030 gc_ver == IP_VERSION(11, 0, 2) ||
2031 gc_ver == IP_VERSION(11, 0, 3) ||
5f571c61 2032 gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
2033 gc_ver == IP_VERSION(9, 4, 4) ||
2034 gc_ver == IP_VERSION(9, 5, 0)))
166a3c73
YW
2035 *states = ATTR_STATE_UNSUPPORTED;
2036 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2037 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2038 gc_ver == IP_VERSION(10, 3, 0) ||
2039 gc_ver == IP_VERSION(11, 0, 2) ||
2040 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2041 *states = ATTR_STATE_UNSUPPORTED;
2042 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2043 if (gc_ver == IP_VERSION(9, 4, 2) ||
5f571c61 2044 gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
2045 gc_ver == IP_VERSION(9, 4, 4) ||
2046 gc_ver == IP_VERSION(9, 5, 0))
166a3c73
YW
2047 *states = ATTR_STATE_UNSUPPORTED;
2048 }
2049
2050 switch (gc_ver) {
2051 case IP_VERSION(9, 4, 1):
2052 case IP_VERSION(9, 4, 2):
2053 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2054 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2055 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2056 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2057 dev_attr->attr.mode &= ~S_IWUGO;
2058 dev_attr->store = NULL;
2059 }
2060 break;
2061 default:
2062 break;
2063 }
2064
2065 /* setting should not be allowed from VF if not in one VF mode */
2066 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
2067 dev_attr->attr.mode &= ~S_IWUGO;
2068 dev_attr->store = NULL;
2069 }
2070
2071 return 0;
2072}
2073
4d154b1c
LL
2074/* pm policy attributes */
2075struct amdgpu_pm_policy_attr {
2076 struct device_attribute dev_attr;
2077 enum pp_pm_policy id;
2078};
2079
f88e570d
LL
2080/**
2081 * DOC: pm_policy
2082 *
2083 * Certain SOCs can support different power policies to optimize application
2084 * performance. However, this policy is provided only at SOC level and not at a
2085 * per-process level. This is useful especially when entire SOC is utilized for
2086 * dedicated workload.
2087 *
2088 * The amdgpu driver provides a sysfs API for selecting the policy. Presently,
2089 * only two types of policies are supported through this interface.
2090 *
2091 * Pstate Policy Selection - This is to select different Pstate profiles which
2092 * decides clock/throttling preferences.
2093 *
2094 * XGMI PLPD Policy Selection - When multiple devices are connected over XGMI,
2095 * this helps to select policy to be applied for per link power down.
2096 *
2097 * The list of available policies and policy levels vary between SOCs. They can
2098 * be viewed under pm_policy node directory. If SOC doesn't support any policy,
2099 * this node won't be available. The different policies supported will be
2100 * available as separate nodes under pm_policy.
2101 *
2102 * cat /sys/bus/pci/devices/.../pm_policy/<policy_type>
2103 *
2104 * Reading the policy file shows the different levels supported. The level which
2105 * is applied presently is denoted by * (asterisk). E.g.,
2106 *
2107 * .. code-block:: console
2108 *
2109 * cat /sys/bus/pci/devices/.../pm_policy/soc_pstate
2110 * 0 : soc_pstate_default
2111 * 1 : soc_pstate_0
2112 * 2 : soc_pstate_1*
2113 * 3 : soc_pstate_2
2114 *
2115 * cat /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2116 * 0 : plpd_disallow
2117 * 1 : plpd_default
2118 * 2 : plpd_optimized*
2119 *
2120 * To apply a specific policy
2121 *
2122 * "echo <level> > /sys/bus/pci/devices/.../pm_policy/<policy_type>"
2123 *
2124 * For the levels listed in the example above, to select "plpd_optimized" for
2125 * XGMI and "soc_pstate_2" for soc pstate policy -
2126 *
2127 * .. code-block:: console
2128 *
2129 * echo "2" > /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2130 * echo "3" > /sys/bus/pci/devices/.../pm_policy/soc_pstate
2131 *
2132 */
4d154b1c
LL
2133static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
2134 struct device_attribute *attr,
2135 char *buf)
2136{
2137 struct drm_device *ddev = dev_get_drvdata(dev);
2138 struct amdgpu_device *adev = drm_to_adev(ddev);
2139 struct amdgpu_pm_policy_attr *policy_attr;
2140
2141 policy_attr =
2142 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2143
4d154b1c
LL
2144 return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
2145}
2146
2147static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
2148 struct device_attribute *attr,
2149 const char *buf, size_t count)
2150{
2151 struct drm_device *ddev = dev_get_drvdata(dev);
2152 struct amdgpu_device *adev = drm_to_adev(ddev);
2153 struct amdgpu_pm_policy_attr *policy_attr;
2154 int ret, num_params = 0;
2155 char delimiter[] = " \n\t";
2156 char tmp_buf[128];
2157 char *tmp, *param;
2158 long val;
2159
4d154b1c
LL
2160 count = min(count, sizeof(tmp_buf));
2161 memcpy(tmp_buf, buf, count);
2162 tmp_buf[count - 1] = '\0';
2163 tmp = tmp_buf;
2164
2165 tmp = skip_spaces(tmp);
2166 while ((param = strsep(&tmp, delimiter))) {
2167 if (!strlen(param)) {
2168 tmp = skip_spaces(tmp);
2169 continue;
2170 }
2171 ret = kstrtol(param, 0, &val);
2172 if (ret)
2173 return -EINVAL;
2174 num_params++;
2175 if (num_params > 1)
2176 return -EINVAL;
2177 }
2178
2179 if (num_params != 1)
2180 return -EINVAL;
2181
2182 policy_attr =
2183 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2184
55aa33c3 2185 ret = amdgpu_pm_get_access(adev);
ded57e49 2186 if (ret < 0)
4d154b1c 2187 return ret;
4d154b1c
LL
2188
2189 ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val);
2190
55aa33c3 2191 amdgpu_pm_put_access(adev);
4d154b1c
LL
2192
2193 if (ret)
2194 return ret;
2195
2196 return count;
2197}
2198
2199#define AMDGPU_PM_POLICY_ATTR(_name, _id) \
2200 static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = { \
2201 .dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \
2202 amdgpu_set_pm_policy_attr), \
2203 .id = PP_PM_POLICY_##_id, \
2204 };
2205
2206#define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr
2207
2208AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE)
83b90b13 2209AMDGPU_PM_POLICY_ATTR(xgmi_plpd, XGMI_PLPD)
4d154b1c
LL
2210
2211static struct attribute *pm_policy_attrs[] = {
2212 &AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate),
83b90b13 2213 &AMDGPU_PM_POLICY_ATTR_VAR(xgmi_plpd),
4d154b1c
LL
2214 NULL
2215};
2216
2217static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj,
2218 struct attribute *attr, int n)
2219{
2220 struct device *dev = kobj_to_dev(kobj);
2221 struct drm_device *ddev = dev_get_drvdata(dev);
2222 struct amdgpu_device *adev = drm_to_adev(ddev);
2223 struct amdgpu_pm_policy_attr *policy_attr;
2224
2225 policy_attr =
2226 container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr);
2227
2228 if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) ==
2229 -ENOENT)
2230 return 0;
2231
2232 return attr->mode;
2233}
2234
2235const struct attribute_group amdgpu_pm_policy_attr_group = {
2236 .name = "pm_policy",
2237 .attrs = pm_policy_attrs,
2238 .is_visible = amdgpu_pm_policy_attr_visible,
2239};
2240
4e01847c
KW
2241static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2242 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
4215a119 2243 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
7884d0e9
JG
2244 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2245 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2246 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2247 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
166a3c73
YW
2248 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2249 .attr_update = pp_dpm_clk_default_attr_update),
2250 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2251 .attr_update = pp_dpm_clk_default_attr_update),
2252 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2253 .attr_update = pp_dpm_clk_default_attr_update),
2254 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2255 .attr_update = pp_dpm_clk_default_attr_update),
2256 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2257 .attr_update = pp_dpm_clk_default_attr_update),
2258 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2259 .attr_update = pp_dpm_clk_default_attr_update),
2260 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2261 .attr_update = pp_dpm_clk_default_attr_update),
2262 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2263 .attr_update = pp_dpm_clk_default_attr_update),
190145f6
YW
2264 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2265 .attr_update = pp_dpm_dcefclk_attr_update),
166a3c73
YW
2266 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2267 .attr_update = pp_dpm_clk_default_attr_update),
4e01847c
KW
2268 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
2269 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
ac82902d 2270 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
98a936c3
YW
2271 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC,
2272 .attr_update = pp_od_clk_voltage_attr_update),
ac82902d
VC
2273 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2274 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
166a3c73 2275 AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
4e01847c 2276 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
ac82902d
VC
2277 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2278 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2279 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
c3ed0e72 2280 AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
ac82902d 2281 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
a7673a1c
S
2282 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
2283 .attr_update = ss_power_attr_update),
2284 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
2285 .attr_update = ss_power_attr_update),
30d95a37
S
2286 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
2287 .attr_update = ss_bias_attr_update),
223aad1b
LL
2288 AMDGPU_DEVICE_ATTR_RO(pm_metrics, ATTR_FLAG_BASIC,
2289 .attr_update = amdgpu_pm_metrics_attr_update),
4e01847c
KW
2290};
2291
2292static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
ba02fd6b 2293 uint32_t mask, enum amdgpu_device_attr_states *states)
4e01847c
KW
2294{
2295 struct device_attribute *dev_attr = &attr->dev_attr;
2ea6f4d9 2296 enum amdgpu_device_attr_id attr_id = attr->attr_id;
4e8303cf 2297 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4e01847c
KW
2298
2299 if (!(attr->flags & mask)) {
ba02fd6b 2300 *states = ATTR_STATE_UNSUPPORTED;
4e01847c
KW
2301 return 0;
2302 }
2303
166a3c73 2304 if (DEVICE_ATTR_IS(mem_busy_percent)) {
5df0f0b3
AK
2305 if ((adev->flags & AMD_IS_APU &&
2306 gc_ver != IP_VERSION(9, 4, 3)) ||
2307 gc_ver == IP_VERSION(9, 0, 1))
ba02fd6b 2308 *states = ATTR_STATE_UNSUPPORTED;
d1b2703c 2309 } else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
15030aee
AD
2310 if (!(gc_ver == IP_VERSION(9, 3, 0) ||
2311 gc_ver == IP_VERSION(10, 3, 1) ||
18537feb
AD
2312 gc_ver == IP_VERSION(10, 3, 3) ||
2313 gc_ver == IP_VERSION(10, 3, 6) ||
2314 gc_ver == IP_VERSION(10, 3, 7) ||
2315 gc_ver == IP_VERSION(11, 0, 0) ||
2316 gc_ver == IP_VERSION(11, 0, 1) ||
2317 gc_ver == IP_VERSION(11, 0, 2) ||
2318 gc_ver == IP_VERSION(11, 0, 3) ||
2319 gc_ver == IP_VERSION(11, 0, 4) ||
2320 gc_ver == IP_VERSION(11, 5, 0) ||
2321 gc_ver == IP_VERSION(11, 5, 1) ||
2322 gc_ver == IP_VERSION(11, 5, 2) ||
5b392222
AD
2323 gc_ver == IP_VERSION(11, 5, 3) ||
2324 gc_ver == IP_VERSION(12, 0, 0) ||
2325 gc_ver == IP_VERSION(12, 0, 1)))
d1b2703c 2326 *states = ATTR_STATE_UNSUPPORTED;
4e01847c
KW
2327 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2328 /* PCIe Perf counters won't work on APU nodes */
5fe4a8d3
AK
2329 if (adev->flags & AMD_IS_APU ||
2330 !adev->asic_funcs->get_pcie_usage)
ba02fd6b 2331 *states = ATTR_STATE_UNSUPPORTED;
4e01847c 2332 } else if (DEVICE_ATTR_IS(unique_id)) {
60044748
KR
2333 switch (gc_ver) {
2334 case IP_VERSION(9, 0, 1):
2335 case IP_VERSION(9, 4, 0):
2336 case IP_VERSION(9, 4, 1):
2337 case IP_VERSION(9, 4, 2):
baf65745 2338 case IP_VERSION(9, 4, 3):
5f571c61 2339 case IP_VERSION(9, 4, 4):
c003b5cc 2340 case IP_VERSION(9, 5, 0):
ebd9c071 2341 case IP_VERSION(10, 3, 0):
276c03a0 2342 case IP_VERSION(11, 0, 0):
35e67ca6
KR
2343 case IP_VERSION(11, 0, 1):
2344 case IP_VERSION(11, 0, 2):
d82758ad 2345 case IP_VERSION(11, 0, 3):
16fbc18c
HK
2346 case IP_VERSION(12, 0, 0):
2347 case IP_VERSION(12, 0, 1):
60044748
KR
2348 *states = ATTR_STATE_SUPPORTED;
2349 break;
2350 default:
ba02fd6b 2351 *states = ATTR_STATE_UNSUPPORTED;
60044748 2352 }
4e01847c 2353 } else if (DEVICE_ATTR_IS(pp_features)) {
fc8e84a2
LL
2354 if ((adev->flags & AMD_IS_APU &&
2355 gc_ver != IP_VERSION(9, 4, 3)) ||
2356 gc_ver < IP_VERSION(9, 0, 0))
ba02fd6b 2357 *states = ATTR_STATE_UNSUPPORTED;
25c933b1 2358 } else if (DEVICE_ATTR_IS(gpu_metrics)) {
8ecad8d6 2359 if (gc_ver < IP_VERSION(9, 1, 0))
25c933b1 2360 *states = ATTR_STATE_UNSUPPORTED;
a7505591 2361 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
79c65f3f 2362 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
a7505591 2363 *states = ATTR_STATE_UNSUPPORTED;
b57c4f1c
VZ
2364 else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2365 gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
1b852572 2366 *states = ATTR_STATE_UNSUPPORTED;
df2a5f74 2367 } else if (DEVICE_ATTR_IS(pp_mclk_od)) {
8cfd6a05
LL
2368 if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
2369 *states = ATTR_STATE_UNSUPPORTED;
df2a5f74 2370 } else if (DEVICE_ATTR_IS(pp_sclk_od)) {
8cfd6a05
LL
2371 if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
2372 *states = ATTR_STATE_UNSUPPORTED;
2373 } else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
2374 u32 limit;
2375
2376 if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
2377 -EOPNOTSUPP)
2378 *states = ATTR_STATE_UNSUPPORTED;
4e01847c
KW
2379 }
2380
8ecad8d6 2381 switch (gc_ver) {
1b852572
DS
2382 case IP_VERSION(10, 3, 0):
2383 if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
2384 amdgpu_sriov_vf(adev)) {
2385 dev_attr->attr.mode &= ~0222;
2386 dev_attr->store = NULL;
2387 }
2388 break;
1d0e622f
KW
2389 default:
2390 break;
4e01847c
KW
2391 }
2392
4e01847c
KW
2393 return 0;
2394}
2395
2396
2397static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2398 struct amdgpu_device_attr *attr,
ba02fd6b 2399 uint32_t mask, struct list_head *attr_list)
4e01847c
KW
2400{
2401 int ret = 0;
ba02fd6b
KW
2402 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2403 struct amdgpu_device_attr_entry *attr_entry;
25e6373a
YW
2404 struct device_attribute *dev_attr;
2405 const char *name;
ba02fd6b 2406
4e01847c 2407 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
ba02fd6b 2408 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
4e01847c 2409
25e6373a
YW
2410 if (!attr)
2411 return -EINVAL;
2412
2413 dev_attr = &attr->dev_attr;
2414 name = dev_attr->attr.name;
4e01847c 2415
8a81028b 2416 attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
4e01847c 2417
ba02fd6b 2418 ret = attr_update(adev, attr, mask, &attr_states);
4e01847c
KW
2419 if (ret) {
2420 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2421 name, ret);
2422 return ret;
2423 }
2424
ba02fd6b 2425 if (attr_states == ATTR_STATE_UNSUPPORTED)
4e01847c
KW
2426 return 0;
2427
2428 ret = device_create_file(adev->dev, dev_attr);
2429 if (ret) {
2430 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2431 name, ret);
2432 }
2433
ba02fd6b
KW
2434 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2435 if (!attr_entry)
2436 return -ENOMEM;
2437
2438 attr_entry->attr = attr;
2439 INIT_LIST_HEAD(&attr_entry->entry);
2440
2441 list_add_tail(&attr_entry->entry, attr_list);
4e01847c
KW
2442
2443 return ret;
2444}
2445
2446static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2447{
2448 struct device_attribute *dev_attr = &attr->dev_attr;
2449
4e01847c 2450 device_remove_file(adev->dev, dev_attr);
4e01847c
KW
2451}
2452
ba02fd6b
KW
2453static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2454 struct list_head *attr_list);
2455
4e01847c
KW
2456static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2457 struct amdgpu_device_attr *attrs,
2458 uint32_t counts,
ba02fd6b
KW
2459 uint32_t mask,
2460 struct list_head *attr_list)
4e01847c
KW
2461{
2462 int ret = 0;
2463 uint32_t i = 0;
2464
2465 for (i = 0; i < counts; i++) {
ba02fd6b 2466 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
4e01847c
KW
2467 if (ret)
2468 goto failed;
2469 }
2470
2471 return 0;
2472
2473failed:
ba02fd6b 2474 amdgpu_device_attr_remove_groups(adev, attr_list);
4e01847c
KW
2475
2476 return ret;
2477}
2478
2479static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
ba02fd6b 2480 struct list_head *attr_list)
4e01847c 2481{
ba02fd6b 2482 struct amdgpu_device_attr_entry *entry, *entry_tmp;
4e01847c 2483
ba02fd6b
KW
2484 if (list_empty(attr_list))
2485 return ;
2486
2487 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2488 amdgpu_device_attr_remove(adev, entry->attr);
2489 list_del(&entry->entry);
2490 kfree(entry);
2491 }
4e01847c 2492}
e3933f26 2493
d38ceaf9
AD
2494static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2495 struct device_attribute *attr,
2496 char *buf)
2497{
2498 struct amdgpu_device *adev = dev_get_drvdata(dev);
a34d1166 2499 int channel = to_sensor_dev_attr(attr)->index;
d78c227f 2500 int r, temp = 0;
48b270bb 2501
a34d1166
EQ
2502 if (channel >= PP_TEMP_MAX)
2503 return -EINVAL;
2504
2505 switch (channel) {
2506 case PP_TEMP_JUNCTION:
2507 /* get current junction temperature */
d78c227f
ML
2508 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2509 (void *)&temp);
a34d1166
EQ
2510 break;
2511 case PP_TEMP_EDGE:
2512 /* get current edge temperature */
d78c227f
ML
2513 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2514 (void *)&temp);
a34d1166
EQ
2515 break;
2516 case PP_TEMP_MEM:
2517 /* get current memory temperature */
d78c227f
ML
2518 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2519 (void *)&temp);
b9a9294b
AD
2520 break;
2521 default:
2522 r = -EINVAL;
a34d1166
EQ
2523 break;
2524 }
d38ceaf9 2525
b9a9294b
AD
2526 if (r)
2527 return r;
2528
a9ca9bb3 2529 return sysfs_emit(buf, "%d\n", temp);
d38ceaf9
AD
2530}
2531
2532static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2533 struct device_attribute *attr,
2534 char *buf)
2535{
2536 struct amdgpu_device *adev = dev_get_drvdata(dev);
2537 int hyst = to_sensor_dev_attr(attr)->index;
2538 int temp;
2539
2540 if (hyst)
2541 temp = adev->pm.dpm.thermal.min_temp;
2542 else
2543 temp = adev->pm.dpm.thermal.max_temp;
2544
a9ca9bb3 2545 return sysfs_emit(buf, "%d\n", temp);
d38ceaf9
AD
2546}
2547
437ccd17
EQ
2548static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2549 struct device_attribute *attr,
2550 char *buf)
2551{
2552 struct amdgpu_device *adev = dev_get_drvdata(dev);
2553 int hyst = to_sensor_dev_attr(attr)->index;
2554 int temp;
2555
2556 if (hyst)
2557 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2558 else
2559 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2560
a9ca9bb3 2561 return sysfs_emit(buf, "%d\n", temp);
437ccd17
EQ
2562}
2563
2564static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2565 struct device_attribute *attr,
2566 char *buf)
2567{
2568 struct amdgpu_device *adev = dev_get_drvdata(dev);
2569 int hyst = to_sensor_dev_attr(attr)->index;
2570 int temp;
2571
2572 if (hyst)
2573 temp = adev->pm.dpm.thermal.min_mem_temp;
2574 else
2575 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2576
a9ca9bb3 2577 return sysfs_emit(buf, "%d\n", temp);
437ccd17
EQ
2578}
2579
2adc1156
EQ
2580static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2581 struct device_attribute *attr,
2582 char *buf)
2583{
2584 int channel = to_sensor_dev_attr(attr)->index;
2585
2586 if (channel >= PP_TEMP_MAX)
2587 return -EINVAL;
2588
a9ca9bb3 2589 return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2adc1156
EQ
2590}
2591
901cb599
EQ
2592static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2593 struct device_attribute *attr,
2594 char *buf)
2595{
2596 struct amdgpu_device *adev = dev_get_drvdata(dev);
2597 int channel = to_sensor_dev_attr(attr)->index;
2598 int temp = 0;
2599
2600 if (channel >= PP_TEMP_MAX)
2601 return -EINVAL;
2602
2603 switch (channel) {
2604 case PP_TEMP_JUNCTION:
2605 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2606 break;
2607 case PP_TEMP_EDGE:
2608 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2609 break;
2610 case PP_TEMP_MEM:
2611 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2612 break;
2613 }
2614
a9ca9bb3 2615 return sysfs_emit(buf, "%d\n", temp);
901cb599
EQ
2616}
2617
d38ceaf9
AD
2618static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2619 struct device_attribute *attr,
2620 char *buf)
2621{
2622 struct amdgpu_device *adev = dev_get_drvdata(dev);
2623 u32 pwm_mode = 0;
b9a9294b
AD
2624 int ret;
2625
55aa33c3
LL
2626 ret = amdgpu_pm_get_access_if_active(adev);
2627 if (ret)
2628 return ret;
c9ffa427 2629
79c65f3f 2630 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
f46587bc 2631
55aa33c3 2632 amdgpu_pm_put_access(adev);
b9a9294b 2633
79c65f3f
EQ
2634 if (ret)
2635 return -EINVAL;
2636
fdf8eea5 2637 return sysfs_emit(buf, "%u\n", pwm_mode);
d38ceaf9
AD
2638}
2639
2640static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2641 struct device_attribute *attr,
2642 const char *buf,
2643 size_t count)
2644{
2645 struct amdgpu_device *adev = dev_get_drvdata(dev);
b9a9294b 2646 int err, ret;
f317c5e5 2647 u32 pwm_mode;
d38ceaf9
AD
2648 int value;
2649
fcd90fee
EQ
2650 err = kstrtoint(buf, 10, &value);
2651 if (err)
2652 return err;
a76ff5af 2653
f317c5e5
MJ
2654 if (value == 0)
2655 pwm_mode = AMD_FAN_CTRL_NONE;
2656 else if (value == 1)
2657 pwm_mode = AMD_FAN_CTRL_MANUAL;
2658 else if (value == 2)
2659 pwm_mode = AMD_FAN_CTRL_AUTO;
2660 else
2661 return -EINVAL;
2662
55aa33c3 2663 ret = amdgpu_pm_get_access(adev);
ded57e49 2664 if (ret < 0)
b9a9294b
AD
2665 return ret;
2666
f317c5e5 2667 ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
f46587bc 2668
55aa33c3 2669 amdgpu_pm_put_access(adev);
b9a9294b 2670
79c65f3f
EQ
2671 if (ret)
2672 return -EINVAL;
2673
d38ceaf9
AD
2674 return count;
2675}
2676
2677static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2680{
fdf8eea5 2681 return sysfs_emit(buf, "%i\n", 0);
d38ceaf9
AD
2682}
2683
2684static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2685 struct device_attribute *attr,
2686 char *buf)
2687{
fdf8eea5 2688 return sysfs_emit(buf, "%i\n", 255);
d38ceaf9
AD
2689}
2690
2691static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2692 struct device_attribute *attr,
2693 const char *buf, size_t count)
2694{
2695 struct amdgpu_device *adev = dev_get_drvdata(dev);
2696 int err;
2697 u32 value;
b8a9c003 2698 u32 pwm_mode;
d38ceaf9 2699
79c65f3f
EQ
2700 err = kstrtou32(buf, 10, &value);
2701 if (err)
2702 return err;
2703
55aa33c3 2704 err = amdgpu_pm_get_access(adev);
ded57e49 2705 if (err < 0)
b9a9294b
AD
2706 return err;
2707
79c65f3f
EQ
2708 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2709 if (err)
2710 goto out;
2711
b8a9c003
RZ
2712 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2713 pr_info("manual fan speed control should be enabled first\n");
79c65f3f
EQ
2714 err = -EINVAL;
2715 goto out;
b8a9c003
RZ
2716 }
2717
79c65f3f 2718 err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
b9a9294b 2719
79c65f3f 2720out:
55aa33c3 2721 amdgpu_pm_put_access(adev);
b9a9294b
AD
2722
2723 if (err)
2724 return err;
d38ceaf9
AD
2725
2726 return count;
2727}
2728
2729static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2730 struct device_attribute *attr,
2731 char *buf)
2732{
2733 struct amdgpu_device *adev = dev_get_drvdata(dev);
2734 int err;
cd4d7464 2735 u32 speed = 0;
d38ceaf9 2736
55aa33c3
LL
2737 err = amdgpu_pm_get_access_if_active(adev);
2738 if (err)
2739 return err;
5ec36e2d 2740
79c65f3f 2741 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
b9a9294b 2742
55aa33c3 2743 amdgpu_pm_put_access(adev);
b9a9294b
AD
2744
2745 if (err)
2746 return err;
d38ceaf9 2747
fdf8eea5 2748 return sysfs_emit(buf, "%i\n", speed);
d38ceaf9
AD
2749}
2750
81c1514b
GI
2751static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2754{
2755 struct amdgpu_device *adev = dev_get_drvdata(dev);
2756 int err;
cd4d7464 2757 u32 speed = 0;
81c1514b 2758
55aa33c3
LL
2759 err = amdgpu_pm_get_access_if_active(adev);
2760 if (err)
2761 return err;
5ec36e2d 2762
79c65f3f 2763 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
b9a9294b 2764
55aa33c3 2765 amdgpu_pm_put_access(adev);
b9a9294b
AD
2766
2767 if (err)
2768 return err;
81c1514b 2769
fdf8eea5 2770 return sysfs_emit(buf, "%i\n", speed);
81c1514b
GI
2771}
2772
c2870527
RZ
2773static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2774 struct device_attribute *attr,
2775 char *buf)
2776{
2777 struct amdgpu_device *adev = dev_get_drvdata(dev);
2778 u32 min_rpm = 0;
c2870527
RZ
2779 int r;
2780
d78c227f
ML
2781 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2782 (void *)&min_rpm);
b9a9294b 2783
c2870527
RZ
2784 if (r)
2785 return r;
2786
a9ca9bb3 2787 return sysfs_emit(buf, "%d\n", min_rpm);
c2870527
RZ
2788}
2789
2790static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2791 struct device_attribute *attr,
2792 char *buf)
2793{
2794 struct amdgpu_device *adev = dev_get_drvdata(dev);
2795 u32 max_rpm = 0;
c2870527
RZ
2796 int r;
2797
d78c227f
ML
2798 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2799 (void *)&max_rpm);
b9a9294b 2800
c2870527
RZ
2801 if (r)
2802 return r;
2803
a9ca9bb3 2804 return sysfs_emit(buf, "%d\n", max_rpm);
c2870527
RZ
2805}
2806
2807static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2808 struct device_attribute *attr,
2809 char *buf)
2810{
2811 struct amdgpu_device *adev = dev_get_drvdata(dev);
2812 int err;
2813 u32 rpm = 0;
c2870527 2814
55aa33c3
LL
2815 err = amdgpu_pm_get_access_if_active(adev);
2816 if (err)
2817 return err;
c2870527 2818
79c65f3f 2819 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
b9a9294b 2820
55aa33c3 2821 amdgpu_pm_put_access(adev);
b9a9294b
AD
2822
2823 if (err)
2824 return err;
c2870527 2825
fdf8eea5 2826 return sysfs_emit(buf, "%i\n", rpm);
c2870527
RZ
2827}
2828
2829static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2832{
2833 struct amdgpu_device *adev = dev_get_drvdata(dev);
2834 int err;
2835 u32 value;
2836 u32 pwm_mode;
2837
79c65f3f
EQ
2838 err = kstrtou32(buf, 10, &value);
2839 if (err)
2840 return err;
2841
55aa33c3 2842 err = amdgpu_pm_get_access(adev);
ded57e49 2843 if (err < 0)
b9a9294b
AD
2844 return err;
2845
79c65f3f
EQ
2846 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2847 if (err)
2848 goto out;
96026ce0 2849
b9a9294b 2850 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
79c65f3f
EQ
2851 err = -ENODATA;
2852 goto out;
b9a9294b 2853 }
c2870527 2854
79c65f3f 2855 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
b9a9294b 2856
79c65f3f 2857out:
55aa33c3 2858 amdgpu_pm_put_access(adev);
b9a9294b
AD
2859
2860 if (err)
2861 return err;
c2870527
RZ
2862
2863 return count;
2864}
2865
2866static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2867 struct device_attribute *attr,
2868 char *buf)
2869{
2870 struct amdgpu_device *adev = dev_get_drvdata(dev);
2871 u32 pwm_mode = 0;
b9a9294b
AD
2872 int ret;
2873
55aa33c3
LL
2874 ret = amdgpu_pm_get_access_if_active(adev);
2875 if (ret)
2876 return ret;
c2870527 2877
79c65f3f 2878 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
f46587bc 2879
55aa33c3 2880 amdgpu_pm_put_access(adev);
b9a9294b 2881
79c65f3f
EQ
2882 if (ret)
2883 return -EINVAL;
2884
fdf8eea5 2885 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
c2870527
RZ
2886}
2887
2888static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2889 struct device_attribute *attr,
2890 const char *buf,
2891 size_t count)
2892{
2893 struct amdgpu_device *adev = dev_get_drvdata(dev);
2894 int err;
2895 int value;
2896 u32 pwm_mode;
2897
c2870527
RZ
2898 err = kstrtoint(buf, 10, &value);
2899 if (err)
2900 return err;
2901
2902 if (value == 0)
2903 pwm_mode = AMD_FAN_CTRL_AUTO;
2904 else if (value == 1)
2905 pwm_mode = AMD_FAN_CTRL_MANUAL;
2906 else
2907 return -EINVAL;
2908
55aa33c3 2909 err = amdgpu_pm_get_access(adev);
ded57e49 2910 if (err < 0)
b9a9294b
AD
2911 return err;
2912
79c65f3f 2913 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
c2870527 2914
55aa33c3 2915 amdgpu_pm_put_access(adev);
b9a9294b 2916
79c65f3f
EQ
2917 if (err)
2918 return -EINVAL;
2919
c2870527
RZ
2920 return count;
2921}
2922
2bd376bf
AD
2923static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2924 struct device_attribute *attr,
2925 char *buf)
2926{
2927 struct amdgpu_device *adev = dev_get_drvdata(dev);
2bd376bf 2928 u32 vddgfx;
d78c227f 2929 int r;
2bd376bf 2930
2bd376bf 2931 /* get the voltage */
d78c227f
ML
2932 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
2933 (void *)&vddgfx);
2bd376bf
AD
2934 if (r)
2935 return r;
2936
a9ca9bb3 2937 return sysfs_emit(buf, "%d\n", vddgfx);
2bd376bf
AD
2938}
2939
96ac487c
AK
2940static ssize_t amdgpu_hwmon_show_vddboard(struct device *dev,
2941 struct device_attribute *attr,
2942 char *buf)
2943{
2944 struct amdgpu_device *adev = dev_get_drvdata(dev);
2945 u32 vddboard;
2946 int r;
2947
2948 /* get the voltage */
2949 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
2950 (void *)&vddboard);
2951 if (r)
2952 return r;
2953
2954 return sysfs_emit(buf, "%d\n", vddboard);
2955}
2956
2bd376bf
AD
2957static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2958 struct device_attribute *attr,
2959 char *buf)
2960{
a9ca9bb3 2961 return sysfs_emit(buf, "vddgfx\n");
2bd376bf
AD
2962}
2963
96ac487c
AK
2964static ssize_t amdgpu_hwmon_show_vddboard_label(struct device *dev,
2965 struct device_attribute *attr,
2966 char *buf)
2967{
2968 return sysfs_emit(buf, "vddboard\n");
2969}
2bd376bf
AD
2970static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2971 struct device_attribute *attr,
2972 char *buf)
2973{
2974 struct amdgpu_device *adev = dev_get_drvdata(dev);
2bd376bf 2975 u32 vddnb;
d78c227f 2976 int r;
48b270bb 2977
2bd376bf 2978 /* only APUs have vddnb */
ccf9ef0b 2979 if (!(adev->flags & AMD_IS_APU))
2bd376bf
AD
2980 return -EINVAL;
2981
2bd376bf 2982 /* get the voltage */
d78c227f
ML
2983 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
2984 (void *)&vddnb);
2bd376bf
AD
2985 if (r)
2986 return r;
2987
a9ca9bb3 2988 return sysfs_emit(buf, "%d\n", vddnb);
2bd376bf
AD
2989}
2990
2991static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2992 struct device_attribute *attr,
2993 char *buf)
2994{
a9ca9bb3 2995 return sysfs_emit(buf, "vddnb\n");
2bd376bf
AD
2996}
2997
a5600853
AD
2998static int amdgpu_hwmon_get_power(struct device *dev,
2999 enum amd_pp_sensors sensor)
2976fc26
AD
3000{
3001 struct amdgpu_device *adev = dev_get_drvdata(dev);
d78c227f 3002 unsigned int uw;
5b79d048 3003 u32 query = 0;
d78c227f 3004 int r;
b9a9294b 3005
d78c227f 3006 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
2976fc26
AD
3007 if (r)
3008 return r;
3009
3010 /* convert to microwatts */
5b79d048 3011 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2976fc26 3012
d78c227f
ML
3013 return uw;
3014}
3015
3016static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3017 struct device_attribute *attr,
3018 char *buf)
3019{
d1090194 3020 ssize_t val;
d78c227f 3021
9366c2e8 3022 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
d78c227f
ML
3023 if (val < 0)
3024 return val;
3025
d1090194 3026 return sysfs_emit(buf, "%zd\n", val);
2976fc26
AD
3027}
3028
bb9f7b68
ML
3029static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3030 struct device_attribute *attr,
3031 char *buf)
3032{
d1090194 3033 ssize_t val;
bb9f7b68 3034
47f1724d 3035 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
bb9f7b68
ML
3036 if (val < 0)
3037 return val;
3038
d1090194 3039 return sysfs_emit(buf, "%zd\n", val);
bb9f7b68
ML
3040}
3041
91161b06
DP
3042static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3043 struct device_attribute *attr,
3044 char *buf,
3045 enum pp_power_limit_level pp_limit_level)
8d81bce7
RZ
3046{
3047 struct amdgpu_device *adev = dev_get_drvdata(dev);
a40a020d
DP
3048 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3049 uint32_t limit;
b9a9294b
AD
3050 ssize_t size;
3051 int r;
3052
55aa33c3
LL
3053 r = amdgpu_pm_get_access_if_active(adev);
3054 if (r)
3055 return r;
8d81bce7 3056
79c65f3f 3057 r = amdgpu_dpm_get_power_limit(adev, &limit,
91161b06 3058 pp_limit_level, power_type);
dc2a8240
DP
3059
3060 if (!r)
09b6744c 3061 size = sysfs_emit(buf, "%u\n", limit * 1000000);
dc2a8240 3062 else
09b6744c 3063 size = sysfs_emit(buf, "\n");
b9a9294b 3064
55aa33c3 3065 amdgpu_pm_put_access(adev);
b9a9294b
AD
3066
3067 return size;
8d81bce7
RZ
3068}
3069
19589468
MJ
3070static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
3071 struct device_attribute *attr,
3072 char *buf)
3073{
3074 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
3075}
91161b06
DP
3076
3077static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
8d81bce7
RZ
3078 struct device_attribute *attr,
3079 char *buf)
3080{
91161b06 3081 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
dc2a8240 3082
91161b06 3083}
b9a9294b 3084
91161b06
DP
3085static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3086 struct device_attribute *attr,
3087 char *buf)
3088{
3089 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
b9a9294b 3090
8d81bce7
RZ
3091}
3092
6e58941c
EH
3093static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
3094 struct device_attribute *attr,
3095 char *buf)
3096{
91161b06 3097 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
6e58941c 3098
6e58941c 3099}
91161b06 3100
ae07970a
XH
3101static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3102 struct device_attribute *attr,
3103 char *buf)
3104{
3b99e8e3 3105 struct amdgpu_device *adev = dev_get_drvdata(dev);
4e8303cf 3106 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
ae07970a 3107
8ecad8d6 3108 if (gc_ver == IP_VERSION(10, 3, 1))
3b99e8e3
YW
3109 return sysfs_emit(buf, "%s\n",
3110 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
3111 "fastPPT" : "slowPPT");
3112 else
3113 return sysfs_emit(buf, "PPT\n");
ae07970a 3114}
8d81bce7
RZ
3115
3116static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3117 struct device_attribute *attr,
3118 const char *buf,
3119 size_t count)
3120{
3121 struct amdgpu_device *adev = dev_get_drvdata(dev);
ae07970a 3122 int limit_type = to_sensor_dev_attr(attr)->index;
8d81bce7
RZ
3123 int err;
3124 u32 value;
3125
c9ffa427
YT
3126 if (amdgpu_sriov_vf(adev))
3127 return -EINVAL;
3128
8d81bce7
RZ
3129 err = kstrtou32(buf, 10, &value);
3130 if (err)
3131 return err;
3132
3133 value = value / 1000000; /* convert to Watt */
ae07970a 3134 value |= limit_type << 24;
b9a9294b 3135
55aa33c3 3136 err = amdgpu_pm_get_access(adev);
ded57e49 3137 if (err < 0)
b9a9294b
AD
3138 return err;
3139
79c65f3f 3140 err = amdgpu_dpm_set_power_limit(adev, value);
b9a9294b 3141
55aa33c3 3142 amdgpu_pm_put_access(adev);
8d81bce7 3143
fcd90fee
EQ
3144 if (err)
3145 return err;
3146
8d81bce7
RZ
3147 return count;
3148}
3149
d0948af7
AD
3150static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3151 struct device_attribute *attr,
3152 char *buf)
3153{
3154 struct amdgpu_device *adev = dev_get_drvdata(dev);
d0948af7 3155 uint32_t sclk;
d78c227f 3156 int r;
d0948af7 3157
d0948af7 3158 /* get the sclk */
d78c227f
ML
3159 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3160 (void *)&sclk);
d0948af7
AD
3161 if (r)
3162 return r;
3163
a9ca9bb3 3164 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
d0948af7
AD
3165}
3166
3167static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3168 struct device_attribute *attr,
3169 char *buf)
3170{
a9ca9bb3 3171 return sysfs_emit(buf, "sclk\n");
d0948af7
AD
3172}
3173
3174static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3175 struct device_attribute *attr,
3176 char *buf)
3177{
3178 struct amdgpu_device *adev = dev_get_drvdata(dev);
d0948af7 3179 uint32_t mclk;
d78c227f 3180 int r;
d0948af7 3181
d0948af7 3182 /* get the sclk */
d78c227f
ML
3183 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3184 (void *)&mclk);
d0948af7
AD
3185 if (r)
3186 return r;
3187
a9ca9bb3 3188 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
d0948af7
AD
3189}
3190
3191static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3192 struct device_attribute *attr,
3193 char *buf)
3194{
a9ca9bb3 3195 return sysfs_emit(buf, "mclk\n");
d0948af7 3196}
844c5419
AD
3197
3198/**
3199 * DOC: hwmon
3200 *
3201 * The amdgpu driver exposes the following sensor interfaces:
dc85db25 3202 *
844c5419 3203 * - GPU temperature (via the on-die sensor)
dc85db25 3204 *
844c5419 3205 * - GPU voltage
dc85db25 3206 *
844c5419 3207 * - Northbridge voltage (APUs only)
dc85db25 3208 *
844c5419 3209 * - GPU power
dc85db25 3210 *
844c5419
AD
3211 * - GPU fan
3212 *
d0948af7
AD
3213 * - GPU gfx/compute engine clock
3214 *
3215 * - GPU memory clock (dGPU only)
3216 *
844c5419 3217 * hwmon interfaces for GPU temperature:
dc85db25 3218 *
a34d1166
EQ
3219 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3220 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
dc85db25 3221 *
2adc1156
EQ
3222 * - temp[1-3]_label: temperature channel label
3223 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3224 *
437ccd17
EQ
3225 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3226 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
dc85db25 3227 *
437ccd17
EQ
3228 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3229 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
844c5419 3230 *
901cb599
EQ
3231 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3232 * - these are supported on SOC15 dGPUs only
3233 *
844c5419 3234 * hwmon interfaces for GPU voltage:
dc85db25 3235 *
844c5419 3236 * - in0_input: the voltage on the GPU in millivolts
dc85db25 3237 *
844c5419
AD
3238 * - in1_input: the voltage on the Northbridge in millivolts
3239 *
3240 * hwmon interfaces for GPU power:
dc85db25 3241 *
29f5be8d 3242 * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU.
dc85db25 3243 *
bb9f7b68
ML
3244 * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU.
3245 *
844c5419 3246 * - power1_cap_min: minimum cap supported in microWatts
dc85db25 3247 *
844c5419 3248 * - power1_cap_max: maximum cap supported in microWatts
dc85db25 3249 *
844c5419
AD
3250 * - power1_cap: selected power cap in microWatts
3251 *
3252 * hwmon interfaces for GPU fan:
dc85db25 3253 *
844c5419 3254 * - pwm1: pulse width modulation fan level (0-255)
dc85db25
AD
3255 *
3256 * - 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)
3257 *
844c5419 3258 * - pwm1_min: pulse width modulation fan control minimum level (0)
dc85db25 3259 *
844c5419 3260 * - pwm1_max: pulse width modulation fan control maximum level (255)
dc85db25 3261 *
e5527d8c 3262 * - fan1_min: a minimum value Unit: revolution/min (RPM)
c2870527 3263 *
e5527d8c 3264 * - fan1_max: a maximum value Unit: revolution/max (RPM)
c2870527 3265 *
844c5419
AD
3266 * - fan1_input: fan speed in RPM
3267 *
879e723d 3268 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
c2870527 3269 *
879e723d 3270 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
c2870527 3271 *
96401f7c
EQ
3272 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3273 * That will get the former one overridden.
3274 *
d0948af7
AD
3275 * hwmon interfaces for GPU clocks:
3276 *
3277 * - freq1_input: the gfx/compute clock in hertz
3278 *
3279 * - freq2_input: the memory clock in hertz
3280 *
844c5419
AD
3281 * You can use hwmon tools like sensors to view this information on your system.
3282 *
3283 */
3284
a34d1166 3285static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
d38ceaf9
AD
3286static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3287static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
901cb599 3288static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
a34d1166 3289static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
437ccd17
EQ
3290static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3291static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
901cb599 3292static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
a34d1166 3293static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
437ccd17
EQ
3294static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3295static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
901cb599 3296static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2adc1156
EQ
3297static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3298static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3299static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
d38ceaf9
AD
3300static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3301static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3302static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3303static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
81c1514b 3304static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
c2870527
RZ
3305static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3306static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3307static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3308static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2bd376bf
AD
3309static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3310static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3311static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3312static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
96ac487c
AK
3313static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, amdgpu_hwmon_show_vddboard, NULL, 0);
3314static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, amdgpu_hwmon_show_vddboard_label, NULL, 0);
2976fc26 3315static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
bb9f7b68 3316static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
8d81bce7
RZ
3317static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3318static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3319static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
6e58941c 3320static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
ae07970a
XH
3321static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3322static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3323static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3324static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3325static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
6e58941c 3326static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
ae07970a 3327static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
d0948af7
AD
3328static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3329static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3330static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3331static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
d38ceaf9
AD
3332
3333static struct attribute *hwmon_attributes[] = {
3334 &sensor_dev_attr_temp1_input.dev_attr.attr,
3335 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3336 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
a34d1166 3337 &sensor_dev_attr_temp2_input.dev_attr.attr,
437ccd17
EQ
3338 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3339 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
a34d1166 3340 &sensor_dev_attr_temp3_input.dev_attr.attr,
437ccd17
EQ
3341 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3342 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
901cb599
EQ
3343 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3344 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3345 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2adc1156
EQ
3346 &sensor_dev_attr_temp1_label.dev_attr.attr,
3347 &sensor_dev_attr_temp2_label.dev_attr.attr,
3348 &sensor_dev_attr_temp3_label.dev_attr.attr,
d38ceaf9
AD
3349 &sensor_dev_attr_pwm1.dev_attr.attr,
3350 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3351 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3352 &sensor_dev_attr_pwm1_max.dev_attr.attr,
81c1514b 3353 &sensor_dev_attr_fan1_input.dev_attr.attr,
c2870527
RZ
3354 &sensor_dev_attr_fan1_min.dev_attr.attr,
3355 &sensor_dev_attr_fan1_max.dev_attr.attr,
3356 &sensor_dev_attr_fan1_target.dev_attr.attr,
3357 &sensor_dev_attr_fan1_enable.dev_attr.attr,
2bd376bf
AD
3358 &sensor_dev_attr_in0_input.dev_attr.attr,
3359 &sensor_dev_attr_in0_label.dev_attr.attr,
3360 &sensor_dev_attr_in1_input.dev_attr.attr,
3361 &sensor_dev_attr_in1_label.dev_attr.attr,
96ac487c
AK
3362 &sensor_dev_attr_in2_input.dev_attr.attr,
3363 &sensor_dev_attr_in2_label.dev_attr.attr,
2976fc26 3364 &sensor_dev_attr_power1_average.dev_attr.attr,
bb9f7b68 3365 &sensor_dev_attr_power1_input.dev_attr.attr,
8d81bce7
RZ
3366 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3367 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3368 &sensor_dev_attr_power1_cap.dev_attr.attr,
6e58941c 3369 &sensor_dev_attr_power1_cap_default.dev_attr.attr,
ae07970a
XH
3370 &sensor_dev_attr_power1_label.dev_attr.attr,
3371 &sensor_dev_attr_power2_average.dev_attr.attr,
3372 &sensor_dev_attr_power2_cap_max.dev_attr.attr,
3373 &sensor_dev_attr_power2_cap_min.dev_attr.attr,
3374 &sensor_dev_attr_power2_cap.dev_attr.attr,
6e58941c 3375 &sensor_dev_attr_power2_cap_default.dev_attr.attr,
ae07970a 3376 &sensor_dev_attr_power2_label.dev_attr.attr,
d0948af7
AD
3377 &sensor_dev_attr_freq1_input.dev_attr.attr,
3378 &sensor_dev_attr_freq1_label.dev_attr.attr,
3379 &sensor_dev_attr_freq2_input.dev_attr.attr,
3380 &sensor_dev_attr_freq2_label.dev_attr.attr,
d38ceaf9
AD
3381 NULL
3382};
3383
3384static umode_t hwmon_attributes_visible(struct kobject *kobj,
3385 struct attribute *attr, int index)
3386{
cc29ec87 3387 struct device *dev = kobj_to_dev(kobj);
d38ceaf9
AD
3388 struct amdgpu_device *adev = dev_get_drvdata(dev);
3389 umode_t effective_mode = attr->mode;
4e8303cf 3390 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
15419813 3391 uint32_t tmp;
d38ceaf9 3392
4f0f1b58
DS
3393 /* under pp one vf mode manage of hwmon attributes is not supported */
3394 if (amdgpu_sriov_is_pp_one_vf(adev))
3395 effective_mode &= ~S_IWUSR;
3396
fc5a136d
RZ
3397 /* Skip fan attributes if fan is not present */
3398 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3399 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3400 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3401 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
c2870527
RZ
3402 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3403 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3404 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3405 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3406 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
fc5a136d 3407 return 0;
135f9711 3408
20a96cd3
AD
3409 /* Skip fan attributes on APU */
3410 if ((adev->flags & AMD_IS_APU) &&
3411 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3412 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3413 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3414 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3415 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3416 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3417 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3418 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3419 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3420 return 0;
3421
35dab589 3422 /* Skip crit temp on APU */
8572fa2a 3423 if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
c003b5cc
AK
3424 (gc_ver == IP_VERSION(9, 4, 3) || gc_ver == IP_VERSION(9, 4, 4) ||
3425 gc_ver == IP_VERSION(9, 5, 0))) &&
35dab589
HR
3426 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3427 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3428 return 0;
3429
1b5708ff 3430 /* Skip limit attributes if DPM is not enabled */
d38ceaf9
AD
3431 if (!adev->pm.dpm_enabled &&
3432 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
27100735
AD
3433 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3434 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3435 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3436 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
c2870527
RZ
3437 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3438 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3439 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3440 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3441 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3442 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
d38ceaf9
AD
3443 return 0;
3444
79c65f3f 3445 /* mask fan attributes if we have no bindings for this asic to expose */
685fae24 3446 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
79c65f3f 3447 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
685fae24 3448 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
79c65f3f
EQ
3449 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3450 effective_mode &= ~S_IRUGO;
239873fc 3451
685fae24 3452 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
79c65f3f 3453 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
685fae24 3454 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
79c65f3f
EQ
3455 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3456 effective_mode &= ~S_IWUSR;
d38ceaf9 3457
8572fa2a 3458 /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
ae07970a 3459 if (((adev->family == AMDGPU_FAMILY_SI) ||
8572fa2a 3460 ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
5f571c61 3461 (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)))) &&
367deb67 3462 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
8ecad8d6 3463 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
6e58941c
EH
3464 attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3465 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
8d81bce7
RZ
3466 return 0;
3467
89317d42 3468 /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
367deb67 3469 if (((adev->family == AMDGPU_FAMILY_SI) ||
8ecad8d6 3470 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
367deb67
AD
3471 (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3472 return 0;
3473
15419813
ML
3474 /* not all products support both average and instantaneous */
3475 if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
3476 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
3477 return 0;
3478 if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
3479 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
3480 return 0;
3481
79c65f3f 3482 /* hide max/min values if we can't both query and manage the fan */
685fae24
EQ
3483 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3484 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3485 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3486 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
79c65f3f
EQ
3487 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3488 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3489 return 0;
239873fc 3490
685fae24
EQ
3491 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3492 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
79c65f3f
EQ
3493 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3494 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3495 return 0;
c2870527 3496
1cdd229b 3497 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
8572fa2a 3498 adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */
5f571c61 3499 (gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
3500 gc_ver == IP_VERSION(9, 4, 4) ||
3501 gc_ver == IP_VERSION(9, 5, 0))) &&
1cdd229b
JD
3502 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3503 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3504 return 0;
3505
8572fa2a 3506 /* only APUs other than gc 9,4,3 have vddnb */
5f571c61
HZ
3507 if ((!(adev->flags & AMD_IS_APU) ||
3508 (gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
3509 gc_ver == IP_VERSION(9, 4, 4) ||
3510 gc_ver == IP_VERSION(9, 5, 0))) &&
0d35bc78
AD
3511 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3512 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
81c1514b
GI
3513 return 0;
3514
96ac487c
AK
3515 /* only few boards support vddboard */
3516 if ((attr == &sensor_dev_attr_in2_input.dev_attr.attr ||
3517 attr == &sensor_dev_attr_in2_label.dev_attr.attr) &&
3518 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
3519 (void *)&tmp) == -EOPNOTSUPP)
3520 return 0;
3521
8572fa2a
AK
3522 /* no mclk on APUs other than gc 9,4,3*/
3523 if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
d0948af7
AD
3524 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3525 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3526 return 0;
3527
8ecad8d6 3528 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
5f571c61 3529 (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) &&
8572fa2a 3530 (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
bfb4fd20 3531 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
07864911 3532 attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
bfb4fd20 3533 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
07864911
AK
3534 attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
3535 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
8572fa2a
AK
3536 return 0;
3537
bfb4fd20 3538 /* hotspot temperature for gc 9,4,3*/
5f571c61 3539 if (gc_ver == IP_VERSION(9, 4, 3) ||
c003b5cc
AK
3540 gc_ver == IP_VERSION(9, 4, 4) ||
3541 gc_ver == IP_VERSION(9, 5, 0)) {
9cff0879
LL
3542 if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
3543 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3544 attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
3545 return 0;
3546
3547 if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3548 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
3549 return attr->mode;
3550 }
8572fa2a
AK
3551
3552 /* only SOC15 dGPUs support hotspot and mem temperatures */
9cff0879
LL
3553 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3554 (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
901cb599
EQ
3555 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3556 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3557 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
bfb4fd20 3558 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
437ccd17
EQ
3559 return 0;
3560
ae07970a 3561 /* only Vangogh has fast PPT limit and power labels */
8ecad8d6 3562 if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
ae07970a 3563 (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
8ecad8d6 3564 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
ae07970a 3565 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
8ecad8d6
LL
3566 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3567 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3568 attr == &sensor_dev_attr_power2_label.dev_attr.attr))
ae07970a
XH
3569 return 0;
3570
d38ceaf9
AD
3571 return effective_mode;
3572}
3573
3574static const struct attribute_group hwmon_attrgroup = {
3575 .attrs = hwmon_attributes,
3576 .is_visible = hwmon_attributes_visible,
3577};
3578
3579static const struct attribute_group *hwmon_groups[] = {
3580 &hwmon_attrgroup,
3581 NULL
3582};
3583
d7bf1b55
EQ
3584static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3585 enum pp_clock_type od_type,
3586 char *buf)
3587{
3588 int size = 0;
3589 int ret;
3590
55aa33c3
LL
3591 ret = amdgpu_pm_get_access_if_active(adev);
3592 if (ret)
3593 return ret;
d7bf1b55
EQ
3594
3595 size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3596 if (size == 0)
3597 size = sysfs_emit(buf, "\n");
3598
55aa33c3 3599 amdgpu_pm_put_access(adev);
d7bf1b55
EQ
3600
3601 return size;
3602}
3603
3604static int parse_input_od_command_lines(const char *buf,
3605 size_t count,
3606 u32 *type,
3607 long *params,
3608 uint32_t *num_of_params)
3609{
3610 const char delimiter[3] = {' ', '\n', '\0'};
3611 uint32_t parameter_size = 0;
3612 char buf_cpy[128] = {0};
3613 char *tmp_str, *sub_str;
3614 int ret;
3615
3616 if (count > sizeof(buf_cpy) - 1)
3617 return -EINVAL;
3618
3619 memcpy(buf_cpy, buf, count);
3620 tmp_str = buf_cpy;
3621
3622 /* skip heading spaces */
3623 while (isspace(*tmp_str))
3624 tmp_str++;
3625
3626 switch (*tmp_str) {
3627 case 'c':
3628 *type = PP_OD_COMMIT_DPM_TABLE;
3629 return 0;
f7f9e48f
MJ
3630 case 'r':
3631 params[parameter_size] = *type;
3632 *num_of_params = 1;
3633 *type = PP_OD_RESTORE_DEFAULT_TABLE;
3634 return 0;
d7bf1b55
EQ
3635 default:
3636 break;
3637 }
3638
3639 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3640 if (strlen(sub_str) == 0)
3641 continue;
3642
3643 ret = kstrtol(sub_str, 0, &params[parameter_size]);
3644 if (ret)
3645 return -EINVAL;
3646 parameter_size++;
3647
3648 while (isspace(*tmp_str))
3649 tmp_str++;
3650 }
3651
3652 *num_of_params = parameter_size;
3653
3654 return 0;
3655}
3656
3657static int
3658amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3659 enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3660 const char *in_buf,
3661 size_t count)
3662{
3663 uint32_t parameter_size = 0;
3664 long parameter[64];
3665 int ret;
3666
d7bf1b55
EQ
3667 ret = parse_input_od_command_lines(in_buf,
3668 count,
3669 &cmd_type,
3670 parameter,
3671 &parameter_size);
3672 if (ret)
3673 return ret;
3674
55aa33c3 3675 ret = amdgpu_pm_get_access(adev);
d7bf1b55 3676 if (ret < 0)
ded57e49 3677 return ret;
d7bf1b55
EQ
3678
3679 ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3680 cmd_type,
3681 parameter,
3682 parameter_size);
3683 if (ret)
ded57e49 3684 goto err_out;
d7bf1b55
EQ
3685
3686 if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3687 ret = amdgpu_dpm_dispatch_task(adev,
3688 AMD_PP_TASK_READJUST_POWER_STATE,
3689 NULL);
3690 if (ret)
ded57e49 3691 goto err_out;
d7bf1b55
EQ
3692 }
3693
55aa33c3 3694 amdgpu_pm_put_access(adev);
d7bf1b55
EQ
3695
3696 return count;
3697
ded57e49 3698err_out:
55aa33c3 3699 amdgpu_pm_put_access(adev);
d7bf1b55
EQ
3700
3701 return ret;
3702}
3703
3704/**
3705 * DOC: fan_curve
3706 *
3707 * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3708 * control curve line.
3709 *
3710 * Reading back the file shows you the current settings(temperature in Celsius
3711 * degree and fan speed in pwm) applied to every anchor point of the curve line
3712 * and their permitted ranges if changable.
3713 *
3714 * Writing a desired string(with the format like "anchor_point_index temperature
3715 * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3716 * point accordingly.
3717 *
3718 * When you have finished the editing, write "c" (commit) to the file to commit
3719 * your changes.
3720 *
f7f9e48f
MJ
3721 * If you want to reset to the default value, write "r" (reset) to the file to
3722 * reset them
3723 *
d7bf1b55
EQ
3724 * There are two fan control modes supported: auto and manual. With auto mode,
3725 * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3726 * While with manual mode, users can set their own fan curve line as what
3727 * described here. Normally the ASIC is booted up with auto mode. Any
3728 * settings via this interface will switch the fan control to manual mode
3729 * implicitly.
3730 */
3731static ssize_t fan_curve_show(struct kobject *kobj,
3732 struct kobj_attribute *attr,
3733 char *buf)
3734{
3735 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3736 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3737
3738 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3739}
3740
3741static ssize_t fan_curve_store(struct kobject *kobj,
3742 struct kobj_attribute *attr,
3743 const char *buf,
3744 size_t count)
3745{
3746 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3747 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3748
3749 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3750 PP_OD_EDIT_FAN_CURVE,
3751 buf,
3752 count);
3753}
3754
3755static umode_t fan_curve_visible(struct amdgpu_device *adev)
3756{
3757 umode_t umode = 0000;
3758
3759 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3760 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3761
3762 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3763 umode |= S_IWUSR;
3764
3765 return umode;
3766}
3767
548009ad
EQ
3768/**
3769 * DOC: acoustic_limit_rpm_threshold
3770 *
3771 * The amdgpu driver provides a sysfs API for checking and adjusting the
3772 * acoustic limit in RPM for fan control.
3773 *
3774 * Reading back the file shows you the current setting and the permitted
3775 * ranges if changable.
3776 *
3777 * Writing an integer to the file, change the setting accordingly.
3778 *
3779 * When you have finished the editing, write "c" (commit) to the file to commit
3780 * your changes.
3781 *
1007bc36
MJ
3782 * If you want to reset to the default value, write "r" (reset) to the file to
3783 * reset them
3784 *
548009ad
EQ
3785 * This setting works under auto fan control mode only. It adjusts the PMFW's
3786 * behavior about the maximum speed in RPM the fan can spin. Setting via this
3787 * interface will switch the fan control to auto mode implicitly.
3788 */
3789static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3790 struct kobj_attribute *attr,
3791 char *buf)
3792{
3793 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3794 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3795
3796 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3797}
3798
3799static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3800 struct kobj_attribute *attr,
3801 const char *buf,
3802 size_t count)
3803{
3804 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3805 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3806
3807 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3808 PP_OD_EDIT_ACOUSTIC_LIMIT,
3809 buf,
3810 count);
3811}
3812
3813static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3814{
3815 umode_t umode = 0000;
3816
3817 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3818 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3819
3820 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3821 umode |= S_IWUSR;
3822
3823 return umode;
3824}
3825
47cf6fcb
EQ
3826/**
3827 * DOC: acoustic_target_rpm_threshold
3828 *
3829 * The amdgpu driver provides a sysfs API for checking and adjusting the
3830 * acoustic target in RPM for fan control.
3831 *
3832 * Reading back the file shows you the current setting and the permitted
3833 * ranges if changable.
3834 *
3835 * Writing an integer to the file, change the setting accordingly.
3836 *
3837 * When you have finished the editing, write "c" (commit) to the file to commit
3838 * your changes.
3839 *
1007bc36
MJ
3840 * If you want to reset to the default value, write "r" (reset) to the file to
3841 * reset them
3842 *
47cf6fcb
EQ
3843 * This setting works under auto fan control mode only. It can co-exist with
3844 * other settings which can work also under auto mode. It adjusts the PMFW's
3845 * behavior about the maximum speed in RPM the fan can spin when ASIC
3846 * temperature is not greater than target temperature. Setting via this
3847 * interface will switch the fan control to auto mode implicitly.
3848 */
3849static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
3850 struct kobj_attribute *attr,
3851 char *buf)
3852{
3853 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3854 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3855
3856 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
3857}
3858
3859static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
3860 struct kobj_attribute *attr,
3861 const char *buf,
3862 size_t count)
3863{
3864 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3865 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3866
3867 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3868 PP_OD_EDIT_ACOUSTIC_TARGET,
3869 buf,
3870 count);
3871}
3872
3873static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
3874{
3875 umode_t umode = 0000;
3876
3877 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
3878 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3879
3880 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
3881 umode |= S_IWUSR;
3882
3883 return umode;
3884}
3885
eedd5a34
EQ
3886/**
3887 * DOC: fan_target_temperature
3888 *
3889 * The amdgpu driver provides a sysfs API for checking and adjusting the
3890 * target tempeature in Celsius degree for fan control.
3891 *
3892 * Reading back the file shows you the current setting and the permitted
3893 * ranges if changable.
3894 *
3895 * Writing an integer to the file, change the setting accordingly.
3896 *
3897 * When you have finished the editing, write "c" (commit) to the file to commit
3898 * your changes.
3899 *
1007bc36
MJ
3900 * If you want to reset to the default value, write "r" (reset) to the file to
3901 * reset them
3902 *
eedd5a34
EQ
3903 * This setting works under auto fan control mode only. It can co-exist with
3904 * other settings which can work also under auto mode. Paring with the
3905 * acoustic_target_rpm_threshold setting, they define the maximum speed in
3906 * RPM the fan can spin when ASIC temperature is not greater than target
3907 * temperature. Setting via this interface will switch the fan control to
3908 * auto mode implicitly.
3909 */
3910static ssize_t fan_target_temperature_show(struct kobject *kobj,
3911 struct kobj_attribute *attr,
3912 char *buf)
3913{
3914 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3915 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3916
3917 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
3918}
3919
3920static ssize_t fan_target_temperature_store(struct kobject *kobj,
3921 struct kobj_attribute *attr,
3922 const char *buf,
3923 size_t count)
3924{
3925 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3926 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3927
3928 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3929 PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
3930 buf,
3931 count);
3932}
3933
3934static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
3935{
3936 umode_t umode = 0000;
3937
3938 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
3939 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3940
3941 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
3942 umode |= S_IWUSR;
3943
3944 return umode;
3945}
3946
9df5d008
EQ
3947/**
3948 * DOC: fan_minimum_pwm
3949 *
3950 * The amdgpu driver provides a sysfs API for checking and adjusting the
3951 * minimum fan speed in PWM.
3952 *
3953 * Reading back the file shows you the current setting and the permitted
3954 * ranges if changable.
3955 *
3956 * Writing an integer to the file, change the setting accordingly.
3957 *
3958 * When you have finished the editing, write "c" (commit) to the file to commit
3959 * your changes.
3960 *
1007bc36
MJ
3961 * If you want to reset to the default value, write "r" (reset) to the file to
3962 * reset them
3963 *
9df5d008
EQ
3964 * This setting works under auto fan control mode only. It can co-exist with
3965 * other settings which can work also under auto mode. It adjusts the PMFW's
3966 * behavior about the minimum fan speed in PWM the fan should spin. Setting
3967 * via this interface will switch the fan control to auto mode implicitly.
3968 */
3969static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
3970 struct kobj_attribute *attr,
3971 char *buf)
3972{
3973 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3974 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3975
3976 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
3977}
3978
3979static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
3980 struct kobj_attribute *attr,
3981 const char *buf,
3982 size_t count)
3983{
3984 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3985 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3986
3987 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3988 PP_OD_EDIT_FAN_MINIMUM_PWM,
3989 buf,
3990 count);
3991}
3992
3993static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
3994{
3995 umode_t umode = 0000;
3996
3997 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
3998 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3999
4000 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
4001 umode |= S_IWUSR;
4002
4003 return umode;
4004}
4005
cfffd980
WM
4006/**
4007 * DOC: fan_zero_rpm_enable
4008 *
4009 * The amdgpu driver provides a sysfs API for checking and adjusting the
4010 * zero RPM feature.
4011 *
4012 * Reading back the file shows you the current setting and the permitted
4013 * ranges if changable.
4014 *
4015 * Writing an integer to the file, change the setting accordingly.
4016 *
4017 * When you have finished the editing, write "c" (commit) to the file to commit
4018 * your changes.
4019 *
4020 * If you want to reset to the default value, write "r" (reset) to the file to
4021 * reset them.
4022 */
4023static ssize_t fan_zero_rpm_enable_show(struct kobject *kobj,
4024 struct kobj_attribute *attr,
4025 char *buf)
4026{
4027 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4028 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4029
4030 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_ENABLE, buf);
4031}
4032
4033static ssize_t fan_zero_rpm_enable_store(struct kobject *kobj,
4034 struct kobj_attribute *attr,
4035 const char *buf,
4036 size_t count)
4037{
4038 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4039 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4040
4041 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4042 PP_OD_EDIT_FAN_ZERO_RPM_ENABLE,
4043 buf,
4044 count);
4045}
4046
4047static umode_t fan_zero_rpm_enable_visible(struct amdgpu_device *adev)
4048{
4049 umode_t umode = 0000;
4050
4051 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE)
4052 umode |= S_IRUSR | S_IRGRP | S_IROTH;
4053
4054 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET)
4055 umode |= S_IWUSR;
4056
4057 return umode;
4058}
4059
6bfe777e
WM
4060/**
4061 * DOC: fan_zero_rpm_stop_temperature
4062 *
4063 * The amdgpu driver provides a sysfs API for checking and adjusting the
4064 * zero RPM stop temperature feature.
4065 *
4066 * Reading back the file shows you the current setting and the permitted
4067 * ranges if changable.
4068 *
4069 * Writing an integer to the file, change the setting accordingly.
4070 *
4071 * When you have finished the editing, write "c" (commit) to the file to commit
4072 * your changes.
4073 *
4074 * If you want to reset to the default value, write "r" (reset) to the file to
4075 * reset them.
4076 *
4077 * This setting works only if the Zero RPM setting is enabled. It adjusts the
4078 * temperature below which the fan can stop.
4079 */
4080static ssize_t fan_zero_rpm_stop_temp_show(struct kobject *kobj,
4081 struct kobj_attribute *attr,
4082 char *buf)
4083{
4084 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4085 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4086
4087 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_STOP_TEMP, buf);
4088}
4089
4090static ssize_t fan_zero_rpm_stop_temp_store(struct kobject *kobj,
4091 struct kobj_attribute *attr,
4092 const char *buf,
4093 size_t count)
4094{
4095 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4096 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4097
4098 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4099 PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP,
4100 buf,
4101 count);
4102}
4103
4104static umode_t fan_zero_rpm_stop_temp_visible(struct amdgpu_device *adev)
4105{
4106 umode_t umode = 0000;
4107
4108 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE)
4109 umode |= S_IRUSR | S_IRGRP | S_IROTH;
4110
4111 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET)
4112 umode |= S_IWUSR;
4113
4114 return umode;
4115}
4116
d7bf1b55
EQ
4117static struct od_feature_set amdgpu_od_set = {
4118 .containers = {
4119 [0] = {
4120 .name = "fan_ctrl",
4121 .sub_feature = {
4122 [0] = {
4123 .name = "fan_curve",
4124 .ops = {
4125 .is_visible = fan_curve_visible,
4126 .show = fan_curve_show,
4127 .store = fan_curve_store,
4128 },
4129 },
548009ad
EQ
4130 [1] = {
4131 .name = "acoustic_limit_rpm_threshold",
4132 .ops = {
4133 .is_visible = acoustic_limit_threshold_visible,
4134 .show = acoustic_limit_threshold_show,
4135 .store = acoustic_limit_threshold_store,
4136 },
4137 },
47cf6fcb
EQ
4138 [2] = {
4139 .name = "acoustic_target_rpm_threshold",
4140 .ops = {
4141 .is_visible = acoustic_target_threshold_visible,
4142 .show = acoustic_target_threshold_show,
4143 .store = acoustic_target_threshold_store,
4144 },
4145 },
eedd5a34
EQ
4146 [3] = {
4147 .name = "fan_target_temperature",
4148 .ops = {
4149 .is_visible = fan_target_temperature_visible,
4150 .show = fan_target_temperature_show,
4151 .store = fan_target_temperature_store,
4152 },
4153 },
9df5d008
EQ
4154 [4] = {
4155 .name = "fan_minimum_pwm",
4156 .ops = {
4157 .is_visible = fan_minimum_pwm_visible,
4158 .show = fan_minimum_pwm_show,
4159 .store = fan_minimum_pwm_store,
4160 },
4161 },
cfffd980
WM
4162 [5] = {
4163 .name = "fan_zero_rpm_enable",
4164 .ops = {
4165 .is_visible = fan_zero_rpm_enable_visible,
4166 .show = fan_zero_rpm_enable_show,
4167 .store = fan_zero_rpm_enable_store,
4168 },
4169 },
6bfe777e
WM
4170 [6] = {
4171 .name = "fan_zero_rpm_stop_temperature",
4172 .ops = {
4173 .is_visible = fan_zero_rpm_stop_temp_visible,
4174 .show = fan_zero_rpm_stop_temp_show,
4175 .store = fan_zero_rpm_stop_temp_store,
4176 },
4177 },
d7bf1b55
EQ
4178 },
4179 },
4180 },
4181};
3e38b634
EQ
4182
4183static void od_kobj_release(struct kobject *kobj)
4184{
4185 struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
4186
4187 kfree(od_kobj);
4188}
4189
4190static const struct kobj_type od_ktype = {
4191 .release = od_kobj_release,
4192 .sysfs_ops = &kobj_sysfs_ops,
4193};
4194
4195static void amdgpu_od_set_fini(struct amdgpu_device *adev)
4196{
4197 struct od_kobj *container, *container_next;
4198 struct od_attribute *attribute, *attribute_next;
4199
4200 if (list_empty(&adev->pm.od_kobj_list))
4201 return;
4202
4203 list_for_each_entry_safe(container, container_next,
4204 &adev->pm.od_kobj_list, entry) {
4205 list_del(&container->entry);
4206
4207 list_for_each_entry_safe(attribute, attribute_next,
4208 &container->attribute, entry) {
4209 list_del(&attribute->entry);
4210 sysfs_remove_file(&container->kobj,
4211 &attribute->attribute.attr);
4212 kfree(attribute);
4213 }
4214
4215 kobject_put(&container->kobj);
4216 }
4217}
4218
4219static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
4220 struct od_feature_ops *feature_ops)
4221{
4222 umode_t mode;
4223
4224 if (!feature_ops->is_visible)
4225 return false;
4226
4227 /*
4228 * If the feature has no user read and write mode set,
4229 * we can assume the feature is actually not supported.(?)
4230 * And the revelant sysfs interface should not be exposed.
4231 */
4232 mode = feature_ops->is_visible(adev);
4233 if (mode & (S_IRUSR | S_IWUSR))
4234 return true;
4235
4236 return false;
4237}
4238
4239static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
4240 struct od_feature_container *container)
4241{
4242 int i;
4243
4244 /*
4245 * If there is no valid entry within the container, the container
4246 * is recognized as a self contained container. And the valid entry
4247 * here means it has a valid naming and it is visible/supported by
4248 * the ASIC.
4249 */
4250 for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
4251 if (container->sub_feature[i].name &&
4252 amdgpu_is_od_feature_supported(adev,
4253 &container->sub_feature[i].ops))
4254 return false;
4255 }
4256
4257 return true;
4258}
4259
4260static int amdgpu_od_set_init(struct amdgpu_device *adev)
d38ceaf9 4261{
3e38b634
EQ
4262 struct od_kobj *top_set, *sub_set;
4263 struct od_attribute *attribute;
4264 struct od_feature_container *container;
4265 struct od_feature_item *feature;
4266 int i, j;
d38ceaf9 4267 int ret;
3e38b634
EQ
4268
4269 /* Setup the top `gpu_od` directory which holds all other OD interfaces */
4270 top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
4271 if (!top_set)
4272 return -ENOMEM;
4273 list_add(&top_set->entry, &adev->pm.od_kobj_list);
4274
4275 ret = kobject_init_and_add(&top_set->kobj,
4276 &od_ktype,
4277 &adev->dev->kobj,
4278 "%s",
4279 "gpu_od");
4280 if (ret)
4281 goto err_out;
4282 INIT_LIST_HEAD(&top_set->attribute);
4283 top_set->priv = adev;
4284
4285 for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
4286 container = &amdgpu_od_set.containers[i];
4287
4288 if (!container->name)
4289 continue;
4290
4291 /*
4292 * If there is valid entries within the container, the container
4293 * will be presented as a sub directory and all its holding entries
4294 * will be presented as plain files under it.
4295 * While if there is no valid entry within the container, the container
4296 * itself will be presented as a plain file under top `gpu_od` directory.
4297 */
4298 if (amdgpu_od_is_self_contained(adev, container)) {
4299 if (!amdgpu_is_od_feature_supported(adev,
4300 &container->ops))
4301 continue;
4302
4303 /*
4304 * The container is presented as a plain file under top `gpu_od`
4305 * directory.
4306 */
4307 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4308 if (!attribute) {
4309 ret = -ENOMEM;
4310 goto err_out;
4311 }
4312 list_add(&attribute->entry, &top_set->attribute);
4313
4314 attribute->attribute.attr.mode =
4315 container->ops.is_visible(adev);
4316 attribute->attribute.attr.name = container->name;
4317 attribute->attribute.show =
4318 container->ops.show;
4319 attribute->attribute.store =
4320 container->ops.store;
4321 ret = sysfs_create_file(&top_set->kobj,
4322 &attribute->attribute.attr);
4323 if (ret)
4324 goto err_out;
4325 } else {
4326 /* The container is presented as a sub directory. */
4327 sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
4328 if (!sub_set) {
4329 ret = -ENOMEM;
4330 goto err_out;
4331 }
4332 list_add(&sub_set->entry, &adev->pm.od_kobj_list);
4333
4334 ret = kobject_init_and_add(&sub_set->kobj,
4335 &od_ktype,
4336 &top_set->kobj,
4337 "%s",
4338 container->name);
4339 if (ret)
4340 goto err_out;
4341 INIT_LIST_HEAD(&sub_set->attribute);
4342 sub_set->priv = adev;
4343
4344 for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
4345 feature = &container->sub_feature[j];
4346 if (!feature->name)
4347 continue;
4348
4349 if (!amdgpu_is_od_feature_supported(adev,
4350 &feature->ops))
4351 continue;
4352
4353 /*
4354 * With the container presented as a sub directory, the entry within
4355 * it is presented as a plain file under the sub directory.
4356 */
4357 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4358 if (!attribute) {
4359 ret = -ENOMEM;
4360 goto err_out;
4361 }
4362 list_add(&attribute->entry, &sub_set->attribute);
4363
4364 attribute->attribute.attr.mode =
4365 feature->ops.is_visible(adev);
4366 attribute->attribute.attr.name = feature->name;
4367 attribute->attribute.show =
4368 feature->ops.show;
4369 attribute->attribute.store =
4370 feature->ops.store;
4371 ret = sysfs_create_file(&sub_set->kobj,
4372 &attribute->attribute.attr);
4373 if (ret)
4374 goto err_out;
4375 }
4376 }
4377 }
4378
69bc7a8a
MJ
4379 /*
4380 * If gpu_od is the only member in the list, that means gpu_od is an
4381 * empty directory, so remove it.
4382 */
4383 if (list_is_singular(&adev->pm.od_kobj_list))
4384 goto err_out;
4385
3e38b634
EQ
4386 return 0;
4387
4388err_out:
4389 amdgpu_od_set_fini(adev);
4390
4391 return ret;
4392}
4393
4394int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4395{
88e5c8f8 4396 enum amdgpu_sriov_vf_mode mode;
4e01847c 4397 uint32_t mask = 0;
3e38b634 4398 int ret;
d38ceaf9 4399
c86f5ebf
AD
4400 if (adev->pm.sysfs_initialized)
4401 return 0;
4402
5fa99373
ZY
4403 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
4404
d2f52ac8
RZ
4405 if (adev->pm.dpm_enabled == 0)
4406 return 0;
4407
88e5c8f8
MJ
4408 mode = amdgpu_virt_get_sriov_vf_mode(adev);
4409
4410 /* under multi-vf mode, the hwmon attributes are all not supported */
4411 if (mode != SRIOV_VF_MODE_MULTI_VF) {
4412 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
948e2094
MJ
4413 DRIVER_NAME, adev,
4414 hwmon_groups);
88e5c8f8
MJ
4415 if (IS_ERR(adev->pm.int_hwmon_dev)) {
4416 ret = PTR_ERR(adev->pm.int_hwmon_dev);
4417 dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4418 return ret;
4419 }
d38ceaf9
AD
4420 }
4421
88e5c8f8 4422 switch (mode) {
4e01847c
KW
4423 case SRIOV_VF_MODE_ONE_VF:
4424 mask = ATTR_FLAG_ONEVF;
4425 break;
4426 case SRIOV_VF_MODE_MULTI_VF:
4427 mask = 0;
4428 break;
4429 case SRIOV_VF_MODE_BARE_METAL:
4430 default:
4431 mask = ATTR_FLAG_MASK_ALL;
4432 break;
8efd7275
ML
4433 }
4434
4e01847c
KW
4435 ret = amdgpu_device_attr_create_groups(adev,
4436 amdgpu_device_attrs,
4437 ARRAY_SIZE(amdgpu_device_attrs),
ba02fd6b
KW
4438 mask,
4439 &adev->pm.pm_attr_list);
4e01847c 4440 if (ret)
3e38b634
EQ
4441 goto err_out0;
4442
4443 if (amdgpu_dpm_is_overdrive_supported(adev)) {
4444 ret = amdgpu_od_set_init(adev);
4445 if (ret)
4446 goto err_out1;
e6f1a194
MJ
4447 } else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
4448 dev_info(adev->dev, "overdrive feature is not supported\n");
3e38b634 4449 }
7ca881a8 4450
4d154b1c
LL
4451 if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) !=
4452 -EOPNOTSUPP) {
4453 ret = devm_device_add_group(adev->dev,
4454 &amdgpu_pm_policy_attr_group);
4455 if (ret)
4456 goto err_out0;
4457 }
4458
c86f5ebf
AD
4459 adev->pm.sysfs_initialized = true;
4460
d38ceaf9 4461 return 0;
3e38b634
EQ
4462
4463err_out1:
4464 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4465err_out0:
4466 if (adev->pm.int_hwmon_dev)
4467 hwmon_device_unregister(adev->pm.int_hwmon_dev);
4468
4469 return ret;
d38ceaf9
AD
4470}
4471
4472void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4473{
3e38b634
EQ
4474 amdgpu_od_set_fini(adev);
4475
d38ceaf9
AD
4476 if (adev->pm.int_hwmon_dev)
4477 hwmon_device_unregister(adev->pm.int_hwmon_dev);
4e01847c 4478
ba02fd6b 4479 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
d38ceaf9
AD
4480}
4481
d38ceaf9
AD
4482/*
4483 * Debugfs info
4484 */
4485#if defined(CONFIG_DEBUG_FS)
4486
517cb957 4487static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
e1b3bcaa
RS
4488 struct amdgpu_device *adev)
4489{
517cb957
HR
4490 uint16_t *p_val;
4491 uint32_t size;
4492 int i;
79c65f3f 4493 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
517cb957 4494
79c65f3f
EQ
4495 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
4496 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
517cb957
HR
4497 GFP_KERNEL);
4498
4499 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4500 (void *)p_val, &size)) {
79c65f3f 4501 for (i = 0; i < num_cpu_cores; i++)
517cb957
HR
4502 seq_printf(m, "\t%u MHz (CPU%d)\n",
4503 *(p_val + i), i);
4504 }
4505
4506 kfree(p_val);
4507 }
4508}
4509
3de4ec57
TSD
4510static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4511{
4e8303cf
LL
4512 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
4513 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
cd7b0c66 4514 uint32_t value;
800c53d6 4515 uint64_t value64 = 0;
5b79d048 4516 uint32_t query = 0;
9f8df7d7 4517 int size;
3de4ec57 4518
3de4ec57 4519 /* GPU Clocks */
9f8df7d7 4520 size = sizeof(value);
3de4ec57 4521 seq_printf(m, "GFX Clocks and Power:\n");
517cb957
HR
4522
4523 amdgpu_debugfs_prints_cpu_info(m, adev);
4524
9f8df7d7 4525 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3de4ec57 4526 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
9f8df7d7 4527 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3de4ec57 4528 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
5ed8d656
RZ
4529 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4530 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4531 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4532 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
9f8df7d7 4533 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3de4ec57 4534 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
9f8df7d7 4535 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3de4ec57 4536 seq_printf(m, "\t%u mV (VDDNB)\n", value);
5b79d048 4537 size = sizeof(uint32_t);
6127d7df
AD
4538 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
4539 if (adev->flags & AMD_IS_APU)
4540 seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
4541 else
4542 seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
4543 }
e0e1764a 4544 size = sizeof(uint32_t);
6127d7df
AD
4545 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
4546 if (adev->flags & AMD_IS_APU)
4547 seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
4548 else
4549 seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
4550 }
9f8df7d7 4551 size = sizeof(value);
3de4ec57
TSD
4552 seq_printf(m, "\n");
4553
4554 /* GPU Temp */
9f8df7d7 4555 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3de4ec57
TSD
4556 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4557
4558 /* GPU Load */
9f8df7d7 4559 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3de4ec57 4560 seq_printf(m, "GPU Load: %u %%\n", value);
9b6eb00d
TSD
4561 /* MEM Load */
4562 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4563 seq_printf(m, "MEM Load: %u %%\n", value);
d1b2703c
XD
4564 /* VCN Load */
4565 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4566 seq_printf(m, "VCN Load: %u %%\n", value);
9b6eb00d 4567
3de4ec57
TSD
4568 seq_printf(m, "\n");
4569
505f8dbb
AD
4570 /* SMC feature mask */
4571 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4572 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4573
8ecad8d6
LL
4574 /* ASICs greater than CHIP_VEGA20 supports these sensors */
4575 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
1f96ecef
EQ
4576 /* VCN clocks */
4577 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4578 if (!value) {
6127d7df 4579 seq_printf(m, "VCN: Powered down\n");
1f96ecef 4580 } else {
6127d7df 4581 seq_printf(m, "VCN: Powered up\n");
1f96ecef
EQ
4582 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4583 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4584 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4585 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4586 }
3de4ec57 4587 }
1f96ecef
EQ
4588 seq_printf(m, "\n");
4589 } else {
4590 /* UVD clocks */
4591 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4592 if (!value) {
6127d7df 4593 seq_printf(m, "UVD: Powered down\n");
1f96ecef 4594 } else {
6127d7df 4595 seq_printf(m, "UVD: Powered up\n");
1f96ecef
EQ
4596 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4597 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4598 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4599 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4600 }
4601 }
4602 seq_printf(m, "\n");
3de4ec57 4603
1f96ecef
EQ
4604 /* VCE clocks */
4605 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4606 if (!value) {
6127d7df 4607 seq_printf(m, "VCE: Powered down\n");
1f96ecef 4608 } else {
6127d7df 4609 seq_printf(m, "VCE: Powered up\n");
1f96ecef
EQ
4610 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4611 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4612 }
3de4ec57
TSD
4613 }
4614 }
4615
4616 return 0;
4617}
4618
44762718
NC
4619static const struct cg_flag_name clocks[] = {
4620 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
4621 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
4622 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
4623 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
4624 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
4625 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
4626 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
4627 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
4628 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
4629 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
4630 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
4631 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
4632 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
4633 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
4634 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
4635 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
4636 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
4637 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
4638 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
4639 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
4640 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
4641 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
4642 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
4643 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
4644 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
4645 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
4646 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
4647 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
4648 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
4649 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
4650 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
4651 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
4652 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
4653 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
4654 {0, NULL},
4655};
4656
25faeddc 4657static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
a8503b15
HR
4658{
4659 int i;
4660
4661 for (i = 0; clocks[i].flag; i++)
4662 seq_printf(m, "\t%s: %s\n", clocks[i].name,
4663 (flags & clocks[i].flag) ? "On" : "Off");
4664}
4665
373720f7 4666static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
d38ceaf9 4667{
373720f7 4668 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
25faeddc 4669 u64 flags = 0;
b9a9294b
AD
4670 int r;
4671
55aa33c3 4672 r = amdgpu_pm_get_access(adev);
ded57e49 4673 if (r < 0)
b9a9294b 4674 return r;
6cb2d4e4 4675
79c65f3f 4676 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
b9a9294b 4677 r = amdgpu_debugfs_pm_info_pp(m, adev);
79c65f3f
EQ
4678 if (r)
4679 goto out;
d38ceaf9 4680 }
81b41ff5 4681
81b41ff5 4682 amdgpu_device_ip_get_clockgating_state(adev, &flags);
81b41ff5 4683
25faeddc 4684 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
81b41ff5
EQ
4685 amdgpu_parse_cg_state(m, flags);
4686 seq_printf(m, "\n");
d38ceaf9 4687
81b41ff5 4688out:
55aa33c3 4689 amdgpu_pm_put_access(adev);
b9a9294b
AD
4690
4691 return r;
d38ceaf9
AD
4692}
4693
373720f7
ND
4694DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4695
27ebf21f
LL
4696/*
4697 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
4698 *
4699 * Reads debug memory region allocated to PMFW
4700 */
4701static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
4702 size_t size, loff_t *pos)
4703{
4704 struct amdgpu_device *adev = file_inode(f)->i_private;
27ebf21f
LL
4705 size_t smu_prv_buf_size;
4706 void *smu_prv_buf;
79c65f3f 4707 int ret = 0;
27ebf21f 4708
543f6e71 4709 ret = amdgpu_pm_dev_state_check(adev, true);
55aa33c3
LL
4710 if (ret)
4711 return ret;
27ebf21f 4712
79c65f3f
EQ
4713 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
4714 if (ret)
4715 return ret;
27ebf21f
LL
4716
4717 if (!smu_prv_buf || !smu_prv_buf_size)
4718 return -EINVAL;
4719
4720 return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
4721 smu_prv_buf_size);
4722}
4723
4724static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
4725 .owner = THIS_MODULE,
4726 .open = simple_open,
4727 .read = amdgpu_pm_prv_buffer_read,
4728 .llseek = default_llseek,
4729};
4730
d38ceaf9
AD
4731#endif
4732
373720f7 4733void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
d38ceaf9
AD
4734{
4735#if defined(CONFIG_DEBUG_FS)
373720f7
ND
4736 struct drm_minor *minor = adev_to_drm(adev)->primary;
4737 struct dentry *root = minor->debugfs_root;
4738
1613f346
FC
4739 if (!adev->pm.dpm_enabled)
4740 return;
4741
373720f7
ND
4742 debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4743 &amdgpu_debugfs_pm_info_fops);
4744
27ebf21f
LL
4745 if (adev->pm.smu_prv_buffer_size > 0)
4746 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
4747 adev,
4748 &amdgpu_debugfs_pm_prv_buffer_fops,
4749 adev->pm.smu_prv_buffer_size);
1f5fc7a5 4750
79c65f3f 4751 amdgpu_dpm_stb_debug_fs_init(adev);
d38ceaf9
AD
4752#endif
4753}