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