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