1 /*
2 * Copyright 2019 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
23 #define SWSMU_CODE_LAYER_L1
24
25 #include <linux/firmware.h>
26 #include <linux/pci.h>
27 #include <linux/power_supply.h>
28 #include <linux/reboot.h>
29
30 #include "amdgpu.h"
31 #include "amdgpu_smu.h"
32 #include "smu_internal.h"
33 #include "atom.h"
34 #include "arcturus_ppt.h"
35 #include "navi10_ppt.h"
36 #include "sienna_cichlid_ppt.h"
37 #include "renoir_ppt.h"
38 #include "vangogh_ppt.h"
39 #include "aldebaran_ppt.h"
40 #include "yellow_carp_ppt.h"
41 #include "cyan_skillfish_ppt.h"
42 #include "smu_v13_0_0_ppt.h"
43 #include "smu_v13_0_4_ppt.h"
44 #include "smu_v13_0_5_ppt.h"
45 #include "smu_v13_0_6_ppt.h"
46 #include "smu_v13_0_7_ppt.h"
47 #include "smu_v14_0_0_ppt.h"
48 #include "smu_v14_0_2_ppt.h"
49 #include "amd_pcie.h"
50
51 /*
52 * DO NOT use these for err/warn/info/debug messages.
53 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
54 * They are more MGPU friendly.
55 */
56 #undef pr_err
57 #undef pr_warn
58 #undef pr_info
59 #undef pr_debug
60
61 static const struct amd_pm_funcs swsmu_pm_funcs;
62 static int smu_force_smuclk_levels(struct smu_context *smu,
63 enum smu_clk_type clk_type,
64 uint32_t mask);
65 static int smu_handle_task(struct smu_context *smu,
66 enum amd_dpm_forced_level level,
67 enum amd_pp_task task_id);
68 static int smu_reset(struct smu_context *smu);
69 static int smu_set_fan_speed_pwm(void *handle, u32 speed);
70 static int smu_set_fan_control_mode(void *handle, u32 value);
71 static int smu_set_power_limit(void *handle, uint32_t limit);
72 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed);
73 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled);
74 static int smu_set_mp1_state(void *handle, enum pp_mp1_state mp1_state);
75 static void smu_power_profile_mode_get(struct smu_context *smu,
76 enum PP_SMC_POWER_PROFILE profile_mode);
77 static void smu_power_profile_mode_put(struct smu_context *smu,
78 enum PP_SMC_POWER_PROFILE profile_mode);
79
smu_sys_get_pp_feature_mask(void * handle,char * buf)80 static int smu_sys_get_pp_feature_mask(void *handle,
81 char *buf)
82 {
83 struct smu_context *smu = handle;
84
85 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
86 return -EOPNOTSUPP;
87
88 return smu_get_pp_feature_mask(smu, buf);
89 }
90
smu_sys_set_pp_feature_mask(void * handle,uint64_t new_mask)91 static int smu_sys_set_pp_feature_mask(void *handle,
92 uint64_t new_mask)
93 {
94 struct smu_context *smu = handle;
95
96 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
97 return -EOPNOTSUPP;
98
99 return smu_set_pp_feature_mask(smu, new_mask);
100 }
101
smu_set_residency_gfxoff(struct smu_context * smu,bool value)102 int smu_set_residency_gfxoff(struct smu_context *smu, bool value)
103 {
104 if (!smu->ppt_funcs->set_gfx_off_residency)
105 return -EINVAL;
106
107 return smu_set_gfx_off_residency(smu, value);
108 }
109
smu_get_residency_gfxoff(struct smu_context * smu,u32 * value)110 int smu_get_residency_gfxoff(struct smu_context *smu, u32 *value)
111 {
112 if (!smu->ppt_funcs->get_gfx_off_residency)
113 return -EINVAL;
114
115 return smu_get_gfx_off_residency(smu, value);
116 }
117
smu_get_entrycount_gfxoff(struct smu_context * smu,u64 * value)118 int smu_get_entrycount_gfxoff(struct smu_context *smu, u64 *value)
119 {
120 if (!smu->ppt_funcs->get_gfx_off_entrycount)
121 return -EINVAL;
122
123 return smu_get_gfx_off_entrycount(smu, value);
124 }
125
smu_get_status_gfxoff(struct smu_context * smu,uint32_t * value)126 int smu_get_status_gfxoff(struct smu_context *smu, uint32_t *value)
127 {
128 if (!smu->ppt_funcs->get_gfx_off_status)
129 return -EINVAL;
130
131 *value = smu_get_gfx_off_status(smu);
132
133 return 0;
134 }
135
smu_set_soft_freq_range(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t min,uint32_t max)136 int smu_set_soft_freq_range(struct smu_context *smu,
137 enum smu_clk_type clk_type,
138 uint32_t min,
139 uint32_t max)
140 {
141 int ret = 0;
142
143 if (smu->ppt_funcs->set_soft_freq_limited_range)
144 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
145 clk_type,
146 min,
147 max);
148
149 return ret;
150 }
151
smu_get_dpm_freq_range(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t * min,uint32_t * max)152 int smu_get_dpm_freq_range(struct smu_context *smu,
153 enum smu_clk_type clk_type,
154 uint32_t *min,
155 uint32_t *max)
156 {
157 int ret = -ENOTSUPP;
158
159 if (!min && !max)
160 return -EINVAL;
161
162 if (smu->ppt_funcs->get_dpm_ultimate_freq)
163 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
164 clk_type,
165 min,
166 max);
167
168 return ret;
169 }
170
smu_set_gfx_power_up_by_imu(struct smu_context * smu)171 int smu_set_gfx_power_up_by_imu(struct smu_context *smu)
172 {
173 int ret = 0;
174 struct amdgpu_device *adev = smu->adev;
175
176 if (smu->ppt_funcs->set_gfx_power_up_by_imu) {
177 ret = smu->ppt_funcs->set_gfx_power_up_by_imu(smu);
178 if (ret)
179 dev_err(adev->dev, "Failed to enable gfx imu!\n");
180 }
181 return ret;
182 }
183
smu_get_mclk(void * handle,bool low)184 static u32 smu_get_mclk(void *handle, bool low)
185 {
186 struct smu_context *smu = handle;
187 uint32_t clk_freq;
188 int ret = 0;
189
190 ret = smu_get_dpm_freq_range(smu, SMU_UCLK,
191 low ? &clk_freq : NULL,
192 !low ? &clk_freq : NULL);
193 if (ret)
194 return 0;
195 return clk_freq * 100;
196 }
197
smu_get_sclk(void * handle,bool low)198 static u32 smu_get_sclk(void *handle, bool low)
199 {
200 struct smu_context *smu = handle;
201 uint32_t clk_freq;
202 int ret = 0;
203
204 ret = smu_get_dpm_freq_range(smu, SMU_GFXCLK,
205 low ? &clk_freq : NULL,
206 !low ? &clk_freq : NULL);
207 if (ret)
208 return 0;
209 return clk_freq * 100;
210 }
211
smu_set_gfx_imu_enable(struct smu_context * smu)212 static int smu_set_gfx_imu_enable(struct smu_context *smu)
213 {
214 struct amdgpu_device *adev = smu->adev;
215
216 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
217 return 0;
218
219 if (amdgpu_in_reset(smu->adev) || adev->in_s0ix)
220 return 0;
221
222 return smu_set_gfx_power_up_by_imu(smu);
223 }
224
is_vcn_enabled(struct amdgpu_device * adev)225 static bool is_vcn_enabled(struct amdgpu_device *adev)
226 {
227 int i;
228
229 for (i = 0; i < adev->num_ip_blocks; i++) {
230 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_VCN ||
231 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_JPEG) &&
232 !adev->ip_blocks[i].status.valid)
233 return false;
234 }
235
236 return true;
237 }
238
smu_dpm_set_vcn_enable(struct smu_context * smu,bool enable)239 static int smu_dpm_set_vcn_enable(struct smu_context *smu,
240 bool enable)
241 {
242 struct smu_power_context *smu_power = &smu->smu_power;
243 struct smu_power_gate *power_gate = &smu_power->power_gate;
244 int ret = 0;
245
246 /*
247 * don't poweron vcn/jpeg when they are skipped.
248 */
249 if (!is_vcn_enabled(smu->adev))
250 return 0;
251
252 if (!smu->ppt_funcs->dpm_set_vcn_enable)
253 return 0;
254
255 if (atomic_read(&power_gate->vcn_gated) ^ enable)
256 return 0;
257
258 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
259 if (!ret)
260 atomic_set(&power_gate->vcn_gated, !enable);
261
262 return ret;
263 }
264
smu_dpm_set_jpeg_enable(struct smu_context * smu,bool enable)265 static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
266 bool enable)
267 {
268 struct smu_power_context *smu_power = &smu->smu_power;
269 struct smu_power_gate *power_gate = &smu_power->power_gate;
270 int ret = 0;
271
272 if (!is_vcn_enabled(smu->adev))
273 return 0;
274
275 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
276 return 0;
277
278 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
279 return 0;
280
281 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
282 if (!ret)
283 atomic_set(&power_gate->jpeg_gated, !enable);
284
285 return ret;
286 }
287
smu_dpm_set_vpe_enable(struct smu_context * smu,bool enable)288 static int smu_dpm_set_vpe_enable(struct smu_context *smu,
289 bool enable)
290 {
291 struct smu_power_context *smu_power = &smu->smu_power;
292 struct smu_power_gate *power_gate = &smu_power->power_gate;
293 int ret = 0;
294
295 if (!smu->ppt_funcs->dpm_set_vpe_enable)
296 return 0;
297
298 if (atomic_read(&power_gate->vpe_gated) ^ enable)
299 return 0;
300
301 ret = smu->ppt_funcs->dpm_set_vpe_enable(smu, enable);
302 if (!ret)
303 atomic_set(&power_gate->vpe_gated, !enable);
304
305 return ret;
306 }
307
smu_dpm_set_umsch_mm_enable(struct smu_context * smu,bool enable)308 static int smu_dpm_set_umsch_mm_enable(struct smu_context *smu,
309 bool enable)
310 {
311 struct smu_power_context *smu_power = &smu->smu_power;
312 struct smu_power_gate *power_gate = &smu_power->power_gate;
313 int ret = 0;
314
315 if (!smu->adev->enable_umsch_mm)
316 return 0;
317
318 if (!smu->ppt_funcs->dpm_set_umsch_mm_enable)
319 return 0;
320
321 if (atomic_read(&power_gate->umsch_mm_gated) ^ enable)
322 return 0;
323
324 ret = smu->ppt_funcs->dpm_set_umsch_mm_enable(smu, enable);
325 if (!ret)
326 atomic_set(&power_gate->umsch_mm_gated, !enable);
327
328 return ret;
329 }
330
smu_set_mall_enable(struct smu_context * smu)331 static int smu_set_mall_enable(struct smu_context *smu)
332 {
333 int ret = 0;
334
335 if (!smu->ppt_funcs->set_mall_enable)
336 return 0;
337
338 ret = smu->ppt_funcs->set_mall_enable(smu);
339
340 return ret;
341 }
342
343 /**
344 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
345 *
346 * @handle: smu_context pointer
347 * @block_type: the IP block to power gate/ungate
348 * @gate: to power gate if true, ungate otherwise
349 *
350 * This API uses no smu->mutex lock protection due to:
351 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
352 * This is guarded to be race condition free by the caller.
353 * 2. Or get called on user setting request of power_dpm_force_performance_level.
354 * Under this case, the smu->mutex lock protection is already enforced on
355 * the parent API smu_force_performance_level of the call path.
356 */
smu_dpm_set_power_gate(void * handle,uint32_t block_type,bool gate)357 static int smu_dpm_set_power_gate(void *handle,
358 uint32_t block_type,
359 bool gate)
360 {
361 struct smu_context *smu = handle;
362 int ret = 0;
363
364 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled) {
365 dev_WARN(smu->adev->dev,
366 "SMU uninitialized but power %s requested for %u!\n",
367 gate ? "gate" : "ungate", block_type);
368 return -EOPNOTSUPP;
369 }
370
371 switch (block_type) {
372 /*
373 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
374 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
375 */
376 case AMD_IP_BLOCK_TYPE_UVD:
377 case AMD_IP_BLOCK_TYPE_VCN:
378 ret = smu_dpm_set_vcn_enable(smu, !gate);
379 if (ret)
380 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
381 gate ? "gate" : "ungate");
382 break;
383 case AMD_IP_BLOCK_TYPE_GFX:
384 ret = smu_gfx_off_control(smu, gate);
385 if (ret)
386 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
387 gate ? "enable" : "disable");
388 break;
389 case AMD_IP_BLOCK_TYPE_SDMA:
390 ret = smu_powergate_sdma(smu, gate);
391 if (ret)
392 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
393 gate ? "gate" : "ungate");
394 break;
395 case AMD_IP_BLOCK_TYPE_JPEG:
396 ret = smu_dpm_set_jpeg_enable(smu, !gate);
397 if (ret)
398 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
399 gate ? "gate" : "ungate");
400 break;
401 case AMD_IP_BLOCK_TYPE_VPE:
402 ret = smu_dpm_set_vpe_enable(smu, !gate);
403 if (ret)
404 dev_err(smu->adev->dev, "Failed to power %s VPE!\n",
405 gate ? "gate" : "ungate");
406 break;
407 default:
408 dev_err(smu->adev->dev, "Unsupported block type!\n");
409 return -EINVAL;
410 }
411
412 return ret;
413 }
414
415 /**
416 * smu_set_user_clk_dependencies - set user profile clock dependencies
417 *
418 * @smu: smu_context pointer
419 * @clk: enum smu_clk_type type
420 *
421 * Enable/Disable the clock dependency for the @clk type.
422 */
smu_set_user_clk_dependencies(struct smu_context * smu,enum smu_clk_type clk)423 static void smu_set_user_clk_dependencies(struct smu_context *smu, enum smu_clk_type clk)
424 {
425 if (smu->adev->in_suspend)
426 return;
427
428 if (clk == SMU_MCLK) {
429 smu->user_dpm_profile.clk_dependency = 0;
430 smu->user_dpm_profile.clk_dependency = BIT(SMU_FCLK) | BIT(SMU_SOCCLK);
431 } else if (clk == SMU_FCLK) {
432 /* MCLK takes precedence over FCLK */
433 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
434 return;
435
436 smu->user_dpm_profile.clk_dependency = 0;
437 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_SOCCLK);
438 } else if (clk == SMU_SOCCLK) {
439 /* MCLK takes precedence over SOCCLK */
440 if (smu->user_dpm_profile.clk_dependency == (BIT(SMU_FCLK) | BIT(SMU_SOCCLK)))
441 return;
442
443 smu->user_dpm_profile.clk_dependency = 0;
444 smu->user_dpm_profile.clk_dependency = BIT(SMU_MCLK) | BIT(SMU_FCLK);
445 } else
446 /* Add clk dependencies here, if any */
447 return;
448 }
449
450 /**
451 * smu_restore_dpm_user_profile - reinstate user dpm profile
452 *
453 * @smu: smu_context pointer
454 *
455 * Restore the saved user power configurations include power limit,
456 * clock frequencies, fan control mode and fan speed.
457 */
smu_restore_dpm_user_profile(struct smu_context * smu)458 static void smu_restore_dpm_user_profile(struct smu_context *smu)
459 {
460 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
461 int ret = 0;
462
463 if (!smu->adev->in_suspend)
464 return;
465
466 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
467 return;
468
469 /* Enable restore flag */
470 smu->user_dpm_profile.flags |= SMU_DPM_USER_PROFILE_RESTORE;
471
472 /* set the user dpm power limit */
473 if (smu->user_dpm_profile.power_limit) {
474 ret = smu_set_power_limit(smu, smu->user_dpm_profile.power_limit);
475 if (ret)
476 dev_err(smu->adev->dev, "Failed to set power limit value\n");
477 }
478
479 /* set the user dpm clock configurations */
480 if (smu_dpm_ctx->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
481 enum smu_clk_type clk_type;
482
483 for (clk_type = 0; clk_type < SMU_CLK_COUNT; clk_type++) {
484 /*
485 * Iterate over smu clk type and force the saved user clk
486 * configs, skip if clock dependency is enabled
487 */
488 if (!(smu->user_dpm_profile.clk_dependency & BIT(clk_type)) &&
489 smu->user_dpm_profile.clk_mask[clk_type]) {
490 ret = smu_force_smuclk_levels(smu, clk_type,
491 smu->user_dpm_profile.clk_mask[clk_type]);
492 if (ret)
493 dev_err(smu->adev->dev,
494 "Failed to set clock type = %d\n", clk_type);
495 }
496 }
497 }
498
499 /* set the user dpm fan configurations */
500 if (smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_MANUAL ||
501 smu->user_dpm_profile.fan_mode == AMD_FAN_CTRL_NONE) {
502 ret = smu_set_fan_control_mode(smu, smu->user_dpm_profile.fan_mode);
503 if (ret != -EOPNOTSUPP) {
504 smu->user_dpm_profile.fan_speed_pwm = 0;
505 smu->user_dpm_profile.fan_speed_rpm = 0;
506 smu->user_dpm_profile.fan_mode = AMD_FAN_CTRL_AUTO;
507 dev_err(smu->adev->dev, "Failed to set manual fan control mode\n");
508 }
509
510 if (smu->user_dpm_profile.fan_speed_pwm) {
511 ret = smu_set_fan_speed_pwm(smu, smu->user_dpm_profile.fan_speed_pwm);
512 if (ret != -EOPNOTSUPP)
513 dev_err(smu->adev->dev, "Failed to set manual fan speed in pwm\n");
514 }
515
516 if (smu->user_dpm_profile.fan_speed_rpm) {
517 ret = smu_set_fan_speed_rpm(smu, smu->user_dpm_profile.fan_speed_rpm);
518 if (ret != -EOPNOTSUPP)
519 dev_err(smu->adev->dev, "Failed to set manual fan speed in rpm\n");
520 }
521 }
522
523 /* Restore user customized OD settings */
524 if (smu->user_dpm_profile.user_od) {
525 if (smu->ppt_funcs->restore_user_od_settings) {
526 ret = smu->ppt_funcs->restore_user_od_settings(smu);
527 if (ret)
528 dev_err(smu->adev->dev, "Failed to upload customized OD settings\n");
529 }
530 }
531
532 /* Disable restore flag */
533 smu->user_dpm_profile.flags &= ~SMU_DPM_USER_PROFILE_RESTORE;
534 }
535
smu_get_power_num_states(void * handle,struct pp_states_info * state_info)536 static int smu_get_power_num_states(void *handle,
537 struct pp_states_info *state_info)
538 {
539 if (!state_info)
540 return -EINVAL;
541
542 /* not support power state */
543 memset(state_info, 0, sizeof(struct pp_states_info));
544 state_info->nums = 1;
545 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
546
547 return 0;
548 }
549
is_support_sw_smu(struct amdgpu_device * adev)550 bool is_support_sw_smu(struct amdgpu_device *adev)
551 {
552 /* vega20 is 11.0.2, but it's supported via the powerplay code */
553 if (adev->asic_type == CHIP_VEGA20)
554 return false;
555
556 if (amdgpu_ip_version(adev, MP1_HWIP, 0) >= IP_VERSION(11, 0, 0))
557 return true;
558
559 return false;
560 }
561
is_support_cclk_dpm(struct amdgpu_device * adev)562 bool is_support_cclk_dpm(struct amdgpu_device *adev)
563 {
564 struct smu_context *smu = adev->powerplay.pp_handle;
565
566 if (!smu_feature_is_enabled(smu, SMU_FEATURE_CCLK_DPM_BIT))
567 return false;
568
569 return true;
570 }
571
572
smu_sys_get_pp_table(void * handle,char ** table)573 static int smu_sys_get_pp_table(void *handle,
574 char **table)
575 {
576 struct smu_context *smu = handle;
577 struct smu_table_context *smu_table = &smu->smu_table;
578
579 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
580 return -EOPNOTSUPP;
581
582 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
583 return -EINVAL;
584
585 if (smu_table->hardcode_pptable)
586 *table = smu_table->hardcode_pptable;
587 else
588 *table = smu_table->power_play_table;
589
590 return smu_table->power_play_table_size;
591 }
592
smu_sys_set_pp_table(void * handle,const char * buf,size_t size)593 static int smu_sys_set_pp_table(void *handle,
594 const char *buf,
595 size_t size)
596 {
597 struct smu_context *smu = handle;
598 struct smu_table_context *smu_table = &smu->smu_table;
599 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
600 int ret = 0;
601
602 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
603 return -EOPNOTSUPP;
604
605 if (header->usStructureSize != size) {
606 dev_err(smu->adev->dev, "pp table size not matched !\n");
607 return -EIO;
608 }
609
610 if (!smu_table->hardcode_pptable) {
611 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
612 if (!smu_table->hardcode_pptable)
613 return -ENOMEM;
614 }
615
616 memcpy(smu_table->hardcode_pptable, buf, size);
617 smu_table->power_play_table = smu_table->hardcode_pptable;
618 smu_table->power_play_table_size = size;
619
620 /*
621 * Special hw_fini action(for Navi1x, the DPMs disablement will be
622 * skipped) may be needed for custom pptable uploading.
623 */
624 smu->uploading_custom_pp_table = true;
625
626 ret = smu_reset(smu);
627 if (ret)
628 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
629
630 smu->uploading_custom_pp_table = false;
631
632 return ret;
633 }
634
smu_get_driver_allowed_feature_mask(struct smu_context * smu)635 static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
636 {
637 struct smu_feature *feature = &smu->smu_feature;
638 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
639 int ret = 0;
640
641 /*
642 * With SCPM enabled, the allowed featuremasks setting(via
643 * PPSMC_MSG_SetAllowedFeaturesMaskLow/High) is not permitted.
644 * That means there is no way to let PMFW knows the settings below.
645 * Thus, we just assume all the features are allowed under
646 * such scenario.
647 */
648 if (smu->adev->scpm_enabled) {
649 bitmap_fill(feature->allowed, SMU_FEATURE_MAX);
650 return 0;
651 }
652
653 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
654
655 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
656 SMU_FEATURE_MAX/32);
657 if (ret)
658 return ret;
659
660 bitmap_or(feature->allowed, feature->allowed,
661 (unsigned long *)allowed_feature_mask,
662 feature->feature_num);
663
664 return ret;
665 }
666
smu_set_funcs(struct amdgpu_device * adev)667 static int smu_set_funcs(struct amdgpu_device *adev)
668 {
669 struct smu_context *smu = adev->powerplay.pp_handle;
670
671 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
672 smu->od_enabled = true;
673
674 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
675 case IP_VERSION(11, 0, 0):
676 case IP_VERSION(11, 0, 5):
677 case IP_VERSION(11, 0, 9):
678 navi10_set_ppt_funcs(smu);
679 break;
680 case IP_VERSION(11, 0, 7):
681 case IP_VERSION(11, 0, 11):
682 case IP_VERSION(11, 0, 12):
683 case IP_VERSION(11, 0, 13):
684 sienna_cichlid_set_ppt_funcs(smu);
685 break;
686 case IP_VERSION(12, 0, 0):
687 case IP_VERSION(12, 0, 1):
688 renoir_set_ppt_funcs(smu);
689 break;
690 case IP_VERSION(11, 5, 0):
691 vangogh_set_ppt_funcs(smu);
692 break;
693 case IP_VERSION(13, 0, 1):
694 case IP_VERSION(13, 0, 3):
695 case IP_VERSION(13, 0, 8):
696 yellow_carp_set_ppt_funcs(smu);
697 break;
698 case IP_VERSION(13, 0, 4):
699 case IP_VERSION(13, 0, 11):
700 smu_v13_0_4_set_ppt_funcs(smu);
701 break;
702 case IP_VERSION(13, 0, 5):
703 smu_v13_0_5_set_ppt_funcs(smu);
704 break;
705 case IP_VERSION(11, 0, 8):
706 cyan_skillfish_set_ppt_funcs(smu);
707 break;
708 case IP_VERSION(11, 0, 2):
709 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
710 arcturus_set_ppt_funcs(smu);
711 /* OD is not supported on Arcturus */
712 smu->od_enabled = false;
713 break;
714 case IP_VERSION(13, 0, 2):
715 aldebaran_set_ppt_funcs(smu);
716 /* Enable pp_od_clk_voltage node */
717 smu->od_enabled = true;
718 break;
719 case IP_VERSION(13, 0, 0):
720 case IP_VERSION(13, 0, 10):
721 smu_v13_0_0_set_ppt_funcs(smu);
722 break;
723 case IP_VERSION(13, 0, 6):
724 case IP_VERSION(13, 0, 14):
725 smu_v13_0_6_set_ppt_funcs(smu);
726 /* Enable pp_od_clk_voltage node */
727 smu->od_enabled = true;
728 break;
729 case IP_VERSION(13, 0, 7):
730 smu_v13_0_7_set_ppt_funcs(smu);
731 break;
732 case IP_VERSION(14, 0, 0):
733 case IP_VERSION(14, 0, 1):
734 case IP_VERSION(14, 0, 4):
735 smu_v14_0_0_set_ppt_funcs(smu);
736 break;
737 case IP_VERSION(14, 0, 2):
738 case IP_VERSION(14, 0, 3):
739 smu_v14_0_2_set_ppt_funcs(smu);
740 break;
741 default:
742 return -EINVAL;
743 }
744
745 return 0;
746 }
747
smu_early_init(void * handle)748 static int smu_early_init(void *handle)
749 {
750 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
751 struct smu_context *smu;
752 int r;
753
754 smu = kzalloc(sizeof(struct smu_context), GFP_KERNEL);
755 if (!smu)
756 return -ENOMEM;
757
758 smu->adev = adev;
759 smu->pm_enabled = !!amdgpu_dpm;
760 smu->is_apu = false;
761 smu->smu_baco.state = SMU_BACO_STATE_NONE;
762 smu->smu_baco.platform_support = false;
763 smu->smu_baco.maco_support = false;
764 smu->user_dpm_profile.fan_mode = -1;
765
766 rw_init(&smu->message_lock, "smuml");
767
768 adev->powerplay.pp_handle = smu;
769 adev->powerplay.pp_funcs = &swsmu_pm_funcs;
770
771 r = smu_set_funcs(adev);
772 if (r)
773 return r;
774 return smu_init_microcode(smu);
775 }
776
smu_set_default_dpm_table(struct smu_context * smu)777 static int smu_set_default_dpm_table(struct smu_context *smu)
778 {
779 struct amdgpu_device *adev = smu->adev;
780 struct smu_power_context *smu_power = &smu->smu_power;
781 struct smu_power_gate *power_gate = &smu_power->power_gate;
782 int vcn_gate, jpeg_gate;
783 int ret = 0;
784
785 if (!smu->ppt_funcs->set_default_dpm_table)
786 return 0;
787
788 if (adev->pg_flags & AMD_PG_SUPPORT_VCN)
789 vcn_gate = atomic_read(&power_gate->vcn_gated);
790 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)
791 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
792
793 if (adev->pg_flags & AMD_PG_SUPPORT_VCN) {
794 ret = smu_dpm_set_vcn_enable(smu, true);
795 if (ret)
796 return ret;
797 }
798
799 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG) {
800 ret = smu_dpm_set_jpeg_enable(smu, true);
801 if (ret)
802 goto err_out;
803 }
804
805 ret = smu->ppt_funcs->set_default_dpm_table(smu);
806 if (ret)
807 dev_err(smu->adev->dev,
808 "Failed to setup default dpm clock tables!\n");
809
810 if (adev->pg_flags & AMD_PG_SUPPORT_JPEG)
811 smu_dpm_set_jpeg_enable(smu, !jpeg_gate);
812 err_out:
813 if (adev->pg_flags & AMD_PG_SUPPORT_VCN)
814 smu_dpm_set_vcn_enable(smu, !vcn_gate);
815
816 return ret;
817 }
818
smu_apply_default_config_table_settings(struct smu_context * smu)819 static int smu_apply_default_config_table_settings(struct smu_context *smu)
820 {
821 struct amdgpu_device *adev = smu->adev;
822 int ret = 0;
823
824 ret = smu_get_default_config_table_settings(smu,
825 &adev->pm.config_table);
826 if (ret)
827 return ret;
828
829 return smu_set_config_table(smu, &adev->pm.config_table);
830 }
831
smu_late_init(void * handle)832 static int smu_late_init(void *handle)
833 {
834 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
835 struct smu_context *smu = adev->powerplay.pp_handle;
836 int ret = 0;
837
838 smu_set_fine_grain_gfx_freq_parameters(smu);
839
840 if (!smu->pm_enabled)
841 return 0;
842
843 ret = smu_post_init(smu);
844 if (ret) {
845 dev_err(adev->dev, "Failed to post smu init!\n");
846 return ret;
847 }
848
849 /*
850 * Explicitly notify PMFW the power mode the system in. Since
851 * the PMFW may boot the ASIC with a different mode.
852 * For those supporting ACDC switch via gpio, PMFW will
853 * handle the switch automatically. Driver involvement
854 * is unnecessary.
855 */
856 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
857 smu_set_ac_dc(smu);
858
859 if ((amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 1)) ||
860 (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 3)))
861 return 0;
862
863 if (!amdgpu_sriov_vf(adev) || smu->od_enabled) {
864 ret = smu_set_default_od_settings(smu);
865 if (ret) {
866 dev_err(adev->dev, "Failed to setup default OD settings!\n");
867 return ret;
868 }
869 }
870
871 ret = smu_populate_umd_state_clk(smu);
872 if (ret) {
873 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
874 return ret;
875 }
876
877 ret = smu_get_asic_power_limits(smu,
878 &smu->current_power_limit,
879 &smu->default_power_limit,
880 &smu->max_power_limit,
881 &smu->min_power_limit);
882 if (ret) {
883 dev_err(adev->dev, "Failed to get asic power limits!\n");
884 return ret;
885 }
886
887 if (!amdgpu_sriov_vf(adev))
888 smu_get_unique_id(smu);
889
890 smu_get_fan_parameters(smu);
891
892 smu_handle_task(smu,
893 smu->smu_dpm.dpm_level,
894 AMD_PP_TASK_COMPLETE_INIT);
895
896 ret = smu_apply_default_config_table_settings(smu);
897 if (ret && (ret != -EOPNOTSUPP)) {
898 dev_err(adev->dev, "Failed to apply default DriverSmuConfig settings!\n");
899 return ret;
900 }
901
902 smu_restore_dpm_user_profile(smu);
903
904 return 0;
905 }
906
smu_init_fb_allocations(struct smu_context * smu)907 static int smu_init_fb_allocations(struct smu_context *smu)
908 {
909 struct amdgpu_device *adev = smu->adev;
910 struct smu_table_context *smu_table = &smu->smu_table;
911 struct smu_table *tables = smu_table->tables;
912 struct smu_table *driver_table = &(smu_table->driver_table);
913 uint32_t max_table_size = 0;
914 int ret, i;
915
916 /* VRAM allocation for tool table */
917 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
918 ret = amdgpu_bo_create_kernel(adev,
919 tables[SMU_TABLE_PMSTATUSLOG].size,
920 tables[SMU_TABLE_PMSTATUSLOG].align,
921 tables[SMU_TABLE_PMSTATUSLOG].domain,
922 &tables[SMU_TABLE_PMSTATUSLOG].bo,
923 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
924 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
925 if (ret) {
926 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
927 return ret;
928 }
929 }
930
931 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT;
932 /* VRAM allocation for driver table */
933 for (i = 0; i < SMU_TABLE_COUNT; i++) {
934 if (tables[i].size == 0)
935 continue;
936
937 /* If one of the tables has VRAM domain restriction, keep it in
938 * VRAM
939 */
940 if ((tables[i].domain &
941 (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) ==
942 AMDGPU_GEM_DOMAIN_VRAM)
943 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
944
945 if (i == SMU_TABLE_PMSTATUSLOG)
946 continue;
947
948 if (max_table_size < tables[i].size)
949 max_table_size = tables[i].size;
950 }
951
952 driver_table->size = max_table_size;
953 driver_table->align = PAGE_SIZE;
954
955 ret = amdgpu_bo_create_kernel(adev,
956 driver_table->size,
957 driver_table->align,
958 driver_table->domain,
959 &driver_table->bo,
960 &driver_table->mc_address,
961 &driver_table->cpu_addr);
962 if (ret) {
963 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
964 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
965 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
966 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
967 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
968 }
969
970 return ret;
971 }
972
smu_fini_fb_allocations(struct smu_context * smu)973 static int smu_fini_fb_allocations(struct smu_context *smu)
974 {
975 struct smu_table_context *smu_table = &smu->smu_table;
976 struct smu_table *tables = smu_table->tables;
977 struct smu_table *driver_table = &(smu_table->driver_table);
978
979 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
980 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
981 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
982 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
983
984 amdgpu_bo_free_kernel(&driver_table->bo,
985 &driver_table->mc_address,
986 &driver_table->cpu_addr);
987
988 return 0;
989 }
990
991 /**
992 * smu_alloc_memory_pool - allocate memory pool in the system memory
993 *
994 * @smu: amdgpu_device pointer
995 *
996 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
997 * and DramLogSetDramAddr can notify it changed.
998 *
999 * Returns 0 on success, error on failure.
1000 */
smu_alloc_memory_pool(struct smu_context * smu)1001 static int smu_alloc_memory_pool(struct smu_context *smu)
1002 {
1003 struct amdgpu_device *adev = smu->adev;
1004 struct smu_table_context *smu_table = &smu->smu_table;
1005 struct smu_table *memory_pool = &smu_table->memory_pool;
1006 uint64_t pool_size = smu->pool_size;
1007 int ret = 0;
1008
1009 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1010 return ret;
1011
1012 memory_pool->size = pool_size;
1013 memory_pool->align = PAGE_SIZE;
1014 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1015
1016 switch (pool_size) {
1017 case SMU_MEMORY_POOL_SIZE_256_MB:
1018 case SMU_MEMORY_POOL_SIZE_512_MB:
1019 case SMU_MEMORY_POOL_SIZE_1_GB:
1020 case SMU_MEMORY_POOL_SIZE_2_GB:
1021 ret = amdgpu_bo_create_kernel(adev,
1022 memory_pool->size,
1023 memory_pool->align,
1024 memory_pool->domain,
1025 &memory_pool->bo,
1026 &memory_pool->mc_address,
1027 &memory_pool->cpu_addr);
1028 if (ret)
1029 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
1030 break;
1031 default:
1032 break;
1033 }
1034
1035 return ret;
1036 }
1037
smu_free_memory_pool(struct smu_context * smu)1038 static int smu_free_memory_pool(struct smu_context *smu)
1039 {
1040 struct smu_table_context *smu_table = &smu->smu_table;
1041 struct smu_table *memory_pool = &smu_table->memory_pool;
1042
1043 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1044 return 0;
1045
1046 amdgpu_bo_free_kernel(&memory_pool->bo,
1047 &memory_pool->mc_address,
1048 &memory_pool->cpu_addr);
1049
1050 memset(memory_pool, 0, sizeof(struct smu_table));
1051
1052 return 0;
1053 }
1054
smu_alloc_dummy_read_table(struct smu_context * smu)1055 static int smu_alloc_dummy_read_table(struct smu_context *smu)
1056 {
1057 struct smu_table_context *smu_table = &smu->smu_table;
1058 struct smu_table *dummy_read_1_table =
1059 &smu_table->dummy_read_1_table;
1060 struct amdgpu_device *adev = smu->adev;
1061 int ret = 0;
1062
1063 if (!dummy_read_1_table->size)
1064 return 0;
1065
1066 ret = amdgpu_bo_create_kernel(adev,
1067 dummy_read_1_table->size,
1068 dummy_read_1_table->align,
1069 dummy_read_1_table->domain,
1070 &dummy_read_1_table->bo,
1071 &dummy_read_1_table->mc_address,
1072 &dummy_read_1_table->cpu_addr);
1073 if (ret)
1074 dev_err(adev->dev, "VRAM allocation for dummy read table failed!\n");
1075
1076 return ret;
1077 }
1078
smu_free_dummy_read_table(struct smu_context * smu)1079 static void smu_free_dummy_read_table(struct smu_context *smu)
1080 {
1081 struct smu_table_context *smu_table = &smu->smu_table;
1082 struct smu_table *dummy_read_1_table =
1083 &smu_table->dummy_read_1_table;
1084
1085
1086 amdgpu_bo_free_kernel(&dummy_read_1_table->bo,
1087 &dummy_read_1_table->mc_address,
1088 &dummy_read_1_table->cpu_addr);
1089
1090 memset(dummy_read_1_table, 0, sizeof(struct smu_table));
1091 }
1092
smu_smc_table_sw_init(struct smu_context * smu)1093 static int smu_smc_table_sw_init(struct smu_context *smu)
1094 {
1095 int ret;
1096
1097 /**
1098 * Create smu_table structure, and init smc tables such as
1099 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
1100 */
1101 ret = smu_init_smc_tables(smu);
1102 if (ret) {
1103 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
1104 return ret;
1105 }
1106
1107 /**
1108 * Create smu_power_context structure, and allocate smu_dpm_context and
1109 * context size to fill the smu_power_context data.
1110 */
1111 ret = smu_init_power(smu);
1112 if (ret) {
1113 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
1114 return ret;
1115 }
1116
1117 /*
1118 * allocate vram bos to store smc table contents.
1119 */
1120 ret = smu_init_fb_allocations(smu);
1121 if (ret)
1122 return ret;
1123
1124 ret = smu_alloc_memory_pool(smu);
1125 if (ret)
1126 return ret;
1127
1128 ret = smu_alloc_dummy_read_table(smu);
1129 if (ret)
1130 return ret;
1131
1132 ret = smu_i2c_init(smu);
1133 if (ret)
1134 return ret;
1135
1136 return 0;
1137 }
1138
smu_smc_table_sw_fini(struct smu_context * smu)1139 static int smu_smc_table_sw_fini(struct smu_context *smu)
1140 {
1141 int ret;
1142
1143 smu_i2c_fini(smu);
1144
1145 smu_free_dummy_read_table(smu);
1146
1147 ret = smu_free_memory_pool(smu);
1148 if (ret)
1149 return ret;
1150
1151 ret = smu_fini_fb_allocations(smu);
1152 if (ret)
1153 return ret;
1154
1155 ret = smu_fini_power(smu);
1156 if (ret) {
1157 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
1158 return ret;
1159 }
1160
1161 ret = smu_fini_smc_tables(smu);
1162 if (ret) {
1163 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
1164 return ret;
1165 }
1166
1167 return 0;
1168 }
1169
smu_throttling_logging_work_fn(struct work_struct * work)1170 static void smu_throttling_logging_work_fn(struct work_struct *work)
1171 {
1172 struct smu_context *smu = container_of(work, struct smu_context,
1173 throttling_logging_work);
1174
1175 smu_log_thermal_throttling(smu);
1176 }
1177
smu_interrupt_work_fn(struct work_struct * work)1178 static void smu_interrupt_work_fn(struct work_struct *work)
1179 {
1180 struct smu_context *smu = container_of(work, struct smu_context,
1181 interrupt_work);
1182
1183 if (smu->ppt_funcs && smu->ppt_funcs->interrupt_work)
1184 smu->ppt_funcs->interrupt_work(smu);
1185 }
1186
smu_swctf_delayed_work_handler(struct work_struct * work)1187 static void smu_swctf_delayed_work_handler(struct work_struct *work)
1188 {
1189 struct smu_context *smu =
1190 container_of(work, struct smu_context, swctf_delayed_work.work);
1191 struct smu_temperature_range *range =
1192 &smu->thermal_range;
1193 struct amdgpu_device *adev = smu->adev;
1194 uint32_t hotspot_tmp, size;
1195
1196 /*
1197 * If the hotspot temperature is confirmed as below SW CTF setting point
1198 * after the delay enforced, nothing will be done.
1199 * Otherwise, a graceful shutdown will be performed to prevent further damage.
1200 */
1201 if (range->software_shutdown_temp &&
1202 smu->ppt_funcs->read_sensor &&
1203 !smu->ppt_funcs->read_sensor(smu,
1204 AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1205 &hotspot_tmp,
1206 &size) &&
1207 hotspot_tmp / 1000 < range->software_shutdown_temp)
1208 return;
1209
1210 dev_emerg(adev->dev, "ERROR: GPU over temperature range(SW CTF) detected!\n");
1211 dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU SW CTF!\n");
1212 orderly_poweroff(true);
1213 }
1214
smu_init_xgmi_plpd_mode(struct smu_context * smu)1215 static void smu_init_xgmi_plpd_mode(struct smu_context *smu)
1216 {
1217 struct smu_dpm_context *dpm_ctxt = &(smu->smu_dpm);
1218 struct smu_dpm_policy_ctxt *policy_ctxt;
1219 struct smu_dpm_policy *policy;
1220
1221 policy = smu_get_pm_policy(smu, PP_PM_POLICY_XGMI_PLPD);
1222 if (amdgpu_ip_version(smu->adev, MP1_HWIP, 0) == IP_VERSION(11, 0, 2)) {
1223 if (policy)
1224 policy->current_level = XGMI_PLPD_DEFAULT;
1225 return;
1226 }
1227
1228 /* PMFW put PLPD into default policy after enabling the feature */
1229 if (smu_feature_is_enabled(smu,
1230 SMU_FEATURE_XGMI_PER_LINK_PWR_DWN_BIT)) {
1231 if (policy)
1232 policy->current_level = XGMI_PLPD_DEFAULT;
1233 } else {
1234 policy_ctxt = dpm_ctxt->dpm_policies;
1235 if (policy_ctxt)
1236 policy_ctxt->policy_mask &=
1237 ~BIT(PP_PM_POLICY_XGMI_PLPD);
1238 }
1239 }
1240
smu_is_workload_profile_available(struct smu_context * smu,u32 profile)1241 static bool smu_is_workload_profile_available(struct smu_context *smu,
1242 u32 profile)
1243 {
1244 if (profile >= PP_SMC_POWER_PROFILE_COUNT)
1245 return false;
1246 return smu->workload_map && smu->workload_map[profile].valid_mapping;
1247 }
1248
smu_sw_init(void * handle)1249 static int smu_sw_init(void *handle)
1250 {
1251 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1252 struct smu_context *smu = adev->powerplay.pp_handle;
1253 int ret;
1254
1255 smu->pool_size = adev->pm.smu_prv_buffer_size;
1256 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
1257 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
1258 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
1259
1260 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
1261 INIT_WORK(&smu->interrupt_work, smu_interrupt_work_fn);
1262 atomic64_set(&smu->throttle_int_counter, 0);
1263 smu->watermarks_bitmap = 0;
1264
1265 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
1266 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
1267 atomic_set(&smu->smu_power.power_gate.vpe_gated, 1);
1268 atomic_set(&smu->smu_power.power_gate.umsch_mm_gated, 1);
1269
1270 if (smu->is_apu ||
1271 !smu_is_workload_profile_available(smu, PP_SMC_POWER_PROFILE_FULLSCREEN3D))
1272 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
1273 else
1274 smu->power_profile_mode = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
1275 smu_power_profile_mode_get(smu, smu->power_profile_mode);
1276
1277 smu->display_config = &adev->pm.pm_display_cfg;
1278
1279 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1280 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
1281
1282 INIT_DELAYED_WORK(&smu->swctf_delayed_work,
1283 smu_swctf_delayed_work_handler);
1284
1285 ret = smu_smc_table_sw_init(smu);
1286 if (ret) {
1287 dev_err(adev->dev, "Failed to sw init smc table!\n");
1288 return ret;
1289 }
1290
1291 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1292 ret = smu_get_vbios_bootup_values(smu);
1293 if (ret) {
1294 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1295 return ret;
1296 }
1297
1298 ret = smu_init_pptable_microcode(smu);
1299 if (ret) {
1300 dev_err(adev->dev, "Failed to setup pptable firmware!\n");
1301 return ret;
1302 }
1303
1304 ret = smu_register_irq_handler(smu);
1305 if (ret) {
1306 dev_err(adev->dev, "Failed to register smc irq handler!\n");
1307 return ret;
1308 }
1309
1310 /* If there is no way to query fan control mode, fan control is not supported */
1311 if (!smu->ppt_funcs->get_fan_control_mode)
1312 smu->adev->pm.no_fan = true;
1313
1314 return 0;
1315 }
1316
smu_sw_fini(void * handle)1317 static int smu_sw_fini(void *handle)
1318 {
1319 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1320 struct smu_context *smu = adev->powerplay.pp_handle;
1321 int ret;
1322
1323 ret = smu_smc_table_sw_fini(smu);
1324 if (ret) {
1325 dev_err(adev->dev, "Failed to sw fini smc table!\n");
1326 return ret;
1327 }
1328
1329 if (smu->custom_profile_params) {
1330 kfree(smu->custom_profile_params);
1331 smu->custom_profile_params = NULL;
1332 }
1333
1334 smu_fini_microcode(smu);
1335
1336 return 0;
1337 }
1338
smu_get_thermal_temperature_range(struct smu_context * smu)1339 static int smu_get_thermal_temperature_range(struct smu_context *smu)
1340 {
1341 struct amdgpu_device *adev = smu->adev;
1342 struct smu_temperature_range *range =
1343 &smu->thermal_range;
1344 int ret = 0;
1345
1346 if (!smu->ppt_funcs->get_thermal_temperature_range)
1347 return 0;
1348
1349 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
1350 if (ret)
1351 return ret;
1352
1353 adev->pm.dpm.thermal.min_temp = range->min;
1354 adev->pm.dpm.thermal.max_temp = range->max;
1355 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
1356 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
1357 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
1358 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
1359 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
1360 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
1361 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
1362
1363 return ret;
1364 }
1365
1366 /**
1367 * smu_wbrf_handle_exclusion_ranges - consume the wbrf exclusion ranges
1368 *
1369 * @smu: smu_context pointer
1370 *
1371 * Retrieve the wbrf exclusion ranges and send them to PMFW for proper handling.
1372 * Returns 0 on success, error on failure.
1373 */
smu_wbrf_handle_exclusion_ranges(struct smu_context * smu)1374 static int smu_wbrf_handle_exclusion_ranges(struct smu_context *smu)
1375 {
1376 STUB();
1377 return 0;
1378 #ifdef notyet
1379 struct wbrf_ranges_in_out wbrf_exclusion = {0};
1380 struct freq_band_range *wifi_bands = wbrf_exclusion.band_list;
1381 struct amdgpu_device *adev = smu->adev;
1382 uint32_t num_of_wbrf_ranges = MAX_NUM_OF_WBRF_RANGES;
1383 uint64_t start, end;
1384 int ret, i, j;
1385
1386 ret = amd_wbrf_retrieve_freq_band(adev->dev, &wbrf_exclusion);
1387 if (ret) {
1388 dev_err(adev->dev, "Failed to retrieve exclusion ranges!\n");
1389 return ret;
1390 }
1391
1392 /*
1393 * The exclusion ranges array we got might be filled with holes and duplicate
1394 * entries. For example:
1395 * {(2400, 2500), (0, 0), (6882, 6962), (2400, 2500), (0, 0), (6117, 6189), (0, 0)...}
1396 * We need to do some sortups to eliminate those holes and duplicate entries.
1397 * Expected output: {(2400, 2500), (6117, 6189), (6882, 6962), (0, 0)...}
1398 */
1399 for (i = 0; i < num_of_wbrf_ranges; i++) {
1400 start = wifi_bands[i].start;
1401 end = wifi_bands[i].end;
1402
1403 /* get the last valid entry to fill the intermediate hole */
1404 if (!start && !end) {
1405 for (j = num_of_wbrf_ranges - 1; j > i; j--)
1406 if (wifi_bands[j].start && wifi_bands[j].end)
1407 break;
1408
1409 /* no valid entry left */
1410 if (j <= i)
1411 break;
1412
1413 start = wifi_bands[i].start = wifi_bands[j].start;
1414 end = wifi_bands[i].end = wifi_bands[j].end;
1415 wifi_bands[j].start = 0;
1416 wifi_bands[j].end = 0;
1417 num_of_wbrf_ranges = j;
1418 }
1419
1420 /* eliminate duplicate entries */
1421 for (j = i + 1; j < num_of_wbrf_ranges; j++) {
1422 if ((wifi_bands[j].start == start) && (wifi_bands[j].end == end)) {
1423 wifi_bands[j].start = 0;
1424 wifi_bands[j].end = 0;
1425 }
1426 }
1427 }
1428
1429 /* Send the sorted wifi_bands to PMFW */
1430 ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1431 /* Try to set the wifi_bands again */
1432 if (unlikely(ret == -EBUSY)) {
1433 mdelay(5);
1434 ret = smu_set_wbrf_exclusion_ranges(smu, wifi_bands);
1435 }
1436
1437 return ret;
1438 #endif
1439 }
1440
1441 /**
1442 * smu_wbrf_event_handler - handle notify events
1443 *
1444 * @nb: notifier block
1445 * @action: event type
1446 * @_arg: event data
1447 *
1448 * Calls relevant amdgpu function in response to wbrf event
1449 * notification from kernel.
1450 */
smu_wbrf_event_handler(struct notifier_block * nb,unsigned long action,void * _arg)1451 static int smu_wbrf_event_handler(struct notifier_block *nb,
1452 unsigned long action, void *_arg)
1453 {
1454 STUB();
1455 return NOTIFY_OK;
1456 #ifdef notyet
1457 struct smu_context *smu = container_of(nb, struct smu_context, wbrf_notifier);
1458
1459 switch (action) {
1460 case WBRF_CHANGED:
1461 schedule_delayed_work(&smu->wbrf_delayed_work,
1462 msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1463 break;
1464 default:
1465 return NOTIFY_DONE;
1466 }
1467
1468 return NOTIFY_OK;
1469 #endif
1470 }
1471
1472 /**
1473 * smu_wbrf_delayed_work_handler - callback on delayed work timer expired
1474 *
1475 * @work: struct work_struct pointer
1476 *
1477 * Flood is over and driver will consume the latest exclusion ranges.
1478 */
smu_wbrf_delayed_work_handler(struct work_struct * work)1479 static void smu_wbrf_delayed_work_handler(struct work_struct *work)
1480 {
1481 struct smu_context *smu = container_of(work, struct smu_context, wbrf_delayed_work.work);
1482
1483 smu_wbrf_handle_exclusion_ranges(smu);
1484 }
1485
1486 /**
1487 * smu_wbrf_support_check - check wbrf support
1488 *
1489 * @smu: smu_context pointer
1490 *
1491 * Verifies the ACPI interface whether wbrf is supported.
1492 */
smu_wbrf_support_check(struct smu_context * smu)1493 static void smu_wbrf_support_check(struct smu_context *smu)
1494 {
1495 struct amdgpu_device *adev = smu->adev;
1496
1497 #ifdef notyet
1498 smu->wbrf_supported = smu_is_asic_wbrf_supported(smu) && amdgpu_wbrf &&
1499 acpi_amd_wbrf_supported_consumer(adev->dev);
1500 #else
1501 smu->wbrf_supported = false;
1502 #endif
1503
1504 if (smu->wbrf_supported)
1505 dev_info(adev->dev, "RF interference mitigation is supported\n");
1506 }
1507
1508 /**
1509 * smu_wbrf_init - init driver wbrf support
1510 *
1511 * @smu: smu_context pointer
1512 *
1513 * Verifies the AMD ACPI interfaces and registers with the wbrf
1514 * notifier chain if wbrf feature is supported.
1515 * Returns 0 on success, error on failure.
1516 */
smu_wbrf_init(struct smu_context * smu)1517 static int smu_wbrf_init(struct smu_context *smu)
1518 {
1519 int ret;
1520
1521 if (!smu->wbrf_supported)
1522 return 0;
1523 STUB();
1524 return 0;
1525 #ifdef notyet
1526
1527 INIT_DELAYED_WORK(&smu->wbrf_delayed_work, smu_wbrf_delayed_work_handler);
1528
1529 smu->wbrf_notifier.notifier_call = smu_wbrf_event_handler;
1530 ret = amd_wbrf_register_notifier(&smu->wbrf_notifier);
1531 if (ret)
1532 return ret;
1533
1534 /*
1535 * Some wifiband exclusion ranges may be already there
1536 * before our driver loaded. To make sure our driver
1537 * is awared of those exclusion ranges.
1538 */
1539 schedule_delayed_work(&smu->wbrf_delayed_work,
1540 msecs_to_jiffies(SMU_WBRF_EVENT_HANDLING_PACE));
1541
1542 return 0;
1543 #endif
1544 }
1545
1546 /**
1547 * smu_wbrf_fini - tear down driver wbrf support
1548 *
1549 * @smu: smu_context pointer
1550 *
1551 * Unregisters with the wbrf notifier chain.
1552 */
smu_wbrf_fini(struct smu_context * smu)1553 static void smu_wbrf_fini(struct smu_context *smu)
1554 {
1555 if (!smu->wbrf_supported)
1556 return;
1557
1558 #ifdef notyet
1559 amd_wbrf_unregister_notifier(&smu->wbrf_notifier);
1560 #endif
1561
1562 cancel_delayed_work_sync(&smu->wbrf_delayed_work);
1563 }
1564
smu_smc_hw_setup(struct smu_context * smu)1565 static int smu_smc_hw_setup(struct smu_context *smu)
1566 {
1567 struct smu_feature *feature = &smu->smu_feature;
1568 struct amdgpu_device *adev = smu->adev;
1569 uint8_t pcie_gen = 0, pcie_width = 0;
1570 uint64_t features_supported;
1571 int ret = 0;
1572
1573 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1574 case IP_VERSION(11, 0, 7):
1575 case IP_VERSION(11, 0, 11):
1576 case IP_VERSION(11, 5, 0):
1577 case IP_VERSION(11, 0, 12):
1578 if (adev->in_suspend && smu_is_dpm_running(smu)) {
1579 dev_info(adev->dev, "dpm has been enabled\n");
1580 ret = smu_system_features_control(smu, true);
1581 if (ret)
1582 dev_err(adev->dev, "Failed system features control!\n");
1583 return ret;
1584 }
1585 break;
1586 default:
1587 break;
1588 }
1589
1590 ret = smu_init_display_count(smu, 0);
1591 if (ret) {
1592 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
1593 return ret;
1594 }
1595
1596 ret = smu_set_driver_table_location(smu);
1597 if (ret) {
1598 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
1599 return ret;
1600 }
1601
1602 /*
1603 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1604 */
1605 ret = smu_set_tool_table_location(smu);
1606 if (ret) {
1607 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
1608 return ret;
1609 }
1610
1611 /*
1612 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1613 * pool location.
1614 */
1615 ret = smu_notify_memory_pool_location(smu);
1616 if (ret) {
1617 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
1618 return ret;
1619 }
1620
1621 /*
1622 * It is assumed the pptable used before runpm is same as
1623 * the one used afterwards. Thus, we can reuse the stored
1624 * copy and do not need to resetup the pptable again.
1625 */
1626 if (!adev->in_runpm) {
1627 ret = smu_setup_pptable(smu);
1628 if (ret) {
1629 dev_err(adev->dev, "Failed to setup pptable!\n");
1630 return ret;
1631 }
1632 }
1633
1634 /* smu_dump_pptable(smu); */
1635
1636 /*
1637 * With SCPM enabled, PSP is responsible for the PPTable transferring
1638 * (to SMU). Driver involvement is not needed and permitted.
1639 */
1640 if (!adev->scpm_enabled) {
1641 /*
1642 * Copy pptable bo in the vram to smc with SMU MSGs such as
1643 * SetDriverDramAddr and TransferTableDram2Smu.
1644 */
1645 ret = smu_write_pptable(smu);
1646 if (ret) {
1647 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
1648 return ret;
1649 }
1650 }
1651
1652 /* issue Run*Btc msg */
1653 ret = smu_run_btc(smu);
1654 if (ret)
1655 return ret;
1656
1657 /* Enable UclkShadow on wbrf supported */
1658 if (smu->wbrf_supported) {
1659 ret = smu_enable_uclk_shadow(smu, true);
1660 if (ret) {
1661 dev_err(adev->dev, "Failed to enable UclkShadow feature to support wbrf!\n");
1662 return ret;
1663 }
1664 }
1665
1666 /*
1667 * With SCPM enabled, these actions(and relevant messages) are
1668 * not needed and permitted.
1669 */
1670 if (!adev->scpm_enabled) {
1671 ret = smu_feature_set_allowed_mask(smu);
1672 if (ret) {
1673 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
1674 return ret;
1675 }
1676 }
1677
1678 ret = smu_system_features_control(smu, true);
1679 if (ret) {
1680 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
1681 return ret;
1682 }
1683
1684 smu_init_xgmi_plpd_mode(smu);
1685
1686 ret = smu_feature_get_enabled_mask(smu, &features_supported);
1687 if (ret) {
1688 dev_err(adev->dev, "Failed to retrieve supported dpm features!\n");
1689 return ret;
1690 }
1691 bitmap_copy(feature->supported,
1692 (unsigned long *)&features_supported,
1693 feature->feature_num);
1694
1695 if (!smu_is_dpm_running(smu))
1696 dev_info(adev->dev, "dpm has been disabled\n");
1697
1698 /*
1699 * Set initialized values (get from vbios) to dpm tables context such as
1700 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1701 * type of clks.
1702 */
1703 ret = smu_set_default_dpm_table(smu);
1704 if (ret) {
1705 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
1706 return ret;
1707 }
1708
1709 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5)
1710 pcie_gen = 4;
1711 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
1712 pcie_gen = 3;
1713 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
1714 pcie_gen = 2;
1715 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
1716 pcie_gen = 1;
1717 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
1718 pcie_gen = 0;
1719
1720 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
1721 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
1722 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
1723 */
1724 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X32)
1725 pcie_width = 7;
1726 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1727 pcie_width = 6;
1728 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1729 pcie_width = 5;
1730 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1731 pcie_width = 4;
1732 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1733 pcie_width = 3;
1734 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1735 pcie_width = 2;
1736 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1737 pcie_width = 1;
1738 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
1739 if (ret) {
1740 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
1741 return ret;
1742 }
1743
1744 ret = smu_get_thermal_temperature_range(smu);
1745 if (ret) {
1746 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
1747 return ret;
1748 }
1749
1750 ret = smu_enable_thermal_alert(smu);
1751 if (ret) {
1752 dev_err(adev->dev, "Failed to enable thermal alert!\n");
1753 return ret;
1754 }
1755
1756 ret = smu_notify_display_change(smu);
1757 if (ret) {
1758 dev_err(adev->dev, "Failed to notify display change!\n");
1759 return ret;
1760 }
1761
1762 /*
1763 * Set min deep sleep dce fclk with bootup value from vbios via
1764 * SetMinDeepSleepDcefclk MSG.
1765 */
1766 ret = smu_set_min_dcef_deep_sleep(smu,
1767 smu->smu_table.boot_values.dcefclk / 100);
1768 if (ret) {
1769 dev_err(adev->dev, "Error setting min deepsleep dcefclk\n");
1770 return ret;
1771 }
1772
1773 /* Init wbrf support. Properly setup the notifier */
1774 ret = smu_wbrf_init(smu);
1775 if (ret)
1776 dev_err(adev->dev, "Error during wbrf init call\n");
1777
1778 return ret;
1779 }
1780
smu_start_smc_engine(struct smu_context * smu)1781 static int smu_start_smc_engine(struct smu_context *smu)
1782 {
1783 struct amdgpu_device *adev = smu->adev;
1784 int ret = 0;
1785
1786 smu->smc_fw_state = SMU_FW_INIT;
1787
1788 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1789 if (amdgpu_ip_version(adev, MP1_HWIP, 0) < IP_VERSION(11, 0, 0)) {
1790 if (smu->ppt_funcs->load_microcode) {
1791 ret = smu->ppt_funcs->load_microcode(smu);
1792 if (ret)
1793 return ret;
1794 }
1795 }
1796 }
1797
1798 if (smu->ppt_funcs->check_fw_status) {
1799 ret = smu->ppt_funcs->check_fw_status(smu);
1800 if (ret) {
1801 dev_err(adev->dev, "SMC is not ready\n");
1802 return ret;
1803 }
1804 }
1805
1806 /*
1807 * Send msg GetDriverIfVersion to check if the return value is equal
1808 * with DRIVER_IF_VERSION of smc header.
1809 */
1810 ret = smu_check_fw_version(smu);
1811 if (ret)
1812 return ret;
1813
1814 return ret;
1815 }
1816
smu_hw_init(void * handle)1817 static int smu_hw_init(void *handle)
1818 {
1819 int ret;
1820 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1821 struct smu_context *smu = adev->powerplay.pp_handle;
1822
1823 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1824 smu->pm_enabled = false;
1825 return 0;
1826 }
1827
1828 ret = smu_start_smc_engine(smu);
1829 if (ret) {
1830 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1831 return ret;
1832 }
1833
1834 /*
1835 * Check whether wbrf is supported. This needs to be done
1836 * before SMU setup starts since part of SMU configuration
1837 * relies on this.
1838 */
1839 smu_wbrf_support_check(smu);
1840
1841 if (smu->is_apu) {
1842 ret = smu_set_gfx_imu_enable(smu);
1843 if (ret)
1844 return ret;
1845 smu_dpm_set_vcn_enable(smu, true);
1846 smu_dpm_set_jpeg_enable(smu, true);
1847 smu_dpm_set_vpe_enable(smu, true);
1848 smu_dpm_set_umsch_mm_enable(smu, true);
1849 smu_set_mall_enable(smu);
1850 smu_set_gfx_cgpg(smu, true);
1851 }
1852
1853 if (!smu->pm_enabled)
1854 return 0;
1855
1856 ret = smu_get_driver_allowed_feature_mask(smu);
1857 if (ret)
1858 return ret;
1859
1860 ret = smu_smc_hw_setup(smu);
1861 if (ret) {
1862 dev_err(adev->dev, "Failed to setup smc hw!\n");
1863 return ret;
1864 }
1865
1866 /*
1867 * Move maximum sustainable clock retrieving here considering
1868 * 1. It is not needed on resume(from S3).
1869 * 2. DAL settings come between .hw_init and .late_init of SMU.
1870 * And DAL needs to know the maximum sustainable clocks. Thus
1871 * it cannot be put in .late_init().
1872 */
1873 ret = smu_init_max_sustainable_clocks(smu);
1874 if (ret) {
1875 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1876 return ret;
1877 }
1878
1879 adev->pm.dpm_enabled = true;
1880
1881 dev_info(adev->dev, "SMU is initialized successfully!\n");
1882
1883 return 0;
1884 }
1885
smu_disable_dpms(struct smu_context * smu)1886 static int smu_disable_dpms(struct smu_context *smu)
1887 {
1888 struct amdgpu_device *adev = smu->adev;
1889 int ret = 0;
1890 bool use_baco = !smu->is_apu &&
1891 ((amdgpu_in_reset(adev) &&
1892 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1893 ((adev->in_runpm || adev->in_s4) && amdgpu_asic_supports_baco(adev)));
1894
1895 /*
1896 * For SMU 13.0.0 and 13.0.7, PMFW will handle the DPM features(disablement or others)
1897 * properly on suspend/reset/unload. Driver involvement may cause some unexpected issues.
1898 */
1899 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1900 case IP_VERSION(13, 0, 0):
1901 case IP_VERSION(13, 0, 7):
1902 case IP_VERSION(13, 0, 10):
1903 case IP_VERSION(14, 0, 2):
1904 case IP_VERSION(14, 0, 3):
1905 return 0;
1906 default:
1907 break;
1908 }
1909
1910 /*
1911 * For custom pptable uploading, skip the DPM features
1912 * disable process on Navi1x ASICs.
1913 * - As the gfx related features are under control of
1914 * RLC on those ASICs. RLC reinitialization will be
1915 * needed to reenable them. That will cost much more
1916 * efforts.
1917 *
1918 * - SMU firmware can handle the DPM reenablement
1919 * properly.
1920 */
1921 if (smu->uploading_custom_pp_table) {
1922 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1923 case IP_VERSION(11, 0, 0):
1924 case IP_VERSION(11, 0, 5):
1925 case IP_VERSION(11, 0, 9):
1926 case IP_VERSION(11, 0, 7):
1927 case IP_VERSION(11, 0, 11):
1928 case IP_VERSION(11, 5, 0):
1929 case IP_VERSION(11, 0, 12):
1930 case IP_VERSION(11, 0, 13):
1931 return 0;
1932 default:
1933 break;
1934 }
1935 }
1936
1937 /*
1938 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1939 * on BACO in. Driver involvement is unnecessary.
1940 */
1941 if (use_baco) {
1942 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
1943 case IP_VERSION(11, 0, 7):
1944 case IP_VERSION(11, 0, 0):
1945 case IP_VERSION(11, 0, 5):
1946 case IP_VERSION(11, 0, 9):
1947 case IP_VERSION(13, 0, 7):
1948 return 0;
1949 default:
1950 break;
1951 }
1952 }
1953
1954 /*
1955 * For GFX11 and subsequent APUs, PMFW will handle the features disablement properly
1956 * for gpu reset and S0i3 cases. Driver involvement is unnecessary.
1957 */
1958 if (IP_VERSION_MAJ(amdgpu_ip_version(adev, GC_HWIP, 0)) >= 11 &&
1959 smu->is_apu && (amdgpu_in_reset(adev) || adev->in_s0ix))
1960 return 0;
1961
1962 /*
1963 * For gpu reset, runpm and hibernation through BACO,
1964 * BACO feature has to be kept enabled.
1965 */
1966 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1967 ret = smu_disable_all_features_with_exception(smu,
1968 SMU_FEATURE_BACO_BIT);
1969 if (ret)
1970 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1971 } else {
1972 /* DisableAllSmuFeatures message is not permitted with SCPM enabled */
1973 if (!adev->scpm_enabled) {
1974 ret = smu_system_features_control(smu, false);
1975 if (ret)
1976 dev_err(adev->dev, "Failed to disable smu features.\n");
1977 }
1978 }
1979
1980 /* Notify SMU RLC is going to be off, stop RLC and SMU interaction.
1981 * otherwise SMU will hang while interacting with RLC if RLC is halted
1982 * this is a WA for Vangogh asic which fix the SMU hang issue.
1983 */
1984 ret = smu_notify_rlc_state(smu, false);
1985 if (ret) {
1986 dev_err(adev->dev, "Fail to notify rlc status!\n");
1987 return ret;
1988 }
1989
1990 if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 2) &&
1991 !((adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs) &&
1992 !amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs->stop)
1993 adev->gfx.rlc.funcs->stop(adev);
1994
1995 return ret;
1996 }
1997
smu_smc_hw_cleanup(struct smu_context * smu)1998 static int smu_smc_hw_cleanup(struct smu_context *smu)
1999 {
2000 struct amdgpu_device *adev = smu->adev;
2001 int ret = 0;
2002
2003 smu_wbrf_fini(smu);
2004
2005 cancel_work_sync(&smu->throttling_logging_work);
2006 cancel_work_sync(&smu->interrupt_work);
2007
2008 ret = smu_disable_thermal_alert(smu);
2009 if (ret) {
2010 dev_err(adev->dev, "Fail to disable thermal alert!\n");
2011 return ret;
2012 }
2013
2014 cancel_delayed_work_sync(&smu->swctf_delayed_work);
2015
2016 ret = smu_disable_dpms(smu);
2017 if (ret) {
2018 dev_err(adev->dev, "Fail to disable dpm features!\n");
2019 return ret;
2020 }
2021
2022 return 0;
2023 }
2024
smu_reset_mp1_state(struct smu_context * smu)2025 static int smu_reset_mp1_state(struct smu_context *smu)
2026 {
2027 struct amdgpu_device *adev = smu->adev;
2028 int ret = 0;
2029
2030 if ((!adev->in_runpm) && (!adev->in_suspend) &&
2031 (!amdgpu_in_reset(adev)) && amdgpu_ip_version(adev, MP1_HWIP, 0) ==
2032 IP_VERSION(13, 0, 10) &&
2033 !amdgpu_device_has_display_hardware(adev))
2034 ret = smu_set_mp1_state(smu, PP_MP1_STATE_UNLOAD);
2035
2036 return ret;
2037 }
2038
smu_hw_fini(void * handle)2039 static int smu_hw_fini(void *handle)
2040 {
2041 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2042 struct smu_context *smu = adev->powerplay.pp_handle;
2043 int ret;
2044
2045 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2046 return 0;
2047
2048 smu_dpm_set_vcn_enable(smu, false);
2049 smu_dpm_set_jpeg_enable(smu, false);
2050 smu_dpm_set_vpe_enable(smu, false);
2051 smu_dpm_set_umsch_mm_enable(smu, false);
2052
2053 adev->vcn.cur_state = AMD_PG_STATE_GATE;
2054 adev->jpeg.cur_state = AMD_PG_STATE_GATE;
2055
2056 if (!smu->pm_enabled)
2057 return 0;
2058
2059 adev->pm.dpm_enabled = false;
2060
2061 ret = smu_smc_hw_cleanup(smu);
2062 if (ret)
2063 return ret;
2064
2065 ret = smu_reset_mp1_state(smu);
2066 if (ret)
2067 return ret;
2068
2069 return 0;
2070 }
2071
smu_late_fini(void * handle)2072 static void smu_late_fini(void *handle)
2073 {
2074 struct amdgpu_device *adev = handle;
2075 struct smu_context *smu = adev->powerplay.pp_handle;
2076
2077 kfree(smu);
2078 }
2079
smu_reset(struct smu_context * smu)2080 static int smu_reset(struct smu_context *smu)
2081 {
2082 struct amdgpu_device *adev = smu->adev;
2083 int ret;
2084
2085 ret = smu_hw_fini(adev);
2086 if (ret)
2087 return ret;
2088
2089 ret = smu_hw_init(adev);
2090 if (ret)
2091 return ret;
2092
2093 ret = smu_late_init(adev);
2094 if (ret)
2095 return ret;
2096
2097 return 0;
2098 }
2099
smu_suspend(void * handle)2100 static int smu_suspend(void *handle)
2101 {
2102 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2103 struct smu_context *smu = adev->powerplay.pp_handle;
2104 int ret;
2105 uint64_t count;
2106
2107 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2108 return 0;
2109
2110 if (!smu->pm_enabled)
2111 return 0;
2112
2113 adev->pm.dpm_enabled = false;
2114
2115 ret = smu_smc_hw_cleanup(smu);
2116 if (ret)
2117 return ret;
2118
2119 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
2120
2121 smu_set_gfx_cgpg(smu, false);
2122
2123 /*
2124 * pwfw resets entrycount when device is suspended, so we save the
2125 * last value to be used when we resume to keep it consistent
2126 */
2127 ret = smu_get_entrycount_gfxoff(smu, &count);
2128 if (!ret)
2129 adev->gfx.gfx_off_entrycount = count;
2130
2131 /* clear this on suspend so it will get reprogrammed on resume */
2132 smu->workload_mask = 0;
2133
2134 return 0;
2135 }
2136
smu_resume(void * handle)2137 static int smu_resume(void *handle)
2138 {
2139 int ret;
2140 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2141 struct smu_context *smu = adev->powerplay.pp_handle;
2142
2143 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
2144 return 0;
2145
2146 if (!smu->pm_enabled)
2147 return 0;
2148
2149 dev_info(adev->dev, "SMU is resuming...\n");
2150
2151 ret = smu_start_smc_engine(smu);
2152 if (ret) {
2153 dev_err(adev->dev, "SMC engine is not correctly up!\n");
2154 return ret;
2155 }
2156
2157 ret = smu_smc_hw_setup(smu);
2158 if (ret) {
2159 dev_err(adev->dev, "Failed to setup smc hw!\n");
2160 return ret;
2161 }
2162
2163 ret = smu_set_gfx_imu_enable(smu);
2164 if (ret)
2165 return ret;
2166
2167 smu_set_gfx_cgpg(smu, true);
2168
2169 smu->disable_uclk_switch = 0;
2170
2171 adev->pm.dpm_enabled = true;
2172
2173 dev_info(adev->dev, "SMU is resumed successfully!\n");
2174
2175 return 0;
2176 }
2177
smu_display_configuration_change(void * handle,const struct amd_pp_display_configuration * display_config)2178 static int smu_display_configuration_change(void *handle,
2179 const struct amd_pp_display_configuration *display_config)
2180 {
2181 struct smu_context *smu = handle;
2182
2183 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2184 return -EOPNOTSUPP;
2185
2186 if (!display_config)
2187 return -EINVAL;
2188
2189 smu_set_min_dcef_deep_sleep(smu,
2190 display_config->min_dcef_deep_sleep_set_clk / 100);
2191
2192 return 0;
2193 }
2194
smu_set_clockgating_state(void * handle,enum amd_clockgating_state state)2195 static int smu_set_clockgating_state(void *handle,
2196 enum amd_clockgating_state state)
2197 {
2198 return 0;
2199 }
2200
smu_set_powergating_state(void * handle,enum amd_powergating_state state)2201 static int smu_set_powergating_state(void *handle,
2202 enum amd_powergating_state state)
2203 {
2204 return 0;
2205 }
2206
smu_enable_umd_pstate(void * handle,enum amd_dpm_forced_level * level)2207 static int smu_enable_umd_pstate(void *handle,
2208 enum amd_dpm_forced_level *level)
2209 {
2210 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
2211 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
2212 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
2213 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
2214
2215 struct smu_context *smu = (struct smu_context*)(handle);
2216 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2217
2218 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2219 return -EINVAL;
2220
2221 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
2222 /* enter umd pstate, save current level, disable gfx cg*/
2223 if (*level & profile_mode_mask) {
2224 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
2225 smu_gpo_control(smu, false);
2226 smu_gfx_ulv_control(smu, false);
2227 smu_deep_sleep_control(smu, false);
2228 amdgpu_asic_update_umd_stable_pstate(smu->adev, true);
2229 }
2230 } else {
2231 /* exit umd pstate, restore level, enable gfx cg*/
2232 if (!(*level & profile_mode_mask)) {
2233 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
2234 *level = smu_dpm_ctx->saved_dpm_level;
2235 amdgpu_asic_update_umd_stable_pstate(smu->adev, false);
2236 smu_deep_sleep_control(smu, true);
2237 smu_gfx_ulv_control(smu, true);
2238 smu_gpo_control(smu, true);
2239 }
2240 }
2241
2242 return 0;
2243 }
2244
smu_bump_power_profile_mode(struct smu_context * smu,long * custom_params,u32 custom_params_max_idx)2245 static int smu_bump_power_profile_mode(struct smu_context *smu,
2246 long *custom_params,
2247 u32 custom_params_max_idx)
2248 {
2249 u32 workload_mask = 0;
2250 int i, ret = 0;
2251
2252 for (i = 0; i < PP_SMC_POWER_PROFILE_COUNT; i++) {
2253 if (smu->workload_refcount[i])
2254 workload_mask |= 1 << i;
2255 }
2256
2257 if (smu->workload_mask == workload_mask)
2258 return 0;
2259
2260 if (smu->ppt_funcs->set_power_profile_mode)
2261 ret = smu->ppt_funcs->set_power_profile_mode(smu, workload_mask,
2262 custom_params,
2263 custom_params_max_idx);
2264
2265 if (!ret)
2266 smu->workload_mask = workload_mask;
2267
2268 return ret;
2269 }
2270
smu_power_profile_mode_get(struct smu_context * smu,enum PP_SMC_POWER_PROFILE profile_mode)2271 static void smu_power_profile_mode_get(struct smu_context *smu,
2272 enum PP_SMC_POWER_PROFILE profile_mode)
2273 {
2274 smu->workload_refcount[profile_mode]++;
2275 }
2276
smu_power_profile_mode_put(struct smu_context * smu,enum PP_SMC_POWER_PROFILE profile_mode)2277 static void smu_power_profile_mode_put(struct smu_context *smu,
2278 enum PP_SMC_POWER_PROFILE profile_mode)
2279 {
2280 if (smu->workload_refcount[profile_mode])
2281 smu->workload_refcount[profile_mode]--;
2282 }
2283
smu_adjust_power_state_dynamic(struct smu_context * smu,enum amd_dpm_forced_level level,bool skip_display_settings)2284 static int smu_adjust_power_state_dynamic(struct smu_context *smu,
2285 enum amd_dpm_forced_level level,
2286 bool skip_display_settings)
2287 {
2288 int ret = 0;
2289 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2290
2291 if (!skip_display_settings) {
2292 ret = smu_display_config_changed(smu);
2293 if (ret) {
2294 dev_err(smu->adev->dev, "Failed to change display config!");
2295 return ret;
2296 }
2297 }
2298
2299 ret = smu_apply_clocks_adjust_rules(smu);
2300 if (ret) {
2301 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
2302 return ret;
2303 }
2304
2305 if (!skip_display_settings) {
2306 ret = smu_notify_smc_display_config(smu);
2307 if (ret) {
2308 dev_err(smu->adev->dev, "Failed to notify smc display config!");
2309 return ret;
2310 }
2311 }
2312
2313 if (smu_dpm_ctx->dpm_level != level) {
2314 ret = smu_asic_set_performance_level(smu, level);
2315 if (ret) {
2316 dev_err(smu->adev->dev, "Failed to set performance level!");
2317 return ret;
2318 }
2319
2320 /* update the saved copy */
2321 smu_dpm_ctx->dpm_level = level;
2322 }
2323
2324 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2325 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)
2326 smu_bump_power_profile_mode(smu, NULL, 0);
2327
2328 return ret;
2329 }
2330
smu_handle_task(struct smu_context * smu,enum amd_dpm_forced_level level,enum amd_pp_task task_id)2331 static int smu_handle_task(struct smu_context *smu,
2332 enum amd_dpm_forced_level level,
2333 enum amd_pp_task task_id)
2334 {
2335 int ret = 0;
2336
2337 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2338 return -EOPNOTSUPP;
2339
2340 switch (task_id) {
2341 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
2342 ret = smu_pre_display_config_changed(smu);
2343 if (ret)
2344 return ret;
2345 ret = smu_adjust_power_state_dynamic(smu, level, false);
2346 break;
2347 case AMD_PP_TASK_COMPLETE_INIT:
2348 ret = smu_adjust_power_state_dynamic(smu, level, true);
2349 break;
2350 case AMD_PP_TASK_READJUST_POWER_STATE:
2351 ret = smu_adjust_power_state_dynamic(smu, level, true);
2352 break;
2353 default:
2354 break;
2355 }
2356
2357 return ret;
2358 }
2359
smu_handle_dpm_task(void * handle,enum amd_pp_task task_id,enum amd_pm_state_type * user_state)2360 static int smu_handle_dpm_task(void *handle,
2361 enum amd_pp_task task_id,
2362 enum amd_pm_state_type *user_state)
2363 {
2364 struct smu_context *smu = handle;
2365 struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
2366
2367 return smu_handle_task(smu, smu_dpm->dpm_level, task_id);
2368
2369 }
2370
smu_switch_power_profile(void * handle,enum PP_SMC_POWER_PROFILE type,bool enable)2371 static int smu_switch_power_profile(void *handle,
2372 enum PP_SMC_POWER_PROFILE type,
2373 bool enable)
2374 {
2375 struct smu_context *smu = handle;
2376 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2377 int ret;
2378
2379 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2380 return -EOPNOTSUPP;
2381
2382 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
2383 return -EINVAL;
2384
2385 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL &&
2386 smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
2387 if (enable)
2388 smu_power_profile_mode_get(smu, type);
2389 else
2390 smu_power_profile_mode_put(smu, type);
2391 ret = smu_bump_power_profile_mode(smu, NULL, 0);
2392 if (ret) {
2393 if (enable)
2394 smu_power_profile_mode_put(smu, type);
2395 else
2396 smu_power_profile_mode_get(smu, type);
2397 return ret;
2398 }
2399 }
2400
2401 return 0;
2402 }
2403
smu_get_performance_level(void * handle)2404 static enum amd_dpm_forced_level smu_get_performance_level(void *handle)
2405 {
2406 struct smu_context *smu = handle;
2407 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2408
2409 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2410 return -EOPNOTSUPP;
2411
2412 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2413 return -EINVAL;
2414
2415 return smu_dpm_ctx->dpm_level;
2416 }
2417
smu_force_performance_level(void * handle,enum amd_dpm_forced_level level)2418 static int smu_force_performance_level(void *handle,
2419 enum amd_dpm_forced_level level)
2420 {
2421 struct smu_context *smu = handle;
2422 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2423 int ret = 0;
2424
2425 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2426 return -EOPNOTSUPP;
2427
2428 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
2429 return -EINVAL;
2430
2431 ret = smu_enable_umd_pstate(smu, &level);
2432 if (ret)
2433 return ret;
2434
2435 ret = smu_handle_task(smu, level,
2436 AMD_PP_TASK_READJUST_POWER_STATE);
2437
2438 /* reset user dpm clock state */
2439 if (!ret && smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2440 memset(smu->user_dpm_profile.clk_mask, 0, sizeof(smu->user_dpm_profile.clk_mask));
2441 smu->user_dpm_profile.clk_dependency = 0;
2442 }
2443
2444 return ret;
2445 }
2446
smu_set_display_count(void * handle,uint32_t count)2447 static int smu_set_display_count(void *handle, uint32_t count)
2448 {
2449 struct smu_context *smu = handle;
2450
2451 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2452 return -EOPNOTSUPP;
2453
2454 return smu_init_display_count(smu, count);
2455 }
2456
smu_force_smuclk_levels(struct smu_context * smu,enum smu_clk_type clk_type,uint32_t mask)2457 static int smu_force_smuclk_levels(struct smu_context *smu,
2458 enum smu_clk_type clk_type,
2459 uint32_t mask)
2460 {
2461 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
2462 int ret = 0;
2463
2464 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2465 return -EOPNOTSUPP;
2466
2467 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
2468 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
2469 return -EINVAL;
2470 }
2471
2472 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels) {
2473 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
2474 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2475 smu->user_dpm_profile.clk_mask[clk_type] = mask;
2476 smu_set_user_clk_dependencies(smu, clk_type);
2477 }
2478 }
2479
2480 return ret;
2481 }
2482
smu_force_ppclk_levels(void * handle,enum pp_clock_type type,uint32_t mask)2483 static int smu_force_ppclk_levels(void *handle,
2484 enum pp_clock_type type,
2485 uint32_t mask)
2486 {
2487 struct smu_context *smu = handle;
2488 enum smu_clk_type clk_type;
2489
2490 switch (type) {
2491 case PP_SCLK:
2492 clk_type = SMU_SCLK; break;
2493 case PP_MCLK:
2494 clk_type = SMU_MCLK; break;
2495 case PP_PCIE:
2496 clk_type = SMU_PCIE; break;
2497 case PP_SOCCLK:
2498 clk_type = SMU_SOCCLK; break;
2499 case PP_FCLK:
2500 clk_type = SMU_FCLK; break;
2501 case PP_DCEFCLK:
2502 clk_type = SMU_DCEFCLK; break;
2503 case PP_VCLK:
2504 clk_type = SMU_VCLK; break;
2505 case PP_VCLK1:
2506 clk_type = SMU_VCLK1; break;
2507 case PP_DCLK:
2508 clk_type = SMU_DCLK; break;
2509 case PP_DCLK1:
2510 clk_type = SMU_DCLK1; break;
2511 case OD_SCLK:
2512 clk_type = SMU_OD_SCLK; break;
2513 case OD_MCLK:
2514 clk_type = SMU_OD_MCLK; break;
2515 case OD_VDDC_CURVE:
2516 clk_type = SMU_OD_VDDC_CURVE; break;
2517 case OD_RANGE:
2518 clk_type = SMU_OD_RANGE; break;
2519 default:
2520 return -EINVAL;
2521 }
2522
2523 return smu_force_smuclk_levels(smu, clk_type, mask);
2524 }
2525
2526 /*
2527 * On system suspending or resetting, the dpm_enabled
2528 * flag will be cleared. So that those SMU services which
2529 * are not supported will be gated.
2530 * However, the mp1 state setting should still be granted
2531 * even if the dpm_enabled cleared.
2532 */
smu_set_mp1_state(void * handle,enum pp_mp1_state mp1_state)2533 static int smu_set_mp1_state(void *handle,
2534 enum pp_mp1_state mp1_state)
2535 {
2536 struct smu_context *smu = handle;
2537 int ret = 0;
2538
2539 if (!smu->pm_enabled)
2540 return -EOPNOTSUPP;
2541
2542 if (smu->ppt_funcs &&
2543 smu->ppt_funcs->set_mp1_state)
2544 ret = smu->ppt_funcs->set_mp1_state(smu, mp1_state);
2545
2546 return ret;
2547 }
2548
smu_set_df_cstate(void * handle,enum pp_df_cstate state)2549 static int smu_set_df_cstate(void *handle,
2550 enum pp_df_cstate state)
2551 {
2552 struct smu_context *smu = handle;
2553 int ret = 0;
2554
2555 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2556 return -EOPNOTSUPP;
2557
2558 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
2559 return 0;
2560
2561 ret = smu->ppt_funcs->set_df_cstate(smu, state);
2562 if (ret)
2563 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
2564
2565 return ret;
2566 }
2567
smu_write_watermarks_table(struct smu_context * smu)2568 int smu_write_watermarks_table(struct smu_context *smu)
2569 {
2570 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2571 return -EOPNOTSUPP;
2572
2573 return smu_set_watermarks_table(smu, NULL);
2574 }
2575
smu_set_watermarks_for_clock_ranges(void * handle,struct pp_smu_wm_range_sets * clock_ranges)2576 static int smu_set_watermarks_for_clock_ranges(void *handle,
2577 struct pp_smu_wm_range_sets *clock_ranges)
2578 {
2579 struct smu_context *smu = handle;
2580
2581 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2582 return -EOPNOTSUPP;
2583
2584 if (smu->disable_watermark)
2585 return 0;
2586
2587 return smu_set_watermarks_table(smu, clock_ranges);
2588 }
2589
smu_set_ac_dc(struct smu_context * smu)2590 int smu_set_ac_dc(struct smu_context *smu)
2591 {
2592 int ret = 0;
2593
2594 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2595 return -EOPNOTSUPP;
2596
2597 /* controlled by firmware */
2598 if (smu->dc_controlled_by_gpio)
2599 return 0;
2600
2601 ret = smu_set_power_source(smu,
2602 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
2603 SMU_POWER_SOURCE_DC);
2604 if (ret)
2605 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
2606 smu->adev->pm.ac_power ? "AC" : "DC");
2607
2608 return ret;
2609 }
2610
2611 const struct amd_ip_funcs smu_ip_funcs = {
2612 .name = "smu",
2613 .early_init = smu_early_init,
2614 .late_init = smu_late_init,
2615 .sw_init = smu_sw_init,
2616 .sw_fini = smu_sw_fini,
2617 .hw_init = smu_hw_init,
2618 .hw_fini = smu_hw_fini,
2619 .late_fini = smu_late_fini,
2620 .suspend = smu_suspend,
2621 .resume = smu_resume,
2622 .is_idle = NULL,
2623 .check_soft_reset = NULL,
2624 .wait_for_idle = NULL,
2625 .soft_reset = NULL,
2626 .set_clockgating_state = smu_set_clockgating_state,
2627 .set_powergating_state = smu_set_powergating_state,
2628 };
2629
2630 const struct amdgpu_ip_block_version smu_v11_0_ip_block = {
2631 .type = AMD_IP_BLOCK_TYPE_SMC,
2632 .major = 11,
2633 .minor = 0,
2634 .rev = 0,
2635 .funcs = &smu_ip_funcs,
2636 };
2637
2638 const struct amdgpu_ip_block_version smu_v12_0_ip_block = {
2639 .type = AMD_IP_BLOCK_TYPE_SMC,
2640 .major = 12,
2641 .minor = 0,
2642 .rev = 0,
2643 .funcs = &smu_ip_funcs,
2644 };
2645
2646 const struct amdgpu_ip_block_version smu_v13_0_ip_block = {
2647 .type = AMD_IP_BLOCK_TYPE_SMC,
2648 .major = 13,
2649 .minor = 0,
2650 .rev = 0,
2651 .funcs = &smu_ip_funcs,
2652 };
2653
2654 const struct amdgpu_ip_block_version smu_v14_0_ip_block = {
2655 .type = AMD_IP_BLOCK_TYPE_SMC,
2656 .major = 14,
2657 .minor = 0,
2658 .rev = 0,
2659 .funcs = &smu_ip_funcs,
2660 };
2661
smu_load_microcode(void * handle)2662 static int smu_load_microcode(void *handle)
2663 {
2664 struct smu_context *smu = handle;
2665 struct amdgpu_device *adev = smu->adev;
2666 int ret = 0;
2667
2668 if (!smu->pm_enabled)
2669 return -EOPNOTSUPP;
2670
2671 /* This should be used for non PSP loading */
2672 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
2673 return 0;
2674
2675 if (smu->ppt_funcs->load_microcode) {
2676 ret = smu->ppt_funcs->load_microcode(smu);
2677 if (ret) {
2678 dev_err(adev->dev, "Load microcode failed\n");
2679 return ret;
2680 }
2681 }
2682
2683 if (smu->ppt_funcs->check_fw_status) {
2684 ret = smu->ppt_funcs->check_fw_status(smu);
2685 if (ret) {
2686 dev_err(adev->dev, "SMC is not ready\n");
2687 return ret;
2688 }
2689 }
2690
2691 return ret;
2692 }
2693
smu_set_gfx_cgpg(struct smu_context * smu,bool enabled)2694 static int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
2695 {
2696 int ret = 0;
2697
2698 if (smu->ppt_funcs->set_gfx_cgpg)
2699 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
2700
2701 return ret;
2702 }
2703
smu_set_fan_speed_rpm(void * handle,uint32_t speed)2704 static int smu_set_fan_speed_rpm(void *handle, uint32_t speed)
2705 {
2706 struct smu_context *smu = handle;
2707 int ret = 0;
2708
2709 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2710 return -EOPNOTSUPP;
2711
2712 if (!smu->ppt_funcs->set_fan_speed_rpm)
2713 return -EOPNOTSUPP;
2714
2715 if (speed == U32_MAX)
2716 return -EINVAL;
2717
2718 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
2719 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
2720 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_RPM;
2721 smu->user_dpm_profile.fan_speed_rpm = speed;
2722
2723 /* Override custom PWM setting as they cannot co-exist */
2724 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_PWM;
2725 smu->user_dpm_profile.fan_speed_pwm = 0;
2726 }
2727
2728 return ret;
2729 }
2730
2731 /**
2732 * smu_get_power_limit - Request one of the SMU Power Limits
2733 *
2734 * @handle: pointer to smu context
2735 * @limit: requested limit is written back to this variable
2736 * @pp_limit_level: &pp_power_limit_level which limit of the power to return
2737 * @pp_power_type: &pp_power_type type of power
2738 * Return: 0 on success, <0 on error
2739 *
2740 */
smu_get_power_limit(void * handle,uint32_t * limit,enum pp_power_limit_level pp_limit_level,enum pp_power_type pp_power_type)2741 int smu_get_power_limit(void *handle,
2742 uint32_t *limit,
2743 enum pp_power_limit_level pp_limit_level,
2744 enum pp_power_type pp_power_type)
2745 {
2746 struct smu_context *smu = handle;
2747 struct amdgpu_device *adev = smu->adev;
2748 enum smu_ppt_limit_level limit_level;
2749 uint32_t limit_type;
2750 int ret = 0;
2751
2752 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2753 return -EOPNOTSUPP;
2754
2755 switch (pp_power_type) {
2756 case PP_PWR_TYPE_SUSTAINED:
2757 limit_type = SMU_DEFAULT_PPT_LIMIT;
2758 break;
2759 case PP_PWR_TYPE_FAST:
2760 limit_type = SMU_FAST_PPT_LIMIT;
2761 break;
2762 default:
2763 return -EOPNOTSUPP;
2764 }
2765
2766 switch (pp_limit_level) {
2767 case PP_PWR_LIMIT_CURRENT:
2768 limit_level = SMU_PPT_LIMIT_CURRENT;
2769 break;
2770 case PP_PWR_LIMIT_DEFAULT:
2771 limit_level = SMU_PPT_LIMIT_DEFAULT;
2772 break;
2773 case PP_PWR_LIMIT_MAX:
2774 limit_level = SMU_PPT_LIMIT_MAX;
2775 break;
2776 case PP_PWR_LIMIT_MIN:
2777 limit_level = SMU_PPT_LIMIT_MIN;
2778 break;
2779 default:
2780 return -EOPNOTSUPP;
2781 }
2782
2783 if (limit_type != SMU_DEFAULT_PPT_LIMIT) {
2784 if (smu->ppt_funcs->get_ppt_limit)
2785 ret = smu->ppt_funcs->get_ppt_limit(smu, limit, limit_type, limit_level);
2786 } else {
2787 switch (limit_level) {
2788 case SMU_PPT_LIMIT_CURRENT:
2789 switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
2790 case IP_VERSION(13, 0, 2):
2791 case IP_VERSION(13, 0, 6):
2792 case IP_VERSION(13, 0, 14):
2793 case IP_VERSION(11, 0, 7):
2794 case IP_VERSION(11, 0, 11):
2795 case IP_VERSION(11, 0, 12):
2796 case IP_VERSION(11, 0, 13):
2797 ret = smu_get_asic_power_limits(smu,
2798 &smu->current_power_limit,
2799 NULL, NULL, NULL);
2800 break;
2801 default:
2802 break;
2803 }
2804 *limit = smu->current_power_limit;
2805 break;
2806 case SMU_PPT_LIMIT_DEFAULT:
2807 *limit = smu->default_power_limit;
2808 break;
2809 case SMU_PPT_LIMIT_MAX:
2810 *limit = smu->max_power_limit;
2811 break;
2812 case SMU_PPT_LIMIT_MIN:
2813 *limit = smu->min_power_limit;
2814 break;
2815 default:
2816 return -EINVAL;
2817 }
2818 }
2819
2820 return ret;
2821 }
2822
smu_set_power_limit(void * handle,uint32_t limit)2823 static int smu_set_power_limit(void *handle, uint32_t limit)
2824 {
2825 struct smu_context *smu = handle;
2826 uint32_t limit_type = limit >> 24;
2827 int ret = 0;
2828
2829 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2830 return -EOPNOTSUPP;
2831
2832 limit &= (1<<24)-1;
2833 if (limit_type != SMU_DEFAULT_PPT_LIMIT)
2834 if (smu->ppt_funcs->set_power_limit)
2835 return smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2836
2837 if ((limit > smu->max_power_limit) || (limit < smu->min_power_limit)) {
2838 dev_err(smu->adev->dev,
2839 "New power limit (%d) is out of range [%d,%d]\n",
2840 limit, smu->min_power_limit, smu->max_power_limit);
2841 return -EINVAL;
2842 }
2843
2844 if (!limit)
2845 limit = smu->current_power_limit;
2846
2847 if (smu->ppt_funcs->set_power_limit) {
2848 ret = smu->ppt_funcs->set_power_limit(smu, limit_type, limit);
2849 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE))
2850 smu->user_dpm_profile.power_limit = limit;
2851 }
2852
2853 return ret;
2854 }
2855
smu_print_smuclk_levels(struct smu_context * smu,enum smu_clk_type clk_type,char * buf)2856 static int smu_print_smuclk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
2857 {
2858 int ret = 0;
2859
2860 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2861 return -EOPNOTSUPP;
2862
2863 if (smu->ppt_funcs->print_clk_levels)
2864 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
2865
2866 return ret;
2867 }
2868
smu_convert_to_smuclk(enum pp_clock_type type)2869 static enum smu_clk_type smu_convert_to_smuclk(enum pp_clock_type type)
2870 {
2871 enum smu_clk_type clk_type;
2872
2873 switch (type) {
2874 case PP_SCLK:
2875 clk_type = SMU_SCLK; break;
2876 case PP_MCLK:
2877 clk_type = SMU_MCLK; break;
2878 case PP_PCIE:
2879 clk_type = SMU_PCIE; break;
2880 case PP_SOCCLK:
2881 clk_type = SMU_SOCCLK; break;
2882 case PP_FCLK:
2883 clk_type = SMU_FCLK; break;
2884 case PP_DCEFCLK:
2885 clk_type = SMU_DCEFCLK; break;
2886 case PP_VCLK:
2887 clk_type = SMU_VCLK; break;
2888 case PP_VCLK1:
2889 clk_type = SMU_VCLK1; break;
2890 case PP_DCLK:
2891 clk_type = SMU_DCLK; break;
2892 case PP_DCLK1:
2893 clk_type = SMU_DCLK1; break;
2894 case OD_SCLK:
2895 clk_type = SMU_OD_SCLK; break;
2896 case OD_MCLK:
2897 clk_type = SMU_OD_MCLK; break;
2898 case OD_VDDC_CURVE:
2899 clk_type = SMU_OD_VDDC_CURVE; break;
2900 case OD_RANGE:
2901 clk_type = SMU_OD_RANGE; break;
2902 case OD_VDDGFX_OFFSET:
2903 clk_type = SMU_OD_VDDGFX_OFFSET; break;
2904 case OD_CCLK:
2905 clk_type = SMU_OD_CCLK; break;
2906 case OD_FAN_CURVE:
2907 clk_type = SMU_OD_FAN_CURVE; break;
2908 case OD_ACOUSTIC_LIMIT:
2909 clk_type = SMU_OD_ACOUSTIC_LIMIT; break;
2910 case OD_ACOUSTIC_TARGET:
2911 clk_type = SMU_OD_ACOUSTIC_TARGET; break;
2912 case OD_FAN_TARGET_TEMPERATURE:
2913 clk_type = SMU_OD_FAN_TARGET_TEMPERATURE; break;
2914 case OD_FAN_MINIMUM_PWM:
2915 clk_type = SMU_OD_FAN_MINIMUM_PWM; break;
2916 default:
2917 clk_type = SMU_CLK_COUNT; break;
2918 }
2919
2920 return clk_type;
2921 }
2922
smu_print_ppclk_levels(void * handle,enum pp_clock_type type,char * buf)2923 static int smu_print_ppclk_levels(void *handle,
2924 enum pp_clock_type type,
2925 char *buf)
2926 {
2927 struct smu_context *smu = handle;
2928 enum smu_clk_type clk_type;
2929
2930 clk_type = smu_convert_to_smuclk(type);
2931 if (clk_type == SMU_CLK_COUNT)
2932 return -EINVAL;
2933
2934 return smu_print_smuclk_levels(smu, clk_type, buf);
2935 }
2936
smu_emit_ppclk_levels(void * handle,enum pp_clock_type type,char * buf,int * offset)2937 static int smu_emit_ppclk_levels(void *handle, enum pp_clock_type type, char *buf, int *offset)
2938 {
2939 struct smu_context *smu = handle;
2940 enum smu_clk_type clk_type;
2941
2942 clk_type = smu_convert_to_smuclk(type);
2943 if (clk_type == SMU_CLK_COUNT)
2944 return -EINVAL;
2945
2946 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2947 return -EOPNOTSUPP;
2948
2949 if (!smu->ppt_funcs->emit_clk_levels)
2950 return -ENOENT;
2951
2952 return smu->ppt_funcs->emit_clk_levels(smu, clk_type, buf, offset);
2953
2954 }
2955
smu_od_edit_dpm_table(void * handle,enum PP_OD_DPM_TABLE_COMMAND type,long * input,uint32_t size)2956 static int smu_od_edit_dpm_table(void *handle,
2957 enum PP_OD_DPM_TABLE_COMMAND type,
2958 long *input, uint32_t size)
2959 {
2960 struct smu_context *smu = handle;
2961 int ret = 0;
2962
2963 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2964 return -EOPNOTSUPP;
2965
2966 if (smu->ppt_funcs->od_edit_dpm_table) {
2967 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2968 }
2969
2970 return ret;
2971 }
2972
smu_read_sensor(void * handle,int sensor,void * data,int * size_arg)2973 static int smu_read_sensor(void *handle,
2974 int sensor,
2975 void *data,
2976 int *size_arg)
2977 {
2978 struct smu_context *smu = handle;
2979 struct smu_umd_pstate_table *pstate_table =
2980 &smu->pstate_table;
2981 int ret = 0;
2982 uint32_t *size, size_val;
2983
2984 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2985 return -EOPNOTSUPP;
2986
2987 if (!data || !size_arg)
2988 return -EINVAL;
2989
2990 size_val = *size_arg;
2991 size = &size_val;
2992
2993 if (smu->ppt_funcs->read_sensor)
2994 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2995 goto unlock;
2996
2997 switch (sensor) {
2998 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2999 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
3000 *size = 4;
3001 break;
3002 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
3003 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
3004 *size = 4;
3005 break;
3006 case AMDGPU_PP_SENSOR_PEAK_PSTATE_SCLK:
3007 *((uint32_t *)data) = pstate_table->gfxclk_pstate.peak * 100;
3008 *size = 4;
3009 break;
3010 case AMDGPU_PP_SENSOR_PEAK_PSTATE_MCLK:
3011 *((uint32_t *)data) = pstate_table->uclk_pstate.peak * 100;
3012 *size = 4;
3013 break;
3014 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
3015 ret = smu_feature_get_enabled_mask(smu, (uint64_t *)data);
3016 *size = 8;
3017 break;
3018 case AMDGPU_PP_SENSOR_UVD_POWER:
3019 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
3020 *size = 4;
3021 break;
3022 case AMDGPU_PP_SENSOR_VCE_POWER:
3023 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
3024 *size = 4;
3025 break;
3026 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
3027 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0 : 1;
3028 *size = 4;
3029 break;
3030 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
3031 *(uint32_t *)data = 0;
3032 *size = 4;
3033 break;
3034 default:
3035 *size = 0;
3036 ret = -EOPNOTSUPP;
3037 break;
3038 }
3039
3040 unlock:
3041 // assign uint32_t to int
3042 *size_arg = size_val;
3043
3044 return ret;
3045 }
3046
smu_get_apu_thermal_limit(void * handle,uint32_t * limit)3047 static int smu_get_apu_thermal_limit(void *handle, uint32_t *limit)
3048 {
3049 int ret = -EOPNOTSUPP;
3050 struct smu_context *smu = handle;
3051
3052 if (smu->ppt_funcs && smu->ppt_funcs->get_apu_thermal_limit)
3053 ret = smu->ppt_funcs->get_apu_thermal_limit(smu, limit);
3054
3055 return ret;
3056 }
3057
smu_set_apu_thermal_limit(void * handle,uint32_t limit)3058 static int smu_set_apu_thermal_limit(void *handle, uint32_t limit)
3059 {
3060 int ret = -EOPNOTSUPP;
3061 struct smu_context *smu = handle;
3062
3063 if (smu->ppt_funcs && smu->ppt_funcs->set_apu_thermal_limit)
3064 ret = smu->ppt_funcs->set_apu_thermal_limit(smu, limit);
3065
3066 return ret;
3067 }
3068
smu_get_power_profile_mode(void * handle,char * buf)3069 static int smu_get_power_profile_mode(void *handle, char *buf)
3070 {
3071 struct smu_context *smu = handle;
3072
3073 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
3074 !smu->ppt_funcs->get_power_profile_mode)
3075 return -EOPNOTSUPP;
3076 if (!buf)
3077 return -EINVAL;
3078
3079 return smu->ppt_funcs->get_power_profile_mode(smu, buf);
3080 }
3081
smu_set_power_profile_mode(void * handle,long * param,uint32_t param_size)3082 static int smu_set_power_profile_mode(void *handle,
3083 long *param,
3084 uint32_t param_size)
3085 {
3086 struct smu_context *smu = handle;
3087 bool custom = false;
3088 int ret = 0;
3089
3090 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled ||
3091 !smu->ppt_funcs->set_power_profile_mode)
3092 return -EOPNOTSUPP;
3093
3094 if (param[param_size] == PP_SMC_POWER_PROFILE_CUSTOM) {
3095 custom = true;
3096 /* clear frontend mask so custom changes propogate */
3097 smu->workload_mask = 0;
3098 }
3099
3100 if ((param[param_size] != smu->power_profile_mode) || custom) {
3101 /* clear the old user preference */
3102 smu_power_profile_mode_put(smu, smu->power_profile_mode);
3103 /* set the new user preference */
3104 smu_power_profile_mode_get(smu, param[param_size]);
3105 ret = smu_bump_power_profile_mode(smu,
3106 custom ? param : NULL,
3107 custom ? param_size : 0);
3108 if (ret)
3109 smu_power_profile_mode_put(smu, param[param_size]);
3110 else
3111 /* store the user's preference */
3112 smu->power_profile_mode = param[param_size];
3113 }
3114
3115 return ret;
3116 }
3117
smu_get_fan_control_mode(void * handle,u32 * fan_mode)3118 static int smu_get_fan_control_mode(void *handle, u32 *fan_mode)
3119 {
3120 struct smu_context *smu = handle;
3121
3122 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3123 return -EOPNOTSUPP;
3124
3125 if (!smu->ppt_funcs->get_fan_control_mode)
3126 return -EOPNOTSUPP;
3127
3128 if (!fan_mode)
3129 return -EINVAL;
3130
3131 *fan_mode = smu->ppt_funcs->get_fan_control_mode(smu);
3132
3133 return 0;
3134 }
3135
smu_set_fan_control_mode(void * handle,u32 value)3136 static int smu_set_fan_control_mode(void *handle, u32 value)
3137 {
3138 struct smu_context *smu = handle;
3139 int ret = 0;
3140
3141 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3142 return -EOPNOTSUPP;
3143
3144 if (!smu->ppt_funcs->set_fan_control_mode)
3145 return -EOPNOTSUPP;
3146
3147 if (value == U32_MAX)
3148 return -EINVAL;
3149
3150 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
3151 if (ret)
3152 goto out;
3153
3154 if (!(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3155 smu->user_dpm_profile.fan_mode = value;
3156
3157 /* reset user dpm fan speed */
3158 if (value != AMD_FAN_CTRL_MANUAL) {
3159 smu->user_dpm_profile.fan_speed_pwm = 0;
3160 smu->user_dpm_profile.fan_speed_rpm = 0;
3161 smu->user_dpm_profile.flags &= ~(SMU_CUSTOM_FAN_SPEED_RPM | SMU_CUSTOM_FAN_SPEED_PWM);
3162 }
3163 }
3164
3165 out:
3166 return ret;
3167 }
3168
smu_get_fan_speed_pwm(void * handle,u32 * speed)3169 static int smu_get_fan_speed_pwm(void *handle, u32 *speed)
3170 {
3171 struct smu_context *smu = handle;
3172 int ret = 0;
3173
3174 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3175 return -EOPNOTSUPP;
3176
3177 if (!smu->ppt_funcs->get_fan_speed_pwm)
3178 return -EOPNOTSUPP;
3179
3180 if (!speed)
3181 return -EINVAL;
3182
3183 ret = smu->ppt_funcs->get_fan_speed_pwm(smu, speed);
3184
3185 return ret;
3186 }
3187
smu_set_fan_speed_pwm(void * handle,u32 speed)3188 static int smu_set_fan_speed_pwm(void *handle, u32 speed)
3189 {
3190 struct smu_context *smu = handle;
3191 int ret = 0;
3192
3193 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3194 return -EOPNOTSUPP;
3195
3196 if (!smu->ppt_funcs->set_fan_speed_pwm)
3197 return -EOPNOTSUPP;
3198
3199 if (speed == U32_MAX)
3200 return -EINVAL;
3201
3202 ret = smu->ppt_funcs->set_fan_speed_pwm(smu, speed);
3203 if (!ret && !(smu->user_dpm_profile.flags & SMU_DPM_USER_PROFILE_RESTORE)) {
3204 smu->user_dpm_profile.flags |= SMU_CUSTOM_FAN_SPEED_PWM;
3205 smu->user_dpm_profile.fan_speed_pwm = speed;
3206
3207 /* Override custom RPM setting as they cannot co-exist */
3208 smu->user_dpm_profile.flags &= ~SMU_CUSTOM_FAN_SPEED_RPM;
3209 smu->user_dpm_profile.fan_speed_rpm = 0;
3210 }
3211
3212 return ret;
3213 }
3214
smu_get_fan_speed_rpm(void * handle,uint32_t * speed)3215 static int smu_get_fan_speed_rpm(void *handle, uint32_t *speed)
3216 {
3217 struct smu_context *smu = handle;
3218 int ret = 0;
3219
3220 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3221 return -EOPNOTSUPP;
3222
3223 if (!smu->ppt_funcs->get_fan_speed_rpm)
3224 return -EOPNOTSUPP;
3225
3226 if (!speed)
3227 return -EINVAL;
3228
3229 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
3230
3231 return ret;
3232 }
3233
smu_set_deep_sleep_dcefclk(void * handle,uint32_t clk)3234 static int smu_set_deep_sleep_dcefclk(void *handle, uint32_t clk)
3235 {
3236 struct smu_context *smu = handle;
3237
3238 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3239 return -EOPNOTSUPP;
3240
3241 return smu_set_min_dcef_deep_sleep(smu, clk);
3242 }
3243
smu_get_clock_by_type_with_latency(void * handle,enum amd_pp_clock_type type,struct pp_clock_levels_with_latency * clocks)3244 static int smu_get_clock_by_type_with_latency(void *handle,
3245 enum amd_pp_clock_type type,
3246 struct pp_clock_levels_with_latency *clocks)
3247 {
3248 struct smu_context *smu = handle;
3249 enum smu_clk_type clk_type;
3250 int ret = 0;
3251
3252 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3253 return -EOPNOTSUPP;
3254
3255 if (smu->ppt_funcs->get_clock_by_type_with_latency) {
3256 switch (type) {
3257 case amd_pp_sys_clock:
3258 clk_type = SMU_GFXCLK;
3259 break;
3260 case amd_pp_mem_clock:
3261 clk_type = SMU_MCLK;
3262 break;
3263 case amd_pp_dcef_clock:
3264 clk_type = SMU_DCEFCLK;
3265 break;
3266 case amd_pp_disp_clock:
3267 clk_type = SMU_DISPCLK;
3268 break;
3269 default:
3270 dev_err(smu->adev->dev, "Invalid clock type!\n");
3271 return -EINVAL;
3272 }
3273
3274 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
3275 }
3276
3277 return ret;
3278 }
3279
smu_display_clock_voltage_request(void * handle,struct pp_display_clock_request * clock_req)3280 static int smu_display_clock_voltage_request(void *handle,
3281 struct pp_display_clock_request *clock_req)
3282 {
3283 struct smu_context *smu = handle;
3284 int ret = 0;
3285
3286 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3287 return -EOPNOTSUPP;
3288
3289 if (smu->ppt_funcs->display_clock_voltage_request)
3290 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
3291
3292 return ret;
3293 }
3294
3295
smu_display_disable_memory_clock_switch(void * handle,bool disable_memory_clock_switch)3296 static int smu_display_disable_memory_clock_switch(void *handle,
3297 bool disable_memory_clock_switch)
3298 {
3299 struct smu_context *smu = handle;
3300 int ret = -EINVAL;
3301
3302 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3303 return -EOPNOTSUPP;
3304
3305 if (smu->ppt_funcs->display_disable_memory_clock_switch)
3306 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
3307
3308 return ret;
3309 }
3310
smu_set_xgmi_pstate(void * handle,uint32_t pstate)3311 static int smu_set_xgmi_pstate(void *handle,
3312 uint32_t pstate)
3313 {
3314 struct smu_context *smu = handle;
3315 int ret = 0;
3316
3317 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3318 return -EOPNOTSUPP;
3319
3320 if (smu->ppt_funcs->set_xgmi_pstate)
3321 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
3322
3323 if (ret)
3324 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
3325
3326 return ret;
3327 }
3328
smu_get_baco_capability(void * handle)3329 static int smu_get_baco_capability(void *handle)
3330 {
3331 struct smu_context *smu = handle;
3332
3333 if (!smu->pm_enabled)
3334 return false;
3335
3336 if (!smu->ppt_funcs || !smu->ppt_funcs->get_bamaco_support)
3337 return false;
3338
3339 return smu->ppt_funcs->get_bamaco_support(smu);
3340 }
3341
smu_baco_set_state(void * handle,int state)3342 static int smu_baco_set_state(void *handle, int state)
3343 {
3344 struct smu_context *smu = handle;
3345 int ret = 0;
3346
3347 if (!smu->pm_enabled)
3348 return -EOPNOTSUPP;
3349
3350 if (state == 0) {
3351 if (smu->ppt_funcs->baco_exit)
3352 ret = smu->ppt_funcs->baco_exit(smu);
3353 } else if (state == 1) {
3354 if (smu->ppt_funcs->baco_enter)
3355 ret = smu->ppt_funcs->baco_enter(smu);
3356 } else {
3357 return -EINVAL;
3358 }
3359
3360 if (ret)
3361 dev_err(smu->adev->dev, "Failed to %s BACO state!\n",
3362 (state)?"enter":"exit");
3363
3364 return ret;
3365 }
3366
smu_mode1_reset_is_support(struct smu_context * smu)3367 bool smu_mode1_reset_is_support(struct smu_context *smu)
3368 {
3369 bool ret = false;
3370
3371 if (!smu->pm_enabled)
3372 return false;
3373
3374 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
3375 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
3376
3377 return ret;
3378 }
3379
smu_mode2_reset_is_support(struct smu_context * smu)3380 bool smu_mode2_reset_is_support(struct smu_context *smu)
3381 {
3382 bool ret = false;
3383
3384 if (!smu->pm_enabled)
3385 return false;
3386
3387 if (smu->ppt_funcs && smu->ppt_funcs->mode2_reset_is_support)
3388 ret = smu->ppt_funcs->mode2_reset_is_support(smu);
3389
3390 return ret;
3391 }
3392
smu_mode1_reset(struct smu_context * smu)3393 int smu_mode1_reset(struct smu_context *smu)
3394 {
3395 int ret = 0;
3396
3397 if (!smu->pm_enabled)
3398 return -EOPNOTSUPP;
3399
3400 if (smu->ppt_funcs->mode1_reset)
3401 ret = smu->ppt_funcs->mode1_reset(smu);
3402
3403 return ret;
3404 }
3405
smu_mode2_reset(void * handle)3406 static int smu_mode2_reset(void *handle)
3407 {
3408 struct smu_context *smu = handle;
3409 int ret = 0;
3410
3411 if (!smu->pm_enabled)
3412 return -EOPNOTSUPP;
3413
3414 if (smu->ppt_funcs->mode2_reset)
3415 ret = smu->ppt_funcs->mode2_reset(smu);
3416
3417 if (ret)
3418 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
3419
3420 return ret;
3421 }
3422
smu_enable_gfx_features(void * handle)3423 static int smu_enable_gfx_features(void *handle)
3424 {
3425 struct smu_context *smu = handle;
3426 int ret = 0;
3427
3428 if (!smu->pm_enabled)
3429 return -EOPNOTSUPP;
3430
3431 if (smu->ppt_funcs->enable_gfx_features)
3432 ret = smu->ppt_funcs->enable_gfx_features(smu);
3433
3434 if (ret)
3435 dev_err(smu->adev->dev, "enable gfx features failed!\n");
3436
3437 return ret;
3438 }
3439
smu_get_max_sustainable_clocks_by_dc(void * handle,struct pp_smu_nv_clock_table * max_clocks)3440 static int smu_get_max_sustainable_clocks_by_dc(void *handle,
3441 struct pp_smu_nv_clock_table *max_clocks)
3442 {
3443 struct smu_context *smu = handle;
3444 int ret = 0;
3445
3446 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3447 return -EOPNOTSUPP;
3448
3449 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
3450 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
3451
3452 return ret;
3453 }
3454
smu_get_uclk_dpm_states(void * handle,unsigned int * clock_values_in_khz,unsigned int * num_states)3455 static int smu_get_uclk_dpm_states(void *handle,
3456 unsigned int *clock_values_in_khz,
3457 unsigned int *num_states)
3458 {
3459 struct smu_context *smu = handle;
3460 int ret = 0;
3461
3462 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3463 return -EOPNOTSUPP;
3464
3465 if (smu->ppt_funcs->get_uclk_dpm_states)
3466 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
3467
3468 return ret;
3469 }
3470
smu_get_current_power_state(void * handle)3471 static enum amd_pm_state_type smu_get_current_power_state(void *handle)
3472 {
3473 struct smu_context *smu = handle;
3474 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
3475
3476 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3477 return -EOPNOTSUPP;
3478
3479 if (smu->ppt_funcs->get_current_power_state)
3480 pm_state = smu->ppt_funcs->get_current_power_state(smu);
3481
3482 return pm_state;
3483 }
3484
smu_get_dpm_clock_table(void * handle,struct dpm_clocks * clock_table)3485 static int smu_get_dpm_clock_table(void *handle,
3486 struct dpm_clocks *clock_table)
3487 {
3488 struct smu_context *smu = handle;
3489 int ret = 0;
3490
3491 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3492 return -EOPNOTSUPP;
3493
3494 if (smu->ppt_funcs->get_dpm_clock_table)
3495 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
3496
3497 return ret;
3498 }
3499
smu_sys_get_gpu_metrics(void * handle,void ** table)3500 static ssize_t smu_sys_get_gpu_metrics(void *handle, void **table)
3501 {
3502 struct smu_context *smu = handle;
3503
3504 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3505 return -EOPNOTSUPP;
3506
3507 if (!smu->ppt_funcs->get_gpu_metrics)
3508 return -EOPNOTSUPP;
3509
3510 return smu->ppt_funcs->get_gpu_metrics(smu, table);
3511 }
3512
smu_sys_get_pm_metrics(void * handle,void * pm_metrics,size_t size)3513 static ssize_t smu_sys_get_pm_metrics(void *handle, void *pm_metrics,
3514 size_t size)
3515 {
3516 struct smu_context *smu = handle;
3517
3518 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3519 return -EOPNOTSUPP;
3520
3521 if (!smu->ppt_funcs->get_pm_metrics)
3522 return -EOPNOTSUPP;
3523
3524 return smu->ppt_funcs->get_pm_metrics(smu, pm_metrics, size);
3525 }
3526
smu_enable_mgpu_fan_boost(void * handle)3527 static int smu_enable_mgpu_fan_boost(void *handle)
3528 {
3529 struct smu_context *smu = handle;
3530 int ret = 0;
3531
3532 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
3533 return -EOPNOTSUPP;
3534
3535 if (smu->ppt_funcs->enable_mgpu_fan_boost)
3536 ret = smu->ppt_funcs->enable_mgpu_fan_boost(smu);
3537
3538 return ret;
3539 }
3540
smu_gfx_state_change_set(void * handle,uint32_t state)3541 static int smu_gfx_state_change_set(void *handle,
3542 uint32_t state)
3543 {
3544 struct smu_context *smu = handle;
3545 int ret = 0;
3546
3547 if (smu->ppt_funcs->gfx_state_change_set)
3548 ret = smu->ppt_funcs->gfx_state_change_set(smu, state);
3549
3550 return ret;
3551 }
3552
smu_handle_passthrough_sbr(struct smu_context * smu,bool enable)3553 int smu_handle_passthrough_sbr(struct smu_context *smu, bool enable)
3554 {
3555 int ret = 0;
3556
3557 if (smu->ppt_funcs->smu_handle_passthrough_sbr)
3558 ret = smu->ppt_funcs->smu_handle_passthrough_sbr(smu, enable);
3559
3560 return ret;
3561 }
3562
smu_get_ecc_info(struct smu_context * smu,void * umc_ecc)3563 int smu_get_ecc_info(struct smu_context *smu, void *umc_ecc)
3564 {
3565 int ret = -EOPNOTSUPP;
3566
3567 if (smu->ppt_funcs &&
3568 smu->ppt_funcs->get_ecc_info)
3569 ret = smu->ppt_funcs->get_ecc_info(smu, umc_ecc);
3570
3571 return ret;
3572
3573 }
3574
smu_get_prv_buffer_details(void * handle,void ** addr,size_t * size)3575 static int smu_get_prv_buffer_details(void *handle, void **addr, size_t *size)
3576 {
3577 struct smu_context *smu = handle;
3578 struct smu_table_context *smu_table = &smu->smu_table;
3579 struct smu_table *memory_pool = &smu_table->memory_pool;
3580
3581 if (!addr || !size)
3582 return -EINVAL;
3583
3584 *addr = NULL;
3585 *size = 0;
3586 if (memory_pool->bo) {
3587 *addr = memory_pool->cpu_addr;
3588 *size = memory_pool->size;
3589 }
3590
3591 return 0;
3592 }
3593
smu_print_dpm_policy(struct smu_dpm_policy * policy,char * sysbuf,size_t * size)3594 static void smu_print_dpm_policy(struct smu_dpm_policy *policy, char *sysbuf,
3595 size_t *size)
3596 {
3597 size_t offset = *size;
3598 int level;
3599
3600 for_each_set_bit(level, &policy->level_mask, PP_POLICY_MAX_LEVELS) {
3601 if (level == policy->current_level)
3602 offset += sysfs_emit_at(sysbuf, offset,
3603 "%d : %s*\n", level,
3604 policy->desc->get_desc(policy, level));
3605 else
3606 offset += sysfs_emit_at(sysbuf, offset,
3607 "%d : %s\n", level,
3608 policy->desc->get_desc(policy, level));
3609 }
3610
3611 *size = offset;
3612 }
3613
smu_get_pm_policy_info(struct smu_context * smu,enum pp_pm_policy p_type,char * sysbuf)3614 ssize_t smu_get_pm_policy_info(struct smu_context *smu,
3615 enum pp_pm_policy p_type, char *sysbuf)
3616 {
3617 struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3618 struct smu_dpm_policy_ctxt *policy_ctxt;
3619 struct smu_dpm_policy *dpm_policy;
3620 size_t offset = 0;
3621
3622 policy_ctxt = dpm_ctxt->dpm_policies;
3623 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||
3624 !policy_ctxt->policy_mask)
3625 return -EOPNOTSUPP;
3626
3627 if (p_type == PP_PM_POLICY_NONE)
3628 return -EINVAL;
3629
3630 dpm_policy = smu_get_pm_policy(smu, p_type);
3631 if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->desc)
3632 return -ENOENT;
3633
3634 if (!sysbuf)
3635 return -EINVAL;
3636
3637 smu_print_dpm_policy(dpm_policy, sysbuf, &offset);
3638
3639 return offset;
3640 }
3641
smu_get_pm_policy(struct smu_context * smu,enum pp_pm_policy p_type)3642 struct smu_dpm_policy *smu_get_pm_policy(struct smu_context *smu,
3643 enum pp_pm_policy p_type)
3644 {
3645 struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3646 struct smu_dpm_policy_ctxt *policy_ctxt;
3647 int i;
3648
3649 policy_ctxt = dpm_ctxt->dpm_policies;
3650 if (!policy_ctxt)
3651 return NULL;
3652
3653 for (i = 0; i < hweight32(policy_ctxt->policy_mask); ++i) {
3654 if (policy_ctxt->policies[i].policy_type == p_type)
3655 return &policy_ctxt->policies[i];
3656 }
3657
3658 return NULL;
3659 }
3660
smu_set_pm_policy(struct smu_context * smu,enum pp_pm_policy p_type,int level)3661 int smu_set_pm_policy(struct smu_context *smu, enum pp_pm_policy p_type,
3662 int level)
3663 {
3664 struct smu_dpm_context *dpm_ctxt = &smu->smu_dpm;
3665 struct smu_dpm_policy *dpm_policy = NULL;
3666 struct smu_dpm_policy_ctxt *policy_ctxt;
3667 int ret = -EOPNOTSUPP;
3668
3669 policy_ctxt = dpm_ctxt->dpm_policies;
3670 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled || !policy_ctxt ||
3671 !policy_ctxt->policy_mask)
3672 return ret;
3673
3674 if (level < 0 || level >= PP_POLICY_MAX_LEVELS)
3675 return -EINVAL;
3676
3677 dpm_policy = smu_get_pm_policy(smu, p_type);
3678
3679 if (!dpm_policy || !dpm_policy->level_mask || !dpm_policy->set_policy)
3680 return ret;
3681
3682 if (dpm_policy->current_level == level)
3683 return 0;
3684
3685 ret = dpm_policy->set_policy(smu, level);
3686
3687 if (!ret)
3688 dpm_policy->current_level = level;
3689
3690 return ret;
3691 }
3692
3693 static const struct amd_pm_funcs swsmu_pm_funcs = {
3694 /* export for sysfs */
3695 .set_fan_control_mode = smu_set_fan_control_mode,
3696 .get_fan_control_mode = smu_get_fan_control_mode,
3697 .set_fan_speed_pwm = smu_set_fan_speed_pwm,
3698 .get_fan_speed_pwm = smu_get_fan_speed_pwm,
3699 .force_clock_level = smu_force_ppclk_levels,
3700 .print_clock_levels = smu_print_ppclk_levels,
3701 .emit_clock_levels = smu_emit_ppclk_levels,
3702 .force_performance_level = smu_force_performance_level,
3703 .read_sensor = smu_read_sensor,
3704 .get_apu_thermal_limit = smu_get_apu_thermal_limit,
3705 .set_apu_thermal_limit = smu_set_apu_thermal_limit,
3706 .get_performance_level = smu_get_performance_level,
3707 .get_current_power_state = smu_get_current_power_state,
3708 .get_fan_speed_rpm = smu_get_fan_speed_rpm,
3709 .set_fan_speed_rpm = smu_set_fan_speed_rpm,
3710 .get_pp_num_states = smu_get_power_num_states,
3711 .get_pp_table = smu_sys_get_pp_table,
3712 .set_pp_table = smu_sys_set_pp_table,
3713 .switch_power_profile = smu_switch_power_profile,
3714 /* export to amdgpu */
3715 .dispatch_tasks = smu_handle_dpm_task,
3716 .load_firmware = smu_load_microcode,
3717 .set_powergating_by_smu = smu_dpm_set_power_gate,
3718 .set_power_limit = smu_set_power_limit,
3719 .get_power_limit = smu_get_power_limit,
3720 .get_power_profile_mode = smu_get_power_profile_mode,
3721 .set_power_profile_mode = smu_set_power_profile_mode,
3722 .odn_edit_dpm_table = smu_od_edit_dpm_table,
3723 .set_mp1_state = smu_set_mp1_state,
3724 .gfx_state_change_set = smu_gfx_state_change_set,
3725 /* export to DC */
3726 .get_sclk = smu_get_sclk,
3727 .get_mclk = smu_get_mclk,
3728 .display_configuration_change = smu_display_configuration_change,
3729 .get_clock_by_type_with_latency = smu_get_clock_by_type_with_latency,
3730 .display_clock_voltage_request = smu_display_clock_voltage_request,
3731 .enable_mgpu_fan_boost = smu_enable_mgpu_fan_boost,
3732 .set_active_display_count = smu_set_display_count,
3733 .set_min_deep_sleep_dcefclk = smu_set_deep_sleep_dcefclk,
3734 .get_asic_baco_capability = smu_get_baco_capability,
3735 .set_asic_baco_state = smu_baco_set_state,
3736 .get_ppfeature_status = smu_sys_get_pp_feature_mask,
3737 .set_ppfeature_status = smu_sys_set_pp_feature_mask,
3738 .asic_reset_mode_2 = smu_mode2_reset,
3739 .asic_reset_enable_gfx_features = smu_enable_gfx_features,
3740 .set_df_cstate = smu_set_df_cstate,
3741 .set_xgmi_pstate = smu_set_xgmi_pstate,
3742 .get_gpu_metrics = smu_sys_get_gpu_metrics,
3743 .get_pm_metrics = smu_sys_get_pm_metrics,
3744 .set_watermarks_for_clock_ranges = smu_set_watermarks_for_clock_ranges,
3745 .display_disable_memory_clock_switch = smu_display_disable_memory_clock_switch,
3746 .get_max_sustainable_clocks_by_dc = smu_get_max_sustainable_clocks_by_dc,
3747 .get_uclk_dpm_states = smu_get_uclk_dpm_states,
3748 .get_dpm_clock_table = smu_get_dpm_clock_table,
3749 .get_smu_prv_buf_details = smu_get_prv_buffer_details,
3750 };
3751
smu_wait_for_event(struct smu_context * smu,enum smu_event_type event,uint64_t event_arg)3752 int smu_wait_for_event(struct smu_context *smu, enum smu_event_type event,
3753 uint64_t event_arg)
3754 {
3755 int ret = -EINVAL;
3756
3757 if (smu->ppt_funcs->wait_for_event)
3758 ret = smu->ppt_funcs->wait_for_event(smu, event, event_arg);
3759
3760 return ret;
3761 }
3762
smu_stb_collect_info(struct smu_context * smu,void * buf,uint32_t size)3763 int smu_stb_collect_info(struct smu_context *smu, void *buf, uint32_t size)
3764 {
3765
3766 if (!smu->ppt_funcs->stb_collect_info || !smu->stb_context.enabled)
3767 return -EOPNOTSUPP;
3768
3769 /* Confirm the buffer allocated is of correct size */
3770 if (size != smu->stb_context.stb_buf_size)
3771 return -EINVAL;
3772
3773 /*
3774 * No need to lock smu mutex as we access STB directly through MMIO
3775 * and not going through SMU messaging route (for now at least).
3776 * For registers access rely on implementation internal locking.
3777 */
3778 return smu->ppt_funcs->stb_collect_info(smu, buf, size);
3779 }
3780
3781 #if defined(CONFIG_DEBUG_FS)
3782
smu_stb_debugfs_open(struct inode * inode,struct file * filp)3783 static int smu_stb_debugfs_open(struct inode *inode, struct file *filp)
3784 {
3785 struct amdgpu_device *adev = filp->f_inode->i_private;
3786 struct smu_context *smu = adev->powerplay.pp_handle;
3787 unsigned char *buf;
3788 int r;
3789
3790 buf = kvmalloc_array(smu->stb_context.stb_buf_size, sizeof(*buf), GFP_KERNEL);
3791 if (!buf)
3792 return -ENOMEM;
3793
3794 r = smu_stb_collect_info(smu, buf, smu->stb_context.stb_buf_size);
3795 if (r)
3796 goto out;
3797
3798 filp->private_data = buf;
3799
3800 return 0;
3801
3802 out:
3803 kvfree(buf);
3804 return r;
3805 }
3806
smu_stb_debugfs_read(struct file * filp,char __user * buf,size_t size,loff_t * pos)3807 static ssize_t smu_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
3808 loff_t *pos)
3809 {
3810 struct amdgpu_device *adev = filp->f_inode->i_private;
3811 struct smu_context *smu = adev->powerplay.pp_handle;
3812
3813
3814 if (!filp->private_data)
3815 return -EINVAL;
3816
3817 return simple_read_from_buffer(buf,
3818 size,
3819 pos, filp->private_data,
3820 smu->stb_context.stb_buf_size);
3821 }
3822
smu_stb_debugfs_release(struct inode * inode,struct file * filp)3823 static int smu_stb_debugfs_release(struct inode *inode, struct file *filp)
3824 {
3825 kvfree(filp->private_data);
3826 filp->private_data = NULL;
3827
3828 return 0;
3829 }
3830
3831 /*
3832 * We have to define not only read method but also
3833 * open and release because .read takes up to PAGE_SIZE
3834 * data each time so and so is invoked multiple times.
3835 * We allocate the STB buffer in .open and release it
3836 * in .release
3837 */
3838 static const struct file_operations smu_stb_debugfs_fops = {
3839 .owner = THIS_MODULE,
3840 .open = smu_stb_debugfs_open,
3841 .read = smu_stb_debugfs_read,
3842 .release = smu_stb_debugfs_release,
3843 .llseek = default_llseek,
3844 };
3845
3846 #endif
3847
amdgpu_smu_stb_debug_fs_init(struct amdgpu_device * adev)3848 void amdgpu_smu_stb_debug_fs_init(struct amdgpu_device *adev)
3849 {
3850 #if defined(CONFIG_DEBUG_FS)
3851
3852 struct smu_context *smu = adev->powerplay.pp_handle;
3853
3854 if (!smu || (!smu->stb_context.stb_buf_size))
3855 return;
3856
3857 debugfs_create_file_size("amdgpu_smu_stb_dump",
3858 S_IRUSR,
3859 adev_to_drm(adev)->primary->debugfs_root,
3860 adev,
3861 &smu_stb_debugfs_fops,
3862 smu->stb_context.stb_buf_size);
3863 #endif
3864 }
3865
smu_send_hbm_bad_pages_num(struct smu_context * smu,uint32_t size)3866 int smu_send_hbm_bad_pages_num(struct smu_context *smu, uint32_t size)
3867 {
3868 int ret = 0;
3869
3870 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_pages_num)
3871 ret = smu->ppt_funcs->send_hbm_bad_pages_num(smu, size);
3872
3873 return ret;
3874 }
3875
smu_send_hbm_bad_channel_flag(struct smu_context * smu,uint32_t size)3876 int smu_send_hbm_bad_channel_flag(struct smu_context *smu, uint32_t size)
3877 {
3878 int ret = 0;
3879
3880 if (smu->ppt_funcs && smu->ppt_funcs->send_hbm_bad_channel_flag)
3881 ret = smu->ppt_funcs->send_hbm_bad_channel_flag(smu, size);
3882
3883 return ret;
3884 }
3885
smu_send_rma_reason(struct smu_context * smu)3886 int smu_send_rma_reason(struct smu_context *smu)
3887 {
3888 int ret = 0;
3889
3890 if (smu->ppt_funcs && smu->ppt_funcs->send_rma_reason)
3891 ret = smu->ppt_funcs->send_rma_reason(smu);
3892
3893 return ret;
3894 }
3895