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 
24 #define SWSMU_CODE_LAYER_L2
25 
26 #include <linux/firmware.h>
27 #include "amdgpu.h"
28 #include "amdgpu_smu.h"
29 #include "atomfirmware.h"
30 #include "amdgpu_atomfirmware.h"
31 #include "amdgpu_atombios.h"
32 #include "smu_v13_0.h"
33 #include "smu13_driver_if_aldebaran.h"
34 #include "soc15_common.h"
35 #include "atom.h"
36 #include "power_state.h"
37 #include "aldebaran_ppt.h"
38 #include "smu_v13_0_pptable.h"
39 #include "aldebaran_ppsmc.h"
40 #include "nbio/nbio_7_4_offset.h"
41 #include "nbio/nbio_7_4_sh_mask.h"
42 #include "thm/thm_11_0_2_offset.h"
43 #include "thm/thm_11_0_2_sh_mask.h"
44 #include "amdgpu_xgmi.h"
45 #include <linux/pci.h>
46 #include "amdgpu_ras.h"
47 #include "smu_cmn.h"
48 #include "mp/mp_13_0_2_offset.h"
49 
50 /*
51  * DO NOT use these for err/warn/info/debug messages.
52  * Use dev_err, dev_warn, dev_info and dev_dbg instead.
53  * They are more MGPU friendly.
54  */
55 #undef pr_err
56 #undef pr_warn
57 #undef pr_info
58 #undef pr_debug
59 
60 #define to_amdgpu_device(x) (container_of(x, struct amdgpu_device, pm.smu_i2c))
61 
62 #define ALDEBARAN_FEA_MAP(smu_feature, aldebaran_feature) \
63 	[smu_feature] = {1, (aldebaran_feature)}
64 
65 #define FEATURE_MASK(feature) (1ULL << feature)
66 #define SMC_DPM_FEATURE ( \
67 			  FEATURE_MASK(FEATURE_DATA_CALCULATIONS) | \
68 			  FEATURE_MASK(FEATURE_DPM_GFXCLK_BIT)	| \
69 			  FEATURE_MASK(FEATURE_DPM_UCLK_BIT)	| \
70 			  FEATURE_MASK(FEATURE_DPM_SOCCLK_BIT)	| \
71 			  FEATURE_MASK(FEATURE_DPM_FCLK_BIT)	| \
72 			  FEATURE_MASK(FEATURE_DPM_LCLK_BIT)	| \
73 			  FEATURE_MASK(FEATURE_DPM_XGMI_BIT)	| \
74 			  FEATURE_MASK(FEATURE_DPM_VCN_BIT))
75 
76 /* possible frequency drift (1Mhz) */
77 #define EPSILON				1
78 
79 #define smnPCIE_ESM_CTRL			0x111003D0
80 
81 static const struct smu_temperature_range smu13_thermal_policy[] =
82 {
83 	{-273150,  99000, 99000, -273150, 99000, 99000, -273150, 99000, 99000},
84 	{ 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000},
85 };
86 
87 static const struct cmn2asic_msg_mapping aldebaran_message_map[SMU_MSG_MAX_COUNT] = {
88 	MSG_MAP(TestMessage,			     PPSMC_MSG_TestMessage,			0),
89 	MSG_MAP(GetSmuVersion,			     PPSMC_MSG_GetSmuVersion,			1),
90 	MSG_MAP(GetDriverIfVersion,		     PPSMC_MSG_GetDriverIfVersion,		1),
91 	MSG_MAP(EnableAllSmuFeatures,		     PPSMC_MSG_EnableAllSmuFeatures,		0),
92 	MSG_MAP(DisableAllSmuFeatures,		     PPSMC_MSG_DisableAllSmuFeatures,		0),
93 	MSG_MAP(GetEnabledSmuFeaturesLow,	     PPSMC_MSG_GetEnabledSmuFeaturesLow,	1),
94 	MSG_MAP(GetEnabledSmuFeaturesHigh,	     PPSMC_MSG_GetEnabledSmuFeaturesHigh,	1),
95 	MSG_MAP(SetDriverDramAddrHigh,		     PPSMC_MSG_SetDriverDramAddrHigh,		1),
96 	MSG_MAP(SetDriverDramAddrLow,		     PPSMC_MSG_SetDriverDramAddrLow,		1),
97 	MSG_MAP(SetToolsDramAddrHigh,		     PPSMC_MSG_SetToolsDramAddrHigh,		0),
98 	MSG_MAP(SetToolsDramAddrLow,		     PPSMC_MSG_SetToolsDramAddrLow,		0),
99 	MSG_MAP(TransferTableSmu2Dram,		     PPSMC_MSG_TransferTableSmu2Dram,		1),
100 	MSG_MAP(TransferTableDram2Smu,		     PPSMC_MSG_TransferTableDram2Smu,		0),
101 	MSG_MAP(UseDefaultPPTable,		     PPSMC_MSG_UseDefaultPPTable,		0),
102 	MSG_MAP(SetSystemVirtualDramAddrHigh,	     PPSMC_MSG_SetSystemVirtualDramAddrHigh,	0),
103 	MSG_MAP(SetSystemVirtualDramAddrLow,	     PPSMC_MSG_SetSystemVirtualDramAddrLow,	0),
104 	MSG_MAP(SetSoftMinByFreq,		     PPSMC_MSG_SetSoftMinByFreq,		0),
105 	MSG_MAP(SetSoftMaxByFreq,		     PPSMC_MSG_SetSoftMaxByFreq,		0),
106 	MSG_MAP(SetHardMinByFreq,		     PPSMC_MSG_SetHardMinByFreq,		0),
107 	MSG_MAP(SetHardMaxByFreq,		     PPSMC_MSG_SetHardMaxByFreq,		0),
108 	MSG_MAP(GetMinDpmFreq,			     PPSMC_MSG_GetMinDpmFreq,			0),
109 	MSG_MAP(GetMaxDpmFreq,			     PPSMC_MSG_GetMaxDpmFreq,			0),
110 	MSG_MAP(GetDpmFreqByIndex,		     PPSMC_MSG_GetDpmFreqByIndex,		1),
111 	MSG_MAP(SetWorkloadMask,		     PPSMC_MSG_SetWorkloadMask,			1),
112 	MSG_MAP(GetVoltageByDpm,		     PPSMC_MSG_GetVoltageByDpm,			0),
113 	MSG_MAP(GetVoltageByDpmOverdrive,	     PPSMC_MSG_GetVoltageByDpmOverdrive,	0),
114 	MSG_MAP(SetPptLimit,			     PPSMC_MSG_SetPptLimit,			0),
115 	MSG_MAP(GetPptLimit,			     PPSMC_MSG_GetPptLimit,			1),
116 	MSG_MAP(PrepareMp1ForUnload,		     PPSMC_MSG_PrepareMp1ForUnload,		0),
117 	MSG_MAP(GfxDeviceDriverReset,		     PPSMC_MSG_GfxDriverReset,			0),
118 	MSG_MAP(RunDcBtc,			     PPSMC_MSG_RunDcBtc,			0),
119 	MSG_MAP(DramLogSetDramAddrHigh,		     PPSMC_MSG_DramLogSetDramAddrHigh,		0),
120 	MSG_MAP(DramLogSetDramAddrLow,		     PPSMC_MSG_DramLogSetDramAddrLow,		0),
121 	MSG_MAP(DramLogSetDramSize,		     PPSMC_MSG_DramLogSetDramSize,		0),
122 	MSG_MAP(GetDebugData,			     PPSMC_MSG_GetDebugData,			0),
123 	MSG_MAP(WaflTest,			     PPSMC_MSG_WaflTest,			0),
124 	MSG_MAP(SetMemoryChannelEnable,		     PPSMC_MSG_SetMemoryChannelEnable,		0),
125 	MSG_MAP(SetNumBadHbmPagesRetired,	     PPSMC_MSG_SetNumBadHbmPagesRetired,	0),
126 	MSG_MAP(DFCstateControl,		     PPSMC_MSG_DFCstateControl,			0),
127 	MSG_MAP(GetGmiPwrDnHyst,		     PPSMC_MSG_GetGmiPwrDnHyst,			0),
128 	MSG_MAP(SetGmiPwrDnHyst,		     PPSMC_MSG_SetGmiPwrDnHyst,			0),
129 	MSG_MAP(GmiPwrDnControl,		     PPSMC_MSG_GmiPwrDnControl,			0),
130 	MSG_MAP(EnterGfxoff,			     PPSMC_MSG_EnterGfxoff,			0),
131 	MSG_MAP(ExitGfxoff,			     PPSMC_MSG_ExitGfxoff,			0),
132 	MSG_MAP(SetExecuteDMATest,		     PPSMC_MSG_SetExecuteDMATest,		0),
133 	MSG_MAP(EnableDeterminism,		     PPSMC_MSG_EnableDeterminism,		0),
134 	MSG_MAP(DisableDeterminism,		     PPSMC_MSG_DisableDeterminism,		0),
135 	MSG_MAP(SetUclkDpmMode,			     PPSMC_MSG_SetUclkDpmMode,			0),
136 	MSG_MAP(GfxDriverResetRecovery,		     PPSMC_MSG_GfxDriverResetRecovery,		0),
137 	MSG_MAP(BoardPowerCalibration,		     PPSMC_MSG_BoardPowerCalibration,		0),
138 };
139 
140 static const struct cmn2asic_mapping aldebaran_clk_map[SMU_CLK_COUNT] = {
141 	CLK_MAP(GFXCLK, PPCLK_GFXCLK),
142 	CLK_MAP(SCLK,	PPCLK_GFXCLK),
143 	CLK_MAP(SOCCLK, PPCLK_SOCCLK),
144 	CLK_MAP(FCLK, PPCLK_FCLK),
145 	CLK_MAP(UCLK, PPCLK_UCLK),
146 	CLK_MAP(MCLK, PPCLK_UCLK),
147 	CLK_MAP(DCLK, PPCLK_DCLK),
148 	CLK_MAP(VCLK, PPCLK_VCLK),
149 	CLK_MAP(LCLK, 	PPCLK_LCLK),
150 };
151 
152 static const struct cmn2asic_mapping aldebaran_feature_mask_map[SMU_FEATURE_COUNT] = {
153 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DATA_CALCULATIONS_BIT, 		FEATURE_DATA_CALCULATIONS),
154 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_GFXCLK_BIT, 			FEATURE_DPM_GFXCLK_BIT),
155 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_UCLK_BIT, 			FEATURE_DPM_UCLK_BIT),
156 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_SOCCLK_BIT, 			FEATURE_DPM_SOCCLK_BIT),
157 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_FCLK_BIT, 			FEATURE_DPM_FCLK_BIT),
158 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_LCLK_BIT, 			FEATURE_DPM_LCLK_BIT),
159 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DPM_XGMI_BIT, 				FEATURE_DPM_XGMI_BIT),
160 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DS_GFXCLK_BIT, 			FEATURE_DS_GFXCLK_BIT),
161 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DS_SOCCLK_BIT, 			FEATURE_DS_SOCCLK_BIT),
162 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DS_LCLK_BIT, 				FEATURE_DS_LCLK_BIT),
163 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DS_FCLK_BIT, 				FEATURE_DS_FCLK_BIT),
164 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DS_UCLK_BIT,				FEATURE_DS_UCLK_BIT),
165 	ALDEBARAN_FEA_MAP(SMU_FEATURE_GFX_SS_BIT, 				FEATURE_GFX_SS_BIT),
166 	ALDEBARAN_FEA_MAP(SMU_FEATURE_VCN_DPM_BIT, 				FEATURE_DPM_VCN_BIT),
167 	ALDEBARAN_FEA_MAP(SMU_FEATURE_RSMU_SMN_CG_BIT, 			FEATURE_RSMU_SMN_CG_BIT),
168 	ALDEBARAN_FEA_MAP(SMU_FEATURE_WAFL_CG_BIT, 				FEATURE_WAFL_CG_BIT),
169 	ALDEBARAN_FEA_MAP(SMU_FEATURE_PPT_BIT, 					FEATURE_PPT_BIT),
170 	ALDEBARAN_FEA_MAP(SMU_FEATURE_TDC_BIT, 					FEATURE_TDC_BIT),
171 	ALDEBARAN_FEA_MAP(SMU_FEATURE_APCC_PLUS_BIT, 			FEATURE_APCC_PLUS_BIT),
172 	ALDEBARAN_FEA_MAP(SMU_FEATURE_APCC_DFLL_BIT, 			FEATURE_APCC_DFLL_BIT),
173 	ALDEBARAN_FEA_MAP(SMU_FEATURE_FUSE_CG_BIT, 				FEATURE_FUSE_CG_BIT),
174 	ALDEBARAN_FEA_MAP(SMU_FEATURE_MP1_CG_BIT, 				FEATURE_MP1_CG_BIT),
175 	ALDEBARAN_FEA_MAP(SMU_FEATURE_SMUIO_CG_BIT, 			FEATURE_SMUIO_CG_BIT),
176 	ALDEBARAN_FEA_MAP(SMU_FEATURE_THM_CG_BIT, 				FEATURE_THM_CG_BIT),
177 	ALDEBARAN_FEA_MAP(SMU_FEATURE_CLK_CG_BIT, 				FEATURE_CLK_CG_BIT),
178 	ALDEBARAN_FEA_MAP(SMU_FEATURE_FW_CTF_BIT, 				FEATURE_FW_CTF_BIT),
179 	ALDEBARAN_FEA_MAP(SMU_FEATURE_THERMAL_BIT, 				FEATURE_THERMAL_BIT),
180 	ALDEBARAN_FEA_MAP(SMU_FEATURE_OUT_OF_BAND_MONITOR_BIT, 	FEATURE_OUT_OF_BAND_MONITOR_BIT),
181 	ALDEBARAN_FEA_MAP(SMU_FEATURE_XGMI_PER_LINK_PWR_DWN_BIT,FEATURE_XGMI_PER_LINK_PWR_DWN),
182 	ALDEBARAN_FEA_MAP(SMU_FEATURE_DF_CSTATE_BIT, 			FEATURE_DF_CSTATE),
183 };
184 
185 static const struct cmn2asic_mapping aldebaran_table_map[SMU_TABLE_COUNT] = {
186 	TAB_MAP(PPTABLE),
187 	TAB_MAP(AVFS_PSM_DEBUG),
188 	TAB_MAP(AVFS_FUSE_OVERRIDE),
189 	TAB_MAP(PMSTATUSLOG),
190 	TAB_MAP(SMU_METRICS),
191 	TAB_MAP(DRIVER_SMU_CONFIG),
192 	TAB_MAP(I2C_COMMANDS),
193 };
194 
195 static const uint8_t aldebaran_throttler_map[] = {
196 	[THROTTLER_PPT0_BIT]		= (SMU_THROTTLER_PPT0_BIT),
197 	[THROTTLER_PPT1_BIT]		= (SMU_THROTTLER_PPT1_BIT),
198 	[THROTTLER_TDC_GFX_BIT]		= (SMU_THROTTLER_TDC_GFX_BIT),
199 	[THROTTLER_TDC_SOC_BIT]		= (SMU_THROTTLER_TDC_SOC_BIT),
200 	[THROTTLER_TDC_HBM_BIT]		= (SMU_THROTTLER_TDC_MEM_BIT),
201 	[THROTTLER_TEMP_GPU_BIT]	= (SMU_THROTTLER_TEMP_GPU_BIT),
202 	[THROTTLER_TEMP_MEM_BIT]	= (SMU_THROTTLER_TEMP_MEM_BIT),
203 	[THROTTLER_TEMP_VR_GFX_BIT]	= (SMU_THROTTLER_TEMP_VR_GFX_BIT),
204 	[THROTTLER_TEMP_VR_SOC_BIT]	= (SMU_THROTTLER_TEMP_VR_SOC_BIT),
205 	[THROTTLER_TEMP_VR_MEM_BIT]	= (SMU_THROTTLER_TEMP_VR_MEM0_BIT),
206 	[THROTTLER_APCC_BIT]		= (SMU_THROTTLER_APCC_BIT),
207 };
208 
209 static int aldebaran_tables_init(struct smu_context *smu)
210 {
211 	struct smu_table_context *smu_table = &smu->smu_table;
212 	struct smu_table *tables = smu_table->tables;
213 
214 	SMU_TABLE_INIT(tables, SMU_TABLE_PPTABLE, sizeof(PPTable_t),
215 		       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
216 
217 	SMU_TABLE_INIT(tables, SMU_TABLE_PMSTATUSLOG, SMU13_TOOL_SIZE,
218 		       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
219 
220 	SMU_TABLE_INIT(tables, SMU_TABLE_SMU_METRICS, sizeof(SmuMetrics_t),
221 		       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
222 
223 	SMU_TABLE_INIT(tables, SMU_TABLE_I2C_COMMANDS, sizeof(SwI2cRequest_t),
224 		       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
225 
226 	smu_table->metrics_table = kzalloc(sizeof(SmuMetrics_t), GFP_KERNEL);
227 	if (!smu_table->metrics_table)
228 		return -ENOMEM;
229 	smu_table->metrics_time = 0;
230 
231 	smu_table->gpu_metrics_table_size = sizeof(struct gpu_metrics_v1_3);
232 	smu_table->gpu_metrics_table = kzalloc(smu_table->gpu_metrics_table_size, GFP_KERNEL);
233 	if (!smu_table->gpu_metrics_table) {
234 		kfree(smu_table->metrics_table);
235 		return -ENOMEM;
236 	}
237 
238 	return 0;
239 }
240 
241 static int aldebaran_allocate_dpm_context(struct smu_context *smu)
242 {
243 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
244 
245 	smu_dpm->dpm_context = kzalloc(sizeof(struct smu_13_0_dpm_context),
246 				       GFP_KERNEL);
247 	if (!smu_dpm->dpm_context)
248 		return -ENOMEM;
249 	smu_dpm->dpm_context_size = sizeof(struct smu_13_0_dpm_context);
250 
251 	smu_dpm->dpm_current_power_state = kzalloc(sizeof(struct smu_power_state),
252 						   GFP_KERNEL);
253 	if (!smu_dpm->dpm_current_power_state)
254 		return -ENOMEM;
255 
256 	smu_dpm->dpm_request_power_state = kzalloc(sizeof(struct smu_power_state),
257 						   GFP_KERNEL);
258 	if (!smu_dpm->dpm_request_power_state)
259 		return -ENOMEM;
260 
261 	return 0;
262 }
263 
264 static int aldebaran_init_smc_tables(struct smu_context *smu)
265 {
266 	int ret = 0;
267 
268 	ret = aldebaran_tables_init(smu);
269 	if (ret)
270 		return ret;
271 
272 	ret = aldebaran_allocate_dpm_context(smu);
273 	if (ret)
274 		return ret;
275 
276 	return smu_v13_0_init_smc_tables(smu);
277 }
278 
279 static int aldebaran_get_allowed_feature_mask(struct smu_context *smu,
280 					      uint32_t *feature_mask, uint32_t num)
281 {
282 	if (num > 2)
283 		return -EINVAL;
284 
285 	/* pptable will handle the features to enable */
286 	memset(feature_mask, 0xFF, sizeof(uint32_t) * num);
287 
288 	return 0;
289 }
290 
291 static int aldebaran_set_default_dpm_table(struct smu_context *smu)
292 {
293 	struct smu_13_0_dpm_context *dpm_context = smu->smu_dpm.dpm_context;
294 	struct smu_13_0_dpm_table *dpm_table = NULL;
295 	PPTable_t *pptable = smu->smu_table.driver_pptable;
296 	int ret = 0;
297 
298 	/* socclk dpm table setup */
299 	dpm_table = &dpm_context->dpm_tables.soc_table;
300 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT)) {
301 		ret = smu_v13_0_set_single_dpm_table(smu,
302 						     SMU_SOCCLK,
303 						     dpm_table);
304 		if (ret)
305 			return ret;
306 	} else {
307 		dpm_table->count = 1;
308 		dpm_table->dpm_levels[0].value = smu->smu_table.boot_values.socclk / 100;
309 		dpm_table->dpm_levels[0].enabled = true;
310 		dpm_table->min = dpm_table->dpm_levels[0].value;
311 		dpm_table->max = dpm_table->dpm_levels[0].value;
312 	}
313 
314 	/* gfxclk dpm table setup */
315 	dpm_table = &dpm_context->dpm_tables.gfx_table;
316 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_GFXCLK_BIT)) {
317 		/* in the case of gfxclk, only fine-grained dpm is honored */
318 		dpm_table->count = 2;
319 		dpm_table->dpm_levels[0].value = pptable->GfxclkFmin;
320 		dpm_table->dpm_levels[0].enabled = true;
321 		dpm_table->dpm_levels[1].value = pptable->GfxclkFmax;
322 		dpm_table->dpm_levels[1].enabled = true;
323 		dpm_table->min = dpm_table->dpm_levels[0].value;
324 		dpm_table->max = dpm_table->dpm_levels[1].value;
325 	} else {
326 		dpm_table->count = 1;
327 		dpm_table->dpm_levels[0].value = smu->smu_table.boot_values.gfxclk / 100;
328 		dpm_table->dpm_levels[0].enabled = true;
329 		dpm_table->min = dpm_table->dpm_levels[0].value;
330 		dpm_table->max = dpm_table->dpm_levels[0].value;
331 	}
332 
333 	/* memclk dpm table setup */
334 	dpm_table = &dpm_context->dpm_tables.uclk_table;
335 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_UCLK_BIT)) {
336 		ret = smu_v13_0_set_single_dpm_table(smu,
337 						     SMU_UCLK,
338 						     dpm_table);
339 		if (ret)
340 			return ret;
341 	} else {
342 		dpm_table->count = 1;
343 		dpm_table->dpm_levels[0].value = smu->smu_table.boot_values.uclk / 100;
344 		dpm_table->dpm_levels[0].enabled = true;
345 		dpm_table->min = dpm_table->dpm_levels[0].value;
346 		dpm_table->max = dpm_table->dpm_levels[0].value;
347 	}
348 
349 	/* fclk dpm table setup */
350 	dpm_table = &dpm_context->dpm_tables.fclk_table;
351 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_FCLK_BIT)) {
352 		ret = smu_v13_0_set_single_dpm_table(smu,
353 						     SMU_FCLK,
354 						     dpm_table);
355 		if (ret)
356 			return ret;
357 	} else {
358 		dpm_table->count = 1;
359 		dpm_table->dpm_levels[0].value = smu->smu_table.boot_values.fclk / 100;
360 		dpm_table->dpm_levels[0].enabled = true;
361 		dpm_table->min = dpm_table->dpm_levels[0].value;
362 		dpm_table->max = dpm_table->dpm_levels[0].value;
363 	}
364 
365 	return 0;
366 }
367 
368 static int aldebaran_check_powerplay_table(struct smu_context *smu)
369 {
370 	struct smu_table_context *table_context = &smu->smu_table;
371 	struct smu_13_0_powerplay_table *powerplay_table =
372 		table_context->power_play_table;
373 
374 	table_context->thermal_controller_type =
375 		powerplay_table->thermal_controller_type;
376 
377 	return 0;
378 }
379 
380 static int aldebaran_store_powerplay_table(struct smu_context *smu)
381 {
382 	struct smu_table_context *table_context = &smu->smu_table;
383 	struct smu_13_0_powerplay_table *powerplay_table =
384 		table_context->power_play_table;
385 	memcpy(table_context->driver_pptable, &powerplay_table->smc_pptable,
386 	       sizeof(PPTable_t));
387 
388 	return 0;
389 }
390 
391 static int aldebaran_append_powerplay_table(struct smu_context *smu)
392 {
393 	struct smu_table_context *table_context = &smu->smu_table;
394 	PPTable_t *smc_pptable = table_context->driver_pptable;
395 	struct atom_smc_dpm_info_v4_10 *smc_dpm_table;
396 	int index, ret;
397 
398 	index = get_index_into_master_table(atom_master_list_of_data_tables_v2_1,
399 					   smc_dpm_info);
400 
401 	ret = amdgpu_atombios_get_data_table(smu->adev, index, NULL, NULL, NULL,
402 				      (uint8_t **)&smc_dpm_table);
403 	if (ret)
404 		return ret;
405 
406 	dev_info(smu->adev->dev, "smc_dpm_info table revision(format.content): %d.%d\n",
407 			smc_dpm_table->table_header.format_revision,
408 			smc_dpm_table->table_header.content_revision);
409 
410 	if ((smc_dpm_table->table_header.format_revision == 4) &&
411 	    (smc_dpm_table->table_header.content_revision == 10))
412 		smu_memcpy_trailing(smc_pptable, GfxMaxCurrent, reserved,
413 				    smc_dpm_table, GfxMaxCurrent);
414 	return 0;
415 }
416 
417 static int aldebaran_setup_pptable(struct smu_context *smu)
418 {
419 	int ret = 0;
420 
421 	/* VBIOS pptable is the first choice */
422 	smu->smu_table.boot_values.pp_table_id = 0;
423 
424 	ret = smu_v13_0_setup_pptable(smu);
425 	if (ret)
426 		return ret;
427 
428 	ret = aldebaran_store_powerplay_table(smu);
429 	if (ret)
430 		return ret;
431 
432 	ret = aldebaran_append_powerplay_table(smu);
433 	if (ret)
434 		return ret;
435 
436 	ret = aldebaran_check_powerplay_table(smu);
437 	if (ret)
438 		return ret;
439 
440 	return ret;
441 }
442 
443 static bool aldebaran_is_primary(struct smu_context *smu)
444 {
445 	struct amdgpu_device *adev = smu->adev;
446 
447 	if (adev->smuio.funcs && adev->smuio.funcs->get_die_id)
448 		return adev->smuio.funcs->get_die_id(adev) == 0;
449 
450 	return true;
451 }
452 
453 static int aldebaran_run_board_btc(struct smu_context *smu)
454 {
455 	u32 smu_version;
456 	int ret;
457 
458 	if (!aldebaran_is_primary(smu))
459 		return 0;
460 
461 	ret = smu_cmn_get_smc_version(smu, NULL, &smu_version);
462 	if (ret) {
463 		dev_err(smu->adev->dev, "Failed to get smu version!\n");
464 		return ret;
465 	}
466 	if (smu_version <= 0x00441d00)
467 		return 0;
468 
469 	ret = smu_cmn_send_smc_msg(smu, SMU_MSG_BoardPowerCalibration, NULL);
470 	if (ret)
471 		dev_err(smu->adev->dev, "Board power calibration failed!\n");
472 
473 	return ret;
474 }
475 
476 static int aldebaran_run_btc(struct smu_context *smu)
477 {
478 	int ret;
479 
480 	ret = smu_cmn_send_smc_msg(smu, SMU_MSG_RunDcBtc, NULL);
481 	if (ret)
482 		dev_err(smu->adev->dev, "RunDcBtc failed!\n");
483 	else
484 		ret = aldebaran_run_board_btc(smu);
485 
486 	return ret;
487 }
488 
489 static int aldebaran_populate_umd_state_clk(struct smu_context *smu)
490 {
491 	struct smu_13_0_dpm_context *dpm_context =
492 		smu->smu_dpm.dpm_context;
493 	struct smu_13_0_dpm_table *gfx_table =
494 		&dpm_context->dpm_tables.gfx_table;
495 	struct smu_13_0_dpm_table *mem_table =
496 		&dpm_context->dpm_tables.uclk_table;
497 	struct smu_13_0_dpm_table *soc_table =
498 		&dpm_context->dpm_tables.soc_table;
499 	struct smu_umd_pstate_table *pstate_table =
500 		&smu->pstate_table;
501 
502 	pstate_table->gfxclk_pstate.min = gfx_table->min;
503 	pstate_table->gfxclk_pstate.peak = gfx_table->max;
504 	pstate_table->gfxclk_pstate.curr.min = gfx_table->min;
505 	pstate_table->gfxclk_pstate.curr.max = gfx_table->max;
506 
507 	pstate_table->uclk_pstate.min = mem_table->min;
508 	pstate_table->uclk_pstate.peak = mem_table->max;
509 	pstate_table->uclk_pstate.curr.min = mem_table->min;
510 	pstate_table->uclk_pstate.curr.max = mem_table->max;
511 
512 	pstate_table->socclk_pstate.min = soc_table->min;
513 	pstate_table->socclk_pstate.peak = soc_table->max;
514 	pstate_table->socclk_pstate.curr.min = soc_table->min;
515 	pstate_table->socclk_pstate.curr.max = soc_table->max;
516 
517 	if (gfx_table->count > ALDEBARAN_UMD_PSTATE_GFXCLK_LEVEL &&
518 	    mem_table->count > ALDEBARAN_UMD_PSTATE_MCLK_LEVEL &&
519 	    soc_table->count > ALDEBARAN_UMD_PSTATE_SOCCLK_LEVEL) {
520 		pstate_table->gfxclk_pstate.standard =
521 			gfx_table->dpm_levels[ALDEBARAN_UMD_PSTATE_GFXCLK_LEVEL].value;
522 		pstate_table->uclk_pstate.standard =
523 			mem_table->dpm_levels[ALDEBARAN_UMD_PSTATE_MCLK_LEVEL].value;
524 		pstate_table->socclk_pstate.standard =
525 			soc_table->dpm_levels[ALDEBARAN_UMD_PSTATE_SOCCLK_LEVEL].value;
526 	} else {
527 		pstate_table->gfxclk_pstate.standard =
528 			pstate_table->gfxclk_pstate.min;
529 		pstate_table->uclk_pstate.standard =
530 			pstate_table->uclk_pstate.min;
531 		pstate_table->socclk_pstate.standard =
532 			pstate_table->socclk_pstate.min;
533 	}
534 
535 	return 0;
536 }
537 
538 static int aldebaran_get_clk_table(struct smu_context *smu,
539 				   struct pp_clock_levels_with_latency *clocks,
540 				   struct smu_13_0_dpm_table *dpm_table)
541 {
542 	int i, count;
543 
544 	count = (dpm_table->count > MAX_NUM_CLOCKS) ? MAX_NUM_CLOCKS : dpm_table->count;
545 	clocks->num_levels = count;
546 
547 	for (i = 0; i < count; i++) {
548 		clocks->data[i].clocks_in_khz =
549 			dpm_table->dpm_levels[i].value * 1000;
550 		clocks->data[i].latency_in_us = 0;
551 	}
552 
553 	return 0;
554 }
555 
556 static int aldebaran_freqs_in_same_level(int32_t frequency1,
557 					 int32_t frequency2)
558 {
559 	return (abs(frequency1 - frequency2) <= EPSILON);
560 }
561 
562 static int aldebaran_get_smu_metrics_data(struct smu_context *smu,
563 					  MetricsMember_t member,
564 					  uint32_t *value)
565 {
566 	struct smu_table_context *smu_table= &smu->smu_table;
567 	SmuMetrics_t *metrics = (SmuMetrics_t *)smu_table->metrics_table;
568 	int ret = 0;
569 
570 	mutex_lock(&smu->metrics_lock);
571 
572 	ret = smu_cmn_get_metrics_table_locked(smu,
573 					       NULL,
574 					       false);
575 	if (ret) {
576 		mutex_unlock(&smu->metrics_lock);
577 		return ret;
578 	}
579 
580 	switch (member) {
581 	case METRICS_CURR_GFXCLK:
582 		*value = metrics->CurrClock[PPCLK_GFXCLK];
583 		break;
584 	case METRICS_CURR_SOCCLK:
585 		*value = metrics->CurrClock[PPCLK_SOCCLK];
586 		break;
587 	case METRICS_CURR_UCLK:
588 		*value = metrics->CurrClock[PPCLK_UCLK];
589 		break;
590 	case METRICS_CURR_VCLK:
591 		*value = metrics->CurrClock[PPCLK_VCLK];
592 		break;
593 	case METRICS_CURR_DCLK:
594 		*value = metrics->CurrClock[PPCLK_DCLK];
595 		break;
596 	case METRICS_CURR_FCLK:
597 		*value = metrics->CurrClock[PPCLK_FCLK];
598 		break;
599 	case METRICS_AVERAGE_GFXCLK:
600 		*value = metrics->AverageGfxclkFrequency;
601 		break;
602 	case METRICS_AVERAGE_SOCCLK:
603 		*value = metrics->AverageSocclkFrequency;
604 		break;
605 	case METRICS_AVERAGE_UCLK:
606 		*value = metrics->AverageUclkFrequency;
607 		break;
608 	case METRICS_AVERAGE_GFXACTIVITY:
609 		*value = metrics->AverageGfxActivity;
610 		break;
611 	case METRICS_AVERAGE_MEMACTIVITY:
612 		*value = metrics->AverageUclkActivity;
613 		break;
614 	case METRICS_AVERAGE_SOCKETPOWER:
615 		/* Valid power data is available only from primary die */
616 		*value = aldebaran_is_primary(smu) ?
617 				 metrics->AverageSocketPower << 8 :
618 				 0;
619 		break;
620 	case METRICS_TEMPERATURE_EDGE:
621 		*value = metrics->TemperatureEdge *
622 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
623 		break;
624 	case METRICS_TEMPERATURE_HOTSPOT:
625 		*value = metrics->TemperatureHotspot *
626 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
627 		break;
628 	case METRICS_TEMPERATURE_MEM:
629 		*value = metrics->TemperatureHBM *
630 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
631 		break;
632 	case METRICS_TEMPERATURE_VRGFX:
633 		*value = metrics->TemperatureVrGfx *
634 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
635 		break;
636 	case METRICS_TEMPERATURE_VRSOC:
637 		*value = metrics->TemperatureVrSoc *
638 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
639 		break;
640 	case METRICS_TEMPERATURE_VRMEM:
641 		*value = metrics->TemperatureVrMem *
642 			SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
643 		break;
644 	case METRICS_THROTTLER_STATUS:
645 		*value = metrics->ThrottlerStatus;
646 		break;
647 	default:
648 		*value = UINT_MAX;
649 		break;
650 	}
651 
652 	mutex_unlock(&smu->metrics_lock);
653 
654 	return ret;
655 }
656 
657 static int aldebaran_get_current_clk_freq_by_table(struct smu_context *smu,
658 						   enum smu_clk_type clk_type,
659 						   uint32_t *value)
660 {
661 	MetricsMember_t member_type;
662 	int clk_id = 0;
663 
664 	if (!value)
665 		return -EINVAL;
666 
667 	clk_id = smu_cmn_to_asic_specific_index(smu,
668 						CMN2ASIC_MAPPING_CLK,
669 						clk_type);
670 	if (clk_id < 0)
671 		return -EINVAL;
672 
673 	switch (clk_id) {
674 	case PPCLK_GFXCLK:
675 		/*
676 		 * CurrClock[clk_id] can provide accurate
677 		 *   output only when the dpm feature is enabled.
678 		 * We can use Average_* for dpm disabled case.
679 		 *   But this is available for gfxclk/uclk/socclk/vclk/dclk.
680 		 */
681 		if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_GFXCLK_BIT))
682 			member_type = METRICS_CURR_GFXCLK;
683 		else
684 			member_type = METRICS_AVERAGE_GFXCLK;
685 		break;
686 	case PPCLK_UCLK:
687 		if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_UCLK_BIT))
688 			member_type = METRICS_CURR_UCLK;
689 		else
690 			member_type = METRICS_AVERAGE_UCLK;
691 		break;
692 	case PPCLK_SOCCLK:
693 		if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT))
694 			member_type = METRICS_CURR_SOCCLK;
695 		else
696 			member_type = METRICS_AVERAGE_SOCCLK;
697 		break;
698 	case PPCLK_VCLK:
699 		if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_VCN_PG_BIT))
700 			member_type = METRICS_CURR_VCLK;
701 		else
702 			member_type = METRICS_AVERAGE_VCLK;
703 		break;
704 	case PPCLK_DCLK:
705 		if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_VCN_PG_BIT))
706 			member_type = METRICS_CURR_DCLK;
707 		else
708 			member_type = METRICS_AVERAGE_DCLK;
709 		break;
710 	case PPCLK_FCLK:
711 		member_type = METRICS_CURR_FCLK;
712 		break;
713 	default:
714 		return -EINVAL;
715 	}
716 
717 	return aldebaran_get_smu_metrics_data(smu,
718 					      member_type,
719 					      value);
720 }
721 
722 static int aldebaran_print_clk_levels(struct smu_context *smu,
723 				      enum smu_clk_type type, char *buf)
724 {
725 	int i, now, size = 0;
726 	int ret = 0;
727 	struct smu_umd_pstate_table *pstate_table = &smu->pstate_table;
728 	struct pp_clock_levels_with_latency clocks;
729 	struct smu_13_0_dpm_table *single_dpm_table;
730 	struct smu_dpm_context *smu_dpm = &smu->smu_dpm;
731 	struct smu_13_0_dpm_context *dpm_context = NULL;
732 	uint32_t display_levels;
733 	uint32_t freq_values[3] = {0};
734 	uint32_t min_clk, max_clk;
735 
736 	smu_cmn_get_sysfs_buf(&buf, &size);
737 
738 	if (amdgpu_ras_intr_triggered()) {
739 		size += sysfs_emit_at(buf, size, "unavailable\n");
740 		return size;
741 	}
742 
743 	dpm_context = smu_dpm->dpm_context;
744 
745 	switch (type) {
746 
747 	case SMU_OD_SCLK:
748 		size += sysfs_emit_at(buf, size, "%s:\n", "GFXCLK");
749 		fallthrough;
750 	case SMU_SCLK:
751 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_GFXCLK, &now);
752 		if (ret) {
753 			dev_err(smu->adev->dev, "Attempt to get current gfx clk Failed!");
754 			return ret;
755 		}
756 
757 		single_dpm_table = &(dpm_context->dpm_tables.gfx_table);
758 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
759 		if (ret) {
760 			dev_err(smu->adev->dev, "Attempt to get gfx clk levels Failed!");
761 			return ret;
762 		}
763 
764 		display_levels = clocks.num_levels;
765 
766 		min_clk = pstate_table->gfxclk_pstate.curr.min;
767 		max_clk = pstate_table->gfxclk_pstate.curr.max;
768 
769 		freq_values[0] = min_clk;
770 		freq_values[1] = max_clk;
771 
772 		/* fine-grained dpm has only 2 levels */
773 		if (now > min_clk && now < max_clk) {
774 			display_levels = clocks.num_levels + 1;
775 			freq_values[2] = max_clk;
776 			freq_values[1] = now;
777 		}
778 
779 		/*
780 		 * For DPM disabled case, there will be only one clock level.
781 		 * And it's safe to assume that is always the current clock.
782 		 */
783 		if (display_levels == clocks.num_levels) {
784 			for (i = 0; i < clocks.num_levels; i++)
785 				size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
786 					freq_values[i],
787 					(clocks.num_levels == 1) ?
788 						"*" :
789 						(aldebaran_freqs_in_same_level(
790 							 freq_values[i], now) ?
791 							 "*" :
792 							 ""));
793 		} else {
794 			for (i = 0; i < display_levels; i++)
795 				size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n", i,
796 						freq_values[i], i == 1 ? "*" : "");
797 		}
798 
799 		break;
800 
801 	case SMU_OD_MCLK:
802 		size += sysfs_emit_at(buf, size, "%s:\n", "MCLK");
803 		fallthrough;
804 	case SMU_MCLK:
805 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_UCLK, &now);
806 		if (ret) {
807 			dev_err(smu->adev->dev, "Attempt to get current mclk Failed!");
808 			return ret;
809 		}
810 
811 		single_dpm_table = &(dpm_context->dpm_tables.uclk_table);
812 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
813 		if (ret) {
814 			dev_err(smu->adev->dev, "Attempt to get memory clk levels Failed!");
815 			return ret;
816 		}
817 
818 		for (i = 0; i < clocks.num_levels; i++)
819 			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
820 					i, clocks.data[i].clocks_in_khz / 1000,
821 					(clocks.num_levels == 1) ? "*" :
822 					(aldebaran_freqs_in_same_level(
823 								       clocks.data[i].clocks_in_khz / 1000,
824 								       now) ? "*" : ""));
825 		break;
826 
827 	case SMU_SOCCLK:
828 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_SOCCLK, &now);
829 		if (ret) {
830 			dev_err(smu->adev->dev, "Attempt to get current socclk Failed!");
831 			return ret;
832 		}
833 
834 		single_dpm_table = &(dpm_context->dpm_tables.soc_table);
835 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
836 		if (ret) {
837 			dev_err(smu->adev->dev, "Attempt to get socclk levels Failed!");
838 			return ret;
839 		}
840 
841 		for (i = 0; i < clocks.num_levels; i++)
842 			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
843 					i, clocks.data[i].clocks_in_khz / 1000,
844 					(clocks.num_levels == 1) ? "*" :
845 					(aldebaran_freqs_in_same_level(
846 								       clocks.data[i].clocks_in_khz / 1000,
847 								       now) ? "*" : ""));
848 		break;
849 
850 	case SMU_FCLK:
851 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_FCLK, &now);
852 		if (ret) {
853 			dev_err(smu->adev->dev, "Attempt to get current fclk Failed!");
854 			return ret;
855 		}
856 
857 		single_dpm_table = &(dpm_context->dpm_tables.fclk_table);
858 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
859 		if (ret) {
860 			dev_err(smu->adev->dev, "Attempt to get fclk levels Failed!");
861 			return ret;
862 		}
863 
864 		for (i = 0; i < single_dpm_table->count; i++)
865 			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
866 					i, single_dpm_table->dpm_levels[i].value,
867 					(clocks.num_levels == 1) ? "*" :
868 					(aldebaran_freqs_in_same_level(
869 								       clocks.data[i].clocks_in_khz / 1000,
870 								       now) ? "*" : ""));
871 		break;
872 
873 	case SMU_VCLK:
874 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_VCLK, &now);
875 		if (ret) {
876 			dev_err(smu->adev->dev, "Attempt to get current vclk Failed!");
877 			return ret;
878 		}
879 
880 		single_dpm_table = &(dpm_context->dpm_tables.vclk_table);
881 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
882 		if (ret) {
883 			dev_err(smu->adev->dev, "Attempt to get vclk levels Failed!");
884 			return ret;
885 		}
886 
887 		for (i = 0; i < single_dpm_table->count; i++)
888 			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
889 					i, single_dpm_table->dpm_levels[i].value,
890 					(clocks.num_levels == 1) ? "*" :
891 					(aldebaran_freqs_in_same_level(
892 								       clocks.data[i].clocks_in_khz / 1000,
893 								       now) ? "*" : ""));
894 		break;
895 
896 	case SMU_DCLK:
897 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_DCLK, &now);
898 		if (ret) {
899 			dev_err(smu->adev->dev, "Attempt to get current dclk Failed!");
900 			return ret;
901 		}
902 
903 		single_dpm_table = &(dpm_context->dpm_tables.dclk_table);
904 		ret = aldebaran_get_clk_table(smu, &clocks, single_dpm_table);
905 		if (ret) {
906 			dev_err(smu->adev->dev, "Attempt to get dclk levels Failed!");
907 			return ret;
908 		}
909 
910 		for (i = 0; i < single_dpm_table->count; i++)
911 			size += sysfs_emit_at(buf, size, "%d: %uMhz %s\n",
912 					i, single_dpm_table->dpm_levels[i].value,
913 					(clocks.num_levels == 1) ? "*" :
914 					(aldebaran_freqs_in_same_level(
915 								       clocks.data[i].clocks_in_khz / 1000,
916 								       now) ? "*" : ""));
917 		break;
918 
919 	default:
920 		break;
921 	}
922 
923 	return size;
924 }
925 
926 static int aldebaran_upload_dpm_level(struct smu_context *smu,
927 				      bool max,
928 				      uint32_t feature_mask,
929 				      uint32_t level)
930 {
931 	struct smu_13_0_dpm_context *dpm_context =
932 		smu->smu_dpm.dpm_context;
933 	uint32_t freq;
934 	int ret = 0;
935 
936 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_GFXCLK_BIT) &&
937 	    (feature_mask & FEATURE_MASK(FEATURE_DPM_GFXCLK_BIT))) {
938 		freq = dpm_context->dpm_tables.gfx_table.dpm_levels[level].value;
939 		ret = smu_cmn_send_smc_msg_with_param(smu,
940 						      (max ? SMU_MSG_SetSoftMaxByFreq : SMU_MSG_SetSoftMinByFreq),
941 						      (PPCLK_GFXCLK << 16) | (freq & 0xffff),
942 						      NULL);
943 		if (ret) {
944 			dev_err(smu->adev->dev, "Failed to set soft %s gfxclk !\n",
945 				max ? "max" : "min");
946 			return ret;
947 		}
948 	}
949 
950 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_UCLK_BIT) &&
951 	    (feature_mask & FEATURE_MASK(FEATURE_DPM_UCLK_BIT))) {
952 		freq = dpm_context->dpm_tables.uclk_table.dpm_levels[level].value;
953 		ret = smu_cmn_send_smc_msg_with_param(smu,
954 						      (max ? SMU_MSG_SetSoftMaxByFreq : SMU_MSG_SetSoftMinByFreq),
955 						      (PPCLK_UCLK << 16) | (freq & 0xffff),
956 						      NULL);
957 		if (ret) {
958 			dev_err(smu->adev->dev, "Failed to set soft %s memclk !\n",
959 				max ? "max" : "min");
960 			return ret;
961 		}
962 	}
963 
964 	if (smu_cmn_feature_is_enabled(smu, SMU_FEATURE_DPM_SOCCLK_BIT) &&
965 	    (feature_mask & FEATURE_MASK(FEATURE_DPM_SOCCLK_BIT))) {
966 		freq = dpm_context->dpm_tables.soc_table.dpm_levels[level].value;
967 		ret = smu_cmn_send_smc_msg_with_param(smu,
968 						      (max ? SMU_MSG_SetSoftMaxByFreq : SMU_MSG_SetSoftMinByFreq),
969 						      (PPCLK_SOCCLK << 16) | (freq & 0xffff),
970 						      NULL);
971 		if (ret) {
972 			dev_err(smu->adev->dev, "Failed to set soft %s socclk !\n",
973 				max ? "max" : "min");
974 			return ret;
975 		}
976 	}
977 
978 	return ret;
979 }
980 
981 static int aldebaran_force_clk_levels(struct smu_context *smu,
982 				      enum smu_clk_type type, uint32_t mask)
983 {
984 	struct smu_13_0_dpm_context *dpm_context = smu->smu_dpm.dpm_context;
985 	struct smu_13_0_dpm_table *single_dpm_table = NULL;
986 	uint32_t soft_min_level, soft_max_level;
987 	int ret = 0;
988 
989 	soft_min_level = mask ? (ffs(mask) - 1) : 0;
990 	soft_max_level = mask ? (fls(mask) - 1) : 0;
991 
992 	switch (type) {
993 	case SMU_SCLK:
994 		single_dpm_table = &(dpm_context->dpm_tables.gfx_table);
995 		if (soft_max_level >= single_dpm_table->count) {
996 			dev_err(smu->adev->dev, "Clock level specified %d is over max allowed %d\n",
997 				soft_max_level, single_dpm_table->count - 1);
998 			ret = -EINVAL;
999 			break;
1000 		}
1001 
1002 		ret = aldebaran_upload_dpm_level(smu,
1003 						 false,
1004 						 FEATURE_MASK(FEATURE_DPM_GFXCLK_BIT),
1005 						 soft_min_level);
1006 		if (ret) {
1007 			dev_err(smu->adev->dev, "Failed to upload boot level to lowest!\n");
1008 			break;
1009 		}
1010 
1011 		ret = aldebaran_upload_dpm_level(smu,
1012 						 true,
1013 						 FEATURE_MASK(FEATURE_DPM_GFXCLK_BIT),
1014 						 soft_max_level);
1015 		if (ret)
1016 			dev_err(smu->adev->dev, "Failed to upload dpm max level to highest!\n");
1017 
1018 		break;
1019 
1020 	case SMU_MCLK:
1021 	case SMU_SOCCLK:
1022 	case SMU_FCLK:
1023 		/*
1024 		 * Should not arrive here since aldebaran does not
1025 		 * support mclk/socclk/fclk softmin/softmax settings
1026 		 */
1027 		ret = -EINVAL;
1028 		break;
1029 
1030 	default:
1031 		break;
1032 	}
1033 
1034 	return ret;
1035 }
1036 
1037 static int aldebaran_get_thermal_temperature_range(struct smu_context *smu,
1038 						   struct smu_temperature_range *range)
1039 {
1040 	struct smu_table_context *table_context = &smu->smu_table;
1041 	struct smu_13_0_powerplay_table *powerplay_table =
1042 		table_context->power_play_table;
1043 	PPTable_t *pptable = smu->smu_table.driver_pptable;
1044 
1045 	if (!range)
1046 		return -EINVAL;
1047 
1048 	memcpy(range, &smu13_thermal_policy[0], sizeof(struct smu_temperature_range));
1049 
1050 	range->hotspot_crit_max = pptable->ThotspotLimit *
1051 		SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
1052 	range->hotspot_emergency_max = (pptable->ThotspotLimit + CTF_OFFSET_HOTSPOT) *
1053 		SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
1054 	range->mem_crit_max = pptable->TmemLimit *
1055 		SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
1056 	range->mem_emergency_max = (pptable->TmemLimit + CTF_OFFSET_MEM)*
1057 		SMU_TEMPERATURE_UNITS_PER_CENTIGRADES;
1058 	range->software_shutdown_temp = powerplay_table->software_shutdown_temp;
1059 
1060 	return 0;
1061 }
1062 
1063 static int aldebaran_get_current_activity_percent(struct smu_context *smu,
1064 						  enum amd_pp_sensors sensor,
1065 						  uint32_t *value)
1066 {
1067 	int ret = 0;
1068 
1069 	if (!value)
1070 		return -EINVAL;
1071 
1072 	switch (sensor) {
1073 	case AMDGPU_PP_SENSOR_GPU_LOAD:
1074 		ret = aldebaran_get_smu_metrics_data(smu,
1075 						     METRICS_AVERAGE_GFXACTIVITY,
1076 						     value);
1077 		break;
1078 	case AMDGPU_PP_SENSOR_MEM_LOAD:
1079 		ret = aldebaran_get_smu_metrics_data(smu,
1080 						     METRICS_AVERAGE_MEMACTIVITY,
1081 						     value);
1082 		break;
1083 	default:
1084 		dev_err(smu->adev->dev, "Invalid sensor for retrieving clock activity\n");
1085 		return -EINVAL;
1086 	}
1087 
1088 	return ret;
1089 }
1090 
1091 static int aldebaran_get_gpu_power(struct smu_context *smu, uint32_t *value)
1092 {
1093 	if (!value)
1094 		return -EINVAL;
1095 
1096 	return aldebaran_get_smu_metrics_data(smu,
1097 					      METRICS_AVERAGE_SOCKETPOWER,
1098 					      value);
1099 }
1100 
1101 static int aldebaran_thermal_get_temperature(struct smu_context *smu,
1102 					     enum amd_pp_sensors sensor,
1103 					     uint32_t *value)
1104 {
1105 	int ret = 0;
1106 
1107 	if (!value)
1108 		return -EINVAL;
1109 
1110 	switch (sensor) {
1111 	case AMDGPU_PP_SENSOR_HOTSPOT_TEMP:
1112 		ret = aldebaran_get_smu_metrics_data(smu,
1113 						     METRICS_TEMPERATURE_HOTSPOT,
1114 						     value);
1115 		break;
1116 	case AMDGPU_PP_SENSOR_EDGE_TEMP:
1117 		ret = aldebaran_get_smu_metrics_data(smu,
1118 						     METRICS_TEMPERATURE_EDGE,
1119 						     value);
1120 		break;
1121 	case AMDGPU_PP_SENSOR_MEM_TEMP:
1122 		ret = aldebaran_get_smu_metrics_data(smu,
1123 						     METRICS_TEMPERATURE_MEM,
1124 						     value);
1125 		break;
1126 	default:
1127 		dev_err(smu->adev->dev, "Invalid sensor for retrieving temp\n");
1128 		return -EINVAL;
1129 	}
1130 
1131 	return ret;
1132 }
1133 
1134 static int aldebaran_read_sensor(struct smu_context *smu,
1135 				 enum amd_pp_sensors sensor,
1136 				 void *data, uint32_t *size)
1137 {
1138 	int ret = 0;
1139 
1140 	if (amdgpu_ras_intr_triggered())
1141 		return 0;
1142 
1143 	if (!data || !size)
1144 		return -EINVAL;
1145 
1146 	mutex_lock(&smu->sensor_lock);
1147 	switch (sensor) {
1148 	case AMDGPU_PP_SENSOR_MEM_LOAD:
1149 	case AMDGPU_PP_SENSOR_GPU_LOAD:
1150 		ret = aldebaran_get_current_activity_percent(smu,
1151 							     sensor,
1152 							     (uint32_t *)data);
1153 		*size = 4;
1154 		break;
1155 	case AMDGPU_PP_SENSOR_GPU_POWER:
1156 		ret = aldebaran_get_gpu_power(smu, (uint32_t *)data);
1157 		*size = 4;
1158 		break;
1159 	case AMDGPU_PP_SENSOR_HOTSPOT_TEMP:
1160 	case AMDGPU_PP_SENSOR_EDGE_TEMP:
1161 	case AMDGPU_PP_SENSOR_MEM_TEMP:
1162 		ret = aldebaran_thermal_get_temperature(smu, sensor,
1163 							(uint32_t *)data);
1164 		*size = 4;
1165 		break;
1166 	case AMDGPU_PP_SENSOR_GFX_MCLK:
1167 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_UCLK, (uint32_t *)data);
1168 		/* the output clock frequency in 10K unit */
1169 		*(uint32_t *)data *= 100;
1170 		*size = 4;
1171 		break;
1172 	case AMDGPU_PP_SENSOR_GFX_SCLK:
1173 		ret = aldebaran_get_current_clk_freq_by_table(smu, SMU_GFXCLK, (uint32_t *)data);
1174 		*(uint32_t *)data *= 100;
1175 		*size = 4;
1176 		break;
1177 	case AMDGPU_PP_SENSOR_VDDGFX:
1178 		ret = smu_v13_0_get_gfx_vdd(smu, (uint32_t *)data);
1179 		*size = 4;
1180 		break;
1181 	default:
1182 		ret = -EOPNOTSUPP;
1183 		break;
1184 	}
1185 	mutex_unlock(&smu->sensor_lock);
1186 
1187 	return ret;
1188 }
1189 
1190 static int aldebaran_get_power_limit(struct smu_context *smu,
1191 				     uint32_t *current_power_limit,
1192 				     uint32_t *default_power_limit,
1193 				     uint32_t *max_power_limit)
1194 {
1195 	PPTable_t *pptable = smu->smu_table.driver_pptable;
1196 	uint32_t power_limit = 0;
1197 	int ret;
1198 
1199 	if (!smu_cmn_feature_is_enabled(smu, SMU_FEATURE_PPT_BIT)) {
1200 		if (current_power_limit)
1201 			*current_power_limit = 0;
1202 		if (default_power_limit)
1203 			*default_power_limit = 0;
1204 		if (max_power_limit)
1205 			*max_power_limit = 0;
1206 
1207 		dev_warn(smu->adev->dev,
1208 			"PPT feature is not enabled, power values can't be fetched.");
1209 
1210 		return 0;
1211 	}
1212 
1213 	/* Valid power data is available only from primary die.
1214 	 * For secondary die show the value as 0.
1215 	 */
1216 	if (aldebaran_is_primary(smu)) {
1217 		ret = smu_cmn_send_smc_msg(smu, SMU_MSG_GetPptLimit,
1218 					   &power_limit);
1219 
1220 		if (ret) {
1221 			/* the last hope to figure out the ppt limit */
1222 			if (!pptable) {
1223 				dev_err(smu->adev->dev,
1224 					"Cannot get PPT limit due to pptable missing!");
1225 				return -EINVAL;
1226 			}
1227 			power_limit = pptable->PptLimit;
1228 		}
1229 	}
1230 
1231 	if (current_power_limit)
1232 		*current_power_limit = power_limit;
1233 	if (default_power_limit)
1234 		*default_power_limit = power_limit;
1235 
1236 	if (max_power_limit) {
1237 		if (pptable)
1238 			*max_power_limit = pptable->PptLimit;
1239 	}
1240 
1241 	return 0;
1242 }
1243 
1244 static int aldebaran_set_power_limit(struct smu_context *smu,
1245 				     enum smu_ppt_limit_type limit_type,
1246 				     uint32_t limit)
1247 {
1248 	/* Power limit can be set only through primary die */
1249 	if (aldebaran_is_primary(smu))
1250 		return smu_v13_0_set_power_limit(smu, limit_type, limit);
1251 
1252 	return -EINVAL;
1253 }
1254 
1255 static int aldebaran_system_features_control(struct  smu_context *smu, bool enable)
1256 {
1257 	int ret;
1258 
1259 	ret = smu_v13_0_system_features_control(smu, enable);
1260 	if (!ret && enable)
1261 		ret = aldebaran_run_btc(smu);
1262 
1263 	return ret;
1264 }
1265 
1266 static int aldebaran_set_performance_level(struct smu_context *smu,
1267 					   enum amd_dpm_forced_level level)
1268 {
1269 	struct smu_dpm_context *smu_dpm = &(smu->smu_dpm);
1270 	struct smu_13_0_dpm_context *dpm_context = smu_dpm->dpm_context;
1271 	struct smu_13_0_dpm_table *gfx_table =
1272 		&dpm_context->dpm_tables.gfx_table;
1273 	struct smu_umd_pstate_table *pstate_table = &smu->pstate_table;
1274 
1275 	/* Disable determinism if switching to another mode */
1276 	if ((smu_dpm->dpm_level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) &&
1277 	    (level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM)) {
1278 		smu_cmn_send_smc_msg(smu, SMU_MSG_DisableDeterminism, NULL);
1279 		pstate_table->gfxclk_pstate.curr.max = gfx_table->max;
1280 	}
1281 
1282 	switch (level) {
1283 
1284 	case AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM:
1285 		return 0;
1286 
1287 	case AMD_DPM_FORCED_LEVEL_HIGH:
1288 	case AMD_DPM_FORCED_LEVEL_LOW:
1289 	case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
1290 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
1291 	case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
1292 	case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
1293 	default:
1294 		break;
1295 	}
1296 
1297 	return smu_v13_0_set_performance_level(smu, level);
1298 }
1299 
1300 static int aldebaran_set_soft_freq_limited_range(struct smu_context *smu,
1301 					  enum smu_clk_type clk_type,
1302 					  uint32_t min,
1303 					  uint32_t max)
1304 {
1305 	struct smu_dpm_context *smu_dpm = &(smu->smu_dpm);
1306 	struct smu_13_0_dpm_context *dpm_context = smu_dpm->dpm_context;
1307 	struct smu_umd_pstate_table *pstate_table = &smu->pstate_table;
1308 	struct amdgpu_device *adev = smu->adev;
1309 	uint32_t min_clk;
1310 	uint32_t max_clk;
1311 	int ret = 0;
1312 
1313 	if (clk_type != SMU_GFXCLK && clk_type != SMU_SCLK)
1314 		return -EINVAL;
1315 
1316 	if ((smu_dpm->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1317 			&& (smu_dpm->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM))
1318 		return -EINVAL;
1319 
1320 	if (smu_dpm->dpm_level == AMD_DPM_FORCED_LEVEL_MANUAL) {
1321 		if (min >= max) {
1322 			dev_err(smu->adev->dev,
1323 				"Minimum GFX clk should be less than the maximum allowed clock\n");
1324 			return -EINVAL;
1325 		}
1326 
1327 		if ((min == pstate_table->gfxclk_pstate.curr.min) &&
1328 		    (max == pstate_table->gfxclk_pstate.curr.max))
1329 			return 0;
1330 
1331 		ret = smu_v13_0_set_soft_freq_limited_range(smu, SMU_GFXCLK,
1332 							    min, max);
1333 		if (!ret) {
1334 			pstate_table->gfxclk_pstate.curr.min = min;
1335 			pstate_table->gfxclk_pstate.curr.max = max;
1336 		}
1337 
1338 		return ret;
1339 	}
1340 
1341 	if (smu_dpm->dpm_level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) {
1342 		if (!max || (max < dpm_context->dpm_tables.gfx_table.min) ||
1343 			(max > dpm_context->dpm_tables.gfx_table.max)) {
1344 			dev_warn(adev->dev,
1345 					"Invalid max frequency %d MHz specified for determinism\n", max);
1346 			return -EINVAL;
1347 		}
1348 
1349 		/* Restore default min/max clocks and enable determinism */
1350 		min_clk = dpm_context->dpm_tables.gfx_table.min;
1351 		max_clk = dpm_context->dpm_tables.gfx_table.max;
1352 		ret = smu_v13_0_set_soft_freq_limited_range(smu, SMU_GFXCLK, min_clk, max_clk);
1353 		if (!ret) {
1354 			usleep_range(500, 1000);
1355 			ret = smu_cmn_send_smc_msg_with_param(smu,
1356 					SMU_MSG_EnableDeterminism,
1357 					max, NULL);
1358 			if (ret) {
1359 				dev_err(adev->dev,
1360 						"Failed to enable determinism at GFX clock %d MHz\n", max);
1361 			} else {
1362 				pstate_table->gfxclk_pstate.curr.min = min_clk;
1363 				pstate_table->gfxclk_pstate.curr.max = max;
1364 			}
1365 		}
1366 	}
1367 
1368 	return ret;
1369 }
1370 
1371 static int aldebaran_usr_edit_dpm_table(struct smu_context *smu, enum PP_OD_DPM_TABLE_COMMAND type,
1372 							long input[], uint32_t size)
1373 {
1374 	struct smu_dpm_context *smu_dpm = &(smu->smu_dpm);
1375 	struct smu_13_0_dpm_context *dpm_context = smu_dpm->dpm_context;
1376 	struct smu_umd_pstate_table *pstate_table = &smu->pstate_table;
1377 	uint32_t min_clk;
1378 	uint32_t max_clk;
1379 	int ret = 0;
1380 
1381 	/* Only allowed in manual or determinism mode */
1382 	if ((smu_dpm->dpm_level != AMD_DPM_FORCED_LEVEL_MANUAL)
1383 			&& (smu_dpm->dpm_level != AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM))
1384 		return -EINVAL;
1385 
1386 	switch (type) {
1387 	case PP_OD_EDIT_SCLK_VDDC_TABLE:
1388 		if (size != 2) {
1389 			dev_err(smu->adev->dev, "Input parameter number not correct\n");
1390 			return -EINVAL;
1391 		}
1392 
1393 		if (input[0] == 0) {
1394 			if (input[1] < dpm_context->dpm_tables.gfx_table.min) {
1395 				dev_warn(smu->adev->dev, "Minimum GFX clk (%ld) MHz specified is less than the minimum allowed (%d) MHz\n",
1396 					input[1], dpm_context->dpm_tables.gfx_table.min);
1397 				pstate_table->gfxclk_pstate.custom.min =
1398 					pstate_table->gfxclk_pstate.curr.min;
1399 				return -EINVAL;
1400 			}
1401 
1402 			pstate_table->gfxclk_pstate.custom.min = input[1];
1403 		} else if (input[0] == 1) {
1404 			if (input[1] > dpm_context->dpm_tables.gfx_table.max) {
1405 				dev_warn(smu->adev->dev, "Maximum GFX clk (%ld) MHz specified is greater than the maximum allowed (%d) MHz\n",
1406 					input[1], dpm_context->dpm_tables.gfx_table.max);
1407 				pstate_table->gfxclk_pstate.custom.max =
1408 					pstate_table->gfxclk_pstate.curr.max;
1409 				return -EINVAL;
1410 			}
1411 
1412 			pstate_table->gfxclk_pstate.custom.max = input[1];
1413 		} else {
1414 			return -EINVAL;
1415 		}
1416 		break;
1417 	case PP_OD_RESTORE_DEFAULT_TABLE:
1418 		if (size != 0) {
1419 			dev_err(smu->adev->dev, "Input parameter number not correct\n");
1420 			return -EINVAL;
1421 		} else {
1422 			/* Use the default frequencies for manual and determinism mode */
1423 			min_clk = dpm_context->dpm_tables.gfx_table.min;
1424 			max_clk = dpm_context->dpm_tables.gfx_table.max;
1425 
1426 			return aldebaran_set_soft_freq_limited_range(smu, SMU_GFXCLK, min_clk, max_clk);
1427 		}
1428 		break;
1429 	case PP_OD_COMMIT_DPM_TABLE:
1430 		if (size != 0) {
1431 			dev_err(smu->adev->dev, "Input parameter number not correct\n");
1432 			return -EINVAL;
1433 		} else {
1434 			if (!pstate_table->gfxclk_pstate.custom.min)
1435 				pstate_table->gfxclk_pstate.custom.min =
1436 					pstate_table->gfxclk_pstate.curr.min;
1437 
1438 			if (!pstate_table->gfxclk_pstate.custom.max)
1439 				pstate_table->gfxclk_pstate.custom.max =
1440 					pstate_table->gfxclk_pstate.curr.max;
1441 
1442 			min_clk = pstate_table->gfxclk_pstate.custom.min;
1443 			max_clk = pstate_table->gfxclk_pstate.custom.max;
1444 
1445 			return aldebaran_set_soft_freq_limited_range(smu, SMU_GFXCLK, min_clk, max_clk);
1446 		}
1447 		break;
1448 	default:
1449 		return -ENOSYS;
1450 	}
1451 
1452 	return ret;
1453 }
1454 
1455 static bool aldebaran_is_dpm_running(struct smu_context *smu)
1456 {
1457 	int ret;
1458 	uint32_t feature_mask[2];
1459 	unsigned long feature_enabled;
1460 
1461 	ret = smu_cmn_get_enabled_mask(smu, feature_mask, 2);
1462 	if (ret)
1463 		return false;
1464 	feature_enabled = (unsigned long)((uint64_t)feature_mask[0] |
1465 					  ((uint64_t)feature_mask[1] << 32));
1466 	return !!(feature_enabled & SMC_DPM_FEATURE);
1467 }
1468 
1469 static int aldebaran_i2c_xfer(struct i2c_adapter *i2c_adap,
1470 			      struct i2c_msg *msg, int num_msgs)
1471 {
1472 	struct amdgpu_device *adev = to_amdgpu_device(i2c_adap);
1473 	struct smu_table_context *smu_table = &adev->smu.smu_table;
1474 	struct smu_table *table = &smu_table->driver_table;
1475 	SwI2cRequest_t *req, *res = (SwI2cRequest_t *)table->cpu_addr;
1476 	int i, j, r, c;
1477 	u16 dir;
1478 
1479 	req = kzalloc(sizeof(*req), GFP_KERNEL);
1480 	if (!req)
1481 		return -ENOMEM;
1482 
1483 	req->I2CcontrollerPort = 0;
1484 	req->I2CSpeed = I2C_SPEED_FAST_400K;
1485 	req->SlaveAddress = msg[0].addr << 1; /* wants an 8-bit address */
1486 	dir = msg[0].flags & I2C_M_RD;
1487 
1488 	for (c = i = 0; i < num_msgs; i++) {
1489 		for (j = 0; j < msg[i].len; j++, c++) {
1490 			SwI2cCmd_t *cmd = &req->SwI2cCmds[c];
1491 
1492 			if (!(msg[i].flags & I2C_M_RD)) {
1493 				/* write */
1494 				cmd->CmdConfig |= CMDCONFIG_READWRITE_MASK;
1495 				cmd->ReadWriteData = msg[i].buf[j];
1496 			}
1497 
1498 			if ((dir ^ msg[i].flags) & I2C_M_RD) {
1499 				/* The direction changes.
1500 				 */
1501 				dir = msg[i].flags & I2C_M_RD;
1502 				cmd->CmdConfig |= CMDCONFIG_RESTART_MASK;
1503 			}
1504 
1505 			req->NumCmds++;
1506 
1507 			/*
1508 			 * Insert STOP if we are at the last byte of either last
1509 			 * message for the transaction or the client explicitly
1510 			 * requires a STOP at this particular message.
1511 			 */
1512 			if ((j == msg[i].len - 1) &&
1513 			    ((i == num_msgs - 1) || (msg[i].flags & I2C_M_STOP))) {
1514 				cmd->CmdConfig &= ~CMDCONFIG_RESTART_MASK;
1515 				cmd->CmdConfig |= CMDCONFIG_STOP_MASK;
1516 			}
1517 		}
1518 	}
1519 	mutex_lock(&adev->smu.mutex);
1520 	r = smu_cmn_update_table(&adev->smu, SMU_TABLE_I2C_COMMANDS, 0, req, true);
1521 	mutex_unlock(&adev->smu.mutex);
1522 	if (r)
1523 		goto fail;
1524 
1525 	for (c = i = 0; i < num_msgs; i++) {
1526 		if (!(msg[i].flags & I2C_M_RD)) {
1527 			c += msg[i].len;
1528 			continue;
1529 		}
1530 		for (j = 0; j < msg[i].len; j++, c++) {
1531 			SwI2cCmd_t *cmd = &res->SwI2cCmds[c];
1532 
1533 			msg[i].buf[j] = cmd->ReadWriteData;
1534 		}
1535 	}
1536 	r = num_msgs;
1537 fail:
1538 	kfree(req);
1539 	return r;
1540 }
1541 
1542 static u32 aldebaran_i2c_func(struct i2c_adapter *adap)
1543 {
1544 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1545 }
1546 
1547 
1548 static const struct i2c_algorithm aldebaran_i2c_algo = {
1549 	.master_xfer = aldebaran_i2c_xfer,
1550 	.functionality = aldebaran_i2c_func,
1551 };
1552 
1553 static const struct i2c_adapter_quirks aldebaran_i2c_control_quirks = {
1554 	.flags = I2C_AQ_COMB | I2C_AQ_COMB_SAME_ADDR | I2C_AQ_NO_ZERO_LEN,
1555 	.max_read_len  = MAX_SW_I2C_COMMANDS,
1556 	.max_write_len = MAX_SW_I2C_COMMANDS,
1557 	.max_comb_1st_msg_len = 2,
1558 	.max_comb_2nd_msg_len = MAX_SW_I2C_COMMANDS - 2,
1559 };
1560 
1561 static int aldebaran_i2c_control_init(struct smu_context *smu, struct i2c_adapter *control)
1562 {
1563 	struct amdgpu_device *adev = to_amdgpu_device(control);
1564 	int res;
1565 
1566 	control->owner = THIS_MODULE;
1567 	control->class = I2C_CLASS_SPD;
1568 	control->dev.parent = &adev->pdev->dev;
1569 	control->algo = &aldebaran_i2c_algo;
1570 	snprintf(control->name, sizeof(control->name), "AMDGPU SMU");
1571 	control->quirks = &aldebaran_i2c_control_quirks;
1572 
1573 	res = i2c_add_adapter(control);
1574 	if (res)
1575 		DRM_ERROR("Failed to register hw i2c, err: %d\n", res);
1576 
1577 	return res;
1578 }
1579 
1580 static void aldebaran_i2c_control_fini(struct smu_context *smu, struct i2c_adapter *control)
1581 {
1582 	i2c_del_adapter(control);
1583 }
1584 
1585 static void aldebaran_get_unique_id(struct smu_context *smu)
1586 {
1587 	struct amdgpu_device *adev = smu->adev;
1588 	SmuMetrics_t *metrics = smu->smu_table.metrics_table;
1589 	uint32_t upper32 = 0, lower32 = 0;
1590 	int ret;
1591 
1592 	mutex_lock(&smu->metrics_lock);
1593 	ret = smu_cmn_get_metrics_table_locked(smu, NULL, false);
1594 	if (ret)
1595 		goto out_unlock;
1596 
1597 	upper32 = metrics->PublicSerialNumUpper32;
1598 	lower32 = metrics->PublicSerialNumLower32;
1599 
1600 out_unlock:
1601 	mutex_unlock(&smu->metrics_lock);
1602 
1603 	adev->unique_id = ((uint64_t)upper32 << 32) | lower32;
1604 	sprintf(adev->serial, "%016llx", adev->unique_id);
1605 }
1606 
1607 static bool aldebaran_is_baco_supported(struct smu_context *smu)
1608 {
1609 	/* aldebaran is not support baco */
1610 
1611 	return false;
1612 }
1613 
1614 static int aldebaran_set_df_cstate(struct smu_context *smu,
1615 				   enum pp_df_cstate state)
1616 {
1617 	return smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_DFCstateControl, state, NULL);
1618 }
1619 
1620 static int aldebaran_allow_xgmi_power_down(struct smu_context *smu, bool en)
1621 {
1622 	return smu_cmn_send_smc_msg_with_param(smu,
1623 					       SMU_MSG_GmiPwrDnControl,
1624 					       en ? 1 : 0,
1625 					       NULL);
1626 }
1627 
1628 static const struct throttling_logging_label {
1629 	uint32_t feature_mask;
1630 	const char *label;
1631 } logging_label[] = {
1632 	{(1U << THROTTLER_TEMP_MEM_BIT), "HBM"},
1633 	{(1U << THROTTLER_TEMP_VR_GFX_BIT), "VR of GFX rail"},
1634 	{(1U << THROTTLER_TEMP_VR_MEM_BIT), "VR of HBM rail"},
1635 	{(1U << THROTTLER_TEMP_VR_SOC_BIT), "VR of SOC rail"},
1636 };
1637 static void aldebaran_log_thermal_throttling_event(struct smu_context *smu)
1638 {
1639 	int ret;
1640 	int throttler_idx, throtting_events = 0, buf_idx = 0;
1641 	struct amdgpu_device *adev = smu->adev;
1642 	uint32_t throttler_status;
1643 	char log_buf[256];
1644 
1645 	ret = aldebaran_get_smu_metrics_data(smu,
1646 					     METRICS_THROTTLER_STATUS,
1647 					     &throttler_status);
1648 	if (ret)
1649 		return;
1650 
1651 	memset(log_buf, 0, sizeof(log_buf));
1652 	for (throttler_idx = 0; throttler_idx < ARRAY_SIZE(logging_label);
1653 	     throttler_idx++) {
1654 		if (throttler_status & logging_label[throttler_idx].feature_mask) {
1655 			throtting_events++;
1656 			buf_idx += snprintf(log_buf + buf_idx,
1657 					    sizeof(log_buf) - buf_idx,
1658 					    "%s%s",
1659 					    throtting_events > 1 ? " and " : "",
1660 					    logging_label[throttler_idx].label);
1661 			if (buf_idx >= sizeof(log_buf)) {
1662 				dev_err(adev->dev, "buffer overflow!\n");
1663 				log_buf[sizeof(log_buf) - 1] = '\0';
1664 				break;
1665 			}
1666 		}
1667 	}
1668 
1669 	dev_warn(adev->dev, "WARN: GPU thermal throttling temperature reached, expect performance decrease. %s.\n",
1670 		 log_buf);
1671 	kgd2kfd_smi_event_throttle(smu->adev->kfd.dev,
1672 		smu_cmn_get_indep_throttler_status(throttler_status,
1673 						   aldebaran_throttler_map));
1674 }
1675 
1676 static int aldebaran_get_current_pcie_link_speed(struct smu_context *smu)
1677 {
1678 	struct amdgpu_device *adev = smu->adev;
1679 	uint32_t esm_ctrl;
1680 
1681 	/* TODO: confirm this on real target */
1682 	esm_ctrl = RREG32_PCIE(smnPCIE_ESM_CTRL);
1683 	if ((esm_ctrl >> 15) & 0x1FFFF)
1684 		return (((esm_ctrl >> 8) & 0x3F) + 128);
1685 
1686 	return smu_v13_0_get_current_pcie_link_speed(smu);
1687 }
1688 
1689 static ssize_t aldebaran_get_gpu_metrics(struct smu_context *smu,
1690 					 void **table)
1691 {
1692 	struct smu_table_context *smu_table = &smu->smu_table;
1693 	struct gpu_metrics_v1_3 *gpu_metrics =
1694 		(struct gpu_metrics_v1_3 *)smu_table->gpu_metrics_table;
1695 	SmuMetrics_t metrics;
1696 	int i, ret = 0;
1697 
1698 	ret = smu_cmn_get_metrics_table(smu,
1699 					&metrics,
1700 					true);
1701 	if (ret)
1702 		return ret;
1703 
1704 	smu_cmn_init_soft_gpu_metrics(gpu_metrics, 1, 3);
1705 
1706 	gpu_metrics->temperature_edge = metrics.TemperatureEdge;
1707 	gpu_metrics->temperature_hotspot = metrics.TemperatureHotspot;
1708 	gpu_metrics->temperature_mem = metrics.TemperatureHBM;
1709 	gpu_metrics->temperature_vrgfx = metrics.TemperatureVrGfx;
1710 	gpu_metrics->temperature_vrsoc = metrics.TemperatureVrSoc;
1711 	gpu_metrics->temperature_vrmem = metrics.TemperatureVrMem;
1712 
1713 	gpu_metrics->average_gfx_activity = metrics.AverageGfxActivity;
1714 	gpu_metrics->average_umc_activity = metrics.AverageUclkActivity;
1715 	gpu_metrics->average_mm_activity = 0;
1716 
1717 	/* Valid power data is available only from primary die */
1718 	if (aldebaran_is_primary(smu)) {
1719 		gpu_metrics->average_socket_power = metrics.AverageSocketPower;
1720 		gpu_metrics->energy_accumulator =
1721 			(uint64_t)metrics.EnergyAcc64bitHigh << 32 |
1722 			metrics.EnergyAcc64bitLow;
1723 	} else {
1724 		gpu_metrics->average_socket_power = 0;
1725 		gpu_metrics->energy_accumulator = 0;
1726 	}
1727 
1728 	gpu_metrics->average_gfxclk_frequency = metrics.AverageGfxclkFrequency;
1729 	gpu_metrics->average_socclk_frequency = metrics.AverageSocclkFrequency;
1730 	gpu_metrics->average_uclk_frequency = metrics.AverageUclkFrequency;
1731 	gpu_metrics->average_vclk0_frequency = 0;
1732 	gpu_metrics->average_dclk0_frequency = 0;
1733 
1734 	gpu_metrics->current_gfxclk = metrics.CurrClock[PPCLK_GFXCLK];
1735 	gpu_metrics->current_socclk = metrics.CurrClock[PPCLK_SOCCLK];
1736 	gpu_metrics->current_uclk = metrics.CurrClock[PPCLK_UCLK];
1737 	gpu_metrics->current_vclk0 = metrics.CurrClock[PPCLK_VCLK];
1738 	gpu_metrics->current_dclk0 = metrics.CurrClock[PPCLK_DCLK];
1739 
1740 	gpu_metrics->throttle_status = metrics.ThrottlerStatus;
1741 	gpu_metrics->indep_throttle_status =
1742 			smu_cmn_get_indep_throttler_status(metrics.ThrottlerStatus,
1743 							   aldebaran_throttler_map);
1744 
1745 	gpu_metrics->current_fan_speed = 0;
1746 
1747 	gpu_metrics->pcie_link_width =
1748 		smu_v13_0_get_current_pcie_link_width(smu);
1749 	gpu_metrics->pcie_link_speed =
1750 		aldebaran_get_current_pcie_link_speed(smu);
1751 
1752 	gpu_metrics->system_clock_counter = ktime_get_boottime_ns();
1753 
1754 	gpu_metrics->gfx_activity_acc = metrics.GfxBusyAcc;
1755 	gpu_metrics->mem_activity_acc = metrics.DramBusyAcc;
1756 
1757 	for (i = 0; i < NUM_HBM_INSTANCES; i++)
1758 		gpu_metrics->temperature_hbm[i] = metrics.TemperatureAllHBM[i];
1759 
1760 	gpu_metrics->firmware_timestamp = ((uint64_t)metrics.TimeStampHigh << 32) |
1761 					metrics.TimeStampLow;
1762 
1763 	*table = (void *)gpu_metrics;
1764 
1765 	return sizeof(struct gpu_metrics_v1_3);
1766 }
1767 
1768 static int aldebaran_mode2_reset(struct smu_context *smu)
1769 {
1770 	u32 smu_version;
1771 	int ret = 0, index;
1772 	struct amdgpu_device *adev = smu->adev;
1773 	int timeout = 10;
1774 
1775 	smu_cmn_get_smc_version(smu, NULL, &smu_version);
1776 
1777 	index = smu_cmn_to_asic_specific_index(smu, CMN2ASIC_MAPPING_MSG,
1778 						SMU_MSG_GfxDeviceDriverReset);
1779 
1780 	mutex_lock(&smu->message_lock);
1781 	if (smu_version >= 0x00441400) {
1782 		ret = smu_cmn_send_msg_without_waiting(smu, (uint16_t)index, SMU_RESET_MODE_2);
1783 		/* This is similar to FLR, wait till max FLR timeout */
1784 		msleep(100);
1785 		dev_dbg(smu->adev->dev, "restore config space...\n");
1786 		/* Restore the config space saved during init */
1787 		amdgpu_device_load_pci_state(adev->pdev);
1788 
1789 		dev_dbg(smu->adev->dev, "wait for reset ack\n");
1790 		while (ret == -ETIME && timeout)  {
1791 			ret = smu_cmn_wait_for_response(smu);
1792 			/* Wait a bit more time for getting ACK */
1793 			if (ret == -ETIME) {
1794 				--timeout;
1795 				usleep_range(500, 1000);
1796 				continue;
1797 			}
1798 
1799 			if (ret != 1) {
1800 				dev_err(adev->dev, "failed to send mode2 message \tparam: 0x%08x response %#x\n",
1801 						SMU_RESET_MODE_2, ret);
1802 				goto out;
1803 			}
1804 		}
1805 
1806 	} else {
1807 		dev_err(adev->dev, "smu fw 0x%x does not support MSG_GfxDeviceDriverReset MSG\n",
1808 				smu_version);
1809 	}
1810 
1811 	if (ret == 1)
1812 		ret = 0;
1813 out:
1814 	mutex_unlock(&smu->message_lock);
1815 
1816 	return ret;
1817 }
1818 
1819 static bool aldebaran_is_mode1_reset_supported(struct smu_context *smu)
1820 {
1821 #if 0
1822 	struct amdgpu_device *adev = smu->adev;
1823 	u32 smu_version;
1824 	uint32_t val;
1825 	/**
1826 	 * PM FW version support mode1 reset from 68.07
1827 	 */
1828 	smu_cmn_get_smc_version(smu, NULL, &smu_version);
1829 	if ((smu_version < 0x00440700))
1830 		return false;
1831 	/**
1832 	 * mode1 reset relies on PSP, so we should check if
1833 	 * PSP is alive.
1834 	 */
1835 	val = RREG32_SOC15(MP0, 0, regMP0_SMN_C2PMSG_81);
1836 
1837 	return val != 0x0;
1838 #endif
1839 	return true;
1840 }
1841 
1842 static bool aldebaran_is_mode2_reset_supported(struct smu_context *smu)
1843 {
1844 	return true;
1845 }
1846 
1847 static int aldebaran_set_mp1_state(struct smu_context *smu,
1848 				   enum pp_mp1_state mp1_state)
1849 {
1850 	switch (mp1_state) {
1851 	case PP_MP1_STATE_UNLOAD:
1852 		return smu_cmn_set_mp1_state(smu, mp1_state);
1853 	default:
1854 		return 0;
1855 	}
1856 }
1857 
1858 static int aldebaran_smu_send_hbm_bad_page_num(struct smu_context *smu,
1859 		uint32_t size)
1860 {
1861 	int ret = 0;
1862 
1863 	/* message SMU to update the bad page number on SMUBUS */
1864 	ret = smu_cmn_send_smc_msg_with_param(smu, SMU_MSG_SetNumBadHbmPagesRetired, size, NULL);
1865 	if (ret)
1866 		dev_err(smu->adev->dev, "[%s] failed to message SMU to update HBM bad pages number\n",
1867 				__func__);
1868 
1869 	return ret;
1870 }
1871 
1872 static const struct pptable_funcs aldebaran_ppt_funcs = {
1873 	/* init dpm */
1874 	.get_allowed_feature_mask = aldebaran_get_allowed_feature_mask,
1875 	/* dpm/clk tables */
1876 	.set_default_dpm_table = aldebaran_set_default_dpm_table,
1877 	.populate_umd_state_clk = aldebaran_populate_umd_state_clk,
1878 	.get_thermal_temperature_range = aldebaran_get_thermal_temperature_range,
1879 	.print_clk_levels = aldebaran_print_clk_levels,
1880 	.force_clk_levels = aldebaran_force_clk_levels,
1881 	.read_sensor = aldebaran_read_sensor,
1882 	.set_performance_level = aldebaran_set_performance_level,
1883 	.get_power_limit = aldebaran_get_power_limit,
1884 	.is_dpm_running = aldebaran_is_dpm_running,
1885 	.get_unique_id = aldebaran_get_unique_id,
1886 	.init_microcode = smu_v13_0_init_microcode,
1887 	.load_microcode = smu_v13_0_load_microcode,
1888 	.fini_microcode = smu_v13_0_fini_microcode,
1889 	.init_smc_tables = aldebaran_init_smc_tables,
1890 	.fini_smc_tables = smu_v13_0_fini_smc_tables,
1891 	.init_power = smu_v13_0_init_power,
1892 	.fini_power = smu_v13_0_fini_power,
1893 	.check_fw_status = smu_v13_0_check_fw_status,
1894 	/* pptable related */
1895 	.setup_pptable = aldebaran_setup_pptable,
1896 	.get_vbios_bootup_values = smu_v13_0_get_vbios_bootup_values,
1897 	.check_fw_version = smu_v13_0_check_fw_version,
1898 	.write_pptable = smu_cmn_write_pptable,
1899 	.set_driver_table_location = smu_v13_0_set_driver_table_location,
1900 	.set_tool_table_location = smu_v13_0_set_tool_table_location,
1901 	.notify_memory_pool_location = smu_v13_0_notify_memory_pool_location,
1902 	.system_features_control = aldebaran_system_features_control,
1903 	.send_smc_msg_with_param = smu_cmn_send_smc_msg_with_param,
1904 	.send_smc_msg = smu_cmn_send_smc_msg,
1905 	.get_enabled_mask = smu_cmn_get_enabled_mask,
1906 	.feature_is_enabled = smu_cmn_feature_is_enabled,
1907 	.disable_all_features_with_exception = smu_cmn_disable_all_features_with_exception,
1908 	.set_power_limit = aldebaran_set_power_limit,
1909 	.init_max_sustainable_clocks = smu_v13_0_init_max_sustainable_clocks,
1910 	.enable_thermal_alert = smu_v13_0_enable_thermal_alert,
1911 	.disable_thermal_alert = smu_v13_0_disable_thermal_alert,
1912 	.set_xgmi_pstate = smu_v13_0_set_xgmi_pstate,
1913 	.register_irq_handler = smu_v13_0_register_irq_handler,
1914 	.set_azalia_d3_pme = smu_v13_0_set_azalia_d3_pme,
1915 	.get_max_sustainable_clocks_by_dc = smu_v13_0_get_max_sustainable_clocks_by_dc,
1916 	.baco_is_support= aldebaran_is_baco_supported,
1917 	.get_dpm_ultimate_freq = smu_v13_0_get_dpm_ultimate_freq,
1918 	.set_soft_freq_limited_range = aldebaran_set_soft_freq_limited_range,
1919 	.od_edit_dpm_table = aldebaran_usr_edit_dpm_table,
1920 	.set_df_cstate = aldebaran_set_df_cstate,
1921 	.allow_xgmi_power_down = aldebaran_allow_xgmi_power_down,
1922 	.log_thermal_throttling_event = aldebaran_log_thermal_throttling_event,
1923 	.get_pp_feature_mask = smu_cmn_get_pp_feature_mask,
1924 	.set_pp_feature_mask = smu_cmn_set_pp_feature_mask,
1925 	.get_gpu_metrics = aldebaran_get_gpu_metrics,
1926 	.mode1_reset_is_support = aldebaran_is_mode1_reset_supported,
1927 	.mode2_reset_is_support = aldebaran_is_mode2_reset_supported,
1928 	.mode1_reset = smu_v13_0_mode1_reset,
1929 	.set_mp1_state = aldebaran_set_mp1_state,
1930 	.mode2_reset = aldebaran_mode2_reset,
1931 	.wait_for_event = smu_v13_0_wait_for_event,
1932 	.i2c_init = aldebaran_i2c_control_init,
1933 	.i2c_fini = aldebaran_i2c_control_fini,
1934 	.send_hbm_bad_pages_num = aldebaran_smu_send_hbm_bad_page_num,
1935 };
1936 
1937 void aldebaran_set_ppt_funcs(struct smu_context *smu)
1938 {
1939 	smu->ppt_funcs = &aldebaran_ppt_funcs;
1940 	smu->message_map = aldebaran_message_map;
1941 	smu->clock_map = aldebaran_clk_map;
1942 	smu->feature_map = aldebaran_feature_mask_map;
1943 	smu->table_map = aldebaran_table_map;
1944 }
1945