2 * Copyright 2020 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #define SWSMU_CODE_LAYER_L2
27 #include "amdgpu_smu.h"
28 #include "smu_v13_0.h"
29 #include "smu13_driver_if_yellow_carp.h"
30 #include "yellow_carp_ppt.h"
31 #include "smu_v13_0_1_ppsmc.h"
32 #include "smu_v13_0_1_pmfw.h"
36 * DO NOT use these for err/warn/info/debug messages.
37 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
38 * They are more MGPU friendly.
45 #define FEATURE_MASK(feature) (1ULL << feature)
46 #define SMC_DPM_FEATURE ( \
47 FEATURE_MASK(FEATURE_CCLK_DPM_BIT) | \
48 FEATURE_MASK(FEATURE_VCN_DPM_BIT) | \
49 FEATURE_MASK(FEATURE_FCLK_DPM_BIT) | \
50 FEATURE_MASK(FEATURE_SOCCLK_DPM_BIT) | \
51 FEATURE_MASK(FEATURE_MP0CLK_DPM_BIT) | \
52 FEATURE_MASK(FEATURE_LCLK_DPM_BIT) | \
53 FEATURE_MASK(FEATURE_SHUBCLK_DPM_BIT) | \
54 FEATURE_MASK(FEATURE_DCFCLK_DPM_BIT)| \
55 FEATURE_MASK(FEATURE_GFX_DPM_BIT))
57 static struct cmn2asic_msg_mapping yellow_carp_message_map[SMU_MSG_MAX_COUNT] = {
58 MSG_MAP(TestMessage, PPSMC_MSG_TestMessage, 1),
59 MSG_MAP(GetSmuVersion, PPSMC_MSG_GetSmuVersion, 1),
60 MSG_MAP(GetDriverIfVersion, PPSMC_MSG_GetDriverIfVersion, 1),
61 MSG_MAP(EnableGfxOff, PPSMC_MSG_EnableGfxOff, 1),
62 MSG_MAP(AllowGfxOff, PPSMC_MSG_AllowGfxOff, 1),
63 MSG_MAP(DisallowGfxOff, PPSMC_MSG_DisallowGfxOff, 1),
64 MSG_MAP(PowerDownVcn, PPSMC_MSG_PowerDownVcn, 1),
65 MSG_MAP(PowerUpVcn, PPSMC_MSG_PowerUpVcn, 1),
66 MSG_MAP(SetHardMinVcn, PPSMC_MSG_SetHardMinVcn, 1),
67 MSG_MAP(ActiveProcessNotify, PPSMC_MSG_ActiveProcessNotify, 1),
68 MSG_MAP(PrepareMp1ForUnload, PPSMC_MSG_PrepareMp1ForUnload, 1),
69 MSG_MAP(SetDriverDramAddrHigh, PPSMC_MSG_SetDriverDramAddrHigh, 1),
70 MSG_MAP(SetDriverDramAddrLow, PPSMC_MSG_SetDriverDramAddrLow, 1),
71 MSG_MAP(TransferTableSmu2Dram, PPSMC_MSG_TransferTableSmu2Dram, 1),
72 MSG_MAP(TransferTableDram2Smu, PPSMC_MSG_TransferTableDram2Smu, 1),
73 MSG_MAP(GfxDeviceDriverReset, PPSMC_MSG_GfxDeviceDriverReset, 1),
74 MSG_MAP(GetEnabledSmuFeatures, PPSMC_MSG_GetEnabledSmuFeatures, 1),
75 MSG_MAP(SetHardMinSocclkByFreq, PPSMC_MSG_SetHardMinSocclkByFreq, 1),
76 MSG_MAP(SetSoftMinVcn, PPSMC_MSG_SetSoftMinVcn, 1),
77 MSG_MAP(GetGfxclkFrequency, PPSMC_MSG_GetGfxclkFrequency, 1),
78 MSG_MAP(GetFclkFrequency, PPSMC_MSG_GetFclkFrequency, 1),
79 MSG_MAP(SetSoftMaxGfxClk, PPSMC_MSG_SetSoftMaxGfxClk, 1),
80 MSG_MAP(SetHardMinGfxClk, PPSMC_MSG_SetHardMinGfxClk, 1),
81 MSG_MAP(SetSoftMaxSocclkByFreq, PPSMC_MSG_SetSoftMaxSocclkByFreq, 1),
82 MSG_MAP(SetSoftMaxFclkByFreq, PPSMC_MSG_SetSoftMaxFclkByFreq, 1),
83 MSG_MAP(SetSoftMaxVcn, PPSMC_MSG_SetSoftMaxVcn, 1),
84 MSG_MAP(SetPowerLimitPercentage, PPSMC_MSG_SetPowerLimitPercentage, 1),
85 MSG_MAP(PowerDownJpeg, PPSMC_MSG_PowerDownJpeg, 1),
86 MSG_MAP(PowerUpJpeg, PPSMC_MSG_PowerUpJpeg, 1),
87 MSG_MAP(SetHardMinFclkByFreq, PPSMC_MSG_SetHardMinFclkByFreq, 1),
88 MSG_MAP(SetSoftMinSocclkByFreq, PPSMC_MSG_SetSoftMinSocclkByFreq, 1),
91 static struct cmn2asic_mapping yellow_carp_feature_mask_map[SMU_FEATURE_COUNT] = {
93 FEA_MAP(FAN_CONTROLLER),
99 FEA_MAP_REVERSE(FCLK),
100 FEA_MAP_REVERSE(SOCCLK),
102 FEA_MAP(SHUBCLK_DPM),
104 FEA_MAP_HALF_REVERSE(GFX),
120 FEA_MAP(RSMU_LOW_POWER),
121 FEA_MAP(SMN_LOW_POWER),
122 FEA_MAP(THM_LOW_POWER),
123 FEA_MAP(SMUIO_LOW_POWER),
124 FEA_MAP(MP1_LOW_POWER),
128 FEA_MAP(MSMU_LOW_POWER),
132 static struct cmn2asic_mapping yellow_carp_table_map[SMU_TABLE_COUNT] = {
133 TAB_MAP_VALID(WATERMARKS),
134 TAB_MAP_VALID(SMU_METRICS),
135 TAB_MAP_VALID(CUSTOM_DPM),
136 TAB_MAP_VALID(DPMCLOCKS),
139 static struct cmn2asic_mapping yellow_carp_workload_map[PP_SMC_POWER_PROFILE_COUNT] = {
140 WORKLOAD_MAP(PP_SMC_POWER_PROFILE_FULLSCREEN3D, WORKLOAD_PPLIB_FULL_SCREEN_3D_BIT),
141 WORKLOAD_MAP(PP_SMC_POWER_PROFILE_VIDEO, WORKLOAD_PPLIB_VIDEO_BIT),
142 WORKLOAD_MAP(PP_SMC_POWER_PROFILE_VR, WORKLOAD_PPLIB_VR_BIT),
143 WORKLOAD_MAP(PP_SMC_POWER_PROFILE_COMPUTE, WORKLOAD_PPLIB_COMPUTE_BIT),
144 WORKLOAD_MAP(PP_SMC_POWER_PROFILE_CUSTOM, WORKLOAD_PPLIB_CUSTOM_BIT),
147 static int yellow_carp_init_smc_tables(struct smu_context *smu)
149 struct smu_table_context *smu_table = &smu->smu_table;
150 struct smu_table *tables = smu_table->tables;
152 SMU_TABLE_INIT(tables, SMU_TABLE_WATERMARKS, sizeof(Watermarks_t),
153 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
154 SMU_TABLE_INIT(tables, SMU_TABLE_DPMCLOCKS, sizeof(DpmClocks_t),
155 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
156 SMU_TABLE_INIT(tables, SMU_TABLE_SMU_METRICS, sizeof(SmuMetrics_t),
157 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
159 smu_table->clocks_table = kzalloc(sizeof(DpmClocks_t), GFP_KERNEL);
160 if (!smu_table->clocks_table)
163 smu_table->metrics_table = kzalloc(sizeof(SmuMetrics_t), GFP_KERNEL);
164 if (!smu_table->metrics_table)
166 smu_table->metrics_time = 0;
168 smu_table->watermarks_table = kzalloc(sizeof(Watermarks_t), GFP_KERNEL);
169 if (!smu_table->watermarks_table)
172 smu_table->gpu_metrics_table_size = sizeof(struct gpu_metrics_v2_1);
173 smu_table->gpu_metrics_table = kzalloc(smu_table->gpu_metrics_table_size, GFP_KERNEL);
174 if (!smu_table->gpu_metrics_table)
180 kfree(smu_table->watermarks_table);
182 kfree(smu_table->metrics_table);
184 kfree(smu_table->clocks_table);
189 static int yellow_carp_fini_smc_tables(struct smu_context *smu)
191 struct smu_table_context *smu_table = &smu->smu_table;
193 kfree(smu_table->clocks_table);
194 smu_table->clocks_table = NULL;
196 kfree(smu_table->metrics_table);
197 smu_table->metrics_table = NULL;
199 kfree(smu_table->watermarks_table);
200 smu_table->watermarks_table = NULL;
205 static int yellow_carp_system_features_control(struct smu_context *smu, bool en)
207 struct smu_feature *feature = &smu->smu_feature;
208 struct amdgpu_device *adev = smu->adev;
209 uint32_t feature_mask[2];
212 if (!en && !adev->in_s0ix)
213 ret = smu_cmn_send_smc_msg(smu, SMU_MSG_PrepareMp1ForUnload, NULL);
215 bitmap_zero(feature->enabled, feature->feature_num);
216 bitmap_zero(feature->supported, feature->feature_num);
221 ret = smu_cmn_get_enabled_32_bits_mask(smu, feature_mask, 2);
225 bitmap_copy(feature->enabled, (unsigned long *)&feature_mask,
226 feature->feature_num);
227 bitmap_copy(feature->supported, (unsigned long *)&feature_mask,
228 feature->feature_num);
233 static int yellow_carp_dpm_set_vcn_enable(struct smu_context *smu, bool enable)
237 /* vcn dpm on is a prerequisite for vcn power gate messages */
239 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_PowerUpVcn,
242 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_PowerDownVcn,
248 static int yellow_carp_dpm_set_jpeg_enable(struct smu_context *smu, bool enable)
253 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_PowerUpJpeg,
256 ret = smu_cmn_send_smc_msg_with_param(smu,
257 SMU_MSG_PowerDownJpeg, 0,
264 static bool yellow_carp_is_dpm_running(struct smu_context *smu)
267 uint32_t feature_mask[2];
268 uint64_t feature_enabled;
270 ret = smu_cmn_get_enabled_32_bits_mask(smu, feature_mask, 2);
275 feature_enabled = (uint64_t)feature_mask[1] << 32 | feature_mask[0];
277 return !!(feature_enabled & SMC_DPM_FEATURE);
280 static int yellow_carp_post_smu_init(struct smu_context *smu)
282 struct amdgpu_device *adev = smu->adev;
285 /* allow message will be sent after enable message on Yellow Carp*/
286 ret = smu_cmn_send_smc_msg(smu, SMU_MSG_EnableGfxOff, NULL);
288 dev_err(adev->dev, "Failed to Enable GfxOff!\n");
292 static int yellow_carp_mode_reset(struct smu_context *smu, int type)
294 int ret = 0, index = 0;
296 index = smu_cmn_to_asic_specific_index(smu, CMN2ASIC_MAPPING_MSG,
297 SMU_MSG_GfxDeviceDriverReset);
299 return index == -EACCES ? 0 : index;
301 ret = smu_cmn_send_smc_msg_with_param(smu, (uint16_t)index, type, NULL);
303 dev_err(smu->adev->dev, "Failed to mode reset!\n");
308 static int yellow_carp_mode2_reset(struct smu_context *smu)
310 return yellow_carp_mode_reset(smu, SMU_RESET_MODE_2);
313 static int yellow_carp_get_smu_metrics_data(struct smu_context *smu,
314 MetricsMember_t member,
317 struct smu_table_context *smu_table = &smu->smu_table;
319 SmuMetrics_t *metrics = (SmuMetrics_t *)smu_table->metrics_table;
322 mutex_lock(&smu->metrics_lock);
324 ret = smu_cmn_get_metrics_table_locked(smu, NULL, false);
326 mutex_unlock(&smu->metrics_lock);
331 case METRICS_AVERAGE_GFXCLK:
332 *value = metrics->GfxclkFrequency;
334 case METRICS_AVERAGE_SOCCLK:
335 *value = metrics->SocclkFrequency;
337 case METRICS_AVERAGE_VCLK:
338 *value = metrics->VclkFrequency;
340 case METRICS_AVERAGE_DCLK:
341 *value = metrics->DclkFrequency;
343 case METRICS_AVERAGE_UCLK:
344 *value = metrics->MemclkFrequency;
346 case METRICS_AVERAGE_GFXACTIVITY:
347 *value = metrics->GfxActivity / 100;
349 case METRICS_AVERAGE_VCNACTIVITY:
350 *value = metrics->UvdActivity;
352 case METRICS_AVERAGE_SOCKETPOWER:
353 *value = (metrics->CurrentSocketPower << 8) / 1000;
355 case METRICS_TEMPERATURE_EDGE:
356 *value = metrics->GfxTemperature / 100 *
357 SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
359 case METRICS_TEMPERATURE_HOTSPOT:
360 *value = metrics->SocTemperature / 100 *
361 SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
363 case METRICS_THROTTLER_STATUS:
364 *value = metrics->ThrottlerStatus;
366 case METRICS_VOLTAGE_VDDGFX:
367 *value = metrics->Voltage[0];
369 case METRICS_VOLTAGE_VDDSOC:
370 *value = metrics->Voltage[1];
372 case METRICS_SS_APU_SHARE:
373 /* return the percentage of APU power with respect to APU's power limit.
374 * percentage is reported, this isn't boost value. Smartshift power
375 * boost/shift is only when the percentage is more than 100.
377 if (metrics->StapmOpnLimit > 0)
378 *value = (metrics->ApuPower * 100) / metrics->StapmOpnLimit;
382 case METRICS_SS_DGPU_SHARE:
383 /* return the percentage of dGPU power with respect to dGPU's power limit.
384 * percentage is reported, this isn't boost value. Smartshift power
385 * boost/shift is only when the percentage is more than 100.
387 if ((metrics->dGpuPower > 0) &&
388 (metrics->StapmCurrentLimit > metrics->StapmOpnLimit))
389 *value = (metrics->dGpuPower * 100) /
390 (metrics->StapmCurrentLimit - metrics->StapmOpnLimit);
399 mutex_unlock(&smu->metrics_lock);
404 static int yellow_carp_read_sensor(struct smu_context *smu,
405 enum amd_pp_sensors sensor,
406 void *data, uint32_t *size)
413 mutex_lock(&smu->sensor_lock);
415 case AMDGPU_PP_SENSOR_GPU_LOAD:
416 ret = yellow_carp_get_smu_metrics_data(smu,
417 METRICS_AVERAGE_GFXACTIVITY,
421 case AMDGPU_PP_SENSOR_GPU_POWER:
422 ret = yellow_carp_get_smu_metrics_data(smu,
423 METRICS_AVERAGE_SOCKETPOWER,
427 case AMDGPU_PP_SENSOR_EDGE_TEMP:
428 ret = yellow_carp_get_smu_metrics_data(smu,
429 METRICS_TEMPERATURE_EDGE,
433 case AMDGPU_PP_SENSOR_HOTSPOT_TEMP:
434 ret = yellow_carp_get_smu_metrics_data(smu,
435 METRICS_TEMPERATURE_HOTSPOT,
439 case AMDGPU_PP_SENSOR_GFX_MCLK:
440 ret = yellow_carp_get_smu_metrics_data(smu,
441 METRICS_AVERAGE_UCLK,
443 *(uint32_t *)data *= 100;
446 case AMDGPU_PP_SENSOR_GFX_SCLK:
447 ret = yellow_carp_get_smu_metrics_data(smu,
448 METRICS_AVERAGE_GFXCLK,
450 *(uint32_t *)data *= 100;
453 case AMDGPU_PP_SENSOR_VDDGFX:
454 ret = yellow_carp_get_smu_metrics_data(smu,
455 METRICS_VOLTAGE_VDDGFX,
459 case AMDGPU_PP_SENSOR_VDDNB:
460 ret = yellow_carp_get_smu_metrics_data(smu,
461 METRICS_VOLTAGE_VDDSOC,
465 case AMDGPU_PP_SENSOR_SS_APU_SHARE:
466 ret = yellow_carp_get_smu_metrics_data(smu,
467 METRICS_SS_APU_SHARE,
471 case AMDGPU_PP_SENSOR_SS_DGPU_SHARE:
472 ret = yellow_carp_get_smu_metrics_data(smu,
473 METRICS_SS_DGPU_SHARE,
481 mutex_unlock(&smu->sensor_lock);
486 static int yellow_carp_set_watermarks_table(struct smu_context *smu,
487 struct pp_smu_wm_range_sets *clock_ranges)
491 Watermarks_t *table = smu->smu_table.watermarks_table;
493 if (!table || !clock_ranges)
497 if (clock_ranges->num_reader_wm_sets > NUM_WM_RANGES ||
498 clock_ranges->num_writer_wm_sets > NUM_WM_RANGES)
501 for (i = 0; i < clock_ranges->num_reader_wm_sets; i++) {
502 table->WatermarkRow[WM_DCFCLK][i].MinClock =
503 clock_ranges->reader_wm_sets[i].min_drain_clk_mhz;
504 table->WatermarkRow[WM_DCFCLK][i].MaxClock =
505 clock_ranges->reader_wm_sets[i].max_drain_clk_mhz;
506 table->WatermarkRow[WM_DCFCLK][i].MinMclk =
507 clock_ranges->reader_wm_sets[i].min_fill_clk_mhz;
508 table->WatermarkRow[WM_DCFCLK][i].MaxMclk =
509 clock_ranges->reader_wm_sets[i].max_fill_clk_mhz;
511 table->WatermarkRow[WM_DCFCLK][i].WmSetting =
512 clock_ranges->reader_wm_sets[i].wm_inst;
515 for (i = 0; i < clock_ranges->num_writer_wm_sets; i++) {
516 table->WatermarkRow[WM_SOCCLK][i].MinClock =
517 clock_ranges->writer_wm_sets[i].min_fill_clk_mhz;
518 table->WatermarkRow[WM_SOCCLK][i].MaxClock =
519 clock_ranges->writer_wm_sets[i].max_fill_clk_mhz;
520 table->WatermarkRow[WM_SOCCLK][i].MinMclk =
521 clock_ranges->writer_wm_sets[i].min_drain_clk_mhz;
522 table->WatermarkRow[WM_SOCCLK][i].MaxMclk =
523 clock_ranges->writer_wm_sets[i].max_drain_clk_mhz;
525 table->WatermarkRow[WM_SOCCLK][i].WmSetting =
526 clock_ranges->writer_wm_sets[i].wm_inst;
529 smu->watermarks_bitmap |= WATERMARKS_EXIST;
532 /* pass data to smu controller */
533 if ((smu->watermarks_bitmap & WATERMARKS_EXIST) &&
534 !(smu->watermarks_bitmap & WATERMARKS_LOADED)) {
535 ret = smu_cmn_write_watermarks_table(smu);
537 dev_err(smu->adev->dev, "Failed to update WMTABLE!");
540 smu->watermarks_bitmap |= WATERMARKS_LOADED;
546 static int yellow_carp_get_power_profile_mode(struct smu_context *smu,
549 static const char *profile_name[] = {
557 uint32_t i, size = 0;
558 int16_t workload_type = 0;
563 for (i = 0; i <= PP_SMC_POWER_PROFILE_CUSTOM; i++) {
565 * Conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT.
566 * Not all profile modes are supported on yellow carp.
568 workload_type = smu_cmn_to_asic_specific_index(smu,
569 CMN2ASIC_MAPPING_WORKLOAD,
572 if (workload_type < 0)
575 size += sysfs_emit_at(buf, size, "%2d %14s%s\n",
576 i, profile_name[i], (i == smu->power_profile_mode) ? "*" : " ");
582 static int yellow_carp_set_power_profile_mode(struct smu_context *smu,
583 long *input, uint32_t size)
585 int workload_type, ret;
586 uint32_t profile_mode = input[size];
588 if (profile_mode > PP_SMC_POWER_PROFILE_CUSTOM) {
589 dev_err(smu->adev->dev, "Invalid power profile mode %d\n", profile_mode);
593 if (profile_mode == PP_SMC_POWER_PROFILE_BOOTUP_DEFAULT ||
594 profile_mode == PP_SMC_POWER_PROFILE_POWERSAVING)
597 /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
598 workload_type = smu_cmn_to_asic_specific_index(smu,
599 CMN2ASIC_MAPPING_WORKLOAD,
601 if (workload_type < 0) {
602 dev_dbg(smu->adev->dev, "Unsupported power profile mode %d on YELLOWCARP\n",
607 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_ActiveProcessNotify,
611 dev_err_once(smu->adev->dev, "Fail to set workload type %d\n",
616 smu->power_profile_mode = profile_mode;
621 static ssize_t yellow_carp_get_gpu_metrics(struct smu_context *smu,
624 struct smu_table_context *smu_table = &smu->smu_table;
625 struct gpu_metrics_v2_1 *gpu_metrics =
626 (struct gpu_metrics_v2_1 *)smu_table->gpu_metrics_table;
627 SmuMetrics_t metrics;
630 ret = smu_cmn_get_metrics_table(smu, &metrics, true);
634 smu_cmn_init_soft_gpu_metrics(gpu_metrics, 2, 1);
636 gpu_metrics->temperature_gfx = metrics.GfxTemperature;
637 gpu_metrics->temperature_soc = metrics.SocTemperature;
638 memcpy(&gpu_metrics->temperature_core[0],
639 &metrics.CoreTemperature[0],
640 sizeof(uint16_t) * 8);
641 gpu_metrics->temperature_l3[0] = metrics.L3Temperature;
643 gpu_metrics->average_gfx_activity = metrics.GfxActivity;
644 gpu_metrics->average_mm_activity = metrics.UvdActivity;
646 gpu_metrics->average_socket_power = metrics.CurrentSocketPower;
647 gpu_metrics->average_gfx_power = metrics.Power[0];
648 gpu_metrics->average_soc_power = metrics.Power[1];
649 memcpy(&gpu_metrics->average_core_power[0],
650 &metrics.CorePower[0],
651 sizeof(uint16_t) * 8);
653 gpu_metrics->average_gfxclk_frequency = metrics.GfxclkFrequency;
654 gpu_metrics->average_socclk_frequency = metrics.SocclkFrequency;
655 gpu_metrics->average_uclk_frequency = metrics.MemclkFrequency;
656 gpu_metrics->average_fclk_frequency = metrics.MemclkFrequency;
657 gpu_metrics->average_vclk_frequency = metrics.VclkFrequency;
658 gpu_metrics->average_dclk_frequency = metrics.DclkFrequency;
660 memcpy(&gpu_metrics->current_coreclk[0],
661 &metrics.CoreFrequency[0],
662 sizeof(uint16_t) * 8);
663 gpu_metrics->current_l3clk[0] = metrics.L3Frequency;
665 gpu_metrics->throttle_status = metrics.ThrottlerStatus;
667 gpu_metrics->system_clock_counter = ktime_get_boottime_ns();
669 *table = (void *)gpu_metrics;
671 return sizeof(struct gpu_metrics_v2_1);
674 static int yellow_carp_set_default_dpm_tables(struct smu_context *smu)
676 struct smu_table_context *smu_table = &smu->smu_table;
678 return smu_cmn_update_table(smu, SMU_TABLE_DPMCLOCKS, 0, smu_table->clocks_table, false);
681 static int yellow_carp_od_edit_dpm_table(struct smu_context *smu, enum PP_OD_DPM_TABLE_COMMAND type,
682 long input[], uint32_t size)
684 struct smu_dpm_context *smu_dpm = &(smu->smu_dpm);
687 /* Only allowed in manual mode */
688 if (smu_dpm->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
692 case PP_OD_EDIT_SCLK_VDDC_TABLE:
694 dev_err(smu->adev->dev, "Input parameter number not correct\n");
699 if (input[1] < smu->gfx_default_hard_min_freq) {
700 dev_warn(smu->adev->dev,
701 "Fine grain setting minimum sclk (%ld) MHz is less than the minimum allowed (%d) MHz\n",
702 input[1], smu->gfx_default_hard_min_freq);
705 smu->gfx_actual_hard_min_freq = input[1];
706 } else if (input[0] == 1) {
707 if (input[1] > smu->gfx_default_soft_max_freq) {
708 dev_warn(smu->adev->dev,
709 "Fine grain setting maximum sclk (%ld) MHz is greater than the maximum allowed (%d) MHz\n",
710 input[1], smu->gfx_default_soft_max_freq);
713 smu->gfx_actual_soft_max_freq = input[1];
718 case PP_OD_RESTORE_DEFAULT_TABLE:
720 dev_err(smu->adev->dev, "Input parameter number not correct\n");
723 smu->gfx_actual_hard_min_freq = smu->gfx_default_hard_min_freq;
724 smu->gfx_actual_soft_max_freq = smu->gfx_default_soft_max_freq;
727 case PP_OD_COMMIT_DPM_TABLE:
729 dev_err(smu->adev->dev, "Input parameter number not correct\n");
732 if (smu->gfx_actual_hard_min_freq > smu->gfx_actual_soft_max_freq) {
733 dev_err(smu->adev->dev,
734 "The setting minimum sclk (%d) MHz is greater than the setting maximum sclk (%d) MHz\n",
735 smu->gfx_actual_hard_min_freq,
736 smu->gfx_actual_soft_max_freq);
740 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_SetHardMinGfxClk,
741 smu->gfx_actual_hard_min_freq, NULL);
743 dev_err(smu->adev->dev, "Set hard min sclk failed!");
747 ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_SetSoftMaxGfxClk,
748 smu->gfx_actual_soft_max_freq, NULL);
750 dev_err(smu->adev->dev, "Set soft max sclk failed!");
762 static int yellow_carp_get_current_clk_freq(struct smu_context *smu,
763 enum smu_clk_type clk_type,
766 MetricsMember_t member_type;
770 member_type = METRICS_AVERAGE_SOCCLK;
773 member_type = METRICS_AVERAGE_VCLK;
776 member_type = METRICS_AVERAGE_DCLK;
779 member_type = METRICS_AVERAGE_UCLK;
782 return smu_cmn_send_smc_msg_with_param(smu,
783 SMU_MSG_GetFclkFrequency, 0, value);
788 return yellow_carp_get_smu_metrics_data(smu, member_type, value);
791 static int yellow_carp_get_dpm_level_count(struct smu_context *smu,
792 enum smu_clk_type clk_type,
795 DpmClocks_t *clk_table = smu->smu_table.clocks_table;
799 *count = clk_table->NumSocClkLevelsEnabled;
802 *count = clk_table->VcnClkLevelsEnabled;
805 *count = clk_table->VcnClkLevelsEnabled;
808 *count = clk_table->NumDfPstatesEnabled;
811 *count = clk_table->NumDfPstatesEnabled;
820 static int yellow_carp_get_dpm_freq_by_index(struct smu_context *smu,
821 enum smu_clk_type clk_type,
825 DpmClocks_t *clk_table = smu->smu_table.clocks_table;
827 if (!clk_table || clk_type >= SMU_CLK_COUNT)
832 if (dpm_level >= clk_table->NumSocClkLevelsEnabled)
834 *freq = clk_table->SocClocks[dpm_level];
837 if (dpm_level >= clk_table->VcnClkLevelsEnabled)
839 *freq = clk_table->VClocks[dpm_level];
842 if (dpm_level >= clk_table->VcnClkLevelsEnabled)
844 *freq = clk_table->DClocks[dpm_level];
848 if (dpm_level >= clk_table->NumDfPstatesEnabled)
850 *freq = clk_table->DfPstateTable[dpm_level].MemClk;
853 if (dpm_level >= clk_table->NumDfPstatesEnabled)
855 *freq = clk_table->DfPstateTable[dpm_level].FClk;
864 static bool yellow_carp_clk_dpm_is_enabled(struct smu_context *smu,
865 enum smu_clk_type clk_type)
867 enum smu_feature_mask feature_id = 0;
873 feature_id = SMU_FEATURE_DPM_FCLK_BIT;
877 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
880 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
884 feature_id = SMU_FEATURE_VCN_DPM_BIT;
890 return smu_cmn_feature_is_enabled(smu, feature_id);
893 static int yellow_carp_get_dpm_ultimate_freq(struct smu_context *smu,
894 enum smu_clk_type clk_type,
898 DpmClocks_t *clk_table = smu->smu_table.clocks_table;
899 uint32_t clock_limit;
900 uint32_t max_dpm_level, min_dpm_level;
903 if (!yellow_carp_clk_dpm_is_enabled(smu, clk_type)) {
907 clock_limit = smu->smu_table.boot_values.uclk;
910 clock_limit = smu->smu_table.boot_values.fclk;
914 clock_limit = smu->smu_table.boot_values.gfxclk;
917 clock_limit = smu->smu_table.boot_values.socclk;
920 clock_limit = smu->smu_table.boot_values.vclk;
923 clock_limit = smu->smu_table.boot_values.dclk;
930 /* clock in Mhz unit */
932 *min = clock_limit / 100;
934 *max = clock_limit / 100;
943 *max = clk_table->MaxGfxClk;
951 max_dpm_level = clk_table->NumSocClkLevelsEnabled - 1;
955 max_dpm_level = clk_table->VcnClkLevelsEnabled - 1;
962 if (clk_type != SMU_GFXCLK && clk_type != SMU_SCLK) {
963 ret = yellow_carp_get_dpm_freq_by_index(smu, clk_type, max_dpm_level, max);
973 *min = clk_table->MinGfxClk;
978 min_dpm_level = clk_table->NumDfPstatesEnabled - 1;
992 if (clk_type != SMU_GFXCLK && clk_type != SMU_SCLK) {
993 ret = yellow_carp_get_dpm_freq_by_index(smu, clk_type, min_dpm_level, min);
1003 static int yellow_carp_set_soft_freq_limited_range(struct smu_context *smu,
1004 enum smu_clk_type clk_type,
1008 enum smu_message_type msg_set_min, msg_set_max;
1011 if (!yellow_carp_clk_dpm_is_enabled(smu, clk_type))
1017 msg_set_min = SMU_MSG_SetHardMinGfxClk;
1018 msg_set_max = SMU_MSG_SetSoftMaxGfxClk;
1021 msg_set_min = SMU_MSG_SetHardMinFclkByFreq;
1022 msg_set_max = SMU_MSG_SetSoftMaxFclkByFreq;
1025 msg_set_min = SMU_MSG_SetHardMinSocclkByFreq;
1026 msg_set_max = SMU_MSG_SetSoftMaxSocclkByFreq;
1030 msg_set_min = SMU_MSG_SetHardMinVcn;
1031 msg_set_max = SMU_MSG_SetSoftMaxVcn;
1037 ret = smu_cmn_send_smc_msg_with_param(smu, msg_set_min, min, NULL);
1041 ret = smu_cmn_send_smc_msg_with_param(smu, msg_set_max, max, NULL);
1049 static int yellow_carp_print_clk_levels(struct smu_context *smu,
1050 enum smu_clk_type clk_type, char *buf)
1052 int i, size = 0, ret = 0;
1053 uint32_t cur_value = 0, value = 0, count = 0;
1055 smu_cmn_get_sysfs_buf(&buf, &size);
1059 size += sysfs_emit_at(buf, size, "%s:\n", "OD_SCLK");
1060 size += sysfs_emit_at(buf, size, "0: %10uMhz\n",
1061 (smu->gfx_actual_hard_min_freq > 0) ? smu->gfx_actual_hard_min_freq : smu->gfx_default_hard_min_freq);
1062 size += sysfs_emit_at(buf, size, "1: %10uMhz\n",
1063 (smu->gfx_actual_soft_max_freq > 0) ? smu->gfx_actual_soft_max_freq : smu->gfx_default_soft_max_freq);
1066 size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
1067 size += sysfs_emit_at(buf, size, "SCLK: %7uMhz %10uMhz\n",
1068 smu->gfx_default_hard_min_freq, smu->gfx_default_soft_max_freq);
1075 ret = yellow_carp_get_current_clk_freq(smu, clk_type, &cur_value);
1079 ret = yellow_carp_get_dpm_level_count(smu, clk_type, &count);
1083 for (i = 0; i < count; i++) {
1084 ret = yellow_carp_get_dpm_freq_by_index(smu, clk_type, i, &value);
1088 size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i, value,
1089 cur_value == value ? "*" : "");
1100 static int yellow_carp_force_clk_levels(struct smu_context *smu,
1101 enum smu_clk_type clk_type, uint32_t mask)
1103 uint32_t soft_min_level = 0, soft_max_level = 0;
1104 uint32_t min_freq = 0, max_freq = 0;
1107 soft_min_level = mask ? (ffs(mask) - 1) : 0;
1108 soft_max_level = mask ? (fls(mask) - 1) : 0;
1115 ret = yellow_carp_get_dpm_freq_by_index(smu, clk_type, soft_min_level, &min_freq);
1117 goto force_level_out;
1119 ret = yellow_carp_get_dpm_freq_by_index(smu, clk_type, soft_max_level, &max_freq);
1121 goto force_level_out;
1123 ret = yellow_carp_set_soft_freq_limited_range(smu, clk_type, min_freq, max_freq);
1125 goto force_level_out;
1136 static int yellow_carp_set_performance_level(struct smu_context *smu,
1137 enum amd_dpm_forced_level level)
1139 struct amdgpu_device *adev = smu->adev;
1140 uint32_t sclk_min = 0, sclk_max = 0;
1141 uint32_t fclk_min = 0, fclk_max = 0;
1142 uint32_t socclk_min = 0, socclk_max = 0;
1146 case AMD_DPM_FORCED_LEVEL_HIGH:
1147 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SCLK, NULL, &sclk_max);
1148 yellow_carp_get_dpm_ultimate_freq(smu, SMU_FCLK, NULL, &fclk_max);
1149 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SOCCLK, NULL, &socclk_max);
1150 sclk_min = sclk_max;
1151 fclk_min = fclk_max;
1152 socclk_min = socclk_max;
1154 case AMD_DPM_FORCED_LEVEL_LOW:
1155 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SCLK, &sclk_min, NULL);
1156 yellow_carp_get_dpm_ultimate_freq(smu, SMU_FCLK, &fclk_min, NULL);
1157 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SOCCLK, &socclk_min, NULL);
1158 sclk_max = sclk_min;
1159 fclk_max = fclk_min;
1160 socclk_max = socclk_min;
1162 case AMD_DPM_FORCED_LEVEL_AUTO:
1163 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SCLK, &sclk_min, &sclk_max);
1164 yellow_carp_get_dpm_ultimate_freq(smu, SMU_FCLK, &fclk_min, &fclk_max);
1165 yellow_carp_get_dpm_ultimate_freq(smu, SMU_SOCCLK, &socclk_min, &socclk_max);
1167 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1168 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
1169 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
1170 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
1171 /* Temporarily do nothing since the optimal clocks haven't been provided yet */
1173 case AMD_DPM_FORCED_LEVEL_MANUAL:
1174 case AMD_DPM_FORCED_LEVEL_PROFILE_EXIT:
1177 dev_err(adev->dev, "Invalid performance level %d\n", level);
1181 if (sclk_min && sclk_max) {
1182 ret = yellow_carp_set_soft_freq_limited_range(smu,
1189 smu->gfx_actual_hard_min_freq = sclk_min;
1190 smu->gfx_actual_soft_max_freq = sclk_max;
1193 if (fclk_min && fclk_max) {
1194 ret = yellow_carp_set_soft_freq_limited_range(smu,
1202 if (socclk_min && socclk_max) {
1203 ret = yellow_carp_set_soft_freq_limited_range(smu,
1214 static int yellow_carp_set_fine_grain_gfx_freq_parameters(struct smu_context *smu)
1216 DpmClocks_t *clk_table = smu->smu_table.clocks_table;
1218 smu->gfx_default_hard_min_freq = clk_table->MinGfxClk;
1219 smu->gfx_default_soft_max_freq = clk_table->MaxGfxClk;
1220 smu->gfx_actual_hard_min_freq = 0;
1221 smu->gfx_actual_soft_max_freq = 0;
1226 static const struct pptable_funcs yellow_carp_ppt_funcs = {
1227 .check_fw_status = smu_v13_0_check_fw_status,
1228 .check_fw_version = smu_v13_0_check_fw_version,
1229 .init_smc_tables = yellow_carp_init_smc_tables,
1230 .fini_smc_tables = yellow_carp_fini_smc_tables,
1231 .get_vbios_bootup_values = smu_v13_0_get_vbios_bootup_values,
1232 .system_features_control = yellow_carp_system_features_control,
1233 .send_smc_msg_with_param = smu_cmn_send_smc_msg_with_param,
1234 .send_smc_msg = smu_cmn_send_smc_msg,
1235 .dpm_set_vcn_enable = yellow_carp_dpm_set_vcn_enable,
1236 .dpm_set_jpeg_enable = yellow_carp_dpm_set_jpeg_enable,
1237 .set_default_dpm_table = yellow_carp_set_default_dpm_tables,
1238 .read_sensor = yellow_carp_read_sensor,
1239 .is_dpm_running = yellow_carp_is_dpm_running,
1240 .set_watermarks_table = yellow_carp_set_watermarks_table,
1241 .get_power_profile_mode = yellow_carp_get_power_profile_mode,
1242 .set_power_profile_mode = yellow_carp_set_power_profile_mode,
1243 .get_gpu_metrics = yellow_carp_get_gpu_metrics,
1244 .get_enabled_mask = smu_cmn_get_enabled_32_bits_mask,
1245 .get_pp_feature_mask = smu_cmn_get_pp_feature_mask,
1246 .set_driver_table_location = smu_v13_0_set_driver_table_location,
1247 .gfx_off_control = smu_v13_0_gfx_off_control,
1248 .post_init = yellow_carp_post_smu_init,
1249 .mode2_reset = yellow_carp_mode2_reset,
1250 .get_dpm_ultimate_freq = yellow_carp_get_dpm_ultimate_freq,
1251 .od_edit_dpm_table = yellow_carp_od_edit_dpm_table,
1252 .print_clk_levels = yellow_carp_print_clk_levels,
1253 .force_clk_levels = yellow_carp_force_clk_levels,
1254 .set_performance_level = yellow_carp_set_performance_level,
1255 .set_fine_grain_gfx_freq_parameters = yellow_carp_set_fine_grain_gfx_freq_parameters,
1258 void yellow_carp_set_ppt_funcs(struct smu_context *smu)
1260 smu->ppt_funcs = &yellow_carp_ppt_funcs;
1261 smu->message_map = yellow_carp_message_map;
1262 smu->feature_map = yellow_carp_feature_mask_map;
1263 smu->table_map = yellow_carp_table_map;
1264 smu->workload_map = yellow_carp_workload_map;