Loading...
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
28#include "amdgpu.h"
29#include "amdgpu_smu.h"
30#include "smu_internal.h"
31#include "atom.h"
32#include "arcturus_ppt.h"
33#include "navi10_ppt.h"
34#include "sienna_cichlid_ppt.h"
35#include "renoir_ppt.h"
36#include "amd_pcie.h"
37
38/*
39 * DO NOT use these for err/warn/info/debug messages.
40 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
41 * They are more MGPU friendly.
42 */
43#undef pr_err
44#undef pr_warn
45#undef pr_info
46#undef pr_debug
47
48size_t smu_sys_get_pp_feature_mask(struct smu_context *smu, char *buf)
49{
50 size_t size = 0;
51
52 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
53 return -EOPNOTSUPP;
54
55 mutex_lock(&smu->mutex);
56
57 size = smu_get_pp_feature_mask(smu, buf);
58
59 mutex_unlock(&smu->mutex);
60
61 return size;
62}
63
64int smu_sys_set_pp_feature_mask(struct smu_context *smu, uint64_t new_mask)
65{
66 int ret = 0;
67
68 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
69 return -EOPNOTSUPP;
70
71 mutex_lock(&smu->mutex);
72
73 ret = smu_set_pp_feature_mask(smu, new_mask);
74
75 mutex_unlock(&smu->mutex);
76
77 return ret;
78}
79
80int smu_get_status_gfxoff(struct amdgpu_device *adev, uint32_t *value)
81{
82 int ret = 0;
83 struct smu_context *smu = &adev->smu;
84
85 if (is_support_sw_smu(adev) && smu->ppt_funcs->get_gfx_off_status)
86 *value = smu_get_gfx_off_status(smu);
87 else
88 ret = -EINVAL;
89
90 return ret;
91}
92
93int smu_set_soft_freq_range(struct smu_context *smu,
94 enum smu_clk_type clk_type,
95 uint32_t min,
96 uint32_t max)
97{
98 int ret = 0;
99
100 mutex_lock(&smu->mutex);
101
102 if (smu->ppt_funcs->set_soft_freq_limited_range)
103 ret = smu->ppt_funcs->set_soft_freq_limited_range(smu,
104 clk_type,
105 min,
106 max);
107
108 mutex_unlock(&smu->mutex);
109
110 return ret;
111}
112
113int smu_get_dpm_freq_range(struct smu_context *smu,
114 enum smu_clk_type clk_type,
115 uint32_t *min,
116 uint32_t *max)
117{
118 int ret = 0;
119
120 if (!min && !max)
121 return -EINVAL;
122
123 mutex_lock(&smu->mutex);
124
125 if (smu->ppt_funcs->get_dpm_ultimate_freq)
126 ret = smu->ppt_funcs->get_dpm_ultimate_freq(smu,
127 clk_type,
128 min,
129 max);
130
131 mutex_unlock(&smu->mutex);
132
133 return ret;
134}
135
136static int smu_dpm_set_vcn_enable_locked(struct smu_context *smu,
137 bool enable)
138{
139 struct smu_power_context *smu_power = &smu->smu_power;
140 struct smu_power_gate *power_gate = &smu_power->power_gate;
141 int ret = 0;
142
143 if (!smu->ppt_funcs->dpm_set_vcn_enable)
144 return 0;
145
146 if (atomic_read(&power_gate->vcn_gated) ^ enable)
147 return 0;
148
149 ret = smu->ppt_funcs->dpm_set_vcn_enable(smu, enable);
150 if (!ret)
151 atomic_set(&power_gate->vcn_gated, !enable);
152
153 return ret;
154}
155
156static int smu_dpm_set_vcn_enable(struct smu_context *smu,
157 bool enable)
158{
159 struct smu_power_context *smu_power = &smu->smu_power;
160 struct smu_power_gate *power_gate = &smu_power->power_gate;
161 int ret = 0;
162
163 mutex_lock(&power_gate->vcn_gate_lock);
164
165 ret = smu_dpm_set_vcn_enable_locked(smu, enable);
166
167 mutex_unlock(&power_gate->vcn_gate_lock);
168
169 return ret;
170}
171
172static int smu_dpm_set_jpeg_enable_locked(struct smu_context *smu,
173 bool enable)
174{
175 struct smu_power_context *smu_power = &smu->smu_power;
176 struct smu_power_gate *power_gate = &smu_power->power_gate;
177 int ret = 0;
178
179 if (!smu->ppt_funcs->dpm_set_jpeg_enable)
180 return 0;
181
182 if (atomic_read(&power_gate->jpeg_gated) ^ enable)
183 return 0;
184
185 ret = smu->ppt_funcs->dpm_set_jpeg_enable(smu, enable);
186 if (!ret)
187 atomic_set(&power_gate->jpeg_gated, !enable);
188
189 return ret;
190}
191
192static int smu_dpm_set_jpeg_enable(struct smu_context *smu,
193 bool enable)
194{
195 struct smu_power_context *smu_power = &smu->smu_power;
196 struct smu_power_gate *power_gate = &smu_power->power_gate;
197 int ret = 0;
198
199 mutex_lock(&power_gate->jpeg_gate_lock);
200
201 ret = smu_dpm_set_jpeg_enable_locked(smu, enable);
202
203 mutex_unlock(&power_gate->jpeg_gate_lock);
204
205 return ret;
206}
207
208/**
209 * smu_dpm_set_power_gate - power gate/ungate the specific IP block
210 *
211 * @smu: smu_context pointer
212 * @block_type: the IP block to power gate/ungate
213 * @gate: to power gate if true, ungate otherwise
214 *
215 * This API uses no smu->mutex lock protection due to:
216 * 1. It is either called by other IP block(gfx/sdma/vcn/uvd/vce).
217 * This is guarded to be race condition free by the caller.
218 * 2. Or get called on user setting request of power_dpm_force_performance_level.
219 * Under this case, the smu->mutex lock protection is already enforced on
220 * the parent API smu_force_performance_level of the call path.
221 */
222int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
223 bool gate)
224{
225 int ret = 0;
226
227 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
228 return -EOPNOTSUPP;
229
230 switch (block_type) {
231 /*
232 * Some legacy code of amdgpu_vcn.c and vcn_v2*.c still uses
233 * AMD_IP_BLOCK_TYPE_UVD for VCN. So, here both of them are kept.
234 */
235 case AMD_IP_BLOCK_TYPE_UVD:
236 case AMD_IP_BLOCK_TYPE_VCN:
237 ret = smu_dpm_set_vcn_enable(smu, !gate);
238 if (ret)
239 dev_err(smu->adev->dev, "Failed to power %s VCN!\n",
240 gate ? "gate" : "ungate");
241 break;
242 case AMD_IP_BLOCK_TYPE_GFX:
243 ret = smu_gfx_off_control(smu, gate);
244 if (ret)
245 dev_err(smu->adev->dev, "Failed to %s gfxoff!\n",
246 gate ? "enable" : "disable");
247 break;
248 case AMD_IP_BLOCK_TYPE_SDMA:
249 ret = smu_powergate_sdma(smu, gate);
250 if (ret)
251 dev_err(smu->adev->dev, "Failed to power %s SDMA!\n",
252 gate ? "gate" : "ungate");
253 break;
254 case AMD_IP_BLOCK_TYPE_JPEG:
255 ret = smu_dpm_set_jpeg_enable(smu, !gate);
256 if (ret)
257 dev_err(smu->adev->dev, "Failed to power %s JPEG!\n",
258 gate ? "gate" : "ungate");
259 break;
260 default:
261 dev_err(smu->adev->dev, "Unsupported block type!\n");
262 return -EINVAL;
263 }
264
265 return ret;
266}
267
268int smu_get_power_num_states(struct smu_context *smu,
269 struct pp_states_info *state_info)
270{
271 if (!state_info)
272 return -EINVAL;
273
274 /* not support power state */
275 memset(state_info, 0, sizeof(struct pp_states_info));
276 state_info->nums = 1;
277 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
278
279 return 0;
280}
281
282bool is_support_sw_smu(struct amdgpu_device *adev)
283{
284 if (adev->asic_type >= CHIP_ARCTURUS)
285 return true;
286
287 return false;
288}
289
290int smu_sys_get_pp_table(struct smu_context *smu, void **table)
291{
292 struct smu_table_context *smu_table = &smu->smu_table;
293 uint32_t powerplay_table_size;
294
295 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
296 return -EOPNOTSUPP;
297
298 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
299 return -EINVAL;
300
301 mutex_lock(&smu->mutex);
302
303 if (smu_table->hardcode_pptable)
304 *table = smu_table->hardcode_pptable;
305 else
306 *table = smu_table->power_play_table;
307
308 powerplay_table_size = smu_table->power_play_table_size;
309
310 mutex_unlock(&smu->mutex);
311
312 return powerplay_table_size;
313}
314
315int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
316{
317 struct smu_table_context *smu_table = &smu->smu_table;
318 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
319 int ret = 0;
320
321 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
322 return -EOPNOTSUPP;
323
324 if (header->usStructureSize != size) {
325 dev_err(smu->adev->dev, "pp table size not matched !\n");
326 return -EIO;
327 }
328
329 mutex_lock(&smu->mutex);
330 if (!smu_table->hardcode_pptable)
331 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
332 if (!smu_table->hardcode_pptable) {
333 ret = -ENOMEM;
334 goto failed;
335 }
336
337 memcpy(smu_table->hardcode_pptable, buf, size);
338 smu_table->power_play_table = smu_table->hardcode_pptable;
339 smu_table->power_play_table_size = size;
340
341 /*
342 * Special hw_fini action(for Navi1x, the DPMs disablement will be
343 * skipped) may be needed for custom pptable uploading.
344 */
345 smu->uploading_custom_pp_table = true;
346
347 ret = smu_reset(smu);
348 if (ret)
349 dev_info(smu->adev->dev, "smu reset failed, ret = %d\n", ret);
350
351 smu->uploading_custom_pp_table = false;
352
353failed:
354 mutex_unlock(&smu->mutex);
355 return ret;
356}
357
358static int smu_get_driver_allowed_feature_mask(struct smu_context *smu)
359{
360 struct smu_feature *feature = &smu->smu_feature;
361 int ret = 0;
362 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
363
364 mutex_lock(&feature->mutex);
365 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
366 mutex_unlock(&feature->mutex);
367
368 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
369 SMU_FEATURE_MAX/32);
370 if (ret)
371 return ret;
372
373 mutex_lock(&feature->mutex);
374 bitmap_or(feature->allowed, feature->allowed,
375 (unsigned long *)allowed_feature_mask,
376 feature->feature_num);
377 mutex_unlock(&feature->mutex);
378
379 return ret;
380}
381
382static int smu_set_funcs(struct amdgpu_device *adev)
383{
384 struct smu_context *smu = &adev->smu;
385
386 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
387 smu->od_enabled = true;
388
389 switch (adev->asic_type) {
390 case CHIP_NAVI10:
391 case CHIP_NAVI14:
392 case CHIP_NAVI12:
393 navi10_set_ppt_funcs(smu);
394 break;
395 case CHIP_ARCTURUS:
396 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
397 arcturus_set_ppt_funcs(smu);
398 /* OD is not supported on Arcturus */
399 smu->od_enabled =false;
400 break;
401 case CHIP_SIENNA_CICHLID:
402 case CHIP_NAVY_FLOUNDER:
403 sienna_cichlid_set_ppt_funcs(smu);
404 break;
405 case CHIP_RENOIR:
406 renoir_set_ppt_funcs(smu);
407 break;
408 default:
409 return -EINVAL;
410 }
411
412 return 0;
413}
414
415static int smu_early_init(void *handle)
416{
417 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
418 struct smu_context *smu = &adev->smu;
419
420 smu->adev = adev;
421 smu->pm_enabled = !!amdgpu_dpm;
422 smu->is_apu = false;
423 mutex_init(&smu->mutex);
424
425 return smu_set_funcs(adev);
426}
427
428static int smu_set_default_dpm_table(struct smu_context *smu)
429{
430 struct smu_power_context *smu_power = &smu->smu_power;
431 struct smu_power_gate *power_gate = &smu_power->power_gate;
432 int vcn_gate, jpeg_gate;
433 int ret = 0;
434
435 if (!smu->ppt_funcs->set_default_dpm_table)
436 return 0;
437
438 mutex_lock(&power_gate->vcn_gate_lock);
439 mutex_lock(&power_gate->jpeg_gate_lock);
440
441 vcn_gate = atomic_read(&power_gate->vcn_gated);
442 jpeg_gate = atomic_read(&power_gate->jpeg_gated);
443
444 ret = smu_dpm_set_vcn_enable_locked(smu, true);
445 if (ret)
446 goto err0_out;
447
448 ret = smu_dpm_set_jpeg_enable_locked(smu, true);
449 if (ret)
450 goto err1_out;
451
452 ret = smu->ppt_funcs->set_default_dpm_table(smu);
453 if (ret)
454 dev_err(smu->adev->dev,
455 "Failed to setup default dpm clock tables!\n");
456
457 smu_dpm_set_jpeg_enable_locked(smu, !jpeg_gate);
458err1_out:
459 smu_dpm_set_vcn_enable_locked(smu, !vcn_gate);
460err0_out:
461 mutex_unlock(&power_gate->jpeg_gate_lock);
462 mutex_unlock(&power_gate->vcn_gate_lock);
463
464 return ret;
465}
466
467static int smu_late_init(void *handle)
468{
469 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
470 struct smu_context *smu = &adev->smu;
471 int ret = 0;
472
473 if (!smu->pm_enabled)
474 return 0;
475
476 ret = smu_set_default_od_settings(smu);
477 if (ret) {
478 dev_err(adev->dev, "Failed to setup default OD settings!\n");
479 return ret;
480 }
481
482 ret = smu_populate_umd_state_clk(smu);
483 if (ret) {
484 dev_err(adev->dev, "Failed to populate UMD state clocks!\n");
485 return ret;
486 }
487
488 ret = smu_get_asic_power_limits(smu);
489 if (ret) {
490 dev_err(adev->dev, "Failed to get asic power limits!\n");
491 return ret;
492 }
493
494 smu_get_unique_id(smu);
495
496 smu_handle_task(&adev->smu,
497 smu->smu_dpm.dpm_level,
498 AMD_PP_TASK_COMPLETE_INIT,
499 false);
500
501 return 0;
502}
503
504static int smu_init_fb_allocations(struct smu_context *smu)
505{
506 struct amdgpu_device *adev = smu->adev;
507 struct smu_table_context *smu_table = &smu->smu_table;
508 struct smu_table *tables = smu_table->tables;
509 struct smu_table *driver_table = &(smu_table->driver_table);
510 uint32_t max_table_size = 0;
511 int ret, i;
512
513 /* VRAM allocation for tool table */
514 if (tables[SMU_TABLE_PMSTATUSLOG].size) {
515 ret = amdgpu_bo_create_kernel(adev,
516 tables[SMU_TABLE_PMSTATUSLOG].size,
517 tables[SMU_TABLE_PMSTATUSLOG].align,
518 tables[SMU_TABLE_PMSTATUSLOG].domain,
519 &tables[SMU_TABLE_PMSTATUSLOG].bo,
520 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
521 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
522 if (ret) {
523 dev_err(adev->dev, "VRAM allocation for tool table failed!\n");
524 return ret;
525 }
526 }
527
528 /* VRAM allocation for driver table */
529 for (i = 0; i < SMU_TABLE_COUNT; i++) {
530 if (tables[i].size == 0)
531 continue;
532
533 if (i == SMU_TABLE_PMSTATUSLOG)
534 continue;
535
536 if (max_table_size < tables[i].size)
537 max_table_size = tables[i].size;
538 }
539
540 driver_table->size = max_table_size;
541 driver_table->align = PAGE_SIZE;
542 driver_table->domain = AMDGPU_GEM_DOMAIN_VRAM;
543
544 ret = amdgpu_bo_create_kernel(adev,
545 driver_table->size,
546 driver_table->align,
547 driver_table->domain,
548 &driver_table->bo,
549 &driver_table->mc_address,
550 &driver_table->cpu_addr);
551 if (ret) {
552 dev_err(adev->dev, "VRAM allocation for driver table failed!\n");
553 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
554 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
555 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
556 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
557 }
558
559 return ret;
560}
561
562static int smu_fini_fb_allocations(struct smu_context *smu)
563{
564 struct smu_table_context *smu_table = &smu->smu_table;
565 struct smu_table *tables = smu_table->tables;
566 struct smu_table *driver_table = &(smu_table->driver_table);
567
568 if (!tables)
569 return 0;
570
571 if (tables[SMU_TABLE_PMSTATUSLOG].mc_address)
572 amdgpu_bo_free_kernel(&tables[SMU_TABLE_PMSTATUSLOG].bo,
573 &tables[SMU_TABLE_PMSTATUSLOG].mc_address,
574 &tables[SMU_TABLE_PMSTATUSLOG].cpu_addr);
575
576 amdgpu_bo_free_kernel(&driver_table->bo,
577 &driver_table->mc_address,
578 &driver_table->cpu_addr);
579
580 return 0;
581}
582
583/**
584 * smu_alloc_memory_pool - allocate memory pool in the system memory
585 *
586 * @smu: amdgpu_device pointer
587 *
588 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
589 * and DramLogSetDramAddr can notify it changed.
590 *
591 * Returns 0 on success, error on failure.
592 */
593static int smu_alloc_memory_pool(struct smu_context *smu)
594{
595 struct amdgpu_device *adev = smu->adev;
596 struct smu_table_context *smu_table = &smu->smu_table;
597 struct smu_table *memory_pool = &smu_table->memory_pool;
598 uint64_t pool_size = smu->pool_size;
599 int ret = 0;
600
601 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
602 return ret;
603
604 memory_pool->size = pool_size;
605 memory_pool->align = PAGE_SIZE;
606 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
607
608 switch (pool_size) {
609 case SMU_MEMORY_POOL_SIZE_256_MB:
610 case SMU_MEMORY_POOL_SIZE_512_MB:
611 case SMU_MEMORY_POOL_SIZE_1_GB:
612 case SMU_MEMORY_POOL_SIZE_2_GB:
613 ret = amdgpu_bo_create_kernel(adev,
614 memory_pool->size,
615 memory_pool->align,
616 memory_pool->domain,
617 &memory_pool->bo,
618 &memory_pool->mc_address,
619 &memory_pool->cpu_addr);
620 if (ret)
621 dev_err(adev->dev, "VRAM allocation for dramlog failed!\n");
622 break;
623 default:
624 break;
625 }
626
627 return ret;
628}
629
630static int smu_free_memory_pool(struct smu_context *smu)
631{
632 struct smu_table_context *smu_table = &smu->smu_table;
633 struct smu_table *memory_pool = &smu_table->memory_pool;
634
635 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
636 return 0;
637
638 amdgpu_bo_free_kernel(&memory_pool->bo,
639 &memory_pool->mc_address,
640 &memory_pool->cpu_addr);
641
642 memset(memory_pool, 0, sizeof(struct smu_table));
643
644 return 0;
645}
646
647static int smu_smc_table_sw_init(struct smu_context *smu)
648{
649 int ret;
650
651 /**
652 * Create smu_table structure, and init smc tables such as
653 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
654 */
655 ret = smu_init_smc_tables(smu);
656 if (ret) {
657 dev_err(smu->adev->dev, "Failed to init smc tables!\n");
658 return ret;
659 }
660
661 /**
662 * Create smu_power_context structure, and allocate smu_dpm_context and
663 * context size to fill the smu_power_context data.
664 */
665 ret = smu_init_power(smu);
666 if (ret) {
667 dev_err(smu->adev->dev, "Failed to init smu_init_power!\n");
668 return ret;
669 }
670
671 /*
672 * allocate vram bos to store smc table contents.
673 */
674 ret = smu_init_fb_allocations(smu);
675 if (ret)
676 return ret;
677
678 ret = smu_alloc_memory_pool(smu);
679 if (ret)
680 return ret;
681
682 ret = smu_i2c_init(smu, &smu->adev->pm.smu_i2c);
683 if (ret)
684 return ret;
685
686 return 0;
687}
688
689static int smu_smc_table_sw_fini(struct smu_context *smu)
690{
691 int ret;
692
693 smu_i2c_fini(smu, &smu->adev->pm.smu_i2c);
694
695 ret = smu_free_memory_pool(smu);
696 if (ret)
697 return ret;
698
699 ret = smu_fini_fb_allocations(smu);
700 if (ret)
701 return ret;
702
703 ret = smu_fini_power(smu);
704 if (ret) {
705 dev_err(smu->adev->dev, "Failed to init smu_fini_power!\n");
706 return ret;
707 }
708
709 ret = smu_fini_smc_tables(smu);
710 if (ret) {
711 dev_err(smu->adev->dev, "Failed to smu_fini_smc_tables!\n");
712 return ret;
713 }
714
715 return 0;
716}
717
718static void smu_throttling_logging_work_fn(struct work_struct *work)
719{
720 struct smu_context *smu = container_of(work, struct smu_context,
721 throttling_logging_work);
722
723 smu_log_thermal_throttling(smu);
724}
725
726static int smu_sw_init(void *handle)
727{
728 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
729 struct smu_context *smu = &adev->smu;
730 int ret;
731
732 smu->pool_size = adev->pm.smu_prv_buffer_size;
733 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
734 mutex_init(&smu->smu_feature.mutex);
735 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
736 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
737 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
738
739 mutex_init(&smu->smu_baco.mutex);
740 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
741 smu->smu_baco.platform_support = false;
742
743 mutex_init(&smu->sensor_lock);
744 mutex_init(&smu->metrics_lock);
745 mutex_init(&smu->message_lock);
746
747 INIT_WORK(&smu->throttling_logging_work, smu_throttling_logging_work_fn);
748 smu->watermarks_bitmap = 0;
749 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
750 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
751
752 atomic_set(&smu->smu_power.power_gate.vcn_gated, 1);
753 atomic_set(&smu->smu_power.power_gate.jpeg_gated, 1);
754 mutex_init(&smu->smu_power.power_gate.vcn_gate_lock);
755 mutex_init(&smu->smu_power.power_gate.jpeg_gate_lock);
756
757 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
758 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
759 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
760 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
761 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
762 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
763 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
764 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
765
766 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
767 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
768 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
769 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
770 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
771 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
772 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
773 smu->display_config = &adev->pm.pm_display_cfg;
774
775 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
776 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
777 ret = smu_init_microcode(smu);
778 if (ret) {
779 dev_err(adev->dev, "Failed to load smu firmware!\n");
780 return ret;
781 }
782
783 ret = smu_smc_table_sw_init(smu);
784 if (ret) {
785 dev_err(adev->dev, "Failed to sw init smc table!\n");
786 return ret;
787 }
788
789 ret = smu_register_irq_handler(smu);
790 if (ret) {
791 dev_err(adev->dev, "Failed to register smc irq handler!\n");
792 return ret;
793 }
794
795 return 0;
796}
797
798static int smu_sw_fini(void *handle)
799{
800 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
801 struct smu_context *smu = &adev->smu;
802 int ret;
803
804 ret = smu_smc_table_sw_fini(smu);
805 if (ret) {
806 dev_err(adev->dev, "Failed to sw fini smc table!\n");
807 return ret;
808 }
809
810 smu_fini_microcode(smu);
811
812 return 0;
813}
814
815static int smu_get_thermal_temperature_range(struct smu_context *smu)
816{
817 struct amdgpu_device *adev = smu->adev;
818 struct smu_temperature_range *range =
819 &smu->thermal_range;
820 int ret = 0;
821
822 if (!smu->ppt_funcs->get_thermal_temperature_range)
823 return 0;
824
825 ret = smu->ppt_funcs->get_thermal_temperature_range(smu, range);
826 if (ret)
827 return ret;
828
829 adev->pm.dpm.thermal.min_temp = range->min;
830 adev->pm.dpm.thermal.max_temp = range->max;
831 adev->pm.dpm.thermal.max_edge_emergency_temp = range->edge_emergency_max;
832 adev->pm.dpm.thermal.min_hotspot_temp = range->hotspot_min;
833 adev->pm.dpm.thermal.max_hotspot_crit_temp = range->hotspot_crit_max;
834 adev->pm.dpm.thermal.max_hotspot_emergency_temp = range->hotspot_emergency_max;
835 adev->pm.dpm.thermal.min_mem_temp = range->mem_min;
836 adev->pm.dpm.thermal.max_mem_crit_temp = range->mem_crit_max;
837 adev->pm.dpm.thermal.max_mem_emergency_temp = range->mem_emergency_max;
838
839 return ret;
840}
841
842static int smu_smc_hw_setup(struct smu_context *smu)
843{
844 struct amdgpu_device *adev = smu->adev;
845 uint32_t pcie_gen = 0, pcie_width = 0;
846 int ret;
847
848 if (adev->in_suspend && smu_is_dpm_running(smu)) {
849 dev_info(adev->dev, "dpm has been enabled\n");
850 return 0;
851 }
852
853 ret = smu_init_display_count(smu, 0);
854 if (ret) {
855 dev_info(adev->dev, "Failed to pre-set display count as 0!\n");
856 return ret;
857 }
858
859 ret = smu_set_driver_table_location(smu);
860 if (ret) {
861 dev_err(adev->dev, "Failed to SetDriverDramAddr!\n");
862 return ret;
863 }
864
865 /*
866 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
867 */
868 ret = smu_set_tool_table_location(smu);
869 if (ret) {
870 dev_err(adev->dev, "Failed to SetToolsDramAddr!\n");
871 return ret;
872 }
873
874 /*
875 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
876 * pool location.
877 */
878 ret = smu_notify_memory_pool_location(smu);
879 if (ret) {
880 dev_err(adev->dev, "Failed to SetDramLogDramAddr!\n");
881 return ret;
882 }
883
884 /* smu_dump_pptable(smu); */
885 /*
886 * Copy pptable bo in the vram to smc with SMU MSGs such as
887 * SetDriverDramAddr and TransferTableDram2Smu.
888 */
889 ret = smu_write_pptable(smu);
890 if (ret) {
891 dev_err(adev->dev, "Failed to transfer pptable to SMC!\n");
892 return ret;
893 }
894
895 /* issue Run*Btc msg */
896 ret = smu_run_btc(smu);
897 if (ret)
898 return ret;
899
900 ret = smu_feature_set_allowed_mask(smu);
901 if (ret) {
902 dev_err(adev->dev, "Failed to set driver allowed features mask!\n");
903 return ret;
904 }
905
906 ret = smu_system_features_control(smu, true);
907 if (ret) {
908 dev_err(adev->dev, "Failed to enable requested dpm features!\n");
909 return ret;
910 }
911
912 if (!smu_is_dpm_running(smu))
913 dev_info(adev->dev, "dpm has been disabled\n");
914
915 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
916 pcie_gen = 3;
917 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
918 pcie_gen = 2;
919 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
920 pcie_gen = 1;
921 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
922 pcie_gen = 0;
923
924 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
925 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
926 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
927 */
928 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
929 pcie_width = 6;
930 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
931 pcie_width = 5;
932 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
933 pcie_width = 4;
934 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
935 pcie_width = 3;
936 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
937 pcie_width = 2;
938 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
939 pcie_width = 1;
940 ret = smu_update_pcie_parameters(smu, pcie_gen, pcie_width);
941 if (ret) {
942 dev_err(adev->dev, "Attempt to override pcie params failed!\n");
943 return ret;
944 }
945
946 ret = smu_get_thermal_temperature_range(smu);
947 if (ret) {
948 dev_err(adev->dev, "Failed to get thermal temperature ranges!\n");
949 return ret;
950 }
951
952 ret = smu_enable_thermal_alert(smu);
953 if (ret) {
954 dev_err(adev->dev, "Failed to enable thermal alert!\n");
955 return ret;
956 }
957
958 ret = smu_disable_umc_cdr_12gbps_workaround(smu);
959 if (ret) {
960 dev_err(adev->dev, "Workaround failed to disable UMC CDR feature on 12Gbps SKU!\n");
961 return ret;
962 }
963
964 /*
965 * For Navi1X, manually switch it to AC mode as PMFW
966 * may boot it with DC mode.
967 */
968 ret = smu_set_power_source(smu,
969 adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
970 SMU_POWER_SOURCE_DC);
971 if (ret) {
972 dev_err(adev->dev, "Failed to switch to %s mode!\n", adev->pm.ac_power ? "AC" : "DC");
973 return ret;
974 }
975
976 /*
977 * Set initialized values (get from vbios) to dpm tables context such as
978 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
979 * type of clks.
980 */
981 ret = smu_set_default_dpm_table(smu);
982 if (ret) {
983 dev_err(adev->dev, "Failed to setup default dpm clock tables!\n");
984 return ret;
985 }
986
987 ret = smu_notify_display_change(smu);
988 if (ret)
989 return ret;
990
991 /*
992 * Set min deep sleep dce fclk with bootup value from vbios via
993 * SetMinDeepSleepDcefclk MSG.
994 */
995 ret = smu_set_min_dcef_deep_sleep(smu,
996 smu->smu_table.boot_values.dcefclk / 100);
997 if (ret)
998 return ret;
999
1000 return ret;
1001}
1002
1003static int smu_start_smc_engine(struct smu_context *smu)
1004{
1005 struct amdgpu_device *adev = smu->adev;
1006 int ret = 0;
1007
1008 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1009 if (adev->asic_type < CHIP_NAVI10) {
1010 if (smu->ppt_funcs->load_microcode) {
1011 ret = smu->ppt_funcs->load_microcode(smu);
1012 if (ret)
1013 return ret;
1014 }
1015 }
1016 }
1017
1018 if (smu->ppt_funcs->check_fw_status) {
1019 ret = smu->ppt_funcs->check_fw_status(smu);
1020 if (ret) {
1021 dev_err(adev->dev, "SMC is not ready\n");
1022 return ret;
1023 }
1024 }
1025
1026 /*
1027 * Send msg GetDriverIfVersion to check if the return value is equal
1028 * with DRIVER_IF_VERSION of smc header.
1029 */
1030 ret = smu_check_fw_version(smu);
1031 if (ret)
1032 return ret;
1033
1034 return ret;
1035}
1036
1037static int smu_hw_init(void *handle)
1038{
1039 int ret;
1040 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1041 struct smu_context *smu = &adev->smu;
1042
1043 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
1044 smu->pm_enabled = false;
1045 return 0;
1046 }
1047
1048 ret = smu_start_smc_engine(smu);
1049 if (ret) {
1050 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1051 return ret;
1052 }
1053
1054 if (smu->is_apu) {
1055 smu_powergate_sdma(&adev->smu, false);
1056 smu_dpm_set_vcn_enable(smu, true);
1057 smu_dpm_set_jpeg_enable(smu, true);
1058 smu_set_gfx_cgpg(&adev->smu, true);
1059 }
1060
1061 if (!smu->pm_enabled)
1062 return 0;
1063
1064 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1065 ret = smu_get_vbios_bootup_values(smu);
1066 if (ret) {
1067 dev_err(adev->dev, "Failed to get VBIOS boot clock values!\n");
1068 return ret;
1069 }
1070
1071 ret = smu_setup_pptable(smu);
1072 if (ret) {
1073 dev_err(adev->dev, "Failed to setup pptable!\n");
1074 return ret;
1075 }
1076
1077 ret = smu_get_driver_allowed_feature_mask(smu);
1078 if (ret)
1079 return ret;
1080
1081 ret = smu_smc_hw_setup(smu);
1082 if (ret) {
1083 dev_err(adev->dev, "Failed to setup smc hw!\n");
1084 return ret;
1085 }
1086
1087 /*
1088 * Move maximum sustainable clock retrieving here considering
1089 * 1. It is not needed on resume(from S3).
1090 * 2. DAL settings come between .hw_init and .late_init of SMU.
1091 * And DAL needs to know the maximum sustainable clocks. Thus
1092 * it cannot be put in .late_init().
1093 */
1094 ret = smu_init_max_sustainable_clocks(smu);
1095 if (ret) {
1096 dev_err(adev->dev, "Failed to init max sustainable clocks!\n");
1097 return ret;
1098 }
1099
1100 adev->pm.dpm_enabled = true;
1101
1102 dev_info(adev->dev, "SMU is initialized successfully!\n");
1103
1104 return 0;
1105}
1106
1107static int smu_disable_dpms(struct smu_context *smu)
1108{
1109 struct amdgpu_device *adev = smu->adev;
1110 int ret = 0;
1111 bool use_baco = !smu->is_apu &&
1112 ((adev->in_gpu_reset &&
1113 (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO)) ||
1114 ((adev->in_runpm || adev->in_hibernate) && amdgpu_asic_supports_baco(adev)));
1115
1116 /*
1117 * For custom pptable uploading, skip the DPM features
1118 * disable process on Navi1x ASICs.
1119 * - As the gfx related features are under control of
1120 * RLC on those ASICs. RLC reinitialization will be
1121 * needed to reenable them. That will cost much more
1122 * efforts.
1123 *
1124 * - SMU firmware can handle the DPM reenablement
1125 * properly.
1126 */
1127 if (smu->uploading_custom_pp_table &&
1128 (adev->asic_type >= CHIP_NAVI10) &&
1129 (adev->asic_type <= CHIP_NAVY_FLOUNDER))
1130 return 0;
1131
1132 /*
1133 * For Sienna_Cichlid, PMFW will handle the features disablement properly
1134 * on BACO in. Driver involvement is unnecessary.
1135 */
1136 if ((adev->asic_type == CHIP_SIENNA_CICHLID) &&
1137 use_baco)
1138 return 0;
1139
1140 /*
1141 * For gpu reset, runpm and hibernation through BACO,
1142 * BACO feature has to be kept enabled.
1143 */
1144 if (use_baco && smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT)) {
1145 ret = smu_disable_all_features_with_exception(smu,
1146 SMU_FEATURE_BACO_BIT);
1147 if (ret)
1148 dev_err(adev->dev, "Failed to disable smu features except BACO.\n");
1149 } else {
1150 ret = smu_system_features_control(smu, false);
1151 if (ret)
1152 dev_err(adev->dev, "Failed to disable smu features.\n");
1153 }
1154
1155 if (adev->asic_type >= CHIP_NAVI10 &&
1156 adev->gfx.rlc.funcs->stop)
1157 adev->gfx.rlc.funcs->stop(adev);
1158
1159 return ret;
1160}
1161
1162static int smu_smc_hw_cleanup(struct smu_context *smu)
1163{
1164 struct amdgpu_device *adev = smu->adev;
1165 int ret = 0;
1166
1167 cancel_work_sync(&smu->throttling_logging_work);
1168
1169 ret = smu_disable_thermal_alert(smu);
1170 if (ret) {
1171 dev_err(adev->dev, "Fail to disable thermal alert!\n");
1172 return ret;
1173 }
1174
1175 ret = smu_disable_dpms(smu);
1176 if (ret) {
1177 dev_err(adev->dev, "Fail to disable dpm features!\n");
1178 return ret;
1179 }
1180
1181 return 0;
1182}
1183
1184static int smu_hw_fini(void *handle)
1185{
1186 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1187 struct smu_context *smu = &adev->smu;
1188 int ret = 0;
1189
1190 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1191 return 0;
1192
1193 if (smu->is_apu) {
1194 smu_powergate_sdma(&adev->smu, true);
1195 smu_dpm_set_vcn_enable(smu, false);
1196 smu_dpm_set_jpeg_enable(smu, false);
1197 }
1198
1199 if (!smu->pm_enabled)
1200 return 0;
1201
1202 adev->pm.dpm_enabled = false;
1203
1204 ret = smu_smc_hw_cleanup(smu);
1205 if (ret)
1206 return ret;
1207
1208 return 0;
1209}
1210
1211int smu_reset(struct smu_context *smu)
1212{
1213 struct amdgpu_device *adev = smu->adev;
1214 int ret;
1215
1216 amdgpu_gfx_off_ctrl(smu->adev, false);
1217
1218 ret = smu_hw_fini(adev);
1219 if (ret)
1220 return ret;
1221
1222 ret = smu_hw_init(adev);
1223 if (ret)
1224 return ret;
1225
1226 ret = smu_late_init(adev);
1227 if (ret)
1228 return ret;
1229
1230 amdgpu_gfx_off_ctrl(smu->adev, true);
1231
1232 return 0;
1233}
1234
1235static int smu_suspend(void *handle)
1236{
1237 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1238 struct smu_context *smu = &adev->smu;
1239 int ret;
1240
1241 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1242 return 0;
1243
1244 if (!smu->pm_enabled)
1245 return 0;
1246
1247 adev->pm.dpm_enabled = false;
1248
1249 ret = smu_smc_hw_cleanup(smu);
1250 if (ret)
1251 return ret;
1252
1253 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1254
1255 if (smu->is_apu)
1256 smu_set_gfx_cgpg(&adev->smu, false);
1257
1258 return 0;
1259}
1260
1261static int smu_resume(void *handle)
1262{
1263 int ret;
1264 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1265 struct smu_context *smu = &adev->smu;
1266
1267 if (amdgpu_sriov_vf(adev)&& !amdgpu_sriov_is_pp_one_vf(adev))
1268 return 0;
1269
1270 if (!smu->pm_enabled)
1271 return 0;
1272
1273 dev_info(adev->dev, "SMU is resuming...\n");
1274
1275 ret = smu_start_smc_engine(smu);
1276 if (ret) {
1277 dev_err(adev->dev, "SMC engine is not correctly up!\n");
1278 return ret;
1279 }
1280
1281 ret = smu_smc_hw_setup(smu);
1282 if (ret) {
1283 dev_err(adev->dev, "Failed to setup smc hw!\n");
1284 return ret;
1285 }
1286
1287 if (smu->is_apu)
1288 smu_set_gfx_cgpg(&adev->smu, true);
1289
1290 smu->disable_uclk_switch = 0;
1291
1292 adev->pm.dpm_enabled = true;
1293
1294 dev_info(adev->dev, "SMU is resumed successfully!\n");
1295
1296 return 0;
1297}
1298
1299int smu_display_configuration_change(struct smu_context *smu,
1300 const struct amd_pp_display_configuration *display_config)
1301{
1302 int index = 0;
1303 int num_of_active_display = 0;
1304
1305 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1306 return -EOPNOTSUPP;
1307
1308 if (!display_config)
1309 return -EINVAL;
1310
1311 mutex_lock(&smu->mutex);
1312
1313 smu_set_min_dcef_deep_sleep(smu,
1314 display_config->min_dcef_deep_sleep_set_clk / 100);
1315
1316 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1317 if (display_config->displays[index].controller_id != 0)
1318 num_of_active_display++;
1319 }
1320
1321 smu_set_active_display_count(smu, num_of_active_display);
1322
1323 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1324 display_config->cpu_cc6_disable,
1325 display_config->cpu_pstate_disable,
1326 display_config->nb_pstate_switch_disable);
1327
1328 mutex_unlock(&smu->mutex);
1329
1330 return 0;
1331}
1332
1333static int smu_get_clock_info(struct smu_context *smu,
1334 struct smu_clock_info *clk_info,
1335 enum smu_perf_level_designation designation)
1336{
1337 int ret;
1338 struct smu_performance_level level = {0};
1339
1340 if (!clk_info)
1341 return -EINVAL;
1342
1343 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1344 if (ret)
1345 return -EINVAL;
1346
1347 clk_info->min_mem_clk = level.memory_clock;
1348 clk_info->min_eng_clk = level.core_clock;
1349 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1350
1351 ret = smu_get_perf_level(smu, designation, &level);
1352 if (ret)
1353 return -EINVAL;
1354
1355 clk_info->min_mem_clk = level.memory_clock;
1356 clk_info->min_eng_clk = level.core_clock;
1357 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1358
1359 return 0;
1360}
1361
1362int smu_get_current_clocks(struct smu_context *smu,
1363 struct amd_pp_clock_info *clocks)
1364{
1365 struct amd_pp_simple_clock_info simple_clocks = {0};
1366 struct smu_clock_info hw_clocks;
1367 int ret = 0;
1368
1369 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1370 return -EOPNOTSUPP;
1371
1372 mutex_lock(&smu->mutex);
1373
1374 smu_get_dal_power_level(smu, &simple_clocks);
1375
1376 if (smu->support_power_containment)
1377 ret = smu_get_clock_info(smu, &hw_clocks,
1378 PERF_LEVEL_POWER_CONTAINMENT);
1379 else
1380 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1381
1382 if (ret) {
1383 dev_err(smu->adev->dev, "Error in smu_get_clock_info\n");
1384 goto failed;
1385 }
1386
1387 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1388 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1389 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1390 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1391 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1392 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1393 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1394 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1395
1396 if (simple_clocks.level == 0)
1397 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1398 else
1399 clocks->max_clocks_state = simple_clocks.level;
1400
1401 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1402 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1403 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1404 }
1405
1406failed:
1407 mutex_unlock(&smu->mutex);
1408 return ret;
1409}
1410
1411static int smu_set_clockgating_state(void *handle,
1412 enum amd_clockgating_state state)
1413{
1414 return 0;
1415}
1416
1417static int smu_set_powergating_state(void *handle,
1418 enum amd_powergating_state state)
1419{
1420 return 0;
1421}
1422
1423static int smu_enable_umd_pstate(void *handle,
1424 enum amd_dpm_forced_level *level)
1425{
1426 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1427 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1428 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1429 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1430
1431 struct smu_context *smu = (struct smu_context*)(handle);
1432 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1433
1434 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1435 return -EINVAL;
1436
1437 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1438 /* enter umd pstate, save current level, disable gfx cg*/
1439 if (*level & profile_mode_mask) {
1440 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1441 smu_dpm_ctx->enable_umd_pstate = true;
1442 amdgpu_device_ip_set_powergating_state(smu->adev,
1443 AMD_IP_BLOCK_TYPE_GFX,
1444 AMD_PG_STATE_UNGATE);
1445 amdgpu_device_ip_set_clockgating_state(smu->adev,
1446 AMD_IP_BLOCK_TYPE_GFX,
1447 AMD_CG_STATE_UNGATE);
1448 }
1449 } else {
1450 /* exit umd pstate, restore level, enable gfx cg*/
1451 if (!(*level & profile_mode_mask)) {
1452 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1453 *level = smu_dpm_ctx->saved_dpm_level;
1454 smu_dpm_ctx->enable_umd_pstate = false;
1455 amdgpu_device_ip_set_clockgating_state(smu->adev,
1456 AMD_IP_BLOCK_TYPE_GFX,
1457 AMD_CG_STATE_GATE);
1458 amdgpu_device_ip_set_powergating_state(smu->adev,
1459 AMD_IP_BLOCK_TYPE_GFX,
1460 AMD_PG_STATE_GATE);
1461 }
1462 }
1463
1464 return 0;
1465}
1466
1467static int smu_adjust_power_state_dynamic(struct smu_context *smu,
1468 enum amd_dpm_forced_level level,
1469 bool skip_display_settings)
1470{
1471 int ret = 0;
1472 int index = 0;
1473 long workload;
1474 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1475
1476 if (!skip_display_settings) {
1477 ret = smu_display_config_changed(smu);
1478 if (ret) {
1479 dev_err(smu->adev->dev, "Failed to change display config!");
1480 return ret;
1481 }
1482 }
1483
1484 ret = smu_apply_clocks_adjust_rules(smu);
1485 if (ret) {
1486 dev_err(smu->adev->dev, "Failed to apply clocks adjust rules!");
1487 return ret;
1488 }
1489
1490 if (!skip_display_settings) {
1491 ret = smu_notify_smc_display_config(smu);
1492 if (ret) {
1493 dev_err(smu->adev->dev, "Failed to notify smc display config!");
1494 return ret;
1495 }
1496 }
1497
1498 if (smu_dpm_ctx->dpm_level != level) {
1499 ret = smu_asic_set_performance_level(smu, level);
1500 if (ret) {
1501 dev_err(smu->adev->dev, "Failed to set performance level!");
1502 return ret;
1503 }
1504
1505 /* update the saved copy */
1506 smu_dpm_ctx->dpm_level = level;
1507 }
1508
1509 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1510 index = fls(smu->workload_mask);
1511 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1512 workload = smu->workload_setting[index];
1513
1514 if (smu->power_profile_mode != workload)
1515 smu_set_power_profile_mode(smu, &workload, 0, false);
1516 }
1517
1518 return ret;
1519}
1520
1521int smu_handle_task(struct smu_context *smu,
1522 enum amd_dpm_forced_level level,
1523 enum amd_pp_task task_id,
1524 bool lock_needed)
1525{
1526 int ret = 0;
1527
1528 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1529 return -EOPNOTSUPP;
1530
1531 if (lock_needed)
1532 mutex_lock(&smu->mutex);
1533
1534 switch (task_id) {
1535 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1536 ret = smu_pre_display_config_changed(smu);
1537 if (ret)
1538 goto out;
1539 ret = smu_set_cpu_power_state(smu);
1540 if (ret)
1541 goto out;
1542 ret = smu_adjust_power_state_dynamic(smu, level, false);
1543 break;
1544 case AMD_PP_TASK_COMPLETE_INIT:
1545 case AMD_PP_TASK_READJUST_POWER_STATE:
1546 ret = smu_adjust_power_state_dynamic(smu, level, true);
1547 break;
1548 default:
1549 break;
1550 }
1551
1552out:
1553 if (lock_needed)
1554 mutex_unlock(&smu->mutex);
1555
1556 return ret;
1557}
1558
1559int smu_switch_power_profile(struct smu_context *smu,
1560 enum PP_SMC_POWER_PROFILE type,
1561 bool en)
1562{
1563 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1564 long workload;
1565 uint32_t index;
1566
1567 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1568 return -EOPNOTSUPP;
1569
1570 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1571 return -EINVAL;
1572
1573 mutex_lock(&smu->mutex);
1574
1575 if (!en) {
1576 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1577 index = fls(smu->workload_mask);
1578 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1579 workload = smu->workload_setting[index];
1580 } else {
1581 smu->workload_mask |= (1 << smu->workload_prority[type]);
1582 index = fls(smu->workload_mask);
1583 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1584 workload = smu->workload_setting[index];
1585 }
1586
1587 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1588 smu_set_power_profile_mode(smu, &workload, 0, false);
1589
1590 mutex_unlock(&smu->mutex);
1591
1592 return 0;
1593}
1594
1595enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1596{
1597 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1598 enum amd_dpm_forced_level level;
1599
1600 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1601 return -EOPNOTSUPP;
1602
1603 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1604 return -EINVAL;
1605
1606 mutex_lock(&(smu->mutex));
1607 level = smu_dpm_ctx->dpm_level;
1608 mutex_unlock(&(smu->mutex));
1609
1610 return level;
1611}
1612
1613int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1614{
1615 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1616 int ret = 0;
1617
1618 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1619 return -EOPNOTSUPP;
1620
1621 if (!smu->is_apu && !smu_dpm_ctx->dpm_context)
1622 return -EINVAL;
1623
1624 mutex_lock(&smu->mutex);
1625
1626 ret = smu_enable_umd_pstate(smu, &level);
1627 if (ret) {
1628 mutex_unlock(&smu->mutex);
1629 return ret;
1630 }
1631
1632 ret = smu_handle_task(smu, level,
1633 AMD_PP_TASK_READJUST_POWER_STATE,
1634 false);
1635
1636 mutex_unlock(&smu->mutex);
1637
1638 return ret;
1639}
1640
1641int smu_set_display_count(struct smu_context *smu, uint32_t count)
1642{
1643 int ret = 0;
1644
1645 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1646 return -EOPNOTSUPP;
1647
1648 mutex_lock(&smu->mutex);
1649 ret = smu_init_display_count(smu, count);
1650 mutex_unlock(&smu->mutex);
1651
1652 return ret;
1653}
1654
1655int smu_force_clk_levels(struct smu_context *smu,
1656 enum smu_clk_type clk_type,
1657 uint32_t mask)
1658{
1659 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1660 int ret = 0;
1661
1662 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1663 return -EOPNOTSUPP;
1664
1665 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1666 dev_dbg(smu->adev->dev, "force clock level is for dpm manual mode only.\n");
1667 return -EINVAL;
1668 }
1669
1670 mutex_lock(&smu->mutex);
1671
1672 if (smu->ppt_funcs && smu->ppt_funcs->force_clk_levels)
1673 ret = smu->ppt_funcs->force_clk_levels(smu, clk_type, mask);
1674
1675 mutex_unlock(&smu->mutex);
1676
1677 return ret;
1678}
1679
1680/*
1681 * On system suspending or resetting, the dpm_enabled
1682 * flag will be cleared. So that those SMU services which
1683 * are not supported will be gated.
1684 * However, the mp1 state setting should still be granted
1685 * even if the dpm_enabled cleared.
1686 */
1687int smu_set_mp1_state(struct smu_context *smu,
1688 enum pp_mp1_state mp1_state)
1689{
1690 uint16_t msg;
1691 int ret;
1692
1693 if (!smu->pm_enabled)
1694 return -EOPNOTSUPP;
1695
1696 mutex_lock(&smu->mutex);
1697
1698 switch (mp1_state) {
1699 case PP_MP1_STATE_SHUTDOWN:
1700 msg = SMU_MSG_PrepareMp1ForShutdown;
1701 break;
1702 case PP_MP1_STATE_UNLOAD:
1703 msg = SMU_MSG_PrepareMp1ForUnload;
1704 break;
1705 case PP_MP1_STATE_RESET:
1706 msg = SMU_MSG_PrepareMp1ForReset;
1707 break;
1708 case PP_MP1_STATE_NONE:
1709 default:
1710 mutex_unlock(&smu->mutex);
1711 return 0;
1712 }
1713
1714 ret = smu_send_smc_msg(smu, msg, NULL);
1715 /* some asics may not support those messages */
1716 if (ret == -EINVAL)
1717 ret = 0;
1718 if (ret)
1719 dev_err(smu->adev->dev, "[PrepareMp1] Failed!\n");
1720
1721 mutex_unlock(&smu->mutex);
1722
1723 return ret;
1724}
1725
1726int smu_set_df_cstate(struct smu_context *smu,
1727 enum pp_df_cstate state)
1728{
1729 int ret = 0;
1730
1731 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1732 return -EOPNOTSUPP;
1733
1734 if (!smu->ppt_funcs || !smu->ppt_funcs->set_df_cstate)
1735 return 0;
1736
1737 mutex_lock(&smu->mutex);
1738
1739 ret = smu->ppt_funcs->set_df_cstate(smu, state);
1740 if (ret)
1741 dev_err(smu->adev->dev, "[SetDfCstate] failed!\n");
1742
1743 mutex_unlock(&smu->mutex);
1744
1745 return ret;
1746}
1747
1748int smu_allow_xgmi_power_down(struct smu_context *smu, bool en)
1749{
1750 int ret = 0;
1751
1752 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1753 return -EOPNOTSUPP;
1754
1755 if (!smu->ppt_funcs || !smu->ppt_funcs->allow_xgmi_power_down)
1756 return 0;
1757
1758 mutex_lock(&smu->mutex);
1759
1760 ret = smu->ppt_funcs->allow_xgmi_power_down(smu, en);
1761 if (ret)
1762 dev_err(smu->adev->dev, "[AllowXgmiPowerDown] failed!\n");
1763
1764 mutex_unlock(&smu->mutex);
1765
1766 return ret;
1767}
1768
1769int smu_write_watermarks_table(struct smu_context *smu)
1770{
1771 int ret = 0;
1772
1773 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1774 return -EOPNOTSUPP;
1775
1776 mutex_lock(&smu->mutex);
1777
1778 ret = smu_set_watermarks_table(smu, NULL);
1779
1780 mutex_unlock(&smu->mutex);
1781
1782 return ret;
1783}
1784
1785int smu_set_watermarks_for_clock_ranges(struct smu_context *smu,
1786 struct dm_pp_wm_sets_with_clock_ranges_soc15 *clock_ranges)
1787{
1788 int ret = 0;
1789
1790 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1791 return -EOPNOTSUPP;
1792
1793 mutex_lock(&smu->mutex);
1794
1795 if (!smu->disable_watermark &&
1796 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_DCEFCLK_BIT) &&
1797 smu_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT)) {
1798 ret = smu_set_watermarks_table(smu, clock_ranges);
1799
1800 if (!(smu->watermarks_bitmap & WATERMARKS_EXIST)) {
1801 smu->watermarks_bitmap |= WATERMARKS_EXIST;
1802 smu->watermarks_bitmap &= ~WATERMARKS_LOADED;
1803 }
1804 }
1805
1806 mutex_unlock(&smu->mutex);
1807
1808 return ret;
1809}
1810
1811int smu_set_ac_dc(struct smu_context *smu)
1812{
1813 int ret = 0;
1814
1815 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1816 return -EOPNOTSUPP;
1817
1818 /* controlled by firmware */
1819 if (smu->dc_controlled_by_gpio)
1820 return 0;
1821
1822 mutex_lock(&smu->mutex);
1823 ret = smu_set_power_source(smu,
1824 smu->adev->pm.ac_power ? SMU_POWER_SOURCE_AC :
1825 SMU_POWER_SOURCE_DC);
1826 if (ret)
1827 dev_err(smu->adev->dev, "Failed to switch to %s mode!\n",
1828 smu->adev->pm.ac_power ? "AC" : "DC");
1829 mutex_unlock(&smu->mutex);
1830
1831 return ret;
1832}
1833
1834const struct amd_ip_funcs smu_ip_funcs = {
1835 .name = "smu",
1836 .early_init = smu_early_init,
1837 .late_init = smu_late_init,
1838 .sw_init = smu_sw_init,
1839 .sw_fini = smu_sw_fini,
1840 .hw_init = smu_hw_init,
1841 .hw_fini = smu_hw_fini,
1842 .suspend = smu_suspend,
1843 .resume = smu_resume,
1844 .is_idle = NULL,
1845 .check_soft_reset = NULL,
1846 .wait_for_idle = NULL,
1847 .soft_reset = NULL,
1848 .set_clockgating_state = smu_set_clockgating_state,
1849 .set_powergating_state = smu_set_powergating_state,
1850 .enable_umd_pstate = smu_enable_umd_pstate,
1851};
1852
1853const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1854{
1855 .type = AMD_IP_BLOCK_TYPE_SMC,
1856 .major = 11,
1857 .minor = 0,
1858 .rev = 0,
1859 .funcs = &smu_ip_funcs,
1860};
1861
1862const struct amdgpu_ip_block_version smu_v12_0_ip_block =
1863{
1864 .type = AMD_IP_BLOCK_TYPE_SMC,
1865 .major = 12,
1866 .minor = 0,
1867 .rev = 0,
1868 .funcs = &smu_ip_funcs,
1869};
1870
1871int smu_load_microcode(struct smu_context *smu)
1872{
1873 int ret = 0;
1874
1875 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1876 return -EOPNOTSUPP;
1877
1878 mutex_lock(&smu->mutex);
1879
1880 if (smu->ppt_funcs->load_microcode)
1881 ret = smu->ppt_funcs->load_microcode(smu);
1882
1883 mutex_unlock(&smu->mutex);
1884
1885 return ret;
1886}
1887
1888int smu_check_fw_status(struct smu_context *smu)
1889{
1890 int ret = 0;
1891
1892 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1893 return -EOPNOTSUPP;
1894
1895 mutex_lock(&smu->mutex);
1896
1897 if (smu->ppt_funcs->check_fw_status)
1898 ret = smu->ppt_funcs->check_fw_status(smu);
1899
1900 mutex_unlock(&smu->mutex);
1901
1902 return ret;
1903}
1904
1905int smu_set_gfx_cgpg(struct smu_context *smu, bool enabled)
1906{
1907 int ret = 0;
1908
1909 mutex_lock(&smu->mutex);
1910
1911 if (smu->ppt_funcs->set_gfx_cgpg)
1912 ret = smu->ppt_funcs->set_gfx_cgpg(smu, enabled);
1913
1914 mutex_unlock(&smu->mutex);
1915
1916 return ret;
1917}
1918
1919int smu_set_fan_speed_rpm(struct smu_context *smu, uint32_t speed)
1920{
1921 int ret = 0;
1922
1923 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1924 return -EOPNOTSUPP;
1925
1926 mutex_lock(&smu->mutex);
1927
1928 if (smu->ppt_funcs->set_fan_speed_rpm)
1929 ret = smu->ppt_funcs->set_fan_speed_rpm(smu, speed);
1930
1931 mutex_unlock(&smu->mutex);
1932
1933 return ret;
1934}
1935
1936int smu_get_power_limit(struct smu_context *smu,
1937 uint32_t *limit,
1938 bool max_setting)
1939{
1940 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1941 return -EOPNOTSUPP;
1942
1943 mutex_lock(&smu->mutex);
1944
1945 *limit = (max_setting ? smu->max_power_limit : smu->current_power_limit);
1946
1947 mutex_unlock(&smu->mutex);
1948
1949 return 0;
1950}
1951
1952int smu_set_power_limit(struct smu_context *smu, uint32_t limit)
1953{
1954 int ret = 0;
1955
1956 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1957 return -EOPNOTSUPP;
1958
1959 mutex_lock(&smu->mutex);
1960
1961 if (limit > smu->max_power_limit) {
1962 dev_err(smu->adev->dev,
1963 "New power limit (%d) is over the max allowed %d\n",
1964 limit, smu->max_power_limit);
1965 goto out;
1966 }
1967
1968 if (!limit)
1969 limit = smu->current_power_limit;
1970
1971 if (smu->ppt_funcs->set_power_limit)
1972 ret = smu->ppt_funcs->set_power_limit(smu, limit);
1973
1974out:
1975 mutex_unlock(&smu->mutex);
1976
1977 return ret;
1978}
1979
1980int smu_print_clk_levels(struct smu_context *smu, enum smu_clk_type clk_type, char *buf)
1981{
1982 int ret = 0;
1983
1984 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
1985 return -EOPNOTSUPP;
1986
1987 mutex_lock(&smu->mutex);
1988
1989 if (smu->ppt_funcs->print_clk_levels)
1990 ret = smu->ppt_funcs->print_clk_levels(smu, clk_type, buf);
1991
1992 mutex_unlock(&smu->mutex);
1993
1994 return ret;
1995}
1996
1997int smu_get_od_percentage(struct smu_context *smu, enum smu_clk_type type)
1998{
1999 int ret = 0;
2000
2001 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2002 return -EOPNOTSUPP;
2003
2004 mutex_lock(&smu->mutex);
2005
2006 if (smu->ppt_funcs->get_od_percentage)
2007 ret = smu->ppt_funcs->get_od_percentage(smu, type);
2008
2009 mutex_unlock(&smu->mutex);
2010
2011 return ret;
2012}
2013
2014int smu_set_od_percentage(struct smu_context *smu, enum smu_clk_type type, uint32_t value)
2015{
2016 int ret = 0;
2017
2018 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2019 return -EOPNOTSUPP;
2020
2021 mutex_lock(&smu->mutex);
2022
2023 if (smu->ppt_funcs->set_od_percentage)
2024 ret = smu->ppt_funcs->set_od_percentage(smu, type, value);
2025
2026 mutex_unlock(&smu->mutex);
2027
2028 return ret;
2029}
2030
2031int smu_od_edit_dpm_table(struct smu_context *smu,
2032 enum PP_OD_DPM_TABLE_COMMAND type,
2033 long *input, uint32_t size)
2034{
2035 int ret = 0;
2036
2037 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2038 return -EOPNOTSUPP;
2039
2040 mutex_lock(&smu->mutex);
2041
2042 if (smu->ppt_funcs->od_edit_dpm_table) {
2043 ret = smu->ppt_funcs->od_edit_dpm_table(smu, type, input, size);
2044 if (!ret && (type == PP_OD_COMMIT_DPM_TABLE))
2045 ret = smu_handle_task(smu,
2046 smu->smu_dpm.dpm_level,
2047 AMD_PP_TASK_READJUST_POWER_STATE,
2048 false);
2049 }
2050
2051 mutex_unlock(&smu->mutex);
2052
2053 return ret;
2054}
2055
2056int smu_read_sensor(struct smu_context *smu,
2057 enum amd_pp_sensors sensor,
2058 void *data, uint32_t *size)
2059{
2060 struct smu_umd_pstate_table *pstate_table =
2061 &smu->pstate_table;
2062 int ret = 0;
2063
2064 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2065 return -EOPNOTSUPP;
2066
2067 if (!data || !size)
2068 return -EINVAL;
2069
2070 mutex_lock(&smu->mutex);
2071
2072 if (smu->ppt_funcs->read_sensor)
2073 if (!smu->ppt_funcs->read_sensor(smu, sensor, data, size))
2074 goto unlock;
2075
2076 switch (sensor) {
2077 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
2078 *((uint32_t *)data) = pstate_table->gfxclk_pstate.standard * 100;
2079 *size = 4;
2080 break;
2081 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
2082 *((uint32_t *)data) = pstate_table->uclk_pstate.standard * 100;
2083 *size = 4;
2084 break;
2085 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
2086 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
2087 *size = 8;
2088 break;
2089 case AMDGPU_PP_SENSOR_UVD_POWER:
2090 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
2091 *size = 4;
2092 break;
2093 case AMDGPU_PP_SENSOR_VCE_POWER:
2094 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
2095 *size = 4;
2096 break;
2097 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
2098 *(uint32_t *)data = atomic_read(&smu->smu_power.power_gate.vcn_gated) ? 0: 1;
2099 *size = 4;
2100 break;
2101 case AMDGPU_PP_SENSOR_MIN_FAN_RPM:
2102 *(uint32_t *)data = 0;
2103 *size = 4;
2104 break;
2105 default:
2106 *size = 0;
2107 ret = -EOPNOTSUPP;
2108 break;
2109 }
2110
2111unlock:
2112 mutex_unlock(&smu->mutex);
2113
2114 return ret;
2115}
2116
2117int smu_get_power_profile_mode(struct smu_context *smu, char *buf)
2118{
2119 int ret = 0;
2120
2121 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2122 return -EOPNOTSUPP;
2123
2124 mutex_lock(&smu->mutex);
2125
2126 if (smu->ppt_funcs->get_power_profile_mode)
2127 ret = smu->ppt_funcs->get_power_profile_mode(smu, buf);
2128
2129 mutex_unlock(&smu->mutex);
2130
2131 return ret;
2132}
2133
2134int smu_set_power_profile_mode(struct smu_context *smu,
2135 long *param,
2136 uint32_t param_size,
2137 bool lock_needed)
2138{
2139 int ret = 0;
2140
2141 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2142 return -EOPNOTSUPP;
2143
2144 if (lock_needed)
2145 mutex_lock(&smu->mutex);
2146
2147 if (smu->ppt_funcs->set_power_profile_mode)
2148 ret = smu->ppt_funcs->set_power_profile_mode(smu, param, param_size);
2149
2150 if (lock_needed)
2151 mutex_unlock(&smu->mutex);
2152
2153 return ret;
2154}
2155
2156
2157int smu_get_fan_control_mode(struct smu_context *smu)
2158{
2159 int ret = 0;
2160
2161 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2162 return -EOPNOTSUPP;
2163
2164 mutex_lock(&smu->mutex);
2165
2166 if (smu->ppt_funcs->get_fan_control_mode)
2167 ret = smu->ppt_funcs->get_fan_control_mode(smu);
2168
2169 mutex_unlock(&smu->mutex);
2170
2171 return ret;
2172}
2173
2174int smu_set_fan_control_mode(struct smu_context *smu, int value)
2175{
2176 int ret = 0;
2177
2178 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2179 return -EOPNOTSUPP;
2180
2181 mutex_lock(&smu->mutex);
2182
2183 if (smu->ppt_funcs->set_fan_control_mode)
2184 ret = smu->ppt_funcs->set_fan_control_mode(smu, value);
2185
2186 mutex_unlock(&smu->mutex);
2187
2188 return ret;
2189}
2190
2191int smu_get_fan_speed_percent(struct smu_context *smu, uint32_t *speed)
2192{
2193 int ret = 0;
2194
2195 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2196 return -EOPNOTSUPP;
2197
2198 mutex_lock(&smu->mutex);
2199
2200 if (smu->ppt_funcs->get_fan_speed_percent)
2201 ret = smu->ppt_funcs->get_fan_speed_percent(smu, speed);
2202
2203 mutex_unlock(&smu->mutex);
2204
2205 return ret;
2206}
2207
2208int smu_set_fan_speed_percent(struct smu_context *smu, uint32_t speed)
2209{
2210 int ret = 0;
2211
2212 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2213 return -EOPNOTSUPP;
2214
2215 mutex_lock(&smu->mutex);
2216
2217 if (smu->ppt_funcs->set_fan_speed_percent)
2218 ret = smu->ppt_funcs->set_fan_speed_percent(smu, speed);
2219
2220 mutex_unlock(&smu->mutex);
2221
2222 return ret;
2223}
2224
2225int smu_get_fan_speed_rpm(struct smu_context *smu, uint32_t *speed)
2226{
2227 int ret = 0;
2228
2229 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2230 return -EOPNOTSUPP;
2231
2232 mutex_lock(&smu->mutex);
2233
2234 if (smu->ppt_funcs->get_fan_speed_rpm)
2235 ret = smu->ppt_funcs->get_fan_speed_rpm(smu, speed);
2236
2237 mutex_unlock(&smu->mutex);
2238
2239 return ret;
2240}
2241
2242int smu_set_deep_sleep_dcefclk(struct smu_context *smu, int clk)
2243{
2244 int ret = 0;
2245
2246 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2247 return -EOPNOTSUPP;
2248
2249 mutex_lock(&smu->mutex);
2250
2251 ret = smu_set_min_dcef_deep_sleep(smu, clk);
2252
2253 mutex_unlock(&smu->mutex);
2254
2255 return ret;
2256}
2257
2258int smu_set_active_display_count(struct smu_context *smu, uint32_t count)
2259{
2260 int ret = 0;
2261
2262 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2263 return -EOPNOTSUPP;
2264
2265 if (smu->ppt_funcs->set_active_display_count)
2266 ret = smu->ppt_funcs->set_active_display_count(smu, count);
2267
2268 return ret;
2269}
2270
2271int smu_get_clock_by_type(struct smu_context *smu,
2272 enum amd_pp_clock_type type,
2273 struct amd_pp_clocks *clocks)
2274{
2275 int ret = 0;
2276
2277 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2278 return -EOPNOTSUPP;
2279
2280 mutex_lock(&smu->mutex);
2281
2282 if (smu->ppt_funcs->get_clock_by_type)
2283 ret = smu->ppt_funcs->get_clock_by_type(smu, type, clocks);
2284
2285 mutex_unlock(&smu->mutex);
2286
2287 return ret;
2288}
2289
2290int smu_get_max_high_clocks(struct smu_context *smu,
2291 struct amd_pp_simple_clock_info *clocks)
2292{
2293 int ret = 0;
2294
2295 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2296 return -EOPNOTSUPP;
2297
2298 mutex_lock(&smu->mutex);
2299
2300 if (smu->ppt_funcs->get_max_high_clocks)
2301 ret = smu->ppt_funcs->get_max_high_clocks(smu, clocks);
2302
2303 mutex_unlock(&smu->mutex);
2304
2305 return ret;
2306}
2307
2308int smu_get_clock_by_type_with_latency(struct smu_context *smu,
2309 enum smu_clk_type clk_type,
2310 struct pp_clock_levels_with_latency *clocks)
2311{
2312 int ret = 0;
2313
2314 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2315 return -EOPNOTSUPP;
2316
2317 mutex_lock(&smu->mutex);
2318
2319 if (smu->ppt_funcs->get_clock_by_type_with_latency)
2320 ret = smu->ppt_funcs->get_clock_by_type_with_latency(smu, clk_type, clocks);
2321
2322 mutex_unlock(&smu->mutex);
2323
2324 return ret;
2325}
2326
2327int smu_get_clock_by_type_with_voltage(struct smu_context *smu,
2328 enum amd_pp_clock_type type,
2329 struct pp_clock_levels_with_voltage *clocks)
2330{
2331 int ret = 0;
2332
2333 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2334 return -EOPNOTSUPP;
2335
2336 mutex_lock(&smu->mutex);
2337
2338 if (smu->ppt_funcs->get_clock_by_type_with_voltage)
2339 ret = smu->ppt_funcs->get_clock_by_type_with_voltage(smu, type, clocks);
2340
2341 mutex_unlock(&smu->mutex);
2342
2343 return ret;
2344}
2345
2346
2347int smu_display_clock_voltage_request(struct smu_context *smu,
2348 struct pp_display_clock_request *clock_req)
2349{
2350 int ret = 0;
2351
2352 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2353 return -EOPNOTSUPP;
2354
2355 mutex_lock(&smu->mutex);
2356
2357 if (smu->ppt_funcs->display_clock_voltage_request)
2358 ret = smu->ppt_funcs->display_clock_voltage_request(smu, clock_req);
2359
2360 mutex_unlock(&smu->mutex);
2361
2362 return ret;
2363}
2364
2365
2366int smu_display_disable_memory_clock_switch(struct smu_context *smu, bool disable_memory_clock_switch)
2367{
2368 int ret = -EINVAL;
2369
2370 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2371 return -EOPNOTSUPP;
2372
2373 mutex_lock(&smu->mutex);
2374
2375 if (smu->ppt_funcs->display_disable_memory_clock_switch)
2376 ret = smu->ppt_funcs->display_disable_memory_clock_switch(smu, disable_memory_clock_switch);
2377
2378 mutex_unlock(&smu->mutex);
2379
2380 return ret;
2381}
2382
2383int smu_notify_smu_enable_pwe(struct smu_context *smu)
2384{
2385 int ret = 0;
2386
2387 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2388 return -EOPNOTSUPP;
2389
2390 mutex_lock(&smu->mutex);
2391
2392 if (smu->ppt_funcs->notify_smu_enable_pwe)
2393 ret = smu->ppt_funcs->notify_smu_enable_pwe(smu);
2394
2395 mutex_unlock(&smu->mutex);
2396
2397 return ret;
2398}
2399
2400int smu_set_xgmi_pstate(struct smu_context *smu,
2401 uint32_t pstate)
2402{
2403 int ret = 0;
2404
2405 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2406 return -EOPNOTSUPP;
2407
2408 mutex_lock(&smu->mutex);
2409
2410 if (smu->ppt_funcs->set_xgmi_pstate)
2411 ret = smu->ppt_funcs->set_xgmi_pstate(smu, pstate);
2412
2413 mutex_unlock(&smu->mutex);
2414
2415 if(ret)
2416 dev_err(smu->adev->dev, "Failed to set XGMI pstate!\n");
2417
2418 return ret;
2419}
2420
2421int smu_set_azalia_d3_pme(struct smu_context *smu)
2422{
2423 int ret = 0;
2424
2425 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2426 return -EOPNOTSUPP;
2427
2428 mutex_lock(&smu->mutex);
2429
2430 if (smu->ppt_funcs->set_azalia_d3_pme)
2431 ret = smu->ppt_funcs->set_azalia_d3_pme(smu);
2432
2433 mutex_unlock(&smu->mutex);
2434
2435 return ret;
2436}
2437
2438/*
2439 * On system suspending or resetting, the dpm_enabled
2440 * flag will be cleared. So that those SMU services which
2441 * are not supported will be gated.
2442 *
2443 * However, the baco/mode1 reset should still be granted
2444 * as they are still supported and necessary.
2445 */
2446bool smu_baco_is_support(struct smu_context *smu)
2447{
2448 bool ret = false;
2449
2450 if (!smu->pm_enabled)
2451 return false;
2452
2453 mutex_lock(&smu->mutex);
2454
2455 if (smu->ppt_funcs && smu->ppt_funcs->baco_is_support)
2456 ret = smu->ppt_funcs->baco_is_support(smu);
2457
2458 mutex_unlock(&smu->mutex);
2459
2460 return ret;
2461}
2462
2463int smu_baco_get_state(struct smu_context *smu, enum smu_baco_state *state)
2464{
2465 if (smu->ppt_funcs->baco_get_state)
2466 return -EINVAL;
2467
2468 mutex_lock(&smu->mutex);
2469 *state = smu->ppt_funcs->baco_get_state(smu);
2470 mutex_unlock(&smu->mutex);
2471
2472 return 0;
2473}
2474
2475int smu_baco_enter(struct smu_context *smu)
2476{
2477 int ret = 0;
2478
2479 if (!smu->pm_enabled)
2480 return -EOPNOTSUPP;
2481
2482 mutex_lock(&smu->mutex);
2483
2484 if (smu->ppt_funcs->baco_enter)
2485 ret = smu->ppt_funcs->baco_enter(smu);
2486
2487 mutex_unlock(&smu->mutex);
2488
2489 if (ret)
2490 dev_err(smu->adev->dev, "Failed to enter BACO state!\n");
2491
2492 return ret;
2493}
2494
2495int smu_baco_exit(struct smu_context *smu)
2496{
2497 int ret = 0;
2498
2499 if (!smu->pm_enabled)
2500 return -EOPNOTSUPP;
2501
2502 mutex_lock(&smu->mutex);
2503
2504 if (smu->ppt_funcs->baco_exit)
2505 ret = smu->ppt_funcs->baco_exit(smu);
2506
2507 mutex_unlock(&smu->mutex);
2508
2509 if (ret)
2510 dev_err(smu->adev->dev, "Failed to exit BACO state!\n");
2511
2512 return ret;
2513}
2514
2515bool smu_mode1_reset_is_support(struct smu_context *smu)
2516{
2517 bool ret = false;
2518
2519 if (!smu->pm_enabled)
2520 return false;
2521
2522 mutex_lock(&smu->mutex);
2523
2524 if (smu->ppt_funcs && smu->ppt_funcs->mode1_reset_is_support)
2525 ret = smu->ppt_funcs->mode1_reset_is_support(smu);
2526
2527 mutex_unlock(&smu->mutex);
2528
2529 return ret;
2530}
2531
2532int smu_mode1_reset(struct smu_context *smu)
2533{
2534 int ret = 0;
2535
2536 if (!smu->pm_enabled)
2537 return -EOPNOTSUPP;
2538
2539 mutex_lock(&smu->mutex);
2540
2541 if (smu->ppt_funcs->mode1_reset)
2542 ret = smu->ppt_funcs->mode1_reset(smu);
2543
2544 mutex_unlock(&smu->mutex);
2545
2546 return ret;
2547}
2548
2549int smu_mode2_reset(struct smu_context *smu)
2550{
2551 int ret = 0;
2552
2553 if (!smu->pm_enabled)
2554 return -EOPNOTSUPP;
2555
2556 mutex_lock(&smu->mutex);
2557
2558 if (smu->ppt_funcs->mode2_reset)
2559 ret = smu->ppt_funcs->mode2_reset(smu);
2560
2561 mutex_unlock(&smu->mutex);
2562
2563 if (ret)
2564 dev_err(smu->adev->dev, "Mode2 reset failed!\n");
2565
2566 return ret;
2567}
2568
2569int smu_get_max_sustainable_clocks_by_dc(struct smu_context *smu,
2570 struct pp_smu_nv_clock_table *max_clocks)
2571{
2572 int ret = 0;
2573
2574 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2575 return -EOPNOTSUPP;
2576
2577 mutex_lock(&smu->mutex);
2578
2579 if (smu->ppt_funcs->get_max_sustainable_clocks_by_dc)
2580 ret = smu->ppt_funcs->get_max_sustainable_clocks_by_dc(smu, max_clocks);
2581
2582 mutex_unlock(&smu->mutex);
2583
2584 return ret;
2585}
2586
2587int smu_get_uclk_dpm_states(struct smu_context *smu,
2588 unsigned int *clock_values_in_khz,
2589 unsigned int *num_states)
2590{
2591 int ret = 0;
2592
2593 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2594 return -EOPNOTSUPP;
2595
2596 mutex_lock(&smu->mutex);
2597
2598 if (smu->ppt_funcs->get_uclk_dpm_states)
2599 ret = smu->ppt_funcs->get_uclk_dpm_states(smu, clock_values_in_khz, num_states);
2600
2601 mutex_unlock(&smu->mutex);
2602
2603 return ret;
2604}
2605
2606enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
2607{
2608 enum amd_pm_state_type pm_state = POWER_STATE_TYPE_DEFAULT;
2609
2610 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2611 return -EOPNOTSUPP;
2612
2613 mutex_lock(&smu->mutex);
2614
2615 if (smu->ppt_funcs->get_current_power_state)
2616 pm_state = smu->ppt_funcs->get_current_power_state(smu);
2617
2618 mutex_unlock(&smu->mutex);
2619
2620 return pm_state;
2621}
2622
2623int smu_get_dpm_clock_table(struct smu_context *smu,
2624 struct dpm_clocks *clock_table)
2625{
2626 int ret = 0;
2627
2628 if (!smu->pm_enabled || !smu->adev->pm.dpm_enabled)
2629 return -EOPNOTSUPP;
2630
2631 mutex_lock(&smu->mutex);
2632
2633 if (smu->ppt_funcs->get_dpm_clock_table)
2634 ret = smu->ppt_funcs->get_dpm_clock_table(smu, clock_table);
2635
2636 mutex_unlock(&smu->mutex);
2637
2638 return ret;
2639}
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#include <linux/firmware.h>
24
25#include "pp_debug.h"
26#include "amdgpu.h"
27#include "amdgpu_smu.h"
28#include "soc15_common.h"
29#include "smu_v11_0.h"
30#include "smu_v12_0.h"
31#include "atom.h"
32#include "amd_pcie.h"
33
34#undef __SMU_DUMMY_MAP
35#define __SMU_DUMMY_MAP(type) #type
36static const char* __smu_message_names[] = {
37 SMU_MESSAGE_TYPES
38};
39
40const char *smu_get_message_name(struct smu_context *smu, enum smu_message_type type)
41{
42 if (type < 0 || type >= SMU_MSG_MAX_COUNT)
43 return "unknown smu message";
44 return __smu_message_names[type];
45}
46
47#undef __SMU_DUMMY_MAP
48#define __SMU_DUMMY_MAP(fea) #fea
49static const char* __smu_feature_names[] = {
50 SMU_FEATURE_MASKS
51};
52
53const char *smu_get_feature_name(struct smu_context *smu, enum smu_feature_mask feature)
54{
55 if (feature < 0 || feature >= SMU_FEATURE_COUNT)
56 return "unknown smu feature";
57 return __smu_feature_names[feature];
58}
59
60size_t smu_sys_get_pp_feature_mask(struct smu_context *smu, char *buf)
61{
62 size_t size = 0;
63 int ret = 0, i = 0;
64 uint32_t feature_mask[2] = { 0 };
65 int32_t feature_index = 0;
66 uint32_t count = 0;
67 uint32_t sort_feature[SMU_FEATURE_COUNT];
68 uint64_t hw_feature_count = 0;
69
70 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
71 if (ret)
72 goto failed;
73
74 size = sprintf(buf + size, "features high: 0x%08x low: 0x%08x\n",
75 feature_mask[1], feature_mask[0]);
76
77 for (i = 0; i < SMU_FEATURE_COUNT; i++) {
78 feature_index = smu_feature_get_index(smu, i);
79 if (feature_index < 0)
80 continue;
81 sort_feature[feature_index] = i;
82 hw_feature_count++;
83 }
84
85 for (i = 0; i < hw_feature_count; i++) {
86 size += sprintf(buf + size, "%02d. %-20s (%2d) : %s\n",
87 count++,
88 smu_get_feature_name(smu, sort_feature[i]),
89 i,
90 !!smu_feature_is_enabled(smu, sort_feature[i]) ?
91 "enabled" : "disabled");
92 }
93
94failed:
95 return size;
96}
97
98int smu_sys_set_pp_feature_mask(struct smu_context *smu, uint64_t new_mask)
99{
100 int ret = 0;
101 uint32_t feature_mask[2] = { 0 };
102 uint64_t feature_2_enabled = 0;
103 uint64_t feature_2_disabled = 0;
104 uint64_t feature_enables = 0;
105
106 ret = smu_feature_get_enabled_mask(smu, feature_mask, 2);
107 if (ret)
108 return ret;
109
110 feature_enables = ((uint64_t)feature_mask[1] << 32 | (uint64_t)feature_mask[0]);
111
112 feature_2_enabled = ~feature_enables & new_mask;
113 feature_2_disabled = feature_enables & ~new_mask;
114
115 if (feature_2_enabled) {
116 ret = smu_feature_update_enable_state(smu, feature_2_enabled, true);
117 if (ret)
118 return ret;
119 }
120 if (feature_2_disabled) {
121 ret = smu_feature_update_enable_state(smu, feature_2_disabled, false);
122 if (ret)
123 return ret;
124 }
125
126 return ret;
127}
128
129int smu_get_smc_version(struct smu_context *smu, uint32_t *if_version, uint32_t *smu_version)
130{
131 int ret = 0;
132
133 if (!if_version && !smu_version)
134 return -EINVAL;
135
136 if (if_version) {
137 ret = smu_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion);
138 if (ret)
139 return ret;
140
141 ret = smu_read_smc_arg(smu, if_version);
142 if (ret)
143 return ret;
144 }
145
146 if (smu_version) {
147 ret = smu_send_smc_msg(smu, SMU_MSG_GetSmuVersion);
148 if (ret)
149 return ret;
150
151 ret = smu_read_smc_arg(smu, smu_version);
152 if (ret)
153 return ret;
154 }
155
156 return ret;
157}
158
159int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
160 uint32_t min, uint32_t max)
161{
162 int ret = 0, clk_id = 0;
163 uint32_t param;
164
165 if (min <= 0 && max <= 0)
166 return -EINVAL;
167
168 if (!smu_clk_dpm_is_enabled(smu, clk_type))
169 return 0;
170
171 clk_id = smu_clk_get_index(smu, clk_type);
172 if (clk_id < 0)
173 return clk_id;
174
175 if (max > 0) {
176 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
177 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxByFreq,
178 param);
179 if (ret)
180 return ret;
181 }
182
183 if (min > 0) {
184 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
185 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMinByFreq,
186 param);
187 if (ret)
188 return ret;
189 }
190
191
192 return ret;
193}
194
195int smu_set_hard_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
196 uint32_t min, uint32_t max)
197{
198 int ret = 0, clk_id = 0;
199 uint32_t param;
200
201 if (min <= 0 && max <= 0)
202 return -EINVAL;
203
204 if (!smu_clk_dpm_is_enabled(smu, clk_type))
205 return 0;
206
207 clk_id = smu_clk_get_index(smu, clk_type);
208 if (clk_id < 0)
209 return clk_id;
210
211 if (max > 0) {
212 param = (uint32_t)((clk_id << 16) | (max & 0xffff));
213 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMaxByFreq,
214 param);
215 if (ret)
216 return ret;
217 }
218
219 if (min > 0) {
220 param = (uint32_t)((clk_id << 16) | (min & 0xffff));
221 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinByFreq,
222 param);
223 if (ret)
224 return ret;
225 }
226
227
228 return ret;
229}
230
231int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
232 uint32_t *min, uint32_t *max)
233{
234 uint32_t clock_limit;
235 int ret = 0;
236
237 if (!min && !max)
238 return -EINVAL;
239
240 if (!smu_clk_dpm_is_enabled(smu, clk_type)) {
241 switch (clk_type) {
242 case SMU_MCLK:
243 case SMU_UCLK:
244 clock_limit = smu->smu_table.boot_values.uclk;
245 break;
246 case SMU_GFXCLK:
247 case SMU_SCLK:
248 clock_limit = smu->smu_table.boot_values.gfxclk;
249 break;
250 case SMU_SOCCLK:
251 clock_limit = smu->smu_table.boot_values.socclk;
252 break;
253 default:
254 clock_limit = 0;
255 break;
256 }
257
258 /* clock in Mhz unit */
259 if (min)
260 *min = clock_limit / 100;
261 if (max)
262 *max = clock_limit / 100;
263
264 return 0;
265 }
266 /*
267 * Todo: Use each asic(ASIC_ppt funcs) control the callbacks exposed to the
268 * core driver and then have helpers for stuff that is common(SMU_v11_x | SMU_v12_x funcs).
269 */
270 ret = smu_get_dpm_ultimate_freq(smu, clk_type, min, max);
271 return ret;
272}
273
274int smu_get_dpm_freq_by_index(struct smu_context *smu, enum smu_clk_type clk_type,
275 uint16_t level, uint32_t *value)
276{
277 int ret = 0, clk_id = 0;
278 uint32_t param;
279
280 if (!value)
281 return -EINVAL;
282
283 if (!smu_clk_dpm_is_enabled(smu, clk_type))
284 return 0;
285
286 clk_id = smu_clk_get_index(smu, clk_type);
287 if (clk_id < 0)
288 return clk_id;
289
290 param = (uint32_t)(((clk_id & 0xffff) << 16) | (level & 0xffff));
291
292 ret = smu_send_smc_msg_with_param(smu,SMU_MSG_GetDpmFreqByIndex,
293 param);
294 if (ret)
295 return ret;
296
297 ret = smu_read_smc_arg(smu, ¶m);
298 if (ret)
299 return ret;
300
301 /* BIT31: 0 - Fine grained DPM, 1 - Dicrete DPM
302 * now, we un-support it */
303 *value = param & 0x7fffffff;
304
305 return ret;
306}
307
308int smu_get_dpm_level_count(struct smu_context *smu, enum smu_clk_type clk_type,
309 uint32_t *value)
310{
311 return smu_get_dpm_freq_by_index(smu, clk_type, 0xff, value);
312}
313
314bool smu_clk_dpm_is_enabled(struct smu_context *smu, enum smu_clk_type clk_type)
315{
316 enum smu_feature_mask feature_id = 0;
317
318 switch (clk_type) {
319 case SMU_MCLK:
320 case SMU_UCLK:
321 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
322 break;
323 case SMU_GFXCLK:
324 case SMU_SCLK:
325 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
326 break;
327 case SMU_SOCCLK:
328 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
329 break;
330 default:
331 return true;
332 }
333
334 if(!smu_feature_is_enabled(smu, feature_id)) {
335 return false;
336 }
337
338 return true;
339}
340
341
342int smu_dpm_set_power_gate(struct smu_context *smu, uint32_t block_type,
343 bool gate)
344{
345 int ret = 0;
346
347 switch (block_type) {
348 case AMD_IP_BLOCK_TYPE_UVD:
349 ret = smu_dpm_set_uvd_enable(smu, gate);
350 break;
351 case AMD_IP_BLOCK_TYPE_VCE:
352 ret = smu_dpm_set_vce_enable(smu, gate);
353 break;
354 case AMD_IP_BLOCK_TYPE_GFX:
355 ret = smu_gfx_off_control(smu, gate);
356 break;
357 case AMD_IP_BLOCK_TYPE_SDMA:
358 ret = smu_powergate_sdma(smu, gate);
359 break;
360 default:
361 break;
362 }
363
364 return ret;
365}
366
367enum amd_pm_state_type smu_get_current_power_state(struct smu_context *smu)
368{
369 /* not support power state */
370 return POWER_STATE_TYPE_DEFAULT;
371}
372
373int smu_get_power_num_states(struct smu_context *smu,
374 struct pp_states_info *state_info)
375{
376 if (!state_info)
377 return -EINVAL;
378
379 /* not support power state */
380 memset(state_info, 0, sizeof(struct pp_states_info));
381 state_info->nums = 1;
382 state_info->states[0] = POWER_STATE_TYPE_DEFAULT;
383
384 return 0;
385}
386
387int smu_common_read_sensor(struct smu_context *smu, enum amd_pp_sensors sensor,
388 void *data, uint32_t *size)
389{
390 struct smu_power_context *smu_power = &smu->smu_power;
391 struct smu_power_gate *power_gate = &smu_power->power_gate;
392 int ret = 0;
393
394 if(!data || !size)
395 return -EINVAL;
396
397 switch (sensor) {
398 case AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK:
399 *((uint32_t *)data) = smu->pstate_sclk;
400 *size = 4;
401 break;
402 case AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK:
403 *((uint32_t *)data) = smu->pstate_mclk;
404 *size = 4;
405 break;
406 case AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK:
407 ret = smu_feature_get_enabled_mask(smu, (uint32_t *)data, 2);
408 *size = 8;
409 break;
410 case AMDGPU_PP_SENSOR_UVD_POWER:
411 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_UVD_BIT) ? 1 : 0;
412 *size = 4;
413 break;
414 case AMDGPU_PP_SENSOR_VCE_POWER:
415 *(uint32_t *)data = smu_feature_is_enabled(smu, SMU_FEATURE_DPM_VCE_BIT) ? 1 : 0;
416 *size = 4;
417 break;
418 case AMDGPU_PP_SENSOR_VCN_POWER_STATE:
419 *(uint32_t *)data = power_gate->vcn_gated ? 0 : 1;
420 *size = 4;
421 break;
422 default:
423 ret = -EINVAL;
424 break;
425 }
426
427 if (ret)
428 *size = 0;
429
430 return ret;
431}
432
433int smu_update_table(struct smu_context *smu, enum smu_table_id table_index, int argument,
434 void *table_data, bool drv2smu)
435{
436 struct smu_table_context *smu_table = &smu->smu_table;
437 struct amdgpu_device *adev = smu->adev;
438 struct smu_table *table = NULL;
439 int ret = 0;
440 int table_id = smu_table_get_index(smu, table_index);
441
442 if (!table_data || table_id >= smu_table->table_count || table_id < 0)
443 return -EINVAL;
444
445 table = &smu_table->tables[table_index];
446
447 if (drv2smu)
448 memcpy(table->cpu_addr, table_data, table->size);
449
450 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrHigh,
451 upper_32_bits(table->mc_address));
452 if (ret)
453 return ret;
454 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_SetDriverDramAddrLow,
455 lower_32_bits(table->mc_address));
456 if (ret)
457 return ret;
458 ret = smu_send_smc_msg_with_param(smu, drv2smu ?
459 SMU_MSG_TransferTableDram2Smu :
460 SMU_MSG_TransferTableSmu2Dram,
461 table_id | ((argument & 0xFFFF) << 16));
462 if (ret)
463 return ret;
464
465 /* flush hdp cache */
466 adev->nbio_funcs->hdp_flush(adev, NULL);
467
468 if (!drv2smu)
469 memcpy(table_data, table->cpu_addr, table->size);
470
471 return ret;
472}
473
474bool is_support_sw_smu(struct amdgpu_device *adev)
475{
476 if (adev->asic_type == CHIP_VEGA20)
477 return (amdgpu_dpm == 2) ? true : false;
478 else if (adev->asic_type >= CHIP_ARCTURUS)
479 return true;
480 else
481 return false;
482}
483
484bool is_support_sw_smu_xgmi(struct amdgpu_device *adev)
485{
486 if (amdgpu_dpm != 1)
487 return false;
488
489 if (adev->asic_type == CHIP_VEGA20)
490 return true;
491
492 return false;
493}
494
495int smu_sys_get_pp_table(struct smu_context *smu, void **table)
496{
497 struct smu_table_context *smu_table = &smu->smu_table;
498
499 if (!smu_table->power_play_table && !smu_table->hardcode_pptable)
500 return -EINVAL;
501
502 if (smu_table->hardcode_pptable)
503 *table = smu_table->hardcode_pptable;
504 else
505 *table = smu_table->power_play_table;
506
507 return smu_table->power_play_table_size;
508}
509
510int smu_sys_set_pp_table(struct smu_context *smu, void *buf, size_t size)
511{
512 struct smu_table_context *smu_table = &smu->smu_table;
513 ATOM_COMMON_TABLE_HEADER *header = (ATOM_COMMON_TABLE_HEADER *)buf;
514 int ret = 0;
515
516 if (!smu->pm_enabled)
517 return -EINVAL;
518 if (header->usStructureSize != size) {
519 pr_err("pp table size not matched !\n");
520 return -EIO;
521 }
522
523 mutex_lock(&smu->mutex);
524 if (!smu_table->hardcode_pptable)
525 smu_table->hardcode_pptable = kzalloc(size, GFP_KERNEL);
526 if (!smu_table->hardcode_pptable) {
527 ret = -ENOMEM;
528 goto failed;
529 }
530
531 memcpy(smu_table->hardcode_pptable, buf, size);
532 smu_table->power_play_table = smu_table->hardcode_pptable;
533 smu_table->power_play_table_size = size;
534 mutex_unlock(&smu->mutex);
535
536 ret = smu_reset(smu);
537 if (ret)
538 pr_info("smu reset failed, ret = %d\n", ret);
539
540 return ret;
541
542failed:
543 mutex_unlock(&smu->mutex);
544 return ret;
545}
546
547int smu_feature_init_dpm(struct smu_context *smu)
548{
549 struct smu_feature *feature = &smu->smu_feature;
550 int ret = 0;
551 uint32_t allowed_feature_mask[SMU_FEATURE_MAX/32];
552
553 if (!smu->pm_enabled)
554 return ret;
555 mutex_lock(&feature->mutex);
556 bitmap_zero(feature->allowed, SMU_FEATURE_MAX);
557 mutex_unlock(&feature->mutex);
558
559 ret = smu_get_allowed_feature_mask(smu, allowed_feature_mask,
560 SMU_FEATURE_MAX/32);
561 if (ret)
562 return ret;
563
564 mutex_lock(&feature->mutex);
565 bitmap_or(feature->allowed, feature->allowed,
566 (unsigned long *)allowed_feature_mask,
567 feature->feature_num);
568 mutex_unlock(&feature->mutex);
569
570 return ret;
571}
572int smu_feature_update_enable_state(struct smu_context *smu, uint64_t feature_mask, bool enabled)
573{
574 uint32_t feature_low = 0, feature_high = 0;
575 int ret = 0;
576
577 if (!smu->pm_enabled)
578 return ret;
579
580 feature_low = (feature_mask >> 0 ) & 0xffffffff;
581 feature_high = (feature_mask >> 32) & 0xffffffff;
582
583 if (enabled) {
584 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesLow,
585 feature_low);
586 if (ret)
587 return ret;
588 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_EnableSmuFeaturesHigh,
589 feature_high);
590 if (ret)
591 return ret;
592
593 } else {
594 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesLow,
595 feature_low);
596 if (ret)
597 return ret;
598 ret = smu_send_smc_msg_with_param(smu, SMU_MSG_DisableSmuFeaturesHigh,
599 feature_high);
600 if (ret)
601 return ret;
602
603 }
604
605 return ret;
606}
607
608int smu_feature_is_enabled(struct smu_context *smu, enum smu_feature_mask mask)
609{
610 struct amdgpu_device *adev = smu->adev;
611 struct smu_feature *feature = &smu->smu_feature;
612 int feature_id;
613 int ret = 0;
614
615 if (adev->flags & AMD_IS_APU)
616 return 1;
617
618 feature_id = smu_feature_get_index(smu, mask);
619 if (feature_id < 0)
620 return 0;
621
622 WARN_ON(feature_id > feature->feature_num);
623
624 mutex_lock(&feature->mutex);
625 ret = test_bit(feature_id, feature->enabled);
626 mutex_unlock(&feature->mutex);
627
628 return ret;
629}
630
631int smu_feature_set_enabled(struct smu_context *smu, enum smu_feature_mask mask,
632 bool enable)
633{
634 struct smu_feature *feature = &smu->smu_feature;
635 int feature_id;
636 uint64_t feature_mask = 0;
637 int ret = 0;
638
639 feature_id = smu_feature_get_index(smu, mask);
640 if (feature_id < 0)
641 return -EINVAL;
642
643 WARN_ON(feature_id > feature->feature_num);
644
645 feature_mask = 1ULL << feature_id;
646
647 mutex_lock(&feature->mutex);
648 ret = smu_feature_update_enable_state(smu, feature_mask, enable);
649 if (ret)
650 goto failed;
651
652 if (enable)
653 test_and_set_bit(feature_id, feature->enabled);
654 else
655 test_and_clear_bit(feature_id, feature->enabled);
656
657failed:
658 mutex_unlock(&feature->mutex);
659
660 return ret;
661}
662
663int smu_feature_is_supported(struct smu_context *smu, enum smu_feature_mask mask)
664{
665 struct smu_feature *feature = &smu->smu_feature;
666 int feature_id;
667 int ret = 0;
668
669 feature_id = smu_feature_get_index(smu, mask);
670 if (feature_id < 0)
671 return 0;
672
673 WARN_ON(feature_id > feature->feature_num);
674
675 mutex_lock(&feature->mutex);
676 ret = test_bit(feature_id, feature->supported);
677 mutex_unlock(&feature->mutex);
678
679 return ret;
680}
681
682int smu_feature_set_supported(struct smu_context *smu,
683 enum smu_feature_mask mask,
684 bool enable)
685{
686 struct smu_feature *feature = &smu->smu_feature;
687 int feature_id;
688 int ret = 0;
689
690 feature_id = smu_feature_get_index(smu, mask);
691 if (feature_id < 0)
692 return -EINVAL;
693
694 WARN_ON(feature_id > feature->feature_num);
695
696 mutex_lock(&feature->mutex);
697 if (enable)
698 test_and_set_bit(feature_id, feature->supported);
699 else
700 test_and_clear_bit(feature_id, feature->supported);
701 mutex_unlock(&feature->mutex);
702
703 return ret;
704}
705
706static int smu_set_funcs(struct amdgpu_device *adev)
707{
708 struct smu_context *smu = &adev->smu;
709
710 switch (adev->asic_type) {
711 case CHIP_VEGA20:
712 case CHIP_NAVI10:
713 case CHIP_NAVI14:
714 case CHIP_NAVI12:
715 case CHIP_ARCTURUS:
716 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
717 smu->od_enabled = true;
718 smu_v11_0_set_smu_funcs(smu);
719 break;
720 case CHIP_RENOIR:
721 if (adev->pm.pp_feature & PP_OVERDRIVE_MASK)
722 smu->od_enabled = true;
723 smu_v12_0_set_smu_funcs(smu);
724 break;
725 default:
726 return -EINVAL;
727 }
728
729 return 0;
730}
731
732static int smu_early_init(void *handle)
733{
734 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
735 struct smu_context *smu = &adev->smu;
736
737 smu->adev = adev;
738 smu->pm_enabled = !!amdgpu_dpm;
739 mutex_init(&smu->mutex);
740
741 return smu_set_funcs(adev);
742}
743
744static int smu_late_init(void *handle)
745{
746 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
747 struct smu_context *smu = &adev->smu;
748
749 if (!smu->pm_enabled)
750 return 0;
751
752 mutex_lock(&smu->mutex);
753 smu_handle_task(&adev->smu,
754 smu->smu_dpm.dpm_level,
755 AMD_PP_TASK_COMPLETE_INIT);
756 mutex_unlock(&smu->mutex);
757
758 return 0;
759}
760
761int smu_get_atom_data_table(struct smu_context *smu, uint32_t table,
762 uint16_t *size, uint8_t *frev, uint8_t *crev,
763 uint8_t **addr)
764{
765 struct amdgpu_device *adev = smu->adev;
766 uint16_t data_start;
767
768 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, table,
769 size, frev, crev, &data_start))
770 return -EINVAL;
771
772 *addr = (uint8_t *)adev->mode_info.atom_context->bios + data_start;
773
774 return 0;
775}
776
777static int smu_initialize_pptable(struct smu_context *smu)
778{
779 /* TODO */
780 return 0;
781}
782
783static int smu_smc_table_sw_init(struct smu_context *smu)
784{
785 int ret;
786
787 ret = smu_initialize_pptable(smu);
788 if (ret) {
789 pr_err("Failed to init smu_initialize_pptable!\n");
790 return ret;
791 }
792
793 /**
794 * Create smu_table structure, and init smc tables such as
795 * TABLE_PPTABLE, TABLE_WATERMARKS, TABLE_SMU_METRICS, and etc.
796 */
797 ret = smu_init_smc_tables(smu);
798 if (ret) {
799 pr_err("Failed to init smc tables!\n");
800 return ret;
801 }
802
803 /**
804 * Create smu_power_context structure, and allocate smu_dpm_context and
805 * context size to fill the smu_power_context data.
806 */
807 ret = smu_init_power(smu);
808 if (ret) {
809 pr_err("Failed to init smu_init_power!\n");
810 return ret;
811 }
812
813 return 0;
814}
815
816static int smu_smc_table_sw_fini(struct smu_context *smu)
817{
818 int ret;
819
820 ret = smu_fini_smc_tables(smu);
821 if (ret) {
822 pr_err("Failed to smu_fini_smc_tables!\n");
823 return ret;
824 }
825
826 return 0;
827}
828
829static int smu_sw_init(void *handle)
830{
831 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
832 struct smu_context *smu = &adev->smu;
833 int ret;
834
835 smu->pool_size = adev->pm.smu_prv_buffer_size;
836 smu->smu_feature.feature_num = SMU_FEATURE_MAX;
837 mutex_init(&smu->smu_feature.mutex);
838 bitmap_zero(smu->smu_feature.supported, SMU_FEATURE_MAX);
839 bitmap_zero(smu->smu_feature.enabled, SMU_FEATURE_MAX);
840 bitmap_zero(smu->smu_feature.allowed, SMU_FEATURE_MAX);
841
842 mutex_init(&smu->smu_baco.mutex);
843 smu->smu_baco.state = SMU_BACO_STATE_EXIT;
844 smu->smu_baco.platform_support = false;
845
846 mutex_init(&smu->sensor_lock);
847
848 smu->watermarks_bitmap = 0;
849 smu->power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
850 smu->default_power_profile_mode = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
851
852 smu->workload_mask = 1 << smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT];
853 smu->workload_prority[PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT] = 0;
854 smu->workload_prority[PP_SMC_POWER_PROFILE_FULLSCREEN3D] = 1;
855 smu->workload_prority[PP_SMC_POWER_PROFILE_POWERSAVING] = 2;
856 smu->workload_prority[PP_SMC_POWER_PROFILE_VIDEO] = 3;
857 smu->workload_prority[PP_SMC_POWER_PROFILE_VR] = 4;
858 smu->workload_prority[PP_SMC_POWER_PROFILE_COMPUTE] = 5;
859 smu->workload_prority[PP_SMC_POWER_PROFILE_CUSTOM] = 6;
860
861 smu->workload_setting[0] = PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT;
862 smu->workload_setting[1] = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
863 smu->workload_setting[2] = PP_SMC_POWER_PROFILE_POWERSAVING;
864 smu->workload_setting[3] = PP_SMC_POWER_PROFILE_VIDEO;
865 smu->workload_setting[4] = PP_SMC_POWER_PROFILE_VR;
866 smu->workload_setting[5] = PP_SMC_POWER_PROFILE_COMPUTE;
867 smu->workload_setting[6] = PP_SMC_POWER_PROFILE_CUSTOM;
868 smu->display_config = &adev->pm.pm_display_cfg;
869
870 smu->smu_dpm.dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
871 smu->smu_dpm.requested_dpm_level = AMD_DPM_FORCED_LEVEL_AUTO;
872 ret = smu_init_microcode(smu);
873 if (ret) {
874 pr_err("Failed to load smu firmware!\n");
875 return ret;
876 }
877
878 ret = smu_smc_table_sw_init(smu);
879 if (ret) {
880 pr_err("Failed to sw init smc table!\n");
881 return ret;
882 }
883
884 ret = smu_register_irq_handler(smu);
885 if (ret) {
886 pr_err("Failed to register smc irq handler!\n");
887 return ret;
888 }
889
890 return 0;
891}
892
893static int smu_sw_fini(void *handle)
894{
895 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
896 struct smu_context *smu = &adev->smu;
897 int ret;
898
899 kfree(smu->irq_source);
900 smu->irq_source = NULL;
901
902 ret = smu_smc_table_sw_fini(smu);
903 if (ret) {
904 pr_err("Failed to sw fini smc table!\n");
905 return ret;
906 }
907
908 ret = smu_fini_power(smu);
909 if (ret) {
910 pr_err("Failed to init smu_fini_power!\n");
911 return ret;
912 }
913
914 return 0;
915}
916
917static int smu_init_fb_allocations(struct smu_context *smu)
918{
919 struct amdgpu_device *adev = smu->adev;
920 struct smu_table_context *smu_table = &smu->smu_table;
921 struct smu_table *tables = smu_table->tables;
922 uint32_t table_count = smu_table->table_count;
923 uint32_t i = 0;
924 int32_t ret = 0;
925
926 if (table_count <= 0)
927 return -EINVAL;
928
929 for (i = 0 ; i < table_count; i++) {
930 if (tables[i].size == 0)
931 continue;
932 ret = amdgpu_bo_create_kernel(adev,
933 tables[i].size,
934 tables[i].align,
935 tables[i].domain,
936 &tables[i].bo,
937 &tables[i].mc_address,
938 &tables[i].cpu_addr);
939 if (ret)
940 goto failed;
941 }
942
943 return 0;
944failed:
945 for (; i > 0; i--) {
946 if (tables[i].size == 0)
947 continue;
948 amdgpu_bo_free_kernel(&tables[i].bo,
949 &tables[i].mc_address,
950 &tables[i].cpu_addr);
951
952 }
953 return ret;
954}
955
956static int smu_fini_fb_allocations(struct smu_context *smu)
957{
958 struct smu_table_context *smu_table = &smu->smu_table;
959 struct smu_table *tables = smu_table->tables;
960 uint32_t table_count = smu_table->table_count;
961 uint32_t i = 0;
962
963 if (table_count == 0 || tables == NULL)
964 return 0;
965
966 for (i = 0 ; i < table_count; i++) {
967 if (tables[i].size == 0)
968 continue;
969 amdgpu_bo_free_kernel(&tables[i].bo,
970 &tables[i].mc_address,
971 &tables[i].cpu_addr);
972 }
973
974 return 0;
975}
976
977static int smu_override_pcie_parameters(struct smu_context *smu)
978{
979 struct amdgpu_device *adev = smu->adev;
980 uint32_t pcie_gen = 0, pcie_width = 0, smu_pcie_arg;
981 int ret;
982
983 if (adev->flags & AMD_IS_APU)
984 return 0;
985
986 if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4)
987 pcie_gen = 3;
988 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)
989 pcie_gen = 2;
990 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2)
991 pcie_gen = 1;
992 else if (adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1)
993 pcie_gen = 0;
994
995 /* Bit 31:16: LCLK DPM level. 0 is DPM0, and 1 is DPM1
996 * Bit 15:8: PCIE GEN, 0 to 3 corresponds to GEN1 to GEN4
997 * Bit 7:0: PCIE lane width, 1 to 7 corresponds is x1 to x32
998 */
999 if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X16)
1000 pcie_width = 6;
1001 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X12)
1002 pcie_width = 5;
1003 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X8)
1004 pcie_width = 4;
1005 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X4)
1006 pcie_width = 3;
1007 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X2)
1008 pcie_width = 2;
1009 else if (adev->pm.pcie_mlw_mask & CAIL_PCIE_LINK_WIDTH_SUPPORT_X1)
1010 pcie_width = 1;
1011
1012 smu_pcie_arg = (1 << 16) | (pcie_gen << 8) | pcie_width;
1013 ret = smu_send_smc_msg_with_param(smu,
1014 SMU_MSG_OverridePcieParameters,
1015 smu_pcie_arg);
1016 if (ret)
1017 pr_err("[%s] Attempt to override pcie params failed!\n", __func__);
1018 return ret;
1019}
1020
1021static int smu_smc_table_hw_init(struct smu_context *smu,
1022 bool initialize)
1023{
1024 struct amdgpu_device *adev = smu->adev;
1025 int ret;
1026
1027 if (smu_is_dpm_running(smu) && adev->in_suspend) {
1028 pr_info("dpm has been enabled\n");
1029 return 0;
1030 }
1031
1032 if (adev->asic_type != CHIP_ARCTURUS) {
1033 ret = smu_init_display_count(smu, 0);
1034 if (ret)
1035 return ret;
1036 }
1037
1038 if (initialize) {
1039 /* get boot_values from vbios to set revision, gfxclk, and etc. */
1040 ret = smu_get_vbios_bootup_values(smu);
1041 if (ret)
1042 return ret;
1043
1044 ret = smu_setup_pptable(smu);
1045 if (ret)
1046 return ret;
1047
1048 ret = smu_get_clk_info_from_vbios(smu);
1049 if (ret)
1050 return ret;
1051
1052 /*
1053 * check if the format_revision in vbios is up to pptable header
1054 * version, and the structure size is not 0.
1055 */
1056 ret = smu_check_pptable(smu);
1057 if (ret)
1058 return ret;
1059
1060 /*
1061 * allocate vram bos to store smc table contents.
1062 */
1063 ret = smu_init_fb_allocations(smu);
1064 if (ret)
1065 return ret;
1066
1067 /*
1068 * Parse pptable format and fill PPTable_t smc_pptable to
1069 * smu_table_context structure. And read the smc_dpm_table from vbios,
1070 * then fill it into smc_pptable.
1071 */
1072 ret = smu_parse_pptable(smu);
1073 if (ret)
1074 return ret;
1075
1076 /*
1077 * Send msg GetDriverIfVersion to check if the return value is equal
1078 * with DRIVER_IF_VERSION of smc header.
1079 */
1080 ret = smu_check_fw_version(smu);
1081 if (ret)
1082 return ret;
1083 }
1084
1085 /* smu_dump_pptable(smu); */
1086
1087 /*
1088 * Copy pptable bo in the vram to smc with SMU MSGs such as
1089 * SetDriverDramAddr and TransferTableDram2Smu.
1090 */
1091 ret = smu_write_pptable(smu);
1092 if (ret)
1093 return ret;
1094
1095 /* issue RunAfllBtc msg */
1096 ret = smu_run_afll_btc(smu);
1097 if (ret)
1098 return ret;
1099
1100 ret = smu_feature_set_allowed_mask(smu);
1101 if (ret)
1102 return ret;
1103
1104 ret = smu_system_features_control(smu, true);
1105 if (ret)
1106 return ret;
1107
1108 if (adev->asic_type != CHIP_ARCTURUS) {
1109 ret = smu_override_pcie_parameters(smu);
1110 if (ret)
1111 return ret;
1112
1113 ret = smu_notify_display_change(smu);
1114 if (ret)
1115 return ret;
1116
1117 /*
1118 * Set min deep sleep dce fclk with bootup value from vbios via
1119 * SetMinDeepSleepDcefclk MSG.
1120 */
1121 ret = smu_set_min_dcef_deep_sleep(smu);
1122 if (ret)
1123 return ret;
1124 }
1125
1126 /*
1127 * Set initialized values (get from vbios) to dpm tables context such as
1128 * gfxclk, memclk, dcefclk, and etc. And enable the DPM feature for each
1129 * type of clks.
1130 */
1131 if (initialize) {
1132 ret = smu_populate_smc_tables(smu);
1133 if (ret)
1134 return ret;
1135
1136 ret = smu_init_max_sustainable_clocks(smu);
1137 if (ret)
1138 return ret;
1139 }
1140
1141 ret = smu_set_default_od_settings(smu, initialize);
1142 if (ret)
1143 return ret;
1144
1145 if (initialize) {
1146 ret = smu_populate_umd_state_clk(smu);
1147 if (ret)
1148 return ret;
1149
1150 ret = smu_get_power_limit(smu, &smu->default_power_limit, true);
1151 if (ret)
1152 return ret;
1153 }
1154
1155 /*
1156 * Set PMSTATUSLOG table bo address with SetToolsDramAddr MSG for tools.
1157 */
1158 ret = smu_set_tool_table_location(smu);
1159
1160 if (!smu_is_dpm_running(smu))
1161 pr_info("dpm has been disabled\n");
1162
1163 return ret;
1164}
1165
1166/**
1167 * smu_alloc_memory_pool - allocate memory pool in the system memory
1168 *
1169 * @smu: amdgpu_device pointer
1170 *
1171 * This memory pool will be used for SMC use and msg SetSystemVirtualDramAddr
1172 * and DramLogSetDramAddr can notify it changed.
1173 *
1174 * Returns 0 on success, error on failure.
1175 */
1176static int smu_alloc_memory_pool(struct smu_context *smu)
1177{
1178 struct amdgpu_device *adev = smu->adev;
1179 struct smu_table_context *smu_table = &smu->smu_table;
1180 struct smu_table *memory_pool = &smu_table->memory_pool;
1181 uint64_t pool_size = smu->pool_size;
1182 int ret = 0;
1183
1184 if (pool_size == SMU_MEMORY_POOL_SIZE_ZERO)
1185 return ret;
1186
1187 memory_pool->size = pool_size;
1188 memory_pool->align = PAGE_SIZE;
1189 memory_pool->domain = AMDGPU_GEM_DOMAIN_GTT;
1190
1191 switch (pool_size) {
1192 case SMU_MEMORY_POOL_SIZE_256_MB:
1193 case SMU_MEMORY_POOL_SIZE_512_MB:
1194 case SMU_MEMORY_POOL_SIZE_1_GB:
1195 case SMU_MEMORY_POOL_SIZE_2_GB:
1196 ret = amdgpu_bo_create_kernel(adev,
1197 memory_pool->size,
1198 memory_pool->align,
1199 memory_pool->domain,
1200 &memory_pool->bo,
1201 &memory_pool->mc_address,
1202 &memory_pool->cpu_addr);
1203 break;
1204 default:
1205 break;
1206 }
1207
1208 return ret;
1209}
1210
1211static int smu_free_memory_pool(struct smu_context *smu)
1212{
1213 struct smu_table_context *smu_table = &smu->smu_table;
1214 struct smu_table *memory_pool = &smu_table->memory_pool;
1215 int ret = 0;
1216
1217 if (memory_pool->size == SMU_MEMORY_POOL_SIZE_ZERO)
1218 return ret;
1219
1220 amdgpu_bo_free_kernel(&memory_pool->bo,
1221 &memory_pool->mc_address,
1222 &memory_pool->cpu_addr);
1223
1224 memset(memory_pool, 0, sizeof(struct smu_table));
1225
1226 return ret;
1227}
1228
1229static int smu_hw_init(void *handle)
1230{
1231 int ret;
1232 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1233 struct smu_context *smu = &adev->smu;
1234
1235 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1236 if (adev->asic_type < CHIP_NAVI10) {
1237 ret = smu_load_microcode(smu);
1238 if (ret)
1239 return ret;
1240 }
1241 }
1242
1243 ret = smu_check_fw_status(smu);
1244 if (ret) {
1245 pr_err("SMC firmware status is not correct\n");
1246 return ret;
1247 }
1248
1249 if (adev->flags & AMD_IS_APU) {
1250 smu_powergate_sdma(&adev->smu, false);
1251 smu_powergate_vcn(&adev->smu, false);
1252 }
1253
1254 if (!smu->pm_enabled)
1255 return 0;
1256
1257 ret = smu_feature_init_dpm(smu);
1258 if (ret)
1259 goto failed;
1260
1261 ret = smu_smc_table_hw_init(smu, true);
1262 if (ret)
1263 goto failed;
1264
1265 ret = smu_alloc_memory_pool(smu);
1266 if (ret)
1267 goto failed;
1268
1269 /*
1270 * Use msg SetSystemVirtualDramAddr and DramLogSetDramAddr can notify
1271 * pool location.
1272 */
1273 ret = smu_notify_memory_pool_location(smu);
1274 if (ret)
1275 goto failed;
1276
1277 ret = smu_start_thermal_control(smu);
1278 if (ret)
1279 goto failed;
1280
1281 if (!smu->pm_enabled)
1282 adev->pm.dpm_enabled = false;
1283 else
1284 adev->pm.dpm_enabled = true; /* TODO: will set dpm_enabled flag while VCN and DAL DPM is workable */
1285
1286 pr_info("SMU is initialized successfully!\n");
1287
1288 return 0;
1289
1290failed:
1291 return ret;
1292}
1293
1294static int smu_hw_fini(void *handle)
1295{
1296 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1297 struct smu_context *smu = &adev->smu;
1298 struct smu_table_context *table_context = &smu->smu_table;
1299 int ret = 0;
1300
1301 if (adev->flags & AMD_IS_APU) {
1302 smu_powergate_sdma(&adev->smu, true);
1303 smu_powergate_vcn(&adev->smu, true);
1304 }
1305
1306 kfree(table_context->driver_pptable);
1307 table_context->driver_pptable = NULL;
1308
1309 kfree(table_context->max_sustainable_clocks);
1310 table_context->max_sustainable_clocks = NULL;
1311
1312 kfree(table_context->overdrive_table);
1313 table_context->overdrive_table = NULL;
1314
1315 ret = smu_fini_fb_allocations(smu);
1316 if (ret)
1317 return ret;
1318
1319 ret = smu_free_memory_pool(smu);
1320 if (ret)
1321 return ret;
1322
1323 return 0;
1324}
1325
1326int smu_reset(struct smu_context *smu)
1327{
1328 struct amdgpu_device *adev = smu->adev;
1329 int ret = 0;
1330
1331 ret = smu_hw_fini(adev);
1332 if (ret)
1333 return ret;
1334
1335 ret = smu_hw_init(adev);
1336 if (ret)
1337 return ret;
1338
1339 return ret;
1340}
1341
1342static int smu_suspend(void *handle)
1343{
1344 int ret;
1345 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1346 struct smu_context *smu = &adev->smu;
1347 bool baco_feature_is_enabled = smu_feature_is_enabled(smu, SMU_FEATURE_BACO_BIT);
1348
1349 ret = smu_system_features_control(smu, false);
1350 if (ret)
1351 return ret;
1352
1353 if (adev->in_gpu_reset && baco_feature_is_enabled) {
1354 ret = smu_feature_set_enabled(smu, SMU_FEATURE_BACO_BIT, true);
1355 if (ret) {
1356 pr_warn("set BACO feature enabled failed, return %d\n", ret);
1357 return ret;
1358 }
1359 }
1360
1361 smu->watermarks_bitmap &= ~(WATERMARKS_LOADED);
1362
1363 if (adev->asic_type >= CHIP_NAVI10 &&
1364 adev->gfx.rlc.funcs->stop)
1365 adev->gfx.rlc.funcs->stop(adev);
1366
1367 return 0;
1368}
1369
1370static int smu_resume(void *handle)
1371{
1372 int ret;
1373 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1374 struct smu_context *smu = &adev->smu;
1375
1376 pr_info("SMU is resuming...\n");
1377
1378 mutex_lock(&smu->mutex);
1379
1380 ret = smu_smc_table_hw_init(smu, false);
1381 if (ret)
1382 goto failed;
1383
1384 ret = smu_start_thermal_control(smu);
1385 if (ret)
1386 goto failed;
1387
1388 mutex_unlock(&smu->mutex);
1389
1390 pr_info("SMU is resumed successfully!\n");
1391
1392 return 0;
1393failed:
1394 mutex_unlock(&smu->mutex);
1395 return ret;
1396}
1397
1398int smu_display_configuration_change(struct smu_context *smu,
1399 const struct amd_pp_display_configuration *display_config)
1400{
1401 int index = 0;
1402 int num_of_active_display = 0;
1403
1404 if (!smu->pm_enabled || !is_support_sw_smu(smu->adev))
1405 return -EINVAL;
1406
1407 if (!display_config)
1408 return -EINVAL;
1409
1410 mutex_lock(&smu->mutex);
1411
1412 smu_set_deep_sleep_dcefclk(smu,
1413 display_config->min_dcef_deep_sleep_set_clk / 100);
1414
1415 for (index = 0; index < display_config->num_path_including_non_display; index++) {
1416 if (display_config->displays[index].controller_id != 0)
1417 num_of_active_display++;
1418 }
1419
1420 smu_set_active_display_count(smu, num_of_active_display);
1421
1422 smu_store_cc6_data(smu, display_config->cpu_pstate_separation_time,
1423 display_config->cpu_cc6_disable,
1424 display_config->cpu_pstate_disable,
1425 display_config->nb_pstate_switch_disable);
1426
1427 mutex_unlock(&smu->mutex);
1428
1429 return 0;
1430}
1431
1432static int smu_get_clock_info(struct smu_context *smu,
1433 struct smu_clock_info *clk_info,
1434 enum smu_perf_level_designation designation)
1435{
1436 int ret;
1437 struct smu_performance_level level = {0};
1438
1439 if (!clk_info)
1440 return -EINVAL;
1441
1442 ret = smu_get_perf_level(smu, PERF_LEVEL_ACTIVITY, &level);
1443 if (ret)
1444 return -EINVAL;
1445
1446 clk_info->min_mem_clk = level.memory_clock;
1447 clk_info->min_eng_clk = level.core_clock;
1448 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1449
1450 ret = smu_get_perf_level(smu, designation, &level);
1451 if (ret)
1452 return -EINVAL;
1453
1454 clk_info->min_mem_clk = level.memory_clock;
1455 clk_info->min_eng_clk = level.core_clock;
1456 clk_info->min_bus_bandwidth = level.non_local_mem_freq * level.non_local_mem_width;
1457
1458 return 0;
1459}
1460
1461int smu_get_current_clocks(struct smu_context *smu,
1462 struct amd_pp_clock_info *clocks)
1463{
1464 struct amd_pp_simple_clock_info simple_clocks = {0};
1465 struct smu_clock_info hw_clocks;
1466 int ret = 0;
1467
1468 if (!is_support_sw_smu(smu->adev))
1469 return -EINVAL;
1470
1471 mutex_lock(&smu->mutex);
1472
1473 smu_get_dal_power_level(smu, &simple_clocks);
1474
1475 if (smu->support_power_containment)
1476 ret = smu_get_clock_info(smu, &hw_clocks,
1477 PERF_LEVEL_POWER_CONTAINMENT);
1478 else
1479 ret = smu_get_clock_info(smu, &hw_clocks, PERF_LEVEL_ACTIVITY);
1480
1481 if (ret) {
1482 pr_err("Error in smu_get_clock_info\n");
1483 goto failed;
1484 }
1485
1486 clocks->min_engine_clock = hw_clocks.min_eng_clk;
1487 clocks->max_engine_clock = hw_clocks.max_eng_clk;
1488 clocks->min_memory_clock = hw_clocks.min_mem_clk;
1489 clocks->max_memory_clock = hw_clocks.max_mem_clk;
1490 clocks->min_bus_bandwidth = hw_clocks.min_bus_bandwidth;
1491 clocks->max_bus_bandwidth = hw_clocks.max_bus_bandwidth;
1492 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1493 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1494
1495 if (simple_clocks.level == 0)
1496 clocks->max_clocks_state = PP_DAL_POWERLEVEL_7;
1497 else
1498 clocks->max_clocks_state = simple_clocks.level;
1499
1500 if (!smu_get_current_shallow_sleep_clocks(smu, &hw_clocks)) {
1501 clocks->max_engine_clock_in_sr = hw_clocks.max_eng_clk;
1502 clocks->min_engine_clock_in_sr = hw_clocks.min_eng_clk;
1503 }
1504
1505failed:
1506 mutex_unlock(&smu->mutex);
1507 return ret;
1508}
1509
1510static int smu_set_clockgating_state(void *handle,
1511 enum amd_clockgating_state state)
1512{
1513 return 0;
1514}
1515
1516static int smu_set_powergating_state(void *handle,
1517 enum amd_powergating_state state)
1518{
1519 return 0;
1520}
1521
1522static int smu_enable_umd_pstate(void *handle,
1523 enum amd_dpm_forced_level *level)
1524{
1525 uint32_t profile_mode_mask = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
1526 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
1527 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
1528 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
1529
1530 struct smu_context *smu = (struct smu_context*)(handle);
1531 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1532 if (!smu->pm_enabled || !smu_dpm_ctx->dpm_context)
1533 return -EINVAL;
1534
1535 if (!(smu_dpm_ctx->dpm_level & profile_mode_mask)) {
1536 /* enter umd pstate, save current level, disable gfx cg*/
1537 if (*level & profile_mode_mask) {
1538 smu_dpm_ctx->saved_dpm_level = smu_dpm_ctx->dpm_level;
1539 smu_dpm_ctx->enable_umd_pstate = true;
1540 amdgpu_device_ip_set_clockgating_state(smu->adev,
1541 AMD_IP_BLOCK_TYPE_GFX,
1542 AMD_CG_STATE_UNGATE);
1543 amdgpu_device_ip_set_powergating_state(smu->adev,
1544 AMD_IP_BLOCK_TYPE_GFX,
1545 AMD_PG_STATE_UNGATE);
1546 }
1547 } else {
1548 /* exit umd pstate, restore level, enable gfx cg*/
1549 if (!(*level & profile_mode_mask)) {
1550 if (*level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)
1551 *level = smu_dpm_ctx->saved_dpm_level;
1552 smu_dpm_ctx->enable_umd_pstate = false;
1553 amdgpu_device_ip_set_clockgating_state(smu->adev,
1554 AMD_IP_BLOCK_TYPE_GFX,
1555 AMD_CG_STATE_GATE);
1556 amdgpu_device_ip_set_powergating_state(smu->adev,
1557 AMD_IP_BLOCK_TYPE_GFX,
1558 AMD_PG_STATE_GATE);
1559 }
1560 }
1561
1562 return 0;
1563}
1564
1565static int smu_default_set_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1566{
1567 int ret = 0;
1568 uint32_t sclk_mask, mclk_mask, soc_mask;
1569
1570 switch (level) {
1571 case AMD_DPM_FORCED_LEVEL_HIGH:
1572 ret = smu_force_dpm_limit_value(smu, true);
1573 break;
1574 case AMD_DPM_FORCED_LEVEL_LOW:
1575 ret = smu_force_dpm_limit_value(smu, false);
1576 break;
1577 case AMD_DPM_FORCED_LEVEL_AUTO:
1578 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1579 ret = smu_unforce_dpm_levels(smu);
1580 break;
1581 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
1582 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
1583 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
1584 ret = smu_get_profiling_clk_mask(smu, level,
1585 &sclk_mask,
1586 &mclk_mask,
1587 &soc_mask);
1588 if (ret)
1589 return ret;
1590 smu_force_clk_levels(smu, SMU_SCLK, 1 << sclk_mask);
1591 smu_force_clk_levels(smu, SMU_MCLK, 1 << mclk_mask);
1592 smu_force_clk_levels(smu, SMU_SOCCLK, 1 << soc_mask);
1593 break;
1594 case AMD_DPM_FORCED_LEVEL_MANUAL:
1595 case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
1596 default:
1597 break;
1598 }
1599 return ret;
1600}
1601
1602int smu_adjust_power_state_dynamic(struct smu_context *smu,
1603 enum amd_dpm_forced_level level,
1604 bool skip_display_settings)
1605{
1606 int ret = 0;
1607 int index = 0;
1608 long workload;
1609 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1610
1611 if (!smu->pm_enabled)
1612 return -EINVAL;
1613
1614 if (!skip_display_settings) {
1615 ret = smu_display_config_changed(smu);
1616 if (ret) {
1617 pr_err("Failed to change display config!");
1618 return ret;
1619 }
1620 }
1621
1622 ret = smu_apply_clocks_adjust_rules(smu);
1623 if (ret) {
1624 pr_err("Failed to apply clocks adjust rules!");
1625 return ret;
1626 }
1627
1628 if (!skip_display_settings) {
1629 ret = smu_notify_smc_dispaly_config(smu);
1630 if (ret) {
1631 pr_err("Failed to notify smc display config!");
1632 return ret;
1633 }
1634 }
1635
1636 if (smu_dpm_ctx->dpm_level != level) {
1637 ret = smu_asic_set_performance_level(smu, level);
1638 if (ret) {
1639 ret = smu_default_set_performance_level(smu, level);
1640 if (ret) {
1641 pr_err("Failed to set performance level!");
1642 return ret;
1643 }
1644 }
1645
1646 /* update the saved copy */
1647 smu_dpm_ctx->dpm_level = level;
1648 }
1649
1650 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL) {
1651 index = fls(smu->workload_mask);
1652 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1653 workload = smu->workload_setting[index];
1654
1655 if (smu->power_profile_mode != workload)
1656 smu_set_power_profile_mode(smu, &workload, 0);
1657 }
1658
1659 return ret;
1660}
1661
1662int smu_handle_task(struct smu_context *smu,
1663 enum amd_dpm_forced_level level,
1664 enum amd_pp_task task_id)
1665{
1666 int ret = 0;
1667
1668 switch (task_id) {
1669 case AMD_PP_TASK_DISPLAY_CONFIG_CHANGE:
1670 ret = smu_pre_display_config_changed(smu);
1671 if (ret)
1672 return ret;
1673 ret = smu_set_cpu_power_state(smu);
1674 if (ret)
1675 return ret;
1676 ret = smu_adjust_power_state_dynamic(smu, level, false);
1677 break;
1678 case AMD_PP_TASK_COMPLETE_INIT:
1679 case AMD_PP_TASK_READJUST_POWER_STATE:
1680 ret = smu_adjust_power_state_dynamic(smu, level, true);
1681 break;
1682 default:
1683 break;
1684 }
1685
1686 return ret;
1687}
1688
1689int smu_switch_power_profile(struct smu_context *smu,
1690 enum PP_SMC_POWER_PROFILE type,
1691 bool en)
1692{
1693 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1694 long workload;
1695 uint32_t index;
1696
1697 if (!smu->pm_enabled)
1698 return -EINVAL;
1699
1700 if (!(type < PP_SMC_POWER_PROFILE_CUSTOM))
1701 return -EINVAL;
1702
1703 mutex_lock(&smu->mutex);
1704
1705 if (!en) {
1706 smu->workload_mask &= ~(1 << smu->workload_prority[type]);
1707 index = fls(smu->workload_mask);
1708 index = index > 0 && index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1709 workload = smu->workload_setting[index];
1710 } else {
1711 smu->workload_mask |= (1 << smu->workload_prority[type]);
1712 index = fls(smu->workload_mask);
1713 index = index <= WORKLOAD_POLICY_MAX ? index - 1 : 0;
1714 workload = smu->workload_setting[index];
1715 }
1716
1717 if (smu_dpm_ctx->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1718 smu_set_power_profile_mode(smu, &workload, 0);
1719
1720 mutex_unlock(&smu->mutex);
1721
1722 return 0;
1723}
1724
1725enum amd_dpm_forced_level smu_get_performance_level(struct smu_context *smu)
1726{
1727 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1728 enum amd_dpm_forced_level level;
1729
1730 if (!smu_dpm_ctx->dpm_context)
1731 return -EINVAL;
1732
1733 mutex_lock(&(smu->mutex));
1734 level = smu_dpm_ctx->dpm_level;
1735 mutex_unlock(&(smu->mutex));
1736
1737 return level;
1738}
1739
1740int smu_force_performance_level(struct smu_context *smu, enum amd_dpm_forced_level level)
1741{
1742 struct smu_dpm_context *smu_dpm_ctx = &(smu->smu_dpm);
1743 int ret = 0;
1744
1745 if (!smu_dpm_ctx->dpm_context)
1746 return -EINVAL;
1747
1748 ret = smu_enable_umd_pstate(smu, &level);
1749 if (ret)
1750 return ret;
1751
1752 ret = smu_handle_task(smu, level,
1753 AMD_PP_TASK_READJUST_POWER_STATE);
1754
1755 return ret;
1756}
1757
1758int smu_set_display_count(struct smu_context *smu, uint32_t count)
1759{
1760 int ret = 0;
1761
1762 mutex_lock(&smu->mutex);
1763 ret = smu_init_display_count(smu, count);
1764 mutex_unlock(&smu->mutex);
1765
1766 return ret;
1767}
1768
1769const struct amd_ip_funcs smu_ip_funcs = {
1770 .name = "smu",
1771 .early_init = smu_early_init,
1772 .late_init = smu_late_init,
1773 .sw_init = smu_sw_init,
1774 .sw_fini = smu_sw_fini,
1775 .hw_init = smu_hw_init,
1776 .hw_fini = smu_hw_fini,
1777 .suspend = smu_suspend,
1778 .resume = smu_resume,
1779 .is_idle = NULL,
1780 .check_soft_reset = NULL,
1781 .wait_for_idle = NULL,
1782 .soft_reset = NULL,
1783 .set_clockgating_state = smu_set_clockgating_state,
1784 .set_powergating_state = smu_set_powergating_state,
1785 .enable_umd_pstate = smu_enable_umd_pstate,
1786};
1787
1788const struct amdgpu_ip_block_version smu_v11_0_ip_block =
1789{
1790 .type = AMD_IP_BLOCK_TYPE_SMC,
1791 .major = 11,
1792 .minor = 0,
1793 .rev = 0,
1794 .funcs = &smu_ip_funcs,
1795};
1796
1797const struct amdgpu_ip_block_version smu_v12_0_ip_block =
1798{
1799 .type = AMD_IP_BLOCK_TYPE_SMC,
1800 .major = 12,
1801 .minor = 0,
1802 .rev = 0,
1803 .funcs = &smu_ip_funcs,
1804};